rrt.c
Go to the documentation of this file.
1 #include "rrt.h"
2 
3 #include "defines.h"
4 #include "algebra.h"
5 #include "random.h"
6 #include "samples.h"
7 #include "filename.h"
8 
9 #include <string.h>
10 #include <math.h>
11 
29 void NewRRTBranch(TRRTStatistics *rst);
30 
40 
50 
61 
69 void NewRRTStep(TRRTStatistics *rst);
70 
81 void NewRRTDistanceQrand(double dqr,TRRTStatistics *rst);
82 
91 
100 
109 void NewRRTHighCost(TRRTStatistics *rst);
110 
119 
128 
138 
147 void NewRRTTooFar(TRRTStatistics *rst);
148 
157 void NewRRTStalled(TRRTStatistics *rst);
158 
167 void NewRRTSample(TRRTStatistics *rst);
168 
175 
182 
189 
190 
191 /******************************************************************************/
192 /******************************************************************************/
193 /******************************************************************************/
194 
196 {
197  if (rst!=NULL)
198  {
199  rst->n=0;
200 
201  rst->nBranch=0;
202  rst->nNoEmptyBranch=0;
203 
204  rst->nTreeConnection=0;
205  rst->nNoEmptyTreeConnection=0;
206 
207  rst->nStep=0;
208  rst->dQrand=0.0;
209 
210  rst->nQrandReached=0;
211  rst->nConvergenceError=0;
212  rst->nHighCost=0;
213  rst->nOutOfDomain=0;
214  rst->nDistanceIncreases=0;
215  rst->nCollision=0;
216  rst->nTooFar=0;
217  rst->nStalled=0;
218 
219  rst->nSample=0;
220 
221  rst->nRandom=0;
222  rst->nRejections=0;
223 
224  rst->nCollisionChecks=0;
225  }
226 }
227 
229 {
230  if (rst!=NULL) rst->nBranch++;
231 }
232 
234 {
235  if (rst!=NULL) rst->nNoEmptyBranch++;
236 }
237 
239 {
240  if (rst!=NULL) rst->nTreeConnection++;
241 }
242 
244 {
245  if (rst!=NULL) rst->nNoEmptyTreeConnection++;
246 }
247 
248 void NewRRTDistanceQrand(double dqr,TRRTStatistics *rst)
249 {
250  if (rst!=NULL) rst->dQrand+=dqr;
251 }
252 
254 {
255  if (rst!=NULL) rst->nStep++;
256 }
257 
259 {
260  if (rst!=NULL) rst->nQrandReached++;
261 }
262 
264 {
265  if (rst!=NULL) rst->nConvergenceError++;
266 }
267 
269 {
270  if (rst!=NULL) rst->nHighCost++;
271 }
272 
274 {
275  if (rst!=NULL) rst->nOutOfDomain++;
276 }
277 
279 {
280  if (rst!=NULL) rst->nDistanceIncreases++;
281 }
282 
284 {
285  if (rst!=NULL) rst->nCollision++;
286 }
287 
289 {
290  if (rst!=NULL) rst->nTooFar++;
291 }
292 
294 {
295  if (rst!=NULL) rst->nStalled++;
296 }
297 
299 {
300  if (rst!=NULL) rst->nSample++;
301 }
302 
304 {
305  if (rst!=NULL) rst->nRandom++;
306 }
307 
309 {
310  if (rst!=NULL) rst->nRejections++;
311 }
312 
314 {
315  if (rst!=NULL) rst->nCollisionChecks++;
316 }
317 
319 {
320  if ((rst1!=NULL)&&(rst2!=NULL))
321  {
322  if (rst1->n==0)
323  rst2->n++;
324  else
325  rst2->n+=rst1->n;
326 
327  rst2->nBranch+=rst1->nBranch;
328  rst2->nNoEmptyBranch+=rst1->nNoEmptyBranch;
329 
330  rst2->nTreeConnection+=rst1->nTreeConnection;
332 
333  rst2->nStep+=rst1->nStep;
334  rst2->dQrand+=rst1->dQrand;
335 
336  rst2->nQrandReached+=rst1->nQrandReached;
337  rst2->nOutOfDomain+=rst1->nOutOfDomain;
339  rst2->nCollision+=rst1->nCollision;
340  rst2->nTooFar+=rst1->nTooFar;
341  rst2->nStalled+=rst1->nStalled;
342 
343  rst2->nSample+=rst1->nSample;
344 
345  rst2->nRandom+=rst1->nRandom;
346  rst2->nRejections+=rst1->nRejections;
347 
348  rst2->nCollisionChecks+=rst1->nCollisionChecks;
349  }
350 }
351 
353 {
354  if (rst!=NULL)
355  {
356  unsigned int m,nExt;
357 
358  if (rst->n==0)
359  rst->n=1;
360 
361  nExt=rst->nBranch+rst->nTreeConnection;
362 
363  fprintf(stderr,"%% **************************************************\n");
364  if (rst->n>1)
365  fprintf(stderr,"RRT Statistics (averaged over %u repetitions)\n",rst->n);
366  else
367  fprintf(stderr,"RRT Statistics\n\n");
368  fprintf(stderr," Num. random samples : %.2f\n",
369  (double)rst->nRandom/(double)rst->n);
370  fprintf(stderr," Num. rejected random samples : %.2f (%.2f)\n",
371  (double)rst->nRejections/(double)rst->n,
372  (double)rst->nRejections/(double)rst->nRandom);
373  fprintf(stderr," Num. accepted random samples : %.2f (%.2f)\n",
374  (double)(rst->nRandom-rst->nRejections)/(double)rst->n,
375  (double)(rst->nRandom-rst->nRejections)/(double)rst->nRandom);
376 
377  if ((rst->n==1)&&(rrt!=NULL)&&(DynamicDomainRRT(rrt)))
378  {
379  unsigned int i,n;
380  double miDD,maDD,avDD,r;
381 
382  miDD=+INF;
383  maDD=-INF;
384  avDD=0.0;
385  n=0;
386  for(i=0;i<rrt->ns;i++)
387  {
388  r=GetDynamicDomainRadius(i,rrt);
389  if (r>0)
390  {
391  if (r<miDD)
392  miDD=r;
393  if (r>maDD)
394  maDD=r;
395  avDD+=r;
396  n++;
397  }
398  }
399  if (n==0)
400  fprintf(stderr," No dynamic domain effect.");
401  else
402  fprintf(stderr," Dynamic domain radius : %.2f <-- %.2f --> %.2f (%u/%u)\n",
403  miDD,avDD/(double)n,maDD,n,rrt->ns);
404  }
405  fprintf(stderr," Average distance to Qrand : %.2f\n",
406  rst->dQrand/(double)rst->nBranch);
407 
408  if (rst->nTreeConnection>0)
409  {
410  fprintf(stderr," Num. direct RRT extensions : %.2f\n",
411  (double)rst->nBranch/(double)rst->n);
412 
413  m=rst->nNoEmptyBranch;
414  fprintf(stderr," Num. no-empty direct extensions : %.2f (%.2f)\n",
415  (double)m/(double)rst->n,
416  (double)m/(double)rst->nBranch);
417 
418  fprintf(stderr," Num. RRT connections : %.2f\n",
419  (double)rst->nTreeConnection/(double)rst->n);
420  m=rst->nNoEmptyTreeConnection;
421  fprintf(stderr," Num. no-empty RRT connections : %.2f (%.2f)\n",
422  (double)m/(double)rst->n,
423  (double)m/(double)rst->nTreeConnection);
424 
425  fprintf(stderr," Total branches : %.2f\n",
426  (double)nExt/(double)rst->n);
428  fprintf(stderr," Total no-empty branches : %.2f (%.2f)\n",
429  (double)m/(double)rst->n,
430  (double)m/(double)nExt);
431  }
432  else
433  {
434  fprintf(stderr," Total branches : %.2f\n",
435  (double)rst->nBranch/(double)rst->n);
436  m=rst->nNoEmptyBranch;
437  fprintf(stderr," Total no-empty branches : %.2f (%.2f)\n",
438  (double)m/(double)rst->n,
439  (double)m/(double)rst->nBranch);
440  }
441  fprintf(stderr," Reasons to stop the branch/connection\n");
442  m=rst->nQrandReached;
443  fprintf(stderr," q_rand reached : %.2f (%.2f)\n",
444  (double)m/(double)rst->n,
445  (double)m/(double)nExt);
446 
447  m=rst->nConvergenceError;
448  fprintf(stderr," Convergence errors : %.2f (%.2f)\n",
449  (double)m/(double)rst->n,
450  (double)m/(double)nExt);
451 
452  m=rst->nHighCost;
453  fprintf(stderr," High cost configurations : %.2f (%.2f)\n",
454  (double)m/(double)rst->n,
455  (double)m/(double)nExt);
456 
457  m=rst->nOutOfDomain;
458  fprintf(stderr," Out of domain : %.2f (%.2f)\n",
459  (double)m/(double)rst->n,
460  (double)m/(double)nExt);
461 
462  m=rst->nDistanceIncreases;
463  fprintf(stderr," Distance to q_rand increases : %.2f (%.2f)\n",
464  (double)m/(double)rst->n,
465  (double)m/(double)nExt);
466 
467  m=rst->nCollision;
468  fprintf(stderr," Collision : %.2f (%.2f)\n",
469  (double)m/(double)rst->n,
470  (double)m/(double)nExt);
471 
472  m=rst->nTooFar;
473  fprintf(stderr," Too far from previous sample : %.2f (%.2f)\n",
474  (double)m/(double)rst->n,
475  (double)m/(double)nExt);
476 
477  m=rst->nStalled;
478  fprintf(stderr," Stalled branch : %.2f (%.2f)\n",
479  (double)m/(double)rst->n,
480  (double)m/(double)nExt);
481 
482 
483  fprintf(stderr," Num. extension steps : %.2f\n",
484  (double)rst->nStep/(double)rst->n);
485  fprintf(stderr," Num. steps per branch : %.2f\n",
486  (double)rst->nStep/(double)nExt);
487 
488  fprintf(stderr," Num. collision checks : %.2f\n",
489  (double)rst->nCollisionChecks/(double)rst->n);
490 
491 
492  fprintf(stderr," Num. samples in (bi)RRT : %.2f\n",
493  (double)rst->nSample/(double)rst->n);
494  fprintf(stderr," Samples per non-empty extens. : %.2f\n",
495  (double)rst->nSample/(double)(rst->nNoEmptyBranch+rst->nNoEmptyTreeConnection));
496  }
497 }
498 
500 {
501 }
502 
503 
504 /******************************************************************************/
505 /******************************************************************************/
506 /******************************************************************************/
507 
508 
517 void SetRRTTopology(Tparameters *pr,Trrt *rrt);
518 
555 boolean AddBranchToRRT(Tparameters *pr,boolean ccRRT,
556  unsigned int tree,unsigned int i_near,
557  double *q_rand,double *goal,
558  unsigned int *lastSample,
559  double (*costF)(Tparameters*, boolean , double*, void*),
560  void *costData,
561  TRRTStatistics *rst,Trrt *rrt);
562 
582 unsigned int ReconstructRRTPath(Tparameters *pr,unsigned int sID,
583  double *pl,double* pc,unsigned int *ns,
584  double ***path,Trrt *rrt);
585 
604 unsigned int Steps2PathinRRT(Tparameters *pr,Tvector *steps,double *pl,double* pc,
605  unsigned int *ns,double ***path,Trrt *rrt);
606 
618 double RRTPathLength(Tparameters *pr,unsigned int sID,Trrt *rrt);
619 
646 unsigned int AddStepToRRTstar(Tparameters *pr,unsigned int i_near,
647  double *q_rand,double *goal,
648  unsigned int *i_goal,
649  TRRTStatistics *rst,Trrt *rrt);
675 void WireRRTstar(Tparameters *pr,unsigned int id_new,unsigned int i_near,
676  double gamma,unsigned int nn,unsigned int *n,double **c,
677  double h,Theap *q,unsigned int *t,
678  TRRTStatistics *rst,Trrt *rrt);
679 
700 void ReWireRRTstar(Tparameters *pr,
701  unsigned int id_new,double gamma,
702  unsigned int nn,unsigned int *n,double *c,
703  Tvector *steps,double *l,
704  Trrt *rrt);
705 
721 void RRTstarCloseIteration(unsigned int it,unsigned int id_goal,
722  double time,double gamma,
723  double *times,double *costs,
724  Trrt *rrt);
725 
741 void BiRRTstarCloseIteration(unsigned int it,double l,
742  double time,double gamma,
743  double *times,double *costs,
744  Trrt *rrt);
745 
746 /**********************************************************************************/
747 /**********************************************************************************/
748 /**********************************************************************************/
749 
750 void CopyRRTStep(void *a,void *b)
751 {
752  ((TRRTStep *)a)->id=((TRRTStep *)b)->id;
753  ((TRRTStep *)a)->cost=((TRRTStep *)b)->cost;
754 }
755 
756 void DeleteRRTStep(void *a)
757 {
758 }
759 
761 {
762  boolean hasS;
763  unsigned int i;
764 
765  if (CS_WD_GET_SIMP_TOPOLOGY(pr,&(rrt->tp),rrt->w)!=rrt->m)
766  Error("Missmatch number of variables in SetRRTTopology");
767 
768  /* Search for a "circular" variable */
769  hasS=FALSE;
770  i=0;
771  while((i<rrt->m)&&(!hasS))
772  {
773  hasS=(rrt->tp[i]==TOPOLOGY_S);
774  i++;
775  }
776  if (!hasS)
777  {
778  /* if all variables are real no need to consider topology */
779  free(rrt->tp);
780  rrt->tp=NULL;
781  }
782 }
783 
784 void AddEdgeToRRT(unsigned int i,unsigned int j,double c,Trrt *rrt)
785 {
786  if (rrt->graph)
787  {
788  TRRTSampleInfo *si;
789  unsigned int k;
790 
791  if ((i>=rrt->ns)||(j>=rrt->ns))
792  Error("Nodes out of range in AddEdgeToRRT");
793 
794  for(k=0;k<2;k++)
795  {
796  si=(k==0?rrt->si[i]:rrt->si[j]);
797  if (si->nn==si->m)
798  {
799  MEM_DUP(si->n,si->m,unsigned int);
800  MEM_EXPAND(si->cn,si->m,double);
801  }
802  si->n[si->nn]=(k==0?j:i);
803  si->cn[si->nn]=c;
804  si->nn++;
805  }
806  }
807 }
808 
809 boolean TransitionTestRRT(Tparameters *pr,unsigned int parent,double* q_next,double deltaStep,double *cost,
810  double (*costF)(Tparameters*,boolean,double*,void*),void *costData,Trrt *rrt)
811 {
812  double prevCost;
813  double threshold;
814  boolean success;
815  double epsilon;
816  double coefTemp;
817  double nFailMax;
818 
819  *cost=costF(pr,TRUE,q_next,costData);
820  prevCost=rrt->si[parent]->cost;
821 
822  epsilon=GetParameter(CT_EPSILON,pr);
823 
824  if ((prevCost+epsilon)>(*cost))
825  return(TRUE);
826  else
827  {
828  threshold=exp((prevCost-(*cost))/(rrt->temperature*deltaStep));
829  coefTemp=GetParameter(CT_COEF_TEMP,pr);
830  success=(randomDouble()<threshold);
831  if(success)
832  {
833  rrt->nFail=0;
834  rrt->temperature=rrt->temperature/pow(coefTemp,((*cost)-prevCost)/10);
835  }
836  else
837  {
838  rrt->nFail=rrt->nFail+1;
839  nFailMax=GetParameter(CT_NFAIL_MAX,pr);
840  if (rrt->nFail>nFailMax)
841  {
842  rrt->temperature=rrt->temperature*coefTemp;
843  rrt->nFail=0;
844  }
845  }
846  return(success);
847  }
848 }
849 
850 boolean AddBranchToRRT(Tparameters *pr,boolean ccRRT,
851  unsigned int tree,unsigned int i_near,
852  double *q_rand,double *goal,
853  unsigned int *lastSample,
854  double (*costF)(Tparameters*,boolean,double*,void*),
855  void *costData,
856  TRRTStatistics *rst,Trrt *rrt)
857 {
858  boolean validSample,inDomain,inCollision,goalReached,distanceDecrease,tooFar,
859  qRandReached,tooClose;
860  unsigned int it;
861  double d,d1,dp;
862  #if (!EXPLORATION_RRT)
863  double dg;
864  #endif
865  double *v,*q_near,*q_next,*q_tmp;
866  unsigned int parent;
867  double eps;
868  boolean trans;
869  double cost;
870 
871  if (ccRRT)
872  eps=0;
873  else
874  eps=rrt->delta/50.0; /* for delta = 0.05 -> eps= 0.001 (value used in original cbirrt2) */
875 
876  q_near=rrt->si[i_near]->samples;
877 
878  NEW(v,rrt->m,double);
879  NEW(q_next,rrt->m,double);
880  NEW(q_tmp,rrt->m,double);
881  memcpy(q_next,q_near,rrt->m*sizeof(double)); /* start from q_near */
882  parent=i_near; /* inear will be parent for 1st sample if any */
883  *lastSample=i_near;
884 
885  DifferenceVectorTopology(rrt->m,rrt->tp,q_rand,q_next,v); /* q_next=q_near */
886  d1=Norm(rrt->m,v);
887  if (d1>rrt->delta)
888  ScaleVector(rrt->delta/d1,rrt->m,v);
889 
890  #if (_DEBUG>1)
891  printf(" Distance q_rand-tree: %f (%.12f)\n",d1,rrt->delta);
892  #endif
893 
894  goalReached=FALSE;
895  validSample=TRUE;
896  inDomain=TRUE;
897  inCollision=FALSE;
898  distanceDecrease=TRUE;
899  tooFar=FALSE;
900  tooClose=FALSE;
901  qRandReached=FALSE;
902  trans=TRUE;
903 
904  it=1;
905  while((!goalReached)&&(validSample)&&(!qRandReached)&&(inDomain)&&(trans)&&
906  (!inCollision)&&(distanceDecrease)&&(!tooFar)&&(!tooClose))
907  {
908  if (ccRRT)
909  SumVectorScale(rrt->m,q_near,(double)it,v,q_next);
910  else
911  AccumulateVector(rrt->m,v,q_next);
912 
913  if (rrt->n>0)
914  validSample=(CS_WD_NEWTON_IN_SIMP(pr,q_next,rrt->w)!=DIVERGED);
915 
916  if (validSample)
917  {
918  /* Compute the distance to previous sample */
919  dp=DistanceTopology(rrt->m,rrt->tp,rrt->si[parent]->samples,q_next);
920 
921  /* Distance to previous sample should not be too small */
922  tooClose=(dp<eps);
923  if (!tooClose)
924  {
925  /* Distance to previous sample should not too large */
926  tooFar=(dp>2*rrt->delta);
927  if (!tooFar)
928  {
929  /* Distance to q_rand should not increase */
930  d=d1;
931  d1=DistanceTopology(rrt->m,rrt->tp,q_rand,q_next);
932  distanceDecrease=(d1<d); /* new < previous */
933 
934  if (distanceDecrease)
935  {
936  /* before checking the inDomain condition, we have to
937  move the ranges to [-pi,pi], if necessary*/
938  inDomain=((PointInBoxTopology(NULL,FALSE,rrt->m,q_next,0,rrt->tp,rrt->ambient))&&
939  (CS_WD_SIMP_INEQUALITIES_HOLD(pr,q_next,rrt->w)));
940  if (inDomain)
941  {
942  if(costF!=NULL)
943  trans=TransitionTestRRT(pr,parent,q_next,dp,&cost,costF,costData,rrt);
944 
945  if(trans)
946  {
947  CS_WD_IN_COLLISION(inCollision,pr,q_next,rrt->si[parent]->samples,rrt->w);
948  #if (GET_RRT_STATISTICS)
950  #endif
951  if (!inCollision)
952  {
953  // fprintf(stderr," exp Step: %f \n",dp);
954  qRandReached=AddNodeToRRT(pr,tree,parent,q_next,q_rand,
955  lastSample,NULL,dp,cost,rst,rrt);
956  #if (GET_RRT_STATISTICS)
957  if (qRandReached)
958  NewRRTQrandReached(rst);
959  #endif
960 
961  /* currentNode will be parent for next iteration,
962  if any */
963  parent=*lastSample;
964 
965  #if (!EXPLORATION_RRT)
966  dg=DistanceTopology(rrt->m,rrt->tp,q_next,goal);
967  goalReached=(dg<rrt->delta);
968  #endif
969 
970  if ((!ccRRT)&&(!goalReached))
971  {
972  DifferenceVectorTopology(rrt->m,rrt->tp,
973  q_rand,q_next,v);
974  /* d1 computed above =
975  distance(q_rand_q_next)*/
976  if (d1>rrt->delta)
977  ScaleVector(rrt->delta/d1,rrt->m,v);
978  }
979  }
980  #if ((RRT_VERBOSE)||(GET_RRT_STATISTICS))
981  else
982  {
983  #if (GET_RRT_STATISTICS)
984  NewRRTCollision(rst);
985  #endif
986  #if (RRT_VERBOSE)
987  fprintf(stderr," Configuration in collision.\n");
988  #endif
989  }
990  #endif
991  }
992  #if ((RRT_VERBOSE)||(GET_RRT_STATISTICS))
993  else
994  {
995  #if (GET_RRT_STATISTICS)
996  NewRRTHighCost(rst);
997  #endif
998  #if (RRT_VERBOSE)
999  fprintf(stderr," Configuration cost too high.\n");
1000  #endif
1001  }
1002  #endif
1003  }
1004  #if ((RRT_VERBOSE)||(GET_RRT_STATISTICS))
1005  else
1006  {
1007  #if (GET_RRT_STATISTICS)
1008  NewRRTOutOfDomain(rst);
1009  #endif
1010  #if (RRT_VERBOSE)
1011  fprintf(stderr," Configuration not in domain.\n");
1012  #endif
1013  }
1014  #endif
1015  }
1016  #if ((RRT_VERBOSE)||(GET_RRT_STATISTICS))
1017  else
1018  {
1019  #if (GET_RRT_STATISTICS)
1021  #endif
1022  #if (RRT_VERBOSE)
1023  fprintf(stderr," Distance to q_rand increases (or stalled)\n");
1024  #endif
1025  }
1026  #endif
1027  }
1028  #if ((RRT_VERBOSE)||(GET_RRT_STATISTICS))
1029  else
1030  {
1031  #if (GET_RRT_STATISTICS)
1032  NewRRTTooFar(rst);
1033  #endif
1034  #if (RRT_VERBOSE)
1035  fprintf(stderr," Previous configuration is too far (too large advance step)\n");
1036  #endif
1037  }
1038  #endif
1039  }
1040  #if ((RRT_VERBOSE)||(GET_RRT_STATISTICS))
1041  else
1042  {
1043  #if (GET_RRT_STATISTICS)
1044  NewRRTStalled(rst);
1045  #endif
1046  #if (RRT_VERBOSE)
1047  fprintf(stderr," Previous configuration is too close (small advance step)\n");
1048  #endif
1049  }
1050  #endif
1051  }
1052  #if ((RRT_VERBOSE)||(GET_RRT_STATISTICS))
1053  else
1054  {
1055  #if (GET_RRT_STATISTICS)
1057  #endif
1058  #if (RRT_VERBOSE)
1059  fprintf(stderr," Sample does not converge to manifold.\n");
1060  #endif
1061  }
1062  #endif
1063  it++;
1064  }
1065 
1066  if (rrt->dd>0)
1067  AdjustDynamicDomain(i_near,inCollision,rrt);
1068 
1069  free(v);
1070  free(q_next);
1071  free(q_tmp);
1072 
1073  return(goalReached);
1074 }
1075 
1076 
1077 unsigned int ReconstructRRTPath(Tparameters *pr,unsigned int sID,double *pl,double *pc,
1078  unsigned int *ns,double ***path,Trrt *rrt)
1079 {
1080  Tvector steps;
1081  unsigned int nvs;
1082 
1083  RRTPathSteps(sID,&steps,rrt);
1084 
1085  nvs=Steps2PathinRRT(pr,&steps,pl,pc,ns,path,rrt);
1086  ReverseSamples(*ns,*path);
1087 
1088  DeleteVector(&steps);
1089 
1090  return(nvs);
1091 }
1092 
1093 
1094 unsigned int Steps2PathinRRT(Tparameters *pr,Tvector *steps,double *pl,double *pc,
1095  unsigned int *ns,double ***path,Trrt *rrt)
1096 {
1097  TRRTStep *st;
1098  unsigned int nv,nvs,ms,n,i,k,current,next;
1099  signed int j;
1100  double *o,c;
1101  boolean *systemVars;
1102  unsigned int lns;
1103  double **lpath;
1104  boolean reached,collision;
1105  double epsilon,dc;
1106 
1107  *pl=0; /* path length */
1108  if (pc!=NULL) /* path cost (mechanical work) */
1109  {
1110  *pc=0;
1111  epsilon=GetParameter(CT_EPSILON,pr);
1112  }
1113 
1114  /* Identify system variables */
1115  nv=CS_WD_GET_SYSTEM_VARS(&systemVars,rrt->w);
1116  nvs=0;
1117  for(i=0;i<nv;i++)
1118  {
1119  if (systemVars[i])
1120  nvs++;
1121  }
1122 
1123  InitSamples(&ms,ns,path);
1124 
1125  st=(TRRTStep *)GetVectorElement(0,steps);
1126  current=st->id;
1127  n=VectorSize(steps);
1128  k=1;
1129  while (k<n)
1130  {
1131  st=(TRRTStep *)GetVectorElement(k,steps);
1132  next=st->id;
1133 
1134  CS_WD_REGENERATE_ORIGINAL_POINT(pr,rrt->si[current]->samples,&o,rrt->w);
1135  AddSample2Samples(nv,o,nvs,systemVars,&ms,ns,path);
1136  free(o);
1137 
1138  c=ConnectSamples(pr,rrt->tp,rrt->ambient,rrt->m,rrt->n,
1139  rrt->si[current]->samples,rrt->si[next]->samples,INF,
1140  FALSE,&reached,&collision,NULL,&lns,&lpath,rrt->w);
1141  #if (RRT_VERBOSE)
1142  fprintf(stderr,"{%u->%u}[%g]",current,next,c);
1143  #endif
1144 
1145  (*pl)+=c; /* Update the path length */
1146  if(pc!=NULL) /* Update the path cost (mechanical work) */
1147  {
1148  dc=rrt->si[current]->cost-rrt->si[next]->cost;
1149  (*pc)+=(dc>0?dc:0)+epsilon*c;
1150  }
1151 
1152  if (!reached)
1153  Warning("Path segment could not be reconstructed in Steps2PathinRRT");
1154  else
1155  {
1156  for(j=0;j<lns;j++)
1157  {
1158  CS_WD_REGENERATE_ORIGINAL_POINT(pr,lpath[j],&o,rrt->w);
1159  AddSample2Samples(nv,o,nvs,systemVars,&ms,ns,path);
1160  free(o);
1161  }
1162  DeleteSamples(lns,lpath);
1163  }
1164 
1165  current=next;
1166  k++;
1167  }
1168  if (n>1)
1169  {
1170  /* Ensure that the last sample is also added (ConnectSamples stops close to the
1171  goal but not exactly on it) */
1172  CS_WD_REGENERATE_ORIGINAL_POINT(pr,rrt->si[current]->samples,&o,rrt->w);
1173  AddSample2Samples(nv,o,nvs,systemVars,&ms,ns,path);
1174  free(o);
1175  }
1176 
1177  free(systemVars);
1178 
1179  return(nvs);
1180 }
1181 
1182 double RRTPathLength(Tparameters *pr,unsigned int sID,Trrt *rrt)
1183 {
1184  unsigned int i;
1185  double pl;
1186 
1187  pl=0;
1188  i=sID;
1189  while (i!=NO_UINT)
1190  {
1191  if (rrt->si[i]->parent!=NO_UINT)
1192  pl+=DistanceTopology(rrt->m,rrt->tp,
1193  rrt->si[i]->samples,rrt->si[rrt->si[i]->parent]->samples);
1194  i=rrt->si[i]->parent;
1195  }
1196 
1197  return(pl);
1198 }
1199 
1200 /**********************************************************************************/
1201 /**********************************************************************************/
1202 /**********************************************************************************/
1203 
1204 void InitRRT(Tparameters *pr,boolean parallel,boolean simp,double *ps,
1205  unsigned int mode,boolean graph,double *pg,
1206  unsigned int m,TAtlasBase *w,Trrt *rrt)
1207 {
1208  double epsilon,er,*pWithDummies;
1209  unsigned int i;
1210  boolean collision=FALSE;
1211  unsigned int samplingMode;
1212  double dr;
1213 
1214  #if (EXPLORATION_RRT)
1215  if (mode!=ONE_TREE)
1216  Error("An EXPLORATION_RRT must be one directional");
1217  #endif
1218 
1219  /* Init the RRT structure */
1220  epsilon=GetParameter(CT_EPSILON,pr);
1221  samplingMode=(unsigned int)GetParameter(CT_SAMPLING,pr);
1223 
1224  /* Set up the possible parallel execution */
1225  rrt->parallel=FALSE; /* The default is not to execute in parallel */
1226  #ifdef _OPENMP
1227  if (parallel)
1228  {
1229  rrt->nCores=omp_get_max_threads();
1230  /* even if having many cores ans OpenMP we only execute in parallel
1231  large problems. */
1232  rrt->parallel=(rrt->nCores>1);
1233  }
1234  #endif
1235  //rrt->parallel=FALSE;
1236  if (!rrt->parallel)
1237  rrt->nCores=1;
1238 
1239  fprintf(stderr,"Number of computing cores (rrt) : %u\n",rrt->nCores);
1240 
1241  /* The base information with the equations, collions,... */
1242  rrt->w=w;
1243 
1244  /* one_tree two_trees, two_trees_with_swap */
1245  rrt->mode=mode;
1246  rrt->graph=graph;
1247  /* in kdtree sampling mode the dynamic domain is fixed
1248  by the kdtree, and not in the samples. */
1249  if (samplingMode==KDTREE_SAMPLING)
1250  rrt->dd=0.0;
1251  else
1252  rrt->dd=dr;
1253 
1254  /* only used for t-rrt */
1256  rrt->nFail=0;
1257 
1258  /* Information for each sample */
1259  rrt->ms=INIT_NUM_SAMPLES_RRT;
1260  rrt->ns=0;
1261  NEW(rrt->si,rrt->ms,TRRTSampleInfo*);
1262  for(i=0;i<rrt->ms;i++)
1263  rrt->si[i]=NULL;
1264 
1265  /* Initialize the collision detection, just in case */
1266  CS_WD_INIT_CD(pr,rrt->nCores,rrt->w);
1267 
1268  /* Box enclosing the part of the manifold to explore */
1269  NEW(rrt->ambient,1,Tbox);
1271 
1272  NEW(rrt->si[0],1,TRRTSampleInfo);
1273  if (simp)
1274  {
1275  /* If the input point is already in simplified form, use
1276  it directly. This is not the usual case. */
1277  rrt->m=m;
1278  NEW(rrt->si[0]->samples,rrt->m,double);
1279  memcpy(rrt->si[0]->samples,ps,sizeof(double)*rrt->m);
1280  if (rrt->mode!=ONE_TREE)
1281  {
1282  /* In bi-directional trees the goal is also added */
1283  NEW(rrt->si[1],1,TRRTSampleInfo);
1284  NEW(rrt->si[1]->samples,rrt->m,double);
1285  memcpy(rrt->si[1]->samples,pg,sizeof(double)*rrt->m);
1286  }
1287  }
1288  else
1289  {
1290  /* Simplify the input point and add it to the tree */
1291  CS_WD_REGENERATE_SOLUTION_POINT(pr,ps,&pWithDummies,rrt->w);
1292  rrt->m=CS_WD_GENERATE_SIMPLIFIED_POINT(pr,pWithDummies,
1293  &(rrt->si[0]->samples),rrt->w);
1294  free(pWithDummies);
1295 
1296  if (rrt->mode!=ONE_TREE)
1297  {
1298  /* In bi-directional trees the goal is also added */
1299  NEW(rrt->si[1],1,TRRTSampleInfo);
1300  CS_WD_REGENERATE_SOLUTION_POINT(pr,pg,&pWithDummies,rrt->w);
1301  CS_WD_GENERATE_SIMPLIFIED_POINT(pr,pWithDummies,
1302  &(rrt->si[1]->samples),rrt->w);
1303  free(pWithDummies);
1304  }
1305  }
1306 
1307  // rrt->m dimension of the ambient space
1308  rrt->k=(unsigned int)GetParameter(CT_N_DOF,pr); //dimension of the manifold
1309  rrt->n=rrt->m-rrt->k; //num of independent equations
1310  rrt->delta=GetParameter(CT_DELTA,pr);
1311 
1312  SetRRTTopology(pr,rrt);
1313 
1314  if ((!PointInBoxTopology(NULL,TRUE,rrt->m,rrt->si[0]->samples,0,rrt->tp,rrt->ambient))||
1315  (!CS_WD_SIMP_INEQUALITIES_HOLD(pr,rrt->si[0]->samples,rrt->w)))
1316  Error("The start point is not in domain");
1317 
1318  /* Verify the start/goal samples, now that they are already simplified
1319  and added to the RRT */
1320  if (rrt->n==0)
1321  er=0;
1322  else
1323  er=CS_WD_ERROR_IN_SIMP_EQUATIONS(pr,rrt->si[0]->samples,rrt->w);
1324  if (er>epsilon)
1325  Error("The start point is not on the manifold");
1326 
1327  CS_WD_IN_COLLISION(collision,pr,rrt->si[0]->samples,NULL,rrt->w);
1328  if (collision)
1329  Error("Start point for the RRT is in collision");
1330 
1331  rrt->si[0]->costp=0.0;
1332  rrt->si[0]->cost=0.0;
1333  rrt->si[0]->ddr=0.0;
1334  rrt->si[0]->tree=START2GOAL;
1335  rrt->si[0]->parent=NO_UINT;
1336  if (rrt->graph)
1337  {
1338  rrt->si[0]->g=0;
1339  rrt->si[0]->m=10; /* initial space for 10 neighbours (extended when necessary) */
1340  rrt->si[0]->nn=0;
1341  NEW(rrt->si[0]->n,rrt->si[0]->m,unsigned int);
1342  NEW(rrt->si[0]->cn,rrt->si[0]->m,double);
1343  }
1344  rrt->ns=1;
1345 
1346  if (rrt->mode!=ONE_TREE)
1347  {
1348  rrt->si[1]->costp=0.0;
1349  rrt->si[1]->cost=0.0;
1350  rrt->si[1]->ddr=0.0;
1351  rrt->si[1]->tree=GOAL2START;
1352  rrt->si[1]->parent=NO_UINT;
1353  if (rrt->graph)
1354  {
1355  rrt->si[1]->g=0;
1356  rrt->si[1]->m=10; /* initial space for 10 neighbours (extended when necessary) */
1357  rrt->si[1]->nn=0;
1358  NEW(rrt->si[1]->n,rrt->si[1]->m,unsigned int);
1359  NEW(rrt->si[1]->cn,rrt->si[1]->m,double);
1360  }
1361  rrt->ns=2;
1362 
1363  if ((!PointInBoxTopology(NULL,TRUE,rrt->m,rrt->si[1]->samples,0,rrt->tp,rrt->ambient))||
1364  (!CS_WD_SIMP_INEQUALITIES_HOLD(pr,rrt->si[1]->samples,rrt->w)))
1365  Error("The goal point is not in domain");
1366 
1367  if (rrt->n==0)
1368  er=0;
1369  else
1370  er=CS_WD_ERROR_IN_SIMP_EQUATIONS(pr,rrt->si[1]->samples,rrt->w);
1371  if (er>epsilon)
1372  Error("The goal point is not on the manifold");
1373 
1374  CS_WD_IN_COLLISION(collision,pr,rrt->si[1]->samples,NULL,rrt->w);
1375  if (collision)
1376  Error("Goal point for the RRT is in collision");
1377  }
1378 
1379  #if (_KDTREE==1)
1380  {
1381  unsigned int nd,i;
1382  Trectangle rec;
1383  Tinterval *range;
1384 
1385  nd=GetBoxNIntervals(rrt->ambient);
1386  InitRectangle(nd,NULL,NULL,&rec);
1387  for(i=0;i<nd;i++)
1388  {
1389  range=GetBoxInterval(i,rrt->ambient);
1390  SetRectangleLimits(i,LowerLimit(range),UpperLimit(range),&rec);
1391  }
1392  i=0;
1393  rrt->treeS2G=InitKDTree(&rec,rrt->tp,25,dr,1,&(rrt->si[0]->samples),&i);
1394 
1395  if (rrt->mode!=ONE_TREE)
1396  {
1397  i=1;
1398  if (rrt->mode==TWO_TREES)
1399  rrt->treeG2S=InitKDTree(&rec,rrt->tp,25,dr,1,&(rrt->si[1]->samples),&i);
1400  else
1401  /* TWO_TREES_WITH_SWAP -> only one tree is used */
1402  AddPoint2KDtree(rrt->si[1]->samples,i,&(rrt->treeS2G));
1403  }
1404  else
1405  rrt->treeG2S=NULL;
1406 
1407  DeleteRectangle(&rec);
1408  }
1409  #endif
1410 
1411  #if (_KDTREE==2)
1412  /* Create the start 2 goal tree */
1413  if ((!RRT_NN_TOPOLOGY)||(rrt->tp==NULL))
1414  rrt->treeS2G=CreateKDTree(rrt->m);
1415  else
1416  rrt->treeS2G=CreateKDTreeT(rrt->m,(int *)rrt->tp);
1417 
1418  AddPoint2KDTree(rrt->si[0]->samples,rrt->treeS2G);
1419  if (rrt->mode!=ONE_TREE)
1420  {
1421  /* and the goal to start, if necessary */
1422  if (rrt->mode==TWO_TREES)
1423  {
1424  if ((!RRT_NN_TOPOLOGY)||(rrt->tp==NULL))
1425  rrt->treeG2S=CreateKDTree(rrt->m);
1426  else
1427  rrt->treeG2S=CreateKDTreeT(rrt->m,(int *)rrt->tp);
1428 
1429  AddPoint2KDTree(rrt->si[1]->samples,rrt->treeG2S);
1430 
1431  rrt->maxNodesT1=rrt->ms;
1432  rrt->maxNodesT2=rrt->ms;
1433 
1434  NEW(rrt->t1ToId,rrt->maxNodesT1,unsigned int);
1435  NEW(rrt->t2ToId,rrt->maxNodesT2,unsigned int);
1436 
1437  rrt->t1ToId[0]=0;
1438  rrt->t2ToId[0]=1;
1439 
1440  rrt->nodesT1=1;
1441  rrt->nodesT2=1;
1442  }
1443  else
1444  {
1445  /* TWO_TREES_WITH_SWAP -> only one kd-tree is used
1446  to speed up the search for nearest neighbours,
1447  independent of the tree. Searches in a particular
1448  three, though, will be serial */
1449  AddPoint2KDTree(rrt->si[1]->samples,rrt->treeS2G);
1450  }
1451  }
1452  else
1453  {
1454  rrt->treeG2S=NULL;
1455  rrt->t1ToId=NULL;
1456  rrt->t2ToId=NULL;
1457  }
1458  #endif
1459 }
1460 
1462 {
1463  return(rrt->temperature);
1464 }
1465 
1466 boolean IsRRTGraph(Trrt *rrt)
1467 {
1468  return(rrt->graph);
1469 }
1470 
1471 boolean RRTSample(unsigned int samplingMode,unsigned int tree,
1472  double *goal,double *q_rand,
1473  TRRTStatistics *rst,Trrt *rrt)
1474 {
1475  boolean expand2Goal;
1476 
1477  #if (GET_RRT_STATISTICS)
1478  if (rst!=NULL)
1479  NewRRTRandomSample(rst);
1480  #endif
1481 
1482  expand2Goal=((goal!=NULL)&&(randomDouble()<0.01));
1483  if (expand2Goal)
1484  {
1485  memcpy(q_rand,goal,rrt->m*sizeof(double));
1486  #if (RRT_VERBOSE)
1487  fprintf(stderr," Expanding to goal----------------------------------\n");
1488  #endif
1489  }
1490  else
1491  {
1492  #if (_KDTREE==1)
1493  if (samplingMode==KDTREE_SAMPLING)
1494  {
1495  if ((rrt->mode==ONE_TREE)||(tree==START2GOAL)||
1496  ((tree==BOTHTREES)&&(randomDouble()<0.5)))
1497  SampleInKDtree(q_rand,rrt->treeS2G);
1498  else
1499  SampleInKDtree(q_rand,rrt->treeG2S);
1500  }
1501  else
1502  #endif
1503  /* samplingMode != KDTREE_SAMPLING (AMBIENT or TANGENT) */
1504  RandomPointInBox(NULL,q_rand,rrt->ambient);
1505  }
1506 
1507  return(expand2Goal);
1508 }
1509 
1510 boolean RRTValidateSample(Tparameters *pr,double *q_rand,unsigned int tree,boolean expand2goal,
1511  double *goal,double l,double *h,unsigned int *i_near,
1512  TRRTStatistics *rst,Trrt *rrt)
1513 {
1514  boolean validSample=TRUE;
1515 
1516  /* First check the optimality heuristics, if any */
1517  if ((expand2goal)||(goal==NULL))
1518  *h=0.0;
1519  else
1520  {
1521  *h=DistanceTopology(rrt->m,rrt->tp,q_rand,goal);
1522  if ((l<INF)&&((HEURISTIC_RRT_STAR)&(1)))
1523  validSample=((DistanceTopology(rrt->m,rrt->tp,rrt->si[0]->samples,q_rand)+*h)<=l);
1524  }
1525 
1526  /* Now check the domain */
1527  if (validSample)
1528  validSample=((expand2goal)||(CS_WD_SIMP_INEQUALITIES_HOLD(pr,q_rand,rrt->w)));
1529 
1530  /* Now the NN conditions (dynamic domain) */
1531  if (validSample)
1532  {
1533  *i_near=GetRRTNN(tree,q_rand,rrt);
1534 
1535  if (rrt->dd>0)
1536  validSample=InDynamicDomain(*i_near,q_rand,rrt);
1537  }
1538 
1539  #if (GET_RRT_STATISTICS)
1540  if (!validSample)
1541  NewRRTSampleRejection(rst);
1542  #endif
1543 
1544  return(validSample);
1545 }
1546 
1547 unsigned int GetRRTNN(unsigned int tree,double *q_rand,Trrt *rrt)
1548 {
1549  #if (_KDTREE==1)
1550  if ((rrt->mode!=TWO_TREES_WITH_SWAP)||(tree==BOTHTREES))
1551  {
1552  unsigned int inear;
1553 
1554  if (rrt->mode==TWO_TREES)
1555  {
1556  if (tree==START2GOAL)
1557  inear=NearestNeighbour(q_rand,rrt->treeS2G);
1558  else
1559  {
1560  if (tree==GOAL2START)
1561  inear=NearestNeighbour(q_rand,rrt->treeG2S);
1562  else
1563  {
1564  /* BOTHTREES */
1565  unsigned int inear1,inear2;
1566  double d1,d2;
1567 
1568  inear1=NearestNeighbour(q_rand,rrt->treeS2G);
1569  inear2=NearestNeighbour(q_rand,rrt->treeG2S);
1570 
1571  d1=SquaredDistanceTopology(rrt->m,rrt->tp,rrt->si[inear1]->samples,q_rand);
1572  d2=SquaredDistanceTopology(rrt->m,rrt->tp,rrt->si[inear2]->samples,q_rand);
1573 
1574  inear=(d1<d2?inear1:inear2);
1575  }
1576  }
1577  }
1578  else
1579  /* rrt->mode==ONE_TREE or (rrt->mode==TWO_TREES_WITH_SWAP and tree==BOTHTREES) */
1580  inear=NearestNeighbour(q_rand,rrt->treeS2G);
1581 
1582  return(inear);
1583  }
1584  else
1585  #endif
1586 
1587  #if (_KDTREE==2)
1588  if ((rrt->mode!=TWO_TREES_WITH_SWAP)||(tree==BOTHTREES))
1589  {
1590  double d;
1591  signed int inear;
1592 
1593  if (rrt->mode==TWO_TREES)
1594  {
1595  if (tree==START2GOAL)
1596  {
1597  QueryKDTree(q_rand,&inear,&d,rrt->treeS2G);
1598  inear=rrt->t1ToId[inear];
1599  }
1600  else
1601  {
1602  if (tree==GOAL2START)
1603  {
1604  QueryKDTree(q_rand,&inear,&d,rrt->treeG2S);
1605  inear=rrt->t2ToId[inear];
1606  }
1607  else
1608  {
1609  /* NN in any of the two trees */
1610  double d1,d2;
1611  signed int inear1,inear2;
1612 
1613  QueryKDTree(q_rand,&inear1,&d1,rrt->treeG2S);
1614  QueryKDTree(q_rand,&inear2,&d2,rrt->treeS2G);
1615 
1616  if (d1<d2)
1617  inear=rrt->t1ToId[inear1];
1618  else
1619  inear=rrt->t2ToId[inear2];
1620  }
1621  }
1622  }
1623  else
1624  /* rrt->mode==ONE_TREE or (rrt->mode==TWO_TREES_WITH_SWAP and tree==BOTHTREES) */
1625  QueryKDTree(q_rand,&inear,&d,rrt->treeS2G);
1626 
1627  return((unsigned int)inear);
1628  }
1629  else
1630  #endif
1631  {
1632  unsigned int j,inear;
1633  double d,d1;
1634 
1635  /* _KDTREE==0 or (BOTH_TREES_WITH_SWAP and tree!=BOTH_TREES) */
1636  d=INF;
1637  inear=NO_UINT;
1638  for(j=0;j<rrt->ns;j++)
1639  {
1640  if ((rrt->mode==ONE_TREE)||(rrt->si[j]->tree&tree))
1641  {
1642  d1=DistanceTopologyMin(d,rrt->m,rrt->tp,q_rand,rrt->si[j]->samples);
1643  if (d1<d)
1644  {
1645  d=d1;
1646  inear=j;
1647  }
1648  }
1649  }
1650  return(inear);
1651  }
1652 }
1653 
1654 void GetRRTNNInBall(unsigned int tree,double *q_rand,double r,
1655  unsigned int *nn,unsigned int **n,Trrt *rrt)
1656 {
1657  #if (_KDTREE==1)
1658  if ((rrt->mode!=TWO_TREES_WITH_SWAP)||(tree==BOTHTREES))
1659  {
1660  if (rrt->mode==TWO_TREES)
1661  {
1662  if (tree==START2GOAL)
1663  NeighboursInBall(q_rand,r,nn,n,rrt->treeS2G);
1664  else
1665  {
1666  if (tree==GOAL2START)
1667  NeighboursInBall(q_rand,r,nn,n,rrt->treeG2S);
1668  else
1669  {
1670  unsigned int i;
1671  unsigned int nn1,nn2;
1672  unsigned int *n1,*n2;
1673 
1674  NeighboursInBall(q_rand,r,&nn1,&n1,rrt->treeS2G);
1675  NeighboursInBall(q_rand,r,&nn2,&n2,rrt->treeG2S);
1676 
1677  *nn=nn1+nn2;
1678  NEW(*n,*nn,unsigned int);
1679  for(i=0;i<nn1;i++)
1680  (*n)[i]=n1[i];
1681 
1682  for(i=0;i<nn2;i++)
1683  (*n)[nn1+i]=n2[i];
1684  }
1685  }
1686  }
1687  else
1688  /* rrt->mode==ONE_TREE or (rrt->mode==TWO_TREES_WITH_SWAP and tree==BOTHTREES) */
1689  NeighboursInBall(q_rand,r,nn,n,rrt->treeS2G);
1690  }
1691  else
1692  #endif
1693  #if (_KDTREE==2)
1694  if ((rrt->mode!=TWO_TREES_WITH_SWAP)||(tree==BOTHTREES))
1695  {
1696  double *dst;
1697 
1698  if (rrt->mode==TWO_TREES)
1699  {
1700  unsigned int i;
1701 
1702  if (tree==START2GOAL)
1703  {
1704  QueryKDTreeR(q_rand,r,(int *)(nn),(int **)(n),&dst,rrt->treeS2G);
1705  free(dst);
1706 
1707  for(i=0;i<*nn;i++)
1708  (*n)[i]=rrt->t1ToId[(*n)[i]];
1709  }
1710  else
1711  {
1712  if (tree==GOAL2START)
1713  {
1714  QueryKDTreeR(q_rand,r,(int *)(nn),(int **)(n),&dst,rrt->treeG2S);
1715  free(dst);
1716 
1717  for(i=0;i<*nn;i++)
1718  (*n)[i]=rrt->t2ToId[(*n)[i]];
1719  }
1720  else
1721  {
1722  /* Both trees */
1723  unsigned int nn1,nn2;
1724  unsigned int *n1,*n2;
1725 
1726  QueryKDTreeR(q_rand,r,(int *)(&nn1),(int **)(&n1),&dst,rrt->treeS2G);
1727  free(dst);
1728  QueryKDTreeR(q_rand,r,(int *)(&nn2),(int **)(&n2),&dst,rrt->treeG2S);
1729  free(dst);
1730 
1731  *nn=nn1+nn2;
1732  NEW(*n,*nn,unsigned int);
1733  for(i=0;i<nn1;i++)
1734  (*n)[i]=rrt->t1ToId[n1[i]];
1735 
1736  for(i=0;i<nn2;i++)
1737  (*n)[nn1+i]=rrt->t2ToId[n2[i]];
1738  }
1739  }
1740  }
1741  else
1742  {
1743  /* rrt->mode==ONE_TREE or (rrt->mode==TWO_TREES_WITH_SWAP and tree==BOTHTREES) */
1744  QueryKDTreeR(q_rand,r,(int *)(nn),(int **)(n),&dst,rrt->treeS2G);
1745  free(dst);
1746  }
1747  }
1748  else
1749  #endif
1750  {
1751  unsigned int m,j;
1752  double d1;
1753 
1754  /* _KDTREE==0 or BOTH_TREES_WITH_SWAP and tree!=BOTH_TREES */
1755  m=10;
1756  *nn=0;
1757  NEW(*n,m,unsigned int);
1758  for(j=0;j<rrt->ns;j++)
1759  {
1760  if ((rrt->mode==ONE_TREE)||(rrt->si[j]->tree&tree))
1761  {
1762  d1=DistanceTopologyMin(r,rrt->m,rrt->tp,q_rand,rrt->si[j]->samples);
1763  if (d1<r)
1764  {
1765  if (*nn==m)
1766  MEM_DUP(*n,m,unsigned int);
1767  (*n)[*nn]=j;
1768  (*nn)++;
1769  }
1770  }
1771  }
1772  }
1773 }
1774 
1775 void GetRRTNNInBranch(unsigned int tree,
1776  unsigned int n1,unsigned int n2,
1777  unsigned int *n,unsigned int *nn,
1778  Trrt *rrt)
1779 {
1780  double md,cd;
1781  unsigned int cn;
1782  int cnn;
1783  boolean done;
1784 
1785  /* Conceptually this operation is used only in bi-directional trees (altough in practice
1786  it could be implemented in single-trees too */
1787  if (rrt->mode==ONE_TREE)
1788  Error("GetRRTNNInBranch is only defined for bidirectional RRTs");
1789 
1790  if (rrt->si[n1]->tree!=rrt->si[n2]->tree)
1791  Error("The two nodes defining the branch in GetRRTNNInBranch are not in the same tree");
1792 
1793  if (tree==rrt->si[n1]->tree)
1794  Error("Parameter missmatch in GetRRTNNInBranch (the branch is in the query tree)");
1795 
1796  if (tree==BOTHTREES)
1797  Error("Can not query both trees in GetRRTNNInBranch");
1798 
1799  cn=n2;
1800  md=INF;
1801  *n=NO_UINT;
1802  *nn=NO_UINT;
1803  done=FALSE;
1804  do {
1805  #if (_KDTREE==1)
1806  if (rrt->mode!=TWO_TREES_WITH_SWAP)
1807  {
1808  if (tree==START2GOAL)
1809  cnn=NearestNeighbour(rrt->si[cn]->samples,rrt->treeS2G);
1810  else
1811  /* tree==GOAL2START */
1812  cnn=NearestNeighbour(rrt->si[cn]->samples,rrt->treeG2S);
1813  }
1814  else
1815  #endif
1816 
1817  #if (_KDTREE==2)
1818  if (rrt->mode!=TWO_TREES_WITH_SWAP)
1819  {
1820  if (tree==START2GOAL)
1821  {
1822  QueryKDTree(rrt->si[cn]->samples,&cnn,&cd,rrt->treeS2G);
1823  cnn=rrt->t1ToId[cnn];
1824  }
1825  else
1826  {
1827  /* tree==GOAL2START */
1828  QueryKDTree(rrt->si[cn]->samples,&cnn,&cd,rrt->treeG2S);
1829  cnn=rrt->t2ToId[cnn];
1830  }
1831  }
1832  else
1833  #endif
1834  {
1835  unsigned int j;
1836  double d1;
1837 
1838  cd=INF;
1839  cnn=NO_UINT;
1840  for(j=0;j<rrt->ns;j++)
1841  {
1842  if (rrt->si[j]->tree==tree)
1843  {
1844  d1=DistanceTopologyMin(md,rrt->m,rrt->tp,rrt->si[cn]->samples,rrt->si[j]->samples);
1845  if (d1<cd)
1846  {
1847  cd=d1;
1848  cnn=j;
1849  }
1850  }
1851  }
1852  }
1853 
1854  if (cd<md)
1855  {
1856  md=cd;
1857  *n=cn;
1858  *nn=cnn;
1859  }
1860  done=(cn==n1);
1861  if (!done)
1862  cn=rrt->si[cn]->parent;
1863  } while((!done)||(cn==NO_UINT));
1864 
1865  if (cn==NO_UINT)
1866  Error("Wrongly delimited branch in GetRRTNNInBranch");
1867 }
1868 
1869 boolean AddNodeToRRT(Tparameters *pr,unsigned int tree,
1870  unsigned int i_near,double *sample,
1871  double *goal,unsigned int *lastSample,
1872  void *info,double costp,double cost,
1873  TRRTStatistics *rst,Trrt *rrt)
1874 {
1875  TRRTSampleInfo *si;
1876 
1877  #if (GET_RRT_STATISTICS)
1878  if (rst!=NULL)
1879  NewRRTSample(rst);
1880  #endif
1881 
1882  if (tree==BOTHTREES)
1883  Error("Samples must be assigned to a single tree in AddNodeToRRT");
1884 
1885  if (rrt->ns==rrt->ms)
1886  MEM_DUP(rrt->si,rrt->ms,TRRTSampleInfo *);
1887 
1888  NEW(rrt->si[rrt->ns],1,TRRTSampleInfo);
1889  si=rrt->si[rrt->ns];
1890 
1891  NEW(si->samples,rrt->m,double);
1892  memcpy(si->samples,sample,rrt->m*sizeof(double));
1893  if (rrt->tp!=NULL)
1894  ArrayPi2Pi(rrt->m,rrt->tp,si->samples);
1895 
1896  si->parent=i_near;
1897  si->userInfo=info;
1898  si->costp=costp;
1899  si->cost=cost;
1900 
1901  si->ddr=0.0; /* dynamic domain set to 0 -> infty. */
1902 
1903  /* the tree of the node can be different from that of its parent!!
1904  This can happen when loading a BiRRT* that is not fully updated
1905  (see \ref UpdateCostAndPath) */
1906  si->tree=tree;
1907 
1908  #if (_KDTREE==1)
1909  if (rrt->mode==TWO_TREES)
1910  {
1911  if (tree==START2GOAL)
1912  AddPoint2KDtree(si->samples,rrt->ns,&(rrt->treeS2G));
1913  else
1914  AddPoint2KDtree(si->samples,rrt->ns,&(rrt->treeG2S));
1915  }
1916  else
1917  AddPoint2KDtree(si->samples,rrt->ns,&(rrt->treeS2G));
1918  #endif
1919 
1920  #if (_KDTREE==2)
1921  if (rrt->mode==TWO_TREES)
1922  {
1923  if (tree==START2GOAL)
1924  {
1925  if (rrt->nodesT1==rrt->maxNodesT1)
1926  MEM_DUP(rrt->t1ToId,rrt->maxNodesT1,unsigned int);
1927  rrt->t1ToId[rrt->nodesT1]=rrt->ns;
1928  rrt->nodesT1++;
1929 
1930  AddPoint2KDTree(si->samples,rrt->treeS2G);
1931  }
1932  else
1933  {
1934  /* tree is GOAL2START */
1935  if (rrt->nodesT2==rrt->maxNodesT2)
1936  MEM_DUP(rrt->t2ToId,rrt->maxNodesT2,unsigned int);
1937  rrt->t2ToId[rrt->nodesT2]=rrt->ns;
1938  rrt->nodesT2++;
1939 
1940  AddPoint2KDTree(si->samples,rrt->treeG2S);
1941  }
1942  }
1943  else
1944  AddPoint2KDTree(si->samples,rrt->treeS2G);
1945  #endif
1946 
1947  *lastSample=rrt->ns;
1948  rrt->ns++;
1949 
1950  if (rrt->graph)
1951  {
1952  si->g=INF;
1953  si->m=10; /* initial space for 10 neighbours (extended when necessary) */
1954  si->nn=0;
1955  NEW(si->n,si->m,unsigned int);
1956  NEW(si->cn,si->m,double);
1957  AddEdgeToRRT(*lastSample,i_near,costp,rrt);
1958  }
1959 
1960  #if (_DEBUG>1)
1961  fprintf(stderr," New sample %u dp:%f\n",rrt->ns,
1962  DistanceTopology(rrt->m,rrt->tp,rrt->si[i_near]->samples,sample));
1963  #endif
1964 
1965  if (goal!=NULL)
1966  return(DistanceTopology(rrt->m,rrt->tp,sample,goal)<GetParameter(CT_EPSILON,pr));
1967  else
1968  return(FALSE);
1969 }
1970 
1971 unsigned int AddStepToRRTstar(Tparameters *pr,unsigned int i_near,
1972  double *q_rand,double *goal,
1973  unsigned int *i_goal,
1974  TRRTStatistics *rst,Trrt *rrt)
1975 {
1976  double *q_next,d;
1977  unsigned int i_next;
1978  boolean goalFound,collision,reached;
1979  unsigned int tree;
1980 
1981  NEW(q_next,rrt->m,double);
1982 
1983  d=ConnectSamples(pr,rrt->tp,rrt->ambient,
1984  rrt->m,rrt->n,rrt->si[i_near]->samples,q_rand,(double)INF,
1985  TRUE,&reached,&collision,q_next,NULL,NULL,rrt->w);
1986 
1987  if (rrt->dd>0)
1988  AdjustDynamicDomain(i_near,collision,rrt);
1989 
1990  i_next=NO_UINT;
1991 
1992  tree=rrt->si[i_near]->tree; /* the sample is assigned to the tree of the nearest-node */
1993 
1994  if (d>0)
1995  {
1996  goalFound=AddNodeToRRT(pr,tree,i_near,q_next,goal,&i_next,NULL,
1997  d,rrt->si[i_near]->cost+d,rst,rrt);
1998 
1999  if ((goal!=NULL)&&(i_goal!=NULL)&&(goalFound)&&(*i_goal==NO_UINT))
2000  {
2001  /* The first time we are close enough to the goal we add the goal as a
2002  node in the RRT */
2003  double d,epsilon;
2004 
2005  epsilon=GetParameter(CT_EPSILON,pr);
2006  d=DistanceTopology(rrt->m,rrt->tp,q_next,goal);
2007 
2008  if (d<epsilon)
2009  *i_goal=i_next;
2010  else
2011  AddNodeToRRT(pr,tree,i_next,goal,goal,i_goal,NULL,d,
2012  rrt->si[i_next]->cost+d,rst,rrt);
2013  #if (RRT_VERBOSE)
2014  fprintf(stderr," {%u->%u}[%g] **\n",*i_goal,rrt->si[*i_goal]->parent,
2015  rrt->si[*i_goal]->costp);
2016  #endif
2017  }
2018  }
2019  free(q_next);
2020 
2021  return(i_next);
2022 }
2023 
2024 void WireRRTstar(Tparameters *pr,unsigned int id_new,unsigned int i_near,
2025  double gamma,unsigned int nn,unsigned int *n,double **c,
2026  double h,Theap *q,unsigned int *t,
2027  TRRTStatistics *rst,Trrt *rrt)
2028 {
2029  unsigned int i;
2030  double ic,cn,c_new,epsilon,*q_new;
2031  boolean *reached;
2032  TRRTSampleInfo *sNew;
2033  TDoublePair pair;
2034  double *cost;
2035 
2036  epsilon=GetParameter(CT_EPSILON,pr);
2037 
2038  /*******************************************************************/
2039  /* Search for the lowest cost path to the new node */
2040  #if (RRT_VERBOSE)
2041  fprintf(stderr," Wire %u [nn:%u g:%g]\n",id_new,nn,gamma);
2042  #endif
2043  #if (RRTSTAR_SYMMETRIC_COST)
2044  if (!rrt->graph)
2045  NEW(*c,nn,double);
2046  #endif
2047  sNew=rrt->si[id_new];
2048  ic=sNew->costp; /*cost to i_near*/
2049  q_new=sNew->samples;
2050  *t=0; /* initially no tree in the set of neighbours */
2051 
2052  NEW(reached,nn,boolean);
2053  NEW(cost,nn,double);
2054 
2055  #pragma omp parallel for private(i) schedule(dynamic) if (rrt->parallel)
2056  for(i=0;i<nn;i++)
2057  {
2058  /* do not consider connection to itself */
2059  if (n[i]==id_new)
2060  reached[i]=FALSE;
2061  else
2062  {
2063  if (n[i]==i_near)
2064  {
2065  /* we already know the connection to i_near */
2066  cost[i]=ic;
2067  reached[i]=TRUE;
2068  }
2069  else
2070  {
2071  boolean collision;
2072 
2073  /* compute the connection to other samples */
2074  cost[i]=ConnectSamples(pr,rrt->tp,rrt->ambient,
2075  rrt->m,rrt->n,rrt->si[n[i]]->samples,q_new,gamma,
2076  TRUE,&(reached[i]),&collision,NULL,NULL,NULL,rrt->w);
2077  }
2078  }
2079  }
2080 
2081  for(i=0;i<nn;i++)
2082  {
2083  if (n[i]!=id_new)
2084  {
2085  #if (GET_RRT_STATISTICS)
2086  NewRRTBranch(rst);
2087  #endif
2088  (*t)|=rrt->si[n[i]]->tree; /* take note of the tree of this neighbour (even if not reached)*/
2089  }
2090 
2091  if (reached[i])
2092  {
2093  cn=cost[i];
2094 
2095  /* Ensure that cost and tree are correct before using this node */
2096  /* This might be unnecessary when in graph mode (but it's cheap) */
2097  #if (GET_RRT_STATISTICS)
2098  NewRRTNoEmptyBranch(rst);
2099  #endif
2100 
2101  #if (!RRTSTAR_UPDATE_COSTS)
2102  if (rrt->mode==TWO_TREES_WITH_SWAP)
2103  #endif
2104  UpdateCostAndTree(n[i],rrt);
2105 
2106  /* Add a bidirectional edge with the given cost. */
2107  if (rrt->graph)
2108  AddEdgeToRRT(id_new,n[i],cn,rrt);
2109 
2110  c_new=rrt->si[n[i]]->cost+cn;
2111  if (c_new<(sNew->cost-epsilon))
2112  {
2113  #if (RRT_VERBOSE)
2114  fprintf(stderr," {%u->%u}[%g] ->",id_new,sNew->parent,sNew->costp);
2115  #endif
2116 
2117  SetRRTCostAndParent(id_new,n[i],cn,c_new,rrt);
2118 
2119  #if (RRT_VERBOSE)
2120  fprintf(stderr,"{%u->%u}[%g]\n",id_new,sNew->parent,sNew->costp);
2121  #endif
2122  }
2123  }
2124  else
2125  cn=INF;
2126 
2127  #if (RRTSTAR_SYMMETRIC_COST)
2128  if (!rrt->graph) /* if graph, the cost are already stored in the graph edges. */
2129  (*c)[i]=cn;
2130  #endif
2131  }
2132  free(reached);
2133  free(cost);
2134 
2135  if (rrt->graph)
2136  {
2137  if (!q)
2138  Error("RRT must be in graph mode to use a heap");
2139  c_new=rrt->si[id_new]->cost;
2140  NewDoublePair(c_new+h,c_new,&pair);
2141  AddElement2Heap(id_new,(void *)&pair,q);
2142  }
2143 }
2144 
2146  Tvector *steps,double *l,
2147  Trrt *rrt)
2148 {
2149  unsigned int id_new,i;
2150  double c_new,epsilon,ct;
2151  TRRTSampleInfo *sNew,*sNear;
2152  TDoublePair pair;
2153 
2154  if (!rrt->graph)
2155  Error("RecursiveReWireRRTstar can only be used if RRT in graph mode");
2156 
2157  epsilon=GetParameter(CT_EPSILON,pr);
2158 
2159  #if (RRT_VERBOSE)
2160  fprintf(stderr," Rewire\n");
2161  #endif
2162  while ((!HeapEmpty(q))&&(FirstInPair((TDoublePair *)GetHeapElement(0,q))<=(*l)))
2163  {
2164  id_new=ExtractMinElement(&pair,q);
2165  if (id_new==NO_UINT)
2166  Error("Reading an inconsistent node from the priority queue in RecursiveReWireRRTstar");
2167 
2168  sNew=rrt->si[id_new];
2169  sNew->g=sNew->cost; /* convert the node to stationary */
2170 
2171  for(i=0;i<sNew->nn;i++)
2172  {
2173  sNear=rrt->si[sNew->n[i]];
2174 
2175  /* ensure that cost and tree are correct before using this node. This is already updated
2176  in the wire but might change during the re-wire */
2177  if (rrt->mode==TWO_TREES_WITH_SWAP)
2178  UpdateCostAndTree(sNew->n[i],rrt);
2179 
2180  /* cost to n[i] via id_new */
2181  c_new=sNew->cost+sNew->cn[i];
2182  if (c_new<(sNear->cost-epsilon)) /* recall that here, cost = rhs */
2183  {
2184  #if (RRT_VERBOSE)
2185  fprintf(stderr," {%u->%u}[%g %g] ->",sNew->n[i],sNear->parent,sNear->costp,sNear->cost);
2186  #endif
2187 
2188  /* sets 'rhs' (cost) but not 'g' -> inconsistent node */
2189  SetRRTCostAndParent(sNew->n[i],id_new,sNew->cn[i],c_new,rrt);
2190 
2191  ct=sNear->cost+DistanceTopology(rrt->m,rrt->tp,sNear->samples,g);
2192  NewDoublePair(ct,sNear->cost,&pair);
2193  UpdateHeapElement(sNew->n[i],(void *)&pair,q); /* updates/adds */
2194 
2195  #if (RRT_VERBOSE)
2196  fprintf(stderr,"{%u->%u}[%g %g]\n",sNew->n[i],sNear->parent,sNear->costp,sNear->cost);
2197  #endif
2198  }
2199  else
2200  {
2201  /* if the node is connected to a sample in a different tree, update the optimal path */
2202  if ((rrt->mode==TWO_TREES_WITH_SWAP)&&(steps!=NULL)&&(sNear->tree!=sNew->tree))
2203  {
2204  c_new+=sNear->cost; /* Total cost of start<->goal via this connection between trees.*/
2205  if (c_new<*l)
2206  *l=ChangeBiRRTSteps(sNew->n[i],id_new,sNew->cn[i],steps,rrt);
2207  }
2208  }
2209  }
2210  }
2211 }
2212 
2214  unsigned int id_new,double gamma,
2215  unsigned int nn,unsigned int *n,double *c,
2216  Tvector *steps,double *l,
2217  Trrt *rrt)
2218 {
2219  unsigned int i,parent;
2220  boolean reached;
2221  double cn,c_new,epsilon;
2222  TRRTSampleInfo *sNew,*sNear;
2223  #if (!RRTSTAR_SYMMETRIC_COST)
2224  double *q_new;
2225  boolean collision;
2226  #endif
2227 
2228  if (rrt->graph)
2229  Error("ReWireRRTstar must not be used if the RRT is in graph mode");
2230 
2231  epsilon=GetParameter(CT_EPSILON,pr);
2232 
2233  #if (RRT_VERBOSE)
2234  fprintf(stderr," Rewire\n");
2235  #endif
2236  sNew=rrt->si[id_new];
2237  #if (!RRTSTAR_SYMMETRIC_COST)
2238  /* if symmetric costs -> cost to neighbours given as parameters */
2239  q_new=sNew->samples;
2240  #endif
2241  parent=sNew->parent;
2242  for(i=0;i<nn;i++)
2243  {
2244  sNear=rrt->si[n[i]];
2245 
2246  /* There is no need to re-wire the best parent of the new node
2247  (it will produce an inf. loop) or the new node itself. */
2248  if ((n[i]==id_new)||(n[i]==parent))
2249  reached=FALSE;
2250  else
2251  {
2252  #if (RRTSTAR_SYMMETRIC_COST)
2253  cn=c[i];
2254  reached=(cn<INF);
2255  #else
2256  cn=ConnectSamples(pr,rrt->tp,rrt->ambient,
2257  rrt->m,rrt->n,q_new,sNear->samples,gamma,
2258  TRUE,&reached,&collision,NULL,NULL,NULL,rrt->w);
2259  #endif
2260  }
2261 
2262  if (reached)
2263  {
2264  /* Ensure that any previous-rewire is propagated to the currently tested node */
2265  if (rrt->mode==TWO_TREES_WITH_SWAP)
2266  UpdateCostAndTree(n[i],rrt);
2267 
2268  c_new=sNew->cost+cn;
2269  if (c_new<sNear->cost-epsilon)
2270  {
2271  #if (RRT_VERBOSE)
2272  fprintf(stderr," {%u->%u}[%g] ->",n[i],sNear->parent,sNear->costp);
2273  #endif
2274 
2275  SetRRTCostAndParent(n[i],id_new,cn,c_new,rrt);
2276 
2277  #if (RRT_VERBOSE)
2278  fprintf(stderr,"{%u->%u}[%g]\n",n[i],sNear->parent,sNear->costp);
2279  #endif
2280  }
2281  else
2282  {
2283  /* if the node is connected to a sample in a different tree, update the optimal path */
2284  if ((rrt->mode==TWO_TREES_WITH_SWAP)&&(steps!=NULL)&&(l!=NULL)&&(sNear->tree!=sNew->tree))
2285  {
2286  c_new+=sNear->cost; /* Total cost of start<->goal via this connection between trees.*/
2287  if (c_new<*l)
2288  *l=ChangeBiRRTSteps(n[i],id_new,cn,steps,rrt);
2289  }
2290  }
2291  }
2292  }
2293 }
2294 
2295 void RRTstarCloseIteration(unsigned int it,unsigned int id_goal,
2296  double time,double gamma,
2297  double *times,double *costs,
2298  Trrt *rrt)
2299 {
2300  if ((RRT_VERBOSE)||((it%1000)==0))
2301  {
2302  if (id_goal!=NO_UINT)
2303  {
2304  fprintf(stderr,"Iteration: %u (s:%u t:%g g:%g c:%g [%g])\n",it,rrt->ns,time,gamma,
2305  rrt->si[id_goal]->cost,CostToRoot(id_goal,rrt));
2306  }
2307  else
2308  fprintf(stderr,"Iteration: %u (s:%u t:%g)\n",it,rrt->ns,time);
2309  fflush(stdout);
2310  }
2311 
2312  if (times!=NULL)
2313  times[it]=time;
2314 
2315  if (costs!=NULL)
2316  {
2317  if (id_goal==NO_UINT)
2318  costs[it]=-1.0;
2319  else
2320  costs[it]=rrt->si[id_goal]->cost; //CostToRoot(id_goal,rrt);
2321  }
2322 }
2323 
2324 void BiRRTstarCloseIteration(unsigned int it,double l,
2325  double time,double gamma,
2326  double *times,double *costs,
2327  Trrt *rrt)
2328 {
2329  if ((RRT_VERBOSE)||((it%1000)==0))
2330  {
2331  if (l<INF)
2332  {
2333  fprintf(stderr,"Iteration: %u (s:%u t:%g g:%g c:%g)\n",it,rrt->ns,time,gamma,l);
2334  }
2335  else
2336  fprintf(stderr,"Iteration: %u (s:%u t:%g)\n",it,rrt->ns,time);
2337  fflush(stdout);
2338  }
2339 
2340  if (times!=NULL)
2341  times[it]=time;
2342 
2343  if (costs!=NULL)
2344  {
2345  if (l<INF)
2346  costs[it]=-1.0;
2347  else
2348  costs[it]=l;
2349  }
2350 }
2351 
2352 boolean RRTstar(Tparameters *pr,double *pg,
2353  unsigned int *it,double *times,double *costs,
2354  double *planningTime,double *pl,unsigned int *ns,double ***path,
2355  TRRTStatistics *grst,Trrt *rrt)
2356 {
2357  if (EXPLORATION_RRT)
2358  Error("RRTstar is not designed for exploration RRTs");
2359 
2360  if (rrt->mode!=ONE_TREE)
2361  return(BiRRTstar(pr,pg,it,times,costs,planningTime,pl,ns,path,grst,rrt));
2362  else
2363  {
2364  double *pWithDummies,*pgs,*q_rand;
2365  unsigned int samplingMode;
2366  unsigned int i_near;
2367  double epsilon;
2368  boolean expand2Goal;
2369  double er;
2370  Tstatistics st; /* used just to measure execution time */
2371  boolean collision;
2372  double time;
2373  double *c; /* if the cost function is symmetrical, we cache
2374  the evaluation of the cost to neighbour. */
2375  /* neighbours for each new node */
2376  unsigned int nn;
2377  unsigned int *n;
2378  /* distance q_rand,tree */
2379  double gamma,ct_gamma;
2380  unsigned int i,id_new,id_goal;
2381  double *q_new;
2382  boolean done,optimal,validSample;
2383  double maxTime;
2384  unsigned int maxIterations;
2385  double h,l;
2386  Theap q;
2387  unsigned int vt; /* trees in set of neighbours, only relevant in bi-RRTs */
2388  unsigned int numSamples;
2389  TRRTStatistics *rst;
2390 
2391  if (rrt->ns>1)
2392  Error("RRTstart must be used to a just initilized RRT");
2393 
2394  #if (GET_RRT_STATISTICS)
2395  {
2396  unsigned int i;
2397 
2398  NEW(rst,1,TRRTStatistics);
2399  InitRRTStatistics(rst);
2400 
2401  for(i=0;i<rrt->ns;i++)
2402  NewRRTSample(rst);
2403  }
2404  #endif
2405 
2406  /* Init the RRT structure */
2407  epsilon=GetParameter(CT_EPSILON,pr);
2408  ct_gamma=GetParameter(CT_GAMMA,pr);
2409  maxTime=GetParameter(CT_MAX_PLANNING_TIME,pr);
2410  maxIterations=(unsigned int)GetParameter(CT_MAX_PLANNING_ITERATIONS,pr);
2411  samplingMode=(unsigned int)GetParameter(CT_SAMPLING,pr);
2412 
2413 
2414  CS_WD_REGENERATE_SOLUTION_POINT(pr,pg,&pWithDummies,rrt->w);
2415  CS_WD_GENERATE_SIMPLIFIED_POINT(pr,pWithDummies,&pgs,rrt->w);
2416 
2417  if ((!PointInBoxTopology(NULL,TRUE,rrt->m,pgs,0,rrt->tp,rrt->ambient))||
2418  (!CS_WD_SIMP_INEQUALITIES_HOLD(pr,pgs,rrt->w)))
2419  Error("The targed point for the RRTstar is not in domain");
2420 
2421  if (rrt->n==0)
2422  er=0;
2423  else
2424  er=CS_WD_ERROR_IN_SIMP_EQUATIONS(pr,pgs,rrt->w);
2425  if (er>epsilon)
2426  Error("The target point for the RRTstar is not on the manifold");
2427 
2428  collision=CS_WD_ORIGINAL_IN_COLLISION(pr,pWithDummies,NULL,rrt->w);
2429  if (collision)
2430  Error("The target point for the RRTstar is in collision");
2431  free(pWithDummies);
2432 
2433  /* Room for the randomly generated samples (and its correspoinding point
2434  on the manifold) */
2435  NEW(q_rand,rrt->m,double);
2436 
2437  /* Reset the output statistics (just in case maxIterations
2438  is not reached) */
2439  for(i=0;i<maxIterations;i++)
2440  {
2441  times[i]=0;
2442  costs[i]=-1;
2443  }
2444 
2445  if (rrt->graph)
2447  LessThanDoublePair,NULL,TRUE,rrt->ms,&q);
2448 
2449  gamma=ct_gamma;
2450  optimal=(gamma>0.0);
2451 
2452  id_goal=NO_UINT;
2453  *it=0;
2454 
2455  InitStatistics(rrt->nCores,0,&st);
2456  time=0.0;
2457  done=FALSE;
2458  l=INF; /* cost to goal (INF while goal not found) */
2459  c=NULL; /* default -> no cache of costs to neighbours */
2460 
2461  while ((!done)&&(time<maxTime)&&(*it<maxIterations))
2462  {
2463  #if (HEURISTIC_RRT_STAR&(1))
2464  /* keep the cost to goal up to date so that the heuristic
2465  is more effective*/
2466  if (id_goal!=NO_UINT)
2467  UpdateCostAndTree(id_goal,rrt);
2468  #endif
2469  /*******************************************************************/
2470  /* best estimation of the cost to goal (up to now) */
2471  if (id_goal==NO_UINT)
2472  l=INF;
2473  else
2474  l=rrt->si[id_goal]->cost;
2475 
2476  /*******************************************************************/
2477  /* Random sample */
2478  do {
2479  /* Generate a random sample */
2480  expand2Goal=RRTSample(samplingMode,START2GOAL,(id_goal==NO_UINT?pgs:NULL),
2481  q_rand,rst,rrt);
2482 
2483  /* Check if the sample is valid */
2484  validSample=RRTValidateSample(pr,q_rand,START2GOAL,expand2Goal,pgs,l,
2485  &h,&i_near,rst,rrt);
2486  time=GetElapsedTime(&st);
2487  } while((!validSample)&&(time<maxTime));
2488 
2489  if (time<maxTime)
2490  {
2491  /*******************************************************************/
2492  /* Extend the RRT from q_near to q_rand using the cbiRRT extend method */
2493  /* Note that here we try to reach q_rand but we do not add samples along the path */
2494  id_new=AddStepToRRTstar(pr,i_near,q_rand,pgs,&id_goal,rst,rrt);
2495 
2496  /*******************************************************************/
2497  if (!optimal)
2498  {
2499  if (id_goal!=NO_UINT)
2500  done=TRUE;
2501  }
2502  else
2503  {
2504  /* If we actually added something */
2505  if ((id_new!=NO_UINT)&&((((HEURISTIC_RRT_STAR)&(2))==0)||(id_goal!=NO_UINT)))
2506  {
2507  /* q_new is the last sample added to the tree */
2508  q_new=rrt->si[id_new]->samples;
2509  h=DistanceTopology(rrt->m,rrt->tp,q_new,pgs);
2510 
2511  /*******************************************************************/
2512  /* Search for the set of potential neighbours */
2513  numSamples=(rrt->ns<3?3:rrt->ns);
2514  gamma=ct_gamma*pow(log(numSamples)/numSamples,1/(double)rrt->k);
2515  gamma=(gamma<rrt->delta?rrt->delta:gamma);
2516 
2517  GetRRTNNInBall(START2GOAL,q_new,gamma,&nn,&n,rrt);
2518 
2519  /*******************************************************************/
2520  /* Wire: Search for the lowest cost path to the new node */
2521  WireRRTstar(pr,id_new,i_near,gamma,nn,n,&c,h,(rrt->graph?&q:NULL),&vt,rst,rrt);
2522 
2523  /*******************************************************************/
2524  /* Rewire the tree around the new point */
2525  if (rrt->graph)
2526  RecursiveReWireRRTstar(pr,&q,pgs,NULL,&l,rrt);
2527  else
2528  ReWireRRTstar(pr,id_new,gamma,nn,n,c,NULL,&l,rrt);
2529 
2530  /*******************************************************************/
2531  /* release the data (cost (if actually used) and neighbours) */
2532  if (c!=NULL)
2533  free(c);
2534  free(n);
2535  }
2536  #if (RRT_VERBOSE)
2537  else
2538  {
2539  if (id_new==NO_UINT)
2540  fprintf(stderr," Blocked [%u]\n",i_near);
2541  }
2542  #endif
2543  }
2544 
2545  /*******************************************************************/
2546  /* Conclude the iteration */
2547  time=GetElapsedTime(&st);
2548 
2549  RRTstarCloseIteration(*it,id_goal,time,gamma,times,costs,rrt);
2550 
2551  (*it)++;
2552  }
2553  }
2554 
2555  DeleteStatistics(&st);
2556  #if (GET_RRT_STATISTICS)
2557  if (grst==NULL)
2558  PrintRRTStatistics(rrt,rst);
2559  else
2560  AccumulateRRTStatistics(rst,grst);
2561  DeleteRRTStatistics(rst);
2562  free(rst);
2563  #endif
2564 
2565  if (rrt->graph)
2566  DeleteHeap(&q);
2567 
2568  *planningTime=GetElapsedTime(&st);
2569 
2570  fprintf(stderr,"Final number of samples: %u\n",rrt->ns);
2571 
2572  /* Reconstruct the path (if any) */
2573  if (id_goal!=NO_UINT)
2574  ReconstructRRTPath(pr,id_goal,pl,NULL,ns,path,rrt);
2575 
2576  /* Release memory */
2577  free(q_rand);
2578  free(pgs);
2579 
2580  return(id_goal!=NO_UINT);
2581  }
2582 }
2583 
2584 boolean BiRRTstar(Tparameters *pr,double *pg,
2585  unsigned int *it,double *times,double *costs,
2586  double *planningTime,double *pl,unsigned int *ns,double ***path,
2587  TRRTStatistics *grst,Trrt *rrt)
2588 {
2589  if (EXPLORATION_RRT)
2590  Error("RRTstar is not designed for exploration RRTs");
2591 
2592  if (rrt->mode!=TWO_TREES_WITH_SWAP)
2593  {
2594  if (rrt->mode!=ONE_TREE)
2595  Error("RRTstar operates on two-directional RRTs");
2596 
2597  return(RRTstar(pr,pg,it,times,costs,planningTime,pl,ns,path,grst,rrt));
2598  }
2599  else
2600  {
2601  double *q_rand,*g;
2602  unsigned int i_near;
2603  Tstatistics st; /* used just to measure execution time */
2604  double time;
2605  double *c; /* if the cost function is symmetrical, we cache
2606  the evaluation of the cost to neighbour. */
2607  /* neighbours for each new node */
2608  unsigned int nn;
2609  unsigned int *n;
2610  /* distance q_rand,tree */
2611  double gamma,ct_gamma;
2612  unsigned int i,id_new;
2613  double *q_new;
2614  boolean done,optimal,validSample;
2615  double l; /* length of the shortest path so far */
2616  Tvector steps;
2617  double maxTime;
2618  unsigned int maxIterations,samplingMode;
2619  Theap q;
2620  double h;
2621  unsigned int vt; /* trees in the set of neighbours. */
2622  unsigned int numSamples;
2623  double l1,c12;
2624  boolean connected,collision;
2625  TRRTStatistics *rst;
2626 
2627  if (rrt->ns>2)
2628  Error("RRTstart must be used to a just initilized RRT");
2629 
2630  #if (GET_RRT_STATISTICS)
2631  {
2632  unsigned int i;
2633 
2634  NEW(rst,1,TRRTStatistics);
2635  InitRRTStatistics(rst);
2636 
2637  for(i=0;i<rrt->ns;i++)
2638  NewRRTSample(rst);
2639  }
2640  #endif
2641 
2642  /* Init the RRT structure */
2643  ct_gamma=GetParameter(CT_GAMMA,pr);
2644  maxTime=GetParameter(CT_MAX_PLANNING_TIME,pr);
2645  maxIterations=(unsigned int)GetParameter(CT_MAX_PLANNING_ITERATIONS,pr);
2646  /* for BiRRTStart the sampling mode should be "ambient_sampling" but we accept
2647  other modes just for the sake of completeness. */
2648  samplingMode=(unsigned int)GetParameter(CT_SAMPLING,pr);
2649 
2650  /* Room for the randomly generated samples (and its correspoinding point
2651  on the manifold) */
2652  NEW(q_rand,rrt->m,double);
2653 
2654  /* Reset the output statistics (just in case maxIterations
2655  is not reached) */
2656  for(i=0;i<maxIterations;i++)
2657  {
2658  times[i]=0;
2659  costs[i]=-1;
2660  }
2661 
2662  if (rrt->graph)
2664  LessThanDoublePair,NULL,TRUE,rrt->ms,&q);
2665 
2666  gamma=ct_gamma;
2667  optimal=(gamma>0.0);
2668 
2669  *it=0;
2670 
2671  InitStatistics(rrt->nCores,0,&st);
2672  time=0.0;
2673  done=FALSE;
2674 
2675  InitVector(sizeof(TRRTStep),CopyRRTStep,DeleteRRTStep,100,&steps);
2676  l=INF;
2677  c=NULL; /* default not to use cache of cost to neighbours */
2678 
2679  while ((!done)&&(time<maxTime)&&(*it<maxIterations))
2680  {
2681  /* Keep the current path up to date (if any) */
2682  if (l<INF)
2683  l=UpdateBiRRTSteps(&steps,rrt);
2684 
2685  /*******************************************************************/
2686  do {
2687  /* Generate a random sample */
2688  RRTSample(samplingMode,BOTHTREES,NULL,q_rand,rst,rrt);
2689 
2690  /* Check if the sample is valid */
2691  validSample=RRTValidateSample(pr,q_rand,BOTHTREES,FALSE,rrt->si[1]->samples,l,
2692  &h,&i_near,rst,rrt);
2693  time=GetElapsedTime(&st);
2694  } while((!validSample)&&(time<maxTime));
2695 
2696  if (time<maxTime)
2697  {
2698  /*******************************************************************/
2699  /* heuristic w.r.t distance from q_rand to goal */
2700  if (rrt->si[i_near]->tree==START2GOAL)
2701  g=rrt->si[1]->samples;
2702  else
2703  g=rrt->si[0]->samples;
2704  h=DistanceTopology(rrt->m,rrt->tp,q_rand,g);
2705 
2706  /*******************************************************************/
2707  /* Extend the RRT from q_near to q_rand using the cbiRRT extend method */
2708  /* Note that here we use a extend strategy (we only add one sample) */
2709 
2710  id_new=AddStepToRRTstar(pr,i_near,q_rand,NULL,NULL,rst,rrt);
2711 
2712  /* If we actually added something */
2713  if ((id_new!=NO_UINT)&&((((HEURISTIC_RRT_STAR)&(2))==0)||(l<INF)))
2714  {
2715  /* q_new is the last samples added to the tree */
2716  q_new=rrt->si[id_new]->samples;
2717  h=DistanceTopology(rrt->m,rrt->tp,q_new,g);
2718 
2719  /*******************************************************************/
2720  /* Search for the set of potential neighbours */
2721  numSamples=(rrt->ns<3?3:rrt->ns);
2722  gamma=ct_gamma*pow(log(numSamples)/numSamples,1/(double)rrt->k);
2723  gamma=(gamma<rrt->delta?rrt->delta:gamma);
2724 
2725  GetRRTNNInBall(BOTHTREES,q_new,gamma,&nn,&n,rrt);
2726 
2727  /*******************************************************************/
2728  /* Wire: Search for the lowest cost path to the new node */
2729  WireRRTstar(pr,id_new,i_near,gamma,nn,n,&c,h,(rrt->graph?&q:NULL),&vt,rst,rrt);
2730 
2731  /*******************************************************************/
2732  /* Rewire the tree around the new point */
2733  if (rrt->graph)
2734  RecursiveReWireRRTstar(pr,&q,g,&steps,&l,rrt);
2735  else
2736  ReWireRRTstar(pr,id_new,gamma,nn,n,c,&steps,&l,rrt);
2737 
2738  /*******************************************************************/
2739  /* release the data (cost (if actually used) and neighbours) */
2740  if (c!=NULL)
2741  free(c);
2742  free(n);
2743 
2744  /*******************************************************************/
2745  /* If the set of neighbours does not include nodes in the two trees
2746  and we do not have a path start<->goal we try to connect the two
2747  trees (this is hardly used since when l=INF, gamma is large and
2748  the neighbours always include nodes in the two trees) */
2749  //if ((vt!=BOTHTREES)&&(l==INF))
2750  if (vt!=BOTHTREES)
2751  {
2752  /* In the re-wire the current sample might end up in tree 't2'
2753  and the connection must be always with the other tree */
2754  if (rrt->si[id_new]->tree==START2GOAL)
2755  i_near=GetRRTNN(GOAL2START,q_new,rrt);
2756  else
2757  i_near=GetRRTNN(START2GOAL,q_new,rrt);
2758 
2759  l1=rrt->si[id_new]->cost+rrt->si[i_near]->cost;
2760  c12=ConnectSamples(pr,rrt->tp,rrt->ambient,
2761  rrt->m,rrt->n,q_new,rrt->si[i_near]->samples,l-l1,
2762  TRUE,&connected,&collision,NULL,NULL,NULL,rrt->w);
2763  if (connected)
2764  {
2765  if (rrt->graph)
2766  AddEdgeToRRT(id_new,i_near,c12,rrt);
2767  l1+=c12;
2768  if (l1<l)
2769  l=ChangeBiRRTSteps(id_new,i_near,c12,&steps,rrt);
2770  }
2771  }
2772  }
2773  #if (RRT_VERBOSE)
2774  else
2775  {
2776  if (id_new==NO_UINT)
2777  fprintf(stderr," Blocked [%u]\n",i_near);
2778  }
2779  #endif
2780 
2781  if (!optimal)
2782  done=(l<INF);
2783 
2784  /*******************************************************************/
2785  /* Conclude the iteration */
2786  time=GetElapsedTime(&st);
2787 
2788  BiRRTstarCloseIteration(*it,l,time,gamma,times,costs,rrt);
2789 
2790  (*it)++;
2791  }
2792  }
2793 
2794  DeleteStatistics(&st);
2795  #if (GET_RRT_STATISTICS)
2796  if (grst==NULL)
2797  PrintRRTStatistics(rrt,rst);
2798  else
2799  AccumulateRRTStatistics(rst,grst);
2800  DeleteRRTStatistics(rst);
2801  free(rst);
2802  #endif
2803 
2804  if (rrt->graph)
2805  DeleteHeap(&q);
2806 
2807  *planningTime=GetElapsedTime(&st);
2808 
2809  fprintf(stderr,"Final number of samples: %u\n",rrt->ns);
2810 
2811  /* Reconstruct the path (if any) */
2812  if (l<INF)
2813  Steps2PathinRRT(pr,&steps,pl,NULL,ns,path,rrt);
2814 
2815  /* Release memory */
2816  free(q_rand);
2817  DeleteVector(&steps);
2818 
2819  return(l<INF);
2820  }
2821 }
2822 
2823 boolean ccTRRT(Tparameters *pr,double *pg,double *time,
2824  double *pl, double* pc, unsigned int *ns,double ***path,
2825  double (*costF)(Tparameters*,boolean,double*,void*),
2826  void *costData,
2827  TRRTStatistics *grst,Trrt *rrt)
2828 {
2829  double *pWithDummies,*pgs,*q_rand;
2830  unsigned int it,i_near;
2831  double epsilon;
2832  boolean pathFound,done,expand2Goal;
2833  unsigned int lastSample;
2834  double er,h;
2835  Tstatistics st; /* used just to measure execution time */
2836  boolean collision;
2837  double maxTime;
2838  unsigned int maxNodes;
2839  unsigned int samplingMode;
2840  boolean validSample;
2841  TRRTStatistics *rst;
2842 
2843  if(rrt->ns >1)
2844  Error("ccTRRT should only be initialized with the start config");
2845 
2846  if (rrt->mode!=ONE_TREE)
2847  Error("ccTRRT operates on one-directional RRTs");
2848 
2849  #if (GET_RRT_STATISTICS)
2850  {
2851  unsigned int i;
2852 
2853  NEW(rst,1,TRRTStatistics);
2854  InitRRTStatistics(rst);
2855 
2856  for(i=0;i<rrt->ns;i++)
2857  NewRRTSample(rst);
2858  }
2859  #endif
2860 
2861  /* Init the RRT structure */
2862  epsilon=GetParameter(CT_EPSILON,pr);
2863  maxTime=GetParameter(CT_MAX_PLANNING_TIME,pr);
2864  maxNodes=(unsigned int)GetParameter(CT_MAX_NODES_RRT,pr);
2865  samplingMode=(unsigned int)GetParameter(CT_SAMPLING,pr);
2866 
2867  CS_WD_REGENERATE_SOLUTION_POINT(pr,pg,&pWithDummies,rrt->w);
2868  CS_WD_GENERATE_SIMPLIFIED_POINT(pr,pWithDummies,&pgs,rrt->w);
2869 
2870  if ((!PointInBoxTopology(NULL,TRUE,rrt->m,pgs,0,rrt->tp,rrt->ambient))||
2871  (!CS_WD_SIMP_INEQUALITIES_HOLD(pr,pgs,rrt->w)))
2872  Error("The targed point for the ccRRT is not in domain");
2873 
2874  if (rrt->n==0)
2875  er=0;
2876  else
2877  er=CS_WD_ERROR_IN_SIMP_EQUATIONS(pr,pgs,rrt->w);
2878  if (er>epsilon)
2879  Error("The target point for the ccRRT is not on the manifold");
2880 
2881  collision=CS_WD_ORIGINAL_IN_COLLISION(pr,pWithDummies,NULL,rrt->w);
2882  if (collision)
2883  Warning("The target point for the ccRRT is in collision");
2884  free(pWithDummies);
2885 
2886  /*init the cost of the first node*/
2887  rrt->si[0]->cost=costF(pr,TRUE,GetRRTNode(0,rrt),costData);
2888 
2889  /* Room for the randomly generated samples (and its correspoinding point
2890  on the manifold) */
2891  NEW(q_rand,rrt->m,double);
2892 
2893  done=FALSE;
2894  it=0;
2895 
2896  InitStatistics(rrt->nCores,0,&st);
2897  *time=0.0;
2898 
2899  while ((!done)&&(*time<maxTime)&&(rrt->ns<maxNodes))
2900  {
2901  if ((RRT_VERBOSE)||((it%1000)==0))
2902  {
2903  fprintf(stderr,"Iteration: %u (s:%u t:%g)\n",it,rrt->ns,*time);
2904  fflush(stdout);
2905  }
2906 
2907  /*******************************************************************/
2908  /* Sample in ambient space (with bias toward goal) */
2909  do {
2910  /* Generate a random sample */
2911  expand2Goal=RRTSample(samplingMode,START2GOAL,(EXPLORATION_RRT?NULL:pgs),
2912  q_rand,rst,rrt);
2913 
2914  /* Check if the sample is valid */
2915  validSample=RRTValidateSample(pr,q_rand,START2GOAL,expand2Goal,NULL,INF,
2916  &h,&i_near,rst,rrt);
2917 
2918  *time=GetElapsedTime(&st);
2919  } while((!validSample)&&(*time<maxTime));
2920 
2921  if (*time<maxTime)
2922  {
2923  /*******************************************************************/
2924  /* Extend the RRT from q_near to q_rand using the ccRRT extend method */
2925 
2926  #if (GET_RRT_STATISTICS)
2927  NewRRTBranch(rst);
2928  NewRRTDistanceQrand(DistanceTopology(rrt->m,rrt->tp,rrt->si[i_near]->samples,q_rand),rst);
2929  #endif
2930  done=AddBranchToRRT(pr,TRUE,START2GOAL,i_near,q_rand,pgs,&lastSample,
2931  costF,costData,rst,rrt);
2932  #if (GET_RRT_STATISTICS)
2933  if (lastSample!=i_near)
2934  NewRRTNoEmptyBranch(rst);
2935  #endif
2936 
2937  *time=GetElapsedTime(&st);
2938  it++;
2939  }
2940  }
2941 
2942  fprintf(stderr,"Final number of samples: %u\n",rrt->ns);
2943 
2944  /* Reconstruct the path (if any) */
2945  pathFound=PathStart2GoalInRRT(pr,pgs,NO_UINT,NO_UINT,pl,pc,ns,path,rrt);
2946  if((pathFound)&&(!EXPLORATION_RRT))
2947  rrt->si[rrt->ns-1]->cost=costF(pr,TRUE,GetRRTNode(rrt->ns-1,rrt),costData);
2948 
2949  /* Release memory */
2950  DeleteStatistics(&st);
2951  #if (GET_RRT_STATISTICS)
2952  if (grst==NULL)
2953  PrintRRTStatistics(rrt,rst);
2954  else
2955  AccumulateRRTStatistics(rst,grst);
2956  DeleteRRTStatistics(rst);
2957  free(rst);
2958  #endif
2959 
2960  free(q_rand);
2961  free(pgs);
2962 
2963  return(pathFound);
2964 }
2965 
2966 boolean ccRRT(Tparameters *pr,double *pg,
2967  double *time,
2968  double *pl,unsigned int *ns,double ***path,
2969  TRRTStatistics *grst,Trrt *rrt)
2970 {
2971  double *pWithDummies,*pgs,*q_rand;
2972  unsigned int it,i_near;
2973  double epsilon;
2974  boolean pathFound,done,expand2Goal;
2975  unsigned int lastSample;
2976  double er,h;
2977  Tstatistics st; /* used just to measure execution time */
2978  boolean collision;
2979  double maxTime;
2980  unsigned int maxNodes;
2981  unsigned int samplingMode;
2982  boolean validSample;
2983  TRRTStatistics *rst;
2984 
2985  if (rrt->mode!=ONE_TREE)
2986  Error("ccRRT operates on one-directional RRTs");
2987 
2988  #if (GET_RRT_STATISTICS)
2989  {
2990  unsigned int i;
2991 
2992  NEW(rst,1,TRRTStatistics);
2993  InitRRTStatistics(rst);
2994 
2995  for(i=0;i<rrt->ns;i++)
2996  NewRRTSample(rst);
2997  }
2998  #endif
2999 
3000  /* Init the RRT structure */
3001  epsilon=GetParameter(CT_EPSILON,pr);
3002  maxTime=GetParameter(CT_MAX_PLANNING_TIME,pr);
3003  maxNodes=(unsigned int)GetParameter(CT_MAX_NODES_RRT,pr);
3004  samplingMode=(unsigned int)GetParameter(CT_SAMPLING,pr);
3005 
3006  CS_WD_REGENERATE_SOLUTION_POINT(pr,pg,&pWithDummies,rrt->w);
3007  CS_WD_GENERATE_SIMPLIFIED_POINT(pr,pWithDummies,&pgs,rrt->w);
3008 
3009  if ((!PointInBoxTopology(NULL,TRUE,rrt->m,pgs,0,rrt->tp,rrt->ambient))||
3010  (!CS_WD_SIMP_INEQUALITIES_HOLD(pr,pgs,rrt->w)))
3011  Error("The targed point for the ccRRT is not in domain");
3012 
3013  if (rrt->n==0)
3014  er=0;
3015  else
3016  er=CS_WD_ERROR_IN_SIMP_EQUATIONS(pr,pgs,rrt->w);
3017  if (er>epsilon)
3018  Error("The target point for the ccRRT is not on the manifold");
3019 
3020  collision=CS_WD_ORIGINAL_IN_COLLISION(pr,pWithDummies,NULL,rrt->w);
3021  if (collision)
3022  Warning("The target point for the ccRRT is in collision");
3023  free(pWithDummies);
3024 
3025  /* Room for the randomly generated samples (and its correspoinding point
3026  on the manifold) */
3027  NEW(q_rand,rrt->m,double);
3028 
3029  done=FALSE;
3030  it=0;
3031 
3032  InitStatistics(rrt->nCores,0,&st);
3033  *time=0.0;
3034 
3035  while ((!done)&&(*time<maxTime)&&(rrt->ns<maxNodes))
3036  {
3037  if ((RRT_VERBOSE)||((it%1000)==0))
3038  {
3039  fprintf(stderr,"Iteration: %u (s:%u t:%g)\n",it,rrt->ns,*time);
3040  fflush(stdout);
3041  }
3042 
3043  /*******************************************************************/
3044  /* Sample in ambient space (with bias toward goal) */
3045  do {
3046  /* Generate a random sample */
3047  expand2Goal=RRTSample(samplingMode,START2GOAL,(EXPLORATION_RRT?NULL:pgs),
3048  q_rand,rst,rrt);
3049 
3050  /* Check the sample is valid */
3051  validSample=RRTValidateSample(pr,q_rand,START2GOAL,expand2Goal,NULL,INF,
3052  &h,&i_near,rst,rrt);
3053 
3054  *time=GetElapsedTime(&st);
3055  } while((!validSample)&&(*time<maxTime));
3056 
3057  if (*time<maxTime)
3058  {
3059  /*******************************************************************/
3060  /* Extend the RRT from q_near to q_rand using the ccRRT extend method */
3061  #if (GET_RRT_STATISTICS)
3062  NewRRTBranch(rst);
3063  NewRRTDistanceQrand(DistanceTopology(rrt->m,rrt->tp,rrt->si[i_near]->samples,q_rand),rst);
3064  #endif
3065  done=AddBranchToRRT(pr,TRUE,START2GOAL,i_near,q_rand,pgs,&lastSample,
3066  NULL,NULL,rst,rrt);
3067  #if (GET_RRT_STATISTICS)
3068  if (lastSample!=i_near)
3069  NewRRTNoEmptyBranch(rst);
3070  #endif
3071 
3072  *time=GetElapsedTime(&st);
3073  it++;
3074  }
3075  }
3076 
3077  fprintf(stderr,"Final number of samples: %u\n",rrt->ns);
3078 
3079  /* Reconstruct the path (if any) */
3080  pathFound=PathStart2GoalInRRT(pr,pgs,NO_UINT,NO_UINT,pl,NULL,ns,path,rrt);
3081 
3082  /* Release memory */
3083  DeleteStatistics(&st);
3084  #if (GET_RRT_STATISTICS)
3085  if (grst==NULL)
3086  PrintRRTStatistics(rrt,rst);
3087  else
3088  AccumulateRRTStatistics(rst,grst);
3089  DeleteRRTStatistics(rst);
3090  free(rst);
3091  #endif
3092 
3093  free(q_rand);
3094  free(pgs);
3095 
3096  return(pathFound);
3097 }
3098 
3099 boolean cBiRRT(Tparameters *pr,double *pg,
3100  double *time,
3101  double *pl,unsigned int *ns,double ***path,
3102  TRRTStatistics *grst,Trrt *rrt)
3103 {
3104  double *pWithDummies,*pgs,*q_rand;
3105  unsigned int it,i_near;
3106  double epsilon;
3107  boolean pathFound;
3108  double er,h;
3109  unsigned int t1,t2;
3110  unsigned int lastSample1;
3111  unsigned int samplingMode;
3112  Tstatistics st; /* used just to measure execution time */
3113  boolean collision;
3114  unsigned int lastSample2;
3115  double maxTime;
3116  boolean validSample;
3117  TRRTStatistics *rst;
3118 
3119  #if (EXPLORATION_RRT)
3120  Error("cBiRRT can not be used in exploration mode.");
3121  #endif
3122 
3123  if (rrt->mode!=TWO_TREES)
3124  Error("cBiRRT operates on bidirectional RRTs");
3125 
3126  #if (GET_RRT_STATISTICS)
3127  {
3128  unsigned int i;
3129 
3130  NEW(rst,1,TRRTStatistics);
3131  InitRRTStatistics(rst);
3132 
3133  for(i=0;i<rrt->ns;i++)
3134  NewRRTSample(rst);
3135  }
3136  #endif
3137 
3138  /* Init the RRT structure */
3139  epsilon=GetParameter(CT_EPSILON,pr);
3140  maxTime=GetParameter(CT_MAX_PLANNING_TIME,pr);
3141  samplingMode=(unsigned int)GetParameter(CT_SAMPLING,pr);
3142 
3143  CS_WD_REGENERATE_SOLUTION_POINT(pr,pg,&pWithDummies,rrt->w);
3144  CS_WD_GENERATE_SIMPLIFIED_POINT(pr,pWithDummies,&pgs,rrt->w);
3145 
3146  if ((!PointInBoxTopology(NULL,TRUE,rrt->m,pgs,0,rrt->tp,rrt->ambient))||
3147  (!CS_WD_SIMP_INEQUALITIES_HOLD(pr,pgs,rrt->w)))
3148  Error("The targed point is not in domain");
3149 
3150  if (rrt->n==0)
3151  er=0;
3152  else
3153  er=CS_WD_ERROR_IN_SIMP_EQUATIONS(pr,pgs,rrt->w);
3154  if (er>epsilon)
3155  Error("The target point for the RRT is not on the manifold");
3156 
3157  collision=CS_WD_ORIGINAL_IN_COLLISION(pr,pWithDummies,NULL,rrt->w);
3158  if (collision)
3159  Warning("The target point for the RRT is in collision");
3160  free(pWithDummies);
3161 
3162  /* Room for the randomly generated samples (and its correspoinding point
3163  on the manifold) */
3164  NEW(q_rand,rrt->m,double);
3165 
3166  pathFound=FALSE;
3167  it=0;
3168 
3169  /* The two trees. This is only used for bidirectiononal RRTs. In norma
3170  RRTs only the START2GOAL tree is extended*/
3171  t1=START2GOAL;
3172  t2=GOAL2START;
3173 
3174  InitStatistics(rrt->nCores,0,&st);
3175  *time=0.0;
3176 
3177  while ((!pathFound)&&(*time<maxTime))
3178  {
3179  if ((RRT_VERBOSE)||((it%1000)==0))
3180  {
3181  fprintf(stderr,"Iteration: %u (s:%u t:%g)\n",it,rrt->ns,*time);
3182  fflush(stdout);
3183  }
3184 
3185  /*******************************************************************/
3186  /* Sample */
3187  do {
3188  /* Generate a random sample */
3189  RRTSample(samplingMode,t1,NULL,q_rand,rst,rrt);
3190 
3191  /* Check the sample is valid */
3192  validSample=RRTValidateSample(pr,q_rand,t1,FALSE,NULL,INF,
3193  &h,&i_near,rst,rrt);
3194 
3195  *time=GetElapsedTime(&st);
3196  } while((!validSample)&&(*time<maxTime));
3197 
3198  if (*time<maxTime)
3199  {
3200  /*******************************************************************/
3201  /* Extend the first tree from q_near toward q_rand */
3202  #if (GET_RRT_STATISTICS)
3203  NewRRTBranch(rst);
3204  NewRRTDistanceQrand(DistanceTopology(rrt->m,rrt->tp,rrt->si[i_near]->samples,q_rand),rst);
3205  #endif
3206  AddBranchToRRT(pr,FALSE,t1,i_near,q_rand,q_rand,&lastSample1,
3207  NULL,NULL,rst,rrt);
3208  #if (GET_RRT_STATISTICS)
3209  if (lastSample1!=i_near)
3210  NewRRTNoEmptyBranch(rst);
3211  #endif
3212 
3213  /*******************************************************************/
3214  /* The last node added to t1 is used as random sample */
3215  memcpy(q_rand,rrt->si[lastSample1]->samples,rrt->m*sizeof(double));
3216 
3217  /*******************************************************************/
3218  /* Search for q_near in the second tree */
3219  i_near=GetRRTNN(t2,q_rand,rrt);
3220 
3221  /*******************************************************************/
3222  /* Extend t2 toward the last sample added to t1 */
3223  #if (GET_RRT_STATISTICS)
3224  NewRRTTreeConnection(rst);
3225  #endif
3226  pathFound=AddBranchToRRT(pr,FALSE,t2,i_near,q_rand,q_rand,&lastSample2,
3227  NULL,NULL,rst,rrt);
3228  #if (GET_RRT_STATISTICS)
3229  if (lastSample2!=i_near)
3231  #endif
3232 
3233  /*******************************************************************/
3234  /* Swap the trees (t1 and t2) */
3235  if ((it%2)==0)
3236  {
3237  t1=GOAL2START;
3238  t2=START2GOAL;
3239  }
3240  else
3241  {
3242  t1=START2GOAL;
3243  t2=GOAL2START;
3244  }
3245 
3246  *time=GetElapsedTime(&st);
3247  it++;
3248  }
3249  }
3250 
3251  fprintf(stderr,"Final number of samples: %u\n",rrt->ns);
3252 
3253  /* Reconstruct the path, if possible */
3254  if (pathFound)
3255  {
3256  if (!PathStart2GoalInRRT(pr,pgs,lastSample1,lastSample2,pl,NULL,ns,path,rrt))
3257  Error("Inconsitancy in cBiRRT pathFound");
3258  }
3259 
3260  /* Release memory */
3261  DeleteStatistics(&st);
3262  #if (GET_RRT_STATISTICS)
3263  if (grst==NULL)
3264  PrintRRTStatistics(rrt,rst);
3265  else
3266  AccumulateRRTStatistics(rst,grst);
3267  DeleteRRTStatistics(rst);
3268  free(rst);
3269  #endif
3270 
3271  free(q_rand);
3272  free(pgs);
3273 
3274  return(pathFound);
3275 }
3276 
3277 double RRTPathSteps(unsigned int sID,Tvector *path,Trrt *rrt)
3278 {
3279  TRRTStep st;
3280  unsigned int s;
3281  double l;
3282 
3283  InitVector(sizeof(unsigned int),CopyRRTStep,DeleteRRTStep,100,path);
3284 
3285  s=sID;
3286  l=0;
3287  while (s!=NO_UINT)
3288  {
3289  st.id=s;
3290  st.cost=rrt->si[s]->costp;/* this is 0 for the root */
3291  NewVectorElement((void *)(&st),path);
3292  l+=st.cost;
3293  s=rrt->si[s]->parent; /* this is NO_UINT for the root */
3294  }
3295  return(l);
3296 }
3297 
3298 double ChangeBiRRTSteps(unsigned int l1,unsigned int l2,double c12,Tvector *steps,Trrt *rrt)
3299 {
3300  Tvector v1,v2;
3301  double lp1,lp2;
3302  unsigned int n;
3303  TRRTStep st,*st1,*st2;
3304  int i;
3305  double c;
3306 
3307  lp1=RRTPathSteps(l1,&v1,rrt);
3308  lp2=RRTPathSteps(l2,&v2,rrt);
3309  if (rrt->si[l1]->tree==START2GOAL)
3310  {
3311  /* reverse(v1) -> v2 */
3312  ResetVector(steps);
3313  n=VectorSize(&v1);
3314  for(i=n-1;i>=0;i--)
3315  {
3316  st1=(TRRTStep *)GetVectorElement(i,&v1);
3317  if (i==0)
3318  c=c12;
3319  else
3320  {
3321  st2=(TRRTStep *)GetVectorElement(i-1,&v1);
3322  c=st2->cost;
3323  }
3324  st.id=st1->id;
3325  st.cost=c;
3326  NewVectorElement((void*)&st,steps);
3327  }
3328  ConcatVectors(&v2,steps);
3329  }
3330  else
3331  {
3332  /* reverse(v2) -> v1 */
3333  ResetVector(steps);
3334  n=VectorSize(&v2);
3335  for(i=n-1;i>=0;i--)
3336  {
3337  st1=(TRRTStep *)GetVectorElement(i,&v2);
3338  if (i==0)
3339  c=c12;
3340  else
3341  {
3342  st2=(TRRTStep *)GetVectorElement(i-1,&v2);
3343  c=st2->cost;
3344  }
3345  st.id=st1->id;
3346  st.cost=c;
3347  NewVectorElement((void*)&st,steps);
3348  }
3349  ConcatVectors(&v1,steps);
3350  }
3351  DeleteVector(&v1);
3352  DeleteVector(&v2);
3353 
3354  return(lp1+c12+lp2);
3355 }
3356 
3357 double UpdateBiRRTSteps(Tvector *steps,Trrt *rrt)
3358 {
3359  TRRTStep *st;
3360  unsigned int i,n,current,next,id1,id2;
3361  double c12,l,ct;
3362 
3363  /* Ensure that all nodes in the path are correctly assigned
3364  to the trees (and that theirs costs are up to date) */
3365  n=VectorSize(steps);
3366  for(i=0;i<n;i++)
3367  {
3368  st=(TRRTStep *)GetVectorElement(i,steps);
3369  UpdateCostAndTree(st->id,rrt);
3370  }
3371 
3372  /* Search for the best transition between trees along the path */
3373  st=(TRRTStep *)GetVectorElement(0,steps);
3374  current=st->id;
3375  c12=st->cost;
3376  l=INF;
3377  id1=NO_UINT;
3378  id2=NO_UINT;
3379  for(i=1;i<n;i++)
3380  {
3381  st=(TRRTStep *)GetVectorElement(i,steps);
3382  next=st->id;
3383  if ((current==NO_UINT)||(next==NO_UINT))
3384  Error("Undefined node in path in BiRRT");
3385  if ((rrt->si[current]->tree!=rrt->si[next]->tree)&&
3386  ((rrt->si[current]->cost+c12+rrt->si[next]->cost)<l))
3387  {
3388  id1=current;
3389  id2=next;
3390  ct=c12;
3391  l=rrt->si[id1]->cost+ct+rrt->si[id2]->cost;
3392  }
3393  current=next;
3394  c12=st->cost;
3395  }
3396  if (id1==NO_UINT) /* (id2==NO_UINT) (l==INF) */
3397  Error("A global path in a BiRRT without transition between trees?");
3398 
3399  return(ChangeBiRRTSteps(id1,id2,ct,steps,rrt));
3400 }
3401 
3402 boolean PathStart2GoalInRRT(Tparameters *pr,double *pgs,
3403  unsigned int l1,unsigned int l2,
3404  double *pl,double* pc,unsigned int *ns,double ***path,
3405  Trrt *rrt)
3406 {
3407  boolean pathFound;
3408 
3409  #if (EXPLORATION_RRT)
3410  /* We reconstruct the path from root to last sample in the tree */
3411  pathFound=TRUE;
3412  ReconstructRRTPath(pr,rrt->ns-1,pl,pc,ns,path,rrt);
3413  #else
3414  double pl1,pl2,d12;
3415 
3416  if (rrt->mode!=ONE_TREE)
3417  {
3418  d12=DistanceTopology(rrt->m,rrt->tp,rrt->si[l1]->samples,rrt->si[l2]->samples);
3419  pathFound=(d12<2.0*rrt->delta);
3420 
3421  if (pathFound)
3422  {
3423  unsigned int ns1,ns2;
3424  double **p1,**p2;
3425  unsigned int nvs;
3426 
3427  nvs=ReconstructRRTPath(pr,l1,&pl1,pc,&ns1,&p1,rrt);
3428  ReconstructRRTPath(pr,l2,&pl2,pc,&ns2,&p2,rrt);
3429 
3430  if (rrt->si[l1]->tree==START2GOAL)
3431  ReverseConcatSamples(nvs,ns1,p1,ns2,p2,ns,path);
3432  else
3433  ReverseConcatSamples(nvs,ns2,p2,ns1,p1,ns,path);
3434 
3435  DeleteSamples(ns1,p1);
3436  DeleteSamples(ns2,p2);
3437 
3438  *pl=pl1+d12+pl2;
3439  }
3440  }
3441  else
3442  {
3443  unsigned int nn;
3444  double d;
3445 
3446  nn=GetRRTNN(START2GOAL,pgs,rrt);
3447  d=DistanceTopology(rrt->m,rrt->tp,rrt->si[nn]->samples,pgs);
3448  pathFound=(d<2.0*rrt->delta);
3449  if (pathFound)
3450  {
3451  unsigned int g;
3452 
3453  /* Todo : compute the cost of the goal if required*/
3454  AddNodeToRRT(pr,START2GOAL,nn,pgs,pgs,&g,NULL,d,0,NULL,rrt);
3455  ReconstructRRTPath(pr,g,pl,pc,ns,path,rrt);
3456  }
3457  }
3458  #endif
3459 
3460  return(pathFound);
3461 }
3462 
3463 boolean Bidirectional(Trrt *rrt)
3464 {
3465  return(rrt->mode!=ONE_TREE);
3466 }
3467 
3468 
3469 unsigned int GetRRTMode(Trrt *rrt)
3470 {
3471  return(rrt->mode);
3472 }
3473 
3474 boolean InDynamicDomain(unsigned int i,double *q,Trrt *rrt)
3475 {
3476  if ((rrt->dd>0)&&(rrt->si[i]->ddr>0.0))
3477  return(DistanceTopology(rrt->m,rrt->tp,rrt->si[i]->samples,q)<rrt->si[i]->ddr);
3478  else
3479  return(TRUE);
3480 }
3481 
3482 void AdjustDynamicDomain(unsigned int i,boolean collision,Trrt *rrt)
3483 {
3484  if (rrt->dd>0) /* If the dynamic domain is active */
3485  {
3486  if (rrt->si[i]->ddr==0.0)
3487  {
3488  /* The dynamic domain radius is not set yet. Set it if we
3489  detected a collision during branch extension. */
3490  if (collision)
3491  rrt->si[i]->ddr=rrt->dd; /* Set to the initial radius. */
3492  }
3493 #if (0)
3494  else
3495  {
3496  /* Adjust the radious according to the collision status.
3497  Branches interrupted by a collision -> decrease the radius
3498  Branches fully extended -> increase the radius */
3499  if (collision)
3500  rrt->si[i]->ddr*=(1.0-MOV_AVG_DOWN);
3501  else
3502  rrt->si[i]->ddr*=(1.0+MOV_AVG_UP);
3503  }
3504 #endif
3505  }
3506 }
3507 
3508 double GetDynamicDomainRadius(unsigned int i,Trrt *rrt)
3509 {
3510  if (rrt->dd>0)
3511  return(rrt->si[i]->ddr);
3512  else
3513  return(0.0);
3514 }
3515 
3516 boolean DynamicDomainRRT(Trrt *rrt)
3517 {
3518  return(rrt->dd>0);
3519 }
3520 
3521 unsigned int GetRRTNumNodes(Trrt *rrt)
3522 {
3523  return(rrt->ns);
3524 }
3525 
3526 double *GetRRTNode(unsigned int i,Trrt *rrt)
3527 {
3528  if (i>=rrt->ns)
3529  return(NULL);
3530  else
3531  return(rrt->si[i]->samples);
3532 }
3533 
3534 unsigned int GetRRTNodeTree(unsigned int i,Trrt *rrt)
3535 {
3536  if (i>=rrt->ns)
3537  return(NO_UINT);
3538  else
3539  {
3540  if (rrt->mode==ONE_TREE)
3541  return(START2GOAL);
3542  else
3543  return(rrt->si[i]->tree);
3544  }
3545 }
3546 
3547 unsigned int GetRRTParent(unsigned int i,Trrt *rrt)
3548 {
3549  if (i>=rrt->ns)
3550  return(NO_UINT);
3551  else
3552  return(rrt->si[i]->parent);
3553 }
3554 
3555 void SetRRTParent(unsigned int i,unsigned int p,Trrt *rrt)
3556 {
3557  if ((i>=rrt->ns)||(p>=rrt->ns))
3558  Error("Wrong node/parent in SetRRTParent");
3559 
3560  if (rrt->si[i]->parent==NO_UINT)
3561  Error("Can not change the parent of a root node");
3562 
3563  rrt->si[i]->parent=p;
3564  if (rrt->si[i]->tree!=rrt->si[p]->tree)
3565  {
3566  if (rrt->mode!=TWO_TREES_WITH_SWAP)
3567  Error("Nodes can only swap trees in TWO_TREES_WITH_SWAP RRTs");
3568  rrt->si[i]->tree=rrt->si[p]->tree;
3569  }
3570 }
3571 
3572 void *GetRRTNodeInfo(unsigned int i,Trrt *rrt)
3573 {
3574  if (i<rrt->ns)
3575  return(rrt->si[i]->userInfo);
3576  else
3577  return(NULL);
3578 }
3579 
3580 void SetRRTNodeInfo(unsigned int i,void *info,Trrt *rrt)
3581 {
3582  if (i<rrt->ns)
3583  rrt->si[i]->userInfo=info;
3584  else
3585  Error("RRT node out of range in SetRRTNodeInfo");
3586 }
3587 
3588 double GetRRTNodeCost(unsigned int i,Trrt *rrt)
3589 {
3590  if (i<rrt->ns)
3591  return(rrt->si[i]->cost);
3592  else
3593  return(0.0);
3594 }
3595 
3596 double GetRRTNodeCostFromParent(unsigned int i,Trrt *rrt)
3597 {
3598  if (i<rrt->ns)
3599  return(rrt->si[i]->costp);
3600  else
3601  return(0.0);
3602 }
3603 
3604 void SetRRTNodeCost(unsigned int i,double costp,double cost,Trrt *rrt)
3605 {
3606  if (i<rrt->ns)
3607  {
3608  rrt->si[i]->costp=costp;
3609  rrt->si[i]->cost=cost;
3610  }
3611  else
3612  Error("RRT node out of range in SetRRTNodeCost");
3613 }
3614 
3615 void SetRRTCostAndParent(unsigned int i,unsigned int p,double costp,double cost,Trrt *rrt)
3616 {
3617  if (i<rrt->ns)
3618  {
3619  rrt->si[i]->costp=costp;
3620  rrt->si[i]->cost=cost;
3621  rrt->si[i]->parent=p;
3622  if (rrt->si[i]->tree!=rrt->si[p]->tree)
3623  {
3624  if (rrt->mode!=TWO_TREES_WITH_SWAP)
3625  Error("A RRT node moved from one tree to another in SetRRTCostAndParent");
3626  rrt->si[i]->tree=rrt->si[p]->tree;
3627  }
3628  }
3629  else
3630  Error("RRT node out of range in SetRRTCostAndParent");
3631 }
3632 
3633 unsigned int *GetRRTTopology(Trrt *rrt)
3634 {
3635  return(rrt->tp);
3636 }
3637 
3638 double CostToRoot(unsigned int sID,Trrt* rrt)
3639 {
3640  double l;
3641  unsigned int s;
3642 
3643  l=0.0;
3644  s=sID;
3645  while (s!=NO_UINT)
3646  {
3647  l+=rrt->si[s]->costp;
3648  s=rrt->si[s]->parent;
3649  }
3650  return(l);
3651 }
3652 
3653 void UpdateCostToRoot(unsigned int sID,Trrt* rrt)
3654 {
3655  rrt->si[sID]->cost=CostToRoot(sID,rrt);
3656 }
3657 
3658 void UpdateTree(unsigned int sID,Trrt* rrt)
3659 {
3660  unsigned int s,p;
3661 
3662  if (rrt->mode!=TWO_TREES_WITH_SWAP)
3663  Error("Nodes can only change trees in TWO_TREES_WITH_SWAP mode");
3664 
3665  s=sID;
3666  p=NO_UINT; /* parent node */
3667  while (s!=NO_UINT)
3668  {
3669  p=s;
3670  s=rrt->si[s]->parent;
3671  }
3672  if (p==NO_UINT)
3673  Error("Undefined root node in UpdateTree");
3674 
3675  /* here p is the root of the tree including sample sID (0 or 1) */
3676  if (p>2)
3677  Error("Wrong root in UpdateTree");
3678 
3679  /* update the tree assigment for this node */
3680  /* alternatively we could update the tree assigment all along the branch */
3681  if (p==0)
3682  rrt->si[sID]->tree=START2GOAL;
3683  else
3684  rrt->si[sID]->tree=GOAL2START;
3685 }
3686 
3687 void UpdateCostAndTree(unsigned int sID,Trrt* rrt)
3688 {
3689  double l;
3690  unsigned int s,p;
3691 
3692  /* This is just cut+paste+joint of CostToRoot, UpdateCost, and UpdateTree
3693  defined to gain some efficiency. */
3694 
3695  l=0.0;
3696  s=sID;
3697  p=NO_UINT; /* previous node */
3698  while (s!=NO_UINT)
3699  {
3700  l+=rrt->si[s]->costp;
3701  p=s;
3702  s=rrt->si[s]->parent;
3703  }
3704 
3705  /* update the cost and the tree of the given node */
3706  rrt->si[sID]->cost=l;
3707  /* the update of the tree only has effect in TWO_TREES_WITH_SWAP mode */
3708  if (p==0)
3709  rrt->si[sID]->tree=START2GOAL;
3710  else
3711  rrt->si[sID]->tree=GOAL2START;
3712 }
3713 
3714 unsigned int StepsToRoot(unsigned int sID,Trrt* rrt)
3715 {
3716  unsigned int l;
3717  unsigned int s;
3718 
3719  l=0;
3720  s=sID;
3721  while (s!=NO_UINT)
3722  {
3723  l++;
3724  s=rrt->si[s]->parent;
3725  }
3726  return(l);
3727 }
3728 
3729 void PlotRRT(char *fname,int argc, char **arg,Tparameters *pr,
3730  unsigned int xID,unsigned int yID,unsigned int zID,Trrt *rrt)
3731 {
3732  Tplot3d p3d;
3733  unsigned int i;
3734  Tcolor color;
3735  double *o;
3736  double *px,*py,*pz;
3737  double x[2],y[2],z[2];
3738  double cx,cy,cz;
3739 
3740  if (rrt->mode==TWO_TREES_WITH_SWAP)
3741  {
3742  /* To obtain nicer plots we update the tree assigment for all nodes */
3743  for(i=0;i<rrt->ns;i++)
3744  UpdateTree(i,rrt);
3745  }
3746 
3747  cx=GetParameter(CT_CUT_X,pr);
3748  cy=GetParameter(CT_CUT_Y,pr);
3749  cz=GetParameter(CT_CUT_Z,pr);
3750 
3751  /* Project the RRT nodes */
3752  NEW(px,rrt->ns,double);
3753  NEW(py,rrt->ns,double);
3754  NEW(pz,rrt->ns,double);
3755 
3756  for(i=0;i<rrt->ns;i++)
3757  {
3758  CS_WD_REGENERATE_ORIGINAL_POINT(pr,rrt->si[i]->samples,&o,rrt->w);
3759 
3760  px[i]=o[xID];
3761  py[i]=o[yID];
3762  pz[i]=o[zID];
3763 
3764  if ((cx<0)&&(px[i]<cx))
3765  px[i]+=M_2PI;
3766  if ((cx>0)&&(px[i]>cx))
3767  px[i]-=M_2PI;
3768  if ((cy<0)&&(py[i]<cy))
3769  py[i]+=M_2PI;
3770  if ((cy>0)&&(py[i]>cy))
3771  py[i]-=M_2PI;
3772  if ((cz<0)&&(pz[i]<cz))
3773  pz[i]+=M_2PI;
3774  if ((cz>0)&&(pz[i]>cz))
3775  pz[i]-=M_2PI;
3776 
3777  free(o);
3778  }
3779 
3780  InitPlot3d(fname,FALSE,argc,arg,&p3d);
3781 
3782  NewColor(1,0,0,&color); /*red*/
3783  StartNew3dObject(&color,&p3d);
3784 
3785  for(i=((rrt->mode!=ONE_TREE)?2:1);i<rrt->ns;i++)
3786  {
3787  if ((rrt->mode==ONE_TREE)||(rrt->si[i]->tree==START2GOAL))
3788  {
3789  x[0]=px[i];
3790  y[0]=py[i];
3791  z[0]=pz[i];
3792 
3793  x[1]=px[rrt->si[i]->parent];
3794  y[1]=py[rrt->si[i]->parent];
3795  z[1]=pz[rrt->si[i]->parent];
3796 
3797  PlotVect3d(2,x,y,z,&p3d);
3798  }
3799  }
3800 
3801  DeleteColor(&color);
3802  Close3dObject(&p3d);
3803 
3804  if (rrt->mode!=ONE_TREE)
3805  {
3806  NewColor(0,1,0,&color); /*green*/
3807  StartNew3dObject(&color,&p3d);
3808 
3809  for(i=2;i<rrt->ns;i++)
3810  {
3811  if (rrt->si[i]->tree==GOAL2START)
3812  {
3813  x[0]=px[i];
3814  y[0]=py[i];
3815  z[0]=pz[i];
3816 
3817  x[1]=px[rrt->si[i]->parent];
3818  y[1]=py[rrt->si[i]->parent];
3819  z[1]=pz[rrt->si[i]->parent];
3820 
3821  PlotVect3d(2,x,y,z,&p3d);
3822  }
3823  }
3824 
3825  DeleteColor(&color);
3826  Close3dObject(&p3d);
3827  }
3828 
3829  #if (RRT_PLOT_NODES)
3830  /* Dots, one per node */
3831  NewColor(0,0,1,&color); /*blue*/
3832  StartNew3dObject(&color,&p3d);
3833 
3834  for(i=0;i<rrt->ns;i++)
3835  {
3836  x[0]=px[i]-0.01;
3837  y[0]=py[i];
3838  z[0]=pz[i];
3839 
3840  x[1]=px[i]+0.01;
3841  y[1]=py[i];
3842  z[1]=pz[i];
3843 
3844  PlotVect3d(2,x,y,z,&p3d);
3845 
3846  x[0]=px[i];
3847  y[0]=py[i]-0.01;
3848  z[0]=pz[i];
3849 
3850  x[1]=px[i];
3851  y[1]=py[i]+0.01;
3852  z[1]=pz[i];
3853 
3854  PlotVect3d(2,x,y,z,&p3d);
3855 
3856  x[0]=px[i];
3857  y[0]=py[i];
3858  z[0]=pz[i]-0.01;
3859 
3860  x[1]=px[i];
3861  y[1]=py[i];
3862  z[1]=pz[i]+0.01;
3863 
3864  PlotVect3d(2,x,y,z,&p3d);
3865  }
3866 
3867  DeleteColor(&color);
3868  Close3dObject(&p3d);
3869  #endif
3870 
3871  ClosePlot3d(FALSE,0,0,0,&p3d);
3872 
3873  free(px);
3874  free(py);
3875  free(pz);
3876 }
3877 
3878 void SaveRRTNodes(Tparameters *pr,char *fname,boolean saveWithDummies,Trrt *rrt)
3879 {
3880  double *o;
3881  unsigned int i,j,nv,nvs;
3882  boolean *systemVars;
3883  double *c;
3884  FILE *fo;
3885  Tfilename fsol;
3886 
3887  if (saveWithDummies)
3888  CreateFileName(NULL,fname,"_nodes",SOL_WITH_DUMMIES_EXT,&fsol);
3889  else
3890  CreateFileName(NULL,fname,"_nodes",SOL_EXT,&fsol);
3891  fprintf(stderr,"Writing boxes to : %s\n",GetFileFullName(&fsol));
3892  fo=fopen(GetFileFullName(&fsol),"w");
3893  if (!fo)
3894  Error("Could not open the file to store the boxes in SaveRRTNodes");
3895 
3896  nv=CS_WD_GET_SYSTEM_VARS(&systemVars,rrt->w);
3897  nvs=0;
3898  for(j=0;j<nv;j++)
3899  {
3900  if (systemVars[j])
3901  nvs++;
3902  }
3903 
3904  for(i=0;i<rrt->ns;i++)
3905  {
3906  c=rrt->si[i]->samples;
3907 
3908  CS_WD_REGENERATE_ORIGINAL_POINT(pr,c,&o,rrt->w);
3909 
3910  if (saveWithDummies)
3911  fprintf(fo,"%u { %u ",i,nv);
3912  else
3913  fprintf(fo,"%u { %u ",i,nvs);
3914  for(j=0;j<nv;j++)
3915  {
3916  if ((saveWithDummies)||(systemVars[j]))
3917  fprintf(fo,"[%.12g,%.12g] ",o[j],o[j]);
3918  }
3919  fprintf(fo,"}\n");
3920 
3921  free(o);
3922  }
3923 
3924  fclose(fo);
3925  free(systemVars);
3926  DeleteFileName(&fsol);
3927 }
3928 
3929 void SaveRRTCosts(Tparameters *pr,char *fname,Trrt *rrt)
3930 {
3931  unsigned int i;
3932  FILE *fo;
3933  Tfilename fcost;
3934  double ma,mi;
3935  unsigned int ima,imi;
3936  double c;
3937 
3938  CreateFileName(NULL,fname,"_nodes",COST_EXT,&fcost);
3939  fprintf(stderr,"Writing costs to : %s\n",GetFileFullName(&fcost));
3940 
3941  fo=fopen(GetFileFullName(&fcost),"w");
3942  if (!fo)
3943  Error("Could not open the file to store the costs in SaveRRTCosts");
3944 
3945  ma=-INF;
3946  mi=INF;
3947  for(i=0;i<rrt->ns;i++)
3948  {
3949  c=rrt->si[i]->cost;
3950  fprintf(fo,"%f\n",c);
3951  if (c>ma)
3952  {
3953  ma=c;
3954  ima=i;
3955  }
3956  if (c<mi)
3957  {
3958  mi=c;
3959  imi=i;
3960  }
3961  }
3962  fprintf(stderr," Extreme costs : %f (%u) -- %f (%u)\n",mi,imi,ma,ima);
3963 
3964  fclose(fo);
3965  DeleteFileName(&fcost);
3966 }
3967 
3968 unsigned int RRTMemSize(Trrt *rrt)
3969 {
3970  unsigned int b,i;
3971 
3972  /* The samples */
3973  b=sizeof(double)*rrt->m*rrt->ns;
3974 
3975  if (rrt->graph)
3976  {
3977  /* The neighbours */
3978  for(i=0;i<rrt->ns;i++)
3979  b+=rrt->si[i]->nn*(sizeof(double)+sizeof(unsigned int));
3980  }
3981 
3982  return(b);
3983 }
3984 
3985 void SaveRRT(Tfilename *fname,Trrt *rrt)
3986 {
3987  FILE *f;
3988  unsigned int i,j;
3989 
3990 
3991  f=fopen(GetFileFullName(fname),"w");
3992  if (!f)
3993  Error("Could not open file to store rrt");
3994 
3995  fprintf(f,"%u\n",rrt->graph);
3996  fprintf(f,"%u\n",rrt->mode);
3997  fprintf(f,"%f\n",rrt->dd);
3998  #if (_KDTREE==1)
3999  fprintf(f,"%f\n",KDtreeSamplingExpansion(rrt->treeS2G));
4000  #else
4001  fprintf(f,"0\n");
4002  #endif
4003 
4004  fprintf(f,"%u\n",rrt->m);
4005  fprintf(f,"%u\n",rrt->k);
4006  fprintf(f,"%u\n",rrt->n);
4007 
4008  fprintf(f,"%f\n",rrt->delta);
4009 
4010  fprintf(f,"%f\n",rrt->temperature);
4011  fprintf(f,"%u\n",rrt->nFail);
4012 
4013  fprintf(f,"%u\n",rrt->ms);
4014  fprintf(f,"%u\n",rrt->ns);
4015 
4016  for(i=0;i<rrt->ns;i++)
4017  {
4018  for(j=0;j<rrt->m;j++)
4019  fprintf(f,"%.12f ",rrt->si[i]->samples[j]);
4020  fprintf(f,"\n");
4021  fprintf(f,"%u %u %f %f %f\n",rrt->si[i]->parent,rrt->si[i]->tree,
4022  rrt->si[i]->costp,rrt->si[i]->cost,rrt->si[i]->ddr);
4023 
4024  if (rrt->graph)
4025  {
4026  fprintf(f,"%f %u %u",rrt->si[i]->g,rrt->si[i]->m,rrt->si[i]->nn);
4027  for(j=0;j<rrt->si[i]->nn;j++)
4028  fprintf(f," %u %f",rrt->si[i]->n[j],rrt->si[i]->cn[j]);
4029  fprintf(f,"\n");
4030  }
4031  }
4032 
4033  fclose(f);
4034 }
4035 
4036 void LoadRRT(Tparameters *pr,Tfilename *fname,TAtlasBase *w,Trrt *rrt)
4037 {
4038  FILE *f;
4039  unsigned int i,j;
4040  double dr;
4041 
4042  f=fopen(GetFileFullName(fname),"r");
4043  if (!f)
4044  Error("Could not open file to read the RRT");
4045 
4046  rrt->w=w;
4047 
4048  fscanf(f,"%u",&(rrt->graph));
4049  fscanf(f,"%u",&(rrt->mode));
4050  fscanf(f,"%lf",&(rrt->dd));
4051  fscanf(f,"%lf",&dr);
4052 
4053  fscanf(f,"%u",&(rrt->m));
4054  fscanf(f,"%u",&(rrt->k));
4055  fscanf(f,"%u",&(rrt->n));
4056 
4057  fscanf(f,"%lf",&(rrt->delta));
4058 
4059  fscanf(f,"%lf",&(rrt->temperature));
4060  fscanf(f,"%u",&(rrt->nFail));
4061 
4062  fscanf(f,"%u",&(rrt->ms));
4063  fscanf(f,"%u",&(rrt->ns));
4064 
4065  NEW(rrt->si,rrt->ms,TRRTSampleInfo*);
4066  for(i=0;i<rrt->ms;i++)
4067  rrt->si[i]=NULL;
4068 
4069  SetRRTTopology(pr,rrt);
4070 
4071  NEW(rrt->ambient,1,Tbox);
4073 
4074  #if (_KDTREE==1)
4075  {
4076  unsigned int nd,i;
4077  Trectangle rec;
4078  Tinterval *range;
4079 
4080  nd=GetBoxNIntervals(rrt->ambient);
4081  InitRectangle(nd,NULL,NULL,&rec);
4082  for(i=0;i<nd;i++)
4083  {
4084  range=GetBoxInterval(i,rrt->ambient);
4085  SetRectangleLimits(i,LowerLimit(range),UpperLimit(range),&rec);
4086  }
4087 
4088  rrt->treeS2G=InitKDTree(&rec,rrt->tp,25,dr,0,NULL,NULL);
4089  if (rrt->mode==TWO_TREES)
4090  rrt->treeG2S=InitKDTree(&rec,rrt->tp,25,dr,0,NULL,NULL);
4091 
4092  DeleteRectangle(&rec);
4093  }
4094  #endif
4095 
4096  #if (_KDTREE==2)
4097  if ((!RRT_NN_TOPOLOGY)||(rrt->tp==NULL))
4098  rrt->treeS2G=CreateKDTree(rrt->m);
4099  else
4100  rrt->treeS2G=CreateKDTreeT(rrt->m,(int *)rrt->tp);
4101 
4102  if (rrt->mode==TWO_TREES)
4103  {
4104  if ((!RRT_NN_TOPOLOGY)||(rrt->tp==NULL))
4105  rrt->treeG2S=CreateKDTree(rrt->m);
4106  else
4107  rrt->treeG2S=CreateKDTreeT(rrt->m,(int *)rrt->tp);
4108 
4109  rrt->maxNodesT1=rrt->ms;
4110  rrt->maxNodesT2=rrt->ms;
4111 
4112  NEW(rrt->t1ToId,rrt->maxNodesT1,unsigned int);
4113  NEW(rrt->t2ToId,rrt->maxNodesT2,unsigned int);
4114 
4115  rrt->nodesT1=0;
4116  rrt->nodesT2=0;
4117  }
4118  #endif
4119 
4120  for(i=0;i<rrt->ns;i++)
4121  {
4122  NEW(rrt->si[i],1,TRRTSampleInfo);
4123 
4124  NEW(rrt->si[i]->samples,rrt->m,double);
4125  for(j=0;j<rrt->m;j++) /* sample */
4126  fscanf(f,"%lf",&(rrt->si[i]->samples[j]));
4127 
4128  fscanf(f,"%u",&(rrt->si[i]->parent));
4129  fscanf(f,"%u",&(rrt->si[i]->tree));
4130  fscanf(f,"%lf",&(rrt->si[i]->costp));
4131  fscanf(f,"%lf",&(rrt->si[i]->cost));
4132  fscanf(f,"%lf",&(rrt->si[i]->ddr));
4133 
4134  rrt->si[i]->userInfo=NULL; /* This information is not stored :( */
4135 
4136  #if (_KDTREE==1)
4137  if (rrt->mode==TWO_TREES)
4138  {
4139  if (rrt->si[i]->tree==START2GOAL)
4140  AddPoint2KDtree(rrt->si[i]->samples,i,&(rrt->treeS2G));
4141  else
4142  AddPoint2KDtree(rrt->si[i]->samples,i,&(rrt->treeG2S));
4143  }
4144  else
4145  AddPoint2KDtree(rrt->si[i]->samples,i,&(rrt->treeS2G));
4146  #endif
4147 
4148  #if (_KDTREE==2)
4149  if (rrt->mode==TWO_TREES)
4150  {
4151  if (rrt->si[i]->tree==START2GOAL)
4152  {
4153  rrt->t1ToId[rrt->nodesT1]=i;
4154  rrt->nodesT1++;
4155  AddPoint2KDTree(rrt->si[i]->samples,rrt->treeS2G);
4156  }
4157  else
4158  {
4159  rrt->t2ToId[rrt->nodesT2]=i;
4160  rrt->nodesT2++;
4161  AddPoint2KDTree(rrt->si[i]->samples,rrt->treeG2S);
4162  }
4163  }
4164  else
4165  AddPoint2KDTree(rrt->si[i]->samples,rrt->treeS2G);
4166  #endif
4167 
4168  if (rrt->graph)
4169  {
4170  fscanf(f,"%lf",&(rrt->si[i]->g));
4171  fscanf(f,"%u",&(rrt->si[i]->m));
4172  fscanf(f,"%u",&(rrt->si[i]->nn));
4173  NEW(rrt->si[i]->n,rrt->si[i]->m,unsigned int);
4174  NEW(rrt->si[i]->cn,rrt->si[i]->m,double);
4175  for(j=0;j<rrt->si[i]->nn;j++)
4176  {
4177  fscanf(f,"%u",&(rrt->si[i]->n[j]));
4178  fscanf(f,"%lf",&(rrt->si[i]->cn[j]));
4179  }
4180  }
4181  }
4182 
4183  fclose(f);
4184 
4185  NEW(rrt->ambient,1,Tbox);
4186  /* The Jacobian is not saved but re-computed */
4188 
4189  /* The parallelism information is not stored! We de-activate it
4190  when loading an RRT (load is typically used for visualization
4191  and not for further processing)*/
4192  rrt->nCores=1;
4193  rrt->parallel=FALSE;
4194 }
4195 
4196 void DeleteRRT(Trrt *rrt)
4197 {
4198  unsigned int i;
4199 
4200  for(i=0;i<rrt->ns;i++)
4201  {
4202  free(rrt->si[i]->samples);
4203  if (rrt->graph)
4204  {
4205  free(rrt->si[i]->n);
4206  free(rrt->si[i]->cn);
4207  }
4208  free(rrt->si[i]);
4209  }
4210 
4211  if (rrt->ambient!=NULL)
4212  {
4213  DeleteBox(rrt->ambient);
4214  free(rrt->ambient);
4215  }
4216 
4217  free(rrt->si);
4218 
4219  if (rrt->tp!=NULL)
4220  free(rrt->tp);
4221 
4222  #if (_KDTREE==1)
4223  DeleteKDtree(rrt->treeS2G);
4224  if (rrt->mode==TWO_TREES)
4225  DeleteKDtree(rrt->treeG2S);
4226  #endif
4227  #if (_KDTREE==2)
4228  DeleteKDTree(rrt->treeS2G);
4229  if (rrt->mode==TWO_TREES)
4230  {
4231  DeleteKDTree(rrt->treeG2S);
4232 
4233  free(rrt->t1ToId);
4234  free(rrt->t2ToId);
4235  }
4236  #endif
4237 }
4238 
double GetRRTNodeCost(unsigned int i, Trrt *rrt)
Returns the cost associated with a node.
Definition: rrt.c:3588
TRRTSampleInfo ** si
Definition: rrt.h:354
unsigned int nFail
Definition: rrt.h:340
double * cn
Definition: rrt.h:314
boolean cBiRRT(Tparameters *pr, double *pg, double *time, double *pl, unsigned int *ns, double ***path, TRRTStatistics *grst, Trrt *rrt)
Extends a RRT until we reach a targed point.
Definition: rrt.c:3099
void PlotVect3d(unsigned int n, double *x, double *y, double *z, Tplot3d *p)
Adds a polyline to the current object.
Definition: plot3d.c:447
CBLAS_INLINE void ScaleVector(double f, unsigned int s, double *v)
Scales a vector.
Definition: basic_algebra.c:30
Statistics on the RRT construction.
Definition: rrt.h:199
void DeleteVector(void *vector)
Destructor.
Definition: vector.c:388
void AddEdgeToRRT(unsigned int i, unsigned int j, double c, Trrt *rrt)
Adds a neighbouring relation to an RRT.
Definition: rrt.c:784
unsigned int nCollisionChecks
Definition: rrt.h:230
Tinterval * GetBoxInterval(unsigned int n, Tbox *b)
Returns a pointer to one of the intervals defining the box.
Definition: box.c:270
unsigned int StepsToRoot(unsigned int sID, Trrt *rrt)
Computes the number of steps from a node to the root.
Definition: rrt.c:3714
boolean RRTSample(unsigned int samplingMode, unsigned int tree, double *goal, double *q_rand, TRRTStatistics *rst, Trrt *rrt)
Generates a random sample to expand the RRT.
Definition: rrt.c:1471
unsigned int id
Definition: rrt.h:247
unsigned int nRandom
Definition: rrt.h:225
void DeleteRRT(Trrt *rrt)
Destructor.
Definition: rrt.c:4196
unsigned int VectorSize(Tvector *vector)
Gets the number of elements in a vector.
Definition: vector.c:169
double temperature
Definition: rrt.h:339
void PrintRRTStatistics(Trrt *rrt, TRRTStatistics *rst)
Prints the summary of RRT statistics.
Definition: rrt.c:352
void SaveRRTNodes(Tparameters *pr, char *fname, boolean saveWithDummies, Trrt *rrt)
Stores the nodes of the RRT.
Definition: rrt.c:3878
#define CS_WD_ERROR_IN_SIMP_EQUATIONS(pr, p, wcs)
Computes the error in the simplified system for a given point.
Definition: wcs.h:446
#define CT_EPSILON
Numerical tolerance.
Definition: parameters.h:194
#define FALSE
FALSE.
Definition: boolean.h:30
unsigned int GetRRTParent(unsigned int i, Trrt *rrt)
Returns identifier of the parent of one of the nodes of the RRT.
Definition: rrt.c:3547
#define CS_WD_GENERATE_SIMPLIFIED_POINT(pr, p, r, wcs)
Generates a simplified point from an original one.
Definition: wcs.h:432
boolean BiRRTstar(Tparameters *pr, double *pg, unsigned int *it, double *times, double *costs, double *planningTime, double *pl, unsigned int *ns, double ***path, TRRTStatistics *grst, Trrt *rrt)
Optimal RRT on manifolds.
Definition: rrt.c:2584
double DistanceTopology(unsigned int s, unsigned int *tp, double *v1, double *v2)
Computes the distance of two points.
boolean ccRRT(Tparameters *pr, double *pg, double *time, double *pl, unsigned int *ns, double ***path, TRRTStatistics *grst, Trrt *rrt)
Extends a RRT until we reach a targed point.
Definition: rrt.c:2966
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
void GetRRTNNInBall(unsigned int tree, double *q_rand, double r, unsigned int *nn, unsigned int **n, Trrt *rrt)
Locates the all the sample closer than a given distance.
Definition: rrt.c:1654
Data structure to hold the information about the name of a file.
Definition: filename.h:248
void ArrayPi2Pi(unsigned int n, unsigned int *t, double *a)
Applies PI2PI to an array.
A pair of dubles.
Definition: vector.h:114
double RRTPathLength(Tparameters *pr, unsigned int sID, Trrt *rrt)
Path length from a sample to the root.
Definition: rrt.c:1182
void DeleteRRTStep(void *a)
RRTStep destructor.
Definition: rrt.c:756
void ConcatVectors(Tvector *vector1, Tvector *vector)
Concatenates two vectors.
Definition: vector.c:332
void ReWireRRTstar(Tparameters *pr, unsigned int id_new, double gamma, unsigned int nn, unsigned int *n, double *c, Tvector *steps, double *l, Trrt *rrt)
Rewires an RRT star.
Definition: rrt.c:2213
void SetRRTNodeInfo(unsigned int i, void *info, Trrt *rrt)
Changes the user info associated with a node.
Definition: rrt.c:3580
unsigned int nConvergenceError
Definition: rrt.h:214
boolean TransitionTestRRT(Tparameters *pr, unsigned int parent, double *q_next, double deltaStep, double *cost, double(*costF)(Tparameters *, boolean, double *, void *), void *costData, Trrt *rrt)
transition Test for Transition RRT
Definition: rrt.c:809
void WireRRTstar(Tparameters *pr, unsigned int id_new, unsigned int i_near, double gamma, unsigned int nn, unsigned int *n, double **c, double h, Theap *q, unsigned int *t, TRRTStatistics *rst, Trrt *rrt)
Wires a node to the RRT star.
Definition: rrt.c:2024
void * GetVectorElement(unsigned int i, Tvector *vector)
Returns a pointer to a vector element.
Definition: vector.c:269
unsigned int Steps2PathinRRT(Tparameters *pr, Tvector *steps, double *pl, double *pc, unsigned int *ns, double ***path, Trrt *rrt)
Defines a path from a vector of steps.
Definition: rrt.c:1094
unsigned int GetRRTNodeTree(unsigned int i, Trrt *rrt)
Returns the tree for a given node.
Definition: rrt.c:3534
#define CT_NFAIL_MAX
Number of successive trnasition test failues requiered before increasing the temperature.
Definition: parameters.h:460
#define RRT_VERBOSE
Vebosity of the RRT operations.
Definition: rrt.h:32
double g
Definition: rrt.h:308
unsigned int mode
Definition: rrt.h:356
void InitStatistics(unsigned int np, double v, Tstatistics *t)
Constructor.
Definition: statistics.c:21
CBLAS_INLINE double Norm(unsigned int s, double *v)
Computes the norm of a vector.
void SetRRTNodeCost(unsigned int i, double costp, double cost, Trrt *rrt)
Changes the cost associated with a node.
Definition: rrt.c:3604
#define CT_MAX_PLANNING_TIME
Maximum time for path planning.
Definition: parameters.h:475
void DifferenceVectorTopology(unsigned int s, unsigned int *tp, double *v1, double *v2, double *v)
Substracts two vectors.
void NewRRTHighCost(TRRTStatistics *rst)
A high cost configuration is reached.
Definition: rrt.c:268
void NewRRTQrandReached(TRRTStatistics *rst)
Random sample reached.
Definition: rrt.c:258
boolean AddNodeToRRT(Tparameters *pr, unsigned int tree, unsigned int i_near, double *sample, double *goal, unsigned int *lastSample, void *info, double costp, double cost, TRRTStatistics *rst, Trrt *rrt)
Adds a point to a RRT.
Definition: rrt.c:1869
CBLAS_INLINE void AccumulateVector(unsigned int s, double *v1, double *v2)
Adds a vector to another vectors.
Definition: basic_algebra.c:55
void NewRRTRandomSample(TRRTStatistics *rst)
New random sample.
Definition: rrt.c:303
#define CT_SAMPLING
Sampling mode.
Definition: parameters.h:563
double UpdateBiRRTSteps(Tvector *steps, Trrt *rrt)
Updates the current optimal path.
Definition: rrt.c:3357
Definition of the Tfilename type and the associated functions.
void LoadRRT(Tparameters *pr, Tfilename *fname, TAtlasBase *w, Trrt *rrt)
Defines a RRT from the information on a file.
Definition: rrt.c:4036
#define START2GOAL
One of the possible trees.
Definition: rrt.h:166
#define ONE_TREE
One of the modes for the RRT.
Definition: rrt.h:130
unsigned int tree
Definition: rrt.h:316
#define TEMPERATURE_INIT
Initial temperature used in Transition based rrt searches.
Definition: rrt.h:79
TAtlasBase * w
Definition: rrt.h:330
void InitPlot3d(char *name, boolean axes, int argc, char **arg, Tplot3d *p)
Constructor.
Definition: plot3d.c:41
unsigned int m
Definition: rrt.h:311
#define COST_EXT
File extension for arrays of costs.
Definition: filename.h:155
void NewRRTOutOfDomain(TRRTStatistics *rst)
The branch leaves the domain.
Definition: rrt.c:273
#define CT_GAMMA
Contant part of the search radius for nearest neightours in RRT*.
Definition: parameters.h:325
#define TRUE
TRUE.
Definition: boolean.h:21
void NewRRTStep(TRRTStatistics *rst)
New step in branch extension.
Definition: rrt.c:253
void Error(const char *s)
General error function.
Definition: error.c:80
void NewRRTTreeConnection(TRRTStatistics *rst)
New attempt to connect the two trees.
Definition: rrt.c:238
A color.
Definition: color.h:23
A RRT on a manifold.
Definition: rrt.h:329
unsigned int n
Definition: rrt.h:335
#define INIT_NUM_SAMPLES_RRT
Initial room for samples of an rrt.
Definition: rrt.h:123
unsigned int nStalled
Definition: rrt.h:220
boolean graph
Definition: rrt.h:358
void NewRRTBranch(TRRTStatistics *rst)
New branch creation.
Definition: rrt.c:228
boolean ccTRRT(Tparameters *pr, double *pg, double *time, double *pl, double *pc, unsigned int *ns, double ***path, double(*costF)(Tparameters *, boolean, double *, void *), void *costData, TRRTStatistics *grst, Trrt *rrt)
Extends a T-RRT until we reach a targed point.
Definition: rrt.c:2823
void RandomPointInBox(boolean *used, double *c, Tbox *b)
Returns the a random point along the selected dimensions.
Definition: box.c:682
#define CT_MAX_PLANNING_ITERATIONS
Maximum iterations for path planning.
Definition: parameters.h:483
unsigned int GetBoxNIntervals(Tbox *b)
Box dimensionality.
Definition: box.c:992
double randomDouble()
Returns a random double in the [0,1] interval.
Definition: random.c:33
void NewRRTTooFar(TRRTStatistics *rst)
New large step.
Definition: rrt.c:288
void AddElement2Heap(unsigned int id, void *e, Theap *heap)
Adds an element to the heap.
Definition: heap.c:294
void DeleteDoublePair(void *a)
Destructor for pairs of doubles.
Definition: vector.c:77
Definition of a rrt on a manifold.
void ReverseConcatSamples(unsigned int nvs, unsigned int ns1, double **path1, unsigned int ns2, double **path2, unsigned int *ns, double ***path)
Reverses and concats a path.
Definition: samples.c:1171
void DeleteFileName(Tfilename *fn)
Destructor.
Definition: filename.c:205
A 3D plot.
Definition: plot3d.h:54
unsigned int ms
Definition: rrt.h:350
unsigned int GetRRTNN(unsigned int tree, double *q_rand, Trrt *rrt)
Locates the nearest sample in the tree of samples.
Definition: rrt.c:1547
#define CT_CUT_X
Limit of domain of the X dimension of 3d plots.
Definition: parameters.h:420
boolean RRTValidateSample(Tparameters *pr, double *q_rand, unsigned int tree, boolean expand2goal, double *goal, double l, double *h, unsigned int *i_near, TRRTStatistics *rst, Trrt *rrt)
Validates a sample generate with RRTSample.
Definition: rrt.c:1510
void RecursiveReWireRRTstar(Tparameters *pr, Theap *q, double *g, Tvector *steps, double *l, Trrt *rrt)
A rewire that is propagates as much as necessary over the graph.
Definition: rrt.c:2145
unsigned int * tp
Definition: rrt.h:347
void AddSample2Samples(unsigned int nv, double *sample, unsigned int nvs, boolean *systemVars, unsigned int *ms, unsigned int *ns, double ***path)
Adds a sample to a set of samples.
Definition: samples.c:150
double costp
Definition: rrt.h:294
boolean LessThanDoublePair(void *a, void *b, void *userData)
Comparison operator for paris of doubles.
Definition: heap.c:224
unsigned int ReconstructRRTPath(Tparameters *pr, unsigned int sID, double *pl, double *pc, unsigned int *ns, double ***path, Trrt *rrt)
Collects samples from the tree root to a given sample.
Definition: rrt.c:1077
double LowerLimit(Tinterval *i)
Gets the lower limit.
Definition: interval.c:79
Tbox * ambient
Definition: rrt.h:352
void GetRRTNNInBranch(unsigned int tree, unsigned int n1, unsigned int n2, unsigned int *n, unsigned int *nn, Trrt *rrt)
Locates the nearest sample in the tree to a branch.
Definition: rrt.c:1775
void SetRRTCostAndParent(unsigned int i, unsigned int p, double costp, double cost, Trrt *rrt)
Changes the cost and the parent associated with a node.
Definition: rrt.c:3615
unsigned int * GetRRTTopology(Trrt *rrt)
Returns a pointer to an array with the topology for each variable.
Definition: rrt.c:3633
void ResetVector(Tvector *vector)
Resets a vector.
Definition: vector.c:116
void UpdateCostToRoot(unsigned int sID, Trrt *rrt)
Updates the cost from a node to the root.
Definition: rrt.c:3653
void * GetHeapElement(unsigned int i, Theap *heap)
Returns a pointer to a heap element.
Definition: heap.c:331
double FirstInPair(TDoublePair *p)
The first element of a pair.
Definition: vector.c:55
unsigned int ns
Definition: rrt.h:349
#define TWO_TREES
One of the modes for the RRT.
Definition: rrt.h:139
#define CT_CUT_Z
Limit of domain of the Z dimension of 3d plots.
Definition: parameters.h:444
#define CS_WD_SIMP_INEQUALITIES_HOLD(pr, p, wcs)
Cheks if all inequalities hold.
Definition: wcs.h:207
boolean InDynamicDomain(unsigned int i, double *q, Trrt *rrt)
Checks if a sample is in the dynamic domaiin of a given node.
Definition: rrt.c:3474
#define KDTREE_SAMPLING
One of the possible sampling modes.
Definition: parameters.h:162
boolean AddBranchToRRT(Tparameters *pr, boolean ccRRT, unsigned int tree, unsigned int i_near, double *q_rand, double *goal, unsigned int *lastSample, double(*costF)(Tparameters *, boolean, double *, void *), void *costData, TRRTStatistics *rst, Trrt *rrt)
Extends a RRT as much as possible towards a given sample.
Definition: rrt.c:850
Definitions of constants and macros used in several parts of the cuik library.
unsigned int nNoEmptyBranch
Definition: rrt.h:204
void InitVector(unsigned int ele_size, void(*Copy)(void *, void *), void(*Delete)(void *), unsigned int max_ele, Tvector *vector)
Constructor.
Definition: vector.c:100
#define HEURISTIC_RRT_STAR
Whether to use an heuristic in AtlarRRT*.
Definition: rrt.h:54
unsigned int k
Definition: rrt.h:334
#define CS_WD_NEWTON_IN_SIMP(pr, p, wcs)
Applies a Newton procedure to move a point towads the manifold.
Definition: wcs.h:488
boolean parallel
Definition: rrt.h:344
Statistics associated with a solving process.
Definition: statistics.h:28
unsigned int nQrandReached
Definition: rrt.h:213
#define MOV_AVG_UP
Weight of new data when computing moving averages.
Definition: defines.h:451
boolean HeapEmpty(Theap *heap)
Checks if a heap is empty.
Definition: heap.c:289
boolean DynamicDomainRRT(Trrt *rrt)
TRUE if the dynamic domain is active.
Definition: rrt.c:3516
double dd
Definition: rrt.h:362
double RRTPathSteps(unsigned int sID, Tvector *path, Trrt *rrt)
Produces a vector with the nodes in a given branch.
Definition: rrt.c:3277
void DeleteHeap(Theap *heap)
Destructor.
Definition: heap.c:388
unsigned int nNoEmptyTreeConnection
Definition: rrt.h:207
void Warning(const char *s)
General warning function.
Definition: error.c:116
A table of parameters.
#define BOTHTREES
Used for samples in both trees.
Definition: rrt.h:181
void CreateFileName(char *path, char *name, char *suffix, char *ext, Tfilename *fn)
Constructor.
Definition: filename.c:22
double GetRRTNodeCostFromParent(unsigned int i, Trrt *rrt)
Returns the cost from the parent node, if defined.
Definition: rrt.c:3596
double UpperLimit(Tinterval *i)
Gets the uppser limit.
Definition: interval.c:87
double cost
Definition: rrt.h:296
void PlotRRT(char *fname, int argc, char **arg, Tparameters *pr, unsigned int xID, unsigned int yID, unsigned int zID, Trrt *rrt)
Pots a projection of a RRT.
Definition: rrt.c:3729
void InitRRT(Tparameters *pr, boolean parallel, boolean simp, double *ps, unsigned int mode, boolean graph, double *pg, unsigned int m, TAtlasBase *w, Trrt *rrt)
Defines a RRT from a given point.
Definition: rrt.c:1204
void CopyRRTStep(void *a, void *b)
Copy constructor for RRTSteps.
Definition: rrt.c:750
void UpdateTree(unsigned int sID, Trrt *rrt)
Updates the tree assignment a node to the root.
Definition: rrt.c:3658
boolean PathStart2GoalInRRT(Tparameters *pr, double *pgs, unsigned int l1, unsigned int l2, double *pl, double *pc, unsigned int *ns, double ***path, Trrt *rrt)
Determines the path from start to goal using an RRT.
Definition: rrt.c:3402
double * GetRRTNode(unsigned int i, Trrt *rrt)
Returns one of the nodes of the RRT.
Definition: rrt.c:3526
Type defining the equations on which the atlas is defined.
Definition: wcs.h:30
unsigned int RRTMemSize(Trrt *rrt)
Memory used by a given RRT.
Definition: rrt.c:3968
A generic vector.
Definition: vector.h:227
#define CS_WD_GET_SYSTEM_VARS(sv, wcs)
Gets the system variables.
Definition: wcs.h:238
char * GetFileFullName(Tfilename *fn)
Gets the file full name (paht+name+extension).
Definition: filename.c:151
#define M_2PI
2*Pi.
Definition: defines.h:100
#define SOL_EXT
File extension for solution files.
Definition: filename.h:137
#define EXPLORATION_RRT
Select between exploration and goal driven RRT.
Definition: rrt.h:96
unsigned int nOutOfDomain
Definition: rrt.h:216
#define CS_WD_REGENERATE_ORIGINAL_POINT(pr, p, o, wcs)
Completes an original point from a simplified one.
Definition: wcs.h:279
unsigned int m
Definition: rrt.h:332
unsigned int nCollision
Definition: rrt.h:218
double cost
Definition: rrt.h:248
void InitHeap(unsigned int ele_size, void(*Copy)(void *, void *), void(*Delete)(void *), boolean(*LessThan)(void *, void *, void *), void *userData, boolean hasIDs, unsigned int max_ele, Theap *heap)
Constructor.
Definition: heap.c:237
double CostToRoot(unsigned int sID, Trrt *rrt)
Computes the cost from a node to the root.
Definition: rrt.c:3638
unsigned int nDistanceIncreases
Definition: rrt.h:217
A box.
Definition: box.h:83
double ChangeBiRRTSteps(unsigned int l1, unsigned int l2, double c12, Tvector *steps, Trrt *rrt)
Changes the optimal path in a bidirectional RRT.
Definition: rrt.c:3298
void ReverseSamples(unsigned int ns, double **path)
Reverses a set of samples.
Definition: samples.c:1196
void BiRRTstarCloseIteration(unsigned int it, double l, double time, double gamma, double *times, double *costs, Trrt *rrt)
Prints information about the BiRRT* iteration.
Definition: rrt.c:2324
unsigned int GetRRTNumNodes(Trrt *rrt)
Number of nodes in the RRT.
Definition: rrt.c:3521
void * GetRRTNodeInfo(unsigned int i, Trrt *rrt)
Returns the user info associated with a node.
Definition: rrt.c:3572
#define MEM_DUP(_var, _n, _type)
Duplicates a previously allocated memory space.
Definition: defines.h:414
#define NO_UINT
Used to denote an identifier that has not been initialized.
Definition: defines.h:435
#define RRT_NN_TOPOLOGY
Set to 1 to use the topology in the search for nearest-neighbours.
Definition: rrt.h:115
CBLAS_INLINE void SumVectorScale(unsigned int s, double *v1, double w, double *v2, double *v)
Adds two vectors with a scale.
Definition: basic_algebra.c:86
unsigned int nTooFar
Definition: rrt.h:219
boolean IsRRTGraph(Trrt *rrt)
Identifies RRT with a graph structure.
Definition: rrt.c:1466
void NewRRTSampleRejection(TRRTStatistics *rst)
New sample rejection.
Definition: rrt.c:308
void NewRRTConvergenceError(TRRTStatistics *rst)
Convergence error.
Definition: rrt.c:263
void InitSamples(unsigned int *ms, unsigned int *ns, double ***path)
Initializes a set of samples.
Definition: samples.c:143
void SetRRTParent(unsigned int i, unsigned int p, Trrt *rrt)
Changes the parent for a given node.
Definition: rrt.c:3555
Step in a solution path.
Definition: rrt.h:246
double SquaredDistanceTopology(unsigned int s, unsigned int *tp, double *v1, double *v2)
Computes the squared distance of two points.
unsigned int AddStepToRRTstar(Tparameters *pr, unsigned int i_near, double *q_rand, double *goal, unsigned int *i_goal, TRRTStatistics *rst, Trrt *rrt)
Extend strategy for the RRT.
Definition: rrt.c:1971
void DeleteBox(void *b)
Destructor.
Definition: box.c:1259
#define CT_DYNAMIC_DOMAIN
Dynamic domain.
Definition: parameters.h:533
void DeleteRRTStatistics(TRRTStatistics *rst)
Destructor.
Definition: rrt.c:499
#define SOL_WITH_DUMMIES_EXT
File extension for solution files with dummies.
Definition: filename.h:143
void NewDoublePair(double f, double s, TDoublePair *p)
Constructor.
Definition: vector.c:49
unsigned int GetRRTMode(Trrt *rrt)
Identifies mode of the RRTs.
Definition: rrt.c:3469
void SetRRTTopology(Tparameters *pr, Trrt *rrt)
Initializes the topology array in the RRT.
Definition: rrt.c:760
void NewRRTCollisionCheck(TRRTStatistics *rst)
New collision check.
Definition: rrt.c:313
unsigned int ExtractMinElement(void *e, Theap *heap)
Extracts and removes the minimal element of a heap.
Definition: heap.c:356
double * samples
Definition: rrt.h:291
double DistanceTopologyMin(double t, unsigned int s, unsigned int *tp, double *v1, double *v2)
Computes the distance of two points, if it is below a given threshold.
#define MEM_EXPAND(_var, _n, _type)
Expands a previously allocated memory space.
Definition: defines.h:404
#define TWO_TREES_WITH_SWAP
One of the modes for the RRT.
Definition: rrt.h:152
double GetParameter(unsigned int n, Tparameters *p)
Gets the value for a particular parameter.
Definition: parameters.c:93
A generic binary heap.
Definition: heap.h:105
#define CS_WD_REGENERATE_SOLUTION_POINT(pr, p, r, wcs)
Compleates a solution point with the dummy variables.
Definition: wcs.h:417
double ConnectSamples(Tparameters *pr, unsigned int *tp, Tbox *domain, unsigned int m, unsigned int n, double *s1, double *s2, double md, boolean checkCollisions, boolean *reached, boolean *collision, double *lastSample, unsigned int *ns, double ***path, TAtlasBase *w)
Determines the connection between two points on the manifold.
Definition: samples.c:434
#define CT_CUT_Y
Limit of domain of the Y dimension of 3d plots.
Definition: parameters.h:432
unsigned int nCores
Definition: rrt.h:343
void SaveRRTCosts(Tparameters *pr, char *fname, Trrt *rrt)
Stores the cost associated with the nodes of the RRT.
Definition: rrt.c:3929
void DeleteColor(Tcolor *c)
Destructor.
Definition: color.c:93
unsigned int parent
Definition: rrt.h:292
#define CS_WD_IN_COLLISION(f, pr, s, sPrev, wcs)
Checks if a configuration is in collision.
Definition: wcs.h:319
#define INF
Infinite.
Definition: defines.h:70
void NewRRTDistanceQrand(double dqr, TRRTStatistics *rst)
New distance to q_rand.
Definition: rrt.c:248
void RRTstarCloseIteration(unsigned int it, unsigned int id_goal, double time, double gamma, double *times, double *costs, Trrt *rrt)
Prints information about the RRT* iteration.
Definition: rrt.c:2295
#define CT_DELTA
Size of the steps in the path connecting two configurations.
Definition: parameters.h:282
double GetTRRTTemperature(Trrt *rrt)
Temperature of the T-RRT.
Definition: rrt.c:1461
#define CT_COEF_TEMP
Scale factor of the temperature parameter for Transition-based RRT.
Definition: parameters.h:451
#define CT_N_DOF
Dimensionality of the solution space for the mechanism at hand.
Definition: parameters.h:318
Auxiliary functions to deal with sets of samples.
#define GOAL2START
One of the possible trees.
Definition: rrt.h:174
void NewRRTCollision(TRRTStatistics *rst)
New collision.
Definition: rrt.c:283
Definition of basic randomization functions.
#define CS_WD_INIT_CD(pr, mt, wcs)
Initializes the collision detector.
Definition: wcs.h:294
double GetElapsedTime(Tstatistics *t)
Elapsed time.
Definition: statistics.c:92
void NewColor(double r, double g, double b, Tcolor *c)
Constructor.
Definition: color.c:14
void CopyDoublePair(void *a, void *b)
Copy constructor for pairs of doubles.
Definition: vector.c:71
boolean Bidirectional(Trrt *rrt)
Identifies bidirectional RRTs.
Definition: rrt.c:3463
void AccumulateRRTStatistics(TRRTStatistics *rst1, TRRTStatistics *rst2)
Accumulates two sets of RRT statistics.
Definition: rrt.c:318
void DeleteSamples(unsigned int ns, double **path)
Deletes the space used by a set of samples.
Definition: samples.c:1495
unsigned int nn
Definition: rrt.h:312
void AdjustDynamicDomain(unsigned int i, boolean collision, Trrt *rrt)
Sets the dynamic domain.
Definition: rrt.c:3482
void NewRRTNoEmptyBranch(TRRTStatistics *rst)
New no empty branch.
Definition: rrt.c:233
unsigned int nTreeConnection
Definition: rrt.h:206
unsigned int nRejections
Definition: rrt.h:227
#define CS_WD_GENERATE_SIMP_INITIAL_BOX(pr, b, wcs)
Computes the global box for the simplified system.
Definition: wcs.h:460
unsigned int nSample
Definition: rrt.h:222
Defines a interval.
Definition: interval.h:33
void * userInfo
Definition: rrt.h:317
boolean RRTstar(Tparameters *pr, double *pg, unsigned int *it, double *times, double *costs, double *planningTime, double *pl, unsigned int *ns, double ***path, TRRTStatistics *grst, Trrt *rrt)
Optimal RRT on manifolds.
Definition: rrt.c:2352
void InitRRTStatistics(TRRTStatistics *rst)
Init the RRT statistics.
Definition: rrt.c:195
unsigned int nHighCost
Definition: rrt.h:215
void NewRRTSample(TRRTStatistics *rst)
New sample.
Definition: rrt.c:298
unsigned int nStep
Definition: rrt.h:209
void NewRRTNoEmptyTreeConnection(TRRTStatistics *rst)
New non-void attempt to connect the two trees.
Definition: rrt.c:243
unsigned int StartNew3dObject(Tcolor *c, Tplot3d *p)
Start a composed object.
Definition: plot3d.c:157
void DeleteStatistics(Tstatistics *t)
Destructor.
Definition: statistics.c:274
Information for each sample in a RRT.
Definition: rrt.h:290
#define DIVERGED
One of the possible outputs of the Newton iteration.
Definition: cuiksystem.h:111
void NewRRTDistanceIncreases(TRRTStatistics *rst)
The distance to q_rand increase.
Definition: rrt.c:278
unsigned int n
Definition: rrt.h:200
double dQrand
Definition: rrt.h:210
#define CS_WD_GET_SIMP_TOPOLOGY(pr, tp, wcs)
Gets the simplified variable topology.
Definition: wcs.h:401
double GetDynamicDomainRadius(unsigned int i, Trrt *rrt)
Returns the current dynamic domain radius for a given sample.
Definition: rrt.c:3508
void Close3dObject(Tplot3d *p)
Closes a composed object.
Definition: plot3d.c:171
void UpdateCostAndTree(unsigned int sID, Trrt *rrt)
A combination of UpdateCostToRoot and UpdateTree.
Definition: rrt.c:3687
unsigned int nBranch
Definition: rrt.h:203
double ddr
Definition: rrt.h:306
void UpdateHeapElement(unsigned int id, void *e, Theap *heap)
Updates the position of an element in the heap.
Definition: heap.c:308
void ClosePlot3d(boolean quit, double average_x, double average_y, double average_z, Tplot3d *p)
Destructor.
Definition: plot3d.c:473
#define CT_MAX_NODES_RRT
Maximum number of nodes in an RRT.
Definition: parameters.h:497
unsigned int NewVectorElement(void *e, Tvector *vector)
Adds an element to the vector.
Definition: vector.c:212
#define TOPOLOGY_S
One of the possible topologies.
Definition: defines.h:139
double delta
Definition: rrt.h:337
boolean PointInBoxTopology(boolean *used, boolean update, unsigned int n, double *v, double tol, unsigned int *tp, Tbox *b)
Checks if a point is included in a(sub-) box.
Definition: box.c:350
void NewRRTStalled(TRRTStatistics *rst)
New stalled branch.
Definition: rrt.c:293
void SaveRRT(Tfilename *fname, Trrt *rrt)
Stores the RRT information on a file.
Definition: rrt.c:3985
unsigned int * n
Definition: rrt.h:313
#define CS_WD_ORIGINAL_IN_COLLISION(pr, o, oPrev, wcs)
Checks if a configuration is in collision.
Definition: wcs.h:348
#define MOV_AVG_DOWN
Weight of new data when computing moving averages.
Definition: defines.h:467