ATLAS Offline Software
Loading...
Searching...
No Matches
SoGenericBox.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5
7// //
8// Implementation of class SoGenericBox //
9// //
10// Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
11// Initial version: December 2008 //
12// //
14
17
18#include <Inventor/SbBox.h>
19#include <Inventor/actions/SoPickAction.h>
20#include <Inventor/nodes/SoSeparator.h>
21#include <Inventor/nodes/SoIndexedFaceSet.h>
22#include <Inventor/nodes/SoIndexedLineSet.h>
23//#include <Inventor/nodes/SoLightModel.h>
24#include <Inventor/SoPrimitiveVertex.h>
25#include <Inventor/C/glue/gl.h>
26#include <Inventor/elements/SoGLCacheContextElement.h>
27#include <iostream>
28
30
33
35{
36 3, 2, 1, 0,
37 4, 5, 6, 7,
38 0, 1, 5, 4,
39 1, 2, 6, 5,
40 2, 3, 7, 6,
41 3, 0, 4, 7
42};
43
45{
46 3, 2, 1, 0, SO_END_FACE_INDEX,
47 4, 5, 6, 7, SO_END_FACE_INDEX,
48 0, 1, 5, 4, SO_END_FACE_INDEX,
49 1, 2, 6, 5, SO_END_FACE_INDEX,
50 2, 3, 7, 6, SO_END_FACE_INDEX,
51 3, 0, 4, 7, SO_END_FACE_INDEX
52};
53
55{
56 0, 1, 2, 3, 0, 4, 5, 6, 7, 4, SO_END_LINE_INDEX,
57 3, 7, SO_END_LINE_INDEX,
58 6, 2, SO_END_LINE_INDEX,
59 5, 1, SO_END_LINE_INDEX,
60};
61
62//____________________________________________________________________
64{
65 [[maybe_unused]] static const bool didInit = [&]() {
66 SO_NODE_INIT_CLASS(SoGenericBox, SoShape, "Shape");
67 return true;
68 }();
69}
70
71//____________________________________________________________________
73 : m_points(0), m_normals(0)
74{
75 SO_NODE_CONSTRUCTOR(SoGenericBox);
76 SO_NODE_ADD_FIELD(drawEdgeLines, (false));
77 SO_NODE_ADD_FIELD(forceEdgeLinesInBaseColour, (true));
78 SO_NODE_ADD_FIELD(alternateRep,(NULL));
79 setNodeType(EXTENSION);
80}
81
82//____________________________________________________________________
84{
85 delete [] m_normals;
86 delete [] m_points;
87}
88
89//____________________________________________________________________
90void SoGenericBox::generatePrimitives(SoAction *action) {
91
92 if (!m_points)
94
95 // Access the state from the action
96 SoState *state = action->getState();
97
98 if ( action->getTypeId().isDerivedFrom(SoPickAction::getClassTypeId()) ) {
99 //For picking:
100
101 SoPrimitiveVertex vertex;
102 beginShape(action,QUADS);
103 int *iptr = sogenericbox_vindices;
104 for (int i=0;i<SOGENERICBOX_NFACES;++i) {
105 for (int j = 0; j < 4; ++j) {
106 const int ivert = *iptr++;
107 vertex.setPoint(SbVec3f(m_points[ivert*3+0], m_points[ivert*3+1], m_points[ivert*3+2]));
108 shapeVertex(&vertex);
109 }
110 }
111 endShape();
112 } else {
113
114 glBegin(GL_QUADS);
115 int *iptr = sogenericbox_vindices;
116 for (int i=0;i<SOGENERICBOX_NFACES;i++) {
117 glNormal3fv(static_cast<const GLfloat*>(&m_normals[i*3]));
118 for (int j = 0; j < 4; j++)
119 glVertex3fv(static_cast<const GLfloat*>(&m_points[(*iptr++)*3]));
120 }
121 glEnd();
122
123
124 if (drawEdgeLines.getValue()) {
125
126 SbColor outlineColour;
127 const bool customColour = SoOutlineColour::get(state, outlineColour);
128 // Preserve Coin's colour/material state, including GL_COLOR_MATERIAL.
129 if (customColour)
130 glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT);
131 const bool disableLighting((customColour || forceEdgeLinesInBaseColour.getValue()) && glIsEnabled(GL_LIGHTING));
132 const bool transparencyOn = glIsEnabled(GL_BLEND);
133
134 if (disableLighting) glDisable(GL_LIGHTING);
135 if (transparencyOn) glDisable(GL_BLEND);
136 if (customColour)
137 glColor3fv(outlineColour.getValue());
138#define trdV(i) glVertex3fv(static_cast<const GLfloat*>(&m_points[i*3]));
139 glBegin(GL_LINE_STRIP);
140 trdV(0)trdV(1)trdV(2)trdV(3)trdV(0)trdV(4)trdV(5)trdV(6)trdV(7)trdV(4)
141 glEnd();
142
143 glBegin(GL_LINES);
144 trdV(3)trdV(7)
145 trdV(6)trdV(2)
146 trdV(5)trdV(1)
147 glEnd();
148
149 if (disableLighting) glEnable(GL_LIGHTING);
150 if (transparencyOn) glEnable(GL_BLEND);
151 if (customColour)
152 glPopAttrib();
153 }
154 }
155
156 if (state&&state->isElementEnabled(SoGLCacheContextElement::getClassStackIndex())) {
157 //Encourage auto caching
158 SoGLCacheContextElement::shouldAutoCache(state, SoGLCacheContextElement::DO_AUTO_CACHE);
159#if ((COIN_MAJOR_VERSION>=3)||((COIN_MAJOR_VERSION==2)&&(COIN_MINOR_VERSION>=5)))
160 SoGLCacheContextElement::incNumShapes(state);
161#endif
162 }
163
164}
165
166//____________________________________________________________________
168{
169 if (!m_points)
170 m_points = new float[SOGENERICBOX_NPOINTS*3];
171}
172
173//____________________________________________________________________
175{
176 setParametersForBox( 0.5, 0.5, 0.5 );
177}
178
179//____________________________________________________________________
180void SoGenericBox::computeBBox(SoAction *, SbBox3f &box, SbVec3f &center ){
181 if (!m_points)
183 box = m_bbox;
184 center = m_center;
185}
186
187//____________________________________________________________________
188void SoGenericBox::setParametersForBox( float dx, float dy, float dz,
189 float xcenter, float ycenter, float zcenter ) {
191 m_points[0*3+0] = xcenter+dx; m_points[0*3+1] = ycenter+dy; m_points[0*3+2] = zcenter-dz;
192 m_points[1*3+0] = xcenter-dx; m_points[1*3+1] = ycenter+dy; m_points[1*3+2] = zcenter-dz;
193 m_points[2*3+0] = xcenter-dx; m_points[2*3+1] = ycenter-dy; m_points[2*3+2] = zcenter-dz;
194 m_points[3*3+0] = xcenter+dx; m_points[3*3+1] = ycenter-dy; m_points[3*3+2] = zcenter-dz;
195 m_points[4*3+0] = xcenter+dx; m_points[4*3+1] = ycenter+dy; m_points[4*3+2] = zcenter+dz;
196 m_points[5*3+0] = xcenter-dx; m_points[5*3+1] = ycenter+dy; m_points[5*3+2] = zcenter+dz;
197 m_points[6*3+0] = xcenter-dx; m_points[6*3+1] = ycenter-dy; m_points[6*3+2] = zcenter+dz;
198 m_points[7*3+0] = xcenter+dx; m_points[7*3+1] = ycenter-dy; m_points[7*3+2] = zcenter+dz;
200}
201
202//____________________________________________________________________
203void SoGenericBox::internalSetParametersForEtaPhiCell( bool barrel, double etaMin, double etaMax,
204 double phiMin, double phiMax,
205 double cellDepth, double cellDistance,
206 double etasqueezefact, double phisqueezefact )
207{
209 if (phiMax<phiMin) std::swap(phiMin,phiMax);
210 if (etaMax<etaMin) std::swap(etaMin,etaMax);
211
212 if (etasqueezefact!=1.0) {
213 double etashift(0.5*(1.0-etasqueezefact)*fabs(etaMax-etaMin));
214 etaMax -= etashift;
215 etaMin += etashift;
216 }
217 if (phisqueezefact!=1.0) {
218 double phishift(0.5*(1.0-phisqueezefact)*fabs(phiMax-phiMin));
219 phiMax -= phishift;
220 phiMin += phishift;
221 }
222
223 if (cellDistance<0) cellDistance = -cellDistance;
224 if (cellDepth<0) cellDistance = std::max(cellDistance-cellDepth,0.0);
225 double tantheta2 = tan(2*atan(exp(-etaMin)));
226 double tantheta1 = tan(2*atan(exp(-etaMax)));
227 const double cpMin(cos(phiMin));
228 const double cpMax(cos(phiMax));
229 const double spMin(sin(phiMin));
230 const double spMax(sin(phiMax));
231 if (barrel) {
232 const double rf = cellDistance;
233 const double rb = cellDistance+cellDepth;
234 const double rfdivtantheta1(rf/tantheta1);
235 const double rbdivtantheta1(rb/tantheta1);
236 const double rfdivtantheta2(rf/tantheta2);
237 const double rbdivtantheta2(rb/tantheta2);
238 m_points[0*3+0] = rf*cpMin; m_points[0*3+1] = rf*spMin; m_points[0*3+2] = rfdivtantheta1;
239 m_points[1*3+0] = rf*cpMin; m_points[1*3+1] = rf*spMin; m_points[1*3+2] = rfdivtantheta2;
240 m_points[2*3+0] = rf*cpMax; m_points[2*3+1] = rf*spMax; m_points[2*3+2] = rfdivtantheta2;
241 m_points[3*3+0] = rf*cpMax; m_points[3*3+1] = rf*spMax; m_points[3*3+2] = rfdivtantheta1;
242 m_points[4*3+0] = rb*cpMin; m_points[4*3+1] = rb*spMin; m_points[4*3+2] = rbdivtantheta1;
243 m_points[5*3+0] = rb*cpMin; m_points[5*3+1] = rb*spMin; m_points[5*3+2] = rbdivtantheta2;
244 m_points[6*3+0] = rb*cpMax; m_points[6*3+1] = rb*spMax; m_points[6*3+2] = rbdivtantheta2;
245 m_points[7*3+0] = rb*cpMax; m_points[7*3+1] = rb*spMax; m_points[7*3+2] = rbdivtantheta1;
246 } else {
247 const double eta(0.5*(etaMin+etaMax));
248 const double zf(eta>0?cellDistance:-cellDistance);
249 double zb = (zf > 0 ? zf+cellDepth: zf-cellDepth);
250 m_points[0*3+0] = zf*tantheta1*cpMin; m_points[0*3+1] = zf*tantheta1*spMin; m_points[0*3+2] = zf;
251 m_points[1*3+0] = zf*tantheta2*cpMin; m_points[1*3+1] = zf*tantheta2*spMin; m_points[1*3+2] = zf;
252 m_points[2*3+0] = zf*tantheta2*cpMax; m_points[2*3+1] = zf*tantheta2*spMax; m_points[2*3+2] = zf;
253 m_points[3*3+0] = zf*tantheta1*cpMax; m_points[3*3+1] = zf*tantheta1*spMax; m_points[3*3+2] = zf;
254 m_points[4*3+0] = zb*tantheta1*cpMin; m_points[4*3+1] = zb*tantheta1*spMin; m_points[4*3+2] = zb;
255 m_points[5*3+0] = zb*tantheta2*cpMin; m_points[5*3+1] = zb*tantheta2*spMin; m_points[5*3+2] = zb;
256 m_points[6*3+0] = zb*tantheta2*cpMax; m_points[6*3+1] = zb*tantheta2*spMax; m_points[6*3+2] = zb;
257 m_points[7*3+0] = zb*tantheta1*cpMax; m_points[7*3+1] = zb*tantheta1*spMax; m_points[7*3+2] = zb;
258 }
259
261}
262
263
264//____________________________________________________________________
265void SoGenericBox::setParametersForBarrelEtaPhiCell( double etaMin, double etaMax,
266 double phiMin, double phiMax,
267 double cellDepth, double cellDistance,
268 double etasqueezefact, double phisqueezefact )
269{
270 internalSetParametersForEtaPhiCell( true, etaMin, etaMax, phiMin, phiMax,
271 cellDepth, cellDistance, etasqueezefact, phisqueezefact );
272}
273
274//____________________________________________________________________
275void SoGenericBox::setParametersForEndCapEtaPhiCell( double etaMin, double etaMax,
276 double phiMin, double phiMax,
277 double cellDepth, double cellDistance,
278 double etasqueezefact, double phisqueezefact )
279{
280 internalSetParametersForEtaPhiCell( false, etaMin, etaMax, phiMin, phiMax,
281 cellDepth, cellDistance, etasqueezefact, phisqueezefact );
282}
283
284//____________________________________________________________________
285void SoGenericBox::setParametersForTrd( float dx1, float dx2,
286 float dy1, float dy2,
287 float dz )
288{
290 m_points[0*3+0] = dx1; m_points[0*3+1] = dy1; m_points[0*3+2] = -dz;
291 m_points[1*3+0] = -dx1; m_points[1*3+1] = dy1; m_points[1*3+2] = -dz;
292 m_points[2*3+0] = -dx1; m_points[2*3+1] = -dy1; m_points[2*3+2] = -dz;
293 m_points[3*3+0] = dx1; m_points[3*3+1] = -dy1; m_points[3*3+2] = -dz;
294 m_points[4*3+0] = dx2; m_points[4*3+1] = dy2; m_points[4*3+2] = dz;
295 m_points[5*3+0] = -dx2; m_points[5*3+1] = dy2; m_points[5*3+2] = dz;
296 m_points[6*3+0] = -dx2; m_points[6*3+1] = -dy2; m_points[6*3+2] = dz;
297 m_points[7*3+0] = dx2; m_points[7*3+1] = -dy2; m_points[7*3+2] = dz;
299}
300
301//____________________________________________________________________
302void SoGenericBox::setParametersForTrapezoid( float dz, float theta, float phi, float dy1,
303 float dx1, float dx2, float dy2, float dx3,
304 float dx4, float alp1, float alp2 )
305
306{
308 const float tanTheta(tan(theta));
309 const float TthetaCphi = tanTheta*cos(phi);
310 const float TthetaSphi = tanTheta*sin(phi);
311 const float Talp1 = tan(alp1);
312 const float Talp2 = tan(alp2);
313
314 m_points[0*3+0] = dx2+dy1*Talp1;
315 m_points[0*3+1] = dy1;
316 m_points[0*3+2] = -dz;
317
318 m_points[1*3+0] = -dx2+dy1*Talp1;
319 m_points[1*3+1] = dy1;
320 m_points[1*3+2] = -dz;
321
322 m_points[2*3+0] = -dx1-dy1*Talp1;
323 m_points[2*3+1] = -dy1;
324 m_points[2*3+2] = -dz;
325
326 m_points[3*3+0] = dx1-dy1*Talp1;
327 m_points[3*3+1] = -dy1;
328 m_points[3*3+2] = -dz;
329
330 m_points[4*3+0] = dx4+dy2*Talp2;
331 m_points[4*3+1] = dy2;
332 m_points[4*3+2] = dz;
333
334 m_points[5*3+0] = -dx4+dy2*Talp2;
335 m_points[5*3+1] = dy2;
336 m_points[5*3+2] = dz;
337
338 m_points[6*3+0] = -dx3-dy2*Talp2;
339 m_points[6*3+1] = -dy2;
340 m_points[6*3+2] = dz;
341
342 m_points[7*3+0] = dx3-dy2*Talp2;
343 m_points[7*3+1] = -dy2;
344 m_points[7*3+2] = dz;
345
346 const float dzTthetaCphi(dz*TthetaCphi);
347 const float dzTthetaSphi(dz*TthetaSphi);
348 int i;
349 for (i=0;i<4;i++) {
350 m_points[i*3+0] -= dzTthetaCphi;
351 m_points[i*3+1] -= dzTthetaSphi;
352 }
353 for (i=4;i<8;i++) {
354 m_points[i*3+0] += dzTthetaCphi;
355 m_points[i*3+1] += dzTthetaSphi;
356 }
358}
359
360
361//____________________________________________________________________
362void SoGenericBox::setGenericParameters( float x0, float y0, float z0,
363 float x1, float y1, float z1,
364 float x2, float y2, float z2,
365 float x3, float y3, float z3,
366 float x4, float y4, float z4,
367 float x5, float y5, float z5,
368 float x6, float y6, float z6,
369 float x7, float y7, float z7 )
370{
372 m_points[0*3+0] = x0; m_points[0*3+1] = y0; m_points[0*3+2] = z0;
373 m_points[1*3+0] = x1; m_points[1*3+1] = y1; m_points[1*3+2] = z1;
374 m_points[2*3+0] = x2; m_points[2*3+1] = y2; m_points[2*3+2] = z2;
375 m_points[3*3+0] = x3; m_points[3*3+1] = y3; m_points[3*3+2] = z3;
376 m_points[4*3+0] = x4; m_points[4*3+1] = y4; m_points[4*3+2] = z4;
377 m_points[5*3+0] = x5; m_points[5*3+1] = y5; m_points[5*3+2] = z5;
378 m_points[6*3+0] = x6; m_points[6*3+1] = y6; m_points[6*3+2] = z6;
379 m_points[7*3+0] = x7; m_points[7*3+1] = y7; m_points[7*3+2] = z7;
381}
382
383//____________________________________________________________________
385{
386 //Normals:
387 if (!m_normals)
388 m_normals = new float[SOGENERICBOX_NFACES*3];
389 for (int iface = 0; iface < SOGENERICBOX_NFACES; ++iface) {
390 const int index_v1 = sogenericbox_vindices[iface*4+0];
391 const int index_v2 = sogenericbox_vindices[iface*4+1];
392 const int index_v3 = sogenericbox_vindices[iface*4+2];
393
394 //Figure out normal: (v2-v1)(x)(v3-v1)
395 const double v1X(m_points[index_v1*3+0]), v1Y(m_points[index_v1*3+1]), v1Z(m_points[index_v1*3+2]);
396 const double edge1X(m_points[index_v2*3+0]-v1X), edge1Y(m_points[index_v2*3+1]-v1Y), edge1Z(m_points[index_v2*3+2]-v1Z);
397 const double edge2X(m_points[index_v3*3+0]-v1X), edge2Y(m_points[index_v3*3+1]-v1Y), edge2Z(m_points[index_v3*3+2]-v1Z);
398
399 double nx(edge1Y*edge2Z-edge1Z*edge2Y);
400 double ny(edge1Z*edge2X-edge1X*edge2Z);
401 double nz(edge1X*edge2Y-edge1Y*edge2X);
402 double nl(nx*nx+ny*ny+nz*nz);
403 if (nl<=0) {
404 std::cout<<"SoGenericBox::Error Could not calculate normal due to degenerate face edge"<<std::endl;
405 nx=1.0;ny=0.0;nz=0.0;nl=1;
406 //Fixme: We should try to use index_v4 instead of one vertex,
407 //to get the normal (this would allow faces where one edge was
408 //collapsed).
409 //const int index_v4 = sogenericbox_vindices[iface*4+3];
410 }
411 const double s(1.0/sqrt(nl));
412 m_normals[iface*3+0] = nx*s;
413 m_normals[iface*3+1] = ny*s;
414 m_normals[iface*3+2] = nz*s;
415 }
416
417 //BBox:
418 float xmin(1.0e99), ymin(1.0e99), zmin(1.0e99);//fixme: -> inf
419 float xmax(-1.0e99), ymax(-1.0e99), zmax(-1.0e99);
420 for (int i=0;i<SOGENERICBOX_NPOINTS;++i) {
421 const float x(m_points[i*3+0]), y(m_points[i*3+1]), z(m_points[i*3+2]);
422 xmin = std::min(xmin,x); xmax = std::max(xmax,x);
423 ymin = std::min(ymin,y); ymax = std::max(ymax,y);
424 zmin = std::min(zmin,z); zmax = std::max(zmax,z);
425 }
426 m_bbox.setBounds (xmin, ymin, zmin, xmax, ymax, zmax);
427 m_center.setValue(0.5*(xmin+xmax),0.5*(ymin+ymax),0.5*(zmin+zmax));
428
429 drawEdgeLines.touch();
430}
431
432
434// Stuff for alternate rep //
436
437//____________________________________________________________________
439{
440 if (!m_points)
442 if (alternateRep.getValue())
444 SoSeparator * sep = new SoSeparator;
445
446 //Vertice coordinates:
447 SoVertexProperty *vertices = new SoVertexProperty();
448 for (int i=0;i<SOGENERICBOX_NPOINTS;++i)
449 vertices->vertex.set1Value ( i, m_points[i*3+0],m_points[i*3+1],m_points[i*3+2] );
450 SoIndexedFaceSet * faceset = new SoIndexedFaceSet;
451 faceset->coordIndex.setValues(0,5*6, sogenericbox_vindices_foraltrepfaces);
452 faceset->vertexProperty = vertices;
453 sep->addChild(faceset);
454
455 if (drawEdgeLines.getValue()) {
456//Disable the lightmodel now since we in any case gets baselightning with the lineset defined this way...
457// if (forceEdgeLinesInBaseColour.getValue()) {
458// SoLightModel * lm = new SoLightModel;//Fixme: share
459// lm->model = SoLightModel::BASE_COLOR;
460// sep->addChild(lm);
461// }
462 SoIndexedLineSet * lineset = new SoIndexedLineSet;
463 lineset->coordIndex.setValues(0,20,sogenericbox_vindices_foraltreplines);
464 lineset->vertexProperty = vertices;
465 sep->addChild(lineset);
466 }
467
468 alternateRep.setValue(sep);
469
470}
471
472//____________________________________________________________________
474{
475 alternateRep.setValue(NULL);
476 //Hmm... no ref/unref??
477}
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
Scalar theta() const
theta method
static const int SOGENERICBOX_NPOINTS
static const int SOGENERICBOX_NFACES
static int sogenericbox_vindices[]
static int sogenericbox_vindices_foraltreplines[20]
static int sogenericbox_vindices_foraltrepfaces[5 *SOGENERICBOX_NFACES]
#define trdV(i)
SO_NODE_SOURCE(SoOutlineColour)
#define y
#define x
#define z
SoSFNode alternateRep
virtual void clearAlternateRep()
void setParametersForBox(float dx, float dy, float dz, float xcenter=0.0, float ycenter=0.0, float zcenter=0.0)
void internalSetParametersForEtaPhiCell(bool barrel, double etaMin, double etaMax, double phiMin, double phiMax, double cellDepth, double cellDistance, double etasqueezefact, double phisqueezefact)
void ensurePointsAllocated()
virtual ~SoGenericBox()
void setParametersForTrapezoid(float dz, float theta, float phi, float dy1, float dx1, float dx2, float dy2, float dx3, float dx4, float alp1, float alp2)
float * m_points
void setParametersForTrd(float dx1, float dx2, float dy1, float dy2, float dz)
void setGenericParameters(float x0, float y0, float z0, float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4, float x5, float y5, float z5, float x6, float y6, float z6, float x7, float y7, float z7)
SoSFBool forceEdgeLinesInBaseColour
virtual void computeBBox(SoAction *action, SbBox3f &box, SbVec3f &center)
void setParametersForBarrelEtaPhiCell(double etaMin, double etaMax, double phiMin, double phiMax, double cellDepth, double cellDistance, double etasqueezefact=1.0, double phisqueezefact=1.0)
static void initClass()
void setupDefaultPoints()
float * m_normals
virtual void generatePrimitives(SoAction *action)
SoSFBool drawEdgeLines
SbVec3f m_center
void setParametersForEndCapEtaPhiCell(double etaMin, double etaMax, double phiMin, double phiMax, double cellDepth, double cellDistance, double etasqueezefact=1.0, double phisqueezefact=1.0)
virtual void generateAlternateRep()
static bool get(SoState *, SbColor &)
double xmax
Definition listroot.cxx:61
double ymin
Definition listroot.cxx:63
double xmin
Definition listroot.cxx:60
double ymax
Definition listroot.cxx:64
void swap(ElementLinkVector< DOBJ > &lhs, ElementLinkVector< DOBJ > &rhs)