cuikplotatlasrrt.c
Go to the documentation of this file.
1 
2 #include "world.h"
3 #include "parameters.h"
4 
5 #include "defines.h"
6 #include "error.h"
7 #include "filename.h"
8 #include "atlasrrt.h"
9 #include "samples.h"
10 
11 #include <stdlib.h>
12 
67 int main(int argc, char **arg)
68 {
69  TAtlasBase world; /* The set of mechanism and obstacles. */
70  Tparameters parameters; /* Parameters used in the Cuik process. */
71  Tatlasrrt atlasRRT; /* The atlas to plot. */
72 
73  unsigned int nx,ny,nz;
74  unsigned int xID,yID,zID;
75  unsigned int i,nv,nvs;
76  boolean *systemVars;
77 
78  double *s1,*s2;
79  double *p1,*p2;
80 
81  Tfilename fparam;
82  Tfilename fpath;
83 
84  boolean havePath,traj;
85  unsigned int ns;
86  double **path;
87  unsigned int da;
88  double **actions;
89  double *times;
90 
91 
92  if (argc==5)
93  {
94  /*Init parameters*/
95  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
96  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
97  fprintf(stderr,"Reading parameters from : %s\n",GetFileFullName(&fparam));
98 
99  /*Read the world from file*/
100  CS_WD_INIT(&parameters,arg[1],&world);
101 
102  /* Validate the input projection dimensions */
103  nv=CS_WD_GET_SYSTEM_VARS(&systemVars,&world);
104  nvs=CS_WD_GET_NUM_SYSTEM_VARS(&world);
105 
106  nx=atoi(arg[2]);
107  ny=atoi(arg[3]);
108  nz=atoi(arg[4]);
109 
110  if (nx>=nvs)
111  Error("First index out of range");
112  if (ny>=nvs)
113  Error("Second index out of range");
114  if (nz>=nvs)
115  Error("Third index out of range");
116 
117  fprintf(stderr,"Projecting on variables : ");
118  PRINT_VARIABLE_NAME(stderr,CS_WD_GET_SYSTEM_VAR_NAME(nx,&world));fprintf(stderr,",");
119  PRINT_VARIABLE_NAME(stderr,CS_WD_GET_SYSTEM_VAR_NAME(ny,&world));fprintf(stderr,",");
120  PRINT_VARIABLE_NAME(stderr,CS_WD_GET_SYSTEM_VAR_NAME(nz,&world));fprintf(stderr,"\n");
121 
122  nvs=0;
123  for(i=0;i<nv;i++)
124  {
125  if (systemVars[i])
126  {
127  /* Transform index from system variables only to
128  all variables (in the original system in both cases!!) */
129  if (nx==nvs) xID=i;
130  if (ny==nvs) yID=i;
131  if (nz==nvs) zID=i;
132  nvs++;
133  }
134  }
135 
136  /* Read the Atlas, the RRT, and information linking them */
137  LoadAtlasRRT(&parameters,arg[1],&world,&atlasRRT);
138 
139  /* Read the start and goal of the RRT */
140  NEW(p1,3,double);
141  if (EXPLORATION_RRT)
142  {
143  ReadOneSample(&parameters,arg[1],CS_WD_GET_NUM_SYSTEM_VARS(&world),&s1);
144  s2=NULL;
145  p2=NULL;
146  }
147  else
148  {
149  ReadTwoSamples(&parameters,arg[1],CS_WD_GET_NUM_SYSTEM_VARS(&world),&s1,&s2);
150  NEW(p2,3,double);
151  p2[0]=s2[nx];
152  p2[1]=s2[ny];
153  p2[2]=s2[nz];
154  }
155  p1[0]=s1[nx];
156  p1[1]=s1[ny];
157  p1[2]=s1[nz];
158 
159  /* Plot the atlas, the RRT and the information linking them */
160  PlotAtlasRRT(arg[1],argc,arg,&parameters,xID,yID,zID,p1,p2,&atlasRRT);
161 
162  traj=TRUE;
163  CreateFileName(NULL,arg[1],"_traj",SOL_EXT,&fpath);
164 
165  havePath=traj=LoadTrajectory(&fpath,&nv,&ns,&path,&da,&actions,&times);
166  if (!traj)
167  {
168  DeleteFileName(&fpath);
169  CreateFileName(NULL,arg[1],"_path",SOL_EXT,&fpath);
170  havePath=LoadSamples(&fpath,&nv,&ns,&path);
171  }
172 
173  if (havePath)
174  {
175  Tfilename fpathplot;
176  Tplot3d p3d;
177  Tcolor red;
178 
179  double *x,*y,*z;
180 
181  NEW(x,ns,double);
182  NEW(y,ns,double);
183  NEW(z,ns,double);
184 
185  for(i=0;i<ns;i++)
186  {
187  /* For the paths we only store system variables */
188  x[i]=path[i][nx];
189  y[i]=path[i][ny];
190  z[i]=path[i][nz];
191  }
192 
193  if (traj)
194  CreateFileName(NULL,arg[1],"_traj",PLOT3D_EXT,&fpathplot);
195  else
196  CreateFileName(NULL,arg[1],"_path",PLOT3D_EXT,&fpathplot);
197  fprintf(stderr,"Generating path plot to : %s\n",GetFileFullName(&fpathplot));
198 
199  NewColor(1,0,0,&red);
200 
201  InitPlot3d(GetFileFullName(&fpathplot),TRUE,argc,arg,&p3d);
202  StartNew3dObject(&red,&p3d);
203  PlotVect3d(ns,x,y,z,&p3d);
204  Close3dObject(&p3d);
205  ClosePlot3d(FALSE,&p3d);
206 
207  DeleteColor(&red);
208 
209  DeleteFileName(&fpathplot);
210 
211  free(x);
212  free(y);
213  free(z);
214  DeleteTrajectory(ns,path,actions,times);
215  }
216 
217  /* Release memory */
218  free(s1); free(p1);
219  if (s2!=NULL) {free(s2);free(p2);}
220 
221  DeleteParameters(&parameters);
222 
223  free(systemVars);
224 
225  CS_WD_DELETE(&world);
226 
227  DeleteAtlasRRT(&atlasRRT);
228 
229  DeleteFileName(&fpath);
230  DeleteFileName(&fparam);
231  }
232  else
233  {
234  fprintf(stderr," Wrong number of parameters.\n");
235  fprintf(stderr," Use:\n");
236  fprintf(stderr," cuikplotatlasrrt <problem filename> <xID> <yID> <zID>\n");
237  fprintf(stderr," where <problem filename> is the atlas to plot.\n");
238  fprintf(stderr," <xID> <yID> <zID> are the 3 projection dimensions.\n");
239  fprintf(stderr," (indices of system variables as those in the _path.sol file from 0)\n");
240 
241  }
242  return(EXIT_SUCCESS);
243 }
244