planners/sandros/PL_Sandros.cpp

Go to the documentation of this file.
00001 //Sandros Planner Implemetation,
00002 //Start at April 20
00003 //By Huang Yifeng 
00004 
00005 #include "synchronization/semaphore.h"
00006 #include <assert.h>
00007 #include "PL_Sandros.h"
00008 
00009 #ifndef NOGL
00010 #include "opengl/glos.h"
00011 #include <gl/gl.h>
00012 #endif
00013 
00014 #include <LEDA/node_map.h> 
00015 #include <LEDA/node_pq.h>
00016 #include <LEDA/node_set.h>
00017 #include <LEDA/templates/shortest_path.t> 
00018 #include <LEDA/list.h>
00019 #include <math.h>
00020 
00021 #include "math/math2.h"
00022 
00023 #define LEDA_CHECKING_OFF
00024 
00025 //yifeng has made some changes.
00026 
00027 #define for_allnodes_inV(v,G)   forall_nodes(v, G)\
00028         if(m_nodeUVPsPt[v]==NodeInV)
00029 
00030 #define for_allnodes_inPs(v,G)  forall_nodes(v, G)\
00031         if(m_nodeUVPsPt[v]==NodeInPs)
00032 
00033 #define for_allnodes_inPsNoIsland(v,G)  forall_nodes(v, G)\
00034         if(m_nodeUVPsPt[v]==NodeInPs&&m_nodeInPsIsland[v]==false)
00035 
00036 
00037 #define for_allnodes_inPt(v,G)  forall_nodes(v, G)\
00038         if(m_nodeUVPsPt[v]==NodeInPt)
00039 
00040 #define forall_nodesSan(v,G)    forall_nodes(v, G)\
00041         if(m_nodeUVPsPt[v]==NodeInPt||m_nodeUVPsPt[v]==NodeInPs||m_nodeUVPsPt[v]==NodeInV)
00042 
00043 // Class PL_Sandros 
00044 
00045 //-----------------------------------------------------------------------------
00046 // Destructor
00047 //
00048 // comment - Deletes any temorary data
00049 //-----------------------------------------------------------------------------
00050 PL_Sandros::~PL_Sandros()
00051 {
00052         //this does nothing right now
00053 }
00054 
00055 int PL_Sandros::GetRefinedNode (int i)
00056 {
00057         int k;
00058         if(NodeRefined!=nil&&i<dof)
00059         {
00060                 k=NodeRefinedc[i];
00061         }
00062         else
00063         {
00064                 k=-1;
00065         }
00066 
00067         return k;
00068 }
00069 //-----------------------------------------------------------------------------
00070 // Plan
00071 //
00072 // comment - Called when planning is to be performed
00073 //-----------------------------------------------------------------------------
00074 bool PL_Sandros::Plan ()
00075 {
00076 
00077         //## begin PL_Sandros::Plan%929728915.body preserve=yes
00078         //some early check
00079         plan_success=FAIL;
00080 
00081         dof=collisionDetector->DOF(); //local copy of the Degree Of Freedom
00082 
00083         // Perform early checks
00084         if ( startNode == goalNode )
00085         {
00086                 graphPath.clear();
00087                 TranslatePath();
00088                 plan_success = PASS;
00089         }
00090         if ( plan_success == PASS )
00091         {
00092                 return true;
00093         }
00094         
00095         //Initialize Sandros's Graph:
00096         //Ps={s}, Pt={t},U={},V={root}
00097         
00098         InitailizeGraph();
00099         
00100         PlannerBase::StartTimer();
00101 
00102         Semaphore semaphore( guid );
00103         semaphore.Lock();
00104 
00105 
00106         //THIS IS THE MASTER WHILE LOOP
00107         bool planningDone = false;
00108         //Do some pretest here
00109 
00110         
00111         node_list L;            //node list used to store the sequence aviliable
00112         m_bResolutionLimit=false;
00113 
00114         while( !planningDone )
00115         {
00116                 //PLACE ALL PLANNING CALLS IN HERE
00117                 //every main loop planner will search for all the sequence avilialbe 
00118                 //for a free path, if no free path find then will do the node refinment
00119                 //untill path find or failure report.
00120                 m_bNoOtherSequence=false;
00121                 do
00122                 {
00123                         if(!L.empty())L.clear();        //clear L for a new sequence
00124 
00125                         if(SearchForSequence(L))
00126                         {
00127                                 DealwithSequence(L);    //a sequence is find then use local planner judge 
00128                         }                                                       //whether a node is reachable
00129                         else
00130                         {
00131                                 if(m_bResolutionLimit)  //now resolution has reach to the upbound limit and no other 
00132                                 {                                               //sequence can be generated, report planner's fail
00133                                         plan_success = FAIL;
00134                                         return false;
00135                                 }
00136                                 m_bNoOtherSequence=true;
00137                         }
00138                         if(plan_success==PASS)
00139                         {
00140                                 //when you actually finish planning, set this to true
00141                                 planningDone = true;
00142                                 SaveSandrosPath(L);
00143                         }
00144                 }while(!m_bNoOtherSequence&&planningDone!=true);
00145 
00146                 if(!planningDone)
00147                         ConstructGraph();       //construct the Sandros graph for search a path from Ps to Pt
00148                                                                 //and split the node in Q into subnodes
00149                 if(m_bResolutionLimit)  //now resolution has reach to the upbound limit and no other 
00150                 {                                               //sequence can be generated, report planner's fail
00151                         plan_success = FAIL;
00152                         return false;
00153                 }
00154 
00155                 //Every once in a while, you should release the semaphores, and check to 
00156                 //see if the time limit has expired.  If you don't release the semaphore,
00157                 //the drawing thread will never draw anything
00158                 semaphore.Unlock();
00159                 //check to see if the time limit has expired
00160 //              if( HasTimeLimitExpired() == true )
00161 //              {
00162 //                      return false ;
00163 //              }
00164                 semaphore.Lock();
00165         }
00166         //if we have in fact found a path, return a true value, otherwise, return false
00167         return true;
00168   //## end PL_Sandros::Plan%929728915.body
00169 }
00170 
00171 //when path found then translate the path into the mpk 'path' virente
00172 void PL_Sandros::SaveSandrosPath(const node_list& L)
00173 {
00174         path.Clear();
00175         if(!L.empty())
00176         {
00177                 node n1=L.head();
00178                 node n=n1;
00179                 while(n!=startNode)
00180                 {
00181                         path.AppendPointToBeginning (m_nodeInUPs[n]);
00182                         n=m_nodePs[n];
00183                 }
00184                 path.AppendPointToBeginning(G.inf(startNode));
00185                 do
00186                 {
00187                         Configuration c2=m_nodeInUPs[n1];
00188                         n1=L.succ(n1);          //find first node in V in the node_list
00189                         Configuration c1;
00190 /*                      if(IsInterferingSan(c2,n1,c1))          //here use a linear path use as local planner
00191                         {
00192 
00193                                 path.AppendPoint (G.inf(goalNode));
00194                                 return;         
00195                         }
00196 */
00197                         path.AppendPoint(c2);
00198                         if(!IsInterfering(c2,G.inf(goalNode)))
00199                         {
00200                                 path.AppendPoint(c2);
00201                                 path.AppendPoint (G.inf(goalNode));
00202                                 return;
00203                         }
00204                 }while(m_nodeUVPsPt[n1]!=NodeInPt);
00205                 path.AppendPoint (G.inf(goalNode));
00206         }//end if(!L.empty())
00207         return;
00208 }
00209 
00210 node PL_Sandros::GenerateNewNodeGrid(int res,const node n1,int no)
00211 {
00212         node returnme=nil;
00213         if(no>=nextNo)
00214         {
00215         Configuration config_return;
00216         config_return.SetLength(dof);
00217 
00218         double max = collisionDetector->JointMax( res ) ;
00219         double min = collisionDetector->JointMin( res ) ;
00220 
00221         double jointvalue=min+(max-min)/seedofa*(no-1);
00222 
00223         config_return=G.inf(n1);
00224         config_return[ res ] = jointvalue;
00225 
00226         for(int i=res+2;i<=dof;i++)
00227         {
00228                 collisionDetector->DeactivateFrames(0,i) ;
00229         }
00230 
00231         int m=0;
00232         while(IsInterfering(config_return))
00233         {
00234                 m++;
00235                 config_return[res]=config_return[res]+stride;
00236         }
00237         for(i=res+2;i<=dof;i++)
00238         {
00239                 collisionDetector->ActivateFrames(0,i) ;
00240         }
00241         if(config_return[res]==-1)
00242         {
00243                 config_return[res]=-2;
00244         }
00245 
00246         returnme=G.new_node(config_return);
00247         m_refine_level[returnme]=res+1;
00248         int temp=Tolerance/stride;
00249         nextNo=no+m+temp+1;
00250         }
00251         return returnme;
00252 }
00253 
00254 node PL_Sandros::GenerateNewNode(int res,const node n1)
00255 {
00256         Configuration config_return;
00257         config_return.SetLength(dof);
00258 
00259         node returnme;
00260         double max = collisionDetector->JointMax( res ) ;
00261         double min = collisionDetector->JointMin( res ) ;
00262         double random = 2*( double( rand() ) / RAND_MAX ) - 1; //this is a random number between 
00263         
00264         double jointvalue=(max-min)*random;
00265         if ( jointvalue < min )
00266         {
00267                 jointvalue += (max - min);
00268         }
00269         else if ( jointvalue > max )
00270         {
00271                 jointvalue -= (max - min);
00272         }
00273 
00274         config_return=G.inf(n1);
00275         config_return[ res ] = jointvalue;
00276         returnme=G.new_node(config_return);
00277 
00278         return returnme;
00279 }
00280 
00281 void PL_Sandros::SetValue(int m_Grid,int m_Neighbor,int m_Tolerance)
00282 {
00283         stride=m_Grid;
00284         ThreshHold=m_Neighbor;
00285         Tolerance=m_Tolerance;
00286 }
00287 void PL_Sandros::InitailizeGraph()
00288 {
00289         Configuration config_root;
00290         config_root.SetLength(dof);
00291 
00292         for(int i=0;i<dof;i++)
00293         {
00294                 config_root[i]=-1;                      //-1: mean the dof not yet specified
00295         }
00296         node root=G.new_node(config_root);
00297         
00298         m_nodePs.init(G,nil);
00299         m_nodePt.init(G,nil);
00300         m_nodeUVPsPt.init(G);
00301         m_nodeInUPs.init(G);
00302         m_refine_level.init(G);
00303         m_nodeInPsIsland.init(G,false);
00304         m_bEdgeHiden.init(G,false);
00305         
00306         m_nodeUVPsPt[startNode]=NodeInPs;
00307 //      m_nodeUVPsPt[goalNode]=NodeInPt;
00308         m_nodeUVPsPt[root]=NodeInV;
00309         m_nodeInUPs[startNode]=G.inf(startNode);
00310         m_refine_level[startNode]=dof;
00311         m_refine_level[goalNode]=dof;
00312         m_refine_level[root]=0;
00313         
00314 
00315         m_quenQ=new node_pq<int>(G);    // pointer to open priority queue set to be empty
00316         m_quenQ1=new queue<node>;       // pointer to open queue set to be empty
00317 
00318         double dis=DistanceSandros(startNode,root);     //connect the node with edges
00319         edge e1=G.new_edge(root,startNode,dis);         //startNode to root
00320         edge e3=G.new_edge(startNode,root,dis);         //startNode to root
00321         dis=DistanceSandros(root,goalNode);
00322         edge e2=G.new_edge(root,goalNode,dis);          //root to goalNode
00323         edge e4=G.new_edge(goalNode,root,dis);          //startNode to root
00324 
00325 
00326 //      m_quenQ->insert(root,current_res);
00327         m_c.init(G);
00328         m_c[e1]=G.inf(e1);
00329         m_c[e2]=G.inf(e2);
00330         m_c[e3]=G.inf(e3);
00331         m_c[e4]=G.inf(e4);
00332 
00333 //      G.make_undirected();
00334 }
00335 
00336 bool PL_Sandros::Save (const char *filename) const
00337 {
00338   //## begin PL_Sandros::Save%958948890.body preserve=yes
00339         char filenameWithExtension[ 256 ] = "" ;
00340         strcpy( filenameWithExtension, filename ) ;
00341         strcat( filenameWithExtension, ".SAN" ) ;                       //IMPROVE: store the extension elsewhere
00342 
00343         //open a binary file
00344         FILE* outfile = fopen( filenameWithExtension, "wb" ) ;
00345         assert( outfile != NULL ) ;
00346 
00347         //output the file header
00348         fwrite( "PSAN", 1, 4, outfile ) ;
00349 
00350         //OUTPUT ALL THE PLANNER SPECIFIC DATA IN HERE
00351 
00352         //close the binary file 
00353         fclose( outfile ) ;
00354 
00355         return false ;
00356   //## end PL_Sandros::Save%958948890.body
00357 }
00358 
00359 bool PL_Sandros::Load (const char *filename)
00360 {
00361   //## begin PL_Sandros::Load%958953407.body preserve=yes
00362         Semaphore semaphore( this->guid );
00363         semaphore.Lock();
00364 
00365         char filenameWithExtension[ 256 ] = "" ;
00366         strcpy( filenameWithExtension, filename ) ;
00367         strcat( filenameWithExtension, ".SAN" ) ;
00368 
00369         //open a binary file
00370         FILE* infile = fopen( filenameWithExtension, "rb" ) ;
00371         if( infile == NULL )
00372         {
00373                 semaphore.Unlock();
00374                 return false ;
00375         }
00376 
00377         //input the file header
00378         char header[ 5 ] = "" ;
00379         fread( header, 4, 1, infile ) ;
00380         header[ 4 ] = NULL ;
00381         assert( strcmp( header, "PSAN" ) == 0 ) ;
00382 
00383         //INPUT ALL THE SANDROS SPECIFIC DATA HERE
00384 
00385         //close the binary file 
00386         fclose( infile ) ;
00387 
00388         semaphore.Unlock();
00389         return true ;
00390   //## end PL_Sandros::Load%958953407.body
00391 }
00392 
00393 PL_Sandros::PL_Sandros()
00394 {
00395         // Clear the graph and initialize the node maps and open list
00396         ClearGraph();
00397         NodeRefined=nil;
00398         nextNo=1;
00399         plan_success=FAIL;
00400         seedofa=0;
00401         stride=5;
00402         current_res=0;
00403         m_bResolutionLimit=false;
00404         ThreshHold=16;
00405         Tolerance=9;
00406         TRACE("PL_Sandros Default Constructor completed.\n");
00407 }
00408 
00409 void PL_Sandros::SetStartConfig( const Configuration& config )
00410 {
00411         // Check if the configuration has changed.
00412         if (( startNode != nil ) && ( GetStartConfig() == config ))
00413         {
00414                 // no change in configuration.  Can exit early.
00415                 return;
00416         }
00417 
00418         // A new configuration has been requested.  Need to clear the current graph and search parameters
00419         ClearGraph();
00420 
00421         // Set the startConfig, add it to the graph, and assign the startNode to it by
00422         // calling the parent's SetStartConfig.
00423         PL_GraphBase::SetStartConfig( config );
00424 
00425         // Verify the startNode was initiated.
00426         ASSERT( startNode != nil );
00427 
00428         // Make sure the start node is valid
00429         if ( IsInterfering( GetStartConfig() ) )
00430         {
00431                 // Remove invalid start from graph.
00432                 G.del_node( startNode );
00433                 startNode = nil;
00434 
00435                 // Start Interfers, there will be no valid path possible.
00436                 plan_success = FAIL;
00437                 return;
00438         }
00439 
00440         // Assume the current goal config is correct, but still need to add it to new graph 
00441         // and assign the goal node.
00442         PL_GraphBase::SetGoalConfig( GetGoalConfig() );
00443 
00444         // Verify the goalNode was initiated.
00445         ASSERT( goalNode != nil );
00446 
00447         if ( IsInterfering( GetGoalConfig() ) )
00448         {
00449                 // Remove the invalid goal from graph
00450                 G.del_node( goalNode );
00451                 goalNode = nil;
00452 
00453                 // Goal Interfers, there will be no valid path possible.
00454                 plan_success = FAIL;
00455                 return;
00456         }
00457 }
00458 void PL_Sandros::SetGoalConfig ( const Configuration& config )
00459 {
00460         // Check if the configuration has changed.
00461         if (( goalNode != nil ) && ( GetGoalConfig() == config ))
00462         {
00463                 // no change in configuration.  Can exit early.
00464                 return;
00465         }
00466 
00467         // calling the parent's SetGoalConfig.
00468         PL_GraphBase::SetGoalConfig( config );
00469 
00470 
00471         // Verify the goalNode was initiated.
00472         ASSERT( goalNode != nil );
00473 
00474         // Ensure the new goal is valid
00475         if ( IsInterfering( GetGoalConfig() ) )
00476         {
00477                 G.del_node( goalNode );
00478                 goalNode = nil;
00479 
00480                 // Goal interferes.  There is no point in performing a search.  Exit early
00481                 plan_success = FAIL;
00482                 return;
00483         }
00484 }
00485 
00486 void PL_Sandros::SetCollisionDetector (CD_BasicStyle* collisionDetector)
00487 {
00488         PL_GraphBase::SetCollisionDetector( collisionDetector );
00489 }
00490 
00491 edge PL_Sandros::FindEdgeBetweenTwoNodes(const node& n1,const node& n2)
00492 {
00493         edge e1;
00494         forall_adj_edges(e1, n1) 
00495         {
00496                 node n3=G.target(e1);
00497                 if(n3==n2)
00498                         return e1;
00499         }
00500         return nil;
00501 }
00502 
00503 void PL_Sandros::DealwithSequence(const node_list& L)
00504 {
00505         if(!L.empty())
00506         {
00507                 node n1=L.head();
00508                 do
00509                 {
00510                         Configuration c2=m_nodeInUPs[n1];
00511                         node nn=n1;
00512                         n1=L.succ(n1);          //find first node in V in the node_list
00513                         Configuration c1;
00514                         if(n1!=nil)
00515                         {
00516                                 if(IsInterferingSan(c2,n1,c1))          //here use a linear path use as local planner
00517                                 {
00518                                         node n2=L.pred(n1);
00519                                         edge e1=FindEdgeBetweenTwoNodes(n1,n2);         //cut the edge
00520                                         edge e2=FindEdgeBetweenTwoNodes(n2,n1);         //cut the edge
00521                                         G.hide_edge(e1);
00522                                         m_bEdgeHiden[e1]=true;
00523                                         G.hide_edge(e2);
00524                                         m_bEdgeHiden[e2]=true;
00525                                         int nnnnn=G.number_of_edges();
00526                                         if(!m_quenQ->member(n1))
00527                                         {
00528                                                 m_quenQ->insert(n1,m_refine_level[n1]);
00529                                                 m_quenQ1->append(n1);
00530                                         }
00531                                         if(!m_quenQ->member(nn))
00532                                         {
00533                                                 m_quenQ->insert(nn,m_refine_level[nn]);
00534                                                 m_quenQ1->append(nn);
00535                                         }
00536                                         return;         
00537                                 }
00538                                 else
00539                                 {
00540                                         m_nodeUVPsPt[n1]=NodeInPs;                      //find a node in v reachable from Ps
00541                                         m_nodeInUPs[n1]=c1;
00542                                         m_nodePs[n1]=L.pred(n1);
00543                                         node n2=L.pred(n1);
00544                                         edge e1=FindEdgeBetweenTwoNodes(n1,n2);
00545                                         G.del_edge(e1);
00546                                         edge e2=FindEdgeBetweenTwoNodes(n2,n1);
00547                                         G.del_edge(e2);
00548                                         if(!IsInterfering(c1,G.inf(goalNode)))
00549                                         {
00550                                                 plan_success=PASS;
00551                                                 return;
00552                                         }
00553                                 }
00554                         }
00555 //                      n4=L.succ(n1);
00556                 }while(n1!=nil);//m_nodeUVPsPt[n4]!=NodeInPt
00557         }//end if(!L.empty())
00558         return;
00559 }
00560 
00561 void PL_Sandros::ClearGraph()
00562 {
00563         PL_GraphBase::ClearGraph();
00564 }
00565 
00566 
00567 bool PL_Sandros::DrawExplicit () const
00568 {
00569   //## begin PL_Sandros::DrawExplicit%964739313.body preserve=yes
00570 #ifndef NOGL
00571         Semaphore semaphore( this->guid );
00572         semaphore.Lock();
00573 
00574         glClearColor( 0.0f, 0.0f, 0.2f, 1.0f );
00575         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00576         BOOL lightingState = glIsEnabled( GL_LIGHTING );
00577         glDisable( GL_LIGHTING );
00578 
00579         //INSERT ALL YOUR DRAWING CODE HERE
00580         glBegin( GL_POLYGON );
00581                 glColor4f( 0.0f, 0.0f, 0.0f, 1.0f );    //black
00582                 glVertex3f( 0.0f, 0.0f, 0.0f );
00583                 glColor4f( 1.0f, 0.0f, 0.0f, 1.0f );    //red
00584                 glVertex3f( 0.0f, 5.0f, 0.0f );
00585                 glColor4f( 0.0f, 1.0f, 0.0f, 1.0f );    //green
00586                 glVertex3f( 5.0f, 5.0f, 0.0f );
00587                 glColor4f( 0.0f, 0.0f, 1.0f, 1.0f );    //blue
00588                 glVertex3f( 5.0f, 0.0f, 0.0f );
00589         glEnd();
00590 
00591         if( lightingState != 0x00 )
00592         {
00593                 glEnable( GL_LIGHTING );
00594         }
00595         semaphore.Unlock();
00596 #endif
00597         return true;
00598   //## end PL_Sandros::DrawExplicit%964739313.body
00599 }
00600 
00601 node PL_Sandros::FindNextNodeToRefined()
00602 {
00603         node n1=m_quenQ->find_min();
00604         int res=m_quenQ->prio(n1);
00605         node n2;
00606         bool findmin=false;
00607         queue<node> Q;
00608         n2=nil;
00609         do{
00610                 n2=m_quenQ1->pop();
00611                 if(m_quenQ->prio(n2)==res)
00612                         findmin=true;
00613                 else
00614                         Q.append(n2);
00615         }while(!findmin);
00616         while(!m_quenQ1->empty())
00617         {
00618                 Q.append(m_quenQ1->pop());
00619         }
00620         while(!Q.empty())
00621         {
00622                 m_quenQ1->append(Q.pop());
00623         }
00624 
00625 
00626 /*      do
00627         {
00628                 n2=m_quenQ1->pop();
00629                 if(m_quenQ->prio(n2)<dof)
00630                         findmin=true;
00631         }while(!findmin&&!m_quenQ1->empty ());
00632 */
00633         return n2;
00634 }
00635 
00636 void PL_Sandros::ConstructGraph()
00637 {
00638         if(!m_quenQ->empty())
00639         { 
00640                 G.restore_all_edges();
00641                 node n1=FindNextNodeToRefined();
00642                 int res;
00643                 if(n1!=nil)
00644                 {
00645                         res=m_quenQ->prio(n1);
00646                         current_res=res;
00647                 }
00648                 if(res==(dof)||n1==nil)
00649                 {
00650                         m_bResolutionLimit=true;
00651                         return;
00652                 }
00653                 int recordstride,recordtolerance,recordthreshhold;
00654                 if(dof>=4&&res==0)
00655                 {
00656                         recordstride=stride;
00657                         recordtolerance=Tolerance;
00658                         recordthreshhold=ThreshHold;
00659                         stride=7;
00660                         Tolerance=13;
00661                         ThreshHold=25;
00662                 }
00663                 NodeRefined=n1;
00664                 NodeRefinedc=G.inf(n1);
00665                 edge ee;
00666                 forall_edges(ee,G)
00667                 {
00668                         if(m_bEdgeHiden[ee])
00669                         {
00670                                 if((n1==G.source(ee))||(n1==G.target(ee)))
00671                                         m_bEdgeHiden[ee]=false;
00672                                 else
00673                                         G.hide_edge(ee);
00674                         }
00675                 }
00676 
00677                 node n6;
00678                 if(m_nodeUVPsPt[n1]==NodeInPs)  //if the node to be refined is reachable 
00679                                                                                 //from start point
00680                 {
00681                         Configuration cres,cres1;
00682                         cres=m_nodeInUPs[n1];
00683                         double jointvalue=cres[res];
00684                         cres1=G.inf(n1);
00685                         cres1[res]=jointvalue;
00686                         node newn=G.new_node(cres1);
00687                         m_nodeUVPsPt[newn]=NodeInPs;
00688                         m_nodeInPsIsland[newn]=true;
00689                         node v;
00690                         for_allnodes_inPs(v,G)
00691                         {
00692                                 if(m_nodePs[v]==n1)
00693                                 {
00694                                         m_nodePs[v]=newn;
00695                                 }
00696                         }
00697                         m_nodePs[newn]=m_nodePs[n1];
00698                         m_nodeInUPs[newn]=m_nodeInUPs[n1];
00699                         m_refine_level[newn]=dof;
00700                         node x=m_nodePs[newn];
00701 
00702                         if(!m_quenQ->member(x))
00703                         {
00704                                 m_quenQ->insert(x,m_refine_level[x]);
00705                                 m_quenQ1->append(x);
00706                         }
00707                         n6=newn;
00708                 }
00709                 
00710                 node n2;
00711                 bool firstnode=true;
00712                 double max = collisionDetector->JointMax( res ) ;
00713                 double min = collisionDetector->JointMin( res ) ;
00714                 seedofa=(max-min)/stride+2;
00715                 node prev;
00716                 nextNo=1;
00717                 for(int i=1;i<=seedofa;i++)
00718                 {
00719                         n2=GenerateNewNodeGrid(res,n1,i);
00720                         if(n2!=nil)
00721                         {
00722                                 if(!firstnode)
00723                                 {
00724                                         if(IsNeighber(prev,n2))
00725                                         {
00726 
00727                                                 double dis=DistanceSandros(prev,n2);
00728                                                 edge e1=G.new_edge(n2,prev,dis);
00729                                                 e1=G.new_edge(prev,n2,dis);
00730                                         }
00731                                 }
00732                                 m_nodeUVPsPt[n2]=NodeInV;
00733                                 node v1;
00734                                 forall_adj_nodes(v1, n1) 
00735                                 {
00736                                         if(IsNeighber(v1,n2))
00737                                         {
00738                                                 double dis=DistanceSandros(v1,n2);
00739                                                 edge e1=G.new_edge(n2,v1,dis);
00740                                                 edge e2=G.new_edge(v1,n2,dis);
00741                                                 if(m_nodeUVPsPt[v1]==NodeInPs)
00742                                                 {
00743                                                         Configuration temp=m_nodeInUPs[v1];
00744                                                         Configuration cdd;
00745                                                         if(!IsNeighberSan(temp,n2))             
00746                                                         {
00747                                                                 G.hide_edge(e1);
00748                                                                 G.hide_edge(e2);
00749                                                         }
00750 /*                                                      else if(IsInterferingSan(temp,n2,cdd))
00751                                                         {
00752                                                                 G.hide_edge(e1);
00753                                                                 G.hide_edge(e2);
00754                                                         }
00755 */
00756                                                 }
00757 
00758                                         }
00759                                 }
00760                                 node n5;
00761                                 if(m_nodeUVPsPt[n1]==NodeInPs)  //if the node to be refined is reachable 
00762                                 {
00763                                         for_allnodes_inPs(n5,G)
00764                                         {
00765                                                 if(m_nodePs[n5]==n6)
00766                                                 {
00767                                                         if(IsNeighber(n5,n2))
00768                                                         {
00769                                                                 double dis=DistanceSandros(n5,n2);
00770                                                                 edge e1=G.new_edge(n2,n5,dis);
00771                                                                 edge e2=G.new_edge(n5,n2,dis);
00772                                                                 Configuration temp=m_nodeInUPs[n5];
00773                                                                 Configuration cdd;
00774                                                                 if(!IsNeighberSan(temp,n2))             
00775                                                                 {
00776                                                                         G.hide_edge(e1);
00777                                                                         G.hide_edge(e2);
00778                                                                 }
00779 /*                                                              else if(IsInterferingSan(temp,n2,cdd))
00780                                                                 {
00781                                                                         G.hide_edge(e1);
00782                                                                         G.hide_edge(e2);
00783                                                                 }
00784 */
00785                                                         }
00786                                                 }
00787                                         }
00788                                         node n7=m_nodePs[n6];
00789                                         if(IsNeighber(n7,n2))
00790                                         {
00791                                                 double dis=DistanceSandros(n7,n2);
00792                                                 edge e1=G.new_edge(n7,n2,dis);
00793                                                 edge e2=G.new_edge(n2,n7,dis);
00794                                                 Configuration temp=m_nodeInUPs[n7];
00795                                                 Configuration cdd;
00796                                                 if(!IsNeighberSan(temp,n2))             
00797                                                 {
00798                                                         G.hide_edge(e1);
00799                                                         G.hide_edge(e2);
00800                                                 }
00801 /*                                              else if(IsInterferingSan(temp,n2,cdd))
00802                                                 {
00803                                                         G.hide_edge(e1);
00804                                                         G.hide_edge(e2);
00805                                                 }
00806 */
00807                                         }
00808 
00809                                 }
00810                                 prev=n2;
00811                                 firstnode=false;
00812                         }
00813                 }
00814                 m_quenQ->del(n1);
00815                 G.del_node(n1);
00816                 if(dof>=4&&res==0)
00817                 {
00818                         stride=recordstride;
00819                         Tolerance=recordtolerance;
00820                         ThreshHold=recordthreshhold;
00821                 }
00822         }//end if(!m_quenQ.empty())
00823         else
00824         {
00825                 m_bResolutionLimit=true;
00826                 return;
00827         }
00828         
00829         int m=G.number_of_nodes();
00830         int n=G.number_of_edges();
00831 
00832         m_c.init(G);
00833         edge e2;
00834         forall_edges(e2,G)
00835         {
00836                 m_c[e2]=G.inf(e2);
00837         }
00838 
00839         return;
00840 }
00841 
00842 bool PL_Sandros::IsNeighber(const node& n1,const node& n2)
00843 {
00844         Configuration c1,c2;
00845         c1=G.inf(n1);
00846         c2=G.inf(n2);
00847         double dis=0;
00848 //      if(m_nodeUVPsPt[n1]==NodeInPs)
00849 //      c1=m_nodeInUPs[n1];
00850         for(int i=0;i<dof;i++)
00851         {
00852                 double d1=c1[i];
00853                 double d2=c2[i];
00854                 if(c1[i]!=-1&&c2[i]!=-1)
00855                 {
00856                         dis+=abs(c1[i]-c2[i]);
00857 //                      if(abs(c1[i]-c2[i])>=ThreshHold)
00858 //                              return false;
00859                 }
00860         }
00861 //      if(dis>=Tolerance&&dis<=ThreshHold)
00862         if(dis<=ThreshHold)
00863                 return true;
00864         else 
00865                 return false;
00866 }
00867 
00868 bool PL_Sandros::IsNeighberSan(const Configuration& c1,const node& n2)
00869 {
00870         Configuration c2;
00871         c2=G.inf(n2);
00872         double dis=0;
00873 //      if(m_nodeUVPsPt[n1]==NodeInPs)
00874 //      c1=m_nodeInUPs[n1];
00875         for(int i=0;i<dof;i++)
00876         {
00877                 double d1=c1[i];
00878                 double d2=c2[i];
00879                 if(c1[i]!=-1&&c2[i]!=-1)
00880                 {
00881                         dis+=abs(c1[i]-c2[i]);
00882 //                      if(abs(c1[i]-c2[i])>=ThreshHold)
00883 //                              return false;
00884                 }
00885         }
00886 //      if(dis>=Tolerance&&dis<=ThreshHold)
00887         if(dis<=ThreshHold)
00888                 return true;
00889         else 
00890                 return false;
00891 }
00892 
00893 
00894 bool PL_Sandros::IsInterferingSan(const Configuration& c1,const node& n2,Configuration& p)
00895 {
00896         Configuration c2;
00897         c2=G.inf(n2);
00898         for(int i=0;i<dof;i++)
00899         {
00900                 if(c2[i]==-1)
00901                         c2[i]=c1[i];
00902         }
00903         p=c2;
00904         return IsInterfering(c1,c2);
00905 }
00906 
00907 //defination of the edge longth between the two nodes
00908 double PL_Sandros::DistanceSandros(const node& n1,const node& n2)
00909 {
00910         Configuration c1,c2;
00911         c1=G.inf(n1);
00912         c2=G.inf(n2);
00913         double returndis=0;
00914         for(int i=0;i<dof;i++)
00915         {
00916                 if(c1[i]==-1)
00917                 {
00918                         c1[i]=c2[i];
00919                 }
00920                 if(c2[i]==-1)
00921                 {
00922                         c2[i]=c1[i];
00923                 }
00924                 returndis+=abs(c1[i]-c2[i]);
00925         }
00926         if(returndis<1)
00927                 returndis=1;
00928         return returndis;
00929 }
00930 
00931 //find a sequence from graph of sandros 
00932 //and return the node_list and the result
00933 bool PL_Sandros::SearchForSequence(node_list& L)
00934 {
00935         node n1;
00936         double shortest,test;
00937         shortest=0;
00938         test=0;
00939         node_array<edge>        pred;
00940         pred.init(G);
00941 
00942         node shortest_start,shortest_goal;
00943 
00944         bool b_pathfound=false;
00945 
00946         for_allnodes_inPsNoIsland(n1,G)
00947         {
00948                 if(!b_pathfound)
00949                 {
00950                         test=DIJKSTRA_T(G,n1,goalNode,m_c,pred);        //find shortest path from n1 to n2
00951                         if(pred[goalNode]!=nil)                                 //path found
00952                         {
00953                                 if(shortest==0||shortest>=test)
00954                                 {
00955                                         shortest=test;
00956                                         shortest_start=n1;
00957                                         shortest_goal=goalNode;
00958                                 }       
00959                                 b_pathfound=true;
00960                                 goto ttt;
00961                         }// end if(pred[n2]!=nil)
00962                 }
00963         }//endif for_allnodes_inPs
00964 
00965 ttt:
00966         if(b_pathfound==false)  return false;
00967         else{                                                   //store the sequence for future use
00968                 node next_node;
00969                 node previous_node=shortest_goal;
00970                 L.push(shortest_goal);
00971                 test=DIJKSTRA_T(G,shortest_start,shortest_goal,m_c,pred);
00972                 edge e1=pred[shortest_goal];
00973                 do                                      //extract all the nodes along the shortest path
00974                 {
00975                         next_node=G.source(e1);
00976                         if(next_node==previous_node)
00977                                 next_node=G.target(e1);
00978                         e1=pred[next_node];
00979                         L.push(next_node);
00980                         Configuration cx=G.inf(next_node);
00981                         for(int kk=0;kk<dof;kk++)
00982                         {
00983                                 int hh=cx[kk];
00984                         }
00985                 }while(pred[next_node]!=nil&&m_nodeUVPsPt[next_node]!=NodeInPs);
00986                 return true;
00987         }//end else
00988 }

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