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 5.0.p1)


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