27 #include <boost/regex.hpp>
28 #include <boost/lexical_cast.hpp>
29 #include <boost/tokenizer.hpp>
33 #include "../common/WLogger.h"
34 #include "WGraphicsEngine.h"
35 #include "WGEViewer.h"
37 #include "WGEProjectFileIO.h"
58 double* parseDoubleSequence( std::string seq,
unsigned int size )
62 typedef boost::tokenizer<boost::char_separator< char > > tokenizer;
63 boost::char_separator< char > sep(
";" );
64 tokenizer tok( seq, sep );
67 double* values =
new double[ size ];
69 for( tokenizer::iterator it = tok.begin(); ( it != tok.end() ) && ( i < size ); ++it )
71 values[ i ] = boost::lexical_cast<
double >( ( *it ) );
86 double* parseMatrix( std::string matrix )
88 return parseDoubleSequence( matrix, 16 );
98 double* parseVector( std::string vec )
100 return parseDoubleSequence( vec, 3 );
106 static const boost::regex camRe(
"^ *CAMERA:([0-9]*):(.*)$" );
107 static const boost::regex matrixRe(
"^ *MANIPULATOR:\\(([0-9]*),Matrix\\)=(.*)$" );
108 static const boost::regex homeEyeRe(
"^ *MANIPULATOR:\\(([0-9]*),HomeEye\\)=(.*)$" );
109 static const boost::regex homeCenterRe(
"^ *MANIPULATOR:\\(([0-9]*),HomeCenter\\)=(.*)$" );
110 static const boost::regex homeUpRe(
"^ *MANIPULATOR:\\(([0-9]*),HomeUp\\)=(.*)$" );
113 boost::smatch matches;
114 if( boost::regex_match( line, matches, camRe ) )
119 wlog::debug(
"Project Loader [Parser]" ) <<
"Line " << lineNumber <<
": Camera \"" << matches[2] <<
"\" with ID " << matches[1];
122 m_cameras[ boost::lexical_cast<
unsigned int >( matches[1] ) ] = matches[2];
126 else if( boost::regex_match( line, matches, matrixRe ) )
131 wlog::debug(
"Project Loader [Parser]" ) <<
"Line " << lineNumber <<
": Camera Manipulator Matrix with ID " << matches[1];
135 osg::Matrixd( parseMatrix( boost::lexical_cast< std::string >( matches[2] ) ) );
139 else if( boost::regex_match( line, matches, homeEyeRe ) )
145 wlog::debug(
"Project Loader [Parser]" ) <<
"Line " << lineNumber <<
": Camera Manipulator Home Eye Point with ID " << matches[1];
148 double* vals = parseVector( boost::lexical_cast< std::string >( matches[2] ) );
149 m_homeEyeVectors[ boost::lexical_cast<
unsigned int >( matches[1] ) ] = osg::Vec3d( vals[0], vals[1], vals[2] );
153 else if( boost::regex_match( line, matches, homeCenterRe ) )
159 wlog::debug(
"Project Loader [Parser]" ) <<
"Line " << lineNumber <<
": Camera Manipulator Home Center Point with ID " << matches[1];
162 double* vals = parseVector( boost::lexical_cast< std::string >( matches[2] ) );
163 m_homeCenterVectors[ boost::lexical_cast<
unsigned int >( matches[1] ) ] = osg::Vec3d( vals[0], vals[1], vals[2] );
167 else if( boost::regex_match( line, matches, homeUpRe ) )
173 wlog::debug(
"Project Loader [Parser]" ) <<
"Line " << lineNumber <<
": Camera Manipulator Home Up Point with ID " << matches[1];
176 double* vals = parseVector( boost::lexical_cast< std::string >( matches[2] ) );
177 m_homeUpVectors[ boost::lexical_cast<
unsigned int >( matches[1] ) ] = osg::Vec3d( vals[0], vals[1], vals[2] );
188 for( CameraList::const_iterator iter =
m_cameras.begin(); iter !=
m_cameras.end(); ++iter )
194 wlog::warn(
"Project Loader" ) <<
"Project file contained a camera \"" << ( *iter ).second <<
"\" but the corresponding view does " <<
195 "not exist. Ignoring.";
201 wlog::warn(
"Project Loader" ) <<
"Project file contained a camera \"" << ( *iter ).second <<
"\" but no proper manipulator matrix. " <<
202 "Leaving current matrix untouched.";
215 wlog::warn(
"Project Loader" ) <<
"Project file contained a camera \"" << ( *iter ).second <<
"\" but no proper manipulator home " <<
216 "position. Leaving current home untouched.";
220 view->getCameraManipulator()->setHomePosition(
m_homeEyeVectors[ ( *iter ).first ],
230 output <<
"//////////////////////////////////////////////////////////////////////////////////////////////////////////////////" << std::endl <<
231 "// Camera definitions" << std::endl <<
232 "//////////////////////////////////////////////////////////////////////////////////////////////////////////////////" << std::endl <<
239 output <<
"CAMERA:0:" << name << std::endl;
245 osg::Matrixd view = mani->getMatrix();
247 output <<
"//Camera Matrices: \"" << name <<
"\"" << std::endl;
248 output <<
" MANIPULATOR:(0,Matrix)=";
249 for(
unsigned int i = 0; i < 16; ++i )
251 output << view.ptr()[i];
263 mani->getHomePosition( eye, center, up );
268 output <<
" MANIPULATOR:(0,HomeEye)=";
269 output << eye.x() <<
";" << eye.y() <<
";" << eye.z() << std::endl;
270 output <<
" MANIPULATOR:(0,HomeCenter)=";
271 output << center.x() <<
";" << center.y() <<
";" << center.z() << std::endl;
272 output <<
" MANIPULATOR:(0,HomeUp)=";
273 output << up.x() <<
";" << up.y() <<
";" << up.z() << std::endl;
275 output <<
"//Camera Matrices END: \"" << name <<
"\"" << std::endl;