arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

Manip.cpp

Go to the documentation of this file.
00001 ///////////////////////////////////////////////////////////////////////////
00002 //              _____________  ______________________    __   __  _____
00003 //             /  ________  |  |   ___   ________   /   |  \ /  \   |
00004 //            |  |       |  |_ |  |_ |  |       /  /    \__  |      |
00005 //            |  |  ___  |  || |  || |  |      /  /        | |      |
00006 //            |  | |   \ |  || |  || |  |     /  /      \__/ \__/ __|__
00007 //            |  | |_@  ||  || |  || |  |    /  /          Institute
00008 //            |  |___/  ||  ||_|  || |  |   /  /_____________________
00009 //             \_______/  \______/ | |__|  /___________________________
00010 //                        |  |__|  |
00011 //                         \______/
00012 //                    University of Utah       
00013 //                           2002
00014 ///////////////////////////////////////////////////////////////////////////
00015 
00016 //manip.cpp
00017 //Joe Kniss
00018 
00019 #pragma warning(disable:4786) //dissable stl identifier too long warnings
00020 
00021 #include <graphicsGutz.h>
00022 #include <iostream>
00023 #include <math.h>
00024 #include "Manip.h"
00025 #include "ManipEvents.h"
00026 #include <serialize/SerializeUtil.h>
00027 #include "../eventGutz/mouseEvent.h"
00028 
00029 #ifndef M_PI
00030 #define M_PI 3.141592654f
00031 #endif
00032 
00033 #ifdef KONG
00034 #include <KokoBuffer.h>
00035 #endif
00036 
00037 using namespace gutz;
00038 using namespace std;
00039 
00040 const float defaultRad = .8f;
00041 
00042 ////////////////////////////////////////////////////////////
00043 /// Constructor
00044 
00045 Manip::Manip(Camera *cam)
00046 : BaseManip(),
00047   ManipEventMap(),
00048   _objtrackquat(quatf_id),
00049   _currquat(quatf_id),
00050   _om(mat4f_id),
00051   _oinv(mat4f_id),
00052   _parent(0),
00053   _rad(defaultRad),
00054   _userRad(0),
00055   _center(vec3f_zero),
00056   _baked(mat4f_id)
00057 {
00058 }
00059 
00060 Manip::Manip(const Manip &m)
00061 : BaseManip(m),
00062   ManipEventMap(m),
00063   _objtrackquat(m._objtrackquat),
00064   _currquat(m._currquat),
00065   _om(m._om),
00066   _oinv(m._oinv),
00067   _parent(m._parent),
00068   _rad(m._rad),
00069   _userRad(m._userRad),
00070   _center(m._center),
00071   _baked(m._baked)
00072 {
00073    
00074 }
00075 
00076 Manip &Manip::operator=(const Manip &m)
00077 {
00078    BaseManip::operator =(m);
00079    ManipEventMap::operator =(m);
00080    _objtrackquat = m._objtrackquat;
00081    _currquat = m._currquat;
00082    _om = m._om;
00083    _oinv = m._oinv;
00084    _parent = m._parent;
00085    _rad = m._rad;
00086    _userRad = m._userRad;
00087    _center = m._center;
00088    _baked = m._baked;
00089    return *this;
00090 }
00091 
00092 //=========================================================================
00093 //  (Mouse Event) Get Radius
00094 //=========================================================================
00095 float   Manip::getRad(const gutz::MouseEvent &me)
00096 {
00097    ////////////////////////////////////////////
00098    /// determine radius for trackball rotation
00099    if(_userRad)/// user specifed a radius
00100    {     
00101       _rad = _userRad;
00102    }  
00103    else
00104    {
00105       if( me.getPos().z)
00106       {
00107          _rad = ( getLocalPosScreen(me.getPos()) - _center ).norm() * 2.0f;
00108       }
00109       else
00110       {
00111          return defaultRad;
00112       }
00113    }
00114    /// figure out the correct radius for rotation
00115    vec3f cscreen = getScreenPosLocal(_center); /// project center to screen
00116    vec3f crad = cscreen + vec3f_x;             /// add one increment in x direction
00117    crad = getLocalPosScreen(crad);             /// project back to local space
00118    crad -= _center;                            /// get local x vector
00119    crad.normalize();                           /// normalize that
00120    crad *= _rad;                               /// multiply by the radius
00121    crad = getScreenPosLocal(crad+_center);     /// project that point to the screen
00122    crad -= cscreen;                            /// compute the difference in screen space
00123    /// rad is the norm of this vector
00124    _rad = sqrt( crad.x * crad.x + crad.y * crad.y )/getScreenX(); 
00125    cerr << "rad = " << _rad << endl;
00126    ////////////////////////////////////////////
00127 
00128    return _rad;
00129 }
00130 
00131 
00132 //=========================================================================
00133 //  Set/Get Matrix-components
00134 //=========================================================================
00135 void   Manip::setCenter(const vec3f &center)
00136 {
00137    if(_center == center) return;
00138    setMatrix( _om );
00139    _center = center;
00140 }
00141 
00142 void Manip::setLocalQuat(const quatf &lq)
00143 {
00144    setMatrix( mat4f( mat3f(lq), _om.trans() ) );
00145 }
00146 
00147 void Manip::setLocalPos(const vec3f &pos)
00148 {
00149    if( pos == getLocalPos() ) return;
00150    _baked.trans( _baked.trans() + pos - getLocalPos() );
00151    updateMatrix();
00152 }
00153 
00154 mat4f Manip::buildMatrix() const         
00155 {
00156    return _baked * mat4f(mat3f_id, _center)
00157           * mat4f( mat3f( _currquat ), vec3f_zero ) * mat4f( mat3f_id, -_center );
00158 }
00159 
00160 void  Manip::setMatrix(const gutz::mat4f &m)
00161 {
00162    _baked = m;
00163    _currquat = quatf_id;
00164    _om = m;
00165    _oinv = _om.inv();
00166    //_currquat = _om.rot();
00167 }
00168 
00169 void Manip::updateMatrix()
00170 {
00171    _om   = buildMatrix();
00172    _oinv = _om.inv();
00173 }
00174 
00175 //=========================================================================
00176 //  Set Parent
00177 //=========================================================================
00178 void Manip::setParent(const BaseManipSP parent)
00179 {
00180    _parent = parent;
00181 }
00182 
00183 //=========================================================================
00184 //  Insert Parent
00185 //=========================================================================
00186 void Manip::insertParent(const BaseManipSP parent)
00187 {
00188    /// can't insert a "0" parent, use setParent()
00189    if( !parent ) return; 
00190 
00191    if( _parent ) /// we currently have a parent
00192    {
00193       if( parent->isCamera() )  /// if our new parent is a camera
00194       {
00195          if( _parent->isCamera() ) /// and our current parent is a camera 
00196          {
00197             _parent = parent;  /// exchange old camera for new one
00198             return;
00199          }  
00200          else  // current parent isn't a camera, so send new camera/parent up the chain
00201          {
00202             _parent->insertParent( parent );
00203             return;
00204          }
00205       }
00206       /// new parent isn't a camera, just insert new parent between current and us
00207       parent->setParent(_parent);
00208       _parent = parent;
00209       return;
00210    }
00211    _parent = parent;
00212 }
00213 
00214 //=========================================================================
00215 //  get camera
00216 //=========================================================================
00217 Camera *Manip::getCamera() const
00218 {
00219    if(_parent)
00220    {
00221       if(_parent->isCamera())
00222       {
00223          return _parent.cast<Camera>();
00224       }
00225       else
00226       {
00227          return _parent->getCamera();
00228       }
00229    }
00230    return CameraSP(0);
00231 
00232 }
00233 
00234 ////////////////////////////////////////////////////////////
00235 /// Serialization.
00236 /// write.
00237 //=========================================================================
00238 //  Uber Serialize
00239 //=========================================================================
00240 const string DATA_STR("Data{");
00241 const string END_SECTION_STR("\n}\n");
00242 
00243 bool Manip::serialize(std::ostream &os, const std::string indent)
00244 {
00245    string ind = indent;
00246    ind += "   ";
00247 
00248    /// serial data
00249    os << indent << DATA_STR << " \n";
00250    serialData(os, ind);
00251    os << indent << END_SECTION_STR;
00252 
00253    bool status = false;
00254 
00255    return status;
00256 }
00257 
00258 /// read.
00259 //=========================================================================
00260 //  Uber UnSerialize
00261 //=========================================================================
00262 bool Manip::unserialize(std::istream &is)
00263 {
00264    string tstr;
00265 
00266    bool status = false;
00267 
00268    /// unserial data
00269    is >> tstr;
00270    if(unserialKeyCheck(DATA_STR, tstr, "baseManip, data")) return true;   
00271    if(unserialData(is)) return true;
00272    is >> tstr;
00273    if(unserialKeyCheck(END_SECTION_STR,tstr,"baseManip, data")) return true;
00274 
00275    status = status || is.eof() || is.fail();
00276    return status;
00277 }
00278 
00279 /// serialize the data parts of this guy for momentos & transmission
00280 //=========================================================================
00281 //  Serialize Data
00282 //=========================================================================
00283 const string MAT_STR("Matrix: \n");
00284 
00285 bool Manip::serialData(std::ostream &os, const std::string indent)
00286 {
00287    BaseManip::serialData(os,indent);
00288 
00289    os << indent << MAT_STR << _om;
00290 
00291    return false;
00292 }
00293 
00294 //=========================================================================
00295 //  UnSerialize Data
00296 //=========================================================================
00297 bool Manip::unserialData(std::istream &is)
00298 {
00299    BaseManip::unserialData(is);
00300 
00301    string tstr;
00302    is >> tstr;
00303    if(unserialKeyCheck(MAT_STR, tstr, "Manip, matrix")) return true;
00304    is >> _om;
00305 
00306    _currquat = _om.rot();
00307 
00308    return is.eof() || is.fail();
00309 }
00310 
00311 //=========================================================================
00312 //  Mouse/move/tumble
00313 //=========================================================================
00314 bool Manip::mouse(const gutz::MouseEvent &me)
00315 {
00316    setCamera(me.getCamera());
00317    return ManipEventMap::mouse(this, me);
00318 }
00319 
00320 bool Manip::move(const gutz::MouseMoveEvent &mme)
00321 {
00322    return ManipEventMap::move(this, mme);
00323 }
00324 
00325 void Manip::tumble(float speed)
00326 {
00327    ManipEventMap::tumble(this);
00328 }
00329 
00330 //=========================================================================
00331 //  Kong
00332 //=========================================================================
00333 #ifdef KONG
00334 // manip pack and unpack methods =====================================
00335 
00336 // ----------------
00337 // --- packSize ---
00338 // ----------------
00339 //
00340 unsigned int Manip::packSize(void)
00341 {
00342   // How big of a buffer do we need to make a packed version
00343   // of the camera?
00344   return( sizeof(mat4f) + sizeof(vec3f) );
00345 }
00346 
00347 
00348 // ------------
00349 // --- pack ---
00350 // ------------
00351 //
00352 void Manip::pack(KokoBuffer &kbuf, bool reset)
00353 {
00354   if (reset) 
00355     kbuf.Pack();  // Put the buffer in pack mode. 
00356   kbuf.Pack((unsigned char *)&_om,  sizeof(mat4f));      
00357   kbuf.Pack((unsigned char *)&_center,  sizeof(vec3f));      
00358 }
00359 
00360 
00361 // --------------
00362 // --- unpack ---
00363 // --------------
00364 //
00365 void Manip::unpack(KokoBuffer &kbuf, bool reset) 
00366 {
00367   if (reset)
00368     kbuf.Unpack();  // Put the buffer in extraction mode.
00369   kbuf.Unpack((unsigned char *)&_om,  sizeof(mat4f));    
00370   kbuf.Unpack((unsigned char *)&_center,  sizeof(vec3f));    
00371   setMatrix( _om );
00372 }
00373 
00374 #endif
00375 //=========================================================================
00376 //  end Kong
00377 //=========================================================================
00378 
00379 
00380 

Send questions, comments, and bug reports to:
jmk