root/trunk/matml/src/ternary/geomview.c

Revision 503, 7.7 kB (checked in by powell, 2 years ago)

Allow NULL boundary argument to display nothing.

  • Property svn:eol-style set to native
Line 
1/***************************************
2  $Header$
3
4  This file handles the Geomview interaction.
5  ***************************************/
6
7
8#include "gibbs.h" /*+ Ternary prototypes, typedefs, etc. +*/
9#include <unistd.h>  /*+ For execlp() +*/
10#include <stdio.h>   /*+ For FILE, fdopen(), etc. +*/
11
12
13/*++++++++++++++++++++++++++++++++++++++
14  This function launches a new Geomview process and returns a FILE pointer and
15  optionally the Geomview version string.
16
17  int GeomviewBegin It returns zero or an error code.
18
19  FILE **geompipe Address of the FILE * object where GeomviewBegin will you can
20  later fprintf commands to Geomview.  Your code should declare a FILE * and
21  then pass its address to GeomviewBegin.
22
23  char *version Character array of at least 100 characters where GeomviewBegin
24  will put the Geomview version string (ignored if NULL).
25  ++++++++++++++++++++++++++++++++++++++*/
26
27int GeomviewBegin (FILE **geompipe, char *version)
28{
29  int ierr, gv_pid, gv_pipe[2], to_gv_pipe[2];
30
31  pipe (gv_pipe); /* from geomview stdout */
32  pipe (to_gv_pipe); /* to geomview stdin */
33  gv_pid = fork ();
34
35  if(gv_pid==0) /* child */
36    {
37      close (0);
38      dup (to_gv_pipe[0]);
39      close (to_gv_pipe[0]);
40      close (to_gv_pipe[1]);
41      close (1);
42      dup (gv_pipe[1]);
43      close (gv_pipe[0]);
44      close (gv_pipe[1]);
45      /* signal(SIGINT,SIG_IGN); */
46      execlp (GEOMVIEW,GEOMVIEW,"-c","(interest (pick world))","-",NULL);
47      perror (GEOMVIEW); /* only error gets here */
48    }
49
50  /* Program execution resumes here */
51  close (gv_pipe[1]);
52  close (to_gv_pipe[0]);
53  *geompipe = fdopen (to_gv_pipe[1], "w"); /* hooked to stdin of geomview */
54
55  if (version)
56    {
57      fprintf (*geompipe, "(echo (geomview-version) \"\n\")\n");
58      fflush (*geompipe);
59      read (gv_pipe[0], version, 99);
60    }
61
62  return 0;
63}
64
65
66/*++++++++++++++++++++++++++++++++++++++
67  This sends a bunch of triangles to Geomview in COFF format.
68
69  int GeomviewDisplayTriangleCOFF It returns zero or an error code.
70
71  FILE *geompipe FILE pointer hooked to Geomview stdin.
72
73  char *name Full name of this Geomview object.
74
75  char *label Short label of this Geomview object.
76
77  char *appearance Appearance tag for this Geomview object.
78
79  int npoints Number of points.
80
81  visual_point *points Point coordinates and colors.
82
83  int ntriangles Number of triangles.
84
85  int *vertices Vertex indices for each triangle.
86++++++++++++++++++++++++++++++++++++++*/
87
88int GeomviewDisplayTriangleCOFF
89(FILE *geompipe, char *name, char *label, char *appearance,
90 int npoints, visual_point *points, int ntriangles, int *vertices)
91{
92  int i;
93
94  fprintf (geompipe, "(geometry \"%s\" { : %s })\n", name, label);
95  fprintf (geompipe, "(read geometry { define %s \n", label);
96  fprintf (geompipe, "appearance { %s }\nCOFF\n", appearance);
97  fprintf (geompipe, "%d %d 0\n", npoints, ntriangles);
98
99  for (i=0; i<npoints; i++)
100    fprintf (geompipe, "%g %g %g %g %g %g %g\n", points[i].x, points[i].y,
101             points[i].z, points[i].red, points[i].green, points[i].blue,
102             points[i].alpha);
103
104  for (i=0; i<ntriangles; i++)
105    fprintf (geompipe, "3 %d %d %d\n", vertices[3*i], vertices[3*i+1],
106             vertices[3*i+2]);
107
108  fprintf (geompipe, "})\n");
109  fflush (geompipe);
110
111  return 0;
112}
113
114
115/*++++++++++++++++++++++++++++++++++++++
116  This sends a bunch of facet objects to Geomview in COFF format.
117
118  int GeomviewDisplayFacets It returns zero or an error code.
119
120  FILE *geompipe FILE pointer hooked to Geomview stdin.
121
122  char *name Full name of this Geomview object.
123
124  char *label Short label of this Geomview object.
125
126  char *appearance Appearance tag for this Geomview object.
127
128  int npoints Number of points.
129
130  visual_point *points Point coordinates and colors.
131
132  int numfacets Number of facet objects to display.
133
134  hull_facet *facets Array of facet objects.
135
136  facet_type type Facet type to display, or ALL_TYPES to display all of them.
137  ++++++++++++++++++++++++++++++++++++++*/
138
139int GeomviewDisplayFacets
140(FILE *geompipe, char *name, char *label, char *appearance,
141 int npoints, visual_point *points, int numfacets, hull_facet *facets,
142 facet_type type)
143{
144  int i, n=0;
145
146  // Count number of relevant facets
147  if (type == ALL_TYPES)
148    n = numfacets;
149  else
150    for (i=0; i<numfacets; i++)
151      if (facets [i].type == type)
152        n++;
153
154  fprintf (geompipe, "(geometry \"%s\" { : %s })\n", name, label);
155  fprintf (geompipe, "(read geometry { define %s \n", label);
156  fprintf (geompipe, "appearance { %s }\nCOFF\n", appearance);
157  fprintf (geompipe, "%d %d 0\n", npoints, n);
158
159  for (i=0; i<npoints; i++)
160    fprintf (geompipe, "%g %g %g %g %g %g %g\n", points[i].x, points[i].y,
161             points[i].z, points[i].red, points[i].green, points[i].blue,
162             points[i].alpha);
163
164  for (i=0; i<numfacets; i++)
165    if (type == ALL_TYPES || type == facets[i].type)
166      fprintf (geompipe, "3 %d %d %d\n", facets[i].vertex[0], facets[i].vertex[1], facets[i].vertex[2]);
167
168  fprintf (geompipe, "})\n");
169  fflush (geompipe);
170
171  return 0;
172}
173
174
175/*++++++++++++++++++++++++++++++++++++++
176  Displays a phase boundary.  Note this pipes all of the points back to
177  geomview again.
178
179  int GeomviewDisplayPhaseBoundary It returns zero or an error code.
180
181  FILE *geompipe FILE pointer hooked to Geomview stdin.
182
183  char *name Full name of this Geomview object.
184
185  char *label Short label of this Geomview object.
186
187  char *appearance Appearance tag for this Geomview object.
188
189  int npoints Number of points.
190
191  visual_point *points Point coordinates and colors.
192
193  phase_boundary theboundary Phase boundary object to display, or NULL to
194  display nothing.
195  ++++++++++++++++++++++++++++++++++++++*/
196
197int GeomviewDisplayPhaseBoundary
198(FILE *geompipe, char *name, char *label, char *appearance,
199 int npoints, visual_point *points, phase_boundary *theboundary)
200{
201  int i, n_edges=0, c;
202
203  if (theboundary)
204    {
205      n_edges = theboundary->n_edges;
206      c = theboundary->compos;
207    }
208  else
209    npoints = 0;
210
211  fprintf (geompipe, "(geometry \"%s\" { : %s })\n", name, label);
212  fprintf (geompipe, "(read geometry { define %s \n", label);
213  fprintf (geompipe, "appearance { %s }\nCOFF\n", appearance);
214  fprintf (geompipe, "%d %d 0\n", npoints, n_edges);
215
216#ifdef DEBUG
217  printf ("(geometry \"%s\" { : %s })\n", name, label);
218  printf ("(read geometry { define %s \n", label);
219  printf ("appearance { %s }\nCOFF\n", appearance);
220  printf ("%d %d 0\n", npoints, n_edges);
221#endif
222
223  for (i=0; i<npoints; i++)
224    {
225      fprintf (geompipe, "%g %g %g %g %g %g %g\n", points[i].x, points[i].y,
226               points[i].z, points[i].red, points[i].green, points[i].blue,
227               points[i].alpha);
228#ifdef DEBUG
229      printf ("%g %g %g %g %g %g %g\n", points[i].x, points[i].y,
230              points[i].z, points[i].red, points[i].green, points[i].blue,
231              points[i].alpha);
232#endif
233    }
234  for (i=0; i<n_edges; i++)
235    {
236      fprintf (geompipe, "3 %d %d %d\n", theboundary->edges [c*i],
237               (c>1) ? theboundary->edges [c*i+1] : theboundary->edges [c*i],
238               (c>2) ? theboundary->edges [c*i+2] : theboundary->edges [c*i]);
239#ifdef DEBUG
240      printf ("3 %d %d %d\n", theboundary->edges [c*i],
241              (c>1) ? theboundary->edges [c*i+1] : theboundary->edges [c*i],
242              (c>2) ? theboundary->edges [c*i+2] : theboundary->edges [c*i]);
243#endif
244    }
245  fprintf (geompipe, "})\n");
246#ifdef DEBUG
247  printf ("})\n");
248#endif
249  fflush (geompipe);
250
251  return 0;
252}
253
254
255/*++++++++++++++++++++++++++++++++++++++
256  GeomviewEnd exits geomview, closes its FILE, and sets its FILE to NULL so
257  nobody tries to use it again.
258
259  int GeomviewEnd It returns zero or an error code.
260
261  FILE **geompipe Address of the FILE * object originally opened by
262  GeomviewBegin().
263  ++++++++++++++++++++++++++++++++++++++*/
264
265int GeomviewEnd (FILE **geompipe)
266{
267  fprintf (*geompipe, "(exit)");
268  fclose (*geompipe);
269  *geompipe = NULL;
270  return 0;
271}
Note: See TracBrowser for help on using the browser.