cuiklinks2joints.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 "world.h"
6 
7 #include <stdlib.h>
8 
62 int main(int argc, char **arg)
63 {
64  if (argc>1)
65  {
66  Tparameters parameters;
67  Tworld world;
68 
69  Tfilename fworld;
70  Tfilename fparam;
71  Tfilename flinks;
72  Tfilename fjoints;
73 
74  unsigned int i,j;
75 
76  unsigned int ndof;
77  double *dof;
78  unsigned int nv,nvs;
79  boolean *sv;
80  double *sample;
81 
82  FILE *fd,*fs;
83 
84  boolean end;
85  int token;
86 
87  unsigned int r;
88 
89  /* Init parameters */
90  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
91  fprintf(stderr,"Reading parameters : %s\n",GetFileFullName(&fparam));
92  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
93 
94  r=(unsigned int)(GetParameter(CT_REPRESENTATION,&parameters));
95  if (r==REP_JOINTS)
96  Error("The system representation can not be JOINTS when converting to JOINTS");
97 
98  /* Read the problem file */
99  CreateFileName(NULL,arg[1],NULL,WORLD_EXT,&fworld);
100  fprintf(stderr,"Reading mechanism : %s\n",GetFileFullName(&fworld));
101  InitWorldFromFile(&parameters,&fworld,&world);
102 
103  /* Open the file from where to read the samples */
104  CreateFileName(NULL,arg[1],NULL,LINKS_EXT,&flinks);
105  fprintf(stderr,"Reading link poses fro : %s\n",GetFileFullName(&flinks));
106  fs=fopen(GetFileFullName(&flinks),"r");
107  if (!fs)
108  Error("Could not open the file to read the link poses");
109 
110  /* Open the file where to store the dof */
111  if (argc>2)
112  CreateFileName(NULL,arg[2],NULL,JOINTS_EXT,&fjoints);
113  else
114  CreateFileName(NULL,arg[1],NULL,JOINTS_EXT,&fjoints);
115  fprintf(stderr,"Writing dof to : %s\n",GetFileFullName(&fjoints));
116  fd=fopen(GetFileFullName(&fjoints),"w");
117  if (!fd)
118  Error("Could not open the file to store the dof");
119 
120  /* Allocate space for the sample and dof */
121  nv=GetWorldSystemVars(&sv,&world);
122  nvs=0;
123  for(i=0;i<nv;i++)
124  {
125  if (sv[i])
126  nvs++;
127  }
128  NEW(sample,nvs,double);
129 
130  ndof=GetWorldNDOF(&world);
131  NEW(dof,ndof,double);
132 
133  end=FALSE;
134  do {
135  for(i=0;((!end)&&(i<nvs));i++)
136  {
137  token=fscanf(fs,"%lf",&(sample[i]));
138  if ((token==EOF)||(token==0))
139  end=TRUE;
140  }
141  if (!end)
142  {
143  WorldSample2DOF(&parameters,sample,dof,&world);
144  for(j=0;j<ndof;j++)
145  fprintf(fd,"%.16f ",dof[j]);
146  fprintf(fd,"\n");
147  }
148  } while (!end);
149 
150  free(sv);
151  free(dof);
152  free(sample);
153 
154  fclose(fd);
155  fclose(fs);
156 
157  DeleteWorld(&world);
158  DeleteParameters(&parameters);
159 
160  DeleteFileName(&fworld);
161  DeleteFileName(&fparam);
162  DeleteFileName(&fjoints);
163  DeleteFileName(&flinks);
164  }
165  else
166  {
167  fprintf(stderr,"Use:\n");
168  fprintf(stderr," cuiklinks2joints <problem_name> <dof_name>\n");
169  fprintf(stderr," <problem_name> the prefix to define the .world and.\n");
170  fprintf(stderr," .links files.\n");
171  fprintf(stderr," <dof_name> [optional] The prefis to define the .dof.\n");
172  fprintf(stderr," file. If not given the <problem_name> is used.\n");
173  }
174 
175  return(EXIT_SUCCESS);
176 }
#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
void InitWorldFromFile(Tparameters *p, Tfilename *f, Tworld *w)
Constructor.
void WorldSample2DOF(Tparameters *p, double *sample, double *dof, Tworld *w)
Transforms a sample degrees of freedom.
Definition: world.c:2625
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
Definition of the Tbox type and the associated functions.
Definitions of constants and macros used in several parts of the cuik library.
int main(int argc, char **arg)
Main body of the cuiklinks2joints application.
A table of parameters.
void CreateFileName(char *path, char *name, char *suffix, char *ext, Tfilename *fn)
Constructor.
Definition: filename.c:22
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 CT_REPRESENTATION
Representation.
Definition: parameters.h:215
#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
double GetParameter(unsigned int n, Tparameters *p)
Gets the value for a particular parameter.
Definition: parameters.c:93
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