38#ifdef G4VIS_BUILD_OI_DRIVER
45#include <Inventor/SbBox.h>
46#include <Inventor/actions/SoGLRenderAction.h>
47#include <Inventor/actions/SoAction.h>
48#include <Inventor/fields/SoSFFloat.h>
49#include <Inventor/misc/SoChildList.h>
50#include <Inventor/nodes/SoSeparator.h>
51#include <Inventor/nodes/SoIndexedFaceSet.h>
52#include <Inventor/nodes/SoNormal.h>
53#include <Inventor/nodes/SoCoordinate3.h>
54#include <Inventor/nodes/SoNormalBinding.h>
55#include <Inventor/SoPrimitiveVertex.h>
56#include <Inventor/elements/SoTextureCoordinateElement.h>
66 SO_NODE_CONSTRUCTOR(
SoTrap);
69 SO_NODE_ADD_FIELD(pDz, (1.0));
70 SO_NODE_ADD_FIELD(pTheta, (0.0));
71 SO_NODE_ADD_FIELD(pPhi, (0.0));
72 SO_NODE_ADD_FIELD(pDy1, (1.0));
73 SO_NODE_ADD_FIELD(pDx1, (1.0));
74 SO_NODE_ADD_FIELD(pDx2, (1.0));
75 SO_NODE_ADD_FIELD(pDy2, (1.0));
76 SO_NODE_ADD_FIELD(pDx3, (1.0));
77 SO_NODE_ADD_FIELD(pDx4, (1.0));
78 SO_NODE_ADD_FIELD(pAlp1, (0.0));
79 SO_NODE_ADD_FIELD(pAlp2, (0.0));
80 SO_NODE_ADD_FIELD(alternateRep, (NULL));
81 children =
new SoChildList(
this);
93 static bool first =
true;
96 SO_NODE_INIT_CLASS(
SoTrap,SoShape,
"Shape");
104 SoPrimitiveVertex pv;
107 SoState *state = action->getState();
111 SbBool useTexFunction=
112 (SoTextureCoordinateElement::getType(state) ==
113 SoTextureCoordinateElement::FUNCTION);
118 const SoTextureCoordinateElement *tce = NULL;
120 if (useTexFunction) {
121 tce = SoTextureCoordinateElement::getInstance(state);
127 SbVec3f point, normal;
132#define GEN_VERTEX(pv,x,y,z,s,t,nx,ny,nz) \
133 point.setValue(x,y,z); \
134 normal.setValue(nx,ny,nz); \
135 if (useTexFunction) { \
136 texCoord=tce->get(point,normal); \
142 pv.setPoint(point); \
143 pv.setNormal(normal); \
144 pv.setTextureCoords(texCoord); \
149 const int NPOINTS=8, NFACES=6, NINDICES = NFACES*5;
150 int indices[NINDICES] = {3,2,1,0, SO_END_FACE_INDEX,
151 4,5,6,7, SO_END_FACE_INDEX,
152 0,1,5,4, SO_END_FACE_INDEX,
153 1,2,6,5, SO_END_FACE_INDEX,
154 2,3,7,6, SO_END_FACE_INDEX,
155 3,0,4,7, SO_END_FACE_INDEX};
163 float points[NPOINTS][3];
164 points[0][0] =
pDx2.getValue()+
pDy1.getValue()*Talp1;
165 points[0][1] =
pDy1.getValue();
166 points[0][2] = -
pDz.getValue();
168 points[1][0] = -
pDx2.getValue()+
pDy1.getValue()*Talp1;
169 points[1][1] =
pDy1.getValue();
170 points[1][2] = -
pDz.getValue();
172 points[2][0] = -
pDx1.getValue()-
pDy1.getValue()*Talp1;
173 points[2][1] = -
pDy1.getValue();
174 points[2][2] = -
pDz.getValue();
176 points[3][0] =
pDx1.getValue()-
pDy1.getValue()*Talp1;
177 points[3][1] = -
pDy1.getValue();
178 points[3][2] = -
pDz.getValue();
180 points[4][0] =
pDx4.getValue()+
pDy2.getValue()*Talp2;
181 points[4][1] =
pDy2.getValue();
182 points[4][2] =
pDz.getValue();
184 points[5][0] = -
pDx4.getValue()+
pDy2.getValue()*Talp2;
185 points[5][1] =
pDy2.getValue();
186 points[5][2] =
pDz.getValue();
188 points[6][0] = -
pDx3.getValue()-
pDy2.getValue()*Talp2;
189 points[6][1] = -
pDy2.getValue();
190 points[6][2] =
pDz.getValue();
192 points[7][0] =
pDx3.getValue()-
pDy2.getValue()*Talp2;
193 points[7][1] = -
pDy2.getValue();
194 points[7][2] =
pDz.getValue();
198 points[i][0] -=
pDz.getValue()*TthetaCphi;
199 points[i][1] -=
pDz.getValue()*TthetaSphi;
202 points[i][0] +=
pDz.getValue()*TthetaCphi;
203 points[i][1] +=
pDz.getValue()*TthetaSphi;
206 SbVec3f normals[NFACES];
208 for (nf=0;nf<NFACES;nf++) {
209 int j0 = indices[5*nf + 0];
210 int j1 = indices[5*nf + 1];
211 int j2 = indices[5*nf + 2];
212 SbVec3f p0(points[j0][0],points[j0][1],points[j0][2]);
213 SbVec3f p1(points[j1][0],points[j1][1],points[j1][2]);
214 SbVec3f p2(points[j2][0],points[j2][1],points[j2][2]);
215 normals[nf] = (p1-p0).cross(p2-p0);
216 normals[nf].normalize();
221 for (nf=0;nf<NFACES;nf++) {
222 beginShape(action,TRIANGLE_FAN);
223 index = indices[nf * 5];
224 x = points[index][0];
225 y = points[index][1];
226 z = points[index][2];
227 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
228 index = indices[nf * 5 + 1];
229 x = points[index][0];
230 y = points[index][1];
231 z = points[index][2];
232 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
233 index = indices[nf * 5 + 2];
234 x = points[index][0];
235 y = points[index][1];
236 z = points[index][2];
237 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
238 index = indices[nf * 5 + 3];
239 x = points[index][0];
240 y = points[index][1];
241 z = points[index][2];
242 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
255 float pDx=
pDx1.getValue(),pDy=
pDy1.getValue();
257 if (
pDx2.getValue() > pDx) pDx =
pDx2.getValue();
258 if (
pDx3.getValue() > pDx) pDx =
pDx3.getValue();
259 if (
pDx4.getValue() > pDx) pDx =
pDx4.getValue();
260 if (
pDy2.getValue() > pDy) pDy =
pDy2.getValue();
265 if (Xalp< Xalp2) Xalp=Xalp2;
266 pDx +=
FFABS(TthetaCphi*
pDz.getValue());
268 pDy +=
FFABS(TthetaSphi*
pDz.getValue());
271 center.setValue(0,0,0);
272 box.setBounds(SbVec3f(-pDx,-pDy,-
pDz.getValue()),
273 SbVec3f( pDx, pDy,
pDz.getValue()));
280void SoTrap::updateChildren() {
285 assert(children->getLength()==1);
286 SoSeparator *sep = (SoSeparator *) ( *children)[0];
287 SoCoordinate3 *theCoordinates = (SoCoordinate3 *) ( sep->getChild(0));
288 SoNormal *theNormals = (SoNormal *) ( sep->getChild(1));
289 SoNormalBinding *theNormalBinding = (SoNormalBinding *) ( sep->getChild(2));
290 SoIndexedFaceSet *theFaceSet = (SoIndexedFaceSet *) ( sep->getChild(3));
292 const int NPOINTS=8, NFACES=6, NINDICES = NFACES*5;
293 float points[NPOINTS][3];
300 indices[NINDICES] = {3,2,1,0, SO_END_FACE_INDEX,
301 4,5,6,7, SO_END_FACE_INDEX,
302 0,1,5,4, SO_END_FACE_INDEX,
303 1,2,6,5, SO_END_FACE_INDEX,
304 2,3,7,6, SO_END_FACE_INDEX,
305 3,0,4,7, SO_END_FACE_INDEX};
314 points[0][0] =
pDx2.getValue()+
pDy1.getValue()*Talp1;
315 points[0][1] =
pDy1.getValue();
316 points[0][2] = -
pDz.getValue();
318 points[1][0] = -
pDx2.getValue()+
pDy1.getValue()*Talp1;
319 points[1][1] =
pDy1.getValue();
320 points[1][2] = -
pDz.getValue();
322 points[2][0] = -
pDx1.getValue()-
pDy1.getValue()*Talp1;
323 points[2][1] = -
pDy1.getValue();
324 points[2][2] = -
pDz.getValue();
326 points[3][0] =
pDx1.getValue()-
pDy1.getValue()*Talp1;
327 points[3][1] = -
pDy1.getValue();
328 points[3][2] = -
pDz.getValue();
330 points[4][0] =
pDx4.getValue()+
pDy2.getValue()*Talp2;
331 points[4][1] =
pDy2.getValue();
332 points[4][2] =
pDz.getValue();
334 points[5][0] = -
pDx4.getValue()+
pDy2.getValue()*Talp2;
335 points[5][1] =
pDy2.getValue();
336 points[5][2] =
pDz.getValue();
338 points[6][0] = -
pDx3.getValue()-
pDy2.getValue()*Talp2;
339 points[6][1] = -
pDy2.getValue();
340 points[6][2] =
pDz.getValue();
342 points[7][0] =
pDx3.getValue()-
pDy2.getValue()*Talp2;
343 points[7][1] = -
pDy2.getValue();
344 points[7][2] =
pDz.getValue();
348 points[i][0] -=
pDz.getValue()*TthetaCphi;
349 points[i][1] -=
pDz.getValue()*TthetaSphi;
352 points[i][0] +=
pDz.getValue()*TthetaCphi;
353 points[i][1] +=
pDz.getValue()*TthetaSphi;
356 for (
int np=0;np<NPOINTS;np++) theCoordinates->point.set1Value(np,points[np][0],points[np][1],points[np][2]);
357 theFaceSet->coordIndex.setValues(0,NINDICES,indices);
358 theNormals->vector.deleteValues(0);
359 theNormals->vector.insertSpace(0,6);
360 for (
int n=0;
n<6;
n++) {
361 int i0 = 5*
n+0,i1=5*
n+1,i2=5*
n+2;
362 int j0 = theFaceSet->coordIndex[i0];
363 int j1 = theFaceSet->coordIndex[i1];
364 int j2 = theFaceSet->coordIndex[i2];
365 SbVec3f p0= theCoordinates->point[j0];
366 SbVec3f p1= theCoordinates->point[j1];
367 SbVec3f p2= theCoordinates->point[j2];
368 SbVec3f normal = (p1-p0).cross(p2-p0);
370 theNormals->vector.set1Value(n,normal);
372 theNormalBinding->value=SoNormalBinding::PER_FACE;
376void SoTrap::generateChildren() {
383 assert(children->getLength() ==0);
384 SoSeparator *sep =
new SoSeparator();
385 SoCoordinate3 *theCoordinates =
new SoCoordinate3();
386 SoNormal *theNormals =
new SoNormal();
387 SoNormalBinding *theNormalBinding =
new SoNormalBinding();
388 SoIndexedFaceSet *theFaceSet =
new SoIndexedFaceSet();
392 sep->addChild(theCoordinates);
393 sep->addChild(theNormals);
394 sep->addChild(theNormalBinding);
395 sep->addChild(theFaceSet);
396 children->append(sep);
405 if (children->getLength() == 0) generateChildren();
SoSFFloat pDx2
Half-length along x of the side at y=+pDy1 of the face at -pDz.
SoSFFloat pDz
half-length along Z
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 computeBBox(SoAction *action, SbBox3f &box, SbVec3f ¢er)
compute bounding Box, required
static void initClass()
Class Initializer, required.
virtual void generatePrimitives(SoAction *action)
Generate Primitives, required.
SoTrap()
Constructor, required.
SoSFFloat pTheta
Polar angle of the line joining the centres of the faces at -/+pDz.
SoSFFloat pDx3
Half-length along x of the side at y=-pDy2 of the face at +pDz.
virtual SoChildList * getChildren() const
GetChildList, required whenever the class has hidden children.
SoSFFloat pDx1
Half-length along x of the side at y=-pDy1 of the face at -pDz.
virtual ~SoTrap()
Destructor, required.
virtual void clearAlternateRep()
We better be able to clear it, too!
virtual void generateAlternateRep()
SoSFFloat pDy2
Half-length along y of the face at +pDz.
SoSFNode alternateRep
Alternate rep - required.