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,FALSE,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 }