43#include <Inventor/SbBox.h>
44#include <Inventor/actions/SoGLRenderAction.h>
45#include <Inventor/actions/SoAction.h>
46#include <Inventor/fields/SoSFFloat.h>
47#include <Inventor/misc/SoChildList.h>
48#include <Inventor/nodes/SoSeparator.h>
49#include <Inventor/nodes/SoIndexedFaceSet.h>
50#include <Inventor/nodes/SoNormal.h>
51#include <Inventor/nodes/SoCoordinate3.h>
52#include <Inventor/nodes/SoNormalBinding.h>
53#include <Inventor/SoPrimitiveVertex.h>
54#include <Inventor/elements/SoTextureCoordinateElement.h>
64 SO_NODE_CONSTRUCTOR(
SoTrap);
67 SO_NODE_ADD_FIELD(pDz, (1.0));
68 SO_NODE_ADD_FIELD(pTheta, (0.0));
69 SO_NODE_ADD_FIELD(pPhi, (0.0));
70 SO_NODE_ADD_FIELD(pDy1, (1.0));
71 SO_NODE_ADD_FIELD(pDx1, (1.0));
72 SO_NODE_ADD_FIELD(pDx2, (1.0));
73 SO_NODE_ADD_FIELD(pDy2, (1.0));
74 SO_NODE_ADD_FIELD(pDx3, (1.0));
75 SO_NODE_ADD_FIELD(pDx4, (1.0));
76 SO_NODE_ADD_FIELD(pAlp1, (0.0));
77 SO_NODE_ADD_FIELD(pAlp2, (0.0));
78 SO_NODE_ADD_FIELD(alternateRep, (NULL));
79 children =
new SoChildList(
this);
91 static bool first =
true;
94 SO_NODE_INIT_CLASS(
SoTrap,SoShape,
"Shape");
102 SoPrimitiveVertex pv;
105 SoState *state = action->getState();
109 SbBool useTexFunction=
110 (SoTextureCoordinateElement::getType(state) ==
111 SoTextureCoordinateElement::FUNCTION);
116 const SoTextureCoordinateElement *tce = NULL;
118 if (useTexFunction) {
119 tce = SoTextureCoordinateElement::getInstance(state);
125 SbVec3f point, normal;
130#define GEN_VERTEX(pv,x,y,z,s,t,nx,ny,nz) \
131 point.setValue(x,y,z); \
132 normal.setValue(nx,ny,nz); \
133 if (useTexFunction) { \
134 texCoord=tce->get(point,normal); \
140 pv.setPoint(point); \
141 pv.setNormal(normal); \
142 pv.setTextureCoords(texCoord); \
147 const int NPOINTS=8, NFACES=6, NINDICES = NFACES*5;
148 int indices[NINDICES] = {3,2,1,0, SO_END_FACE_INDEX,
149 4,5,6,7, SO_END_FACE_INDEX,
150 0,1,5,4, SO_END_FACE_INDEX,
151 1,2,6,5, SO_END_FACE_INDEX,
152 2,3,7,6, SO_END_FACE_INDEX,
153 3,0,4,7, SO_END_FACE_INDEX};
161 float points[NPOINTS][3];
162 points[0][0] =
pDx2.getValue()+
pDy1.getValue()*Talp1;
163 points[0][1] =
pDy1.getValue();
164 points[0][2] = -
pDz.getValue();
166 points[1][0] = -
pDx2.getValue()+
pDy1.getValue()*Talp1;
167 points[1][1] =
pDy1.getValue();
168 points[1][2] = -
pDz.getValue();
170 points[2][0] = -
pDx1.getValue()-
pDy1.getValue()*Talp1;
171 points[2][1] = -
pDy1.getValue();
172 points[2][2] = -
pDz.getValue();
174 points[3][0] =
pDx1.getValue()-
pDy1.getValue()*Talp1;
175 points[3][1] = -
pDy1.getValue();
176 points[3][2] = -
pDz.getValue();
178 points[4][0] =
pDx4.getValue()+
pDy2.getValue()*Talp2;
179 points[4][1] =
pDy2.getValue();
180 points[4][2] =
pDz.getValue();
182 points[5][0] = -
pDx4.getValue()+
pDy2.getValue()*Talp2;
183 points[5][1] =
pDy2.getValue();
184 points[5][2] =
pDz.getValue();
186 points[6][0] = -
pDx3.getValue()-
pDy2.getValue()*Talp2;
187 points[6][1] = -
pDy2.getValue();
188 points[6][2] =
pDz.getValue();
190 points[7][0] =
pDx3.getValue()-
pDy2.getValue()*Talp2;
191 points[7][1] = -
pDy2.getValue();
192 points[7][2] =
pDz.getValue();
196 points[i][0] -=
pDz.getValue()*TthetaCphi;
197 points[i][1] -=
pDz.getValue()*TthetaSphi;
200 points[i][0] +=
pDz.getValue()*TthetaCphi;
201 points[i][1] +=
pDz.getValue()*TthetaSphi;
204 SbVec3f normals[NFACES];
206 for (nf=0;nf<NFACES;nf++) {
207 int j0 = indices[5*nf + 0];
208 int j1 = indices[5*nf + 1];
209 int j2 = indices[5*nf + 2];
210 SbVec3f p0(points[j0][0],points[j0][1],points[j0][2]);
211 SbVec3f p1(points[j1][0],points[j1][1],points[j1][2]);
212 SbVec3f p2(points[j2][0],points[j2][1],points[j2][2]);
213 normals[nf] = (p1-p0).cross(p2-p0);
214 normals[nf].normalize();
219 for (nf=0;nf<NFACES;nf++) {
220 beginShape(action,TRIANGLE_FAN);
221 index = indices[nf * 5];
222 x = points[index][0];
223 y = points[index][1];
224 z = points[index][2];
225 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
226 index = indices[nf * 5 + 1];
227 x = points[index][0];
228 y = points[index][1];
229 z = points[index][2];
230 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
231 index = indices[nf * 5 + 2];
232 x = points[index][0];
233 y = points[index][1];
234 z = points[index][2];
235 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
236 index = indices[nf * 5 + 3];
237 x = points[index][0];
238 y = points[index][1];
239 z = points[index][2];
240 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
253 float pDx=
pDx1.getValue(),pDy=
pDy1.getValue();
255 if (
pDx2.getValue() > pDx) pDx =
pDx2.getValue();
256 if (
pDx3.getValue() > pDx) pDx =
pDx3.getValue();
257 if (
pDx4.getValue() > pDx) pDx =
pDx4.getValue();
258 if (
pDy2.getValue() > pDy) pDy =
pDy2.getValue();
263 if (Xalp< Xalp2) Xalp=Xalp2;
264 pDx +=
FFABS(TthetaCphi*
pDz.getValue());
266 pDy +=
FFABS(TthetaSphi*
pDz.getValue());
269 center.setValue(0,0,0);
270 box.setBounds(SbVec3f(-pDx,-pDy,-
pDz.getValue()),
271 SbVec3f( pDx, pDy,
pDz.getValue()));
278void SoTrap::updateChildren() {
283 assert(children->getLength()==1);
284 SoSeparator *sep = (SoSeparator *) ( *children)[0];
285 SoCoordinate3 *theCoordinates = (SoCoordinate3 *) ( sep->getChild(0));
286 SoNormal *theNormals = (SoNormal *) ( sep->getChild(1));
287 SoNormalBinding *theNormalBinding = (SoNormalBinding *) ( sep->getChild(2));
288 SoIndexedFaceSet *theFaceSet = (SoIndexedFaceSet *) ( sep->getChild(3));
290 const int NPOINTS=8, NFACES=6, NINDICES = NFACES*5;
291 float points[NPOINTS][3];
298 indices[NINDICES] = {3,2,1,0, SO_END_FACE_INDEX,
299 4,5,6,7, SO_END_FACE_INDEX,
300 0,1,5,4, SO_END_FACE_INDEX,
301 1,2,6,5, SO_END_FACE_INDEX,
302 2,3,7,6, SO_END_FACE_INDEX,
303 3,0,4,7, SO_END_FACE_INDEX};
312 points[0][0] =
pDx2.getValue()+
pDy1.getValue()*Talp1;
313 points[0][1] =
pDy1.getValue();
314 points[0][2] = -
pDz.getValue();
316 points[1][0] = -
pDx2.getValue()+
pDy1.getValue()*Talp1;
317 points[1][1] =
pDy1.getValue();
318 points[1][2] = -
pDz.getValue();
320 points[2][0] = -
pDx1.getValue()-
pDy1.getValue()*Talp1;
321 points[2][1] = -
pDy1.getValue();
322 points[2][2] = -
pDz.getValue();
324 points[3][0] =
pDx1.getValue()-
pDy1.getValue()*Talp1;
325 points[3][1] = -
pDy1.getValue();
326 points[3][2] = -
pDz.getValue();
328 points[4][0] =
pDx4.getValue()+
pDy2.getValue()*Talp2;
329 points[4][1] =
pDy2.getValue();
330 points[4][2] =
pDz.getValue();
332 points[5][0] = -
pDx4.getValue()+
pDy2.getValue()*Talp2;
333 points[5][1] =
pDy2.getValue();
334 points[5][2] =
pDz.getValue();
336 points[6][0] = -
pDx3.getValue()-
pDy2.getValue()*Talp2;
337 points[6][1] = -
pDy2.getValue();
338 points[6][2] =
pDz.getValue();
340 points[7][0] =
pDx3.getValue()-
pDy2.getValue()*Talp2;
341 points[7][1] = -
pDy2.getValue();
342 points[7][2] =
pDz.getValue();
346 points[i][0] -=
pDz.getValue()*TthetaCphi;
347 points[i][1] -=
pDz.getValue()*TthetaSphi;
350 points[i][0] +=
pDz.getValue()*TthetaCphi;
351 points[i][1] +=
pDz.getValue()*TthetaSphi;
354 for (
int np=0;np<NPOINTS;np++) theCoordinates->point.set1Value(np,points[np][0],points[np][1],points[np][2]);
355 theFaceSet->coordIndex.setValues(0,NINDICES,indices);
356 theNormals->vector.deleteValues(0);
357 theNormals->vector.insertSpace(0,6);
358 for (
int n=0;
n<6;
n++) {
359 int i0 = 5*
n+0,i1=5*
n+1,i2=5*
n+2;
360 int j0 = theFaceSet->coordIndex[i0];
361 int j1 = theFaceSet->coordIndex[i1];
362 int j2 = theFaceSet->coordIndex[i2];
363 SbVec3f p0= theCoordinates->point[j0];
364 SbVec3f p1= theCoordinates->point[j1];
365 SbVec3f p2= theCoordinates->point[j2];
366 SbVec3f normal = (p1-p0).cross(p2-p0);
368 theNormals->vector.set1Value(n,normal);
370 theNormalBinding->value=SoNormalBinding::PER_FACE;
374void SoTrap::generateChildren() {
381 assert(children->getLength() ==0);
382 SoSeparator *sep =
new SoSeparator();
383 SoCoordinate3 *theCoordinates =
new SoCoordinate3();
384 SoNormal *theNormals =
new SoNormal();
385 SoNormalBinding *theNormalBinding =
new SoNormalBinding();
386 SoIndexedFaceSet *theFaceSet =
new SoIndexedFaceSet();
390 sep->addChild(theCoordinates);
391 sep->addChild(theNormals);
392 sep->addChild(theNormalBinding);
393 sep->addChild(theFaceSet);
394 children->append(sep);
403 if (children->getLength() == 0) generateChildren();
#define GEN_VERTEX(pv, x, y, z, s, t, nx, ny, nz)
SoSFFloat pDx2
Half-length along x of the side at y=+pDy1 of the face at -pDz.
SoSFFloat pDz
half-length along Z
virtual void clearAlternateRep()
We better be able to clear it, too!
SoSFFloat pDx4
Half-length along x of the side at y=+pDy2 of the face at +pDz.
SoSFFloat pDy1
Half-length along y of the face at -pDz.
virtual void generateAlternateRep()
virtual void computeBBox(SoAction *action, SbBox3f &box, SbVec3f ¢er)
compute bounding Box, required
SoSFFloat pTheta
Polar angle of the line joining the centres of the faces at -/+pDz.
virtual ~SoTrap()
Destructor, required.
SoSFFloat pDx3
Half-length along x of the side at y=-pDy2 of the face at +pDz.
SoSFFloat pDx1
Half-length along x of the side at y=-pDy1 of the face at -pDz.
static void initClass()
Class Initializer, required.
virtual SoChildList * getChildren() const
GetChildList, required whenever the class has hidden children.
virtual void generatePrimitives(SoAction *action)
Generate Primitives, required.
SoSFFloat pDy2
Half-length along y of the face at +pDz.
SoSFNode alternateRep
Alternate rep - required.