00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <list>
00016 #include <assert.h>
00017 #include "streams\mystreams.h"
00018
00019
00020
00021 #include "kinematics\Configuration.h"
00022
00023 #include "Universe\Universe.h"
00024
00025 #include "geometry\Mesh.h"
00026
00027 #include "Kinematics\DH_Link.h"
00028
00029 #include "robots\RobotBase.h"
00030
00031 #include "additional\streams\IfstreamWithComments.h"
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 Universe::Universe ()
00043
00044
00045
00046 :
00047 CD_JointLimits()
00048
00049 {
00050
00051
00052 }
00053
00054 Universe::Universe (const Universe& right)
00055 :
00056 CD_JointLimits( right )
00057 {
00058
00059 int thisSize = this->theFrameManager.GetNumberOfFrames() ;
00060 int rightSize = right.theFrameManager.GetNumberOfFrames() ;
00061
00062 unsigned int i ;
00063 for( i = 0; i < right.links.size(); i++ )
00064 {
00065 LinkBase* addme = right.links[ i ];
00066 this->AddLink( addme ) ;
00067 }
00068 this->totalPolys = right.totalPolys;
00069 }
00070
00071
00072 Universe::~Universe()
00073 {
00074
00075 this->Empty();
00076
00077 }
00078
00079
00080
00081
00082 void Universe::AddEntity (Entity* entity)
00083 {
00084
00085 assert( entity != NULL ) ;
00086
00087 entity->SetFrameManager( &theFrameManager ) ;
00088
00089
00090 if( dynamic_cast<RobotBase*>( entity ))
00091 {
00092 RobotBase* robot = dynamic_cast<RobotBase*>( entity->Clone() );
00093 robot->SetFrameManager( &theFrameManager ) ;
00094
00095
00096 std::list< LinkBase* > allLinks = robot->GetAllLinks() ;
00097 for( std::list< LinkBase* >::iterator i = allLinks.begin(); i != allLinks.end(); i++ )
00098 {
00099 LinkBase* link = ( *i )->Clone();
00100 link->SetFrameManager( this->GetFrameManager() ) ;
00101 AddLink( link ) ;
00102 delete link ;
00103 }
00104
00105
00106
00107
00108 entities.push_back( robot ) ;
00109
00110
00111
00112
00113 std::list< int > allToolFrames = robot->GetAllToolFrames();
00114 std::list< int >::iterator fIt;
00115 for( fIt = allToolFrames.begin(); fIt != allToolFrames.end(); fIt++ )
00116 {
00117 int addMe = *fIt;
00118 theFrameManager.MarkToolFrame( addMe );
00119 }
00120 }
00121 else
00122 {
00123 Entity* addme = entity->Clone() ;
00124 addme->SetFrameManager( &theFrameManager ) ;
00125 entities.push_back( addme ) ;
00126 }
00127
00128 }
00129
00130 void Universe::AddLink (LinkBase* link)
00131 {
00132
00133 assert( link != NULL ) ;
00134 LinkBase* cloned = link->Clone() ;
00135 links.push_back( cloned ) ;
00136 int size = links.size();
00137 LinkBase* retrieved = links[ size - 1 ];
00138
00139
00140 unsigned int i;
00141 for( i = 0; i < link->objects.size(); i++ )
00142 {
00143 ObjectBase* object = link->objects[ i ] ;
00144 AddEntity( dynamic_cast< Entity* >( object ) ) ;
00145 }
00146
00147
00148 for( i = 0; i < link->controlledFrames.size(); i++ )
00149 {
00150 theFrameManager.SetBaseFrame( link->controlledFrames[ i ], link->BaseFrameNum() ) ;
00151 }
00152
00153 }
00154
00155 FrameManager* Universe::GetFrameManager ()
00156 {
00157
00158 return &theFrameManager ;
00159
00160 }
00161
00162 const std::vector< Entity* >& Universe::GetAllEntities () const
00163 {
00164
00165 return entities ;
00166
00167 }
00168
00169 const std::vector< LinkBase* > Universe::GetAllLinks () const
00170 {
00171
00172 return links ;
00173
00174 }
00175
00176 void Universe::SetConfiguration (const Configuration& configuration)
00177 {
00178
00179 int lSize = links.size() ;
00180 int cSize = configuration.Length() ;
00181 assert( configuration.Length() == links.size() ) ;
00182
00183 unsigned int i;
00184 for( i = 0; i < links.size(); i++ )
00185 {
00186 double c = configuration[ i ] ;
00187 links[ i ]->SetJointVariable( c ) ;
00188 links[ i ]->UpdateFrames() ;
00189 }
00190 theFrameManager.MarkAllFramesChanged() ;
00191
00192 }
00193
00194 int Universe::TotalPolys () const
00195 {
00196
00197 int totalPolys = 0 ;
00198 unsigned int i;
00199 for( i = 0; i < entities.size(); i++ )
00200 {
00201 ObjectBase* entity = dynamic_cast< ObjectBase* >( entities[ i ] ) ;
00202 if( entity != NULL )
00203 {
00204 totalPolys += entity->TotalPolys() ;
00205 }
00206 }
00207 return totalPolys ;
00208
00209 }
00210
00211 void Universe::Empty ()
00212 {
00213
00214
00215
00216 unsigned int i;
00217 for( i = 0; i < entities.size(); i++ )
00218 {
00219 delete( entities[ i ] ) ;
00220 entities[ i ] = NULL ;
00221 }
00222
00223 for( i = 0; i < links.size(); i++ )
00224 {
00225 delete( links[ i ] ) ;
00226 links[ i ] = NULL ;
00227 }
00228
00229
00230 entities.clear() ;
00231
00232
00233 links.clear() ;
00234
00235
00236 theFrameManager.Clear() ;
00237
00238
00239 }
00240
00241 double Universe::JointMax (const unsigned int jointNum) const
00242 {
00243
00244 unsigned int size = links.size() ;
00245 assert( jointNum < size ) ;
00246 LinkBase* theLink = links[ jointNum ] ;
00247 return theLink->JointMax() ;
00248
00249 }
00250
00251 double Universe::JointMin (const unsigned int jointNum) const
00252 {
00253
00254 assert( jointNum < links.size() ) ;
00255 LinkBase* theLink = links[ jointNum ] ;
00256 return theLink->JointMin() ;
00257
00258
00259 }
00260
00261 unsigned int Universe::DOF () const
00262 {
00263
00264 return links.size() ;
00265
00266 }
00267
00268 bool Universe::JointWraps (const unsigned int jointNum) const
00269 {
00270
00271 assert( jointNum < links.size() ) ;
00272 LinkBase* theLink = links[ jointNum ] ;
00273 return theLink->JointWraps() ;
00274
00275 }
00276
00277 Configuration Universe::GetConfiguration () const
00278 {
00279
00280 Configuration returnMe ;
00281 int length = links.size() ;
00282 returnMe.SetLength( length ) ;
00283
00284 for( int i = 0; i < length; i++ )
00285 {
00286 DH_Link* link = dynamic_cast< DH_Link* >( links[ i ] );
00287 returnMe[ i ] = link->GetJointVariable() ;
00288 }
00289
00290 return returnMe ;
00291
00292 }
00293
00294 const Entity* Universe::GetEntity (const char* name) const
00295 {
00296
00297 unsigned int i;
00298 for( i = 0; i < entities.size(); i++ )
00299 {
00300 Entity* testMe = entities[ i ] ;
00301 if( strcmp( testMe->GetName(), name ) == 0 )
00302 {
00303 return testMe ;
00304 }
00305 }
00306 return NULL ;
00307
00308 }
00309
00310 CollisionDetectorBase* Universe::Clone () const
00311 {
00312
00313 assert( false ) ;
00314 return NULL ;
00315
00316 }
00317
00318
00319
00320
00321 std::ostream & operator<<(std::ostream &os, const Universe& v)
00322 {
00323
00324 using std::endl ;
00325
00326
00327 std::vector< LinkBase*> links = v.GetAllLinks() ;
00328 os << "#links " << links.size() << endl ;
00329 unsigned int i;
00330 for( i = 0; i < links.size(); i++ )
00331 {
00332 links[ i ]->Serialize( os ) ;
00333 }
00334 os << "#endLinks" << endl ;
00335
00336
00337 std::vector< ObjectBase* > frame0Entities ;
00338 std::vector< Entity* > allEntities = v.GetAllEntities() ;
00339
00340 for( i = 0; i < allEntities.size(); i++ )
00341 {
00342 Entity* entity = allEntities[ i ] ;
00343 ObjectBase* object = dynamic_cast< ObjectBase* >( entity ) ;
00344 if( object == NULL )
00345 {
00346
00347 }
00348 else
00349 {
00350 if( object->BaseFrame() != 0 )
00351 {
00352 continue ;
00353 }
00354 frame0Entities.push_back( object ) ;
00355 }
00356 }
00357
00358 os << "#obstacles " << frame0Entities.size() << endl ;
00359 for( i = 0; i < frame0Entities.size(); i++ )
00360 {
00361 frame0Entities[ i ]->Serialize( os ) ;
00362 os << endl ;
00363 }
00364
00365
00366 os << "#endUniverse" << endl ;
00367 return os ;
00368 }
00369
00370 IfstreamWithComments & operator>>( IfstreamWithComments &is, Universe& v )
00371 {
00372 v.Empty() ;
00373
00374 char marker[ 300 ] = "" ;
00375
00376
00377 eatwhite( is ) ;
00378 is.getline( marker, 300, ' ' ) ;
00379 assert( strcmp( marker, "#links" ) == 0 ) ;
00380
00381
00382 int size = 0 ;
00383 is >> size ;
00384
00385
00386 int i;
00387 for( i = 0; i < size; i++ )
00388 {
00389 DH_Link readme( v.GetFrameManager() ) ;
00390 is >> readme ;
00391 v.AddLink( &readme ) ;
00392 }
00393
00394
00395 eatwhite( is ) ;
00396 is.getline( marker, 300 ) ;
00397 assert( strcmp( marker, "#endLinks" ) == 0 ) ;
00398
00399
00400 eatwhite( is ) ;
00401 is.getline( marker, 300, ' ' ) ;
00402 assert( strcmp( marker, "#obstacles" ) == 0 ) ;
00403 int numObstacles = -1 ;
00404 is >> numObstacles ;
00405
00406 for( i = 0; i < numObstacles; i++ )
00407 {
00408 ObjectBase* object = ObjectBase::Deserialize( is ) ;
00409 object->SetBaseFrame( 0 ) ;
00410 v.AddEntity( object ) ;
00411 delete object ;
00412 }
00413
00414
00415 eatwhite( is ) ;
00416 is.getline( marker, 300 ) ;
00417 assert( strcmp( marker, "#endUniverse" ) == 0 ) ;
00418
00419 return is ;
00420 }
00421
00422
00423
00424