cuikplay.c
Go to the documentation of this file.
1 #include "cuikplay.h"
2 
3 #include "world.h"
4 #include "box_list.h"
5 #include "error.h"
6 
7 #include "cuikplay_interface.h"
8 #include "cuikplay_support.h"
9 
10 #include <gtk/gtk.h>
11 
12 #include <string.h>
13 #include <unistd.h>
14 #include <sys/time.h>
15 
16 
60 #define RECORD 0
61 
88 int main(int argc, char **arg)
89 {
90 
91  if (argc>2)
92  {
93  Tworld world;
94  Tparameters parameters;
95  Tfilename fsols;
96  Tfilename fparam;
97  Tfilename fworld;
98 
99  Tlist sol_box_list;
100  Titerator it;
101 
102  Tplot3d pt;
103 
104  TCuikPlayControl status;
105  double frameDelay,axesLength,t;
106 
107  struct timeval tp;
108 
109  GtkWidget *window,*slider;
110  #if (RECORD)
111  char *snapshotFile;
112  unsigned int l;
113 
114  fprintf(stderr,"RECORD MODE ON\n");
115  #endif
116 
117  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
118  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
119 
120  CreateFileName(NULL,arg[1],NULL,WORLD_EXT,&fworld);
121  InitWorldFromFile(&parameters,TRUE,arg[1],&world);
122  #if (RECORD)
123  l=strlen(GetFileName(&fworld));
124  NEW(snapshotFile,l+100,char);
125  #endif
126 
127  CreateFileName(NULL,arg[2],NULL,SOL_EXT,&fsols);
128  if (!ReadListOfBoxes(GetFileFullName(&fsols),&sol_box_list))
129  Error("Solution file can not be opened");
130 
131  InitIterator(&it,&sol_box_list);
132  First(&it);
133 
134  if (argc>3)
135  axesLength=atof(arg[3]);
136  else
137  axesLength=0;
138 
139  if (argc>4)
140  frameDelay=atof(arg[4]);
141  else
142  frameDelay=0.01;
143 
144  InitPlot3d(NULL,FALSE,argc,arg,&pt);
145 
146  Start3dBlock(&pt);
147  PlotWorld(&parameters,&pt,axesLength,&world);
148 
149  MoveWorld(&parameters,&pt,(Tbox *)GetCurrent(&it),&world);
150  Close3dBlock(&pt);
151 
152  status.maxFrame=ListSize(&sol_box_list);
153  if (status.maxFrame==0)
154  Error("Empty list of solutions");
155 
156  status.end=FALSE;
157  status.mode=1;/*PAUSED*/
158  status.currentFrame=0;
159  status.nextFrame=0;
160 
161  gtk_set_locale();
162  gtk_init(&argc,&arg);
163 
164  window=create_window_cuikplay((gpointer)(&status),status.maxFrame);
165  slider=lookup_widget(window,"hscale1");
166  gtk_widget_show(window);
167 
168  gettimeofday(&tp,NULL);
169  status.time=(tp.tv_sec+tp.tv_usec/1000)+frameDelay;
170 
171  #if (_DEBUG>1)
172  fprintf(stderr,"Max frame %u \n",status.maxFrame);
173  #endif
174  while(!status.end)
175  {
176  if (status.mode==0) /* if playing */
177  {
178  /* when we reach the end -> pause */
179  if (status.currentFrame<(status.maxFrame-1))
180  {
181  gettimeofday(&tp,NULL);
182  t=(tp.tv_sec+tp.tv_usec/1000)+frameDelay;
183 
184  /* if it is time to move to the next frame */
185  if (t>status.time)
186  {
187  /* Move the slider to currentFrame+1 and this will trigger and
188  even that actually sets nextFrame to currentFrame+1 and
189  actually displays the frame */
190  double v;
191 
192  v=(double)(status.currentFrame+1);
193 
194  #if (_DEBUG>1)
195  fprintf(stderr,"Moving Slider from %g to %g \n",
196  gtk_range_get_value(GTK_RANGE(slider)),v);
197  #endif
198 
199  gtk_range_set_value(GTK_RANGE(slider),v);
200  }
201  }
202  else
203  status.mode=1; /*PAUSE*/
204  }
205 
206  /* if we have to display a new configuration */
207  if (status.currentFrame!=status.nextFrame)
208  {
209  /* if we reached the end -> pause */
210  if (status.nextFrame<status.maxFrame)
211  {
212  fprintf(stderr,"Displaying: %u\n",status.nextFrame+1);
213 
214  MoveTo(status.nextFrame,&it);
215  MoveWorld(&parameters,&pt,(Tbox *)GetCurrent(&it),&world);
216  #if (RECORD)
217  sprintf(snapshotFile,"/tmp/%s_%04u",GetFileName(&fworld),
218  status.nextFrame+1);
219  Take3dSnapshot(snapshotFile,&pt);
220  #endif
221  status.currentFrame=status.nextFrame;
222 
223  /* set the time for the next frame */
224  gettimeofday(&tp,NULL);
225  t=(tp.tv_sec+tp.tv_usec/1000)+frameDelay;
226  status.time=t+frameDelay;
227  }
228  else
229  {
230  status.currentFrame=status.nextFrame=status.maxFrame;
231  status.mode=1; /*PAUSE*/
232  }
233  }
234  gtk_main_iteration_do(FALSE);
235  }
236 
237  ClosePlot3d(TRUE,0,0,0,&pt);
238 
239  DeleteListOfBoxes(&sol_box_list);
240 
241  DeleteFileName(&fsols);
242  DeleteFileName(&fworld);
243  DeleteFileName(&fparam);
244  #if (RECORD)
245  free(snapshotFile);
246  #endif
247 
248  DeleteWorld(&world);
249  DeleteParameters(&parameters);
250  }
251  else
252  {
253  fprintf(stdout," Wrong number of parameters.\n");
254  fprintf(stdout," Use:\n");
255  fprintf(stdout," cuikplay <world>.world <solutions>.sol [<axes> <delay>]\n");
256  fprintf(stdout," Where:\n");
257  fprintf(stdout," <world>: File describing the problem\n");
258  fprintf(stdout," <solutions>: Is the path of solutions to be animated\n");
259  fprintf(stdout," <axes>: Optional. Length for the axes for each link.\n");
260  fprintf(stdout," The default value is 0, i.e., not to display them.\n");
261  fprintf(stdout," <delay>: Optional. Delay (in seconds) between frames.\n");
262  fprintf(stdout," The default delay is 0.1 seconds.\n");
263  fprintf(stdout," File extensions are not required\n");
264  }
265 
266  return(EXIT_SUCCESS);
267 }
void First(Titerator *i)
Moves an iterator to the first position of its associated list.
Definition: list.c:356
int main(int argc, char **arg)
Main body of the cuikplay application.
Definition: cuikplay.c:88
char * GetFileName(Tfilename *fn)
Gets the file name.
Definition: filename.c:161
void MoveWorld(Tparameters *pr, Tplot3d *pt, Tbox *b, Tworld *w)
Moves the mechanisms defined in a world information to a given configuration.
Definition: world.c:3581
#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
void InitPlot3d(char *name, boolean axes, int argc, char **arg, Tplot3d *p)
Constructor.
Definition: plot3d.c:41
#define TRUE
TRUE.
Definition: boolean.h:21
void Close3dBlock(Tplot3d *p)
Ends a block of commands.
Definition: plot3d.c:146
unsigned int maxFrame
Definition: cuikplay.h:26
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
boolean MoveTo(unsigned int n, Titerator *i)
Moves an iterator to a given position of its associated list.
Definition: list.c:417
boolean ReadListOfBoxes(char *filename, Tlist *l)
Reads a list of boxes from a file.
Definition: box_list.c:286
Headers of the GTK interface functions for cuikplay.
Definition of the cuikplay control structure.
Collection of methods to work on Tlist of boxes.
unsigned int mode
Definition: cuikplay.h:23
unsigned int currentFrame
Definition: cuikplay.h:24
Definition of the Tworld type and the associated functions.
void DeleteWorld(Tworld *w)
Destructor.
Definition: world.c:3952
Error and warning functions.
void DeleteFileName(Tfilename *fn)
Destructor.
Definition: filename.c:205
A 3D plot.
Definition: plot3d.h:54
void DeleteListOfBoxes(Tlist *l)
Destructor.
Definition: box_list.c:353
A generic list.
Definition: list.h:46
void InitIterator(Titerator *i, Tlist *list)
Constructor.
Definition: list.c:284
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
Definition of the cuikplay control structure.
Definition: cuikplay.h:21
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
#define WORLD_EXT
File extension for problem files.
Definition: filename.h:162
void * GetCurrent(Titerator *i)
Gets the element pointed by the iterator.
Definition: list.c:299
void DeleteParameters(Tparameters *p)
Destructor.
Definition: parameters.c:294
void Take3dSnapshot(char *file, Tplot3d *p)
Takes a screenshot of the geometry.
Definition: plot3d.c:467
void PlotWorld(Tparameters *pr, Tplot3d *pt, double axesLength, Tworld *w)
Adds a world (environment plus mechanism) in a 3D scene.
Definition: world.c:3452
List iterator.
Definition: list.h:61
unsigned int nextFrame
Definition: cuikplay.h:25
boolean end
Definition: cuikplay.h:22
unsigned int ListSize(Tlist *list)
Gets the number of elements in the list.
Definition: list.c:180
void Start3dBlock(Tplot3d *p)
Starts a block of commands.
Definition: plot3d.c:141
void ClosePlot3d(boolean quit, double average_x, double average_y, double average_z, Tplot3d *p)
Destructor.
Definition: plot3d.c:473
Headers of the GTK support functions for cuikplay.