cuikcollisions.c
Go to the documentation of this file.
1 #include "world.h"
2 #include "error.h"
3 
4 
53 int main(int argc, char **arg)
54 {
55 
56  if (argc>2)
57  {
58  Tworld world;
59  Tparameters parameters;
60 
61  Tbox box;
62  int token;
63  double *s,*sfull;
64  boolean collision;
65 
66  Tfilename fcollisions;
67  Tfilename fsols;
68  Tfilename fparam;
69  Tfilename fworld;
70 
71  unsigned int total,good;
72 
73  FILE *fin,*fout;
74 
75  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
76  fprintf(stderr,"Reading parameter file : %s\n",GetFileFullName(&fparam));
77  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
78 
79  CreateFileName(NULL,arg[1],NULL,WORLD_EXT,&fworld);
80  fprintf(stderr,"Reading world file : %s\n",GetFileFullName(&fworld));
81  InitWorldFromFile(&parameters,&fworld,&world);
82 
83  CreateFileName(NULL,arg[2],NULL,SOL_EXT,&fsols);
84  fprintf(stderr,"Reading solution file : %s\n",GetFileFullName(&fsols));
85  fin=fopen(GetFileFullName(&fsols),"r");
86  if (!fin)
87  Error("The file with the solution boxes can not be opened");
88 
89  CreateFileName(NULL,arg[2],"_collision",COST_EXT,&fcollisions);
90  fprintf(stderr,"Writing collisions file : %s\n",GetFileFullName(&fcollisions));
91 
92  fout=fopen(GetFileFullName(&fcollisions),"w");
93  if (!fout)
94  Error("The file for the collision information can not be opened");
95 
96  /* Init the collision detection */
97  InitWorldCD(&parameters,FALSE,&world);
98  /* Check all the given solutions for possible collsion */
99  total=0;
100  good=0;
101  do {
102  token=ReadBox(fin,&box);
103  if (token!=EOF)
104  {
105  /* Get the center of the box as a solution point. */
106  NEW(s,GetBoxNIntervals(&box),double);
107  GetBoxCenter(NULL,s,&box);
108 
109  /* The solutions are stored using only the system variables.
110  Before checking for collision we have to regenerate the
111  solution and create values for the non-sytem variables */
112  RegenerateWorldSolutionPoint(&parameters,s,&sfull,&world);
113 
114  /* Check for collision and store the result */
115  collision=MoveAndCheckCD(&parameters,FALSE,0,sfull,NULL,&world);
116  if (collision)
117  fprintf(fout,"1\n");
118  else
119  {
120  fprintf(fout,"0.1\n");
121  good++;
122  }
123 
124  /* Release the memory used for this solution. */
125  free(sfull);
126  free(s);
127  DeleteBox(&box);
128  total++;
129  }
130  } while(token!=EOF);
131 
132  fprintf(stderr,"Collision-free ratio : %g\n",(double)good/(double)total);
133 
134  fclose(fin);
135  fclose(fout);
136 
137  DeleteFileName(&fcollisions);
138  DeleteFileName(&fsols);
139  DeleteFileName(&fworld);
140  DeleteFileName(&fparam);
141 
142  DeleteWorld(&world);
143  DeleteParameters(&parameters);
144  }
145  else
146  {
147  fprintf(stdout," Wrong number of parameters.\n");
148  fprintf(stdout," Use:\n");
149  fprintf(stdout," cuikcollisions <world>.world <solutions>.sol\n");
150  fprintf(stdout," Where:\n");
151  fprintf(stdout," <world>: File describing the problem\n");
152  fprintf(stdout," <solutions>: Solutions for which to extract the axes.\n");
153  fprintf(stdout," File extensions are not required\n");
154  }
155 
156  return(EXIT_SUCCESS);
157 }
unsigned int RegenerateWorldSolutionPoint(Tparameters *pr, double *p, double **v, Tworld *w)
Computes the missing values in a kinematic solution.
Definition: world.c:2276
#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
boolean MoveAndCheckCD(Tparameters *p, boolean all, unsigned int tID, double *sol, double *solPrev, Tworld *w)
Checks a point for collision.
Definition: world.c:1076
void InitWorldFromFile(Tparameters *p, Tfilename *f, Tworld *w)
Constructor.
#define COST_EXT
File extension for arrays of costs.
Definition: filename.h:155
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
unsigned int GetBoxNIntervals(Tbox *b)
Box dimensionality.
Definition: box.c:992
Error and warning functions.
void DeleteFileName(Tfilename *fn)
Destructor.
Definition: filename.c:205
int ReadBox(FILE *f, Tbox *b)
Reads a box from a file.
Definition: box.c:1172
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 SOL_EXT
File extension for solution files.
Definition: filename.h:137
A box.
Definition: box.h:83
int main(int argc, char **arg)
Main body of the cuikcollisions application.
#define WORLD_EXT
File extension for problem files.
Definition: filename.h:161
void DeleteParameters(Tparameters *p)
Destructor.
Definition: parameters.c:295
void DeleteBox(void *b)
Destructor.
Definition: box.c:1259
void GetBoxCenter(boolean *used, double *c, Tbox *b)
Returns the box center along the selected dimensions.
Definition: box.c:697
void InitWorldCD(Tparameters *pr, unsigned int mt, Tworld *w)
Initializes the collision detector.
Definition: world.c:1013