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