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 SO_NODE_INIT_CLASS(SoTubs,SoShape,
"Shape");
97 SoState *state = action->getState();
101 SbBool useTexFunction=
102 (SoTextureCoordinateElement::getType(state) ==
103 SoTextureCoordinateElement::FUNCTION);
108 const SoTextureCoordinateElement* tce = NULL;
110 if (useTexFunction) {
111 tce = SoTextureCoordinateElement::getInstance(state);
122 #define GEN_VERTEX(pv,x,y,z,s,t,nx,ny,nz) \
123 point.setValue((float)(x),(float)(y),(float)(z)); \
124 normal.setValue((float)(nx),(float)(ny),(float)(nz)); \
125 if (useTexFunction) { \
126 texCoord=tce->get(point,normal); \
128 texCoord[0]=(float)(s); \
129 texCoord[1]=(float)(t); \
131 pv.setPoint(point); \
132 pv.setNormal(normal); \
133 pv.setTextureCoords(texCoord); \
138 int NPHI = (
int)(2+22*std::fabs(
pDPhi.getValue()/(2.0*
M_PI)));
139 double deltaPhi =
pDPhi.getValue()/NPHI, phi0 =
pSPhi.getValue(),phi1=phi0+
pDPhi.getValue();
140 double rMax=
pRMax.getValue(),rMin=
pRMin.getValue();
141 double zMax=
pDz.getValue(),zMin=-zMax;
142 double cosPhi0=std::cos(phi0), sinPhi0=std::sin(phi0);
143 double cosPhi1=std::cos(phi1), sinPhi1=std::sin(phi1);
144 double cosDeltaPhi=std::cos(deltaPhi),sinDeltaPhi=std::sin(deltaPhi);
149 double sinPhi,cosPhi;
150 beginShape(action,TRIANGLE_STRIP);
153 for (i = 0; i<=NPHI; i++) {
154 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMax,0.0,0.0,cosPhi,sinPhi,0);
155 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMin,1.0,1.0,cosPhi,sinPhi,0);
156 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
163 beginShape(action,TRIANGLE_STRIP);
166 for (i = 0; i<=NPHI; i++) {
167 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi,zMax,0.0,0.0,-cosPhi,-sinPhi,0);
168 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi,zMin,1.0,1.0,-cosPhi,-sinPhi,0);
169 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
173 if (std::fabs(deltaPhi)<2.0*
M_PI) {
177 beginShape(action,TRIANGLE_STRIP);
180 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMax,0.0,0.0,sinPhi,-cosPhi,0);
181 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMin,1.0,1.0,sinPhi,-cosPhi,0);
182 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi,zMax,1.0,0.0,sinPhi,-cosPhi,0);
183 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi,zMin,0.0,1.0,sinPhi,-cosPhi,0);
188 beginShape(action,TRIANGLE_STRIP);
191 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi, zMax,0.0,0.0,-sinPhi,+cosPhi,0);
192 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi, zMin,1.0,1.0,-sinPhi,+cosPhi,0);
193 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi, zMax,1.0,0.0,-sinPhi,+cosPhi,0);
194 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi, zMin,0.0,1.0,-sinPhi,+cosPhi,0);
201 beginShape(action,TRIANGLE_FAN);
204 GEN_VERTEX(pv,0,0,zMax,0.0,0.0,0,0,1);
205 for (i = 0; i<=NPHI; i++) {
206 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMax,1.0,1.0,0,0,1);
207 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
213 beginShape(action,TRIANGLE_FAN);
216 GEN_VERTEX(pv,0,0,zMin,0.0,0.0,0,0,-1);
217 for (i = 0; i<=NPHI; i++) {
218 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMin,1.0,1.0,0,0,-1);
219 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
223 beginShape(action,TRIANGLE_STRIP);
226 for (i = 0; i<=NPHI; i++) {
227 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi,zMax,0.0,0.0,0,0,1);
228 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMax,1.0,1.0,0,0,1);
229 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
235 beginShape(action,TRIANGLE_STRIP);
238 for (i = 0; i<=NPHI; i++) {
239 GEN_VERTEX(pv,rMin*cosPhi,rMin*sinPhi,zMin,0.0,0.0,0,0,-1);
240 GEN_VERTEX(pv,rMax*cosPhi,rMax*sinPhi,zMin,1.0,1.0,0,0,-1);
241 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
255 SbVec3f vmin(-
pRMax.getValue(),-
pRMax.getValue(),-
pDz.getValue()),
257 center.setValue(0,0,0);
258 box.setBounds(vmin,vmax);
268 SoSeparator *sep = (SoSeparator *) ( *
children)[0];
269 SoCoordinate3 *theCoordinates = (SoCoordinate3 *) ( sep->getChild(0));
270 SoNormal *theNormals = (SoNormal *) ( sep->getChild(1));
271 SoNormalBinding *theNormalBinding = (SoNormalBinding *) ( sep->getChild(2));
272 SoIndexedFaceSet *theFaceSet = (SoIndexedFaceSet *) ( sep->getChild(3));
275 const int NPHI=24, NPOINTS=2*(2*NPHI+2), NFACES=4*NPHI+2, NINDICES = NFACES*5;
276 float points[NPOINTS][3],normals[NFACES][3];
278 static long indices[NINDICES];
280 static int32_t indices[NINDICES];
284 double phi,
pp, DeltaPhi;
293 for (i = 0; i< NPHI; i++) {
295 indices[5*i+0] = 2*i+0;
296 indices[5*i+1] = 2*i+1;
297 indices[5*i+2] = 2*i+3;
298 indices[5*i+3] = 2*i+2;
299 indices[5*i+4] = SO_END_FACE_INDEX;
302 for (i=0;i<NPHI;i++) {
303 indices[5*1*NPHI + 5*i+0] = 2*NPHI+2 + 2*i+0;
304 indices[5*1*NPHI + 5*i+1] = 2*NPHI+2 + 2*i+1;
305 indices[5*1*NPHI + 5*i+2] = 2*NPHI+2 + 2*i+3;
306 indices[5*1*NPHI + 5*i+3] = 2*NPHI+2 + 2*i+2;
307 indices[5*1*NPHI + 5*i+4] = SO_END_FACE_INDEX;
310 for (i=0;i<NPHI;i++) {
311 indices[5*2*NPHI + 5*i+0] = 2*i+0;
312 indices[5*2*NPHI + 5*i+1] = 2*i+2;
313 indices[5*2*NPHI + 5*i+2] = NPOINTS - (2*i+4);
314 indices[5*2*NPHI + 5*i+3] = NPOINTS - (2*i+2);
315 indices[5*2*NPHI + 5*i+4] = SO_END_FACE_INDEX;
318 for (i=0;i<NPHI;i++) {
319 indices[5*3*NPHI + 5*i+0] = 2*i+1;
320 indices[5*3*NPHI + 5*i+1] = NPOINTS - (2*i+1);
321 indices[5*3*NPHI + 5*i+2] = NPOINTS - (2*i+3);
322 indices[5*3*NPHI + 5*i+3] = 2*i+3;
323 indices[5*3*NPHI + 5*i+4] = SO_END_FACE_INDEX;
326 indices[5*4*NPHI +0] = 2*NPHI;
327 indices[5*4*NPHI +1] = 2*NPHI+1;
328 indices[5*4*NPHI +2] = 2*NPHI+3;
329 indices[5*4*NPHI +3] = 2*NPHI+2;
330 indices[5*4*NPHI +4] = SO_END_FACE_INDEX;
332 indices[5*4*NPHI +5 +0] = 0;
333 indices[5*4*NPHI +5 +1] = NPOINTS-2;
334 indices[5*4*NPHI +5 +2] = NPOINTS-1;
335 indices[5*4*NPHI +5 +3] = 1;
336 indices[5*4*NPHI +5 +4] = SO_END_FACE_INDEX;
341 indices[5*4*NPHI +0] = 2*NPHI;
342 indices[5*4*NPHI +1] = 2*NPHI+1;
343 indices[5*4*NPHI +2] = 2*NPHI+3;
344 indices[5*4*NPHI +3] = 2*NPHI+2;
345 indices[5*4*NPHI +4] = SO_END_FACE_INDEX;
347 indices[5*4*NPHI +5 +0] = 0;
348 indices[5*4*NPHI +5 +1] = NPOINTS-2;
349 indices[5*4*NPHI +5 +2] = NPOINTS-1;
350 indices[5*4*NPHI +5 +3] = 1;
351 indices[5*4*NPHI +5 +4] = SO_END_FACE_INDEX;
355 indices[5*4*NPHI +0] = SO_END_FACE_INDEX;
356 indices[5*4*NPHI +1] = SO_END_FACE_INDEX;
357 indices[5*4*NPHI +2] = SO_END_FACE_INDEX;
358 indices[5*4*NPHI +3] = SO_END_FACE_INDEX;
359 indices[5*4*NPHI +4] = SO_END_FACE_INDEX;
361 indices[5*4*NPHI +5 +0] = SO_END_FACE_INDEX;
362 indices[5*4*NPHI +5 +1] = SO_END_FACE_INDEX;
363 indices[5*4*NPHI +5 +2] = SO_END_FACE_INDEX;
364 indices[5*4*NPHI +5 +3] = SO_END_FACE_INDEX;
365 indices[5*4*NPHI +5 +4] = SO_END_FACE_INDEX;
368 DeltaPhi =
pDPhi.getValue()/NPHI, phi =
pSPhi.getValue();
369 for (i = 0; i<=NPHI; i++) {
370 points[2*i+0][0] =
pRMax.getValue()*
FCOS(phi);
371 points[2*i+0][1]=
pRMax.getValue()*
FSIN(phi);
372 points[2*i+0][2] = +
pDz.getValue();
374 points[2*i+1][0] =
pRMax.getValue()*
FCOS(phi);
375 points[2*i+1][1]=
pRMax.getValue()*
FSIN(phi);
376 points[2*i+1][2] = -
pDz.getValue();
378 pp = phi+DeltaPhi/2.0;
380 normals[i][0] =
FCOS(pp);
381 normals[i][1] =
FSIN(pp);
388 for (i = 0; i<=NPHI; i++) {
389 points[2*NPHI+2+2*i+0][0] =
pRMin.getValue()*
FCOS(phi);
390 points[2*NPHI+2+2*i+0][1] =
pRMin.getValue()*
FSIN(phi);
391 points[2*NPHI+2+2*i+0][2] = +
pDz.getValue();
392 points[2*NPHI+2+2*i+1][0] =
pRMin.getValue()*
FCOS(phi);
393 points[2*NPHI+2+2*i+1][1] =
pRMin.getValue()*
FSIN(phi);
394 points[2*NPHI+2+2*i+1][2] = -
pDz.getValue();
395 pp = phi-DeltaPhi/2.0;
397 normals[NPHI+i][0] = -
FCOS(pp);
398 normals[NPHI+i][1] = -
FSIN(pp);
399 normals[NPHI+i][2] = 0;
404 for (i=0;i<NPHI;i++) {
405 normals[2*NPHI+i][0]=normals[2*NPHI+i][1]=0;
406 normals[2*NPHI+i][2]= 1.0;
409 for (i=0;i<NPHI;i++) {
410 normals[3*NPHI+i][0]=normals[3*NPHI+i][1]=0;
411 normals[3*NPHI+i][2]= -1.0;
414 phi =
pSPhi.getValue();
415 normals[4*NPHI+0][0]=
FSIN(phi);
416 normals[4*NPHI+0][1]= -
FCOS(phi);
417 normals[4*NPHI+0][2]=0;
421 normals[4*NPHI+1][0]= -
FSIN(phi);
422 normals[4*NPHI+1][1]= +
FCOS(phi);
423 normals[4*NPHI+1][2]=0;
425 for (
int np=0;np<NPOINTS; np++) theCoordinates->point.set1Value(np,points[np][0],points[np][1],points[np][2]);
426 for (
int ni=0;ni<NINDICES;ni++) theFaceSet->coordIndex.set1Value(ni,indices[ni]);
427 for (
int nf=0;nf<NFACES;nf++) theNormals->vector.set1Value(nf,normals[nf][0],normals[nf][1],normals[nf][2]);
428 theNormalBinding->value=SoNormalBinding::PER_FACE;
440 SoSeparator *sep =
new SoSeparator();
441 SoCoordinate3 *theCoordinates =
new SoCoordinate3();
442 SoNormal *theNormals =
new SoNormal();
443 SoNormalBinding *theNormalBinding =
new SoNormalBinding();
444 SoIndexedFaceSet *theFaceSet =
new SoIndexedFaceSet();
448 sep->addChild(theCoordinates);
449 sep->addChild(theNormals);
450 sep->addChild(theNormalBinding);
451 sep->addChild(theFaceSet);