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