Geant4 Cross Reference

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


  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 // G4ReduciblePolygon implementation; a utilit <<  23 //
 27 // test, reduce, and/or otherwise manipulate a <<  24 // $Id: G4ReduciblePolygon.cc,v 1.6 2002/10/30 13:52:23 gcosmo Exp $
                                                   >>  25 // GEANT4 tag $Name: geant4-05-00 $
                                                   >>  26 //
                                                   >>  27 // 
                                                   >>  28 // --------------------------------------------------------------------
                                                   >>  29 // GEANT 4 class source file
                                                   >>  30 //
                                                   >>  31 //
                                                   >>  32 // G4ReduciblePolygon.cc
                                                   >>  33 //
                                                   >>  34 // Implementation of a utility class used to specify, test, reduce,
                                                   >>  35 // and/or otherwise manipulate a 2D polygon.
                                                   >>  36 //
 28 // See G4ReduciblePolygon.hh for more info.        37 // See G4ReduciblePolygon.hh for more info.
 29 //                                                 38 //
 30 // Author: David C. Williams (davidw@scipp.ucs << 
 31 // -------------------------------------------     39 // --------------------------------------------------------------------
 32                                                    40 
 33 #include "G4ReduciblePolygon.hh"               << 
 34 #include "globals.hh"                              41 #include "globals.hh"
                                                   >>  42 #include "G4ReduciblePolygon.hh"
 35                                                    43 
                                                   >>  44 //
 36 // Constructor: with simple arrays                 45 // Constructor: with simple arrays
 37 //                                                 46 //
 38 G4ReduciblePolygon::G4ReduciblePolygon( const      47 G4ReduciblePolygon::G4ReduciblePolygon( const G4double a[],
 39                                         const      48                                         const G4double b[],
 40                                                    49                                               G4int n )
 41   : aMin(0.), aMax(0.), bMin(0.), bMax(0.)     <<  50   : aMin(0.), aMax(0.), bMin(0.), bMax(0.),
                                                   >>  51     vertexHead(0)
 42 {                                                  52 {
 43   //                                               53   //
 44   // Do all of the real work in Create             54   // Do all of the real work in Create
 45   //                                               55   //
 46   Create( a, b, n );                               56   Create( a, b, n );
 47 }                                                  57 }
 48                                                    58 
                                                   >>  59 
                                                   >>  60 //
 49 // Constructor: special PGON/PCON case             61 // Constructor: special PGON/PCON case
 50 //                                                 62 //
 51 G4ReduciblePolygon::G4ReduciblePolygon( const      63 G4ReduciblePolygon::G4ReduciblePolygon( const G4double rmin[],
 52                                         const      64                                         const G4double rmax[], 
 53                                         const      65                                         const G4double z[], G4int n )
 54   : aMin(0.), aMax(0.), bMin(0.), bMax(0.)     <<  66   : aMin(0.), aMax(0.), bMin(0.), bMax(0.),
                                                   >>  67     vertexHead(0)
 55 {                                                  68 {
 56   //                                               69   //
 57   // Translate                                     70   // Translate
 58   //                                               71   //
 59   auto a = new G4double[n*2];                  <<  72   G4double *a = new G4double[n*2];
 60   auto b = new G4double[n*2];                  <<  73   G4double *b = new G4double[n*2];
 61                                                    74   
 62   G4double *rOut = a + n,                          75   G4double *rOut = a + n,
 63            *zOut = b + n,                          76            *zOut = b + n,
 64             *rIn = rOut-1,                         77             *rIn = rOut-1,
 65             *zIn = zOut-1;                         78             *zIn = zOut-1;
 66                                                    79   
 67   for( G4int i=0; i < n; ++i, ++rOut, ++zOut,  <<  80   G4int i;   
                                                   >>  81   for( i=0; i < n; i++, rOut++, zOut++, rIn--, zIn-- )
 68   {                                                82   {
 69     *rOut = rmax[i];                               83     *rOut = rmax[i];
 70     *rIn  = rmin[i];                               84     *rIn  = rmin[i];
 71     *zOut = *zIn = z[i];                           85     *zOut = *zIn = z[i];
 72   }                                                86   }
 73                                                    87   
 74   Create( a, b, n*2 );                             88   Create( a, b, n*2 );
 75                                                    89   
 76   delete [] a;                                     90   delete [] a;
 77   delete [] b;                                     91   delete [] b;
 78 }                                                  92 }
 79                                                    93 
                                                   >>  94 
                                                   >>  95 //
 80 // Create                                          96 // Create
 81 //                                                 97 //
 82 // To be called by constructors, fill in the l     98 // To be called by constructors, fill in the list and statistics for a new
 83 // polygon                                         99 // polygon
 84 //                                                100 //
 85 void G4ReduciblePolygon::Create( const G4doubl    101 void G4ReduciblePolygon::Create( const G4double a[],
 86                                  const G4doubl    102                                  const G4double b[], G4int n )
 87 {                                                 103 {
 88   if (n<3)                                        104   if (n<3)
 89    G4Exception("G4ReduciblePolygon::Create()", << 105    G4Exception("G4ReduciblePolygon::Create() - less than 3 vertices specified");
 90                FatalErrorInArgument, "Less tha << 
 91                                                   106   
 92   const G4double *anext = a, *bnext = b;          107   const G4double *anext = a, *bnext = b;
 93   ABVertex* prev = nullptr;                    << 108   ABVertex *prev = 0;
 94   do    // Loop checking, 13.08.2015, G.Cosmo  << 109   do
 95   {                                               110   {
 96     auto newVertex = new ABVertex;             << 111     ABVertex *newVertex = new ABVertex;
 97     newVertex->a = *anext;                        112     newVertex->a = *anext;
 98     newVertex->b = *bnext;                        113     newVertex->b = *bnext;
 99     newVertex->next = nullptr;                 << 114     newVertex->next = 0;
100     if (prev==nullptr)                         << 115     if (prev==0)
101     {                                             116     {
102       vertexHead = newVertex;                     117       vertexHead = newVertex;
103     }                                             118     }
104     else                                          119     else
105     {                                             120     {
106       prev->next = newVertex;                     121       prev->next = newVertex;
107     }                                             122     }
108                                                   123       
109     prev = newVertex;                             124     prev = newVertex;
110   } while( ++anext, ++bnext < b+n );              125   } while( ++anext, ++bnext < b+n );
111                                                   126 
112   numVertices = n;                                127   numVertices = n;
113                                                   128   
114   CalculateMaxMin();                              129   CalculateMaxMin();
115 }                                                 130 }
116                                                   131 
117 // Fake default constructor - sets only member << 
118 //                            for usage restri << 
119 //                                             << 
120 G4ReduciblePolygon::G4ReduciblePolygon( __void << 
121   : aMin(0.), aMax(0.), bMin(0.), bMax(0.)     << 
122 {                                              << 
123 }                                              << 
124                                                << 
125                                                   132 
126 //                                                133 //
127 // Destructor                                     134 // Destructor
128 //                                                135 //
129 G4ReduciblePolygon::~G4ReduciblePolygon()         136 G4ReduciblePolygon::~G4ReduciblePolygon()
130 {                                                 137 {
131   ABVertex* curr = vertexHead;                 << 138   ABVertex *curr = vertexHead;
132   while( curr != nullptr )    // Loop checking << 139   while( curr )
133   {                                               140   {
134     ABVertex* toDelete = curr;                 << 141     ABVertex *toDelete = curr;
135     curr = curr->next;                            142     curr = curr->next;
136     delete toDelete;                              143     delete toDelete;
137   }                                               144   }
138 }                                                 145 }
139                                                   146 
                                                   >> 147 
                                                   >> 148 //
140 // CopyVertices                                   149 // CopyVertices
141 //                                                150 //
142 // Copy contents into simple linear arrays.       151 // Copy contents into simple linear arrays.
143 // ***** CAUTION ***** Be care to declare the     152 // ***** CAUTION ***** Be care to declare the arrays to a large
144 // enough size!                                   153 // enough size!
145 //                                                154 //
146 void G4ReduciblePolygon::CopyVertices( G4doubl    155 void G4ReduciblePolygon::CopyVertices( G4double a[], G4double b[] ) const
147 {                                                 156 {
148   G4double *anext = a, *bnext = b;                157   G4double *anext = a, *bnext = b;
149   ABVertex *curr = vertexHead;                    158   ABVertex *curr = vertexHead;
150   while( curr != nullptr )    // Loop checking << 159   while( curr )
151   {                                               160   {
152     *anext++ = curr->a;                           161     *anext++ = curr->a;
153     *bnext++ = curr->b;                           162     *bnext++ = curr->b;
154     curr = curr->next;                            163     curr = curr->next;
155   }                                               164   }
156 }                                                 165 }
157                                                   166 
                                                   >> 167 
                                                   >> 168 //
158 // ScaleA                                         169 // ScaleA
159 //                                                170 //
160 // Multiply all a values by a common scale        171 // Multiply all a values by a common scale
161 //                                                172 //
162 void G4ReduciblePolygon::ScaleA( G4double scal    173 void G4ReduciblePolygon::ScaleA( G4double scale )
163 {                                                 174 {
164   ABVertex* curr = vertexHead;                 << 175   ABVertex *curr = vertexHead;
165   while( curr != nullptr )    // Loop checking << 176   while( curr )
166   {                                               177   {
167     curr->a *= scale;                             178     curr->a *= scale;
168     curr = curr->next;                            179     curr = curr->next;
169   }                                               180   }
170 }                                                 181 }  
171                                                   182 
                                                   >> 183 
                                                   >> 184 //
172 // ScaleB                                         185 // ScaleB
173 //                                                186 //
174 // Multiply all b values by a common scale        187 // Multiply all b values by a common scale
175 //                                                188 //
176 void G4ReduciblePolygon::ScaleB( G4double scal    189 void G4ReduciblePolygon::ScaleB( G4double scale )
177 {                                                 190 {
178   ABVertex* curr = vertexHead;                 << 191   ABVertex *curr = vertexHead;
179   while( curr != nullptr )    // Loop checking << 192   while( curr )
180   {                                               193   {
181     curr->b *= scale;                             194     curr->b *= scale;
182     curr = curr->next;                            195     curr = curr->next;
183   }                                               196   }
184 }                                                 197 }  
185                                                   198 
                                                   >> 199 
                                                   >> 200 //
186 // RemoveDuplicateVertices                        201 // RemoveDuplicateVertices
187 //                                                202 //
188 // Remove adjacent vertices that are equal. Re    203 // Remove adjacent vertices that are equal. Returns "false" if there
189 // is a problem (too few vertices remaining).     204 // is a problem (too few vertices remaining).
190 //                                                205 //
191 G4bool G4ReduciblePolygon::RemoveDuplicateVert    206 G4bool G4ReduciblePolygon::RemoveDuplicateVertices( G4double tolerance )
192 {                                                 207 {
193   ABVertex *curr = vertexHead,                    208   ABVertex *curr = vertexHead, 
194            *prev = nullptr, *next = nullptr;   << 209            *prev = 0,
195   while( curr != nullptr )    // Loop checking << 210            *next = curr->next;  // A little dangerous
                                                   >> 211   while( curr )
196   {                                               212   {
197     next = curr->next;                            213     next = curr->next;
198     if (next == nullptr) next = vertexHead;    << 214     if (next == 0) next = vertexHead;
199                                                   215     
200     if (std::fabs(curr->a-next->a) < tolerance << 216     if (fabs(curr->a-next->a) < tolerance &&
201         std::fabs(curr->b-next->b) < tolerance << 217         fabs(curr->b-next->b) < tolerance     )
202     {                                             218     {
203       //                                          219       //
204       // Duplicate found: do we have > 3 verti    220       // Duplicate found: do we have > 3 vertices?
205       //                                          221       //
206       if (numVertices <= 3)                       222       if (numVertices <= 3)
207       {                                           223       {
208         CalculateMaxMin();                        224         CalculateMaxMin();
209         return false;                             225         return false;
210       }                                           226       }
211                                                   227       
212       //                                          228       //
213       // Delete                                   229       // Delete
214       //                                          230       //
215       ABVertex* toDelete = curr;               << 231       ABVertex *toDelete = curr;
216       curr = curr->next;                          232       curr = curr->next;
217       delete toDelete;                            233       delete toDelete;
218                                                   234       
219       numVertices--;                              235       numVertices--;
220                                                   236       
221       if (prev != nullptr)                     << 237       if (prev) prev->next = curr; else vertexHead = curr;
222         prev->next = curr;                     << 
223       else                                     << 
224         vertexHead = curr;                     << 
225     }                                             238     }
226     else                                          239     else
227     {                                             240     {
228       prev = curr;                                241       prev = curr;
229       curr = curr->next;                          242       curr = curr->next;
230     }                                             243     }
231   }                                               244   }
232                                                   245   
233   //                                              246   //
234   // In principle, this is not needed, but why    247   // In principle, this is not needed, but why not just play it safe?
235   //                                              248   //
236   CalculateMaxMin();                              249   CalculateMaxMin();
237                                                   250   
238   return true;                                    251   return true;
239 }                                                 252 }
240                                                   253 
                                                   >> 254 
                                                   >> 255 //
241 // RemoveRedundantVertices                        256 // RemoveRedundantVertices
242 //                                                257 //
243 // Remove any unneeded vertices, i.e. those ve    258 // Remove any unneeded vertices, i.e. those vertices which
244 // are on the line connecting the previous and    259 // are on the line connecting the previous and next vertices.
245 //                                                260 //
246 G4bool G4ReduciblePolygon::RemoveRedundantVert    261 G4bool G4ReduciblePolygon::RemoveRedundantVertices( G4double tolerance )
247 {                                                 262 {
248   //                                              263   //
249   // Under these circumstances, we can quit no    264   // Under these circumstances, we can quit now!
250   //                                              265   //
251   if (numVertices <= 2) return false;             266   if (numVertices <= 2) return false;
252                                                   267   
253   G4double tolerance2 = tolerance*tolerance;      268   G4double tolerance2 = tolerance*tolerance;
254                                                   269 
255   //                                              270   //
256   // Loop over all vertices                       271   // Loop over all vertices
257   //                                              272   //
258   ABVertex *curr = vertexHead, *next = nullptr << 273   ABVertex *curr = vertexHead, 
259   while( curr != nullptr )    // Loop checking << 274            *next = curr->next;  // A little dangerous
                                                   >> 275   while( curr )
260   {                                               276   {
261     next = curr->next;                            277     next = curr->next;
262     if (next == nullptr) next = vertexHead;    << 278     if (next == 0) next = vertexHead;
263                                                   279     
264     G4double da = next->a - curr->a,              280     G4double da = next->a - curr->a,
265              db = next->b - curr->b;              281              db = next->b - curr->b;
266                                                   282     
267     //                                            283     //
268     // Loop over all subsequent vertices, up t    284     // Loop over all subsequent vertices, up to curr
269     //                                            285     //
270     for(;;)                                       286     for(;;)
271     {                                             287     {
272       //                                          288       //
273       // Get vertex after next                    289       // Get vertex after next
274       //                                          290       //
275       ABVertex* test = next->next;             << 291       ABVertex *test = next->next;
276       if (test == nullptr) test = vertexHead;  << 292       if (test == 0) test = vertexHead;
277                                                   293       
278       //                                          294       //
279       // If we are back to the original vertex    295       // If we are back to the original vertex, stop
280       //                                          296       //
281       if (test==curr) break;                      297       if (test==curr) break;
282                                                   298     
283       //                                          299       //
284       // Test for parallel line segments          300       // Test for parallel line segments
285       //                                          301       //
286       G4double dat = test->a - curr->a,           302       G4double dat = test->a - curr->a,
287                dbt = test->b - curr->b;           303                dbt = test->b - curr->b;
288                                                   304          
289       if (std::fabs(dat*db-dbt*da)>tolerance2) << 305       if (fabs(dat*db-dbt*da)>tolerance2) break;
290                                                   306       
291       //                                          307       //
292       // Redundant vertex found: do we have >     308       // Redundant vertex found: do we have > 3 vertices?
293       //                                          309       // 
294       if (numVertices <= 3)                       310       if (numVertices <= 3)
295       {                                           311       {
296         CalculateMaxMin();                        312         CalculateMaxMin();
297         return false;                             313         return false;
298       }                                           314       }
299                                                   315 
300       //                                          316       //
301       // Delete vertex pointed to by next. Car    317       // Delete vertex pointed to by next. Carefully!
302       //                                          318       //
303       if (curr->next != nullptr)               << 319       if (curr->next)
304       {    // next is not head                    320       {    // next is not head
305         if (next->next != nullptr)             << 321         if (next->next)
306           curr->next = test;  // next is not t    322           curr->next = test;  // next is not tail
307         else                                      323         else
308           curr->next = nullptr;    // New tail << 324           curr->next = 0;    // New tail
309       }                                           325       }
310       else                                        326       else
311         vertexHead = test;  // New head           327         vertexHead = test;  // New head
312                                                   328         
313       if ((curr != next) && (next != test)) de << 329       delete next;
314                                                   330       
315       --numVertices;                           << 331       numVertices--;
316                                                   332       
317       //                                          333       //
318       // Replace next by the vertex we just te    334       // Replace next by the vertex we just tested,
319       // and keep on going...                     335       // and keep on going...
320       //                                          336       //
321       next = test;                                337       next = test;
322       da = dat; db = dbt;                         338       da = dat; db = dbt;
323     }                                             339     }
324     curr = curr->next;                            340     curr = curr->next;
325   }                                               341   }
326                                                   342   
327   //                                              343   //
328   // In principle, this is not needed, but why    344   // In principle, this is not needed, but why not just play it safe?
329   //                                              345   //
330   CalculateMaxMin();                              346   CalculateMaxMin();
331                                                   347   
332   return true;                                    348   return true;
333 }                                                 349 }
334                                                   350 
                                                   >> 351 
                                                   >> 352 //
335 // ReverseOrder                                   353 // ReverseOrder
336 //                                                354 //
337 // Reverse the order of the vertices              355 // Reverse the order of the vertices
338 //                                                356 //
339 void G4ReduciblePolygon::ReverseOrder()           357 void G4ReduciblePolygon::ReverseOrder()
340 {                                                 358 {
341   //                                              359   //
342   // Loop over all vertices                       360   // Loop over all vertices
343   //                                              361   //
344   ABVertex* prev = vertexHead;                 << 362   ABVertex *prev = vertexHead;
345   if (prev==nullptr) return;    // No vertices << 363   if (prev==0) return;    // No vertices
346                                                   364   
347   ABVertex* curr = prev->next;                 << 365   ABVertex *curr = prev->next;
348   if (curr==nullptr) return;    // Just one ve << 366   if (curr==0) return;    // Just one vertex
349                                                   367   
350   //                                              368   //
351   // Our new tail                                 369   // Our new tail
352   //                                              370   //
353   vertexHead->next = nullptr;                  << 371   vertexHead->next = 0;
354                                                   372   
355   for(;;)                                         373   for(;;)
356   {                                               374   {
357     //                                            375     //
358     // Save pointer to next vertex (in origina    376     // Save pointer to next vertex (in original order)
359     //                                            377     //
360     ABVertex *save = curr->next;                  378     ABVertex *save = curr->next;
361                                                   379     
362     //                                            380     //
363     // Replace it with a pointer to the previo    381     // Replace it with a pointer to the previous one
364     // (in original order)                        382     // (in original order)
365     //                                            383     //
366     curr->next = prev;                            384     curr->next = prev;
367                                                   385     
368     //                                            386     //
369     // Last vertex?                               387     // Last vertex?
370     //                                            388     //
371     if (save == nullptr) break;                << 389     if (save == 0) break;
372                                                   390     
373     //                                            391     //
374     // Next vertex                                392     // Next vertex
375     //                                            393     //
376     prev = curr;                                  394     prev = curr;
377     curr = save;                                  395     curr = save;
378   }                                               396   }
379                                                   397   
380   //                                              398   //
381   // Our new head                                 399   // Our new head
382   //                                              400   //
383   vertexHead = curr;                              401   vertexHead = curr;
384 }                                                 402 }
385                                                   403 
386                                                   404 
387 // StartWithZMin                               << 
388 //                                             << 
389 // Starting alway with Zmin=bMin               << 
390 // This method is used for GenericPolycone     << 
391 //                                                405 //
392 void G4ReduciblePolygon::StartWithZMin()       << 
393 {                                              << 
394   ABVertex* curr = vertexHead;                 << 
395   G4double bcurr = curr->b;                    << 
396   ABVertex* prev = curr;                       << 
397   while( curr != nullptr)    // Loop checking, << 
398   {                                            << 
399     if(curr->b < bcurr)                        << 
400     {                                          << 
401       bcurr = curr->b;                         << 
402       ABVertex* curr1 = curr;                  << 
403       while( curr1 != nullptr )    // Loop che << 
404       {                                        << 
405         if(curr1->next == nullptr) { curr1->ne << 
406         curr1 = curr1->next;                   << 
407       }                                        << 
408       vertexHead = curr;                       << 
409       prev->next = nullptr;                    << 
410     }                                          << 
411     prev = curr;                               << 
412     curr = curr->next;                         << 
413   }                                            << 
414 }                                              << 
415                                                << 
416 // CrossesItself                                  406 // CrossesItself
417 //                                                407 //
418 // Return "true" if the polygon crosses itself    408 // Return "true" if the polygon crosses itself
419 //                                                409 //
420 // Warning: this routine is not very fast (run    410 // Warning: this routine is not very fast (runs as N**2)
421 //                                                411 //
422 G4bool G4ReduciblePolygon::CrossesItself( G4do    412 G4bool G4ReduciblePolygon::CrossesItself( G4double tolerance )
423 {                                                 413 {
424   G4double tolerance2 = tolerance*tolerance;      414   G4double tolerance2 = tolerance*tolerance;
425   G4double one  = 1.0-tolerance,               << 415   G4double one = 1.0-tolerance,
426            zero = tolerance;                   << 416      zero = tolerance;
427   //                                              417   //
428   // Top loop over line segments. By the time     418   // Top loop over line segments. By the time we finish
429   // with the second to last segment, we're do    419   // with the second to last segment, we're done.
430   //                                              420   //
431   ABVertex *curr1 = vertexHead, *next1 = nullp << 421   ABVertex *curr1 = vertexHead, *next1=0;
432   while (curr1->next != nullptr)    // Loop ch << 422   while (curr1->next) {
433   {                                            << 423           next1 = curr1->next;
434     next1 = curr1->next;                       << 
435     G4double da1 = next1->a-curr1->a,             424     G4double da1 = next1->a-curr1->a,
436              db1 = next1->b-curr1->b;          << 425        db1 = next1->b-curr1->b;
437                                                   426     
438     //                                            427     //
439     // Inner loop over subsequent line segment    428     // Inner loop over subsequent line segments
440     //                                            429     //
441     ABVertex* curr2 = next1->next;             << 430     ABVertex *curr2 = next1->next;
442     while( curr2 != nullptr )    // Loop check << 431     while( curr2 ) {
443     {                                          << 432       ABVertex *next2 = curr2->next;
444       ABVertex* next2 = curr2->next;           << 433       if (next2==0) next2 = vertexHead;
445       if (next2==nullptr) next2 = vertexHead;  << 
446       G4double da2 = next2->a-curr2->a,           434       G4double da2 = next2->a-curr2->a,
447                db2 = next2->b-curr2->b;        << 435          db2 = next2->b-curr2->b;
448       G4double a12 = curr2->a-curr1->a,           436       G4double a12 = curr2->a-curr1->a,
449                b12 = curr2->b-curr1->b;        << 437          b12 = curr2->b-curr1->b;
450                                                   438          
451       //                                          439       //
452       // Calculate intersection of the two lin    440       // Calculate intersection of the two lines
453       //                                          441       //
454       G4double deter = da1*db2 - db1*da2;         442       G4double deter = da1*db2 - db1*da2;
455       if (std::fabs(deter) > tolerance2)       << 443       if (fabs(deter) > tolerance2) {
456       {                                        << 
457         G4double s1, s2;                          444         G4double s1, s2;
458         s1 = (a12*db2-b12*da2)/deter;             445         s1 = (a12*db2-b12*da2)/deter;
459                                                   446         
460         if (s1 >= zero && s1 < one)            << 447         if (s1 >= zero && s1 < one) {
461         {                                      << 
462           s2 = -(da1*b12-db1*a12)/deter;          448           s2 = -(da1*b12-db1*a12)/deter;
463           if (s2 >= zero && s2 < one) return t    449           if (s2 >= zero && s2 < one) return true;
464         }                                         450         }
465       }                                           451       }
466       curr2 = curr2->next;                        452       curr2 = curr2->next;   
467     }                                             453     }
468     curr1 = next1;                                454     curr1 = next1;
469   }                                               455   }
470   return false;                                   456   return false;
471 }                                                 457 }
472                                                   458 
                                                   >> 459 
                                                   >> 460 
                                                   >> 461 //
473 // BisectedBy                                     462 // BisectedBy
474 //                                                463 //
475 // Decide if a line through two points crosses    464 // Decide if a line through two points crosses the polygon, within tolerance
476 //                                                465 //
477 G4bool G4ReduciblePolygon::BisectedBy( G4doubl    466 G4bool G4ReduciblePolygon::BisectedBy( G4double a1, G4double b1,
478                                        G4doubl    467                                        G4double a2, G4double b2,
479                                        G4doubl    468                                        G4double tolerance )
480 {                                                 469 {
481   G4int nNeg = 0, nPos = 0;                       470   G4int nNeg = 0, nPos = 0;
482                                                   471   
483   G4double a12 = a2-a1, b12 = b2-b1;              472   G4double a12 = a2-a1, b12 = b2-b1;
484   G4double len12 = std::sqrt( a12*a12 + b12*b1 << 473   G4double len12 = sqrt( a12*a12 + b12*b12 );
485   a12 /= len12; b12 /= len12;                     474   a12 /= len12; b12 /= len12;
486                                                   475   
487   ABVertex* curr = vertexHead;                 << 476   ABVertex *curr = vertexHead;
488   do    // Loop checking, 13.08.2015, G.Cosmo  << 477   do
489   {                                               478   {
490     G4double av = curr->a - a1,                   479     G4double av = curr->a - a1,
491              bv = curr->b - b1;                << 480        bv = curr->b - b1;
492                                                   481        
493     G4double cross = av*b12 - bv*a12;             482     G4double cross = av*b12 - bv*a12;
494                                                   483     
495     if (cross < -tolerance)                       484     if (cross < -tolerance)
496     {                                             485     {
497       if (nPos != 0) return true;              << 486       if (nPos) return true;
498       ++nNeg;                                  << 487       nNeg++;
499     }                                             488     }
500     else if (cross > tolerance)                   489     else if (cross > tolerance)
501     {                                             490     {
502       if (nNeg != 0) return true;              << 491       if (nNeg) return true;
503       ++nPos;                                  << 492       nPos++;
504     }                                             493     }
505     curr = curr->next;                            494     curr = curr->next;
506   } while( curr != nullptr );                  << 495   } while( curr );
507                                                   496     
508   return false;                                   497   return false;
509 }                                                 498 }
510                                                   499 
                                                   >> 500 
                                                   >> 501 
                                                   >> 502 //
511 // Area                                           503 // Area
512 //                                                504 //
513 // Calculated signed polygon area, where polyg    505 // Calculated signed polygon area, where polygons specified in a
514 // clockwise manner (where x==a, y==b) have ne    506 // clockwise manner (where x==a, y==b) have negative area
515 //                                                507 //
516 //    References: [O' Rourke (C)] pp. 18-27; [    508 //    References: [O' Rourke (C)] pp. 18-27; [Gems II] pp. 5-6:
517 //    "The Area of a Simple Polygon", Jon Rokn    509 //    "The Area of a Simple Polygon", Jon Rokne.
518 //                                                510 //
519 G4double G4ReduciblePolygon::Area()               511 G4double G4ReduciblePolygon::Area()
520 {                                                 512 {
521   G4double answer = 0;                            513   G4double answer = 0;
522                                                   514   
523   ABVertex *curr = vertexHead, *next = nullptr << 515   ABVertex *curr = vertexHead, *next;
524   do    // Loop checking, 13.08.2015, G.Cosmo  << 516   do
525   {                                               517   {
526     next = curr->next;                            518     next = curr->next;
527     if (next==nullptr) next = vertexHead;      << 519     if (next==0) next = vertexHead;
528                                                   520     
529     answer += curr->a*next->b - curr->b*next->    521     answer += curr->a*next->b - curr->b*next->a;
530     curr = curr->next;                            522     curr = curr->next;
531   } while( curr != nullptr );                  << 523   } while( curr );
532                                                   524   
533   return 0.5*answer;                              525   return 0.5*answer;
534 }                                                 526 }
535                                                   527 
                                                   >> 528 
                                                   >> 529 //
536 // Print                                          530 // Print
537 //                                                531 //
538 void G4ReduciblePolygon::Print()                  532 void G4ReduciblePolygon::Print()
539 {                                                 533 {
540   ABVertex* curr = vertexHead;                 << 534   ABVertex *curr = vertexHead;
541   do    // Loop checking, 13.08.2015, G.Cosmo  << 535   do
542   {                                               536   {
543     G4cerr << curr->a << " " << curr->b << G4e    537     G4cerr << curr->a << " " << curr->b << G4endl;
544     curr = curr->next;                            538     curr = curr->next;
545   } while( curr != nullptr );                  << 539   } while( curr );
546 }                                                 540 }
547                                                   541 
                                                   >> 542 
                                                   >> 543 //
548 // CalculateMaxMin                                544 // CalculateMaxMin
549 //                                                545 //
550 // To be called when the vertices are changed,    546 // To be called when the vertices are changed, this
551 // routine re-calculates global values            547 // routine re-calculates global values
552 //                                                548 //
553 void G4ReduciblePolygon::CalculateMaxMin()        549 void G4ReduciblePolygon::CalculateMaxMin()
554 {                                                 550 {
555   ABVertex* curr = vertexHead;                 << 551   ABVertex *curr = vertexHead;
556   aMin = aMax = curr->a;                          552   aMin = aMax = curr->a;
557   bMin = bMax = curr->b;                          553   bMin = bMax = curr->b;
558   curr = curr->next;                              554   curr = curr->next;
559   while( curr != nullptr )    // Loop checking << 555   while( curr )
560   {                                               556   {
561     if (curr->a < aMin)                           557     if (curr->a < aMin)
562       aMin = curr->a;                             558       aMin = curr->a;
563     else if (curr->a > aMax)                      559     else if (curr->a > aMax)
564       aMax = curr->a;                             560       aMax = curr->a;
565                                                   561 
566     if (curr->b < bMin)                           562     if (curr->b < bMin)
567       bMin = curr->b;                             563       bMin = curr->b;
568     else if (curr->b > bMax)                      564     else if (curr->b > bMax)
569       bMax = curr->b;                             565       bMax = curr->b;
570                                                   566     
571     curr = curr->next;                            567     curr = curr->next;
572   }                                               568   }
573 }                                                 569 }
574                                                   570