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
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
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
00049
00050
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
00067 permCollisionMatrix = right.permCollisionMatrix ;
00068 collisionMatrix = right.collisionMatrix ;
00069
00070
00071 theFrameManager = right.theFrameManager ;
00072 }
00073
00074
00075 CD_BasicStyle::~CD_BasicStyle()
00076 {
00077
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
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
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
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
00120
00121 assert( jointNum < links.size() ) ;
00122 LinkBase* theLink = links[ jointNum ] ;
00123 return theLink->JointMin() ;
00124
00125 }
00126
00127 double CD_BasicStyle::JointMax (const unsigned int jointNum) const
00128 {
00129
00130
00131 assert( jointNum < links.size() ) ;
00132 LinkBase* theLink = links[ jointNum ] ;
00133 return theLink->JointMax() ;
00134
00135 }
00136
00137
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
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
00157 assert( jointNum < links.size() ) ;
00158 LinkBase* theLink = links[ jointNum ] ;
00159 return theLink->JointWraps() ;
00160
00161 }
00162
00163 bool CD_BasicStyle::ShouldPerformCollisionCheck (unsigned int frameA, unsigned int frameB) const
00164 {
00165
00166
00167
00168 bool c = false ;
00169 return true ;
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 assert( false ) ;
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 assert( false ) ;
00200
00201 return c ;
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 }
00213
00214 unsigned int CD_BasicStyle::DOF () const
00215 {
00216
00217 return links.size() ;
00218
00219 }
00220
00221 void CD_BasicStyle::DeactivateFrames (const unsigned int frame1, const unsigned int frame2)
00222 {
00223
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
00252
00253 }
00254
00255 void CD_BasicStyle::ActivateFrames (const unsigned int frame1, const unsigned int frame2)
00256 {
00257
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
00274
00275 }
00276
00277 void CD_BasicStyle::PermDeactivateFrames (const unsigned int frame1, const unsigned int frame2)
00278 {
00279
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
00308
00309
00310 }
00311
00312 void CD_BasicStyle::PermActivateFrames (const unsigned int frame1, const unsigned int frame2)
00313 {
00314
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
00329 }
00330
00331 bool CD_BasicStyle::FramePairPermEnabled (const unsigned int frame1, const unsigned int frame2) const
00332 {
00333
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
00342 }
00343
00344
00345 FrameManager* CD_BasicStyle::GetFrameManager()
00346 {
00347 return &theFrameManager;
00348 }
00349
00350 void CD_BasicStyle::DeactivateAllFrames ()
00351 {
00352
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
00361 }
00362
00363 void CD_BasicStyle::PermActivateAll ()
00364 {
00365
00366
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
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389 }
00390
00391 void CD_BasicStyle::PermDeactivateFramesWithThemselves ()
00392 {
00393
00394 for( int i = 0; i <= DOF(); i++ )
00395 {
00396 PermDeactivateFrames( i, i ) ;
00397 }
00398
00399 }
00400
00401 void CD_BasicStyle::PermDeactivateAll ()
00402 {
00403
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
00412 }
00413
00414
00415 std::vector<LinkBase*> CD_BasicStyle::GetAllLinks() const
00416 {
00417 return links;
00418 }
00419
00420
00421 std::vector<Entity*> CD_BasicStyle::GetAllElements() const
00422 {
00423 return entities;
00424 }
00425
00426
00427
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
00455
00456
00457
00458
00459
00460
00461