VGM Version 5.3
Loading...
Searching...
No Matches
BooleanSolid.cxx
Go to the documentation of this file.
1// $Id$
2
3// -----------------------------------------------------------------------
4// The RootGM package of the Virtual Geometry Model
5// Copyright (C) 2007, Ivana Hrivnacova
6// All rights reserved.
7//
8// For the licensing terms see vgm/LICENSE.
9// Contact: ivana@ipno.in2p3.fr
10// -----------------------------------------------------------------------
11
12//
13// Class BooleanSolid
14// ---------------------
15// VGM implementation for Root Boolean solid
16//
17// Author: Ivana Hrivnacova; IPN Orsay
18
21
25
26#include "TGeoBoolNode.h"
27#include "TGeoCompositeShape.h"
28#include "TGeoMatrix.h"
29
30#include <cstdlib>
31
32const char RootGM::BooleanSolid::fgkIntersectionChar = '*';
33const char RootGM::BooleanSolid::fgkSubtractionChar = '-';
34const char RootGM::BooleanSolid::fgkUnionChar = '+';
35const char RootGM::BooleanSolid::fgkSeparator = ':';
36
37//_____________________________________________________________________________
38RootGM::BooleanSolid::BooleanSolid(const std::string& name,
39 VGM::BooleanType boolType, VGM::ISolid* solidA, VGM::ISolid* solidB,
40 TGeoMatrix* displacementB)
41 : VGM::ISolid(),
42 VGM::IBooleanSolid(),
43 BaseVGM::VBooleanSolid(),
44 fCompositeShape(0)
45{
52
53 // Get solid displacement, if present, and update transformation
54 // which will be used in Root node
55 //
56 TGeoHMatrix matrixA;
57 VGM::ISolid* constSolidA = solidA;
58 if (constSolidA->Type() == VGM::kDisplaced) {
59 VGM::IDisplacedSolid* displacedSolid =
60 dynamic_cast<VGM::IDisplacedSolid*>(constSolidA);
61 TGeoHMatrix displacement(
62 *RootGM::CreateTransform(displacedSolid->Displacement()));
63 matrixA = matrixA * displacement;
64 constSolidA = displacedSolid->ConstituentSolid();
65 }
66
67 TGeoHMatrix matrixB;
68 VGM::ISolid* constSolidB = solidB;
69 if (constSolidB->Type() == VGM::kDisplaced) {
70 VGM::IDisplacedSolid* displacedSolid =
71 dynamic_cast<VGM::IDisplacedSolid*>(constSolidB);
72 TGeoHMatrix displacement(
73 *RootGM::CreateTransform(displacedSolid->Displacement()));
74 matrixB = matrixB * displacement;
75 constSolidB = displacedSolid->ConstituentSolid();
76 }
77
78 // Get solids from the volumes map
79 TGeoShape* rootSolidA = RootGM::SolidMap::Instance()->GetSolid(constSolidA);
80 TGeoShape* rootSolidB = RootGM::SolidMap::Instance()->GetSolid(constSolidB);
81
82 // Create new TGeo matrices
83 TGeoMatrix* newMatrixA = 0;
84 if (!matrixA.IsIdentity()) newMatrixA = new TGeoHMatrix(matrixA);
85
86 TGeoMatrix* newMatrixB = displacementB;
87 if (!matrixB.IsIdentity())
88 newMatrixB = new TGeoHMatrix(TGeoHMatrix(*displacementB) * matrixB);
89
90 TGeoBoolNode* boolNode = 0;
91 switch (boolType) {
93 boolNode =
94 new TGeoIntersection(rootSolidA, rootSolidB, newMatrixA, newMatrixB);
95 break;
97 boolNode =
98 new TGeoSubtraction(rootSolidA, rootSolidB, newMatrixA, newMatrixB);
99 break;
100 case VGM::kUnion:
101 boolNode = new TGeoUnion(rootSolidA, rootSolidB, newMatrixA, newMatrixB);
102 break;
104 default:
105 std::cerr << " RootGM::BooleanSolid::BooleanSolid: " << std::endl;
106 std::cerr << " Unknown Boolean solid type" << std::endl;
107 std::cerr << "*** Error: Aborting execution ***" << std::endl;
108 exit(1);
109 }
110
111 fCompositeShape = new TGeoCompositeShape(name.data(), boolNode);
112
113 RootGM::SolidMap::Instance()->AddSolid(this, fCompositeShape);
114}
115
116//_____________________________________________________________________________
117RootGM::BooleanSolid::BooleanSolid(TGeoCompositeShape* compositeShape)
118 : VGM::ISolid(),
119 VGM::IBooleanSolid(),
120 BaseVGM::VBooleanSolid(),
121 fCompositeShape(compositeShape)
122{
124
125 RootGM::SolidMap::Instance()->AddSolid(this, fCompositeShape);
126}
127
128//_____________________________________________________________________________
130 : VGM::ISolid(), VGM::IBooleanSolid(), BaseVGM::VBooleanSolid()
131{
133}
134
135//_____________________________________________________________________________
137 : VGM::ISolid(rhs), VGM::IBooleanSolid(rhs), BaseVGM::VBooleanSolid(rhs)
138{
140}
141
142//_____________________________________________________________________________
147
148//
149// public methods
150//
151
152//_____________________________________________________________________________
153std::string RootGM::BooleanSolid::Name() const
154{
155 // Returns the Boolean solid name
156 // ---
157
158 return fCompositeShape->GetName();
159}
160
161//_____________________________________________________________________________
163{
164 // Returns the Boolean solid type name
165 // ---
166
167 TGeoBoolNode* boolNode = fCompositeShape->GetBoolNode();
168
169 if (boolNode->GetBooleanOperator() == TGeoBoolNode::kGeoIntersection)
170 return VGM::kIntersection;
171
172 if (boolNode->GetBooleanOperator() == TGeoBoolNode::kGeoSubtraction)
173 return VGM::kSubtraction;
174
175 if (boolNode->GetBooleanOperator() == TGeoBoolNode::kGeoUnion)
176 return VGM::kUnion;
177
179}
180
181//_____________________________________________________________________________
183{
184 // Returns the first constituent solid.
185 // ---
186
187 TGeoBoolNode* boolNode = fCompositeShape->GetBoolNode();
188
189 TGeoShape* rootSolidA = boolNode->GetLeftShape();
190 VGM::ISolid* solidA = RootGM::SolidMap::Instance()->GetSolid(rootSolidA);
191
192 return solidA;
193}
194
195//_____________________________________________________________________________
197{
198 // Returns the second constituent solid.
199 // ---
200
201 TGeoBoolNode* boolNode = fCompositeShape->GetBoolNode();
202
203 TGeoShape* rootSolidB = boolNode->GetRightShape();
204 VGM::ISolid* solidB = RootGM::SolidMap::Instance()->GetSolid(rootSolidB);
205
206 return solidB;
207}
208
209//_____________________________________________________________________________
211{
212 // Returns the solid displacemnt transformation
213 // in the frame of the first (left) solid.
214 // ---
215
216 TGeoBoolNode* boolNode = fCompositeShape->GetBoolNode();
217
218 TGeoMatrix* matrixA = boolNode->GetLeftMatrix();
219 TGeoMatrix* matrixB = boolNode->GetRightMatrix();
220
221 TGeoHMatrix transformA(*matrixA);
222 TGeoHMatrix transformB(*matrixB);
223
224 // If constituents are composite shapes,
225 // the displacement have to take into account the transformation
226 // of left constituent not passed to the solid
227
228 TGeoHMatrix totalTransformA(transformA);
229 TGeoShape* shapeA = boolNode->GetLeftShape();
230
231 // Take into account shifted origin
232 // totalTransformA = totalTransformA * RootGM::Displacement(shapeA);
233
234 while (shapeA->IsComposite()) {
235
236 TGeoBoolNode* boolNodeAC = ((TGeoCompositeShape*)shapeA)->GetBoolNode();
237
238 TGeoShape* shapeAC = boolNodeAC->GetLeftShape();
239 // left component of the shape A
240
241 TGeoMatrix* matrixAC = boolNodeAC->GetLeftMatrix();
242 TGeoHMatrix transformAC(*matrixAC);
243
244 totalTransformA = totalTransformA * transformAC;
245
246 // Take into account shifted origin
247 // totalTransformA = totalTransformA * RootGM::Displacement(shapeAC);
248
249 shapeA = shapeAC;
250 }
251
252 TGeoHMatrix totalTransformB(transformB);
253 TGeoShape* shapeB = boolNode->GetRightShape();
254 // Take into account shifted origin
255 // totalTransformB = totalTransformB * RootGM::Displacement(shapeB);
256
257 while (shapeB->IsComposite()) {
258
259 TGeoBoolNode* boolNodeBC = ((TGeoCompositeShape*)shapeB)->GetBoolNode();
260
261 TGeoShape* shapeBC = boolNodeBC->GetLeftShape();
262 // left component of the shape B
263
264 TGeoMatrix* matrixBC = boolNodeBC->GetLeftMatrix();
265 TGeoHMatrix transformBC(*matrixBC);
266
267 totalTransformB = totalTransformB * transformBC;
268
269 // Take into account shifted origin
270 // totalTransformB = totalTransformB * RootGM::Displacement(shapeBC);
271
272 shapeB = shapeBC;
273 }
274
275 return Transform(totalTransformA.Inverse() * totalTransformB);
276}
277
278//_____________________________________________________________________________
280 int index, TGeoCompositeShape* compositeShape)
281{
283
284 TGeoBoolNode* boolNode = compositeShape->GetBoolNode();
285
286 if (index == 0)
287 return boolNode->GetLeftShape();
288 else if (index == 1)
289 return boolNode->GetRightShape();
290 else {
291 std::cerr << " RootGM::BooleanSolid::GetConstituentSolid: " << std::endl;
292 std::cerr << " Index out of scope <0, 1>" << std::endl;
293 std::cerr << "*** Error: Aborting execution ***" << std::endl;
294 exit(1);
295 }
296}
VGM implementation for Root Boolean solid.
static TGeoShape * GetConstituentSolid(int index, TGeoCompositeShape *compositeShape)
virtual VGM::Transform Displacement() const
Return the 3D displacement of the second constituent solid with respect to the first one.
virtual VGM::ISolid * ConstituentSolidB() const
Return the second constituent solid.
virtual VGM::BooleanType BoolType() const
Return the Boolean type of this solid.
virtual VGM::ISolid * ConstituentSolidA() const
Return the first constituent solid.
virtual std::string Name() const
Return the name of this solid.
TGeoShape * GetSolid(VGM::ISolid *iSolid) const
Definition SolidMap.cxx:86
void AddSolid(VGM::ISolid *, TGeoShape *)
Definition SolidMap.cxx:59
static SolidMap * Instance()
Definition SolidMap.cxx:28
The VGM interface to displaced solids.
virtual ISolid * ConstituentSolid() const =0
Return the constituent solid.
virtual Transform Displacement() const =0
Return the 3D displacement of the constituent solid.
The VGM interface to solids.
Definition ISolid.h:58
virtual SolidType Type() const =0
Return the type of this solid.
BaseVGM utilities.
Definition utilities.h:23
TGeoMatrix * CreateTransform(const VGM::Transform &transform)
VGM::Transform Transform(const TGeoMatrix &matrix)
Definition transform.cxx:37
VGM interfaces.
Definition VMedium.h:28
std::vector< double > Transform
Definition Transform.h:40
@ kUnknownBoolean
@ kIntersection
@ kSubtraction
@ kDisplaced
Definition ISolid.h:49