basic/kinematics/FrameManager.cpp

Go to the documentation of this file.
00001 //## begin module%3717CCD90370.cm preserve=no
00002 //        %X% %Q% %Z% %W%
00003 //## end module%3717CCD90370.cm
00004 
00005 //## begin module%3717CCD90370.cp preserve=no
00006 //## end module%3717CCD90370.cp
00007 
00008 //## Module: FrameManager%3717CCD90370; Pseudo Package body
00009 //## Source file: E:\mpk\code\Kinematics\FrameManager.cpp
00010 
00011 //## begin module%3717CCD90370.additionalIncludes preserve=no
00012 //## end module%3717CCD90370.additionalIncludes
00013 
00014 //## begin module%3717CCD90370.includes preserve=yes
00015 #pragma warning( disable : 4786 )
00016 #include <set>
00017 #include <list>
00018 #include <algorithm>
00019 #include <assert.h>
00020 //## end module%3717CCD90370.includes
00021 
00022 // Matrix4x4
00023 #include <math\Matrix4x4.h>
00024 // Frame
00025 #include "geometry\Frame.h"
00026 // FrameManager
00027 #include "Kinematics\FrameManager.h"
00028 //## begin module%3717CCD90370.additionalDeclarations preserve=yes
00029 //## end module%3717CCD90370.additionalDeclarations
00030 
00031 
00032 // Class FrameManager 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 FrameManager::FrameManager ()
00042   //## begin FrameManager::FrameManager%924391854.hasinit preserve=no
00043   //## end FrameManager::FrameManager%924391854.hasinit
00044   //## begin FrameManager::FrameManager%924391854.initialization preserve=yes
00045   //## end FrameManager::FrameManager%924391854.initialization
00046 {
00047   //## begin FrameManager::FrameManager%924391854.body preserve=yes
00048         assert( allFrames.size() == allWorldFrames.size() ) ;
00049         //the 0 frame is set to the identity matrix
00050         Frame identity ;
00051         allFrames.push_back( identity ) ;
00052         allWorldFrames.push_back( identity ) ;
00053         worldFrameValid.push_back( true ) ;
00054         assert( allFrames.size() == allWorldFrames.size() ) ;
00055   //## end FrameManager::FrameManager%924391854.body
00056 }
00057 
00058 FrameManager::FrameManager (const FrameManager& right)
00059   //## begin FrameManager::FrameManager%924978291.hasinit preserve=no
00060   //## end FrameManager::FrameManager%924978291.hasinit
00061   //## begin FrameManager::FrameManager%924978291.initialization preserve=yes
00062   : allFrames( right.allFrames ),
00063   allWorldFrames( right.allWorldFrames ),
00064   worldFrameValid( right.worldFrameValid )
00065   //## end FrameManager::FrameManager%924978291.initialization
00066 {
00067   //## begin FrameManager::FrameManager%924978291.body preserve=yes
00068         assert( allFrames.size() == allWorldFrames.size() ) ;
00069         Clear() ;
00070 
00071         //copy all the values in the frame manager 
00072         for( int i = 0 ; i < right.allFrames.size() ; i++ )
00073         {
00074                 allFrames.push_back( right.allFrames[ i ] ) ;
00075                 allWorldFrames.push_back( right.allWorldFrames[ i ] ) ;
00076                 worldFrameValid.push_back( right.worldFrameValid[ i ] ) ;
00077         }
00078         assert( allFrames.size() == allWorldFrames.size() ) ;
00079   //## end FrameManager::FrameManager%924978291.body
00080 }
00081 
00082 
00083 FrameManager::~FrameManager()
00084 {
00085   //## begin FrameManager::~FrameManager%.body preserve=yes
00086         assert( allFrames.size() == allWorldFrames.size() ) ;
00087   //## end FrameManager::~FrameManager%.body
00088 }
00089 
00090 
00091 
00092 //## Other Operations (implementation)
00093 int FrameManager::AddFrame ()
00094 {
00095         //ensure that the sets of frames are the same size
00096         int allFramesSize = allFrames.size() ;
00097         int allWorldFramesSize = allWorldFrames.size() ;
00098         assert( allFramesSize == allWorldFramesSize ) ;
00099 
00100         Frame blank ;
00101         allFrames.push_back( blank ) ;
00102         allWorldFrames.push_back( blank ) ;
00103         worldFrameValid.push_back( false ) ;    //frame does not initally start valid
00104         int afs = allFrames.size() ;
00105         int awfs= allWorldFrames.size() ;
00106         int wfv = worldFrameValid.size() ;
00107         assert( allFrames.size() == allWorldFrames.size() ) ;
00108         int returnMe = allFrames.size() - 1;
00109         return( returnMe ) ;
00110   //## end FrameManager::AddFrame%924306824.body
00111 }
00112 
00113 unsigned int FrameManager::GetNumberOfFrames () const
00114 {
00115   //## begin FrameManager::GetNumberOfFrames%924391848.body preserve=yes
00116         return allFrames.size() ;
00117   //## end FrameManager::GetNumberOfFrames%924391848.body
00118 }
00119 
00120 //=============================================================================
00121 // GetToolFrame
00122 //
00123 // Purpose: allows access to the tool frame
00124 //=============================================================================
00125 Matrix4x4 FrameManager::GetToolFrame( const int index ) const
00126 {
00127         assert( false ); //this is never going to work
00128         assert( index == 0 );
00129         int numberOfFrames = GetNumberOfFrames();
00130         Matrix4x4 returnme = GetWorldFrame( numberOfFrames - 1 );
00131         return returnme;
00132 }
00133 
00134 Frame FrameManager::operator [] (const unsigned int index) const
00135 {
00136   //## begin FrameManager::operator[]%924391849.body preserve=yes
00137         assert( false ) ;
00138         return( allFrames[ index ] ) ;
00139   //## end FrameManager::operator[]%924391849.body
00140 }
00141 
00142 bool FrameManager::ValidateFrame (const unsigned int frame) const
00143 {
00144   //## begin FrameManager::ValidateFrame%924391855.body preserve=yes
00145         // Detached code regions:
00146         //check all the frames beneath this one to see if any of them are the root
00147         std::set< unsigned int > frames ;
00148         frames.insert( frame ) ;
00149 
00150         unsigned int lastChecked = frame ;
00151         while( lastChecked != 0 )
00152         {
00153                 if( frames.find( lastChecked ) != frames.end() ) 
00154                 {
00155                         assert( false ) ;
00156                         return false ;
00157                 }
00158                 frames.insert( lastChecked ) ;
00159                 lastChecked = GetFrame(lastChecked).BaseFrameNum() ;
00160         }
00161         return true ;
00162   //## end FrameManager::ValidateFrame%924391855.body
00163 }
00164 
00165 Frame* FrameManager::GetFrameRef (const unsigned int number)
00166 {
00167   //## begin FrameManager::GetFrameRef%925158794.body preserve=yes
00168         int allFramesSize = allFrames.size() ;
00169         assert( number < allFramesSize ) ;
00170         assert( number >= 0 ) ;
00171         return( &allFrames[ number ] ) ;
00172   //## end FrameManager::GetFrameRef%925158794.body
00173 }
00174 
00175 FrameManager& FrameManager::operator = (const FrameManager& right)
00176 {
00177   //## begin FrameManager::operator=%925235860.body preserve=yes
00178         assert( allFrames.size() == allWorldFrames.size() ) ;
00179         if( &right == this )
00180         { 
00181                 return *this ;
00182         }
00183         
00184         //copy all the frames ;
00185         allFrames.clear() ;
00186         allWorldFrames.clear() ;
00187         worldFrameValid.clear() ;
00188         for( int i = 0 ; i < right.allFrames.size() ; i++ )
00189         {
00190                 allFrames.push_back( right.allFrames[ i ] ) ;
00191                 allWorldFrames.push_back( right.allWorldFrames[ i ] ) ;
00192                 worldFrameValid.push_back( right.worldFrameValid[ i ] ) ;
00193         }
00194         assert( allFrames.size() == allWorldFrames.size() ) ;
00195         return *this ;
00196   //## end FrameManager::operator=%925235860.body
00197 }
00198 
00199 Matrix4x4 FrameManager::GetTransformRelative (const unsigned int frame, const unsigned int base) const
00200 {
00201   //## begin FrameManager::GetTransformRelative%925324832.body preserve=yes
00202         assert( allFrames.size() == allWorldFrames.size() ) ;
00203         const Matrix4x4& mf = GetWorldFrame( frame ) ;
00204         if( base == 0 )
00205         {
00206                 return mf ;
00207         }
00208         else
00209         {
00210                 Matrix4x4 mb = GetWorldFrame( base ) ;  
00211                 return mf * mb.Inverse();               //IMPROVE: order of multiplication
00212         }
00213         
00214 /*      
00215         //find a common base frame to both these frames
00216         unsigned int commonBase = 0 ;
00217         std::list< unsigned int > frameHistory ;
00218         unsigned int addme = frame ;
00219         do
00220         {
00221                 frameHistory.insert( frameHistory.begin(), addme ) ;
00222                 addme = BaseFrame( addme ) ;
00223         } while( addme != 0 ) ;
00224         frameHistory.insert( frameHistory.begin(), 0 ) ;
00225         
00226         //check the base frame, and all its parents for commonality
00227         std::list< unsigned int > baseFrameList ;
00228         unsigned int root = base ;
00229         while( std::find( frameHistory.begin(), frameHistory.end(), root ) == frameHistory.end() ) 
00230         {
00231                 baseFrameList.insert( baseFrameList.begin(), root ) ;
00232                 root = BaseFrame( root ) ;      //IMPROVE: given 3 and 1 this doesn't work right
00233                                                                         //IMPROVE: given 1 and 0 this doesn't work right either
00234         }
00235         baseFrameList.insert( baseFrameList.begin(), root ) ;
00236         commonBase = root ;
00237 
00238         //now we know that there's a common frame that we can use to relate these frames in 
00239         
00240         Frame relative ;        //this is the frame we're looking at
00241         //multiply back to the common frame by the inverse of frames from the frame backwards
00242         unsigned int frameNum = frame ;
00243         while( frameNum != commonBase )
00244         {
00245                 Frame multiplyMe = allFrames[ frameNum ] ;
00246                 relative = multiplyMe * relative ;
00247                 frameNum = BaseFrame( frameNum ) ;
00248         }
00249         
00250         //multiply out to the base frame
00251         Frame toBase ;  //this represents the base frame
00252         unsigned int baseNum = base ;
00253         while( baseNum != commonBase )
00254         {
00255                 Frame multiplyMe = allFrames[ baseNum ] ;
00256                 toBase = multiplyMe * toBase ;
00257                 baseNum = BaseFrame( baseNum ) ;
00258         }
00259 
00260         return toBase.Inverse() * relative ;
00261 */
00262   //## end FrameManager::GetTransformRelative%925324832.body
00263 }
00264 
00265 Matrix4x4 FrameManager::GetTransformRelative (const Matrix4x4& frame, const Matrix4x4& base )
00266 {
00267         Matrix4x4 baseInverse = base.Inverse();
00268         return baseInverse * frame;             // Shane:  This is now the correct order of multiplication.  DO NOT CHANGE!
00269                                                                         //  Tba = Tbc * Tca = (Tcb)^-1 * Tca
00270                                                                         //  "aWRTb" = "cWRTb" * "aWRTb"
00271 }
00272 
00273 unsigned int FrameManager::BaseFrame (const unsigned int frame) const
00274 {
00275   //## begin FrameManager::BaseFrame%925324835.body preserve=yes
00276         assert( frame >= 0 ) ;
00277         assert( frame < allFrames.size() ) ;
00278         Frame frameInQuestion = allFrames[ frame ] ;
00279         return frameInQuestion.BaseFrameNum() ;
00280   //## end FrameManager::BaseFrame%925324835.body
00281 }
00282 
00283 void FrameManager::SetBaseFrame (const unsigned int frame, const unsigned int baseFrame)
00284 {
00285   //## begin FrameManager::SetBaseFrame%925703339.body preserve=yes
00286         allFrames[ frame ].SetBaseFrame( baseFrame ) ;
00287   //## end FrameManager::SetBaseFrame%925703339.body
00288 }
00289 
00290 void FrameManager::Clear ()
00291 {
00292   //## begin FrameManager::Clear%927844452.body preserve=yes
00293         allFrames.clear() ;
00294         allWorldFrames.clear() ;
00295         worldFrameValid.clear() ;
00296 
00297         //the 0 frame is set to the identity matrix             //IMPROVE: this is duplicated in the constructor
00298         Frame identity ;                
00299         allFrames.push_back( identity ) ;
00300         allWorldFrames.push_back( identity ) ;
00301         worldFrameValid.push_back( true ) ;
00302 
00303   //## end FrameManager::Clear%927844452.body
00304 }
00305 
00306 void FrameManager::MarkFrameChanged (unsigned int frameNum)
00307 {
00308   //## begin FrameManager::MarkFrameChanged%932581525.body preserve=yes
00309         //this frame has changed, so the world frames associated with it also have to change
00310         if( frameNum != 0 )
00311         {
00312                 worldFrameValid[ frameNum ] = false ;
00313         }
00314 
00315   //## end FrameManager::MarkFrameChanged%932581525.body
00316 }
00317 
00318 //=============================================================================
00319 // MarkToolFrame
00320 //
00321 // Purpose: Marks a particular frame as a tool frame
00322 //=============================================================================
00323 void FrameManager::MarkToolFrame( const int index )
00324 {
00325         m_ToolFrames.insert( index );
00326 }
00327 
00328 Frame FrameManager::GetFrame (const unsigned int frameNum) const
00329 {
00330   //## begin FrameManager::GetFrame%932581526.body preserve=yes
00331         return ( allFrames[ frameNum ] ) ;      //IMPROVE: is it wise to return an object? reference better?
00332   //## end FrameManager::GetFrame%932581526.body
00333 }
00334 
00335 Matrix4x4 FrameManager::GetWorldFrame (const unsigned int frameNum) const
00336 {
00337   //## begin FrameManager::GetWorldFrame%932581527.body preserve=yes
00338         if( frameNum == 0 )
00339         {
00340                 assert( worldFrameValid[ frameNum ] == true );
00341         }
00342         if( worldFrameValid[ frameNum ] == false )
00343         {
00344                 //recompute frames that are invalid
00345                 unsigned int baseFrame      = allFrames[ frameNum ].BaseFrameNum() ;            
00346         Matrix4x4 baseFrameMatrix   = GetWorldFrame( baseFrame );
00347         Matrix4x4 frameMatrix       = allFrames[ frameNum ];
00348                 allWorldFrames[ frameNum ]  =  baseFrameMatrix * frameMatrix;
00349                 worldFrameValid[ frameNum ] = true ;
00350         }
00351         Matrix4x4 returnme = allWorldFrames[ frameNum ] ;
00352         return( allWorldFrames[ frameNum ] );
00353 
00354   //## end FrameManager::GetWorldFrame%932581527.body
00355 }
00356 
00357 void FrameManager::MarkAllFramesChanged ()
00358 {
00359   //## begin FrameManager::MarkAllFramesChanged%932581528.body preserve=yes
00360         for( int i = 1; i < worldFrameValid.size(); i++ )
00361         {
00362                 worldFrameValid[ i ] = false ;
00363         }
00364   //## end FrameManager::MarkAllFramesChanged%932581528.body
00365 }
00366 
00367 // Additional Declarations
00368   //## begin FrameManager%3717CCD90370.declarations preserve=yes
00369   //## end FrameManager%3717CCD90370.declarations
00370 
00371 //## begin module%3717CCD90370.epilog preserve=yes
00372 //## end module%3717CCD90370.epilog

Generated on Sat Apr 1 21:30:34 2006 for Motion Planning Kernel by  doxygen 1.4.6-NO