1 /* Copyright (C) 1992-1998 The Geometry Center
2  * Copyright (C) 1998-2000 Stuart Levy, Tamara Munzner, Mark Phillips
3  *
4  * This file is part of Geomview.
5  *
6  * Geomview is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * Geomview is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Geomview; see the file COPYING.  If not, write
18  * to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
19  * USA, or visit http://www.gnu.org.
20  */
21 
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25 
26 #if 0
27 static char copyright[] = "Copyright (C) 1992-1998 The Geometry Center\n\
28 Copyright (C) 1998-2000 Stuart Levy, Tamara Munzner, Mark Phillips";
29 #endif
30 
31 /*
32  *	polyhedron.c:	routines which operate on the polyhedron data structure
33  */
34 /*
35 */
36 
37 #include "geom.h"
38 #include "geomclass.h"
39 #include "polylistP.h"
40 #include "ooglutil.h"
41 #include "point.h"
42 #include "winged_edge.h"
43 #include "extern.h"
44 
45 Geom *
WEPolyhedronToVect(WEpolyhedron * poly,HPoint3 origin)46 WEPolyhedronToVect(WEpolyhedron *poly, HPoint3 origin)
47 {
48     int i, ii, jj;
49     Geom *orbit;
50     HPoint3 gorigin;
51     /*static HPoint3 origin2 = {0,0,0,1};*/
52     WEface *fptr;
53     Transform T;
54     short *vnvert;
55     short *vncolor;
56     HPoint3 *p;
57     ColorA *c;
58 
59     vnvert = OOGLNewN(short, poly->num_faces);
60     vncolor = OOGLNewN(short, poly->num_faces);
61     p = OOGLNewN(HPoint3, 2*poly->num_faces);
62     c = OOGLNewN(ColorA, poly->num_faces);
63 
64     for (i=0, fptr=poly->face_list; i<poly->num_faces;
65 	 ++i, fptr = fptr->next)
66 	{
67 	vnvert[i] = 2;	/* line segment! */
68 	vncolor[i] = 1;
69 	/* next two lines assume the indices in two sorts of groups match */
70 	c[i] =  GetCmapEntry( fptr->fill_tone);
71 	for (ii=0; ii<4; ++ii) for (jj=0; jj<4; ++jj)
72 	    /* fptr's tforms are transposed! */
73 	    T[jj][ii] = fptr->group_element[ii][jj];
74 	HPt3Transform( T, &origin, &gorigin);
75 	p[2*i] = origin;
76 	p[2*i+1] = gorigin;
77 	}
78 
79     orbit = GeomCreate("vect",
80 				CR_NOCOPY,
81 				CR_NVECT, poly->num_faces,
82 				CR_NVERT, 2*poly->num_faces,
83 				CR_NCOLR, poly->num_faces,
84 				CR_VECTC, vnvert,
85 				CR_COLRC, vncolor,
86 				CR_POINT4, p,
87 				CR_COLOR, c,
88 				CR_4D, 1,
89 				CR_END);
90     return(orbit);
91 }
92 /*static ColorA white = {1,1,1,1};*/
93 
94 Geom *
WEPolyhedronToPolyList(poly)95   WEPolyhedronToPolyList(poly)
96 WEpolyhedron *poly;
97 {
98   ColorA *colors = NULL;
99   HPoint3 *points = NULL;
100   int *nvert = NULL;
101   int *vindex = NULL;
102 
103   WEvertex	*vptr;
104   WEedge	*eptr;
105   WEface	*fptr;
106   int		cnt,  cnt2, total;
107 
108   Geom *plist;
109 
110   points = OOGLNewN(HPoint3, poly->num_vertices);
111   colors = OOGLNewN(ColorA,poly->num_faces);
112   nvert = OOGLNewN(int, poly->num_faces);
113 
114   vptr = poly->vertex_list;
115   cnt = 0;
116   do {
117     points[cnt].x = vptr->x[0];
118     points[cnt].y = vptr->x[1];
119     points[cnt].z = vptr->x[2];
120     points[cnt].w = vptr->x[3];
121     vptr->ideal = cnt++;
122     vptr = vptr->next;
123   } while (vptr != NULL);
124 
125   cnt = 0;
126   fptr = poly->face_list;
127   total = 0;
128   do {
129     colors[cnt] = GetCmapEntry(fptr->fill_tone);
130     nvert[cnt] = fptr->order;
131     total += nvert[cnt++];
132     fptr = fptr->next;
133   } while (fptr != NULL);
134   vindex = OOGLNewN(int, total);
135   cnt = 0;
136   fptr = poly->face_list;
137   do {
138     eptr = fptr->some_edge;
139     cnt2 = 0;
140     do {
141       if (eptr->fL == fptr) {
142 	vindex[cnt+cnt2] = eptr->v0->ideal;
143 	cnt2++;
144 	eptr = eptr->e1L;
145       } else {
146 	vindex[cnt+cnt2] = eptr->v1->ideal;
147 	cnt2++;
148 	eptr = eptr->e0R;
149       }
150     } while (eptr != fptr->some_edge);
151     cnt+= fptr->order;
152     fptr = fptr->next;  }
153   while (fptr != NULL);
154 
155   plist =
156     GeomCreate("polylist",
157 		/*CR_NOCOPY,		isn't supported for polylists! */
158 		CR_4D, 1,
159 		CR_NPOLY, poly->num_faces,
160 	       	CR_NVERT, nvert,
161 		CR_VERT, vindex,
162 		CR_POINT4, points,
163 		CR_POLYCOLOR, colors,
164 		CR_FLAG, PL_HASPCOL, 	/* put this here or meet a bug libpolylist */
165 		CR_END);
166   return(plist);
167 }
168 
169 Geom *
WEPolyhedronToBeams(WEpolyhedron * poly,float alpha)170 WEPolyhedronToBeams( WEpolyhedron *poly, float alpha)
171 {
172     WEedge *eptr;
173     WEvertex *vptr;
174     Geom *beams;
175     HPoint3 *points, p0, p1, v0, v1;
176     ColorA *colors;
177     int *nvert, *vindex, vcnt, fcnt;
178     float omega;
179     static ColorA white = {1,1,1,1};
180 
181     points = OOGLNewN(HPoint3, 4 * poly->num_edges);
182     colors = OOGLNewN(ColorA,  poly->num_edges);
183     nvert = OOGLNewN(int, poly->num_edges);
184     vindex = OOGLNewN(int, 4*poly->num_edges);
185 
186     omega = 1.0 - alpha;
187 
188     vcnt = fcnt = 0;
189     eptr = poly->edge_list;
190 #define CCOPY( d4, hpt3)	\
191 	(hpt3)->x = d4[0];	\
192 	(hpt3)->y = d4[1];	\
193 	(hpt3)->z = d4[2];	\
194 	(hpt3)->w = d4[3];
195     do {
196 	CCOPY( eptr->v0->x, &v0);
197       	if ((eptr->e0L->v0 = eptr->v0))	vptr = eptr->e0L->v1;
198       	else vptr = eptr->e0L->v0;
199 	CCOPY( vptr->x, &v1);
200 	HPt3Scale(omega, &v0, &p0);
201 	HPt3Scale(alpha, &v1, &p1);
202 	HPt3Add(&p0, &p1, &points[vcnt]);
203 	vindex[vcnt] = vcnt;
204 	vcnt++;
205 
206     if (eptr->fR == eptr->e0R->fR)
207       	if ((eptr->e0R->v0 = eptr->v0))	vptr = eptr->e0R->v1;
208       	else vptr = eptr->e0R->v0;
209     else
210       	if ((eptr->e0L->v0 = eptr->v0))	vptr = eptr->e0L->v1;
211       	else vptr = eptr->e0L->v0;
212 	CCOPY( vptr->x, &v1);
213 	HPt3Scale(alpha, &v1, &p1);
214 	HPt3Add(&p0, &p1, &points[vcnt]);
215 	vindex[vcnt] = vcnt;
216 	vcnt++;
217 
218 	CCOPY( eptr->v1->x, &v0);
219       	if ((eptr->e1R->v0 = eptr->v1))	vptr = eptr->e1R->v1;
220       	else vptr = eptr->e1R->v0;
221 	CCOPY( vptr->x, &v1);
222 	HPt3Scale(omega, &v0, &p0);
223 	HPt3Scale(alpha, &v1, &p1);
224 	HPt3Add(&p0, &p1, &points[vcnt]);
225 	vindex[vcnt] = vcnt;
226 	vcnt++;
227 
228       	if ((eptr->e1L->v0 = eptr->v1))	vptr = eptr->e1L->v1;
229       	else vptr = eptr->e1L->v0;
230 	CCOPY( vptr->x, &v1);
231 	HPt3Scale(alpha, &v1, &p1);
232 	HPt3Add(&p0, &p1, &points[vcnt]);
233 	vindex[vcnt] = vcnt;
234 	vcnt++;
235 
236     	colors[fcnt] = white;
237     	nvert[fcnt] = 4;
238 	fcnt++;
239 
240       	eptr = eptr->next;
241     }
242     while (eptr != NULL);
243 
244     beams = GeomCreate("polylist",
245                 CR_NPOLY, poly->num_edges,
246 	       	CR_NVERT, nvert,
247                 CR_VERT, vindex,
248                 CR_POINT4, points,
249                 CR_POLYCOLOR, colors,
250                 CR_FLAG, PL_HASPCOL,    /* put this here or meet a bug libpolyli
251 st */
252                 CR_END);
253     return(beams);
254 }
255 
256