Import Geant4 9.1.0 source tree

This commit is contained in:
Gabriele Cosmo
2016-06-09 15:37:50 +02:00
parent a8e9364cea
commit 96c8bcd0af
6923 changed files with 198390 additions and 41849 deletions
@@ -25,7 +25,7 @@
//
//
// $Id: G4ClippablePolygon.cc,v 1.12 2007/05/11 13:54:28 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -24,7 +24,7 @@
// ********************************************************************
//
// $Id: G4Ellipsoid.cc,v 1.14 2007/05/18 07:39:56 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
// class G4Ellipsoid
//
@@ -23,8 +23,8 @@
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// $Id: G4EllipticalCone.cc,v 1.13 2007/05/18 07:39:56 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// $Id: G4EllipticalCone.cc,v 1.15 2007/08/21 12:58:36 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-01 $
//
// Implementation of G4EllipticalCone class
//
@@ -458,9 +458,9 @@ G4double G4EllipticalCone::DistanceToIn( const G4ThreeVector& p,
//
return (sigz < -halfTol) ? s : 0;
}
else if (xi/(xSemiAxis*xSemiAxis)*v.x() + yi/(ySemiAxis*ySemiAxis)*v.y() >= 0)
else if (xi/(xSemiAxis*xSemiAxis)*v.x()
+ yi/(ySemiAxis*ySemiAxis)*v.y() >= 0)
{
//
// Else, if we are traveling outwards, we know
// we must miss
@@ -498,7 +498,8 @@ G4double G4EllipticalCone::DistanceToIn( const G4ThreeVector& p,
{
return (sigz > -halfTol) ? s : 0;
}
else if (xi/(xSemiAxis*xSemiAxis)*v.x() + yi/(ySemiAxis*ySemiAxis)*v.y() >= 0)
else if (xi/(xSemiAxis*xSemiAxis)*v.x()
+ yi/(ySemiAxis*ySemiAxis)*v.y() >= 0)
{
// return kInfinity;
}
@@ -540,18 +541,20 @@ G4double G4EllipticalCone::DistanceToIn( const G4ThreeVector& p,
}
}
if (p.z() > zTopCut - 0.5*kCarTolerance && p.z() < zTopCut + 0.5*kCarTolerance )
if (p.z() > zTopCut - 0.5*kCarTolerance
&& p.z() < zTopCut + 0.5*kCarTolerance )
{
if (v.z() > 0.)
return kInfinity;
{ return kInfinity; }
return distMin = 0.;
}
if (p.z() < -zTopCut + 0.5*kCarTolerance && p.z() > -zTopCut - 0.5*kCarTolerance)
if (p.z() < -zTopCut + 0.5*kCarTolerance
&& p.z() > -zTopCut - 0.5*kCarTolerance)
{
if (v.z() < 0.)
return distMin = kInfinity;
{ return distMin = kInfinity; }
return distMin = 0.;
}
@@ -638,17 +641,18 @@ G4double G4EllipticalCone::DistanceToIn(const G4ThreeVector& p) const
G4double distR, distR2, distZ, maxDim;
G4double distRad;
// check if the point lies either below z=-zTopCut in bottom elliptical region
// or on top within cut elliptical region
// check if the point lies either below z=-zTopCut in bottom elliptical
// region or on top within cut elliptical region
//
if( (p.z() < -zTopCut) && (sqr(p.x()/xSemiAxis) + sqr(p.y()/ySemiAxis)
< sqr(zTopCut + zheight + 0.5*kCarTolerance )) )
if( (p.z() <= -zTopCut) && (sqr(p.x()/xSemiAxis) + sqr(p.y()/ySemiAxis)
<= sqr(zTopCut + zheight + 0.5*kCarTolerance )) )
{
return distZ = std::fabs(zTopCut - p.z());
//return distZ = std::fabs(zTopCut - p.z());
return distZ = std::fabs(zTopCut + p.z());
}
if( (p.z() > zTopCut) && (sqr(p.x()/xSemiAxis)+sqr(p.y()/ySemiAxis)
< sqr(zheight - zTopCut + kCarTolerance/2.0 )) )
if( (p.z() >= zTopCut) && (sqr(p.x()/xSemiAxis)+sqr(p.y()/ySemiAxis)
<= sqr(zheight - zTopCut + kCarTolerance/2.0 )) )
{
return distZ = std::fabs(p.z() - zTopCut);
}
@@ -702,9 +706,9 @@ G4double G4EllipticalCone::DistanceToOut(const G4ThreeVector& p,
distMin = kInfinity;
surface = kNoSurf;
#ifdef G4SPECSDEBUG
#ifdef G4SPECSDEBUG
G4cout << "DToOut: vz < 0" << G4endl ;
#endif
#endif
if (v.z() < 0.0)
{
@@ -722,9 +726,9 @@ G4double G4EllipticalCone::DistanceToOut(const G4ThreeVector& p,
surface = kPlaneSurf;
}
#ifdef G4SPECSDEBUG
#ifdef G4SPECSDEBUG
G4cout << "DToOut: vz > 0" << G4endl ;
#endif
#endif
if (v.z() > 0.0)
{
@@ -744,12 +748,6 @@ G4double G4EllipticalCone::DistanceToOut(const G4ThreeVector& p,
// if we are here then it either intersects or grazes the
// curved surface...
//
#ifdef G4SPECSDEBUG
G4cout << " distMin = " << distMin << G4endl ;
G4cout << " if we are here then it either intersects or grazes the curved surface..." << G4endl ;
#endif
G4double A = sqr(v.x()/xSemiAxis) + sqr(v.y()/ySemiAxis) - sqr(v.z());
G4double B = 2.*(v.x()*p.x()/sqr(xSemiAxis) +
v.y()*p.y()/sqr(ySemiAxis) + v.z()*(zheight-p.z()));
@@ -768,18 +766,22 @@ G4double G4EllipticalCone::DistanceToOut(const G4ThreeVector& p,
G4double plus = (-B+std::sqrt(discr))/(2.*A);
G4double minus = (-B-std::sqrt(discr))/(2.*A);
if ( plus > 0.5*kCarTolerance && minus > 0.5*kCarTolerance ) { // take the shorter distance
lambda = std::fabs(plus) < std::fabs(minus) ? plus:minus;
if ( plus > 0.5*kCarTolerance && minus > 0.5*kCarTolerance )
{
// take the shorter distance
//
lambda = std::fabs(plus) < std::fabs(minus) ? plus : minus;
}
else { // at least one solution is close to zero or negaive -> take the longer distance
lambda = std::fabs(plus) > std::fabs(minus) ? plus:minus;
else
{
// at least one solution is close to zero or negative
// so, take small positive solution or zero
//
lambda = plus > -0.5*kCarTolerance ? plus : 0;
}
#ifdef G4SPECSDEBUG
G4cout << "plus,minus, lambda = " << plus << ", " << minus << ", " << lambda << G4endl ;
#endif
if ( std::fabs(lambda) < distMin ) {
if ( std::fabs(lambda) < distMin )
{
distMin = std::fabs(lambda);
surface = kCurvedSurf;
}
@@ -846,7 +848,7 @@ G4double G4EllipticalCone::DistanceToOut(const G4ThreeVector& p,
//
G4double G4EllipticalCone::DistanceToOut(const G4ThreeVector& p) const
{
G4double rad, distR, distZ, distMin=0.;
G4double rad,roo,roo1, distR, distZ, distMin=0.;
G4double minAxis = xSemiAxis < ySemiAxis ? xSemiAxis : ySemiAxis;
#ifdef G4SPECSDEBUG
@@ -871,15 +873,20 @@ G4double G4EllipticalCone::DistanceToOut(const G4ThreeVector& p) const
if( sqr(p.x()/minAxis)+sqr(p.y()/minAxis) < sqr(zheight - p.z()) )
{
rad = std::sqrt(sqr(p.x()) + sqr(p.y()));
distZ = p.z() + (distMin*(rad-distMin*zheight)-p.z())/(1+sqr(distMin));
distR = rad-(distMin*(zheight-distZ ));
distMin = std::sqrt(sqr(distR) + sqr(distZ));
distMin = distMin < std::fabs(p.z() + zTopCut)
? distMin : std::fabs(p.z() + zTopCut);
distMin = distMin < std::fabs(zTopCut - p.z())
? distMin : std::fabs(zTopCut - p.z());
roo = minAxis*(zheight-p.z()); // radius of cone at z= p.z()
roo1 = minAxis*(zheight-zTopCut); // radius of cone at z=+zTopCut
distZ=zTopCut - std::fabs(p.z()) ;
distR=(roo-rad)/(std::sqrt(1+sqr(minAxis)));
if(rad>roo1)
{
distMin=(zTopCut-p.z())*(roo-rad)/(roo-roo1);
distMin=std::min(distMin,distR);
}
distMin=std::min(distR,distZ);
}
return distMin;
}
@@ -25,7 +25,7 @@
//
//
// $Id: G4EllipticalTube.cc,v 1.27 2006/10/20 13:45:21 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -25,7 +25,7 @@
//
//
// $Id: G4EnclosingCylinder.cc,v 1.10 2007/05/11 13:54:29 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -25,7 +25,7 @@
//
//
// $Id: G4ExtrudedSolid.cc,v 1.7 2007/05/02 14:59:31 gunter Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -26,7 +26,7 @@
//
// $Id: G4Hype.cc,v 1.25 2006/10/20 13:45:21 gcosmo Exp $
// $Original: G4Hype.cc,v 1.0 1998/06/09 16:57:50 safai Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -25,7 +25,7 @@
//
//
// $Id: G4IntersectingCone.cc,v 1.8 2006/06/29 18:48:38 gunter Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
File diff suppressed because it is too large Load Diff
@@ -24,8 +24,8 @@
// ********************************************************************
//
//
// $Id: G4PolyPhiFace.cc,v 1.12 2007/05/31 13:52:48 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// $Id: G4PolyPhiFace.cc,v 1.13 2007/07/19 12:57:14 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -269,6 +269,7 @@ void G4PolyPhiFace::Diagnose( G4VSolid *owner )
// for usage restricted to object persistency.
//
G4PolyPhiFace::G4PolyPhiFace( __void__&)
: edges(0), corners(0)
{
}
@@ -24,8 +24,8 @@
// ********************************************************************
//
//
// $Id: G4Polycone.cc,v 1.37 2007/04/26 13:34:04 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// $Id: G4Polycone.cc,v 1.39 2007/10/02 09:50:46 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -313,7 +313,7 @@ void G4Polycone::Create( G4double phiStart,
// for usage restricted to object persistency.
//
G4Polycone::G4Polycone( __void__& a )
: G4VCSGfaceted(a), genericPcon(false),
: G4VCSGfaceted(a), genericPcon(false), corners(0),
original_parameters(0), enclosingCylinder(0)
{
}
@@ -638,7 +638,6 @@ G4ThreeVector G4Polycone::GetPointOnCone(G4double fRmin1, G4double fRmax1,
fRmax2-((zRand-fDz)/(2.*fDz))*(fRmax1-fRmax2));
point = G4ThreeVector (rRand1*std::cos(startPhi),
rRand1*std::sin(startPhi), zRand);
G4cout<<"Point3="<<point<<G4endl;
}
else
{
@@ -24,8 +24,8 @@
// ********************************************************************
//
//
// $Id: G4PolyconeSide.cc,v 1.16 2007/05/31 13:52:48 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// $Id: G4PolyconeSide.cc,v 1.17 2007/08/13 10:33:04 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -60,7 +60,7 @@ G4PolyconeSide::G4PolyconeSide( const G4PolyconeSideRZ *prevRZ,
G4double theDeltaPhi,
G4bool thePhiIsOpen,
G4bool isAllBehind )
: corners(0)
: ncorners(0), corners(0)
{
kCarTolerance = G4GeometryTolerance::GetInstance()->GetSurfaceTolerance();
@@ -85,7 +85,8 @@ G4PolyconeSide::G4PolyconeSide( const G4PolyconeSideRZ *prevRZ,
//
// Calculate corner coordinates
//
corners = new G4ThreeVector[4];
ncorners = 4;
corners = new G4ThreeVector[ncorners];
corners[0] = G4ThreeVector( tail->r*std::cos(startPhi),
tail->r*std::sin(startPhi), tail->z );
@@ -152,7 +153,7 @@ G4PolyconeSide::G4PolyconeSide( const G4PolyconeSideRZ *prevRZ,
// for usage restricted to object persistency.
//
G4PolyconeSide::G4PolyconeSide( __void__& )
: cone(0), corners(0)
: phiIsOpen(false), cone(0), ncorners(0), corners(0)
{
}
@@ -229,7 +230,8 @@ void G4PolyconeSide::CopyStuff( const G4PolyconeSide &source )
if (phiIsOpen)
{
corners = new G4ThreeVector[4];
ncorners = 4;
corners = new G4ThreeVector[ncorners];
corners[0] = source.corners[0];
corners[1] = source.corners[1];
@@ -24,8 +24,8 @@
// ********************************************************************
//
//
// $Id: G4Polyhedra.cc,v 1.33 2007/01/22 12:58:53 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// $Id: G4Polyhedra.cc,v 1.36 2007/07/12 15:52:21 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -677,14 +677,16 @@ G4ThreeVector G4Polyhedra::GetPointOnSurface() const
G4double chose, totArea=0., Achose1, Achose2,
rad1, rad2, sinphi1, sinphi2, cosphi1, cosphi2;
G4double a, b, l2, rang,
ksi = (endPhi-startPhi)/(double)numSide,
totalPhi,ksi,
area, aTop=0., aBottom=0.,zVal=0.;
G4ThreeVector p0, p1, p2, p3;
std::vector<G4double> aVector1;
std::vector<G4double> aVector2;
std::vector<G4double> aVector3;
G4double cosksi = std::cos(ksi/2.);
totalPhi= (phiIsOpen) ? (endPhi-startPhi) : twopi;
ksi = totalPhi/numSide;
G4double cosksi = std::cos(ksi/2.);
// below we generate the areas relevant to our solid
//
@@ -750,14 +752,15 @@ G4ThreeVector G4Polyhedra::GetPointOnSurface() const
chose = RandFlat::shoot(0.,totArea+aTop+aBottom);
if( (chose >= 0.) && (chose < aTop + aBottom) )
{
chose = RandFlat::shoot(startPhi,endPhi);
chose = RandFlat::shoot(startPhi,startPhi+totalPhi);
rang = std::floor((chose-startPhi)/ksi-0.01);
if(rang<0)rang=0;
rang = std::fabs(rang);
sinphi1 = std::sin(startPhi+rang*ksi);
sinphi2 = std::sin(startPhi+(rang+1)*ksi);
cosphi1 = std::cos(startPhi+rang*ksi);
cosphi2 = std::cos(startPhi+(rang+1)*ksi);
chose = RandFlat::shoot(0., aTop + aBottom);
if(chose>=0. && chose<aTop)
{
@@ -798,8 +801,9 @@ G4ThreeVector G4Polyhedra::GetPointOnSurface() const
if( (chose>=0.) && (chose<numSide*aVector1[j]) )
{
chose = RandFlat::shoot(startPhi,endPhi);
rang = std::floor((chose-startPhi)/ksi-0.01);
chose = RandFlat::shoot(startPhi,startPhi+totalPhi);
rang = std::floor((chose-startPhi)/ksi-0.01);
if(rang<0)rang=0;
rang = std::fabs(rang);
rad1 = original_parameters->Rmax[j];
rad2 = original_parameters->Rmax[j+1];
@@ -816,14 +820,15 @@ G4ThreeVector G4Polyhedra::GetPointOnSurface() const
p2 = G4ThreeVector(rad2*cosphi2,rad2*sinphi2,zVal);
p3 = G4ThreeVector(rad2*cosphi1,rad2*sinphi1,zVal);
return GetPointOnPlane(p0,p1,p2,p3);
}
else if ( (chose >= numSide*aVector1[j])
&& (chose <= numSide*(aVector1[j]+aVector2[j])) )
{
chose = RandFlat::shoot(startPhi,endPhi);
chose = RandFlat::shoot(startPhi,startPhi+totalPhi);
rang = std::floor((chose-startPhi)/ksi-0.01);
if(rang<0)rang=0;
rang = std::fabs(rang);
rad1 = original_parameters->Rmin[j];
rad2 = original_parameters->Rmin[j+1];
@@ -840,7 +845,6 @@ G4ThreeVector G4Polyhedra::GetPointOnSurface() const
p2 = G4ThreeVector(rad2*cosphi2,rad2*sinphi2,zVal);
p3 = G4ThreeVector(rad2*cosphi1,rad2*sinphi1,zVal);
return GetPointOnPlane(p0,p1,p2,p3);
}
@@ -869,7 +873,6 @@ G4ThreeVector G4Polyhedra::GetPointOnSurface() const
original_parameters->Z_values[j+1]);
p3 = G4ThreeVector(rad2*cosphi1,rad2*sinphi1,
original_parameters->Z_values[j+1]);
return GetPointOnPlane(p0,p1,p2,p3);
}
@@ -1061,7 +1064,8 @@ G4Polyhedron* G4Polyhedra::CreatePolyhedron() const
nFaces = numSide * numCorner;;
xyz = new double3[nNodes];
faces_vec = new int4[nFaces];
const G4double dPhi = (endPhi - startPhi) / numSide;
// const G4double dPhi = (endPhi - startPhi) / numSide;
const G4double dPhi = twopi / numSide; // !phiIsOpen endPhi-startPhi = 360 degrees.
G4double phi = startPhi;
G4int ixyz = 0, iface = 0;
for (G4int iSide = 0; iSide < numSide; ++iSide)
@@ -25,7 +25,7 @@
//
//
// $Id: G4PolyhedraSide.cc,v 1.13 2007/05/31 13:52:48 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -24,8 +24,8 @@
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// $Id: G4QuadrangularFacet.cc,v 1.5 2007/02/15 17:04:10 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// $Id: G4QuadrangularFacet.cc,v 1.6 2007/08/23 14:49:23 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-01 $
//
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//
@@ -123,7 +123,7 @@ G4QuadrangularFacet::G4QuadrangularFacet (const G4ThreeVector Pt0,
surfaceNormal = normal1;
G4ThreeVector vtmp = 0.5 * (E[0] + E[1]);
centroid = P0 + vtmp;
circumcentre = P0 + vtmp;
radiusSqr = vtmp.mag2();
radius = std::sqrt(radiusSqr);
@@ -25,7 +25,7 @@
//
//
// $Id: G4ReduciblePolygon.cc,v 1.11 2006/06/29 18:48:53 gunter Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -25,7 +25,7 @@
//
//
// $Id: G4SolidExtentList.cc,v 1.5 2007/05/11 13:54:29 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -0,0 +1,281 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration and of QinetiQ Ltd, *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// $Id: G4TessellatedGeometryAlgorithms.cc,v 1.5 2007/12/12 16:51:12 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-01 $
//
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//
// MODULE: G4TessellatedGeometryAlgorithms.cc
//
// Date: 07/08/2005
// Author: Rickard Holmberg & Pete Truscott
// Organisation: QinetiQ Ltd, UK (PT)
// Customer: ESA-ESTEC / TEC-EES
// Contract:
//
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//
// CHANGE HISTORY
// --------------
//
// 07 August 2007, P R Truscott, QinetiQ Ltd, UK - Created, with member
// functions based on the work of Rickard Holmberg.
//
// 26 September 2007
// P R Truscott, qinetiQ Ltd, UK
// Updated to assign values of location array, not update
// just the pointer.
//
///////////////////////////////////////////////////////////////////////////////
#include "G4TessellatedGeometryAlgorithms.hh"
///////////////////////////////////////////////////////////////////////////////
//
// Pointer to single instance of class.
//
G4TessellatedGeometryAlgorithms* G4TessellatedGeometryAlgorithms::fInstance = 0;
///////////////////////////////////////////////////////////////////////////////
//
// G4TessellatedGeometryAlgorithms
//
// Constructor doesn't need to do anything since this class just allows access
// to the geometric algorithms contained in member functions.
//
G4TessellatedGeometryAlgorithms::G4TessellatedGeometryAlgorithms ()
{
}
///////////////////////////////////////////////////////////////////////////////
//
// GetInstance
//
// This is the access point for this singleton.
//
G4TessellatedGeometryAlgorithms* G4TessellatedGeometryAlgorithms::GetInstance()
{
static G4TessellatedGeometryAlgorithms worldStdGeom;
if (!fInstance)
{
fInstance = &worldStdGeom;
}
return fInstance;
}
///////////////////////////////////////////////////////////////////////////////
//
// IntersectLineAndTriangle2D
//
// Determines whether there is an intersection between a line defined
// by r = p + s.v and a triangle defined by verticies P0, P0+E0 and P0+E1.
//
// Here:
// p = 2D vector
// s = scaler on [0,infinity)
// v = 2D vector
// P0, E0 and E1 are 2D vectors
// Information about where the intersection occurs is returned in the
// variable location.
//
// This is based on the work of Rickard Holmberg.
//
G4bool G4TessellatedGeometryAlgorithms::IntersectLineAndTriangle2D (
const G4TwoVector p, const G4TwoVector v,
const G4TwoVector P0, const G4TwoVector E0, const G4TwoVector E1,
G4TwoVector location[2])
{
G4TwoVector loc0[2];
G4int e0i = IntersectLineAndLineSegment2D (p,v,P0,E0,loc0);
if (e0i == 2)
{
location[0] = loc0[0];
location[1] = loc0[1];
return true;
}
G4TwoVector loc1[2];
G4int e1i = IntersectLineAndLineSegment2D (p,v,P0,E1,loc1);
if (e1i == 2)
{
location[0] = loc1[0];
location[1] = loc1[1];
return true;
}
if ((e0i == 1) && (e1i == 1))
{
if ((loc0[0]-p).mag2() < (loc1[0]-p).mag2())
{
location[0] = loc0[0];
location[1] = loc1[0];
}
else
{
location[0] = loc1[0];
location[1] = loc0[0];
}
return true;
}
G4TwoVector P1 = P0 + E0;
G4TwoVector DE = E1 - E0;
G4TwoVector loc2[2];
G4int e2i = IntersectLineAndLineSegment2D (p,v,P1,DE,loc2);
if (e2i == 2)
{
location[0] = loc2[0];
location[1] = loc2[1];
return true;
}
if ((e0i == 0) && (e1i == 0) && (e2i == 0)) { return false; }
if ((e0i == 1) && (e2i == 1))
{
if ((loc0[0]-p).mag2() < (loc2[0]-p).mag2())
{
location[0] = loc0[0];
location[1] = loc2[0];
}
else
{
location[0] = loc2[0];
location[1] = loc0[0];
}
return true;
}
if ((e1i == 1) && (e2i == 1))
{
if ((loc1[0]-p).mag2() < (loc2[0]-p).mag2())
{
location[0] = loc1[0];
location[1] = loc2[0];
}
else
{
location[0] = loc2[0];
location[1] = loc1[0];
}
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
//
// IntersectLineAndLineSegment2D
//
// Determines whether there is an intersection between a line defined
// by r = P0 + s.D0 and a line-segment with endpoints P1 and P1+D1.
// Here:
// P0 = 2D vector
// s = scaler on [0,infinity)
// D0 = 2D vector
// P1 and D1 are 2D vectors
//
// This function returns:
// 0 - if there is no intersection;
// 1 - if there is a unique intersection;
// 2 - if the line and line-segments overlap, and the intersection is a
// segment itself.
// Information about where the intersection occurs is returned in the
// as ??.
//
// This is based on the work of Rickard Holmberg as well as material published
// by Philip J Schneider and David H Eberly, "Geometric Tools for Computer
// Graphics," ISBN 1-55860-694-0, pp 244-245, 2003.
//
G4int G4TessellatedGeometryAlgorithms::IntersectLineAndLineSegment2D (
const G4TwoVector P0, const G4TwoVector D0,
const G4TwoVector P1, const G4TwoVector D1,
G4TwoVector location[2])
{
G4TwoVector E = P1 - P0;
G4double kross = cross(D0,D1);
G4double sqrKross = kross * kross;
G4double sqrLen0 = D0.mag2();
G4double sqrLen1 = D1.mag2();
location[0] = G4TwoVector(0.0,0.0);
location[1] = G4TwoVector(0.0,0.0);
if (sqrKross > DBL_EPSILON * DBL_EPSILON * sqrLen0 * sqrLen1)
{
//
//
// The line and line segment are not parallel. Determine if the intersection
// is in positive s where r = P0 + s*D0, and for 0<=t<=1 where r = p1 + t*D1.
//
G4double s = cross(E,D1)/kross;
if (s < 0) return 0; // Intersection does not occur for positive s.
G4double t = cross(E,D0)/kross;
if (t < 0 || t > 1) return 0; // Intersection does not occur on line-segment.
//
//
// Intersection of lines is a single point on the forward-propagating line
// defined by r = P0 + s*D0, and the line segment defined by r = p1 + t*D1.
//
location[0] = P0 + s*D0;
return 1;
}
//
//
// Line and line segment are parallel. Determine whether they overlap or not.
//
G4double sqrLenE = E.mag2();
kross = cross(E,D0);
sqrKross = kross * kross;
if (sqrKross > DBL_EPSILON * DBL_EPSILON * sqrLen0 * sqrLenE)
{
return 0; //Lines are different.
}
//
//
// Lines are the same. Test for overlap.
//
G4double s0 = D0.dot(E)/sqrLen0;
G4double s1 = s0 + D0.dot(D1)/sqrLen0;
G4double smin = 0.0;
G4double smax = 0.0;
if (s0 < s1) {smin = s0; smax = s1;}
else {smin = s1; smax = s0;}
if (smax < 0.0) return 0;
else if (smin < 0.0)
{
location[0] = P0;
location[1] = P0 + smax*D0;
return 2;
}
else
{
location[0] = P0 + smin*D0;
location[1] = P0 + smax*D0;
return 2;
}
}
@@ -17,15 +17,15 @@
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration and of QinetiQ Ltd, *
// * subject DEFCON 705 IPR conditions. *
// * subject to DEFCON 705 IPR conditions. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// $Id: G4TessellatedSolid.cc,v 1.9 2007/02/12 12:08:33 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// $Id: G4TessellatedSolid.cc,v 1.14 2007/12/11 15:28:50 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-01 $
//
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//
@@ -42,6 +42,18 @@
// CHANGE HISTORY
// --------------
//
// 14 November 2007 P R Truscott, QinetiQ & Stan Seibert, U Texas
// Bug fixes to CalculateExtent
//
// 17 September 2007, P R Truscott, QinetiQ Ltd & Richard Holmberg
// Updated extensively prior to this date to deal with
// concaved tessellated surfaces, based on the algorithm
// of Richard Holmberg. This had been slightly modified
// to determine with inside the geometry by projecting
// random rays from the point provided. Now random rays
// are predefined rather than making use of random
// number generator at run-time.
//
// 22 November 2005, F Lei
// - Changed ::DescribeYourselfTo(), line 464
// - added GetPolyHedron()
@@ -77,6 +89,8 @@ G4TessellatedSolid::G4TessellatedSolid ()
yMaxExtent = -kInfinity;
zMinExtent = kInfinity;
zMaxExtent = -kInfinity;
SetRandomVectorSet();
}
///////////////////////////////////////////////////////////////////////////////
@@ -99,6 +113,8 @@ G4TessellatedSolid::G4TessellatedSolid (const G4String &name)
yMaxExtent = -kInfinity;
zMinExtent = kInfinity;
zMaxExtent = -kInfinity;
SetRandomVectorSet();
}
///////////////////////////////////////////////////////////////////////////////
@@ -111,8 +127,9 @@ G4TessellatedSolid::G4TessellatedSolid( __void__& a )
geometryType("G4TessellatedSolid"), cubicVolume(0.), surfaceArea(0.),
vertexList(), xMinExtent(0.), xMaxExtent(0.),
yMinExtent(0.), yMaxExtent(0.), zMinExtent(0.), zMaxExtent(0.),
solidClosed(false), dirTolerance(0.)
solidClosed(false)
{
SetRandomVectorSet();
}
///////////////////////////////////////////////////////////////////////////////
@@ -295,6 +312,32 @@ void G4TessellatedSolid::SetSolidClosed (const G4bool t)
zMinExtent = z;
}
}
//
//
// Compute extremeFacets, i.e. find those facets that have surface
// planes that bound the volume.
// Note that this is going to reject concaved surfaces as being extreme. Also
// note that if the vertex is on the facet, displacement is zero, so IsInside
// returns true. So will this work?? Need non-equality
// "G4bool inside = displacement < 0.0;"
// or
// "G4bool inside = displacement <= -0.5*kCarTolerance"
// (Notes from PT 13/08/2007).
//
for (FacetCI it=facets.begin(); it!=facets.end(); it++)
{
G4bool isExtreme = true;
for (size_t i=0; i<vertexList.size(); i++)
{
if (!(*it)->IsInside(vertexList[i]))
{
isExtreme = false;
break;
}
}
if (isExtreme)
extremeFacets.insert(*it);
}
solidClosed = true;
}
else
@@ -305,11 +348,22 @@ void G4TessellatedSolid::SetSolidClosed (const G4bool t)
///////////////////////////////////////////////////////////////////////////////
//
// GetSolidClosed
//
// Used to determine whether the solid is closed to adding further facets.
//
G4bool G4TessellatedSolid::GetSolidClosed () const
{return solidClosed;}
///////////////////////////////////////////////////////////////////////////////
//
// operator+=
//
// This operator allows the user to add two tessellated solids together, so
// that the solid on the left then includes all of the facets in the solid
// on the right. Note that copies of the facets are generated, rather than
// using the original facet set of the solid on the right.
//
const G4TessellatedSolid &G4TessellatedSolid::operator+=
(const G4TessellatedSolid &right)
{
@@ -320,6 +374,10 @@ const G4TessellatedSolid &G4TessellatedSolid::operator+=
///////////////////////////////////////////////////////////////////////////////
//
// GetFacet
//
// Access pointer to facet in solid, indexed by integer i.
//
G4VFacet *G4TessellatedSolid::GetFacet (size_t i) const
{
return facets[i];
@@ -327,6 +385,8 @@ G4VFacet *G4TessellatedSolid::GetFacet (size_t i) const
///////////////////////////////////////////////////////////////////////////////
//
// GetNumberOfFacets
//
size_t G4TessellatedSolid::GetNumberOfFacets () const
{
return facets.size();
@@ -334,8 +394,20 @@ size_t G4TessellatedSolid::GetNumberOfFacets () const
///////////////////////////////////////////////////////////////////////////////
//
// EInside G4TessellatedSolid::Inside (const G4ThreeVector &p) const
//
// This method must return:
// * kOutside if the point at offset p is outside the shape
// boundaries plus kCarTolerance/2,
// * kSurface if the point is <= kCarTolerance/2 from a surface, or
// * kInside otherwise.
//
EInside G4TessellatedSolid::Inside (const G4ThreeVector &p) const
{
//
// First the simple test - check if we're outside of the X-Y-Z extremes
// of the tessellated solid.
//
if ( p.x() < xMinExtent - kCarTolerance ||
p.x() > xMaxExtent + kCarTolerance ||
p.y() < yMinExtent - kCarTolerance ||
@@ -347,51 +419,144 @@ EInside G4TessellatedSolid::Inside (const G4ThreeVector &p) const
}
G4double minDist = kInfinity;
G4double dist = 0.0;
typedef std::multimap< G4double, FacetCI, std::less<G4double> > DistMapType;
DistMapType distmap;
size_t purgeIntv = 25;
//
//
// Check if we are close to a surface
//
for (FacetCI f=facets.begin(); f!=facets.end(); f++)
{
dist = (*f)->Distance(p,minDist);
distmap.insert(DistMapType::value_type(dist,f));
minDist = distmap.begin()->first;
if (distmap.size() > purgeIntv)
G4double dist = (*f)->Distance(p,minDist);
if (dist < minDist) minDist = dist;
if (dist <= 0.5*kCarTolerance)
{
DistMapType::iterator it =
distmap.lower_bound(minDist + 0.5*kCarTolerance);
it++;
if (it != distmap.end())
return kSurface;
}
}
//
//
// The following is something of an adaptation of the method implemented by
// Rickard Holmberg augmented with information from Schneider & Eberly,
// "Geometric Tools for Computer Graphics," pp700-701, 2003. In essence, we're
// trying to determine whether we're inside the volume by projecting a few rays
// and determining if the first surface crossed is has a normal vector between
// 0 to pi/2 (out-going) or pi/2 to pi (in-going). We should also avoid rays
// which are nearly within the plane of the tessellated surface, and therefore
// produce rays randomly. For the moment, this is a bit over-engineered
// (belt-braces-and-ducttape).
//
#if G4SPECSDEBUG
G4int nTry = 7;
#else
G4int nTry = 3;
#endif
G4double distOut = kInfinity;
G4double distIn = kInfinity;
G4double distO = 0.0;
G4double distI = 0.0;
G4double distFromSurfaceO = 0.0;
G4double distFromSurfaceI = 0.0;
G4ThreeVector normalO(0.0,0.0,0.0);
G4ThreeVector normalI(0.0,0.0,0.0);
G4bool crossingO = false;
G4bool crossingI = false;
EInside location = kOutside;
EInside locationprime = kOutside;
G4int m = 0;
for (G4int i=0; i<nTry; i++)
{
G4bool nearParallel = false;
do
{
//
//
// We loop until we find direction where the vector is not nearly parallel
// to the surface of any facet since this causes ambiguities. The usual
// case is that the angles should be sufficiently different, but there are 20
// random directions to select from - hopefully sufficient.
//
distOut = kInfinity;
distIn = kInfinity;
G4ThreeVector v = randir[m];
m++;
FacetCI f = facets.begin();
do
{
DistMapType::iterator itend = distmap.end();
itend--;
distmap.erase (it,itend);
}
if (distmap.size() > purgeIntv) purgeIntv = 2*distmap.size();
//
//
// Here we loop through the facets to find out if there is an intersection
// between the ray and that facet. The test if performed separately whether
// the ray is entering the facet or exiting.
//
crossingO = ((*f)->Intersect(p,v,true,distO,distFromSurfaceO,normalO));
crossingI = ((*f)->Intersect(p,v,false,distI,distFromSurfaceI,normalI));
if (crossingO || crossingI)
{
nearParallel = crossingO && std::abs(normalO.dot(v))<dirTolerance ||
crossingI && std::abs(normalI.dot(v))<dirTolerance;
if (!nearParallel)
{
if (crossingO && distO > 0.0 && distO < distOut) distOut = distO;
if (crossingI && distI > 0.0 && distI < distIn) distIn = distI;
}
}
} while (!nearParallel && ++f!=facets.end());
} while (nearParallel && m!=maxTries);
if (m == maxTries)
{
//
//
// We've run out of random vector directions. If nTries is set sufficiently
// low (nTries <= 0.5*maxTries) then this would indicate that there is
// something wrong with geometry.
//
G4Exception("G4TessellatedSolid::Inside()",
"UnknownInsideOutside", FatalException,
"Cannot determine whether point is inside or outside volume!");
}
//
//
// In the next if-then-elseif string the logic is as follows:
// (1) You don't hit anything so cannot be inside volume, provided volume
// constructed correctly!
// (2) Distance to inside (ie. nearest facet such that you enter facet) is
// shorter than distance to outside (nearest facet such that you exit
// facet) - on condition of safety distance - therefore we're outside.
// (3) Distance to outside is shorter than distance to inside therefore we're
// inside.
//
if (distIn == kInfinity && distOut == kInfinity)
locationprime = kOutside;
else if (distIn <= distOut - kCarTolerance*0.5)
locationprime = kOutside;
else if (distOut <= distIn - kCarTolerance*0.5)
locationprime = kInside;
if (i == 0) location = locationprime;
else if (locationprime != location)
{
//
//
// Different ray directions result in different answer. Seems like the
// geometry is not constructed correctly.
//
G4Exception("G4TessellatedSolid::Inside()",
"UnknownInsideOutside", FatalException,
"Cannot determine whether point is inside or outside volume!");
}
}
EInside inside = kInside;
if (minDist <= 0.5*kCarTolerance) {inside = kSurface;}
else
{
DistMapType::const_iterator itcut =
distmap.lower_bound(minDist + 0.5* kCarTolerance);
itcut++;
DistMapType::const_iterator it = distmap.begin();
do
{
if (!((*(it->second))->IsInside(p))) {inside = kOutside;}
} while (inside == kInside && ++it != itcut);
}
return inside;
return location;
}
///////////////////////////////////////////////////////////////////////////////
//
// G4ThreeVector G4TessellatedSolid::SurfaceNormal (const G4ThreeVector &p) const
//
// Return the outwards pointing unit normal of the shape for the
// surface closest to the point at offset p.
G4ThreeVector G4TessellatedSolid::SurfaceNormal (const G4ThreeVector &p) const
{
FacetCI minFacet;
@@ -430,6 +595,14 @@ G4ThreeVector G4TessellatedSolid::SurfaceNormal (const G4ThreeVector &p) const
///////////////////////////////////////////////////////////////////////////////
//
// G4double DistanceToIn(const G4ThreeVector& p, const G4ThreeVector& v)
//
// Return the distance along the normalised vector v to the shape,
// from the point at offset p. If there is no intersection, return
// kInfinity. The first intersection resulting from leaving a
// surface/volume is discarded. Hence, this is tolerant of points on
// surface of shape.
G4double G4TessellatedSolid::DistanceToIn (const G4ThreeVector &p,
const G4ThreeVector &v) const
{
@@ -438,11 +611,30 @@ G4double G4TessellatedSolid::DistanceToIn (const G4ThreeVector &p,
G4double distFromSurface = 0.0;
G4ThreeVector normal(0.0,0.0,0.0);
#if G4SPECSDEBUG
if ( Inside(p) == kInside )
{
G4cout.precision(16) ;
G4cout << G4endl ;
// DumpInfo();
G4cout << "Position:" << G4endl << G4endl ;
G4cout << "p.x() = " << p.x()/mm << " mm" << G4endl ;
G4cout << "p.y() = " << p.y()/mm << " mm" << G4endl ;
G4cout << "p.z() = " << p.z()/mm << " mm" << G4endl << G4endl ;
G4cout << "DistanceToOut(p) == " << DistanceToOut(p) << G4endl;
G4Exception("G4TriangularFacet::DistanceToIn(p,v)", "Notification", JustWarning,
"Point p is already inside!?" );
}
#endif
for (FacetCI f=facets.begin(); f!=facets.end(); f++)
{
if ((*f)->Intersect(p,v,false,dist,distFromSurface,normal))
{
if (dist < minDist) minDist = dist;
if (distFromSurface > 0.5*kCarTolerance && dist >= 0.0 && dist < minDist)
{
minDist = dist;
}
}
}
@@ -451,15 +643,36 @@ G4double G4TessellatedSolid::DistanceToIn (const G4ThreeVector &p,
///////////////////////////////////////////////////////////////////////////////
//
// G4double DistanceToIn(const G4ThreeVector& p)
//
// Calculate distance to nearest surface of shape from an outside point p. The
// distance can be an underestimate.
G4double G4TessellatedSolid::DistanceToIn (const G4ThreeVector &p) const
{
G4double minDist = kInfinity;
G4double dist = 0.0;
#if G4SPECSDEBUG
if ( Inside(p) == kInside )
{
G4cout.precision(16) ;
G4cout << G4endl ;
// DumpInfo();
G4cout << "Position:" << G4endl << G4endl ;
G4cout << "p.x() = " << p.x()/mm << " mm" << G4endl ;
G4cout << "p.y() = " << p.y()/mm << " mm" << G4endl ;
G4cout << "p.z() = " << p.z()/mm << " mm" << G4endl << G4endl ;
G4cout << "DistanceToOut(p) == " << DistanceToOut(p) << G4endl;
G4Exception("G4TriangularFacet::DistanceToIn(p)", "Notification", JustWarning,
"Point p is already inside!?" );
}
#endif
for (FacetCI f=facets.begin(); f!=facets.end(); f++)
{
dist = (*f)->Distance(p,minDist,false);
if (dist < minDist) minDist = dist;
if (dist < minDist) { minDist = dist; }
}
return minDist;
@@ -467,65 +680,119 @@ G4double G4TessellatedSolid::DistanceToIn (const G4ThreeVector &p) const
///////////////////////////////////////////////////////////////////////////////
//
// G4double DistanceToOut(const G4ThreeVector& p, const G4ThreeVector& v,
// const G4bool calcNorm=false,
// G4bool *validNorm=0, G4ThreeVector *n=0);
//
// Return distance along the normalised vector v to the shape, from a
// point at an offset p inside or on the surface of the
// shape. Intersections with surfaces, when the point is not greater
// than kCarTolerance/2 from a surface, must be ignored.
// If calcNorm is true, then it must also set validNorm to either
// * true, if the solid lies entirely behind or on the exiting
// surface. Then it must set n to the outwards normal vector
// (the Magnitude of the vector is not defined).
// * false, if the solid does not lie entirely behind or on the
// exiting surface.
// If calcNorm is false, then validNorm and n are unused.
G4double G4TessellatedSolid::DistanceToOut (const G4ThreeVector &p,
const G4ThreeVector &v, const G4bool calcNorm,
G4bool *validNorm, G4ThreeVector *n) const
{
G4double minDist1 = kInfinity;
G4double minDist2 = kInfinity;
G4double minDist = kInfinity;
G4double dist = 0.0;
G4double distFromSurface = 0.0;
G4ThreeVector normal(0.0,0.0,0.0);
G4ThreeVector minNormal1(0.0,0.0,0.0);
G4ThreeVector minNormal2(0.0,0.0,0.0);
G4ThreeVector minNormal(0.0,0.0,0.0);
#if G4SPECSDEBUG
if ( Inside(p) == kOutside )
{
G4cout.precision(16) ;
G4cout << G4endl ;
// DumpInfo();
G4cout << "Position:" << G4endl << G4endl ;
G4cout << "p.x() = " << p.x()/mm << " mm" << G4endl ;
G4cout << "p.y() = " << p.y()/mm << " mm" << G4endl ;
G4cout << "p.z() = " << p.z()/mm << " mm" << G4endl << G4endl ;
G4cout << "DistanceToIn(p) == " << DistanceToIn(p) << G4endl;
G4Exception("G4TriangularFacet::DistanceToOut(p)", "Notification", JustWarning,
"Point p is already outside !?" );
}
#endif
G4bool isExtreme = false;
for (FacetCI f=facets.begin(); f!=facets.end(); f++)
{
if ((*f)->Intersect(p,v,true,dist,distFromSurface,normal))
{
if (dist < minDist1)
{
if (distFromSurface > 0.0 && distFromSurface <= 0.5*kCarTolerance &&
(*f)->Distance(p,kCarTolerance) <= 0.5*kCarTolerance)
{
if (v.dot(normal) > dirTolerance)
{
minDist1 = dist;
minNormal1 = normal;
}
else if (dist < minDist2)
{
minDist2 = dist;
minNormal2 = normal;
}
// We are on a surface. Return zero.
*validNorm = extremeFacets.count(*f);
*n = SurfaceNormal(p);
return 0.0;
}
if (dist >= 0.0 && dist < minDist)
{
minDist = dist;
minNormal = normal;
isExtreme = extremeFacets.count(*f);
}
}
}
if (minDist1 < kInfinity)
if (minDist < kInfinity)
{
if (calcNorm)
{
*validNorm = true;
*n = minNormal1;
*validNorm = isExtreme;
*n = minNormal;
}
return minDist1;
return minDist;
}
else
{
// No intersection found
if (calcNorm)
{
*validNorm = true;
*n = minNormal2;
*validNorm = false;
*n = SurfaceNormal(p);
}
return minDist2;
return 0.0;
}
}
///////////////////////////////////////////////////////////////////////////////
//
// G4double DistanceToOut(const G4ThreeVector& p)
//
// Calculate distance to nearest surface of shape from an inside
// point. The distance can be an underestimate.
G4double G4TessellatedSolid::DistanceToOut (const G4ThreeVector &p) const
{
G4double minDist = kInfinity;
G4double dist = 0.0;
#if G4SPECSDEBUG
if ( Inside(p) == kOutside )
{
G4cout.precision(16) ;
G4cout << G4endl ;
// DumpInfo();
G4cout << "Position:" << G4endl << G4endl ;
G4cout << "p.x() = " << p.x()/mm << " mm" << G4endl ;
G4cout << "p.y() = " << p.y()/mm << " mm" << G4endl ;
G4cout << "p.z() = " << p.z()/mm << " mm" << G4endl << G4endl ;
G4cout << "DistanceToIn(p) == " << DistanceToIn(p) << G4endl;
G4Exception("G4TriangularFacet::DistanceToOut(p)", "Notification", JustWarning,
"Point p is already outside !?" );
}
#endif
for (FacetCI f=facets.begin(); f!=facets.end(); f++)
{
dist = (*f)->Distance(p,minDist,true);
@@ -537,6 +804,11 @@ G4double G4TessellatedSolid::DistanceToOut (const G4ThreeVector &p) const
///////////////////////////////////////////////////////////////////////////////
//
// G4GeometryType GetEntityType() const;
//
// Provide identification of the class of an object (required for
// persistency and STEP interface).
//
G4GeometryType G4TessellatedSolid::GetEntityType () const
{
return geometryType;
@@ -637,105 +909,86 @@ G4Polyhedron* G4TessellatedSolid::GetPolyhedron () const
///////////////////////////////////////////////////////////////////////////////
//
G4bool G4TessellatedSolid::CalculateExtent(const EAxis pAxis,
const G4VoxelLimits& pVoxelLimit, const G4AffineTransform& pTransform,
G4double& pMin, G4double& pMax) const
// CalculateExtent
//
// Based on correction provided by Stan Seibert, University of Texas.
//
G4bool
G4TessellatedSolid::CalculateExtent(const EAxis pAxis,
const G4VoxelLimits& pVoxelLimit,
const G4AffineTransform& pTransform,
G4double& pMin, G4double& pMax) const
{
if (!pTransform.IsRotated())
{
G4double xoffset,xMin,xMax;
G4double yoffset,yMin,yMax;
G4double zoffset,zMin,zMax;
xoffset = pTransform.NetTranslation().x();
xMin = xoffset + xMinExtent;
xMax = xoffset + xMaxExtent;
if (pVoxelLimit.IsXLimited())
G4ThreeVectorList transVertexList(vertexList);
// Put solid into transformed frame
for (size_t i=0; i<vertexList.size(); i++)
{ pTransform.ApplyPointTransform(transVertexList[i]); }
// Find min and max extent in each dimension
G4ThreeVector minExtent(kInfinity, kInfinity, kInfinity);
G4ThreeVector maxExtent(-kInfinity, -kInfinity, -kInfinity);
for (size_t i=0; i<transVertexList.size(); i++)
{
if ( xMin > pVoxelLimit.GetMaxXExtent()+kCarTolerance ||
xMax < pVoxelLimit.GetMinXExtent()-kCarTolerance ) return false ;
else
for (G4int axis=G4ThreeVector::X; axis < G4ThreeVector::SIZE; axis++)
{
if (xMin < pVoxelLimit.GetMinXExtent())
{
xMin = pVoxelLimit.GetMinXExtent() ;
}
if (xMax > pVoxelLimit.GetMaxXExtent())
{
xMax = pVoxelLimit.GetMaxXExtent() ;
}
G4double coordinate = transVertexList[i][axis];
if (coordinate < minExtent[axis])
{ minExtent[axis] = coordinate; }
if (coordinate > maxExtent[axis])
{ maxExtent[axis] = coordinate; }
}
}
yoffset = pTransform.NetTranslation().y();
yMin = yoffset + yMinExtent;
yMax = yoffset + yMaxExtent;
if (pVoxelLimit.IsYLimited())
// Check for containment and clamp to voxel boundaries
for (G4int axis=G4ThreeVector::X; axis < G4ThreeVector::SIZE; axis++)
{
if ( yMin > pVoxelLimit.GetMaxYExtent()+kCarTolerance ||
yMax < pVoxelLimit.GetMinYExtent()-kCarTolerance ) return false ;
else
EAxis geomAxis = kXAxis; // G4 geom classes use different index type
switch(axis)
{
if (yMin < pVoxelLimit.GetMinYExtent())
case G4ThreeVector::X: geomAxis = kXAxis; break;
case G4ThreeVector::Y: geomAxis = kYAxis; break;
case G4ThreeVector::Z: geomAxis = kZAxis; break;
}
G4bool isLimited = pVoxelLimit.IsLimited(geomAxis);
G4double voxelMinExtent = pVoxelLimit.GetMinExtent(geomAxis);
G4double voxelMaxExtent = pVoxelLimit.GetMaxExtent(geomAxis);
if (isLimited)
{
if ( minExtent[axis] > voxelMaxExtent+kCarTolerance ||
maxExtent[axis] < voxelMinExtent-kCarTolerance )
{
yMin = pVoxelLimit.GetMinYExtent() ;
return false ;
}
if (yMax > pVoxelLimit.GetMaxYExtent())
else
{
yMax = pVoxelLimit.GetMaxYExtent() ;
if (minExtent[axis] < voxelMinExtent)
{
minExtent[axis] = voxelMinExtent ;
}
if (maxExtent[axis] > voxelMaxExtent)
{
maxExtent[axis] = voxelMaxExtent;
}
}
}
}
zoffset = pTransform.NetTranslation().z();
zMin = zoffset + zMinExtent;
zMax = zoffset + zMaxExtent;
if (pVoxelLimit.IsZLimited())
// Convert pAxis into G4ThreeVector index
G4int vecAxis=0;
switch(pAxis)
{
if ( zMin > pVoxelLimit.GetMaxZExtent()+kCarTolerance ||
zMax < pVoxelLimit.GetMinZExtent()-kCarTolerance ) return false ;
else
{
if (zMin < pVoxelLimit.GetMinZExtent())
{
zMin = pVoxelLimit.GetMinZExtent() ;
}
if (zMax > pVoxelLimit.GetMaxZExtent())
{
zMax = pVoxelLimit.GetMaxZExtent() ;
}
}
}
case kXAxis: vecAxis = G4ThreeVector::X; break;
case kYAxis: vecAxis = G4ThreeVector::Y; break;
case kZAxis: vecAxis = G4ThreeVector::Z; break;
default: break;
}
pMin = minExtent[vecAxis] - kCarTolerance;
pMax = maxExtent[vecAxis] + kCarTolerance;
switch (pAxis)
{
case kXAxis:
pMin = xMin ;
pMax = xMax ;
break ;
case kYAxis:
pMin=yMin;
pMax=yMax;
break;
case kZAxis:
pMin=zMin;
pMax=zMax;
break;
default:
break;
}
pMin -= kCarTolerance ;
pMax += kCarTolerance ;
return true;
}
else
{
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
@@ -803,7 +1056,41 @@ G4double G4TessellatedSolid::GetSurfaceArea ()
G4ThreeVector G4TessellatedSolid::GetPointOnSurface() const
{
// Select randomly a facet and return a random point on it
G4int i = CLHEP::RandFlat::shootInt(facets.size());
return facets[i]->GetPointOnFace();
}
///////////////////////////////////////////////////////////////////////////////
//
// SetRandomVectorSet
//
// This is a set of predefined random vectors (if that isn't a contradition
// in terms!) used to generate rays from a user-defined point. The member
// function Inside uses these to determine whether the point is inside or
// outside of the tessellated solid. All vectors should be unit vectors.
//
void G4TessellatedSolid::SetRandomVectorSet()
{
randir[0] = G4ThreeVector(-0.9577428892113370, 0.2732676269591740, 0.0897405271949221);
randir[1] = G4ThreeVector(-0.8331264504940770,-0.5162067214954600,-0.1985722492445700);
randir[2] = G4ThreeVector(-0.1516671651108820, 0.9666292616127460, 0.2064580868390110);
randir[3] = G4ThreeVector( 0.6570250350323190,-0.6944539025883300, 0.2933460081893360);
randir[4] = G4ThreeVector(-0.4820456281280320,-0.6331060000098690,-0.6056474264406270);
randir[5] = G4ThreeVector( 0.7629032554236800, 0.1016854697539910,-0.6384658864065180);
randir[6] = G4ThreeVector( 0.7689540409061150, 0.5034929891988220, 0.3939600142169160);
randir[7] = G4ThreeVector( 0.5765188359255740, 0.5997271636278330,-0.5549354566343150);
randir[8] = G4ThreeVector( 0.6660632777862070,-0.6362809868288380, 0.3892379937580790);
randir[9] = G4ThreeVector( 0.3824415020414780, 0.6541792713761380,-0.6525243125110690);
randir[10] = G4ThreeVector(-0.5107726564526760, 0.6020905056811610, 0.6136760679616570);
randir[11] = G4ThreeVector( 0.7459135439578050, 0.6618796061649330, 0.0743530220183488);
randir[12] = G4ThreeVector( 0.1536405855311580, 0.8117477913978260,-0.5634359711967240);
randir[13] = G4ThreeVector( 0.0744395301705579,-0.8707110101772920,-0.4861286795736560);
randir[14] = G4ThreeVector(-0.1665874645185400, 0.6018553940549240,-0.7810369397872780);
randir[15] = G4ThreeVector( 0.7766902003633100, 0.6014617505959970,-0.1870724331097450);
randir[16] = G4ThreeVector(-0.8710128685847430,-0.1434320216603030,-0.4698551243971010);
randir[17] = G4ThreeVector( 0.8901082092766820,-0.4388411398893870, 0.1229871120030100);
randir[18] = G4ThreeVector(-0.6430417431544370,-0.3295938228697690, 0.6912779675984150);
randir[19] = G4ThreeVector( 0.6331124368380410, 0.6306211461665000, 0.4488714875425340);
maxTries = 20;
}
+1 -1
View File
@@ -28,7 +28,7 @@
// ********************************************************************
//
// $Id: G4Tet.cc,v 1.11 2006/11/13 08:58:03 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
// class G4Tet
//
@@ -17,15 +17,15 @@
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration and of QinetiQ Ltd, *
// * subject DEFCON 705 IPR conditions. *
// * subject to DEFCON 705 IPR conditions. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// $Id: G4TriangularFacet.cc,v 1.7 2007/02/15 17:03:49 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// $Id: G4TriangularFacet.cc,v 1.10 2007/12/10 16:30:35 gunter Exp $
// GEANT4 tag $Name: geant4-09-01 $
//
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//
@@ -44,9 +44,21 @@
//
// 31 October 2004, P R Truscott, QinetiQ Ltd, UK - Created.
//
// 01 August 2007 P R Truscott, QinetiQ Ltd, UK
// Significant modification to correct for errors and enhance
// based on patches/observations kindly provided by Rickard
// Holmberg
//
// 26 September 2007
// P R Truscott, QinetiQ Ltd, UK
// Further chamges implemented to the Intersect member
// function to correctly treat rays nearly parallel to the
// plane of the triangle.
//
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#include "G4TriangularFacet.hh"
#include "G4TwoVector.hh"
#include "globals.hh"
#include "Randomize.hh"
@@ -62,6 +74,7 @@ G4TriangularFacet::G4TriangularFacet (const G4ThreeVector Pt0,
G4FacetVertexType vertexType)
: G4VFacet()
{
if (!tGeomAlg) { tGeomAlg = G4TessellatedGeometryAlgorithms::GetInstance(); }
P0 = Pt0;
nVertices = 3;
if (vertexType == ABSOLUTE)
@@ -121,17 +134,25 @@ G4TriangularFacet::G4TriangularFacet (const G4ThreeVector Pt0,
sMax = 1.0 - sMin;
tMin = -0.5*kCarTolerance/std::sqrt(c);
G4ThreeVector vtmp = 0.25 * (E[0] + E[1]);
centroid = P0 + vtmp;
radiusSqr = vtmp.mag2();
radius = std::sqrt(radiusSqr);
area = 0.5 * (E[0].cross(E[1])).mag();
// G4ThreeVector vtmp = 0.25 * (E[0] + E[1]);
G4double lambda0 = (a-b) * c / (8.0*area*area);
G4double lambda1 = (c-b) * a / (8.0*area*area);
circumcentre = P0 + lambda0*E[0] + lambda1*E[1];
radiusSqr = (circumcentre-P0).mag2();
radius = std::sqrt(radiusSqr);
for (size_t i=0; i<3; i++) I.push_back(0);
for (size_t i=0; i<3; i++) { I.push_back(0); }
}
}
///////////////////////////////////////////////////////////////////////////////
//
// ~G4TriangularFacet
//
// A pretty boring destructor indeed!
//
G4TriangularFacet::~G4TriangularFacet ()
{
P.clear();
@@ -141,6 +162,10 @@ G4TriangularFacet::~G4TriangularFacet ()
///////////////////////////////////////////////////////////////////////////////
//
// GetClone
//
// Simple member function to generate a diplicate of the triangular facet.
//
G4VFacet *G4TriangularFacet::GetClone ()
{
G4TriangularFacet *fc = new G4TriangularFacet (P0, P[0], P[1], ABSOLUTE);
@@ -151,6 +176,11 @@ G4VFacet *G4TriangularFacet::GetClone ()
///////////////////////////////////////////////////////////////////////////////
//
// GetFlippedFacet
//
// Member function to generate an identical facet, but with the normal vector
// pointing at 180 degrees.
//
G4TriangularFacet *G4TriangularFacet::GetFlippedFacet ()
{
G4TriangularFacet *flipped = new G4TriangularFacet (P0, P[1], P[0], ABSOLUTE);
@@ -159,10 +189,17 @@ G4TriangularFacet *G4TriangularFacet::GetFlippedFacet ()
///////////////////////////////////////////////////////////////////////////////
//
// Determine the closest distance from the facet to the point p. If the
// direction of the vector to the closest point is outward-going and outgoing
// is true or the vector is in-going and outgoing is false then the distance
// is returned. Otherwise kInfinity is returned.
// Distance (G4ThreeVector)
//
// Determines the vector between p and the closest point on the facet to p.
// This is based on the algorithm published in "Geometric Tools for Computer
// Graphics," Philip J Scheider and David H Eberly, Elsevier Science (USA),
// 2003. at the time of writing, the algorithm is also available in a
// technical note "Distance between point and triangle in 3D," by David Eberly
// at http://www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf
//
// The by-product is the square-distance sqrDist, which is retained
// in case needed by the other "Distance" member functions.
//
G4ThreeVector G4TriangularFacet::Distance (const G4ThreeVector &p)
{
@@ -172,8 +209,9 @@ G4ThreeVector G4TriangularFacet::Distance (const G4ThreeVector &p)
G4double f = D.mag2();
G4double s = b*e - c*d;
G4double t = b*d - a*e;
G4double sqrDist = 0.0;
sqrDist = 0.0;
if (s+t <= det)
{
if (s < 0.0)
@@ -204,7 +242,7 @@ G4ThreeVector G4TriangularFacet::Distance (const G4ThreeVector &p)
//
s = 0.0;
if (e >= 0.0) {t = 0.0; sqrDist = f;}
else if (-e >= c) {t = 0.0; sqrDist = c + 2.0*e + f;}
else if (-e >= c) {t = 1.0; sqrDist = c + 2.0*e + f;}
else {t = -e/c; sqrDist = e*t + f;}
}
}
@@ -223,29 +261,24 @@ G4ThreeVector G4TriangularFacet::Distance (const G4ThreeVector &p)
//
// We are in region 0.
//
G4double invDet = 1.0 / det;
s *= invDet;
t *= invDet;
sqrDist = s*(a*s + b*t + 2.0*d) + t*(b*s + c*t + 2.0*e) + f;
s = s / det;
t = t / det;
sqrDist = s*(a*s + b*t + 2.0*d) + t*(b*s + c*t + 2.0*e) + f;
}
}
else
{
G4double tmp0 = 0.0;
G4double tmp1 = 0.0;
G4double numer = 0.0;
G4double denom = 0.0;
if (s < 0.0)
{
//
// We are in region 2.
//
tmp0 = b + d;
tmp1 = c + e;
G4double tmp0 = b + d;
G4double tmp1 = c + e;
if (tmp1 > tmp0)
{
numer = tmp1 - tmp0;
denom = a - 2.0*b*c;
G4double numer = tmp1 - tmp0;
G4double denom = a - 2.0*b + c;
if (numer >= denom) {s = 1.0; t = 0.0; sqrDist = a + 2.0*d + f;}
else
{
@@ -267,12 +300,12 @@ G4ThreeVector G4TriangularFacet::Distance (const G4ThreeVector &p)
//
// We are in region 6.
//
tmp0 = b + e;
tmp1 = a + d;
G4double tmp0 = b + e;
G4double tmp1 = a + d;
if (tmp1 > tmp0)
{
numer = tmp1 - tmp0;
denom = a - 2.0*b*c;
G4double numer = tmp1 - tmp0;
G4double denom = a - 2.0*b + c;
if (numer >= denom) {t = 1.0; s = 0.0; sqrDist = c + 2.0*e + f;}
else
{
@@ -294,16 +327,16 @@ G4ThreeVector G4TriangularFacet::Distance (const G4ThreeVector &p)
// We are in region 1.
//
{
numer = c + f - b - d;
G4double numer = c + e - b - d;
if (numer <= 0.0)
{
s = 0.0;
t = 1.0;
sqrDist = c + 2.0*e*f;
sqrDist = c + 2.0*e + f;
}
else
{
denom = a - 2.0*b*c;
G4double denom = a - 2.0*b + c;
if (numer >= denom) {s = 1.0; t = 0.0; sqrDist = a + 2.0*d + f;}
else
{
@@ -314,84 +347,108 @@ G4ThreeVector G4TriangularFacet::Distance (const G4ThreeVector &p)
}
}
}
//
//
// Do a heck for rounding errors in the distance-squared.
//
if (sqrDist < 0.0) { sqrDist = 0.0; }
return D + s*E[0] + t*E[1];
}
///////////////////////////////////////////////////////////////////////////////
//
// Distance (G4ThreeVector, G4double)
//
// Determines the closest distance between point p and the facet. This makes
// use of G4ThreeVector G4TriangularFacet::Distance, which stores the
// square of the distance in variable sqrDist. If approximate methods show
// the distance is to be greater than minDist, then forget about further
// computation and return a very large number.
//
G4double G4TriangularFacet::Distance (const G4ThreeVector &p,
const G4double minDist)
{
/*G4ThreeVector D = P0 - p;
G4double d = E[0].dot(D);
G4double e = E[1].dot(D);
G4double s = b*e - c*d;
G4double t = b*d - a*e;*/
//
//
// Start with quicky test to determine if the surface of the sphere enclosing
// the triangle is any closer to p than minDist. If not, then don't bother
// about more accurate test.
//
G4double dist = kInfinity;
/*if (s+t > 1.0 || s < 0.0 || t < 0.0)
if ((p-circumcentre).mag()-radius < minDist)
{
G4ThreeVector D0 = P0 - p;
G4ThreeVector D1 = P[0] - p;
G4ThreeVector D2 = P[1] - p;
G4double d0 = D0.mag();
G4double d1 = D1.mag();
G4double d2 = D2.mag();
dist = min(d0, min(d1, d2));
if (dist > minDist) return kInfinity;
}*/
dist = Distance(p).mag();
if (dist > minDist) return kInfinity;
//
//
// It's possible that the triangle is closer than minDist, so do more accurate
// assessment.
//
dist = Distance(p).mag();
// dist = std::sqrt(sqrDist);
}
return dist;
}
///////////////////////////////////////////////////////////////////////////////
//
// Determine the distance to point p bearing in mind that if the distance is
// likely to be longer than minDist, forget doing further calculation and
// return kInfinity.
// Distance (G4ThreeVector, G4double, G4double)
//
// Determine the distance to point p. kInfinity is returned if either:
// (1) outgoing is TRUE and the dot product of the normal vector to the facet
// and the displacement vector from p to the triangle is negative.
// (2) outgoing is FALSE and the dot product of the normal vector to the facet
// and the displacement vector from p to the triangle is positive.
// If approximate methods show the distance is to be greater than minDist, then
// forget about further computation and return a very large number.
//
// This method has been heavily modified thanks to the valuable comments and
// corrections of Rickard Holmberg.
//
G4double G4TriangularFacet::Distance (const G4ThreeVector &p,
const G4double, const G4bool outgoing)
const G4double minDist, const G4bool outgoing)
{
/*G4ThreeVector D = P0 - p;
G4double d = E[0].dot(D);
G4double e = E[1].dot(D);
G4double s = b*e - c*d;
G4double t = b*d - a*e;*/
//
//
// Start with quicky test to determine if the surface of the sphere enclosing
// the triangle is any closer to p than minDist. If not, then don't bother
// about more accurate test.
//
G4double dist = kInfinity;
/*if (s+t > 1.0 || s < 0.0 || t < 0.0)
if ((p-circumcentre).mag()-radius < minDist)
{
G4ThreeVector D0 = P0 - p;
G4ThreeVector D1 = P[0] - p;
G4ThreeVector D2 = P[1] - p;
G4double d0 = D0.mag();
G4double d1 = D1.mag();
G4double d2 = D2.mag();
dist = min(d0, min(d1, d2));
if (dist > minDist ||
(D0.dot(surfaceNormal) > 0.0 && !outgoing) ||
(D0.dot(surfaceNormal) < 0.0 && outgoing)) return kInfinity;
}*/
G4ThreeVector v = Distance(p);
G4double dir = v.dot(surfaceNormal);
if ((dir > dirTolerance && !outgoing) ||
(dir <-dirTolerance && outgoing)) dist = kInfinity;
else dist = v.mag();
//
//
// It's possible that the triangle is closer than minDist, so do more accurate
// assessment.
//
G4ThreeVector v = Distance(p);
G4double dist1 = std::sqrt(sqrDist);
G4double dir = v.dot(surfaceNormal);
G4bool wrongSide = (dir > 0.0 && !outgoing) || (dir < 0.0 && outgoing);
if (dist1 <= kCarTolerance*0.5)
{
//
//
// Point p is very close to triangle. Check if it's on the wrong side, in
// which case return distance of 0.0 otherwise .
//
if (wrongSide) dist = 0.0;
else dist = dist1;
}
else if (!wrongSide) dist = dist1;
}
return dist;
}
///////////////////////////////////////////////////////////////////////////////
//
// Extent
//
// Calculates the furthest the triangle extends in a particular direction
// defined by the vector axis.
//
G4double G4TriangularFacet::Extent (const G4ThreeVector axis)
{
G4double s = P0.dot(axis);
@@ -405,66 +462,243 @@ G4double G4TriangularFacet::Extent (const G4ThreeVector axis)
///////////////////////////////////////////////////////////////////////////////
//
// Intersect
//
// Member function to find the next intersection when going from p in the
// direction of v. If:
// (1) "outgoing" is TRUE, only consider the face if we are going out through
// the face.
// (2) "outgoing" is FALSE, only consider the face if we are going in through
// the face.
// Member functions returns TRUE if there is an intersection, FALSE otherwise.
// Sets the distance (distance along w), distFromSurface (orthogonal distance)
// and normal.
//
// Also considers intersections that happen with negative distance for small
// distances of distFromSurface = 0.5*kCarTolerance in the wrong direction.
// This is to detect kSurface without doing a full Inside(p) in
// G4TessellatedSolid::Distance(p,v) calculation.
//
// This member function is thanks the valuable work of Rickard Holmberg. PT.
// However, "gotos" are the Work of the Devil have been exorcised with
// extreme prejudice!!
//
// IMPORTANT NOTE: These calculations are predicated on v being a unit
// vector. If G4TessellatedSolid or other classes call this member function
// with |v| != 1 then there will be errors.
//
G4bool G4TriangularFacet::Intersect (const G4ThreeVector &p,
const G4ThreeVector &v, G4bool outgoing, G4double &distance,
G4double &distFromSurface, G4ThreeVector &normal)
{
G4ThreeVector D = P0 - p;
G4double d = E[0].dot(D);
G4double e = E[1].dot(D);
G4double g = E[0].dot(v);
G4double h = E[1].dot(v);
G4double q = D.dot(v);
G4double A00 = a - g*g;
G4double A11 = c - h*h;
G4double A01 = b - g*h;
G4double det2 = A00*A11 - A01*A01;
G4double s = kInfinity;
G4double t = kInfinity;
G4double dist = kInfinity;
G4bool intersect = false;
G4double normalComp = 0.0;
if (det2 != 0.0)
{
G4double B0 = q*g - d;
G4double B1 = q*h - e;
s = (A11*B0 - A01*B1)/det2;
if ((s >= sMin) && (s <= sMax))
{
t = (A00*B1 - A01*B0)/det2;
if ((t >= tMin) && (t < 1.0 - s + std::fabs(sMin)))
{ //THIS IS A FUDGE FOR THE MOMENT
dist = q + g*s + h*t;
normalComp = v.dot(surfaceNormal);
// intersect = (dist >= 0.0 &&
// ((outgoing && normalComp > 0.0) || (!outgoing && normalComp < 0.0)));
intersect = (dist >= -kCarTolerance*0.5 &&
((outgoing && normalComp > dirTolerance) ||
(!outgoing && normalComp <-dirTolerance))); //FUDGE FOR THE MOMENT
}
}
}
if (intersect)
{
if (dist < kCarTolerance * 0.5) { dist = 0.0; }
distance = dist;
distFromSurface = dist * normalComp;
normal = surfaceNormal;
}
else
//
//
// Check whether the direction of the facet is consistent with the vector v
// and the need to be outgoing or ingoing. If inconsistent, disregard and
// return false.
//
G4double w = v.dot(surfaceNormal);
if ((outgoing && (w <-dirTolerance)) || (!outgoing && (w > dirTolerance)))
{
distance = kInfinity;
distFromSurface = kInfinity;
normal = G4ThreeVector(0.0,0.0,0.0);
return false;
}
//
//
// Calculate the orthogonal distance from p to the surface containing the
// triangle. Then determine if we're on the right or wrong side of the
// surface (at a distance greater than kCarTolerance) to be consistent with
// "outgoing".
//
G4ThreeVector D = P0 - p;
distFromSurface = D.dot(surfaceNormal);
G4bool wrongSide = (outgoing && (distFromSurface < -0.5*kCarTolerance)) ||
(!outgoing && (distFromSurface > 0.5*kCarTolerance));
if (wrongSide)
{
distance = kInfinity;
distFromSurface = kInfinity;
normal = G4ThreeVector(0.0,0.0,0.0);
return false;
}
wrongSide = (outgoing && (distFromSurface < 0.0)) ||
(!outgoing && (distFromSurface > 0.0));
if (wrongSide)
{
//
//
// We're slightly on the wrong side of the surface. Check if we're close
// enough using a precise distance calculation.
//
G4ThreeVector u = Distance(p);
if (std::sqrt(sqrDist) <= 0.5*kCarTolerance)
{
//
//
// We're very close. Therefore return a small negative number to pretend
// we intersect.
//
distance = -0.5*kCarTolerance;
normal = surfaceNormal;
return true;
}
else
{
//
//
// We're close to the surface containing the triangle, but sufficiently
// far from the triangle, and on the wrong side compared to the directions
// of the surface normal and v. There is no intersection.
//
distance = kInfinity;
distFromSurface = kInfinity;
normal = G4ThreeVector(0.0,0.0,0.0);
return false;
}
}
if (w < dirTolerance && w > -dirTolerance)
{
//
//
// The ray is within the plane of the triangle. Project the problem into 2D
// in the plane of the triangle. First try to create orthogonal unit vectors
// mu and nu, where mu is E[0]/|E[0]|. This is kinda like
// the original algorithm due to Rickard Holmberg, but with better mathematical
// justification than the original method ... however, beware Rickard's was less
// time-consuming.
//
// Note that vprime is not a unit vector. We need to keep it unnormalised
// since the values of distance along vprime (s0 and s1) for intersection with
// the triangle will be used to determine if we cut the plane at the same
// time.
//
G4ThreeVector mu = E[0].unit();
G4ThreeVector nu = surfaceNormal.cross(mu);
G4TwoVector pprime(p.dot(mu),p.dot(nu));
G4TwoVector vprime(v.dot(mu),v.dot(nu));
G4TwoVector P0prime(P0.dot(mu),P0.dot(nu));
G4TwoVector E0prime(E[0].mag(),0.0);
G4TwoVector E1prime(E[1].dot(mu),E[1].dot(nu));
G4TwoVector loc[2];
if ( tGeomAlg->IntersectLineAndTriangle2D(pprime,vprime,P0prime,
E0prime,E1prime,loc) )
{
//
//
// There is an intersection between the line and triangle in 2D. Now check
// which part of the line intersects with the plane containing the triangle
// in 3D.
//
G4double vprimemag = vprime.mag();
G4double s0 = (loc[0] - pprime).mag()/vprimemag;
G4double s1 = (loc[1] - pprime).mag()/vprimemag;
G4double normDist0 = surfaceNormal.dot(s0*v) - distFromSurface;
G4double normDist1 = surfaceNormal.dot(s1*v) - distFromSurface;
if ((normDist0 < 0.0 && normDist1 < 0.0) ||
(normDist0 > 0.0 && normDist1 > 0.0))
{
distance = kInfinity;
distFromSurface = kInfinity;
normal = G4ThreeVector(0.0,0.0,0.0);
return false;
}
else
{
G4double dnormDist = normDist1-normDist0;
if (std::abs(dnormDist) < DBL_EPSILON)
{
distance = s0;
normal = surfaceNormal;
if (!outgoing) distFromSurface = -distFromSurface;
return true;
}
else
{
distance = s0 - normDist0*(s1-s0)/dnormDist;
normal = surfaceNormal;
if (!outgoing) distFromSurface = -distFromSurface;
return true;
}
}
// G4ThreeVector dloc = loc1 - loc0;
// G4ThreeVector dlocXv = dloc.cross(v);
// G4double dlocXvmag = dlocXv.mag();
// if (dloc.mag() <= 0.5*kCarTolerance || dlocXvmag <= DBL_EPSILON)
// {
// distance = loc0.mag();
// normal = surfaceNormal;
// if (!outgoing) distFromSurface = -distFromSurface;
// return true;
// }
// G4ThreeVector loc0Xv = loc0.cross(v);
// G4ThreeVector loc1Xv = loc1.cross(v);
// G4double sameDir = -loc0Xv.dot(loc1Xv);
// if (sameDir < 0.0)
// {
// distance = kInfinity;
// distFromSurface = kInfinity;
// normal = G4ThreeVector(0.0,0.0,0.0);
// return false;
// }
// else
// {
// distance = loc0.mag() + loc0Xv.mag() * dloc.mag()/dlocXvmag;
// normal = surfaceNormal;
// if (!outgoing) distFromSurface = -distFromSurface;
// return true;
// }
}
else
{
distance = kInfinity;
distFromSurface = kInfinity;
normal = G4ThreeVector(0.0,0.0,0.0);
return false;
}
}
//
//
// Use conventional algorithm to determine the whether there is an
// intersection. This involves determining the point of intersection of the
// line with the plane containing the triangle, and then calculating if the
// point is within the triangle.
//
distance = distFromSurface / w;
G4ThreeVector pp = p + v*distance;
G4ThreeVector DD = P0 - pp;
G4double d = E[0].dot(DD);
G4double e = E[1].dot(DD);
G4double s = b*e - c*d;
G4double t = b*d - a*e;
if (s < 0.0 || t < 0.0 || s+t > det)
{
//
//
// The intersection is outside of the triangle.
//
distance = kInfinity;
distFromSurface = kInfinity;
normal = G4ThreeVector(0.0,0.0,0.0);
return false;
}
else
{
//
//
// There is an intersection. Now we only need to set the surface normal.
//
normal = surfaceNormal;
if (!outgoing) distFromSurface = -distFromSurface;
return true;
}
return intersect;
}
////////////////////////////////////////////////////////////////////////
@@ -475,16 +709,10 @@ G4bool G4TriangularFacet::Intersect (const G4ThreeVector &p,
G4ThreeVector G4TriangularFacet::GetPointOnFace() const
{
G4double lambda1,lambda2;
G4ThreeVector v, w;
G4double lambda0 = CLHEP::RandFlat::shoot(0.,1.);
G4double lambda1 = CLHEP::RandFlat::shoot(0.,lambda0);
v = P[1] - P[0];
w = P[0] - P0;
lambda1 = CLHEP::RandFlat::shoot(0.,1.);
lambda2 = CLHEP::RandFlat::shoot(0.,lambda1);
return (P0 + lambda1*w + lambda2*v);
return (P0 + lambda0*E[0] + lambda1*E[1]);
}
////////////////////////////////////////////////////////////////////////
@@ -495,13 +723,5 @@ G4ThreeVector G4TriangularFacet::GetPointOnFace() const
G4double G4TriangularFacet::GetArea()
{
if (area) { return area; }
G4ThreeVector v, w;
v = P[1] - P[0];
w = P[0] - P0;
area = 0.5*(v.cross(w)).mag();
return area;
}
@@ -25,7 +25,7 @@
//
//
// $Id: G4TwistBoxSide.cc,v 1.6 2007/05/23 09:31:02 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -25,7 +25,7 @@
//
//
// $Id: G4TwistTrapAlphaSide.cc,v 1.8 2007/05/23 13:26:06 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -25,7 +25,7 @@
//
//
// $Id: G4TwistTrapFlatSide.cc,v 1.6 2007/05/23 09:31:02 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -25,7 +25,7 @@
//
//
// $Id: G4TwistTrapParallelSide.cc,v
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -25,7 +25,7 @@
//
//
// $Id: G4TwistTubsFlatSide.cc,v 1.7 2007/05/23 09:31:02 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -25,7 +25,7 @@
//
//
// $Id: G4TwistTubsHypeSide.cc,v 1.6 2007/05/18 07:39:56 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -25,7 +25,7 @@
//
//
// $Id: G4TwistTubsSide.cc,v 1.5 2006/06/29 18:49:18 gunter Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -25,7 +25,7 @@
//
//
// $Id: G4TwistedBox.cc,v 1.12 2006/06/29 18:49:20 gunter Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -25,7 +25,7 @@
//
//
// $Id: G4TwistedTrap.cc,v 1.14 2006/06/29 18:49:23 gunter Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -25,7 +25,7 @@
//
//
// $Id: G4TwistedTrd.cc,v 1.7 2006/06/29 18:49:25 gunter Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -25,7 +25,7 @@
//
//
// $Id: G4TwistedTubs.cc,v 1.24 2007/05/18 07:39:56 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -30,7 +30,7 @@
// and all its terms.
//
// $Id: G4VCSGfaceted.cc,v 1.20 2006/10/20 14:21:36 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -24,8 +24,8 @@
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// $Id: G4VFacet.cc,v 1.5 2007/05/11 13:54:29 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// $Id: G4VFacet.cc,v 1.6 2007/08/23 14:45:03 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-01 $
//
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//
@@ -60,7 +60,7 @@ G4VFacet::G4VFacet ()
P.clear();
E.clear();
centroid = G4ThreeVector(0.0,0.0,0.0);
circumcentre = G4ThreeVector(0.0,0.0,0.0);
radius = 0.0;
radiusSqr = 0.0;
area = 0.0;
@@ -81,7 +81,7 @@ G4bool G4VFacet::operator== (const G4VFacet &right) const
G4double tolerance = kCarTolerance*kCarTolerance/4.0;
if (nVertices != right.GetNumberOfVertices())
{ return false; }
else if ((centroid-right.GetCentroid()).mag2() > tolerance)
else if ((circumcentre-right.GetCircumcentre()).mag2() > tolerance)
{ return false; }
else if (std::fabs((right.GetSurfaceNormal()).dot(surfaceNormal)) < 0.9999999999)
{ return false; }
@@ -25,7 +25,7 @@
//
//
// $Id: G4VTwistSurface.cc,v 1.9 2007/05/31 13:52:48 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------
@@ -24,7 +24,7 @@
// ********************************************************************
//
// $Id: G4VTwistedFaceted.cc,v 1.18 2007/05/25 09:42:34 gcosmo Exp $
// GEANT4 tag $Name: geant4-09-00 $
// GEANT4 tag $Name: geant4-09-01 $
//
//
// --------------------------------------------------------------------