Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/geometry/solids/CSG/src/G4Sphere.cc

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /geometry/solids/CSG/src/G4Sphere.cc (Version 11.3.0) and /geometry/solids/CSG/src/G4Sphere.cc (Version 9.2.p1)


  1 //                                                  1 //
  2 // *******************************************      2 // ********************************************************************
  3 // * License and Disclaimer                         3 // * License and Disclaimer                                           *
  4 // *                                                4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of th      5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided      6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License      7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/      8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.           9 // * include a list of copyright holders.                             *
 10 // *                                               10 // *                                                                  *
 11 // * Neither the authors of this software syst     11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing fin     12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warran     13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assum     14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file      15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitatio     16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                               17 // *                                                                  *
 18 // * This  code  implementation is the result      18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboratio     19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distri     20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  ag     21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publicati     22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Sof     23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // *******************************************     24 // ********************************************************************
 25 //                                                 25 //
                                                   >>  26 //
                                                   >>  27 // $Id: G4Sphere.cc,v 1.68 2008/07/07 09:35:16 grichine Exp $
                                                   >>  28 // GEANT4 tag $Name: geant4-09-02 $
                                                   >>  29 //
                                                   >>  30 // class G4Sphere
                                                   >>  31 //
 26 // Implementation for G4Sphere class               32 // Implementation for G4Sphere class
 27 //                                                 33 //
 28 // 28.03.94 P.Kent: old C++ code converted to  <<  34 // History:
 29 // 17.09.96 V.Grichine: final modifications to <<  35 //
 30 // 30.10.03 J.Apostolakis: new algorithm in In <<  36 // 12.06.08 V.Grichine: fix for theta intersections in DistanceToOut(p,v,...)
                                                   >>  37 // 22.07.05 O.Link    : Added check for intersection with double cone
 31 // 03.05.05 V.Grichine: SurfaceNormal(p) accor     38 // 03.05.05 V.Grichine: SurfaceNormal(p) according to J. Apostolakis proposal
 32 // 22.07.05 O.Link: Added check for intersecti <<  39 // 16.09.04 V.Grichine: bug fixed in SurfaceNormal(p), theta normals
 33 // 26.03.09 G.Cosmo: optimisations and uniform <<  40 // 16.07.04 V.Grichine: bug fixed in DistanceToOut(p,v), Rmin go outside
 34 // 26.10.16 E.Tcherniaev: re-implemented Calcu <<  41 // 02.06.04 V.Grichine: bug fixed in DistanceToIn(p,v), on Rmax,Rmin go inside
 35 //                        G4BoundingEnvelope,  <<  42 // 30.10.03 J.Apostolakis: new algorithm in Inside for SPhi-sections
                                                   >>  43 // 29.10.03 J.Apostolakis: fix in Inside for SPhi-0.5*kAngTol < phi < SPhi, SPhi<0
                                                   >>  44 // 19.06.02 V.Grichine: bug fixed in Inside(p), && -> && fDTheta - kAngTolerance
                                                   >>  45 // 30.01.02 V.Grichine: bug fixed in Inside(p), && -> || at l.451
                                                   >>  46 // 06.03.00 V.Grichine: modifications in Distance ToOut(p,v,...)
                                                   >>  47 // 18.11.99 V.Grichine: side = kNull in Distance ToOut(p,v,...)
                                                   >>  48 // 25.11.98 V.Grichine: bug fixed in DistanceToIn(p,v), phi intersections
                                                   >>  49 // 12.11.98 V.Grichine: bug fixed in DistanceToIn(p,v), theta intersections
                                                   >>  50 // 09.10.98 V.Grichine: modifications in DistanceToOut(p,v,...)
                                                   >>  51 // 17.09.96 V.Grichine: final modifications to commit
                                                   >>  52 // 28.03.94 P.Kent: old C++ code converted to tolerant geometry
 36 // -------------------------------------------     53 // --------------------------------------------------------------------
 37                                                    54 
 38 #include "G4Sphere.hh"                         <<  55 #include <assert.h>
 39                                                    56 
 40 #if !defined(G4GEOM_USE_USPHERE)               <<  57 #include "G4Sphere.hh"
 41                                                    58 
 42 #include "G4GeomTools.hh"                      << 
 43 #include "G4VoxelLimits.hh"                        59 #include "G4VoxelLimits.hh"
 44 #include "G4AffineTransform.hh"                    60 #include "G4AffineTransform.hh"
 45 #include "G4GeometryTolerance.hh"                  61 #include "G4GeometryTolerance.hh"
 46 #include "G4BoundingEnvelope.hh"               << 
 47                                                    62 
 48 #include "G4VPVParameterisation.hh"                63 #include "G4VPVParameterisation.hh"
 49                                                    64 
 50 #include "G4QuickRand.hh"                      <<  65 #include "Randomize.hh"
 51                                                    66 
 52 #include "meshdefs.hh"                             67 #include "meshdefs.hh"
 53                                                    68 
 54 #include "G4VGraphicsScene.hh"                     69 #include "G4VGraphicsScene.hh"
 55 #include "G4VisExtent.hh"                          70 #include "G4VisExtent.hh"
                                                   >>  71 #include "G4Polyhedron.hh"
                                                   >>  72 #include "G4NURBS.hh"
                                                   >>  73 #include "G4NURBSbox.hh"
 56                                                    74 
 57 using namespace CLHEP;                             75 using namespace CLHEP;
 58                                                    76 
 59 // Private enum: Not for external use - used b     77 // Private enum: Not for external use - used by distanceToOut
 60                                                    78 
 61 enum ESide {kNull,kRMin,kRMax,kSPhi,kEPhi,kSTh     79 enum ESide {kNull,kRMin,kRMax,kSPhi,kEPhi,kSTheta,kETheta};
 62                                                    80 
 63 // used by normal                                  81 // used by normal
 64                                                    82 
 65 enum ENorm {kNRMin,kNRMax,kNSPhi,kNEPhi,kNSThe     83 enum ENorm {kNRMin,kNRMax,kNSPhi,kNEPhi,kNSTheta,kNETheta};
 66                                                    84 
 67 //////////////////////////////////////////////     85 ////////////////////////////////////////////////////////////////////////
 68 //                                                 86 //
 69 // constructor - check parameters, convert ang     87 // constructor - check parameters, convert angles so 0<sphi+dpshi<=2_PI
 70 //             - note if pDPhi>2PI then reset      88 //             - note if pDPhi>2PI then reset to 2PI
 71                                                    89 
 72 G4Sphere::G4Sphere( const G4String& pName,         90 G4Sphere::G4Sphere( const G4String& pName,
 73                           G4double pRmin, G4do     91                           G4double pRmin, G4double pRmax,
 74                           G4double pSPhi, G4do     92                           G4double pSPhi, G4double pDPhi,
 75                           G4double pSTheta, G4     93                           G4double pSTheta, G4double pDTheta )
 76   : G4CSGSolid(pName), fSPhi(0.0), fFullPhiSph <<  94   : G4CSGSolid(pName)
 77 {                                                  95 {
 78   kAngTolerance = G4GeometryTolerance::GetInst <<  96   fEpsilon = 1.0e-14;
 79   kRadTolerance = G4GeometryTolerance::GetInst << 
 80                                                    97 
 81   halfCarTolerance = 0.5*kCarTolerance;        <<  98   kRadTolerance = G4GeometryTolerance::GetInstance()->GetRadialTolerance();
 82   halfAngTolerance = 0.5*kAngTolerance;        <<  99   kAngTolerance = G4GeometryTolerance::GetInstance()->GetAngularTolerance();
 83                                                   100 
 84   // Check radii and set radial tolerances     << 101   // Check radii
 85                                                   102 
 86   if ( (pRmin >= pRmax) || (pRmax < 1.1*kRadTo << 103   if (pRmin<pRmax&&pRmin>=0)
                                                   >> 104   {
                                                   >> 105     fRmin=pRmin; fRmax=pRmax;
                                                   >> 106   }
                                                   >> 107   else
 87   {                                               108   {
 88     std::ostringstream message;                << 109     G4cerr << "ERROR - G4Sphere()::G4Sphere(): " << GetName() << G4endl
 89     message << "Invalid radii for Solid: " <<  << 110            << "        Invalide values for radii ! - "
 90             << "        pRmin = " << pRmin <<  << 111            << "        pRmin = " << pRmin << ", pRmax = " << pRmax << G4endl;
 91     G4Exception("G4Sphere::G4Sphere()", "GeomS << 112     G4Exception("G4Sphere::G4Sphere()", "InvalidSetup", FatalException,
 92                 FatalException, message);      << 113                 "Invalid radii");
 93   }                                            << 114   }
 94   fRmin=pRmin; fRmax=pRmax;                    << 
 95   fRminTolerance = (fRmin) != 0.0 ? std::max(  << 
 96   fRmaxTolerance = std::max( kRadTolerance, fE << 
 97                                                   115 
 98   // Check angles                                 116   // Check angles
 99                                                   117 
100   CheckPhiAngles(pSPhi, pDPhi);                << 118   if (pDPhi>=twopi)
101   CheckThetaAngles(pSTheta, pDTheta);          << 119   {
                                                   >> 120     fDPhi=twopi;
                                                   >> 121   }
                                                   >> 122   else if (pDPhi>0)
                                                   >> 123   {
                                                   >> 124     fDPhi=pDPhi;
                                                   >> 125   }
                                                   >> 126   else
                                                   >> 127   {
                                                   >> 128     G4cerr << "ERROR - G4Sphere()::G4Sphere(): " << GetName() << G4endl
                                                   >> 129            << "        Negative Z delta-Phi ! - "
                                                   >> 130            << pDPhi << G4endl;
                                                   >> 131     G4Exception("G4Sphere::G4Sphere()", "InvalidSetup", FatalException,
                                                   >> 132                 "Invalid DPhi.");
                                                   >> 133   }
                                                   >> 134 
                                                   >> 135   // Convert fSPhi to 0-2PI
                                                   >> 136 
                                                   >> 137   if (pSPhi<0)
                                                   >> 138   {
                                                   >> 139     fSPhi=twopi-std::fmod(std::fabs(pSPhi),twopi);
                                                   >> 140   }
                                                   >> 141   else
                                                   >> 142   {
                                                   >> 143     fSPhi=std::fmod(pSPhi,twopi);
                                                   >> 144   }
                                                   >> 145 
                                                   >> 146   // Sphere is placed such that fSPhi+fDPhi>twopi !
                                                   >> 147   // fSPhi could be < 0 !!?
                                                   >> 148   //
                                                   >> 149   if (fSPhi+fDPhi>twopi) fSPhi-=twopi;
                                                   >> 150 
                                                   >> 151   // Check theta angles
                                                   >> 152 
                                                   >> 153   if (pSTheta<0 || pSTheta>pi)
                                                   >> 154   {
                                                   >> 155     G4cerr << "ERROR - G4Sphere()::G4Sphere(): " << GetName() << G4endl;
                                                   >> 156     G4Exception("G4Sphere::G4Sphere()", "InvalidSetup", FatalException,
                                                   >> 157                 "stheta outside 0-PI range.");
                                                   >> 158   }
                                                   >> 159   else
                                                   >> 160   {
                                                   >> 161     fSTheta=pSTheta;
                                                   >> 162   }
                                                   >> 163 
                                                   >> 164   if (pDTheta+pSTheta>=pi)
                                                   >> 165   {
                                                   >> 166     fDTheta=pi-pSTheta;
                                                   >> 167   }
                                                   >> 168   else if (pDTheta>0)
                                                   >> 169   {
                                                   >> 170     fDTheta=pDTheta;
                                                   >> 171   }
                                                   >> 172   else
                                                   >> 173   {
                                                   >> 174     G4cerr << "ERROR - G4Sphere()::G4Sphere(): " << GetName() << G4endl
                                                   >> 175            << "        Negative delta-Theta ! - "
                                                   >> 176            << pDTheta << G4endl;
                                                   >> 177     G4Exception("G4Sphere::G4Sphere()", "InvalidSetup", FatalException,
                                                   >> 178                 "Invalid pDTheta.");
                                                   >> 179   }
102 }                                                 180 }
103                                                   181 
104 //////////////////////////////////////////////    182 ///////////////////////////////////////////////////////////////////////
105 //                                                183 //
106 // Fake default constructor - sets only member    184 // Fake default constructor - sets only member data and allocates memory
107 //                            for usage restri    185 //                            for usage restricted to object persistency.
108 //                                                186 //
109 G4Sphere::G4Sphere( __void__& a )                 187 G4Sphere::G4Sphere( __void__& a )
110   : G4CSGSolid(a)                                 188   : G4CSGSolid(a)
111 {                                                 189 {
112 }                                                 190 }
113                                                   191 
114 //////////////////////////////////////////////    192 /////////////////////////////////////////////////////////////////////
115 //                                                193 //
116 // Destructor                                     194 // Destructor
117                                                   195 
118 G4Sphere::~G4Sphere() = default;               << 196 G4Sphere::~G4Sphere()
119                                                << 
120 ////////////////////////////////////////////// << 
121 //                                             << 
122 // Copy constructor                            << 
123                                                << 
124 G4Sphere::G4Sphere(const G4Sphere&) = default; << 
125                                                << 
126 ////////////////////////////////////////////// << 
127 //                                             << 
128 // Assignment operator                         << 
129                                                << 
130 G4Sphere& G4Sphere::operator = (const G4Sphere << 
131 {                                                 197 {
132    // Check assignment to self                 << 
133    //                                          << 
134    if (this == &rhs)  { return *this; }        << 
135                                                << 
136    // Copy base class data                     << 
137    //                                          << 
138    G4CSGSolid::operator=(rhs);                 << 
139                                                << 
140    // Copy data                                << 
141    //                                          << 
142    fRminTolerance = rhs.fRminTolerance; fRmaxT << 
143    kAngTolerance = rhs.kAngTolerance; kRadTole << 
144    fEpsilon = rhs.fEpsilon; fRmin = rhs.fRmin; << 
145    fSPhi = rhs.fSPhi; fDPhi = rhs.fDPhi; fSThe << 
146    fDTheta = rhs.fDTheta; sinCPhi = rhs.sinCPh << 
147    cosHDPhi = rhs.cosHDPhi;                    << 
148    cosHDPhiOT = rhs.cosHDPhiOT; cosHDPhiIT = r << 
149    sinSPhi = rhs.sinSPhi; cosSPhi = rhs.cosSPh << 
150    sinEPhi = rhs.sinEPhi; cosEPhi = rhs.cosEPh << 
151    hDPhi = rhs.hDPhi; cPhi = rhs.cPhi; ePhi =  << 
152    sinSTheta = rhs.sinSTheta; cosSTheta = rhs. << 
153    sinETheta = rhs.sinETheta; cosETheta = rhs. << 
154    tanSTheta = rhs.tanSTheta; tanSTheta2 = rhs << 
155    tanETheta = rhs.tanETheta; tanETheta2 = rhs << 
156    eTheta = rhs.eTheta; fFullPhiSphere = rhs.f << 
157    fFullThetaSphere = rhs.fFullThetaSphere; fF << 
158    halfCarTolerance = rhs.halfCarTolerance;    << 
159    halfAngTolerance = rhs.halfAngTolerance;    << 
160                                                << 
161    return *this;                               << 
162 }                                                 198 }
163                                                   199 
164 //////////////////////////////////////////////    200 //////////////////////////////////////////////////////////////////////////
165 //                                                201 //
166 // Dispatch to parameterisation for replicatio    202 // Dispatch to parameterisation for replication mechanism dimension
167 // computation & modification.                    203 // computation & modification.
168                                                   204 
169 void G4Sphere::ComputeDimensions(       G4VPVP    205 void G4Sphere::ComputeDimensions(       G4VPVParameterisation* p,
170                                   const G4int     206                                   const G4int n,
171                                   const G4VPhy    207                                   const G4VPhysicalVolume* pRep)
172 {                                                 208 {
173   p->ComputeDimensions(*this,n,pRep);             209   p->ComputeDimensions(*this,n,pRep);
174 }                                                 210 }
175                                                   211 
176 ////////////////////////////////////////////// << 
177 //                                             << 
178 // Get bounding box                            << 
179                                                << 
180 void G4Sphere::BoundingLimits(G4ThreeVector& p << 
181 {                                              << 
182   G4double rmin = GetInnerRadius();            << 
183   G4double rmax = GetOuterRadius();            << 
184                                                << 
185   // Find bounding box                         << 
186   //                                           << 
187   if (GetDeltaThetaAngle() >= pi && GetDeltaPh << 
188   {                                            << 
189     pMin.set(-rmax,-rmax,-rmax);               << 
190     pMax.set( rmax, rmax, rmax);               << 
191   }                                            << 
192   else                                         << 
193   {                                            << 
194     G4double sinStart = GetSinStartTheta();    << 
195     G4double cosStart = GetCosStartTheta();    << 
196     G4double sinEnd   = GetSinEndTheta();      << 
197     G4double cosEnd   = GetCosEndTheta();      << 
198                                                << 
199     G4double stheta = GetStartThetaAngle();    << 
200     G4double etheta = stheta + GetDeltaThetaAn << 
201     G4double rhomin = rmin*std::min(sinStart,s << 
202     G4double rhomax = rmax;                    << 
203     if (stheta > halfpi) rhomax = rmax*sinStar << 
204     if (etheta < halfpi) rhomax = rmax*sinEnd; << 
205                                                << 
206     G4TwoVector xymin,xymax;                   << 
207     G4GeomTools::DiskExtent(rhomin,rhomax,     << 
208                             GetSinStartPhi(),G << 
209                             GetSinEndPhi(),Get << 
210                             xymin,xymax);      << 
211                                                << 
212     G4double zmin = std::min(rmin*cosEnd,rmax* << 
213     G4double zmax = std::max(rmin*cosStart,rma << 
214     pMin.set(xymin.x(),xymin.y(),zmin);        << 
215     pMax.set(xymax.x(),xymax.y(),zmax);        << 
216   }                                            << 
217                                                << 
218   // Check correctness of the bounding box     << 
219   //                                           << 
220   if (pMin.x() >= pMax.x() || pMin.y() >= pMax << 
221   {                                            << 
222     std::ostringstream message;                << 
223     message << "Bad bounding box (min >= max)  << 
224             << GetName() << " !"               << 
225             << "\npMin = " << pMin             << 
226             << "\npMax = " << pMax;            << 
227     G4Exception("G4Sphere::BoundingLimits()",  << 
228                 JustWarning, message);         << 
229     DumpInfo();                                << 
230   }                                            << 
231 }                                              << 
232                                                << 
233 //////////////////////////////////////////////    212 ////////////////////////////////////////////////////////////////////////////
234 //                                                213 //
235 // Calculate extent under transform and specif    214 // Calculate extent under transform and specified limit
236                                                   215 
237 G4bool G4Sphere::CalculateExtent( const EAxis     216 G4bool G4Sphere::CalculateExtent( const EAxis pAxis,
238                                   const G4Voxe    217                                   const G4VoxelLimits& pVoxelLimit,
239                                   const G4Affi    218                                   const G4AffineTransform& pTransform,
240                                         G4doub    219                                         G4double& pMin, G4double& pMax ) const
241 {                                                 220 {
242   G4ThreeVector bmin, bmax;                    << 221   if ( fDPhi==twopi && fDTheta==pi)  // !pTransform.IsRotated() &&
                                                   >> 222   {
                                                   >> 223     // Special case handling for solid spheres-shells
                                                   >> 224     // (rotation doesn't influence).
                                                   >> 225     // Compute x/y/z mins and maxs for bounding box respecting limits,
                                                   >> 226     // with early returns if outside limits. Then switch() on pAxis,
                                                   >> 227     // and compute exact x and y limit for x/y case
                                                   >> 228       
                                                   >> 229     G4double xoffset,xMin,xMax;
                                                   >> 230     G4double yoffset,yMin,yMax;
                                                   >> 231     G4double zoffset,zMin,zMax;
                                                   >> 232 
                                                   >> 233     G4double diff1,diff2,maxDiff,newMin,newMax;
                                                   >> 234     G4double xoff1,xoff2,yoff1,yoff2;
                                                   >> 235 
                                                   >> 236     xoffset=pTransform.NetTranslation().x();
                                                   >> 237     xMin=xoffset-fRmax;
                                                   >> 238     xMax=xoffset+fRmax;
                                                   >> 239     if (pVoxelLimit.IsXLimited())
                                                   >> 240     {
                                                   >> 241       if ( (xMin>pVoxelLimit.GetMaxXExtent()+kCarTolerance)
                                                   >> 242         || (xMax<pVoxelLimit.GetMinXExtent()-kCarTolerance) )
                                                   >> 243       {
                                                   >> 244         return false;
                                                   >> 245       }
                                                   >> 246       else
                                                   >> 247       {
                                                   >> 248         if (xMin<pVoxelLimit.GetMinXExtent())
                                                   >> 249         {
                                                   >> 250           xMin=pVoxelLimit.GetMinXExtent();
                                                   >> 251         }
                                                   >> 252         if (xMax>pVoxelLimit.GetMaxXExtent())
                                                   >> 253         {
                                                   >> 254           xMax=pVoxelLimit.GetMaxXExtent();
                                                   >> 255         }
                                                   >> 256       }
                                                   >> 257     }
243                                                   258 
244   // Get bounding box                          << 259     yoffset=pTransform.NetTranslation().y();
245   BoundingLimits(bmin,bmax);                   << 260     yMin=yoffset-fRmax;
                                                   >> 261     yMax=yoffset+fRmax;
                                                   >> 262     if (pVoxelLimit.IsYLimited())
                                                   >> 263     {
                                                   >> 264       if ( (yMin>pVoxelLimit.GetMaxYExtent()+kCarTolerance)
                                                   >> 265         || (yMax<pVoxelLimit.GetMinYExtent()-kCarTolerance) )
                                                   >> 266       {
                                                   >> 267         return false;
                                                   >> 268       }
                                                   >> 269       else
                                                   >> 270       {
                                                   >> 271         if (yMin<pVoxelLimit.GetMinYExtent())
                                                   >> 272         {
                                                   >> 273           yMin=pVoxelLimit.GetMinYExtent();
                                                   >> 274         }
                                                   >> 275         if (yMax>pVoxelLimit.GetMaxYExtent())
                                                   >> 276         {
                                                   >> 277           yMax=pVoxelLimit.GetMaxYExtent();
                                                   >> 278         }
                                                   >> 279       }
                                                   >> 280     }
246                                                   281 
247   // Find extent                               << 282     zoffset=pTransform.NetTranslation().z();
248   G4BoundingEnvelope bbox(bmin,bmax);          << 283     zMin=zoffset-fRmax;
249   return bbox.CalculateExtent(pAxis,pVoxelLimi << 284     zMax=zoffset+fRmax;
250 }                                              << 285     if (pVoxelLimit.IsZLimited())
                                                   >> 286     {
                                                   >> 287       if ( (zMin>pVoxelLimit.GetMaxZExtent()+kCarTolerance)
                                                   >> 288         || (zMax<pVoxelLimit.GetMinZExtent()-kCarTolerance) )
                                                   >> 289       {
                                                   >> 290         return false;
                                                   >> 291       }
                                                   >> 292       else
                                                   >> 293       {
                                                   >> 294         if (zMin<pVoxelLimit.GetMinZExtent())
                                                   >> 295         {
                                                   >> 296           zMin=pVoxelLimit.GetMinZExtent();
                                                   >> 297         }
                                                   >> 298         if (zMax>pVoxelLimit.GetMaxZExtent())
                                                   >> 299         {
                                                   >> 300           zMax=pVoxelLimit.GetMaxZExtent();
                                                   >> 301         }
                                                   >> 302       }
                                                   >> 303     }
251                                                   304 
252 ////////////////////////////////////////////// << 305     // Known to cut sphere
253 //                                             << 
254 // Return whether point inside/outside/on surf << 
255 // Split into radius, phi, theta checks        << 
256 // Each check modifies 'in', or returns as app << 
257                                                   306 
258 EInside G4Sphere::Inside( const G4ThreeVector& << 307     switch (pAxis)
259 {                                              << 308     {
260   G4double rho,rho2,rad2,tolRMin,tolRMax;      << 309       case kXAxis:
261   G4double pPhi,pTheta;                        << 310         yoff1=yoffset-yMin;
262   EInside in = kOutside;                       << 311         yoff2=yMax-yoffset;
                                                   >> 312         if (yoff1>=0&&yoff2>=0)
                                                   >> 313         {
                                                   >> 314           // Y limits cross max/min x => no change
                                                   >> 315           //
                                                   >> 316           pMin=xMin;
                                                   >> 317           pMax=xMax;
                                                   >> 318         }
                                                   >> 319         else
                                                   >> 320         {
                                                   >> 321           // Y limits don't cross max/min x => compute max delta x,
                                                   >> 322           // hence new mins/maxs
                                                   >> 323           //
                                                   >> 324           diff1=std::sqrt(fRmax*fRmax-yoff1*yoff1);
                                                   >> 325           diff2=std::sqrt(fRmax*fRmax-yoff2*yoff2);
                                                   >> 326           maxDiff=(diff1>diff2) ? diff1:diff2;
                                                   >> 327           newMin=xoffset-maxDiff;
                                                   >> 328           newMax=xoffset+maxDiff;
                                                   >> 329           pMin=(newMin<xMin) ? xMin : newMin;
                                                   >> 330           pMax=(newMax>xMax) ? xMax : newMax;
                                                   >> 331         }
                                                   >> 332         break;
                                                   >> 333       case kYAxis:
                                                   >> 334         xoff1=xoffset-xMin;
                                                   >> 335         xoff2=xMax-xoffset;
                                                   >> 336         if (xoff1>=0&&xoff2>=0)
                                                   >> 337         {
                                                   >> 338           // X limits cross max/min y => no change
                                                   >> 339           //
                                                   >> 340           pMin=yMin;
                                                   >> 341           pMax=yMax;
                                                   >> 342         }
                                                   >> 343         else
                                                   >> 344         {
                                                   >> 345           // X limits don't cross max/min y => compute max delta y,
                                                   >> 346           // hence new mins/maxs
                                                   >> 347           //
                                                   >> 348           diff1=std::sqrt(fRmax*fRmax-xoff1*xoff1);
                                                   >> 349           diff2=std::sqrt(fRmax*fRmax-xoff2*xoff2);
                                                   >> 350           maxDiff=(diff1>diff2) ? diff1:diff2;
                                                   >> 351           newMin=yoffset-maxDiff;
                                                   >> 352           newMax=yoffset+maxDiff;
                                                   >> 353           pMin=(newMin<yMin) ? yMin : newMin;
                                                   >> 354           pMax=(newMax>yMax) ? yMax : newMax;
                                                   >> 355         }
                                                   >> 356         break;
                                                   >> 357       case kZAxis:
                                                   >> 358         pMin=zMin;
                                                   >> 359         pMax=zMax;
                                                   >> 360         break;
                                                   >> 361       default:
                                                   >> 362         break;
                                                   >> 363     }
                                                   >> 364     pMin-=kCarTolerance;
                                                   >> 365     pMax+=kCarTolerance;
                                                   >> 366 
                                                   >> 367     return true;  
                                                   >> 368   }
                                                   >> 369   else       // Transformed cutted sphere
                                                   >> 370   {
                                                   >> 371     G4int i,j,noEntries,noBetweenSections;
                                                   >> 372     G4bool existsAfterClip=false;
263                                                   373 
264   const G4double halfRmaxTolerance = fRmaxTole << 374     // Calculate rotated vertex coordinates
265   const G4double halfRminTolerance = fRminTole << 
266   const G4double Rmax_minus = fRmax - halfRmax << 
267   const G4double Rmin_plus  = (fRmin > 0) ? fR << 
268                                                   375 
269   rho2 = p.x()*p.x() + p.y()*p.y() ;           << 376     G4ThreeVectorList* vertices;
270   rad2 = rho2 + p.z()*p.z() ;                  << 377     G4int  noPolygonVertices ;
                                                   >> 378     vertices=CreateRotatedVertices(pTransform,noPolygonVertices);
271                                                   379 
272   // Check radial surfaces. Sets 'in'          << 380     pMin=+kInfinity;
                                                   >> 381     pMax=-kInfinity;
273                                                   382 
274   tolRMin = Rmin_plus;                         << 383     noEntries=vertices->size();  // noPolygonVertices*noPhiCrossSections
275   tolRMax = Rmax_minus;                        << 384     noBetweenSections=noEntries-noPolygonVertices;
276                                                   385 
277   if(rad2 == 0.0)                              << 386     G4ThreeVectorList ThetaPolygon ;
278   {                                            << 387     for (i=0;i<noEntries;i+=noPolygonVertices)
279     if (fRmin > 0.0)                           << 
280     {                                             388     {
281       return in = kOutside;                    << 389       for(j=0;j<(noPolygonVertices/2)-1;j++)
                                                   >> 390       {
                                                   >> 391         ThetaPolygon.push_back((*vertices)[i+j]) ;      
                                                   >> 392         ThetaPolygon.push_back((*vertices)[i+j+1]) ;      
                                                   >> 393         ThetaPolygon.push_back((*vertices)[i+noPolygonVertices-2-j]) ;      
                                                   >> 394         ThetaPolygon.push_back((*vertices)[i+noPolygonVertices-1-j]) ;      
                                                   >> 395         CalculateClippedPolygonExtent(ThetaPolygon,pVoxelLimit,pAxis,pMin,pMax);
                                                   >> 396         ThetaPolygon.clear() ;
                                                   >> 397       }
282     }                                             398     }
283     if ( (!fFullPhiSphere) || (!fFullThetaSphe << 399     for (i=0;i<noBetweenSections;i+=noPolygonVertices)
284     {                                             400     {
285       return in = kSurface;                    << 401       for(j=0;j<noPolygonVertices-1;j++)
                                                   >> 402       {
                                                   >> 403         ThetaPolygon.push_back((*vertices)[i+j]) ;      
                                                   >> 404         ThetaPolygon.push_back((*vertices)[i+j+1]) ;      
                                                   >> 405         ThetaPolygon.push_back((*vertices)[i+noPolygonVertices+j+1]) ;      
                                                   >> 406         ThetaPolygon.push_back((*vertices)[i+noPolygonVertices+j]) ;      
                                                   >> 407         CalculateClippedPolygonExtent(ThetaPolygon,pVoxelLimit,pAxis,pMin,pMax);
                                                   >> 408         ThetaPolygon.clear() ;
                                                   >> 409       }
                                                   >> 410       ThetaPolygon.push_back((*vertices)[i+noPolygonVertices-1]) ;      
                                                   >> 411       ThetaPolygon.push_back((*vertices)[i]) ;  
                                                   >> 412       ThetaPolygon.push_back((*vertices)[i+noPolygonVertices]) ;      
                                                   >> 413       ThetaPolygon.push_back((*vertices)[i+2*noPolygonVertices-1]) ;      
                                                   >> 414       CalculateClippedPolygonExtent(ThetaPolygon,pVoxelLimit,pAxis,pMin,pMax);
                                                   >> 415       ThetaPolygon.clear() ;
                                                   >> 416     }
                                                   >> 417       
                                                   >> 418     if (pMin!=kInfinity || pMax!=-kInfinity)
                                                   >> 419     {
                                                   >> 420       existsAfterClip=true;
                                                   >> 421 
                                                   >> 422       // Add 2*tolerance to avoid precision troubles
                                                   >> 423       //
                                                   >> 424       pMin-=kCarTolerance;
                                                   >> 425       pMax+=kCarTolerance;
286     }                                             426     }
287     else                                          427     else
288     {                                             428     {
289       return in = kInside;                     << 429       // Check for case where completely enveloping clipping volume
                                                   >> 430       // If point inside then we are confident that the solid completely
                                                   >> 431       // envelopes the clipping volume. Hence set min/max extents according
                                                   >> 432       // to clipping volume extents along the specified axis.
                                                   >> 433 
                                                   >> 434       G4ThreeVector clipCentre(
                                                   >> 435           (pVoxelLimit.GetMinXExtent()+pVoxelLimit.GetMaxXExtent())*0.5,
                                                   >> 436           (pVoxelLimit.GetMinYExtent()+pVoxelLimit.GetMaxYExtent())*0.5,
                                                   >> 437           (pVoxelLimit.GetMinZExtent()+pVoxelLimit.GetMaxZExtent())*0.5);
                                                   >> 438         
                                                   >> 439       if (Inside(pTransform.Inverse().TransformPoint(clipCentre))!=kOutside)
                                                   >> 440       {
                                                   >> 441         existsAfterClip=true;
                                                   >> 442         pMin=pVoxelLimit.GetMinExtent(pAxis);
                                                   >> 443         pMax=pVoxelLimit.GetMaxExtent(pAxis);
                                                   >> 444       }
290     }                                             445     }
                                                   >> 446     delete vertices;
                                                   >> 447     return existsAfterClip;
291   }                                               448   }
                                                   >> 449 }
292                                                   450 
293   if ( (rad2 <= Rmax_minus*Rmax_minus) && (rad << 451 ///////////////////////////////////////////////////////////////////////////
294   {                                            << 452 //
295     in = kInside;                              << 453 // Return whether point inside/outside/on surface
296   }                                            << 454 // Split into radius, phi, theta checks
                                                   >> 455 // Each check modifies `in', or returns as approprate
                                                   >> 456 
                                                   >> 457 EInside G4Sphere::Inside( const G4ThreeVector& p ) const
                                                   >> 458 {
                                                   >> 459   G4double rho,rho2,rad2,tolRMin,tolRMax;
                                                   >> 460   G4double pPhi,pTheta;
                                                   >> 461   EInside in=kOutside;
                                                   >> 462 
                                                   >> 463   rho2 = p.x()*p.x() + p.y()*p.y() ;
                                                   >> 464   rad2 = rho2 + p.z()*p.z() ;
                                                   >> 465 
                                                   >> 466   //  if(rad2 >= 1.369e+19) DBG();
                                                   >> 467   //  G4double rad = std::sqrt(rad2);
                                                   >> 468   // Check radial surfaces
                                                   >> 469   // sets `in'
                                                   >> 470 
                                                   >> 471   if ( fRmin ) tolRMin = fRmin + kRadTolerance*0.5;
                                                   >> 472   else         tolRMin = 0 ;
                                                   >> 473   
                                                   >> 474   tolRMax = fRmax - kRadTolerance*0.5 ;
                                                   >> 475   //  const G4double  fractionTolerance = 1.0e-12;
                                                   >> 476   const G4double  flexRadMaxTolerance = // kRadTolerance;
                                                   >> 477     std::max(kRadTolerance, fEpsilon * fRmax);
                                                   >> 478 
                                                   >> 479   const G4double  Rmax_minus = fRmax - flexRadMaxTolerance*0.5;
                                                   >> 480   const G4double  flexRadMinTolerance = std::max(kRadTolerance, 
                                                   >> 481                      fEpsilon * fRmin);
                                                   >> 482   const G4double  Rmin_plus = (fRmin > 0) ? fRmin + flexRadMinTolerance*0.5 : 0 ;
                                                   >> 483     
                                                   >> 484 if(rad2 <= Rmax_minus*Rmax_minus && rad2 >= Rmin_plus*Rmin_plus) in = kInside ; 
                                                   >> 485 
                                                   >> 486 // if ( rad2 <= tolRMax*tolRMax && rad2 >= tolRMin*tolRMin )  in = kInside ;
                                                   >> 487   // if ( rad <= tolRMax && rad >= tolRMin )  in = kInside ;
297   else                                            488   else
298   {                                               489   {
299     tolRMax = fRmax + halfRmaxTolerance;       << 490     tolRMax = fRmax + kRadTolerance*0.5 ;
300     tolRMin = std::max(fRmin-halfRminTolerance << 491     tolRMin = fRmin - kRadTolerance*0.5 ;
301     if ( (rad2 <= tolRMax*tolRMax) && (rad2 >= << 492 
302     {                                          << 493     if ( tolRMin < 0.0 ) tolRMin = 0.0 ;
303       in = kSurface;                           << 494     
304     }                                          << 495      if ( rad2 <= tolRMax*tolRMax && rad2 >= tolRMin*tolRMin )  in = kSurface ;
305     else                                       << 496     //  if ( rad <= tolRMax && rad >= tolRMin )  in = kSurface ;
306     {                                          << 497     else                                                return in = kOutside ;
307       return in = kOutside;                    << 
308     }                                          << 
309   }                                               498   }
310                                                   499 
311   // Phi boundaries   : Do not check if it has    500   // Phi boundaries   : Do not check if it has no phi boundary!
                                                   >> 501   // (in != kOutside). It is new J.Apostolakis proposal of 30.10.03
312                                                   502 
313   if ( !fFullPhiSphere && (rho2 != 0.0) )  //  << 503   if ( ( fDPhi < twopi - kAngTolerance ) &&
                                                   >> 504        ( (p.x() != 0.0 ) || (p.y() != 0.0) ) )
314   {                                               505   {
315     pPhi = std::atan2(p.y(),p.x()) ;              506     pPhi = std::atan2(p.y(),p.x()) ;
316                                                   507 
317     if      ( pPhi < fSPhi - halfAngTolerance  << 508     if      ( pPhi < fSPhi - kAngTolerance*0.5  )         pPhi += twopi ; 
318     else if ( pPhi > ePhi + halfAngTolerance ) << 509     else if ( pPhi > fSPhi + fDPhi + kAngTolerance*0.5 )  pPhi -= twopi; 
319                                                << 510     
320     if ( (pPhi < fSPhi - halfAngTolerance)     << 511     if ((pPhi < fSPhi - kAngTolerance*0.5) ||  
321       || (pPhi > ePhi + halfAngTolerance) )    << 512         (pPhi > fSPhi + fDPhi + kAngTolerance*0.5) )  return in = kOutside ;
322                                                << 513     
323     else if (in == kInside)  // else it's kSur    514     else if (in == kInside)  // else it's kSurface anyway already
324     {                                             515     {
325       if ( (pPhi < fSPhi + halfAngTolerance)   << 516       if ( (pPhi < fSPhi + kAngTolerance*0.5) || 
326         || (pPhi > ePhi - halfAngTolerance) )  << 517            (pPhi > fSPhi + fDPhi - kAngTolerance*0.5) )      in = kSurface ;       
327     }                                             518     }
328   }                                               519   }
329                                                   520 
330   // Theta bondaries                              521   // Theta bondaries
331                                                << 522   // (in!=kOutside)
332   if ( ((rho2 != 0.0) || (p.z() != 0.0)) && (! << 523   
                                                   >> 524   if ( (rho2 || p.z()) && fDTheta < pi - kAngTolerance*0.5 )
333   {                                               525   {
334     rho    = std::sqrt(rho2);                     526     rho    = std::sqrt(rho2);
335     pTheta = std::atan2(rho,p.z());               527     pTheta = std::atan2(rho,p.z());
336                                                   528 
337     if ( in == kInside )                          529     if ( in == kInside )
338     {                                             530     {
339       if ( ((fSTheta > 0.0) && (pTheta < fSThe << 531       if ( (pTheta < fSTheta + kAngTolerance*0.5)
340         || ((eTheta < pi) && (pTheta > eTheta  << 532         || (pTheta > fSTheta + fDTheta - kAngTolerance*0.5) )
341       {                                           533       {
342         if ( (( (fSTheta>0.0)&&(pTheta>=fSThet << 534         if ( (pTheta >= fSTheta - kAngTolerance*0.5)
343              || (fSTheta == 0.0) )             << 535           && (pTheta <= fSTheta + fDTheta + kAngTolerance*0.5) )
344           && ((eTheta==pi)||(pTheta <= eTheta  << 
345         {                                         536         {
346           in = kSurface;                       << 537           in = kSurface ;
347         }                                         538         }
348         else                                      539         else
349         {                                         540         {
350           in = kOutside;                       << 541           in = kOutside ;
351         }                                         542         }
352       }                                           543       }
353     }                                             544     }
354     else                                          545     else
355     {                                             546     {
356         if ( ((fSTheta > 0.0)&&(pTheta < fSThe << 547       if ( (pTheta < fSTheta - kAngTolerance*0.5)
357            ||((eTheta < pi  )&&(pTheta > eThet << 548         || (pTheta > fSTheta + fDTheta + kAngTolerance*0.5) )
358       {                                           549       {
359         in = kOutside;                         << 550         in = kOutside ;
360       }                                           551       }
361     }                                             552     }
362   }                                               553   }
363   return in;                                      554   return in;
364 }                                                 555 }
365                                                   556 
366 //////////////////////////////////////////////    557 /////////////////////////////////////////////////////////////////////
367 //                                                558 //
368 // Return unit normal of surface closest to p     559 // Return unit normal of surface closest to p
369 // - note if point on z axis, ignore phi divid    560 // - note if point on z axis, ignore phi divided sides
370 // - unsafe if point close to z axis a rmin=0     561 // - unsafe if point close to z axis a rmin=0 - no explicit checks
371                                                   562 
372 G4ThreeVector G4Sphere::SurfaceNormal( const G    563 G4ThreeVector G4Sphere::SurfaceNormal( const G4ThreeVector& p ) const
373 {                                                 564 {
374   G4int noSurfaces = 0;                        << 565   G4int noSurfaces = 0;  
375   G4double rho, rho2, radius, pTheta, pPhi=0.; << 566   G4double rho, rho2, rad, pTheta, pPhi=0.;
376   G4double distRMin = kInfinity;                  567   G4double distRMin = kInfinity;
377   G4double distSPhi = kInfinity, distEPhi = kI    568   G4double distSPhi = kInfinity, distEPhi = kInfinity;
378   G4double distSTheta = kInfinity, distETheta     569   G4double distSTheta = kInfinity, distETheta = kInfinity;
                                                   >> 570   G4double delta = 0.5*kCarTolerance, dAngle = 0.5*kAngTolerance;
379   G4ThreeVector nR, nPs, nPe, nTs, nTe, nZ(0.,    571   G4ThreeVector nR, nPs, nPe, nTs, nTe, nZ(0.,0.,1.);
380   G4ThreeVector norm, sumnorm(0.,0.,0.);          572   G4ThreeVector norm, sumnorm(0.,0.,0.);
381                                                   573 
382   rho2 = p.x()*p.x()+p.y()*p.y();                 574   rho2 = p.x()*p.x()+p.y()*p.y();
383   radius = std::sqrt(rho2+p.z()*p.z());        << 575   rad  = std::sqrt(rho2+p.z()*p.z());
384   rho  = std::sqrt(rho2);                         576   rho  = std::sqrt(rho2);
385                                                   577 
386   G4double    distRMax = std::fabs(radius-fRma << 578   G4double    distRMax = std::fabs(rad-fRmax);
387   if (fRmin != 0.0)  distRMin = std::fabs(radi << 579   if (fRmin)  distRMin = std::fabs(rad-fRmin);
388                                                << 580     
389   if ( (rho != 0.0) && !fFullSphere )          << 581   if ( rho && (fDPhi < twopi || fDTheta < pi) )
390   {                                               582   {
391     pPhi = std::atan2(p.y(),p.x());               583     pPhi = std::atan2(p.y(),p.x());
392                                                   584 
393     if (pPhi < fSPhi-halfAngTolerance)     { p << 585     if(pPhi  < fSPhi-dAngle)           pPhi     += twopi;
394     else if (pPhi > ePhi+halfAngTolerance) { p << 586     else if(pPhi > fSPhi+fDPhi+dAngle) pPhi     -= twopi;
395   }                                               587   }
396   if ( !fFullPhiSphere )                       << 588   if ( fDPhi < twopi ) // && rho ) // old limitation against (0,0,z)
397   {                                               589   {
398     if ( rho != 0.0 )                          << 590     if ( rho )
399     {                                             591     {
400       distSPhi = std::fabs( pPhi-fSPhi );      << 592       distSPhi = std::fabs( pPhi - fSPhi ); 
401       distEPhi = std::fabs( pPhi-ePhi );       << 593       distEPhi = std::fabs(pPhi-fSPhi-fDPhi); 
402     }                                             594     }
403     else if( fRmin == 0.0 )                    << 595     else if( !fRmin )
404     {                                             596     {
405       distSPhi = 0.;                           << 597       distSPhi = 0.; 
406       distEPhi = 0.;                           << 598       distEPhi = 0.; 
407     }                                             599     }
408     nPs = G4ThreeVector(sinSPhi,-cosSPhi,0);   << 600     nPs = G4ThreeVector(std::sin(fSPhi),-std::cos(fSPhi),0);
409     nPe = G4ThreeVector(-sinEPhi,cosEPhi,0);   << 601     nPe = G4ThreeVector(-std::sin(fSPhi+fDPhi),std::cos(fSPhi+fDPhi),0);
410   }                                            << 602   }        
411   if ( !fFullThetaSphere )                     << 603   if ( fDTheta < pi ) // && rad ) // old limitation against (0,0,0)
412   {                                               604   {
413     if ( rho != 0.0 )                          << 605     if ( rho )
414     {                                             606     {
415       pTheta     = std::atan2(rho,p.z());         607       pTheta     = std::atan2(rho,p.z());
416       distSTheta = std::fabs(pTheta-fSTheta);  << 608       distSTheta = std::fabs(pTheta-fSTheta); 
417       distETheta = std::fabs(pTheta-eTheta);   << 609       distETheta = std::fabs(pTheta-fSTheta-fDTheta);
418                                                << 610  
419       nTs = G4ThreeVector(-cosSTheta*p.x()/rho << 611       nTs = G4ThreeVector(-std::cos(fSTheta)*p.x()/rho, //  *std::cos(pPhi),
420                           -cosSTheta*p.y()/rho << 612                           -std::cos(fSTheta)*p.y()/rho, //  *std::sin(pPhi),
421                            sinSTheta           << 613                            std::sin(fSTheta)                   );
422                                                << 614 
423       nTe = G4ThreeVector( cosETheta*p.x()/rho << 615       nTe = G4ThreeVector( std::cos(fSTheta+fDTheta)*p.x()/rho, // *std::cos(pPhi),
424                            cosETheta*p.y()/rho << 616                            std::cos(fSTheta+fDTheta)*p.y()/rho, // *std::sin(pPhi),
425                           -sinETheta           << 617                           -std::sin(fSTheta+fDTheta)                  );    
426     }                                             618     }
427     else if( fRmin == 0.0 )                    << 619     else if( !fRmin )
428     {                                             620     {
429       if ( fSTheta != 0.0 )                    << 621       if ( fSTheta )  
430       {                                        << 622       {              
431         distSTheta = 0.;                          623         distSTheta = 0.;
432         nTs = G4ThreeVector(0.,0.,-1.);        << 624   nTs = G4ThreeVector(0.,0.,-1.);
433       }                                           625       }
434       if ( eTheta < pi )                       << 626       if ( fSTheta + fDTheta < pi ) // distETheta = 0.;
435       {                                        << 627       {              
436         distETheta = 0.;                          628         distETheta = 0.;
437         nTe = G4ThreeVector(0.,0.,1.);         << 629   nTe = G4ThreeVector(0.,0.,1.);
438       }                                           630       }
439     }                                          << 631     }    
440   }                                               632   }
441   if( radius != 0.0 )  { nR = G4ThreeVector(p. << 633   if( rad )  nR = G4ThreeVector(p.x()/rad,p.y()/rad,p.z()/rad);
442                                                   634 
443   if( distRMax <= halfCarTolerance )           << 635   if( distRMax <= delta )
444   {                                               636   {
445     ++noSurfaces;                              << 637     noSurfaces ++;
446     sumnorm += nR;                                638     sumnorm += nR;
447   }                                               639   }
448   if( (fRmin != 0.0) && (distRMin <= halfCarTo << 640   if( fRmin && distRMin <= delta )
449   {                                               641   {
450     ++noSurfaces;                              << 642     noSurfaces ++;
451     sumnorm -= nR;                                643     sumnorm -= nR;
452   }                                               644   }
453   if( !fFullPhiSphere )                        << 645   if( fDPhi < twopi )   
454   {                                               646   {
455     if (distSPhi <= halfAngTolerance)          << 647     if (distSPhi <= dAngle)
456     {                                             648     {
457       ++noSurfaces;                            << 649       noSurfaces ++;
458       sumnorm += nPs;                             650       sumnorm += nPs;
459     }                                             651     }
460     if (distEPhi <= halfAngTolerance)          << 652     if (distEPhi <= dAngle) 
461     {                                             653     {
462       ++noSurfaces;                            << 654       noSurfaces ++;
463       sumnorm += nPe;                             655       sumnorm += nPe;
464     }                                             656     }
465   }                                               657   }
466   if ( !fFullThetaSphere )                     << 658   if ( fDTheta < pi )
467   {                                               659   {
468     if ((distSTheta <= halfAngTolerance) && (f << 660     if (distSTheta <= dAngle && fSTheta > 0.)
469     {                                             661     {
470       ++noSurfaces;                            << 662       noSurfaces ++;
471       if ((radius <= halfCarTolerance) && fFul << 663       if( rad <= delta && fDPhi >= twopi) sumnorm += nZ;
472       else                                     << 664       else                                sumnorm += nTs;
473     }                                             665     }
474     if ((distETheta <= halfAngTolerance) && (e << 666     if (distETheta <= dAngle && fSTheta+fDTheta < pi) 
475     {                                             667     {
476       ++noSurfaces;                            << 668       noSurfaces ++;
477       if ((radius <= halfCarTolerance) && fFul << 669       if( rad <= delta && fDPhi >= twopi) sumnorm -= nZ;
478       else                                     << 670       else                                sumnorm += nTe;
479       if(sumnorm.z() == 0.)  { sumnorm += nZ;  << 671       if(sumnorm.z() == 0.)               sumnorm += nZ;
480     }                                             672     }
481   }                                               673   }
482   if ( noSurfaces == 0 )                          674   if ( noSurfaces == 0 )
483   {                                               675   {
484 #ifdef G4CSGDEBUG                                 676 #ifdef G4CSGDEBUG
485     G4Exception("G4Sphere::SurfaceNormal(p)",  << 677     G4Exception("G4Sphere::SurfaceNormal(p)", "Notification", JustWarning, 
486                 JustWarning, "Point p is not o << 678                 "Point p is not on surface !?" ); 
487 #endif                                            679 #endif
488      norm = ApproxSurfaceNormal(p);               680      norm = ApproxSurfaceNormal(p);
489   }                                               681   }
490   else if ( noSurfaces == 1 )  { norm = sumnor << 682   else if ( noSurfaces == 1 ) norm = sumnorm;
491   else                         { norm = sumnor << 683   else                        norm = sumnorm.unit();
492   return norm;                                    684   return norm;
493 }                                                 685 }
494                                                   686 
495                                                   687 
496 ////////////////////////////////////////////// << 688 /////////////////////////////////////////////////////////////////////////////////////////////
497 //                                                689 //
498 // Algorithm for SurfaceNormal() following the    690 // Algorithm for SurfaceNormal() following the original specification
499 // for points not on the surface                  691 // for points not on the surface
500                                                   692 
501 G4ThreeVector G4Sphere::ApproxSurfaceNormal( c    693 G4ThreeVector G4Sphere::ApproxSurfaceNormal( const G4ThreeVector& p ) const
502 {                                                 694 {
503   ENorm side;                                     695   ENorm side;
504   G4ThreeVector norm;                             696   G4ThreeVector norm;
505   G4double rho,rho2,radius,pPhi,pTheta;        << 697   G4double rho,rho2,rad,pPhi,pTheta;
506   G4double distRMin,distRMax,distSPhi,distEPhi    698   G4double distRMin,distRMax,distSPhi,distEPhi,
507            distSTheta,distETheta,distMin;         699            distSTheta,distETheta,distMin;
508                                                   700 
509   rho2=p.x()*p.x()+p.y()*p.y();                   701   rho2=p.x()*p.x()+p.y()*p.y();
510   radius=std::sqrt(rho2+p.z()*p.z());          << 702   rad=std::sqrt(rho2+p.z()*p.z());
511   rho=std::sqrt(rho2);                            703   rho=std::sqrt(rho2);
512                                                   704 
513   //                                              705   //
514   // Distance to r shells                         706   // Distance to r shells
515   //                                              707   //
516                                                   708 
517   distRMax=std::fabs(radius-fRmax);            << 709   distRMax=std::fabs(rad-fRmax);
518   if (fRmin != 0.0)                            << 710   if (fRmin)
519   {                                               711   {
520     distRMin=std::fabs(radius-fRmin);          << 712     distRMin=std::fabs(rad-fRmin);
521                                                << 713       
522     if (distRMin<distRMax)                        714     if (distRMin<distRMax)
523     {                                             715     {
524       distMin=distRMin;                           716       distMin=distRMin;
525       side=kNRMin;                                717       side=kNRMin;
526     }                                             718     }
527     else                                          719     else
528     {                                          << 720     {   
529       distMin=distRMax;                           721       distMin=distRMax;
530       side=kNRMax;                                722       side=kNRMax;
531     }                                             723     }
532   }                                               724   }
533   else                                            725   else
534   {                                               726   {
535     distMin=distRMax;                             727     distMin=distRMax;
536     side=kNRMax;                                  728     side=kNRMax;
537   }                                               729   }
538                                                   730 
539   //                                              731   //
540   // Distance to phi planes                       732   // Distance to phi planes
541   //                                              733   //
542   // Protected against (0,0,z)                 << 734   // Protected against (0,0,z) 
543                                                << 735     
544   pPhi = std::atan2(p.y(),p.x());                 736   pPhi = std::atan2(p.y(),p.x());
545   if (pPhi<0) { pPhi += twopi; }               << 737   if (pPhi<0) pPhi += twopi;
546                                                   738 
547   if (!fFullPhiSphere && (rho != 0.0))         << 739   if (fDPhi<twopi&&rho)
548   {                                               740   {
549     if (fSPhi<0)                                  741     if (fSPhi<0)
550     {                                             742     {
551       distSPhi=std::fabs(pPhi-(fSPhi+twopi))*r    743       distSPhi=std::fabs(pPhi-(fSPhi+twopi))*rho;
552     }                                             744     }
553     else                                          745     else
554     {                                             746     {
555       distSPhi=std::fabs(pPhi-fSPhi)*rho;         747       distSPhi=std::fabs(pPhi-fSPhi)*rho;
556     }                                             748     }
557                                                   749 
558     distEPhi=std::fabs(pPhi-fSPhi-fDPhi)*rho;     750     distEPhi=std::fabs(pPhi-fSPhi-fDPhi)*rho;
559                                                   751 
560     // Find new minimum                           752     // Find new minimum
561     //                                            753     //
562     if (distSPhi<distEPhi)                        754     if (distSPhi<distEPhi)
563     {                                             755     {
564       if (distSPhi<distMin)                       756       if (distSPhi<distMin)
565       {                                           757       {
566         distMin = distSPhi;                    << 758         distMin=distSPhi;
567         side = kNSPhi;                         << 759         side=kNSPhi;
568       }                                           760       }
569     }                                             761     }
570     else                                          762     else
571     {                                             763     {
572       if (distEPhi<distMin)                       764       if (distEPhi<distMin)
573       {                                           765       {
574         distMin = distEPhi;                    << 766         distMin=distEPhi;
575         side = kNEPhi;                         << 767         side=kNEPhi;
576       }                                           768       }
577     }                                             769     }
578   }                                               770   }
579                                                   771 
580   //                                              772   //
581   // Distance to theta planes                     773   // Distance to theta planes
582   //                                              774   //
583                                                   775 
584   if (!fFullThetaSphere && (radius != 0.0))    << 776   if (fDTheta<pi&&rad)
585   {                                               777   {
586     pTheta=std::atan2(rho,p.z());                 778     pTheta=std::atan2(rho,p.z());
587     distSTheta=std::fabs(pTheta-fSTheta)*radiu << 779     distSTheta=std::fabs(pTheta-fSTheta)*rad;
588     distETheta=std::fabs(pTheta-fSTheta-fDThet << 780     distETheta=std::fabs(pTheta-fSTheta-fDTheta)*rad;
589                                                   781 
590     // Find new minimum                           782     // Find new minimum
591     //                                            783     //
592     if (distSTheta<distETheta)                    784     if (distSTheta<distETheta)
593     {                                             785     {
594       if (distSTheta<distMin)                     786       if (distSTheta<distMin)
595       {                                           787       {
596         distMin = distSTheta ;                    788         distMin = distSTheta ;
597         side = kNSTheta ;                         789         side = kNSTheta ;
598       }                                           790       }
599     }                                             791     }
600     else                                          792     else
601     {                                             793     {
602       if (distETheta<distMin)                     794       if (distETheta<distMin)
603       {                                           795       {
604         distMin = distETheta ;                    796         distMin = distETheta ;
605         side = kNETheta ;                         797         side = kNETheta ;
606       }                                           798       }
607     }                                             799     }
608   }                                               800   }
609                                                   801 
610   switch (side)                                   802   switch (side)
611   {                                               803   {
612     case kNRMin:      // Inner radius             804     case kNRMin:      // Inner radius
613       norm=G4ThreeVector(-p.x()/radius,-p.y()/ << 805       norm=G4ThreeVector(-p.x()/rad,-p.y()/rad,-p.z()/rad);
614       break;                                      806       break;
615     case kNRMax:      // Outer radius             807     case kNRMax:      // Outer radius
616       norm=G4ThreeVector(p.x()/radius,p.y()/ra << 808       norm=G4ThreeVector(p.x()/rad,p.y()/rad,p.z()/rad);
617       break;                                      809       break;
618     case kNSPhi:                                  810     case kNSPhi:
619       norm=G4ThreeVector(sinSPhi,-cosSPhi,0);  << 811       norm=G4ThreeVector(std::sin(fSPhi),-std::cos(fSPhi),0);
620       break;                                      812       break;
621     case kNEPhi:                                  813     case kNEPhi:
622       norm=G4ThreeVector(-sinEPhi,cosEPhi,0);  << 814       norm=G4ThreeVector(-std::sin(fSPhi+fDPhi),std::cos(fSPhi+fDPhi),0);
623       break;                                      815       break;
624     case kNSTheta:                                816     case kNSTheta:
625       norm=G4ThreeVector(-cosSTheta*std::cos(p << 817       norm=G4ThreeVector(-std::cos(fSTheta)*std::cos(pPhi),
626                          -cosSTheta*std::sin(p << 818                          -std::cos(fSTheta)*std::sin(pPhi),
627                           sinSTheta            << 819                           std::sin(fSTheta)            );
                                                   >> 820       //  G4cout<<G4endl<<" case kNSTheta:"<<G4endl;
                                                   >> 821       //  G4cout<<"pPhi = "<<pPhi<<G4endl;
                                                   >> 822       //  G4cout<<"rad  = "<<rad<<G4endl;
                                                   >> 823       //  G4cout<<"pho  = "<<rho<<G4endl;
                                                   >> 824       //  G4cout<<"p:    "<<p.x()<<"; "<<p.y()<<"; "<<p.z()<<G4endl;
                                                   >> 825       //  G4cout<<"norm: "<<norm.x()<<"; "<<norm.y()<<"; "<<norm.z()<<G4endl;
628       break;                                      826       break;
629     case kNETheta:                                827     case kNETheta:
630       norm=G4ThreeVector( cosETheta*std::cos(p << 828       norm=G4ThreeVector( std::cos(fSTheta+fDTheta)*std::cos(pPhi),
631                           cosETheta*std::sin(p << 829                           std::cos(fSTheta+fDTheta)*std::sin(pPhi),
632                          -sinETheta            << 830                          -std::sin(fSTheta+fDTheta)              );
                                                   >> 831 
                                                   >> 832       //  G4cout<<G4endl<<" case kNETheta:"<<G4endl;
                                                   >> 833       //  G4cout<<"pPhi = "<<pPhi<<G4endl;
                                                   >> 834       //  G4cout<<"rad  = "<<rad<<G4endl;
                                                   >> 835       //  G4cout<<"pho  = "<<rho<<G4endl;
                                                   >> 836       //  G4cout<<"p:    "<<p.x()<<"; "<<p.y()<<"; "<<p.z()<<G4endl;
                                                   >> 837       //  G4cout<<"norm: "<<norm.x()<<"; "<<norm.y()<<"; "<<norm.z()<<G4endl;
633       break;                                      838       break;
634     default:          // Should never reach th << 839     default:
635       DumpInfo();                                 840       DumpInfo();
636       G4Exception("G4Sphere::ApproxSurfaceNorm << 841       G4Exception("G4Sphere::ApproxSurfaceNormal()", "Notification", JustWarning,
637                   "GeomSolids1002", JustWarnin << 
638                   "Undefined side for valid su    842                   "Undefined side for valid surface normal to solid.");
639       break;                                   << 843       break;    
640   }                                            << 844   } // end case
641                                                   845 
642   return norm;                                    846   return norm;
643 }                                                 847 }
644                                                   848 
645 //////////////////////////////////////////////    849 ///////////////////////////////////////////////////////////////////////////////
646 //                                                850 //
647 // Calculate distance to shape from outside, a    851 // Calculate distance to shape from outside, along normalised vector
648 // - return kInfinity if no intersection, or i    852 // - return kInfinity if no intersection, or intersection distance <= tolerance
649 //                                                853 //
650 // -> If point is outside outer radius, comput    854 // -> If point is outside outer radius, compute intersection with rmax
651 //        - if no intersection return             855 //        - if no intersection return
652 //        - if  valid phi,theta return interse    856 //        - if  valid phi,theta return intersection Dist
653 //                                                857 //
654 // -> If shell, compute intersection with inne    858 // -> If shell, compute intersection with inner radius, taking largest +ve root
655 //        - if valid phi,theta, save intersect    859 //        - if valid phi,theta, save intersection
656 //                                                860 //
657 // -> If phi segmented, compute intersection w    861 // -> If phi segmented, compute intersection with phi half planes
658 //        - if valid intersection(r,theta), re    862 //        - if valid intersection(r,theta), return smallest intersection of
659 //          inner shell & phi intersection        863 //          inner shell & phi intersection
660 //                                                864 //
661 // -> If theta segmented, compute intersection    865 // -> If theta segmented, compute intersection with theta cones
662 //        - if valid intersection(r,phi), retu    866 //        - if valid intersection(r,phi), return smallest intersection of
663 //          inner shell & theta intersection      867 //          inner shell & theta intersection
664 //                                                868 //
665 //                                                869 //
666 // NOTE:                                          870 // NOTE:
667 // - `if valid' (above) implies tolerant check    871 // - `if valid' (above) implies tolerant checking of intersection points
668 //                                                872 //
669 // OPT:                                           873 // OPT:
670 // Move tolIO/ORmin/RMax2 precalcs to where th    874 // Move tolIO/ORmin/RMax2 precalcs to where they are needed -
671 // not required for most cases.                   875 // not required for most cases.
672 // Avoid atan2 for non theta cut G4Sphere.        876 // Avoid atan2 for non theta cut G4Sphere.
673                                                   877 
674 G4double G4Sphere::DistanceToIn( const G4Three    878 G4double G4Sphere::DistanceToIn( const G4ThreeVector& p,
675                                  const G4Three    879                                  const G4ThreeVector& v  ) const
676 {                                                 880 {
677   G4double snxt = kInfinity ;      // snxt = d    881   G4double snxt = kInfinity ;      // snxt = default return value
                                                   >> 882 
678   G4double rho2, rad2, pDotV2d, pDotV3d, pThet    883   G4double rho2, rad2, pDotV2d, pDotV3d, pTheta ;
679   G4double tolSTheta=0., tolETheta=0. ;        << 
680   const G4double dRmax = 100.*fRmax;           << 
681                                                   884 
682   const G4double halfRmaxTolerance = fRmaxTole << 885   G4double tolIRMin2, tolORMin2, tolORMax2, tolIRMax2 ;
683   const G4double halfRminTolerance = fRminTole << 886   G4double tolSTheta=0., tolETheta=0. ;
684   const G4double tolORMin2 = (fRmin>halfRminTo << 
685                ? (fRmin-halfRminTolerance)*(fR << 
686   const G4double tolIRMin2 =                   << 
687                (fRmin+halfRminTolerance)*(fRmi << 
688   const G4double tolORMax2 =                   << 
689                (fRmax+halfRmaxTolerance)*(fRma << 
690   const G4double tolIRMax2 =                   << 
691                (fRmax-halfRmaxTolerance)*(fRma << 
692                                                   887 
693   // Intersection point                           888   // Intersection point
694   //                                           << 889 
695   G4double xi, yi, zi, rhoi, rhoi2, radi2, iTh    890   G4double xi, yi, zi, rhoi, rhoi2, radi2, iTheta ;
696                                                   891 
697   // Phi intersection                             892   // Phi intersection
698   //                                           << 
699   G4double Comp ;                              << 
700                                                   893 
701   // Phi precalcs                              << 894   G4double sinSPhi, cosSPhi, ePhi, sinEPhi, cosEPhi , Comp ; 
702   //                                           << 895 
                                                   >> 896   // Phi flag and precalcs
                                                   >> 897 
                                                   >> 898   G4bool segPhi ;        
                                                   >> 899   G4double hDPhi, hDPhiOT, hDPhiIT, cPhi, sinCPhi=0., cosCPhi=0. ; 
                                                   >> 900   G4double cosHDPhiOT=0., cosHDPhiIT=0. ;
703   G4double Dist, cosPsi ;                         901   G4double Dist, cosPsi ;
704                                                   902 
705   // Theta precalcs                            << 903   G4bool segTheta ;                             // Theta flag and precals
706   //                                           << 904   G4double tanSTheta, tanETheta ;
                                                   >> 905   G4double tanSTheta2, tanETheta2 ;
707   G4double dist2STheta, dist2ETheta ;             906   G4double dist2STheta, dist2ETheta ;
708   G4double t1, t2, b, c, d2, d, sd = kInfinity << 907   G4double t1, t2, b, c, d2, d, s = kInfinity ;
709                                                   908 
710   // General Precalcs                             909   // General Precalcs
711   //                                           << 910 
712   rho2 = p.x()*p.x() + p.y()*p.y() ;              911   rho2 = p.x()*p.x() + p.y()*p.y() ;
713   rad2 = rho2 + p.z()*p.z() ;                     912   rad2 = rho2 + p.z()*p.z() ;
714   pTheta = std::atan2(std::sqrt(rho2),p.z()) ;    913   pTheta = std::atan2(std::sqrt(rho2),p.z()) ;
715                                                   914 
716   pDotV2d = p.x()*v.x() + p.y()*v.y() ;           915   pDotV2d = p.x()*v.x() + p.y()*v.y() ;
717   pDotV3d = pDotV2d + p.z()*v.z() ;               916   pDotV3d = pDotV2d + p.z()*v.z() ;
718                                                   917 
719   // Theta precalcs                            << 918   // Radial Precalcs
720   //                                           << 919 
721   if (!fFullThetaSphere)                       << 920   if (fRmin > kRadTolerance*0.5)
722   {                                               921   {
723     tolSTheta = fSTheta - halfAngTolerance ;   << 922     tolORMin2=(fRmin-kRadTolerance*0.5)*(fRmin-kRadTolerance*0.5);
724     tolETheta = eTheta + halfAngTolerance ;    << 923   }
                                                   >> 924   else
                                                   >> 925   {
                                                   >> 926     tolORMin2 = 0 ;
                                                   >> 927   }
                                                   >> 928   tolIRMin2 = (fRmin+kRadTolerance*0.5)*(fRmin+kRadTolerance*0.5) ;
                                                   >> 929   tolORMax2 = (fRmax+kRadTolerance*0.5)*(fRmax+kRadTolerance*0.5) ;
                                                   >> 930   tolIRMax2 = (fRmax-kRadTolerance*0.5)*(fRmax-kRadTolerance*0.5) ;
725                                                   931 
726     // Special case rad2 = 0 comparing with di << 932   // Set phi divided flag and precalcs
727     //                                         << 933 
728     if ((rad2!=0.0) || (fRmin!=0.0))           << 934   if (fDPhi < twopi)
729     {                                          << 935   {
730       // Keep going for computation of distanc << 936     segPhi = true ;
731     }                                          << 937     hDPhi = 0.5*fDPhi ;    // half delta phi
732     else  // Positioned on the sphere's origin << 938     cPhi = fSPhi + hDPhi ;
733     {                                          << 939 
734       G4double vTheta = std::atan2(std::sqrt(v << 940     hDPhiOT = hDPhi+0.5*kAngTolerance; // Outer Tolerant half delta phi 
735       if ( (vTheta < tolSTheta) || (vTheta > t << 941     hDPhiIT = hDPhi-0.5*kAngTolerance;
736       {                                        << 942 
737         return snxt ; // kInfinity             << 943     sinCPhi    = std::sin(cPhi) ;
738       }                                        << 944     cosCPhi    = std::cos(cPhi) ;
739       return snxt = 0.0 ;                      << 945     cosHDPhiOT = std::cos(hDPhiOT) ;
740     }                                          << 946     cosHDPhiIT = std::cos(hDPhiIT) ;
                                                   >> 947   }
                                                   >> 948   else
                                                   >> 949   {
                                                   >> 950     segPhi = false ;
                                                   >> 951   }
                                                   >> 952 
                                                   >> 953   // Theta precalcs
                                                   >> 954     
                                                   >> 955   if (fDTheta < pi )
                                                   >> 956   {
                                                   >> 957     segTheta  = true ;
                                                   >> 958     tolSTheta = fSTheta - kAngTolerance*0.5 ;
                                                   >> 959     tolETheta = fSTheta + fDTheta + kAngTolerance*0.5 ;
                                                   >> 960   }
                                                   >> 961   else
                                                   >> 962   {
                                                   >> 963     segTheta = false ;
741   }                                               964   }
742                                                   965 
743   // Outer spherical shell intersection           966   // Outer spherical shell intersection
744   // - Only if outside tolerant fRmax             967   // - Only if outside tolerant fRmax
745   // - Check for if inside and outer G4Sphere     968   // - Check for if inside and outer G4Sphere heading through solid (-> 0)
746   // - No intersect -> no intersection with G4    969   // - No intersect -> no intersection with G4Sphere
747   //                                              970   //
748   // Shell eqn: x^2+y^2+z^2=RSPH^2                971   // Shell eqn: x^2+y^2+z^2=RSPH^2
749   //                                              972   //
750   // => (px+svx)^2+(py+svy)^2+(pz+svz)^2=R^2      973   // => (px+svx)^2+(py+svy)^2+(pz+svz)^2=R^2
751   //                                              974   //
752   // => (px^2+py^2+pz^2) +2sd(pxvx+pyvy+pzvz)+ << 975   // => (px^2+py^2+pz^2) +2s(pxvx+pyvy+pzvz)+s^2(vx^2+vy^2+vz^2)=R^2
753   // =>      rad2        +2sd(pDotV3d)       + << 976   // =>      rad2        +2s(pDotV3d)       +s^2                =R^2
754   //                                              977   //
755   // => sd=-pDotV3d+-std::sqrt(pDotV3d^2-(rad2 << 978   // => s=-pDotV3d+-std::sqrt(pDotV3d^2-(rad2-R^2))
756                                                   979 
757   c = rad2 - fRmax*fRmax ;                        980   c = rad2 - fRmax*fRmax ;
                                                   >> 981   const G4double  flexRadMaxTolerance = // kRadTolerance;
                                                   >> 982     std::max(kRadTolerance, fEpsilon * fRmax);
758                                                   983 
759   if (c > fRmaxTolerance*fRmax)                << 984   //  if (c > kRadTolerance*fRmax)
                                                   >> 985   if (c > flexRadMaxTolerance*fRmax)
760   {                                               986   {
761     // If outside tolerant boundary of outer G << 987     // If outside toleranct boundary of outer G4Sphere
762     // [should be std::sqrt(rad2)-fRmax > half << 988     // [should be std::sqrt(rad2)-fRmax > kRadTolerance*0.5]
763                                                   989 
764     d2 = pDotV3d*pDotV3d - c ;                    990     d2 = pDotV3d*pDotV3d - c ;
765                                                   991 
766     if ( d2 >= 0 )                                992     if ( d2 >= 0 )
767     {                                             993     {
768       sd = -pDotV3d - std::sqrt(d2) ;          << 994       s = -pDotV3d - std::sqrt(d2) ;
769                                                   995 
770       if (sd >= 0 )                            << 996       if (s >= 0 )
771       {                                           997       {
772         if ( sd>dRmax ) // Avoid rounding erro << 998         xi   = p.x() + s*v.x() ;
773         {               // 64 bits systems. Sp << 999         yi   = p.y() + s*v.y() ;
774           G4double fTerm = sd-std::fmod(sd,dRm << 
775           sd = fTerm + DistanceToIn(p+fTerm*v, << 
776         }                                      << 
777         xi   = p.x() + sd*v.x() ;              << 
778         yi   = p.y() + sd*v.y() ;              << 
779         rhoi = std::sqrt(xi*xi + yi*yi) ;         1000         rhoi = std::sqrt(xi*xi + yi*yi) ;
780                                                   1001 
781         if (!fFullPhiSphere && (rhoi != 0.0))  << 1002         if (segPhi && rhoi)    // Check phi intersection
782         {                                         1003         {
783           cosPsi = (xi*cosCPhi + yi*sinCPhi)/r    1004           cosPsi = (xi*cosCPhi + yi*sinCPhi)/rhoi ;
784                                                   1005 
785           if (cosPsi >= cosHDPhiOT)               1006           if (cosPsi >= cosHDPhiOT)
786           {                                       1007           {
787             if (!fFullThetaSphere)   // Check  << 1008             if (segTheta)   // Check theta intersection
788             {                                     1009             {
789               zi = p.z() + sd*v.z() ;          << 1010               zi = p.z() + s*v.z() ;
790                                                   1011 
791               // rhoi & zi can never both be 0    1012               // rhoi & zi can never both be 0
792               // (=>intersect at origin =>fRma    1013               // (=>intersect at origin =>fRmax=0)
793               //                                  1014               //
794               iTheta = std::atan2(rhoi,zi) ;      1015               iTheta = std::atan2(rhoi,zi) ;
795               if ( (iTheta >= tolSTheta) && (i    1016               if ( (iTheta >= tolSTheta) && (iTheta <= tolETheta) )
796               {                                   1017               {
797                 return snxt = sd ;             << 1018                 return snxt = s ;
798               }                                   1019               }
799             }                                     1020             }
800             else                                  1021             else
801             {                                     1022             {
802               return snxt=sd;                  << 1023               return snxt=s;
803             }                                     1024             }
804           }                                       1025           }
805         }                                         1026         }
806         else                                      1027         else
807         {                                         1028         {
808           if (!fFullThetaSphere)    // Check t << 1029           if (segTheta)    // Check theta intersection
809           {                                       1030           {
810             zi = p.z() + sd*v.z() ;            << 1031             zi = p.z() + s*v.z() ;
811                                                   1032 
812             // rhoi & zi can never both be 0      1033             // rhoi & zi can never both be 0
813             // (=>intersect at origin => fRmax    1034             // (=>intersect at origin => fRmax=0 !)
814             //                                    1035             //
815             iTheta = std::atan2(rhoi,zi) ;        1036             iTheta = std::atan2(rhoi,zi) ;
816             if ( (iTheta >= tolSTheta) && (iTh    1037             if ( (iTheta >= tolSTheta) && (iTheta <= tolETheta) )
817             {                                     1038             {
818               return snxt=sd;                  << 1039               return snxt=s;
819             }                                     1040             }
820           }                                       1041           }
821           else                                    1042           else
822           {                                       1043           {
823             return snxt = sd;                  << 1044             return snxt = s ;
824           }                                       1045           }
825         }                                      << 1046         }          
826       }                                           1047       }
827     }                                             1048     }
828     else    // No intersection with G4Sphere      1049     else    // No intersection with G4Sphere
829     {                                             1050     {
830       return snxt=kInfinity;                      1051       return snxt=kInfinity;
831     }                                             1052     }
832   }                                               1053   }
833   else                                            1054   else
834   {                                               1055   {
835     // Inside outer radius                        1056     // Inside outer radius
836     // check not inside, and heading through G    1057     // check not inside, and heading through G4Sphere (-> 0 to in)
837                                                   1058 
838     d2 = pDotV3d*pDotV3d - c ;                    1059     d2 = pDotV3d*pDotV3d - c ;
839                                                   1060 
840     if ( (rad2 > tolIRMax2)                    << 1061     // if (rad2 > tolIRMin2 && pDotV3d < 0 )
841       && ( (d2 >= fRmaxTolerance*fRmax) && (pD << 1062 
                                                   >> 1063     if (rad2 > tolIRMax2 && ( d2 >= flexRadMaxTolerance*fRmax && pDotV3d < 0 ) )
842     {                                             1064     {
843       if (!fFullPhiSphere)                     << 1065       if (segPhi)
844       {                                           1066       {
845         // Use inner phi tolerant boundary ->     1067         // Use inner phi tolerant boundary -> if on tolerant
846         // phi boundaries, phi intersect code     1068         // phi boundaries, phi intersect code handles leaving/entering checks
847                                                   1069 
848         cosPsi = (p.x()*cosCPhi + p.y()*sinCPh    1070         cosPsi = (p.x()*cosCPhi + p.y()*sinCPhi)/std::sqrt(rho2) ;
849                                                   1071 
850         if (cosPsi>=cosHDPhiIT)                   1072         if (cosPsi>=cosHDPhiIT)
851         {                                      << 1073         { 
852           // inside radii, delta r -ve, inside    1074           // inside radii, delta r -ve, inside phi
853                                                   1075 
854           if ( !fFullThetaSphere )             << 1076           if (segTheta)
855           {                                       1077           {
856             if ( (pTheta >= tolSTheta + kAngTo    1078             if ( (pTheta >= tolSTheta + kAngTolerance)
857               && (pTheta <= tolETheta - kAngTo    1079               && (pTheta <= tolETheta - kAngTolerance) )
858             {                                     1080             {
859               return snxt=0;                      1081               return snxt=0;
860             }                                     1082             }
861           }                                       1083           }
862           else    // strictly inside Theta in     1084           else    // strictly inside Theta in both cases
863           {                                       1085           {
864             return snxt=0;                        1086             return snxt=0;
865           }                                       1087           }
866         }                                         1088         }
867       }                                           1089       }
868       else                                        1090       else
869       {                                           1091       {
870         if ( !fFullThetaSphere )               << 1092         if ( segTheta )
871         {                                         1093         {
872           if ( (pTheta >= tolSTheta + kAngTole    1094           if ( (pTheta >= tolSTheta + kAngTolerance)
873             && (pTheta <= tolETheta - kAngTole    1095             && (pTheta <= tolETheta - kAngTolerance) )
874           {                                       1096           {
875             return snxt=0;                        1097             return snxt=0;
876           }                                       1098           }
877         }                                         1099         }
878         else   // strictly inside Theta in bot    1100         else   // strictly inside Theta in both cases
879         {                                         1101         {
880           return snxt=0;                          1102           return snxt=0;
881         }                                         1103         }
882       }                                           1104       }
883     }                                             1105     }
884   }                                               1106   }
885                                                   1107 
886   // Inner spherical shell intersection           1108   // Inner spherical shell intersection
887   // - Always farthest root, because would hav    1109   // - Always farthest root, because would have passed through outer
888   //   surface first.                             1110   //   surface first.
889   // - Tolerant check if travelling through so << 1111   // - Tolerant check for if travelling through solid
890                                                   1112 
891   if (fRmin != 0.0)                            << 1113   if (fRmin)
892   {                                               1114   {
893     c  = rad2 - fRmin*fRmin ;                     1115     c  = rad2 - fRmin*fRmin ;
894     d2 = pDotV3d*pDotV3d - c ;                    1116     d2 = pDotV3d*pDotV3d - c ;
895                                                   1117 
896     // Within tolerance inner radius of inner     1118     // Within tolerance inner radius of inner G4Sphere
897     // Check for immediate entry/already insid    1119     // Check for immediate entry/already inside and travelling outwards
898                                                   1120 
899     if ( (c > -halfRminTolerance) && (rad2 < t << 1121     // if (c >- kRadTolerance*0.5 && pDotV3d >= 0 && rad2 < tolIRMin2 )
900       && ( (d2 < fRmin*kCarTolerance) || (pDot << 1122 
                                                   >> 1123     if ( c > -kRadTolerance*0.5 && rad2 < tolIRMin2 && 
                                                   >> 1124          ( d2 < fRmin*kCarTolerance || pDotV3d >= 0 ) )
901     {                                             1125     {
902       if ( !fFullPhiSphere )                   << 1126       if (segPhi)
903       {                                           1127       {
904         // Use inner phi tolerant boundary ->     1128         // Use inner phi tolerant boundary -> if on tolerant
905         // phi boundaries, phi intersect code     1129         // phi boundaries, phi intersect code handles leaving/entering checks
906                                                   1130 
907         cosPsi = (p.x()*cosCPhi+p.y()*sinCPhi)    1131         cosPsi = (p.x()*cosCPhi+p.y()*sinCPhi)/std::sqrt(rho2) ;
908         if (cosPsi >= cosHDPhiIT)                 1132         if (cosPsi >= cosHDPhiIT)
909         {                                      << 1133         { 
910           // inside radii, delta r -ve, inside    1134           // inside radii, delta r -ve, inside phi
911           //                                      1135           //
912           if ( !fFullThetaSphere )             << 1136           if (segTheta)
913           {                                       1137           {
914             if ( (pTheta >= tolSTheta + kAngTo    1138             if ( (pTheta >= tolSTheta + kAngTolerance)
915               && (pTheta <= tolETheta - kAngTo    1139               && (pTheta <= tolETheta - kAngTolerance) )
916             {                                     1140             {
917               return snxt=0;                      1141               return snxt=0;
918             }                                     1142             }
919           }                                       1143           }
920           else                                    1144           else
921           {                                       1145           {
922             return snxt = 0 ;                     1146             return snxt = 0 ;
923           }                                       1147           }
924         }                                         1148         }
925       }                                           1149       }
926       else                                        1150       else
927       {                                           1151       {
928         if ( !fFullThetaSphere )               << 1152         if (segTheta)
929         {                                         1153         {
930           if ( (pTheta >= tolSTheta + kAngTole    1154           if ( (pTheta >= tolSTheta + kAngTolerance)
931             && (pTheta <= tolETheta - kAngTole    1155             && (pTheta <= tolETheta - kAngTolerance) )
932           {                                       1156           {
933             return snxt = 0 ;                     1157             return snxt = 0 ;
934           }                                       1158           }
935         }                                         1159         }
936         else                                      1160         else
937         {                                         1161         {
938           return snxt=0;                          1162           return snxt=0;
939         }                                         1163         }
940       }                                           1164       }
941     }                                             1165     }
942     else   // Not special tolerant case           1166     else   // Not special tolerant case
943     {                                             1167     {
                                                   >> 1168       //  d2 = pDotV3d*pDotV3d - c ;
                                                   >> 1169 
944       if (d2 >= 0)                                1170       if (d2 >= 0)
945       {                                           1171       {
946         sd = -pDotV3d + std::sqrt(d2) ;        << 1172         s = -pDotV3d + std::sqrt(d2) ;
947         if ( sd >= halfRminTolerance )  // It  << 1173         if ( s >= kRadTolerance*0.5 )  // It was >= 0 ??
948         {                                         1174         {
949           xi   = p.x() + sd*v.x() ;            << 1175           xi   = p.x() + s*v.x() ;
950           yi   = p.y() + sd*v.y() ;            << 1176           yi   = p.y() + s*v.y() ;
951           rhoi = std::sqrt(xi*xi+yi*yi) ;         1177           rhoi = std::sqrt(xi*xi+yi*yi) ;
952                                                   1178 
953           if ( !fFullPhiSphere && (rhoi != 0.0 << 1179           if ( segPhi && rhoi )   // Check phi intersection
954           {                                       1180           {
955             cosPsi = (xi*cosCPhi + yi*sinCPhi)    1181             cosPsi = (xi*cosCPhi + yi*sinCPhi)/rhoi ;
956                                                   1182 
957             if (cosPsi >= cosHDPhiOT)             1183             if (cosPsi >= cosHDPhiOT)
958             {                                     1184             {
959               if ( !fFullThetaSphere )  // Che << 1185               if (segTheta)  // Check theta intersection
960               {                                   1186               {
961                 zi = p.z() + sd*v.z() ;        << 1187                 zi = p.z() + s*v.z() ;
962                                                   1188 
963                 // rhoi & zi can never both be    1189                 // rhoi & zi can never both be 0
964                 // (=>intersect at origin =>fR    1190                 // (=>intersect at origin =>fRmax=0)
965                 //                                1191                 //
966                 iTheta = std::atan2(rhoi,zi) ;    1192                 iTheta = std::atan2(rhoi,zi) ;
967                 if ( (iTheta >= tolSTheta) &&     1193                 if ( (iTheta >= tolSTheta) && (iTheta<=tolETheta) )
968                 {                                 1194                 {
969                   snxt = sd;                   << 1195                   snxt = s ;
970                 }                                 1196                 }
971               }                                   1197               }
972               else                                1198               else
973               {                                   1199               {
974                 snxt=sd;                       << 1200                 snxt=s;
975               }                                   1201               }
976             }                                     1202             }
977           }                                       1203           }
978           else                                    1204           else
979           {                                       1205           {
980             if ( !fFullThetaSphere )   // Chec << 1206             if (segTheta)   // Check theta intersection
981             {                                     1207             {
982               zi = p.z() + sd*v.z() ;          << 1208               zi = p.z() + s*v.z() ;
983                                                   1209 
984               // rhoi & zi can never both be 0    1210               // rhoi & zi can never both be 0
985               // (=>intersect at origin => fRm    1211               // (=>intersect at origin => fRmax=0 !)
986               //                                  1212               //
987               iTheta = std::atan2(rhoi,zi) ;      1213               iTheta = std::atan2(rhoi,zi) ;
988               if ( (iTheta >= tolSTheta) && (i    1214               if ( (iTheta >= tolSTheta) && (iTheta <= tolETheta) )
989               {                                   1215               {
990                 snxt = sd;                     << 1216                 snxt = s ;
991               }                                   1217               }
992             }                                     1218             }
993             else                                  1219             else
994             {                                     1220             {
995               snxt = sd;                       << 1221               snxt=s;
996             }                                     1222             }
997           }                                       1223           }
998         }                                         1224         }
999       }                                           1225       }
1000     }                                            1226     }
1001   }                                              1227   }
1002                                                  1228 
1003   // Phi segment intersection                    1229   // Phi segment intersection
1004   //                                             1230   //
1005   // o Tolerant of points inside phi planes b    1231   // o Tolerant of points inside phi planes by up to kCarTolerance*0.5
1006   //                                             1232   //
1007   // o NOTE: Large duplication of code betwee    1233   // o NOTE: Large duplication of code between sphi & ephi checks
1008   //         -> only diffs: sphi -> ephi, Com    1234   //         -> only diffs: sphi -> ephi, Comp -> -Comp and half-plane
1009   //            intersection check <=0 -> >=0    1235   //            intersection check <=0 -> >=0
1010   //         -> Should use some form of loop     1236   //         -> Should use some form of loop Construct
1011   //                                             1237   //
1012   if ( !fFullPhiSphere )                      << 1238   if ( segPhi )
1013   {                                              1239   {
1014     // First phi surface ('S'tarting phi)     << 1240     // First phi surface (`S'tarting phi)
                                                   >> 1241 
                                                   >> 1242     sinSPhi = std::sin(fSPhi) ;
                                                   >> 1243     cosSPhi = std::cos(fSPhi) ;
                                                   >> 1244 
1015     // Comp = Component in outwards normal di    1245     // Comp = Component in outwards normal dirn
1016     //                                           1246     //
1017     Comp = v.x()*sinSPhi - v.y()*cosSPhi ;    << 1247     Comp    = v.x()*sinSPhi - v.y()*cosSPhi  ;
1018                                               << 1248                     
1019     if ( Comp < 0 )                              1249     if ( Comp < 0 )
1020     {                                            1250     {
1021       Dist = p.y()*cosSPhi - p.x()*sinSPhi ;     1251       Dist = p.y()*cosSPhi - p.x()*sinSPhi ;
1022                                                  1252 
1023       if (Dist < halfCarTolerance)            << 1253       if (Dist < kCarTolerance*0.5)
1024       {                                          1254       {
1025         sd = Dist/Comp ;                      << 1255         s = Dist/Comp ;
1026                                                  1256 
1027         if (sd < snxt)                        << 1257         if (s < snxt)
1028         {                                        1258         {
1029           if ( sd > 0 )                       << 1259           if ( s > 0 )
1030           {                                      1260           {
1031             xi    = p.x() + sd*v.x() ;        << 1261             xi    = p.x() + s*v.x() ;
1032             yi    = p.y() + sd*v.y() ;        << 1262             yi    = p.y() + s*v.y() ;
1033             zi    = p.z() + sd*v.z() ;        << 1263             zi    = p.z() + s*v.z() ;
1034             rhoi2 = xi*xi + yi*yi   ;            1264             rhoi2 = xi*xi + yi*yi   ;
1035             radi2 = rhoi2 + zi*zi   ;            1265             radi2 = rhoi2 + zi*zi   ;
1036           }                                      1266           }
1037           else                                   1267           else
1038           {                                      1268           {
1039             sd    = 0     ;                   << 1269             s     = 0     ;
1040             xi    = p.x() ;                      1270             xi    = p.x() ;
1041             yi    = p.y() ;                      1271             yi    = p.y() ;
1042             zi    = p.z() ;                      1272             zi    = p.z() ;
1043             rhoi2 = rho2  ;                      1273             rhoi2 = rho2  ;
1044             radi2 = rad2  ;                      1274             radi2 = rad2  ;
1045           }                                      1275           }
1046           if ( (radi2 <= tolORMax2)              1276           if ( (radi2 <= tolORMax2)
1047             && (radi2 >= tolORMin2)              1277             && (radi2 >= tolORMin2)
1048             && ((yi*cosCPhi-xi*sinCPhi) <= 0)    1278             && ((yi*cosCPhi-xi*sinCPhi) <= 0) )
1049           {                                      1279           {
1050             // Check theta intersection          1280             // Check theta intersection
1051             // rhoi & zi can never both be 0     1281             // rhoi & zi can never both be 0
1052             // (=>intersect at origin =>fRmax    1282             // (=>intersect at origin =>fRmax=0)
1053             //                                   1283             //
1054             if ( !fFullThetaSphere )          << 1284             if ( segTheta )
1055             {                                    1285             {
1056               iTheta = std::atan2(std::sqrt(r    1286               iTheta = std::atan2(std::sqrt(rhoi2),zi) ;
1057               if ( (iTheta >= tolSTheta) && (    1287               if ( (iTheta >= tolSTheta) && (iTheta <= tolETheta) )
1058               {                                  1288               {
1059                 // r and theta intersections     1289                 // r and theta intersections good
1060                 // - check intersecting with     1290                 // - check intersecting with correct half-plane
1061                                                  1291 
1062                 if ((yi*cosCPhi-xi*sinCPhi) <    1292                 if ((yi*cosCPhi-xi*sinCPhi) <= 0)
1063                 {                                1293                 {
1064                   snxt = sd;                  << 1294                   snxt = s ;
1065                 }                                1295                 }
1066               }                                  1296               }
1067             }                                    1297             }
1068             else                                 1298             else
1069             {                                    1299             {
1070               snxt = sd;                      << 1300               snxt = s ;
1071             }                                    1301             }
1072           }                                      1302           }
1073         }                                        1303         }
1074       }                                          1304       }
1075     }                                            1305     }
1076                                                  1306 
1077     // Second phi surface ('E'nding phi)      << 1307     // Second phi surface (`E'nding phi)
1078     // Component in outwards normal dirn      << 1308 
                                                   >> 1309     ePhi    = fSPhi + fDPhi ;
                                                   >> 1310     sinEPhi = std::sin(ePhi)     ;
                                                   >> 1311     cosEPhi = std::cos(ePhi)     ;
1079                                                  1312 
1080     Comp = -( v.x()*sinEPhi-v.y()*cosEPhi ) ; << 1313     // Compnent in outwards normal dirn
1081                                                  1314 
                                                   >> 1315     Comp    = -( v.x()*sinEPhi-v.y()*cosEPhi ) ;
                                                   >> 1316         
1082     if (Comp < 0)                                1317     if (Comp < 0)
1083     {                                            1318     {
1084       Dist = -(p.y()*cosEPhi-p.x()*sinEPhi) ;    1319       Dist = -(p.y()*cosEPhi-p.x()*sinEPhi) ;
1085       if ( Dist < halfCarTolerance )          << 1320       if ( Dist < kCarTolerance*0.5 )
1086       {                                          1321       {
1087         sd = Dist/Comp ;                      << 1322         s = Dist/Comp ;
1088                                                  1323 
1089         if ( sd < snxt )                      << 1324         if ( s < snxt )
1090         {                                        1325         {
1091           if (sd > 0)                         << 1326           if (s > 0)
1092           {                                      1327           {
1093             xi    = p.x() + sd*v.x() ;        << 1328             xi    = p.x() + s*v.x() ;
1094             yi    = p.y() + sd*v.y() ;        << 1329             yi    = p.y() + s*v.y() ;
1095             zi    = p.z() + sd*v.z() ;        << 1330             zi    = p.z() + s*v.z() ;
1096             rhoi2 = xi*xi + yi*yi   ;            1331             rhoi2 = xi*xi + yi*yi   ;
1097             radi2 = rhoi2 + zi*zi   ;            1332             radi2 = rhoi2 + zi*zi   ;
1098           }                                      1333           }
1099           else                                   1334           else
1100           {                                      1335           {
1101             sd    = 0     ;                   << 1336             s     = 0     ;
1102             xi    = p.x() ;                      1337             xi    = p.x() ;
1103             yi    = p.y() ;                      1338             yi    = p.y() ;
1104             zi    = p.z() ;                      1339             zi    = p.z() ;
1105             rhoi2 = rho2  ;                      1340             rhoi2 = rho2  ;
1106             radi2 = rad2  ;                      1341             radi2 = rad2  ;
1107           }                                   << 1342           } if ( (radi2 <= tolORMax2)
1108           if ( (radi2 <= tolORMax2)           << 
1109             && (radi2 >= tolORMin2)              1343             && (radi2 >= tolORMin2)
1110             && ((yi*cosCPhi-xi*sinCPhi) >= 0)    1344             && ((yi*cosCPhi-xi*sinCPhi) >= 0) )
1111           {                                      1345           {
1112             // Check theta intersection          1346             // Check theta intersection
1113             // rhoi & zi can never both be 0     1347             // rhoi & zi can never both be 0
1114             // (=>intersect at origin =>fRmax    1348             // (=>intersect at origin =>fRmax=0)
1115             //                                   1349             //
1116             if ( !fFullThetaSphere )          << 1350             if ( segTheta )
1117             {                                    1351             {
1118               iTheta = std::atan2(std::sqrt(r    1352               iTheta = std::atan2(std::sqrt(rhoi2),zi) ;
1119               if ( (iTheta >= tolSTheta) && (    1353               if ( (iTheta >= tolSTheta) && (iTheta <= tolETheta) )
1120               {                                  1354               {
1121                 // r and theta intersections     1355                 // r and theta intersections good
1122                 // - check intersecting with     1356                 // - check intersecting with correct half-plane
1123                                                  1357 
1124                 if ((yi*cosCPhi-xi*sinCPhi) >    1358                 if ((yi*cosCPhi-xi*sinCPhi) >= 0)
1125                 {                                1359                 {
1126                   snxt = sd;                  << 1360                   snxt = s ;
1127                 }                                1361                 }
1128               }                                  1362               }
1129             }                                    1363             }
1130             else                                 1364             else
1131             {                                    1365             {
1132               snxt = sd;                      << 1366               snxt = s ;
1133             }                                    1367             }
1134           }                                      1368           }
1135         }                                        1369         }
1136       }                                          1370       }
1137     }                                            1371     }
1138   }                                              1372   }
1139                                                  1373 
1140   // Theta segment intersection                  1374   // Theta segment intersection
1141                                                  1375 
1142   if ( !fFullThetaSphere )                    << 1376   if ( segTheta )
1143   {                                              1377   {
1144                                                  1378 
1145     // Intersection with theta surfaces          1379     // Intersection with theta surfaces
1146     // Known failure cases:                      1380     // Known failure cases:
1147     // o  Inside tolerance of stheta surface,    1381     // o  Inside tolerance of stheta surface, skim
1148     //    ~parallel to cone and Hit & enter e    1382     //    ~parallel to cone and Hit & enter etheta surface [& visa versa]
1149     //                                           1383     //
1150     //    To solve: Check 2nd root of etheta     1384     //    To solve: Check 2nd root of etheta surface in addition to stheta
1151     //                                           1385     //
1152     // o  start/end theta is exactly pi/2     << 1386     // o  start/end theta is exactly pi/2 
1153     // Intersections with cones                  1387     // Intersections with cones
1154     //                                           1388     //
1155     // Cone equation: x^2+y^2=z^2tan^2(t)        1389     // Cone equation: x^2+y^2=z^2tan^2(t)
1156     //                                           1390     //
1157     // => (px+svx)^2+(py+svy)^2=(pz+svz)^2tan    1391     // => (px+svx)^2+(py+svy)^2=(pz+svz)^2tan^2(t)
1158     //                                           1392     //
1159     // => (px^2+py^2-pz^2tan^2(t))+2sd(pxvx+p << 1393     // => (px^2+py^2-pz^2tan^2(t))+2s(pxvx+pyvy-pzvztan^2(t))
1160     //       + sd^2(vx^2+vy^2-vz^2tan^2(t)) = << 1394     //       + s^2(vx^2+vy^2-vz^2tan^2(t)) = 0
1161     //                                           1395     //
1162     // => sd^2(1-vz^2(1+tan^2(t))+2sd(pdotv2d << 1396     // => s^2(1-vz^2(1+tan^2(t))+2s(pdotv2d-pzvztan^2(t))+(rho2-pz^2tan^2(t))=0
1163     //       + (rho2-pz^2tan^2(t)) = 0        << 
1164                                                  1397 
1165     if (fSTheta != 0.0)                       << 1398     tanSTheta  = std::tan(fSTheta)         ;
                                                   >> 1399     tanSTheta2 = tanSTheta*tanSTheta  ;
                                                   >> 1400     tanETheta  = std::tan(fSTheta+fDTheta) ;
                                                   >> 1401     tanETheta2 = tanETheta*tanETheta  ;
                                                   >> 1402       
                                                   >> 1403     if (fSTheta)
1166     {                                            1404     {
1167       dist2STheta = rho2 - p.z()*p.z()*tanSTh    1405       dist2STheta = rho2 - p.z()*p.z()*tanSTheta2 ;
1168     }                                            1406     }
1169     else                                         1407     else
1170     {                                            1408     {
1171       dist2STheta = kInfinity ;                  1409       dist2STheta = kInfinity ;
1172     }                                            1410     }
1173     if ( eTheta < pi )                        << 1411     if ( fSTheta + fDTheta < pi )
1174     {                                            1412     {
1175       dist2ETheta=rho2-p.z()*p.z()*tanETheta2    1413       dist2ETheta=rho2-p.z()*p.z()*tanETheta2;
1176     }                                            1414     }
1177     else                                      << 1415       else
1178     {                                            1416     {
1179       dist2ETheta=kInfinity;                     1417       dist2ETheta=kInfinity;
1180     }                                         << 1418     }      
1181     if ( pTheta < tolSTheta )                 << 1419     if ( pTheta < tolSTheta) // dist2STheta<-kRadTolerance*0.5 && dist2ETheta>0)
1182     {                                            1420     {
1183       // Inside (theta<stheta-tol) stheta con << 1421       // Inside (theta<stheta-tol) s theta cone
1184       // First root of stheta cone, second if    1422       // First root of stheta cone, second if first root -ve
1185                                                  1423 
1186       t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) ;    1424       t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) ;
1187       t2 = pDotV2d - p.z()*v.z()*tanSTheta2 ;    1425       t2 = pDotV2d - p.z()*v.z()*tanSTheta2 ;
1188       if (t1 != 0.0)                          << 1426         
                                                   >> 1427       b  = t2/t1 ;
                                                   >> 1428       c  = dist2STheta/t1 ;
                                                   >> 1429       d2 = b*b - c ;
                                                   >> 1430 
                                                   >> 1431       if ( d2 >= 0 )
1189       {                                          1432       {
1190         b  = t2/t1 ;                          << 1433         d = std::sqrt(d2) ;
1191         c  = dist2STheta/t1 ;                 << 1434         s = -b - d ;    // First root
1192         d2 = b*b - c ;                        << 1435         zi    = p.z() + s*v.z();
1193                                                  1436 
1194         if ( d2 >= 0 )                        << 1437         if ( s < 0 || zi*(fSTheta - halfpi) > 0 )
1195         {                                        1438         {
1196           d  = std::sqrt(d2) ;                << 1439           s = -b+d;    // Second root
1197           sd = -b - d ;    // First root      << 1440         }
1198           zi = p.z() + sd*v.z();              << 1441         if (s >= 0 && s < snxt)
1199                                               << 1442         {
1200           if ( (sd < 0) || (zi*(fSTheta - hal << 1443           xi    = p.x() + s*v.x();
1201           {                                   << 1444           yi    = p.y() + s*v.y();
1202             sd = -b+d;    // Second root      << 1445           zi    = p.z() + s*v.z();
1203           }                                   << 1446           rhoi2 = xi*xi + yi*yi;
1204           if ((sd >= 0) && (sd < snxt))       << 1447           radi2 = rhoi2 + zi*zi;
                                                   >> 1448           if ( (radi2 <= tolORMax2)
                                                   >> 1449             && (radi2 >= tolORMin2)
                                                   >> 1450             && (zi*(fSTheta - halfpi) <= 0) )
1205           {                                      1451           {
1206             xi    = p.x() + sd*v.x();         << 1452             if ( segPhi && rhoi2 )  // Check phi intersection
1207             yi    = p.y() + sd*v.y();         << 
1208             zi    = p.z() + sd*v.z();         << 
1209             rhoi2 = xi*xi + yi*yi;            << 
1210             radi2 = rhoi2 + zi*zi;            << 
1211             if ( (radi2 <= tolORMax2)         << 
1212               && (radi2 >= tolORMin2)         << 
1213               && (zi*(fSTheta - halfpi) <= 0) << 
1214             {                                    1453             {
1215               if ( !fFullPhiSphere && (rhoi2  << 1454               cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ;
                                                   >> 1455               if (cosPsi >= cosHDPhiOT)
1216               {                                  1456               {
1217                 cosPsi = (xi*cosCPhi + yi*sin << 1457                 snxt = s ;
1218                 if (cosPsi >= cosHDPhiOT)     << 
1219                 {                             << 
1220                   snxt = sd;                  << 
1221                 }                             << 
1222               }                               << 
1223               else                            << 
1224               {                               << 
1225                 snxt = sd;                    << 
1226               }                                  1458               }
1227             }                                    1459             }
                                                   >> 1460             else
                                                   >> 1461             {
                                                   >> 1462               snxt = s ;
                                                   >> 1463             }
1228           }                                      1464           }
1229         }                                        1465         }
1230       }                                          1466       }
1231                                                  1467 
1232       // Possible intersection with ETheta co << 1468       // Possible intersection with ETheta cone. 
1233       // Second >= 0 root should be considere    1469       // Second >= 0 root should be considered
1234                                               << 1470         
1235       if ( eTheta < pi )                      << 1471       if ( fSTheta + fDTheta < pi )
1236       {                                          1472       {
1237         t1 = 1 - v.z()*v.z()*(1 + tanETheta2)    1473         t1 = 1 - v.z()*v.z()*(1 + tanETheta2) ;
1238         t2 = pDotV2d - p.z()*v.z()*tanETheta2    1474         t2 = pDotV2d - p.z()*v.z()*tanETheta2 ;
1239         if (t1 != 0.0)                        << 1475         
                                                   >> 1476         b  = t2/t1 ;
                                                   >> 1477         c  = dist2ETheta/t1 ;
                                                   >> 1478         d2 = b*b - c ;
                                                   >> 1479 
                                                   >> 1480         if (d2 >= 0)
1240         {                                        1481         {
1241           b  = t2/t1 ;                        << 1482           d = std::sqrt(d2) ;
1242           c  = dist2ETheta/t1 ;               << 1483           s = -b + d ;    // Second root
1243           d2 = b*b - c ;                      << 
1244                                                  1484 
1245           if (d2 >= 0)                        << 1485           if (s >= 0 && s < snxt)
1246           {                                      1486           {
1247             d  = std::sqrt(d2) ;              << 1487             xi    = p.x() + s*v.x() ;
1248             sd = -b + d ;    // Second root   << 1488             yi    = p.y() + s*v.y() ;
                                                   >> 1489             zi    = p.z() + s*v.z() ;
                                                   >> 1490             rhoi2 = xi*xi + yi*yi   ;
                                                   >> 1491             radi2 = rhoi2 + zi*zi   ;
1249                                                  1492 
1250             if ( (sd >= 0) && (sd < snxt) )   << 1493             if ( (radi2 <= tolORMax2)
                                                   >> 1494               && (radi2 >= tolORMin2)
                                                   >> 1495               && (zi*(fSTheta + fDTheta - halfpi) <= 0) )
1251             {                                    1496             {
1252               xi    = p.x() + sd*v.x() ;      << 1497               if (segPhi && rhoi2)   // Check phi intersection
1253               yi    = p.y() + sd*v.y() ;      << 
1254               zi    = p.z() + sd*v.z() ;      << 
1255               rhoi2 = xi*xi + yi*yi   ;       << 
1256               radi2 = rhoi2 + zi*zi   ;       << 
1257                                               << 
1258               if ( (radi2 <= tolORMax2)       << 
1259                 && (radi2 >= tolORMin2)       << 
1260                 && (zi*(eTheta - halfpi) <= 0 << 
1261               {                                  1498               {
1262                 if (!fFullPhiSphere && (rhoi2 << 1499                 cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ;
1263                 {                             << 1500                 if (cosPsi >= cosHDPhiOT)
1264                   cosPsi = (xi*cosCPhi + yi*s << 
1265                   if (cosPsi >= cosHDPhiOT)   << 
1266                   {                           << 
1267                     snxt = sd;                << 
1268                   }                           << 
1269                 }                             << 
1270                 else                          << 
1271                 {                                1501                 {
1272                   snxt = sd;                  << 1502                   snxt = s ;
1273                 }                                1503                 }
1274               }                                  1504               }
                                                   >> 1505               else
                                                   >> 1506               {
                                                   >> 1507                 snxt = s ;
                                                   >> 1508               }
1275             }                                    1509             }
1276           }                                      1510           }
1277         }                                        1511         }
1278       }                                          1512       }
1279     }                                         << 1513     }  
1280     else if ( pTheta > tolETheta )            << 1514     else if ( pTheta > tolETheta ) 
1281     {                                         << 1515     { 
1282       // dist2ETheta<-kRadTolerance*0.5 && di    1516       // dist2ETheta<-kRadTolerance*0.5 && dist2STheta>0)
1283       // Inside (theta > etheta+tol) e-theta     1517       // Inside (theta > etheta+tol) e-theta cone
1284       // First root of etheta cone, second if << 1518       // First root of etheta cone, second if first root `imaginary'
1285                                                  1519 
1286       t1 = 1 - v.z()*v.z()*(1 + tanETheta2) ;    1520       t1 = 1 - v.z()*v.z()*(1 + tanETheta2) ;
1287       t2 = pDotV2d - p.z()*v.z()*tanETheta2 ;    1521       t2 = pDotV2d - p.z()*v.z()*tanETheta2 ;
1288       if (t1 != 0.0)                          << 1522         
                                                   >> 1523       b  = t2/t1 ;
                                                   >> 1524       c  = dist2ETheta/t1 ;
                                                   >> 1525       d2 = b*b - c ;
                                                   >> 1526 
                                                   >> 1527       if (d2 >= 0)
1289       {                                          1528       {
1290         b  = t2/t1 ;                          << 1529         d = std::sqrt(d2) ;
1291         c  = dist2ETheta/t1 ;                 << 1530         s = -b - d ;    // First root
1292         d2 = b*b - c ;                        << 1531         zi    = p.z() + s*v.z();
1293                                                  1532 
1294         if (d2 >= 0)                          << 1533         if (s < 0 || zi*(fSTheta + fDTheta - halfpi) > 0)
                                                   >> 1534         {
                                                   >> 1535           s = -b + d ;           // second root
                                                   >> 1536         }
                                                   >> 1537         if (s >= 0 && s < snxt)
1295         {                                        1538         {
1296           d  = std::sqrt(d2) ;                << 1539           xi    = p.x() + s*v.x() ;
1297           sd = -b - d ;    // First root      << 1540           yi    = p.y() + s*v.y() ;
1298           zi = p.z() + sd*v.z();              << 1541           zi    = p.z() + s*v.z() ;
                                                   >> 1542           rhoi2 = xi*xi + yi*yi   ;
                                                   >> 1543           radi2 = rhoi2 + zi*zi   ;
1299                                                  1544 
1300           if ( (sd < 0) || (zi*(eTheta - half << 1545           if ( (radi2 <= tolORMax2)
1301           {                                   << 1546             && (radi2 >= tolORMin2) 
1302             sd = -b + d ;           // second << 1547             && (zi*(fSTheta + fDTheta - halfpi) <= 0) )
1303           }                                   << 
1304           if ( (sd >= 0) && (sd < snxt) )     << 
1305           {                                      1548           {
1306             xi    = p.x() + sd*v.x() ;        << 1549             if (segPhi && rhoi2)  // Check phi intersection
1307             yi    = p.y() + sd*v.y() ;        << 
1308             zi    = p.z() + sd*v.z() ;        << 
1309             rhoi2 = xi*xi + yi*yi   ;         << 
1310             radi2 = rhoi2 + zi*zi   ;         << 
1311                                               << 
1312             if ( (radi2 <= tolORMax2)         << 
1313               && (radi2 >= tolORMin2)         << 
1314               && (zi*(eTheta - halfpi) <= 0)  << 
1315             {                                    1550             {
1316               if (!fFullPhiSphere && (rhoi2 ! << 1551               cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ;
                                                   >> 1552               if (cosPsi >= cosHDPhiOT)
1317               {                                  1553               {
1318                 cosPsi = (xi*cosCPhi + yi*sin << 1554                 snxt = s ;
1319                 if (cosPsi >= cosHDPhiOT)     << 
1320                 {                             << 
1321                   snxt = sd;                  << 
1322                 }                             << 
1323               }                               << 
1324               else                            << 
1325               {                               << 
1326                 snxt = sd;                    << 
1327               }                                  1555               }
1328             }                                    1556             }
                                                   >> 1557             else
                                                   >> 1558             {
                                                   >> 1559               snxt = s ;
                                                   >> 1560             }
1329           }                                      1561           }
1330         }                                        1562         }
1331       }                                          1563       }
1332                                                  1564 
1333       // Possible intersection with STheta co << 1565       // Possible intersection with STheta cone. 
1334       // Second >= 0 root should be considere    1566       // Second >= 0 root should be considered
1335                                               << 1567         
1336       if ( fSTheta != 0.0 )                   << 1568       if ( fSTheta )
1337       {                                          1569       {
1338         t1 = 1 - v.z()*v.z()*(1 + tanSTheta2)    1570         t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) ;
1339         t2 = pDotV2d - p.z()*v.z()*tanSTheta2    1571         t2 = pDotV2d - p.z()*v.z()*tanSTheta2 ;
1340         if (t1 != 0.0)                        << 1572 
                                                   >> 1573         b  = t2/t1 ;
                                                   >> 1574         c  = dist2STheta/t1 ;
                                                   >> 1575         d2 = b*b - c ;
                                                   >> 1576 
                                                   >> 1577         if (d2 >= 0)
1341         {                                        1578         {
1342           b  = t2/t1 ;                        << 1579           d = std::sqrt(d2) ;
1343           c  = dist2STheta/t1 ;               << 1580           s = -b + d ;    // Second root
1344           d2 = b*b - c ;                      << 
1345                                                  1581 
1346           if (d2 >= 0)                        << 1582           if ( (s >= 0) && (s < snxt) )
1347           {                                      1583           {
1348             d  = std::sqrt(d2) ;              << 1584             xi    = p.x() + s*v.x() ;
1349             sd = -b + d ;    // Second root   << 1585             yi    = p.y() + s*v.y() ;
                                                   >> 1586             zi    = p.z() + s*v.z() ;
                                                   >> 1587             rhoi2 = xi*xi + yi*yi   ;
                                                   >> 1588             radi2 = rhoi2 + zi*zi   ;
1350                                                  1589 
1351             if ( (sd >= 0) && (sd < snxt) )   << 1590             if ( (radi2 <= tolORMax2)
                                                   >> 1591               && (radi2 >= tolORMin2)
                                                   >> 1592               && (zi*(fSTheta - halfpi) <= 0) )
1352             {                                    1593             {
1353               xi    = p.x() + sd*v.x() ;      << 1594               if (segPhi && rhoi2)   // Check phi intersection
1354               yi    = p.y() + sd*v.y() ;      << 
1355               zi    = p.z() + sd*v.z() ;      << 
1356               rhoi2 = xi*xi + yi*yi   ;       << 
1357               radi2 = rhoi2 + zi*zi   ;       << 
1358                                               << 
1359               if ( (radi2 <= tolORMax2)       << 
1360                 && (radi2 >= tolORMin2)       << 
1361                 && (zi*(fSTheta - halfpi) <=  << 
1362               {                                  1595               {
1363                 if (!fFullPhiSphere && (rhoi2 << 1596                 cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ;
1364                 {                             << 1597                 if (cosPsi >= cosHDPhiOT)
1365                   cosPsi = (xi*cosCPhi + yi*s << 
1366                   if (cosPsi >= cosHDPhiOT)   << 
1367                   {                           << 
1368                     snxt = sd;                << 
1369                   }                           << 
1370                 }                             << 
1371                 else                          << 
1372                 {                                1598                 {
1373                   snxt = sd;                  << 1599                   snxt = s ;
1374                 }                                1600                 }
1375               }                                  1601               }
                                                   >> 1602               else
                                                   >> 1603               {
                                                   >> 1604                 snxt = s ;
                                                   >> 1605               }
1376             }                                    1606             }
1377           }                                      1607           }
1378         }                                        1608         }
1379       }                                       << 1609       }  
1380     }                                         << 1610     }     
1381     else if ( (pTheta < tolSTheta + kAngToler << 1611     else if ( (pTheta <tolSTheta + kAngTolerance)
1382            && (fSTheta > halfAngTolerance) )  << 1612            && (fSTheta > kAngTolerance) )
1383     {                                            1613     {
1384       // In tolerance of stheta                  1614       // In tolerance of stheta
1385       // If entering through solid [r,phi] =>    1615       // If entering through solid [r,phi] => 0 to in
1386       // else try 2nd root                       1616       // else try 2nd root
1387                                                  1617 
1388       t2 = pDotV2d - p.z()*v.z()*tanSTheta2 ;    1618       t2 = pDotV2d - p.z()*v.z()*tanSTheta2 ;
1389       if ( (t2>=0 && tolIRMin2<rad2 && rad2<t << 1619       if ( (t2>=0 && tolIRMin2<rad2 && rad2<tolIRMax2 && fSTheta<pi*.5)
1390         || (t2<0  && tolIRMin2<rad2 && rad2<t << 1620         || (t2<0  && tolIRMin2<rad2 && rad2<tolIRMax2 && fSTheta>pi*.5)
1391         || (v.z()<0 && tolIRMin2<rad2 && rad2 << 1621         || (v.z()<0 && tolIRMin2<rad2 && rad2<tolIRMax2 && fSTheta==pi*.5) )
1392       {                                          1622       {
1393         if (!fFullPhiSphere && (rho2 != 0.0)) << 1623         if (segPhi && rho2)  // Check phi intersection
1394         {                                        1624         {
1395           cosPsi = (p.x()*cosCPhi + p.y()*sin    1625           cosPsi = (p.x()*cosCPhi + p.y()*sinCPhi)/std::sqrt(rho2) ;
1396           if (cosPsi >= cosHDPhiIT)              1626           if (cosPsi >= cosHDPhiIT)
1397           {                                      1627           {
1398             return 0 ;                           1628             return 0 ;
1399           }                                      1629           }
1400         }                                        1630         }
1401         else                                     1631         else
1402         {                                        1632         {
1403           return 0 ;                             1633           return 0 ;
1404         }                                        1634         }
1405       }                                          1635       }
1406                                                  1636 
1407       // Not entering immediately/travelling     1637       // Not entering immediately/travelling through
1408                                                  1638 
1409       t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) ;    1639       t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) ;
1410       if (t1 != 0.0)                          << 1640       b  = t2/t1 ;
1411       {                                       << 1641       c  = dist2STheta/t1 ;
1412         b  = t2/t1 ;                          << 1642       d2 = b*b - c ;
1413         c  = dist2STheta/t1 ;                 << 
1414         d2 = b*b - c ;                        << 
1415                                                  1643 
1416         if (d2 >= 0)                          << 1644       if (d2 >= 0)
1417         {                                     << 1645       {
1418           d  = std::sqrt(d2) ;                << 1646         d = std::sqrt(d2) ;
1419           sd = -b + d ;                       << 1647         s = -b + d ;
1420           if ( (sd >= halfCarTolerance) && (s << 1648         if ( (s >= kCarTolerance*0.5) && (s < snxt) && (fSTheta < pi*0.5) )
1421           {   // ^^^^^^^^^^^^^^^^^^^^^  shoul << 1649         {
1422             xi    = p.x() + sd*v.x() ;        << 1650           xi    = p.x() + s*v.x() ;
1423             yi    = p.y() + sd*v.y() ;        << 1651           yi    = p.y() + s*v.y() ;
1424             zi    = p.z() + sd*v.z() ;        << 1652           zi    = p.z() + s*v.z() ;
1425             rhoi2 = xi*xi + yi*yi   ;         << 1653           rhoi2 = xi*xi + yi*yi   ;
1426             radi2 = rhoi2 + zi*zi   ;         << 1654           radi2 = rhoi2 + zi*zi   ;
1427                                                  1655 
1428             if ( (radi2 <= tolORMax2)         << 1656           if ( (radi2 <= tolORMax2)
1429               && (radi2 >= tolORMin2)         << 1657             && (radi2 >= tolORMin2)
1430               && (zi*(fSTheta - halfpi) <= 0) << 1658             && (zi*(fSTheta - halfpi) <= 0) )
                                                   >> 1659           {
                                                   >> 1660             if ( segPhi && rhoi2 )    // Check phi intersection
1431             {                                    1661             {
1432               if ( !fFullPhiSphere && (rhoi2  << 1662               cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ;
1433               {                               << 1663               if ( cosPsi >= cosHDPhiOT )
1434                 cosPsi = (xi*cosCPhi + yi*sin << 
1435                 if ( cosPsi >= cosHDPhiOT )   << 
1436                 {                             << 
1437                   snxt = sd;                  << 
1438                 }                             << 
1439               }                               << 
1440               else                            << 
1441               {                                  1664               {
1442                 snxt = sd;                    << 1665                 snxt = s ;
1443               }                                  1666               }
1444             }                                    1667             }
                                                   >> 1668             else
                                                   >> 1669             {
                                                   >> 1670               snxt = s ;
                                                   >> 1671             }
1445           }                                      1672           }
1446         }                                        1673         }
1447       }                                          1674       }
1448     }                                         << 1675     }   
1449     else if ((pTheta > tolETheta-kAngToleranc << 1676     else if ( (pTheta > tolETheta - kAngTolerance)
                                                   >> 1677            && ((fSTheta + fDTheta) < pi-kAngTolerance) )   
1450     {                                            1678     {
1451                                                  1679 
1452       // In tolerance of etheta                  1680       // In tolerance of etheta
1453       // If entering through solid [r,phi] =>    1681       // If entering through solid [r,phi] => 0 to in
1454       // else try 2nd root                       1682       // else try 2nd root
1455                                                  1683 
1456       t2 = pDotV2d - p.z()*v.z()*tanETheta2 ;    1684       t2 = pDotV2d - p.z()*v.z()*tanETheta2 ;
1457                                                  1685 
1458       if (   ((t2<0) && (eTheta < halfpi)     << 1686       if (
1459           && (tolIRMin2 < rad2) && (rad2 < to << 1687     (t2<0    && (fSTheta+fDTheta) <pi*0.5 && tolIRMin2<rad2 && rad2<tolIRMax2)
1460         ||   ((t2>=0) && (eTheta > halfpi)    << 1688  || (t2>=0   && (fSTheta+fDTheta) >pi*0.5 && tolIRMin2<rad2 && rad2<tolIRMax2)
1461           && (tolIRMin2 < rad2) && (rad2 < to << 1689  || (v.z()>0 && (fSTheta+fDTheta)==pi*0.5 && tolIRMin2<rad2 && rad2<tolIRMax2)
1462         ||   ((v.z()>0) && (eTheta == halfpi) << 1690          )
1463           && (tolIRMin2 < rad2) && (rad2 < to << 
1464       {                                          1691       {
1465         if (!fFullPhiSphere && (rho2 != 0.0)) << 1692         if (segPhi && rho2)   // Check phi intersection
1466         {                                        1693         {
1467           cosPsi = (p.x()*cosCPhi + p.y()*sin    1694           cosPsi = (p.x()*cosCPhi + p.y()*sinCPhi)/std::sqrt(rho2) ;
1468           if (cosPsi >= cosHDPhiIT)              1695           if (cosPsi >= cosHDPhiIT)
1469           {                                      1696           {
1470             return 0 ;                           1697             return 0 ;
1471           }                                      1698           }
1472         }                                        1699         }
1473         else                                     1700         else
1474         {                                        1701         {
1475           return 0 ;                             1702           return 0 ;
1476         }                                        1703         }
1477       }                                          1704       }
1478                                                  1705 
1479       // Not entering immediately/travelling     1706       // Not entering immediately/travelling through
1480                                                  1707 
1481       t1 = 1 - v.z()*v.z()*(1 + tanETheta2) ;    1708       t1 = 1 - v.z()*v.z()*(1 + tanETheta2) ;
1482       if (t1 != 0.0)                          << 1709       b  = t2/t1 ;
1483       {                                       << 1710       c  = dist2ETheta/t1 ;
1484         b  = t2/t1 ;                          << 1711       d2 = b*b - c ;
1485         c  = dist2ETheta/t1 ;                 << 
1486         d2 = b*b - c ;                        << 
1487                                                  1712 
1488         if (d2 >= 0)                          << 1713       if (d2 >= 0)
1489         {                                     << 1714       {
1490           d  = std::sqrt(d2) ;                << 1715         d = std::sqrt(d2) ;
1491           sd = -b + d ;                       << 1716         s = -b + d ;
                                                   >> 1717         
                                                   >> 1718         if ( (s >= kCarTolerance*0.5)
                                                   >> 1719           && (s < snxt) && ((fSTheta + fDTheta) > pi*0.5) )
                                                   >> 1720         {
                                                   >> 1721           xi    = p.x() + s*v.x() ;
                                                   >> 1722           yi    = p.y() + s*v.y() ;
                                                   >> 1723           zi    = p.z() + s*v.z() ;
                                                   >> 1724           rhoi2 = xi*xi + yi*yi   ;
                                                   >> 1725           radi2 = rhoi2 + zi*zi   ;
1492                                                  1726 
1493           if ( (sd >= halfCarTolerance)       << 1727           if ( (radi2 <= tolORMax2)
1494             && (sd < snxt) && (eTheta > halfp << 1728             && (radi2 >= tolORMin2)
                                                   >> 1729             && (zi*(fSTheta + fDTheta - halfpi) <= 0) )
1495           {                                      1730           {
1496             xi    = p.x() + sd*v.x() ;        << 1731             if (segPhi && rhoi2)   // Check phi intersection
1497             yi    = p.y() + sd*v.y() ;        << 
1498             zi    = p.z() + sd*v.z() ;        << 
1499             rhoi2 = xi*xi + yi*yi   ;         << 
1500             radi2 = rhoi2 + zi*zi   ;         << 
1501                                               << 
1502             if ( (radi2 <= tolORMax2)         << 
1503               && (radi2 >= tolORMin2)         << 
1504               && (zi*(eTheta - halfpi) <= 0)  << 
1505             {                                    1732             {
1506               if (!fFullPhiSphere && (rhoi2 ! << 1733               cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ;
                                                   >> 1734               if (cosPsi>=cosHDPhiOT)
1507               {                                  1735               {
1508                 cosPsi = (xi*cosCPhi + yi*sin << 1736                 snxt = s ;
1509                 if (cosPsi >= cosHDPhiOT)     << 
1510                 {                             << 
1511                   snxt = sd;                  << 
1512                 }                             << 
1513               }                               << 
1514               else                            << 
1515               {                               << 
1516                 snxt = sd;                    << 
1517               }                                  1737               }
1518             }                                    1738             }
                                                   >> 1739             else
                                                   >> 1740             {
                                                   >> 1741               snxt = s ;
                                                   >> 1742             }
1519           }                                      1743           }
1520         }                                        1744         }
1521       }                                       << 1745       }    
1522     }                                         << 1746     }  
1523     else                                         1747     else
1524     {                                            1748     {
1525       // stheta+tol<theta<etheta-tol             1749       // stheta+tol<theta<etheta-tol
1526       // For BOTH stheta & etheta check 2nd r    1750       // For BOTH stheta & etheta check 2nd root for validity [r,phi]
1527                                                  1751 
1528       t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) ;    1752       t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) ;
1529       t2 = pDotV2d - p.z()*v.z()*tanSTheta2 ;    1753       t2 = pDotV2d - p.z()*v.z()*tanSTheta2 ;
1530       if (t1 != 0.0)                          << 1754 
                                                   >> 1755       b  = t2/t1;
                                                   >> 1756       c  = dist2STheta/t1 ;
                                                   >> 1757       d2 = b*b - c ;
                                                   >> 1758 
                                                   >> 1759       if (d2 >= 0)
1531       {                                          1760       {
1532         b  = t2/t1;                           << 1761         d = std::sqrt(d2) ;
1533         c  = dist2STheta/t1 ;                 << 1762         s = -b + d ;    // second root
1534         d2 = b*b - c ;                        << 
1535                                                  1763 
1536         if (d2 >= 0)                          << 1764         if (s >= 0 && s < snxt)
1537         {                                        1765         {
1538           d  = std::sqrt(d2) ;                << 1766           xi    = p.x() + s*v.x() ;
1539           sd = -b + d ;    // second root     << 1767           yi    = p.y() + s*v.y() ;
                                                   >> 1768           zi    = p.z() + s*v.z() ;
                                                   >> 1769           rhoi2 = xi*xi + yi*yi   ;
                                                   >> 1770           radi2 = rhoi2 + zi*zi   ;
1540                                                  1771 
1541           if ((sd >= 0) && (sd < snxt))       << 1772           if ( (radi2 <= tolORMax2)
                                                   >> 1773             && (radi2 >= tolORMin2)
                                                   >> 1774             && (zi*(fSTheta - halfpi) <= 0) )
1542           {                                      1775           {
1543             xi    = p.x() + sd*v.x() ;        << 1776             if (segPhi && rhoi2)   // Check phi intersection
1544             yi    = p.y() + sd*v.y() ;        << 
1545             zi    = p.z() + sd*v.z() ;        << 
1546             rhoi2 = xi*xi + yi*yi   ;         << 
1547             radi2 = rhoi2 + zi*zi   ;         << 
1548                                               << 
1549             if ( (radi2 <= tolORMax2)         << 
1550               && (radi2 >= tolORMin2)         << 
1551               && (zi*(fSTheta - halfpi) <= 0) << 
1552             {                                    1777             {
1553               if (!fFullPhiSphere && (rhoi2 ! << 1778               cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ;
                                                   >> 1779               if (cosPsi >= cosHDPhiOT)
1554               {                                  1780               {
1555                 cosPsi = (xi*cosCPhi + yi*sin << 1781                 snxt = s ;
1556                 if (cosPsi >= cosHDPhiOT)     << 
1557                 {                             << 
1558                   snxt = sd;                  << 
1559                 }                             << 
1560               }                               << 
1561               else                            << 
1562               {                               << 
1563                 snxt = sd;                    << 
1564               }                                  1782               }
1565             }                                    1783             }
                                                   >> 1784             else
                                                   >> 1785             {
                                                   >> 1786               snxt = s ;
                                                   >> 1787             }
1566           }                                      1788           }
1567         }                                        1789         }
1568       }                                       << 1790       }        
1569       t1 = 1 - v.z()*v.z()*(1 + tanETheta2) ;    1791       t1 = 1 - v.z()*v.z()*(1 + tanETheta2) ;
1570       t2 = pDotV2d - p.z()*v.z()*tanETheta2 ;    1792       t2 = pDotV2d - p.z()*v.z()*tanETheta2 ;
1571       if (t1 != 0.0)                          << 1793         
                                                   >> 1794       b  = t2/t1 ;
                                                   >> 1795       c  = dist2ETheta/t1 ;
                                                   >> 1796       d2 = b*b - c ;
                                                   >> 1797 
                                                   >> 1798       if (d2 >= 0)
1572       {                                          1799       {
1573         b  = t2/t1 ;                          << 1800         d = std::sqrt(d2) ;
1574         c  = dist2ETheta/t1 ;                 << 1801         s = -b + d;    // second root
1575         d2 = b*b - c ;                        << 
1576                                                  1802 
1577         if (d2 >= 0)                          << 1803         if (s >= 0 && s < snxt)
1578         {                                        1804         {
1579           d  = std::sqrt(d2) ;                << 1805           xi    = p.x() + s*v.x() ;
1580           sd = -b + d;    // second root      << 1806           yi    = p.y() + s*v.y() ;
                                                   >> 1807           zi    = p.z() + s*v.z() ;
                                                   >> 1808           rhoi2 = xi*xi + yi*yi   ;
                                                   >> 1809           radi2 = rhoi2 + zi*zi   ;
1581                                                  1810 
1582           if ((sd >= 0) && (sd < snxt))       << 1811           if ( (radi2 <= tolORMax2)
                                                   >> 1812             && (radi2 >= tolORMin2)
                                                   >> 1813             && (zi*(fSTheta + fDTheta - halfpi) <= 0) )
1583           {                                      1814           {
1584             xi    = p.x() + sd*v.x() ;        << 1815             if (segPhi && rhoi2)   // Check phi intersection
1585             yi    = p.y() + sd*v.y() ;        << 
1586             zi    = p.z() + sd*v.z() ;        << 
1587             rhoi2 = xi*xi + yi*yi   ;         << 
1588             radi2 = rhoi2 + zi*zi   ;         << 
1589                                               << 
1590             if ( (radi2 <= tolORMax2)         << 
1591               && (radi2 >= tolORMin2)         << 
1592               && (zi*(eTheta - halfpi) <= 0)  << 
1593             {                                    1816             {
1594               if (!fFullPhiSphere && (rhoi2 ! << 1817               cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ;
                                                   >> 1818               if ( cosPsi >= cosHDPhiOT )
1595               {                                  1819               {
1596                 cosPsi = (xi*cosCPhi + yi*sin << 1820                 snxt=s;
1597                 if ( cosPsi >= cosHDPhiOT )   << 
1598                 {                             << 
1599                   snxt = sd;                  << 
1600                 }                             << 
1601               }                               << 
1602               else                            << 
1603               {                               << 
1604                 snxt = sd;                    << 
1605               }                                  1821               }
1606             }                                    1822             }
                                                   >> 1823             else
                                                   >> 1824             {
                                                   >> 1825               snxt = s ;
                                                   >> 1826             }
1607           }                                      1827           }
1608         }                                        1828         }
1609       }                                          1829       }
1610     }                                         << 1830     }  
1611   }                                              1831   }
1612   return snxt;                                   1832   return snxt;
1613 }                                                1833 }
1614                                                  1834 
1615 /////////////////////////////////////////////    1835 //////////////////////////////////////////////////////////////////////
1616 //                                               1836 //
1617 // Calculate distance (<= actual) to closest     1837 // Calculate distance (<= actual) to closest surface of shape from outside
1618 // - Calculate distance to radial planes         1838 // - Calculate distance to radial planes
1619 // - Only to phi planes if outside phi extent    1839 // - Only to phi planes if outside phi extent
1620 // - Only to theta planes if outside theta ex    1840 // - Only to theta planes if outside theta extent
1621 // - Return 0 if point inside                    1841 // - Return 0 if point inside
1622                                                  1842 
1623 G4double G4Sphere::DistanceToIn( const G4Thre    1843 G4double G4Sphere::DistanceToIn( const G4ThreeVector& p ) const
1624 {                                                1844 {
1625   G4double safe=0.0,safeRMin,safeRMax,safePhi    1845   G4double safe=0.0,safeRMin,safeRMax,safePhi,safeTheta;
1626   G4double rho2,rds,rho;                      << 1846   G4double rho2,rad,rho;
1627   G4double cosPsi;                            << 1847   G4double phiC,cosPhiC,sinPhiC,cosPsi,ePhi;
1628   G4double pTheta,dTheta1,dTheta2;               1848   G4double pTheta,dTheta1,dTheta2;
1629   rho2=p.x()*p.x()+p.y()*p.y();                  1849   rho2=p.x()*p.x()+p.y()*p.y();
1630   rds=std::sqrt(rho2+p.z()*p.z());            << 1850   rad=std::sqrt(rho2+p.z()*p.z());
1631   rho=std::sqrt(rho2);                           1851   rho=std::sqrt(rho2);
1632                                                  1852 
1633   //                                             1853   //
1634   // Distance to r shells                        1854   // Distance to r shells
1635   //                                          << 1855   //    
1636   if (fRmin != 0.0)                           << 1856   if (fRmin)
1637   {                                              1857   {
1638     safeRMin=fRmin-rds;                       << 1858     safeRMin=fRmin-rad;
1639     safeRMax=rds-fRmax;                       << 1859     safeRMax=rad-fRmax;
1640     if (safeRMin>safeRMax)                       1860     if (safeRMin>safeRMax)
1641     {                                            1861     {
1642       safe=safeRMin;                             1862       safe=safeRMin;
1643     }                                            1863     }
1644     else                                         1864     else
1645     {                                            1865     {
1646       safe=safeRMax;                             1866       safe=safeRMax;
1647     }                                            1867     }
1648   }                                              1868   }
1649   else                                           1869   else
1650   {                                              1870   {
1651     safe=rds-fRmax;                           << 1871     safe=rad-fRmax;
1652   }                                              1872   }
1653                                                  1873 
1654   //                                             1874   //
1655   // Distance to phi extent                      1875   // Distance to phi extent
1656   //                                             1876   //
1657   if (!fFullPhiSphere && (rho != 0.0))        << 1877   if (fDPhi<twopi&&rho)
1658   {                                              1878   {
                                                   >> 1879     phiC=fSPhi+fDPhi*0.5;
                                                   >> 1880     cosPhiC=std::cos(phiC);
                                                   >> 1881     sinPhiC=std::sin(phiC);
                                                   >> 1882 
1659     // Psi=angle from central phi to point       1883     // Psi=angle from central phi to point
1660     //                                           1884     //
1661     cosPsi=(p.x()*cosCPhi+p.y()*sinCPhi)/rho; << 1885     cosPsi=(p.x()*cosPhiC+p.y()*sinPhiC)/rho;
1662     if (cosPsi<cosHDPhi)                      << 1886     if (cosPsi<std::cos(fDPhi*0.5))
1663     {                                            1887     {
1664       // Point lies outside phi range            1888       // Point lies outside phi range
1665       //                                         1889       //
1666       if ((p.y()*cosCPhi-p.x()*sinCPhi)<=0)   << 1890       if ((p.y()*cosPhiC-p.x()*sinPhiC)<=0)
1667       {                                          1891       {
1668         safePhi=std::fabs(p.x()*sinSPhi-p.y() << 1892         safePhi=std::fabs(p.x()*std::sin(fSPhi)-p.y()*std::cos(fSPhi));
1669       }                                          1893       }
1670       else                                       1894       else
1671       {                                          1895       {
1672         safePhi=std::fabs(p.x()*sinEPhi-p.y() << 1896         ePhi=fSPhi+fDPhi;
                                                   >> 1897         safePhi=std::fabs(p.x()*std::sin(ePhi)-p.y()*std::cos(ePhi));
1673       }                                          1898       }
1674       if (safePhi>safe)  { safe=safePhi; }    << 1899       if (safePhi>safe) safe=safePhi;
1675     }                                            1900     }
1676   }                                              1901   }
1677   //                                             1902   //
1678   // Distance to Theta extent                    1903   // Distance to Theta extent
1679   //                                          << 1904   //    
1680   if ((rds!=0.0) && (!fFullThetaSphere))      << 1905   if ((rad!=0.0) && (fDTheta<pi))
1681   {                                              1906   {
1682     pTheta=std::acos(p.z()/rds);              << 1907     pTheta=std::acos(p.z()/rad);
1683     if (pTheta<0)  { pTheta+=pi; }            << 1908     if (pTheta<0) pTheta+=pi;
1684     dTheta1=fSTheta-pTheta;                      1909     dTheta1=fSTheta-pTheta;
1685     dTheta2=pTheta-eTheta;                    << 1910     dTheta2=pTheta-(fSTheta+fDTheta);
1686     if (dTheta1>dTheta2)                         1911     if (dTheta1>dTheta2)
1687     {                                            1912     {
1688       if (dTheta1>=0)             // WHY ????    1913       if (dTheta1>=0)             // WHY ???????????
1689       {                                          1914       {
1690         safeTheta=rds*std::sin(dTheta1);      << 1915         safeTheta=rad*std::sin(dTheta1);
1691         if (safe<=safeTheta)                     1916         if (safe<=safeTheta)
1692         {                                        1917         {
1693           safe=safeTheta;                        1918           safe=safeTheta;
1694         }                                        1919         }
1695       }                                          1920       }
1696     }                                            1921     }
1697     else                                         1922     else
1698     {                                            1923     {
1699       if (dTheta2>=0)                            1924       if (dTheta2>=0)
1700       {                                          1925       {
1701         safeTheta=rds*std::sin(dTheta2);      << 1926         safeTheta=rad*std::sin(dTheta2);
1702         if (safe<=safeTheta)                     1927         if (safe<=safeTheta)
1703         {                                        1928         {
1704           safe=safeTheta;                        1929           safe=safeTheta;
1705         }                                        1930         }
1706       }                                          1931       }
1707     }                                            1932     }
1708   }                                              1933   }
1709                                                  1934 
1710   if (safe<0)  { safe=0; }                    << 1935   if (safe<0) safe=0;
1711   return safe;                                   1936   return safe;
1712 }                                                1937 }
1713                                                  1938 
1714 /////////////////////////////////////////////    1939 /////////////////////////////////////////////////////////////////////
1715 //                                               1940 //
1716 // Calculate distance to surface of shape fro << 1941 // Calculate distance to surface of shape from `inside', allowing for tolerance
1717 // - Only Calc rmax intersection if no valid     1942 // - Only Calc rmax intersection if no valid rmin intersection
1718                                                  1943 
1719 G4double G4Sphere::DistanceToOut( const G4Thr    1944 G4double G4Sphere::DistanceToOut( const G4ThreeVector& p,
1720                                   const G4Thr    1945                                   const G4ThreeVector& v,
1721                                   const G4boo    1946                                   const G4bool calcNorm,
1722                                         G4boo << 1947                                         G4bool *validNorm,
1723                                         G4Thr << 1948                                         G4ThreeVector *n   ) const
1724 {                                                1949 {
1725   G4double snxt = kInfinity;     // snxt is d    1950   G4double snxt = kInfinity;     // snxt is default return value
1726   G4double sphi= kInfinity,stheta= kInfinity;    1951   G4double sphi= kInfinity,stheta= kInfinity;
1727   ESide side=kNull,sidephi=kNull,sidetheta=kN << 1952   ESide side=kNull,sidephi=kNull,sidetheta=kNull;  
1728                                                  1953 
1729   const G4double halfRmaxTolerance = fRmaxTol << 
1730   const G4double halfRminTolerance = fRminTol << 
1731   const G4double Rmax_plus  = fRmax + halfRma << 
1732   const G4double Rmin_minus = (fRmin) != 0.0  << 
1733   G4double t1,t2;                                1954   G4double t1,t2;
1734   G4double b,c,d;                                1955   G4double b,c,d;
1735                                                  1956 
1736   // Variables for phi intersection:             1957   // Variables for phi intersection:
1737                                                  1958 
                                                   >> 1959   G4double sinSPhi,cosSPhi,ePhi,sinEPhi,cosEPhi;
                                                   >> 1960   G4double cPhi,sinCPhi,cosCPhi;
1738   G4double pDistS,compS,pDistE,compE,sphi2,vp    1961   G4double pDistS,compS,pDistE,compE,sphi2,vphi;
                                                   >> 1962     
                                                   >> 1963   G4double rho2,rad2,pDotV2d,pDotV3d,pTheta;
1739                                                  1964 
1740   G4double rho2,rad2,pDotV2d,pDotV3d;         << 1965   G4double tolSTheta=0.,tolETheta=0.;
1741                                               << 
1742   G4double xi,yi,zi;      // Intersection poi    1966   G4double xi,yi,zi;      // Intersection point
1743                                                  1967 
1744   // Theta precals                            << 1968   // G4double Comp; // Phi intersection
1745   //                                          << 1969 
1746   G4double rhoSecTheta;                       << 1970   G4bool segPhi;        // Phi flag and precalcs
                                                   >> 1971   G4double hDPhi,hDPhiOT,hDPhiIT; 
                                                   >> 1972   G4double cosHDPhiOT,cosHDPhiIT;
                                                   >> 1973 
                                                   >> 1974   G4bool segTheta;                             // Theta flag and precals
                                                   >> 1975   G4double tanSTheta=0.,tanETheta=0., rhoSecTheta;
                                                   >> 1976   G4double tanSTheta2=0.,tanETheta2=0.;
1747   G4double dist2STheta, dist2ETheta, distThet    1977   G4double dist2STheta, dist2ETheta, distTheta;
1748   G4double d2,sd;                             << 1978   G4double d2,s;
1749                                                  1979 
1750   // General Precalcs                            1980   // General Precalcs
1751   //                                          << 1981 
1752   rho2 = p.x()*p.x()+p.y()*p.y();                1982   rho2 = p.x()*p.x()+p.y()*p.y();
1753   rad2 = rho2+p.z()*p.z();                       1983   rad2 = rho2+p.z()*p.z();
                                                   >> 1984   //  G4double rad=std::sqrt(rad2);
                                                   >> 1985 
                                                   >> 1986   pTheta = std::atan2(std::sqrt(rho2),p.z());
1754                                                  1987 
1755   pDotV2d = p.x()*v.x()+p.y()*v.y();             1988   pDotV2d = p.x()*v.x()+p.y()*v.y();
1756   pDotV3d = pDotV2d+p.z()*v.z();                 1989   pDotV3d = pDotV2d+p.z()*v.z();
1757                                                  1990 
                                                   >> 1991   // Set phi divided flag and precalcs
                                                   >> 1992 
                                                   >> 1993   if( fDPhi < twopi )
                                                   >> 1994   {
                                                   >> 1995     segPhi=true;
                                                   >> 1996     hDPhi=0.5*fDPhi;    // half delta phi
                                                   >> 1997     cPhi=fSPhi+hDPhi;;
                                                   >> 1998     hDPhiOT=hDPhi+0.5*kAngTolerance; // Outer Tolerant half delta phi 
                                                   >> 1999     hDPhiIT=hDPhi-0.5*kAngTolerance;
                                                   >> 2000     sinCPhi=std::sin(cPhi);
                                                   >> 2001     cosCPhi=std::cos(cPhi);
                                                   >> 2002     cosHDPhiOT=std::cos(hDPhiOT);
                                                   >> 2003     cosHDPhiIT=std::cos(hDPhiIT);
                                                   >> 2004   }
                                                   >> 2005   else
                                                   >> 2006   {
                                                   >> 2007     segPhi=false;
                                                   >> 2008   }
                                                   >> 2009 
                                                   >> 2010   // Theta precalcs
                                                   >> 2011     
                                                   >> 2012   if ( fDTheta < pi )
                                                   >> 2013   {
                                                   >> 2014     segTheta  = true;
                                                   >> 2015     tolSTheta = fSTheta - kAngTolerance*0.5;
                                                   >> 2016     tolETheta = fSTheta + fDTheta + kAngTolerance*0.5;
                                                   >> 2017   }
                                                   >> 2018   else segTheta = false;
                                                   >> 2019 
                                                   >> 2020     
1758   // Radial Intersections from G4Sphere::Dist    2021   // Radial Intersections from G4Sphere::DistanceToIn
1759   //                                             2022   //
1760   // Outer spherical shell intersection          2023   // Outer spherical shell intersection
1761   // - Only if outside tolerant fRmax            2024   // - Only if outside tolerant fRmax
1762   // - Check for if inside and outer G4Sphere    2025   // - Check for if inside and outer G4Sphere heading through solid (-> 0)
1763   // - No intersect -> no intersection with G    2026   // - No intersect -> no intersection with G4Sphere
1764   //                                             2027   //
1765   // Shell eqn: x^2+y^2+z^2=RSPH^2               2028   // Shell eqn: x^2+y^2+z^2=RSPH^2
1766   //                                             2029   //
1767   // => (px+svx)^2+(py+svy)^2+(pz+svz)^2=R^2     2030   // => (px+svx)^2+(py+svy)^2+(pz+svz)^2=R^2
1768   //                                             2031   //
1769   // => (px^2+py^2+pz^2) +2sd(pxvx+pyvy+pzvz) << 2032   // => (px^2+py^2+pz^2) +2s(pxvx+pyvy+pzvz)+s^2(vx^2+vy^2+vz^2)=R^2
1770   // =>      rad2        +2sd(pDotV3d)        << 2033   // =>      rad2        +2s(pDotV3d)       +s^2                =R^2
                                                   >> 2034   //
                                                   >> 2035   // => s=-pDotV3d+-std::sqrt(pDotV3d^2-(rad2-R^2))
1771   //                                             2036   //
1772   // => sd=-pDotV3d+-std::sqrt(pDotV3d^2-(rad << 2037   // const G4double  fractionTolerance = 1.0e-12;
                                                   >> 2038 
                                                   >> 2039   const G4double  flexRadMaxTolerance =            // kRadTolerance;
                                                   >> 2040     std::max(kRadTolerance, fEpsilon * fRmax);
                                                   >> 2041 
                                                   >> 2042   const G4double  Rmax_plus = fRmax + flexRadMaxTolerance*0.5;
                                                   >> 2043 
                                                   >> 2044   const G4double  flexRadMinTolerance = std::max(kRadTolerance, 
                                                   >> 2045                      fEpsilon * fRmin);
                                                   >> 2046 
                                                   >> 2047   const G4double  Rmin_minus= (fRmin > 0) ? fRmin-flexRadMinTolerance*0.5 : 0 ;
1773                                                  2048 
1774   if( (rad2 <= Rmax_plus*Rmax_plus) && (rad2  << 2049   if(rad2 <= Rmax_plus*Rmax_plus && rad2 >= Rmin_minus*Rmin_minus)
                                                   >> 2050     //  if(rad <= Rmax_plus && rad >= Rmin_minus)
1775   {                                              2051   {
1776     c = rad2 - fRmax*fRmax;                      2052     c = rad2 - fRmax*fRmax;
1777                                                  2053 
1778     if (c < fRmaxTolerance*fRmax)             << 2054     if (c < flexRadMaxTolerance*fRmax) 
1779     {                                            2055     {
1780       // Within tolerant Outer radius         << 2056       // Within tolerant Outer radius 
1781       //                                      << 2057       // 
1782       // The test is                             2058       // The test is
1783       //     rad  - fRmax < 0.5*kRadTolerance    2059       //     rad  - fRmax < 0.5*kRadTolerance
1784       // =>  rad  < fRmax + 0.5*kRadTol          2060       // =>  rad  < fRmax + 0.5*kRadTol
1785       // =>  rad2 < (fRmax + 0.5*kRadTol)^2      2061       // =>  rad2 < (fRmax + 0.5*kRadTol)^2
1786       // =>  rad2 < fRmax^2 + 2.*0.5*fRmax*kR    2062       // =>  rad2 < fRmax^2 + 2.*0.5*fRmax*kRadTol + 0.25*kRadTol*kRadTol
1787       // =>  rad2 - fRmax^2    <~    fRmax*kR << 2063       // =>  rad2 - fRmax^2    <~    fRmax*kRadTol 
1788                                                  2064 
1789       d2 = pDotV3d*pDotV3d - c;                  2065       d2 = pDotV3d*pDotV3d - c;
1790                                                  2066 
1791       if( (c >- fRmaxTolerance*fRmax)       / << 2067       if( (c >- flexRadMaxTolerance*fRmax)       // on tolerant surface
1792        && ((pDotV3d >=0) || (d2 < 0)) )     / << 2068        && ((pDotV3d >=0) || (d2 < 0)) )          // leaving outside from Rmax 
1793                                             / << 2069                                                  // not re-entering
1794       {                                          2070       {
1795         if(calcNorm)                             2071         if(calcNorm)
1796         {                                        2072         {
1797           *validNorm = true ;                    2073           *validNorm = true ;
1798           *n         = G4ThreeVector(p.x()/fR    2074           *n         = G4ThreeVector(p.x()/fRmax,p.y()/fRmax,p.z()/fRmax) ;
1799         }                                        2075         }
1800         return snxt = 0;                         2076         return snxt = 0;
1801       }                                          2077       }
1802       else                                    << 2078       else 
1803       {                                          2079       {
1804         snxt = -pDotV3d+std::sqrt(d2);    //     2080         snxt = -pDotV3d+std::sqrt(d2);    // second root since inside Rmax
1805         side =  kRMax ;                       << 2081         side =  kRMax ; 
1806       }                                          2082       }
1807     }                                            2083     }
1808                                                  2084 
1809     // Inner spherical shell intersection:       2085     // Inner spherical shell intersection:
1810     // Always first >=0 root, because would h    2086     // Always first >=0 root, because would have passed
1811     // from outside of Rmin surface .            2087     // from outside of Rmin surface .
1812                                                  2088 
1813     if (fRmin != 0.0)                         << 2089     if (fRmin)
1814     {                                            2090     {
1815       c  = rad2 - fRmin*fRmin;                   2091       c  = rad2 - fRmin*fRmin;
1816       d2 = pDotV3d*pDotV3d - c;                  2092       d2 = pDotV3d*pDotV3d - c;
1817                                                  2093 
1818       if (c >- fRminTolerance*fRmin) // 2.0 * << 2094       if ( c >- flexRadMinTolerance*fRmin ) // 2.0 * (0.5*kRadTolerance) * fRmin
1819       {                                          2095       {
1820         if ( (c < fRminTolerance*fRmin)       << 2096         if( c < flexRadMinTolerance*fRmin && 
1821           && (d2 >= fRminTolerance*fRmin) &&  << 2097             d2 >= flexRadMinTolerance*fRmin && pDotV3d < 0 ) // leaving from Rmin
1822         {                                        2098         {
1823           if(calcNorm)  { *validNorm = false; << 2099           if(calcNorm)  *validNorm = false ;   // Rmin surface is concave         
1824           return snxt = 0 ;                   << 2100                        return snxt = 0 ;
1825         }                                        2101         }
1826         else                                     2102         else
1827         {                                     << 2103         {  
1828           if ( d2 >= 0. )                        2104           if ( d2 >= 0. )
1829           {                                      2105           {
1830             sd = -pDotV3d-std::sqrt(d2);      << 2106             s = -pDotV3d-std::sqrt(d2);
1831                                                  2107 
1832             if ( sd >= 0. )     // Always int << 2108             if ( s >= 0. )     // Always intersect Rmin first
1833             {                                    2109             {
1834               snxt = sd ;                     << 2110               snxt = s ;
1835               side = kRMin ;                     2111               side = kRMin ;
1836             }                                    2112             }
1837           }                                      2113           }
1838         }                                        2114         }
1839       }                                          2115       }
1840     }                                            2116     }
1841   }                                              2117   }
1842                                                  2118 
1843   // Theta segment intersection                  2119   // Theta segment intersection
1844                                                  2120 
1845   if ( !fFullThetaSphere )                    << 2121   if (segTheta)
1846   {                                              2122   {
1847     // Intersection with theta surfaces          2123     // Intersection with theta surfaces
1848     //                                           2124     //
1849     // Known failure cases:                      2125     // Known failure cases:
1850     // o  Inside tolerance of stheta surface,    2126     // o  Inside tolerance of stheta surface, skim
1851     //    ~parallel to cone and Hit & enter e    2127     //    ~parallel to cone and Hit & enter etheta surface [& visa versa]
1852     //                                           2128     //
1853     //    To solve: Check 2nd root of etheta     2129     //    To solve: Check 2nd root of etheta surface in addition to stheta
1854     //                                           2130     //
1855     // o  start/end theta is exactly pi/2     << 2131     // o  start/end theta is exactly pi/2 
1856     //                                           2132     //
1857     // Intersections with cones                  2133     // Intersections with cones
1858     //                                           2134     //
1859     // Cone equation: x^2+y^2=z^2tan^2(t)        2135     // Cone equation: x^2+y^2=z^2tan^2(t)
1860     //                                           2136     //
1861     // => (px+svx)^2+(py+svy)^2=(pz+svz)^2tan    2137     // => (px+svx)^2+(py+svy)^2=(pz+svz)^2tan^2(t)
1862     //                                           2138     //
1863     // => (px^2+py^2-pz^2tan^2(t))+2sd(pxvx+p << 2139     // => (px^2+py^2-pz^2tan^2(t))+2s(pxvx+pyvy-pzvztan^2(t))
1864     //       + sd^2(vx^2+vy^2-vz^2tan^2(t)) = << 2140     //       + s^2(vx^2+vy^2-vz^2tan^2(t)) = 0
1865     //                                           2141     //
1866     // => sd^2(1-vz^2(1+tan^2(t))+2sd(pdotv2d << 2142     // => s^2(1-vz^2(1+tan^2(t))+2s(pdotv2d-pzvztan^2(t))+(rho2-pz^2tan^2(t))=0
1867     //       + (rho2-pz^2tan^2(t)) = 0        << 
1868     //                                           2143     //
                                                   >> 2144   
                                                   >> 2145     /* ////////////////////////////////////////////////////////
                                                   >> 2146 
                                                   >> 2147     tanSTheta=std::tan(fSTheta);
                                                   >> 2148     tanSTheta2=tanSTheta*tanSTheta;
                                                   >> 2149     tanETheta=std::tan(fSTheta+fDTheta);
                                                   >> 2150     tanETheta2=tanETheta*tanETheta;
                                                   >> 2151       
                                                   >> 2152     if (fSTheta)
                                                   >> 2153     {
                                                   >> 2154       dist2STheta=rho2-p.z()*p.z()*tanSTheta2;
                                                   >> 2155     }
                                                   >> 2156     else
                                                   >> 2157     {
                                                   >> 2158       dist2STheta = kInfinity;
                                                   >> 2159     }
                                                   >> 2160     if (fSTheta + fDTheta < pi)
                                                   >> 2161     {
                                                   >> 2162       dist2ETheta = rho2-p.z()*p.z()*tanETheta2;
                                                   >> 2163     }
                                                   >> 2164     else
                                                   >> 2165     {
                                                   >> 2166       dist2ETheta = kInfinity ;
                                                   >> 2167     }
                                                   >> 2168     if (pTheta > tolSTheta && pTheta < tolETheta)   // Inside theta  
                                                   >> 2169     {
                                                   >> 2170       // In tolerance of STheta and possible leaving out to small thetas N-
                                                   >> 2171 
                                                   >> 2172       if(pTheta < tolSTheta + kAngTolerance  && fSTheta > kAngTolerance)  
                                                   >> 2173       {
                                                   >> 2174         t2=pDotV2d-p.z()*v.z()*tanSTheta2 ; // =(VdotN+)*rhoSecSTheta
                                                   >> 2175 
                                                   >> 2176         if( fSTheta < pi*0.5 && t2 < 0)
                                                   >> 2177         {
                                                   >> 2178           if(calcNorm) *validNorm = false ;
                                                   >> 2179           return snxt = 0 ;
                                                   >> 2180         }
                                                   >> 2181         else if(fSTheta > pi*0.5 && t2 >= 0)
                                                   >> 2182         {
                                                   >> 2183           if(calcNorm)
                                                   >> 2184           {
                                                   >> 2185             rhoSecTheta = std::sqrt(rho2*(1+tanSTheta2)) ;
                                                   >> 2186             *validNorm = true ;
                                                   >> 2187             *n = G4ThreeVector(-p.x()/rhoSecTheta,   // N-
                                                   >> 2188                                -p.y()/rhoSecTheta,
                                                   >> 2189                                tanSTheta/std::sqrt(1+tanSTheta2) ) ;
                                                   >> 2190           }
                                                   >> 2191           return snxt = 0 ;
                                                   >> 2192         }
                                                   >> 2193         else if( fSTheta == pi*0.5 && v.z() > 0)
                                                   >> 2194         {
                                                   >> 2195           if(calcNorm)
                                                   >> 2196           {
                                                   >> 2197             *validNorm = true ;
                                                   >> 2198             *n = G4ThreeVector(0,0,1) ;
                                                   >> 2199           }
                                                   >> 2200           return snxt = 0 ;
                                                   >> 2201         }
                                                   >> 2202       }
                                                   >> 2203 
                                                   >> 2204       // In tolerance of ETheta and possible leaving out to larger thetas N+
                                                   >> 2205 
                                                   >> 2206       if ( (pTheta  > tolETheta - kAngTolerance)
                                                   >> 2207         && (( fSTheta + fDTheta) < pi - kAngTolerance) )  
                                                   >> 2208       {
                                                   >> 2209         t2=pDotV2d-p.z()*v.z()*tanETheta2 ;
                                                   >> 2210         if((fSTheta+fDTheta)>pi*0.5 && t2<0)
                                                   >> 2211         {
                                                   >> 2212           if(calcNorm) *validNorm = false ;
                                                   >> 2213           return snxt = 0 ;
                                                   >> 2214         }
                                                   >> 2215         else if( (fSTheta+fDTheta) < pi*0.5 && t2 >= 0 )
                                                   >> 2216         {
                                                   >> 2217           if(calcNorm)
                                                   >> 2218           {
                                                   >> 2219             rhoSecTheta = std::sqrt(rho2*(1+tanETheta2)) ;
                                                   >> 2220             *validNorm = true ;
                                                   >> 2221             *n = G4ThreeVector( p.x()/rhoSecTheta,  // N+
                                                   >> 2222                                 p.y()/rhoSecTheta,
                                                   >> 2223                                 -tanETheta/std::sqrt(1+tanETheta2)  ) ; 
                                                   >> 2224           }
                                                   >> 2225           return snxt = 0 ;
                                                   >> 2226         }
                                                   >> 2227         else if( ( fSTheta+fDTheta) == pi*0.5 && v.z() < 0 )
                                                   >> 2228         {
                                                   >> 2229           if(calcNorm)
                                                   >> 2230           {
                                                   >> 2231             *validNorm = true ;
                                                   >> 2232             *n = G4ThreeVector(0,0,-1) ;
                                                   >> 2233           }
                                                   >> 2234           return snxt = 0 ;
                                                   >> 2235         }
                                                   >> 2236       }
                                                   >> 2237       if( fSTheta > 0 )
                                                   >> 2238       {       
                                                   >> 2239         // First root of fSTheta cone, second if first root -ve
                                                   >> 2240 
                                                   >> 2241         t1 = 1-v.z()*v.z()*(1+tanSTheta2);
                                                   >> 2242         t2 = pDotV2d-p.z()*v.z()*tanSTheta2;
                                                   >> 2243         
                                                   >> 2244         b  = t2/t1;
                                                   >> 2245         c  = dist2STheta/t1;
                                                   >> 2246         d2 = b*b - c ;
                                                   >> 2247 
                                                   >> 2248         if ( d2 >= 0 )
                                                   >> 2249         {
                                                   >> 2250           d = std::sqrt(d2) ;
                                                   >> 2251           s = -b - d ;    // First root
                                                   >> 2252 
                                                   >> 2253           if ( s < 0 )
                                                   >> 2254           {
                                                   >> 2255             s = -b + d ;    // Second root
                                                   >> 2256           }
                                                   >> 2257           if (s > flexRadMaxTolerance*0.5 )   // && s<sr)
                                                   >> 2258           {
                                                   >> 2259             // check against double cone solution
                                                   >> 2260             zi=p.z()+s*v.z();
                                                   >> 2261             if (fSTheta<pi*0.5 && zi<0)
                                                   >> 2262             {
                                                   >> 2263               s = kInfinity ;  // wrong cone
                                                   >> 2264             }
                                                   >> 2265             if (fSTheta>pi*0.5 && zi>0)
                                                   >> 2266             {
                                                   >> 2267               s = kInfinity ;  // wrong cone
                                                   >> 2268             }
                                                   >> 2269             stheta = s ;
                                                   >> 2270             sidetheta = kSTheta ;
                                                   >> 2271           }
                                                   >> 2272         }
                                                   >> 2273       }
                                                   >> 2274 
                                                   >> 2275       // Possible intersection with ETheta cone  
                                                   >> 2276       
                                                   >> 2277       if (fSTheta + fDTheta < pi)
                                                   >> 2278       {
                                                   >> 2279         t1 = 1-v.z()*v.z()*(1+tanETheta2);
                                                   >> 2280         t2 = pDotV2d-p.z()*v.z()*tanETheta2;        
                                                   >> 2281         b  = t2/t1;
                                                   >> 2282         c  = dist2ETheta/t1;
                                                   >> 2283         d2 = b*b-c ;
                                                   >> 2284 
                                                   >> 2285         if ( d2 >= 0 )
                                                   >> 2286         {
                                                   >> 2287           d = std::sqrt(d2);
                                                   >> 2288           s = -b - d ;          // First root
                                                   >> 2289 
                                                   >> 2290           if ( s < 0 )
                                                   >> 2291           {
                                                   >> 2292             s=-b+d;    // Second root
                                                   >> 2293           }
                                                   >> 2294           if (s > flexRadMaxTolerance*0.5 && s < stheta )
                                                   >> 2295           {
                                                   >> 2296             // check against double cone solution
                                                   >> 2297             zi=p.z()+s*v.z();
                                                   >> 2298             if (fSTheta+fDTheta<pi*0.5 && zi<0)
                                                   >> 2299             {
                                                   >> 2300               s = kInfinity ;  // wrong cone
                                                   >> 2301             }
                                                   >> 2302             if (fSTheta+fDTheta>pi*0.5 && zi>0)
                                                   >> 2303             {
                                                   >> 2304               s = kInfinity ;  // wrong cone
                                                   >> 2305             }
                                                   >> 2306           }
                                                   >> 2307           if (s < stheta)
                                                   >> 2308           {
                                                   >> 2309             stheta = s ;
                                                   >> 2310             sidetheta = kETheta ;
                                                   >> 2311           }
                                                   >> 2312         }
                                                   >> 2313       }
                                                   >> 2314     }  
                                                   >> 2315     */  ////////////////////////////////////////////////////////////
1869                                                  2316 
1870     if(fSTheta != 0.0) // intersection with f << 2317     if(fSTheta) // intersection with first cons
1871     {                                            2318     {
                                                   >> 2319 
                                                   >> 2320       tanSTheta = std::tan(fSTheta);
                                                   >> 2321 
1872       if( std::fabs(tanSTheta) > 5./kAngToler    2322       if( std::fabs(tanSTheta) > 5./kAngTolerance ) // kons is plane z=0
1873       {                                          2323       {
1874         if( v.z() > 0. )                      << 2324         if( v.z() > 0. ) 
1875         {                                        2325         {
1876           if ( std::fabs( p.z() ) <= halfRmax << 2326           if ( std::fabs( p.z() ) <= flexRadMaxTolerance*0.5 )
1877           {                                      2327           {
1878             if(calcNorm)                         2328             if(calcNorm)
1879             {                                    2329             {
1880               *validNorm = true;                 2330               *validNorm = true;
1881               *n = G4ThreeVector(0.,0.,1.);      2331               *n = G4ThreeVector(0.,0.,1.);
1882             }                                    2332             }
1883             return snxt = 0 ;                    2333             return snxt = 0 ;
1884           }                                   << 2334           }  
                                                   >> 2335           // s = -p.z()/v.z();
1885           stheta    = -p.z()/v.z();              2336           stheta    = -p.z()/v.z();
1886           sidetheta = kSTheta;                   2337           sidetheta = kSTheta;
1887         }                                        2338         }
1888       }                                          2339       }
1889       else // kons is not plane               << 2340       else // kons is not plane 
1890       {                                          2341       {
                                                   >> 2342         tanSTheta2  = tanSTheta*tanSTheta;
1891         t1          = 1-v.z()*v.z()*(1+tanSTh    2343         t1          = 1-v.z()*v.z()*(1+tanSTheta2);
1892         t2          = pDotV2d-p.z()*v.z()*tan    2344         t2          = pDotV2d-p.z()*v.z()*tanSTheta2;  // ~vDotN if p on cons
1893         dist2STheta = rho2-p.z()*p.z()*tanSTh << 2345         dist2STheta = rho2-p.z()*p.z()*tanSTheta2;      // t3
1894                                                  2346 
                                                   >> 2347         // distTheta = std::sqrt(std::fabs(dist2STheta/(1+tanSTheta2)));
1895         distTheta = std::sqrt(rho2)-p.z()*tan    2348         distTheta = std::sqrt(rho2)-p.z()*tanSTheta;
1896                                                  2349 
1897         if( std::fabs(t1) < halfAngTolerance  << 2350         if( std::fabs(t1) < 0.5*kAngTolerance ) // 1st order equation, v parallel to kons
1898         {                                     << 2351         {
1899           if( v.z() > 0. )                       2352           if( v.z() > 0. )
1900           {                                      2353           {
1901             if(std::fabs(distTheta) < halfRma << 2354             if(std::fabs(distTheta) < flexRadMaxTolerance*0.5) // p on surface
1902             {                                    2355             {
1903               if( (fSTheta < halfpi) && (p.z( << 2356               if( fSTheta < halfpi && p.z() > 0. )
1904               {                                  2357               {
1905                 if( calcNorm )  { *validNorm  << 2358                 if( calcNorm ) *validNorm = false;
1906                 return snxt = 0.;             << 2359                               return snxt = 0.;
1907               }                                  2360               }
1908               else if( (fSTheta > halfpi) &&  << 2361               else if( fSTheta > halfpi && p.z() <= 0)
1909               {                                  2362               {
1910                 if( calcNorm )                << 2363                 if( calcNorm ) 
1911                 {                                2364                 {
1912                   *validNorm = true;             2365                   *validNorm = true;
1913                   if (rho2 != 0.0)            << 2366                   if (rho2)
1914                   {                              2367                   {
1915                     rhoSecTheta = std::sqrt(r    2368                     rhoSecTheta = std::sqrt(rho2*(1+tanSTheta2));
1916                                               << 2369                    
1917                     *n = G4ThreeVector( p.x() << 2370                     *n = G4ThreeVector( p.x()/rhoSecTheta,   
1918                                         p.y()    2371                                         p.y()/rhoSecTheta,
1919                                         std::    2372                                         std::sin(fSTheta)  );
1920                   }                              2373                   }
1921                   else *n = G4ThreeVector(0.,    2374                   else *n = G4ThreeVector(0.,0.,1.);
1922                 }                                2375                 }
1923                 return snxt = 0.;             << 2376                 return snxt = 0.;               
1924               }                                  2377               }
1925             }                                    2378             }
                                                   >> 2379             // s = -0.5*dist2STheta/t2;
                                                   >> 2380 
1926             stheta    = -0.5*dist2STheta/t2;     2381             stheta    = -0.5*dist2STheta/t2;
1927             sidetheta = kSTheta;                 2382             sidetheta = kSTheta;
1928           }                                   << 2383           }  
1929         }      // 2nd order equation, 1st roo << 2384         }
1930         else   // 2nd if 1st root -ve         << 2385         else   // 2nd order equation, 1st root of fSTheta cone, 2nd if 1st root -ve
1931         {                                        2386         {
1932           if( std::fabs(distTheta) < halfRmax << 2387           if( std::fabs(distTheta) < flexRadMaxTolerance*0.5) // && t2 >= 0.) surface
1933           {                                      2388           {
1934             if( (fSTheta > halfpi) && (t2 >=  << 2389             if( fSTheta > halfpi && t2 >= 0. ) // leave
1935             {                                    2390             {
1936               if( calcNorm )                  << 2391               if( calcNorm ) 
1937               {                                  2392               {
1938                 *validNorm = true;               2393                 *validNorm = true;
1939                 if (rho2 != 0.0)              << 2394                 if (rho2)
1940                 {                                2395                 {
1941                   rhoSecTheta = std::sqrt(rho << 2396                     rhoSecTheta = std::sqrt(rho2*(1+tanSTheta2));
1942                                               << 2397                    
1943                   *n = G4ThreeVector( p.x()/r << 2398                     *n = G4ThreeVector( p.x()/rhoSecTheta,   
1944                                       p.y()/r << 2399                                         p.y()/rhoSecTheta,
1945                                       std::si << 2400                                         std::sin(fSTheta)  );
1946                 }                                2401                 }
1947                 else  { *n = G4ThreeVector(0. << 2402                 else *n = G4ThreeVector(0.,0.,1.);
1948               }                               << 2403               }                           
1949               return snxt = 0.;                  2404               return snxt = 0.;
1950             }                                    2405             }
1951             else if( (fSTheta < halfpi) && (t << 2406             else if( fSTheta < halfpi  && t2 < 0. && p.z() >=0. ) // leave
1952             {                                    2407             {
1953               if( calcNorm )  { *validNorm =  << 2408                 if( calcNorm )   *validNorm = false;                                                 
1954               return snxt = 0.;               << 2409                 return snxt = 0.;
1955             }                                 << 2410             }                               
1956           }                                      2411           }
1957           b  = t2/t1;                            2412           b  = t2/t1;
1958           c  = dist2STheta/t1;                   2413           c  = dist2STheta/t1;
1959           d2 = b*b - c ;                         2414           d2 = b*b - c ;
1960                                                  2415 
1961           if ( d2 >= 0. )                        2416           if ( d2 >= 0. )
1962           {                                      2417           {
1963             d = std::sqrt(d2);                   2418             d = std::sqrt(d2);
1964                                                  2419 
1965             if( fSTheta > halfpi )               2420             if( fSTheta > halfpi )
1966             {                                    2421             {
1967               sd = -b - d;         // First r << 2422               s = -b - d;         // First root
1968                                                  2423 
1969               if ( ((std::fabs(s) < halfRmaxT << 2424               if( (std::fabs(s) < flexRadMaxTolerance*0.5 && t2 < 0.) ||
1970                ||  (sd < 0.)  || ( (sd > 0.)  << 2425                               s < 0.  ||
                                                   >> 2426                   ( s > 0. && p.z() + s*v.z() > 0.)                   ) 
1971               {                                  2427               {
1972                 sd = -b + d ; // 2nd root     << 2428                 s = -b + d ; // 2nd root
1973               }                                  2429               }
1974               if( (sd > halfRmaxTolerance) && << 2430               if( s >  flexRadMaxTolerance*0.5 && p.z() + s*v.z() <= 0.)  
1975               {                                  2431               {
1976                 stheta    = sd;               << 2432                 stheta    = s;
1977                 sidetheta = kSTheta;             2433                 sidetheta = kSTheta;
1978               }                                  2434               }
1979             }                                    2435             }
1980             else // sTheta < pi/2, concave su    2436             else // sTheta < pi/2, concave surface, no normal
1981             {                                    2437             {
1982               sd = -b - d;         // First r << 2438               s = -b - d;         // First root
1983                                                  2439 
1984               if ( ( (std::fabs(sd) < halfRma << 2440               if( (std::fabs(s) < flexRadMaxTolerance*0.5 && t2 >= 0.) ||
1985                 || (sd < 0.) || ( (sd > 0.) & << 2441                               s < 0.                                   ||
                                                   >> 2442                   ( s > 0. && p.z() + s*v.z() < 0.)                      )
1986               {                                  2443               {
1987                 sd = -b + d ; // 2nd root     << 2444                 s = -b + d ; // 2nd root
1988               }                                  2445               }
1989               if( (sd > halfRmaxTolerance) && << 2446               if( s >  flexRadMaxTolerance*0.5 && p.z() + s*v.z() >= 0.)  
1990               {                                  2447               {
1991                 stheta    = sd;               << 2448                 stheta    = s;
1992                 sidetheta = kSTheta;             2449                 sidetheta = kSTheta;
1993               }                               << 2450               }            
1994             }                                    2451             }
1995           }                                      2452           }
1996         }                                        2453         }
1997       }                                          2454       }
1998     }                                            2455     }
1999     if (eTheta < pi) // intersection with sec << 2456     if (fSTheta + fDTheta < pi) // intersection with second cons
2000     {                                            2457     {
                                                   >> 2458 
                                                   >> 2459       tanETheta = std::tan(fSTheta+fDTheta);
                                                   >> 2460 
2001       if( std::fabs(tanETheta) > 5./kAngToler    2461       if( std::fabs(tanETheta) > 5./kAngTolerance ) // kons is plane z=0
2002       {                                          2462       {
2003         if( v.z() < 0. )                      << 2463         if( v.z() < 0. ) 
2004         {                                        2464         {
2005           if ( std::fabs( p.z() ) <= halfRmax << 2465           if ( std::fabs( p.z() ) <= flexRadMaxTolerance*0.5 )
2006           {                                      2466           {
2007             if(calcNorm)                         2467             if(calcNorm)
2008             {                                    2468             {
2009               *validNorm = true;                 2469               *validNorm = true;
2010               *n = G4ThreeVector(0.,0.,-1.);     2470               *n = G4ThreeVector(0.,0.,-1.);
2011             }                                    2471             }
2012             return snxt = 0 ;                    2472             return snxt = 0 ;
2013           }                                   << 2473           }  
2014           sd = -p.z()/v.z();                  << 2474           s = -p.z()/v.z();
2015                                                  2475 
2016           if( sd < stheta )                   << 2476           if( s < stheta)
2017           {                                      2477           {
2018             stheta    = sd;                   << 2478             stheta    = s;
2019             sidetheta = kETheta;                 2479             sidetheta = kETheta;
2020           }                                      2480           }
2021         }                                        2481         }
2022       }                                          2482       }
2023       else // kons is not plane               << 2483       else // kons is not plane 
2024       {                                          2484       {
                                                   >> 2485         tanETheta2  = tanETheta*tanETheta;
2025         t1          = 1-v.z()*v.z()*(1+tanETh    2486         t1          = 1-v.z()*v.z()*(1+tanETheta2);
2026         t2          = pDotV2d-p.z()*v.z()*tan    2487         t2          = pDotV2d-p.z()*v.z()*tanETheta2;  // ~vDotN if p on cons
2027         dist2ETheta = rho2-p.z()*p.z()*tanETh << 2488         dist2ETheta = rho2-p.z()*p.z()*tanETheta2;      // t3
2028                                                  2489 
                                                   >> 2490         // distTheta = std::sqrt(std::fabs(dist2ETheta/(1+tanETheta2)));
2029         distTheta = std::sqrt(rho2)-p.z()*tan    2491         distTheta = std::sqrt(rho2)-p.z()*tanETheta;
2030                                                  2492 
2031         if( std::fabs(t1) < halfAngTolerance  << 2493         if( std::fabs(t1) < 0.5*kAngTolerance ) // 1st order equation, v parallel to kons
2032         {                                     << 2494         {
2033           if( v.z() < 0. )                       2495           if( v.z() < 0. )
2034           {                                      2496           {
2035             if(std::fabs(distTheta) < halfRma << 2497             if(std::fabs(distTheta) < flexRadMaxTolerance*0.5) // p on surface
2036             {                                    2498             {
2037               if( (eTheta > halfpi) && (p.z() << 2499               if( fSTheta+fDTheta > halfpi && p.z() < 0. )
2038               {                                  2500               {
2039                 if( calcNorm )  { *validNorm  << 2501                 if( calcNorm ) *validNorm = false;
2040                 return snxt = 0.;             << 2502                               return snxt = 0.;
2041               }                                  2503               }
2042               else if ( (eTheta < halfpi) &&  << 2504               else if( fSTheta+fDTheta < halfpi && p.z() >= 0)
2043               {                                  2505               {
2044                 if( calcNorm )                << 2506                 if( calcNorm ) 
2045                 {                                2507                 {
2046                   *validNorm = true;             2508                   *validNorm = true;
2047                   if (rho2 != 0.0)            << 2509                   if (rho2)
2048                   {                              2510                   {
2049                     rhoSecTheta = std::sqrt(r    2511                     rhoSecTheta = std::sqrt(rho2*(1+tanETheta2));
2050                     *n = G4ThreeVector( p.x() << 2512                    
                                                   >> 2513                     *n = G4ThreeVector( p.x()/rhoSecTheta,   
2051                                         p.y()    2514                                         p.y()/rhoSecTheta,
2052                                         -sinE << 2515                                         -std::sin(fSTheta+fDTheta)  );
2053                   }                              2516                   }
2054                   else  { *n = G4ThreeVector( << 2517                   else *n = G4ThreeVector(0.,0.,-1.);
2055                 }                                2518                 }
2056                 return snxt = 0.;             << 2519                 return snxt = 0.;               
2057               }                                  2520               }
2058             }                                    2521             }
2059             sd = -0.5*dist2ETheta/t2;         << 2522             s = -0.5*dist2ETheta/t2;
2060                                                  2523 
2061             if( sd < stheta )                 << 2524             if( s < stheta)
2062             {                                    2525             {
2063               stheta    = sd;                 << 2526               stheta    = s;
2064               sidetheta = kETheta;               2527               sidetheta = kETheta;
2065             }                                    2528             }
2066           }                                   << 2529           }  
2067         }      // 2nd order equation, 1st roo << 2530         }
2068         else   // 2nd if 1st root -ve         << 2531         else   // 2nd order equation, 1st root of fSTheta cone, 2nd if 1st root -ve
2069         {                                        2532         {
2070           if ( std::fabs(distTheta) < halfRma << 2533           if( std::fabs(distTheta) < flexRadMaxTolerance*0.5) // && t2 >= 0.) surface
2071           {                                      2534           {
2072             if( (eTheta < halfpi) && (t2 >= 0 << 2535             if( fSTheta+fDTheta < halfpi && t2 >= 0. ) // leave
2073             {                                    2536             {
2074               if( calcNorm )                  << 2537               if( calcNorm ) 
2075               {                                  2538               {
2076                 *validNorm = true;               2539                 *validNorm = true;
2077                 if (rho2 != 0.0)              << 2540                 if (rho2)
2078                 {                                2541                 {
2079                     rhoSecTheta = std::sqrt(r    2542                     rhoSecTheta = std::sqrt(rho2*(1+tanETheta2));
2080                     *n = G4ThreeVector( p.x() << 2543                    
                                                   >> 2544                     *n = G4ThreeVector( p.x()/rhoSecTheta,   
2081                                         p.y()    2545                                         p.y()/rhoSecTheta,
2082                                         -sinE << 2546                                         -std::sin(fSTheta+fDTheta)  );
2083                 }                                2547                 }
2084                 else *n = G4ThreeVector(0.,0.    2548                 else *n = G4ThreeVector(0.,0.,-1.);
2085               }                               << 2549               }                           
2086               return snxt = 0.;                  2550               return snxt = 0.;
2087             }                                    2551             }
2088             else if ( (eTheta > halfpi)       << 2552             else if( fSTheta+fDTheta > halfpi  && t2 < 0. && p.z() <=0. ) // leave
2089                    && (t2 < 0.) && (p.z() <=0 << 
2090             {                                    2553             {
2091               if( calcNorm )  { *validNorm =  << 2554                 if( calcNorm )   *validNorm = false;                                                 
2092               return snxt = 0.;               << 2555                 return snxt = 0.;
2093             }                                 << 2556             }                               
2094           }                                      2557           }
2095           b  = t2/t1;                            2558           b  = t2/t1;
2096           c  = dist2ETheta/t1;                   2559           c  = dist2ETheta/t1;
2097           d2 = b*b - c ;                         2560           d2 = b*b - c ;
2098           if ( (d2 <halfRmaxTolerance) && (d2 << 2561 
2099           {                                   << 
2100             d2 = 0.;                          << 
2101           }                                   << 
2102           if ( d2 >= 0. )                        2562           if ( d2 >= 0. )
2103           {                                      2563           {
2104             d = std::sqrt(d2);                   2564             d = std::sqrt(d2);
2105                                                  2565 
2106             if( eTheta < halfpi )             << 2566             if( fSTheta+fDTheta < halfpi )
2107             {                                    2567             {
2108               sd = -b - d;         // First r << 2568               s = -b - d;         // First root
2109                                                  2569 
2110               if( ((std::fabs(sd) < halfRmaxT << 2570               if( (std::fabs(s) < flexRadMaxTolerance*0.5 && t2 < 0.) ||
2111                || (sd < 0.) )                 << 2571                               s < 0. ) 
2112               {                                  2572               {
2113                 sd = -b + d ; // 2nd root     << 2573                 s = -b + d ; // 2nd root
2114               }                                  2574               }
2115               if( sd > halfRmaxTolerance )    << 2575               if( s >  flexRadMaxTolerance*0.5 )  
2116               {                                  2576               {
2117                 if( sd < stheta )             << 2577                 if( s < stheta )
2118                 {                                2578                 {
2119                   stheta    = sd;             << 2579                   stheta    = s;
2120                   sidetheta = kETheta;           2580                   sidetheta = kETheta;
2121                 }                                2581                 }
2122               }                                  2582               }
2123             }                                    2583             }
2124             else // sTheta+fDTheta > pi/2, co    2584             else // sTheta+fDTheta > pi/2, concave surface, no normal
2125             {                                    2585             {
2126               sd = -b - d;         // First r << 2586               s = -b - d;         // First root
2127                                                  2587 
2128               if ( ((std::fabs(sd) < halfRmax << 2588               if( (std::fabs(s) < flexRadMaxTolerance*0.5 && t2 >= 0.) ||
2129                 || (sd < 0.)                  << 2589                               s < 0.                                   ||
2130                 || ( (sd > 0.) && (p.z() + sd << 2590                   ( s > 0. && p.z() + s*v.z() > 0.)                      )
2131               {                                  2591               {
2132                 sd = -b + d ; // 2nd root     << 2592                 s = -b + d ; // 2nd root
2133               }                                  2593               }
2134               if ( ( sd>halfRmaxTolerance )   << 2594               if( s >  flexRadMaxTolerance*0.5 && p.z() + s*v.z() <= 0.)  
2135                 && ( p.z()+sd*v.z() <= halfRm << 
2136               {                                  2595               {
2137                 if( sd < stheta )             << 2596                 if( s < stheta )
2138                 {                                2597                 {
2139                   stheta    = sd;             << 2598                   stheta    = s;
2140                   sidetheta = kETheta;           2599                   sidetheta = kETheta;
2141                 }                                2600                 }
2142               }                               << 2601               }            
2143             }                                    2602             }
2144           }                                      2603           }
2145         }                                        2604         }
2146       }                                          2605       }
2147     }                                            2606     }
2148                                                  2607 
2149   } // end theta intersections                   2608   } // end theta intersections
2150                                                  2609 
2151   // Phi Intersection                            2610   // Phi Intersection
2152                                               << 2611     
2153   if ( !fFullPhiSphere )                      << 2612   if ( fDPhi < twopi)
2154   {                                              2613   {
2155     if ( (p.x() != 0.0) || (p.y() != 0.0) ) / << 2614     sinSPhi=std::sin(fSPhi);
                                                   >> 2615     cosSPhi=std::cos(fSPhi);
                                                   >> 2616     ePhi=fSPhi+fDPhi;
                                                   >> 2617     sinEPhi=std::sin(ePhi);
                                                   >> 2618     cosEPhi=std::cos(ePhi);
                                                   >> 2619     cPhi=fSPhi+fDPhi*0.5;
                                                   >> 2620     sinCPhi=std::sin(cPhi);
                                                   >> 2621     cosCPhi=std::cos(cPhi);
                                                   >> 2622 
                                                   >> 2623     if ( p.x()||p.y() ) // Check if on z axis (rho not needed later)
2156     {                                            2624     {
2157       // pDist -ve when inside                   2625       // pDist -ve when inside
2158                                                  2626 
2159       pDistS=p.x()*sinSPhi-p.y()*cosSPhi;        2627       pDistS=p.x()*sinSPhi-p.y()*cosSPhi;
2160       pDistE=-p.x()*sinEPhi+p.y()*cosEPhi;       2628       pDistE=-p.x()*sinEPhi+p.y()*cosEPhi;
2161                                                  2629 
2162       // Comp -ve when in direction of outwar    2630       // Comp -ve when in direction of outwards normal
2163                                                  2631 
2164       compS   = -sinSPhi*v.x()+cosSPhi*v.y()     2632       compS   = -sinSPhi*v.x()+cosSPhi*v.y() ;
2165       compE   =  sinEPhi*v.x()-cosEPhi*v.y()     2633       compE   =  sinEPhi*v.x()-cosEPhi*v.y() ;
2166       sidephi = kNull ;                          2634       sidephi = kNull ;
2167                                                  2635 
2168       if ( (pDistS <= 0) && (pDistE <= 0) )   << 2636       if ( pDistS <= 0 && pDistE <= 0 )
2169       {                                          2637       {
2170         // Inside both phi *full* planes         2638         // Inside both phi *full* planes
2171                                                  2639 
2172         if ( compS < 0 )                         2640         if ( compS < 0 )
2173         {                                        2641         {
2174           sphi = pDistS/compS ;                  2642           sphi = pDistS/compS ;
2175           xi   = p.x()+sphi*v.x() ;              2643           xi   = p.x()+sphi*v.x() ;
2176           yi   = p.y()+sphi*v.y() ;              2644           yi   = p.y()+sphi*v.y() ;
2177                                                  2645 
2178           // Check intersection with correct  << 2646           // Check intersecting with correct half-plane
2179           //                                  << 2647           // (if not -> no intersect)
2180           if( (std::fabs(xi)<=kCarTolerance)  << 2648 
2181           {                                   << 2649           if ( ( yi*cosCPhi - xi*sinCPhi ) >= 0 )
2182             vphi = std::atan2(v.y(),v.x());   << 
2183             sidephi = kSPhi;                  << 
2184             if ( ( (fSPhi-halfAngTolerance) < << 
2185               && ( (ePhi+halfAngTolerance)  > << 
2186             {                                 << 
2187               sphi = kInfinity;               << 
2188             }                                 << 
2189           }                                   << 
2190           else if ( ( yi*cosCPhi - xi*sinCPhi << 
2191           {                                      2650           {
2192             sphi=kInfinity;                      2651             sphi=kInfinity;
2193           }                                      2652           }
2194           else                                   2653           else
2195           {                                      2654           {
2196             sidephi = kSPhi ;                    2655             sidephi = kSPhi ;
2197             if ( pDistS > -halfCarTolerance)  << 2656             if ( pDistS > -0.5*kCarTolerance) sphi =0 ; // Leave by sphi 
2198           }                                      2657           }
2199         }                                        2658         }
2200         else  { sphi = kInfinity; }           << 2659         else sphi = kInfinity ;
2201                                                  2660 
2202         if ( compE < 0 )                         2661         if ( compE < 0 )
2203         {                                        2662         {
2204           sphi2=pDistE/compE ;                   2663           sphi2=pDistE/compE ;
2205           if (sphi2 < sphi) // Only check fur    2664           if (sphi2 < sphi) // Only check further if < starting phi intersection
2206           {                                      2665           {
2207             xi = p.x()+sphi2*v.x() ;             2666             xi = p.x()+sphi2*v.x() ;
2208             yi = p.y()+sphi2*v.y() ;             2667             yi = p.y()+sphi2*v.y() ;
2209                                                  2668 
2210             // Check intersection with correc << 2669             // Check intersecting with correct half-plane
2211             //                                << 2670  
2212             if ( (std::fabs(xi)<=kCarToleranc << 2671             if ((yi*cosCPhi-xi*sinCPhi)>=0) // Leaving via ending phi
2213               && (std::fabs(yi)<=kCarToleranc << 
2214             {                                 << 
2215               // Leaving via ending phi       << 
2216               //                              << 
2217               vphi = std::atan2(v.y(),v.x())  << 
2218                                               << 
2219               if( (fSPhi-halfAngTolerance > v << 
2220                   ||(fSPhi+fDPhi+halfAngToler << 
2221               {                               << 
2222                 sidephi = kEPhi;              << 
2223                 if ( pDistE <= -halfCarTolera << 
2224                 else                          << 
2225               }                               << 
2226             }                                 << 
2227             else if ((yi*cosCPhi-xi*sinCPhi)> << 
2228             {                                    2672             {
2229               sidephi = kEPhi ;                  2673               sidephi = kEPhi ;
2230               if ( pDistE <= -halfCarToleranc << 2674               if ( pDistE <= -0.5*kCarTolerance )
2231               {                                  2675               {
2232                 sphi=sphi2;                      2676                 sphi=sphi2;
2233               }                                  2677               }
2234               else                            << 2678               else 
2235               {                                  2679               {
2236                 sphi = 0 ;                       2680                 sphi = 0 ;
2237               }                                  2681               }
2238             }                                    2682             }
2239           }                                      2683           }
2240         }                                     << 2684         }        
2241       }                                          2685       }
2242       else if ((pDistS >= 0) && (pDistE >= 0) << 2686       else if ( pDistS >= 0 && pDistE >= 0 ) // Outside both *full* phi planes
2243       {                                          2687       {
2244         if ( pDistS <= pDistE )                  2688         if ( pDistS <= pDistE )
2245         {                                        2689         {
2246           sidephi = kSPhi ;                      2690           sidephi = kSPhi ;
2247         }                                        2691         }
2248         else                                     2692         else
2249         {                                        2693         {
2250           sidephi = kEPhi ;                      2694           sidephi = kEPhi ;
2251         }                                        2695         }
2252         if ( fDPhi > pi )                        2696         if ( fDPhi > pi )
2253         {                                        2697         {
2254           if ( (compS < 0) && (compE < 0) )   << 2698           if ( compS < 0 && compE < 0 ) sphi = 0 ;
2255           else                                << 2699           else                          sphi = kInfinity ;
2256         }                                        2700         }
2257         else                                     2701         else
2258         {                                        2702         {
2259           // if towards both >=0 then once in    2703           // if towards both >=0 then once inside (after error)
2260           // will remain inside                  2704           // will remain inside
2261                                                  2705 
2262           if ( (compS >= 0) && (compE >= 0) ) << 2706           if ( compS >= 0 && compE >= 0 )
2263           else                                << 2707           {
2264         }                                     << 2708             sphi=kInfinity;
                                                   >> 2709           }
                                                   >> 2710           else
                                                   >> 2711           {
                                                   >> 2712             sphi=0;
                                                   >> 2713           }
                                                   >> 2714         }    
2265       }                                          2715       }
2266       else if ( (pDistS > 0) && (pDistE < 0)  << 2716       else if ( pDistS > 0 && pDistE < 0 )
2267       {                                          2717       {
2268         // Outside full starting plane, insid    2718         // Outside full starting plane, inside full ending plane
2269                                                  2719 
2270         if ( fDPhi > pi )                        2720         if ( fDPhi > pi )
2271         {                                        2721         {
2272           if ( compE < 0 )                       2722           if ( compE < 0 )
2273           {                                      2723           {
2274             sphi = pDistE/compE ;                2724             sphi = pDistE/compE ;
2275             xi   = p.x() + sphi*v.x() ;          2725             xi   = p.x() + sphi*v.x() ;
2276             yi   = p.y() + sphi*v.y() ;          2726             yi   = p.y() + sphi*v.y() ;
2277                                                  2727 
2278             // Check intersection in correct     2728             // Check intersection in correct half-plane
2279             // (if not -> not leaving phi ext    2729             // (if not -> not leaving phi extent)
2280             //                                   2730             //
2281             if( (std::fabs(xi)<=kCarTolerance << 2731             if ( ( yi*cosCPhi - xi*sinCPhi ) <= 0 )
2282             {                                 << 
2283               vphi = std::atan2(v.y(),v.x()); << 
2284               sidephi = kSPhi;                << 
2285               if ( ( (fSPhi-halfAngTolerance) << 
2286                 && ( (ePhi+halfAngTolerance)  << 
2287               {                               << 
2288                 sphi = kInfinity;             << 
2289               }                               << 
2290             }                                 << 
2291             else if ( ( yi*cosCPhi - xi*sinCP << 
2292             {                                    2732             {
2293               sphi = kInfinity ;                 2733               sphi = kInfinity ;
2294             }                                    2734             }
2295             else // Leaving via Ending phi       2735             else // Leaving via Ending phi
2296             {                                    2736             {
2297               sidephi = kEPhi ;                  2737               sidephi = kEPhi ;
2298               if ( pDistE > -halfCarTolerance << 2738               if ( pDistE > -0.5*kCarTolerance ) sphi = 0. ;
2299             }                                    2739             }
2300           }                                      2740           }
2301           else                                   2741           else
2302           {                                      2742           {
2303             sphi = kInfinity ;                   2743             sphi = kInfinity ;
2304           }                                      2744           }
2305         }                                        2745         }
2306         else                                     2746         else
2307         {                                        2747         {
2308           if ( compS >= 0 )                      2748           if ( compS >= 0 )
2309           {                                      2749           {
2310             if ( compE < 0 )                     2750             if ( compE < 0 )
2311             {                                 << 2751             {            
2312               sphi = pDistE/compE ;              2752               sphi = pDistE/compE ;
2313               xi   = p.x() + sphi*v.x() ;        2753               xi   = p.x() + sphi*v.x() ;
2314               yi   = p.y() + sphi*v.y() ;        2754               yi   = p.y() + sphi*v.y() ;
2315                                                  2755 
2316               // Check intersection in correc    2756               // Check intersection in correct half-plane
2317               // (if not -> remain in extent)    2757               // (if not -> remain in extent)
2318               //                                 2758               //
2319               if( (std::fabs(xi)<=kCarToleran << 2759               if ( ( yi*cosCPhi - xi*sinCPhi) <= 0 )
2320                && (std::fabs(yi)<=kCarToleran << 
2321               {                               << 
2322                 vphi = std::atan2(v.y(),v.x() << 
2323                 sidephi = kSPhi;              << 
2324                 if ( ( (fSPhi-halfAngToleranc << 
2325                   && ( (ePhi+halfAngTolerance << 
2326                 {                             << 
2327                   sphi = kInfinity;           << 
2328                 }                             << 
2329               }                               << 
2330               else if ( ( yi*cosCPhi - xi*sin << 
2331               {                                  2760               {
2332                 sphi=kInfinity;                  2761                 sphi=kInfinity;
2333               }                                  2762               }
2334               else // otherwise leaving via E    2763               else // otherwise leaving via Ending phi
2335               {                                  2764               {
2336                 sidephi = kEPhi ;                2765                 sidephi = kEPhi ;
2337               }                                  2766               }
2338             }                                    2767             }
2339             else sphi=kInfinity;                 2768             else sphi=kInfinity;
2340           }                                      2769           }
2341           else // leaving immediately by star    2770           else // leaving immediately by starting phi
2342           {                                      2771           {
2343             sidephi = kSPhi ;                    2772             sidephi = kSPhi ;
2344             sphi    = 0 ;                        2773             sphi    = 0 ;
2345           }                                      2774           }
2346         }                                        2775         }
2347       }                                          2776       }
2348       else                                       2777       else
2349       {                                          2778       {
2350         // Must be pDistS < 0 && pDistE > 0      2779         // Must be pDistS < 0 && pDistE > 0
2351         // Inside full starting plane, outsid    2780         // Inside full starting plane, outside full ending plane
2352                                                  2781 
2353         if ( fDPhi > pi )                        2782         if ( fDPhi > pi )
2354         {                                        2783         {
2355           if ( compS < 0 )                       2784           if ( compS < 0 )
2356           {                                      2785           {
2357             sphi=pDistS/compS;                   2786             sphi=pDistS/compS;
2358             xi=p.x()+sphi*v.x();                 2787             xi=p.x()+sphi*v.x();
2359             yi=p.y()+sphi*v.y();                 2788             yi=p.y()+sphi*v.y();
2360                                                  2789 
2361             // Check intersection in correct     2790             // Check intersection in correct half-plane
2362             // (if not -> not leaving phi ext    2791             // (if not -> not leaving phi extent)
2363             //                                   2792             //
2364             if( (std::fabs(xi)<=kCarTolerance << 2793             if ( ( yi*cosCPhi - xi*sinCPhi ) >= 0 )
2365             {                                 << 
2366               vphi = std::atan2(v.y(),v.x())  << 
2367               sidephi = kSPhi;                << 
2368               if ( ( (fSPhi-halfAngTolerance) << 
2369                 && ( (ePhi+halfAngTolerance)  << 
2370               {                               << 
2371               sphi = kInfinity;               << 
2372               }                               << 
2373             }                                 << 
2374             else if ( ( yi*cosCPhi - xi*sinCP << 
2375             {                                    2794             {
2376               sphi = kInfinity ;                 2795               sphi = kInfinity ;
2377             }                                    2796             }
2378             else  // Leaving via Starting phi << 2797            else  // Leaving via Starting phi
2379             {                                 << 2798            {
2380               sidephi = kSPhi ;                  2799               sidephi = kSPhi ;
2381               if ( pDistS > -halfCarTolerance << 2800              if ( pDistS > -0.5*kCarTolerance ) sphi = 0 ;
2382             }                                    2801             }
2383           }                                      2802           }
2384           else                                   2803           else
2385           {                                      2804           {
2386             sphi = kInfinity ;                   2805             sphi = kInfinity ;
2387           }                                      2806           }
2388         }                                        2807         }
2389         else                                     2808         else
2390         {                                        2809         {
2391           if ( compE >= 0 )                      2810           if ( compE >= 0 )
2392           {                                      2811           {
2393             if ( compS < 0 )                     2812             if ( compS < 0 )
2394             {                                    2813             {
2395               sphi = pDistS/compS ;              2814               sphi = pDistS/compS ;
2396               xi   = p.x()+sphi*v.x() ;          2815               xi   = p.x()+sphi*v.x() ;
2397               yi   = p.y()+sphi*v.y() ;          2816               yi   = p.y()+sphi*v.y() ;
2398                                                  2817 
2399               // Check intersection in correc    2818               // Check intersection in correct half-plane
2400               // (if not -> remain in extent)    2819               // (if not -> remain in extent)
2401               //                                 2820               //
2402               if( (std::fabs(xi)<=kCarToleran << 2821               if ( ( yi*cosCPhi - xi*sinCPhi ) >= 0 )
2403                && (std::fabs(yi)<=kCarToleran << 
2404               {                               << 
2405                 vphi = std::atan2(v.y(),v.x() << 
2406                 sidephi = kSPhi;              << 
2407                 if ( ( (fSPhi-halfAngToleranc << 
2408                   && ( (ePhi+halfAngTolerance << 
2409                 {                             << 
2410                   sphi = kInfinity;           << 
2411                 }                             << 
2412               }                               << 
2413               else if ( ( yi*cosCPhi - xi*sin << 
2414               {                                  2822               {
2415                 sphi = kInfinity ;               2823                 sphi = kInfinity ;
2416               }                                  2824               }
2417               else // otherwise leaving via S    2825               else // otherwise leaving via Starting phi
2418               {                                  2826               {
2419                 sidephi = kSPhi ;                2827                 sidephi = kSPhi ;
2420               }                                  2828               }
2421             }                                    2829             }
2422             else                                 2830             else
2423             {                                    2831             {
2424               sphi = kInfinity ;                 2832               sphi = kInfinity ;
2425             }                                    2833             }
2426           }                                      2834           }
2427           else // leaving immediately by endi    2835           else // leaving immediately by ending
2428           {                                      2836           {
2429             sidephi = kEPhi ;                    2837             sidephi = kEPhi ;
2430             sphi    = 0     ;                    2838             sphi    = 0     ;
2431           }                                      2839           }
2432         }                                        2840         }
2433       }                                       << 2841       }      
2434     }                                            2842     }
2435     else                                         2843     else
2436     {                                            2844     {
2437       // On z axis + travel not || to z axis     2845       // On z axis + travel not || to z axis -> if phi of vector direction
2438       // within phi of shape, Step limited by    2846       // within phi of shape, Step limited by rmax, else Step =0
2439                                                  2847 
2440       if ( (v.x() != 0.0) || (v.y() != 0.0) ) << 2848       if ( v.x() || v.y() )
2441       {                                          2849       {
2442         vphi = std::atan2(v.y(),v.x()) ;         2850         vphi = std::atan2(v.y(),v.x()) ;
2443         if ((fSPhi-halfAngTolerance < vphi) & << 2851         if ( fSPhi < vphi && vphi < fSPhi + fDPhi )
2444         {                                        2852         {
2445           sphi = kInfinity;                   << 2853           sphi=kInfinity;
2446         }                                        2854         }
2447         else                                     2855         else
2448         {                                        2856         {
2449           sidephi = kSPhi ; // arbitrary      << 2857           sidephi = kSPhi ; // arbitrary 
2450           sphi    = 0     ;                      2858           sphi    = 0     ;
2451         }                                        2859         }
2452       }                                          2860       }
2453       else  // travel along z - no phi inters << 2861       else  // travel along z - no phi intersaction
2454       {                                          2862       {
2455         sphi = kInfinity ;                       2863         sphi = kInfinity ;
2456       }                                          2864       }
2457     }                                            2865     }
2458     if ( sphi < snxt )  // Order intersecttio    2866     if ( sphi < snxt )  // Order intersecttions
2459     {                                            2867     {
2460       snxt = sphi ;                              2868       snxt = sphi ;
2461       side = sidephi ;                           2869       side = sidephi ;
2462     }                                            2870     }
2463   }                                              2871   }
2464   if (stheta < snxt ) // Order intersections     2872   if (stheta < snxt ) // Order intersections
2465   {                                              2873   {
2466     snxt = stheta ;                              2874     snxt = stheta ;
2467     side = sidetheta ;                           2875     side = sidetheta ;
2468   }                                              2876   }
2469                                                  2877 
2470   if (calcNorm)    // Output switch operator     2878   if (calcNorm)    // Output switch operator
2471   {                                              2879   {
2472     switch( side )                               2880     switch( side )
2473     {                                            2881     {
2474       case kRMax:                                2882       case kRMax:
2475         xi=p.x()+snxt*v.x();                     2883         xi=p.x()+snxt*v.x();
2476         yi=p.y()+snxt*v.y();                     2884         yi=p.y()+snxt*v.y();
2477         zi=p.z()+snxt*v.z();                     2885         zi=p.z()+snxt*v.z();
2478         *n=G4ThreeVector(xi/fRmax,yi/fRmax,zi    2886         *n=G4ThreeVector(xi/fRmax,yi/fRmax,zi/fRmax);
2479         *validNorm=true;                         2887         *validNorm=true;
2480         break;                                   2888         break;
2481                                                  2889 
2482       case kRMin:                                2890       case kRMin:
2483         *validNorm=false;  // Rmin is concave    2891         *validNorm=false;  // Rmin is concave
2484         break;                                   2892         break;
2485                                                  2893 
2486       case kSPhi:                                2894       case kSPhi:
2487         if ( fDPhi <= pi )     // Normal to P    2895         if ( fDPhi <= pi )     // Normal to Phi-
2488         {                                        2896         {
2489           *n=G4ThreeVector(sinSPhi,-cosSPhi,0 << 2897           *n=G4ThreeVector(std::sin(fSPhi),-std::cos(fSPhi),0);
2490           *validNorm=true;                       2898           *validNorm=true;
2491         }                                        2899         }
2492         else  { *validNorm=false; }           << 2900         else *validNorm=false;
2493         break ;                                  2901         break ;
2494                                                  2902 
2495       case kEPhi:                                2903       case kEPhi:
2496         if ( fDPhi <= pi )      // Normal to     2904         if ( fDPhi <= pi )      // Normal to Phi+
2497         {                                        2905         {
2498           *n=G4ThreeVector(-sinEPhi,cosEPhi,0 << 2906           *n=G4ThreeVector(-std::sin(fSPhi+fDPhi),std::cos(fSPhi+fDPhi),0);
2499           *validNorm=true;                       2907           *validNorm=true;
2500         }                                        2908         }
2501         else  { *validNorm=false; }           << 2909         else *validNorm=false;
2502         break;                                   2910         break;
2503                                                  2911 
2504       case kSTheta:                              2912       case kSTheta:
2505         if( fSTheta == halfpi )                  2913         if( fSTheta == halfpi )
2506         {                                        2914         {
2507           *n=G4ThreeVector(0.,0.,1.);            2915           *n=G4ThreeVector(0.,0.,1.);
2508           *validNorm=true;                       2916           *validNorm=true;
2509         }                                        2917         }
2510         else if ( fSTheta > halfpi )             2918         else if ( fSTheta > halfpi )
2511         {                                        2919         {
2512           xi = p.x() + snxt*v.x();               2920           xi = p.x() + snxt*v.x();
2513           yi = p.y() + snxt*v.y();               2921           yi = p.y() + snxt*v.y();
2514           rho2=xi*xi+yi*yi;                   << 2922           rhoSecTheta = std::sqrt((xi*xi+yi*yi)*(1+tanSTheta2));
2515           if (rho2 != 0.0)                    << 2923           *n = G4ThreeVector( xi/rhoSecTheta,   // N-
2516           {                                   << 2924                               yi/rhoSecTheta,
2517             rhoSecTheta = std::sqrt(rho2*(1+t << 2925                               -tanSTheta/std::sqrt(1+tanSTheta2));
2518             *n = G4ThreeVector( xi/rhoSecThet << 
2519                                -tanSTheta/std << 
2520           }                                   << 
2521           else                                << 
2522           {                                   << 
2523             *n = G4ThreeVector(0.,0.,1.);     << 
2524           }                                   << 
2525           *validNorm=true;                       2926           *validNorm=true;
2526         }                                        2927         }
2527         else  { *validNorm=false; }  // Conca << 2928         else *validNorm=false;  // Concave STheta cone
2528         break;                                   2929         break;
2529                                                  2930 
2530       case kETheta:                              2931       case kETheta:
2531         if( eTheta == halfpi )                << 2932         if( ( fSTheta + fDTheta ) == halfpi )
2532         {                                        2933         {
2533           *n         = G4ThreeVector(0.,0.,-1    2934           *n         = G4ThreeVector(0.,0.,-1.);
2534           *validNorm = true;                     2935           *validNorm = true;
2535         }                                        2936         }
2536         else if ( eTheta < halfpi )           << 2937         else if ( ( fSTheta + fDTheta ) < halfpi)
2537         {                                        2938         {
2538           xi=p.x()+snxt*v.x();                   2939           xi=p.x()+snxt*v.x();
2539           yi=p.y()+snxt*v.y();                   2940           yi=p.y()+snxt*v.y();
2540           rho2=xi*xi+yi*yi;                   << 2941           rhoSecTheta = std::sqrt((xi*xi+yi*yi)*(1+tanETheta2));
2541           if (rho2 != 0.0)                    << 2942           *n = G4ThreeVector( xi/rhoSecTheta,   // N+
2542           {                                   << 2943                               yi/rhoSecTheta,
2543             rhoSecTheta = std::sqrt(rho2*(1+t << 2944                               -tanETheta/std::sqrt(1+tanETheta2) );
2544             *n = G4ThreeVector( xi/rhoSecThet << 
2545                                -tanETheta/std << 
2546           }                                   << 
2547           else                                << 
2548           {                                   << 
2549             *n = G4ThreeVector(0.,0.,-1.);    << 
2550           }                                   << 
2551           *validNorm=true;                       2945           *validNorm=true;
2552         }                                        2946         }
2553         else  { *validNorm=false; }   // Conc << 2947         else *validNorm=false;   // Concave ETheta cone
2554         break;                                   2948         break;
2555                                                  2949 
2556       default:                                   2950       default:
                                                   >> 2951         G4cout.precision(16);
2557         G4cout << G4endl;                        2952         G4cout << G4endl;
2558         DumpInfo();                              2953         DumpInfo();
2559         std::ostringstream message;           << 2954         G4cout << "Position:"  << G4endl << G4endl;
2560         G4long oldprc = message.precision(16) << 2955         G4cout << "p.x() = "   << p.x()/mm << " mm" << G4endl;
2561         message << "Undefined side for valid  << 2956         G4cout << "p.y() = "   << p.y()/mm << " mm" << G4endl;
2562                 << G4endl                     << 2957         G4cout << "p.z() = "   << p.z()/mm << " mm" << G4endl << G4endl;
2563                 << "Position:"  << G4endl <<  << 2958         G4cout << "Direction:" << G4endl << G4endl;
2564                 << "p.x() = "   << p.x()/mm < << 2959         G4cout << "v.x() = "   << v.x() << G4endl;
2565                 << "p.y() = "   << p.y()/mm < << 2960         G4cout << "v.y() = "   << v.y() << G4endl;
2566                 << "p.z() = "   << p.z()/mm < << 2961         G4cout << "v.z() = "   << v.z() << G4endl << G4endl;
2567                 << "Direction:" << G4endl <<  << 2962         G4cout << "Proposed distance :" << G4endl << G4endl;
2568                 << "v.x() = "   << v.x() << G << 2963         G4cout << "snxt = "    << snxt/mm << " mm" << G4endl << G4endl;
2569                 << "v.y() = "   << v.y() << G << 
2570                 << "v.z() = "   << v.z() << G << 
2571                 << "Proposed distance :" << G << 
2572                 << "snxt = "    << snxt/mm << << 
2573         message.precision(oldprc);            << 
2574         G4Exception("G4Sphere::DistanceToOut(    2964         G4Exception("G4Sphere::DistanceToOut(p,v,..)",
2575                     "GeomSolids1002", JustWar << 2965                     "Notification", JustWarning,
                                                   >> 2966                     "Undefined side for valid surface normal to solid.");
2576         break;                                   2967         break;
2577     }                                            2968     }
2578   }                                              2969   }
2579   if (snxt == kInfinity)                         2970   if (snxt == kInfinity)
2580   {                                              2971   {
                                                   >> 2972     G4cout.precision(24);
2581     G4cout << G4endl;                            2973     G4cout << G4endl;
2582     DumpInfo();                                  2974     DumpInfo();
2583     std::ostringstream message;               << 2975     G4cout << "Position:"  << G4endl << G4endl;
2584     G4long oldprc = message.precision(16);    << 2976     G4cout << "p.x() = "   << p.x()/mm << " mm" << G4endl;
2585     message << "Logic error: snxt = kInfinity << 2977     G4cout << "p.y() = "   << p.y()/mm << " mm" << G4endl;
2586             << "Position:"  << G4endl << G4en << 2978     G4cout << "p.z() = "   << p.z()/mm << " mm" << G4endl << G4endl;
2587             << "p.x() = "   << p.x()/mm << "  << 2979     G4cout << "Rp = "<< std::sqrt( p.x()*p.x()+p.y()*p.y()+p.z()*p.z() )/mm << " mm" 
2588             << "p.y() = "   << p.y()/mm << "  << 2980            << G4endl << G4endl;
2589             << "p.z() = "   << p.z()/mm << "  << 2981     G4cout << "Direction:" << G4endl << G4endl;
2590             << "Rp = "<< std::sqrt( p.x()*p.x << 2982     G4cout << "v.x() = "   << v.x() << G4endl;
2591             << " mm" << G4endl << G4endl      << 2983     G4cout << "v.y() = "   << v.y() << G4endl;
2592             << "Direction:" << G4endl << G4en << 2984     G4cout << "v.z() = "   << v.z() << G4endl << G4endl;
2593             << "v.x() = "   << v.x() << G4end << 2985     G4cout << "Proposed distance :" << G4endl << G4endl;
2594             << "v.y() = "   << v.y() << G4end << 2986     G4cout << "snxt = "    << snxt/mm << " mm" << G4endl << G4endl;
2595             << "v.z() = "   << v.z() << G4end << 
2596             << "Proposed distance :" << G4end << 
2597             << "snxt = "    << snxt/mm << " m << 
2598     message.precision(oldprc);                << 
2599     G4Exception("G4Sphere::DistanceToOut(p,v,    2987     G4Exception("G4Sphere::DistanceToOut(p,v,..)",
2600                 "GeomSolids1002", JustWarning << 2988                 "Notification", JustWarning,
                                                   >> 2989                 "Logic error: snxt = kInfinity  ???");
2601   }                                              2990   }
2602                                                  2991 
2603   return snxt;                                   2992   return snxt;
2604 }                                                2993 }
2605                                                  2994 
2606 /////////////////////////////////////////////    2995 /////////////////////////////////////////////////////////////////////////
2607 //                                               2996 //
2608 // Calculate distance (<=actual) to closest s << 2997 // Calcluate distance (<=actual) to closest surface of shape from inside
2609                                                  2998 
2610 G4double G4Sphere::DistanceToOut( const G4Thr    2999 G4double G4Sphere::DistanceToOut( const G4ThreeVector& p ) const
2611 {                                                3000 {
2612   G4double safe=0.0,safeRMin,safeRMax,safePhi    3001   G4double safe=0.0,safeRMin,safeRMax,safePhi,safeTheta;
2613   G4double rho2,rds,rho;                      << 3002   G4double rho2,rad,rho;
2614   G4double pTheta,dTheta1 = kInfinity,dTheta2 << 3003   G4double phiC,cosPhiC,sinPhiC,ePhi;
                                                   >> 3004   G4double pTheta,dTheta1,dTheta2;
2615   rho2=p.x()*p.x()+p.y()*p.y();                  3005   rho2=p.x()*p.x()+p.y()*p.y();
2616   rds=std::sqrt(rho2+p.z()*p.z());            << 3006   rad=std::sqrt(rho2+p.z()*p.z());
2617   rho=std::sqrt(rho2);                           3007   rho=std::sqrt(rho2);
2618                                                  3008 
2619 #ifdef G4CSGDEBUG                                3009 #ifdef G4CSGDEBUG
2620   if( Inside(p) == kOutside )                    3010   if( Inside(p) == kOutside )
2621   {                                              3011   {
2622      G4long old_prc = G4cout.precision(16);   << 3012      G4cout.precision(16) ;
2623      G4cout << G4endl;                        << 3013      G4cout << G4endl ;
2624      DumpInfo();                                 3014      DumpInfo();
2625      G4cout << "Position:"  << G4endl << G4en    3015      G4cout << "Position:"  << G4endl << G4endl ;
2626      G4cout << "p.x() = "   << p.x()/mm << "     3016      G4cout << "p.x() = "   << p.x()/mm << " mm" << G4endl ;
2627      G4cout << "p.y() = "   << p.y()/mm << "     3017      G4cout << "p.y() = "   << p.y()/mm << " mm" << G4endl ;
2628      G4cout << "p.z() = "   << p.z()/mm << "     3018      G4cout << "p.z() = "   << p.z()/mm << " mm" << G4endl << G4endl ;
2629      G4cout.precision(old_prc) ;              << 
2630      G4Exception("G4Sphere::DistanceToOut(p)"    3019      G4Exception("G4Sphere::DistanceToOut(p)",
2631                  "GeomSolids1002", JustWarnin << 3020                  "Notification", JustWarning, "Point p is outside !?" );
2632   }                                              3021   }
2633 #endif                                           3022 #endif
2634                                                  3023 
2635   // Distance to r shells                     << 
2636   //                                             3024   //
2637   safeRMax = fRmax-rds;                       << 3025   // Distance to r shells
2638   safe = safeRMax;                            << 3026   //    
2639   if (fRmin != 0.0)                           << 3027   if (fRmin)
2640   {                                              3028   {
2641      safeRMin = rds-fRmin;                    << 3029     safeRMin=rad-fRmin;
2642      safe = std::min( safeRMin, safeRMax );   << 3030     safeRMax=fRmax-rad;
                                                   >> 3031     if (safeRMin<safeRMax)
                                                   >> 3032     {
                                                   >> 3033       safe=safeRMin;
                                                   >> 3034     }
                                                   >> 3035     else
                                                   >> 3036     {
                                                   >> 3037       safe=safeRMax;
                                                   >> 3038     }
                                                   >> 3039   }
                                                   >> 3040   else
                                                   >> 3041   {
                                                   >> 3042     safe=fRmax-rad;
2643   }                                              3043   }
2644                                                  3044 
                                                   >> 3045   //
2645   // Distance to phi extent                      3046   // Distance to phi extent
2646   //                                             3047   //
2647   if ( !fFullPhiSphere )                      << 3048   if (fDPhi<twopi && rho)
2648   {                                              3049   {
2649      if (rho>0.0)                             << 3050     phiC=fSPhi+fDPhi*0.5;
2650      {                                        << 3051     cosPhiC=std::cos(phiC);
2651         if ((p.y()*cosCPhi-p.x()*sinCPhi)<=0) << 3052     sinPhiC=std::sin(phiC);
2652         {                                     << 3053     if ((p.y()*cosPhiC-p.x()*sinPhiC)<=0)
2653            safePhi=-(p.x()*sinSPhi-p.y()*cosS << 3054     {
2654         }                                     << 3055       safePhi=-(p.x()*std::sin(fSPhi)-p.y()*std::cos(fSPhi));
2655         else                                  << 3056     }
2656         {                                     << 3057     else
2657            safePhi=(p.x()*sinEPhi-p.y()*cosEP << 3058     {
2658         }                                     << 3059       ePhi=fSPhi+fDPhi;
2659      }                                        << 3060       safePhi=(p.x()*std::sin(ePhi)-p.y()*std::cos(ePhi));
2660      else                                     << 3061     }
2661      {                                        << 3062     if (safePhi<safe) safe=safePhi;
2662         safePhi = 0.0;  // Distance to both P << 
2663      }                                        << 
2664      // Both cases above can be improved - in << 
2665      //  although it may be costlier (good fo << 
2666                                               << 
2667      safe= std::min(safe, safePhi);           << 
2668   }                                              3063   }
2669                                                  3064 
2670   // Distance to Theta extent                 << 
2671   //                                             3065   //
2672   if ( !fFullThetaSphere )                    << 3066   // Distance to Theta extent
                                                   >> 3067   //    
                                                   >> 3068   if (rad)
2673   {                                              3069   {
2674     if( rds > 0.0 )                           << 3070     pTheta=std::acos(p.z()/rad);
                                                   >> 3071     if (pTheta<0) pTheta+=pi;
                                                   >> 3072     dTheta1=pTheta-fSTheta;
                                                   >> 3073     dTheta2=(fSTheta+fDTheta)-pTheta;
                                                   >> 3074     if (dTheta1<dTheta2)
2675     {                                            3075     {
2676        pTheta=std::acos(p.z()/rds);           << 3076       safeTheta=rad*std::sin(dTheta1);
2677        if (pTheta<0) { pTheta+=pi; }          << 3077       if (safe>safeTheta)
2678        if(fSTheta>0.)                         << 3078       {
2679        { dTheta1=pTheta-fSTheta;}             << 3079         safe=safeTheta;
2680        if(eTheta<pi)                          << 3080       }
2681        { dTheta2=eTheta-pTheta;}              << 
2682                                               << 
2683        safeTheta=rds*std::sin(std::min(dTheta << 
2684     }                                            3081     }
2685     else                                         3082     else
2686     {                                            3083     {
2687        safeTheta= 0.0;                        << 3084       safeTheta=rad*std::sin(dTheta2);
2688          // An improvement will be to return  << 3085       if (safe>safeTheta)
                                                   >> 3086       {
                                                   >> 3087         safe=safeTheta;
                                                   >> 3088       }
2689     }                                            3089     }
2690     safe = std::min( safe, safeTheta );       << 
2691   }                                              3090   }
2692                                                  3091 
2693   if (safe<0.0) { safe=0; }                   << 3092   if (safe<0) safe=0;
2694     // An improvement to return negative answ << 3093     return safe;
2695                                               << 
2696   return safe;                                << 
2697 }                                                3094 }
2698                                                  3095 
2699 /////////////////////////////////////////////    3096 //////////////////////////////////////////////////////////////////////////
2700 //                                               3097 //
2701 // G4EntityType                               << 3098 // Create a List containing the transformed vertices
                                                   >> 3099 // Ordering [0-3] -fDz cross section
                                                   >> 3100 //          [4-7] +fDz cross section such that [0] is below [4],
                                                   >> 3101 //                                             [1] below [5] etc.
                                                   >> 3102 // Note:
                                                   >> 3103 //  Caller has deletion resposibility
                                                   >> 3104 //  Potential improvement: For last slice, use actual ending angle
                                                   >> 3105 //                         to avoid rounding error problems.
                                                   >> 3106 
                                                   >> 3107 G4ThreeVectorList*
                                                   >> 3108 G4Sphere::CreateRotatedVertices( const G4AffineTransform& pTransform,
                                                   >> 3109                                        G4int& noPolygonVertices ) const
                                                   >> 3110 {
                                                   >> 3111   G4ThreeVectorList *vertices;
                                                   >> 3112   G4ThreeVector vertex;
                                                   >> 3113   G4double meshAnglePhi,meshRMax,crossAnglePhi,
                                                   >> 3114            coscrossAnglePhi,sincrossAnglePhi,sAnglePhi;
                                                   >> 3115   G4double meshTheta,crossTheta,startTheta;
                                                   >> 3116   G4double rMaxX,rMaxY,rMinX,rMinY,rMinZ,rMaxZ;
                                                   >> 3117   G4int crossSectionPhi,noPhiCrossSections,crossSectionTheta,noThetaSections;
                                                   >> 3118 
                                                   >> 3119   // Phi cross sections
                                                   >> 3120     
                                                   >> 3121   noPhiCrossSections=G4int (fDPhi/kMeshAngleDefault)+1;
                                                   >> 3122     
                                                   >> 3123   if (noPhiCrossSections<kMinMeshSections)
                                                   >> 3124   {
                                                   >> 3125     noPhiCrossSections=kMinMeshSections;
                                                   >> 3126   }
                                                   >> 3127   else if (noPhiCrossSections>kMaxMeshSections)
                                                   >> 3128   {
                                                   >> 3129     noPhiCrossSections=kMaxMeshSections;
                                                   >> 3130   }
                                                   >> 3131   meshAnglePhi=fDPhi/(noPhiCrossSections-1);
                                                   >> 3132     
                                                   >> 3133   // If complete in phi, set start angle such that mesh will be at fRMax
                                                   >> 3134   // on the x axis. Will give better extent calculations when not rotated.
                                                   >> 3135     
                                                   >> 3136   if (fDPhi==pi*2.0 && fSPhi==0)
                                                   >> 3137   {
                                                   >> 3138     sAnglePhi = -meshAnglePhi*0.5;
                                                   >> 3139   }
                                                   >> 3140     else
                                                   >> 3141   {
                                                   >> 3142     sAnglePhi=fSPhi;
                                                   >> 3143   }    
2702                                                  3144 
2703 G4GeometryType G4Sphere::GetEntityType() cons << 3145   // Theta cross sections
2704 {                                             << 3146     
2705   return {"G4Sphere"};                        << 3147   noThetaSections = G4int(fDTheta/kMeshAngleDefault)+1;
                                                   >> 3148     
                                                   >> 3149   if (noThetaSections<kMinMeshSections)
                                                   >> 3150   {
                                                   >> 3151     noThetaSections=kMinMeshSections;
                                                   >> 3152   }
                                                   >> 3153   else if (noThetaSections>kMaxMeshSections)
                                                   >> 3154   {
                                                   >> 3155     noThetaSections=kMaxMeshSections;
                                                   >> 3156   }
                                                   >> 3157   meshTheta=fDTheta/(noThetaSections-1);
                                                   >> 3158     
                                                   >> 3159   // If complete in Theta, set start angle such that mesh will be at fRMax
                                                   >> 3160   // on the z axis. Will give better extent calculations when not rotated.
                                                   >> 3161     
                                                   >> 3162   if (fDTheta==pi && fSTheta==0)
                                                   >> 3163   {
                                                   >> 3164     startTheta = -meshTheta*0.5;
                                                   >> 3165   }
                                                   >> 3166   else
                                                   >> 3167   {
                                                   >> 3168     startTheta=fSTheta;
                                                   >> 3169   }    
                                                   >> 3170 
                                                   >> 3171   meshRMax = (meshAnglePhi >= meshTheta) ?
                                                   >> 3172              fRmax/std::cos(meshAnglePhi*0.5) : fRmax/std::cos(meshTheta*0.5);
                                                   >> 3173   G4double* cosCrossTheta = new G4double[noThetaSections];
                                                   >> 3174   G4double* sinCrossTheta = new G4double[noThetaSections];    
                                                   >> 3175   vertices=new G4ThreeVectorList();
                                                   >> 3176   vertices->reserve(noPhiCrossSections*(noThetaSections*2));
                                                   >> 3177   if (vertices && cosCrossTheta && sinCrossTheta)
                                                   >> 3178   {
                                                   >> 3179     for (crossSectionPhi=0;
                                                   >> 3180          crossSectionPhi<noPhiCrossSections; crossSectionPhi++)
                                                   >> 3181     {
                                                   >> 3182       crossAnglePhi=sAnglePhi+crossSectionPhi*meshAnglePhi;
                                                   >> 3183       coscrossAnglePhi=std::cos(crossAnglePhi);
                                                   >> 3184       sincrossAnglePhi=std::sin(crossAnglePhi);
                                                   >> 3185       for (crossSectionTheta=0;
                                                   >> 3186            crossSectionTheta<noThetaSections;crossSectionTheta++)
                                                   >> 3187       {
                                                   >> 3188         // Compute coordinates of cross section at section crossSectionPhi
                                                   >> 3189         //
                                                   >> 3190         crossTheta=startTheta+crossSectionTheta*meshTheta;
                                                   >> 3191         cosCrossTheta[crossSectionTheta]=std::cos(crossTheta);
                                                   >> 3192         sinCrossTheta[crossSectionTheta]=std::sin(crossTheta);
                                                   >> 3193 
                                                   >> 3194         rMinX=fRmin*sinCrossTheta[crossSectionTheta]*coscrossAnglePhi;
                                                   >> 3195         rMinY=fRmin*sinCrossTheta[crossSectionTheta]*sincrossAnglePhi;
                                                   >> 3196         rMinZ=fRmin*cosCrossTheta[crossSectionTheta];
                                                   >> 3197         
                                                   >> 3198         vertex=G4ThreeVector(rMinX,rMinY,rMinZ);
                                                   >> 3199         vertices->push_back(pTransform.TransformPoint(vertex));
                                                   >> 3200         
                                                   >> 3201       }    // Theta forward 
                                                   >> 3202     
                                                   >> 3203       for (crossSectionTheta=noThetaSections-1;
                                                   >> 3204            crossSectionTheta>=0; crossSectionTheta--)
                                                   >> 3205       {
                                                   >> 3206         rMaxX=meshRMax*sinCrossTheta[crossSectionTheta]*coscrossAnglePhi;
                                                   >> 3207         rMaxY=meshRMax*sinCrossTheta[crossSectionTheta]*sincrossAnglePhi;
                                                   >> 3208         rMaxZ=meshRMax*cosCrossTheta[crossSectionTheta];
                                                   >> 3209         
                                                   >> 3210         vertex=G4ThreeVector(rMaxX,rMaxY,rMaxZ);
                                                   >> 3211         vertices->push_back(pTransform.TransformPoint(vertex));
                                                   >> 3212 
                                                   >> 3213       }   // Theta back 
                                                   >> 3214     }   // Phi
                                                   >> 3215     noPolygonVertices = noThetaSections*2 ;
                                                   >> 3216   }
                                                   >> 3217   else
                                                   >> 3218   {
                                                   >> 3219     DumpInfo();
                                                   >> 3220     G4Exception("G4Sphere::CreateRotatedVertices()",
                                                   >> 3221                 "FatalError", FatalException,
                                                   >> 3222                 "Error in allocation of vertices. Out of memory !");
                                                   >> 3223   }
                                                   >> 3224 
                                                   >> 3225   delete[] cosCrossTheta;
                                                   >> 3226   delete[] sinCrossTheta;
                                                   >> 3227 
                                                   >> 3228   return vertices;
2706 }                                                3229 }
2707                                                  3230 
2708 /////////////////////////////////////////////    3231 //////////////////////////////////////////////////////////////////////////
2709 //                                               3232 //
2710 // Make a clone of the object                 << 3233 // G4EntityType
2711 //                                            << 3234 
2712 G4VSolid* G4Sphere::Clone() const             << 3235 G4GeometryType G4Sphere::GetEntityType() const
2713 {                                                3236 {
2714   return new G4Sphere(*this);                 << 3237   return G4String("G4Sphere");
2715 }                                                3238 }
2716                                                  3239 
2717 /////////////////////////////////////////////    3240 //////////////////////////////////////////////////////////////////////////
2718 //                                               3241 //
2719 // Stream object contents to an output stream    3242 // Stream object contents to an output stream
2720                                                  3243 
2721 std::ostream& G4Sphere::StreamInfo( std::ostr    3244 std::ostream& G4Sphere::StreamInfo( std::ostream& os ) const
2722 {                                                3245 {
2723   G4long oldprc = os.precision(16);           << 
2724   os << "------------------------------------    3246   os << "-----------------------------------------------------------\n"
2725      << "    *** Dump for solid - " << GetNam    3247      << "    *** Dump for solid - " << GetName() << " ***\n"
2726      << "    ================================    3248      << "    ===================================================\n"
2727      << " Solid type: G4Sphere\n"                3249      << " Solid type: G4Sphere\n"
2728      << " Parameters: \n"                        3250      << " Parameters: \n"
2729      << "    inner radius: " << fRmin/mm << "    3251      << "    inner radius: " << fRmin/mm << " mm \n"
2730      << "    outer radius: " << fRmax/mm << "    3252      << "    outer radius: " << fRmax/mm << " mm \n"
2731      << "    starting phi of segment  : " <<     3253      << "    starting phi of segment  : " << fSPhi/degree << " degrees \n"
2732      << "    delta phi of segment     : " <<     3254      << "    delta phi of segment     : " << fDPhi/degree << " degrees \n"
2733      << "    starting theta of segment: " <<     3255      << "    starting theta of segment: " << fSTheta/degree << " degrees \n"
2734      << "    delta theta of segment   : " <<     3256      << "    delta theta of segment   : " << fDTheta/degree << " degrees \n"
2735      << "------------------------------------    3257      << "-----------------------------------------------------------\n";
2736   os.precision(oldprc);                       << 
2737                                                  3258 
2738   return os;                                     3259   return os;
2739 }                                                3260 }
2740                                                  3261 
2741 /////////////////////////////////////////////    3262 ////////////////////////////////////////////////////////////////////////////////
2742 //                                               3263 //
2743 // Get volume                                 << 3264 // GetPointOnSurface
2744                                                  3265 
2745 G4double G4Sphere::GetCubicVolume()           << 3266 G4ThreeVector G4Sphere::GetPointOnSurface() const
2746 {                                                3267 {
2747   if (fCubicVolume == 0.)                     << 3268   G4double zRand, aOne, aTwo, aThr, aFou, aFiv, chose, phi, sinphi, cosphi;
                                                   >> 3269   G4double height1, height2, slant1, slant2, costheta, sintheta,theta,rRand;
                                                   >> 3270 
                                                   >> 3271   height1 = (fRmax-fRmin)*std::cos(fSTheta);
                                                   >> 3272   height2 = (fRmax-fRmin)*std::cos(fSTheta+fDTheta);
                                                   >> 3273   slant1  = std::sqrt(sqr((fRmax - fRmin)*std::sin(fSTheta))
                                                   >> 3274                       + height1*height1);
                                                   >> 3275   slant2  = std::sqrt(sqr((fRmax - fRmin)*std::sin(fSTheta+fDTheta))
                                                   >> 3276                       + height2*height2);
                                                   >> 3277   rRand   = RandFlat::shoot(fRmin,fRmax);
                                                   >> 3278   
                                                   >> 3279   aOne = fRmax*fRmax*fDPhi*(std::cos(fSTheta)-std::cos(fSTheta+fDTheta));
                                                   >> 3280   aTwo = fRmin*fRmin*fDPhi*(std::cos(fSTheta)-std::cos(fSTheta+fDTheta));
                                                   >> 3281   aThr = fDPhi*((fRmax + fRmin)*std::sin(fSTheta))*slant1;
                                                   >> 3282   aFou = fDPhi*((fRmax + fRmin)*std::sin(fSTheta+fDTheta))*slant2;
                                                   >> 3283   aFiv = 0.5*fDTheta*(fRmax*fRmax-fRmin*fRmin);
                                                   >> 3284   
                                                   >> 3285   phi = RandFlat::shoot(fSPhi, fSPhi + fDPhi); 
                                                   >> 3286   cosphi = std::cos(phi); 
                                                   >> 3287   sinphi = std::sin(phi);
                                                   >> 3288   theta = RandFlat::shoot(fSTheta,fSTheta+fDTheta);
                                                   >> 3289   costheta = std::cos(theta);
                                                   >> 3290   sintheta = std::sqrt(1.-sqr(costheta));
                                                   >> 3291 
                                                   >> 3292   if( ((fSPhi==0) && (fDPhi==2.*pi)) || (fDPhi==2.*pi) ) {aFiv = 0;}
                                                   >> 3293   if(fSTheta == 0)  {aThr=0;}
                                                   >> 3294   if(fDTheta + fSTheta == pi) {aFou = 0;}
                                                   >> 3295   if(fSTheta == 0.5*pi) {aThr = pi*(fRmax*fRmax-fRmin*fRmin);}
                                                   >> 3296   if(fSTheta + fDTheta == 0.5*pi) { aFou = pi*(fRmax*fRmax-fRmin*fRmin);}
                                                   >> 3297 
                                                   >> 3298   chose = RandFlat::shoot(0.,aOne+aTwo+aThr+aFou+2.*aFiv);
                                                   >> 3299   if( (chose>=0.) && (chose<aOne) )
                                                   >> 3300   {
                                                   >> 3301     return G4ThreeVector(fRmax*sintheta*cosphi,
                                                   >> 3302                          fRmax*sintheta*sinphi, fRmax*costheta);
                                                   >> 3303   }
                                                   >> 3304   else if( (chose>=aOne) && (chose<aOne+aTwo) )
                                                   >> 3305   {
                                                   >> 3306     return G4ThreeVector(fRmin*sintheta*cosphi,
                                                   >> 3307                          fRmin*sintheta*sinphi, fRmin*costheta);
                                                   >> 3308   }
                                                   >> 3309   else if( (chose>=aOne+aTwo) && (chose<aOne+aTwo+aThr) )
                                                   >> 3310   {
                                                   >> 3311     if (fSTheta != 0.5*pi)
                                                   >> 3312     {
                                                   >> 3313       zRand = RandFlat::shoot(fRmin*std::cos(fSTheta),fRmax*std::cos(fSTheta));
                                                   >> 3314       return G4ThreeVector(std::tan(fSTheta)*zRand*cosphi,
                                                   >> 3315                            std::tan(fSTheta)*zRand*sinphi,zRand);
                                                   >> 3316     }
                                                   >> 3317     else
                                                   >> 3318     {
                                                   >> 3319       return G4ThreeVector(rRand*cosphi, rRand*sinphi, 0.);
                                                   >> 3320     }    
                                                   >> 3321   }
                                                   >> 3322   else if( (chose>=aOne+aTwo+aThr) && (chose<aOne+aTwo+aThr+aFou) )
2748   {                                              3323   {
2749     G4double RRR = fRmax*fRmax*fRmax;         << 3324     if(fSTheta + fDTheta != 0.5*pi)
2750     G4double rrr = fRmin*fRmin*fRmin;         << 3325     {
2751     fCubicVolume = fDPhi*(cosSTheta - cosEThe << 3326       zRand = RandFlat::shoot(fRmin*std::cos(fSTheta+fDTheta),
                                                   >> 3327                               fRmax*std::cos(fSTheta+fDTheta));
                                                   >> 3328       return G4ThreeVector  (std::tan(fSTheta+fDTheta)*zRand*cosphi,
                                                   >> 3329                              std::tan(fSTheta+fDTheta)*zRand*sinphi,zRand);
                                                   >> 3330     }
                                                   >> 3331     else
                                                   >> 3332     {
                                                   >> 3333       return G4ThreeVector(rRand*cosphi, rRand*sinphi, 0.);
                                                   >> 3334     }
2752   }                                              3335   }
2753   return fCubicVolume;                        << 3336   else if( (chose>=aOne+aTwo+aThr+aFou) && (chose<aOne+aTwo+aThr+aFou+aFiv) )
2754 }                                             << 
2755                                               << 
2756 ///////////////////////////////////////////// << 
2757 //                                            << 
2758 // Get surface area                           << 
2759                                               << 
2760 G4double G4Sphere::GetSurfaceArea()           << 
2761 {                                             << 
2762   if (fSurfaceArea == 0.)                     << 
2763   {                                              3337   {
2764     G4double RR = fRmax*fRmax;                << 3338     return G4ThreeVector(rRand*sintheta*std::cos(fSPhi),
2765     G4double rr = fRmin*fRmin;                << 3339                          rRand*sintheta*std::sin(fSPhi),rRand*costheta);
2766     fSurfaceArea = fDPhi*(RR + rr)*(cosSTheta << 
2767     if (!fFullPhiSphere)    fSurfaceArea += f << 
2768     if (fSTheta > 0)        fSurfaceArea += 0 << 
2769     if (eTheta < CLHEP::pi) fSurfaceArea += 0 << 
2770   }                                              3340   }
2771   return fSurfaceArea;                        << 3341   else
2772 }                                             << 3342   {
2773                                               << 3343     return G4ThreeVector(rRand*sintheta*std::cos(fSPhi+fDPhi),
2774 ///////////////////////////////////////////// << 3344                          rRand*sintheta*std::sin(fSPhi+fDPhi),rRand*costheta);
2775 //                                            << 
2776 // Return a point randomly and uniformly sele << 
2777                                               << 
2778 G4ThreeVector G4Sphere::GetPointOnSurface() c << 
2779 {                                             << 
2780   G4double RR = fRmax*fRmax;                  << 
2781   G4double rr = fRmin*fRmin;                  << 
2782                                               << 
2783   // Find surface areas                       << 
2784   //                                          << 
2785   G4double aInner   = fDPhi*rr*(cosSTheta - c << 
2786   G4double aOuter   = fDPhi*RR*(cosSTheta - c << 
2787   G4double aPhi     = (!fFullPhiSphere) ? fDT << 
2788   G4double aSTheta  = (fSTheta > 0) ? 0.5*fDP << 
2789   G4double aETheta  = (eTheta < pi) ? 0.5*fDP << 
2790   G4double aTotal   = aInner + aOuter + aPhi  << 
2791                                               << 
2792   // Select surface and generate a point      << 
2793   //                                          << 
2794   G4double select = aTotal*G4QuickRand();     << 
2795   G4double u = G4QuickRand();                 << 
2796   G4double v = G4QuickRand();                 << 
2797   if (select < aInner + aOuter)            // << 
2798   {                                           << 
2799     G4double r   = (select < aInner) ? fRmin  << 
2800     G4double z   = cosSTheta + (cosETheta - c << 
2801     G4double rho = std::sqrt(1. - z*z);       << 
2802     G4double phi = fDPhi*v + fSPhi;           << 
2803     return { r*rho*std::cos(phi), r*rho*std:: << 
2804   }                                           << 
2805   else if (select < aInner + aOuter + aPhi) / << 
2806   {                                           << 
2807     G4double phi   = (select < aInner + aOute << 
2808     G4double r     = std::sqrt((RR - rr)*u +  << 
2809     G4double theta = fDTheta*v + fSTheta;     << 
2810     G4double z     = std::cos(theta);         << 
2811     G4double rho   = std::sin(theta);         << 
2812     return { r*rho*std::cos(phi), r*rho*std:: << 
2813   }                                           << 
2814   else                                     // << 
2815   {                                           << 
2816     G4double theta = (select < aTotal - aEThe << 
2817     G4double r     = std::sqrt((RR - rr)*u +  << 
2818     G4double phi   = fDPhi*v + fSPhi;         << 
2819     G4double z     = std::cos(theta);         << 
2820     G4double rho   = std::sin(theta);         << 
2821     return { r*rho*std::cos(phi), r*rho*std:: << 
2822   }                                              3345   }
2823 }                                                3346 }
2824                                                  3347 
2825 /////////////////////////////////////////////    3348 /////////////////////////////////////////////////////////////////////////////
2826 //                                               3349 //
2827 // Methods for visualisation                     3350 // Methods for visualisation
2828                                                  3351 
2829 G4VisExtent G4Sphere::GetExtent() const          3352 G4VisExtent G4Sphere::GetExtent() const
2830 {                                                3353 {
2831   return { -fRmax, fRmax,-fRmax, fRmax,-fRmax << 3354   return G4VisExtent(-fRmax, fRmax,-fRmax, fRmax,-fRmax, fRmax );
2832 }                                                3355 }
2833                                                  3356 
2834                                                  3357 
2835 void G4Sphere::DescribeYourselfTo ( G4VGraphi    3358 void G4Sphere::DescribeYourselfTo ( G4VGraphicsScene& scene ) const
2836 {                                                3359 {
2837   scene.AddSolid (*this);                        3360   scene.AddSolid (*this);
2838 }                                                3361 }
2839                                                  3362 
2840 G4Polyhedron* G4Sphere::CreatePolyhedron () c    3363 G4Polyhedron* G4Sphere::CreatePolyhedron () const
2841 {                                                3364 {
2842   return new G4PolyhedronSphere (fRmin, fRmax    3365   return new G4PolyhedronSphere (fRmin, fRmax, fSPhi, fDPhi, fSTheta, fDTheta);
2843 }                                                3366 }
2844                                                  3367 
2845 #endif                                        << 3368 G4NURBS* G4Sphere::CreateNURBS () const
                                                   >> 3369 {
                                                   >> 3370   return new G4NURBSbox (fRmax, fRmax, fRmax);       // Box for now!!!
                                                   >> 3371 }
2846                                                  3372