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