Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
SoTubs.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27//
28//
29/*-----------------------------HEPVis---------------------------------------*/
30/* */
31/* Node: SoTubs */
32/* Description: Represents the G4Tubs Geant Geometry entity */
33/* Author: Joe Boudreau Nov 11 1996 */
34/* */
35/*--------------------------------------------------------------------------*/
36
37#ifdef G4VIS_BUILD_OI_DRIVER
38
39// this :
40#include "HEPVis/nodes/SoTubs.h"
41
42#include <assert.h>
43#include <cmath>
44
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>
56
57#include "HEPVis/SbMath.h"
58
59// This statement is required
60SO_NODE_SOURCE(SoTubs)
61
62// Constructor
64
65
66 // This statement is required
67 SO_NODE_CONSTRUCTOR(SoTubs);
68
69 // Data fields are initialized like this:
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);
77}
78
79// Destructor
81 delete children;
82}
83
84
85// initClass
87 // This statement is required.
88 static bool first = true;
89 if (first) {
90 first = false;
91 SO_NODE_INIT_CLASS(SoTubs,SoShape,"Shape");
92 }
93}
94
95// generatePrimitives
96void SoTubs::generatePrimitives(SoAction *action) {
97 // This variable is used to store each vertex
98 SoPrimitiveVertex pv;
99
100 // Access the stat from the action
101 SoState *state = action->getState();
102
103 // See if we have to use a texture coordinate function,
104 // rather than generating explicit texture coordinates.
105 SbBool useTexFunction=
106 (SoTextureCoordinateElement::getType(state) ==
107 SoTextureCoordinateElement::FUNCTION);
108
109 // If we need to generate texture coordinates with a function,
110 // we'll need an SoGLTextureCoordinateElement. Otherwise, we'll
111 // set up the coordinates directly.
112 const SoTextureCoordinateElement* tce = NULL;
113 SbVec4f texCoord;
114 if (useTexFunction) {
115 tce = SoTextureCoordinateElement::getInstance(state);
116 }
117 else {
118 texCoord[2] = 0.0;
119 texCoord[3] = 1.0;
120 }
121 SbVec3f point, normal;
122
123
124 ///////////////////////////////////////////////////////
125 //-----------------------------------------------------
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); \
131 } else { \
132 texCoord[0]=(float)(s); \
133 texCoord[1]=(float)(t); \
134 } \
135 pv.setPoint(point); \
136 pv.setNormal(normal); \
137 pv.setTextureCoords(texCoord); \
138 shapeVertex(&pv);
139 //-----------------------------------------------------
140 ///////////////////////////////////////////////////////
141
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);
149 //
150 // The outer surface!
151 //
152 int i;
153 double sinPhi,cosPhi;
154 beginShape(action,TRIANGLE_STRIP);
155 sinPhi=sinPhi0;
156 cosPhi=cosPhi0;
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);
161 }
162 endShape();
163 //
164 // The inner surface!
165 //
166 if(rMin!=0.F) {
167 beginShape(action,TRIANGLE_STRIP);
168 sinPhi=sinPhi0;
169 cosPhi=cosPhi0;
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);
174 }
175 endShape();
176 }
177 if (std::fabs(deltaPhi)<2.0*M_PI) {
178 //
179 // The end
180 //
181 beginShape(action,TRIANGLE_STRIP);
182 sinPhi=sinPhi0;
183 cosPhi=cosPhi0;
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);
188 endShape();
189 //
190 // The other end
191 //
192 beginShape(action,TRIANGLE_STRIP);
193 sinPhi=sinPhi1;
194 cosPhi=cosPhi1;
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);
199 endShape();
200 }
201 //
202 // The outer surface at z=+PDZ
203 //
204 if(rMin==0.F) {
205 beginShape(action,TRIANGLE_FAN);
206 sinPhi=sinPhi0;
207 cosPhi=cosPhi0;
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);
212 }
213 endShape();
214 //
215 // The outer surface at z=-PDZ
216 //
217 beginShape(action,TRIANGLE_FAN);
218 sinPhi=sinPhi0;
219 cosPhi=cosPhi0;
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);
224 }
225 endShape();
226 } else {
227 beginShape(action,TRIANGLE_STRIP);
228 sinPhi=sinPhi0;
229 cosPhi=cosPhi0;
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);
234 }
235 endShape();
236 //
237 // The outer surface at z=-PDZ
238 //
239 beginShape(action,TRIANGLE_STRIP);
240 sinPhi=sinPhi0;
241 cosPhi=cosPhi0;
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);
246 }
247 endShape();
248 }
249}
250
251// getChildren
252SoChildList *SoTubs::getChildren() const {
253 return children;
254}
255
256
257// computeBBox
258void SoTubs::computeBBox(SoAction *, SbBox3f &box, SbVec3f &center ){
259 SbVec3f vmin(-pRMax.getValue(),-pRMax.getValue(),-pDz.getValue()),
260 vmax( pRMax.getValue(), pRMax.getValue(), pDz.getValue());
261 center.setValue(0,0,0);
262 box.setBounds(vmin,vmax);
263}
264
265
266// updateChildren
267void SoTubs::updateChildren() {
268
269 // Redraw the G4Tubs....
270
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));
277
278
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];
281#ifdef INVENTOR2_0
282 static long indices[NINDICES];
283#else
284 static int32_t indices[NINDICES];
285#endif
286
287 static int init=0;
288 double phi, pp, DeltaPhi;
289
290 // Indices need to be generated once! This is here to keep it close to the point
291 // generation, since otherwise it will be confusing.
292
293 int i;
294 if (!init) {
295 init = 1;
296 // Outer face
297 for (i = 0; i< NPHI; i++) {
298 // 0 1 3 2;
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;
304 }
305 // the inner face
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;
312 }
313 // the top side
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;
320 }
321 // the bottom side
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;
328 }
329 // the odd side
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;
335 // aother odd side
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;
341 }
342 // Points need to be generated each time:
343 if (pDPhi.getValue()<2*M_PI) {
344 // the odd side
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;
350 // aother odd side
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;
356 }
357 else {
358 // the odd side
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;
364 // aother odd side
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;
370 }
371 // The outer surface
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();
377
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();
381
382 pp = phi+DeltaPhi/2.0;
383 if (i!=NPHI) {
384 normals[i][0] = FCOS(pp);
385 normals[i][1] = FSIN(pp);
386 normals[i][2] = 0;
387 }
388 phi+=DeltaPhi;
389 }
390 // The inner surface
391 phi = pSPhi.getValue() + pDPhi.getValue();
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;
400 if (i!=NPHI) {
401 normals[NPHI+i][0] = -FCOS(pp);
402 normals[NPHI+i][1] = -FSIN(pp);
403 normals[NPHI+i][2] = 0;
404 }
405 phi-=DeltaPhi;
406 }
407 // The top side
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;
411 }
412 // The bottom side
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;
416 }
417 // The odd side
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;
422
423 // Another odd side
424 phi = pSPhi.getValue()+pDPhi.getValue();
425 normals[4*NPHI+1][0]= -FSIN(phi);
426 normals[4*NPHI+1][1]= +FCOS(phi);
427 normals[4*NPHI+1][2]=0;
428
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;
433}
434
435// generateChildren
436void SoTubs::generateChildren() {
437
438 // This routines creates one SoSeparator, one SoCoordinate3, and
439 // one SoLineSet, and puts it in the child list. This is done only
440 // once, whereas redrawing the position of the coordinates occurs each
441 // time an update is necessary, in the updateChildren routine.
442
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();
449 //
450 // This line costs some in render quality! but gives speed.
451 //
452 sep->addChild(theCoordinates);
453 sep->addChild(theNormals);
454 sep->addChild(theNormalBinding);
455 sep->addChild(theFaceSet);
456 children->append(sep);
457}
458
459// generateAlternateRep
461
462 // This routine sets the alternate representation to the child
463 // list of this mode.
464
465 if (children->getLength() == 0) generateChildren();
466 updateChildren();
467 alternateRep.setValue((SoSeparator *) ( *children)[0]);
468}
469
470// clearAlternateRep
472 alternateRep.setValue(NULL);
473}
474
475#endif
#define FCOS(x)
Definition: SbMath.h:40
#define FSIN(x)
Definition: SbMath.h:41
#define M_PI
Definition: SbMath.h:33
Definition: SoTubs.h:71
virtual ~SoTubs()
Destructor, required.
virtual SoChildList * getChildren() const
GetChildList, required whenever the class has hidden children.
virtual void computeBBox(SoAction *action, SbBox3f &box, SbVec3f &center)
compute bounding Box, required
virtual void generateAlternateRep()
SoSFFloat pDz
Half-length in Z.
Definition: SoTubs.h:89
SoSFNode alternateRep
Alternate rep - required.
Definition: SoTubs.h:101
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.
Definition: SoTubs.h:97
SoSFFloat pSPhi
Starting angle, in radians.
Definition: SoTubs.h:93
SoTubs()
Constructor, required.
SoSFFloat pRMin
Inside radius of the tube.
Definition: SoTubs.h:81
SoSFFloat pRMax
Outside radius of the tube.
Definition: SoTubs.h:85