cuikexplore.c
Go to the documentation of this file.
1 #include "cuikexplore.h"
2 
3 #include "world.h"
4 #include "interval.h"
5 #include "chart.h"
6 #include "defines.h"
7 #include "jacobian.h"
8 #include "samples.h"
9 #include "error.h"
10 #include "algebra.h"
11 
12 #include "cuikexplore_interface.h"
13 #include "cuikplay_support.h"
14 
15 #include <gtk/gtk.h>
16 
17 #include <string.h>
18 #include <unistd.h>
19 #include <time.h>
20 
72 int main(int argc, char **arg)
73 {
74 
75  if (argc>1)
76  {
77  TAtlasBase world;
78  Tworld *worldBase;
79  Tparameters parameters;
80 
81  Tfilename fparam;
82 
83  Tplot3d pt;
84 
85  TCuikexploreControl status;
86  unsigned int i,j,me;
87  double epsilon,delta;
88 
89  unsigned int k,m,ms,d,nit;
90  double r,e,ce;
91 
92  TJacobian J;
93 
94  GtkWidget *window_cuikexplore;
95 
96  Tchart chart,chartTmp;
97  Tbox ambient;
98  unsigned int *tp;
99  boolean hasS;
100  boolean collide,in;
101 
102  Tinterval range;
103 
104  /* We differentiate between the internal representation of the equations
105  and the DOF-based representation. They are not necessarily the same. */
106  double *newPointDOF;
107  double *simpPoint;
108  double *t;
109  double *initialPoint,*initialPointDOF,*initialSimpPoint;
110 
111  boolean ndxOK;
112  boolean outside,outsideInit;
113  boolean collision,collisionInit;
114  double *pointWithDummies;
115 
116  status.fw=arg[1];
117 
118  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
119  fprintf(stderr,"Reading parameters from : %s\n",GetFileFullName(&fparam));
120  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
121 
122  nit=(unsigned int)(GetParameter(CT_MAX_NEWTON_ITERATIONS,&parameters));
123  if (nit==0)
125 
126  /* Read the equation info from world */
127  NEW(worldBase,1,Tworld);
128  InitWorldFromFile(&parameters,TRUE,arg[1],worldBase);
129  InitWorldCD(&parameters,1,worldBase);
130  CS_WD_FROM_WORLD(worldBase,&world); /* Necessary to generate charts */
131 
132  /* Number of system variables */
133  m=GetWorldNumSystemVariables(worldBase);
134  status.m=m;
135 
136  /* Read the initial point */
137  ReadOneSample(&parameters,arg[1],m,&status.point);
138 
139  /* anotate the underlying representation */
140  status.rep=(unsigned int)(GetParameter(CT_REPRESENTATION,&parameters));
141 
142  epsilon=GetParameter(CT_EPSILON,&parameters);
143  delta=GetParameter(CT_DELTA,&parameters);
144  r=GetParameter(CT_R,&parameters);
145  e=GetParameter(CT_E,&parameters);
146  ce=GetParameter(CT_CE,&parameters);
147  k=(unsigned int)GetParameter(CT_N_DOF,&parameters); /* DOF of the manifold */
148  if (k==0)
149  k=WorldManifoldDimension(&parameters,status.point,worldBase);
150  if (k>=m)
151  Error("The mobility is larger than the number of variables?");
152  status.k=k;
153 
154  /* space for projections to the charts */
155  NEW(t,k,double);
156 
157  /* number of degrees of freedom */
158  d=GetWorldNDOF(worldBase);
159  if (k>d) /* k can be equal to d for non-constrained system */
160  Error("The mobility is larger than the number of dof?");
161  status.d=d;
162 
163  /* number of simplified variables and topology */
164  ms=GetWorldSimpTopology(&parameters,&tp,worldBase);
165 
166  /* Space for the vector of displacements in DOF space */
167  NEW(status.vectDOF,d,double);
168  NEW(status.e2d,d,unsigned int); /* menu entry to dof */
169  NEW(status.d2e,d,unsigned int) /* dof to meny entry */
170  me=0;
171  for(i=0;i<d;i++)
172  {
173  status.vectDOF[i]=0;
174  GetWorldRangeDOF(i,&range,worldBase);
175  if (IntervalSize(&range)<ZERO)
176  {
177  /* dof not to be shown in the menu */
178  status.d2e[i]=NO_UINT;
179  }
180  else
181  {
182  status.d2e[i]=me;
183  status.e2d[me]=i;
184  me++;
185  }
186  DeleteInterval(&range);
187  }
188  if (me<k)
189  Error("Insuficient non-fixed degrees of freedom");
190 
191  /* simplify the initial point */
192  RegenerateWorldSolutionPoint(&parameters,status.point,&pointWithDummies,worldBase);
193  WorldGenerateSimplifiedPoint(&parameters,pointWithDummies,&(status.simpPoint),worldBase);
194  free(pointWithDummies);
195  /* convert the start configuration to DOF */
196  NEW(status.pointDOF,d,double);
197  WorldSample2DOF(&parameters,status.point,status.pointDOF,worldBase);
198 
199  /* Temporary space for the new points/dofs */
200  NEW(newPointDOF,d,double);
201 
202  /* Copy of the initial point/DOF used in the reset */
203  NEW(initialPointDOF,d,double);
204  memcpy(initialPointDOF,status.pointDOF,d*sizeof(double));
205  NEW(initialPoint,m,double);
206  memcpy(initialPoint,status.point,m*sizeof(double));
207  NEW(initialSimpPoint,ms,double);
208  memcpy(initialSimpPoint,status.simpPoint,ms*sizeof(double));
209 
210  /* Initialize the chart */
211  GetWorldSimpInitialBox(&parameters,&ambient,worldBase);
212 
213  /* Search for a non-real variable */
214  hasS=FALSE;
215  i=0;
216  while((i<ms)&&(!hasS))
217  {
218  hasS=(tp[i]==TOPOLOGY_S);
219  i++;
220  }
221  if (!hasS)
222  {
223  free(tp);
224  tp=NULL;
225  }
226  GetWorldSimpJacobian(&parameters,&J,worldBase);
227 
228  if (InitChart(&parameters,TRUE,&ambient,tp,ms,k,status.simpPoint,e,ce,r,
229  &J,&world,&chart)!=0)
230  Error("Could not define a chart in the start configuration.");
231 
232  /* It can be the case that the initial point is out of the domain or in collision
233  In this case, we initially ignore the inDocmain and inCollision checks.
234  When we finally find a inDomain and collision-free configuration, these
235  checks are activated again. */
236  outside=(!PointInBoxTopology(NULL,TRUE,ms,status.simpPoint,epsilon,tp,&ambient));
237  outsideInit=outside;
238  if (outside)
239  fprintf(stderr,"Initial point is out of domain\n");
240 
241  CS_WD_ALL_COLLISIONS(collision,&parameters,status.simpPoint,NULL,&world);
242  collisionInit=collision;
243  if (collision)
244  fprintf(stderr,"Initial point is in collision\n");
245 
246  /* indices of the dofs used as a parameters */
247  NEW(status.ndx,k,unsigned int);
248  ndxOK=TRUE;
249  if (argc>1+k)
250  {
251  for(i=0;((ndxOK)&&(i<k));i++)
252  {
253  status.ndx[i]=atoi(arg[2+i]);
254  ndxOK=(status.d2e[status.ndx[i]]!=NO_UINT);
255  }
256  }
257  if ((!ndxOK)||(argc<=1+k))
258  {
259  /* just select the first 'k' non-fixed dof */
260  j=0;
261  i=0;
262  while((i<status.d)&&(j<k))
263  {
264  if (status.d2e[i]!=NO_UINT)
265  {
266  status.ndx[j]=i;
267  j++;
268  }
269  i++;
270  }
271  if (i<k)
272  Error("Can not select enough input parameters");
273  }
274 
275  status.changed=FALSE;
276  status.end=FALSE;
277  status.reset=FALSE;
278  status.step=delta; /* default value is 0.05 */
279 
280  status.p=&parameters;
281  status.w=&world;
282 
283  gtk_set_locale();
284  gtk_init(&argc,&arg);
285 
286  window_cuikexplore=create_window_cuikexplore((gpointer)(&status));
287  gtk_widget_show(window_cuikexplore);
288 
289  InitPlot3d(NULL,FALSE,argc,arg,&pt);
290  PlotWorld(&parameters,&pt,0.0,worldBase);
291  MoveWorldDOF(&parameters,&pt,status.pointDOF,worldBase);
292 
293  while(!status.end)
294  {
295  if (status.changed)
296  {
297  /* Change the DOF */
298  for(i=0;i<d;i++)
299  newPointDOF[i]=status.pointDOF[i]+status.vectDOF[i];
300  /* Generate the solution point form the DOF. The result is not a solution
301  of the system of equations */
302  WorldDOF2Sol(&parameters,newPointDOF,&pointWithDummies,NULL,worldBase);
303  /* and simplify the solution point (even if 'pointWithDummies' is not a solution) */
304  WorldGenerateSimplifiedPoint(&parameters,pointWithDummies,&simpPoint,worldBase);
305  free(pointWithDummies);
306 
307  /* Verify that we are not moving orthogonaly to the tangent space */
308  Manifold2Chart(simpPoint,tp,t,&chart);
309  if (Norm(k,t)>(1e-3*status.step))
310  {
311  /* Enforce the equations */
312  if (Chart2Manifold(&parameters,&J,t,tp,NULL,simpPoint,&chart)<INF)
313  {
314  /* Check that the solution point is in domain. Only after we are inside the
315  domain for the first time */
316  in=PointInBoxTopology(NULL,TRUE,ms,simpPoint,epsilon,tp,&ambient);
317  if ((outside)||(in))
318  {
319  /* and not in collision. Collision are not considered, thouth, while ouside
320  the domain */
321  CS_WD_ALL_COLLISIONS(collide,&parameters,simpPoint,status.simpPoint,&world);
322  if ((collision)||(!collide))
323  {
324  /* here we have a new valid solution point where it should be possible
325  to generate a new chart */
326  CopyChart(&chartTmp,&chart);
327  DeleteChart(&chart);
328 
329  if (InitChart(&parameters,TRUE,&ambient,tp,ms,k,simpPoint,e,ce,r,
330  &J,&world,&chart)==0)
331  {
332  memcpy(status.simpPoint,simpPoint,sizeof(double)*ms);
333  free(status.point);
334  /* chart2manifold changed the simpPoint -> regenerate the original form it */
335  RegenerateWorldOriginalSystemPoint(&parameters,simpPoint,&(status.point),worldBase);
336  /* and the DOF from the original */
337  WorldSample2DOF(&parameters,status.point,status.pointDOF,worldBase);
338  /* and plot using DOF */
339  MoveWorldDOF(&parameters,&pt,status.pointDOF,worldBase);
340 
341  if (outside)
342  {
343  /* Check if whe already reached the valid domain */
344  outside=(!in);
345  if (!outside)
346  fprintf(stderr,"Entering the domain-----------------------------------------------------------\n"); /* outside flag will remain to false from now on */
347  }
348  if (collision)
349  {
350  collision=collide;
351  if (!collision)
352  fprintf(stderr,"Entering C-free -----------------------------------------------------------\n"); /* outside flag will remain to false from now on */
353  }
354  }
355  else
356  {
357  CopyChart(&chart,&chartTmp);
358  fprintf(stderr,"Error defining chart. Staying in previous point.\n");
359  }
360  DeleteChart(&chartTmp);
361  }
362  else
363  fprintf(stderr,"Collision\n");
364  }
365  else
366  fprintf(stderr,"Out of the ambient space\n");
367  }
368  else
369  fprintf(stderr,"Error in parametrization. Change it?\n");
370  }
371  else
372  fprintf(stderr,"Singularity (%f). Change the parametrization?\n",Norm(k,t));
373 
374  free(simpPoint);
375 
376  /* Reset the displacement vector (if only one step must be executed) */
377  if (status.oneStep)
378  {
379  for(i=0;i<d;i++)
380  status.vectDOF[i]=0;
381  status.changed=FALSE;
382  status.oneStep=FALSE;
383  }
384  else
385  usleep(10000);
386  }
387  if (status.reset)
388  {
389  memcpy(status.pointDOF,initialPointDOF,d*sizeof(double));
390  memcpy(status.point,initialPoint,m*sizeof(double));
391  memcpy(status.simpPoint,initialSimpPoint,ms*sizeof(double));
392  DeleteChart(&chart);
393  if (InitChart(&parameters,TRUE,&ambient,tp,ms,k,status.simpPoint,e,ce,r,
394  &J,&world,&chart)!=0)
395  Error("Can not move to the initial point?");
396 
397  MoveWorldDOF(&parameters,&pt,status.pointDOF,worldBase);
398  status.reset=FALSE;
399  outside=outsideInit;
400  collision=collisionInit;
401  }
402  gtk_main_iteration_do(FALSE);
403  }
404 
405  ClosePlot3d(TRUE,0,0,0,&pt);
406 
407  DeleteChart(&chart);
408  DeleteBox(&ambient);
409  if (tp!=NULL)
410  free(tp);
411  DeleteJacobian(&J);
412 
413  free(newPointDOF);
414  free(t);
415  free(initialPointDOF);
416  free(initialPoint);
417  free(initialSimpPoint);
418 
419  free(status.pointDOF);
420  free(status.point);
421  free(status.simpPoint);
422  free(status.ndx);
423  free(status.vectDOF);
424  free(status.d2e);
425  free(status.e2d);
426 
427  CS_WD_DELETE(&world);
428  DeleteParameters(&parameters);
429 
430  DeleteFileName(&fparam);
431  }
432  else
433  {
434  fprintf(stdout," Wrong number of parameters.\n");
435  fprintf(stdout," Use:\n");
436  fprintf(stdout," cuikexplore <world>.world [dof_1 ... dof_k]\n");
437  fprintf(stdout," Where:\n");
438  fprintf(stdout," <world>: File describing the problem.\n");
439  fprintf(stdout," [dof_1 ... dof_k] Index of the variables to be used\n");
440  fprintf(stdout," as parameters.\n");
441  fprintf(stdout," File extensions are not required\n");
442  }
443 
444  return(EXIT_SUCCESS);
445 }
#define CT_R
Ball radius in atlas.
Definition: parameters.h:257
unsigned int RegenerateWorldOriginalSystemPoint(Tparameters *p, double *s, double **o, Tworld *w)
Reconstruct a point in original kinematic system from a simplified point.
Definition: world.c:3334
unsigned int RegenerateWorldSolutionPoint(Tparameters *pr, double *p, double **v, Tworld *w)
Computes the missing values in a kinematic solution.
Definition: world.c:3468
void CopyChart(Tchart *c_dst, Tchart *c_src)
Copy constructor.
Definition: chart.c:836
void MoveWorldDOF(Tparameters *pr, Tplot3d *pt, double *dof, Tworld *w)
Moves a mechanisms to a configuration given by the degrees of freedom.
Definition: world.c:3680
#define CT_EPSILON
Numerical tolerance.
Definition: parameters.h:194
#define FALSE
FALSE.
Definition: boolean.h:30
void GetWorldRangeDOF(unsigned int ndof, Tinterval *r, Tworld *w)
Gets the range for a given degree of freedom.
Definition: world.c:2002
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
Data structure to hold the information about the name of a file.
Definition: filename.h:271
unsigned int WorldDOF2Sol(Tparameters *p, double *dof, double **sol, Tbox *b, Tworld *w)
Transforms from degrees of freedom to cuik variables.
Definition: world.c:3708
Definition of the TJacobian type and the associated functions.
unsigned int * e2d
Definition: cuikexplore.h:31
CBLAS_INLINE double Norm(unsigned int s, double *v)
Computes the norm of a vector.
void DeleteJacobian(TJacobian *j)
Destructor.
Definition: jacobian.c:255
void WorldSample2DOF(Tparameters *p, double *sample, double *dof, Tworld *w)
Transforms a sample degrees of freedom.
Definition: world.c:3800
void InitPlot3d(char *name, boolean axes, int argc, char **arg, Tplot3d *p)
Constructor.
Definition: plot3d.c:41
#define TRUE
TRUE.
Definition: boolean.h:21
unsigned int WorldManifoldDimension(Tparameters *p, double *point, Tworld *w)
Dimensionality of the solution manifold.+.
Definition: world.c:3404
unsigned int k
Definition: cuikexplore.h:29
double Chart2Manifold(Tparameters *pr, TJacobian *sJ, double *t, unsigned int *tp, double *pInit, double *p, Tchart *c)
Returns the point in the manifold for a given set of parameteres.
Definition: chart.c:1069
void Error(const char *s)
General error function.
Definition: error.c:80
unsigned int GetWorldNumSystemVariables(Tworld *w)
Number of system variables in the kinematic subsystem.
Definition: world.c:3297
#define PARAM_EXT
File extension for parameter files.
Definition: filename.h:132
All the necessary information to generate equations for mechanisms.
Definition: world.h:229
#define CS_WD_DELETE(wcs)
Destructor of the equation structure.
Definition: wcs.h:592
#define ZERO
Floating point operations giving a value below this constant (in absolute value) are considered 0...
Definition: defines.h:37
A chart on a manifold.
Definition: chart.h:70
Definition of the Tworld type and the associated functions.
Error and warning functions.
void DeleteFileName(Tfilename *fn)
Destructor.
Definition: filename.c:205
A 3D plot.
Definition: plot3d.h:54
unsigned int d
Definition: cuikexplore.h:34
unsigned int InitChart(Tparameters *pr, boolean simple, Tbox *domain, unsigned int *tp, unsigned int m, unsigned int k, double *p, double e, double eCurv, double r, TJacobian *sJ, TAtlasBase *w, Tchart *c)
Constructor.
Definition: chart.c:795
Tparameters * p
Definition: cuikexplore.h:43
Definition of the cuikexplore control structure.
Definitions of constants and macros used in several parts of the cuik library.
void DeleteInterval(Tinterval *i)
Destructor.
Definition: interval.c:1021
unsigned int GetWorldSimpTopology(Tparameters *p, unsigned int **t, Tworld *w)
Gets the topology of the variables.
Definition: world.c:3153
A table of parameters.
void CreateFileName(char *path, char *name, char *suffix, char *ext, Tfilename *fn)
Constructor.
Definition: filename.c:22
void DeleteChart(Tchart *c)
Destructor.
Definition: chart.c:2014
void InitParametersFromFile(char *file, Tparameters *p)
Constructor from a file.
Definition: parameters.c:51
Type defining the equations on which the atlas is defined.
Definition: wcs.h:30
Definition of a local chart on a manifold.
#define CS_WD_ALL_COLLISIONS(f, pr, s, sPrev, wcs)
Checks if a configuration is in collision.
Definition: wcs.h:373
char * GetFileFullName(Tfilename *fn)
Gets the file full name (paht+name+extension).
Definition: filename.c:151
#define CT_REPRESENTATION
Representation.
Definition: parameters.h:215
boolean InitWorldFromFile(Tparameters *p, boolean error, char *fn, Tworld *w)
Constructor.
A box.
Definition: box.h:83
TAtlasBase * w
Definition: cuikexplore.h:44
unsigned int * d2e
Definition: cuikexplore.h:33
#define NO_UINT
Used to denote an identifier that has not been initialized.
Definition: defines.h:435
void DeleteParameters(Tparameters *p)
Destructor.
Definition: parameters.c:294
int main(int argc, char **arg)
Main body of the cuikexplore application.
Definition: cuikexplore.c:72
void PlotWorld(Tparameters *pr, Tplot3d *pt, double axesLength, Tworld *w)
Adds a world (environment plus mechanism) in a 3D scene.
Definition: world.c:3452
void DeleteBox(void *b)
Destructor.
Definition: box.c:1283
unsigned int * ndx
Definition: cuikexplore.h:30
The Jacobian of a set of equations.
Definition: jacobian.h:23
unsigned int m
Definition: cuikexplore.h:35
#define CT_MAX_NEWTON_ITERATIONS
Maximum number of iterations in the Newton-Raphson function.
Definition: parameters.h:311
#define CT_E
Chart error in atlas.
Definition: parameters.h:243
double Manifold2Chart(double *p, unsigned int *tp, double *t, Tchart *c)
Returns the parametrization of a point.
Definition: chart.c:1039
double GetParameter(unsigned int n, Tparameters *p)
Gets the value for a particular parameter.
Definition: parameters.c:93
Definition of the cuikexplore control structure.
Definition: cuikexplore.h:23
#define INF
Infinite.
Definition: defines.h:70
void GetWorldSimpJacobian(Tparameters *p, TJacobian *J, Tworld *w)
Gets the simplified kinematic Jacobian.
Definition: world.c:3145
unsigned int WorldGenerateSimplifiedPoint(Tparameters *p, double *o, double **s, Tworld *w)
Reconstruct a point in simplified kinematic system from an original point.
Definition: world.c:3362
#define CT_DELTA
Size of the steps in the path connecting two configurations.
Definition: parameters.h:282
unsigned int rep
Definition: cuikexplore.h:36
#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 CS_WD_FROM_WORLD(ptr, wcs)
Initializes the equations from a world structure.
Definition: wcs.h:108
unsigned int GetWorldNDOF(Tworld *w)
Gets the number of degrees of freedom in the world.
Definition: world.c:1997
void InitWorldCD(Tparameters *pr, unsigned int mt, Tworld *w)
Initializes the collision detector.
Definition: world.c:1030
Defines a interval.
Definition: interval.h:33
void ChangeParameter(unsigned int n, double v, Tparameters *p)
Sets the value for a particular parameter.
Definition: parameters.c:164
double IntervalSize(Tinterval *i)
Gets the uppser limit.
Definition: interval.c:96
void GetWorldSimpInitialBox(Tparameters *p, Tbox *b, Tworld *w)
Gets the kinematic simplified search space for a given problem.
Definition: world.c:3396
#define CT_CE
Chart error in atlas.
Definition: parameters.h:250
unsigned int ReadOneSample(Tparameters *p, char *fname, unsigned int nvs, double **s)
Reads one sample from a file.
Definition: samples.c:2772
Headers of the GTK interface functions for cuikexplore.
void ClosePlot3d(boolean quit, double average_x, double average_y, double average_z, Tplot3d *p)
Destructor.
Definition: plot3d.c:473
Headers of the GTK support functions for cuikplay.
#define TOPOLOGY_S
One of the possible topologies.
Definition: defines.h:139
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:374
Definition of the Tinterval type and the associated functions.