Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Data Structures | Public Member Functions | Protected Member Functions | Protected Attributes | Friends
UReduciblePolygon Class Reference

#include <UReduciblePolygon.hh>

Data Structures

struct  ABVertex
 

Public Member Functions

 UReduciblePolygon (const double a[], const double b[], int n)
 
 UReduciblePolygon (const double rmin[], const double rmax[], const double z[], int n)
 
virtual ~UReduciblePolygon ()
 
int NumVertices () const
 
double Amin () const
 
double Amax () const
 
double Bmin () const
 
double Bmax () const
 
void CopyVertices (double a[], double b[]) const
 
void ScaleA (double scale)
 
void ScaleB (double scale)
 
bool RemoveDuplicateVertices (double tolerance)
 
bool RemoveRedundantVertices (double tolerance)
 
void ReverseOrder ()
 
void StartWithZMin ()
 
double Area ()
 
bool CrossesItself (double tolerance)
 
bool BisectedBy (double a1, double b1, double a2, double b2, double tolerance)
 
void Print ()
 

Protected Member Functions

void Create (const double a[], const double b[], int n)
 
void CalculateMaxMin ()
 

Protected Attributes

double aMin
 
double aMax
 
double bMin
 
double bMax
 
int numVertices
 
ABVertexvertexHead
 

Friends

class UReduciblePolygonIterator
 
struct ABVertex
 

Detailed Description

Definition at line 39 of file UReduciblePolygon.hh.

Constructor & Destructor Documentation

UReduciblePolygon::UReduciblePolygon ( const double  a[],
const double  b[],
int  n 
)

Definition at line 28 of file UReduciblePolygon.cc.

References Create().

31  : aMin(0.), aMax(0.), bMin(0.), bMax(0.),
32  vertexHead(0)
33 {
34  //
35  // Do all of the real work in Create
36  //
37  Create(a, b, n);
38 }
void Create(const double a[], const double b[], int n)
const G4int n
UReduciblePolygon::UReduciblePolygon ( const double  rmin[],
const double  rmax[],
const double  z[],
int  n 
)

Definition at line 44 of file UReduciblePolygon.cc.

References test::a, test::b, Create(), and n.

47  : aMin(0.), aMax(0.), bMin(0.), bMax(0.),
48  vertexHead(0)
49 {
50  //
51  // Translate
52  //
53  double* a = new double[n * 2];
54  double* b = new double[n * 2];
55 
56  double* rOut = a + n,
57  *zOut = b + n,
58  *rIn = rOut - 1,
59  *zIn = zOut - 1;
60 
61  int i;
62  for (i = 0; i < n; i++, rOut++, zOut++, rIn--, zIn--)
63  {
64  *rOut = rmax[i];
65  *rIn = rmin[i];
66  *zOut = *zIn = z[i];
67  }
68 
69  Create(a, b, n * 2);
70 
71  delete [] a;
72  delete [] b;
73 }
G4double z
Definition: TRTMaterials.hh:39
void Create(const double a[], const double b[], int n)
const G4int n
UReduciblePolygon::~UReduciblePolygon ( )
virtual

Definition at line 121 of file UReduciblePolygon.cc.

References curr(), UReduciblePolygon::ABVertex::next, and vertexHead.

122 {
124  while (curr)
125  {
126  ABVertex* toDelete = curr;
127  curr = curr->next;
128  delete toDelete;
129  }
130 }
friend struct ABVertex
subroutine curr(MNUM, PIM1, PIM2, PIM3, PIM4, HADCUR)
Definition: leptonew.f:2041

Member Function Documentation

double UReduciblePolygon::Amax ( ) const
inline

Definition at line 71 of file UReduciblePolygon.hh.

References aMax.

Referenced by UGenericPolycone::Create(), UPolyhedra::Create(), UPolycone::Init(), and UPolyPhiFace::UPolyPhiFace().

72  {
73  return aMax;
74  }
double UReduciblePolygon::Amin ( ) const
inline

Definition at line 67 of file UReduciblePolygon.hh.

References aMin.

Referenced by UGenericPolycone::Create(), UPolyhedra::Create(), UPolycone::Init(), and UPolyPhiFace::UPolyPhiFace().

68  {
69  return aMin;
70  }
double UReduciblePolygon::Area ( )

Definition at line 537 of file UReduciblePolygon.cc.

References UReduciblePolygon::ABVertex::a, UReduciblePolygon::ABVertex::b, curr(), UReduciblePolygon::ABVertex::next, and vertexHead.

Referenced by UGenericPolycone::Create(), and UPolyhedra::Create().

538 {
539  double answer = 0;
540 
541  ABVertex* curr = vertexHead, *next;
542  do
543  {
544  next = curr->next;
545  if (next == 0) next = vertexHead;
546 
547  answer += curr->a * next->b - curr->b * next->a;
548  curr = curr->next;
549  }
550  while (curr);
551 
552  return 0.5 * answer;
553 }
friend struct ABVertex
subroutine curr(MNUM, PIM1, PIM2, PIM3, PIM4, HADCUR)
Definition: leptonew.f:2041
bool UReduciblePolygon::BisectedBy ( double  a1,
double  b1,
double  a2,
double  b2,
double  tolerance 
)

Definition at line 490 of file UReduciblePolygon.cc.

References UReduciblePolygon::ABVertex::a, UReduciblePolygon::ABVertex::b, curr(), UReduciblePolygon::ABVertex::next, and vertexHead.

Referenced by UGenericPolycone::Create().

493 {
494  int nNeg = 0, nPos = 0;
495 
496  double a12 = a2 - a1, b12 = b2 - b1;
497  double len12 = std::sqrt(a12 * a12 + b12 * b12);
498  a12 /= len12;
499  b12 /= len12;
500 
502  do
503  {
504  double av = curr->a - a1,
505  bv = curr->b - b1;
506 
507  double Cross = av * b12 - bv * a12;
508 
509  if (Cross < -tolerance)
510  {
511  if (nPos) return true;
512  nNeg++;
513  }
514  else if (Cross > tolerance)
515  {
516  if (nNeg) return true;
517  nPos++;
518  }
519  curr = curr->next;
520  }
521  while (curr);
522 
523  return false;
524 }
friend struct ABVertex
subroutine curr(MNUM, PIM1, PIM2, PIM3, PIM4, HADCUR)
Definition: leptonew.f:2041
double UReduciblePolygon::Bmax ( ) const
inline
double UReduciblePolygon::Bmin ( ) const
inline

Definition at line 75 of file UReduciblePolygon.hh.

References bMin.

Referenced by UGenericPolycone::Create(), UPolyhedra::Create(), UPolycone::Init(), and UPolyPhiFace::UPolyPhiFace().

76  {
77  return bMin;
78  }
void UReduciblePolygon::CalculateMaxMin ( )
protected

Definition at line 577 of file UReduciblePolygon.cc.

References UReduciblePolygon::ABVertex::a, aMax, aMin, UReduciblePolygon::ABVertex::b, bMax, bMin, curr(), UReduciblePolygon::ABVertex::next, and vertexHead.

Referenced by Create(), RemoveDuplicateVertices(), and RemoveRedundantVertices().

578 {
580  aMin = aMax = curr->a;
581  bMin = bMax = curr->b;
582  curr = curr->next;
583  while (curr)
584  {
585  if (curr->a < aMin)
586  aMin = curr->a;
587  else if (curr->a > aMax)
588  aMax = curr->a;
589 
590  if (curr->b < bMin)
591  bMin = curr->b;
592  else if (curr->b > bMax)
593  bMax = curr->b;
594 
595  curr = curr->next;
596  }
597 }
friend struct ABVertex
subroutine curr(MNUM, PIM1, PIM2, PIM3, PIM4, HADCUR)
Definition: leptonew.f:2041
void UReduciblePolygon::CopyVertices ( double  a[],
double  b[] 
) const

Definition at line 140 of file UReduciblePolygon.cc.

References test::a, UReduciblePolygon::ABVertex::a, test::b, UReduciblePolygon::ABVertex::b, curr(), UReduciblePolygon::ABVertex::next, and vertexHead.

Referenced by UVCSGfaceted::InitVoxels().

141 {
142  double* anext = a, *bnext = b;
144  while (curr)
145  {
146  *anext++ = curr->a;
147  *bnext++ = curr->b;
148  curr = curr->next;
149  }
150 }
friend struct ABVertex
subroutine curr(MNUM, PIM1, PIM2, PIM3, PIM4, HADCUR)
Definition: leptonew.f:2041
void UReduciblePolygon::Create ( const double  a[],
const double  b[],
int  n 
)
protected

Definition at line 82 of file UReduciblePolygon.cc.

References test::a, UReduciblePolygon::ABVertex::a, ABVertex, test::b, UReduciblePolygon::ABVertex::b, CalculateMaxMin(), UUtils::Exception(), FatalErrorInArguments, n, UReduciblePolygon::ABVertex::next, numVertices, and vertexHead.

Referenced by UReduciblePolygon().

84 {
85  if (n < 3)
86  UUtils::Exception("UReduciblePolygon::Create()", "GeomSolids0002",
87  FatalErrorInArguments, 1, "Less than 3 vertices specified.");
88 
89  const double* anext = a, *bnext = b;
90 
91  ABVertex* prev = 0;
92  do
93  {
94  ABVertex* newVertex = new ABVertex;
95  newVertex->a = *anext;
96  newVertex->b = *bnext;
97  newVertex->next = 0;
98  if (prev == 0)
99  {
100  vertexHead = newVertex;
101  }
102  else
103  {
104  prev->next = newVertex;
105  }
106 
107  prev = newVertex;
108  }
109  while (++anext, ++bnext < b + n);
110 
111  numVertices = n;
112 
113  CalculateMaxMin();
114 }
friend struct ABVertex
const G4int n
void Exception(const char *originOfException, const char *exceptionCode, ExceptionSeverity severity, int level, const char *description)
Definition: UUtils.cc:177
bool UReduciblePolygon::CrossesItself ( double  tolerance)

Definition at line 432 of file UReduciblePolygon.cc.

References UReduciblePolygon::ABVertex::a, UReduciblePolygon::ABVertex::b, UReduciblePolygon::ABVertex::next, and vertexHead.

Referenced by UGenericPolycone::Create(), and UPolyhedra::Create().

433 {
434  double tolerance2 = tolerance * tolerance;
435  double one = 1.0 - tolerance,
436  zero = tolerance;
437  //
438  // Top loop over line segments. By the time we finish
439  // with the second to last segment, we're done.
440  //
441  ABVertex* curr1 = vertexHead, *next1 = 0;
442  while (curr1->next)
443  {
444  next1 = curr1->next;
445  double da1 = next1->a - curr1->a,
446  db1 = next1->b - curr1->b;
447 
448  //
449  // Inner loop over subsequent line segments
450  //
451  ABVertex* curr2 = next1->next;
452  while (curr2)
453  {
454  ABVertex* next2 = curr2->next;
455  if (next2 == 0) next2 = vertexHead;
456  double da2 = next2->a - curr2->a,
457  db2 = next2->b - curr2->b;
458  double a12 = curr2->a - curr1->a,
459  b12 = curr2->b - curr1->b;
460 
461  //
462  // Calculate intersection of the two lines
463  //
464  double deter = da1 * db2 - db1 * da2;
465  if (std::fabs(deter) > tolerance2)
466  {
467  double s1, s2;
468  s1 = (a12 * db2 - b12 * da2) / deter;
469 
470  if (s1 >= zero && s1 < one)
471  {
472  s2 = -(da1 * b12 - db1 * a12) / deter;
473  if (s2 >= zero && s2 < one) return true;
474  }
475  }
476  curr2 = curr2->next;
477  }
478  curr1 = next1;
479  }
480  return false;
481 }
friend struct ABVertex
int UReduciblePolygon::NumVertices ( ) const
inline
void UReduciblePolygon::Print ( void  )

Definition at line 559 of file UReduciblePolygon.cc.

References UReduciblePolygon::ABVertex::a, UReduciblePolygon::ABVertex::b, curr(), UReduciblePolygon::ABVertex::next, and vertexHead.

560 {
562  do
563  {
564  std::cerr << curr->a << " " << curr->b << std::endl;
565  curr = curr->next;
566  }
567  while (curr);
568 }
friend struct ABVertex
subroutine curr(MNUM, PIM1, PIM2, PIM3, PIM4, HADCUR)
Definition: leptonew.f:2041
bool UReduciblePolygon::RemoveDuplicateVertices ( double  tolerance)

Definition at line 191 of file UReduciblePolygon.cc.

References UReduciblePolygon::ABVertex::a, UReduciblePolygon::ABVertex::b, CalculateMaxMin(), curr(), UReduciblePolygon::ABVertex::next, numVertices, and vertexHead.

Referenced by UGenericPolycone::Create(), and UPolyhedra::Create().

192 {
194  *prev = 0, *next = 0;
195  while (curr)
196  {
197  next = curr->next;
198  if (next == 0) next = vertexHead;
199 
200  if (std::fabs(curr->a - next->a) < tolerance &&
201  std::fabs(curr->b - next->b) < tolerance)
202  {
203  //
204  // Duplicate found: do we have > 3 vertices?
205  //
206  if (numVertices <= 3)
207  {
208  CalculateMaxMin();
209  return false;
210  }
211 
212  //
213  // Delete
214  //
215  ABVertex* toDelete = curr;
216  curr = curr->next;
217  delete toDelete;
218 
219  numVertices--;
220 
221  if (prev) prev->next = curr;
222  else vertexHead = curr;
223  }
224  else
225  {
226  prev = curr;
227  curr = curr->next;
228  }
229  }
230 
231  //
232  // In principle, this is not needed, but why not just play it safe?
233  //
234  CalculateMaxMin();
235 
236  return true;
237 }
friend struct ABVertex
subroutine curr(MNUM, PIM1, PIM2, PIM3, PIM4, HADCUR)
Definition: leptonew.f:2041
bool UReduciblePolygon::RemoveRedundantVertices ( double  tolerance)

Definition at line 246 of file UReduciblePolygon.cc.

References UReduciblePolygon::ABVertex::a, UReduciblePolygon::ABVertex::b, CalculateMaxMin(), curr(), UReduciblePolygon::ABVertex::next, numVertices, mcscore::test(), and vertexHead.

Referenced by UGenericPolycone::Create(), and UPolyhedra::Create().

247 {
248  //
249  // Under these circumstances, we can quit now!
250  //
251  if (numVertices <= 2) return false;
252 
253  double tolerance2 = tolerance * tolerance;
254 
255  //
256  // Loop over all vertices
257  //
258  ABVertex* curr = vertexHead, *next = 0;
259  while (curr)
260  {
261  next = curr->next;
262  if (next == 0) next = vertexHead;
263 
264  double da = next->a - curr->a,
265  db = next->b - curr->b;
266 
267  //
268  // Loop over all subsequent vertices, up to curr
269  //
270  for (;;)
271  {
272  //
273  // Get vertex after next
274  //
275  ABVertex* test = next->next;
276  if (test == 0) test = vertexHead;
277 
278  //
279  // If we are back to the original vertex, stop
280  //
281  if (test == curr) break;
282 
283  //
284  // Test for parallel line segments
285  //
286  double dat = test->a - curr->a,
287  dbt = test->b - curr->b;
288 
289  if (std::fabs(dat * db - dbt * da) > tolerance2) break;
290 
291  //
292  // Redundant vertex found: do we have > 3 vertices?
293  //
294  if (numVertices <= 3)
295  {
296  CalculateMaxMin();
297  return false;
298  }
299 
300  //
301  // Delete vertex pointed to by next. Carefully!
302  //
303  if (curr->next)
304  {
305  // next is not head
306  if (next->next)
307  curr->next = test; // next is not tail
308  else
309  curr->next = 0; // New tail
310  }
311  else
312  vertexHead = test; // New head
313 
314  if ((curr != next) && (next != test)) delete next;
315 
316  numVertices--;
317 
318  //
319  // Replace next by the vertex we just tested,
320  // and keep on going...
321  //
322  next = test;
323  da = dat;
324  db = dbt;
325  }
326  curr = curr->next;
327  }
328 
329  //
330  // In principle, this is not needed, but why not just play it safe?
331  //
332  CalculateMaxMin();
333 
334  return true;
335 }
friend struct ABVertex
subroutine curr(MNUM, PIM1, PIM2, PIM3, PIM4, HADCUR)
Definition: leptonew.f:2041
def test
Definition: mcscore.py:117
void UReduciblePolygon::ReverseOrder ( )

Definition at line 343 of file UReduciblePolygon.cc.

References curr(), UReduciblePolygon::ABVertex::next, and vertexHead.

Referenced by UGenericPolycone::Create(), and UPolyhedra::Create().

344 {
345  //
346  // Loop over all vertices
347  //
348  ABVertex* prev = vertexHead;
349  if (prev == 0) return; // No vertices
350 
351  ABVertex* curr = prev->next;
352  if (curr == 0) return; // Just one vertex
353 
354  //
355  // Our new tail
356  //
357  vertexHead->next = 0;
358 
359  for (;;)
360  {
361  //
362  // Save pointer to next vertex (in original order)
363  //
364  ABVertex* save = curr->next;
365 
366  //
367  // Replace it with a pointer to the previous one
368  // (in original order)
369  //
370  curr->next = prev;
371 
372  //
373  // Last vertex?
374  //
375  if (save == 0) break;
376 
377  //
378  // Next vertex
379  //
380  prev = curr;
381  curr = save;
382  }
383 
384  //
385  // Our new head
386  //
387  vertexHead = curr;
388 }
friend struct ABVertex
subroutine curr(MNUM, PIM1, PIM2, PIM3, PIM4, HADCUR)
Definition: leptonew.f:2041
void UReduciblePolygon::ScaleA ( double  scale)

Definition at line 158 of file UReduciblePolygon.cc.

References UReduciblePolygon::ABVertex::a, curr(), UReduciblePolygon::ABVertex::next, and vertexHead.

Referenced by UPolyhedra::Init().

159 {
161  while (curr)
162  {
163  curr->a *= scale;
164  curr = curr->next;
165  }
166 }
friend struct ABVertex
subroutine curr(MNUM, PIM1, PIM2, PIM3, PIM4, HADCUR)
Definition: leptonew.f:2041
void UReduciblePolygon::ScaleB ( double  scale)

Definition at line 174 of file UReduciblePolygon.cc.

References UReduciblePolygon::ABVertex::b, curr(), UReduciblePolygon::ABVertex::next, and vertexHead.

175 {
177  while (curr)
178  {
179  curr->b *= scale;
180  curr = curr->next;
181  }
182 }
friend struct ABVertex
subroutine curr(MNUM, PIM1, PIM2, PIM3, PIM4, HADCUR)
Definition: leptonew.f:2041
void UReduciblePolygon::StartWithZMin ( )

Definition at line 395 of file UReduciblePolygon.cc.

References UReduciblePolygon::ABVertex::b, curr(), UReduciblePolygon::ABVertex::next, and vertexHead.

Referenced by UPolycone::SetOriginalParameters().

396 {
398  double bcurr = curr->b;
399  ABVertex* prev = curr;
400  while (curr)
401  {
402  if (curr->b < bcurr)
403  {
404  bcurr = curr->b;
405  ABVertex* curr1 = curr;
406  while (curr1)
407  {
408  if (curr1->next == 0)
409  {
410  curr1->next = vertexHead;
411  break;
412  }
413  curr1 = curr1->next;
414  }
415  vertexHead = curr;
416  prev->next = 0;
417  }
418  prev = curr;
419  curr = curr->next;
420  }
421 }
friend struct ABVertex
subroutine curr(MNUM, PIM1, PIM2, PIM3, PIM4, HADCUR)
Definition: leptonew.f:2041

Friends And Related Function Documentation

friend struct ABVertex
friend

Definition at line 127 of file UReduciblePolygon.hh.

Referenced by Create().

friend class UReduciblePolygonIterator
friend

Definition at line 41 of file UReduciblePolygon.hh.

Field Documentation

double UReduciblePolygon::aMax
protected

Definition at line 118 of file UReduciblePolygon.hh.

Referenced by Amax(), and CalculateMaxMin().

double UReduciblePolygon::aMin
protected

Definition at line 118 of file UReduciblePolygon.hh.

Referenced by Amin(), and CalculateMaxMin().

double UReduciblePolygon::bMax
protected

Definition at line 118 of file UReduciblePolygon.hh.

Referenced by Bmax(), and CalculateMaxMin().

double UReduciblePolygon::bMin
protected

Definition at line 118 of file UReduciblePolygon.hh.

Referenced by Bmin(), and CalculateMaxMin().

int UReduciblePolygon::numVertices
protected
ABVertex* UReduciblePolygon::vertexHead
protected

The documentation for this class was generated from the following files: