basic/universe/Universe.cpp

Go to the documentation of this file.
00001 //## begin module%36FB1455010E.cm preserve=no
00002 //        %X% %Q% %Z% %W%
00003 //## end module%36FB1455010E.cm
00004 
00005 //## begin module%36FB1455010E.cp preserve=no
00006 //## end module%36FB1455010E.cp
00007 
00008 //## Module: Universe%36FB1455010E; Pseudo Package body
00009 //## Source file: C:\project\mpk\code\Universe\Universe.cpp
00010 
00011 //## begin module%36FB1455010E.additionalIncludes preserve=no
00012 //## end module%36FB1455010E.additionalIncludes
00013 
00014 //## begin module%36FB1455010E.includes preserve=yes
00015 #include <list>
00016 #include <assert.h>
00017 #include "streams\mystreams.h"
00018 //## end module%36FB1455010E.includes
00019 
00020 // Configuration
00021 #include "kinematics\Configuration.h"
00022 // Universe
00023 #include "Universe\Universe.h"
00024 // Mesh
00025 #include "geometry\Mesh.h"
00026 // DH_Link
00027 #include "Kinematics\DH_Link.h"
00028 // RobotBase
00029 #include "robots\RobotBase.h"
00030 // IfstreamWithComments
00031 #include "additional\streams\IfstreamWithComments.h"
00032 //## begin module%36FB1455010E.additionalDeclarations preserve=yes
00033 //## end module%36FB1455010E.additionalDeclarations
00034 
00035 
00036 // Class Universe 
00037 
00038 
00039 
00040 
00041 
00042 Universe::Universe ()
00043   //## begin Universe::Universe%961125355.hasinit preserve=no
00044   //## end Universe::Universe%961125355.hasinit
00045   //## begin Universe::Universe%961125355.initialization preserve=yes
00046 :
00047         CD_JointLimits() 
00048   //## end Universe::Universe%961125355.initialization
00049 {
00050   //## begin Universe::Universe%961125355.body preserve=yes
00051   //## end Universe::Universe%961125355.body
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   //## begin Universe::~Universe%.body preserve=yes
00075         this->Empty();
00076   //## end Universe::~Universe%.body
00077 }
00078 
00079 
00080 
00081 //## Other Operations (implementation)
00082 void Universe::AddEntity (Entity* entity)
00083 {
00084   //## begin Universe::AddEntity%922649211.body preserve=yes
00085         assert( entity != NULL ) ;
00086 
00087         entity->SetFrameManager( &theFrameManager ) ;   //IMPROVE: should this be done on the cloned version
00088 
00089         //if the entity is really a robot, then each link needs to be added individually
00090         if( dynamic_cast<RobotBase*>( entity ))
00091         {
00092                 RobotBase* robot = dynamic_cast<RobotBase*>( entity->Clone() );
00093                 robot->SetFrameManager( &theFrameManager ) ;
00094                 
00095                 //add all the links independantly
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                 // Add the entity portion of the robot
00107                 //
00108                 entities.push_back( robot ) ;
00109 
00110                 //
00111                 // Add all the tool frames to the universe
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   //## end Universe::AddEntity%922649211.body
00128 }
00129 
00130 void Universe::AddLink (LinkBase* link)
00131 {
00132   //## begin Universe::AddLink%924739817.body preserve=yes
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         //add the objects that are attached to this link
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         //set up the base frames for the frames controlled by the link just added
00148         for( i = 0; i < link->controlledFrames.size(); i++ )
00149         {
00150                 theFrameManager.SetBaseFrame( link->controlledFrames[ i ], link->BaseFrameNum() ) ;
00151         }
00152   //## end Universe::AddLink%924739817.body
00153 }
00154 
00155 FrameManager* Universe::GetFrameManager ()
00156 {
00157   //## begin Universe::GetFrameManager%924978286.body preserve=yes
00158         return &theFrameManager ;
00159   //## end Universe::GetFrameManager%924978286.body
00160 }
00161 
00162 const std::vector< Entity* >& Universe::GetAllEntities () const
00163 {
00164   //## begin Universe::GetAllEntities%924978289.body preserve=yes
00165         return entities ;
00166   //## end Universe::GetAllEntities%924978289.body
00167 }
00168 
00169 const std::vector< LinkBase* > Universe::GetAllLinks () const
00170 {
00171   //## begin Universe::GetAllLinks%924978292.body preserve=yes
00172         return links ;
00173   //## end Universe::GetAllLinks%924978292.body
00174 }
00175 
00176 void Universe::SetConfiguration (const Configuration& configuration)
00177 {
00178   //## begin Universe::SetConfiguration%926014533.body preserve=yes
00179         int lSize = links.size() ;
00180         int cSize = configuration.Length() ;
00181         assert( configuration.Length() == links.size() ) ;
00182         //for all the links, update their frame values
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() ;        //IMPROVE: don't have to mark them all
00191   //## end Universe::SetConfiguration%926014533.body
00192 }
00193 
00194 int Universe::TotalPolys () const
00195 {
00196   //## begin Universe::TotalPolys%961125357.body preserve=yes
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   //## end Universe::TotalPolys%961125357.body
00209 }
00210 
00211 void Universe::Empty ()
00212 {
00213   //## begin Universe::Empty%927844451.body preserve=yes
00214 
00215         //delete every entity in the universe
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         //remove all the entities
00230         entities.clear() ;
00231 
00232         //remove all the links
00233         links.clear() ;
00234 
00235         //clear the frameManager
00236         theFrameManager.Clear() ;
00237 
00238   //## end Universe::Empty%927844451.body
00239 }
00240 
00241 double Universe::JointMax (const unsigned int jointNum) const
00242 {
00243   //## begin Universe::JointMax%928263339.body preserve=yes
00244         unsigned int size = links.size() ;
00245         assert( jointNum < size ) ;
00246         LinkBase* theLink = links[ jointNum ] ;
00247         return theLink->JointMax() ;
00248   //## end Universe::JointMax%928263339.body
00249 }
00250 
00251 double Universe::JointMin (const unsigned int jointNum) const
00252 {
00253   //## begin Universe::JointMin%928263340.body preserve=yes
00254         assert( jointNum < links.size() ) ;
00255         LinkBase* theLink = links[ jointNum ] ;
00256         return theLink->JointMin() ;
00257         //IMPROVE: not yet written
00258   //## end Universe::JointMin%928263340.body
00259 }
00260 
00261 unsigned int Universe::DOF () const
00262 {
00263   //## begin Universe::DOF%928263341.body preserve=yes
00264         return links.size() ;           //IMPROVE: is this correct?
00265   //## end Universe::DOF%928263341.body
00266 }
00267 
00268 bool Universe::JointWraps (const unsigned int jointNum) const
00269 {
00270   //## begin Universe::JointWraps%928263342.body preserve=yes
00271         assert( jointNum < links.size() ) ;
00272         LinkBase* theLink = links[ jointNum ] ;
00273         return theLink->JointWraps() ;
00274   //## end Universe::JointWraps%928263342.body
00275 }
00276 
00277 Configuration Universe::GetConfiguration () const
00278 {
00279   //## begin Universe::GetConfiguration%940357560.body preserve=yes
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   //## end Universe::GetConfiguration%940357560.body
00292 }
00293 
00294 const Entity* Universe::GetEntity (const char* name) const
00295 {
00296   //## begin Universe::GetEntity%961125356.body preserve=yes
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   //## end Universe::GetEntity%961125356.body
00308 }
00309 
00310 CollisionDetectorBase* Universe::Clone () const
00311 {
00312   //## begin Universe::Clone%961125360.body preserve=yes
00313         assert( false ) ;
00314         return NULL ;
00315   //## end Universe::Clone%961125360.body
00316 }
00317 
00318 // Additional Declarations
00319   //## begin Universe%36FB1455010E.declarations preserve=yes
00320 //-----------------------------------------------------------------------------
00321 std::ostream & operator<<(std::ostream &os, const Universe& v)
00322 {
00323 
00324         using std::endl ;
00325 
00326         //output all the links in the universe
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         //output all the meshes in frame 0
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                         //IMPROVE: this should not happen
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         //IMPROVE: this code is not yet written
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         //input the links
00377         eatwhite( is ) ;
00378         is.getline( marker, 300, ' ' ) ;
00379         assert( strcmp( marker, "#links" ) == 0 ) ;
00380 
00381         //get the number of links stored
00382         int size = 0 ;
00383         is >> size ;
00384 
00385         //get each of the links, and add it to the universe
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         //get the end of links marker
00395         eatwhite( is ) ;
00396         is.getline( marker, 300 ) ;
00397         assert( strcmp( marker, "#endLinks" ) == 0 ) ;          //IMPROVE: it's bad to have strings, especially twice
00398 
00399         //read in all the meshes in link 0      //improve: this should be all the meshes and obejcts
00400         eatwhite( is ) ;
00401         is.getline( marker, 300, ' ' ) ;
00402         assert( strcmp( marker, "#obstacles" ) == 0 ) ;         //IMPROVE: it's bad to have strings, especially twice
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 ) ;             //IMPROVE: is this necessary?
00410                 v.AddEntity( object ) ;
00411                 delete object ;
00412         }
00413 
00414         //get the end of universe marker
00415         eatwhite( is ) ;
00416         is.getline( marker, 300 ) ;
00417         assert( strcmp( marker, "#endUniverse" ) == 0 ) ;               //IMPROVE: it's bad to have strings, especially twice
00418 
00419         return is ;
00420 }
00421 //-----------------------------------------------------------------------------
00422   //## end Universe%36FB1455010E.declarations
00423 //## begin module%36FB1455010E.epilog preserve=yes
00424 //## end module%36FB1455010E.epilog

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