Debian Codegraph @

apps/demos/revo/RevClass.c++

raw symbols SCIP
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 //
38 // Code for a Surface of Revolution class. This is not really very
39 // robust or well-designed (or complete or...).
40 //
41
42 #include <Inventor/SbLinear.h>cxx . . $ `<file>/lib/database/include/Inventor/SbLinear.h`/
43 #include <Inventor/nodes/SoSeparator.h>cxx . . $ `<file>/lib/database/include/Inventor/nodes/SoSeparator.h`/
44 #include <Inventor/nodes/SoNormal.h>cxx . . $ `<file>/lib/database/include/Inventor/nodes/SoNormal.h`/
45 #include <Inventor/nodes/SoNormalBinding.h>cxx . . $ `<file>/lib/database/include/Inventor/nodes/SoNormalBinding.h`/
46 #include <Inventor/nodes/SoShapeHints.h>cxx . . $ `<file>/lib/database/include/Inventor/nodes/SoShapeHints.h`/
47 #include <Inventor/nodes/SoCoordinate3.h>cxx . . $ `<file>/lib/database/include/Inventor/nodes/SoCoordinate3.h`/
48 #include <Inventor/nodes/SoQuadMesh.h>cxx . . $ `<file>/lib/database/include/Inventor/nodes/SoQuadMesh.h`/
49
50 #include "RevClass.h"cxx . . $ `<file>/apps/demos/revo/RevClass.h`/
51
52 //
53 // Utility function used to figure out the normal of a 2D line
54 // segment.
55 //
56 static inline void
57 figureNormalcxx . . $ figureNormal(4e513e8962fbc8c9).(const SbVec3fcxx . . $ SbVec3f# &p1, const SbVec3fcxx . . $ SbVec3f# &p2, SbVec3fcxx . . $ SbVec3f# &result)
58 {
59 double dx = p1[cxx . . $ SbVec3f#`operator[]`(c60a38c584b9a5cc).0] - p2[cxx . . $ SbVec3f#`operator[]`(c60a38c584b9a5cc).0];
60 double dy = p1[cxx . . $ SbVec3f#`operator[]`(c60a38c584b9a5cc).1] - p2[cxx . . $ SbVec3f#`operator[]`(c60a38c584b9a5cc).1];
61 result.setValuecxx . . $ SbVec3f#setValue(4e87f9d875f2d08d).(-dy, dx, 0.0);
62 result.normalizecxx . . $ SbVec3f#normalize(9b8c9e39889c762c).();
63 }
64
65 //
66 // This is used to figure out if the angle between the three given
67 // points is greater than the given angle (given as cos(angle)). If
68 // it is, code further down will create a sharp (faceted) edge.
69 //
70 static int
71 angleGreatercxx . . $ angleGreater(376b318d51d6d5c8).(const SbVec3fcxx . . $ SbVec3f# &p0, const SbVec3fcxx . . $ SbVec3f# &p1, const SbVec3fcxx . . $ SbVec3f# &p2,
72 double cosCreaseAngle)
73 {
74 SbVec3fcxx . . $ SbVec3f# v1 = p0 -cxx . . $ operator-(af63bf8d0639b76b). p1;
75 SbVec3fcxx . . $ SbVec3f# v2 = p1 -cxx . . $ operator-(af63bf8d0639b76b). p2;
76 double cAngle;
77 double l1 = v1.lengthcxx . . $ SbVec3f#length(5036dfe8d4170af2).();
78 double l2 = v2.lengthcxx . . $ SbVec3f#length(5036dfe8d4170af2).();
79 if (l1 < 0.001 || l2 < 0.001) cAngle = 0.0;
80 else
81 {
82 v1 /=cxx . . $ SbVec3f#`operator/=`(3aaca777b5795e29). l1; v2 /=cxx . . $ SbVec3f#`operator/=`(3aaca777b5795e29). l2;
83 cAngle = v1.dotcxx . . $ SbVec3f#dot(adcfb95606509c95).(v2);
84 }
85 return cAngle < cosCreaseAngle;
86 }
87
88 //
89 // Given a set of 2D profile coordinates (the z-coordinate of this
90 // Vec3f field is ignored), this creates a surface of revolution
91 // (including normals).
92 // If passed NULL, this routine will use the last coordinates passed
93 // (this is used when the number of sides in the revolution is
94 // changed).
95 //
96 void
97 RevolutionSurfacecxx . . $ RevolutionSurface#::createSurfacecxx . . $ RevolutionSurface#createSurface(a6ee1969b7754f32).(const SoMFVec3fcxx . . $ SoMFVec3f# *prof)
98 {
99 static int p_alloc = 0;
100 static SbVec3fcxx . . $ SbVec3f# *profile = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!;
101 static double cca;
102
103 //
104 // Ok, cheesy hack!
105 // First time through, allocate space for profile points, and
106 // calculate cosine of crease angle.
107 //
108 if (p_alloc == 0)
109 {
110 p_alloc = 2;
111 profile = new SbVec3fcxx . . $ SbVec3f#SbVec3f(49f6e7a06ebc5aa8).[p_alloc*2];
112 cca = cos(creaseAnglecxx . . $ creaseAngle.);
113 }
114
115 int nr, nc;
116 int i, j;
117
118 if (prof != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!)
119 {
120 nc = NumSidescxx . . $ RevolutionSurface#NumSides.;
121 nr = 0;
122
123 const SoMFVec3fcxx . . $ SoMFVec3f# &pc = (*prof);
124 int pcn = pc.getNumcxx . . $ SoMField#getNum(455f465bc33b4cdf).();
125 if (pcn*2 > p_alloc)
126 {
127 // Allocate more space for profile points
128 p_alloc = pcn*4;
129 delete[] profile;
130 profile = new SbVec3fcxx . . $ SbVec3f#SbVec3f(49f6e7a06ebc5aa8).[p_alloc];
131 }
132
133 //
134 // If the user drew from bottom to top, cool...
135 //
136 if (pcn > 0 && pc[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).0][cxx . . $ SbVec3f#`operator[]`(c60a38c584b9a5cc).1] < pc[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).pcn-1][cxx . . $ SbVec3f#`operator[]`(c60a38c584b9a5cc).1])
137 {
138 for (j = 0; j < pcn; j++)
139 {
140 profile[nr] =cxx . . $ SbVec3f#`operator=`(c7808628d070cfbe). pc[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).j];
141 ++nr;
142 //
143 // If the angle at an interior vertex is too big, we
144 // create an extra vertex to get a sharp edge
145 //
146 if (j > 0 && j < pcn-1 &&
147 angleGreatercxx . . $ angleGreater(376b318d51d6d5c8).(pc[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).j-1], pc[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).j], pc[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).j+1], cca))
148 {
149 profile[nr] =cxx . . $ SbVec3f#`operator=`(c7808628d070cfbe). pc[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).j];
150 ++nr;
151 }
152 }
153 }
154 //
155 // If the user drew profile from top to bottom, gotta reverse
156 //
157 else
158 {
159 for (j = pcn-1; j >= 0; j--)
160 {
161 profile[nr] =cxx . . $ SbVec3f#`operator=`(c7808628d070cfbe). pc[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).j];
162 ++nr;
163 //
164 // If the angle at an interior vertex is too big, we
165 // create an extra vertex to get a sharp edge
166 //
167 if (j > 0 && j < pcn-1 &&
168 angleGreatercxx . . $ angleGreater(376b318d51d6d5c8).(pc[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).j+1], pc[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).j], pc[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).j-1], cca))
169 {
170 profile[nr] =cxx . . $ SbVec3f#`operator=`(c7808628d070cfbe). pc[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).j];
171 ++nr;
172 }
173 }
174 }
175 qmeshcxx . . $ RevolutionSurface#qmesh.->verticesPerRowcxx . . $ SoQuadMesh#verticesPerRow. =cxx . . $ SoSFInt32#`operator=`(7864480464b09eea). nc;
176 qmeshcxx . . $ RevolutionSurface#qmesh.->verticesPerColumncxx . . $ SoQuadMesh#verticesPerColumn. =cxx . . $ SoSFInt32#`operator=`(7864480464b09eea). nr;
177 }
178 else // Profile already figured out
179 {
180 nc = NumSidescxx . . $ RevolutionSurface#NumSides.;
181 qmeshcxx . . $ RevolutionSurface#qmesh.->verticesPerRowcxx . . $ SoQuadMesh#verticesPerRow..setValuecxx . . $ SoSFInt32#setValue(d4f767463ce0a6b3).(nc);
182 nr = (int)qmeshcxx . . $ RevolutionSurface#qmesh.->verticesPerColumncxx . . $ SoQuadMesh#verticesPerColumn..getValuecxx . . $ SoSFInt32#getValue(455f465bc33b4cdf).();
183 }
184
185
186 // Make sure we have enough room in coords/norms:
187 coordscxx . . $ RevolutionSurface#coords.->pointcxx . . $ SoCoordinate3#point..setNumcxx . . $ SoMField#setNum(d4f767463ce0a6b3).(nr*nc);
188 normscxx . . $ RevolutionSurface#norms.->vectorcxx . . $ SoNormal#vector..setNumcxx . . $ SoMField#setNum(d4f767463ce0a6b3).(nr*nc);
189
190 // Grab a pointer to efficiently edit coords/norms:
191 SbVec3fcxx . . $ SbVec3f# *coordPtr = coordscxx . . $ RevolutionSurface#coords.->pointcxx . . $ SoCoordinate3#point..startEditingcxx . . $ SoMFVec3f#startEditing(d7218d170b8c46e1).();
192 SbVec3fcxx . . $ SbVec3f# *normPtr = normscxx . . $ RevolutionSurface#norms.->vectorcxx . . $ SoNormal#vector..startEditingcxx . . $ SoMFVec3f#startEditing(d7218d170b8c46e1).();
193
194 //
195 // Ok, figure out the first (and last) set of normals
196 //
197 for (j = 1; j < nr-1; j++)
198 {
199 SbVec3fcxx . . $ SbVec3f# ncxx . . $ SbVec3f#SbVec3f(49f6e7a06ebc5aa8).;
200 figureNormalcxx . . $ figureNormal(4e513e8962fbc8c9).(profile[j-1], profile[j+1], n);
201 normPtr[j*nc] =cxx . . $ SbVec3f#`operator=`(c7808628d070cfbe). n;
202 normPtr[j*nc + nc-1] =cxx . . $ SbVec3f#`operator=`(c7808628d070cfbe). n;
203 }
204 //
205 // And figure out the normals at the top and bottom
206 //
207 if (nr > 1)
208 {
209 SbVec3fcxx . . $ SbVec3f# ncxx . . $ SbVec3f#SbVec3f(49f6e7a06ebc5aa8).;
210 figureNormalcxx . . $ figureNormal(4e513e8962fbc8c9).(profile[0], profile[1], n);
211 normPtr[0] =cxx . . $ SbVec3f#`operator=`(c7808628d070cfbe). n;
212 normPtr[nc-1] =cxx . . $ SbVec3f#`operator=`(c7808628d070cfbe). n;
213
214 figureNormalcxx . . $ figureNormal(4e513e8962fbc8c9).(profile[nr-2], profile[nr-1], n);
215 normPtr[nr*nc-nc] =cxx . . $ SbVec3f#`operator=`(c7808628d070cfbe). n;
216 normPtr[nr*nc-1] =cxx . . $ SbVec3f#`operator=`(c7808628d070cfbe). n;
217 }
218
219 //
220 // Ok, now that the initial profile and normals have been figured
221 // out, just rotate them all about the y axis to get all of the
222 // other points...
223 //
224 for (i = 0; i < nc; i++) {
225 double angle = 2.0*M_PIcxx . . $ `<external>/11263917310783833665/math.h:1121:10`! * i / double(nc-1);
226 double ca = cos(angle);
227 double sa = sin(angle);
228
229 // Set coordinates
230 for (j = 0; j < nr; j++) {
231 coordPtr[j*nc + i] =cxx . . $ SbVec3f#`operator=`(c7808628d070cfbe). SbVec3fcxx . . $ SbVec3f#SbVec3f(bd1040fe176e0786).(profile[j][cxx . . $ SbVec3f#`operator[]`(5dbdaf238659ae67).0]*ca,
232 profile[j][cxx . . $ SbVec3f#`operator[]`(5dbdaf238659ae67).1], profile[j][cxx . . $ SbVec3f#`operator[]`(5dbdaf238659ae67).0]*sa);
233 }
234 // And normals
235 if (i != 0 && i != nc-1) for (j = 0; j < nr; j++) {
236 normPtr[j*nc + i] =cxx . . $ SbVec3f#`operator=`(c7808628d070cfbe).
237 SbVec3fcxx . . $ SbVec3f#SbVec3f(bd1040fe176e0786).(normscxx . . $ RevolutionSurface#norms.->vectorcxx . . $ SoNormal#vector.[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).j*nc][cxx . . $ SbVec3f#`operator[]`(c60a38c584b9a5cc).0]*ca,
238 normscxx . . $ RevolutionSurface#norms.->vectorcxx . . $ SoNormal#vector.[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).j*nc][cxx . . $ SbVec3f#`operator[]`(c60a38c584b9a5cc).1], normscxx . . $ RevolutionSurface#norms.->vectorcxx . . $ SoNormal#vector.[cxx . . $ SoMFVec3f#`operator[]`(e550528ef0703ba8).j*nc][cxx . . $ SbVec3f#`operator[]`(c60a38c584b9a5cc).0]*sa);
239 }
240 }
241 // Let Inventor know we're done modifying these for now:
242 coordscxx . . $ RevolutionSurface#coords.->pointcxx . . $ SoCoordinate3#point..finishEditingcxx . . $ SoMFVec3f#finishEditing(49f6e7a06ebc5aa8).();
243 normscxx . . $ RevolutionSurface#norms.->vectorcxx . . $ SoNormal#vector..finishEditingcxx . . $ SoMFVec3f#finishEditing(49f6e7a06ebc5aa8).();
244 }
245
246 //
247 // Construct a new surface of revolution. This sets up the scene
248 // graph (which will be returned by getSceneGraph())
249 //
250 RevolutionSurfacecxx . . $ RevolutionSurface#::RevolutionSurfacecxx . . $ RevolutionSurface#RevolutionSurface(49f6e7a06ebc5aa8).()
251 {
252 rootcxx . . $ RevolutionSurface#root. = new SoSeparatorcxx . . $ SoSeparator#SoSeparator(49f6e7a06ebc5aa8).;
253 rootcxx . . $ RevolutionSurface#root.->refcxx . . $ SoBase#ref(83f5f38750778ac8).();
254
255 //
256 // This combination of shape hints will turn on two-sided lighting
257 // for us (if it is available)
258 //
259 SoShapeHintscxx . . $ SoShapeHints# *hints = new SoShapeHintscxx . . $ SoShapeHints#;
260 rootcxx . . $ RevolutionSurface#root.->addChildcxx . . $ SoGroup#addChild(da6c7544ccee32bd).(hints);
261 hints->vertexOrderingcxx . . $ SoShapeHints#vertexOrdering. =cxx . . $ SoSFEnum#`operator=`(7864480464b09eea). SoShapeHintscxx . . $ SoShapeHints#::COUNTERCLOCKWISEcxx . . $ SoShapeHints#COUNTERCLOCKWISE.;
262 hints->faceTypecxx . . $ SoShapeHints#faceType. =cxx . . $ SoSFEnum#`operator=`(7864480464b09eea). SoShapeHintscxx . . $ SoShapeHints#::CONVEXcxx . . $ SoShapeHints#CONVEX.;
263
264 coordscxx . . $ RevolutionSurface#coords. = new SoCoordinate3cxx . . $ SoCoordinate3#;
265 rootcxx . . $ RevolutionSurface#root.->addChildcxx . . $ SoGroup#addChild(da6c7544ccee32bd).(coordscxx . . $ RevolutionSurface#coords.);
266
267 normscxx . . $ RevolutionSurface#norms. = new SoNormalcxx . . $ SoNormal#;
268 rootcxx . . $ RevolutionSurface#root.->addChildcxx . . $ SoGroup#addChild(da6c7544ccee32bd).(normscxx . . $ RevolutionSurface#norms.);
269
270 SoNormalBindingcxx . . $ SoNormalBinding# *nb = new SoNormalBindingcxx . . $ SoNormalBinding#;
271 rootcxx . . $ RevolutionSurface#root.->addChildcxx . . $ SoGroup#addChild(da6c7544ccee32bd).(nb);
272 nb->valuecxx . . $ SoNormalBinding#value. =cxx . . $ SoSFEnum#`operator=`(7864480464b09eea). SoNormalBindingcxx . . $ SoNormalBinding#::PER_VERTEX_INDEXEDcxx . . $ SoNormalBinding#PER_VERTEX_INDEXED.;
273
274 qmeshcxx . . $ RevolutionSurface#qmesh. = new SoQuadMeshcxx . . $ SoQuadMesh#;
275 rootcxx . . $ RevolutionSurface#root.->addChildcxx . . $ SoGroup#addChild(da6c7544ccee32bd).(qmeshcxx . . $ RevolutionSurface#qmesh.);
276
277 NumSidescxx . . $ RevolutionSurface#NumSides. = 20; // Default...
278 }
279
280 //
281 // Destructor. Must unref everything reffed in the constructor.
282 //
283 RevolutionSurfacecxx . . $ RevolutionSurface#::~cxx . . $ RevolutionSurface#`~RevolutionSurface`(49f6e7a06ebc5aa8).RevolutionSurfacecxx . . $ RevolutionSurface#()
284 {
285 rootcxx . . $ RevolutionSurface#root.->unrefcxx . . $ SoBase#unref(83f5f38750778ac8).();
286 }
287
288 void
289 RevolutionSurfacecxx . . $ RevolutionSurface#::changeNumSidescxx . . $ RevolutionSurface#changeNumSides(d4f767463ce0a6b3).(int n)
290 {
291 NumSidescxx . . $ RevolutionSurface#NumSides. = n;
292
293 createSurfacecxx . . $ RevolutionSurface#createSurface(a6ee1969b7754f32).(NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!); // Re-calculate from old profile
294 }
295
296 SoSeparatorcxx . . $ SoSeparator# *
297 RevolutionSurfacecxx . . $ RevolutionSurface#::getSceneGraphcxx . . $ RevolutionSurface#getSceneGraph(144169e46c9b0e8a).()
298 {
299 return rootcxx . . $ RevolutionSurface#root.;
300 }