Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
SoCons.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: SoCons */
32/* Description: Represents the G4Cons 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/SoCons.h"
41
42#include <assert.h>
43#include <cmath>
44#include <Inventor/SbBox.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>
55
56#include "HEPVis/SbMath.h"
57
58// This statement is required
59SO_NODE_SOURCE(SoCons)
60
61// Constructor
63 // This statement is required
64 SO_NODE_CONSTRUCTOR(SoCons);
65
66 // Data fields are initialized like this:
67 SO_NODE_ADD_FIELD(fRmin1, (0.0));
68 SO_NODE_ADD_FIELD(fRmin2, (0.0));
69 SO_NODE_ADD_FIELD(fRmax1, (1.0));
70 SO_NODE_ADD_FIELD(fRmax2, (1.0));
71 SO_NODE_ADD_FIELD(fDz, (10.0));
72 SO_NODE_ADD_FIELD(fSPhi, (0.0));
73 SO_NODE_ADD_FIELD(fDPhi, ((float)(2*M_PI)));
74 SO_NODE_ADD_FIELD(smoothDraw, (TRUE));
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(SoCons,SoShape,"Shape");
92 }
93}
94
95
96// generatePrimitives
97void SoCons::generatePrimitives(SoAction *action) {
98 // This variable is used to store each vertex
99 SoPrimitiveVertex pv;
100
101 // Access the stat from the action
102 SoState *state = action->getState();
103
104 // See if we have to use a texture coordinate function,
105 // rather than generating explicit texture coordinates.
106 SbBool useTexFunction=
107 (SoTextureCoordinateElement::getType(state) ==
108 SoTextureCoordinateElement::FUNCTION);
109
110 // If we need to generate texture coordinates with a function,
111 // we'll need an SoGLTextureCoordinateElement. Otherwise, we'll
112 // set up the coordinates directly.
113 const SoTextureCoordinateElement *tce = NULL;
114 SbVec4f texCoord;
115 if (useTexFunction) {
116 tce = SoTextureCoordinateElement::getInstance(state);
117 } else {
118 texCoord[2] = 0.0;
119 texCoord[3] = 1.0;
120 }
121 SbVec3f point, normal;
122
123 ///////////////////////////////////////////////////////
124 //-----------------------------------------------------
125#define GEN_VERTEX(pv,x,y,z,s,t,nx,ny,nz) \
126 point.setValue((float)(x),(float)(y),(float)(z)); \
127 normal.setValue((float)(nx),(float)(ny),(float)(nz)); \
128 if (useTexFunction) { \
129 texCoord=tce->get(point,normal); \
130 } else { \
131 texCoord[0]=(float)(s); \
132 texCoord[1]=(float)(t); \
133 } \
134 pv.setPoint(point); \
135 pv.setNormal(normal); \
136 pv.setTextureCoords(texCoord); \
137 shapeVertex(&pv);
138 //-----------------------------------------------------
139 ///////////////////////////////////////////////////////
140
141
142 int NPHI = (int)(2+22*std::fabs(fDPhi.getValue()/(2.0*M_PI)));
143 double deltaPhi = fDPhi.getValue()/NPHI;
144 double phi0 = fSPhi.getValue();
145 double phi1 = phi0 + fDPhi.getValue();
146 double rMax1 = fRmax1.getValue();
147 double rMin1 = fRmin1.getValue();
148 double rMax2 = fRmax2.getValue();
149 double rMin2 = fRmin2.getValue();
150 double zMax = fDz.getValue();
151 double zMin = -zMax;
152 double cosPhi0 = std::cos(phi0);
153 double sinPhi0 = std::sin(phi0);
154 double cosPhi1 = std::cos(phi1);
155 double sinPhi1 = std::sin(phi1);
156 double cosDeltaPhi = std::cos(deltaPhi);
157 double sinDeltaPhi = std::sin(deltaPhi);
158 //
159 // The outer surface!
160 //
161 beginShape(action,TRIANGLE_STRIP);
162 int i;
163 double sinPhi=sinPhi0;
164 double cosPhi=cosPhi0;
165 for (i = 0; i<=NPHI; i++) {
166 GEN_VERTEX(pv,rMax2*cosPhi,rMax2*sinPhi,zMax,0.0,0.0,cosPhi,sinPhi,0);
167 GEN_VERTEX(pv,rMax1*cosPhi,rMax1*sinPhi,zMin,1.0,1.0,cosPhi,sinPhi,0);
168 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
169 }
170 endShape();
171 //
172 // The inner surface!
173 //
174 beginShape(action,TRIANGLE_STRIP);
175 sinPhi=sinPhi0;
176 cosPhi=cosPhi0;
177 for (i = 0; i<=NPHI; i++) {
178 GEN_VERTEX(pv,rMin2*cosPhi,rMin2*sinPhi,zMax,0.0,0.0,-cosPhi,-sinPhi,0);
179 GEN_VERTEX(pv,rMin1*cosPhi,rMin1*sinPhi,zMin,1.0,1.0,-cosPhi,-sinPhi,0);
180 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
181 }
182 endShape();
183 if (std::fabs(deltaPhi)<2.0*M_PI) {
184 //
185 // The end
186 //
187 beginShape(action,TRIANGLE_STRIP);
188 sinPhi=sinPhi0;
189 cosPhi=cosPhi0;
190 GEN_VERTEX(pv,rMax2*cosPhi,rMax2*sinPhi,zMax,0.0,0.0,sinPhi,-cosPhi,0);
191 GEN_VERTEX(pv,rMax1*cosPhi,rMax1*sinPhi,zMin,1.0,1.0,sinPhi,-cosPhi,0);
192 GEN_VERTEX(pv,rMin2*cosPhi,rMin2*sinPhi,zMax,1.0,0.0,sinPhi,-cosPhi,0);
193 GEN_VERTEX(pv,rMin1*cosPhi,rMin1*sinPhi,zMin,0.0,1.0,sinPhi,-cosPhi,0);
194 endShape();
195 //
196 // The other end
197 //
198 beginShape(action,TRIANGLE_STRIP);
199 sinPhi=sinPhi1;
200 cosPhi=cosPhi1;
201 GEN_VERTEX(pv,rMax2*cosPhi,rMax2*sinPhi, zMax,0.0,0.0,-sinPhi,+cosPhi,0);
202 GEN_VERTEX(pv,rMax1*cosPhi,rMax1*sinPhi, zMin,1.0,1.0,-sinPhi,+cosPhi,0);
203 GEN_VERTEX(pv,rMin2*cosPhi,rMin2*sinPhi, zMax,1.0,0.0,-sinPhi,+cosPhi,0);
204 GEN_VERTEX(pv,rMin1*cosPhi,rMin1*sinPhi, zMin,0.0,1.0,-sinPhi,+cosPhi,0);
205 endShape();
206 }
207
208 //
209 // The outer surface at z=+PDZ
210 //
211 beginShape(action,TRIANGLE_STRIP);
212 sinPhi=sinPhi0;
213 cosPhi=cosPhi0;
214 for (i = 0; i<=NPHI; i++) {
215 GEN_VERTEX(pv,rMin2*cosPhi,rMin2*sinPhi,zMax,0.0,0.0,0,0,1);
216 GEN_VERTEX(pv,rMax2*cosPhi,rMax2*sinPhi,zMax,1.0,1.0,0,0,1);
217 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
218 }
219 endShape();
220 //
221 // The outer surface at z=-PDZ
222 //
223 beginShape(action,TRIANGLE_STRIP);
224 sinPhi=sinPhi0;
225 cosPhi=cosPhi0;
226 for (i = 0; i<=NPHI; i++) {
227 GEN_VERTEX(pv,rMin1*cosPhi,rMin1*sinPhi,zMin,0.0,0.0,0,0,-1);
228 GEN_VERTEX(pv,rMax1*cosPhi,rMax1*sinPhi,zMin,1.0,1.0,0,0,-1);
229 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
230 }
231 endShape();
232
233}
234
235// getChildren
236SoChildList *SoCons::getChildren() const {
237 return children;
238}
239
240
241// computeBBox
242void SoCons::computeBBox(SoAction *, SbBox3f &box, SbVec3f &center ){
243 float fRmax= fRmax1.getValue();
244 if (fRmax2.getValue() > fRmax) fRmax = fRmax2.getValue();
245
246 SbVec3f vmin(-fRmax,-fRmax,-fDz.getValue()),
247 vmax( fRmax, fRmax, fDz.getValue());
248 center.setValue(0,0,0);
249 box.setBounds(vmin,vmax);
250}
251
252
253
254
255// updateChildren
256void SoCons::updateChildren() {
257
258
259 // Redraw the G4Cons....
260
261 assert(children->getLength()==1);
262 SoSeparator *sep = (SoSeparator *) ( *children)[0];
263 SoCoordinate3 *theCoordinates = (SoCoordinate3 *) ( sep->getChild(0));
264 SoNormal *theNormals = (SoNormal *) ( sep->getChild(1));
265 SoNormalBinding *theNormalBinding = (SoNormalBinding *) ( sep->getChild(2));
266 SoIndexedFaceSet *theFaceSet = (SoIndexedFaceSet *) ( sep->getChild(3));
267
268 const int NPHI=24, NPOINTS=2*(2*NPHI+2), NFACES=4*NPHI+2, NINDICES = NFACES*5;
269 float points[NPOINTS][3], normals[NFACES][3];
270#ifdef INVENTOR2_0
271 static long indices[NINDICES];
272#else
273 static int32_t indices[NINDICES];
274#endif
275 static int init=0;
276 double phi, pp, DeltaPhi;
277
278 // Indices need to be generated once! This is here to keep it close to the point
279 // generation, since otherwise it will be confusing.
280
281 int i;
282 if (!init) {
283 init = 1;
284 // Outer face
285 for (i = 0; i< NPHI; i++) {
286 // 0 1 3 2;
287 indices[5*i+0] = 2*i+0;
288 indices[5*i+1] = 2*i+1;
289 indices[5*i+2] = 2*i+3;
290 indices[5*i+3] = 2*i+2;
291 indices[5*i+4] = SO_END_FACE_INDEX;
292 }
293 // the inner face
294 for (i=0;i<NPHI;i++) {
295 indices[5*1*NPHI + 5*i+0] = 2*NPHI+2 + 2*i+0;
296 indices[5*1*NPHI + 5*i+1] = 2*NPHI+2 + 2*i+1;
297 indices[5*1*NPHI + 5*i+2] = 2*NPHI+2 + 2*i+3;
298 indices[5*1*NPHI + 5*i+3] = 2*NPHI+2 + 2*i+2;
299 indices[5*1*NPHI + 5*i+4] = SO_END_FACE_INDEX;
300 }
301 // the top side
302 for (i=0;i<NPHI;i++) {
303 indices[5*2*NPHI + 5*i+0] = 2*i+0;
304 indices[5*2*NPHI + 5*i+1] = 2*i+2;
305 indices[5*2*NPHI + 5*i+2] = NPOINTS - (2*i+4);
306 indices[5*2*NPHI + 5*i+3] = NPOINTS - (2*i+2);
307 indices[5*2*NPHI + 5*i+4] = SO_END_FACE_INDEX;
308 }
309 // the bottom side
310 for (i=0;i<NPHI;i++) {
311 indices[5*3*NPHI + 5*i+0] = 2*i+1;
312 indices[5*3*NPHI + 5*i+1] = NPOINTS - (2*i+1);
313 indices[5*3*NPHI + 5*i+2] = NPOINTS - (2*i+3);
314 indices[5*3*NPHI + 5*i+3] = 2*i+3;
315 indices[5*3*NPHI + 5*i+4] = SO_END_FACE_INDEX;
316 }
317 // the odd side
318 indices[5*4*NPHI +0] = 2*NPHI;
319 indices[5*4*NPHI +1] = 2*NPHI+1;
320 indices[5*4*NPHI +2] = 2*NPHI+3;
321 indices[5*4*NPHI +3] = 2*NPHI+2;
322 indices[5*4*NPHI +4] = SO_END_FACE_INDEX;
323 // aother odd side
324 indices[5*4*NPHI +5 +0] = 0;
325 indices[5*4*NPHI +5 +1] = NPOINTS-2;
326 indices[5*4*NPHI +5 +2] = NPOINTS-1;
327 indices[5*4*NPHI +5 +3] = 1;
328 indices[5*4*NPHI +5 +4] = SO_END_FACE_INDEX;
329 }
330 // Points need to be generated each time:
331 // The outer surface
332 DeltaPhi = fDPhi.getValue()/NPHI, phi = fSPhi.getValue();
333 float t,st,ct;
334 t = FATAN((fRmax2.getValue()-fRmax1.getValue())/(2*fDz.getValue()));
335 st = FSIN(t);
336 ct = FCOS(t);
337 for (i = 0; i<=NPHI; i++) {
338 points[2*i+0][0] = fRmax2.getValue()*FCOS(phi);
339 points[2*i+0][1] = fRmax2.getValue()*FSIN(phi);
340 points[2*i+0][2] = +fDz.getValue();
341 points[2*i+1][0] = fRmax1.getValue()*FCOS(phi);
342 points[2*i+1][1] = fRmax1.getValue()*FSIN(phi);
343 points[2*i+1][2] = -fDz.getValue();
344 pp = phi+DeltaPhi/2.0;
345 if (i!=NPHI) {
346 normals[i][0] = ct * FCOS(pp);
347 normals[i][1] = ct * FSIN(pp);
348 normals[i][2] = -st;
349 }
350 phi+=DeltaPhi;
351 }
352 // The inner surface
353 phi = fSPhi.getValue() + fDPhi.getValue();
354 t = FATAN((fRmin2.getValue()-fRmin1.getValue())/(2*fDz.getValue()));
355 st = FSIN(t);
356 ct = FCOS(t);
357 for (i = 0; i<=NPHI; i++) {
358 points[2*NPHI+2+2*i+0][0] = fRmin2.getValue()*FCOS(phi);
359 points[2*NPHI+2+2*i+0][1] = fRmin2.getValue()*FSIN(phi);
360 points[2*NPHI+2+2*i+0][2] = +fDz.getValue();
361 points[2*NPHI+2+2*i+1][0] = fRmin1.getValue()*FCOS(phi);
362 points[2*NPHI+2+2*i+1][1] = fRmin1.getValue()*FSIN(phi);
363 points[2*NPHI+2+2*i+1][2] = -fDz.getValue();
364 pp = phi-DeltaPhi/2.0;
365 if (i!=NPHI) {
366 normals[NPHI+i][0] = -ct*FCOS(pp);
367 normals[NPHI+i][1] = -ct*FSIN(pp);
368 normals[NPHI+i][2] = st;
369 }
370 phi-=DeltaPhi;
371 }
372 // The top side
373 for (i=0;i<NPHI;i++) {
374 normals[2*NPHI+i][0]=normals[2*NPHI+i][1]=0;
375 normals[2*NPHI+i][2]= 1.0;
376 }
377 // The bottom side
378 for (i=0;i<NPHI;i++) {
379 normals[3*NPHI+i][0]=normals[3*NPHI+i][1]=0;
380 normals[3*NPHI+i][2]= -1.0;
381 }
382 // The odd side
383 phi = fSPhi.getValue();
384 normals[4*NPHI+0][0]= FSIN(phi);
385 normals[4*NPHI+0][1]= -FCOS(phi);
386 normals[4*NPHI+0][2]= 0;
387
388 // Another odd side
389 phi = fSPhi.getValue()+fDPhi.getValue();
390 normals[4*NPHI+1][0]= -FSIN(phi);
391 normals[4*NPHI+1][1]= +FCOS(phi);
392 normals[4*NPHI+1][2]=0;
393
394 for (int np=0;np<NPOINTS;np++) theCoordinates->point.set1Value(np,points[np][0],points[np][1],points[np][2]);
395 theFaceSet->coordIndex.setValues(0,NINDICES,indices);
396 if (smoothDraw.getValue()) {
397 // This Line is replaced by the next one because of an apparent Bug in Inventor (mem. leak).
398 // theNormals->vector.deleteValues(0);
399 for (int nf=0;nf<NFACES;nf++) theNormals->vector.set1Value(nf,normals[nf][0],normals[nf][1],normals[nf][2]);
400 theNormalBinding->value=SoNormalBinding::PER_FACE;
401 }
402 else {
403 for (int nf=0;nf<NFACES;nf++) theNormals->vector.set1Value(nf,normals[nf][0],normals[nf][1],normals[nf][2]);
404 theNormalBinding->value=SoNormalBinding::PER_FACE;
405 }
406}
407
408// generateChildren
409void SoCons::generateChildren() {
410
411 // This routines creates one SoSeparator, one SoCoordinate3, and
412 // one SoLineSet, and puts it in the child list. This is done only
413 // once, whereas redrawing the position of the coordinates occurs each
414 // time an update is necessary, in the updateChildren routine.
415
416 assert(children->getLength() ==0);
417 SoSeparator *sep = new SoSeparator();
418 SoCoordinate3 *theCoordinates = new SoCoordinate3();
419 SoNormal *theNormals = new SoNormal();
420 SoNormalBinding *theNormalBinding = new SoNormalBinding();
421 SoIndexedFaceSet *theFaceSet = new SoIndexedFaceSet();
422 //
423 // This line costs some in render quality! but gives speed.
424 //
425 sep->addChild(theCoordinates);
426 sep->addChild(theNormals);
427 sep->addChild(theNormalBinding);
428 sep->addChild(theFaceSet);
429 children->append(sep);
430}
431
432// generateAlternateRep
434
435 // This routine sets the alternate representation to the child
436 // list of this mode.
437
438 if (children->getLength() == 0) generateChildren();
439 updateChildren();
440 alternateRep.setValue((SoSeparator *) ( *children)[0]);
441}
442
443// clearAlternateRep
445 alternateRep.setValue(NULL);
446}
447
448#endif
#define TRUE
Definition: Globals.hh:27
#define FCOS(x)
Definition: SbMath.h:40
#define FSIN(x)
Definition: SbMath.h:41
#define FATAN(x)
Definition: SbMath.h:45
#define M_PI
Definition: SbMath.h:33
Definition: SoCons.h:71
SoCons()
Constructor, required.
SoSFFloat fDPhi
Delta-angle, in radians.
Definition: SoCons.h:105
virtual SoChildList * getChildren() const
GetChildList, required whenever the class has hidden children.
SoSFFloat fSPhi
Starting angle, in radians.
Definition: SoCons.h:101
SoSFFloat fRmax2
Outside radius at +fDz.
Definition: SoCons.h:93
static void initClass()
Class Initializer, required.
SoSFFloat fRmax1
Outside radius at -fDz.
Definition: SoCons.h:89
SoSFFloat fRmin1
Inside radius at -fDz.
Definition: SoCons.h:81
SoSFFloat fRmin2
Inside radius at +fDz.
Definition: SoCons.h:85
SoSFNode alternateRep
Alternate rep required - for use by users without HEPVis shared objects.
Definition: SoCons.h:113
SoSFFloat fDz
Half-length along Z.
Definition: SoCons.h:97
virtual void generatePrimitives(SoAction *action)
Generate Primitives, required.
virtual void generateAlternateRep()
virtual ~SoCons()
Destructor, required.
SoSFBool smoothDraw
An Inventor option - slightly better render, worse performance.
Definition: SoCons.h:109
virtual void clearAlternateRep()
We better be able to clear it, too!
virtual void computeBBox(SoAction *action, SbBox3f &box, SbVec3f &center)
compute bounding Box, required