37#ifdef G4VIS_BUILD_OI_DRIVER
45#include <Inventor/SbBox.h>
46#include <Inventor/actions/SoGLRenderAction.h>
47#include <Inventor/fields/SoSFFloat.h>
48#include <Inventor/misc/SoChildList.h>
49#include <Inventor/nodes/SoSeparator.h>
50#include <Inventor/nodes/SoIndexedFaceSet.h>
51#include <Inventor/nodes/SoNormal.h>
52#include <Inventor/nodes/SoCoordinate3.h>
53#include <Inventor/nodes/SoNormalBinding.h>
54#include <Inventor/SoPrimitiveVertex.h>
55#include <Inventor/elements/SoTextureCoordinateElement.h>
67 SO_NODE_CONSTRUCTOR(
SoTubs);
70 SO_NODE_ADD_FIELD(pRMin, (0));
71 SO_NODE_ADD_FIELD(pRMax, (1));
72 SO_NODE_ADD_FIELD(pDz, (10));
73 SO_NODE_ADD_FIELD(pSPhi, (0));
74 SO_NODE_ADD_FIELD(pDPhi, ((
float)(2*
M_PI)));
75 SO_NODE_ADD_FIELD(alternateRep, (NULL));
76 children =
new SoChildList(
this);
88 static bool first =
true;
91 SO_NODE_INIT_CLASS(
SoTubs,SoShape,
"Shape");
101 SoState *state = action->getState();
105 SbBool useTexFunction=
106 (SoTextureCoordinateElement::getType(state) ==
107 SoTextureCoordinateElement::FUNCTION);
112 const SoTextureCoordinateElement* tce = NULL;
114 if (useTexFunction) {
115 tce = SoTextureCoordinateElement::getInstance(state);
121 SbVec3f point, normal;
126#define GEN_VERTEX(pv,x,y,z,s,t,nx,ny,nz) \
127 point.setValue((float)(x),(float)(y),(float)(z)); \
128 normal.setValue((float)(nx),(float)(ny),(float)(nz)); \
129 if (useTexFunction) { \
130 texCoord=tce->get(point,normal); \
132 texCoord[0]=(float)(s); \
133 texCoord[1]=(float)(t); \
135 pv.setPoint(point); \
136 pv.setNormal(normal); \
137 pv.setTextureCoords(texCoord); \
142 int NPHI = (int)(2+22*std::fabs(
pDPhi.getValue()/(2.0*
M_PI)));
143 double deltaPhi =
pDPhi.getValue()/NPHI, phi0 =
pSPhi.getValue(),phi1=phi0+
pDPhi.getValue();
144 double rMax=
pRMax.getValue(),rMin=
pRMin.getValue();
145 double zMax=
pDz.getValue(),zMin=-zMax;
146 double cosPhi0=std::cos(phi0), sinPhi0=std::sin(phi0);
147 double cosPhi1=std::cos(phi1), sinPhi1=std::sin(phi1);
148 double cosDeltaPhi=std::cos(deltaPhi),sinDeltaPhi=std::sin(deltaPhi);
153 double sinPhi,cosPhi;
154 beginShape(action,TRIANGLE_STRIP);
157 for (i = 0; i<=NPHI; i++) {
158 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMax,0.0,0.0,cosPhi,sinPhi,0);
159 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMin,1.0,1.0,cosPhi,sinPhi,0);
160 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
167 beginShape(action,TRIANGLE_STRIP);
170 for (i = 0; i<=NPHI; i++) {
171 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi,zMax,0.0,0.0,-cosPhi,-sinPhi,0);
172 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi,zMin,1.0,1.0,-cosPhi,-sinPhi,0);
173 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
177 if (std::fabs(deltaPhi)<2.0*
M_PI) {
181 beginShape(action,TRIANGLE_STRIP);
184 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMax,0.0,0.0,sinPhi,-cosPhi,0);
185 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMin,1.0,1.0,sinPhi,-cosPhi,0);
186 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi,zMax,1.0,0.0,sinPhi,-cosPhi,0);
187 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi,zMin,0.0,1.0,sinPhi,-cosPhi,0);
192 beginShape(action,TRIANGLE_STRIP);
195 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi, zMax,0.0,0.0,-sinPhi,+cosPhi,0);
196 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi, zMin,1.0,1.0,-sinPhi,+cosPhi,0);
197 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi, zMax,1.0,0.0,-sinPhi,+cosPhi,0);
198 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi, zMin,0.0,1.0,-sinPhi,+cosPhi,0);
205 beginShape(action,TRIANGLE_FAN);
208 GEN_VERTEX(pv,0,0,zMax,0.0,0.0,0,0,1);
209 for (i = 0; i<=NPHI; i++) {
210 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMax,1.0,1.0,0,0,1);
211 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
217 beginShape(action,TRIANGLE_FAN);
220 GEN_VERTEX(pv,0,0,zMin,0.0,0.0,0,0,-1);
221 for (i = 0; i<=NPHI; i++) {
222 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMin,1.0,1.0,0,0,-1);
223 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
227 beginShape(action,TRIANGLE_STRIP);
230 for (i = 0; i<=NPHI; i++) {
231 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi,zMax,0.0,0.0,0,0,1);
232 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMax,1.0,1.0,0,0,1);
233 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
239 beginShape(action,TRIANGLE_STRIP);
242 for (i = 0; i<=NPHI; i++) {
243 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi,zMin,0.0,0.0,0,0,-1);
244 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMin,1.0,1.0,0,0,-1);
245 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
259 SbVec3f vmin(-
pRMax.getValue(),-
pRMax.getValue(),-
pDz.getValue()),
261 center.setValue(0,0,0);
262 box.setBounds(vmin,vmax);
267void SoTubs::updateChildren() {
271 assert(children->getLength()==1);
272 SoSeparator *sep = (SoSeparator *) ( *children)[0];
273 SoCoordinate3 *theCoordinates = (SoCoordinate3 *) ( sep->getChild(0));
274 SoNormal *theNormals = (SoNormal *) ( sep->getChild(1));
275 SoNormalBinding *theNormalBinding = (SoNormalBinding *) ( sep->getChild(2));
276 SoIndexedFaceSet *theFaceSet = (SoIndexedFaceSet *) ( sep->getChild(3));
279 const int NPHI=24, NPOINTS=2*(2*NPHI+2), NFACES=4*NPHI+2, NINDICES = NFACES*5;
280 float points[NPOINTS][3],normals[NFACES][3];
282 static long indices[NINDICES];
284 static int32_t indices[NINDICES];
288 double phi,
pp, DeltaPhi;
297 for (i = 0; i< NPHI; i++) {
299 indices[5*i+0] = 2*i+0;
300 indices[5*i+1] = 2*i+1;
301 indices[5*i+2] = 2*i+3;
302 indices[5*i+3] = 2*i+2;
303 indices[5*i+4] = SO_END_FACE_INDEX;
306 for (i=0;i<NPHI;i++) {
307 indices[5*1*NPHI + 5*i+0] = 2*NPHI+2 + 2*i+0;
308 indices[5*1*NPHI + 5*i+1] = 2*NPHI+2 + 2*i+1;
309 indices[5*1*NPHI + 5*i+2] = 2*NPHI+2 + 2*i+3;
310 indices[5*1*NPHI + 5*i+3] = 2*NPHI+2 + 2*i+2;
311 indices[5*1*NPHI + 5*i+4] = SO_END_FACE_INDEX;
314 for (i=0;i<NPHI;i++) {
315 indices[5*2*NPHI + 5*i+0] = 2*i+0;
316 indices[5*2*NPHI + 5*i+1] = 2*i+2;
317 indices[5*2*NPHI + 5*i+2] = NPOINTS - (2*i+4);
318 indices[5*2*NPHI + 5*i+3] = NPOINTS - (2*i+2);
319 indices[5*2*NPHI + 5*i+4] = SO_END_FACE_INDEX;
322 for (i=0;i<NPHI;i++) {
323 indices[5*3*NPHI + 5*i+0] = 2*i+1;
324 indices[5*3*NPHI + 5*i+1] = NPOINTS - (2*i+1);
325 indices[5*3*NPHI + 5*i+2] = NPOINTS - (2*i+3);
326 indices[5*3*NPHI + 5*i+3] = 2*i+3;
327 indices[5*3*NPHI + 5*i+4] = SO_END_FACE_INDEX;
330 indices[5*4*NPHI +0] = 2*NPHI;
331 indices[5*4*NPHI +1] = 2*NPHI+1;
332 indices[5*4*NPHI +2] = 2*NPHI+3;
333 indices[5*4*NPHI +3] = 2*NPHI+2;
334 indices[5*4*NPHI +4] = SO_END_FACE_INDEX;
336 indices[5*4*NPHI +5 +0] = 0;
337 indices[5*4*NPHI +5 +1] = NPOINTS-2;
338 indices[5*4*NPHI +5 +2] = NPOINTS-1;
339 indices[5*4*NPHI +5 +3] = 1;
340 indices[5*4*NPHI +5 +4] = SO_END_FACE_INDEX;
345 indices[5*4*NPHI +0] = 2*NPHI;
346 indices[5*4*NPHI +1] = 2*NPHI+1;
347 indices[5*4*NPHI +2] = 2*NPHI+3;
348 indices[5*4*NPHI +3] = 2*NPHI+2;
349 indices[5*4*NPHI +4] = SO_END_FACE_INDEX;
351 indices[5*4*NPHI +5 +0] = 0;
352 indices[5*4*NPHI +5 +1] = NPOINTS-2;
353 indices[5*4*NPHI +5 +2] = NPOINTS-1;
354 indices[5*4*NPHI +5 +3] = 1;
355 indices[5*4*NPHI +5 +4] = SO_END_FACE_INDEX;
359 indices[5*4*NPHI +0] = SO_END_FACE_INDEX;
360 indices[5*4*NPHI +1] = SO_END_FACE_INDEX;
361 indices[5*4*NPHI +2] = SO_END_FACE_INDEX;
362 indices[5*4*NPHI +3] = SO_END_FACE_INDEX;
363 indices[5*4*NPHI +4] = SO_END_FACE_INDEX;
365 indices[5*4*NPHI +5 +0] = SO_END_FACE_INDEX;
366 indices[5*4*NPHI +5 +1] = SO_END_FACE_INDEX;
367 indices[5*4*NPHI +5 +2] = SO_END_FACE_INDEX;
368 indices[5*4*NPHI +5 +3] = SO_END_FACE_INDEX;
369 indices[5*4*NPHI +5 +4] = SO_END_FACE_INDEX;
372 DeltaPhi =
pDPhi.getValue()/NPHI, phi =
pSPhi.getValue();
373 for (i = 0; i<=NPHI; i++) {
374 points[2*i+0][0] =
pRMax.getValue()*
FCOS(phi);
375 points[2*i+0][1]=
pRMax.getValue()*
FSIN(phi);
376 points[2*i+0][2] = +
pDz.getValue();
378 points[2*i+1][0] =
pRMax.getValue()*
FCOS(phi);
379 points[2*i+1][1]=
pRMax.getValue()*
FSIN(phi);
380 points[2*i+1][2] = -
pDz.getValue();
382 pp = phi+DeltaPhi/2.0;
384 normals[i][0] =
FCOS(pp);
385 normals[i][1] =
FSIN(pp);
392 for (i = 0; i<=NPHI; i++) {
393 points[2*NPHI+2+2*i+0][0] =
pRMin.getValue()*
FCOS(phi);
394 points[2*NPHI+2+2*i+0][1] =
pRMin.getValue()*
FSIN(phi);
395 points[2*NPHI+2+2*i+0][2] = +
pDz.getValue();
396 points[2*NPHI+2+2*i+1][0] =
pRMin.getValue()*
FCOS(phi);
397 points[2*NPHI+2+2*i+1][1] =
pRMin.getValue()*
FSIN(phi);
398 points[2*NPHI+2+2*i+1][2] = -
pDz.getValue();
399 pp = phi-DeltaPhi/2.0;
401 normals[NPHI+i][0] = -
FCOS(pp);
402 normals[NPHI+i][1] = -
FSIN(pp);
403 normals[NPHI+i][2] = 0;
408 for (i=0;i<NPHI;i++) {
409 normals[2*NPHI+i][0]=normals[2*NPHI+i][1]=0;
410 normals[2*NPHI+i][2]= 1.0;
413 for (i=0;i<NPHI;i++) {
414 normals[3*NPHI+i][0]=normals[3*NPHI+i][1]=0;
415 normals[3*NPHI+i][2]= -1.0;
418 phi =
pSPhi.getValue();
419 normals[4*NPHI+0][0]=
FSIN(phi);
420 normals[4*NPHI+0][1]= -
FCOS(phi);
421 normals[4*NPHI+0][2]=0;
425 normals[4*NPHI+1][0]= -
FSIN(phi);
426 normals[4*NPHI+1][1]= +
FCOS(phi);
427 normals[4*NPHI+1][2]=0;
429 for (
int np=0;np<NPOINTS; np++) theCoordinates->point.set1Value(np,points[np][0],points[np][1],points[np][2]);
430 for (
int ni=0;ni<NINDICES;ni++) theFaceSet->coordIndex.set1Value(ni,indices[ni]);
431 for (
int nf=0;nf<NFACES;nf++) theNormals->vector.set1Value(nf,normals[nf][0],normals[nf][1],normals[nf][2]);
432 theNormalBinding->value=SoNormalBinding::PER_FACE;
436void SoTubs::generateChildren() {
443 assert(children->getLength() ==0);
444 SoSeparator *sep =
new SoSeparator();
445 SoCoordinate3 *theCoordinates =
new SoCoordinate3();
446 SoNormal *theNormals =
new SoNormal();
447 SoNormalBinding *theNormalBinding =
new SoNormalBinding();
448 SoIndexedFaceSet *theFaceSet =
new SoIndexedFaceSet();
452 sep->addChild(theCoordinates);
453 sep->addChild(theNormals);
454 sep->addChild(theNormalBinding);
455 sep->addChild(theFaceSet);
456 children->append(sep);
465 if (children->getLength() == 0) generateChildren();
virtual ~SoTubs()
Destructor, required.
virtual SoChildList * getChildren() const
GetChildList, required whenever the class has hidden children.
virtual void computeBBox(SoAction *action, SbBox3f &box, SbVec3f ¢er)
compute bounding Box, required
virtual void generateAlternateRep()
SoSFFloat pDz
Half-length in Z.
SoSFNode alternateRep
Alternate rep - required.
virtual void generatePrimitives(SoAction *action)
Generate Primitives, required.
static void initClass()
Class Initializer, required.
virtual void clearAlternateRep()
We better be able to clear it, too!
SoSFFloat pDPhi
Delta-angle, in radians.
SoSFFloat pSPhi
Starting angle, in radians.
SoTubs()
Constructor, required.
SoSFFloat pRMin
Inside radius of the tube.
SoSFFloat pRMax
Outside radius of the tube.