Geant4  10.01.p03
UPolyconeSide.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * This Software is part of the AIDA Unified Solids Library package *
4 // * See: https://aidasoft.web.cern.ch/USolids *
5 // ********************************************************************
6 //
7 // $Id:$
8 //
9 // --------------------------------------------------------------------
10 //
11 // UPolyconeSide
12 //
13 // 19.04.13 Marek Gayer
14 // Created from original implementation in Geant4
15 // --------------------------------------------------------------------
16 
17 #include "UUtils.hh"
18 #include <string>
19 #include <cmath>
20 #include <sstream>
21 #include "UPolyconeSide.hh"
22 #include "UIntersectingCone.hh"
23 #include "VUSolid.hh"
24 
25 //
26 // Constructor
27 //
28 // Values for r1,z1 and r2,z2 should be specified in clockwise
29 // order in (r,z).
30 //
32  const UPolyconeSideRZ* tail,
33  const UPolyconeSideRZ* head,
34  const UPolyconeSideRZ* nextRZ,
35  double thePhiStart,
36  double theDeltaPhi,
37  bool thePhiIsOpen,
38  bool isAllBehind)
39  : ncorners(0), corners(0)
40 {
41 
43 
44  fSurfaceArea = 0.0;
45 
46  //
47  // Record values
48  //
49  r[0] = tail->r;
50  z[0] = tail->z;
51  r[1] = head->r;
52  z[1] = head->z;
53 
54  phiIsOpen = thePhiIsOpen;
55  if (phiIsOpen)
56  {
57  deltaPhi = theDeltaPhi;
58  startPhi = thePhiStart;
59 
60  //
61  // Set phi values to our conventions
62  //
63  while (deltaPhi < 0.0) deltaPhi += 2 * UUtils::kPi;
64  while (startPhi < 0.0) startPhi += 2 * UUtils::kPi;
65 
66  //
67  // Calculate corner coordinates
68  //
69  ncorners = 4;
70  corners = new UVector3[ncorners];
71 
72  corners[0] = UVector3(tail->r * std::cos(startPhi),
73  tail->r * std::sin(startPhi), tail->z);
74  corners[1] = UVector3(head->r * std::cos(startPhi),
75  head->r * std::sin(startPhi), head->z);
76  corners[2] = UVector3(tail->r * std::cos(startPhi + deltaPhi),
77  tail->r * std::sin(startPhi + deltaPhi), tail->z);
78  corners[3] = UVector3(head->r * std::cos(startPhi + deltaPhi),
79  head->r * std::sin(startPhi + deltaPhi), head->z);
80  }
81  else
82  {
83  deltaPhi = 2 * UUtils::kPi;
84  startPhi = 0.0;
85  }
86 
87  allBehind = isAllBehind;
88 
89  //
90  // Make our intersecting cone
91  //
92  cone = new UIntersectingCone(r, z);
93 
94  //
95  // Calculate vectors in r,z space
96  //
97  rS = r[1] - r[0];
98  zS = z[1] - z[0];
99  length = std::sqrt(rS * rS + zS * zS);
100  rS /= length;
101  zS /= length;
102 
103  rNorm = +zS;
104  zNorm = -rS;
105 
106  double lAdj;
107 
108  prevRS = r[0] - prevRZ->r;
109  prevZS = z[0] - prevRZ->z;
110  lAdj = std::sqrt(prevRS * prevRS + prevZS * prevZS);
111  prevRS /= lAdj;
112  prevZS /= lAdj;
113 
114  rNormEdge[0] = rNorm + prevZS;
115  zNormEdge[0] = zNorm - prevRS;
116  lAdj = std::sqrt(rNormEdge[0] * rNormEdge[0] + zNormEdge[0] * zNormEdge[0]);
117  rNormEdge[0] /= lAdj;
118  zNormEdge[0] /= lAdj;
119 
120  nextRS = nextRZ->r - r[1];
121  nextZS = nextRZ->z - z[1];
122  lAdj = std::sqrt(nextRS * nextRS + nextZS * nextZS);
123  nextRS /= lAdj;
124  nextZS /= lAdj;
125 
126  rNormEdge[1] = rNorm + nextZS;
127  zNormEdge[1] = zNorm - nextRS;
128  lAdj = std::sqrt(rNormEdge[1] * rNormEdge[1] + zNormEdge[1] * zNormEdge[1]);
129  rNormEdge[1] /= lAdj;
130  zNormEdge[1] /= lAdj;
131 }
132 
133 //
134 // Fake default constructor - sets only member data and allocates memory
135 // for usage restricted to object persistency.
136 //
138  : startPhi(0.), deltaPhi(0.), phiIsOpen(false), allBehind(false),
139  cone(0), rNorm(0.), zNorm(0.), rS(0.), zS(0.), length(0.),
140  prevRS(0.), prevZS(0.), nextRS(0.), nextZS(0.), ncorners(0), corners(0),
141  tolerance(0.), fSurfaceArea(0.)
142 {
143  r[0] = r[1] = 0.;
144  z[0] = z[1] = 0.;
145  rNormEdge[0] = rNormEdge[1] = 0.;
146  zNormEdge[0] = zNormEdge[1] = 0.;
147 }
148 
149 //
150 // Destructor
151 //
153 {
154  delete cone;
155  if (phiIsOpen)
156  {
157  delete [] corners;
158  }
159 }
160 
161 
162 //
163 // Copy constructor
164 //
166  : UVCSGface(), ncorners(0), corners(0)
167 {
168 
169  CopyStuff(source);
170 }
171 
172 
173 //
174 // Assignment operator
175 //
177 {
178  if (this == &source)
179  {
180  return *this;
181  }
182 
183  delete cone;
184  if (phiIsOpen)
185  {
186  delete [] corners;
187  }
188 
189  CopyStuff(source);
190 
191  return *this;
192 }
193 
194 
195 //
196 // CopyStuff
197 //
199 {
200  r[0] = source.r[0];
201  r[1] = source.r[1];
202  z[0] = source.z[0];
203  z[1] = source.z[1];
204 
205  startPhi = source.startPhi;
206  deltaPhi = source.deltaPhi;
207  phiIsOpen = source.phiIsOpen;
208  allBehind = source.allBehind;
209 
210  tolerance = source.tolerance;
211  fSurfaceArea = source.fSurfaceArea;
212 
213  cone = new UIntersectingCone(*source.cone);
214 
215  rNorm = source.rNorm;
216  zNorm = source.zNorm;
217  rS = source.rS;
218  zS = source.zS;
219  length = source.length;
220  prevRS = source.prevRS;
221  prevZS = source.prevZS;
222  nextRS = source.nextRS;
223  nextZS = source.nextZS;
224 
225  rNormEdge[0] = source.rNormEdge[0];
226  rNormEdge[1] = source.rNormEdge[1];
227  zNormEdge[0] = source.zNormEdge[0];
228  zNormEdge[1] = source.zNormEdge[1];
229 
230  if (phiIsOpen)
231  {
232  ncorners = 4;
233  corners = new UVector3[ncorners];
234 
235  corners[0] = source.corners[0];
236  corners[1] = source.corners[1];
237  corners[2] = source.corners[2];
238  corners[3] = source.corners[3];
239  }
240 }
241 
242 
243 //
244 // Intersect
245 //
247  const UVector3& v,
248  bool outgoing,
249  double surfTolerance,
250  double& distance,
251  double& distFromSurface,
252  UVector3& normal,
253  bool& isAllBehind)
254 {
255  double s1, s2;
256  double normSign = outgoing ? +1 : -1;
257 
258  isAllBehind = allBehind;
259 
260  //
261  // Check for two possible intersections
262  //
263  int nside = cone->LineHitsCone(p, v, s1, s2);
264  if (nside == 0) return false;
265 
266  //
267  // Check the first side first, since it is (supposed to be) closest
268  //
269  UVector3 hit = p + s1 * v;
270 
271  if (PointOnCone(hit, normSign, p, v, normal))
272  {
273  //
274  // Good intersection! What about the normal?
275  //
276  if (normSign * v.Dot(normal) > 0)
277  {
278  //
279  // We have a valid intersection, but it could very easily
280  // be behind the point. To decide if we tolerate this,
281  // we have to see if the point p is on the surface near
282  // the intersecting point.
283  //
284  // What does it mean exactly for the point p to be "near"
285  // the intersection? It means that if we draw a line from
286  // p to the hit, the line remains entirely within the
287  // tolerance bounds of the cone. To test this, we can
288  // ask if the normal is correct near p.
289  //
290  double pr = p.Perp();
291  if (pr < DBL_MIN) pr = DBL_MIN;
292  UVector3 pNormal(rNorm * p.x() / pr, rNorm * p.y() / pr, zNorm);
293  if (normSign * v.Dot(pNormal) > 0)
294  {
295  //
296  // p and intersection in same hemisphere
297  //
298  double distOutside2;
299  distFromSurface = -normSign * DistanceAway(p, false, distOutside2);
300  if (distOutside2 < surfTolerance * surfTolerance)
301  {
302  if (distFromSurface > -surfTolerance)
303  {
304  //
305  // We are just inside or away from the
306  // surface. Accept *any* value of distance.
307  //
308  distance = s1;
309  return true;
310  }
311  }
312  }
313  else
314  distFromSurface = s1;
315 
316  //
317  // Accept positive distances
318  //
319  if (s1 > 0)
320  {
321  distance = s1;
322  return true;
323  }
324  }
325  }
326 
327  if (nside == 1) return false;
328 
329  //
330  // Well, try the second hit
331  //
332  hit = p + s2 * v;
333 
334  if (PointOnCone(hit, normSign, p, v, normal))
335  {
336  //
337  // Good intersection! What about the normal?
338  //
339  if (normSign * v.Dot(normal) > 0)
340  {
341  double pr = p.Perp();
342  if (pr < DBL_MIN) pr = DBL_MIN;
343  UVector3 pNormal(rNorm * p.x() / pr, rNorm * p.y() / pr, zNorm);
344  if (normSign * v.Dot(pNormal) > 0)
345  {
346  double distOutside2;
347  distFromSurface = -normSign * DistanceAway(p, false, distOutside2);
348  if (distOutside2 < surfTolerance * surfTolerance)
349  {
350  if (distFromSurface > -surfTolerance)
351  {
352  distance = s2;
353  return true;
354  }
355  }
356  }
357  else
358  distFromSurface = s2;
359 
360  if (s2 > 0)
361  {
362  distance = s2;
363  return true;
364  }
365  }
366  }
367 
368  //
369  // Better luck next time
370  //
371  return false;
372 }
373 
374 
375 double UPolyconeSide::Safety(const UVector3& p, bool outgoing)
376 {
377  double normSign = outgoing ? -1 : +1;
378  double distFrom, distOut2;
379 
380  //
381  // We have two tries for each hemisphere. Try the closest first.
382  //
383  distFrom = normSign * DistanceAway(p, false, distOut2);
384  if (distFrom > -0.5 * VUSolid::Tolerance())
385  {
386  //
387  // Good answer
388  //
389  if (distOut2 > 0)
390  return std::sqrt(distFrom * distFrom + distOut2);
391  else
392  return std::fabs(distFrom);
393  }
394 
395  //
396  // Try second side.
397  //
398  distFrom = normSign * DistanceAway(p, true, distOut2);
399  if (distFrom > -0.5 * VUSolid::Tolerance())
400  {
401 
402  if (distOut2 > 0)
403  return std::sqrt(distFrom * distFrom + distOut2);
404  else
405  return std::fabs(distFrom);
406  }
407 
408  return UUtils::kInfinity;
409 }
410 
411 
412 //
413 // Inside
414 //
416  double atolerance,
417  double* bestDistance)
418 {
419  //
420  // Check both sides
421  //
422  double distFrom, distOut2, dist2;
423  double edgeRZnorm;
424 
425  distFrom = DistanceAway(p, distOut2, &edgeRZnorm);
426  dist2 = distFrom * distFrom + distOut2;
427 
428 
429  *bestDistance = std::sqrt(dist2); // could sqrt be removed?
430 
431  //
432  // Okay then, inside or out?
433  //
434  if ((std::fabs(edgeRZnorm) < atolerance)
435  && (distOut2 < atolerance * atolerance))
436  return VUSolid::eSurface;
437  else if (edgeRZnorm < 0)
438  return VUSolid::eInside;
439  else
440  return VUSolid::eOutside;
441 }
442 
443 
444 //
445 // Normal
446 //
448  double* bestDistance)
449 {
450  if (p == UVector3(0., 0., 0.))
451  {
452  return p;
453  }
454 
455  double dFrom, dOut2;
456 
457  dFrom = DistanceAway(p, false, dOut2);
458 
459  *bestDistance = std::sqrt(dFrom * dFrom + dOut2);
460 
461  double rds = p.Perp();
462  if (rds != 0.)
463  {
464  return UVector3(rNorm * p.x() / rds, rNorm * p.y() / rds, zNorm);
465  }
466  return UVector3(0., 0., zNorm).Unit();
467 }
468 
469 
470 //
471 // Extent
472 //
473 double UPolyconeSide::Extent(const UVector3 axis)
474 {
475  if (axis.Perp2() < DBL_MIN)
476  {
477  //
478  // Special case
479  //
480  return axis.z() < 0 ? -cone->ZLo() : cone->ZHi();
481  }
482 
483  //
484  // Is the axis pointing inside our phi gap?
485  //
486  if (phiIsOpen)
487  {
488  double phi = GetPhi(axis);
489  while (phi < startPhi) phi += 2 * UUtils::kPi;
490 
491  if (phi > deltaPhi + startPhi)
492  {
493  //
494  // Yeah, looks so. Make four three vectors defining the phi
495  // opening
496  //
497  double cosP = std::cos(startPhi), sinP = std::sin(startPhi);
498  UVector3 a(r[0]*cosP, r[0]*sinP, z[0]);
499  UVector3 b(r[1]*cosP, r[1]*sinP, z[1]);
500  cosP = std::cos(startPhi + deltaPhi);
501  sinP = std::sin(startPhi + deltaPhi);
502  UVector3 c(r[0]*cosP, r[0]*sinP, z[0]);
503  UVector3 d(r[1]*cosP, r[1]*sinP, z[1]);
504 
505  double ad = axis.Dot(a),
506  bd = axis.Dot(b),
507  cd = axis.Dot(c),
508  dd = axis.Dot(d);
509 
510  if (bd > ad) ad = bd;
511  if (cd > ad) ad = cd;
512  if (dd > ad) ad = dd;
513 
514  return ad;
515  }
516  }
517 
518  //
519  // Check either end
520  //
521  double aPerp = axis.Perp();
522 
523  double a = aPerp * r[0] + axis.z() * z[0];
524  double b = aPerp * r[1] + axis.z() * z[1];
525 
526  if (b > a) a = b;
527 
528  return a;
529 }
530 
531 
532 
533 //
534 // CalculateExtent
535 //
536 // See notes in UVCSGface
537 //
538 
539 /*
540 void UPolyconeSide::CalculateExtent( const EAxisType axis,
541  const UVoxelLimits &voxelLimit,
542  const UAffineTransform &transform,
543  USolidExtentList &extentList )
544 {
545  UClippablePolygon polygon;
546 
547  //
548  // Here we will approximate (ala UCons) and divide our conical section
549  // into segments, like UPolyhedra. When doing so, the radius
550  // is extented far enough such that the segments always lie
551  // just outside the surface of the conical section we are
552  // approximating.
553  //
554 
555  //
556  // Choose phi size of our segment(s) based on constants as
557  // defined in meshdefs.hh
558  //
559  int numPhi = (int)(deltaPhi/UUtils::kMeshAngleDefault) + 1;
560  if (numPhi < UUtils::kMinMeshSections)
561  numPhi = UUtils::kMinMeshSections;
562  else if (numPhi > UUtils::kMaxMeshSections)
563  numPhi = UUtils::kMaxMeshSections;
564 
565  double sigPhi = deltaPhi/numPhi;
566 
567  //
568  // Determine radius factor to keep segments outside
569  //
570  double rFudge = 1.0/std::cos(0.5*sigPhi);
571 
572  //
573  // Decide which radius to use on each end of the side,
574  // and whether a transition mesh is required
575  //
576  // {r0,z0} - Beginning of this side
577  // {r1,z1} - Ending of this side
578  // {r2,z0} - Beginning of transition piece connecting previous
579  // side (and ends at beginning of this side)
580  //
581  // So, order is 2 --> 0 --> 1.
582  // -------
583  //
584  // r2 < 0 indicates that no transition piece is required
585  //
586  double r0, r1, r2, z0, z1;
587 
588  r2 = -1; // By default: no transition piece
589 
590  if (rNorm < -DBL_MIN)
591  {
592  //
593  // This side faces *inward*, and so our mesh has
594  // the same radius
595  //
596  r1 = r[1];
597  z1 = z[1];
598  z0 = z[0];
599  r0 = r[0];
600 
601  r2 = -1;
602 
603  if (prevZS > DBL_MIN)
604  {
605  //
606  // The previous side is facing outwards
607  //
608  if ( prevRS*zS - prevZS*rS > 0 )
609  {
610  //
611  // Transition was convex: build transition piece
612  //
613  if (r[0] > DBL_MIN) r2 = r[0]*rFudge;
614  }
615  else
616  {
617  //
618  // Transition was concave: short this side
619  //
620  FindLineIntersect( z0, r0, zS, rS,
621  z0, r0*rFudge, prevZS, prevRS*rFudge, z0, r0 );
622  }
623  }
624 
625  if ( nextZS > DBL_MIN && (rS*nextZS - zS*nextRS < 0) )
626  {
627  //
628  // The next side is facing outwards, forming a
629  // concave transition: short this side
630  //
631  FindLineIntersect( z1, r1, zS, rS,
632  z1, r1*rFudge, nextZS, nextRS*rFudge, z1, r1 );
633  }
634  }
635  else if (rNorm > DBL_MIN)
636  {
637  //
638  // This side faces *outward* and is given a boost to
639  // it radius
640  //
641  r0 = r[0]*rFudge;
642  z0 = z[0];
643  r1 = r[1]*rFudge;
644  z1 = z[1];
645 
646  if (prevZS < -DBL_MIN)
647  {
648  //
649  // The previous side is facing inwards
650  //
651  if ( prevRS*zS - prevZS*rS > 0 )
652  {
653  //
654  // Transition was convex: build transition piece
655  //
656  if (r[0] > DBL_MIN) r2 = r[0];
657  }
658  else
659  {
660  //
661  // Transition was concave: short this side
662  //
663  FindLineIntersect( z0, r0, zS, rS*rFudge,
664  z0, r[0], prevZS, prevRS, z0, r0 );
665  }
666  }
667 
668  if ( nextZS < -DBL_MIN && (rS*nextZS - zS*nextRS < 0) )
669  {
670  //
671  // The next side is facing inwards, forming a
672  // concave transition: short this side
673  //
674  FindLineIntersect( z1, r1, zS, rS*rFudge,
675  z1, r[1], nextZS, nextRS, z1, r1 );
676  }
677  }
678  else
679  {
680  //
681  // This side is perpendicular to the z axis (is a disk)
682  //
683  // Whether or not r0 needs a rFudge factor depends
684  // on the normal of the previous edge. Similar with r1
685  // and the next edge. No transition piece is required.
686  //
687  r0 = r[0];
688  r1 = r[1];
689  z0 = z[0];
690  z1 = z[1];
691 
692  if (prevZS > DBL_MIN) r0 *= rFudge;
693  if (nextZS > DBL_MIN) r1 *= rFudge;
694  }
695 
696  //
697  // Loop
698  //
699  double phi = startPhi,
700  cosPhi = std::cos(phi),
701  sinPhi = std::sin(phi);
702 
703  UVector3 v0( r0*cosPhi, r0*sinPhi, z0 ),
704  v1( r1*cosPhi, r1*sinPhi, z1 ),
705  v2, w0, w1, w2;
706  transform.ApplyPointTransform( v0 );
707  transform.ApplyPointTransform( v1 );
708 
709  if (r2 >= 0)
710  {
711  v2 = UVector3( r2*cosPhi, r2*sinPhi, z0 );
712  transform.ApplyPointTransform( v2 );
713  }
714 
715  do
716  {
717  phi += sigPhi;
718  if (numPhi == 1) phi = startPhi+deltaPhi; // Try to avoid roundoff
719  cosPhi = std::cos(phi),
720  sinPhi = std::sin(phi);
721 
722  w0 = UVector3( r0*cosPhi, r0*sinPhi, z0 );
723  w1 = UVector3( r1*cosPhi, r1*sinPhi, z1 );
724  transform.ApplyPointTransform( w0 );
725  transform.ApplyPointTransform( w1 );
726 
727  UVector3 deltaV = r0 > r1 ? w0-v0 : w1-v1;
728 
729  //
730  // Build polygon, taking special care to keep the vertices
731  // in order
732  //
733  polygon.ClearAllVertices();
734 
735  polygon.AddVertexInOrder( v0 );
736  polygon.AddVertexInOrder( v1 );
737  polygon.AddVertexInOrder( w1 );
738  polygon.AddVertexInOrder( w0 );
739 
740  //
741  // Get extent
742  //
743  if (polygon.PartialClip( voxelLimit, axis ))
744  {
745  //
746  // Get Dot product of normal with target axis
747  //
748  polygon.SetNormal( deltaV.Cross(v1-v0).Unit() );
749 
750  extentList.AddSurface( polygon );
751  }
752 
753  if (r2 >= 0)
754  {
755  //
756  // Repeat, for transition piece
757  //
758  w2 = UVector3( r2*cosPhi, r2*sinPhi, z0 );
759  transform.ApplyPointTransform( w2 );
760 
761  polygon.ClearAllVertices();
762 
763  polygon.AddVertexInOrder( v2 );
764  polygon.AddVertexInOrder( v0 );
765  polygon.AddVertexInOrder( w0 );
766  polygon.AddVertexInOrder( w2 );
767 
768  if (polygon.PartialClip( voxelLimit, axis ))
769  {
770  polygon.SetNormal( deltaV.Cross(v0-v2).Unit() );
771 
772  extentList.AddSurface( polygon );
773  }
774 
775  v2 = w2;
776  }
777 
778  //
779  // Next vertex
780  //
781  v0 = w0;
782  v1 = w1;
783  } while( --numPhi > 0 );
784 
785  //
786  // We are almost done. But, it is important that we leave no
787  // gaps in the surface of our solid. By using rFudge, however,
788  // we've done exactly that, if we have a phi segment.
789  // Add two additional faces if necessary
790  //
791  if (phiIsOpen && rNorm > DBL_MIN)
792  {
793  cosPhi = std::cos(startPhi);
794  sinPhi = std::sin(startPhi);
795 
796  UVector3 a0( r[0]*cosPhi, r[0]*sinPhi, z[0] ),
797  a1( r[1]*cosPhi, r[1]*sinPhi, z[1] ),
798  b0( r0*cosPhi, r0*sinPhi, z[0] ),
799  b1( r1*cosPhi, r1*sinPhi, z[1] );
800 
801  transform.ApplyPointTransform( a0 );
802  transform.ApplyPointTransform( a1 );
803  transform.ApplyPointTransform( b0 );
804  transform.ApplyPointTransform( b1 );
805 
806  polygon.ClearAllVertices();
807 
808  polygon.AddVertexInOrder( a0 );
809  polygon.AddVertexInOrder( a1 );
810  polygon.AddVertexInOrder( b0 );
811  polygon.AddVertexInOrder( b1 );
812 
813  if (polygon.PartialClip( voxelLimit , axis))
814  {
815  UVector3 normal( sinPhi, -cosPhi, 0 );
816  polygon.SetNormal( transform.TransformAxis( normal ) );
817 
818  extentList.AddSurface( polygon );
819  }
820 
821  cosPhi = std::cos(startPhi+deltaPhi);
822  sinPhi = std::sin(startPhi+deltaPhi);
823 
824  a0 = UVector3( r[0]*cosPhi, r[0]*sinPhi, z[0] ),
825  a1 = UVector3( r[1]*cosPhi, r[1]*sinPhi, z[1] ),
826  b0 = UVector3( r0*cosPhi, r0*sinPhi, z[0] ),
827  b1 = UVector3( r1*cosPhi, r1*sinPhi, z[1] );
828  transform.ApplyPointTransform( a0 );
829  transform.ApplyPointTransform( a1 );
830  transform.ApplyPointTransform( b0 );
831  transform.ApplyPointTransform( b1 );
832 
833  polygon.ClearAllVertices();
834 
835  polygon.AddVertexInOrder( a0 );
836  polygon.AddVertexInOrder( a1 );
837  polygon.AddVertexInOrder( b0 );
838  polygon.AddVertexInOrder( b1 );
839 
840  if (polygon.PartialClip( voxelLimit, axis ))
841  {
842  UVector3 normal( -sinPhi, cosPhi, 0 );
843  polygon.SetNormal( transform.TransformAxis( normal ) );
844 
845  extentList.AddSurface( polygon );
846  }
847  }
848 
849  return;
850 }
851 */
852 
853 //
854 // GetPhi
855 //
856 // Calculate Phi for a given 3-vector (point), if not already cached for the
857 // same point, in the attempt to avoid consecutive computation of the same
858 // quantity
859 //
861 {
862  double val = 0.;
863 
864  val = p.Phi();
865 
866  return val;
867 }
868 
869 //
870 // DistanceAway
871 //
872 // Calculate distance of a point from our conical surface, including the effect
873 // of any phi segmentation
874 //
875 // Arguments:
876 // p - (in) Point to check
877 // opposite - (in) If true, check opposite hemisphere (see below)
878 // distOutside - (out) Additional distance outside the edges of the surface
879 // edgeRZnorm - (out) if negative, point is inside
880 //
881 // return value = distance from the conical plane, if extrapolated beyond edges,
882 // signed by whether the point is in inside or outside the shape
883 //
884 // Notes:
885 // * There are two answers, depending on which hemisphere is considered.
886 //
888  bool opposite,
889  double& distOutside2,
890  double* edgeRZnorm)
891 {
892  //
893  // Convert our point to r and z
894  //
895  double rx = p.Perp(), zx = p.z();
896 
897  //
898  // Change sign of r if opposite says we should
899  //
900  if (opposite) rx = -rx;
901 
902  //
903  // Calculate return value
904  //
905  double deltaR = rx - r[0], deltaZ = zx - z[0];
906  double answer = deltaR * rNorm + deltaZ * zNorm;
907 
908  //
909  // Are we off the surface in r,z space?
910  //
911  double q = deltaR * rS + deltaZ * zS;
912  if (q < 0)
913  {
914  distOutside2 = q * q;
915  if (edgeRZnorm) *edgeRZnorm = deltaR * rNormEdge[0] + deltaZ * zNormEdge[0];
916  }
917  else if (q > length)
918  {
919  distOutside2 = UUtils::sqr(q - length);
920  if (edgeRZnorm)
921  {
922  deltaR = rx - r[1];
923  deltaZ = zx - z[1];
924  *edgeRZnorm = deltaR * rNormEdge[1] + deltaZ * zNormEdge[1];
925  }
926  }
927  else
928  {
929  distOutside2 = 0;
930  if (edgeRZnorm) *edgeRZnorm = answer;
931  }
932 
933  if (phiIsOpen)
934  {
935  //
936  // Finally, check phi
937  //
938  double phi = GetPhi(p);
939  while (phi < startPhi) phi += 2 * UUtils::kPi;
940 
941  if (phi > startPhi + deltaPhi)
942  {
943  //
944  // Oops. Are we closer to the start phi or end phi?
945  //
946  double d1 = phi - startPhi - deltaPhi;
947  while (phi > startPhi) phi -= 2 * UUtils::kPi;
948  double d2 = startPhi - phi;
949 
950  if (d2 < d1) d1 = d2;
951 
952  //
953  // Add result to our distance
954  //
955  double dist = d1 * rx;
956 
957  distOutside2 += dist * dist;
958  if (edgeRZnorm)
959  {
960  *edgeRZnorm = std::max(std::fabs(*edgeRZnorm), std::fabs(dist));
961  }
962  }
963  }
964 
965  return answer;
966 }
967 
968 //
969 // DistanceAway
970 //
971 //
972 // Special version of DistanceAway for Inside.
973 // opposite parameter is not used, instead use sign of rx for choosing the side
974 //
976  double& distOutside2,
977  double* edgeRZnorm)
978 {
979  //
980  // Convert our point to r and z
981  //
982  double rx = p.Perp(), zx = p.z();
983 
984  //
985  // Change sign of r if we should
986  //
987  int part = 1;
988  if (rx < 0) part = -1;
989 
990  //
991  // Calculate return value
992  //
993  double deltaR = rx - r[0]*part, deltaZ = zx - z[0];
994  double answer = deltaR * rNorm*part + deltaZ * zNorm;
995 
996  //
997  // Are we off the surface in r,z space?
998  //
999  double q = deltaR * rS *part+ deltaZ * zS;
1000  if (q < 0)
1001  {
1002  distOutside2 = q * q;
1003  if (edgeRZnorm)
1004  {
1005  *edgeRZnorm = deltaR * rNormEdge[0]*part + deltaZ * zNormEdge[0];
1006  }
1007  }
1008  else if (q > length)
1009  {
1010  distOutside2 = UUtils::sqr(q - length);
1011  if (edgeRZnorm)
1012  {
1013  deltaR = rx - r[1]*part;
1014  deltaZ = zx - z[1];
1015  *edgeRZnorm = deltaR * rNormEdge[1]*part + deltaZ * zNormEdge[1];
1016  }
1017  }
1018  else
1019  {
1020  distOutside2 = 0;
1021  if (edgeRZnorm) *edgeRZnorm = answer;
1022  }
1023 
1024  if (phiIsOpen)
1025  {
1026  //
1027  // Finally, check phi
1028  //
1029  double phi = GetPhi(p);
1030  while (phi < startPhi) phi += 2 * UUtils::kPi;
1031 
1032  if (phi > startPhi + deltaPhi)
1033  {
1034  //
1035  // Oops. Are we closer to the start phi or end phi?
1036  //
1037  double d1 = phi - startPhi - deltaPhi;
1038  while (phi > startPhi) phi -= 2 * UUtils::kPi;
1039  double d2 = startPhi - phi;
1040 
1041  if (d2 < d1) d1 = d2;
1042 
1043  //
1044  // Add result to our distance
1045  //
1046  double dist = d1 * rx*part;
1047 
1048  distOutside2 += dist * dist;
1049  if (edgeRZnorm)
1050  {
1051  *edgeRZnorm = std::max(std::fabs(*edgeRZnorm), std::fabs(dist));
1052  }
1053  }
1054  }
1055 
1056  return answer;
1057 }
1058 
1059 
1060 //
1061 // PointOnCone
1062 //
1063 // Decide if a point is on a cone and return normal if it is
1064 //
1066  double normSign,
1067  const UVector3& p,
1068  const UVector3& v,
1069  UVector3& normal)
1070 {
1071  double rx = hit.Perp();
1072  //
1073  // Check radial/z extent, as appropriate
1074  //
1075  if (!cone->HitOn(rx, hit.z())) return false;
1076 
1077  if (phiIsOpen)
1078  {
1079  double phiTolerant = 2.0 * VUSolid::Tolerance() / (rx + VUSolid::Tolerance());
1080  //
1081  // Check phi segment. Here we have to be careful
1082  // to use the standard method consistent with
1083  // PolyPhiFace. See PolyPhiFace::InsideEdgesExact
1084  //
1085  double phi = GetPhi(hit);
1086  while (phi < startPhi - phiTolerant) phi += 2 * UUtils::kPi;
1087 
1088  if (phi > startPhi + deltaPhi + phiTolerant) return false;
1089 
1090  if (phi > startPhi + deltaPhi - phiTolerant)
1091  {
1092  //
1093  // Exact treatment
1094  //
1095  UVector3 qx = p + v;
1096  UVector3 qa = qx - corners[2],
1097  qb = qx - corners[3];
1098  UVector3 qacb = qa.Cross(qb);
1099 
1100  if (normSign * qacb.Dot(v) < 0) return false;
1101  }
1102  else if (phi < phiTolerant)
1103  {
1104  UVector3 qx = p + v;
1105  UVector3 qa = qx - corners[1],
1106  qb = qx - corners[0];
1107  UVector3 qacb = qa.Cross(qb);
1108 
1109  if (normSign * qacb.Dot(v) < 0) return false;
1110  }
1111  }
1112 
1113  //
1114  // We have a good hit! Calculate normal
1115  //
1116  if (rx < DBL_MIN)
1117  normal = UVector3(0, 0, zNorm < 0 ? -1 : 1);
1118  else
1119  normal = UVector3(rNorm * hit.x() / rx, rNorm * hit.y() / rx, zNorm);
1120  return true;
1121 }
1122 
1123 
1124 //
1125 // FindLineIntersect
1126 //
1127 // Decide the point at which two 2-dimensional lines intersect
1128 //
1129 // Equation of line: x = x1 + s*tx1
1130 // y = y1 + s*ty1
1131 //
1132 // It is assumed that the lines are *not* parallel
1133 //
1134 void UPolyconeSide::FindLineIntersect(double x1, double y1,
1135  double tx1, double ty1,
1136  double x2, double y2,
1137  double tx2, double ty2,
1138  double& x, double& y)
1139 {
1140  //
1141  // The solution is a simple linear equation
1142  //
1143  double deter = tx1 * ty2 - tx2 * ty1;
1144 
1145  double s1 = ((x2 - x1) * ty2 - tx2 * (y2 - y1)) / deter;
1146  double s2 = ((x2 - x1) * ty1 - tx1 * (y2 - y1)) / deter;
1147 
1148  //
1149  // We want the answer to not depend on which order the
1150  // lines were specified. Take average.
1151  //
1152  x = 0.5 * (x1 + s1 * tx1 + x2 + s2 * tx2);
1153  y = 0.5 * (y1 + s1 * ty1 + y2 + s2 * ty2);
1154 }
1155 
1156 //
1157 // Calculate surface area for GetPointOnSurface()
1158 //
1160 {
1161  if (fSurfaceArea == 0)
1162  {
1163  fSurfaceArea = (r[0] + r[1]) * std::sqrt(UUtils::sqr(r[0] - r[1]) + UUtils::sqr(z[0] - z[1]));
1164  fSurfaceArea *= 0.5 * (deltaPhi);
1165  }
1166  return fSurfaceArea;
1167 }
1168 
1169 //
1170 // GetPointOnFace
1171 //
1173 {
1174  double x, y, zz;
1175  double rr, phi, dz, dr;
1176  dr = r[1] - r[0];
1177  dz = z[1] - z[0];
1178  phi = startPhi + deltaPhi * UUtils::Random();
1179  rr = r[0] + dr * UUtils::Random();
1180 
1181  x = rr * std::cos(phi);
1182  y = rr * std::sin(phi);
1183 
1184  // PolyconeSide has a Ring Form
1185  //
1186  if (dz == 0.)
1187  {
1188  zz = z[0];
1189  }
1190  else
1191  {
1192  if (dr == 0.) // PolyconeSide has a Tube Form
1193  {
1194  zz = z[0] + dz * UUtils::Random();
1195  }
1196  else
1197  {
1198  zz = z[0] + (rr - r[0]) * dz / dr;
1199  }
1200  }
1201 
1202  return UVector3(x, y, zz);
1203 }
double & z()
Definition: UVector3.hh:352
static const G4double d1
double & y()
Definition: UVector3.hh:348
UVector3 GetPointOnFace()
void CopyStuff(const UPolyconeSide &source)
double Phi() const
Definition: UVector3.cc:64
double DistanceAway(const UVector3 &p, bool opposite, double &distOutside2, double *rzNorm=0)
UVector3 Cross(const UVector3 &) const
Definition: UVector3.hh:281
double Extent(const UVector3 axis)
double zNormEdge[2]
UPolyconeSide & operator=(const UPolyconeSide &source)
VUSolid::EnumInside Inside(const UVector3 &p, double tolerance, double *bestDistance)
static const G4double tolerance
static const G4double cd
G4double a
Definition: TRTMaterials.hh:39
double GetPhi(const UVector3 &p)
static double Tolerance()
Definition: VUSolid.hh:127
bool HitOn(const double r, const double z)
static double normal(HepRandomEngine *eptr)
Definition: RandPoisson.cc:77
static const double kInfinity
Definition: UUtils.hh:54
UVector3 Normal(const UVector3 &p, double *bestDistance)
double & x()
Definition: UVector3.hh:344
double SurfaceArea()
EnumInside
Definition: VUSolid.hh:23
double Dot(const UVector3 &) const
Definition: UVector3.hh:276
T sqr(const T &x)
Definition: UUtils.hh:138
double Perp2() const
Definition: UVector3.hh:292
T max(const T t1, const T t2)
brief Return the largest of the two arguments
UIntersectingCone * cone
static const double kPi
Definition: UUtils.hh:49
double ZHi() const
UVector3 Unit() const
Definition: UVector3.cc:80
double Safety(const UVector3 &p, bool outgoing)
#define DBL_MIN
Definition: templates.hh:75
virtual ~UPolyconeSide()
UPolyconeSide(const UPolyconeSideRZ *prevRZ, const UPolyconeSideRZ *tail, const UPolyconeSideRZ *head, const UPolyconeSideRZ *nextRZ, double phiStart, double deltaPhi, bool phiIsOpen, bool isAllBehind=false)
double Perp() const
Definition: UVector3.cc:56
UVector3 * corners
bool Distance(const UVector3 &p, const UVector3 &v, bool outgoing, double surfTolerance, double &distance, double &distFromSurface, UVector3 &normal, bool &isAllBehind)
static void FindLineIntersect(double x1, double y1, double tx1, double ty1, double x2, double y2, double tx2, double ty2, double &x, double &y)
static const G4double d2
double Random(double min=0.0, double max=1.0)
Definition: UUtils.cc:69
double ZLo() const
int LineHitsCone(const UVector3 &p, const UVector3 &v, double &s1, double &s2)
double rNormEdge[2]
bool PointOnCone(const UVector3 &hit, double normSign, const UVector3 &p, const UVector3 &v, UVector3 &normal)