plot3d.c
Go to the documentation of this file.
1 #include "plot3d.h"
2 #include "error.h"
3 
4 #include <stdlib.h>
5 #include <string.h>
6 #include <math.h>
7 
8 #include <sys/types.h>
9 #include <unistd.h>
10 #include <time.h>
11 #include <locale.h>
12 
25 /* Documented but not included in the doxygen documentation because it
26  screws up the IRI format. */
27 
37 #define CYLINDER_STRING "{INST geom { CBEZ223 2 1 0 1 1 0 0 1 0 2 1 1 1 1 1 0 1 1 2 0 1 1 0 1 0 0 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 } transforms{ = TLIST 0.5 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0.5 0 0 0 0 0 1 0 0 -1 0 0 0 0 0 1 0.5 0 0 0 0 -1 0 0 0 0 -1 0 0 0 0 1 0.5 0 0 0 0 0 -1 0 0 1 0 0 0 0 0 1}}"
38 
41 void InitPlot3d(char *name,boolean axes,int argc,char **arg,Tplot3d *p)
42 {
43  char command[200];
44  unsigned int i;
45  char pid[20];
46  unsigned int lname,lpid;
47  time_t t;
48  char hostname[50];
49 
50  /* To avoid strange numerical formats (, instead of . for decimals) that confuse geomview. */
51  setlocale(LC_ALL,"C");
52 
53  if (name!=NULL)
54  {
55  p->f=fopen(name,"w");
56 
57  if (!p->f)
58  Error("Can not create output file in InitPlot3d");
59  }
60  else
61  p->f=stdout;
62 
63  //start the ooglfile
64  if (argc>0)
65  {
66  fprintf(p->f,"#\n# Geomview plot generated with the following command line:\n# ");
67  for(i=0;i<(unsigned int)argc;i++)
68  fprintf(p->f,"%s ",arg[i]);
69  }
70  else
71  fprintf(p->f,"#\n# Geomview plot generated");
72  t=time(NULL);
73  gethostname(hostname,50);
74  fprintf(p->f,"\n# on %s# executed at %s\n#\n",ctime(&t),hostname);
75 
76  fprintf(p->f,"(progn ");
77  fprintf(p->f,"(normalization World none)\n");
78  fprintf(p->f,"(bbox-draw World no)\n");
79 
80  /*do not show the main pannel of geomview*/
81  /*fprintf(p->f,"(ui-panel geomview off)\n");*/
82 
83  /*show the tools pannel of geomview*/
84  /*fprintf(p->f,"(ui-panel tools on)\n");*/
85 
86  fprintf(p->f,"(backcolor World 1.0 1.0 1.0)\n"); /*background color de fons*/
87  if (p->f==stdout)
88  fprintf(p->f,"(backcolor Camera 1.0 1.0 1.0)\n"); /*background color de fons*/
89 
90  fprintf(p->f,"(dice World 15.0)\n"); /*granularity of curbed objects*/
91  fprintf(p->f," )\n");
92 
93  sprintf(pid,"_%d",(unsigned int)getpid());
94  lpid=strlen(pid);
95  if (name==NULL)
96  {
97  NEW(p->fileName,7+lpid,char);
98  strcpy(p->fileName,"stdout");
99  strcpy(&(p->fileName[6]),pid);
100  }
101  else
102  {
103  lname=strlen(name);
104  i=lname+lpid+1;
105  NEW(p->fileName,i,char);
106  strcpy(p->fileName,name);
107  strcpy(&(p->fileName[lname]),pid);
108  i=0;
109  while (p->fileName[i]!=0)
110  {
111  if ((p->fileName[i]=='/')||(p->fileName[i]=='.'))
112  p->fileName[i]='_';
113  i++;
114  }
115  }
116 
117  if (axes)
118  {
119  //axes (we assign them identifier 0)
120  fprintf(p->f,"(geometry sol_%s_0 ",p->fileName);
121  if (p->f!=stdout) fclose(p->f);
122 
123  if (p->f==stdout)
124  sprintf(command,"cat %s/share/axes.list",_CUIK_SUITE_MAIN_DIR);
125  else
126  sprintf(command,"cat %s/share/axes.list >> %s",_CUIK_SUITE_MAIN_DIR,name);
127 
128  system(command);
129 
130  if (p->f!=stdout) p->f=fopen(name,"a");
131  fprintf(p->f," )\n");
132  }
133 
134  p->nobj=0; /* Last object added to the scene */
135 
137 
138  p->inObject=FALSE;
139 }
140 
142 {
143  fprintf(p->f,"(progn \n");
144 }
145 
147 {
148  fprintf(p->f,")\n");
149  fflush(p->f);
150 }
151 
152 void Delete3dObject(unsigned int nobj,Tplot3d *p)
153 {
154  fprintf(p->f,"(delete sol_%s_%u )\n",p->fileName,nobj);
155 }
156 
157 unsigned int StartNew3dObject(Tcolor *c,Tplot3d *p)
158 {
159  Close3dObject(p);
160 
161  p->inObject=TRUE;
162  CopyColor(&(p->color),c);
163 
164  p->nobj++; /* We increas the object identifier (empty objects also use an identifier although they are never created) */
165 
166  fprintf(p->f,"(geometry sol_%s_%u {LIST \n",p->fileName,p->nobj);
167 
168  return(p->nobj); /*Return the identifier that will be assigned to the new object*/
169 }
170 
172 {
173  if (p->inObject)
174  {
175  /*close the previous appearance and object, if any*/
176  fprintf(p->f,"})\n");
177  fprintf(p->f,"(merge-ap sol_%s_%u {shading flat})\n",p->fileName,p->nobj);
178 
179  p->inObject=FALSE;
180  SetColor3dObject(p->nobj,&(p->color),p);
181  }
182 }
183 
185 {
186  if (p->inObject)
187  {
188  /*close the previous appearance and object, if any*/
189  fprintf(p->f,"})\n");
190  fprintf(p->f,"(merge-ap sol_%s_%u {shading flat})\n",p->fileName,p->nobj);
191 
192  p->inObject=FALSE;
193  }
194 }
195 
196 void SetColor3dObject(unsigned int nobj,Tcolor *c,Tplot3d *p)
197 {
198  if (!p->inObject)
199  {
200  if (!NullColor(c))
201  {
202  fprintf(p->f,"(merge-ap sol_%s_%u {material {kd 0.75 ",p->fileName,nobj);
203  fprintf(p->f," ambient ");PrintColor(p->f,c);
204  fprintf(p->f," *diffuse ");PrintColor(p->f,c);
205  fprintf(p->f," edgecolor ");PrintColor(p->f,c);
206  fprintf(p->f,"}})\n");
207  }
208  }
209 }
210 
211 void Delay3dObject(double t,Tplot3d *p)
212 {
213  if (t!=0.0)
214  fprintf(p->f,"(sleep-for %f)\n",t);
215 }
216 
217 void Move3dObject(unsigned int nobj,THTransform *t,Tplot3d *p)
218 {
219  fprintf(p->f,"(xform-set sol_%s_%u {",p->fileName,nobj);
220  HTransformPrintT(p->f,t);
221  fprintf(p->f,"})\n");
222 }
223 
224 void PlotBox3d(double min_x,double max_x,
225  double min_y,double max_y,
226  double min_z,double max_z,
227  Tplot3d *p)
228 {
229  if (!p->inObject)
230  Error("Adding geometry to a non-existing object");
231 
232  fprintf(p->f,"{OFF 8 6 1\n");
233 
234  /*vertices*/
235  fprintf(p->f,"%f %f %f\n",min_x,min_y,min_z);
236  fprintf(p->f,"%f %f %f\n",min_x,max_y,min_z);
237  fprintf(p->f,"%f %f %f\n",max_x,max_y,min_z);
238  fprintf(p->f,"%f %f %f\n",max_x,min_y,min_z);
239 
240  fprintf(p->f,"%f %f %f\n",min_x,min_y,max_z);
241  fprintf(p->f,"%f %f %f\n",min_x,max_y,max_z);
242  fprintf(p->f,"%f %f %f\n",max_x,max_y,max_z);
243  fprintf(p->f,"%f %f %f\n",max_x,min_y,max_z);
244 
245  /*faces*/
246  fprintf(p->f,"4 3 2 1 0\n");
247  fprintf(p->f,"4 4 5 6 7\n");
248  fprintf(p->f,"4 0 1 5 4\n");
249  fprintf(p->f,"4 2 3 7 6\n");
250  fprintf(p->f,"4 3 0 4 7\n");
251  fprintf(p->f,"4 1 2 6 5}\n");
252 
253  fflush(p->f);
254 }
255 
256 void PlotTriangle3d(double x1,double y1,double z1,
257  double x2,double y2,double z2,
258  double x3,double y3,double z3,
259  Tplot3d *p)
260 {
261  if (!p->inObject)
262  Error("Adding geometry to a non-existing object");
263 
264  /*vertices*/
265  fprintf(p->f,"{OFF 3 1 1\n");
266  fprintf(p->f,"%f %f %f\n",x1,y1,z1);
267  fprintf(p->f,"%f %f %f\n",x2,y2,z2);
268  fprintf(p->f,"%f %f %f\n",x3,y3,z3);
269 
270  /*faces*/
271  fprintf(p->f,"3 0 1 2}\n");
272 
273  fflush(p->f);
274 }
275 
276 void Plot3dObject(unsigned int nv,unsigned int nf,unsigned int ne,
277  double **v,
278  unsigned int *nvf,
279  unsigned int **fv,Tplot3d *p)
280 {
281  unsigned int i,j;
282 
283  if (!p->inObject)
284  Error("Adding geometry to a non-existing object");
285 
286  fprintf(p->f,"OFF\n");
287  fprintf(p->f,"%u %u %u\n",nv,nf,ne);
288  fprintf(p->f,"\n");
289  for(i=0;i<nv;i++)
290  fprintf(p->f,"%f %f %f\n",v[i][0],v[i][1],v[i][2]);
291 
292  fprintf(p->f,"\n");
293  for(i=0;i<nf;i++)
294  {
295  fprintf(p->f,"%u ",nvf[i]);
296  for(j=0;j<nvf[i];j++)
297  fprintf(p->f,"%u ",fv[i][j]);
298 
299  fprintf(p->f,"\n");
300  }
301  fprintf(p->f,"\n");
302 }
303 
304 void Plot3dObjectWithColor(unsigned int nv,unsigned int nf,unsigned int ne,
305  double **v,
306  unsigned int *nvf,
307  unsigned int **fv,Tcolor *c,Tplot3d *p)
308 {
309  unsigned int i,j;
310 
311  if (!p->inObject)
312  Error("Adding geometry to a non-existing object");
313 
314  fprintf(p->f,"OFF\n");
315  fprintf(p->f,"%u %u %u\n",nv,nf,ne);
316  fprintf(p->f,"\n");
317  for(i=0;i<nv;i++)
318  fprintf(p->f,"%f %f %f\n",v[i][0],v[i][1],v[i][2]);
319 
320  fprintf(p->f,"\n");
321  for(i=0;i<nf;i++)
322  {
323  fprintf(p->f,"%u ",nvf[i]);
324  for(j=0;j<nvf[i];j++)
325  fprintf(p->f,"%u ",fv[i][j]);
326 
327  PrintColor(p->f,c);
328 
329  fprintf(p->f," 1 \n");
330  }
331  fprintf(p->f,"\n");
332 }
333 
334 void Plot3dObjectWithColors(unsigned int nv,unsigned int nf,unsigned int ne,
335  double **v,Tcolor *c,
336  unsigned int *nvf,
337  unsigned int **fv,Tplot3d *p)
338 {
339  unsigned int i,j;
340 
341  if (!p->inObject)
342  Error("Adding geometry to a non-existing object");
343 
344  /* Disable the global color for the object so that the colors
345  provided here are actually visible. */
346  NewColor(-1,-1,-1,&(p->color));
347 
348  fprintf(p->f,"COFF\n");
349  fprintf(p->f,"%u %u %u\n",nv,nf,ne);
350  fprintf(p->f,"\n");
351  for(i=0;i<nv;i++)
352  {
353  fprintf(p->f,"%f %f %f ",v[i][0],v[i][1],v[i][2]);
354  PrintColor(p->f,&(c[i]));
355  fprintf(p->f," 1 \n");
356  }
357 
358  fprintf(p->f,"\n");
359  for(i=0;i<nf;i++)
360  {
361  fprintf(p->f,"%u ",nvf[i]);
362  for(j=0;j<nvf[i];j++)
363  fprintf(p->f,"%u ",fv[i][j]);
364  fprintf(p->f,"\n");
365  }
366  fprintf(p->f,"\n");
367 }
368 
369 void PlotSphere(double r,double x,double y,double z,Tplot3d *p)
370 {
371  if (!p->inObject)
372  Error("Adding geometry to a non-existing object");
373 
374  fprintf(p->f,"{SPHERE %f %f %f %f}\n",r,x,y,z);
375 }
376 
377 void PlotCylinder(double r,double *p1,double *p2,Tplot3d *p)
378 {
379  THTransform f;
380 
381  if (!p->inObject)
382  Error("Adding geometry to a non-existing object");
383 
384  HTransformX2Vect(r,r,p1,p2,&f);
385 
386  fprintf(p->f,"{INST\n");
387  fprintf(p->f," geom %s \n",CYLINDER_STRING);
388  fprintf(p->f," transform ");
389  HTransformPrintT(p->f,&f);
390  fprintf(p->f,"}\n");
391 
392  HTransformDelete(&f);
393 }
394 
395 void PlotLine(double *p1,double *p2,Tplot3d *p)
396 {
397  if (!p->inObject)
398  Error("Adding geometry to a non-existing object");
399 
400  fprintf(p->f,"{VECT 1 2 1 \n 2 \n 1\n");
401 
402  fprintf(p->f," %f %f %f\n",p1[0],p1[1],p1[2]);
403  fprintf(p->f," %f %f %f\n",p2[0],p2[1],p2[2]);
404 
405  PrintColor(p->f,&(p->color));
406  fprintf(p->f," 0}\n"); /*alpha component is 0*/
407 
408  fflush(p->f);
409 }
410 
411 
412 void PlotSegments(unsigned int n,double **pt,Tplot3d *p)
413 {
414  unsigned int k,i;
415 
416  if (!p->inObject)
417  Error("Adding geometry to a non-existing object");
418 
419  if (n%2)
420  Error("PlotSegments requires a even number of vertices");
421  k=n/2;
422 
423  fprintf(p->f,"{VECT %u %u 1 \n",k,n);
424 
425  /* two vertices per line */
426  for(i=0;i<k;i++)
427  fprintf(p->f," 2");
428  fprintf(p->f,"\n");
429 
430  /* Just one color will be provided for all polylines */
431  fprintf(p->f,"1");
432  for(i=1;i<k;i++)
433  fprintf(p->f," 0");
434  fprintf(p->f,"\n");
435 
436  /* The vertices */
437  for(i=0;i<n;i++)
438  fprintf(p->f," %f %f %f\n",pt[i][0],pt[i][1],pt[i][2]);
439 
440  /* the color for all segments (just one color) */
441  PrintColor(p->f,&(p->color));
442  fprintf(p->f," 0}\n"); /*alpha component is 0*/
443 
444  fflush(p->f);
445 }
446 
447 void PlotVect3d(unsigned int n,
448  double *x,double *y,double *z,
449  Tplot3d *p)
450 {
451  unsigned int i;
452 
453  if (!p->inObject)
454  Error("Adding geometry to a non-existing object");
455 
456  fprintf(p->f,"{VECT 1 %u 1 \n %u \n 1\n",n,n);
457 
458  for(i=0;i<n;i++)
459  fprintf(p->f," %f %f %f\n",x[i],y[i],z[i]);
460 
461  PrintColor(p->f,&(p->color));
462  fprintf(p->f," 0}\n"); /*alpha component is 0*/
463 
464  fflush(p->f);
465 }
466 
467 void Take3dSnapshot(char *name,Tplot3d *p)
468 {
469  /*fprintf(p->f,"(snapshot Camera \"| convert - %s.gif\")\n",name);*/
470  fprintf(p->f,"(snapshot Camera \"%s.ppm\")\n",name);
471 }
472 
473 void ClosePlot3d(boolean quit,double average_x,double average_y,double average_z,Tplot3d *p)
474 {
475  unsigned int i;
476 
477  Close3dObject(p);
478 
479  /*translate the objects*/
480  if ((average_x!=0.0)&&(average_y!=0.0)&&(average_z!=0.0))
481  {
482  /* Object 0 (reserved for the global axes, if any) are not plotted */
483  for(i=1;i<=p->nobj;i++)
484  fprintf(p->f,"(xform-set sol_%s_%u {1 0 0 0 0 1 0 0 0 0 1 0 %f %f %f 1})\n",p->fileName,i,average_x,average_y,average_z);
485  }
486 
487  if (quit)
488  fprintf(p->f,"(quit)\n");
489 
490  if (p->f!=stdout) fclose(p->f);
491 
492  free(p->fileName);
493 }
void PlotTriangle3d(double x1, double y1, double z1, double x2, double y2, double z2, double x3, double y3, double z3, Tplot3d *p)
Adds a triangle to the current object.
Definition: plot3d.c:256
void InitPlot3d(char *name, boolean axes, int argc, char **arg, Tplot3d *p)
Constructor.
Definition: plot3d.c:41
void PrintColor(FILE *f, Tcolor *c)
Prints the color.
Definition: color.c:138
#define FALSE
FALSE.
Definition: boolean.h:30
void PlotSphere(double r, double x, double y, double z, Tplot3d *p)
Adds a sphere to the current object.
Definition: plot3d.c:369
void Plot3dObject(unsigned int nv, unsigned int nf, unsigned int ne, double **v, unsigned int *nvf, unsigned int **fv, Tplot3d *p)
Adds a polytope to the current object.
Definition: plot3d.c:276
void Delete3dObject(unsigned int nobj, Tplot3d *p)
Deletes a previously created geometric object.
Definition: plot3d.c:152
Tcolor color
Definition: plot3d.h:62
boolean NullColor(Tcolor *c)
Identifies a non-valid color.
Definition: color.c:64
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
unsigned int StartNew3dObject(Tcolor *c, Tplot3d *p)
Start a composed object.
Definition: plot3d.c:157
A homgeneous transform in R^3.
char * fileName
Definition: plot3d.h:56
FILE * f
Definition: plot3d.h:55
void Start3dBlock(Tplot3d *p)
Starts a block of commands.
Definition: plot3d.c:141
void Take3dSnapshot(char *name, Tplot3d *p)
Takes a screenshot of the geometry.
Definition: plot3d.c:467
Definition of the Tplot3d type and the associated functions.
void Close3dObjectNoColor(Tplot3d *p)
Closes a composed object without assigning any color.
Definition: plot3d.c:184
#define TRUE
TRUE.
Definition: boolean.h:21
void Error(const char *s)
General error function.
Definition: error.c:80
void CopyColor(Tcolor *c_dst, Tcolor *c_src)
Copy constructor.
Definition: color.c:57
A color.
Definition: color.h:86
void PlotCylinder(double r, double *p1, double *p2, Tplot3d *p)
Adds a cylinder to the current object.
Definition: plot3d.c:377
void HTransformX2Vect(double sy, double sz, double *p1, double *p2, THTransform *t)
Transform a unitary vector along the X axis to a generic vector.
Definition: htransform.c:574
Error and warning functions.
A 3D plot.
Definition: plot3d.h:54
void PlotBox3d(double min_x, double max_x, double min_y, double max_y, double min_z, double max_z, Tplot3d *p)
Adds an axis aligned box to the current object.
Definition: plot3d.c:224
void Close3dBlock(Tplot3d *p)
Ends a block of commands.
Definition: plot3d.c:146
void ClosePlot3d(boolean quit, double average_x, double average_y, double average_z, Tplot3d *p)
Destructor.
Definition: plot3d.c:473
void Plot3dObjectWithColors(unsigned int nv, unsigned int nf, unsigned int ne, double **v, Tcolor *c, unsigned int *nvf, unsigned int **fv, Tplot3d *p)
Adds a colored polytope to the current object.
Definition: plot3d.c:334
void PlotVect3d(unsigned int n, double *x, double *y, double *z, Tplot3d *p)
Adds a polyline to the current object.
Definition: plot3d.c:447
void Delay3dObject(double t, Tplot3d *p)
Introduces a delay in the generation of the geometry.
Definition: plot3d.c:211
void PlotLine(double *p1, double *p2, Tplot3d *p)
Adds a line to the current object.
Definition: plot3d.c:395
void Close3dObject(Tplot3d *p)
Closes a composed object.
Definition: plot3d.c:171
void Plot3dObjectWithColor(unsigned int nv, unsigned int nf, unsigned int ne, double **v, unsigned int *nvf, unsigned int **fv, Tcolor *c, Tplot3d *p)
Adds a colored polytope to the current object.
Definition: plot3d.c:304
void Move3dObject(unsigned int nobj, THTransform *t, Tplot3d *p)
Moves a 3d object.
Definition: plot3d.c:217
void HTransformDelete(THTransform *t)
Destructor.
Definition: htransform.c:887
void PlotSegments(unsigned int n, double **pt, Tplot3d *p)
Adds a collecion of segments to the current object.
Definition: plot3d.c:412
#define DCP3D_G
Green component of the default color of 3d objects.
Definition: plot3d.h:38
unsigned int nobj
Definition: plot3d.h:60
void SetColor3dObject(unsigned int nobj, Tcolor *c, Tplot3d *p)
Changes the color of an object.
Definition: plot3d.c:196
#define DCP3D_R
Red component of the default color of 3d objects.
Definition: plot3d.h:30
void HTransformPrintT(FILE *f, THTransform *t)
Prints the transpose of a homogeneous transform to a file.
Definition: htransform.c:848
void NewColor(double r, double g, double b, Tcolor *c)
Constructor.
Definition: color.c:14
boolean inObject
Definition: plot3d.h:64