Geant4  10.03
G4IntersectionSolid.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 // $Id: G4IntersectionSolid.cc 101046 2016-11-04 10:44:26Z gcosmo $
28 //
29 // Implementation of methods for the class G4IntersectionSolid
30 //
31 // History:
32 //
33 // 17.02.05 V.Grichine: bug was fixed in DistanceToIn(p,v) based on algorithm
34 // proposed by Dino Bazzacco <dino.bazzacco@pd.infn.it>
35 // 29.05.01 V.Grichine: bug was fixed in DistanceToIn(p,v)
36 // 16.03.01 V.Grichine: modifications in CalculateExtent() and Inside()
37 // 29.07.99 V.Grichine: modifications in DistanceToIn(p,v)
38 // 12.09.98 V.Grichine: first implementation
39 //
40 // --------------------------------------------------------------------
41 
42 
43 #include <sstream>
44 
45 #include "G4IntersectionSolid.hh"
46 
47 #include "G4SystemOfUnits.hh"
48 #include "G4VoxelLimits.hh"
49 #include "G4VPVParameterisation.hh"
50 
51 #include "G4VGraphicsScene.hh"
52 #include "G4Polyhedron.hh"
53 #include "HepPolyhedronProcessor.h"
54 
56 //
57 // Transfer all data members to G4BooleanSolid which is responsible
58 // for them. pName will be in turn sent to G4VSolid
59 //
60 
62  G4VSolid* pSolidA ,
63  G4VSolid* pSolidB )
64  : G4BooleanSolid(pName,pSolidA,pSolidB)
65 {
66 }
67 
69 //
70 
72  G4VSolid* pSolidA,
73  G4VSolid* pSolidB,
74  G4RotationMatrix* rotMatrix,
75  const G4ThreeVector& transVector )
76  : G4BooleanSolid(pName,pSolidA,pSolidB,rotMatrix,transVector)
77 {
78 }
79 
81 //
82 //
83 
85  G4VSolid* pSolidA,
86  G4VSolid* pSolidB,
87  const G4Transform3D& transform )
88  : G4BooleanSolid(pName,pSolidA,pSolidB,transform)
89 {
90 }
91 
93 //
94 // Fake default constructor - sets only member data and allocates memory
95 // for usage restricted to object persistency.
96 
98  : G4BooleanSolid(a)
99 {
100 }
101 
103 //
104 //
105 
107 {
108 }
109 
111 //
112 // Copy constructor
113 
115  : G4BooleanSolid (rhs)
116 {
117 }
118 
120 //
121 // Assignment operator
122 
125 {
126  // Check assignment to self
127  //
128  if (this == &rhs) { return *this; }
129 
130  // Copy base class data
131  //
133 
134  return *this;
135 }
136 
138 //
139 // Get bounding box
140 
141 void
143 {
144  G4ThreeVector minA,maxA, minB,maxB;
145  fPtrSolidA->Extent(minA,maxA);
146  fPtrSolidB->Extent(minB,maxB);
147 
148  pMin.set(std::max(minA.x(),minB.x()),
149  std::max(minA.y(),minB.y()),
150  std::max(minA.z(),minB.z()));
151 
152  pMax.set(std::min(maxA.x(),maxB.x()),
153  std::min(maxA.y(),maxB.y()),
154  std::min(maxA.z(),maxB.z()));
155 
156  // Check correctness of the bounding box
157  //
158  if (pMin.x() >= pMax.x() || pMin.y() >= pMax.y() || pMin.z() >= pMax.z())
159  {
160  std::ostringstream message;
161  message << "Bad bounding box (min >= max) for solid: "
162  << GetName() << " !"
163  << "\npMin = " << pMin
164  << "\npMax = " << pMax;
165  G4Exception("G4IntersectionSolid::Extent()", "GeomMgt0001",
166  JustWarning, message);
167  DumpInfo();
168  }
169 }
170 
172 //
173 // Calculate extent under transform and specified limit
174 
175 G4bool
177  const G4VoxelLimits& pVoxelLimit,
178  const G4AffineTransform& pTransform,
179  G4double& pMin,
180  G4double& pMax) const
181 {
182  G4bool retA, retB, out;
183  G4double minA, minB, maxA, maxB;
184 
185  retA = fPtrSolidA
186  ->CalculateExtent( pAxis, pVoxelLimit, pTransform, minA, maxA);
187  retB = fPtrSolidB
188  ->CalculateExtent( pAxis, pVoxelLimit, pTransform, minB, maxB);
189 
190  if( retA && retB )
191  {
192  pMin = std::max( minA, minB );
193  pMax = std::min( maxA, maxB );
194  out = (pMax > pMin); // true;
195  }
196  else
197  {
198  out = false;
199  }
200 
201  return out; // It exists in this slice only if both exist in it.
202 }
203 
205 //
206 // Touching ? Empty intersection ?
207 
209 {
210  EInside positionA = fPtrSolidA->Inside(p) ;
211 
212  if( positionA == kOutside ) return kOutside ;
213 
214  EInside positionB = fPtrSolidB->Inside(p) ;
215 
216  if(positionA == kInside && positionB == kInside)
217  {
218  return kInside ;
219  }
220  else
221  {
222  if((positionA == kInside && positionB == kSurface) ||
223  (positionB == kInside && positionA == kSurface) ||
224  (positionA == kSurface && positionB == kSurface) )
225  {
226  return kSurface ;
227  }
228  else
229  {
230  return kOutside ;
231  }
232  }
233 }
234 
236 //
237 
240 {
242  EInside insideA, insideB;
243 
244  insideA= fPtrSolidA->Inside(p);
245  insideB= fPtrSolidB->Inside(p);
246 
247 #ifdef G4BOOLDEBUG
248  if( (insideA == kOutside) || (insideB == kOutside) )
249  {
250  G4cout << "WARNING - Invalid call in "
251  << "G4IntersectionSolid::SurfaceNormal(p)" << G4endl
252  << " Point p is outside !" << G4endl;
253  G4cout << " p = " << p << G4endl;
254  G4cerr << "WARNING - Invalid call in "
255  << "G4IntersectionSolid::SurfaceNormal(p)" << G4endl
256  << " Point p is outside !" << G4endl;
257  G4cerr << " p = " << p << G4endl;
258  }
259 #endif
260 
261  // OLD: if(fPtrSolidA->DistanceToOut(p) <= fPtrSolidB->DistanceToOut(p) )
262 
263  // On the surface of both is difficult ... treat it like on A now!
264  //
265  // if( (insideA == kSurface) && (insideB == kSurface) )
266  // normal= fPtrSolidA->SurfaceNormal(p) ;
267  // else
268  if( insideA == kSurface )
269  {
270  normal= fPtrSolidA->SurfaceNormal(p) ;
271  }
272  else if( insideB == kSurface )
273  {
274  normal= fPtrSolidB->SurfaceNormal(p) ;
275  }
276  // We are on neither surface, so we should generate an exception
277  else
278  {
280  normal= fPtrSolidA->SurfaceNormal(p) ;
281  else
282  normal= fPtrSolidB->SurfaceNormal(p) ;
283 #ifdef G4BOOLDEBUG
284  G4cout << "WARNING - Invalid call in "
285  << "G4IntersectionSolid::SurfaceNormal(p)" << G4endl
286  << " Point p is out of surface !" << G4endl;
287  G4cout << " p = " << p << G4endl;
288  G4cerr << "WARNING - Invalid call in "
289  << "G4IntersectionSolid::SurfaceNormal(p)" << G4endl
290  << " Point p is out of surface !" << G4endl;
291  G4cerr << " p = " << p << G4endl;
292 #endif
293  }
294 
295  return normal;
296 }
297 
299 //
300 // The same algorithm as in DistanceToIn(p)
301 
302 G4double
304  const G4ThreeVector& v ) const
305 {
306  G4double dist = 0.0;
307  if( Inside(p) == kInside )
308  {
309 #ifdef G4BOOLDEBUG
310  G4cout << "WARNING - Invalid call in "
311  << "G4IntersectionSolid::DistanceToIn(p,v)" << G4endl
312  << " Point p is inside !" << G4endl;
313  G4cout << " p = " << p << G4endl;
314  G4cout << " v = " << v << G4endl;
315  G4cerr << "WARNING - Invalid call in "
316  << "G4IntersectionSolid::DistanceToIn(p,v)" << G4endl
317  << " Point p is inside !" << G4endl;
318  G4cerr << " p = " << p << G4endl;
319  G4cerr << " v = " << v << G4endl;
320 #endif
321  }
322  else // if( Inside(p) == kSurface )
323  {
324  EInside wA = fPtrSolidA->Inside(p);
325  EInside wB = fPtrSolidB->Inside(p);
326 
327  G4ThreeVector pA = p, pB = p;
328  G4double dA = 0., dA1=0., dA2=0.;
329  G4double dB = 0., dB1=0., dB2=0.;
330  G4bool doA = true, doB = true;
331 
332  static const size_t max_trials=10000;
333  for (size_t trial=0; trial<max_trials; ++trial)
334  {
335  if(doA)
336  {
337  // find next valid range for A
338 
339  dA1 = 0.;
340 
341  if( wA != kInside )
342  {
343  dA1 = fPtrSolidA->DistanceToIn(pA, v);
344 
345  if( dA1 == kInfinity ) return kInfinity;
346 
347  pA += dA1*v;
348  }
349  dA2 = dA1 + fPtrSolidA->DistanceToOut(pA, v);
350  }
351  dA1 += dA;
352  dA2 += dA;
353 
354  if(doB)
355  {
356  // find next valid range for B
357 
358  dB1 = 0.;
359  if(wB != kInside)
360  {
361  dB1 = fPtrSolidB->DistanceToIn(pB, v);
362 
363  if(dB1 == kInfinity) return kInfinity;
364 
365  pB += dB1*v;
366  }
367  dB2 = dB1 + fPtrSolidB->DistanceToOut(pB, v);
368  }
369  dB1 += dB;
370  dB2 += dB;
371 
372  // check if they overlap
373 
374  if( dA1 < dB1 )
375  {
376  if( dB1 < dA2 ) return dB1;
377 
378  dA = dA2;
379  pA = p + dA*v; // continue from here
380  wA = kSurface;
381  doA = true;
382  doB = false;
383  }
384  else
385  {
386  if( dA1 < dB2 ) return dA1;
387 
388  dB = dB2;
389  pB = p + dB*v; // continue from here
390  wB = kSurface;
391  doB = true;
392  doA = false;
393  }
394  }
395  }
396 #ifdef G4BOOLDEBUG
397  G4Exception("G4IntersectionSolid::DistanceToIn(p,v)",
398  "GeomSolids0001", JustWarning,
399  "Reached maximum number of iterations! Returning zero.");
400 #endif
401  return dist ;
402 }
403 
405 //
406 // Approximate nearest distance from the point p to the intersection of
407 // two solids
408 
409 G4double
411 {
412 #ifdef G4BOOLDEBUG
413  if( Inside(p) == kInside )
414  {
415  G4cout << "WARNING - Invalid call in "
416  << "G4IntersectionSolid::DistanceToIn(p)" << G4endl
417  << " Point p is inside !" << G4endl;
418  G4cout << " p = " << p << G4endl;
419  G4cerr << "WARNING - Invalid call in "
420  << "G4IntersectionSolid::DistanceToIn(p)" << G4endl
421  << " Point p is inside !" << G4endl;
422  G4cerr << " p = " << p << G4endl;
423  }
424 #endif
425  EInside sideA = fPtrSolidA->Inside(p) ;
426  EInside sideB = fPtrSolidB->Inside(p) ;
427  G4double dist=0.0 ;
428 
429  if( sideA != kInside && sideB != kOutside )
430  {
431  dist = fPtrSolidA->DistanceToIn(p) ;
432  }
433  else
434  {
435  if( sideB != kInside && sideA != kOutside )
436  {
437  dist = fPtrSolidB->DistanceToIn(p) ;
438  }
439  else
440  {
441  dist = std::min(fPtrSolidA->DistanceToIn(p),
442  fPtrSolidB->DistanceToIn(p) ) ;
443  }
444  }
445  return dist ;
446 }
447 
449 //
450 // The same algorithm as DistanceToOut(p)
451 
452 G4double
454  const G4ThreeVector& v,
455  const G4bool calcNorm,
456  G4bool *validNorm,
457  G4ThreeVector *n ) const
458 {
459  G4bool validNormA, validNormB;
460  G4ThreeVector nA, nB;
461 
462 #ifdef G4BOOLDEBUG
463  if( Inside(p) == kOutside )
464  {
465  G4cout << "Position:" << G4endl << G4endl;
466  G4cout << "p.x() = " << p.x()/mm << " mm" << G4endl;
467  G4cout << "p.y() = " << p.y()/mm << " mm" << G4endl;
468  G4cout << "p.z() = " << p.z()/mm << " mm" << G4endl << G4endl;
469  G4cout << "Direction:" << G4endl << G4endl;
470  G4cout << "v.x() = " << v.x() << G4endl;
471  G4cout << "v.y() = " << v.y() << G4endl;
472  G4cout << "v.z() = " << v.z() << G4endl << G4endl;
473  G4cout << "WARNING - Invalid call in "
474  << "G4IntersectionSolid::DistanceToOut(p,v)" << G4endl
475  << " Point p is outside !" << G4endl;
476  G4cout << " p = " << p << G4endl;
477  G4cout << " v = " << v << G4endl;
478  G4cerr << "WARNING - Invalid call in "
479  << "G4IntersectionSolid::DistanceToOut(p,v)" << G4endl
480  << " Point p is outside !" << G4endl;
481  G4cerr << " p = " << p << G4endl;
482  G4cerr << " v = " << v << G4endl;
483  }
484 #endif
485  G4double distA = fPtrSolidA->DistanceToOut(p,v,calcNorm,&validNormA,&nA) ;
486  G4double distB = fPtrSolidB->DistanceToOut(p,v,calcNorm,&validNormB,&nB) ;
487 
488  G4double dist = std::min(distA,distB) ;
489 
490  if( calcNorm )
491  {
492  if ( distA < distB )
493  {
494  *validNorm = validNormA;
495  *n = nA;
496  }
497  else
498  {
499  *validNorm = validNormB;
500  *n = nB;
501  }
502  }
503 
504  return dist ;
505 }
506 
508 //
509 // Inverted algorithm of DistanceToIn(p)
510 
511 G4double
513 {
514 #ifdef G4BOOLDEBUG
515  if( Inside(p) == kOutside )
516  {
517  G4cout << "WARNING - Invalid call in "
518  << "G4IntersectionSolid::DistanceToOut(p)" << G4endl
519  << " Point p is outside !" << G4endl;
520  G4cout << " p = " << p << G4endl;
521  G4cerr << "WARNING - Invalid call in "
522  << "G4IntersectionSolid::DistanceToOut(p)" << G4endl
523  << " Point p is outside !" << G4endl;
524  G4cerr << " p = " << p << G4endl;
525  }
526 #endif
527 
528  return std::min(fPtrSolidA->DistanceToOut(p),
529  fPtrSolidB->DistanceToOut(p) ) ;
530 
531 }
532 
534 //
535 //
536 
537 void
539  const G4int,
540  const G4VPhysicalVolume* )
541 {
542 }
543 
545 //
546 //
547 
549 {
550  return G4String("G4IntersectionSolid");
551 }
552 
554 //
555 // Make a clone of the object
556 
558 {
559  return new G4IntersectionSolid(*this);
560 }
561 
563 //
564 //
565 
566 void
568 {
569  scene.AddSolid (*this);
570 }
571 
573 //
574 //
575 
576 G4Polyhedron*
578 {
579  HepPolyhedronProcessor processor;
580  // Stack components and components of components recursively
581  // See G4BooleanSolid::StackPolyhedron
582  G4Polyhedron* top = StackPolyhedron(processor, this);
583  G4Polyhedron* result = new G4Polyhedron(*top);
584  if (processor.execute(*result)) { return result; }
585  else { return 0; }
586 }
G4String GetName() const
G4VSolid * fPtrSolidB
virtual G4bool CalculateExtent(const EAxis pAxis, const G4VoxelLimits &pVoxelLimit, const G4AffineTransform &pTransform, G4double &pMin, G4double &pMax) const =0
void Extent(G4ThreeVector &pMin, G4ThreeVector &pMax) const
static constexpr double mm
Definition: G4SIunits.hh:115
static const G4double kInfinity
Definition: geomdefs.hh:42
EInside Inside(const G4ThreeVector &p) const
CLHEP::Hep3Vector G4ThreeVector
CLHEP::HepRotation G4RotationMatrix
std::vector< ExP01TrackerHit * > a
Definition: ExP01Classes.hh:33
G4bool CalculateExtent(const EAxis pAxis, const G4VoxelLimits &pVoxelLimit, const G4AffineTransform &pTransform, G4double &pMin, G4double &pMax) const
G4VSolid * Clone() const
G4IntersectionSolid(const G4String &pName, G4VSolid *pSolidA, G4VSolid *pSolidB)
virtual void AddSolid(const G4Box &)=0
int G4int
Definition: G4Types.hh:78
void DumpInfo() const
void ComputeDimensions(G4VPVParameterisation *p, const G4int n, const G4VPhysicalVolume *pRep)
static double normal(HepRandomEngine *eptr)
Definition: RandPoisson.cc:77
G4ThreeVector SurfaceNormal(const G4ThreeVector &p) const
G4GLOB_DLL std::ostream G4cout
virtual EInside Inside(const G4ThreeVector &p) const =0
bool G4bool
Definition: G4Types.hh:79
virtual G4ThreeVector SurfaceNormal(const G4ThreeVector &p) const =0
G4GeometryType GetEntityType() const
virtual G4double DistanceToIn(const G4ThreeVector &p, const G4ThreeVector &v) const =0
HepGeom::Transform3D G4Transform3D
const G4int n
G4double DistanceToIn(const G4ThreeVector &p, const G4ThreeVector &v) const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
#define processor
Definition: xmlparse.cc:617
G4Polyhedron * CreatePolyhedron() const
G4double DistanceToOut(const G4ThreeVector &p, const G4ThreeVector &v, const G4bool calcNorm=false, G4bool *validNorm=0, G4ThreeVector *n=0) const
T max(const T t1, const T t2)
brief Return the largest of the two arguments
EInside
Definition: geomdefs.hh:58
EAxis
Definition: geomdefs.hh:54
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
G4VSolid * fPtrSolidA
#define G4endl
Definition: G4ios.hh:61
G4BooleanSolid & operator=(const G4BooleanSolid &rhs)
void DescribeYourselfTo(G4VGraphicsScene &scene) const
double G4double
Definition: G4Types.hh:76
virtual G4double DistanceToOut(const G4ThreeVector &p, const G4ThreeVector &v, const G4bool calcNorm=false, G4bool *validNorm=0, G4ThreeVector *n=0) const =0
G4IntersectionSolid & operator=(const G4IntersectionSolid &rhs)
virtual void Extent(G4ThreeVector &pMin, G4ThreeVector &pMax) const
Definition: G4VSolid.cc:626
G4Polyhedron * StackPolyhedron(HepPolyhedronProcessor &, const G4VSolid *) const
G4GLOB_DLL std::ostream G4cerr