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