Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/geometry/solids/CSG/src/G4Trap.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/G4Trap.cc (Version 11.3.0) and /geometry/solids/CSG/src/G4Trap.cc (Version 10.4.p2)


  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: G4Trap.cc 107555 2017-11-22 15:26:59Z gcosmo $
                                                   >>  28 //
                                                   >>  29 // class G4Trap
                                                   >>  30 //
 26 // Implementation for G4Trap class                 31 // Implementation for G4Trap class
 27 //                                                 32 //
 28 // 21.03.95 P.Kent: Modified for `tolerant' ge <<  33 // History:
 29 // 09.09.96 V.Grichine: Final modifications be <<  34 //
 30 // 08.12.97 J.Allison: Added "nominal" constru << 
 31 // 28.04.05 V.Grichine: new SurfaceNormal acco << 
 32 // 18.04.17 E.Tcherniaev: complete revision, s     35 // 18.04.17 E.Tcherniaev: complete revision, speed-up
 33 // ------------------------------------------- <<  36 // 23.09.16 E.Tcherniaev: use G4BoundingEnvelope for CalculateExtent(),
                                                   >>  37 //                      removed CreateRotatedVertices()
                                                   >>  38 // 28.04.05 V.Grichine: new SurfaceNormal according to J. Apostolakis proposal 
                                                   >>  39 // 26.04.05 V.Grichine: new SurfaceNormal is default 
                                                   >>  40 // 19.04.05 V.Grichine: bug fixed in G4Trap("name",G4ThreeVector[8] vp)
                                                   >>  41 // 12.12.04 V.Grichine: SurfaceNormal with edges/vertices 
                                                   >>  42 // 15.11.04 V.Grichine: bug fixed in G4Trap("name",G4ThreeVector[8] vp)
                                                   >>  43 // 13.12.99 V.Grichine: bug fixed in DistanceToIn(p,v)
                                                   >>  44 // 19.11.99 V.Grichine: kUndef was added to Eside enum
                                                   >>  45 // 04.06.99 S.Giani: Fixed CalculateExtent in rotated case. 
                                                   >>  46 // 08.12.97 J.Allison: Added "nominal" constructor and method SetAllParameters.
                                                   >>  47 // 01.11.96 V.Grichine: Costructor for Right Angular Wedge from STEP, G4Trd/Para
                                                   >>  48 // 09.09.96 V.Grichine: Final modifications before to commit
                                                   >>  49 // 21.03.95 P.Kent: Modified for `tolerant' geometry
                                                   >>  50 //
                                                   >>  51 ///////////////////////////////////////////////////////////////////////////////
 34                                                    52 
 35 #include "G4Trap.hh"                               53 #include "G4Trap.hh"
 36                                                    54 
 37 #if !defined(G4GEOM_USE_UTRAP)                     55 #if !defined(G4GEOM_USE_UTRAP)
 38                                                    56 
 39 #include "globals.hh"                              57 #include "globals.hh"
 40 #include "G4GeomTools.hh"                          58 #include "G4GeomTools.hh"
 41                                                    59 
 42 #include "G4VoxelLimits.hh"                        60 #include "G4VoxelLimits.hh"
 43 #include "G4AffineTransform.hh"                    61 #include "G4AffineTransform.hh"
 44 #include "G4BoundingEnvelope.hh"                   62 #include "G4BoundingEnvelope.hh"
 45                                                    63 
 46 #include "G4VPVParameterisation.hh"                64 #include "G4VPVParameterisation.hh"
 47                                                    65 
 48 #include "G4QuickRand.hh"                      <<  66 #include "Randomize.hh"
 49                                                    67 
 50 #include "G4VGraphicsScene.hh"                     68 #include "G4VGraphicsScene.hh"
 51 #include "G4Polyhedron.hh"                         69 #include "G4Polyhedron.hh"
 52                                                    70 
 53 using namespace CLHEP;                             71 using namespace CLHEP;
 54                                                    72 
 55 //////////////////////////////////////////////     73 //////////////////////////////////////////////////////////////////////////
 56 //                                                 74 //
 57 // Constructor - check and set half-widths as  <<  75 // Constructor - check and set half-widths as well as angles: 
 58 // final check of coplanarity                      76 // final check of coplanarity
 59                                                    77 
 60 G4Trap::G4Trap( const G4String& pName,             78 G4Trap::G4Trap( const G4String& pName,
 61                       G4double pDz,                79                       G4double pDz,
 62                       G4double pTheta, G4doubl     80                       G4double pTheta, G4double pPhi,
 63                       G4double pDy1, G4double      81                       G4double pDy1, G4double pDx1, G4double pDx2,
 64                       G4double pAlp1,              82                       G4double pAlp1,
 65                       G4double pDy2, G4double      83                       G4double pDy2, G4double pDx3, G4double pDx4,
 66                       G4double pAlp2 )         <<  84                       G4double pAlp2)
 67   : G4CSGSolid(pName), halfCarTolerance(0.5*kC     85   : G4CSGSolid(pName), halfCarTolerance(0.5*kCarTolerance)
 68 {                                                  86 {
 69   fDz = pDz;                                       87   fDz = pDz;
 70   fTthetaCphi = std::tan(pTheta)*std::cos(pPhi     88   fTthetaCphi = std::tan(pTheta)*std::cos(pPhi);
 71   fTthetaSphi = std::tan(pTheta)*std::sin(pPhi     89   fTthetaSphi = std::tan(pTheta)*std::sin(pPhi);
 72                                                    90 
 73   fDy1 = pDy1; fDx1 = pDx1; fDx2 = pDx2; fTalp     91   fDy1 = pDy1; fDx1 = pDx1; fDx2 = pDx2; fTalpha1 = std::tan(pAlp1);
 74   fDy2 = pDy2; fDx3 = pDx3; fDx4 = pDx4; fTalp     92   fDy2 = pDy2; fDx3 = pDx3; fDx4 = pDx4; fTalpha2 = std::tan(pAlp2);
 75                                                    93 
 76   CheckParameters();                               94   CheckParameters();
 77   MakePlanes();                                    95   MakePlanes();
 78 }                                                  96 }
 79                                                    97 
 80 //////////////////////////////////////////////     98 //////////////////////////////////////////////////////////////////////////
 81 //                                                 99 //
 82 // Constructor - Design of trapezoid based on  << 100 // Constructor - Design of trapezoid based on 8 G4ThreeVector parameters, 
 83 // which are its vertices. Checking of planari << 101 // which are its vertices. Checking of planarity with preparation of 
 84 // fPlanes[] and than calculation of other mem    102 // fPlanes[] and than calculation of other members
 85                                                   103 
 86 G4Trap::G4Trap( const G4String& pName,            104 G4Trap::G4Trap( const G4String& pName,
 87                 const G4ThreeVector pt[8] )       105                 const G4ThreeVector pt[8] )
 88   : G4CSGSolid(pName), halfCarTolerance(0.5*kC    106   : G4CSGSolid(pName), halfCarTolerance(0.5*kCarTolerance)
 89 {                                                 107 {
 90   // Start with check of centering - the cente    108   // Start with check of centering - the center of gravity trap line
 91   // should cross the origin of frame             109   // should cross the origin of frame
 92   //                                              110   //
 93   if (  pt[0].z() >= 0                         << 111   if (!(   pt[0].z() < 0
 94         || pt[0].z() != pt[1].z()              << 112         && pt[0].z() == pt[1].z()
 95         || pt[0].z() != pt[2].z()              << 113         && pt[0].z() == pt[2].z()
 96         || pt[0].z() != pt[3].z()              << 114         && pt[0].z() == pt[3].z()
 97                                                << 115 
 98         || pt[4].z() <= 0                      << 116         && pt[4].z() > 0
 99         || pt[4].z() != pt[5].z()              << 117         && pt[4].z() == pt[5].z()
100         || pt[4].z() != pt[6].z()              << 118         && pt[4].z() == pt[6].z()
101         || pt[4].z() != pt[7].z()              << 119         && pt[4].z() == pt[7].z()
102                                                << 120 
103         || std::fabs( pt[0].z() + pt[4].z() )  << 121         && std::fabs( pt[0].z() + pt[4].z() ) < kCarTolerance
104                                                << 122 
105         || pt[0].y() != pt[1].y()              << 123         && pt[0].y() == pt[1].y()
106         || pt[2].y() != pt[3].y()              << 124         && pt[2].y() == pt[3].y()
107         || pt[4].y() != pt[5].y()              << 125         && pt[4].y() == pt[5].y()
108         || pt[6].y() != pt[7].y()              << 126         && pt[6].y() == pt[7].y()
109                                                << 127 
110         || std::fabs(pt[0].y()+pt[2].y()+pt[4] << 128         && std::fabs(pt[0].y()+pt[2].y()+pt[4].y()+pt[6].y()) < kCarTolerance
111         || std::fabs(pt[0].x()+pt[1].x()+pt[4] << 129         && std::fabs(pt[0].x()+pt[1].x()+pt[4].x()+pt[5].x() +
112                      pt[2].x()+pt[3].x()+pt[6] << 130                      pt[2].x()+pt[3].x()+pt[6].x()+pt[7].x()) < kCarTolerance ))
113   {                                               131   {
114     std::ostringstream message;                   132     std::ostringstream message;
115     message << "Invalid vertice coordinates fo    133     message << "Invalid vertice coordinates for Solid: " << GetName();
116     G4Exception("G4Trap::G4Trap()", "GeomSolid    134     G4Exception("G4Trap::G4Trap()", "GeomSolids0002",
117                 FatalException, message);         135                 FatalException, message);
118   }                                               136   }
119                                                << 137     
120   // Set parameters                               138   // Set parameters
121   //                                              139   //
122   fDz = (pt[7]).z();                              140   fDz = (pt[7]).z();
123                                                << 141       
124   fDy1     = ((pt[2]).y()-(pt[1]).y())*0.5;       142   fDy1     = ((pt[2]).y()-(pt[1]).y())*0.5;
125   fDx1     = ((pt[1]).x()-(pt[0]).x())*0.5;       143   fDx1     = ((pt[1]).x()-(pt[0]).x())*0.5;
126   fDx2     = ((pt[3]).x()-(pt[2]).x())*0.5;       144   fDx2     = ((pt[3]).x()-(pt[2]).x())*0.5;
127   fTalpha1 = ((pt[2]).x()+(pt[3]).x()-(pt[1]).    145   fTalpha1 = ((pt[2]).x()+(pt[3]).x()-(pt[1]).x()-(pt[0]).x())*0.25/fDy1;
128                                                   146 
129   fDy2     = ((pt[6]).y()-(pt[5]).y())*0.5;       147   fDy2     = ((pt[6]).y()-(pt[5]).y())*0.5;
130   fDx3     = ((pt[5]).x()-(pt[4]).x())*0.5;       148   fDx3     = ((pt[5]).x()-(pt[4]).x())*0.5;
131   fDx4     = ((pt[7]).x()-(pt[6]).x())*0.5;       149   fDx4     = ((pt[7]).x()-(pt[6]).x())*0.5;
132   fTalpha2 = ((pt[6]).x()+(pt[7]).x()-(pt[5]).    150   fTalpha2 = ((pt[6]).x()+(pt[7]).x()-(pt[5]).x()-(pt[4]).x())*0.25/fDy2;
133                                                   151 
134   fTthetaCphi = ((pt[4]).x()+fDy2*fTalpha2+fDx    152   fTthetaCphi = ((pt[4]).x()+fDy2*fTalpha2+fDx3)/fDz;
135   fTthetaSphi = ((pt[4]).y()+fDy2)/fDz;           153   fTthetaSphi = ((pt[4]).y()+fDy2)/fDz;
136                                                   154 
137   CheckParameters();                              155   CheckParameters();
138   MakePlanes(pt);                                 156   MakePlanes(pt);
139 }                                                 157 }
140                                                   158 
141 //////////////////////////////////////////////    159 //////////////////////////////////////////////////////////////////////////
142 //                                                160 //
143 // Constructor for Right Angular Wedge from ST    161 // Constructor for Right Angular Wedge from STEP
144                                                   162 
145 G4Trap::G4Trap( const G4String& pName,            163 G4Trap::G4Trap( const G4String& pName,
146                       G4double pZ,                164                       G4double pZ,
147                       G4double pY,                165                       G4double pY,
148                       G4double pX, G4double pL    166                       G4double pX, G4double pLTX )
149   : G4CSGSolid(pName), halfCarTolerance(0.5*kC    167   : G4CSGSolid(pName), halfCarTolerance(0.5*kCarTolerance)
150 {                                                 168 {
151   fDz  = 0.5*pZ; fTthetaCphi = 0; fTthetaSphi     169   fDz  = 0.5*pZ; fTthetaCphi = 0; fTthetaSphi = 0;
152   fDy1 = 0.5*pY; fDx1 = 0.5*pX; fDx2 = 0.5*pLT    170   fDy1 = 0.5*pY; fDx1 = 0.5*pX; fDx2 = 0.5*pLTX; fTalpha1 = 0.5*(pLTX - pX)/pY;
153   fDy2 = fDy1;   fDx3 = fDx1;   fDx4 = fDx2;      171   fDy2 = fDy1;   fDx3 = fDx1;   fDx4 = fDx2;     fTalpha2 = fTalpha1;
154                                                   172 
155   CheckParameters();                              173   CheckParameters();
156   MakePlanes();                                   174   MakePlanes();
157 }                                                 175 }
158                                                   176 
159 //////////////////////////////////////////////    177 //////////////////////////////////////////////////////////////////////////
160 //                                                178 //
161 // Constructor for G4Trd                          179 // Constructor for G4Trd
162                                                   180 
163 G4Trap::G4Trap( const G4String& pName,            181 G4Trap::G4Trap( const G4String& pName,
164                       G4double pDx1,  G4double    182                       G4double pDx1,  G4double pDx2,
165                       G4double pDy1,  G4double    183                       G4double pDy1,  G4double pDy2,
166                       G4double pDz )              184                       G4double pDz )
167   : G4CSGSolid(pName), halfCarTolerance(0.5*kC    185   : G4CSGSolid(pName), halfCarTolerance(0.5*kCarTolerance), fTrapType(0)
168 {                                                 186 {
169   fDz  = pDz;  fTthetaCphi = 0; fTthetaSphi =     187   fDz  = pDz;  fTthetaCphi = 0; fTthetaSphi = 0;
170   fDy1 = pDy1; fDx1 = pDx1; fDx2 = pDx1; fTalp    188   fDy1 = pDy1; fDx1 = pDx1; fDx2 = pDx1; fTalpha1 = 0;
171   fDy2 = pDy2; fDx3 = pDx2; fDx4 = pDx2; fTalp    189   fDy2 = pDy2; fDx3 = pDx2; fDx4 = pDx2; fTalpha2 = 0;
172                                                   190 
173   CheckParameters();                              191   CheckParameters();
174   MakePlanes();                                   192   MakePlanes();
175 }                                                 193 }
176                                                   194 
177 //////////////////////////////////////////////    195 //////////////////////////////////////////////////////////////////////////
178 //                                                196 //
179 // Constructor for G4Para                         197 // Constructor for G4Para
180                                                   198 
181 G4Trap::G4Trap( const G4String& pName,            199 G4Trap::G4Trap( const G4String& pName,
182                       G4double pDx, G4double p    200                       G4double pDx, G4double pDy,
183                       G4double pDz,               201                       G4double pDz,
184                       G4double pAlpha,            202                       G4double pAlpha,
185                       G4double pTheta, G4doubl    203                       G4double pTheta, G4double pPhi )
186   : G4CSGSolid(pName), halfCarTolerance(0.5*kC    204   : G4CSGSolid(pName), halfCarTolerance(0.5*kCarTolerance)
187 {                                                 205 {
188   fDz = pDz;                                      206   fDz = pDz;
189   fTthetaCphi = std::tan(pTheta)*std::cos(pPhi    207   fTthetaCphi = std::tan(pTheta)*std::cos(pPhi);
190   fTthetaSphi = std::tan(pTheta)*std::sin(pPhi    208   fTthetaSphi = std::tan(pTheta)*std::sin(pPhi);
191                                                   209 
192   fDy1 = pDy; fDx1 = pDx; fDx2 = pDx; fTalpha1    210   fDy1 = pDy; fDx1 = pDx; fDx2 = pDx; fTalpha1 = std::tan(pAlpha);
193   fDy2 = pDy; fDx3 = pDx; fDx4 = pDx; fTalpha2    211   fDy2 = pDy; fDx3 = pDx; fDx4 = pDx; fTalpha2 = fTalpha1;
194                                                   212 
195   CheckParameters();                              213   CheckParameters();
196   MakePlanes();                                   214   MakePlanes();
197 }                                                 215 }
198                                                   216 
199 //////////////////////////////////////////////    217 //////////////////////////////////////////////////////////////////////////
200 //                                                218 //
201 // Nominal constructor for G4Trap whose parame    219 // Nominal constructor for G4Trap whose parameters are to be set by
202 // a G4VParamaterisation later.  Check and set    220 // a G4VParamaterisation later.  Check and set half-widths as well as
203 // angles: final check of coplanarity             221 // angles: final check of coplanarity
204                                                   222 
205 G4Trap::G4Trap( const G4String& pName )           223 G4Trap::G4Trap( const G4String& pName )
206   : G4CSGSolid (pName), halfCarTolerance(0.5*k    224   : G4CSGSolid (pName), halfCarTolerance(0.5*kCarTolerance),
207     fDz(1.), fTthetaCphi(0.), fTthetaSphi(0.),    225     fDz(1.), fTthetaCphi(0.), fTthetaSphi(0.),
208     fDy1(1.), fDx1(1.), fDx2(1.), fTalpha1(0.)    226     fDy1(1.), fDx1(1.), fDx2(1.), fTalpha1(0.),
209     fDy2(1.), fDx3(1.), fDx4(1.), fTalpha2(0.)    227     fDy2(1.), fDx3(1.), fDx4(1.), fTalpha2(0.)
210 {                                                 228 {
211   MakePlanes();                                   229   MakePlanes();
212 }                                                 230 }
213                                                   231 
214 //////////////////////////////////////////////    232 //////////////////////////////////////////////////////////////////////////
215 //                                                233 //
216 // Fake default constructor - sets only member    234 // Fake default constructor - sets only member data and allocates memory
217 //                            for usage restri    235 //                            for usage restricted to object persistency.
218 //                                                236 //
219 G4Trap::G4Trap( __void__& a )                     237 G4Trap::G4Trap( __void__& a )
220   : G4CSGSolid(a), halfCarTolerance(0.5*kCarTo    238   : G4CSGSolid(a), halfCarTolerance(0.5*kCarTolerance),
221     fDz(1.), fTthetaCphi(0.), fTthetaSphi(0.),    239     fDz(1.), fTthetaCphi(0.), fTthetaSphi(0.),
222     fDy1(1.), fDx1(1.), fDx2(1.), fTalpha1(0.)    240     fDy1(1.), fDx1(1.), fDx2(1.), fTalpha1(0.),
223     fDy2(1.), fDx3(1.), fDx4(1.), fTalpha2(0.)    241     fDy2(1.), fDx3(1.), fDx4(1.), fTalpha2(0.)
224 {                                                 242 {
225   MakePlanes();                                   243   MakePlanes();
226 }                                                 244 }
227                                                   245 
228 //////////////////////////////////////////////    246 //////////////////////////////////////////////////////////////////////////
229 //                                                247 //
230 // Destructor                                     248 // Destructor
231                                                   249 
232 G4Trap::~G4Trap() = default;                   << 250 G4Trap::~G4Trap()
                                                   >> 251 {
                                                   >> 252 }
233                                                   253 
234 //////////////////////////////////////////////    254 //////////////////////////////////////////////////////////////////////////
235 //                                                255 //
236 // Copy constructor                               256 // Copy constructor
237                                                   257 
238 G4Trap::G4Trap(const G4Trap& rhs)                 258 G4Trap::G4Trap(const G4Trap& rhs)
239   : G4CSGSolid(rhs), halfCarTolerance(rhs.half    259   : G4CSGSolid(rhs), halfCarTolerance(rhs.halfCarTolerance),
240     fDz(rhs.fDz), fTthetaCphi(rhs.fTthetaCphi)    260     fDz(rhs.fDz), fTthetaCphi(rhs.fTthetaCphi), fTthetaSphi(rhs.fTthetaSphi),
241     fDy1(rhs.fDy1), fDx1(rhs.fDx1), fDx2(rhs.f    261     fDy1(rhs.fDy1), fDx1(rhs.fDx1), fDx2(rhs.fDx2), fTalpha1(rhs.fTalpha1),
242     fDy2(rhs.fDy2), fDx3(rhs.fDx3), fDx4(rhs.f    262     fDy2(rhs.fDy2), fDx3(rhs.fDx3), fDx4(rhs.fDx4), fTalpha2(rhs.fTalpha2)
243 {                                                 263 {
244   for (G4int i=0; i<4; ++i) { fPlanes[i] = rhs    264   for (G4int i=0; i<4; ++i) { fPlanes[i] = rhs.fPlanes[i]; }
245   for (G4int i=0; i<6; ++i) { fAreas[i] = rhs. << 
246   fTrapType = rhs.fTrapType;                      265   fTrapType = rhs.fTrapType;
247 }                                                 266 }
248                                                   267 
249 //////////////////////////////////////////////    268 //////////////////////////////////////////////////////////////////////////
250 //                                                269 //
251 // Assignment operator                            270 // Assignment operator
252                                                   271 
253 G4Trap& G4Trap::operator = (const G4Trap& rhs) << 272 G4Trap& G4Trap::operator = (const G4Trap& rhs) 
254 {                                                 273 {
255   // Check assignment to self                     274   // Check assignment to self
256   //                                              275   //
257   if (this == &rhs)  { return *this; }            276   if (this == &rhs)  { return *this; }
258                                                   277 
259   // Copy base class data                         278   // Copy base class data
260   //                                              279   //
261   G4CSGSolid::operator=(rhs);                     280   G4CSGSolid::operator=(rhs);
262                                                   281 
263   // Copy data                                    282   // Copy data
264   //                                              283   //
265   halfCarTolerance = rhs.halfCarTolerance;        284   halfCarTolerance = rhs.halfCarTolerance;
266   fDz = rhs.fDz; fTthetaCphi = rhs.fTthetaCphi    285   fDz = rhs.fDz; fTthetaCphi = rhs.fTthetaCphi; fTthetaSphi = rhs.fTthetaSphi;
267   fDy1 = rhs.fDy1; fDx1 = rhs.fDx1; fDx2 = rhs    286   fDy1 = rhs.fDy1; fDx1 = rhs.fDx1; fDx2 = rhs.fDx2; fTalpha1 = rhs.fTalpha1;
268   fDy2 = rhs.fDy2; fDx3 = rhs.fDx3; fDx4 = rhs    287   fDy2 = rhs.fDy2; fDx3 = rhs.fDx3; fDx4 = rhs.fDx4; fTalpha2 = rhs.fTalpha2;
269   for (G4int i=0; i<4; ++i) { fPlanes[i] = rhs    288   for (G4int i=0; i<4; ++i) { fPlanes[i] = rhs.fPlanes[i]; }
270   for (G4int i=0; i<6; ++i) { fAreas[i] = rhs. << 
271   fTrapType = rhs.fTrapType;                      289   fTrapType = rhs.fTrapType;
272   return *this;                                   290   return *this;
273 }                                                 291 }
274                                                   292 
275 //////////////////////////////////////////////    293 //////////////////////////////////////////////////////////////////////////
276 //                                                294 //
277 // Set all parameters, as for constructor - ch    295 // Set all parameters, as for constructor - check and set half-widths
278 // as well as angles: final check of coplanari    296 // as well as angles: final check of coplanarity
279                                                   297 
280 void G4Trap::SetAllParameters ( G4double pDz,     298 void G4Trap::SetAllParameters ( G4double pDz,
281                                 G4double pThet    299                                 G4double pTheta,
282                                 G4double pPhi,    300                                 G4double pPhi,
283                                 G4double pDy1,    301                                 G4double pDy1,
284                                 G4double pDx1,    302                                 G4double pDx1,
285                                 G4double pDx2,    303                                 G4double pDx2,
286                                 G4double pAlp1    304                                 G4double pAlp1,
287                                 G4double pDy2,    305                                 G4double pDy2,
288                                 G4double pDx3,    306                                 G4double pDx3,
289                                 G4double pDx4,    307                                 G4double pDx4,
290                                 G4double pAlp2    308                                 G4double pAlp2 )
291 {                                                 309 {
292   // Reset data of the base class                 310   // Reset data of the base class
293   fCubicVolume = 0;                               311   fCubicVolume = 0;
294   fSurfaceArea = 0;                               312   fSurfaceArea = 0;
295   fRebuildPolyhedron = true;                      313   fRebuildPolyhedron = true;
296                                                   314 
297   // Set parameters                               315   // Set parameters
298   fDz = pDz;                                      316   fDz = pDz;
299   fTthetaCphi = std::tan(pTheta)*std::cos(pPhi    317   fTthetaCphi = std::tan(pTheta)*std::cos(pPhi);
300   fTthetaSphi = std::tan(pTheta)*std::sin(pPhi    318   fTthetaSphi = std::tan(pTheta)*std::sin(pPhi);
301                                                   319 
302   fDy1 = pDy1; fDx1 = pDx1; fDx2 = pDx2; fTalp    320   fDy1 = pDy1; fDx1 = pDx1; fDx2 = pDx2; fTalpha1 = std::tan(pAlp1);
303   fDy2 = pDy2; fDx3 = pDx3; fDx4 = pDx4; fTalp    321   fDy2 = pDy2; fDx3 = pDx3; fDx4 = pDx4; fTalpha2 = std::tan(pAlp2);
304                                                   322 
305   CheckParameters();                              323   CheckParameters();
306   MakePlanes();                                   324   MakePlanes();
307 }                                                 325 }
308                                                   326 
309 //////////////////////////////////////////////    327 //////////////////////////////////////////////////////////////////////////
310 //                                                328 //
311 // Check length parameters                        329 // Check length parameters
312                                                   330 
313 void G4Trap::CheckParameters()                    331 void G4Trap::CheckParameters()
314 {                                                 332 {
315   if (fDz<=0 ||                                   333   if (fDz<=0 ||
316       fDy1<=0 || fDx1<=0 || fDx2<=0 ||            334       fDy1<=0 || fDx1<=0 || fDx2<=0 ||
317       fDy2<=0 || fDx3<=0 || fDx4<=0)              335       fDy2<=0 || fDx3<=0 || fDx4<=0)
318   {                                               336   {
319     std::ostringstream message;                   337     std::ostringstream message;
320     message << "Invalid Length Parameters for     338     message << "Invalid Length Parameters for Solid: " << GetName()
321             << "\n  X - " <<fDx1<<", "<<fDx2<<    339             << "\n  X - " <<fDx1<<", "<<fDx2<<", "<<fDx3<<", "<<fDx4
322             << "\n  Y - " <<fDy1<<", "<<fDy2      340             << "\n  Y - " <<fDy1<<", "<<fDy2
323             << "\n  Z - " <<fDz;                  341             << "\n  Z - " <<fDz;
324     G4Exception("G4Trap::CheckParameters()", "    342     G4Exception("G4Trap::CheckParameters()", "GeomSolids0002",
325                 FatalException, message);         343                 FatalException, message);
326   }                                               344   }
327 }                                                 345 }
328                                                   346 
329 //////////////////////////////////////////////    347 //////////////////////////////////////////////////////////////////////////
330 //                                                348 //
331 // Compute vertices and set side planes           349 // Compute vertices and set side planes
332                                                   350 
333 void G4Trap::MakePlanes()                         351 void G4Trap::MakePlanes()
334 {                                                 352 {
335   G4double DzTthetaCphi = fDz*fTthetaCphi;        353   G4double DzTthetaCphi = fDz*fTthetaCphi;
336   G4double DzTthetaSphi = fDz*fTthetaSphi;        354   G4double DzTthetaSphi = fDz*fTthetaSphi;
337   G4double Dy1Talpha1   = fDy1*fTalpha1;          355   G4double Dy1Talpha1   = fDy1*fTalpha1;
338   G4double Dy2Talpha2   = fDy2*fTalpha2;          356   G4double Dy2Talpha2   = fDy2*fTalpha2;
339                                                   357 
340   G4ThreeVector pt[8] =                           358   G4ThreeVector pt[8] =
341   {                                               359   {
342     G4ThreeVector(-DzTthetaCphi-Dy1Talpha1-fDx    360     G4ThreeVector(-DzTthetaCphi-Dy1Talpha1-fDx1,-DzTthetaSphi-fDy1,-fDz),
343     G4ThreeVector(-DzTthetaCphi-Dy1Talpha1+fDx    361     G4ThreeVector(-DzTthetaCphi-Dy1Talpha1+fDx1,-DzTthetaSphi-fDy1,-fDz),
344     G4ThreeVector(-DzTthetaCphi+Dy1Talpha1-fDx    362     G4ThreeVector(-DzTthetaCphi+Dy1Talpha1-fDx2,-DzTthetaSphi+fDy1,-fDz),
345     G4ThreeVector(-DzTthetaCphi+Dy1Talpha1+fDx    363     G4ThreeVector(-DzTthetaCphi+Dy1Talpha1+fDx2,-DzTthetaSphi+fDy1,-fDz),
346     G4ThreeVector( DzTthetaCphi-Dy2Talpha2-fDx    364     G4ThreeVector( DzTthetaCphi-Dy2Talpha2-fDx3, DzTthetaSphi-fDy2, fDz),
347     G4ThreeVector( DzTthetaCphi-Dy2Talpha2+fDx    365     G4ThreeVector( DzTthetaCphi-Dy2Talpha2+fDx3, DzTthetaSphi-fDy2, fDz),
348     G4ThreeVector( DzTthetaCphi+Dy2Talpha2-fDx    366     G4ThreeVector( DzTthetaCphi+Dy2Talpha2-fDx4, DzTthetaSphi+fDy2, fDz),
349     G4ThreeVector( DzTthetaCphi+Dy2Talpha2+fDx    367     G4ThreeVector( DzTthetaCphi+Dy2Talpha2+fDx4, DzTthetaSphi+fDy2, fDz)
350   };                                              368   };
351                                                   369 
352   MakePlanes(pt);                                 370   MakePlanes(pt);
353 }                                                 371 }
354                                                   372 
355 //////////////////////////////////////////////    373 //////////////////////////////////////////////////////////////////////////
356 //                                                374 //
357 // Set side planes, check planarity               375 // Set side planes, check planarity
358                                                   376 
359 void G4Trap::MakePlanes(const G4ThreeVector pt    377 void G4Trap::MakePlanes(const G4ThreeVector pt[8])
360 {                                                 378 {
361   constexpr G4int iface[4][4] = { {0,4,5,1}, { << 379   G4int iface[4][4] = { {0,4,5,1}, {2,3,7,6}, {0,2,6,4}, {1,5,7,3} };
362   const static G4String side[4] = { "~-Y", "~+ << 380   G4String side[4] = { "~-Y", "~+Y", "~-X", "~+X" };
363                                                   381 
364   for (G4int i=0; i<4; ++i)                       382   for (G4int i=0; i<4; ++i)
365   {                                               383   {
366     if (MakePlane(pt[iface[i][0]],                384     if (MakePlane(pt[iface[i][0]],
367                   pt[iface[i][1]],                385                   pt[iface[i][1]],
368                   pt[iface[i][2]],                386                   pt[iface[i][2]],
369                   pt[iface[i][3]],                387                   pt[iface[i][3]],
370                   fPlanes[i])) continue;          388                   fPlanes[i])) continue;
371                                                   389 
372     // Non planar side face                       390     // Non planar side face
373     G4ThreeVector normal(fPlanes[i].a,fPlanes[    391     G4ThreeVector normal(fPlanes[i].a,fPlanes[i].b,fPlanes[i].c);
374     G4double dmax = 0;                            392     G4double dmax = 0;
375     for (G4int k=0; k<4; ++k)                     393     for (G4int k=0; k<4; ++k)
376     {                                             394     {
377       G4double dist = normal.dot(pt[iface[i][k    395       G4double dist = normal.dot(pt[iface[i][k]]) + fPlanes[i].d;
378       if (std::abs(dist) > std::abs(dmax)) dma    396       if (std::abs(dist) > std::abs(dmax)) dmax = dist;
379     }                                             397     }
380     std::ostringstream message;                   398     std::ostringstream message;
381     message << "Side face " << side[i] << " is    399     message << "Side face " << side[i] << " is not planar for solid: "
382             << GetName() << "\nDiscrepancy: "     400             << GetName() << "\nDiscrepancy: " << dmax/mm << " mm\n";
383     StreamInfo(message);                          401     StreamInfo(message);
384     G4Exception("G4Trap::MakePlanes()", "GeomS    402     G4Exception("G4Trap::MakePlanes()", "GeomSolids0002",
385                 FatalException, message);         403                 FatalException, message);
386   }                                               404   }
387                                                   405 
388   // Re-compute parameters                     << 406   // Define type of trapezoid
389   SetCachedValues();                           << 407   fTrapType = 0;
                                                   >> 408   if (fPlanes[0].b  == -1 && fPlanes[1].b == 1 &&
                                                   >> 409       std::abs(fPlanes[0].a) < DBL_EPSILON &&
                                                   >> 410       std::abs(fPlanes[0].c) < DBL_EPSILON &&
                                                   >> 411       std::abs(fPlanes[1].a) < DBL_EPSILON &&
                                                   >> 412       std::abs(fPlanes[1].c) < DBL_EPSILON)
                                                   >> 413   {
                                                   >> 414     fTrapType = 1; // YZ section is a rectangle ...
                                                   >> 415     if (std::abs(fPlanes[2].a + fPlanes[3].a) < DBL_EPSILON &&
                                                   >> 416         std::abs(fPlanes[2].c - fPlanes[3].c) < DBL_EPSILON &&
                                                   >> 417         fPlanes[2].b == 0 &&
                                                   >> 418         fPlanes[3].b == 0)
                                                   >> 419     {
                                                   >> 420       fTrapType = 2; // ... and XZ section is a isosceles trapezoid
                                                   >> 421       fPlanes[2].a = -fPlanes[3].a;
                                                   >> 422       fPlanes[2].c =  fPlanes[3].c;
                                                   >> 423     }
                                                   >> 424     if (std::abs(fPlanes[2].a + fPlanes[3].a) < DBL_EPSILON &&
                                                   >> 425         std::abs(fPlanes[2].b - fPlanes[3].b) < DBL_EPSILON &&
                                                   >> 426         fPlanes[2].c == 0 &&
                                                   >> 427         fPlanes[3].c == 0)
                                                   >> 428     {
                                                   >> 429       fTrapType = 3; // ... and XY section is a isosceles trapezoid
                                                   >> 430       fPlanes[2].a = -fPlanes[3].a;
                                                   >> 431       fPlanes[2].b =  fPlanes[3].b;
                                                   >> 432     }
                                                   >> 433   }
390 }                                                 434 }
391                                                   435 
392 ////////////////////////////////////////////// << 436 ///////////////////////////////////////////////////////////////////////
393 //                                                437 //
394 // Calculate the coef's of the plane p1->p2->p    438 // Calculate the coef's of the plane p1->p2->p3->p4->p1
395 // where the ThreeVectors 1-4 are in anti-cloc    439 // where the ThreeVectors 1-4 are in anti-clockwise order when viewed
396 // from infront of the plane (i.e. from normal    440 // from infront of the plane (i.e. from normal direction).
397 //                                                441 //
398 // Return true if the points are coplanar, fal    442 // Return true if the points are coplanar, false otherwise
399                                                   443 
400 G4bool G4Trap::MakePlane( const G4ThreeVector&    444 G4bool G4Trap::MakePlane( const G4ThreeVector& p1,
401                           const G4ThreeVector&    445                           const G4ThreeVector& p2,
402                           const G4ThreeVector&    446                           const G4ThreeVector& p3,
403                           const G4ThreeVector&    447                           const G4ThreeVector& p4,
404                                 TrapSidePlane&    448                                 TrapSidePlane& plane )
405 {                                                 449 {
406   G4ThreeVector normal = ((p4 - p2).cross(p3 -    450   G4ThreeVector normal = ((p4 - p2).cross(p3 - p1)).unit();
407   if (std::abs(normal.x()) < DBL_EPSILON) norm << 451   if (std::abs(normal.x()) < DBL_EPSILON) normal.setX(0); 
408   if (std::abs(normal.y()) < DBL_EPSILON) norm << 452   if (std::abs(normal.y()) < DBL_EPSILON) normal.setY(0); 
409   if (std::abs(normal.z()) < DBL_EPSILON) norm << 453   if (std::abs(normal.z()) < DBL_EPSILON) normal.setZ(0); 
410   normal = normal.unit();                         454   normal = normal.unit();
411                                                   455 
412   G4ThreeVector centre = (p1 + p2 + p3 + p4)*0    456   G4ThreeVector centre = (p1 + p2 + p3 + p4)*0.25;
413   plane.a =  normal.x();                          457   plane.a =  normal.x();
414   plane.b =  normal.y();                          458   plane.b =  normal.y();
415   plane.c =  normal.z();                          459   plane.c =  normal.z();
416   plane.d = -normal.dot(centre);                  460   plane.d = -normal.dot(centre);
417                                                   461 
418   // compute distances and check planarity        462   // compute distances and check planarity
419   G4double d1 = std::abs(normal.dot(p1) + plan    463   G4double d1 = std::abs(normal.dot(p1) + plane.d);
420   G4double d2 = std::abs(normal.dot(p2) + plan    464   G4double d2 = std::abs(normal.dot(p2) + plane.d);
421   G4double d3 = std::abs(normal.dot(p3) + plan    465   G4double d3 = std::abs(normal.dot(p3) + plane.d);
422   G4double d4 = std::abs(normal.dot(p4) + plan    466   G4double d4 = std::abs(normal.dot(p4) + plane.d);
423   G4double dmax = std::max(std::max(std::max(d    467   G4double dmax = std::max(std::max(std::max(d1,d2),d3),d4);
424                                                << 468   
425   return dmax <= 1000 * kCarTolerance;         << 469   return (dmax > 1000 * kCarTolerance) ? false : true;
426 }                                                 470 }
427                                                   471 
428 ////////////////////////////////////////////// << 472 ///////////////////////////////////////////////////////////////////////
429 //                                             << 
430 // Recompute parameters using planes           << 
431                                                << 
432 void G4Trap::SetCachedValues()                 << 
433 {                                              << 
434   // Set indeces                               << 
435   constexpr  G4int iface[6][4] =               << 
436       { {0,1,3,2}, {0,4,5,1}, {2,3,7,6}, {0,2, << 
437                                                << 
438   // Get vertices                              << 
439   G4ThreeVector pt[8];                         << 
440   GetVertices(pt);                             << 
441                                                << 
442   // Set face areas                            << 
443   for (G4int i=0; i<6; ++i)                    << 
444   {                                            << 
445     fAreas[i] = G4GeomTools::QuadAreaNormal(pt << 
446                                             pt << 
447                                             pt << 
448                                             pt << 
449   }                                            << 
450   for (G4int i=1; i<6; ++i) { fAreas[i] += fAr << 
451                                                << 
452   // Define type of trapezoid                  << 
453   fTrapType = 0;                               << 
454   if (fPlanes[0].b  == -1 && fPlanes[1].b == 1 << 
455       std::abs(fPlanes[0].a) < DBL_EPSILON &&  << 
456       std::abs(fPlanes[0].c) < DBL_EPSILON &&  << 
457       std::abs(fPlanes[1].a) < DBL_EPSILON &&  << 
458       std::abs(fPlanes[1].c) < DBL_EPSILON)    << 
459   {                                            << 
460     fTrapType = 1; // YZ section is a rectangl << 
461     if (std::abs(fPlanes[2].a + fPlanes[3].a)  << 
462         std::abs(fPlanes[2].c - fPlanes[3].c)  << 
463         fPlanes[2].b == 0 &&                   << 
464         fPlanes[3].b == 0)                     << 
465     {                                          << 
466       fTrapType = 2; // ... and XZ section is  << 
467       fPlanes[2].a = -fPlanes[3].a;            << 
468       fPlanes[2].c =  fPlanes[3].c;            << 
469     }                                          << 
470     if (std::abs(fPlanes[2].a + fPlanes[3].a)  << 
471         std::abs(fPlanes[2].b - fPlanes[3].b)  << 
472         fPlanes[2].c == 0 &&                   << 
473         fPlanes[3].c == 0)                     << 
474     {                                          << 
475       fTrapType = 3; // ... and XY section is  << 
476       fPlanes[2].a = -fPlanes[3].a;            << 
477       fPlanes[2].b =  fPlanes[3].b;            << 
478     }                                          << 
479   }                                            << 
480 }                                              << 
481                                                << 
482 ////////////////////////////////////////////// << 
483 //                                                473 //
484 // Get volume                                     474 // Get volume
485                                                   475 
486 G4double G4Trap::GetCubicVolume()                 476 G4double G4Trap::GetCubicVolume()
487 {                                                 477 {
488   if (fCubicVolume == 0)                          478   if (fCubicVolume == 0)
489   {                                               479   {
490     G4ThreeVector pt[8];                          480     G4ThreeVector pt[8];
491     GetVertices(pt);                              481     GetVertices(pt);
492                                                << 482  
493     G4double dz  = pt[4].z() - pt[0].z();         483     G4double dz  = pt[4].z() - pt[0].z();
494     G4double dy1 = pt[2].y() - pt[0].y();         484     G4double dy1 = pt[2].y() - pt[0].y();
495     G4double dx1 = pt[1].x() - pt[0].x();         485     G4double dx1 = pt[1].x() - pt[0].x();
496     G4double dx2 = pt[3].x() - pt[2].x();         486     G4double dx2 = pt[3].x() - pt[2].x();
497     G4double dy2 = pt[6].y() - pt[4].y();         487     G4double dy2 = pt[6].y() - pt[4].y();
498     G4double dx3 = pt[5].x() - pt[4].x();         488     G4double dx3 = pt[5].x() - pt[4].x();
499     G4double dx4 = pt[7].x() - pt[6].x();         489     G4double dx4 = pt[7].x() - pt[6].x();
500                                                   490 
501     fCubicVolume = ((dx1 + dx2 + dx3 + dx4)*(d    491     fCubicVolume = ((dx1 + dx2 + dx3 + dx4)*(dy1 + dy2) +
502                     (dx4 + dx3 - dx2 - dx1)*(d    492                     (dx4 + dx3 - dx2 - dx1)*(dy2 - dy1)/3)*dz*0.125;
503   }                                               493   }
504   return fCubicVolume;                            494   return fCubicVolume;
505 }                                                 495 }
506                                                   496 
507 ////////////////////////////////////////////// << 497 ///////////////////////////////////////////////////////////////////////
508 //                                                498 //
509 // Get surface area                               499 // Get surface area
510                                                   500 
511 G4double G4Trap::GetSurfaceArea()                 501 G4double G4Trap::GetSurfaceArea()
512 {                                                 502 {
513   if (fSurfaceArea == 0)                          503   if (fSurfaceArea == 0)
514   {                                               504   {
515     G4ThreeVector pt[8];                          505     G4ThreeVector pt[8];
516     G4int iface [6][4] =                          506     G4int iface [6][4] =
517       { {0,1,3,2}, {0,4,5,1}, {2,3,7,6}, {0,2,    507       { {0,1,3,2}, {0,4,5,1}, {2,3,7,6}, {0,2,6,4}, {1,5,7,3}, {4,6,7,5} };
518                                                   508 
519     GetVertices(pt);                              509     GetVertices(pt);
520     for (const auto & i : iface)               << 510     for (G4int i=0; i<6; ++i)
521     {                                             511     {
522       fSurfaceArea += G4GeomTools::QuadAreaNor << 512       fSurfaceArea += G4GeomTools::QuadAreaNormal(pt[iface[i][0]],
523                                                << 513                                                   pt[iface[i][1]],
524                                                << 514                                                   pt[iface[i][2]],
525                                                << 515                                                   pt[iface[i][3]]).mag();
526     }                                             516     }
527   }                                               517   }
528   return fSurfaceArea;                            518   return fSurfaceArea;
529 }                                                 519 }
530                                                   520 
531 ////////////////////////////////////////////// << 521 ///////////////////////////////////////////////////////////////////////
532 //                                                522 //
533 // Dispatch to parameterisation for replicatio    523 // Dispatch to parameterisation for replication mechanism dimension
534 // computation & modification.                    524 // computation & modification.
535                                                   525 
536 void G4Trap::ComputeDimensions(       G4VPVPar    526 void G4Trap::ComputeDimensions(       G4VPVParameterisation* p,
537                                 const G4int n,    527                                 const G4int n,
538                                 const G4VPhysi    528                                 const G4VPhysicalVolume* pRep )
539 {                                                 529 {
540   p->ComputeDimensions(*this,n,pRep);             530   p->ComputeDimensions(*this,n,pRep);
541 }                                                 531 }
542                                                   532 
543 ////////////////////////////////////////////// << 533 ///////////////////////////////////////////////////////////////////////
544 //                                                534 //
545 // Get bounding box                               535 // Get bounding box
546                                                   536 
547 void G4Trap::BoundingLimits(G4ThreeVector& pMi    537 void G4Trap::BoundingLimits(G4ThreeVector& pMin, G4ThreeVector& pMax) const
548 {                                                 538 {
549   G4ThreeVector pt[8];                            539   G4ThreeVector pt[8];
550   GetVertices(pt);                                540   GetVertices(pt);
551                                                   541 
552   G4double xmin = kInfinity, xmax = -kInfinity    542   G4double xmin = kInfinity, xmax = -kInfinity;
553   G4double ymin = kInfinity, ymax = -kInfinity    543   G4double ymin = kInfinity, ymax = -kInfinity;
554   for (const auto & i : pt)                    << 544   for (G4int i=0; i<8; ++i)
555   {                                               545   {
556     G4double x = i.x();                        << 546     G4double x = pt[i].x();
557     if (x < xmin) xmin = x;                       547     if (x < xmin) xmin = x;
558     if (x > xmax) xmax = x;                       548     if (x > xmax) xmax = x;
559     G4double y = i.y();                        << 549     G4double y = pt[i].y();
560     if (y < ymin) ymin = y;                       550     if (y < ymin) ymin = y;
561     if (y > ymax) ymax = y;                       551     if (y > ymax) ymax = y;
562   }                                               552   }
563                                                   553 
564   G4double dz   = GetZHalfLength();               554   G4double dz   = GetZHalfLength();
565   pMin.set(xmin,ymin,-dz);                        555   pMin.set(xmin,ymin,-dz);
566   pMax.set(xmax,ymax, dz);                        556   pMax.set(xmax,ymax, dz);
567                                                   557 
568   // Check correctness of the bounding box        558   // Check correctness of the bounding box
569   //                                              559   //
570   if (pMin.x() >= pMax.x() || pMin.y() >= pMax    560   if (pMin.x() >= pMax.x() || pMin.y() >= pMax.y() || pMin.z() >= pMax.z())
571   {                                               561   {
572     std::ostringstream message;                   562     std::ostringstream message;
573     message << "Bad bounding box (min >= max)     563     message << "Bad bounding box (min >= max) for solid: "
574             << GetName() << " !"                  564             << GetName() << " !"
575             << "\npMin = " << pMin                565             << "\npMin = " << pMin
576             << "\npMax = " << pMax;               566             << "\npMax = " << pMax;
577     G4Exception("G4Trap::BoundingLimits()", "G    567     G4Exception("G4Trap::BoundingLimits()", "GeomMgt0001",
578                 JustWarning, message);            568                 JustWarning, message);
579     DumpInfo();                                   569     DumpInfo();
580   }                                               570   }
581 }                                                 571 }
582                                                   572 
583 ////////////////////////////////////////////// << 573 ///////////////////////////////////////////////////////////////////////
584 //                                                574 //
585 // Calculate extent under transform and specif    575 // Calculate extent under transform and specified limit
586                                                   576 
587 G4bool G4Trap::CalculateExtent( const EAxis pA    577 G4bool G4Trap::CalculateExtent( const EAxis pAxis,
588                                 const G4VoxelL    578                                 const G4VoxelLimits& pVoxelLimit,
589                                 const G4Affine    579                                 const G4AffineTransform& pTransform,
590                                       G4double    580                                       G4double& pMin, G4double& pMax) const
591 {                                                 581 {
592   G4ThreeVector bmin, bmax;                       582   G4ThreeVector bmin, bmax;
593   G4bool exist;                                   583   G4bool exist;
594                                                   584 
595   // Check bounding box (bbox)                    585   // Check bounding box (bbox)
596   //                                              586   //
597   BoundingLimits(bmin,bmax);                      587   BoundingLimits(bmin,bmax);
598   G4BoundingEnvelope bbox(bmin,bmax);             588   G4BoundingEnvelope bbox(bmin,bmax);
599 #ifdef G4BBOX_EXTENT                              589 #ifdef G4BBOX_EXTENT
600   return bbox.CalculateExtent(pAxis,pVoxelLimi << 590   if (true) return bbox.CalculateExtent(pAxis,pVoxelLimit,pTransform,pMin,pMax);
601 #endif                                            591 #endif
602   if (bbox.BoundingBoxVsVoxelLimits(pAxis,pVox    592   if (bbox.BoundingBoxVsVoxelLimits(pAxis,pVoxelLimit,pTransform,pMin,pMax))
603   {                                               593   {
604     return exist = pMin < pMax;                << 594     return exist = (pMin < pMax) ? true : false;
605   }                                               595   }
606                                                   596 
607   // Set bounding envelope (benv) and calculat    597   // Set bounding envelope (benv) and calculate extent
608   //                                              598   //
609   G4ThreeVector pt[8];                            599   G4ThreeVector pt[8];
610   GetVertices(pt);                                600   GetVertices(pt);
611                                                   601 
612   G4ThreeVectorList baseA(4), baseB(4);           602   G4ThreeVectorList baseA(4), baseB(4);
613   baseA[0] = pt[0];                               603   baseA[0] = pt[0];
614   baseA[1] = pt[1];                               604   baseA[1] = pt[1];
615   baseA[2] = pt[3];                               605   baseA[2] = pt[3];
616   baseA[3] = pt[2];                               606   baseA[3] = pt[2];
617                                                   607 
618   baseB[0] = pt[4];                               608   baseB[0] = pt[4];
619   baseB[1] = pt[5];                               609   baseB[1] = pt[5];
620   baseB[2] = pt[7];                               610   baseB[2] = pt[7];
621   baseB[3] = pt[6];                               611   baseB[3] = pt[6];
622                                                   612 
623   std::vector<const G4ThreeVectorList *> polyg    613   std::vector<const G4ThreeVectorList *> polygons(2);
624   polygons[0] = &baseA;                           614   polygons[0] = &baseA;
625   polygons[1] = &baseB;                           615   polygons[1] = &baseB;
626                                                   616 
627   G4BoundingEnvelope benv(bmin,bmax,polygons);    617   G4BoundingEnvelope benv(bmin,bmax,polygons);
628   exist = benv.CalculateExtent(pAxis,pVoxelLim    618   exist = benv.CalculateExtent(pAxis,pVoxelLimit,pTransform,pMin,pMax);
629   return exist;                                   619   return exist;
630 }                                                 620 }
631                                                   621 
632 ////////////////////////////////////////////// << 622 ///////////////////////////////////////////////////////////////////////
633 //                                                623 //
634 // Return whether point is inside/outside/on_s    624 // Return whether point is inside/outside/on_surface
635                                                   625 
636 EInside G4Trap::Inside( const G4ThreeVector& p    626 EInside G4Trap::Inside( const G4ThreeVector& p ) const
637 {                                                 627 {
                                                   >> 628   G4double dz = std::abs(p.z())-fDz;
                                                   >> 629   if (dz > halfCarTolerance) return kOutside;
                                                   >> 630 
638   switch (fTrapType)                              631   switch (fTrapType)
639   {                                               632   {
640     case 0: // General case                       633     case 0: // General case
641     {                                             634     {
642       G4double dz = std::abs(p.z())-fDz;       << 
643       G4double dy1 = fPlanes[0].b*p.y()+fPlane    635       G4double dy1 = fPlanes[0].b*p.y()+fPlanes[0].c*p.z()+fPlanes[0].d;
644       G4double dy2 = fPlanes[1].b*p.y()+fPlane    636       G4double dy2 = fPlanes[1].b*p.y()+fPlanes[1].c*p.z()+fPlanes[1].d;
645       G4double dy = std::max(dz,std::max(dy1,d    637       G4double dy = std::max(dz,std::max(dy1,dy2));
646                                                   638 
647       G4double dx1 = fPlanes[2].a*p.x()+fPlane    639       G4double dx1 = fPlanes[2].a*p.x()+fPlanes[2].b*p.y()
648                    + fPlanes[2].c*p.z()+fPlane    640                    + fPlanes[2].c*p.z()+fPlanes[2].d;
649       G4double dx2 = fPlanes[3].a*p.x()+fPlane    641       G4double dx2 = fPlanes[3].a*p.x()+fPlanes[3].b*p.y()
650                    + fPlanes[3].c*p.z()+fPlane    642                    + fPlanes[3].c*p.z()+fPlanes[3].d;
651       G4double dist = std::max(dy,std::max(dx1    643       G4double dist = std::max(dy,std::max(dx1,dx2));
652                                                   644 
653       return (dist > halfCarTolerance) ? kOuts << 645       if (dist > halfCarTolerance) return kOutside;
654         ((dist > -halfCarTolerance) ? kSurface << 646       return (dist > -halfCarTolerance) ? kSurface : kInside;
655     }                                             647     }
656     case 1: // YZ section is a rectangle          648     case 1: // YZ section is a rectangle
657     {                                             649     {
658       G4double dz = std::abs(p.z())-fDz;       << 
659       G4double dy = std::max(dz,std::abs(p.y()    650       G4double dy = std::max(dz,std::abs(p.y())+fPlanes[1].d);
660       G4double dx1 = fPlanes[2].a*p.x()+fPlane    651       G4double dx1 = fPlanes[2].a*p.x()+fPlanes[2].b*p.y()
661                    + fPlanes[2].c*p.z()+fPlane    652                    + fPlanes[2].c*p.z()+fPlanes[2].d;
662       G4double dx2 = fPlanes[3].a*p.x()+fPlane    653       G4double dx2 = fPlanes[3].a*p.x()+fPlanes[3].b*p.y()
663                    + fPlanes[3].c*p.z()+fPlane    654                    + fPlanes[3].c*p.z()+fPlanes[3].d;
664       G4double dist = std::max(dy,std::max(dx1    655       G4double dist = std::max(dy,std::max(dx1,dx2));
665                                                   656 
666       return (dist > halfCarTolerance) ? kOuts << 657       if (dist > halfCarTolerance) return kOutside;
667         ((dist > -halfCarTolerance) ? kSurface << 658       return (dist > -halfCarTolerance) ? kSurface : kInside;
668     }                                             659     }
669     case 2: // YZ section is a rectangle and      660     case 2: // YZ section is a rectangle and
670     {       // XZ section is an isosceles trap    661     {       // XZ section is an isosceles trapezoid
671       G4double dz = std::abs(p.z())-fDz;       << 
672       G4double dy = std::max(dz,std::abs(p.y()    662       G4double dy = std::max(dz,std::abs(p.y())+fPlanes[1].d);
673       G4double dx = fPlanes[3].a*std::abs(p.x(    663       G4double dx = fPlanes[3].a*std::abs(p.x())
674                   + fPlanes[3].c*p.z()+fPlanes    664                   + fPlanes[3].c*p.z()+fPlanes[3].d;
675       G4double dist = std::max(dy,dx);            665       G4double dist = std::max(dy,dx);
676                                                   666 
677       return (dist > halfCarTolerance) ? kOuts << 667       if (dist > halfCarTolerance) return kOutside;
678         ((dist > -halfCarTolerance) ? kSurface << 668       return (dist > -halfCarTolerance) ? kSurface : kInside;
679     }                                             669     }
680     case 3: // YZ section is a rectangle and      670     case 3: // YZ section is a rectangle and
681     {       // XY section is an isosceles trap    671     {       // XY section is an isosceles trapezoid
682       G4double dz = std::abs(p.z())-fDz;       << 
683       G4double dy = std::max(dz,std::abs(p.y()    672       G4double dy = std::max(dz,std::abs(p.y())+fPlanes[1].d);
684       G4double dx = fPlanes[3].a*std::abs(p.x(    673       G4double dx = fPlanes[3].a*std::abs(p.x())
685                   + fPlanes[3].b*p.y()+fPlanes    674                   + fPlanes[3].b*p.y()+fPlanes[3].d;
686       G4double dist = std::max(dy,dx);            675       G4double dist = std::max(dy,dx);
687                                                   676 
688       return (dist > halfCarTolerance) ? kOuts << 677       if (dist > halfCarTolerance) return kOutside;
689         ((dist > -halfCarTolerance) ? kSurface << 678       return (dist > -halfCarTolerance) ? kSurface : kInside;
690     }                                             679     }
691   }                                               680   }
692   return kOutside;                             << 681   return kOutside; 
693 }                                                 682 }
694                                                   683 
695 ////////////////////////////////////////////// << 684 ///////////////////////////////////////////////////////////////////////
696 //                                                685 //
697 // Determine side, and return corresponding no    686 // Determine side, and return corresponding normal
698                                                   687 
699 G4ThreeVector G4Trap::SurfaceNormal( const G4T    688 G4ThreeVector G4Trap::SurfaceNormal( const G4ThreeVector& p ) const
700 {                                                 689 {
                                                   >> 690   G4int nsurf = 0; // number of surfaces where p is placed
701   G4double nx = 0, ny = 0, nz = 0;                691   G4double nx = 0, ny = 0, nz = 0;
702   G4double dz = std::abs(p.z()) - fDz;            692   G4double dz = std::abs(p.z()) - fDz;
703   nz = std::copysign(G4double(std::abs(dz) <=  << 693   if (std::abs(dz) <= halfCarTolerance)
                                                   >> 694   {
                                                   >> 695     nz = (p.z() < 0) ? -1 : 1;
                                                   >> 696     ++nsurf;
                                                   >> 697   }
704                                                   698 
705   switch (fTrapType)                              699   switch (fTrapType)
706   {                                               700   {
707     case 0: // General case                       701     case 0: // General case
708     {                                             702     {
709       for (G4int i=0; i<2; ++i)                   703       for (G4int i=0; i<2; ++i)
710       {                                           704       {
711         G4double dy = fPlanes[i].b*p.y() + fPl    705         G4double dy = fPlanes[i].b*p.y() + fPlanes[i].c*p.z() + fPlanes[i].d;
712         if (std::abs(dy) > halfCarTolerance) c    706         if (std::abs(dy) > halfCarTolerance) continue;
713         ny  = fPlanes[i].b;                       707         ny  = fPlanes[i].b;
714         nz += fPlanes[i].c;                       708         nz += fPlanes[i].c;
                                                   >> 709         ++nsurf;
715         break;                                    710         break;
716       }                                           711       }
717       for (G4int i=2; i<4; ++i)                   712       for (G4int i=2; i<4; ++i)
718       {                                           713       {
719         G4double dx = fPlanes[i].a*p.x() +        714         G4double dx = fPlanes[i].a*p.x() +
720                       fPlanes[i].b*p.y() + fPl    715                       fPlanes[i].b*p.y() + fPlanes[i].c*p.z() + fPlanes[i].d;
721         if (std::abs(dx) > halfCarTolerance) c    716         if (std::abs(dx) > halfCarTolerance) continue;
722         nx  = fPlanes[i].a;                       717         nx  = fPlanes[i].a;
723         ny += fPlanes[i].b;                       718         ny += fPlanes[i].b;
724         nz += fPlanes[i].c;                       719         nz += fPlanes[i].c;
                                                   >> 720         ++nsurf;
725         break;                                    721         break;
726       }                                           722       }
727       break;                                      723       break;
728     }                                             724     }
729     case 1: // YZ section - rectangle          << 725     case 1: // YZ section is a rectangle
730     {                                             726     {
731       G4double dy = std::abs(p.y()) + fPlanes[    727       G4double dy = std::abs(p.y()) + fPlanes[1].d;
732       ny = std::copysign(G4double(std::abs(dy) << 728       if (std::abs(dy) <= halfCarTolerance) ny = (p.y() < 0) ? -1 : 1;
733       for (G4int i=2; i<4; ++i)                   729       for (G4int i=2; i<4; ++i)
734       {                                           730       {
735         G4double dx = fPlanes[i].a*p.x() +        731         G4double dx = fPlanes[i].a*p.x() +
736                       fPlanes[i].b*p.y() + fPl    732                       fPlanes[i].b*p.y() + fPlanes[i].c*p.z() + fPlanes[i].d;
737         if (std::abs(dx) > halfCarTolerance) c    733         if (std::abs(dx) > halfCarTolerance) continue;
738         nx  = fPlanes[i].a;                       734         nx  = fPlanes[i].a;
739         ny += fPlanes[i].b;                       735         ny += fPlanes[i].b;
740         nz += fPlanes[i].c;                       736         nz += fPlanes[i].c;
                                                   >> 737         ++nsurf;
741         break;                                    738         break;
742       }                                           739       }
743       break;                                      740       break;
744     }                                             741     }
745     case 2: // YZ section - rectangle, XZ sect << 742     case 2: // YZ section is a rectangle and
746     {                                          << 743     {       // XZ section is an isosceles trapezoid
747       G4double dy = std::abs(p.y()) + fPlanes[    744       G4double dy = std::abs(p.y()) + fPlanes[1].d;
748       ny = std::copysign(G4double(std::abs(dy) << 745       if (std::abs(dy) <= halfCarTolerance) ny = (p.y() < 0) ? -1 : 1;
749       G4double dx = fPlanes[3].a*std::abs(p.x(    746       G4double dx = fPlanes[3].a*std::abs(p.x()) +
750                     fPlanes[3].c*p.z() + fPlan    747                     fPlanes[3].c*p.z() + fPlanes[3].d;
751       G4double k = std::abs(dx) <= halfCarTole << 748       if (std::abs(dx) <= halfCarTolerance)
752       nx  = std::copysign(k, p.x())*fPlanes[3] << 749       {
753       nz += k*fPlanes[3].c;                    << 750         nx  = (p.x() < 0) ? -fPlanes[3].a : fPlanes[3].a;
                                                   >> 751         nz += fPlanes[3].c;
                                                   >> 752         ++nsurf;
                                                   >> 753       }
754       break;                                      754       break;
755     }                                             755     }
756     case 3: // YZ section - rectangle, XY sect << 756     case 3: // YZ section is a rectangle and
757     {                                          << 757     {       // XY section is an isosceles trapezoid
758       G4double dy = std::abs(p.y()) + fPlanes[    758       G4double dy = std::abs(p.y()) + fPlanes[1].d;
759       ny = std::copysign(G4double(std::abs(dy) << 759       if (std::abs(dy) <= halfCarTolerance) ny = (p.y() < 0) ? -1 : 1;
760       G4double dx = fPlanes[3].a*std::abs(p.x(    760       G4double dx = fPlanes[3].a*std::abs(p.x()) +
761                     fPlanes[3].b*p.y() + fPlan    761                     fPlanes[3].b*p.y() + fPlanes[3].d;
762       G4double k = std::abs(dx) <= halfCarTole << 762       if (std::abs(dx) <= halfCarTolerance)
763       nx  = std::copysign(k, p.x())*fPlanes[3] << 763       {
764       ny += k*fPlanes[3].b;                    << 764         nx  = (p.x() < 0) ? -fPlanes[3].a : fPlanes[3].a;
                                                   >> 765         ny += fPlanes[3].b;
                                                   >> 766         ++nsurf;
                                                   >> 767       }
765       break;                                      768       break;
766     }                                             769     }
767   }                                               770   }
768                                                   771 
769   // Return normal                                772   // Return normal
770   //                                              773   //
771   G4double mag2 = nx*nx + ny*ny + nz*nz;       << 774   if (nsurf == 1)      return G4ThreeVector(nx,ny,nz);
772   if (mag2 == 1)      return { nx,ny,nz };     << 775   else if (nsurf != 0) return G4ThreeVector(nx,ny,nz).unit(); // edge or corner
773   else if (mag2 != 0) return G4ThreeVector(nx, << 
774   else                                            776   else
775   {                                               777   {
776     // Point is not on the surface                778     // Point is not on the surface
777     //                                            779     //
778 #ifdef G4CSGDEBUG                                 780 #ifdef G4CSGDEBUG
779     std::ostringstream message;                   781     std::ostringstream message;
780     G4long oldprc = message.precision(16);     << 782     G4int oldprc = message.precision(16);
781     message << "Point p is not on surface (!?)    783     message << "Point p is not on surface (!?) of solid: "
782             << GetName() << G4endl;               784             << GetName() << G4endl;
783     message << "Position:\n";                     785     message << "Position:\n";
784     message << "   p.x() = " << p.x()/mm << "     786     message << "   p.x() = " << p.x()/mm << " mm\n";
785     message << "   p.y() = " << p.y()/mm << "     787     message << "   p.y() = " << p.y()/mm << " mm\n";
786     message << "   p.z() = " << p.z()/mm << "     788     message << "   p.z() = " << p.z()/mm << " mm";
787     G4cout.precision(oldprc) ;                    789     G4cout.precision(oldprc) ;
788     G4Exception("G4Trap::SurfaceNormal(p)", "G    790     G4Exception("G4Trap::SurfaceNormal(p)", "GeomSolids1002",
789                 JustWarning, message );           791                 JustWarning, message );
790     DumpInfo();                                   792     DumpInfo();
791 #endif                                            793 #endif
792     return ApproxSurfaceNormal(p);                794     return ApproxSurfaceNormal(p);
793   }                                               795   }
794 }                                                 796 }
795                                                   797 
796 ////////////////////////////////////////////// << 798 ///////////////////////////////////////////////////////////////////////
797 //                                                799 //
798 // Algorithm for SurfaceNormal() following the    800 // Algorithm for SurfaceNormal() following the original specification
799 // for points not on the surface                  801 // for points not on the surface
800                                                   802 
801 G4ThreeVector G4Trap::ApproxSurfaceNormal( con    803 G4ThreeVector G4Trap::ApproxSurfaceNormal( const G4ThreeVector& p ) const
802 {                                                 804 {
803   G4double dist = -DBL_MAX;                       805   G4double dist = -DBL_MAX;
804   G4int iside = 0;                                806   G4int iside = 0;
805   for (G4int i=0; i<4; ++i)                       807   for (G4int i=0; i<4; ++i)
806   {                                               808   {
807     G4double d = fPlanes[i].a*p.x() +             809     G4double d = fPlanes[i].a*p.x() +
808                  fPlanes[i].b*p.y() +             810                  fPlanes[i].b*p.y() +
809                  fPlanes[i].c*p.z() + fPlanes[    811                  fPlanes[i].c*p.z() + fPlanes[i].d;
810     if (d > dist) { dist = d; iside = i; }        812     if (d > dist) { dist = d; iside = i; }
811   }                                               813   }
812                                                   814 
813   G4double distz = std::abs(p.z()) - fDz;         815   G4double distz = std::abs(p.z()) - fDz;
814   if (dist > distz)                               816   if (dist > distz)
815     return { fPlanes[iside].a, fPlanes[iside]. << 817     return G4ThreeVector(fPlanes[iside].a, fPlanes[iside].b, fPlanes[iside].c);
816   else                                            818   else
817     return { 0, 0, (G4double)((p.z() < 0) ? -1 << 819     return G4ThreeVector(0, 0, (p.z() < 0) ? -1 : 1);
818 }                                                 820 }
819                                                   821 
820 ////////////////////////////////////////////// << 822 ///////////////////////////////////////////////////////////////////////
821 //                                                823 //
822 // Calculate distance to shape from outside       824 // Calculate distance to shape from outside
823 //  - return kInfinity if no intersection         825 //  - return kInfinity if no intersection
824                                                   826 
825 G4double G4Trap::DistanceToIn(const G4ThreeVec    827 G4double G4Trap::DistanceToIn(const G4ThreeVector& p,
826                               const G4ThreeVec    828                               const G4ThreeVector& v ) const
827 {                                                 829 {
828   // Z intersections                              830   // Z intersections
829   //                                              831   //
830   if ((std::abs(p.z()) - fDz) >= -halfCarToler    832   if ((std::abs(p.z()) - fDz) >= -halfCarTolerance && p.z()*v.z() >= 0)
831     return kInfinity;                             833     return kInfinity;
832   G4double invz = (-v.z() == 0) ? DBL_MAX : -1    834   G4double invz = (-v.z() == 0) ? DBL_MAX : -1./v.z();
833   G4double dz = (invz < 0) ? fDz : -fDz;       << 835   G4double dz = (invz < 0) ? fDz : -fDz; 
834   G4double tzmin = (p.z() + dz)*invz;             836   G4double tzmin = (p.z() + dz)*invz;
835   G4double tzmax = (p.z() - dz)*invz;             837   G4double tzmax = (p.z() - dz)*invz;
836                                                   838 
837   // Y intersections                              839   // Y intersections
838   //                                              840   //
839   G4double tymin = 0, tymax = DBL_MAX;            841   G4double tymin = 0, tymax = DBL_MAX;
840   G4int i = 0;                                    842   G4int i = 0;
841   for ( ; i<2; ++i)                               843   for ( ; i<2; ++i)
842   {                                            << 844   { 
843     G4double cosa = fPlanes[i].b*v.y() + fPlan    845     G4double cosa = fPlanes[i].b*v.y() + fPlanes[i].c*v.z();
844     G4double dist = fPlanes[i].b*p.y() + fPlan    846     G4double dist = fPlanes[i].b*p.y() + fPlanes[i].c*p.z() + fPlanes[i].d;
845     if (dist >= -halfCarTolerance)                847     if (dist >= -halfCarTolerance)
846     {                                             848     {
847       if (cosa >= 0) return kInfinity;            849       if (cosa >= 0) return kInfinity;
848       G4double tmp  = -dist/cosa;                 850       G4double tmp  = -dist/cosa;
849       if (tymin < tmp) tymin = tmp;               851       if (tymin < tmp) tymin = tmp;
850     }                                             852     }
851     else if (cosa > 0)                            853     else if (cosa > 0)
852     {                                             854     {
853       G4double tmp  = -dist/cosa;                 855       G4double tmp  = -dist/cosa;
854       if (tymax > tmp) tymax = tmp;               856       if (tymax > tmp) tymax = tmp;
855     }                                          << 857     } 
856   }                                               858   }
857                                                   859 
858   // Z intersections                              860   // Z intersections
859   //                                              861   //
860   G4double txmin = 0, txmax = DBL_MAX;            862   G4double txmin = 0, txmax = DBL_MAX;
861   for ( ; i<4; ++i)                               863   for ( ; i<4; ++i)
862   {                                            << 864   { 
863     G4double cosa = fPlanes[i].a*v.x()+fPlanes    865     G4double cosa = fPlanes[i].a*v.x()+fPlanes[i].b*v.y()+fPlanes[i].c*v.z();
864     G4double dist = fPlanes[i].a*p.x()+fPlanes    866     G4double dist = fPlanes[i].a*p.x()+fPlanes[i].b*p.y()+fPlanes[i].c*p.z() +
865                     fPlanes[i].d;                 867                     fPlanes[i].d;
866     if (dist >= -halfCarTolerance)                868     if (dist >= -halfCarTolerance)
867     {                                             869     {
868       if (cosa >= 0) return kInfinity;            870       if (cosa >= 0) return kInfinity;
869       G4double tmp  = -dist/cosa;                 871       G4double tmp  = -dist/cosa;
870       if (txmin < tmp) txmin = tmp;               872       if (txmin < tmp) txmin = tmp;
871     }                                             873     }
872     else if (cosa > 0)                            874     else if (cosa > 0)
873     {                                             875     {
874       G4double tmp  = -dist/cosa;                 876       G4double tmp  = -dist/cosa;
875       if (txmax > tmp) txmax = tmp;               877       if (txmax > tmp) txmax = tmp;
876     }                                          << 878     } 
877   }                                               879   }
878                                                   880 
879   // Find distance                                881   // Find distance
880   //                                              882   //
881   G4double tmin = std::max(std::max(txmin,tymi    883   G4double tmin = std::max(std::max(txmin,tymin),tzmin);
882   G4double tmax = std::min(std::min(txmax,tyma    884   G4double tmax = std::min(std::min(txmax,tymax),tzmax);
883                                                << 885      
884   if (tmax <= tmin + halfCarTolerance) return     886   if (tmax <= tmin + halfCarTolerance) return kInfinity; // touch or no hit
885   return (tmin < halfCarTolerance ) ? 0. : tmi    887   return (tmin < halfCarTolerance ) ? 0. : tmin;
886 }                                                 888 }
887                                                   889 
888 ////////////////////////////////////////////// << 890 ////////////////////////////////////////////////////////////////////////////
889 //                                                891 //
890 // Calculate exact shortest distance to any bo    892 // Calculate exact shortest distance to any boundary from outside
891 // This is the best fast estimation of the sho    893 // This is the best fast estimation of the shortest distance to trap
892 // - return 0 if point is inside                  894 // - return 0 if point is inside
893                                                   895 
894 G4double G4Trap::DistanceToIn( const G4ThreeVe    896 G4double G4Trap::DistanceToIn( const G4ThreeVector& p ) const
895 {                                                 897 {
896   switch (fTrapType)                              898   switch (fTrapType)
897   {                                               899   {
898     case 0: // General case                       900     case 0: // General case
899     {                                             901     {
900       G4double dz = std::abs(p.z())-fDz;          902       G4double dz = std::abs(p.z())-fDz;
901       G4double dy1 = fPlanes[0].b*p.y()+fPlane    903       G4double dy1 = fPlanes[0].b*p.y()+fPlanes[0].c*p.z()+fPlanes[0].d;
902       G4double dy2 = fPlanes[1].b*p.y()+fPlane    904       G4double dy2 = fPlanes[1].b*p.y()+fPlanes[1].c*p.z()+fPlanes[1].d;
903       G4double dy = std::max(dz,std::max(dy1,d    905       G4double dy = std::max(dz,std::max(dy1,dy2));
904                                                   906 
905       G4double dx1 = fPlanes[2].a*p.x()+fPlane    907       G4double dx1 = fPlanes[2].a*p.x()+fPlanes[2].b*p.y()
906                    + fPlanes[2].c*p.z()+fPlane    908                    + fPlanes[2].c*p.z()+fPlanes[2].d;
907       G4double dx2 = fPlanes[3].a*p.x()+fPlane    909       G4double dx2 = fPlanes[3].a*p.x()+fPlanes[3].b*p.y()
908                    + fPlanes[3].c*p.z()+fPlane    910                    + fPlanes[3].c*p.z()+fPlanes[3].d;
909       G4double dist = std::max(dy,std::max(dx1    911       G4double dist = std::max(dy,std::max(dx1,dx2));
910       return (dist > 0) ? dist : 0.;              912       return (dist > 0) ? dist : 0.;
911     }                                             913     }
912     case 1: // YZ section is a rectangle          914     case 1: // YZ section is a rectangle
913     {                                             915     {
914       G4double dz = std::abs(p.z())-fDz;          916       G4double dz = std::abs(p.z())-fDz;
915       G4double dy = std::max(dz,std::abs(p.y()    917       G4double dy = std::max(dz,std::abs(p.y())+fPlanes[1].d);
916       G4double dx1 = fPlanes[2].a*p.x()+fPlane    918       G4double dx1 = fPlanes[2].a*p.x()+fPlanes[2].b*p.y()
917                    + fPlanes[2].c*p.z()+fPlane    919                    + fPlanes[2].c*p.z()+fPlanes[2].d;
918       G4double dx2 = fPlanes[3].a*p.x()+fPlane    920       G4double dx2 = fPlanes[3].a*p.x()+fPlanes[3].b*p.y()
919                    + fPlanes[3].c*p.z()+fPlane    921                    + fPlanes[3].c*p.z()+fPlanes[3].d;
920       G4double dist = std::max(dy,std::max(dx1    922       G4double dist = std::max(dy,std::max(dx1,dx2));
921       return (dist > 0) ? dist : 0.;              923       return (dist > 0) ? dist : 0.;
922     }                                             924     }
923     case 2: // YZ section is a rectangle and      925     case 2: // YZ section is a rectangle and
924     {       // XZ section is an isosceles trap    926     {       // XZ section is an isosceles trapezoid
925       G4double dz = std::abs(p.z())-fDz;          927       G4double dz = std::abs(p.z())-fDz;
926       G4double dy = std::max(dz,std::abs(p.y()    928       G4double dy = std::max(dz,std::abs(p.y())+fPlanes[1].d);
927       G4double dx = fPlanes[3].a*std::abs(p.x(    929       G4double dx = fPlanes[3].a*std::abs(p.x())
928                   + fPlanes[3].c*p.z()+fPlanes    930                   + fPlanes[3].c*p.z()+fPlanes[3].d;
929       G4double dist = std::max(dy,dx);            931       G4double dist = std::max(dy,dx);
930       return (dist > 0) ? dist : 0.;              932       return (dist > 0) ? dist : 0.;
931     }                                             933     }
932     case 3: // YZ section is a rectangle and      934     case 3: // YZ section is a rectangle and
933     {       // XY section is an isosceles trap    935     {       // XY section is an isosceles trapezoid
934       G4double dz = std::abs(p.z())-fDz;          936       G4double dz = std::abs(p.z())-fDz;
935       G4double dy = std::max(dz,std::abs(p.y()    937       G4double dy = std::max(dz,std::abs(p.y())+fPlanes[1].d);
936       G4double dx = fPlanes[3].a*std::abs(p.x(    938       G4double dx = fPlanes[3].a*std::abs(p.x())
937                   + fPlanes[3].b*p.y()+fPlanes    939                   + fPlanes[3].b*p.y()+fPlanes[3].d;
938       G4double dist = std::max(dy,dx);            940       G4double dist = std::max(dy,dx);
939       return (dist > 0) ? dist : 0.;              941       return (dist > 0) ? dist : 0.;
940     }                                             942     }
941   }                                               943   }
942   return 0.;                                      944   return 0.;
943 }                                                 945 }
944                                                   946 
945 ////////////////////////////////////////////// << 947 ////////////////////////////////////////////////////////////////////////////
946 //                                                948 //
947 // Calculate distance to surface of shape from    949 // Calculate distance to surface of shape from inside and
948 // find normal at exit point, if required         950 // find normal at exit point, if required
949 // - when leaving the surface, return 0           951 // - when leaving the surface, return 0
950                                                   952 
951 G4double G4Trap::DistanceToOut(const G4ThreeVe    953 G4double G4Trap::DistanceToOut(const G4ThreeVector& p, const G4ThreeVector& v,
952                                const G4bool ca    954                                const G4bool calcNorm,
953                                      G4bool* v << 955                                      G4bool *validNorm, G4ThreeVector *n) const
954 {                                                 956 {
955   // Z intersections                              957   // Z intersections
956   //                                              958   //
957   if ((std::abs(p.z()) - fDz) >= -halfCarToler    959   if ((std::abs(p.z()) - fDz) >= -halfCarTolerance && p.z()*v.z() > 0)
958   {                                               960   {
959     if (calcNorm)                                 961     if (calcNorm)
960     {                                             962     {
961       *validNorm = true;                          963       *validNorm = true;
962       n->set(0, 0, (p.z() < 0) ? -1 : 1);         964       n->set(0, 0, (p.z() < 0) ? -1 : 1);
963     }                                             965     }
964     return 0;                                     966     return 0;
965   }                                               967   }
966   G4double vz = v.z();                            968   G4double vz = v.z();
967   G4double tmax = (vz == 0) ? DBL_MAX : (std::    969   G4double tmax = (vz == 0) ? DBL_MAX : (std::copysign(fDz,vz) - p.z())/vz;
968   G4int iside = (vz < 0) ? -4 : -2; // little     970   G4int iside = (vz < 0) ? -4 : -2; // little trick: (-4+3)=-1, (-2+3)=+1
969                                                   971 
970   // Y intersections                              972   // Y intersections
971   //                                              973   //
972   G4int i = 0;                                    974   G4int i = 0;
973   for ( ; i<2; ++i)                               975   for ( ; i<2; ++i)
974   {                                               976   {
975     G4double cosa = fPlanes[i].b*v.y() + fPlan    977     G4double cosa = fPlanes[i].b*v.y() + fPlanes[i].c*v.z();
976     if (cosa > 0)                                 978     if (cosa > 0)
977     {                                             979     {
978       G4double dist = fPlanes[i].b*p.y() + fPl    980       G4double dist = fPlanes[i].b*p.y() + fPlanes[i].c*p.z() + fPlanes[i].d;
979       if (dist >= -halfCarTolerance)              981       if (dist >= -halfCarTolerance)
980       {                                           982       {
981         if (calcNorm)                             983         if (calcNorm)
982         {                                         984         {
983           *validNorm = true;                      985           *validNorm = true;
984           n->set(0, fPlanes[i].b, fPlanes[i].c    986           n->set(0, fPlanes[i].b, fPlanes[i].c);
985         }                                         987         }
986         return 0;                                 988         return 0;
987       }                                           989       }
988       G4double tmp = -dist/cosa;                  990       G4double tmp = -dist/cosa;
989       if (tmax > tmp) { tmax = tmp; iside = i;    991       if (tmax > tmp) { tmax = tmp; iside = i; }
990     }                                             992     }
991   }                                               993   }
992                                                   994 
993   // X intersections                              995   // X intersections
994   //                                              996   //
995   for ( ; i<4; ++i)                               997   for ( ; i<4; ++i)
996   {                                               998   {
997     G4double cosa = fPlanes[i].a*v.x()+fPlanes    999     G4double cosa = fPlanes[i].a*v.x()+fPlanes[i].b*v.y()+fPlanes[i].c*v.z();
998     if (cosa > 0)                                 1000     if (cosa > 0)
999     {                                             1001     {
1000       G4double dist = fPlanes[i].a*p.x() +       1002       G4double dist = fPlanes[i].a*p.x() +
1001                       fPlanes[i].b*p.y() + fP    1003                       fPlanes[i].b*p.y() + fPlanes[i].c*p.z() + fPlanes[i].d;
1002       if (dist >= -halfCarTolerance)             1004       if (dist >= -halfCarTolerance)
1003       {                                          1005       {
1004         if (calcNorm)                            1006         if (calcNorm)
1005         {                                        1007         {
1006            *validNorm = true;                    1008            *validNorm = true;
1007            n->set(fPlanes[i].a, fPlanes[i].b,    1009            n->set(fPlanes[i].a, fPlanes[i].b, fPlanes[i].c);
1008         }                                        1010         }
1009         return 0;                                1011         return 0;
1010       }                                          1012       }
1011       G4double tmp = -dist/cosa;                 1013       G4double tmp = -dist/cosa;
1012       if (tmax > tmp) { tmax = tmp; iside = i    1014       if (tmax > tmp) { tmax = tmp; iside = i; }
1013     }                                            1015     }
1014   }                                              1016   }
1015                                                  1017 
1016   // Set normal, if required, and return dist    1018   // Set normal, if required, and return distance
1017   //                                             1019   //
1018   if (calcNorm)                               << 1020   if (calcNorm) 
1019   {                                              1021   {
1020     *validNorm = true;                           1022     *validNorm = true;
1021     if (iside < 0)                               1023     if (iside < 0)
1022       n->set(0, 0, iside + 3); // (-4+3)=-1,     1024       n->set(0, 0, iside + 3); // (-4+3)=-1, (-2+3)=+1
1023     else                                         1025     else
1024       n->set(fPlanes[iside].a, fPlanes[iside]    1026       n->set(fPlanes[iside].a, fPlanes[iside].b, fPlanes[iside].c);
1025   }                                              1027   }
1026   return tmax;                                   1028   return tmax;
1027 }                                                1029 }
1028                                                  1030 
1029 ///////////////////////////////////////////// << 1031 ////////////////////////////////////////////////////////////////////////////
1030 //                                               1032 //
1031 // Calculate exact shortest distance to any b    1033 // Calculate exact shortest distance to any boundary from inside
1032 // - Returns 0 is ThreeVector outside            1034 // - Returns 0 is ThreeVector outside
1033                                                  1035 
1034 G4double G4Trap::DistanceToOut( const G4Three    1036 G4double G4Trap::DistanceToOut( const G4ThreeVector& p ) const
1035 {                                                1037 {
1036 #ifdef G4CSGDEBUG                                1038 #ifdef G4CSGDEBUG
1037   if( Inside(p) == kOutside )                    1039   if( Inside(p) == kOutside )
1038   {                                              1040   {
1039     std::ostringstream message;                  1041     std::ostringstream message;
1040     G4long oldprc = message.precision(16);    << 1042     G4int oldprc = message.precision(16);
1041     message << "Point p is outside (!?) of so    1043     message << "Point p is outside (!?) of solid: " << GetName() << G4endl;
1042     message << "Position:\n";                    1044     message << "Position:\n";
1043     message << "   p.x() = " << p.x()/mm << "    1045     message << "   p.x() = " << p.x()/mm << " mm\n";
1044     message << "   p.y() = " << p.y()/mm << "    1046     message << "   p.y() = " << p.y()/mm << " mm\n";
1045     message << "   p.z() = " << p.z()/mm << "    1047     message << "   p.z() = " << p.z()/mm << " mm";
1046     G4cout.precision(oldprc);                 << 1048     G4cout.precision(oldprc) ;
1047     G4Exception("G4Trap::DistanceToOut(p)", "    1049     G4Exception("G4Trap::DistanceToOut(p)", "GeomSolids1002",
1048                 JustWarning, message );          1050                 JustWarning, message );
1049     DumpInfo();                                  1051     DumpInfo();
1050   }                                              1052   }
1051 #endif                                           1053 #endif
1052   switch (fTrapType)                             1054   switch (fTrapType)
1053   {                                              1055   {
1054     case 0: // General case                      1056     case 0: // General case
1055     {                                            1057     {
1056       G4double dz = std::abs(p.z())-fDz;         1058       G4double dz = std::abs(p.z())-fDz;
1057       G4double dy1 = fPlanes[0].b*p.y()+fPlan    1059       G4double dy1 = fPlanes[0].b*p.y()+fPlanes[0].c*p.z()+fPlanes[0].d;
1058       G4double dy2 = fPlanes[1].b*p.y()+fPlan    1060       G4double dy2 = fPlanes[1].b*p.y()+fPlanes[1].c*p.z()+fPlanes[1].d;
1059       G4double dy = std::max(dz,std::max(dy1,    1061       G4double dy = std::max(dz,std::max(dy1,dy2));
1060                                                  1062 
1061       G4double dx1 = fPlanes[2].a*p.x()+fPlan    1063       G4double dx1 = fPlanes[2].a*p.x()+fPlanes[2].b*p.y()
1062                    + fPlanes[2].c*p.z()+fPlan    1064                    + fPlanes[2].c*p.z()+fPlanes[2].d;
1063       G4double dx2 = fPlanes[3].a*p.x()+fPlan    1065       G4double dx2 = fPlanes[3].a*p.x()+fPlanes[3].b*p.y()
1064                    + fPlanes[3].c*p.z()+fPlan    1066                    + fPlanes[3].c*p.z()+fPlanes[3].d;
1065       G4double dist = std::max(dy,std::max(dx    1067       G4double dist = std::max(dy,std::max(dx1,dx2));
1066       return (dist < 0) ? -dist : 0.;            1068       return (dist < 0) ? -dist : 0.;
1067     }                                            1069     }
1068     case 1: // YZ section is a rectangle         1070     case 1: // YZ section is a rectangle
1069     {                                            1071     {
1070       G4double dz = std::abs(p.z())-fDz;         1072       G4double dz = std::abs(p.z())-fDz;
1071       G4double dy = std::max(dz,std::abs(p.y(    1073       G4double dy = std::max(dz,std::abs(p.y())+fPlanes[1].d);
1072       G4double dx1 = fPlanes[2].a*p.x()+fPlan    1074       G4double dx1 = fPlanes[2].a*p.x()+fPlanes[2].b*p.y()
1073                    + fPlanes[2].c*p.z()+fPlan    1075                    + fPlanes[2].c*p.z()+fPlanes[2].d;
1074       G4double dx2 = fPlanes[3].a*p.x()+fPlan    1076       G4double dx2 = fPlanes[3].a*p.x()+fPlanes[3].b*p.y()
1075                    + fPlanes[3].c*p.z()+fPlan    1077                    + fPlanes[3].c*p.z()+fPlanes[3].d;
1076       G4double dist = std::max(dy,std::max(dx    1078       G4double dist = std::max(dy,std::max(dx1,dx2));
1077       return (dist < 0) ? -dist : 0.;            1079       return (dist < 0) ? -dist : 0.;
1078     }                                            1080     }
1079     case 2: // YZ section is a rectangle and     1081     case 2: // YZ section is a rectangle and
1080     {       // XZ section is an isosceles tra    1082     {       // XZ section is an isosceles trapezoid
1081       G4double dz = std::abs(p.z())-fDz;         1083       G4double dz = std::abs(p.z())-fDz;
1082       G4double dy = std::max(dz,std::abs(p.y(    1084       G4double dy = std::max(dz,std::abs(p.y())+fPlanes[1].d);
1083       G4double dx = fPlanes[3].a*std::abs(p.x    1085       G4double dx = fPlanes[3].a*std::abs(p.x())
1084                   + fPlanes[3].c*p.z()+fPlane    1086                   + fPlanes[3].c*p.z()+fPlanes[3].d;
1085       G4double dist = std::max(dy,dx);           1087       G4double dist = std::max(dy,dx);
1086       return (dist < 0) ? -dist : 0.;            1088       return (dist < 0) ? -dist : 0.;
1087     }                                            1089     }
1088     case 3: // YZ section is a rectangle and     1090     case 3: // YZ section is a rectangle and
1089     {       // XY section is an isosceles tra    1091     {       // XY section is an isosceles trapezoid
1090       G4double dz = std::abs(p.z())-fDz;         1092       G4double dz = std::abs(p.z())-fDz;
1091       G4double dy = std::max(dz,std::abs(p.y(    1093       G4double dy = std::max(dz,std::abs(p.y())+fPlanes[1].d);
1092       G4double dx = fPlanes[3].a*std::abs(p.x    1094       G4double dx = fPlanes[3].a*std::abs(p.x())
1093                   + fPlanes[3].b*p.y()+fPlane    1095                   + fPlanes[3].b*p.y()+fPlanes[3].d;
1094       G4double dist = std::max(dy,dx);           1096       G4double dist = std::max(dy,dx);
1095       return (dist < 0) ? -dist : 0.;            1097       return (dist < 0) ? -dist : 0.;
1096     }                                            1098     }
1097   }                                              1099   }
1098   return 0.;                                     1100   return 0.;
1099 }                                                1101 }
1100                                                  1102 
1101 ///////////////////////////////////////////// << 1103 ////////////////////////////////////////////////////////////////////////////
1102 //                                               1104 //
1103 // GetEntityType                                 1105 // GetEntityType
1104                                                  1106 
1105 G4GeometryType G4Trap::GetEntityType() const     1107 G4GeometryType G4Trap::GetEntityType() const
1106 {                                                1108 {
1107   return {"G4Trap"};                          << 1109   return G4String("G4Trap");
1108 }                                             << 
1109                                               << 
1110 ///////////////////////////////////////////// << 
1111 //                                            << 
1112 // IsFaceted                                  << 
1113                                               << 
1114 G4bool G4Trap::IsFaceted() const              << 
1115 {                                             << 
1116   return true;                                << 
1117 }                                                1110 }
1118                                                  1111 
1119 /////////////////////////////////////////////    1112 //////////////////////////////////////////////////////////////////////////
1120 //                                               1113 //
1121 // Make a clone of the object                    1114 // Make a clone of the object
1122 //                                               1115 //
1123 G4VSolid* G4Trap::Clone() const                  1116 G4VSolid* G4Trap::Clone() const
1124 {                                                1117 {
1125   return new G4Trap(*this);                      1118   return new G4Trap(*this);
1126 }                                                1119 }
1127                                                  1120 
1128 /////////////////////////////////////////////    1121 //////////////////////////////////////////////////////////////////////////
1129 //                                               1122 //
1130 // Stream object contents to an output stream    1123 // Stream object contents to an output stream
1131                                                  1124 
1132 std::ostream& G4Trap::StreamInfo( std::ostrea    1125 std::ostream& G4Trap::StreamInfo( std::ostream& os ) const
1133 {                                                1126 {
1134   G4double phi    = GetPhi();                 << 1127   G4double phi    = std::atan2(fTthetaSphi,fTthetaCphi);
1135   G4double theta  = GetTheta();               << 1128   G4double theta  = std::atan(std::sqrt(fTthetaCphi*fTthetaCphi
1136   G4double alpha1 = GetAlpha1();              << 1129                                        +fTthetaSphi*fTthetaSphi));
1137   G4double alpha2 = GetAlpha2();              << 1130   G4double alpha1 = std::atan(fTalpha1);
                                                   >> 1131   G4double alpha2 = std::atan(fTalpha2);
                                                   >> 1132   G4String signDegree = "\u00B0"; 
1138                                                  1133 
1139   G4long oldprc = os.precision(16);           << 1134   G4int oldprc = os.precision(16);
1140   os << "------------------------------------    1135   os << "-----------------------------------------------------------\n"
1141      << "    *** Dump for solid: " << GetName    1136      << "    *** Dump for solid: " << GetName() << " ***\n"
1142      << "    ================================    1137      << "    ===================================================\n"
1143      << " Solid type: G4Trap\n"                  1138      << " Solid type: G4Trap\n"
1144      << " Parameters:\n"                         1139      << " Parameters:\n"
1145      << "    half length Z: " << fDz/mm << "     1140      << "    half length Z: " << fDz/mm << " mm\n"
1146      << "    half length Y, face -Dz: " << fD    1141      << "    half length Y, face -Dz: " << fDy1/mm << " mm\n"
1147      << "    half length X, face -Dz, side -D    1142      << "    half length X, face -Dz, side -Dy1: " << fDx1/mm << " mm\n"
1148      << "    half length X, face -Dz, side +D    1143      << "    half length X, face -Dz, side +Dy1: " << fDx2/mm << " mm\n"
1149      << "    half length Y, face +Dz: " << fD    1144      << "    half length Y, face +Dz: " << fDy2/mm << " mm\n"
1150      << "    half length X, face +Dz, side -D    1145      << "    half length X, face +Dz, side -Dy2: " << fDx3/mm << " mm\n"
1151      << "    half length X, face +Dz, side +D    1146      << "    half length X, face +Dz, side +Dy2: " << fDx4/mm << " mm\n"
1152      << "    theta: " << theta/degree << " de << 1147      << "    theta: " << theta/degree << signDegree << "\n"
1153      << "    phi:   " << phi/degree << " degr << 1148      << "    phi: " << phi/degree << signDegree << "\n"
1154      << "    alpha, face -Dz: " << alpha1/deg << 1149      << "    alpha, face -Dz: " << alpha1/degree << signDegree << "\n"
1155      << "    alpha, face +Dz: " << alpha2/deg << 1150      << "    alpha, face +Dz: " << alpha2/degree << signDegree << "\n"
1156      << "------------------------------------    1151      << "-----------------------------------------------------------\n";
1157   os.precision(oldprc);                          1152   os.precision(oldprc);
1158                                                  1153 
1159   return os;                                     1154   return os;
1160 }                                                1155 }
1161                                                  1156 
1162 /////////////////////////////////////////////    1157 //////////////////////////////////////////////////////////////////////////
1163 //                                               1158 //
1164 // Compute vertices from planes                  1159 // Compute vertices from planes
1165                                                  1160 
1166 void G4Trap::GetVertices(G4ThreeVector pt[8])    1161 void G4Trap::GetVertices(G4ThreeVector pt[8]) const
1167 {                                                1162 {
1168   for (G4int i=0; i<8; ++i)                      1163   for (G4int i=0; i<8; ++i)
1169   {                                              1164   {
1170     G4int iy = (i==0 || i==1 || i==4 || i==5)    1165     G4int iy = (i==0 || i==1 || i==4 || i==5) ? 0 : 1;
1171     G4int ix = (i==0 || i==2 || i==4 || i==6)    1166     G4int ix = (i==0 || i==2 || i==4 || i==6) ? 2 : 3;
1172     G4double z = (i < 4) ? -fDz : fDz;           1167     G4double z = (i < 4) ? -fDz : fDz;
1173     G4double y = -(fPlanes[iy].c*z + fPlanes[    1168     G4double y = -(fPlanes[iy].c*z + fPlanes[iy].d)/fPlanes[iy].b;
1174     G4double x = -(fPlanes[ix].b*y + fPlanes[    1169     G4double x = -(fPlanes[ix].b*y + fPlanes[ix].c*z
1175                    + fPlanes[ix].d)/fPlanes[i    1170                    + fPlanes[ix].d)/fPlanes[ix].a;
1176     pt[i].set(x,y,z);                            1171     pt[i].set(x,y,z);
1177   }                                              1172   }
1178 }                                                1173 }
1179                                                  1174 
1180 ///////////////////////////////////////////// << 1175 /////////////////////////////////////////////////////////////////////////
1181 //                                               1176 //
1182 // Generate random point on the surface          1177 // Generate random point on the surface
1183                                                  1178 
1184 G4ThreeVector G4Trap::GetPointOnSurface() con    1179 G4ThreeVector G4Trap::GetPointOnSurface() const
1185 {                                                1180 {
1186   // Set indeces                              << 
1187   constexpr G4int iface [6][4] =              << 
1188     { {0,1,3,2}, {0,4,5,1}, {2,3,7,6}, {0,2,6 << 
1189                                               << 
1190   // Set vertices                             << 
1191   G4ThreeVector pt[8];                           1181   G4ThreeVector pt[8];
                                                   >> 1182   G4int iface [6][4] =
                                                   >> 1183     { {0,1,3,2}, {0,4,5,1}, {2,3,7,6}, {0,2,6,4}, {1,5,7,3}, {4,6,7,5} }; 
                                                   >> 1184   G4double sface[6];
                                                   >> 1185 
1192   GetVertices(pt);                               1186   GetVertices(pt);
                                                   >> 1187   G4double stotal = 0;
                                                   >> 1188   for (G4int i=0; i<6; ++i)
                                                   >> 1189   {
                                                   >> 1190     G4double ss = G4GeomTools::QuadAreaNormal(pt[iface[i][0]],
                                                   >> 1191                                               pt[iface[i][1]],
                                                   >> 1192                                               pt[iface[i][2]],
                                                   >> 1193                                               pt[iface[i][3]]).mag();
                                                   >> 1194     stotal  += ss;
                                                   >> 1195     sface[i] = stotal;
                                                   >> 1196   }
1193                                                  1197 
1194   // Select face                                 1198   // Select face
1195   //                                             1199   //
1196   G4double select = fAreas[5]*G4QuickRand();  << 1200   G4double select = stotal*G4UniformRand();
1197   G4int k = 5;                                   1201   G4int k = 5;
1198   k -= (G4int)(select <= fAreas[4]);          << 1202   if (select <= sface[4]) k = 4;
1199   k -= (G4int)(select <= fAreas[3]);          << 1203   if (select <= sface[3]) k = 3;
1200   k -= (G4int)(select <= fAreas[2]);          << 1204   if (select <= sface[2]) k = 2;
1201   k -= (G4int)(select <= fAreas[1]);          << 1205   if (select <= sface[1]) k = 1;
1202   k -= (G4int)(select <= fAreas[0]);          << 1206   if (select <= sface[0]) k = 0;
1203                                                  1207 
1204   // Select sub-triangle                         1208   // Select sub-triangle
1205   //                                             1209   //
1206   G4int i0 = iface[k][0];                        1210   G4int i0 = iface[k][0];
1207   G4int i1 = iface[k][1];                        1211   G4int i1 = iface[k][1];
1208   G4int i2 = iface[k][2];                        1212   G4int i2 = iface[k][2];
1209   G4int i3 = iface[k][3];                        1213   G4int i3 = iface[k][3];
                                                   >> 1214   G4double s1 = G4GeomTools::TriangleAreaNormal(pt[i0],pt[i1],pt[i3]).mag();
1210   G4double s2 = G4GeomTools::TriangleAreaNorm    1215   G4double s2 = G4GeomTools::TriangleAreaNormal(pt[i2],pt[i1],pt[i3]).mag();
1211   if (select > fAreas[k] - s2) i0 = i2;       << 1216   if ((s1+s2)*G4UniformRand() > s1) i0 = i2;
1212                                                  1217 
1213   // Generate point                              1218   // Generate point
1214   //                                             1219   //
1215   G4double u = G4QuickRand();                 << 1220   G4double u = G4UniformRand();
1216   G4double v = G4QuickRand();                 << 1221   G4double v = G4UniformRand();
1217   if (u + v > 1.) { u = 1. - u; v = 1. - v; }    1222   if (u + v > 1.) { u = 1. - u; v = 1. - v; }
1218   return (1.-u-v)*pt[i0] + u*pt[i1] + v*pt[i3    1223   return (1.-u-v)*pt[i0] + u*pt[i1] + v*pt[i3];
1219 }                                                1224 }
1220                                                  1225 
1221 /////////////////////////////////////////////    1226 //////////////////////////////////////////////////////////////////////////
1222 //                                               1227 //
1223 // Methods for visualisation                     1228 // Methods for visualisation
1224                                                  1229 
1225 void G4Trap::DescribeYourselfTo ( G4VGraphics    1230 void G4Trap::DescribeYourselfTo ( G4VGraphicsScene& scene ) const
1226 {                                                1231 {
1227   scene.AddSolid (*this);                        1232   scene.AddSolid (*this);
1228 }                                                1233 }
1229                                                  1234 
1230 G4Polyhedron* G4Trap::CreatePolyhedron () con    1235 G4Polyhedron* G4Trap::CreatePolyhedron () const
1231 {                                                1236 {
1232   G4double phi = std::atan2(fTthetaSphi, fTth    1237   G4double phi = std::atan2(fTthetaSphi, fTthetaCphi);
1233   G4double alpha1 = std::atan(fTalpha1);         1238   G4double alpha1 = std::atan(fTalpha1);
1234   G4double alpha2 = std::atan(fTalpha2);         1239   G4double alpha2 = std::atan(fTalpha2);
1235   G4double theta = std::atan(std::sqrt(fTthet    1240   G4double theta = std::atan(std::sqrt(fTthetaCphi*fTthetaCphi
1236                                       +fTthet    1241                                       +fTthetaSphi*fTthetaSphi));
1237                                                  1242 
1238   return new G4PolyhedronTrap(fDz, theta, phi    1243   return new G4PolyhedronTrap(fDz, theta, phi,
1239                               fDy1, fDx1, fDx    1244                               fDy1, fDx1, fDx2, alpha1,
1240                               fDy2, fDx3, fDx    1245                               fDy2, fDx3, fDx4, alpha2);
1241 }                                                1246 }
1242                                                  1247 
1243 #endif                                           1248 #endif
1244                                                  1249