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 fworld;
82  Tfilename fparam;
83 
84  Tplot3d pt;
85 
86  TCuikexploreControl status;
87  unsigned int i,rep;
88  double epsilon;
89 
90  unsigned int k,m,nit;
91  double r,e,ce;
92 
93  TJacobian J;
94 
95  GtkWidget *window_cuikexplore;
96 
97  Tchart chart,chartTmp;
98  Tbox ambient;
99  unsigned int *tp;
100  boolean hasS;
101  boolean collision;
102 
103  double *newPoint,*t,*initialPoint;
104 
105  status.fw=arg[1];
106 
107  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
108  fprintf(stderr,"Reading parameters from : %s\n",GetFileFullName(&fparam));
109  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
110 
111  rep=(unsigned int)(GetParameter(CT_REPRESENTATION,&parameters));
112  if (rep!=REP_JOINTS)
113  ChangeParameter(CT_REPRESENTATION,(double)REP_JOINTS,&parameters);
114 
115  nit=(unsigned int)(GetParameter(CT_MAX_NEWTON_ITERATIONS,&parameters));
116  if (nit==0)
118 
119  epsilon=GetParameter(CT_EPSILON,&parameters);
120  r=GetParameter(CT_R,&parameters);
121  e=GetParameter(CT_E,&parameters);
122  ce=GetParameter(CT_CE,&parameters);
123  k=(unsigned int)GetParameter(CT_N_DOF,&parameters);
124  status.k=k;
125 
126  /* space for parameters in conf. space */
127  NEW(t,k,double);
128 
129  /* Read the equation info from world */
130  CreateFileName(NULL,arg[1],NULL,WORLD_EXT,&fworld);
131  fprintf(stderr,"Reading world from : %s\n",GetFileFullName(&fworld));
132  NEW(worldBase,1,Tworld);
133  InitWorldFromFile(&parameters,&fworld,worldBase);
134  InitWorldCD(&parameters,1,worldBase);
135  CS_WD_FROM_WORLD(worldBase,&world);
136 
137  /* check that k is lower than 'm' (num variables of the problem)? */
138  m=CS_WD_GET_NUM_SYSTEM_VARS(&world);
139  if (k>=m)
140  Error("The mobility is larger than the number of variables?");
141  status.m=m;
142 
143  /* Space for the vector of displacements */
144  NEW(status.vect,m,double);
145  for(i=0;i<m;i++)
146  status.vect[i]=0;
147 
148  /* Read the start configuration */
149  ReadOneSample(&parameters,arg[1],m,&status.point);
150  NEW(newPoint,m,double); /* temporary space */
151  NEW(initialPoint,m,double); /* for the reset */
152  memcpy(initialPoint,status.point,m*sizeof(double));
153 
154  /* Initialize the chart */
155  CS_WD_GENERATE_SIMP_INITIAL_BOX(&parameters,&ambient,&world);
156  if (CS_WD_GET_SIMP_TOPOLOGY(&parameters,&tp,&world)!=m)
157  Error("Missmatch number of variables.");
158 
159  /* Search for a non-real variable */
160  hasS=FALSE;
161  i=0;
162  while((i<m)&&(!hasS))
163  {
164  hasS=(tp[i]==TOPOLOGY_S);
165  i++;
166  }
167  if (!hasS)
168  {
169  free(tp);
170  tp=NULL;
171  }
172  CS_WD_GET_SIMP_JACOBIAN(&parameters,&J,&world);
173 
174  if (InitChart(&parameters,TRUE,&ambient,tp,m,k,status.point,e,ce,r,
175  &J,&world,&chart)!=0)
176  Error("Could not define a chart in the start configuration.");
177 
178  /* indices of the variables used as a parameters */
179  NEW(status.ndx,k,unsigned int);
180  if (argc>1+k)
181  {
182  for(i=0;i<k;i++)
183  status.ndx[i]=atoi(arg[2+i]);
184  }
185  else
186  {
187  for(i=0;i<k;i++)
188  status.ndx[i]=i;
189  }
190 
191  status.changed=FALSE;
192  status.end=FALSE;
193  status.reset=FALSE;
194  status.step=0.01;
195 
196  status.p=&parameters;
197  status.w=&world;
198 
199  gtk_set_locale();
200  gtk_init(&argc,&arg);
201 
202  window_cuikexplore=create_window_cuikexplore((gpointer)(&status));
203  gtk_widget_show(window_cuikexplore);
204 
205  InitPlot3d(NULL,FALSE,argc,arg,&pt);
206  PlotWorld(&parameters,&pt,0.0,GET_WORLD(&world));
207  MoveWorldDOF(&parameters,&pt,status.point,GET_WORLD(&world));
208 
209  while(!status.end)
210  {
211  if (status.changed)
212  {
213  /* project status.vect on the current chart and generate a new chart in
214  the resulting direction.*/
215  for(i=0;i<m;i++)
216  newPoint[i]=status.point[i]+status.vect[i];
217  Manifold2Chart(newPoint,tp,t,&chart);
218  if (Norm(k,t)>(1e-2*status.step))
219  {
220  if (Chart2Manifold(&parameters,&J,t,tp,status.point,newPoint,&chart)<INF)
221  {
222  if (PointInBoxTopology(NULL,TRUE,m,newPoint,epsilon,tp,&ambient))
223  {
224  CS_WD_IN_COLLISION(collision,&parameters,newPoint,status.point,&world);
225  if (!collision)
226  {
227  CopyChart(&chartTmp,&chart);
228  DeleteChart(&chart);
229  if (InitChart(&parameters,TRUE,&ambient,tp,m,k,newPoint,e,ce,r,
230  &J,&world,&chart)==0)
231  {
232  memcpy(status.point,newPoint,m*sizeof(double));
233  MoveWorldDOF(&parameters,&pt,status.point,GET_WORLD(&world));
234  }
235  else
236  {
237  CopyChart(&chart,&chartTmp);
238  fprintf(stderr,"Error defining chart. Staying in previous point.");
239  }
240  DeleteChart(&chartTmp);
241  }
242  else
243  fprintf(stderr,"Collision! (Reduce step?)\n");
244  }
245  else
246  fprintf(stderr,"Out of the ambient space\n");
247  }
248  else
249  fprintf(stderr,"Error in parametrization. Change it?\n");
250  }
251  else
252  fprintf(stderr,"Singularity. Change the parametrization\n");
253 
254  /* Reset the displacement vector (if only one step must be executed) */
255  if (status.oneStep)
256  {
257  for(i=0;i<m;i++)
258  status.vect[i]=0;
259  status.changed=FALSE;
260  status.oneStep=FALSE;
261  }
262  else
263  usleep(10000);
264  }
265  if (status.reset)
266  {
267  memcpy(status.point,initialPoint,m*sizeof(double));
268  DeleteChart(&chart);
269  if (InitChart(&parameters,TRUE,&ambient,tp,m,k,status.point,e,ce,r,
270  &J,&world,&chart)!=0)
271  Error("Can not move to the initial point?");
272  MoveWorldDOF(&parameters,&pt,status.point,GET_WORLD(&world));
273  status.reset=FALSE;
274  }
275  gtk_main_iteration_do(FALSE);
276  }
277 
278  ClosePlot3d(TRUE,0,0,0,&pt);
279 
280  DeleteChart(&chart);
281  DeleteBox(&ambient);
282  if (tp!=NULL)
283  free(tp);
284  DeleteJacobian(&J);
285 
286  free(newPoint);
287  free(t);
288  free(initialPoint);
289 
290  free(status.point);
291  free(status.ndx);
292  free(status.vect);
293 
294  CS_WD_DELETE(&world);
295  DeleteParameters(&parameters);
296 
297  DeleteFileName(&fparam);
298  DeleteFileName(&fworld);
299  }
300  else
301  {
302  fprintf(stdout," Wrong number of parameters.\n");
303  fprintf(stdout," Use:\n");
304  fprintf(stdout," cuikexplore <world>.world [dof_1 ... dof_k]\n");
305  fprintf(stdout," Where:\n");
306  fprintf(stdout," <world>: File describing the problem.\n");
307  fprintf(stdout," [dof_1 ... dof_k] Index of the variables to be used\n");
308  fprintf(stdout," as parameters.\n");
309  fprintf(stdout," File extensions are not required\n");
310  }
311 
312  return(EXIT_SUCCESS);
313 }
#define CT_R
Ball radius in atlas.
Definition: parameters.h:257
void CopyChart(Tchart *c_dst, Tchart *c_src)
Copy constructor.
Definition: chart.c:833
#define GET_WORLD(wcs)
Returns the world from an TAtlasBase.
Definition: wcs.h:150
#define REP_JOINTS
One of the possible values of the REPRESENTATION parameter.
Definition: parameters.h:60
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:2514
#define CT_EPSILON
Numerical tolerance.
Definition: parameters.h:194
#define FALSE
FALSE.
Definition: boolean.h:30
#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:248
Definition of the TJacobian type and the associated functions.
CBLAS_INLINE double Norm(unsigned int s, double *v)
Computes the norm of a vector.
void InitWorldFromFile(Tparameters *p, Tfilename *f, Tworld *w)
Constructor.
void DeleteJacobian(TJacobian *j)
Destructor.
Definition: jacobian.c:249
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 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:1066
void Error(const char *s)
General error function.
Definition: error.c:80
#define PARAM_EXT
File extension for parameter files.
Definition: filename.h:131
All the necessary information to generate equations for mechanisms.
Definition: world.h:124
#define CS_WD_DELETE(wcs)
Destructor of the equation structure.
Definition: wcs.h:500
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 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:792
Tparameters * p
Definition: cuikexplore.h:36
Definition of the cuikexplore control structure.
Definitions of constants and macros used in several parts of the cuik library.
A table of parameters.
void CreateFileName(char *path, char *name, char *suffix, char *ext, Tfilename *fn)
Constructor.
Definition: filename.c:22
#define CS_WD_GET_SIMP_JACOBIAN(pr, J, wcs)
Computes the Jacobian of the simplified system.
Definition: wcs.h:474
void DeleteChart(Tchart *c)
Destructor.
Definition: chart.c:2011
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.
char * GetFileFullName(Tfilename *fn)
Gets the file full name (paht+name+extension).
Definition: filename.c:151
#define CT_REPRESENTATION
Representation.
Definition: parameters.h:215
A box.
Definition: box.h:83
TAtlasBase * w
Definition: cuikexplore.h:37
#define WORLD_EXT
File extension for problem files.
Definition: filename.h:161
void DeleteParameters(Tparameters *p)
Destructor.
Definition: parameters.c:295
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:2260
void DeleteBox(void *b)
Destructor.
Definition: box.c:1259
unsigned int * ndx
Definition: cuikexplore.h:30
The Jacobian of a set of equations.
Definition: jacobian.h:23
unsigned int m
Definition: cuikexplore.h:31
#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:1036
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 CS_WD_GET_NUM_SYSTEM_VARS(wcs)
Gets the number of system variables.
Definition: wcs.h:250
#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
#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
void InitWorldCD(Tparameters *pr, unsigned int mt, Tworld *w)
Initializes the collision detector.
Definition: world.c:1013
#define CS_WD_GENERATE_SIMP_INITIAL_BOX(pr, b, wcs)
Computes the global box for the simplified system.
Definition: wcs.h:460
void ChangeParameter(unsigned int n, double v, Tparameters *p)
Sets the value for a particular parameter.
Definition: parameters.c:164
#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:1216
#define CS_WD_GET_SIMP_TOPOLOGY(pr, tp, wcs)
Gets the simplified variable topology.
Definition: wcs.h:401
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:350
Definition of the Tinterval type and the associated functions.