cuikjoints2links.c
Go to the documentation of this file.
1 #include "box.h"
2 #include "random.h"
3 #include "defines.h"
4 #include "filename.h"
5 #include "geom.h"
6 #include "world.h"
7 
8 #include <stdlib.h>
9 
78 int main(int argc, char **arg)
79 {
80  if (argc>1)
81  {
82  Tparameters parameters;
83  Tworld world;
84 
85  Tfilename fworld;
86  Tfilename fparam;
87  Tfilename flinks;
88  Tfilename fboxes;
89  Tfilename fjoints;
90  Tfilename fvmask;
91  Tfilename fvname;
92 
93  unsigned int r;
94  unsigned int i,j;
95 
96  unsigned int ndof;
97  double *dof;
98  unsigned int nv;
99  boolean *sv,*mask;
100  double *sample;
101  Tbox bsample;
102  char **varNames;
103 
104  FILE *fd,*fs,*fb;
105 
106  boolean end;
107  int token;
108 
109  char **vn;
110 
111  /* Init parameters */
112  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
113  fprintf(stderr,"Reading parameters : %s\n",GetFileFullName(&fparam));
114  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
115  r=(unsigned int)(GetParameter(CT_REPRESENTATION,&parameters));
116 
117  if (r==REP_JOINTS)
118  Error("Please, set a REPRESENTATION different from JOINTS in the parameter file");
119 
120  /* Read the problem file */
121  CreateFileName(NULL,arg[1],NULL,WORLD_EXT,&fworld);
122  fprintf(stderr,"Reading mechanism : %s\n",GetFileFullName(&fworld));
123  InitWorldFromFile(&parameters,&fworld,&world);
124 
125  /* Open the the dof file */
126  if (argc>2)
127  CreateFileName(NULL,arg[2],NULL,JOINTS_EXT,&fjoints);
128  else
129  CreateFileName(NULL,arg[1],NULL,JOINTS_EXT,&fjoints);
130  fprintf(stderr,"Reading dof from : %s\n",GetFileFullName(&fjoints));
131  fd=fopen(GetFileFullName(&fjoints),"r");
132  if (!fd)
133  Error("Could not open the file with the dof");
134 
135  /* Open the file to store the link poses */
136  if (argc>2)
137  CreateFileName(NULL,arg[2],NULL,LINKS_EXT,&flinks);
138  else
139  CreateFileName(NULL,arg[1],NULL,LINKS_EXT,&flinks);
140  fprintf(stderr,"Writing link poses to : %s\n",GetFileFullName(&flinks));
141  fs=fopen(GetFileFullName(&flinks),"w");
142  if (!fs)
143  Error("Could not open the file to store the link poses");
144 
145  if (argc>2)
146  CreateFileName(NULL,arg[2],NULL,SOL_EXT,&fboxes);
147  else
148  CreateFileName(NULL,arg[1],NULL,SOL_EXT,&fboxes);
149  fprintf(stderr,"Writing boxes to : %s\n",GetFileFullName(&fboxes));
150  fb=fopen(GetFileFullName(&fboxes),"w");
151  if (!fb)
152  Error("Could not open the file to store the boxes");
153 
154  /* Allocate space to read the dof */
155  nv=GetWorldSystemVars(&sv,&world);
156 
157  ndof=GetWorldNDOF(&world);
158  NEW(dof,ndof,double);
159 
160  end=FALSE;
161  do {
162  for(i=0;((!end)&&(i<ndof));i++)
163  {
164  token=fscanf(fd,"%lf",&(dof[i]));
165  if ((token==EOF)||(token==0))
166  end=TRUE;
167  }
168  if (!end)
169  {
170  if (WorldDOF2Sol(&parameters,dof,&sample,&bsample,&world)!=nv)
171  Error("Missmatch variable number in cuikjoints2links");
172  for(j=0;j<nv;j++)
173  {
174  if (sv[j])
175  { PrintReal(fs,sample[j]);fprintf(fs," "); }
176  }
177  fprintf(fs,"\n");
178  free(sample);
179  NEW(vn,nv,char *);
180  GetWorldVarNames(vn,&world);
181  PrintBoxSubset(fb,sv,vn,&bsample);
182  free(vn);
183  DeleteBox(&bsample);
184  }
185  } while (!end);
186 
187  fclose(fd);
188  fclose(fs);
189  fclose(fb);
190 
191  free(dof);
192 
193  /* Now write a file with the array of booleans indicating the good
194  variables: the ones refering to link poses that are keept in
195  the simplified cuiksystem. This is very adhoc for generating
196  eigengrasps.... We dot it that way to have an automatic
197  way to generate the eigengrasp without resorting to a manual
198  procedure prone to errors. */
199 
200  nv=GetWorldSimpVariableMask(&parameters,&mask,&world);
201 
202  if (argc>2)
203  CreateFileName(NULL,arg[2],NULL,"vname",&fvname);
204  else
205  CreateFileName(NULL,arg[1],NULL,"vname",&fvname);
206  fprintf(stderr,"Writting variable names to : %s\n",GetFileFullName(&fvname));
207  fs=fopen(GetFileFullName(&fvname),"w");
208  if (!fs)
209  Error("Could not open the file to store the variable names");
210 
211  NEW(varNames,nv,char*);
212  GetWorldVarNames(varNames,&world);
213 
214  for(j=0;j<nv;j++)
215  {
216  if (sv[j])
217  {
218  PRINT_VARIABLE_NAME(fs,varNames[j]);
219  fprintf(fs,"\n");
220  }
221  }
222  fclose(fs);
223  free(varNames);
224 
225 
226  if (argc>2)
227  CreateFileName(NULL,arg[2],NULL,"vmask",&fvmask);
228  else
229  CreateFileName(NULL,arg[1],NULL,"vmask",&fvmask);
230  fprintf(stderr,"Writting variable mask to : %s\n",GetFileFullName(&fvmask));
231  fs=fopen(GetFileFullName(&fvmask),"w");
232  if (!fs)
233  Error("Could not open the file to store the variable mask");
234 
235  for(j=0;j<nv;j++)
236  {
237  if (sv[j])
238  {
239  if (mask[j])
240  fprintf(fs,"1 ");
241  else
242  fprintf(fs,"0 ");
243  }
244  }
245  fprintf(fs,"\n");
246 
247  free(sv);
248  free(mask);
249  fclose(fs);
250 
251  DeleteWorld(&world);
252  DeleteParameters(&parameters);
253 
254  DeleteFileName(&fworld);
255  DeleteFileName(&fparam);
256  DeleteFileName(&fjoints);
257  DeleteFileName(&flinks);
258  DeleteFileName(&fboxes);
259  DeleteFileName(&fvmask);
260  DeleteFileName(&fvname);
261  }
262  else
263  {
264  fprintf(stderr,"Use:\n");
265  fprintf(stderr," cuikjoints2links <problem_name> <output_name>\n");
266  fprintf(stderr," <problem_name> the file with the .world description.\n");
267  fprintf(stderr," <output_name> [optional] Prefix for the .joints file and to generate the .links.\n");
268  fprintf(stderr," .sol, .vname, and .vmask files. If not given the <problem_name> is used.\n");
269  }
270 
271  return(EXIT_SUCCESS);
272 }
Definition of basic functions.
#define REP_JOINTS
One of the possible values of the REPRESENTATION parameter.
Definition: parameters.h:60
#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
unsigned int WorldDOF2Sol(Tparameters *p, double *dof, double **sol, Tbox *b, Tworld *w)
Transforms from degrees of freedom to cuik variables.
Definition: world.c:2543
void InitWorldFromFile(Tparameters *p, Tfilename *f, Tworld *w)
Constructor.
void GetWorldVarNames(char **vn, Tworld *w)
Return the variable names.
Definition: world.c:1842
Definition of the Tfilename type and the associated functions.
#define TRUE
TRUE.
Definition: boolean.h:21
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
Definition of the Tworld type and the associated functions.
void DeleteWorld(Tworld *w)
Destructor.
Definition: world.c:2792
void DeleteFileName(Tfilename *fn)
Destructor.
Definition: filename.c:205
#define PRINT_VARIABLE_NAME(f, name)
Prints a variable name into a file.
Definition: defines.h:427
Definition of the Tbox type and the associated functions.
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
void PrintReal(FILE *f, double r)
Pretty print a real number.
Definition: geom.c:736
void InitParametersFromFile(char *file, Tparameters *p)
Constructor from a file.
Definition: parameters.c:51
char * GetFileFullName(Tfilename *fn)
Gets the file full name (paht+name+extension).
Definition: filename.c:151
#define SOL_EXT
File extension for solution files.
Definition: filename.h:137
#define CT_REPRESENTATION
Representation.
Definition: parameters.h:215
A box.
Definition: box.h:83
#define WORLD_EXT
File extension for problem files.
Definition: filename.h:161
#define JOINTS_EXT
File extension for files with samples represented by the joint values.
Definition: filename.h:187
void DeleteParameters(Tparameters *p)
Destructor.
Definition: parameters.c:295
unsigned int GetWorldSystemVars(boolean **sv, Tworld *w)
Gets the system vars of the kinematic cuiksystem.
Definition: world.c:1810
void DeleteBox(void *b)
Destructor.
Definition: box.c:1259
void PrintBoxSubset(FILE *f, boolean *used, char **varNames, Tbox *b)
Prints a (sub-)box.
Definition: box.c:1138
double GetParameter(unsigned int n, Tparameters *p)
Gets the value for a particular parameter.
Definition: parameters.c:93
unsigned int GetWorldSimpVariableMask(Tparameters *p, boolean **sv, Tworld *w)
Identifies pose related variable that survied in the simplified system.
Definition: world.c:1823
Definition of basic randomization functions.
unsigned int GetWorldNDOF(Tworld *w)
Gets the number of degrees of freedom in the world.
Definition: world.c:1559
#define LINKS_EXT
File extension for files with samples represented by the link poses.
Definition: filename.h:180