00001 //## begin module%377944220020.cm preserve=no 00002 // %X% %Q% %Z% %W% 00003 //## end module%377944220020.cm 00004 00005 //## begin module%377944220020.cp preserve=no 00006 //## end module%377944220020.cp 00007 00008 //## Module: CD_Solid%377944220020; Pseudo Package body 00009 //## Source file: C:\project\mpk\code\CollisionDetectors\CD_Solid.cpp 00010 00011 //## begin module%377944220020.additionalIncludes preserve=no 00012 //## end module%377944220020.additionalIncludes 00013 00014 //## begin module%377944220020.includes preserve=yes 00015 #pragma warning( disable : 4786 ) 00016 #pragma message ( "Solid has been disabled as a collision detector" ) 00017 //#include "SOLID-2.0\include\SOLID\solid.h" //IMPROVE: make this a relational pathl 00018 #include <stdio.h> 00019 #include "robots\robotbase.h" 00020 //## end module%377944220020.includes 00021 00022 // CD_Solid 00023 #include "CollisionDetectors\CD_Solid.h" 00024 // Entity 00025 #include "Universe\Entity.h" 00026 // MPK_Box 00027 #include "geometry\MPK_Box.h" 00028 // MPK_Cylinder 00029 #include "geometry\MPK_Cylinder.h" 00030 // Facet 00031 #include "geometry\Facet.h" 00032 // Mesh 00033 #include "geometry\Mesh.h" 00034 // ObjectGroup 00035 #include "geometry\ObjectGroup.h" 00036 //## begin module%377944220020.additionalDeclarations preserve=yes 00037 00038 //IMPROVE: this global function is BAD put it elsewhere 00039 //void collide1(void * client_data, DtObjectRef obj1, DtObjectRef obj2, 00040 // const DtCollData *coll_data) { 00041 //} 00042 //## end module%377944220020.additionalDeclarations 00043 00044 00045 // Class CD_Solid 00046 00047 //## begin CD_Solid::allIndexesGlobal%377D55C1011B.attr preserve=no private: static std::set< unsigned int> {UA} 00048 std::set< unsigned int> CD_Solid::allIndexesGlobal; 00049 //## end CD_Solid::allIndexesGlobal%377D55C1011B.attr 00050 00051 00052 //## begin CD_Solid::firstUnusedIndex%377D574A0057.attr preserve=no private: static unsigned int {UA} 0 00053 unsigned int CD_Solid::firstUnusedIndex = 0; 00054 //## end CD_Solid::firstUnusedIndex%377D574A0057.attr 00055 00056 00057 //## begin CD_Solid::activeCollisionDetector%378115800299.attr preserve=no private: static int {UA} -1 00058 int CD_Solid::activeCollisionDetector = -1; 00059 //## end CD_Solid::activeCollisionDetector%378115800299.attr 00060 00061 //## begin CD_Solid::maxCollisionDetector%378115A60276.attr preserve=no private: static int {UA} -1 00062 int CD_Solid::maxCollisionDetector = -1; 00063 //## end CD_Solid::maxCollisionDetector%378115A60276.attr 00064 00065 00066 //## begin CD_Solid::currentCollisionDetectorID%3781428500B1.attr preserve=no private: static int {UA} -1 00067 int CD_Solid::currentCollisionDetectorID = -1; 00068 //## end CD_Solid::currentCollisionDetectorID%3781428500B1.attr 00069 00070 00071 00072 CD_Solid::CD_Solid (Universe& universe) 00073 //## begin CD_Solid::CD_Solid%930942586.hasinit preserve=no 00074 //## end CD_Solid::CD_Solid%930942586.hasinit 00075 //## begin CD_Solid::CD_Solid%930942586.initialization preserve=yes 00076 : 00077 CollisionDetectorBase( universe ), 00078 CD_BasicStyle( universe ), 00079 CD_Linear( universe ), 00080 CD_LinearContinuous( universe ), 00081 collisionDetectorID( maxCollisionDetector + 1 ) 00082 //## end CD_Solid::CD_Solid%930942586.initialization 00083 { 00084 //## begin CD_Solid::CD_Solid%930942586.body preserve=yes 00085 maxCollisionDetector++ ; 00086 for( int j = 0; j < entities.size(); j++ ) 00087 { 00088 entities[ j ]->SetFrameManager( &theFrameManager ) ; 00089 Entity* addme = entities[ j ] ; 00090 // Entity* cloned = entities[ j ]->Clone() ; 00091 CD_Bool::allGeometryUsed &= AddEntityToSolid( addme ) ; 00092 } 00093 //IMPROVE: this will cause memory leaks 00094 //## end CD_Solid::CD_Solid%930942586.body 00095 } 00096 00097 CD_Solid::CD_Solid (const CD_Solid& right) 00098 //## begin CD_Solid::CD_Solid%930942587.hasinit preserve=no 00099 //## end CD_Solid::CD_Solid%930942587.hasinit 00100 //## begin CD_Solid::CD_Solid%930942587.initialization preserve=yes 00101 : 00102 CollisionDetectorBase( right ), 00103 CD_BasicStyle( right ), 00104 CD_Linear( right ), 00105 CD_LinearContinuous( right ) 00106 //## end CD_Solid::CD_Solid%930942587.initialization 00107 { 00108 //## begin CD_Solid::CD_Solid%930942587.body preserve=yes 00109 assert( false ); 00110 //## end CD_Solid::CD_Solid%930942587.body 00111 } 00112 00113 00114 CD_Solid::~CD_Solid() 00115 { 00116 /* //## begin CD_Solid::~CD_Solid%.body preserve=yes 00117 00118 //go through and delete all the objects that have been instanced 00119 for( int i = 0; i < shapeIDs.size(); i++ ) 00120 { 00121 void* deleteMe = shapeIDs[ i ] ; 00122 dtDeleteShape( shapeIDs[ i ] ); 00123 } 00124 shapeIDs.clear() ; 00125 00126 //remove the references to these objects from the global list 00127 for( i = 0; i < indexes.size(); i++ ) 00128 { 00129 unsigned int eraseMe = indexes[ i ] ; 00130 allIndexesGlobal.erase( allIndexesGlobal.find( eraseMe ) ) ; 00131 dtDeleteObject( ( DtObjectRef )indexes[ i ] ) ; 00132 } 00133 indexes.clear() ; 00134 frameSolid.clear() ; 00135 00136 currentCollisionDetectorID = -1 ; 00137 //## end CD_Solid::~CD_Solid%.body 00138 */ 00139 } 00140 00141 00142 00143 //## Other Operations (implementation) 00144 CollisionDetectorBase* CD_Solid::Clone () const 00145 { 00146 //## begin CD_Solid::Clone%930942581.body preserve=yes 00147 CD_Solid* newS = new CD_Solid( *this ) ; 00148 CD_InterfaceToCollisionQueries* CDitc = dynamic_cast< CD_Bool* >( newS ) ; 00149 CollisionDetectorBase* returnMe = dynamic_cast< CollisionDetectorBase* >( CDitc ); 00150 return returnMe ; 00151 //## end CD_Solid::Clone%930942581.body 00152 } 00153 00154 bool CD_Solid::IsInterfering (const Configuration& config) 00155 { 00156 /* //## begin CD_Solid::IsInterfering%930942585.body preserve=yes 00157 SetConfiguration( config ) ; 00158 00159 //go through all the frames and look at the meshes that are there to update their frames 00160 for( int i = 0; i < meshes.size(); i++ ) 00161 { 00162 const Mesh* mesh = meshes[ i ] ; 00163 00164 Matrix4x4 m1 = mesh->GetTransform() ; 00165 00166 void* index = ( void* )indexes[ i ] ; 00167 assert( index >= 0 ) ; 00168 00169 //IMPROVE: find a way to do this via a funciton call 00170 dtSelectObject( index ) ; 00171 double matrix[ 16 ] ; 00172 for( int j = 0; j < 16; j++ ) 00173 { 00174 int r = j % 4 ; 00175 int c = j / 4 ; 00176 matrix[ j ] = m1( r, c ) ; 00177 } 00178 dtLoadMatrixd( ( const double* ) matrix ) ; 00179 } 00180 00181 //check if this is the currently selected collision detector 00182 if( activeCollisionDetector != collisionDetectorID ) 00183 { 00184 activeCollisionDetector = collisionDetectorID ; 00185 } 00186 SetCollidingPairs() ; 00187 00188 int number = dtTest() ; 00189 if( number > 0 ) 00190 { 00191 return true ; 00192 } 00193 return false ; 00194 //## end CD_Solid::IsInterfering%930942585.body 00195 */ 00196 return false; 00197 } 00198 00199 bool CD_Solid::AddEntityToSolid (const Entity* entity) 00200 { 00201 //## begin CD_Solid::AddEntityToSolid%930942588.body preserve=yes 00202 00203 //check if the entity is a mesh 00204 const Mesh* mesh = dynamic_cast< const Mesh* >( entity ) ; 00205 if( mesh != NULL ) 00206 { 00207 AddMeshToSolid( *mesh ) ; 00208 return true ; 00209 } 00210 00211 //check if the entity is a group 00212 const ObjectGroup* group = dynamic_cast< const ObjectGroup* >( entity ) ; 00213 if( group != NULL ) 00214 { 00215 bool returnMe = true ; 00216 for( int i = 0; i < group->Size(); i++ ) 00217 { 00218 const Entity* addme = ( *group )[ i ] ; 00219 returnMe &= AddEntityToSolid( addme ) ; 00220 } 00221 return returnMe ; 00222 } 00223 00224 //check if the entity is a cylinder 00225 const MPK_Cylinder* cylinder = dynamic_cast< const MPK_Cylinder* >( entity ) ; 00226 if( cylinder != NULL ) 00227 { 00228 int index = AddCylinderToSolid( cylinder ) ; 00229 //IMPROVE: check the response of AddCylinder... 00230 return true ; 00231 } 00232 00233 //check if the entity is a box 00234 const MPK_Box* box = dynamic_cast< const MPK_Box* >( entity ) ; 00235 if( box != NULL ) 00236 { 00237 //int index = AddCylinderToSolid( cylinder ) ; 00238 //IMPROVE: check the response of AddCylinder... 00239 return true ; 00240 } 00241 00242 //check if the entity is a box 00243 const MPK_Sphere* sphere = dynamic_cast< const MPK_Sphere* >( entity ) ; 00244 if( sphere != NULL ) 00245 { 00246 //int index = AddCylinderToSolid( cylinder ) ; 00247 //IMPROVE: check the response of AddCylinder... 00248 return true ; 00249 } 00250 00251 //check if the entity is a robot - in which case do nothing 00252 const RobotBase* robot = dynamic_cast< const RobotBase* >( entity ) ; 00253 if( robot != NULL ) 00254 { 00255 return true ; 00256 } 00257 00258 //improve: need to mark down what frames all the vcollide indexes are in 00259 return( false ) ; 00260 00261 //## end CD_Solid::AddEntityToSolid%930942588.body 00262 } 00263 00264 int CD_Solid::AddMeshToSolid (const Mesh& mesh) 00265 { 00266 /* 00267 meshes.push_back( &mesh ) ; 00268 00269 //create an array of doubles that list the xyz values of the vertexes 00270 int numberOfVertexes = mesh.vertexes.size() ; 00271 double* vertexList = new double[ numberOfVertexes * 3 ] ; 00272 for( int i = 0; i < numberOfVertexes; i++ ) 00273 { 00274 vertexList[ i * 3 + 0 ] = ( mesh.vertexes[ i ] )[ 0 ] ; 00275 vertexList[ i * 3 + 1 ] = ( mesh.vertexes[ i ] )[ 1 ] ; 00276 vertexList[ i * 3 + 2 ] = ( mesh.vertexes[ i ] )[ 2 ] ; 00277 } 00278 00279 DtShapeRef shape = dtNewComplexShape(); 00280 dtVertexBase( vertexList ); 00281 int fsize = mesh.facets.size() ; 00282 for( int j = 0; j < fsize; j++ ) 00283 { 00284 dtBegin( DT_POLYGON ); 00285 Facet f = mesh.facets[ j ] ; 00286 int size = f.vertexNumbers.size() ; 00287 for( int k = 0; k < size; k++ ) 00288 { 00289 dtVertexIndex( f.vertexNumbers[ k ] ) ; 00290 } 00291 dtEnd(); 00292 } 00293 dtEndComplexShape(); 00294 00295 unsigned int index = GetNewIndex() ; 00296 dtCreateObject( (void*) index, shape ) ; 00297 dtSelectObject( (void*) index ) ; 00298 shapeIDs.push_back( shape ) ; 00299 delete[] vertexList ; 00300 00301 this->MarkIndexEntityRelationship( index, &mesh ) ; 00302 //update the fact that this mesh doesn't collide with anything 00303 return index ; 00304 */ 00305 return false; 00306 } 00307 00308 void CD_Solid::SetCollidingPairs () const 00309 { 00310 /* 00311 //## begin CD_Solid::SetCollidingPairs%931206292.body preserve=yes 00312 if( currentCollisionDetectorID == collisionDetectorID ) 00313 { 00314 return ; 00315 } 00316 currentCollisionDetectorID = collisionDetectorID ; 00317 00318 for( int i = 0; i < meshes.size(); i++ ) 00319 { 00320 for( int j = i; j < meshes.size(); j++ ) 00321 { 00322 bool shouldCollide = true ; 00323 unsigned int base1 = meshes[ i ]->BaseFrame() ; 00324 unsigned int base2 = meshes[ j ]->BaseFrame() ; 00325 // 00326 if( base1 == base2 ) shouldCollide = false ; 00327 if( theFrameManager.BaseFrame( base1 ) == base2 ) shouldCollide = false ; 00328 if( theFrameManager.BaseFrame( base2 ) == base1 ) shouldCollide = false ; 00329 00330 if( shouldCollide ) 00331 { 00332 void* index1 = (void*) indexes[ i ] ; 00333 void* index2 = (void*) indexes[ j ] ; 00334 dtSetPairResponse 00335 ( 00336 index1, 00337 index2, 00338 collide1, 00339 DT_SIMPLE_RESPONSE, 00340 stdout 00341 ) ; 00342 } 00343 } 00344 } 00345 //## end CD_Solid::SetCollidingPairs%931206292.body 00346 */ 00347 } 00348 00349 unsigned int CD_Solid::GetNewIndex () 00350 { 00351 //## begin CD_Solid::GetNewIndex%930961054.body preserve=yes 00352 unsigned int returnMe = firstUnusedIndex ; 00353 firstUnusedIndex ++ ; //IMPROVE: Do we need to worry about running out of indexes? Maybe? 00354 return returnMe ; 00355 //## end CD_Solid::GetNewIndex%930961054.body 00356 } 00357 00358 void CD_Solid::ActivateFrames (const unsigned int frame1, const unsigned int frame2) 00359 { 00360 /* 00361 //## begin CD_Solid::ActivateFrames%931291666.body preserve=yes 00362 if( !FramePairPermEnabled( frame1, frame2 ) ) 00363 { 00364 return ; 00365 } 00366 CD_BasicStyle::ActivateFrames( frame1, frame2 ) ; 00367 std::set< unsigned int > s1 = frameSolid[ frame1 ] ; 00368 std::set< unsigned int > s2 = frameSolid[ frame2 ] ; 00369 std::set< unsigned int >::iterator i1 = s1.begin(); //IMPROVE: added the s1.begin() 00370 std::set< unsigned int >::iterator i2 = s2.begin(); 00371 00372 for( ;i1 != s1.end(); i1++ ) 00373 { 00374 for( ;i2 != s2.end(); i2++ ) 00375 { 00376 dtSetPairResponse 00377 ( 00378 (void*)*i1, 00379 (void*)*i2, 00380 collide1, 00381 DT_SIMPLE_RESPONSE, 00382 stdout 00383 ) ; 00384 } 00385 } 00386 00387 //## end CD_Solid::ActivateFrames%931291666.body 00388 */ 00389 } 00390 00391 void CD_Solid::DeactivateFrames (const unsigned int frame1, const unsigned int frame2) 00392 { 00393 /* 00394 //## begin CD_Solid::DeactivateFrames%931291667.body preserve=yes 00395 CD_BasicStyle::DeactivateFrames( frame1, frame2 ) ; 00396 std::set< unsigned int > s1 = frameSolid[ frame1 ] ; 00397 std::set< unsigned int > s2 = frameSolid[ frame2 ] ; 00398 std::set< unsigned int >::iterator i1 = s1.begin() ; 00399 std::set< unsigned int >::iterator i2 = s2.begin() ; 00400 00401 for( ;i1 != s1.end(); i1++ ) 00402 { 00403 for( ;i2 != s2.end(); i2++ ) 00404 { 00405 dtClearPairResponse 00406 ( 00407 (void*)*i1, 00408 (void*)*i2 00409 ) ; 00410 } 00411 } 00412 00413 //## end CD_Solid::DeactivateFrames%931291667.body 00414 */ 00415 } 00416 00417 // Additional Declarations 00418 //## begin CD_Solid%377944220020.declarations preserve=yes 00419 int CD_Solid::AddCylinderToSolid( const MPK_Cylinder* cylinder ) 00420 { 00421 /* 00422 DtShapeRef shape = dtCylinder( cylinder->Radius(), cylinder->Height() ); 00423 unsigned int index = GetNewIndex() ; 00424 dtCreateObject( (void*) index, shape ) ; 00425 dtSelectObject( (void*) index ) ; 00426 shapeIDs.push_back( shape ) ; 00427 00428 //assert( this should be in the calling function or alter the return valuel 00429 this->MarkIndexEntityRelationship( index, cylinder) ; 00430 return index ; 00431 */ 00432 return 0; 00433 } 00434 00435 void CD_Solid::MarkIndexEntityRelationship( int index, const Entity* entity ) 00436 { 00437 //update the fact that we just used a new index 00438 firstUnusedIndex = index + 1 ; 00439 allIndexesGlobal.insert( index ) ; 00440 indexes.push_back( index ) ; 00441 00442 //update the index frame list 00443 int baseFrame = entity->BaseFrame() ; 00444 //check if we've seen a frame number this high before - IMPROVE: use a mapping? 00445 if( frameSolid.size() < baseFrame + 1 ) 00446 { 00447 frameSolid.resize( baseFrame + 1 ) ; 00448 } 00449 frameSolid[ baseFrame ].insert( index ) ; 00450 } 00451 00452 //## end CD_Solid%377944220020.declarations 00453 //## begin module%377944220020.epilog preserve=yes 00454 //## end module%377944220020.epilog