Geant4  10.03
SoBox.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 // $Id: SoBox.cc 66373 2012-12-18 09:41:34Z gcosmo $
29 //
30 /*----------------------------HEPVis----------------------------------------*/
31 /* */
32 /* Node: SoBox */
33 /* Description: Represents the G4Box Geant Geometry entity */
34 /* Author: Joe Boudreau Nov 11 1996 */
35 /* */
36 /*--------------------------------------------------------------------------*/
37 
38 #ifdef G4VIS_BUILD_OI_DRIVER
39 
40 // this :
41 #include "HEPVis/nodes/SoBox.h"
42 
43 #include <assert.h>
44 #include <cmath>
45 
46 #include <Inventor/SbBox.h>
47 #include <Inventor/fields/SoSFFloat.h>
48 #include <Inventor/misc/SoChildList.h>
49 #include <Inventor/nodes/SoSeparator.h>
50 #include <Inventor/nodes/SoCube.h>
51 #include <Inventor/nodes/SoScale.h>
52 #include <Inventor/actions/SoAction.h>
53 #include <Inventor/nodes/SoIndexedFaceSet.h>
54 #include <Inventor/SoPrimitiveVertex.h>
55 #include <Inventor/elements/SoTextureCoordinateElement.h>
56 
57 // This statement is required
58 SO_NODE_SOURCE(SoBox)
59 
60 // Constructor
61 SoBox::SoBox() {
62  // This statement is required
63  SO_NODE_CONSTRUCTOR(SoBox);
64 
65  // Data fields are initialized like this:
66  SO_NODE_ADD_FIELD(fDx, (1.0));
67  SO_NODE_ADD_FIELD(fDy, (1.0));
68  SO_NODE_ADD_FIELD(fDz, (1.0));
69  SO_NODE_ADD_FIELD(alternateRep, (NULL));
70  children = new SoChildList(this);
71 }
72 
73 // Destructor
74 SoBox::~SoBox() {
75  delete children;
76 }
77 
78 
79 // initClass
80 void SoBox::initClass(){
81  // This statement is required.
82  SO_NODE_INIT_CLASS(SoBox,SoShape,"Shape");
83 }
84 
85 
86 // generatePrimitives
87 void SoBox::generatePrimitives(SoAction *action) {
88  // This variable is used to store each vertex
89  SoPrimitiveVertex pv;
90 
91  // Access the stat from the action
92  SoState *state = action->getState();
93 
94  // See if we have to use a texture coordinate function,
95  // rather than generating explicit texture coordinates.
96  SbBool useTexFunction=
97  (SoTextureCoordinateElement::getType(state) ==
98  SoTextureCoordinateElement::FUNCTION);
99 
100  // If we need to generate texture coordinates with a function,
101  // we'll need an SoGLTextureCoordinateElement. Otherwise, we'll
102  // set up the coordinates directly.
103  const SoTextureCoordinateElement *tce = NULL;
104  SbVec4f texCoord;
105  if (useTexFunction) {
106  tce = SoTextureCoordinateElement::getInstance(state);
107  }
108  else {
109  texCoord[2] = 0.0;
110  texCoord[3] = 1.0;
111  }
112  SbVec3f point, normal;
113 
114 
116  //----------------------------------------
117 #define GEN_VERTEX(pv,x,y,z,s,t,nx,ny,nz) \
118  point.setValue(x,y,z); \
119  normal.setValue(nx,ny,nz); \
120  if (useTexFunction) { \
121  texCoord=tce->get(point,normal); \
122  } \
123  else { \
124  texCoord[0]=s; \
125  texCoord[1]=t; \
126  } \
127  pv.setPoint(point); \
128  pv.setNormal(normal); \
129  pv.setTextureCoords(texCoord); \
130  shapeVertex(&pv);
131  //----------------------------------------
133 
134  const int NPOINTS=8, NFACES=6, NINDICES = NFACES*5;
135  int indices[NINDICES] = {3,2,1,0, SO_END_FACE_INDEX, //z back.
136  4,5,6,7, SO_END_FACE_INDEX, //z front.
137  0,1,5,4, SO_END_FACE_INDEX, //y up.
138  1,2,6,5, SO_END_FACE_INDEX, //x left.
139  2,3,7,6, SO_END_FACE_INDEX, //y down.
140  3,0,4,7, SO_END_FACE_INDEX}; //x right.
141 
142 
143  // points for the eight vertices
144  float points[NPOINTS][3];
145  points[0][0] = fDx.getValue();
146  points[0][1] = fDy.getValue();
147  points[0][2] = -fDz.getValue();
148 
149  points[1][0] = -fDx.getValue();
150  points[1][1] = fDy.getValue();
151  points[1][2] = -fDz.getValue();
152 
153  points[2][0] = -fDx.getValue();
154  points[2][1] = -fDy.getValue();
155  points[2][2] = -fDz.getValue();
156 
157  points[3][0] = fDx.getValue();
158  points[3][1] = -fDy.getValue();
159  points[3][2] = -fDz.getValue();
160 
161  points[4][0] = fDx.getValue();
162  points[4][1] = fDy.getValue();
163  points[4][2] = fDz.getValue();
164 
165  points[5][0] = -fDx.getValue();
166  points[5][1] = fDy.getValue();
167  points[5][2] = fDz.getValue();
168 
169  points[6][0] = -fDx.getValue();
170  points[6][1] = -fDy.getValue();
171  points[6][2] = fDz.getValue();
172 
173  points[7][0] = fDx.getValue();
174  points[7][1] = -fDy.getValue();
175  points[7][2] = fDz.getValue();
176 
177  float normals[NFACES][3];
178  //z back.
179  normals[0][0] = 0 ; normals[0][1] = 0; normals [0][2] = -1;
180  //z front.
181  normals[1][0] = 0 ; normals[1][1] = 0; normals [1][2] = 1;
182  //y up.
183  normals[2][0] = 0 ; normals[2][1] = 1; normals [2][2] = 0;
184  //x left.
185  normals[3][0] = -1 ; normals[3][1] = 0; normals [3][2] = 0;
186  //y down.
187  normals[4][0] = 0 ; normals[4][1] = -1; normals [4][2] = 0;
188  //x right.
189  normals[5][0] = 1 ; normals[5][1] = 0; normals [5][2] = 0;
190 
191  float x,y,z;
192  int index;
193  for (int nf=0;nf<NFACES;nf++) {
194  beginShape(action,TRIANGLE_FAN);
195  index = indices[nf * 5];
196  x = points[index][0];
197  y = points[index][1];
198  z = points[index][2];
199  GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
200  index = indices[nf * 5 + 1];
201  x = points[index][0];
202  y = points[index][1];
203  z = points[index][2];
204  GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
205  index = indices[nf * 5 + 2];
206  x = points[index][0];
207  y = points[index][1];
208  z = points[index][2];
209  GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
210  index = indices[nf * 5 + 3];
211  x = points[index][0];
212  y = points[index][1];
213  z = points[index][2];
214  GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
215  endShape();
216  }
217 }
218 
219 // getChildren
220 SoChildList *SoBox::getChildren() const {
221  return children;
222 }
223 
224 
225 // computeBBox
226 void SoBox::computeBBox(SoAction *, SbBox3f &box, SbVec3f &center ){
227  SbVec3f vmin(-fDx.getValue(),-fDy.getValue(),-fDz.getValue()),
228  vmax( fDx.getValue(), fDy.getValue(), fDz.getValue());
229  center.setValue(0,0,0);
230  box.setBounds(vmin,vmax);
231 }
232 
233 
234 
235 
236 // updateChildren
237 void SoBox::updateChildren() {
238 
239 
240  // Redraw the G4Box....
241 
242  assert(children->getLength()==1);
243  SoSeparator *sep = (SoSeparator *) ( *children)[0];
244  SoScale *scale = (SoScale *)( sep->getChild(0));
245  //SoCube *cube = (SoCube *)( sep->getChild(1));
246  scale->scaleFactor.setValue(fDx.getValue(), fDy.getValue(), fDz.getValue());
247 }
248 
249 // generateChildren
250 void SoBox::generateChildren() {
251 
252  // A box consists of a set of scale factors and a
253  // cube.
254 
255  assert(children->getLength() ==0);
256  SoSeparator *sep = new SoSeparator();
257  SoScale *scale = new SoScale();
258  SoCube *cube = new SoCube();
259 
260  sep->addChild(scale);
261  sep->addChild(cube);
262  children->append(sep);
263 }
264 
265 // generateAlternateRep
266 void SoBox::generateAlternateRep() {
267 
268  // This routine sets the alternate representation to the child
269  // list of this mode.
270 
271  if (children->getLength() == 0) generateChildren();
272  updateChildren();
273  alternateRep.setValue((SoSeparator *) ( *children)[0]);
274 }
275 
276 // clearAlternateRep
277 void SoBox::clearAlternateRep() {
278  alternateRep.setValue(NULL);
279 }
280 
281 #endif
static double normal(HepRandomEngine *eptr)
Definition: RandPoisson.cc:77