Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4TessellatedSolid.hh
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 and of QinetiQ Ltd, *
20// * subject to DEFCON 705 IPR conditions. *
21// * By using, copying, modifying or distributing the software (or *
22// * any work based on the software) you agree to acknowledge its *
23// * use in resulting scientific publications, and indicate your *
24// * acceptance of all terms of the Geant4 Software license. *
25// ********************************************************************
26//
27// G4TessellatedSolid
28//
29// Class description:
30//
31// G4TessellatedSolid is a special Geant4 solid defined by a number of
32// facets (UVFacet). It is important that the supplied facets shall form a
33// fully enclose space which is the solid.
34// At the moment only two types of facet can be used for the construction of
35// a G4TessellatedSolid, i.e. the G4TriangularFacet and G4QuadrangularFacet.
36//
37// How to contruct a G4TessellatedSolid:
38//
39// First declare a tessellated solid:
40//
41// G4TessellatedSolid* solidTarget = new G4TessellatedSolid("Solid_name");
42//
43// Define the facets which form the solid:
44//
45// G4double targetSiz = 10*cm ;
46// G4TriangularFacet *facet1 = new
47// G4TriangularFacet (G4ThreeVector(-targetSize,-targetSize, 0.0),
48// G4ThreeVector(+targetSize,-targetSize, 0.0),
49// G4ThreeVector( 0.0, 0.0,+targetSize),
50// ABSOLUTE);
51// G4TriangularFacet *facet2 = new
52// G4TriangularFacet (G4ThreeVector(+targetSize,-targetSize, 0.0),
53// G4ThreeVector(+targetSize,+targetSize, 0.0),
54// G4ThreeVector( 0.0, 0.0,+targetSize),
55// ABSOLUTE);
56// G4TriangularFacet *facet3 = new
57// G4TriangularFacet (G4ThreeVector(+targetSize,+targetSize, 0.0),
58// G4ThreeVector(-targetSize,+targetSize, 0.0),
59// G4ThreeVector( 0.0, 0.0,+targetSize),
60// ABSOLUTE);
61// G4TriangularFacet *facet4 = new
62// G4TriangularFacet (G4ThreeVector(-targetSize,+targetSize, 0.0),
63// G4ThreeVector(-targetSize,-targetSize, 0.0),
64// G4ThreeVector( 0.0, 0.0,+targetSize),
65// ABSOLUTE);
66// G4QuadrangularFacet *facet5 = new
67// G4QuadrangularFacet (G4ThreeVector(-targetSize,-targetSize, 0.0),
68// G4ThreeVector(-targetSize,+targetSize, 0.0),
69// G4ThreeVector(+targetSize,+targetSize, 0.0),
70// G4ThreeVector(+targetSize,-targetSize, 0.0),
71// ABSOLUTE);
72//
73// Then add the facets to the solid:
74//
75// solidTarget->AddFacet((UVFacet*) facet1);
76// solidTarget->AddFacet((UVFacet*) facet2);
77// solidTarget->AddFacet((UVFacet*) facet3);
78// solidTarget->AddFacet((UVFacet*) facet4);
79// solidTarget->AddFacet((UVFacet*) facet5);
80//
81// Finally declare the solid is complete:
82//
83// solidTarget->SetSolidClosed(true);
84
85// Author: P.R.Truscott (QinetiQ Ltd, UK), 31.10.2004 - Created.
86// M.Gayer (CERN), 12.10.2012 - New implementation with voxelization.
87// --------------------------------------------------------------------
88#ifndef G4TESSELLATEDSOLID_HH
89#define G4TESSELLATEDSOLID_HH
90
91#include "G4GeomTypes.hh"
92
93#if defined(G4GEOM_USE_USOLIDS)
94#define G4GEOM_USE_UTESSELLATEDSOLID 1
95#endif
96
97#if defined(G4GEOM_USE_UTESSELLATEDSOLID)
98 #define G4UTessellatedSolid G4TessellatedSolid
99 #include "G4UTessellatedSolid.hh"
100#else
101
102#include <iostream>
103#include <vector>
104#include <set>
105#include <map>
106
107#include "G4Types.hh"
108#include "G4VSolid.hh"
109#include "G4Voxelizer.hh"
110#include "G4VFacet.hh"
111
117
119{
120 public:
121
122 G4bool operator() (const G4VertexInfo& l, const G4VertexInfo& r) const
123 {
124 return l.mag2 == r.mag2 ? l.id < r.id : l.mag2 < r.mag2;
125 }
126};
127
128/**
129 * @brief G4TessellatedSolid is a solid defined by a number of facets.
130 * It is important that the supplied facets shall form a fully enclose space
131 * which is the solid. The facets can be of two types, G4TriangularFacet and
132 * G4QuadrangularFacet.
133 */
134
136{
137 public:
138
139 /**
140 * Default Constructor.
141 */
143
144 /**
145 * Constructor with solid's name.
146 * @param[in] name The name of the solid.
147 */
148 G4TessellatedSolid (const G4String& name);
149
150 /**
151 * Destructor. Clearing all allocated facets and data.
152 */
153 ~G4TessellatedSolid () override;
154
155 /**
156 * Fake default constructor for usage restricted to direct object
157 * persistency for clients requiring preallocation of memory for
158 * persistifiable objects.
159 */
160 G4TessellatedSolid(__void__&);
161
162 /**
163 * Copy constructor and assignment operator.
164 */
167
168 /**
169 * Operator +=, allowing to add two tessellated solids together, so
170 * that the solid on the left includes all of the facets in the solid
171 * on the right. To note that copies of the facets are generated, rather
172 * than using the original facet set of the solid on the right.
173 */
175
176 /**
177 * Methods for adding or retrieving a facet given an index.
178 */
179 G4bool AddFacet (G4VFacet* aFacet);
180 inline G4VFacet* GetFacet (G4int i) const;
181
182 /**
183 * Accessors.
184 */
185 G4int GetNumberOfFacets () const;
186 G4int GetFacetIndex (const G4ThreeVector& p) const;
187 G4double GetMinXExtent () const;
188 G4double GetMaxXExtent () const;
189 G4double GetMinYExtent () const;
190 G4double GetMaxYExtent () const;
191 G4double GetMinZExtent () const;
192 G4double GetMaxZExtent () const;
193
194
195 /**
196 * Concrete implementations of the expected query interfaces for
197 * solids, as defined in the base class G4VSolid.
198 */
199 EInside Inside (const G4ThreeVector& p) const override;
200 G4ThreeVector SurfaceNormal(const G4ThreeVector& p) const override;
202 const G4ThreeVector& v)const override;
203 G4double DistanceToIn(const G4ThreeVector& p) const override;
204 G4double DistanceToOut(const G4ThreeVector& p) const override;
206 const G4ThreeVector& v,
207 const G4bool calcNorm,
208 G4bool* validNorm,
209 G4ThreeVector* norm) const override;
210
211 /**
212 * Returns the outwards pointing unit normal of the shape for the
213 * surface closest to the point at offset 'p'.
214 * @param[in] p The point coordinates.
215 * @param[out] n The returned normal vector.
216 * @returns false if not a valid normal.
217 */
218 virtual G4bool Normal (const G4ThreeVector& p, G4ThreeVector& n) const;
219
220 /**
221 * Returns the the safety distance from outside the solid at a point 'p'.
222 * @param[in] p The point coordinates.
223 * @param[in] aAccurate Accuracy flag, if false quickly computes and
224 * returns the distance to the voxels bounding-box.
225 * @returns The safety distance.
226 */
228 G4bool aAccurate = false) const;
229
230 /**
231 * Returns the the safety distance from inside the solid at a point 'p'.
232 * @param[in] p The point coordinates.
233 * @param[in] aAccurate Not used.
234 * @returns The safety distance.
235 */
236 virtual G4double SafetyFromInside (const G4ThreeVector& p,
237 G4bool aAccurate = false) const;
238
239 /**
240 * Returns the type ID, "G4TessellatedSolid" of the solid.
241 */
242 G4GeometryType GetEntityType () const override;
243
244 /**
245 * Returns true as the solid has only planar faces.
246 */
247 G4bool IsFaceted () const override;
248
249 /**
250 * Streams the object contents to an output stream.
251 */
252 std::ostream& StreamInfo(std::ostream& os) const override;
253
254 /**
255 * Makes a clone of the object for use in multi-treading.
256 * @returns A pointer to the new cloned allocated solid.
257 */
258 G4VSolid* Clone() const override;
259
260 /**
261 * Returns a random point located and uniformly distributed on the
262 * surface of the solid.
263 */
264 G4ThreeVector GetPointOnSurface() const override;
265
266 /**
267 * Returning an estimation of the solid volume (capacity) and
268 * surface area, in internal units.
269 */
270 G4double GetSurfaceArea() override;
271 G4double GetCubicVolume() override;
272
273 /**
274 * Modifier and accessor to close/finalise the solid.
275 */
276 void SetSolidClosed (const G4bool t);
277 G4bool GetSolidClosed () const;
278
279 /**
280 * Checks the structure of the solid.
281 * @returns A value, sum of the following defect indicators, if any
282 * (0 means no defects):
283 * 1 - cubic volume is negative, wrong orientation of facets;
284 * 2 - some facets have wrong orientation;
285 * 4 - holes in the surface.
286 */
287 G4int CheckStructure() const;
288
289 /**
290 * Allowing to tune the maximum number of voxels to use for optimisation.
291 */
292 inline void SetMaxVoxels(G4int max);
293
294 /**
295 * Returns the voxels structure.
296 */
297 inline G4Voxelizer& GetVoxels();
298
299 /**
300 * Calculates the minimum and maximum extent of the solid, when under the
301 * specified transform, and within the specified limits.
302 * @param[in] pAxis The axis along which compute the extent.
303 * @param[in] pVoxelLimit The limiting space dictated by voxels.
304 * @param[in] pTransform The internal transformation applied to the solid.
305 * @param[out] pMin The minimum extent value.
306 * @param[out] pMax The maximum extent value.
307 * @returns True if the solid is intersected by the extent region.
308 */
309 G4bool CalculateExtent(const EAxis pAxis,
310 const G4VoxelLimits& pVoxelLimit,
311 const G4AffineTransform& pTransform,
312 G4double& pMin, G4double& pMax) const override;
313
314 /**
315 * Computes the bounding limits of the solid.
316 * @param[out] pMin The minimum bounding limit point.
317 * @param[out] pMax The maximum bounding limit point.
318 */
319 void BoundingLimits(G4ThreeVector& pMin, G4ThreeVector& pMax) const override;
320
321 /**
322 * Methods for creating graphical representations (i.e. for visualisation).
323 */
324 G4Polyhedron* CreatePolyhedron() const override;
325 G4Polyhedron* GetPolyhedron() const override;
326 void DescribeYourselfTo(G4VGraphicsScene& scene) const override;
327 G4VisExtent GetExtent() const override;
328
329 /**
330 * Loggers reporting the total allocated memory.
331 */
335
336 private:
337
338 /**
339 * Initialisation/reset of data, used in constructors and operators.
340 */
341 void Initialize();
342
343 /**
344 * Resetting/copying data, used in constructors and operators.
345 */
346 void DeleteObjects ();
347 void CopyObjects (const G4TessellatedSolid& s);
348
349 /**
350 * Internal methods used for computing distances with or without voxels.
351 */
352 G4double DistanceToOutNoVoxels(const G4ThreeVector& p,
353 const G4ThreeVector& v,
354 G4ThreeVector& aNormalVector,
355 G4bool& aConvex,
356 G4double aPstep = kInfinity) const;
357 G4double DistanceToInCandidates(const std::vector<G4int>& candidates,
358 const G4ThreeVector& aPoint,
359 const G4ThreeVector& aDirection) const;
360 void DistanceToOutCandidates(const std::vector<G4int>& candidates,
361 const G4ThreeVector& aPoint,
362 const G4ThreeVector& direction,
363 G4double& minDist,
364 G4ThreeVector& minNormal,
365 G4int& minCandidate) const;
366 G4double DistanceToInNoVoxels(const G4ThreeVector& p,
367 const G4ThreeVector& v,
368 G4double aPstep = kInfinity) const;
369 G4double DistanceToInCore(const G4ThreeVector &p, const G4ThreeVector& v,
370 G4double aPstep = kInfinity) const;
371 G4double DistanceToOutCore(const G4ThreeVector& p, const G4ThreeVector& v,
372 G4ThreeVector& aNormalVector,
373 G4bool& aConvex,
374 G4double aPstep = kInfinity) const;
375
376 /**
377 * Finds those facets that have surface planes that bound the volume.
378 * To note that this is going to reject concave surfaces as being extreme.
379 */
380 void SetExtremeFacets();
381
382 /**
383 * Internal methods used for checking if a point 'p' is inside the solid
384 * in presence or not of voxels.
385 */
386 EInside InsideNoVoxels (const G4ThreeVector& p) const;
387 EInside InsideVoxels(const G4ThreeVector& p) const;
388
389 /**
390 * Performs the voxelisation of the shape, building the optimisation
391 * structure, according to the specified parameters.
392 */
393 void Voxelize();
394
395 /**
396 * Creates a list of vertices with an additional sorted list, where all
397 * the items are sorted by magnitude of vertices vector.
398 */
399 void CreateVertexList();
400
401 /**
402 * Utilities for preparation of voxels indeces. Used in Voxelize() function.
403 */
404 void PrecalculateInsides();
405 G4int SetAllUsingStack(const std::vector<G4int>& voxel,
406 const std::vector<G4int>& max,
407 G4bool status, G4SurfBits& checked);
408
409 /**
410 * Utility to compare sorted voxels.
411 */
412 static G4bool CompareSortedVoxel(const std::pair<G4int, G4double>& l,
413 const std::pair<G4int, G4double>& r);
414
415
416 /**
417 * Prepares a set of predefined random vectors, used to generate rays
418 * from a user-defined point. Used in Inside() function to determine
419 * whether the point is inside or outside of the tessellated solid.
420 * All vectors should be unit vectors.
421 */
422 void SetRandomVectors();
423
424 /**
425 * Computes the minimum distance of a point 'p' from a 'facet'.
426 */
427 G4double MinDistanceFacet(const G4ThreeVector& p, G4bool simple,
428 G4VFacet* &facet) const;
429
430 /**
431 * Computes if a point 'p' is outside or not of the computed extent,
432 * given a 'tolerance'. Used internally in Inside() functions.
433 * @returns true if the point is within the extent.
434 */
435 inline G4bool OutsideOfExtent(const G4ThreeVector& p,
436 G4double tolerance = 0.0) const;
437
438 protected:
439
441
442 private:
443
444 mutable G4bool fRebuildPolyhedron = false;
445 mutable G4Polyhedron* fpPolyhedron = nullptr;
446
447 std::vector<G4VFacet*> fFacets;
448 std::set<G4VFacet*> fExtremeFacets; // Does all other facets lie on
449 // or behind this surface?
450
451 G4GeometryType fGeometryType;
452 G4double fCubicVolume = 0.0;
453 G4double fSurfaceArea = 0.0;
454
455 std::vector<G4ThreeVector> fVertexList;
456
457 std::set<G4VertexInfo,G4VertexComparator> fFacetList;
458
459 G4ThreeVector fMinExtent, fMaxExtent;
460
461 G4bool fSolidClosed = false;
462
463 std::vector<G4ThreeVector> fRandir;
464
465 G4int fMaxTries;
466
467 G4Voxelizer fVoxels; // Pointer to the voxelized solid
468
469 G4SurfBits fInsides;
470};
471
472///////////////////////////////////////////////////////////////////////////////
473// Inline Methods
474///////////////////////////////////////////////////////////////////////////////
475
477{
478 return fFacets[i];
479}
480
482{
483 fVoxels.SetMaxVoxels(max);
484}
485
487{
488 return fVoxels;
489}
490
491inline G4bool G4TessellatedSolid::OutsideOfExtent(const G4ThreeVector& p,
492 G4double tolerance) const
493{
494 return ( p.x() < fMinExtent.x() - tolerance
495 || p.x() > fMaxExtent.x() + tolerance
496 || p.y() < fMinExtent.y() - tolerance
497 || p.y() > fMaxExtent.y() + tolerance
498 || p.z() < fMinExtent.z() - tolerance
499 || p.z() > fMaxExtent.z() + tolerance);
500}
501
502#endif
503
504#endif
CLHEP::Hep3Vector G4ThreeVector
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
G4String G4GeometryType
Definition G4VSolid.hh:70
double z() const
double x() const
double y() const
G4AffineTransform is a class for geometric affine transformations. It supports efficient arbitrary ro...
G4SurfBits provides a simple container of bits, to be used for optimization of tessellated surfaces....
Definition G4SurfBits.hh:57
virtual G4bool Normal(const G4ThreeVector &p, G4ThreeVector &n) const
G4double GetMinYExtent() const
G4double GetMinZExtent() const
EInside Inside(const G4ThreeVector &p) const override
G4TessellatedSolid & operator=(const G4TessellatedSolid &right)
void DescribeYourselfTo(G4VGraphicsScene &scene) const override
G4Polyhedron * GetPolyhedron() const override
G4TessellatedSolid & operator+=(const G4TessellatedSolid &right)
G4bool AddFacet(G4VFacet *aFacet)
G4int GetNumberOfFacets() const
G4double DistanceToOut(const G4ThreeVector &p) const override
G4ThreeVector GetPointOnSurface() const override
G4double GetMaxYExtent() const
G4double GetMaxZExtent() const
G4double GetMaxXExtent() const
G4bool GetSolidClosed() const
G4VFacet * GetFacet(G4int i) const
virtual G4double SafetyFromInside(const G4ThreeVector &p, G4bool aAccurate=false) const
G4double GetMinXExtent() const
void SetSolidClosed(const G4bool t)
G4bool CalculateExtent(const EAxis pAxis, const G4VoxelLimits &pVoxelLimit, const G4AffineTransform &pTransform, G4double &pMin, G4double &pMax) const override
G4VisExtent GetExtent() const override
G4bool IsFaceted() const override
G4Voxelizer & GetVoxels()
void BoundingLimits(G4ThreeVector &pMin, G4ThreeVector &pMax) const override
G4VSolid * Clone() const override
G4double GetSurfaceArea() override
G4int GetFacetIndex(const G4ThreeVector &p) const
virtual G4double SafetyFromOutside(const G4ThreeVector &p, G4bool aAccurate=false) const
void SetMaxVoxels(G4int max)
G4double GetCubicVolume() override
std::ostream & StreamInfo(std::ostream &os) const override
G4ThreeVector SurfaceNormal(const G4ThreeVector &p) const override
G4double DistanceToIn(const G4ThreeVector &p, const G4ThreeVector &v) const override
G4Polyhedron * CreatePolyhedron() const override
G4GeometryType GetEntityType() const override
G4VFacet is a base class defining the facets which are components of a G4TessellatedSolid shape.
Definition G4VFacet.hh:56
G4VSolid(const G4String &name)
Definition G4VSolid.cc:59
G4bool operator()(const G4VertexInfo &l, const G4VertexInfo &r) const
G4VoxelLimits represents limitation/restrictions of space, where restrictions are only made perpendic...
G4Voxelizer is a tool for generating the optimisation structure of tessellated surfaces and solids po...
EAxis
Definition geomdefs.hh:54
EInside
Definition geomdefs.hh:67