00001 #include <assert.h>
00002 #include "synchronization/semaphore.h"
00003 #include "PL_ACA_CONNECT.h"
00004
00005 #include "jma_switch.h"
00006 #include "jma_aca_macros.h"
00007 #include <iosfwd>
00008 #include <iostream>
00009 #include "kinematics/configuration.h"
00010 #include "opengl/glos.h"
00011 #include <gl/gl.h>
00012
00013 #define THICKNESS 3
00014 #define WIN_WIDTH 1000
00015 #define WIN_HEIGHT 500
00016 #define EPSILON 1.0e-3
00017 #define NUMBER_OF_EMBRYOS MAX_EMBRYOS
00018 #define BIG_NUMBER 1.0e+37
00019
00020
00021 #define THICKNESS 3
00022 #define WIN_WIDTH 1000
00023 #define WIN_HEIGHT 500
00024 #define EPSILON 1.0e-3
00025 #define BIG_NUMBER 1.0e+37
00026
00027 PL_ACA_CONNECT::~PL_ACA_CONNECT()
00028 {
00029
00030 }
00031
00032 bool PL_ACA_CONNECT::Plan ()
00033 {
00034 PlannerBase::StartTimer() ;
00035 path.Clear() ;
00036 if( collisionDetector->IsInterfering( GetStartConfig() ) )
00037 {
00038 path.AppendPoint( GetGoalConfig() ) ;
00039 path.AppendPoint( GetGoalConfig() ) ;
00040 return false ;
00041 }
00042 if( collisionDetector->IsInterfering( GetGoalConfig() ) )
00043 {
00044 path.AppendPoint( GetStartConfig() ) ;
00045 path.AppendPoint( GetStartConfig() ) ;
00046 return false ;
00047 }
00048
00049 Semaphore semaphore( guid );
00050 semaphore.Lock();
00051 SetStart_and_GoalConfig (GetStartConfig(), GetGoalConfig());
00052
00053 bool path_found=false;
00054 pointer=1;
00055 JMA_Roadmap_Tree *temp_tree, *temp_tree1, *temp_tree2;
00056
00057 temp_tree1 = &tree1;
00058 temp_tree2 = &tree2;
00059
00060
00061 for(int i=1; i<MAX_LANDMARKS;i++)
00062 {
00063 if (!Search(temp_tree1,temp_tree2))
00064 {
00065 if( HasTimeLimitExpired() == true )
00066 {
00067 return false ;
00068 }
00069
00070 advance(temp_tree1,temp_tree2);
00071
00072
00073 temp_tree=temp_tree1;
00074 temp_tree1=temp_tree2;
00075 temp_tree2=temp_tree;
00076 pointer=pointer+1;
00077
00078
00079 Explore(temp_tree1);
00080 semaphore.Unlock();
00081 semaphore.Lock();
00082 }
00083 else
00084 {
00085 path_found=true;
00086 break;
00087 }
00088
00089 }
00090
00091 semaphore.Unlock();
00092
00093 if ((!(path_found))||( (tree1.n_nodes +tree2.n_nodes)> MAX_LANDMARKS ))
00094 return false ;
00095
00096 double elapsedTime = timer.ElapsedTime();
00097 int mmmmm=tree1.n_nodes +tree2.n_nodes;
00098 int m1=tree1.n_nodes;
00099 int m2=tree2.n_nodes;
00100
00101 if (path_found)
00102 {
00103 GetIdsPath(&tree1, &tree2);
00104
00105 for( int i = 0; i < mark; i++ )
00106 {
00107 int pointIndex = ids_list[ i ] ;
00108 Configuration point = tree1.node[ pointIndex ].landmark.dof ;
00109 path.AppendPoint( point ) ;
00110 }
00111 for(i = mark; i <= end_of_array ; i++ )
00112 {
00113 int pointIndex = ids_list[ i ] ;
00114 Configuration point = tree2.node[ pointIndex ].landmark.dof ;
00115 path.AppendPoint( point ) ;
00116 }
00117 }
00118 else
00119 {
00120 if( path.Size() == 0 )
00121 {
00122 path.AppendPoint( GetStartConfig() ) ;
00123 }
00124 return false ;
00125 }
00126
00127
00128 PA_Points the_path;
00129 int size=path.Size();
00130 the_path.AppendPoint(path[0]);
00131 Configuration p1 = path[ 0 ] ;
00132 int n=size-1;
00133 int indicater=0;
00134 while (indicater!=size-1)
00135 {
00136 Configuration p2 = path[n];
00137 while (collisionDetector->IsInterferingLinear( p1, p2 ))
00138 {
00139 n=n-1;
00140 if (n==indicater)
00141 assert(false);
00142 p2=path[n];
00143 }
00144 the_path.AppendPoint (p2);
00145 p1=p2;
00146 indicater=n;
00147 n=size-1;
00148 }
00149 path.Clear();
00150 for (n=0;n<the_path.Size();n++)
00151 path.AppendPoint(the_path[n]);
00152 the_path.Clear();
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 return true ;
00167 }
00168
00169 double PL_ACA_CONNECT::get_distance(const Configuration& conf1, const Configuration& conf2) const
00170 {
00171 return collisionDetector->DistanceBetween( conf1, conf2 ) ;
00172 }
00173 void PL_ACA_CONNECT::MyRandomPath (int id_landmark, const int id_embryo, const int n_dof, const JMA_Configuration init_conf, JMA_Configuration* embryo) const
00174 {
00175 Configuration config= GetStartConfig() ;
00176
00177
00178 Configuration current = init_conf.dof ;
00179 do
00180 {
00181 for( int j = 0; j < config.DOF(); j++ )
00182 {
00183 double jointMax = collisionDetector->JointMax( j ) ;
00184 double jointMin = collisionDetector->JointMin( j ) ;
00185 double random01 = (double) switch_random()/BIGEST_RND_NUMBER ;
00186 double jointVal = jointMin + random01 * ( jointMax - jointMin );
00187 config[ j ] = jointVal ;
00188 }
00189 } while((collisionDetector->IsInterfering(config)) || (collisionDetector->IsInterferingLinear(current, config)));
00190 (*embryo).dof = config ;
00191 }
00192
00193 int PL_ACA_CONNECT::GetNAncestors (const JMA_Roadmap_Tree* tree, const int landmark_id)
00194 {
00195 int counter;
00196 int id;
00197
00198 id = landmark_id;
00199 counter = 0;
00200
00201 while(id != 0)
00202 {
00203 ++counter;
00204 id = (*tree).node[id].father_id;
00205 }
00206 return ++counter;
00207 }
00208
00209
00210 bool PL_ACA_CONNECT::Search (JMA_Roadmap_Tree* temp_tree1, JMA_Roadmap_Tree* temp_tree2)
00211 {
00212 int numberOfNodes = (*temp_tree1).n_nodes;
00213 current_node = (*temp_tree1).node[ numberOfNodes - 1 ].landmark.dof;
00214
00215 get_nearest_node(¤t_node, temp_tree2, &nearest_node);
00216 if( collisionDetector->IsInterferingLinear(current_node, nearest_node) )
00217 {
00218 return false ;
00219 }
00220 else
00221 {
00222 (*temp_tree1).node[(*temp_tree1).n_nodes].landmark.dof=nearest_node;
00223 (*temp_tree1).node[(*temp_tree1).n_nodes].father_id=(*temp_tree1).n_nodes-1;
00224 ++(*temp_tree1).n_nodes;
00225 return true ;
00226 }
00227 }
00228
00229 void PL_ACA_CONNECT::get_nearest_node(Configuration* the_node, JMA_Roadmap_Tree* temp_tree2, Configuration* the_nearest_node)
00230 {
00231 double min_distance=10000.0;
00232 double temp_distance;
00233 int aaa=(*temp_tree2).n_nodes;
00234
00235 for (int i=0; i<aaa; i++)
00236 {
00237 temp_distance=get_distance((*the_node),(*temp_tree2).node[i].landmark.dof);
00238 if(min_distance>temp_distance)
00239 {
00240 min_distance=temp_distance;
00241 nearest_node_id=i;
00242 }
00243 }
00244 (*the_nearest_node)=(*temp_tree2).node[nearest_node_id].landmark.dof;
00245 }
00246
00247 void PL_ACA_CONNECT::InitRoadmapTree (JMA_Roadmap_Tree* ptr_tree1, const int n_dof, const int n_embryos, const JMA_Configuration init_conf, JMA_Roadmap_Tree* ptr_tree2, const JMA_Configuration goal_conf) const
00248 {
00249 int i;
00250 double dist;
00251
00252 (*ptr_tree1).N_dof = n_dof;
00253 (*ptr_tree1).n_nodes = 1;
00254 (*ptr_tree1).max_dist = 0.0;
00255 (*ptr_tree1).last_max_dist = -1.0;
00256 (*ptr_tree1).best_father_id = 0;
00257
00258 (*ptr_tree1).node[0].N_embryos = n_embryos;
00259 (*ptr_tree1).node[0].father_id = NIL;
00260
00261
00262 (*ptr_tree1).node[0].landmark.dof = init_conf.dof;
00263 (*ptr_tree1).node[0].landmark.dist = init_conf.dist;
00264
00265 dist =-1000.0;
00266 for(i = 0; i < (*ptr_tree1).node[0].N_embryos; i++)
00267 {
00268
00269 GenerateNewEmbryo(ptr_tree1,0,i);
00270
00271 if((*ptr_tree1).node[0].embryo[i].dist > dist)
00272 {
00273 (*ptr_tree1).best_embryo_id = i;
00274 dist = (*ptr_tree1).node[0].embryo[i].dist;
00275 }
00276 }
00277 (*ptr_tree1).max_dist = dist;
00278
00279 (*ptr_tree2).N_dof = n_dof;
00280 (*ptr_tree2).n_nodes = 1;
00281 (*ptr_tree2).max_dist = 0.0;
00282 (*ptr_tree2).last_max_dist = -1.0;
00283 (*ptr_tree2).best_father_id = 0;
00284
00285 (*ptr_tree2).node[0].N_embryos = n_embryos;
00286 (*ptr_tree2).node[0].father_id = NIL;
00287
00288
00289 (*ptr_tree2).node[0].landmark.dof = goal_conf.dof;
00290 (*ptr_tree2).node[0].landmark.dist = goal_conf.dist;
00291 dist =-1000.0;
00292 for(i = 0; i < (*ptr_tree2).node[0].N_embryos; i++)
00293 {
00294
00295 GenerateNewEmbryo(ptr_tree2,0,i);
00296
00297 if((*ptr_tree2).node[0].embryo[i].dist > dist)
00298 {
00299 (*ptr_tree2).best_embryo_id = i;
00300 dist = (*ptr_tree2).node[0].embryo[i].dist;
00301 }
00302 }
00303 (*ptr_tree2).max_dist = dist;
00304 }
00305
00306 void PL_ACA_CONNECT::Explore (JMA_Roadmap_Tree* tree) const
00307 {
00308
00309 (*tree).node[(*tree).n_nodes].landmark = (*tree).node[(*tree).best_father_id].embryo[(*tree).best_embryo_id];
00310 (*tree).node[(*tree).n_nodes].father_id = (*tree).best_father_id;
00311 (*tree).n_nodes=(*tree).n_nodes+1;
00312
00313 (*tree).node[(*tree).n_nodes-1].N_embryos = NUMBER_OF_EMBRYOS;
00314 for(int i = 0; i <MAX_EMBRYOS ; i++)
00315 {
00316 GenerateNewEmbryo(tree,(*tree).n_nodes-1,i);
00317
00318 }
00319
00320 GenerateNewEmbryo(tree,(*tree).best_father_id,(*tree).best_embryo_id);
00321
00322 UpdateRoadmapTree(tree);
00323 }
00324
00325 void PL_ACA_CONNECT::GenerateNewEmbryo (JMA_Roadmap_Tree* tree, const int id_landmark, const int id_embryo) const
00326 {
00327
00328
00329 int N_Dof = (*tree).N_dof ;
00330 MyRandomPath( id_landmark, id_embryo, N_Dof, (*tree).node[id_landmark].landmark, &((*tree).node[id_landmark].embryo[id_embryo]) );
00331
00332 ComputeEmbryoDistance(tree,&((*tree).node[id_landmark].embryo[id_embryo]));
00333 }
00334
00335 void PL_ACA_CONNECT::GetIdsPath (JMA_Roadmap_Tree* ptr_tree1, JMA_Roadmap_Tree* ptr_tree2)
00336 {
00337 int landmark_id, counter, id;
00338 if ((pointer % 2)==1)
00339 {
00340 landmark_id=(*ptr_tree1).n_nodes-1;
00341 counter = GetNAncestors(ptr_tree1,landmark_id);
00342 mark=counter-1;
00343 end_of_array=counter;
00344 id = landmark_id;
00345 while(id != 0)
00346 {
00347 ids_list[mark] = id;
00348 id = (*ptr_tree1).node[id].father_id;
00349 --mark;
00350 }
00351 ids_list[mark] = id;
00352 mark=counter;
00353 counter = GetNAncestors(ptr_tree2,nearest_node_id);
00354 id =nearest_node_id;
00355 while(id != 0)
00356 {
00357 ids_list[end_of_array] = id;
00358 id = (*ptr_tree2).node[id].father_id;
00359 ++end_of_array;
00360 }
00361 ids_list[ end_of_array ] = id;
00362 }
00363 else
00364 {
00365 counter = GetNAncestors(ptr_tree1,nearest_node_id);
00366 mark=counter-1;
00367 end_of_array=counter;
00368 id = nearest_node_id;
00369 while(id != 0)
00370 {
00371 ids_list[mark] = id;
00372 id = (*ptr_tree1).node[id].father_id;
00373 --mark;
00374 }
00375 ids_list[mark] = id;
00376 mark=counter;
00377 landmark_id=(*ptr_tree2).n_nodes-1;
00378 counter = GetNAncestors(ptr_tree2,landmark_id);
00379 id =landmark_id;
00380 while(id != 0)
00381 {
00382 ids_list[end_of_array] = id;
00383 id = (*ptr_tree2).node[id].father_id;
00384 ++end_of_array;
00385 }
00386 ids_list[ end_of_array ] = id;
00387 }
00388 }
00389
00390 void PL_ACA_CONNECT::UpdateRoadmapTree (JMA_Roadmap_Tree* tree) const
00391 {
00392
00393 int i, j;
00394 double distance;
00395
00396 (*tree).last_max_dist = (*tree).max_dist;
00397 (*tree).max_dist = 0.0;
00398
00399 for(i = 0 ; i < (*tree).n_nodes; i++)
00400 {
00401 for(j = 0; j < (*tree).node[i].N_embryos; j++)
00402 {
00403
00404
00405
00406 distance =get_distance((*tree).node[i].embryo[j].dof,
00407 (*tree).node[(*tree).n_nodes-1].landmark.dof);
00408
00409 if (distance < (*tree).node[i].embryo[j].dist)
00410 (*tree).node[i].embryo[j].dist = distance;
00411
00412
00413
00414
00415 if ((*tree).max_dist < (*tree).node[i].embryo[j].dist)
00416 {
00417 (*tree).max_dist = (*tree).node[i].embryo[j].dist;
00418 (*tree).best_father_id = i;
00419 (*tree).best_embryo_id = j;
00420 }
00421 }
00422 }
00423 }
00424
00425 void PL_ACA_CONNECT::ComputeEmbryoDistance (JMA_Roadmap_Tree* tree, JMA_Configuration* embryo) const
00426 {
00427 int i;
00428 double distance;
00429
00430 (*embryo).dist = BIG_NUMBER;
00431
00432 for(i=0; i < (*tree).n_nodes; i++)
00433 {
00434
00435 distance = get_distance((*embryo).dof,(*tree).node[i].landmark.dof);
00436
00437 if (distance < (*embryo).dist)
00438 (*embryo).dist = distance;
00439 }
00440 }
00441
00442 void PL_ACA_CONNECT::advance(JMA_Roadmap_Tree* temp_tree1, JMA_Roadmap_Tree* temp_tree2)
00443 {
00444 bool one_step=get_new_node(¤t_node, &nearest_node, &new_node);
00445 while((!( collisionDetector->IsInterferingLinear(new_node, current_node))) && (one_step))
00446 {
00447 int current_node_id=(*temp_tree1).n_nodes-1;
00448 (*temp_tree1).node[(*temp_tree1).n_nodes].landmark.dof=new_node;
00449 (*temp_tree1).node[(*temp_tree1).n_nodes].father_id=current_node_id;
00450 current_node=new_node;
00451 ++(*temp_tree1).n_nodes;
00452 (*temp_tree1).node[(*temp_tree1).n_nodes-1].N_embryos = NUMBER_OF_EMBRYOS;
00453 for(int i=0; i< NUMBER_OF_EMBRYOS; i++)
00454 GenerateNewEmbryo(temp_tree1,(*temp_tree1).n_nodes-1,i);
00455 UpdateRoadmapTree(temp_tree1);
00456 one_step=get_new_node(¤t_node, &nearest_node, &new_node);
00457 }
00458 }
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471 bool PL_ACA_CONNECT::get_new_node(Configuration* the_current_node, Configuration* the_nearest_node, Configuration* the_new_node) const
00472 {
00473 double Fixed_Distance=150;
00474 double pp=get_distance((*the_current_node), (*the_nearest_node));
00475 (*the_new_node)=(*the_current_node)*(1-Fixed_Distance/pp)+(*the_nearest_node)*(Fixed_Distance/pp);
00476 if( collisionDetector->IsInterfering(*the_new_node))
00477 return false;
00478 return true;
00479 }
00480
00481
00482 void PL_ACA_CONNECT::SetStart_and_GoalConfig(const Configuration& configuration1, const Configuration& configuration2)
00483 {
00484
00485
00486
00487
00488 init_conf.dof = configuration1 ;
00489 init_conf.dist = 0 ;
00490
00491
00492
00493
00494
00495 goal_conf.dof = configuration2 ;
00496 goal_conf.dist = 0 ;
00497
00498 InitRoadmapTree( &tree1, GetStartConfig().DOF(), MAX_EMBRYOS, init_conf, &tree2, goal_conf );
00499 }
00500
00501 void PL_ACA_CONNECT::optimization()
00502 {
00503 int i=0;
00504 int m=0;
00505 int j=mark-1;
00506 new_ids_list[m]=ids_list[0];
00507 ++m;
00508 while (i<mark)
00509 {
00510 while (j>i)
00511 {
00512 if (!collisionDetector->IsInterferingLinear(tree1.node[i].landmark.dof, tree1.node[j].landmark.dof))
00513 {
00514 new_ids_list[m]=ids_list[j];
00515 ++m;
00516 i=j;
00517 j=mark-1;
00518 }
00519 else
00520 --j;
00521 }
00522 ++i;
00523 new_ids_list[m]=ids_list[i];
00524 ++m;
00525 j=mark-2;
00526 }
00527 mark=m;
00528 j=end_of_array;
00529 while (j>i)
00530 {
00531 if (!collisionDetector->IsInterferingLinear(tree2.node[i].landmark.dof, tree2.node[j].landmark.dof))
00532 {
00533 new_ids_list[m]=ids_list[j];
00534 ++m;
00535 i=j;
00536 j=end_of_array;
00537 }
00538 else
00539 --j;
00540 }
00541 end_of_array=m-1;
00542 }
00543
00544 bool PL_ACA_CONNECT::DrawExplicit () const
00545 {
00546 Semaphore semaphore( this->guid );
00547 semaphore.Lock();
00548
00549 glClearColor( 0.0f, 0.0f, 0.2f, 1.0f );
00550 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00551
00552
00553 BOOL lightingState = glIsEnabled( GL_LIGHTING );
00554 glDisable( GL_LIGHTING );
00555
00556
00557 BOOL zbufferState = glIsEnabled( GL_DEPTH_TEST );
00558 glDisable( GL_DEPTH_TEST );
00559
00560
00561 glShadeModel( GL_SMOOTH );
00562
00563
00564 int i = 0;
00565 glColor4d( 1.0, 1.0, 1.0, 1.0 );
00566 glPointSize( 3.0 );
00567
00568 int dof = this->GetGoalConfig().DOF();
00569
00570 glColor3f( 1.0f, 1.0f, 0.0f );
00571 glBegin( GL_POINTS );
00572 double g0 = 0;
00573 double g1 = 0;
00574 double g2 = 0;
00575 double v0 = 0;
00576 double v1 = 0;
00577 double v2 = 0;
00578 double p0 = 0;
00579 double p1 = 0;
00580 double p2 = 0;
00581 if( dof > 0 )
00582 {
00583 g0 = this->GetGoalConfig()[ 0 ];
00584 }
00585 if( dof > 1 )
00586 {
00587 g1 = this->GetGoalConfig()[ 1 ];
00588 }
00589 if( dof > 2 )
00590 {
00591 g2 = this->GetGoalConfig()[ 2 ];
00592 }
00593 glVertex3f( g0, g1, g2 );
00594 glEnd();
00595
00596 for( i = 0; i < tree1.n_nodes; i++ )
00597 {
00598 JMA_Node node = tree1.node[ i ];
00599 JMA_Configuration current = node.landmark;
00600
00601
00602 v0 = current.dof[ 0 ];
00603 v1 = current.dof[ 1 ];
00604 if( dof > 2 )
00605 {
00606 v2 = current.dof[ 2 ];
00607 }
00608
00609 glBegin( GL_POINTS );
00610 glVertex3f( v0, v1, 0 );
00611 glEnd();
00612
00613 int parent = node.father_id;
00614 if( parent == -1 )
00615 {
00616 }
00617 else
00618 {
00619 JMA_Node parentNode = tree1.node[ parent ];
00620 JMA_Configuration parentConfig = parentNode.landmark;
00621
00622
00623 p0 = parentConfig.dof[ 0 ];
00624 p1 = parentConfig.dof[ 1 ];
00625 if( dof > 2 )
00626 {
00627 p2 = parentConfig.dof[ 2 ];
00628 }
00629
00630 glColor4d( 1.0, 1.0, 1.0, 1.0 );
00631 glBegin( GL_LINES );
00632 glVertex3f( v0, v1, v2 );
00633 glVertex3f( p0, p1, p2 );
00634 glEnd();
00635 }
00636
00637
00638 }
00639 for( i = 0; i < tree2.n_nodes; i++ )
00640 {
00641 JMA_Node node = tree2.node[ i ];
00642 JMA_Configuration current = node.landmark;
00643
00644
00645 v0 = current.dof[ 0 ];
00646 v1 = current.dof[ 1 ];
00647 if( dof > 2 )
00648 {
00649 v2 = current.dof[ 2 ];
00650 }
00651
00652 glBegin( GL_POINTS );
00653 glVertex3f( v0, v1, 0 );
00654 glEnd();
00655
00656 int parent = node.father_id;
00657 if( parent == -1 )
00658 {
00659 }
00660 else
00661 {
00662 JMA_Node parentNode = tree2.node[ parent ];
00663 JMA_Configuration parentConfig = parentNode.landmark;
00664
00665 p0 = parentConfig.dof[ 0 ];
00666 p1 = parentConfig.dof[ 1 ];
00667 if( dof > 2 )
00668 {
00669 p2 = parentConfig.dof[ 2 ];
00670 }
00671
00672 glColor4d( 1.0, 1.0, 1.0, 1.0 );
00673 glBegin( GL_LINES );
00674 glVertex3f( v0, v1, v2 );
00675 glVertex3f( p0, p1, p2 );
00676 glEnd();
00677 }
00678
00679
00680 }
00681
00682
00683 if( lightingState != 0x00 )
00684 {
00685 glEnable( GL_LIGHTING );
00686 }
00687
00688
00689 if( zbufferState )
00690 {
00691 glEnable( GL_DEPTH_TEST );
00692 }
00693 else
00694 {
00695 glDisable( GL_DEPTH_TEST );
00696 }
00697
00698 semaphore.Unlock();
00699 return true;
00700 }