Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/geometry/solids/specific/src/G4PolyhedraSide.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/specific/src/G4PolyhedraSide.cc (Version 11.3.0) and /geometry/solids/specific/src/G4PolyhedraSide.cc (Version 9.1.p3)


  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 // Implementation of G4PolyhedraSide, the face << 
 27 // one segmented side of a Polyhedra           << 
 28 //                                                 26 //
 29 // Author: David C. Williams (davidw@scipp.ucs <<  27 // $Id: G4PolyhedraSide.cc,v 1.13 2007/05/31 13:52:48 gcosmo Exp $
                                                   >>  28 // GEANT4 tag $Name: geant4-09-01-patch-03 $
                                                   >>  29 //
                                                   >>  30 // 
                                                   >>  31 // --------------------------------------------------------------------
                                                   >>  32 // GEANT 4 class source file
                                                   >>  33 //
                                                   >>  34 //
                                                   >>  35 // G4PolyhedraSide.cc
                                                   >>  36 //
                                                   >>  37 // Implemenation of the face representing one segmented side of a Polyhedra
                                                   >>  38 //
 30 // -------------------------------------------     39 // --------------------------------------------------------------------
 31                                                    40 
 32 #include "G4PolyhedraSide.hh"                      41 #include "G4PolyhedraSide.hh"
 33 #include "G4PhysicalConstants.hh"              << 
 34 #include "G4IntersectingCone.hh"                   42 #include "G4IntersectingCone.hh"
 35 #include "G4ClippablePolygon.hh"                   43 #include "G4ClippablePolygon.hh"
 36 #include "G4AffineTransform.hh"                    44 #include "G4AffineTransform.hh"
 37 #include "G4SolidExtentList.hh"                    45 #include "G4SolidExtentList.hh"
 38 #include "G4GeometryTolerance.hh"                  46 #include "G4GeometryTolerance.hh"
 39                                                    47 
 40 #include "Randomize.hh"                        << 
 41                                                << 
 42 // This new field helps to use the class G4PhS << 
 43 //                                                 48 //
 44 G4PhSideManager G4PolyhedraSide::subInstanceMa << 
 45                                                << 
 46 // This macro changes the references to fields << 
 47 // in the class G4PhSideData.                  << 
 48 //                                             << 
 49 #define G4MT_phphix ((subInstanceManager.offse << 
 50 #define G4MT_phphiy ((subInstanceManager.offse << 
 51 #define G4MT_phphiz ((subInstanceManager.offse << 
 52 #define G4MT_phphik ((subInstanceManager.offse << 
 53                                                << 
 54 // Returns the private data instance manager.  << 
 55 //                                             << 
 56 const G4PhSideManager& G4PolyhedraSide::GetSub << 
 57 {                                              << 
 58   return subInstanceManager;                   << 
 59 }                                              << 
 60                                                << 
 61 // Constructor                                     49 // Constructor
 62 //                                                 50 //
 63 // Values for r1,z1 and r2,z2 should be specif     51 // Values for r1,z1 and r2,z2 should be specified in clockwise
 64 // order in (r,z).                                 52 // order in (r,z).
 65 //                                                 53 //
 66 G4PolyhedraSide::G4PolyhedraSide( const G4Poly <<  54 G4PolyhedraSide::G4PolyhedraSide( const G4PolyhedraSideRZ *prevRZ,
 67                                   const G4Poly <<  55                                   const G4PolyhedraSideRZ *tail,
 68                                   const G4Poly <<  56                                   const G4PolyhedraSideRZ *head,
 69                                   const G4Poly <<  57                                   const G4PolyhedraSideRZ *nextRZ,
 70                                         G4int      58                                         G4int theNumSide, 
 71                                         G4doub     59                                         G4double thePhiStart, 
 72                                         G4doub     60                                         G4double thePhiTotal, 
 73                                         G4bool     61                                         G4bool thePhiIsOpen,
 74                                         G4bool     62                                         G4bool isAllBehind )
 75 {                                                  63 {
 76                                                    64 
 77   instanceID = subInstanceManager.CreateSubIns << 
 78                                                << 
 79   kCarTolerance = G4GeometryTolerance::GetInst     65   kCarTolerance = G4GeometryTolerance::GetInstance()->GetSurfaceTolerance();
 80   G4MT_phphix = 0.0; G4MT_phphiy = 0.0; G4MT_p << 
 81   G4MT_phphik = 0.0;                           << 
 82                                                    66 
 83   //                                               67   //
 84   // Record values                                 68   // Record values
 85   //                                               69   //
 86   r[0] = tail->r; z[0] = tail->z;                  70   r[0] = tail->r; z[0] = tail->z;
 87   r[1] = head->r; z[1] = head->z;                  71   r[1] = head->r; z[1] = head->z;
 88                                                    72   
 89   G4double phiTotal;                               73   G4double phiTotal;
 90                                                    74   
 91   //                                               75   //
 92   // Set phi to our convention                     76   // Set phi to our convention
 93   //                                               77   //
 94   startPhi = thePhiStart;                          78   startPhi = thePhiStart;
 95   while (startPhi < 0.0)    // Loop checking,  <<  79   while (startPhi < 0.0) startPhi += twopi;
 96     startPhi += twopi;                         << 
 97                                                    80   
 98   phiIsOpen = thePhiIsOpen;                        81   phiIsOpen = thePhiIsOpen;
 99   phiTotal = (phiIsOpen) ? thePhiTotal : twopi     82   phiTotal = (phiIsOpen) ? thePhiTotal : twopi;
100                                                    83   
101   allBehind = isAllBehind;                         84   allBehind = isAllBehind;
102                                                    85     
103   //                                               86   //
104   // Make our intersecting cone                    87   // Make our intersecting cone
105   //                                               88   //
106   cone = new G4IntersectingCone( r, z );           89   cone = new G4IntersectingCone( r, z );
107                                                    90   
108   //                                               91   //
109   // Construct side plane vector set               92   // Construct side plane vector set
110   //                                               93   //
111   numSide = theNumSide>0 ? theNumSide : 1;     <<  94   numSide = theNumSide;
112   deltaPhi = phiTotal/numSide;                 <<  95   deltaPhi = phiTotal/theNumSide;
113   endPhi = startPhi+phiTotal;                      96   endPhi = startPhi+phiTotal;
114                                                <<  97   
115   const std::size_t maxSides = numSide;        <<  98   vecs = new G4PolyhedraSideVec[numSide];
116   vecs = new G4PolyhedraSideVec[maxSides];     <<  99   
117   edges = new G4PolyhedraSideEdge[phiIsOpen ?  << 100   edges = new G4PolyhedraSideEdge[phiIsOpen ? numSide+1 : numSide];
118                                                   101   
119   //                                              102   //
120   // ...this is where we start                    103   // ...this is where we start
121   //                                              104   //
122   G4double phi = startPhi;                        105   G4double phi = startPhi;
123   G4ThreeVector a1( r[0]*std::cos(phi), r[0]*s    106   G4ThreeVector a1( r[0]*std::cos(phi), r[0]*std::sin(phi), z[0] ),
124           b1( r[1]*std::cos(phi), r[1]*std::si    107           b1( r[1]*std::cos(phi), r[1]*std::sin(phi), z[1] ),
125           c1( prevRZ->r*std::cos(phi), prevRZ-    108           c1( prevRZ->r*std::cos(phi), prevRZ->r*std::sin(phi), prevRZ->z ),
126           d1( nextRZ->r*std::cos(phi), nextRZ-    109           d1( nextRZ->r*std::cos(phi), nextRZ->r*std::sin(phi), nextRZ->z ),
127           a2, b2, c2, d2;                         110           a2, b2, c2, d2;
128   G4PolyhedraSideEdge *edge = edges;              111   G4PolyhedraSideEdge *edge = edges;
129                                                   112           
130   G4PolyhedraSideVec *vec = vecs;                 113   G4PolyhedraSideVec *vec = vecs;
131   do    // Loop checking, 13.08.2015, G.Cosmo  << 114   do
132   {                                               115   {
133     //                                            116     //
134     // ...this is where we are going              117     // ...this is where we are going
135     //                                            118     //
136     phi += deltaPhi;                              119     phi += deltaPhi;
137     a2 = G4ThreeVector( r[0]*std::cos(phi), r[    120     a2 = G4ThreeVector( r[0]*std::cos(phi), r[0]*std::sin(phi), z[0] );
138     b2 = G4ThreeVector( r[1]*std::cos(phi), r[    121     b2 = G4ThreeVector( r[1]*std::cos(phi), r[1]*std::sin(phi), z[1] );
139     c2 = G4ThreeVector( prevRZ->r*std::cos(phi    122     c2 = G4ThreeVector( prevRZ->r*std::cos(phi), prevRZ->r*std::sin(phi), prevRZ->z );
140     d2 = G4ThreeVector( nextRZ->r*std::cos(phi    123     d2 = G4ThreeVector( nextRZ->r*std::cos(phi), nextRZ->r*std::sin(phi), nextRZ->z );
141                                                   124     
142     G4ThreeVector tt;                             125     G4ThreeVector tt;  
143                                                   126     
144     //                                            127     //
145     // ...build some relevant vectors.            128     // ...build some relevant vectors.
146     //    the point is to sacrifice a little m    129     //    the point is to sacrifice a little memory with precalcs 
147     //    to gain speed                           130     //    to gain speed
148     //                                            131     //
149     vec->center = 0.25*( a1 + a2 + b1 + b2 );     132     vec->center = 0.25*( a1 + a2 + b1 + b2 );
150                                                   133     
151     tt = b2 + b1 - a2 - a1;                       134     tt = b2 + b1 - a2 - a1;
152     vec->surfRZ = tt.unit();                      135     vec->surfRZ = tt.unit();
153     if (vec==vecs) lenRZ = 0.25*tt.mag();         136     if (vec==vecs) lenRZ = 0.25*tt.mag();
154                                                   137     
155     tt = b2 - b1 + a2 - a1;                       138     tt = b2 - b1 + a2 - a1;
156     vec->surfPhi = tt.unit();                     139     vec->surfPhi = tt.unit();
157     if (vec==vecs)                                140     if (vec==vecs)
158     {                                             141     {
159       lenPhi[0] = 0.25*tt.mag();                  142       lenPhi[0] = 0.25*tt.mag();
160       tt = b2 - b1;                               143       tt = b2 - b1;
161       lenPhi[1] = (0.5*tt.mag()-lenPhi[0])/len    144       lenPhi[1] = (0.5*tt.mag()-lenPhi[0])/lenRZ;
162     }                                             145     }
163                                                   146     
164     tt = vec->surfPhi.cross(vec->surfRZ);         147     tt = vec->surfPhi.cross(vec->surfRZ);
165     vec->normal = tt.unit();                      148     vec->normal = tt.unit();
166                                                   149     
167     //                                            150     //
168     // ...edge normals are the average of the     151     // ...edge normals are the average of the normals of
169     //    the two faces they connect.             152     //    the two faces they connect.
170     //                                            153     //
171     // ...edge normals are necessary if we are    154     // ...edge normals are necessary if we are to accurately
172     //    decide if a point is "inside" a face    155     //    decide if a point is "inside" a face. For non-convex
173     //    shapes, it is absolutely necessary t    156     //    shapes, it is absolutely necessary to know information
174     //    on adjacent faces to accurate determ    157     //    on adjacent faces to accurate determine this.
175     //                                            158     //
176     // ...we don't need them for the phi edges    159     // ...we don't need them for the phi edges, since that
177     //    information is taken care of interna    160     //    information is taken care of internally. The r/z edges,
178     //    however, depend on the adjacent G4Po    161     //    however, depend on the adjacent G4PolyhedraSide.
179     //                                            162     //
180     G4ThreeVector a12, adj;                       163     G4ThreeVector a12, adj;
181                                                   164     
182     a12 = a2-a1;                                  165     a12 = a2-a1;
183                                                   166 
184     adj = 0.5*(c1+c2-a1-a2);                      167     adj = 0.5*(c1+c2-a1-a2);
185     adj = adj.cross(a12);                         168     adj = adj.cross(a12);  
186     adj = adj.unit() + vec->normal;               169     adj = adj.unit() + vec->normal;       
187     vec->edgeNorm[0] = adj.unit();                170     vec->edgeNorm[0] = adj.unit();
188                                                   171     
189     a12 = b1-b2;                                  172     a12 = b1-b2;
190     adj = 0.5*(d1+d2-b1-b2);                      173     adj = 0.5*(d1+d2-b1-b2);
191     adj = adj.cross(a12);                         174     adj = adj.cross(a12);  
192     adj = adj.unit() + vec->normal;               175     adj = adj.unit() + vec->normal;       
193     vec->edgeNorm[1] = adj.unit();                176     vec->edgeNorm[1] = adj.unit();
194                                                   177     
195     //                                            178     //
196     // ...the corners are crucial. It is impor    179     // ...the corners are crucial. It is important that
197     //    they are calculated consistently for    180     //    they are calculated consistently for adjacent
198     //    G4PolyhedraSides, to avoid gaps caus    181     //    G4PolyhedraSides, to avoid gaps caused by roundoff.
199     //                                            182     //
200     vec->edges[0] = edge;                         183     vec->edges[0] = edge;
201     edge->corner[0] = a1;                         184     edge->corner[0] = a1;
202     edge->corner[1] = b1;                         185     edge->corner[1] = b1;
203     edge++;                                       186     edge++;
204     vec->edges[1] = edge;                         187     vec->edges[1] = edge;
205                                                   188 
206     a1 = a2;                                      189     a1 = a2;
207     b1 = b2;                                      190     b1 = b2;
208     c1 = c2;                                      191     c1 = c2;
209     d1 = d2;                                      192     d1 = d2;
210   } while( ++vec < vecs+maxSides );            << 193   } while( ++vec < vecs+numSide );
211                                                   194   
212   //                                              195   //
213   // Clean up hanging edge                        196   // Clean up hanging edge
214   //                                              197   //
215   if (phiIsOpen)                                  198   if (phiIsOpen)
216   {                                               199   {
217     edge->corner[0] = a2;                         200     edge->corner[0] = a2;
218     edge->corner[1] = b2;                         201     edge->corner[1] = b2;
219   }                                               202   }
220   else                                            203   else
221   {                                               204   {
222     vecs[maxSides-1].edges[1] = edges;         << 205     vecs[numSide-1].edges[1] = edges;
223   }                                               206   }
224                                                   207   
225   //                                              208   //
226   // Go back and fill in remaining fields in e    209   // Go back and fill in remaining fields in edges
227   //                                              210   //
228   vec = vecs;                                     211   vec = vecs;
229   G4PolyhedraSideVec *prev = vecs+maxSides-1;  << 212   G4PolyhedraSideVec *prev = vecs+numSide-1;
230   do    // Loop checking, 13.08.2015, G.Cosmo  << 213   do
231   {                                               214   {
232     edge = vec->edges[0];    // The edge betwe    215     edge = vec->edges[0];    // The edge between prev and vec
233                                                   216     
234     //                                            217     //
235     // Okay: edge normal is average of normals    218     // Okay: edge normal is average of normals of adjacent faces
236     //                                            219     //
237     G4ThreeVector eNorm = vec->normal + prev->    220     G4ThreeVector eNorm = vec->normal + prev->normal;
238     edge->normal = eNorm.unit();                  221     edge->normal = eNorm.unit();  
239                                                   222     
240     //                                            223     //
241     // Vertex normal is average of norms of ad    224     // Vertex normal is average of norms of adjacent surfaces (all four)
242     // However, vec->edgeNorm is unit vector i    225     // However, vec->edgeNorm is unit vector in some direction
243     // as the sum of normals of adjacent Polyh    226     // as the sum of normals of adjacent PolyhedraSide with vec.
244     // The normalization used for this vector     227     // The normalization used for this vector should be the same
245     // for vec and prev.                          228     // for vec and prev.
246     //                                            229     //
247     eNorm = vec->edgeNorm[0] + prev->edgeNorm[    230     eNorm = vec->edgeNorm[0] + prev->edgeNorm[0];
248     edge->cornNorm[0] = eNorm.unit();             231     edge->cornNorm[0] = eNorm.unit();
249                                                   232   
250     eNorm = vec->edgeNorm[1] + prev->edgeNorm[    233     eNorm = vec->edgeNorm[1] + prev->edgeNorm[1];
251     edge->cornNorm[1] = eNorm.unit();             234     edge->cornNorm[1] = eNorm.unit();
252   } while( prev=vec, ++vec < vecs + maxSides ) << 235   } while( prev=vec, ++vec < vecs + numSide );
253                                                   236   
254   if (phiIsOpen)                                  237   if (phiIsOpen)
255   {                                               238   {
256     // G4double rFact = std::cos(0.5*deltaPhi)    239     // G4double rFact = std::cos(0.5*deltaPhi);
257     //                                            240     //
258     // If phi is open, we need to patch up nor    241     // If phi is open, we need to patch up normals of the
259     // first and last edges and their correspo    242     // first and last edges and their corresponding
260     // vertices.                                  243     // vertices.
261     //                                            244     //
262     // We use vectors that are in the plane of    245     // We use vectors that are in the plane of the
263     // face. This should be safe.                 246     // face. This should be safe.
264     //                                            247     //
265     vec = vecs;                                   248     vec = vecs;
266                                                   249     
267     G4ThreeVector normvec = vec->edges[0]->cor    250     G4ThreeVector normvec = vec->edges[0]->corner[0]
268                           - vec->edges[0]->cor    251                           - vec->edges[0]->corner[1];
269     normvec = normvec.cross(vec->normal);         252     normvec = normvec.cross(vec->normal);
270     if (normvec.dot(vec->surfPhi) > 0) normvec    253     if (normvec.dot(vec->surfPhi) > 0) normvec = -normvec;
271                                                   254 
272     vec->edges[0]->normal = normvec.unit();       255     vec->edges[0]->normal = normvec.unit();
273                                                   256     
274     vec->edges[0]->cornNorm[0] = (vec->edges[0    257     vec->edges[0]->cornNorm[0] = (vec->edges[0]->corner[0]
275                                 - vec->center)    258                                 - vec->center).unit();
276     vec->edges[0]->cornNorm[1] = (vec->edges[0    259     vec->edges[0]->cornNorm[1] = (vec->edges[0]->corner[1]
277                                 - vec->center)    260                                 - vec->center).unit();
278                                                   261     
279     //                                            262     //
280     // Repeat for ending phi                      263     // Repeat for ending phi
281     //                                            264     //
282     vec = vecs + maxSides - 1;                 << 265     vec = vecs + numSide - 1;
283                                                   266     
284     normvec = vec->edges[1]->corner[0] - vec->    267     normvec = vec->edges[1]->corner[0] - vec->edges[1]->corner[1];
285     normvec = normvec.cross(vec->normal);         268     normvec = normvec.cross(vec->normal);
286     if (normvec.dot(vec->surfPhi) < 0) normvec    269     if (normvec.dot(vec->surfPhi) < 0) normvec = -normvec;
287                                                   270 
288     vec->edges[1]->normal = normvec.unit();       271     vec->edges[1]->normal = normvec.unit();
289                                                   272     
290     vec->edges[1]->cornNorm[0] = (vec->edges[1    273     vec->edges[1]->cornNorm[0] = (vec->edges[1]->corner[0]
291                                 - vec->center)    274                                 - vec->center).unit();
292     vec->edges[1]->cornNorm[1] = (vec->edges[1    275     vec->edges[1]->cornNorm[1] = (vec->edges[1]->corner[1]
293                                 - vec->center)    276                                 - vec->center).unit();
294   }                                               277   }
295                                                   278   
296   //                                              279   //
297   // edgeNorm is the factor one multiplies the    280   // edgeNorm is the factor one multiplies the distance along vector phi
298   // on the surface of one of our sides in ord    281   // on the surface of one of our sides in order to calculate the distance
299   // from the edge. (see routine DistanceAway)    282   // from the edge. (see routine DistanceAway)
300   //                                              283   //
301   edgeNorm = 1.0/std::sqrt( 1.0 + lenPhi[1]*le    284   edgeNorm = 1.0/std::sqrt( 1.0 + lenPhi[1]*lenPhi[1] );
302 }                                                 285 }
303                                                   286 
                                                   >> 287 
                                                   >> 288 //
304 // Fake default constructor - sets only member    289 // Fake default constructor - sets only member data and allocates memory
305 //                            for usage restri    290 //                            for usage restricted to object persistency.
306 //                                                291 //
307 G4PolyhedraSide::G4PolyhedraSide( __void__&)      292 G4PolyhedraSide::G4PolyhedraSide( __void__&)
308   : startPhi(0.), deltaPhi(0.), endPhi(0.),    << 293   : cone(0), vecs(0), edges(0)
309     lenRZ(0.), edgeNorm(0.), kCarTolerance(0.) << 
310 {                                                 294 {
311   r[0] = r[1] = 0.;                            << 
312   z[0] = z[1] = 0.;                            << 
313   lenPhi[0] = lenPhi[1] = 0.;                  << 
314 }                                                 295 }
315                                                   296 
316                                                   297 
                                                   >> 298 //
317 // Destructor                                     299 // Destructor
318 //                                                300 //  
319 G4PolyhedraSide::~G4PolyhedraSide()               301 G4PolyhedraSide::~G4PolyhedraSide()
320 {                                                 302 {
321   delete cone;                                    303   delete cone;
322   delete [] vecs;                                 304   delete [] vecs;
323   delete [] edges;                                305   delete [] edges;
324 }                                                 306 }
325                                                   307 
                                                   >> 308 
                                                   >> 309 //
326 // Copy constructor                               310 // Copy constructor
327 //                                                311 //
328 G4PolyhedraSide::G4PolyhedraSide( const G4Poly << 312 G4PolyhedraSide::G4PolyhedraSide( const G4PolyhedraSide &source )
                                                   >> 313   : G4VCSGface()
329 {                                                 314 {
330   instanceID = subInstanceManager.CreateSubIns << 
331                                                << 
332   CopyStuff( source );                            315   CopyStuff( source );
333 }                                                 316 }
334                                                   317 
335                                                   318 
336 //                                                319 //
337 // Assignment operator                            320 // Assignment operator
338 //                                                321 //
339 G4PolyhedraSide& G4PolyhedraSide::operator=( c << 322 G4PolyhedraSide& G4PolyhedraSide::operator=( const G4PolyhedraSide &source )
340 {                                                 323 {
341   if (this == &source) return *this;              324   if (this == &source) return *this;
342                                                   325   
343   delete cone;                                    326   delete cone;
344   delete [] vecs;                                 327   delete [] vecs;
345   delete [] edges;                                328   delete [] edges;
346                                                   329   
347   CopyStuff( source );                            330   CopyStuff( source );
348                                                   331 
349   return *this;                                   332   return *this;
350 }                                                 333 }
351                                                   334 
                                                   >> 335 
                                                   >> 336 //
352 // CopyStuff                                      337 // CopyStuff
353 //                                                338 //
354 void G4PolyhedraSide::CopyStuff( const G4Polyh << 339 void G4PolyhedraSide::CopyStuff( const G4PolyhedraSide &source )
355 {                                                 340 {
356   //                                              341   //
357   // The simple stuff                             342   // The simple stuff
358   //                                              343   //
                                                   >> 344   numSide    = source.numSide;
359   r[0]    = source.r[0];                          345   r[0]    = source.r[0];
360   r[1]    = source.r[1];                          346   r[1]    = source.r[1];
361   z[0]    = source.z[0];                          347   z[0]    = source.z[0];
362   z[1]    = source.z[1];                          348   z[1]    = source.z[1];
363   numSide   = source.numSide;                  << 
364   startPhi  = source.startPhi;                    349   startPhi  = source.startPhi;
365   deltaPhi  = source.deltaPhi;                    350   deltaPhi  = source.deltaPhi;
366   endPhi    = source.endPhi;                      351   endPhi    = source.endPhi;
367   phiIsOpen = source.phiIsOpen;                   352   phiIsOpen = source.phiIsOpen;
368   allBehind = source.allBehind;                   353   allBehind = source.allBehind;
369                                                   354   
370   lenRZ     = source.lenRZ;                       355   lenRZ     = source.lenRZ;
371   lenPhi[0] = source.lenPhi[0];                   356   lenPhi[0] = source.lenPhi[0];
372   lenPhi[1] = source.lenPhi[1];                   357   lenPhi[1] = source.lenPhi[1];
373   edgeNorm  = source.edgeNorm;                    358   edgeNorm  = source.edgeNorm;
374                                                   359 
375   kCarTolerance = source.kCarTolerance;           360   kCarTolerance = source.kCarTolerance;
376   fSurfaceArea = source.fSurfaceArea;          << 361   
377                                                << 
378   cone = new G4IntersectingCone( *source.cone     362   cone = new G4IntersectingCone( *source.cone );
379                                                   363 
380   //                                              364   //
381   // Duplicate edges                              365   // Duplicate edges
382   //                                              366   //
383   const std::size_t numSides = (numSide > 0) ? << 367   G4int  numEdges = phiIsOpen ? numSide+1 : numSide;
384   const std::size_t numEdges = phiIsOpen ? num << 
385   edges = new G4PolyhedraSideEdge[numEdges];      368   edges = new G4PolyhedraSideEdge[numEdges];
386                                                   369   
387   G4PolyhedraSideEdge *edge = edges,              370   G4PolyhedraSideEdge *edge = edges,
388           *sourceEdge = source.edges;             371           *sourceEdge = source.edges;
389   do    // Loop checking, 13.08.2015, G.Cosmo  << 372   do
390   {                                               373   {
391     *edge = *sourceEdge;                          374     *edge = *sourceEdge;
392   } while( ++sourceEdge, ++edge < edges + numE    375   } while( ++sourceEdge, ++edge < edges + numEdges);
393                                                   376 
394   //                                              377   //
395   // Duplicate vecs                               378   // Duplicate vecs
396   //                                              379   //
397   vecs = new G4PolyhedraSideVec[numSides];     << 380   vecs = new G4PolyhedraSideVec[numSide];
398                                                   381   
399   G4PolyhedraSideVec *vec = vecs,                 382   G4PolyhedraSideVec *vec = vecs,
400          *sourceVec = source.vecs;                383          *sourceVec = source.vecs;
401   do    // Loop checking, 13.08.2015, G.Cosmo  << 384   do
402   {                                               385   {
403     *vec = *sourceVec;                            386     *vec = *sourceVec;
404     vec->edges[0] = edges + (sourceVec->edges[    387     vec->edges[0] = edges + (sourceVec->edges[0] - source.edges);
405     vec->edges[1] = edges + (sourceVec->edges[    388     vec->edges[1] = edges + (sourceVec->edges[1] - source.edges);
406   } while( ++sourceVec, ++vec < vecs + numSide << 389   } while( ++sourceVec, ++vec < vecs + numSide );
407 }                                                 390 }
408                                                   391   
                                                   >> 392 
                                                   >> 393 //
409 // Intersect                                      394 // Intersect
410 //                                                395 //
411 // Decide if a line intersects the face.          396 // Decide if a line intersects the face.
412 //                                                397 //
413 // Arguments:                                     398 // Arguments:
414 //  p    = (in) starting point of line segment    399 //  p    = (in) starting point of line segment
415 //  v    = (in) direction of line segment (ass    400 //  v    = (in) direction of line segment (assumed a unit vector)
416 //  A, B    = (in) 2d transform variables (see    401 //  A, B    = (in) 2d transform variables (see note top of file)
417 //  normSign  = (in) desired sign for dot prod    402 //  normSign  = (in) desired sign for dot product with normal (see below)
418 //  surfTolerance  = (in) minimum distance fro    403 //  surfTolerance  = (in) minimum distance from the surface
419 //  vecs    = (in) Vector set array               404 //  vecs    = (in) Vector set array
420 //  distance  = (out) distance to surface furf    405 //  distance  = (out) distance to surface furfilling all requirements
421 //  distFromSurface = (out) distance from the     406 //  distFromSurface = (out) distance from the surface
422 //  thisNormal  = (out) normal vector of the i    407 //  thisNormal  = (out) normal vector of the intersecting surface
423 //                                                408 //
424 // Return value:                                  409 // Return value:
425 //  true if an intersection is found. Otherwis    410 //  true if an intersection is found. Otherwise, output parameters are
426 //  undefined.                                    411 //  undefined.
427 //                                                412 //
428 // Notes:                                         413 // Notes:
429 // * normSign: if we are "inside" the shape an    414 // * normSign: if we are "inside" the shape and only want to find out how far
430 //   to leave the shape, we only want to consi    415 //   to leave the shape, we only want to consider intersections with surfaces in
431 //   which the trajectory is leaving the shape    416 //   which the trajectory is leaving the shape. Since the normal vectors to the
432 //   surface always point outwards from the in    417 //   surface always point outwards from the inside, this means we want the dot
433 //   product of the trajectory direction v and    418 //   product of the trajectory direction v and the normal of the side normals[i]
434 //   to be positive. Thus, we should specify n    419 //   to be positive. Thus, we should specify normSign as +1.0. Otherwise, if
435 //   we are outside and want to go in, normSig    420 //   we are outside and want to go in, normSign should be set to -1.0.
436 //   Don't set normSign to zero, or you will g    421 //   Don't set normSign to zero, or you will get no intersections!
437 //                                                422 //
438 // * surfTolerance: see notes on argument "sur    423 // * surfTolerance: see notes on argument "surfTolerance" in routine
439 //   "IntersectSidePlane".                        424 //   "IntersectSidePlane".
440 //   ----HOWEVER---- We should *not* apply thi    425 //   ----HOWEVER---- We should *not* apply this surface tolerance if the
441 //   starting point is not within phi or z of     426 //   starting point is not within phi or z of the surface. Specifically,
442 //   if the starting point p angle in x/y plac    427 //   if the starting point p angle in x/y places it on a separate side from the
443 //   intersection or if the starting point p i    428 //   intersection or if the starting point p is outside the z bounds of the
444 //   segment, surfTolerance must be ignored or    429 //   segment, surfTolerance must be ignored or we should *always* accept the
445 //   intersection!                                430 //   intersection! 
446 //   This is simply because the sides do not h    431 //   This is simply because the sides do not have infinite extent.
447 //                                                432 //      
448 //                                                433 //
449 G4bool G4PolyhedraSide::Intersect( const G4Thr << 434 G4bool G4PolyhedraSide::Intersect( const G4ThreeVector &p,
450                                    const G4Thr << 435                                    const G4ThreeVector &v,  
451                                          G4boo    436                                          G4bool outgoing,
452                                          G4dou    437                                          G4double surfTolerance,
453                                          G4dou << 438                                          G4double &distance,
454                                          G4dou << 439                                          G4double &distFromSurface,
455                                          G4Thr << 440                                          G4ThreeVector &normal,
456                                          G4boo << 441                                          G4bool &isAllBehind )
457 {                                                 442 {
458   G4double normSign = outgoing ? +1 : -1;         443   G4double normSign = outgoing ? +1 : -1;
459                                                   444   
460   //                                              445   //
461   // ------------------TO BE IMPLEMENTED------    446   // ------------------TO BE IMPLEMENTED---------------------
462   // Testing the intersection of individual ph    447   // Testing the intersection of individual phi faces is
463   // pretty straight forward. The simple thing    448   // pretty straight forward. The simple thing therefore is to
464   // form a loop and check them all in sequenc    449   // form a loop and check them all in sequence.
465   //                                              450   //
466   // But, I worry about one day someone making    451   // But, I worry about one day someone making
467   // a polygon with a thousands sides. A linea    452   // a polygon with a thousands sides. A linear search
468   // would not be ideal in such a case.           453   // would not be ideal in such a case.
469   //                                              454   //
470   // So, it would be nice to be able to quickl    455   // So, it would be nice to be able to quickly decide
471   // which face would be intersected. One can     456   // which face would be intersected. One can make a very
472   // good guess by using the intersection with    457   // good guess by using the intersection with a cone.
473   // However, this is only reliable in 99% of     458   // However, this is only reliable in 99% of the cases.
474   //                                              459   //
475   // My solution: make a decent guess as to th    460   // My solution: make a decent guess as to the one or
476   // two potential faces might get intersected    461   // two potential faces might get intersected, and then
477   // test them. If we have the wrong face, use    462   // test them. If we have the wrong face, use the test
478   // to make a better guess.                      463   // to make a better guess.
479   //                                              464   //
480   // Since we might have two guesses, form a q    465   // Since we might have two guesses, form a queue of
481   // potential intersecting faces. Keep an arr    466   // potential intersecting faces. Keep an array of 
482   // already tested faces to avoid doing one m    467   // already tested faces to avoid doing one more than
483   // once.                                        468   // once.
484   //                                              469   //
485   // Result: at worst, an iterative search. On    470   // Result: at worst, an iterative search. On average,
486   // a little more than two tests would be req    471   // a little more than two tests would be required.
487   //                                              472   //
488   G4ThreeVector q = p + v;                        473   G4ThreeVector q = p + v;
489                                                   474   
490   G4int face = 0;                                 475   G4int face = 0;
491   G4PolyhedraSideVec* vec = vecs;              << 476   G4PolyhedraSideVec *vec = vecs;
492   do    // Loop checking, 13.08.2015, G.Cosmo  << 477   do
493   {                                               478   {
494     //                                            479     //
495     // Correct normal?                            480     // Correct normal?
496     //                                            481     //
497     G4double dotProd = normSign*v.dot(vec->nor    482     G4double dotProd = normSign*v.dot(vec->normal);
498     if (dotProd <= 0) continue;                   483     if (dotProd <= 0) continue;
499                                                   484   
500     //                                            485     //
501     // Is this face in front of the point alon    486     // Is this face in front of the point along the trajectory?
502     //                                            487     //
503     G4ThreeVector delta = p - vec->center;        488     G4ThreeVector delta = p - vec->center;
504     distFromSurface = -normSign*delta.dot(vec-    489     distFromSurface = -normSign*delta.dot(vec->normal);
505                                                   490     
506     if (distFromSurface < -surfTolerance) cont    491     if (distFromSurface < -surfTolerance) continue;
507                                                   492     
508     //                                            493     //
509     //                            phi             494     //                            phi
510     //      c -------- d           ^              495     //      c -------- d           ^
511     //      |          |           |              496     //      |          |           |
512     //      a -------- b           +---> r/z      497     //      a -------- b           +---> r/z
513     //                                            498     //
514     //                                            499     //
515     // Do we remain on this particular segment    500     // Do we remain on this particular segment?
516     //                                            501     //
517     G4ThreeVector qc = q - vec->edges[1]->corn    502     G4ThreeVector qc = q - vec->edges[1]->corner[0];
518     G4ThreeVector qd = q - vec->edges[1]->corn    503     G4ThreeVector qd = q - vec->edges[1]->corner[1];
519                                                   504     
520     if (normSign*qc.cross(qd).dot(v) < 0) cont    505     if (normSign*qc.cross(qd).dot(v) < 0) continue;
521                                                   506     
522     G4ThreeVector qa = q - vec->edges[0]->corn    507     G4ThreeVector qa = q - vec->edges[0]->corner[0];
523     G4ThreeVector qb = q - vec->edges[0]->corn    508     G4ThreeVector qb = q - vec->edges[0]->corner[1];
524                                                   509     
525     if (normSign*qa.cross(qb).dot(v) > 0) cont    510     if (normSign*qa.cross(qb).dot(v) > 0) continue;
526                                                   511     
527     //                                            512     //
528     // We found the one and only segment we mi    513     // We found the one and only segment we might be intersecting.
529     // Do we remain within r/z bounds?            514     // Do we remain within r/z bounds?
530     //                                            515     //
531                                                   516     
532     if (r[0] > 1/kInfinity && normSign*qa.cros    517     if (r[0] > 1/kInfinity && normSign*qa.cross(qc).dot(v) < 0) return false;
533     if (r[1] > 1/kInfinity && normSign*qb.cros    518     if (r[1] > 1/kInfinity && normSign*qb.cross(qd).dot(v) > 0) return false;
534                                                   519     
535     //                                            520     //
536     // We allow the face to be slightly behind    521     // We allow the face to be slightly behind the trajectory
537     // (surface tolerance) only if the point p    522     // (surface tolerance) only if the point p is within
538     // the vicinity of the face                   523     // the vicinity of the face
539     //                                            524     //
540     if (distFromSurface < 0)                      525     if (distFromSurface < 0)
541     {                                             526     {
542       G4ThreeVector ps = p - vec->center;         527       G4ThreeVector ps = p - vec->center; 
543                                                   528       
544       G4double rz = ps.dot(vec->surfRZ);          529       G4double rz = ps.dot(vec->surfRZ);
545       if (std::fabs(rz) > lenRZ+surfTolerance)    530       if (std::fabs(rz) > lenRZ+surfTolerance) return false; 
546                                                   531 
547       G4double pp = ps.dot(vec->surfPhi);         532       G4double pp = ps.dot(vec->surfPhi);
548       if (std::fabs(pp) > lenPhi[0]+lenPhi[1]* << 533       if (std::fabs(pp) > lenPhi[0] + lenPhi[1]*rz + surfTolerance) return false;
549     }                                             534     }
550                                                   535       
551                                                   536 
552     //                                            537     //
553     // Intersection found. Return answer.         538     // Intersection found. Return answer.
554     //                                            539     //
555     distance = distFromSurface/dotProd;           540     distance = distFromSurface/dotProd;
556     normal = vec->normal;                         541     normal = vec->normal;
557     isAllBehind = allBehind;                      542     isAllBehind = allBehind;
558     return true;                                  543     return true;
559   } while( ++vec, ++face < numSide );             544   } while( ++vec, ++face < numSide );
560                                                   545 
561   //                                              546   //
562   // Oh well. Better luck next time.              547   // Oh well. Better luck next time.
563   //                                              548   //
564   return false;                                   549   return false;
565 }                                                 550 }
566                                                   551 
567 // Distance                                    << 552 
568 //                                             << 553 G4double G4PolyhedraSide::Distance( const G4ThreeVector &p, G4bool outgoing )
569 G4double G4PolyhedraSide::Distance( const G4Th << 
570 {                                                 554 {
571   G4double normSign = outgoing ? -1 : +1;         555   G4double normSign = outgoing ? -1 : +1;
572                                                   556   
573   //                                              557   //
574   // Try the closest phi segment first            558   // Try the closest phi segment first
575   //                                              559   //
576   G4int iPhi = ClosestPhiSegment( GetPhi(p) ); << 560   G4int iPhi = ClosestPhiSegment( p.phi() );
577                                                   561   
578   G4ThreeVector pdotc = p - vecs[iPhi].center;    562   G4ThreeVector pdotc = p - vecs[iPhi].center;
579   G4double normDist = pdotc.dot(vecs[iPhi].nor    563   G4double normDist = pdotc.dot(vecs[iPhi].normal);
580                                                   564   
581   if (normSign*normDist > -0.5*kCarTolerance)     565   if (normSign*normDist > -0.5*kCarTolerance)
582   {                                               566   {
583     return DistanceAway( p, vecs[iPhi], &normD    567     return DistanceAway( p, vecs[iPhi], &normDist );
584   }                                               568   }
585                                                   569 
586   //                                              570   //
587   // Now we have an interesting problem... do     571   // Now we have an interesting problem... do we try to find the
588   // closest facing side??                        572   // closest facing side??
589   //                                              573   //
590   // Considered carefully, the answer is no. W    574   // Considered carefully, the answer is no. We know that if we
591   // are asking for the distance out, we are s    575   // are asking for the distance out, we are supposed to be inside,
592   // and vice versa.                              576   // and vice versa.
593   //                                              577   //
594                                                   578   
595   return kInfinity;                               579   return kInfinity;
596 }                                                 580 }
597                                                   581 
                                                   >> 582 
                                                   >> 583 //
598 // Inside                                         584 // Inside
599 //                                                585 //
600 EInside G4PolyhedraSide::Inside( const G4Three << 586 EInside G4PolyhedraSide::Inside( const G4ThreeVector &p,
601                                        G4doubl    587                                        G4double tolerance, 
602                                        G4doubl << 588                                        G4double *bestDistance )
603 {                                                 589 {
604   //                                              590   //
605   // Which phi segment is closest to this poin    591   // Which phi segment is closest to this point?
606   //                                              592   //
607   G4int iPhi = ClosestPhiSegment( GetPhi(p) ); << 593   G4int iPhi = ClosestPhiSegment( p.phi() );
608                                                   594   
609   G4double norm;                                  595   G4double norm;
610                                                   596   
611   //                                              597   //
612   // Get distance to this segment                 598   // Get distance to this segment
613   //                                              599   //
614   *bestDistance = DistanceToOneSide( p, vecs[i    600   *bestDistance = DistanceToOneSide( p, vecs[iPhi], &norm );
615                                                   601   
616   //                                              602   //
617   // Use distance along normal to decide retur    603   // Use distance along normal to decide return value
618   //                                              604   //
619   if ( (std::fabs(norm) > tolerance) || (*best << 605   if ( (std::fabs(norm) < tolerance) && (*bestDistance < 2.0*tolerance) )
620     return (norm < 0) ? kInside : kOutside;    << 
621   else                                         << 
622     return kSurface;                              606     return kSurface;
                                                   >> 607   else if (norm < 0)
                                                   >> 608     return kInside;
                                                   >> 609   else  
                                                   >> 610     return kOutside;
623 }                                                 611 }
624                                                   612 
                                                   >> 613 
                                                   >> 614 //
625 // Normal                                         615 // Normal
626 //                                                616 //
627 G4ThreeVector G4PolyhedraSide::Normal( const G << 617 G4ThreeVector G4PolyhedraSide::Normal( const G4ThreeVector &p,
628                                              G << 618                                              G4double *bestDistance )
629 {                                                 619 {
630   //                                              620   //
631   // Which phi segment is closest to this poin    621   // Which phi segment is closest to this point?
632   //                                              622   //
633   G4int iPhi = ClosestPhiSegment( GetPhi(p) ); << 623   G4int iPhi = ClosestPhiSegment( p.phi() );
634                                                   624 
635   //                                              625   //
636   // Get distance to this segment                 626   // Get distance to this segment
637   //                                              627   //
638   G4double norm;                                  628   G4double norm;
639   *bestDistance = DistanceToOneSide( p, vecs[i    629   *bestDistance = DistanceToOneSide( p, vecs[iPhi], &norm );
640                                                   630 
641   return vecs[iPhi].normal;                       631   return vecs[iPhi].normal;
642 }                                                 632 }
643                                                   633 
                                                   >> 634 
                                                   >> 635 //
644 // Extent                                         636 // Extent
645 //                                                637 //
646 G4double G4PolyhedraSide::Extent( const G4Thre    638 G4double G4PolyhedraSide::Extent( const G4ThreeVector axis )
647 {                                                 639 {
648   if (axis.perp2() < DBL_MIN)                     640   if (axis.perp2() < DBL_MIN)
649   {                                               641   {
650     //                                            642     //
651     // Special case                               643     // Special case
652     //                                            644     //
653     return axis.z() < 0 ? -cone->ZLo() : cone-    645     return axis.z() < 0 ? -cone->ZLo() : cone->ZHi();
654   }                                               646   }
655                                                   647 
656   G4int iPhi, i1, i2;                             648   G4int iPhi, i1, i2;
657   G4double best;                                  649   G4double best;
658   G4ThreeVector* list[4];                      << 650   G4ThreeVector *list[4];
659                                                   651   
660   //                                              652   //
661   // Which phi segment, if any, does the axis     653   // Which phi segment, if any, does the axis belong to
662   //                                              654   //
663   iPhi = PhiSegment( GetPhi(axis) );           << 655   iPhi = PhiSegment( axis.phi() );
664                                                   656   
665   if (iPhi < 0)                                   657   if (iPhi < 0)
666   {                                               658   {
667     //                                            659     //
668     // No phi segment? Check front edge of fir    660     // No phi segment? Check front edge of first side and
669     // last edge of second side                   661     // last edge of second side
670     //                                            662     //
671     i1 = 0; i2 = numSide-1;                       663     i1 = 0; i2 = numSide-1;
672   }                                               664   }
673   else                                            665   else
674   {                                               666   {
675     //                                            667     //
676     // Check all corners of matching phi side     668     // Check all corners of matching phi side
677     //                                            669     //
678     i1 = iPhi; i2 = iPhi;                         670     i1 = iPhi; i2 = iPhi;
679   }                                               671   }
680                                                   672   
681   list[0] = vecs[i1].edges[0]->corner;            673   list[0] = vecs[i1].edges[0]->corner;
682   list[1] = vecs[i1].edges[0]->corner+1;          674   list[1] = vecs[i1].edges[0]->corner+1;
683   list[2] = vecs[i2].edges[1]->corner;            675   list[2] = vecs[i2].edges[1]->corner;
684   list[3] = vecs[i2].edges[1]->corner+1;          676   list[3] = vecs[i2].edges[1]->corner+1;
685                                                   677         
686   //                                              678   //
687   // Who's biggest?                               679   // Who's biggest?
688   //                                              680   //
689   best = -kInfinity;                              681   best = -kInfinity;
690   G4ThreeVector** vec = list;                  << 682   G4ThreeVector **vec = list;
691   do    // Loop checking, 13.08.2015, G.Cosmo  << 683   do
692   {                                               684   {
693     G4double answer = (*vec)->dot(axis);          685     G4double answer = (*vec)->dot(axis);
694     if (answer > best) best = answer;             686     if (answer > best) best = answer;
695   } while( ++vec < list+4 );                      687   } while( ++vec < list+4 );
696                                                   688   
697   return best;                                    689   return best;
698 }                                                 690 }
699                                                   691 
                                                   >> 692 
                                                   >> 693 //
700 // CalculateExtent                                694 // CalculateExtent
701 //                                                695 //
702 // See notes in G4VCSGface                        696 // See notes in G4VCSGface
703 //                                                697 //
704 void G4PolyhedraSide::CalculateExtent( const E    698 void G4PolyhedraSide::CalculateExtent( const EAxis axis, 
705                                        const G << 699                                        const G4VoxelLimits &voxelLimit,
706                                        const G << 700                                        const G4AffineTransform &transform,
707                                              G << 701                                              G4SolidExtentList &extentList )
708 {                                                 702 {
709   //                                              703   //
710   // Loop over all sides                          704   // Loop over all sides
711   //                                              705   //
712   G4PolyhedraSideVec *vec = vecs;                 706   G4PolyhedraSideVec *vec = vecs;
713   do    // Loop checking, 13.08.2015, G.Cosmo  << 707   do
714   {                                               708   {
715     //                                            709     //
716     // Fill our polygon with the four corners     710     // Fill our polygon with the four corners of
717     // this side, after the specified transfor    711     // this side, after the specified transformation
718     //                                            712     //
719     G4ClippablePolygon polygon;                   713     G4ClippablePolygon polygon;
720                                                   714     
721     polygon.AddVertexInOrder(transform.           715     polygon.AddVertexInOrder(transform.
722                              TransformPoint(ve    716                              TransformPoint(vec->edges[0]->corner[0]));
723     polygon.AddVertexInOrder(transform.           717     polygon.AddVertexInOrder(transform.
724                              TransformPoint(ve    718                              TransformPoint(vec->edges[0]->corner[1]));
725     polygon.AddVertexInOrder(transform.           719     polygon.AddVertexInOrder(transform.
726                              TransformPoint(ve    720                              TransformPoint(vec->edges[1]->corner[1]));
727     polygon.AddVertexInOrder(transform.           721     polygon.AddVertexInOrder(transform.
728                              TransformPoint(ve    722                              TransformPoint(vec->edges[1]->corner[0]));
729                                                   723     
730     //                                            724     //
731     // Get extent                                 725     // Get extent
732     //                                            726     //  
733     if (polygon.PartialClip( voxelLimit, axis     727     if (polygon.PartialClip( voxelLimit, axis ))
734     {                                             728     {
735       //                                          729       //
736       // Get dot product of normal along targe    730       // Get dot product of normal along target axis
737       //                                          731       //
738       polygon.SetNormal( transform.TransformAx    732       polygon.SetNormal( transform.TransformAxis(vec->normal) );
739                                                   733 
740       extentList.AddSurface( polygon );           734       extentList.AddSurface( polygon );
741     }                                             735     }
742   } while( ++vec < vecs+numSide );                736   } while( ++vec < vecs+numSide );
743                                                   737   
744   return;                                         738   return;
745 }                                                 739 }
746                                                   740 
                                                   >> 741 
                                                   >> 742 //
747 // IntersectSidePlane                             743 // IntersectSidePlane
748 //                                                744 //
749 // Decide if a line correctly intersects one s    745 // Decide if a line correctly intersects one side plane of our segment.
750 // It is assumed that the correct side has bee    746 // It is assumed that the correct side has been chosen, and thus only 
751 // the z bounds (of the entire segment) are ch    747 // the z bounds (of the entire segment) are checked.
752 //                                                748 //
753 // normSign - To be multiplied against normal:    749 // normSign - To be multiplied against normal:
754 //            = +1.0 normal is unchanged          750 //            = +1.0 normal is unchanged
755 //            = -1.0 normal is reversed (now p    751 //            = -1.0 normal is reversed (now points inward)
756 //                                                752 //
757 // Arguments:                                     753 // Arguments:
758 //  p    - (in) Point                             754 //  p    - (in) Point
759 //  v    - (in) Direction                         755 //  v    - (in) Direction
760 //  vec    - (in) Description record of the si    756 //  vec    - (in) Description record of the side plane
761 //  normSign  - (in) Sign (+/- 1) to apply to     757 //  normSign  - (in) Sign (+/- 1) to apply to normal
762 //  surfTolerance  - (in) Surface tolerance (g    758 //  surfTolerance  - (in) Surface tolerance (generally > 0, see below)
763 //  distance  - (out) Distance along v to inte    759 //  distance  - (out) Distance along v to intersection
764 //  distFromSurface - (out) Distance from surf    760 //  distFromSurface - (out) Distance from surface normal
765 //                                                761 //
766 // Notes:                                         762 // Notes:
767 //   surfTolerance  - Used to decide if a poin    763 //   surfTolerance  - Used to decide if a point is behind the surface,
768 //        a point is allow to be -surfToleranc    764 //        a point is allow to be -surfTolerance behind the
769 //        surface (as measured along the norma    765 //        surface (as measured along the normal), but *only*
770 //        if the point is within the r/z bound    766 //        if the point is within the r/z bounds + surfTolerance
771 //        of the segment.                         767 //        of the segment.
772 //                                                768 //
773 G4bool G4PolyhedraSide::IntersectSidePlane( co << 769 G4bool G4PolyhedraSide::IntersectSidePlane( const G4ThreeVector &p,
774                                             co << 770                                             const G4ThreeVector &v,
775                                             co << 771                                             const G4PolyhedraSideVec vec,
776                                                   772                                                   G4double normSign, 
777                                                   773                                                   G4double surfTolerance,
778                                                << 774                                                   G4double &distance,
779                                                << 775                                                   G4double &distFromSurface )
780 {                                                 776 {
781   //                                              777   //
782   // Correct normal? Here we have straight sid    778   // Correct normal? Here we have straight sides, and can safely ignore
783   // intersections where the dot product with     779   // intersections where the dot product with the normal is zero.
784   //                                              780   //
785   G4double dotProd = normSign*v.dot(vec.normal    781   G4double dotProd = normSign*v.dot(vec.normal);
786                                                   782   
787   if (dotProd <= 0) return false;                 783   if (dotProd <= 0) return false;
788                                                   784   
789   //                                              785   //
790   // Calculate distance to surface. If the sid    786   // Calculate distance to surface. If the side is too far
791   // behind the point, we must reject it.         787   // behind the point, we must reject it.
792   //                                              788   //
793   G4ThreeVector delta = p - vec.center;           789   G4ThreeVector delta = p - vec.center;
794   distFromSurface = -normSign*delta.dot(vec.no    790   distFromSurface = -normSign*delta.dot(vec.normal);
795                                                   791     
796   if (distFromSurface < -surfTolerance) return    792   if (distFromSurface < -surfTolerance) return false;
797                                                   793 
798   //                                              794   //
799   // Calculate precise distance to intersectio    795   // Calculate precise distance to intersection with the side
800   // (along the trajectory, not normal to the     796   // (along the trajectory, not normal to the surface)
801   //                                              797   //
802   distance = distFromSurface/dotProd;             798   distance = distFromSurface/dotProd;
803                                                   799   
804   //                                              800   //
805   // Do we fall off the r/z extent of the segm    801   // Do we fall off the r/z extent of the segment?
806   //                                              802   //
807   // Calculate this very, very carefully! Why?    803   // Calculate this very, very carefully! Why?
808   //         1. If a RZ end is at R=0, you can    804   //         1. If a RZ end is at R=0, you can't miss!
809   //         2. If you just fall off in RZ, th    805   //         2. If you just fall off in RZ, the answer must
810   //            be consistent with adjacent G4    806   //            be consistent with adjacent G4PolyhedraSide faces.
811   // (2) implies that only variables used by o    807   // (2) implies that only variables used by other G4PolyhedraSide
812   // faces may be used, which includes only: p    808   // faces may be used, which includes only: p, v, and the edge corners.
813   // It also means that one side is a ">" or "    809   // It also means that one side is a ">" or "<", which the other
814   // must be ">=" or "<=". Fortunately, this i    810   // must be ">=" or "<=". Fortunately, this isn't a new problem.
815   // The solution below I borrowed from Joseph    811   // The solution below I borrowed from Joseph O'Rourke,
816   // "Computational Geometry in C (Second Edit    812   // "Computational Geometry in C (Second Edition)"
817   // See: http://cs.smith.edu/~orourke/           813   // See: http://cs.smith.edu/~orourke/
818   //                                              814   //
819   G4ThreeVector ic = p + distance*v - vec.cent    815   G4ThreeVector ic = p + distance*v - vec.center;
820   G4double atRZ = vec.surfRZ.dot(ic);             816   G4double atRZ = vec.surfRZ.dot(ic);
821                                                   817   
822   if (atRZ < 0)                                   818   if (atRZ < 0)
823   {                                               819   {
824     if (r[0]==0) return true;    // Can't miss    820     if (r[0]==0) return true;    // Can't miss!
825                                                   821     
826     if (atRZ < -lenRZ*1.2) return false;  // F    822     if (atRZ < -lenRZ*1.2) return false;  // Forget it! Missed by a mile.
827                                                   823     
828     G4ThreeVector q = p + v;                      824     G4ThreeVector q = p + v;    
829     G4ThreeVector qa = q - vec.edges[0]->corne    825     G4ThreeVector qa = q - vec.edges[0]->corner[0],
830                   qb = q - vec.edges[1]->corne    826                   qb = q - vec.edges[1]->corner[0];
831     G4ThreeVector qacb = qa.cross(qb);            827     G4ThreeVector qacb = qa.cross(qb);
832     if (normSign*qacb.dot(v) < 0) return false    828     if (normSign*qacb.dot(v) < 0) return false;
833                                                   829     
834     if (distFromSurface < 0)                      830     if (distFromSurface < 0)
835     {                                             831     {
836       if (atRZ < -lenRZ-surfTolerance) return     832       if (atRZ < -lenRZ-surfTolerance) return false;
837     }                                             833     }
838   }                                               834   }
839   else if (atRZ > 0)                              835   else if (atRZ > 0)
840   {                                               836   {
841     if (r[1]==0) return true;    // Can't miss    837     if (r[1]==0) return true;    // Can't miss!
842                                                   838     
843     if (atRZ > lenRZ*1.2) return false;  // Mi    839     if (atRZ > lenRZ*1.2) return false;  // Missed by a mile
844                                                   840     
845     G4ThreeVector q = p + v;                      841     G4ThreeVector q = p + v;    
846     G4ThreeVector qa = q - vec.edges[0]->corne    842     G4ThreeVector qa = q - vec.edges[0]->corner[1],
847                   qb = q - vec.edges[1]->corne    843                   qb = q - vec.edges[1]->corner[1];
848     G4ThreeVector qacb = qa.cross(qb);            844     G4ThreeVector qacb = qa.cross(qb);
849     if (normSign*qacb.dot(v) >= 0) return fals    845     if (normSign*qacb.dot(v) >= 0) return false;
850                                                   846     
851     if (distFromSurface < 0)                      847     if (distFromSurface < 0)
852     {                                             848     {
853       if (atRZ > lenRZ+surfTolerance) return f    849       if (atRZ > lenRZ+surfTolerance) return false;
854     }                                             850     }
855   }                                               851   }
856                                                   852 
857   return true;                                    853   return true;
858 }                                                 854 }
859                                                   855 
                                                   >> 856 
                                                   >> 857 //
860 // LineHitsSegments                               858 // LineHitsSegments
861 //                                                859 //
862 // Calculate which phi segments a line interse    860 // Calculate which phi segments a line intersects in three dimensions.
863 // No check is made as to whether the intersec    861 // No check is made as to whether the intersections are within the z bounds of
864 // the segment.                                   862 // the segment.
865 //                                                863 //
866 G4int G4PolyhedraSide::LineHitsSegments( const << 864 G4int G4PolyhedraSide::LineHitsSegments( const G4ThreeVector &p,
867                                          const << 865                                          const G4ThreeVector &v,
868                                                << 866                                                G4int *i1, G4int *i2 )
869 {                                                 867 {
870   G4double s1, s2;                                868   G4double s1, s2;
871   //                                              869   //
872   // First, decide if and where the line inter    870   // First, decide if and where the line intersects the cone
873   //                                              871   //
874   G4int n = cone->LineHitsCone( p, v, &s1, &s2    872   G4int n = cone->LineHitsCone( p, v, &s1, &s2 );
875                                                   873   
876   if (n==0) return 0;                             874   if (n==0) return 0;
877                                                   875   
878   //                                              876   //
879   // Try first intersection.                      877   // Try first intersection.
880   //                                              878   //
881   *i1 = PhiSegment( std::atan2( p.y() + s1*v.y    879   *i1 = PhiSegment( std::atan2( p.y() + s1*v.y(), p.x() + s1*v.x() ) );
882   if (n==1)                                       880   if (n==1)
883   {                                               881   {
884     return (*i1 < 0) ? 0 : 1;                     882     return (*i1 < 0) ? 0 : 1;
885   }                                               883   }
886                                                   884   
887   //                                              885   //
888   // Try second intersection                      886   // Try second intersection
889   //                                              887   //
890   *i2 = PhiSegment( std::atan2( p.y() + s2*v.y    888   *i2 = PhiSegment( std::atan2( p.y() + s2*v.y(), p.x() + s2*v.x() ) );
891   if (*i1 == *i2) return 0;                       889   if (*i1 == *i2) return 0;
892                                                   890   
893   if (*i1 < 0)                                    891   if (*i1 < 0)
894   {                                               892   {
895     if (*i2 < 0) return 0;                        893     if (*i2 < 0) return 0;
896     *i1 = *i2;                                    894     *i1 = *i2;
897     return 1;                                     895     return 1;
898   }                                               896   }
899                                                   897 
900   if (*i2 < 0) return 1;                          898   if (*i2 < 0) return 1;
901                                                   899   
902   return 2;                                       900   return 2;
903 }                                                 901 }
904                                                   902 
                                                   >> 903 
                                                   >> 904 //
905 // ClosestPhiSegment                              905 // ClosestPhiSegment
906 //                                                906 //
907 // Decide which phi segment is closest in phi     907 // Decide which phi segment is closest in phi to the point.
908 // The result is the same as PhiSegment if the    908 // The result is the same as PhiSegment if there is no phi opening.
909 //                                                909 //
910 G4int G4PolyhedraSide::ClosestPhiSegment( G4do    910 G4int G4PolyhedraSide::ClosestPhiSegment( G4double phi0 )
911 {                                                 911 {
912   G4int iPhi = PhiSegment( phi0 );                912   G4int iPhi = PhiSegment( phi0 );
913   if (iPhi >= 0) return iPhi;                     913   if (iPhi >= 0) return iPhi;
914                                                   914   
915   //                                              915   //
916   // Boogers! The points falls inside the phi     916   // Boogers! The points falls inside the phi segment.
917   // Look for the closest point: the start, or    917   // Look for the closest point: the start, or  end
918   //                                              918   //
919   G4double phi = phi0;                            919   G4double phi = phi0;
920                                                   920   
921   while( phi < startPhi )    // Loop checking, << 921   while( phi < startPhi ) phi += twopi;
922     phi += twopi;                              << 
923   G4double d1 = phi-endPhi;                       922   G4double d1 = phi-endPhi;
924                                                   923 
925   while( phi > startPhi )    // Loop checking, << 924   while( phi > startPhi ) phi -= twopi;
926     phi -= twopi;                              << 
927   G4double d2 = startPhi-phi;                     925   G4double d2 = startPhi-phi;
928                                                   926   
929   return (d2 < d1) ? 0 : numSide-1;               927   return (d2 < d1) ? 0 : numSide-1;
930 }                                                 928 }
931                                                   929 
                                                   >> 930 
                                                   >> 931 //
932 // PhiSegment                                     932 // PhiSegment
933 //                                                933 //
934 // Decide which phi segment an angle belongs t    934 // Decide which phi segment an angle belongs to, counting from zero.
935 // A value of -1 indicates that the phi value     935 // A value of -1 indicates that the phi value is outside the shape
936 // (only possible if phiTotal < 360 degrees).     936 // (only possible if phiTotal < 360 degrees).
937 //                                                937 //
938 G4int G4PolyhedraSide::PhiSegment( G4double ph    938 G4int G4PolyhedraSide::PhiSegment( G4double phi0 )
939 {                                                 939 {
940   //                                              940   //
941   // How far are we from phiStart? Come up wit    941   // How far are we from phiStart? Come up with a positive answer
942   // that is less than 2*PI                       942   // that is less than 2*PI
943   //                                              943   //
944   G4double phi = phi0 - startPhi;                 944   G4double phi = phi0 - startPhi;
945   while( phi < 0 )    // Loop checking, 13.08. << 945   while( phi < 0      ) phi += twopi;
946     phi += twopi;                              << 946   while( phi > twopi ) phi -= twopi;
947   while( phi > twopi )    // Loop checking, 13 << 
948     phi -= twopi;                              << 
949                                                   947 
950   //                                              948   //
951   // Divide                                       949   // Divide
952   //                                              950   //
953   auto answer = (G4int)(phi/deltaPhi);         << 951   G4int answer = (G4int)(phi/deltaPhi);
954                                                   952   
955   if (answer >= numSide)                          953   if (answer >= numSide)
956   {                                               954   {
957     if (phiIsOpen)                                955     if (phiIsOpen)
958     {                                             956     {
959       return -1;  // Looks like we missed         957       return -1;  // Looks like we missed
960     }                                             958     }
961     else                                          959     else
962     {                                             960     {
963       answer = numSide-1;  // Probably just ro    961       answer = numSide-1;  // Probably just roundoff
964     }                                             962     }
965   }                                               963   }
966                                                   964   
967   return answer;                                  965   return answer;
968 }                                                 966 }
969                                                   967 
970 // GetPhi                                      << 
971 //                                             << 
972 // Calculate Phi for a given 3-vector (point), << 
973 // same point, in the attempt to avoid consecu << 
974 // quantity                                    << 
975 //                                             << 
976 G4double G4PolyhedraSide::GetPhi( const G4Thre << 
977 {                                              << 
978   G4double val=0.;                             << 
979   G4ThreeVector vphi(G4MT_phphix, G4MT_phphiy, << 
980                                                << 
981   if (vphi != p)                               << 
982   {                                            << 
983     val = p.phi();                             << 
984     G4MT_phphix = p.x(); G4MT_phphiy = p.y();  << 
985     G4MT_phphik = val;                         << 
986   }                                            << 
987   else                                         << 
988   {                                            << 
989     val = G4MT_phphik;                         << 
990   }                                            << 
991   return val;                                  << 
992 }                                              << 
993                                                   968 
                                                   >> 969 //
994 // DistanceToOneSide                              970 // DistanceToOneSide
995 //                                                971 //
996 // Arguments:                                     972 // Arguments:
997 //  p   - (in) Point to check                     973 //  p   - (in) Point to check
998 //  vec   - (in) vector set of this side          974 //  vec   - (in) vector set of this side
999 //  normDist - (out) distance normal to the si    975 //  normDist - (out) distance normal to the side or edge, as appropriate, signed
1000 // Return value = total distance from the sid    976 // Return value = total distance from the side
1001 //                                               977 //
1002 G4double G4PolyhedraSide::DistanceToOneSide(  << 978 G4double G4PolyhedraSide::DistanceToOneSide( const G4ThreeVector &p,
1003                                               << 979                                              const G4PolyhedraSideVec &vec,
1004                                               << 980                                                    G4double *normDist )
1005 {                                                981 {
1006   G4ThreeVector pct = p - vec.center;         << 982   G4ThreeVector pc = p - vec.center;
1007                                                  983   
1008   //                                             984   //
1009   // Get normal distance                         985   // Get normal distance
1010   //                                             986   //
1011   *normDist = vec.normal.dot(pct);            << 987   *normDist = vec.normal.dot(pc);
1012                                                  988 
1013   //                                             989   //
1014   // Add edge penalty                            990   // Add edge penalty
1015   //                                             991   //
1016   return DistanceAway( p, vec, normDist );       992   return DistanceAway( p, vec, normDist );
1017 }                                                993 }
1018                                                  994 
                                                   >> 995 
                                                   >> 996 //
1019 // DistanceAway                                  997 // DistanceAway
1020 //                                               998 //
1021 // Add distance from side edges, if necessary << 999 // Add distance from side edges, if necesssary, to total distance,
1022 // and updates normDist appropriate depending    1000 // and updates normDist appropriate depending on edge normals.
1023 //                                               1001 //
1024 G4double G4PolyhedraSide::DistanceAway( const << 1002 G4double G4PolyhedraSide::DistanceAway( const G4ThreeVector &p,
1025                                         const << 1003                                         const G4PolyhedraSideVec &vec,
1026                                               << 1004                                               G4double *normDist )
1027 {                                                1005 {
1028   G4double distOut2;                             1006   G4double distOut2;
1029   G4ThreeVector pct = p - vec.center;         << 1007   G4ThreeVector pc = p - vec.center;
1030   G4double distFaceNorm = *normDist;             1008   G4double distFaceNorm = *normDist;
1031                                                  1009   
1032   //                                             1010   //
1033   // Okay, are we inside bounds?                 1011   // Okay, are we inside bounds?
1034   //                                             1012   //
1035   G4double pcDotRZ  = pct.dot(vec.surfRZ);    << 1013   G4double pcDotRZ  = pc.dot(vec.surfRZ);
1036   G4double pcDotPhi = pct.dot(vec.surfPhi);   << 1014   G4double pcDotPhi = pc.dot(vec.surfPhi);
1037                                                  1015   
1038   //                                             1016   //
1039   // Go through all permutations.                1017   // Go through all permutations.
1040   //                                             1018   //                                                   Phi
1041   //               |              |              1019   //               |              |                     ^
1042   //           B   |      H       |   E          1020   //           B   |      H       |   E                 |
1043   //        ------[1]------------[3]-----        1021   //        ------[1]------------[3]-----               |
1044   //               |XXXXXXXXXXXXXX|              1022   //               |XXXXXXXXXXXXXX|                     +----> RZ
1045   //           C   |XXXXXXXXXXXXXX|   F          1023   //           C   |XXXXXXXXXXXXXX|   F
1046   //               |XXXXXXXXXXXXXX|              1024   //               |XXXXXXXXXXXXXX|
1047   //        ------[0]------------[2]----         1025   //        ------[0]------------[2]----
1048   //           A   |      G       |   D          1026   //           A   |      G       |   D
1049   //               |              |              1027   //               |              |
1050   //                                             1028   //
1051   // It's real messy, but at least it's quick    1029   // It's real messy, but at least it's quick
1052   //                                             1030   //
1053                                                  1031   
1054   if (pcDotRZ < -lenRZ)                          1032   if (pcDotRZ < -lenRZ)
1055   {                                              1033   {
1056     G4double lenPhiZ = lenPhi[0] - lenRZ*lenP    1034     G4double lenPhiZ = lenPhi[0] - lenRZ*lenPhi[1];
1057     G4double distOutZ = pcDotRZ+lenRZ;           1035     G4double distOutZ = pcDotRZ+lenRZ;
1058     //                                           1036     //
1059     // Below in RZ                               1037     // Below in RZ
1060     //                                           1038     //
1061     if (pcDotPhi < -lenPhiZ)                     1039     if (pcDotPhi < -lenPhiZ)
1062     {                                            1040     {
1063       //                                         1041       //
1064       // ...and below in phi. Find distance t    1042       // ...and below in phi. Find distance to point (A)
1065       //                                         1043       //
1066       G4double distOutPhi = pcDotPhi+lenPhiZ;    1044       G4double distOutPhi = pcDotPhi+lenPhiZ;
1067       distOut2 = distOutPhi*distOutPhi + dist    1045       distOut2 = distOutPhi*distOutPhi + distOutZ*distOutZ;
1068       G4ThreeVector pa = p - vec.edges[0]->co    1046       G4ThreeVector pa = p - vec.edges[0]->corner[0];
1069       *normDist = pa.dot(vec.edges[0]->cornNo    1047       *normDist = pa.dot(vec.edges[0]->cornNorm[0]);
1070     }                                            1048     }
1071     else if (pcDotPhi > lenPhiZ)                 1049     else if (pcDotPhi > lenPhiZ)
1072     {                                            1050     {
1073       //                                         1051       //
1074       // ...and above in phi. Find distance t    1052       // ...and above in phi. Find distance to point (B)
1075       //                                         1053       //
1076       G4double distOutPhi = pcDotPhi-lenPhiZ;    1054       G4double distOutPhi = pcDotPhi-lenPhiZ;
1077       distOut2 = distOutPhi*distOutPhi + dist    1055       distOut2 = distOutPhi*distOutPhi + distOutZ*distOutZ;
1078       G4ThreeVector pb = p - vec.edges[1]->co    1056       G4ThreeVector pb = p - vec.edges[1]->corner[0];
1079       *normDist = pb.dot(vec.edges[1]->cornNo    1057       *normDist = pb.dot(vec.edges[1]->cornNorm[0]);
1080     }                                            1058     }
1081     else                                         1059     else
1082     {                                            1060     {
1083       //                                         1061       //
1084       // ...and inside in phi. Find distance     1062       // ...and inside in phi. Find distance to line (C)
1085       //                                         1063       //
1086       G4ThreeVector pa = p - vec.edges[0]->co    1064       G4ThreeVector pa = p - vec.edges[0]->corner[0];
1087       distOut2 = distOutZ*distOutZ;              1065       distOut2 = distOutZ*distOutZ;
1088       *normDist = pa.dot(vec.edgeNorm[0]);       1066       *normDist = pa.dot(vec.edgeNorm[0]);
1089     }                                            1067     }
1090   }                                              1068   }
1091   else if (pcDotRZ > lenRZ)                      1069   else if (pcDotRZ > lenRZ)
1092   {                                              1070   {
1093     G4double lenPhiZ = lenPhi[0] + lenRZ*lenP    1071     G4double lenPhiZ = lenPhi[0] + lenRZ*lenPhi[1];
1094     G4double distOutZ = pcDotRZ-lenRZ;           1072     G4double distOutZ = pcDotRZ-lenRZ;
1095     //                                           1073     //
1096     // Above in RZ                               1074     // Above in RZ
1097     //                                           1075     //
1098     if (pcDotPhi < -lenPhiZ)                     1076     if (pcDotPhi < -lenPhiZ)
1099     {                                            1077     {
1100       //                                         1078       //
1101       // ...and below in phi. Find distance t    1079       // ...and below in phi. Find distance to point (D)
1102       //                                         1080       //
1103       G4double distOutPhi = pcDotPhi+lenPhiZ;    1081       G4double distOutPhi = pcDotPhi+lenPhiZ;
1104       distOut2 = distOutPhi*distOutPhi + dist    1082       distOut2 = distOutPhi*distOutPhi + distOutZ*distOutZ;
1105       G4ThreeVector pd = p - vec.edges[0]->co    1083       G4ThreeVector pd = p - vec.edges[0]->corner[1];
1106       *normDist = pd.dot(vec.edges[0]->cornNo    1084       *normDist = pd.dot(vec.edges[0]->cornNorm[1]);
1107     }                                            1085     }
1108     else if (pcDotPhi > lenPhiZ)                 1086     else if (pcDotPhi > lenPhiZ)
1109     {                                            1087     {
1110       //                                         1088       //
1111       // ...and above in phi. Find distance t    1089       // ...and above in phi. Find distance to point (E)
1112       //                                         1090       //
1113       G4double distOutPhi = pcDotPhi-lenPhiZ;    1091       G4double distOutPhi = pcDotPhi-lenPhiZ;
1114       distOut2 = distOutPhi*distOutPhi + dist    1092       distOut2 = distOutPhi*distOutPhi + distOutZ*distOutZ;
1115       G4ThreeVector pe = p - vec.edges[1]->co    1093       G4ThreeVector pe = p - vec.edges[1]->corner[1];
1116       *normDist = pe.dot(vec.edges[1]->cornNo    1094       *normDist = pe.dot(vec.edges[1]->cornNorm[1]);
1117     }                                            1095     }
1118     else                                         1096     else
1119     {                                            1097     {
1120       //                                         1098       //
1121       // ...and inside in phi. Find distance     1099       // ...and inside in phi. Find distance to line (F)
1122       //                                         1100       //
1123       distOut2 = distOutZ*distOutZ;              1101       distOut2 = distOutZ*distOutZ;
1124       G4ThreeVector pd = p - vec.edges[0]->co    1102       G4ThreeVector pd = p - vec.edges[0]->corner[1];
1125       *normDist = pd.dot(vec.edgeNorm[1]);       1103       *normDist = pd.dot(vec.edgeNorm[1]);
1126     }                                            1104     }
1127   }                                              1105   }
1128   else                                           1106   else
1129   {                                              1107   {
1130     G4double lenPhiZ = lenPhi[0] + pcDotRZ*le    1108     G4double lenPhiZ = lenPhi[0] + pcDotRZ*lenPhi[1];
1131     //                                           1109     //
1132     // We are inside RZ bounds                   1110     // We are inside RZ bounds
1133     //                                           1111     // 
1134     if (pcDotPhi < -lenPhiZ)                     1112     if (pcDotPhi < -lenPhiZ)
1135     {                                            1113     {
1136       //                                         1114       //
1137       // ...and below in phi. Find distance t    1115       // ...and below in phi. Find distance to line (G)
1138       //                                         1116       //
1139       G4double distOut = edgeNorm*(pcDotPhi+l    1117       G4double distOut = edgeNorm*(pcDotPhi+lenPhiZ);
1140       distOut2 = distOut*distOut;                1118       distOut2 = distOut*distOut;
1141       G4ThreeVector pd = p - vec.edges[0]->co    1119       G4ThreeVector pd = p - vec.edges[0]->corner[1];
1142       *normDist = pd.dot(vec.edges[0]->normal    1120       *normDist = pd.dot(vec.edges[0]->normal);
1143     }                                            1121     }
1144     else if (pcDotPhi > lenPhiZ)                 1122     else if (pcDotPhi > lenPhiZ)
1145     {                                            1123     {
1146       //                                         1124       //
1147       // ...and above in phi. Find distance t    1125       // ...and above in phi. Find distance to line (H)
1148       //                                         1126       //
1149       G4double distOut = edgeNorm*(pcDotPhi-l    1127       G4double distOut = edgeNorm*(pcDotPhi-lenPhiZ);
1150       distOut2 = distOut*distOut;                1128       distOut2 = distOut*distOut;
1151       G4ThreeVector pe = p - vec.edges[1]->co    1129       G4ThreeVector pe = p - vec.edges[1]->corner[1];
1152       *normDist = pe.dot(vec.edges[1]->normal    1130       *normDist = pe.dot(vec.edges[1]->normal);
1153     }                                            1131     }
1154     else                                         1132     else
1155     {                                            1133     {
1156       //                                         1134       //
1157       // Inside bounds! No penalty.              1135       // Inside bounds! No penalty.
1158       //                                         1136       //
1159       return std::fabs(distFaceNorm);            1137       return std::fabs(distFaceNorm);
1160     }                                            1138     }
1161   }                                              1139   }
1162   return std::sqrt( distFaceNorm*distFaceNorm    1140   return std::sqrt( distFaceNorm*distFaceNorm + distOut2 );
1163 }                                             << 
1164                                               << 
1165 // Calculation of surface area of a triangle. << 
1166 // At the same time a random point in the tri << 
1167 //                                            << 
1168 G4double G4PolyhedraSide::SurfaceTriangle( co << 
1169                                            co << 
1170                                            co << 
1171                                            G4 << 
1172 {                                             << 
1173   G4ThreeVector v, w;                         << 
1174                                               << 
1175   v = p3 - p1;                                << 
1176   w = p1 - p2;                                << 
1177   G4double lambda1 = G4UniformRand();         << 
1178   G4double lambda2 = lambda1*G4UniformRand(); << 
1179                                               << 
1180   *p4=p2 + lambda1*w + lambda2*v;             << 
1181   return 0.5*(v.cross(w)).mag();              << 
1182 }                                             << 
1183                                               << 
1184 // GetPointOnPlane                            << 
1185 //                                            << 
1186 // Auxiliary method for GetPointOnSurface()   << 
1187 //                                            << 
1188 G4ThreeVector                                 << 
1189 G4PolyhedraSide::GetPointOnPlane( const G4Thr << 
1190                                   const G4Thr << 
1191                                   G4double* A << 
1192 {                                             << 
1193   G4double chose,aOne,aTwo;                   << 
1194   G4ThreeVector point1,point2;                << 
1195   aOne = SurfaceTriangle(p0,p1,p2,&point1);   << 
1196   aTwo = SurfaceTriangle(p2,p3,p0,&point2);   << 
1197   *Area= aOne+aTwo;                           << 
1198                                               << 
1199   chose = G4UniformRand()*(aOne+aTwo);        << 
1200   if( (chose>=0.) && (chose < aOne) )         << 
1201   {                                           << 
1202    return (point1);                           << 
1203   }                                           << 
1204   return (point2);                            << 
1205 }                                             << 
1206                                               << 
1207 // SurfaceArea()                              << 
1208 //                                            << 
1209 G4double G4PolyhedraSide::SurfaceArea()       << 
1210 {                                             << 
1211   if( fSurfaceArea==0. )                      << 
1212   {                                           << 
1213     // Define the variables                   << 
1214     //                                        << 
1215     G4double area,areas;                      << 
1216     G4ThreeVector point1;                     << 
1217     G4ThreeVector v1,v2,v3,v4;                << 
1218     G4PolyhedraSideVec* vec = vecs;           << 
1219     areas=0.;                                 << 
1220                                               << 
1221     // Do a loop on all SideEdge              << 
1222     //                                        << 
1223     do    // Loop checking, 13.08.2015, G.Cos << 
1224     {                                         << 
1225       // Define 4points for a Plane or Triang << 
1226       //                                      << 
1227       v1=vec->edges[0]->corner[0];            << 
1228       v2=vec->edges[0]->corner[1];            << 
1229       v3=vec->edges[1]->corner[1];            << 
1230       v4=vec->edges[1]->corner[0];            << 
1231       point1=GetPointOnPlane(v1,v2,v3,v4,&are << 
1232       areas+=area;                            << 
1233     } while( ++vec < vecs + numSide);         << 
1234                                               << 
1235     fSurfaceArea=areas;                       << 
1236   }                                           << 
1237   return fSurfaceArea;                        << 
1238 }                                             << 
1239                                               << 
1240 // GetPointOnFace()                           << 
1241 //                                            << 
1242 G4ThreeVector G4PolyhedraSide::GetPointOnFace << 
1243 {                                             << 
1244   // Define the variables                     << 
1245   //                                          << 
1246   std::vector<G4double>areas;                 << 
1247   std::vector<G4ThreeVector>points;           << 
1248   G4double area=0.;                           << 
1249   G4double result1;                           << 
1250   G4ThreeVector point1;                       << 
1251   G4ThreeVector v1,v2,v3,v4;                  << 
1252   G4PolyhedraSideVec* vec = vecs;             << 
1253                                               << 
1254   // Do a loop on all SideEdge                << 
1255   //                                          << 
1256   do    // Loop checking, 13.08.2015, G.Cosmo << 
1257   {                                           << 
1258     // Define 4points for a Plane or Triangle << 
1259     //                                        << 
1260     v1=vec->edges[0]->corner[0];              << 
1261     v2=vec->edges[0]->corner[1];              << 
1262     v3=vec->edges[1]->corner[1];              << 
1263     v4=vec->edges[1]->corner[0];              << 
1264     point1=GetPointOnPlane(v1,v2,v3,v4,&resul << 
1265     points.push_back(point1);                 << 
1266     areas.push_back(result1);                 << 
1267     area+=result1;                            << 
1268   } while( ++vec < vecs+numSide );            << 
1269                                               << 
1270   // Choose randomly one of the surfaces and  << 
1271   //                                          << 
1272   G4double chose = area*G4UniformRand();      << 
1273   G4double Achose1=0., Achose2=0.;            << 
1274   G4int i=0;                                  << 
1275   do    // Loop checking, 13.08.2015, G.Cosmo << 
1276   {                                           << 
1277     Achose2+=areas[i];                        << 
1278     if(chose>=Achose1 && chose<Achose2)       << 
1279     {                                         << 
1280       point1=points[i] ; break;               << 
1281     }                                         << 
1282     ++i; Achose1=Achose2;                     << 
1283   } while( i<numSide );                       << 
1284                                               << 
1285   return point1;                              << 
1286 }                                                1141 }
1287                                                  1142