Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4UTessellatedSolid.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// Implementation of G4UTessellatedSolid wrapper class
27//
28// 11.01.18 G.Cosmo, CERN
29// --------------------------------------------------------------------
30
31#include "G4TessellatedSolid.hh"
33
34#if ( defined(G4GEOM_USE_USOLIDS) || defined(G4GEOM_USE_PARTIAL_USOLIDS) )
35
36#include "G4TriangularFacet.hh"
38
39#include "G4GeomTools.hh"
40#include "G4AffineTransform.hh"
41#include "G4BoundingEnvelope.hh"
42
43////////////////////////////////////////////////////////////////////////
44//
45// Constructors
46//
47G4UTessellatedSolid::G4UTessellatedSolid()
48 : Base_t("")
49{
50}
51
52G4UTessellatedSolid::G4UTessellatedSolid(const G4String& name)
53 : Base_t(name)
54{
55}
56
57//////////////////////////////////////////////////////////////////////////
58//
59// Destructor
60//
61G4UTessellatedSolid::~G4UTessellatedSolid()
62{
63 std::size_t size = fFacets.size();
64 for (std::size_t i = 0; i < size; ++i) { delete fFacets[i]; }
65 fFacets.clear();
66}
67
68//////////////////////////////////////////////////////////////////////////
69//
70// Copy constructor
71//
72G4UTessellatedSolid::G4UTessellatedSolid(const G4UTessellatedSolid& source)
73 : Base_t(source)
74{
75}
76
77//////////////////////////////////////////////////////////////////////////
78//
79// Assignment operator
80//
81G4UTessellatedSolid&
82G4UTessellatedSolid::operator=(const G4UTessellatedSolid& source)
83{
84 if (this == &source) return *this;
85
86 Base_t::operator=( source );
87
88 return *this;
89}
90
91//////////////////////////////////////////////////////////////////////////
92//
93// Accessors
94
95G4bool G4UTessellatedSolid::AddFacet(G4VFacet* aFacet)
96{
97 // Add a facet to the structure, checking validity.
98 //
99 if (GetSolidClosed())
100 {
101 G4Exception("G4UTessellatedSolid::AddFacet()", "GeomSolids1002",
102 JustWarning, "Attempt to add facets when solid is closed.");
103 return false;
104 }
105 if (!aFacet->IsDefined())
106 {
107 G4Exception("G4UTessellatedSolid::AddFacet()", "GeomSolids1002",
108 JustWarning, "Attempt to add facet not properly defined.");
109 aFacet->StreamInfo(G4cout);
110 return false;
111 }
112 if (aFacet->GetNumberOfVertices() == 3)
113 {
114 auto a3Facet = dynamic_cast<G4TriangularFacet*>(aFacet);
115 return Base_t::AddTriangularFacet(U3Vector(a3Facet->GetVertex(0).x(),
116 a3Facet->GetVertex(0).y(),
117 a3Facet->GetVertex(0).z()),
118 U3Vector(a3Facet->GetVertex(1).x(),
119 a3Facet->GetVertex(1).y(),
120 a3Facet->GetVertex(1).z()),
121 U3Vector(a3Facet->GetVertex(2).x(),
122 a3Facet->GetVertex(2).y(),
123 a3Facet->GetVertex(2).z()),
124 true);
125 }
126 else if (aFacet->GetNumberOfVertices() == 4)
127 {
128 auto a4Facet = dynamic_cast<G4QuadrangularFacet*>(aFacet);
129 return Base_t::AddQuadrilateralFacet(U3Vector(a4Facet->GetVertex(0).x(),
130 a4Facet->GetVertex(0).y(),
131 a4Facet->GetVertex(0).z()),
132 U3Vector(a4Facet->GetVertex(1).x(),
133 a4Facet->GetVertex(1).y(),
134 a4Facet->GetVertex(1).z()),
135 U3Vector(a4Facet->GetVertex(2).x(),
136 a4Facet->GetVertex(2).y(),
137 a4Facet->GetVertex(2).z()),
138 U3Vector(a4Facet->GetVertex(3).x(),
139 a4Facet->GetVertex(3).y(),
140 a4Facet->GetVertex(3).z()),
141 true);
142 }
143 else
144 {
145 G4Exception("G4UTessellatedSolid::AddFacet()", "GeomSolids1002",
146 JustWarning, "Attempt to add facet not properly defined.");
147 aFacet->StreamInfo(G4cout);
148 return false;
149 }
150}
151
152G4VFacet* G4UTessellatedSolid::GetFacet(G4int i) const
153{
154 return fFacets[i];
155}
156
157G4int G4UTessellatedSolid::GetNumberOfFacets() const
158{
159 return GetNFacets();
160}
161
162void G4UTessellatedSolid::SetSolidClosed(const G4bool t)
163{
164 if (t && !Base_t::IsClosed())
165 {
166 Base_t::Close();
167 std::size_t nVertices = fTessellated.fVertices.size();
168 std::size_t nFacets = fTessellated.fFacets.size();
169 for (std::size_t j = 0; j < nVertices; ++j)
170 {
171 U3Vector vt = fTessellated.fVertices[j];
172 fVertexList.emplace_back(vt.x(), vt.y(), vt.z());
173 }
174 for (std::size_t i = 0; i < nFacets; ++i)
175 {
176 vecgeom::TriangleFacet<G4double>* afacet = Base_t::GetFacet(i);
177 std::vector<G4ThreeVector> v;
178 for (const auto & vertex : afacet->fVertices)
179 {
180 v.emplace_back(vertex.x(),
181 vertex.y(),
182 vertex.z());
183 }
184 G4VFacet* facet = new G4TriangularFacet(v[0], v[1], v[2],
186 facet->SetVertices(&fVertexList);
187 for (G4int k=0; k<3; ++k)
188 {
189 facet->SetVertexIndex(k, afacet->fIndices[k]);
190 }
191 fFacets.push_back(facet);
192 }
193 }
194}
195
196G4bool G4UTessellatedSolid::GetSolidClosed() const
197{
198 return Base_t::IsClosed();
199}
200
201void G4UTessellatedSolid::SetMaxVoxels(G4int)
202{
203 // Not yet implemented !
204}
205
206G4double G4UTessellatedSolid::GetMinXExtent() const
207{
208 U3Vector aMin, aMax;
209 Base_t::Extent(aMin, aMax);
210 return aMin.x();
211}
212G4double G4UTessellatedSolid::GetMaxXExtent() const
213{
214 U3Vector aMin, aMax;
215 Base_t::Extent(aMin, aMax);
216 return aMax.x();
217}
218G4double G4UTessellatedSolid::GetMinYExtent() const
219{
220 U3Vector aMin, aMax;
221 Base_t::Extent(aMin, aMax);
222 return aMin.y();
223}
224G4double G4UTessellatedSolid::GetMaxYExtent() const
225{
226 U3Vector aMin, aMax;
227 Base_t::Extent(aMin, aMax);
228 return aMax.y();
229}
230G4double G4UTessellatedSolid::GetMinZExtent() const
231{
232 U3Vector aMin, aMax;
233 Base_t::Extent(aMin, aMax);
234 return aMin.z();
235}
236G4double G4UTessellatedSolid::GetMaxZExtent() const
237{
238 U3Vector aMin, aMax;
239 Base_t::Extent(aMin, aMax);
240 return aMax.z();
241}
242
243G4int G4UTessellatedSolid::AllocatedMemoryWithoutVoxels()
244{
245 G4int base = sizeof(*this);
246 base += fVertexList.capacity() * sizeof(G4ThreeVector);
247
248 std::size_t limit = fFacets.size();
249 for (std::size_t i = 0; i < limit; ++i)
250 {
251 G4VFacet &facet = *fFacets[i];
252 base += facet.AllocatedMemory();
253 }
254 return base;
255}
256G4int G4UTessellatedSolid::AllocatedMemory()
257{
258 return AllocatedMemoryWithoutVoxels();
259}
260void G4UTessellatedSolid::DisplayAllocatedMemory()
261{
262 G4int without = AllocatedMemoryWithoutVoxels();
263 // G4int with = AllocatedMemory();
264 // G4double ratio = (G4double) with / without;
265 // G4cout << "G4TessellatedSolid - Allocated memory without voxel overhead "
266 // << without << "; with " << with << "; ratio: " << ratio << G4endl;
267 G4cout << "G4TessellatedSolid - Allocated memory without voxel overhead "
268 << without << G4endl;
269}
270
271
272///////////////////////////////////////////////////////////////////////////////
273//
274// Get bounding box
275
276void G4UTessellatedSolid::BoundingLimits(G4ThreeVector& pMin,
277 G4ThreeVector& pMax) const
278{
279 U3Vector aMin, aMax;
280 Base_t::Extent(aMin, aMax);
281 pMin = G4ThreeVector(aMin.x(), aMin.y(), aMin.z());
282 pMax = G4ThreeVector(aMax.x(), aMax.y(), aMax.z());
283
284 // Check correctness of the bounding box
285 //
286 if (pMin.x() >= pMax.x() || pMin.y() >= pMax.y() || pMin.z() >= pMax.z())
287 {
288 std::ostringstream message;
289 message << "Bad bounding box (min >= max) for solid: "
290 << GetName() << " !"
291 << "\npMin = " << pMin
292 << "\npMax = " << pMax;
293 G4Exception("G4UTessellatedSolid::BoundingLimits()",
294 "GeomMgt0001", JustWarning, message);
295 StreamInfo(G4cout);
296 }
297}
298
299
300//////////////////////////////////////////////////////////////////////////////
301//
302// Calculate extent under transform and specified limit
303
304G4bool
305G4UTessellatedSolid::CalculateExtent(const EAxis pAxis,
306 const G4VoxelLimits& pVoxelLimit,
307 const G4AffineTransform& pTransform,
308 G4double& pMin, G4double& pMax) const
309{
310 G4ThreeVector bmin, bmax;
311
312 // Check bounding box (bbox)
313 //
314 BoundingLimits(bmin,bmax);
315 G4BoundingEnvelope bbox(bmin,bmax);
316
317 // Use simple bounding-box to help in the case of complex meshes
318 //
319 return bbox.CalculateExtent(pAxis,pVoxelLimit,pTransform,pMin,pMax);
320
321#if 0
322 // Precise extent computation (disabled by default for this shape)
323 //
324 G4double kCarToleranceHalf = 0.5*kCarTolerance;
325 if (bbox.BoundingBoxVsVoxelLimits(pAxis,pVoxelLimit,pTransform,pMin,pMax))
326 {
327 return (pMin < pMax) ? true : false;
328 }
329
330 // The extent is calculated as cumulative extent of the pyramids
331 // formed by facets and the center of the bounding box.
332 //
333 G4double eminlim = pVoxelLimit.GetMinExtent(pAxis);
334 G4double emaxlim = pVoxelLimit.GetMaxExtent(pAxis);
335
337 G4ThreeVectorList apex(1);
338 std::vector<const G4ThreeVectorList *> pyramid(2);
339 pyramid[0] = &base;
340 pyramid[1] = &apex;
341 apex[0] = (bmin+bmax)*0.5;
342
343 // main loop along facets
344 pMin = kInfinity;
345 pMax = -kInfinity;
346 for (G4int i=0; i<GetNumberOfFacets(); ++i)
347 {
348 G4VFacet* facet = GetFacet(i);
349 if (std::abs((facet->GetSurfaceNormal()).dot(facet->GetVertex(0)-apex[0]))
350 < kCarToleranceHalf) continue;
351
352 base.resize(3);
353 for (G4int k=0; k<3; ++k) { base[k] = facet->GetVertex(k); }
354 G4double emin,emax;
355 G4BoundingEnvelope benv(pyramid);
356 if (!benv.CalculateExtent(pAxis,pVoxelLimit,pTransform,emin,emax)) continue;
357 if (emin < pMin) pMin = emin;
358 if (emax > pMax) pMax = emax;
359 if (eminlim > pMin && emaxlim < pMax) break; // max possible extent
360 }
361 return (pMin < pMax);
362#endif
363}
364
365
366///////////////////////////////////////////////////////////////////////////////
367//
368// CreatePolyhedron()
369//
370G4Polyhedron* G4UTessellatedSolid::CreatePolyhedron () const
371{
372 auto nVertices = (G4int)fVertexList.size();
373 auto nFacets = (G4int)fFacets.size();
374 auto polyhedron = new G4Polyhedron(nVertices, nFacets);
375 for (auto i = 0; i < nVertices; ++i)
376 {
377 polyhedron->SetVertex(i+1, fVertexList[i]);
378 }
379
380 for (auto i = 0; i < nFacets; ++i)
381 {
382 G4int v[3]; // Only facets with 3 vertices are defined in VecGeom
383 G4VFacet* facet = GetFacet(i);
384 for (auto j = 0; j < 3; ++j) // Retrieve indexing directly from VecGeom
385 {
386 v[j] = facet->GetVertexIndex(j) + 1;
387 }
388 polyhedron->SetFacet(i+1, v[0], v[1], v[2]);
389 }
390 polyhedron->SetReferences();
391
392 return polyhedron;
393}
394
395#endif // G4GEOM_USE_USOLIDS
const G4double kCarTolerance
std::vector< G4ThreeVector > G4ThreeVectorList
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
CLHEP::Hep3Vector G4ThreeVector
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
@ ABSOLUTE
Definition G4VFacet.hh:48
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
double z() const
double x() const
double y() const
G4AffineTransform is a class for geometric affine transformations. It supports efficient arbitrary ro...
G4BoundingEnvelope is a helper class to facilitate calculation of the extent of a solid within the li...
G4QuadrangularFacet defines a facet with 4 vertices, used for the contruction of G4TessellatedSolid....
G4TriangularFacet defines a facet with 3 vertices, used for the contruction of G4TessellatedSolid....
G4VFacet is a base class defining the facets which are components of a G4TessellatedSolid shape.
Definition G4VFacet.hh:56
virtual void SetVertexIndex(G4int i, G4int j)=0
virtual G4int AllocatedMemory()=0
std::ostream & StreamInfo(std::ostream &os) const
Definition G4VFacet.cc:96
virtual G4ThreeVector GetSurfaceNormal() const =0
virtual G4ThreeVector GetVertex(G4int i) const =0
virtual G4int GetNumberOfVertices() const =0
virtual G4int GetVertexIndex(G4int i) const =0
virtual void SetVertices(std::vector< G4ThreeVector > *vertices)=0
virtual G4bool IsDefined() const =0
G4VoxelLimits represents limitation/restrictions of space, where restrictions are only made perpendic...
G4double GetMinExtent(const EAxis pAxis) const
G4double GetMaxExtent(const EAxis pAxis) const
EAxis
Definition geomdefs.hh:54
const char * name(G4int ptype)