server/ServerBase.cpp

Go to the documentation of this file.
00001 //## begin module%37AB2C4702C6.cm preserve=no
00002 //        %X% %Q% %Z% %W%
00003 //## end module%37AB2C4702C6.cm
00004 
00005 //## begin module%37AB2C4702C6.cp preserve=no
00006 //## end module%37AB2C4702C6.cp
00007 
00008 //## Module: ServerBase%37AB2C4702C6; Pseudo Package body
00009 //## Source file: C:\project\mpk\code\MPKserver\ServerBase.cpp
00010 
00011 //## begin module%37AB2C4702C6.additionalIncludes preserve=no
00012 //## end module%37AB2C4702C6.additionalIncludes
00013 
00014 //## begin module%37AB2C4702C6.includes preserve=yes
00015 #pragma warning( disable : 4786 )
00016 #include "debug/debug.h"
00017 #include <collisionDetectors/CD_Swiftpp.h>
00018 //#define CD_Swiftpp  CD_Vcollide
00019 
00020 #include <collisionDetectors/CD_Vcollide.h>
00021 #include <collisionDetectors/CD_Simple.h>
00022 #include <collisionDetectors/CD_Solid.h>
00023 #include <collisionDetectors/CD_Icollide.h>
00024 #include <collisionDetectors/CD_rangesensor\CD_Range_Sensor.h>
00025 #include <collisionDetectors/collisiondetectortype.h>
00026 #include <planners/plannertype.h>
00027 #include <planners/prm/pl_prmijg.h>
00028 #include <planners/obsolete/pl_linear.h>
00029 #include <planners/obsolete/pl_ianrandom.h>
00030 #include <planners/obsolete/pl_SimplexSubdivision.h>
00031 #include <planners/fortest/pl_ForTest.h>
00032 #include <planners/neural/pl_neural.h>
00033 #include <planners/rrt/pl_RRT.h>
00034 #include <planners/rrt/pl_RRT_Connect.h>
00035 #include <planners/aca/pl_ACA_Connect.h>
00036 #include <planners/rangesensor/PL_Range_Sensor.h>
00037 #include <planners/ik_mpep/pl_mpep.h>
00038 #include <planners/atace/pl_atace.h>
00039 #include <smoothers/SM_PathSmoothingSuccessiveNodePair.h>
00040 #include <smoothers/SM_PathSmoothingRandomNodePair.h>
00041 #include <smoothers/SM_SuccessiveAndRandomSmoothing.h>
00042 #include <smoothers/SM_Trisection.h>
00043 #include <robots/robotbase.h>
00044 #include <serializable/MPK_Dir.h>
00045 
00046 
00047 #include <string/string2.h>
00048 
00049 #ifdef WIN32
00050     #include <direct.h>
00051 #endif
00052 //## end module%37AB2C4702C6.includes
00053 
00054 // CD_BasicStyle
00055 #include "CollisionDetectors/CD_BasicStyle.h"
00056 // ObjectBase
00057 #include "geometry/ObjectBase.h"
00058 // VRML_20_Reader
00059 #include "geometry/VRML_20_Reader.h"
00060 // VRML_Reader
00061 #include "geometry/VRML_Reader.h"
00062 // DH_Link
00063 #include "Kinematics/DH_Link.h"
00064 // R_OpenChain
00065 #include "robots/R_OpenChain.h"
00066 // ServerBase
00067 #include "server/ServerBase.h"
00068 // IfstreamWithComments
00069 #include "streams/IfstreamWithComments.h"
00070 // Jacobian-based Inverse Kinematics Planner
00071 #include "planners/ik_mpep/IK_Jacobian.h"
00072 // Inverse Kinematics Planners
00073 #include "planners/inversekin/IK_InvKinBase.h"
00074 // PL_Juan
00075 #include "Planners/aca/PL_Juan.h"
00076 // PL_Sequential
00077 #include "Planners/sequential/PL_Sequential.h"
00078 // PL_Sandros
00079 #include "Planners/sandros/PL_Sandros.h"
00080 
00081 
00082 // IMPROVE:  These header files should be included last because the LEDA libraries they include, redefines 'std' class and causes
00083 //                       compatablilty errors with the other header files.
00084 // PL_PRM
00085 #include "Planners/prm/pl_prm.h"
00086 // PL_Astar
00087 #include "Planners/astar/PL_Astar.h"
00088 // IK_ACA
00089 #include "planners/ik_aca/IK_ACA.h"
00090 
00091 //## begin module%37AB2C4702C6.additionalDeclarations preserve=yes
00092 //## end module%37AB2C4702C6.additionalDeclarations
00093 
00094 #include "planners/closedchain/PL_prm_closedchain.h"
00095 #include "planners/closedchain/PL_rrt_closedchain.h"
00096 #include "planners/atace/PL_rgd_constraint.h"
00097 
00098 // Class ServerBase 
00099 
00100 
00101 
00102 
00103 
00104 
00105 
00106 
00107 
00108 
00109 
00110 
00111 ServerBase::ServerBase ()
00112   //## begin ServerBase::ServerBase%934854390.hasinit preserve=no
00113 : 
00114         collisionDetectorType(CD_NONE), 
00115         planPassed(false), 
00116         plannerType(PLANNER_NONE), 
00117         collisionDetector(NULL), 
00118         planner(NULL),
00119         smoother(NULL)
00120   //## end ServerBase::ServerBase%934854390.hasinit
00121   //## begin ServerBase::ServerBase%934854390.initialization preserve=yes
00122   //## end ServerBase::ServerBase%934854390.initialization
00123 {
00124         strcpy( filename, "NONE" );
00125         strcpy( obstacleFilename, "NONE" );
00126     strcpy( rootpath, "" );
00127         this->SetCollisionDetector( CD_VCOLLIDE );
00128         this->SetPlanner( PLANNER_NONE );
00129         this->SetSmoother(SMOOTHER_NONE);
00130 //      P_Aca();
00131   //## end ServerBase::ServerBase%934854390.body
00132 }
00133 
00134 
00135 ServerBase::~ServerBase()
00136 {
00137   //## begin ServerBase::~ServerBase%.body preserve=yes
00138         delete collisionDetector;
00139         collisionDetector = NULL;
00140         delete planner;
00141         planner = NULL;
00142         delete smoother;
00143         smoother = NULL;
00144   //## end ServerBase::~ServerBase%.body
00145 }
00146 
00147 
00148 void *ServerBase::GetPlannerParameters()
00149 {
00150         if (planner)
00151                 return planner->GetParameters();
00152         else
00153                 return NULL;
00154 }
00155 
00156 bool ServerBase::SetPlannerParameters(const void *param)
00157 {
00158         if (planner)
00159                 return planner->SetParameters(param);
00160         else
00161                 return false;
00162 }
00163 
00164 //## Other Operations (implementation)
00165 void ServerBase::R_ThreeCylinderRobot ()
00166 {
00167   //## begin ServerBase::R_ThreeCylinderRobot%932600045.body preserve=yes
00168         assert( false );                //IMPROVE: ian doesn't want this function called any more
00169         universe.Empty();
00170         //IMPROVE: check to see if this is valid file first
00171         this->ParseRobotFile( "..\\robots\\threeCylinderRobot.txt" );
00172   //## end ServerBase::R_ThreeCylinderRobot%932600045.body
00173 }
00174 
00175 bool ServerBase::ParseRobotFile (const char* filename)
00176 {
00177   //## begin ServerBase::ParseRobotFile%935034310.body preserve=yes
00178         if( strcmp( filename, "NONE" ) == 0 )
00179         {
00180                 return true;
00181         }
00182 
00183     char file[ _MAX_PATH ] = "";
00184     IJG_Assert( strcmp( rootpath, "NONE" ) != 0 );
00185     //strcat( file, rootpath );
00186     //strcat( file, filename );
00187         GetAbsoluteFileName(file, _MAX_PATH, rootpath, filename);
00188         IfstreamWithComments is;
00189         is.open( file );
00190         if( is.is_open() == false )
00191         {
00192                 IJG_AssertMsg( false, "Could not open file" );
00193                 return false;
00194         }
00195 
00196         char line[ 1000 ] = "";
00197 
00198         //here comes the robot part
00199         bool success = true;
00200         FrameManager* theFrameManager = this->universe.GetFrameManager();
00201         RobotBase* robot = RobotBase::DeserializeAbstract( is, theFrameManager );
00202         if( robot != NULL )
00203         {
00204                 universe.AddEntity( robot );
00205                 delete robot;
00206         }
00207         else
00208         {
00209                 success = false;
00210         }
00211 
00212         is.close();
00213 
00214         strcpy( this->filename, filename ); 
00215         this->RefreshCollisionDetector();
00216         return success;
00217   //## end ServerBase::ParseRobotFile%935034310.body
00218 }
00219 
00220 void ServerBase::ParseRobotOpenChain (IfstreamWithComments& is)
00221 {
00222   //## begin ServerBase::ParseRobotOpenChain%935084508.body preserve=yes
00223         assert( false ); //this should never be called
00224   //## end ServerBase::ParseRobotOpenChain%935084508.body
00225 }
00226 
00227 bool ServerBase::ParseObstacleFile (const char* filename)
00228 {
00229   //## begin ServerBase::ParseObstacleFile%936229638.body preserve=yes
00230         if( stricmp( filename, "none" ) == 0 )
00231         {
00232                 return false;
00233         }
00234 
00235         strcpy( this->obstacleFilename, filename );
00236         VRML_20_Reader reader;
00237         char newFilename[ 1000 ] = "";
00238         //strcpy( newFilename, filename );
00239         //MPKstring::TrimLeadingWhitespace( newFilename );
00240         GetAbsoluteFileName(newFilename, _MAX_PATH, rootpath, filename);
00241         reader.SetFilename( newFilename );
00242         
00243         bool success = reader.Read();
00244         if( success )
00245         {
00246                 //ObjectBase* environment = dynamic_cast< ObjectBase* >( reader.GetObjectA()->Clone() );
00247                 ObjectBase* environment = dynamic_cast< ObjectBase* >( reader.GetTheObject()->Clone() );
00248                 universe.AddEntity( environment );
00249                 delete environment;
00250                 environment = NULL;
00251         }
00252         return success;
00253   //## end ServerBase::ParseObstacleFile%936229638.body
00254 }
00255 
00256 bool ServerBase::SetCollisionDetector (CollisionDetectorType collisionDetectorType)
00257 {
00258   //## begin ServerBase::SetCollisionDetector%950474924.body preserve=yes
00259         //update the collision detector only if we need to
00260         if( this->collisionDetectorType != collisionDetectorType )
00261         {
00262                 this->collisionDetectorType = collisionDetectorType;
00263                 this->RefreshCollisionDetector();
00264         }
00265         return true;
00266   //## end ServerBase::SetCollisionDetector%950474924.body
00267 }
00268 
00269 bool ServerBase::RefreshCollisionDetector ()
00270 {
00271   //## begin ServerBase::RefreshCollisionDetector%950474925.body preserve=yes
00272         delete collisionDetector;
00273         collisionDetector = NULL;
00274         GL_Universe& mpkUniverse = universe;
00275         switch( collisionDetectorType )
00276         {
00277                 case CD_SIMPLE :
00278                 {
00279                         collisionDetector = new CD_Simple( mpkUniverse );
00280                         break;
00281                 }
00282                 case CD_VCOLLIDE :
00283                 {
00284                         collisionDetector = new CD_Vcollide( mpkUniverse );
00285 
00286             // this is to test link scaling
00287             RobotBase* robot = collisionDetector->GetRobot( 0 );
00288             if( robot != NULL )
00289             {
00290                 LinkBase* link = robot->GetLink( 0 );
00291             }
00292                         break;
00293                 }
00294                 case CD_SOLID :
00295                 {
00296                         collisionDetector = new CD_Solid( mpkUniverse );
00297                         break;
00298                 }
00299                 case CD_ICOLLIDE :
00300                 {
00301                         collisionDetector = new CD_Icollide( mpkUniverse );
00302                         break;
00303                 }
00304                 case CD_RANGE_SENSOR :
00305                 {
00306                         collisionDetector = new CD_Range_Sensor( mpkUniverse );
00307                         break;
00308                 }
00309                 case CD_SWIFTPP :
00310                 {
00311                         collisionDetector = new CD_Swiftpp( mpkUniverse );
00312 
00313             // this is to test link scaling
00314             //RobotBase* robot = collisionDetector->GetRobot( 0 );
00315             //if( robot != NULL )
00316             //{
00317             //    LinkBase* link = robot->GetLink( 0 );
00318             //}
00319                         break;
00320                 }
00321                 default :
00322                 {
00323                         bool noCollisionDetectorTypeSpecified = false;
00324 //                      assert( noCollisionDetectorTypeSpecified );
00325                         break;
00326                 }
00327         }
00328 
00329         this->RefreshPlanner();
00330 
00331         if( collisionDetector == NULL )
00332         {
00333                 return false;
00334         }
00335         else
00336         {
00337                 return true;
00338         }
00339   //## end ServerBase::RefreshCollisionDetector%950474925.body
00340 }
00341 
00342 bool ServerBase::RefreshRobotFile ()
00343 {
00344   //## begin ServerBase::RefreshRobotFile%950480128.body preserve=yes
00345         universe.Empty();
00346         return ParseRobotFile( filename );
00347   //## end ServerBase::RefreshRobotFile%950480128.body
00348 }
00349 
00350 const RobotBase* ServerBase::GetRobot (const char* const filename) const
00351 {
00352   //## begin ServerBase::GetRobot%950480129.body preserve=yes
00353         const Entity* entity = universe.GetEntity( filename );  //IMPROVE: it's not a filename, its a robot name
00354         const RobotBase* returnMe = dynamic_cast< const RobotBase* >( entity );
00355         return( returnMe );     //this will be null if the name is bad or the robot is not found
00356   //## end ServerBase::GetRobot%950480129.body
00357 }
00358 
00359 const ObjectBase* ServerBase::GetMPKObject (const char* const filename) const
00360 {
00361   //## begin ServerBase::GetMPKObject%950480130.body preserve=yes
00362         assert( false );
00363         return( NULL );
00364   //## end ServerBase::GetMPKObject%950480130.body
00365 }
00366 
00367 const Frame ServerBase::GetFrame (const char* const name) const
00368 {
00369   //## begin ServerBase::GetFrame%950480131.body preserve=yes
00370         assert( false );
00371         Frame returnMe;
00372         return( returnMe );
00373   //## end ServerBase::GetFrame%950480131.body
00374 }
00375 
00376 bool ServerBase::SetPlanner (PlannerType plannerType)
00377 {
00378   //## begin ServerBase::SetPlanner%958847982.body preserve=yes
00379 
00380         //if the planner has changed then update it
00381         if( this->plannerType != plannerType )
00382         {
00383                 this->plannerType = plannerType;
00384                 bool success = this->RefreshPlanner();
00385                 return success;
00386         }
00387         return true;
00388   //## end ServerBase::SetPlanner%958847982.body
00389 }
00390 
00391 bool ServerBase::RefreshPlanner ()
00392 {
00393         collisionDetector->ResetStats();
00394         switch( this->plannerType )
00395         {
00396         case PLANNER_LINEAR:
00397                 {
00398                         PL_Linear* tempPlanner = new( PL_Linear );
00399                         //IMPROVE: does this cause a memory leak?
00400                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );
00401                         if( cd != NULL )
00402                         {
00403                                 tempPlanner->SetCollisionDetector( cd );        //IMPROVE: do the dynamic casting inside the function
00404                                 delete planner;
00405                                 planner = tempPlanner;
00406                         }
00407                         else
00408                         {
00409                                 delete tempPlanner;
00410                                 delete planner;
00411                                 planner = NULL;
00412                                 return false;
00413                         }
00414                         break;
00415                 };
00416         case PLANNER_SEQUENTIAL:
00417                 {
00418                         PL_Sequential* tempPlanner = new( PL_Sequential );
00419                         tempPlanner->initialize_parameters();
00420                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );
00421                         if( cd != NULL )
00422                         {
00423                                 tempPlanner->SetCollisionDetector( cd );
00424                                 delete planner;
00425                                 planner = tempPlanner;
00426                         }
00427                         else
00428                         {
00429                                 delete tempPlanner;
00430                                 delete planner;
00431                                 planner = NULL;
00432                                 return false;
00433                         }
00434                         break;
00435                 }
00436         case PLANNER_ACA:
00437                 {
00438                         PL_Juan* tempPlanner = new( PL_Juan );
00439                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );
00440                         if( cd != NULL )
00441                         {
00442                                 tempPlanner->SetCollisionDetector( cd );
00443                                 delete planner;
00444                                 planner = tempPlanner;
00445                         }
00446                         else
00447                         {
00448                                 delete tempPlanner;
00449                                 delete planner;
00450                                 planner = NULL;
00451                                 return false;
00452                         }
00453                         break;
00454                 }
00455         case PLANNER_RANDOM:
00456                 {
00457                         PL_IanRandom* tempPlanner = new( PL_IanRandom );
00458                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );
00459                         if( cd != NULL )
00460                         {
00461                                 tempPlanner->SetCollisionDetector( cd );
00462                                 delete planner;
00463                                 planner = tempPlanner;
00464                         }
00465                         else
00466                         {
00467                                 delete tempPlanner;
00468                                 delete planner;
00469                                 planner = NULL;
00470                                 return false ;
00471                         }
00472                         break;
00473                 }
00474         case PLANNER_JONATHAN:
00475                 {
00476                         PL_Range_Sensor* tempPlanner = new( PL_Range_Sensor );
00477                         CD_Range_Sensor* cd = dynamic_cast< CD_Range_Sensor* >( collisionDetector );
00478                         if( cd != NULL )
00479                         {
00480                                 tempPlanner->SetCollisionDetector( cd );
00481                                 delete planner;
00482                                 planner = tempPlanner;
00483                         }
00484                         else
00485                         {
00486                                 delete tempPlanner;
00487                                 delete planner;
00488                                 planner = NULL;
00489                                 return false;
00490                         }
00491                         break;
00492                 }
00493         case PLANNER_SIMPLEX_SUBDIVISION:
00494                 {
00495                         PL_SimplexSubdivision* tempPlanner = new( PL_SimplexSubdivision );
00496                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );        //IAN IMPROVE: fix the collision detector hierarchy so that things properly derive from linear, etc
00497                         if( cd != NULL )
00498                         {
00499                                 tempPlanner->SetCollisionDetector( cd );
00500                                 delete planner;
00501                                 planner = tempPlanner;
00502                         }
00503                         else
00504                         {
00505                                 delete tempPlanner;
00506                                 delete planner;
00507                                 planner = NULL;
00508                                 return false;
00509                         }
00510                         break;
00511                 }
00512 
00513         case PLANNER_PRM:
00514                 {
00515                         PL_PRM* tempPlanner = new( PL_PRM );
00516                         tempPlanner->CopySettings( planner );
00517 
00518                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );        //IAN IMPROVE: fix the collision detector hierarchy so that things properly derive from linear, etc
00519                         if( cd != NULL )
00520                         {
00521                                 // Assign the Collision Detector
00522                                 tempPlanner->SetCollisionDetector( cd );
00523 
00524                                 delete planner;
00525                                 planner = tempPlanner;
00526                         }
00527                         else
00528                         {
00529                                 delete tempPlanner;
00530                                 delete planner;
00531                                 planner = NULL;
00532                                 return false;
00533                         }
00534                         break;
00535                 }
00536         case PLANNER_PRM_CLOSEDCHAIN:
00537                 {
00538                         PL_PRM_ClosedChain* tempPlanner = new( PL_PRM_ClosedChain );
00539                         tempPlanner->CopySettings( planner );
00540 
00541                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );        //IAN IMPROVE: fix the collision detector hierarchy so that things properly derive from linear, etc
00542                         if( cd != NULL )
00543                         {
00544                                 // Assign the Collision Detector
00545                                 tempPlanner->SetCollisionDetector( cd );
00546 
00547                                 delete planner;
00548                                 planner = tempPlanner;
00549                         }
00550                         else
00551                         {
00552                                 delete tempPlanner;
00553                                 delete planner;
00554                                 planner = NULL;
00555                                 return false;
00556                         }
00557                         break;
00558                 }
00559         case PLANNER_RRT_CLOSEDCHAIN:
00560                 {
00561                         PL_RRT_ClosedChain* tempPlanner = new( PL_RRT_ClosedChain );
00562                         tempPlanner->CopySettings( planner );
00563 
00564                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );        //IAN IMPROVE: fix the collision detector hierarchy so that things properly derive from linear, etc
00565                         if( cd != NULL )
00566                         {
00567                                 // Assign the Collision Detector
00568                                 tempPlanner->SetCollisionDetector( cd );
00569 
00570                                 delete planner;
00571                                 planner = tempPlanner;
00572                         }
00573                         else
00574                         {
00575                                 delete tempPlanner;
00576                                 delete planner;
00577                                 planner = NULL;
00578                                 return false;
00579                         }
00580                         break;
00581                 }
00582         case PLANNER_RGD_CONSTRAINT:
00583                 {
00584                         PL_RGD_Constraint* tempPlanner = new( PL_RGD_Constraint );
00585                         tempPlanner->CopySettings( planner );
00586 
00587                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );        //IAN IMPROVE: fix the collision detector hierarchy so that things properly derive from linear, etc
00588                         if( cd != NULL )
00589                         {
00590                                 // Assign the Collision Detector
00591                                 tempPlanner->SetCollisionDetector( cd );
00592 
00593                                 delete planner;
00594                                 planner = tempPlanner;
00595                         }
00596                         else
00597                         {
00598                                 delete tempPlanner;
00599                                 delete planner;
00600                                 planner = NULL;
00601                                 return false;
00602                         }
00603                         break;
00604                 }
00605         case PLANNER_ACA_CONNECT:
00606                 {
00607                         PL_ACA_CONNECT* tempPlanner = new( PL_ACA_CONNECT );
00608                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );
00609                         if( cd != NULL )
00610                         {
00611                                 tempPlanner->SetCollisionDetector( cd );
00612                                 delete planner;
00613                                 planner = tempPlanner;
00614                         }
00615                         else
00616                         {
00617                                 delete tempPlanner;
00618                                 delete planner;
00619                                 planner = NULL;
00620                                 return false;
00621                         }
00622                         break;
00623                 }
00624         case PLANNER_RRT:
00625                 {
00626                         PL_RRT* tempPlanner = new( PL_RRT );
00627                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );
00628                         if( cd != NULL )
00629                         {
00630                                 tempPlanner->SetCollisionDetector( cd );
00631                                 delete planner;
00632                                 planner = tempPlanner;
00633                         }
00634                         else
00635                         {
00636                                 delete tempPlanner;
00637                                 delete planner;
00638                                 planner = NULL;
00639                                 return false;
00640                         }
00641                         break;
00642                 }
00643         case PLANNER_RRT_CONNECT:
00644                 {
00645                         PL_RRT_CONNECT* tempPlanner = new( PL_RRT_CONNECT);
00646                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );
00647                         if( cd != NULL )
00648                         {
00649                                 tempPlanner->SetCollisionDetector( cd );
00650                                 delete planner;
00651                                 planner = tempPlanner;
00652                         }
00653                         else
00654                         {
00655                                 delete tempPlanner;
00656                                 delete planner;
00657                                 planner = NULL;
00658                                 return false;
00659                         }
00660                         break;
00661                 }
00662         case PLANNER_PRM_IJG:
00663                 {
00664                         PL_PrmIjg* tempPlanner = new PL_PrmIjg;
00665                         //tempPlanner->CopySettings( planner );
00666 
00667                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );        //IAN IMPROVE: fix the collision detector hierarchy so that things properly derive from linear, etc
00668                         if( cd != NULL )
00669                         {
00670                                 // Assign the Collision Detector
00671                                 tempPlanner->SetCollisionDetector( cd );
00672 
00673                                 delete planner;
00674                                 planner = tempPlanner;
00675                         }
00676                         else
00677                         {
00678                                 delete tempPlanner;
00679                                 delete planner;
00680                                 planner = NULL;
00681                                 return false;
00682                         }
00683                         break;
00684                 }
00685         case PLANNER_SANDROS:
00686                 {
00687                         PL_Sandros* tempPlanner = new( PL_Sandros );
00688                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );        //IAN IMPROVE: fix the collision detector hierarchy so that things properly derive from linear, etc
00689                         if( cd != NULL )
00690                         {
00691                                 // Assign the Collision Detector
00692                                 tempPlanner->SetCollisionDetector( cd );
00693 
00694                                 delete planner;
00695                                 planner = tempPlanner;
00696                         }
00697                         else
00698                         {
00699                                 delete tempPlanner;
00700                                 delete planner;
00701                                 planner = NULL;
00702                                 return false;
00703                         }
00704                         break;
00705                 }
00706         case PLANNER_ASTAR:
00707                 {
00708                         PL_Astar* tempPlanner = new( PL_Astar );
00709                         tempPlanner->CopySettings( planner );
00710                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );        //IAN IMPROVE: fix the collision detector hierarchy so that things properly derive from linear, etc
00711                         if( cd != NULL )
00712                         {
00713                                 // Assign the Collision Detector
00714                                 tempPlanner->SetCollisionDetector( cd );
00715 
00716                                 delete planner;
00717                                 planner = tempPlanner;
00718                         }
00719                         else
00720                         {
00721                                 delete tempPlanner;
00722                                 delete planner;
00723                                 planner = NULL;
00724                                 return false;
00725                         }
00726                         break;
00727                 }
00728         case PLANNER_IK_ACA:
00729                 {
00730                         IK_ACA* tempPlanner = new( IK_ACA );
00731                         tempPlanner->CopySettings( planner );
00732                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );        //IAN IMPROVE: fix the collision detector hierarchy so that things properly derive from linear, etc
00733                         if( cd != NULL )
00734                         {
00735                                 // Assign the Collision Detector
00736                                 tempPlanner->SetCollisionDetector( cd );
00737 
00738                                 delete planner;
00739                                 planner = tempPlanner;
00740                         }
00741                         else
00742                         {
00743                                 delete tempPlanner;
00744                                 delete planner;
00745                                 planner = NULL;
00746                                 return false;
00747                         }
00748                         break;
00749                 }
00750         case PLANNER_FORTEST:
00751                 {
00752                         PL_ForTest* tempPlanner = new( PL_ForTest);
00753                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );        //IAN IMPROVE: fix the collision detector hierarchy so that things properly derive from linear, etc
00754                         if( cd != NULL )
00755                         {
00756                                 // Assign the Collision Detector
00757                                 tempPlanner->SetCollisionDetector( cd );
00758 
00759                                 delete planner;
00760                                 planner = tempPlanner;
00761                         }
00762                         else
00763                         {
00764                                 delete tempPlanner;
00765                                 delete planner;
00766                                 planner = NULL;
00767                                 return false;
00768                         }
00769                         break;
00770                 }
00771         case PLANNER_MPEP:
00772                 {
00773                         PL_MPEP* tempPlanner = new( PL_MPEP );
00774                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );        //IAN IMPROVE: fix the collision detector hierarchy so that things properly derive from linear, etc
00775                         if( cd != NULL )
00776                         {
00777                                 // Assign the Collision Detector
00778                                 tempPlanner->SetCollisionDetector( cd );
00779 
00780                                 delete planner;
00781                                 planner = tempPlanner;
00782                         }
00783                         else
00784                         {
00785                                 delete tempPlanner;
00786                                 delete planner;
00787                                 planner = NULL;
00788                                 return false;
00789                         }
00790                         break;
00791                 }
00792         case PLANNER_IK_JACOBIAN:
00793                 {
00794                         IK_Jacobian* tempPlanner = new( IK_Jacobian );
00795                         tempPlanner->CopySettings( planner );
00796                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );        //IAN IMPROVE: fix the collision detector hierarchy so that things properly derive from linear, etc
00797                         if( cd != NULL )
00798                         {
00799                                 // Assign the Collision Detector
00800                                 tempPlanner->SetCollisionDetector( cd );
00801 
00802                                 delete planner;
00803                                 planner = tempPlanner;
00804                         }
00805                         else
00806                         {
00807                                 delete tempPlanner;
00808                                 delete planner;
00809                                 planner = NULL;
00810                                 return false;
00811                         }
00812                         break;
00813                 }
00814         case PLANNER_ATACE:
00815                 {
00816                         PL_ATACE* tempPlanner = new( PL_ATACE );
00817                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );        //IAN IMPROVE: fix the collision detector hierarchy so that things properly derive from linear, etc
00818                         if( cd != NULL )
00819                         {
00820                                 // Assign the Collision Detector
00821                                 tempPlanner->SetCollisionDetector( cd );
00822 
00823                                 delete planner;
00824                                 planner = tempPlanner;
00825                         }
00826                         else
00827                         {
00828                                 delete tempPlanner;
00829                                 delete planner;
00830                                 planner = NULL;
00831                                 return false;
00832                         }
00833                         break;
00834                 }
00835         case PLANNER_NEURAL:
00836                 {
00837                         PL_Neural* tempPlanner = new PL_Neural;
00838                         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector );        //IAN IMPROVE: fix the collision detector hierarchy so that things properly derive from linear, etc
00839                         if( cd != NULL )
00840                         {
00841                                 // Assign the Collision Detector
00842                                 tempPlanner->SetCollisionDetector( cd );
00843 
00844                                 delete planner;
00845                                 planner = tempPlanner;
00846                         }
00847                         else
00848                         {
00849                                 delete tempPlanner;
00850                                 delete planner;
00851                                 planner = NULL;
00852                                 return false;
00853                         }
00854                         break;
00855                 }
00856     case PLANNER_NONE:
00857                 {
00858                         this->plannerType = PLANNER_LINEAR;
00859                         return this->RefreshPlanner();
00860                 }
00861         default:
00862                 {
00863                         KickOut();
00864                         delete planner;
00865                         planner = new PL_Linear;
00866                         planner->SetCollisionDetector( dynamic_cast< CD_BasicStyle*>( collisionDetector ) );
00867                         return false;
00868                         break;
00869                 }
00870         }
00871         planner->SetStartConfig( this->m_StartConfig );
00872         planner->SetGoalConfig( this->m_GoalConfig );
00873         RefreshSmoother();
00874         return true;
00875 }
00876 
00877 PlannerType ServerBase::GetPlannerType ()
00878 {
00879   //## begin ServerBase::GetPlannerType%958953409.body preserve=yes
00880         return this->plannerType;
00881   //## end ServerBase::GetPlannerType%958953409.body
00882 }
00883 
00884 bool ServerBase::SetSmoother (SmootherType smootherType)
00885 {
00886         //if the planner has changed then update it
00887         if( this->smootherType != smootherType )
00888         {
00889                 this->smootherType = smootherType;
00890                 bool success = this->RefreshSmoother();
00891                 return success;
00892         }
00893         return true;
00894 }
00895 
00896 SmootherType ServerBase::GetSmootherType ()
00897 {
00898         return this->smootherType;
00899 }
00900 
00901 //=============================================================================
00902 // RefreshSmoother
00903 //
00904 // Description: sets the smoothing algorithm that the planner will use
00905 //=============================================================================
00906 bool ServerBase::RefreshSmoother()
00907 {       
00908         switch( this->smootherType )
00909         {
00910                 case SMOOTHER_NONE :
00911                 {
00912                         if (smoother != NULL)
00913                                 delete smoother;
00914                         smoother = NULL;
00915                         break;
00916                 }
00917                 case SMOOTHER_SUCCESSIVE_NODE_PAIR :
00918                 {
00919                         SM_PathSmoothingSuccessiveNodePair* tempSmoother = new( SM_PathSmoothingSuccessiveNodePair );
00920                         if (smoother != NULL)
00921                                 delete smoother;
00922                         smoother = tempSmoother;
00923                         break;
00924                 }
00925                 case SMOOTHER_RANDOM_NODE_PAIR :
00926                 {
00927                         SM_PathSmoothingRandomNodePair* tempSmoother = new( SM_PathSmoothingRandomNodePair );
00928                         if (smoother != NULL)
00929                                 delete smoother;
00930                         smoother = tempSmoother;
00931                         break;
00932                 }
00933                 case SMOOTHER_SUCCESSIVE_AND_RANDOM:
00934                 {                       
00935                         SM_SuccessiveAndRandomSmoothing* tempSmoother = new ( SM_SuccessiveAndRandomSmoothing );
00936                         if (smoother != NULL)
00937                                 delete smoother;
00938                         smoother = tempSmoother;
00939                         break;
00940                 }
00941                 case SMOOTHER_TRISECTION :
00942                 {
00943                         SM_Trisection* tempSmoother = new ( SM_Trisection );
00944                         if (smoother != NULL)
00945                                 delete smoother;
00946                         smoother = tempSmoother;
00947                         break;
00948                 }
00949                 default :
00950                         break;
00951         }
00952 
00953         PL_Boolean_Output* booleanPlanner = dynamic_cast< PL_Boolean_Output* >( planner );
00954         if( booleanPlanner != NULL )
00955         {
00956                 booleanPlanner->SetSmoother( smoother );
00957                 return true;
00958         }
00959         else
00960                 return false;
00961 }
00962 
00963 CollisionDetectorType ServerBase::GetCollisionDetectorType ()
00964 {
00965   //## begin ServerBase::GetCollisionDetectorType%958953410.body preserve=yes
00966         return this->collisionDetectorType;
00967   //## end ServerBase::GetCollisionDetectorType%958953410.body
00968 }
00969 
00970 // Additional Declarations
00971 
00972 //=============================================================================
00973 // OpenExampleFile
00974 //
00975 // comment - opens up a complete example
00976 //=============================================================================
00977 bool ServerBase::OpenExampleFile( const char* filename )
00978 {
00979         //
00980         // Change directories to the path of the example file
00981         //
00982         char drive[ 256 ] = "";
00983         char dir[ 256 ] = "";
00984         char fname[ 256 ] = "";
00985         char ext[ 256 ] = "";
00986     //assert( false );
00987         //::_splitpath( filename, drive, dir, fname, ext );
00988         char rootDirectory[ 256 ] = "";
00989         strcat( rootDirectory, drive );
00990         strcat( rootDirectory, dir );
00991         if( strcmp( rootDirectory, "" ) == 0 )
00992         {
00993                 strcat( rootDirectory, "." );
00994         }
00995 
00996 //  int cdSuccess = ::chdir( rootDirectory );
00997 //      assert( cdSuccess == 0 );
00998 
00999 #ifdef _DEBUG
01000 #ifdef WIN32
01001         char currentDirectory[ 256 ] = "";
01002         ::_getcwd( currentDirectory, 256 );
01003 #endif //WIN32
01004 #endif //_DEBUG
01005 
01006         char totalFileName[ 256 ] = "";
01007         strcat( totalFileName, fname );
01008         strcat( totalFileName, ext );
01009         IfstreamWithComments infile;
01010         infile.open( filename );
01011         IJG_Assert( infile.good() );
01012 
01013         //read the file header
01014         char header[ 300 ] = "";
01015         infile.getline( header, 300 );
01016         assert( strcmp( header, "#MPK save file version 0.1" ) == 0 );
01017 
01018         //read start, goal and current configurations
01019         char marker[ 300 ] = "";
01020         eatwhite( infile );
01021         infile.getline( marker, 300, ' ' );
01022         assert( strcmp( marker, "#start_config" ) == 0 );
01023         infile >> m_StartConfig;
01024 #ifdef _DEBUG
01025         int size;
01026         size = m_StartConfig.DOF();
01027         assert( size != 0 );
01028 #endif
01029 
01030         eatwhite( infile );
01031         infile.getline( marker, 300, ' ' );
01032         assert( strcmp( marker, "#goal_config" ) == 0 );
01033         infile >> m_GoalConfig;
01034 #ifdef _DEBUG
01035         size = m_GoalConfig.DOF();
01036         assert( size != 0 );
01037 #endif
01038 
01039 
01040         eatwhite( infile );
01041         infile.getline( marker, 300, ' ' );
01042         assert( strcmp( marker, "#current_config" ) == 0 );
01043         infile >> m_CurrentConfig;
01044 
01045         //read the camera positions
01046         eatwhite( infile );
01047         infile.getline( marker, 300 );
01048         assert( strcmp( marker, "#camera" ) == 0 );
01049         infile >> m_CameraPosition;
01050         infile >> m_CameraLookat;
01051         infile >> m_CameraUp;
01052 
01053         //read the camera positions
01054         eatwhite( infile );
01055         infile.getline( marker, 300 );
01056     MPKstring::TrimTrailingWhitespace( marker );
01057     if( strcmp( marker, "#cspacecamera" ) == 0 )
01058     {
01059         ParseCspaceCamera( infile );
01060         eatwhite( infile );
01061             infile.getline( marker, 300 );
01062         MPKstring::TrimTrailingWhitespace( marker );
01063     }
01064 
01065         //read the planner type
01066         IJG_Assert( strcmp( marker, "#planner_type" ) == 0 );
01067         int plType = -1;
01068         infile >> plType;
01069 
01070         //read the collision detector type
01071         eatwhite( infile );
01072         infile.getline( marker, 300, ' ' );
01073         IJG_Assert( strcmp( marker, "#collisionDetectorType" ) == 0 );
01074         int cdType = -1;
01075         infile >> cdType;
01076         
01077         //input the filename
01078         eatwhite( infile );
01079         infile.getline( marker, 300 );
01080         IJG_Assert( stricmp( marker, "#robotfile" ) == 0 );
01081         infile.getline( marker, 300 );
01082         this->universe.Empty();
01083         bool success = this->ParseRobotFile( marker );
01084         if( success == false )
01085         {
01086                 //Could not open robot file
01087                 IJG_Assert( false );
01088                 //MessageBox( NULL, "Could not open robot file", "ERROR", MB_OK );
01089                 return false;
01090         }
01091 
01092         //input the obstacle filename
01093         eatwhite( infile );
01094         infile.getline( marker, 300 );
01095         IJG_Assert( stricmp( marker, "#obstacle" ) == 0 );
01096         infile.getline( marker, 300 );
01097         this->ParseObstacleFile( marker );
01098 
01099         //refresh the collision detector
01100         SetCollisionDetector( static_cast< CollisionDetectorType >( cdType ) );
01101         RefreshCollisionDetector();
01102 
01103         //this must be done after the collision detector is opened
01104         SetPlanner( static_cast< PlannerType >( plType ) );
01105         planner->SetStartConfig( this->m_StartConfig );
01106         planner->SetGoalConfig( this->m_GoalConfig );
01107 
01108         //apply the planner type 
01109         bool pSuccess = this->planner->Load( filename );
01110         if( pSuccess == false )
01111         {
01112                 this->RefreshPlanner();
01113         }
01114 
01115         //read the next marker - goalframes
01116         eatwhite( infile );
01117         infile.getline( marker, 300 );
01118     if( stricmp( marker, "#goalFrames") == 0 )
01119     {
01120         ParseGoalFrames( infile );
01121             eatwhite( infile );
01122             infile.getline( marker, 300 );
01123     }
01124 
01125         IJG_Assert( strcmp( marker, "#path" ) == 0 );
01126         infile >> m_Path;
01127 
01128         return true;
01129 }
01130 
01131 bool ServerBase::SaveExampleFile( const char* filename )
01132 {
01133         std::ofstream outfile( filename, std::ios::out );
01134         if( outfile.good() == false )
01135         {
01136                 return false;
01137         }
01138 
01139         //output the file header
01140         outfile << "#MPK save file version 0.1" << endl;
01141 
01142         //output start, goal and current configurations
01143         outfile << "#start_config "     << m_StartConfig        << endl;
01144         outfile << "#goal_config "      << m_GoalConfig << endl;
01145         outfile << "#current_config " << m_CurrentConfig << endl;
01146 
01147         //output camera values
01148         //need to get the first view
01149         outfile << "#camera" << endl;
01150         outfile << m_CameraPosition << endl;
01151         outfile << m_CameraLookat   << endl;
01152         outfile << m_CameraUp       << endl;
01153 
01154         outfile << "#cspacecamera" << endl;
01155         outfile << m_CspaceCameraPosition << endl;
01156         outfile << m_CspaceCameraLookat   << endl;
01157         outfile << m_CspaceCameraUp       << endl;
01158 
01159         //output the planner type
01160         outfile << "#planner_type " << endl << plannerType << endl;
01161 
01162         //output the collision detector type
01163         outfile << "#collisionDetectorType " << endl << collisionDetectorType << endl;
01164         planner->Save( filename );
01165 
01166         //output the robot filename
01167         outfile << "#robotfile" << endl;
01168         outfile << this->filename << endl;
01169 
01170         //output the obstacle filename
01171         outfile << "#obstacle" << endl;
01172         outfile << obstacleFilename << endl;
01173 
01174         //output the current path
01175         outfile << "#path" << endl;
01176         outfile << m_Path << endl;
01177         
01178         return true;
01179 }
01180 
01181 const char* ServerBase::GetRobotFilename() const
01182 {
01183         return filename;
01184 };
01185   
01186 const char* ServerBase::GetObstacleFilename() const
01187 {
01188         return obstacleFilename;
01189 }
01190 
01191 //=============================================================================
01192 // ServerBase::ParseCspaceCamera
01193 //=============================================================================
01194 void ServerBase::ParseCspaceCamera( IfstreamWithComments& is )
01195 {
01196         is >> m_CspaceCameraPosition;
01197         is >> m_CspaceCameraLookat;
01198         is >> m_CspaceCameraUp;
01199 }
01200 
01201 //=============================================================================
01202 // ServerBase::ParseGoalFrames
01203 //=============================================================================
01204 void ServerBase::ParseGoalFrames( IfstreamWithComments& is )
01205 {
01206         int numFrames;
01207         is >> numFrames;
01208         IJG_Assert( numFrames == 0 );
01209     int frameCount;
01210     for( frameCount = 0; frameCount < numFrames; frameCount++ )
01211     {
01212             VectorN xyzrpy;
01213             is >> xyzrpy;
01214             IK_InvKinBase* ik = dynamic_cast< IK_InvKinBase* >( planner );
01215             if( NULL != ik )
01216             {
01217                     ik->SetGoalFrame
01218                     ( 
01219                             xyzrpy[ 0 ],
01220                             xyzrpy[ 1 ],
01221                             xyzrpy[ 2 ],
01222                             xyzrpy[ 3 ],
01223                             xyzrpy[ 4 ],
01224                             xyzrpy[ 5 ]
01225                     );
01226             }
01227     }
01228 }
01229 
01230 //sets the root path for loading files
01231 void ServerBase::SetRootPath( const char* path )
01232 {
01233     //VRML_20_Reader::SetRootPath( path );
01234     ::strcpy( rootpath, path );
01235 }
01236 
01237 void ServerBase::SetMPKWorkingDirectory( const char* path )
01238 {
01239     //VRML_20_Reader::SetRootPath( path );
01240         char mpkpath[_MAX_PATH];
01241         strncpy(mpkpath, path, _MAX_PATH);
01242 
01243         int pathlen = strlen(mpkpath);
01244         if ((pathlen > 0) && (pathlen<(_MAX_PATH-1)))
01245         {
01246                 if (mpkpath[pathlen-1] != '\\')
01247                 {
01248                         strcat(mpkpath, "\\");
01249                 }
01250 
01251         }
01252 
01253     ::SetMPKWorkingDirectory( mpkpath );
01254 }
01255 
01256 bool ServerBase::AddObstacle( double x, double y )
01257 {
01258         char newFilename[ 1000 ] = "";
01259         GetAbsoluteFileName(newFilename, _MAX_PATH, GetMPKWorkingDirectory(), "vrml models\\cylinder.wrl");
01260 
01261         VRML_20_Reader reader;
01262         reader.SetFilename( newFilename );
01263         reader.Read();
01264         const ObjectBase* theObject = reader.GetTheObject();
01265         assert( theObject != NULL );
01266         if( theObject == NULL )
01267         {
01268                 assert( false );
01269                 return false;
01270         }
01271         ObjectBase* obstacle = dynamic_cast< ObjectBase* >( theObject->Clone() );
01272         assert( obstacle != NULL );
01273         obstacle->SetBaseFrame( 0 );
01274         Matrix4x4 transform;
01275         transform.Translate( x, y, 0 );
01276         obstacle->SetFrame( transform );
01277         universe.AddEntity( obstacle );
01278         delete obstacle;
01279         //RefreshCollisionDetector();
01280         userObstacles.push_back(transform);
01281         return true;
01282 }
01283 
01284 bool ServerBase::SaveUserObstacle( const char *obsfile )
01285 {
01286         std::ofstream outfile( obsfile, std::ios::out );
01287         if( outfile.good() == false )
01288         {
01289                 return false;
01290         }
01291 
01292         //output the file header
01293         outfile << "User-added obstacles" << endl;
01294         outfile << userObstacles.size() <<endl;
01295 
01296         //output the transofmration of the obstacles
01297         for (int k=0; k<userObstacles.size(); k++)
01298         {
01299                 Matrix4x4 obsPos = userObstacles[k];
01300                 outfile << obsPos;
01301         }
01302         return true;
01303 }
01304 
01305 bool ServerBase::LoadUserObstacle( const char *obsfile )
01306 {
01307         std::ifstream infile( obsfile, std::ios::in );
01308         if( infile.good() == false )
01309         {
01310                 return false;
01311         }
01312 
01313         //input the file header
01314         char marker[100] = "";
01315         infile.getline(marker, 100);
01316         int numObstacles;
01317         infile >> numObstacles;
01318 
01319         /*
01320         //check if the robot is the sphere/stick one
01321         GL_Universe& mpkUniverse = server->universe;
01322         char currentPath[_MAX_PATH];
01323         char fullFileName[_MAX_PATH];
01324         _getcwd(currentPath, _MAX_PATH);
01325         _makepath(fullFileName, NULL, currentPath, "vrml models\\cylinder.wrl", NULL);
01326         VRML_20_Reader reader;
01327         reader.SetFilename( fullFileName  );
01328         reader.Read();
01329         const ObjectBase* theObject = reader.GetTheObject();
01330         assert( theObject != NULL );
01331         if( theObject == NULL )
01332         {
01333                 MessageBox( NULL, "Could not open filename specified", "ERROR", MB_OK );
01334                 return;
01335         }
01336 
01337         ObjectBase* obstacle = dynamic_cast< ObjectBase* >( theObject->Clone() );
01338         assert( obstacle != NULL );
01339         obstacle->SetBaseFrame( 0 );
01340         //Matrix4x4 transform;
01341         //transform.Translate( x, y, 0 );
01342         //obstacle->SetFrame( transform );
01343         */
01344 
01345         //input the transofmration of the obstacles
01346         for (int k=0; k< numObstacles; k++)
01347         {
01348                 Matrix4x4 obsPos;
01349                 infile >> obsPos;
01350                 //obstacle->SetFrame(obsPos);
01351                 AddObstacle(obsPos(0, 3), obsPos(1, 3));
01352                 //mpkUniverse.AddEntity( obstacle );
01353                 //userObstacles.push_back(obsPos);
01354                 //obstacle->SetFrame(obsPos.Inverse());
01355         }
01356 
01357         //RefreshCollisionDetector();
01358         return true;
01359 }
01360 
01361 //## end ServerBase%37AB2C4702C6.declarations
01362 //## begin module%37AB2C4702C6.epilog preserve=yes
01363 //## end module%37AB2C4702C6.epilog

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