collisiondetectors/CD_BasicStyle.cpp

Go to the documentation of this file.
00001 #include <assert.h>
00002 #include "CollisionDetectors\CD_BasicStyle.h"
00003 #include <math.h>
00004 #include <math/math2.h>
00005 #include "robots/robotbase.h"
00006 #include "Universe\Universe.h"
00007 
00008 CD_BasicStyle::CD_BasicStyle (const Universe& universe)
00009   :
00010         CollisionDetectorBase( universe ),
00011         CD_Linear( universe )
00012 {
00013         Universe *pUniverse = (Universe *)&universe;    
00014         theFrameManager = *(pUniverse->GetFrameManager()) ; 
00015 
00016         //add all the entities to the entity list
00017         std::vector< Entity* > allEntities = universe.GetAllEntities() ;
00018         int size = allEntities.size() ;
00019     int i;
00020         for( i = 0; i < size; i++ )
00021         {
00022                 Entity* cloneMe = allEntities[ i ] ;
00023                 Entity* clone = cloneMe->Clone() ;
00024                 clone->SetFrameManager( &theFrameManager ) ;
00025                 entities.push_back( clone );
00026         }
00027 
00028         //add all the links to the link list
00029         std::vector< LinkBase* > allLinks = universe.GetAllLinks() ;
00030         for( i = 0; i < allLinks.size(); i++ )
00031         {
00032                 LinkBase* cloneMe = allLinks[ i ] ;
00033                 LinkBase* addme = cloneMe->Clone() ;
00034                 addme->SetFrameManager( &theFrameManager ) ;
00035                 links.push_back( addme );
00036         }
00037 
00038         PermActivateAll() ;
00039         PermDeactivateFramesWithThemselves() ;
00040 }
00041 
00042 CD_BasicStyle::CD_BasicStyle (const CD_BasicStyle& right)
00043 :
00044         CollisionDetectorBase( right ),
00045         CD_JointLimits( right ),
00046         CD_Linear( right )
00047 {
00048         //IMPROVE: need to do some serious deep copying here
00049 
00050         //copy the entities
00051         entities.clear() ;
00052     int i;
00053         for( i = 0; i < right.entities.size(); i++ )
00054         {
00055                 entities.push_back( right.entities[ i ]->Clone() ) ;
00056         }
00057 
00058         links.clear() ;
00059         for( i = 0; i < right.links.size(); i++ )
00060         {
00061                 LinkBase* cloneMe = right.links[ i ] ;
00062                 LinkBase* addme = cloneMe->Clone() ;
00063                 links.push_back( addme ) ;
00064         }
00065 
00066         //IMPROVE: will this work?
00067         permCollisionMatrix = right.permCollisionMatrix ;
00068         collisionMatrix = right.collisionMatrix ;
00069 
00070 
00071         theFrameManager = right.theFrameManager ;
00072 }
00073 
00074 
00075 CD_BasicStyle::~CD_BasicStyle()
00076 {
00077     //delete all the cloned entities
00078     for( int i = 0; i < entities.size(); i++ )
00079     {
00080                 Entity* deleteMe = entities[ i ] ;
00081                 delete( deleteMe ) ;
00082                 entities[ i ] = NULL ;
00083         }
00084 
00085         //delete all the cloned links
00086         for( int j = 0; j < links.size(); j++ )
00087         {
00088                 LinkBase* deleteme = links[ j ] ;
00089                 delete( deleteme ) ;
00090                 links[ j ] = NULL ;
00091         }
00092 }
00093 
00094 
00095 
00096 bool CD_BasicStyle::SetConfiguration (const Configuration& config)
00097 {
00098         bool result = CD_Bool::SetConfiguration( config ) ;
00099         //update all the frames         //IMPROVE: only do this for those frames that change
00100         int lsize = links.size();
00101         int csize = config.Length();
00102     IJG_Assert( lsize == csize );
00103         for( int i = 0; i < lsize; ++i )
00104         {
00105                 double jointVal = config[ i ];
00106                 links[ i ]->SetJointVariable( jointVal );
00107                 links[ i ]->UpdateFrames();
00108         }
00109 
00110         //mark all the frames as having changed
00111         theFrameManager.MarkAllFramesChanged() ;
00112 
00113         CD_Bool::configInitialized = true ;
00114         return result ;
00115 }
00116 
00117 double CD_BasicStyle::JointMin (const unsigned int jointNum) const
00118 {
00119   //## begin CD_BasicStyle::JointMin%928263330.body preserve=yes
00120         //IMPROVE: I'd rather have this in a more general class - but..
00121         assert( jointNum < links.size() ) ;
00122         LinkBase* theLink = links[ jointNum ] ;
00123         return theLink->JointMin() ;
00124   //## end CD_BasicStyle::JointMin%928263330.body
00125 }
00126 
00127 double CD_BasicStyle::JointMax (const unsigned int jointNum) const
00128 {
00129   //## begin CD_BasicStyle::JointMax%928263329.body preserve=yes
00130         //IMPROVE: I'd rather have this in a more general class - but..
00131         assert( jointNum < links.size() ) ;
00132         LinkBase* theLink = links[ jointNum ] ;
00133         return theLink->JointMax() ;
00134   //## end CD_BasicStyle::JointMax%928263329.body
00135 }
00136 
00137 //returns the type of the joint
00138 DH_Parameter CD_BasicStyle::JointType( const unsigned int jointNum ) const
00139 {
00140         assert( jointNum < links.size() ) ;
00141         DH_Link* theLink = static_cast< DH_Link* >( links[ jointNum ] ) ;
00142         return theLink->GetJointType() ;
00143 }
00144 
00145 
00146 // returns the frame number associated with given joint
00147 unsigned int CD_BasicStyle::JointFrameNum( const unsigned int jointNum ) const
00148 {
00149         assert( jointNum < links.size() ) ;
00150         DH_Link* theLink = static_cast< DH_Link* >( links[ jointNum ] ) ;
00151         return theLink->FrameNum();
00152 }
00153 
00154 bool CD_BasicStyle::JointWraps (const unsigned int jointNum) const
00155 {
00156   //## begin CD_BasicStyle::JointWraps%928263335.body preserve=yes
00157         assert( jointNum < links.size() ) ;
00158         LinkBase* theLink = links[ jointNum ] ;
00159         return theLink->JointWraps() ;
00160   //## end CD_BasicStyle::JointWraps%928263335.body
00161 }
00162 
00163 bool CD_BasicStyle::ShouldPerformCollisionCheck (unsigned int frameA, unsigned int frameB) const
00164 {
00165   //## begin CD_BasicStyle::ShouldPerformCollisionCheck%928344291.body preserve=yes
00166 
00167         //check the collisionMatrix
00168         bool c = false ;
00169         return true ;   //improve: this is a hack
00170 /*      std::map< unsigned int, std::map< unsigned int, bool > >::const_iterator first = collisionMatrix.find( frameA );
00171         if( first != collisionMatrix.end() )
00172         {
00173                 std::map< unsigned int, bool >::const_iterator second = ( *first ).second.find( frameB ) ;
00174                 if( second != ( *first ).second.end() )
00175                 {
00176                         c = true ;
00177                 }
00178         }*/
00179         assert( false ) ;
00180 
00181         //check the permCollisionMatrix
00182         /*first = permCollisionMatrix.find( frameA );
00183         if( first != permCollisionMatrix.end() )
00184         {
00185                 std::map< unsigned int, bool >::const_iterator second = ( *first ).second.find( frameB ) ;
00186                 if( second != ( *first ).second.end() )
00187                 {
00188                         //cannot be turned on if it's off at this point
00189                 }
00190                 else
00191                 {
00192                         c = false ;     //can only be turned off 
00193                 }
00194         }
00195         else
00196         {
00197                 c = false ;
00198         }*/
00199         assert( false ) ;
00200 
00201         return c ;
00202 //      if( links[ linkB ]->DoesLinkControlFrame( links[ linkA ]->BaseFrameNum() ) == true )
00203 //      {
00204 //              return false ;
00205 //      }
00206 //      if( links[ linkA ]->DoesLinkControlFrame( links[ linkB ]->BaseFrameNum() ) == true )
00207 //      {
00208 //              return false ;
00209 //      }
00210 //      return true ;
00211   //## end CD_BasicStyle::ShouldPerformCollisionCheck%928344291.body
00212 }
00213 
00214 unsigned int CD_BasicStyle::DOF () const
00215 {
00216   //## begin CD_BasicStyle::DOF%930942582.body preserve=yes
00217         return links.size() ;   //IMPROVE: some links may not translate into degrees of freedom ?
00218   //## end CD_BasicStyle::DOF%930942582.body
00219 }
00220 
00221 void CD_BasicStyle::DeactivateFrames (const unsigned int frame1, const unsigned int frame2)
00222 {
00223   //## begin CD_BasicStyle::DeactivateFrames%931291663.body preserve=yes
00224         if( frame2 > frame1 )
00225         {
00226                 DeactivateFrames( frame2, frame1 ) ;
00227                 return ;
00228         }
00229 
00230         if( !FramePairPermEnabled( frame1, frame2 ) )
00231         {
00232                 return ;
00233         }
00234 
00235         PairInt findMe1( frame1, frame2 ) ;
00236         std::map< PairInt, bool >::iterator found1 = collisionMatrix.find( findMe1 ) ;
00237         if( found1 == collisionMatrix.end() )
00238         {
00239                 return ;
00240         }
00241         collisionMatrix.erase( found1 ) ;
00242 
00243         PairInt findMe2( frame2, frame1 ) ;
00244         std::map< PairInt, bool >::iterator found2 = collisionMatrix.find( findMe2 ) ;
00245         if( found2 == collisionMatrix.end() )
00246         {
00247                 return ;
00248         }
00249         collisionMatrix.erase( found2 ) ;
00250 
00251         //IMPROVE: check to see if this worked
00252   //## end CD_BasicStyle::DeactivateFrames%931291663.body
00253 }
00254 
00255 void CD_BasicStyle::ActivateFrames (const unsigned int frame1, const unsigned int frame2)
00256 {
00257   //## begin CD_BasicStyle::ActivateFrames%931291664.body preserve=yes
00258         if( frame2 > frame1 )
00259         {
00260                 ActivateFrames( frame2, frame1 ) ;
00261         }
00262 
00263         if( !this->FramePairPermEnabled( frame1, frame2 ) )
00264         {
00265                 return ;
00266         }
00267 
00268         std::map< PairInt, bool >::value_type insertMe1( PairInt( frame1, frame2 ), true ) ;
00269         collisionMatrix.insert( insertMe1 ) ;
00270         std::map< PairInt, bool >::value_type insertMe2( PairInt( frame2, frame1 ), true ) ;
00271         collisionMatrix.insert( insertMe2 ) ;
00272 
00273         //IMPROVE: check to see if this worked
00274   //## end CD_BasicStyle::ActivateFrames%931291664.body
00275 }
00276 
00277 void CD_BasicStyle::PermDeactivateFrames (const unsigned int frame1, const unsigned int frame2)
00278 {
00279   //## begin CD_BasicStyle::PermDeactivateFrames%931892328.body preserve=yes
00280         if( frame2 > frame1 )
00281         {
00282                 PermDeactivateFrames( frame2, frame1 ) ;
00283         }
00284 
00285         PairInt findMe1( frame1, frame2 ) ;
00286         std::map< PairInt, bool >::iterator found1 = permCollisionMatrix.find( findMe1 ) ;
00287         if( found1 == permCollisionMatrix.end() )
00288         {
00289                 DeactivateFrames( frame1, frame2 ) ;
00290                 return ;
00291         }
00292         permCollisionMatrix.erase( found1 ) ;
00293 
00294         PairInt findMe2( frame2, frame1 ) ;
00295         std::map< PairInt, bool >::iterator found2 = permCollisionMatrix.find( findMe2 ) ;
00296         if( found2 == permCollisionMatrix.end() )
00297         {
00298                 DeactivateFrames( frame2, frame1 ) ;
00299                 return ;
00300         }
00301         permCollisionMatrix.erase( found2 ) ;
00302 
00303         
00304 
00305         assert( FramePairPermEnabled( frame1, frame2 ) == false ) ;
00306         DeactivateFrames( frame1, frame2 ) ;
00307         //frame is deactivated 
00308 
00309   //## end CD_BasicStyle::PermDeactivateFrames%931892328.body
00310 }
00311 
00312 void CD_BasicStyle::PermActivateFrames (const unsigned int frame1, const unsigned int frame2)
00313 {
00314   //## begin CD_BasicStyle::PermActivateFrames%931892329.body preserve=yes
00315         if( frame2 > frame1 )
00316         {
00317                 PermActivateFrames( frame2, frame1 ) ;
00318         }
00319 
00320         std::map< PairInt, bool >::value_type insertMe1( PairInt( frame1, frame2 ), true ) ;
00321         permCollisionMatrix.insert( insertMe1 ) ;
00322         std::map< PairInt, bool >::value_type insertMe2( PairInt( frame2, frame1 ), true ) ;
00323         permCollisionMatrix.insert( insertMe2 ) ;
00324 
00325         assert( FramePairPermEnabled( frame1, frame2 ) == true ) ;
00326         assert( FramePairPermEnabled( frame2, frame1 ) == true ) ;
00327         ActivateFrames( frame1, frame2 ) ;
00328   //## end CD_BasicStyle::PermActivateFrames%931892329.body
00329 }
00330 
00331 bool CD_BasicStyle::FramePairPermEnabled (const unsigned int frame1, const unsigned int frame2) const
00332 {
00333   //## begin CD_BasicStyle::FramePairPermEnabled%931892330.body preserve=yes
00334         PairInt findMe( frame1, frame2 ) ;
00335         std::map< PairInt, bool >::const_iterator found = permCollisionMatrix.find( findMe ) ;
00336         if( found == permCollisionMatrix.end() )
00337         {
00338                 return false ;
00339         }
00340         return ( *found ).second ;
00341   //## end CD_BasicStyle::FramePairPermEnabled%931892330.body
00342 }
00343 
00344 //allows access to the frame manager
00345 FrameManager* CD_BasicStyle::GetFrameManager()
00346 {
00347         return &theFrameManager;
00348 }
00349 
00350 void CD_BasicStyle::DeactivateAllFrames ()
00351 {
00352   //## begin CD_BasicStyle::DeactivateAllFrames%940546750.body preserve=yes
00353         for( int i = 0; i <= DOF(); i++ )
00354         {
00355                 for( int j = 0; j <= DOF(); j++ )
00356                 {
00357                         DeactivateFrames( i, j ) ;
00358                 }
00359         }
00360   //## end CD_BasicStyle::DeactivateAllFrames%940546750.body
00361 }
00362 
00363 void CD_BasicStyle::PermActivateAll ()
00364 {
00365   //## begin CD_BasicStyle::PermActivateAll%941590723.body preserve=yes
00366         //at first all objects are able to interfere with one another - need to turn off inter frame collisions
00367         for( int i = 0; i <= DOF(); i++ ) 
00368         {
00369                 for( int j = 0; j <= i; j++ )
00370                 {
00371                         PermActivateFrames( i, j ) ;
00372                 }
00373         }
00374 
00375 /*      for( int i = 0; i < links.size(); i++ )         //IMPROVE: this used to say linkVcollide
00376         {
00377                 LinkBase* link = links[ i ] ;
00378                 //link->SetFrameManager( &theFrameManager ) ;
00379                 int size = link->controlledFrames.size() ;
00380                 for( int j = 0; j < link->controlledFrames.size(); j++ )
00381                 {
00382                         unsigned int frameNum = link->controlledFrames[ j ] ;
00383                         unsigned int baseNum = link->BaseFrameNum() ;
00384                         PermDeactivateFrames( baseNum, baseNum ) ;                      
00385                         PermDeactivateFrames( frameNum, frameNum ) ;
00386                 }
00387         }*/
00388   //## end CD_BasicStyle::PermActivateAll%941590723.body
00389 }
00390 
00391 void CD_BasicStyle::PermDeactivateFramesWithThemselves ()
00392 {
00393   //## begin CD_BasicStyle::PermDeactivateFramesWithThemselves%941590724.body preserve=yes
00394         for( int i = 0; i <= DOF(); i++ )
00395         {
00396                 PermDeactivateFrames( i, i ) ;
00397         }
00398   //## end CD_BasicStyle::PermDeactivateFramesWithThemselves%941590724.body
00399 }
00400 
00401 void CD_BasicStyle::PermDeactivateAll ()
00402 {
00403   //## begin CD_BasicStyle::PermDeactivateAll%941688703.body preserve=yes
00404         for( int i = 0; i <= DOF(); i++ ) 
00405         {
00406                 for( int j = 0; j <= i; j++ )
00407                 {
00408                         PermDeactivateFrames( i, j ) ;
00409                 }
00410         }
00411   //## end CD_BasicStyle::PermDeactivateAll%941688703.body
00412 }
00413 
00414 //The following two functions are added by Zhenwang.Yao
00415 std::vector<LinkBase*> CD_BasicStyle::GetAllLinks() const 
00416 {
00417   return links;
00418 }
00419 
00420 //The following two functions are added by Zhenwang.Yao
00421 std::vector<Entity*> CD_BasicStyle::GetAllElements() const 
00422 {
00423   return entities;
00424 }
00425 
00426 //=============================================================================
00427 // CD_BasicStyle::GetRobot
00428 //=============================================================================
00429 RobotBase* CD_BasicStyle::GetRobot( const int index ) const
00430 {
00431     int counter = 0;
00432     int i;
00433     int size = entities.size();
00434     for( i = 0; i < size; ++i )
00435     {
00436         Entity* testMe = entities[ i ];
00437         RobotBase* robot = dynamic_cast< RobotBase* >( testMe );
00438         if( robot != NULL )
00439         {
00440             if( counter == index )
00441             {
00442                 return robot;
00443             }
00444             else
00445             {
00446                 ++counter;
00447                 continue;
00448             }
00449         }
00450     }
00451     return NULL;
00452 }
00453 
00454 //End Added by Zhenwang.Yao
00455 
00456 // Additional Declarations
00457   //## begin CD_BasicStyle%37556E320142.declarations preserve=yes
00458   //## end CD_BasicStyle%37556E320142.declarations
00459 
00460 //## begin module%37556E320142.epilog preserve=yes
00461 //## end module%37556E320142.epilog

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