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