Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
SoTrap.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: SoTrap */
32/* Description: Represents the G4Trap Geant Geometry entity */
33/* Author: Joe Boudreau Nov 11 1996 */
34/* */
35/* */
36/*---------------------------------------------------------------------------*/
37
38#ifdef G4VIS_BUILD_OI_DRIVER
39
40// this :
41#include "HEPVis/nodes/SoTrap.h"
42
43#include <assert.h>
44#include <cmath>
45#include <Inventor/SbBox.h>
46#include <Inventor/actions/SoGLRenderAction.h>
47#include <Inventor/actions/SoAction.h>
48#include <Inventor/fields/SoSFFloat.h>
49#include <Inventor/misc/SoChildList.h>
50#include <Inventor/nodes/SoSeparator.h>
51#include <Inventor/nodes/SoIndexedFaceSet.h>
52#include <Inventor/nodes/SoNormal.h>
53#include <Inventor/nodes/SoCoordinate3.h>
54#include <Inventor/nodes/SoNormalBinding.h>
55#include <Inventor/SoPrimitiveVertex.h>
56#include <Inventor/elements/SoTextureCoordinateElement.h>
57
58#include "HEPVis/SbMath.h"
59
60// This statement is required
61SO_NODE_SOURCE(SoTrap)
62
63// Constructor
65 // This statement is required
66 SO_NODE_CONSTRUCTOR(SoTrap);
67
68 // Data fields are initialized like this:
69 SO_NODE_ADD_FIELD(pDz, (1.0));
70 SO_NODE_ADD_FIELD(pTheta, (0.0));
71 SO_NODE_ADD_FIELD(pPhi, (0.0));
72 SO_NODE_ADD_FIELD(pDy1, (1.0));
73 SO_NODE_ADD_FIELD(pDx1, (1.0));
74 SO_NODE_ADD_FIELD(pDx2, (1.0));
75 SO_NODE_ADD_FIELD(pDy2, (1.0));
76 SO_NODE_ADD_FIELD(pDx3, (1.0));
77 SO_NODE_ADD_FIELD(pDx4, (1.0));
78 SO_NODE_ADD_FIELD(pAlp1, (0.0));
79 SO_NODE_ADD_FIELD(pAlp2, (0.0));
80 SO_NODE_ADD_FIELD(alternateRep, (NULL));
81 children = new SoChildList(this);
82}
83
84// Destructor
86 delete children;
87}
88
89
90// initClass
92 // This statement is required.
93 static bool first = true;
94 if (first) {
95 first = false;
96 SO_NODE_INIT_CLASS(SoTrap,SoShape,"Shape");
97 }
98}
99
100
101// generatePrimitives
102void SoTrap::generatePrimitives(SoAction *action) {
103 // This variable is used to store each vertex
104 SoPrimitiveVertex pv;
105
106 // Access the stat from the action
107 SoState *state = action->getState();
108
109 // See if we have to use a texture coordinate function,
110 // rather than generating explicit texture coordinates.
111 SbBool useTexFunction=
112 (SoTextureCoordinateElement::getType(state) ==
113 SoTextureCoordinateElement::FUNCTION);
114
115 // If we need to generate texture coordinates with a function,
116 // we'll need an SoGLTextureCoordinateElement. Otherwise, we'll
117 // set up the coordinates directly.
118 const SoTextureCoordinateElement *tce = NULL;
119 SbVec4f texCoord;
120 if (useTexFunction) {
121 tce = SoTextureCoordinateElement::getInstance(state);
122 }
123 else {
124 texCoord[2] = 0.0;
125 texCoord[3] = 1.0;
126 }
127 SbVec3f point, normal;
128
129
130 //////////////////////////////////////////
131 //----------------------------------------
132#define GEN_VERTEX(pv,x,y,z,s,t,nx,ny,nz) \
133 point.setValue(x,y,z); \
134 normal.setValue(nx,ny,nz); \
135 if (useTexFunction) { \
136 texCoord=tce->get(point,normal); \
137 } \
138 else { \
139 texCoord[0]=s; \
140 texCoord[1]=t; \
141 } \
142 pv.setPoint(point); \
143 pv.setNormal(normal); \
144 pv.setTextureCoords(texCoord); \
145 shapeVertex(&pv);
146 //----------------------------------------
147 //////////////////////////////////////////
148
149 const int NPOINTS=8, NFACES=6, NINDICES = NFACES*5;
150 int indices[NINDICES] = {3,2,1,0, SO_END_FACE_INDEX, //z back.
151 4,5,6,7, SO_END_FACE_INDEX, //z front.
152 0,1,5,4, SO_END_FACE_INDEX, //y up.
153 1,2,6,5, SO_END_FACE_INDEX, //x left.
154 2,3,7,6, SO_END_FACE_INDEX, //y down.
155 3,0,4,7, SO_END_FACE_INDEX}; //x right.
156
157 // points for the eight vertices
158 float TthetaCphi = FTAN(pTheta.getValue())*FCOS(pPhi.getValue());
159 float TthetaSphi = FTAN(pTheta.getValue())*FSIN(pPhi.getValue());
160 float Talp1 = FTAN(pAlp1.getValue());
161 float Talp2 = FTAN(pAlp2.getValue());
162
163 float points[NPOINTS][3];
164 points[0][0] = pDx2.getValue()+pDy1.getValue()*Talp1;
165 points[0][1] = pDy1.getValue();
166 points[0][2] = -pDz.getValue();
167
168 points[1][0] = -pDx2.getValue()+pDy1.getValue()*Talp1;
169 points[1][1] = pDy1.getValue();
170 points[1][2] = -pDz.getValue();
171
172 points[2][0] = -pDx1.getValue()-pDy1.getValue()*Talp1;
173 points[2][1] = -pDy1.getValue();
174 points[2][2] = -pDz.getValue();
175
176 points[3][0] = pDx1.getValue()-pDy1.getValue()*Talp1;
177 points[3][1] = -pDy1.getValue();
178 points[3][2] = -pDz.getValue();
179
180 points[4][0] = pDx4.getValue()+pDy2.getValue()*Talp2;
181 points[4][1] = pDy2.getValue();
182 points[4][2] = pDz.getValue();
183
184 points[5][0] = -pDx4.getValue()+pDy2.getValue()*Talp2;
185 points[5][1] = pDy2.getValue();
186 points[5][2] = pDz.getValue();
187
188 points[6][0] = -pDx3.getValue()-pDy2.getValue()*Talp2;
189 points[6][1] = -pDy2.getValue();
190 points[6][2] = pDz.getValue();
191
192 points[7][0] = pDx3.getValue()-pDy2.getValue()*Talp2;
193 points[7][1] = -pDy2.getValue();
194 points[7][2] = pDz.getValue();
195
196 int i;
197 for (i=0;i<4;i++) {
198 points[i][0] -= pDz.getValue()*TthetaCphi;
199 points[i][1] -= pDz.getValue()*TthetaSphi;
200 }
201 for (i=4;i<8;i++) {
202 points[i][0] += pDz.getValue()*TthetaCphi;
203 points[i][1] += pDz.getValue()*TthetaSphi;
204 }
205
206 SbVec3f normals[NFACES];
207 int nf;
208 for (nf=0;nf<NFACES;nf++) {
209 int j0 = indices[5*nf + 0];
210 int j1 = indices[5*nf + 1];
211 int j2 = indices[5*nf + 2];
212 SbVec3f p0(points[j0][0],points[j0][1],points[j0][2]);
213 SbVec3f p1(points[j1][0],points[j1][1],points[j1][2]);
214 SbVec3f p2(points[j2][0],points[j2][1],points[j2][2]);
215 normals[nf] = (p1-p0).cross(p2-p0);
216 normals[nf].normalize();
217 }
218
219 float x,y,z;
220 int index;
221 for (nf=0;nf<NFACES;nf++) {
222 beginShape(action,TRIANGLE_FAN);
223 index = indices[nf * 5];
224 x = points[index][0];
225 y = points[index][1];
226 z = points[index][2];
227 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
228 index = indices[nf * 5 + 1];
229 x = points[index][0];
230 y = points[index][1];
231 z = points[index][2];
232 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
233 index = indices[nf * 5 + 2];
234 x = points[index][0];
235 y = points[index][1];
236 z = points[index][2];
237 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
238 index = indices[nf * 5 + 3];
239 x = points[index][0];
240 y = points[index][1];
241 z = points[index][2];
242 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
243 endShape();
244 }
245}
246
247// getChildren
248SoChildList *SoTrap::getChildren() const {
249 return children;
250}
251
252
253// computeBBox
254void SoTrap::computeBBox(SoAction *, SbBox3f &box, SbVec3f &center ){
255 float pDx= pDx1.getValue(),pDy=pDy1.getValue();
256
257 if (pDx2.getValue() > pDx) pDx = pDx2.getValue();
258 if (pDx3.getValue() > pDx) pDx = pDx3.getValue();
259 if (pDx4.getValue() > pDx) pDx = pDx4.getValue();
260 if (pDy2.getValue() > pDy) pDy = pDy2.getValue();
261 float TthetaCphi = FTAN(pTheta.getValue())*FCOS(pPhi.getValue());
262 float TthetaSphi = FTAN(pTheta.getValue())*FSIN(pPhi.getValue());
263 float Xalp = FFABS(std::tan(pAlp1.getValue())*pDy1.getValue());
264 float Xalp2 = FFABS(std::tan(pAlp2.getValue())*pDy2.getValue());
265 if (Xalp< Xalp2) Xalp=Xalp2;
266 pDx += FFABS(TthetaCphi*pDz.getValue());
267 pDx += Xalp;
268 pDy += FFABS(TthetaSphi*pDz.getValue());
269
270
271 center.setValue(0,0,0);
272 box.setBounds(SbVec3f(-pDx,-pDy,-pDz.getValue()),
273 SbVec3f( pDx, pDy, pDz.getValue()));
274}
275
276
277
278
279// updateChildren
280void SoTrap::updateChildren() {
281
282
283 // Redraw the G4Trap....
284
285 assert(children->getLength()==1);
286 SoSeparator *sep = (SoSeparator *) ( *children)[0];
287 SoCoordinate3 *theCoordinates = (SoCoordinate3 *) ( sep->getChild(0));
288 SoNormal *theNormals = (SoNormal *) ( sep->getChild(1));
289 SoNormalBinding *theNormalBinding = (SoNormalBinding *) ( sep->getChild(2));
290 SoIndexedFaceSet *theFaceSet = (SoIndexedFaceSet *) ( sep->getChild(3));
291
292 const int NPOINTS=8, NFACES=6, NINDICES = NFACES*5;
293 float points[NPOINTS][3];
294 // Indices for the eight faces
295#ifdef INVENTOR2_0
296 static long
297#else
298 static int32_t
299#endif
300 indices[NINDICES] = {3,2,1,0, SO_END_FACE_INDEX, // bottom
301 4,5,6,7, SO_END_FACE_INDEX, // top
302 0,1,5,4, SO_END_FACE_INDEX,
303 1,2,6,5, SO_END_FACE_INDEX,
304 2,3,7,6, SO_END_FACE_INDEX,
305 3,0,4,7, SO_END_FACE_INDEX};
306
307
308 // points for the eight vertices
309 float TthetaCphi = FTAN(pTheta.getValue())*FCOS(pPhi.getValue());
310 float TthetaSphi = FTAN(pTheta.getValue())*FSIN(pPhi.getValue());
311 float Talp1 = FTAN(pAlp1.getValue());
312 float Talp2 = FTAN(pAlp2.getValue());
313
314 points[0][0] = pDx2.getValue()+pDy1.getValue()*Talp1;
315 points[0][1] = pDy1.getValue();
316 points[0][2] = -pDz.getValue();
317
318 points[1][0] = -pDx2.getValue()+pDy1.getValue()*Talp1;
319 points[1][1] = pDy1.getValue();
320 points[1][2] = -pDz.getValue();
321
322 points[2][0] = -pDx1.getValue()-pDy1.getValue()*Talp1;
323 points[2][1] = -pDy1.getValue();
324 points[2][2] = -pDz.getValue();
325
326 points[3][0] = pDx1.getValue()-pDy1.getValue()*Talp1;
327 points[3][1] = -pDy1.getValue();
328 points[3][2] = -pDz.getValue();
329
330 points[4][0] = pDx4.getValue()+pDy2.getValue()*Talp2;
331 points[4][1] = pDy2.getValue();
332 points[4][2] = pDz.getValue();
333
334 points[5][0] = -pDx4.getValue()+pDy2.getValue()*Talp2;
335 points[5][1] = pDy2.getValue();
336 points[5][2] = pDz.getValue();
337
338 points[6][0] = -pDx3.getValue()-pDy2.getValue()*Talp2;
339 points[6][1] = -pDy2.getValue();
340 points[6][2] = pDz.getValue();
341
342 points[7][0] = pDx3.getValue()-pDy2.getValue()*Talp2;
343 points[7][1] = -pDy2.getValue();
344 points[7][2] = pDz.getValue();
345
346 int i;
347 for (i=0;i<4;i++) {
348 points[i][0] -= pDz.getValue()*TthetaCphi;
349 points[i][1] -= pDz.getValue()*TthetaSphi;
350 }
351 for (i=4;i<8;i++) {
352 points[i][0] += pDz.getValue()*TthetaCphi;
353 points[i][1] += pDz.getValue()*TthetaSphi;
354 }
355
356 for (int np=0;np<NPOINTS;np++) theCoordinates->point.set1Value(np,points[np][0],points[np][1],points[np][2]);
357 theFaceSet->coordIndex.setValues(0,NINDICES,indices);
358 theNormals->vector.deleteValues(0);
359 theNormals->vector.insertSpace(0,6);
360 for (int n=0;n<6;n++) {
361 int i0 = 5*n+0,i1=5*n+1,i2=5*n+2;
362 int j0 = theFaceSet->coordIndex[i0];
363 int j1 = theFaceSet->coordIndex[i1];
364 int j2 = theFaceSet->coordIndex[i2];
365 SbVec3f p0= theCoordinates->point[j0];
366 SbVec3f p1= theCoordinates->point[j1];
367 SbVec3f p2= theCoordinates->point[j2];
368 SbVec3f normal = (p1-p0).cross(p2-p0);
369 normal.normalize();
370 theNormals->vector.set1Value(n,normal);
371 }
372 theNormalBinding->value=SoNormalBinding::PER_FACE;
373}
374
375// generateChildren
376void SoTrap::generateChildren() {
377
378 // This routines creates one SoSeparator, one SoCoordinate3, and
379 // one SoLineSet, and puts it in the child list. This is done only
380 // once, whereas redrawing the position of the coordinates occurs each
381 // time an update is necessary, in the updateChildren routine.
382
383 assert(children->getLength() ==0);
384 SoSeparator *sep = new SoSeparator();
385 SoCoordinate3 *theCoordinates = new SoCoordinate3();
386 SoNormal *theNormals = new SoNormal();
387 SoNormalBinding *theNormalBinding = new SoNormalBinding();
388 SoIndexedFaceSet *theFaceSet = new SoIndexedFaceSet();
389 //
390 // This line costs some in render quality! but gives speed.
391 //
392 sep->addChild(theCoordinates);
393 sep->addChild(theNormals);
394 sep->addChild(theNormalBinding);
395 sep->addChild(theFaceSet);
396 children->append(sep);
397}
398
399// generateAlternateRep
401
402 // This routine sets the alternate representation to the child
403 // list of this mode.
404
405 if (children->getLength() == 0) generateChildren();
406 updateChildren();
407 alternateRep.setValue((SoSeparator *) ( *children)[0]);
408}
409
410// clearAlternateRep
412 alternateRep.setValue(NULL);
413}
414
415#endif
#define FFABS(x)
Definition: SbMath.h:51
#define FCOS(x)
Definition: SbMath.h:40
#define FSIN(x)
Definition: SbMath.h:41
#define FTAN(x)
Definition: SbMath.h:44
Definition: SoTrap.h:81
SoSFFloat pDx2
Half-length along x of the side at y=+pDy1 of the face at -pDz.
Definition: SoTrap.h:118
SoSFFloat pDz
half-length along Z
Definition: SoTrap.h:97
SoSFFloat pAlp2
Definition: SoTrap.h:140
SoSFFloat pDx4
Half-length along x of the side at y=+pDy2 of the face at +pDz.
Definition: SoTrap.h:130
SoSFFloat pDy1
Half-length along y of the face at -pDz.
Definition: SoTrap.h:110
virtual void computeBBox(SoAction *action, SbBox3f &box, SbVec3f &center)
compute bounding Box, required
SoSFFloat pAlp1
Definition: SoTrap.h:135
static void initClass()
Class Initializer, required.
virtual void generatePrimitives(SoAction *action)
Generate Primitives, required.
SoTrap()
Constructor, required.
SoSFFloat pTheta
Polar angle of the line joining the centres of the faces at -/+pDz.
Definition: SoTrap.h:101
SoSFFloat pDx3
Half-length along x of the side at y=-pDy2 of the face at +pDz.
Definition: SoTrap.h:126
virtual SoChildList * getChildren() const
GetChildList, required whenever the class has hidden children.
SoSFFloat pDx1
Half-length along x of the side at y=-pDy1 of the face at -pDz.
Definition: SoTrap.h:114
virtual ~SoTrap()
Destructor, required.
virtual void clearAlternateRep()
We better be able to clear it, too!
SoSFFloat pPhi
Definition: SoTrap.h:106
virtual void generateAlternateRep()
SoSFFloat pDy2
Half-length along y of the face at +pDz.
Definition: SoTrap.h:122
SoSFNode alternateRep
Alternate rep - required.
Definition: SoTrap.h:145