File: HypeUtilities.h

package info (click to toggle)
vecgeom 1.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 23,928 kB
  • sloc: cpp: 88,717; ansic: 6,894; python: 1,035; sh: 582; sql: 538; makefile: 29
file content (442 lines) | stat: -rw-r--r-- 17,886 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
 * HypeUtilities.h
 *
 *  Created on: Jun 19, 2017
 *      Author: rsehgal
 */

#ifndef VOLUMES_HYPEUTILITIES_H_
#define VOLUMES_HYPEUTILITIES_H_

#include "VecGeom/base/Global.h"
#include "VecGeom/volumes/Wedge_Evolution.h"
#include "VecGeom/base/Vector3D.h"
#include "VecGeom/volumes/kernel/GenericKernels.h"
#include "VecGeom/volumes/kernel/shapetypes/HypeTypes.h"
#include <cstdio>

namespace vecgeom {

inline namespace VECGEOM_IMPL_NAMESPACE {

class UnplacedHype;
template <typename T>
struct HypeStruct;

namespace HypeUtilities {
using UnplacedStruct_t = HypeStruct<Precision>;
template <typename Real_v, typename hypeType>
VECCORE_ATT_HOST_DEVICE
typename vecCore::Mask_v<Real_v> IsCompletelyOutside(UnplacedStruct_t const &hype, Vector3D<Real_v> const &point)
{
  using namespace ::vecgeom::HypeTypes;
  using Bool_v = typename vecCore::Mask_v<Real_v>;
  Real_v r2    = point.Perp2();
  Real_v oRad2 = (hype.fRmax2 + hype.fTOut2 * point.z() * point.z());

  Bool_v completelyoutside = (Abs(point.z()) > (hype.fDz + hype.zToleranceLevel));
  if (vecCore::MaskFull(completelyoutside)) return completelyoutside;
  completelyoutside |= (r2 > oRad2 + hype.outerRadToleranceLevel);
  if (vecCore::MaskFull(completelyoutside)) return completelyoutside;

  // if (hype.InnerSurfaceExists()) {
  if (checkInnerSurfaceTreatment<hypeType>(hype)) {
    Real_v iRad2 = (hype.fRmin2 + hype.fTIn2 * point.z() * point.z());
    completelyoutside |= (r2 < (iRad2 - hype.innerRadToleranceLevel));
  }
  return completelyoutside;
}

template <typename Real_v, typename hypeType>
VECCORE_ATT_HOST_DEVICE
typename vecCore::Mask_v<Real_v> IsCompletelyInside(UnplacedStruct_t const &hype, Vector3D<Real_v> const &point)
{
  using namespace ::vecgeom::HypeTypes;
  using Bool_v = typename vecCore::Mask_v<Real_v>;
  Real_v r2    = point.Perp2();
  Real_v oRad2 = (hype.fRmax2 + hype.fTOut2 * point.z() * point.z());

  Bool_v completelyinside =
      (Abs(point.z()) < (hype.fDz - hype.zToleranceLevel)) && (r2 < oRad2 - hype.outerRadToleranceLevel);
  // if (hype.InnerSurfaceExists()) {
  if (checkInnerSurfaceTreatment<hypeType>(hype)) {
    Real_v iRad2 = (hype.fRmin2 + hype.fTIn2 * point.z() * point.z());
    completelyinside &= (r2 > (iRad2 + hype.innerRadToleranceLevel));
  }
  return completelyinside;
}

template <typename Real_v, bool ForInnerRad>
VECCORE_ATT_HOST_DEVICE
Real_v RadiusHypeSq(UnplacedStruct_t const &hype, Real_v z)
{

  if (ForInnerRad)
    return (hype.fRmin2 + hype.fTIn2 * z * z);
  else
    return (hype.fRmax2 + hype.fTOut2 * z * z);
}

template <typename Real_v>
VECCORE_ATT_HOST_DEVICE
typename vecCore::Mask_v<Real_v> IsPointMovingInsideOuterSurface(UnplacedStruct_t const &hype,
                                                                 Vector3D<Real_v> const &point,
                                                                 Vector3D<Real_v> const &direction)
{
  Real_v pz = point.z();
  Real_v vz = direction.z();
  vecCore__MaskedAssignFunc(vz, pz < Real_v(0.), -vz);
  vecCore__MaskedAssignFunc(pz, pz < Real_v(0.), -pz);
  return ((point.x() * direction.x() + point.y() * direction.y() - pz * hype.fTOut2 * vz) < Real_v(0.));
}

template <typename Real_v>
VECCORE_ATT_HOST_DEVICE
typename vecCore::Mask_v<Real_v> IsPointMovingInsideInnerSurface(UnplacedStruct_t const &hype,
                                                                 Vector3D<Real_v> const &point,
                                                                 Vector3D<Real_v> const &direction)
{
  Real_v pz = point.z();
  Real_v vz = direction.z();

  vecCore__MaskedAssignFunc(vz, pz < Real_v(0.), -vz);
  vecCore__MaskedAssignFunc(pz, pz < Real_v(0.), -pz);

  // Precision tanInnerStereo2 = hype.GetTIn2();
  return ((point.x() * direction.x() + point.y() * direction.y() - pz * hype.fTIn2 * vz) > Real_v(0.));
}

template <typename Real_v, typename hypeType>
VECCORE_ATT_HOST_DEVICE
typename vecCore::Mask_v<Real_v> IsPointOnSurfaceAndMovingInside(UnplacedStruct_t const &hype,
                                                                 Vector3D<Real_v> const &point,
                                                                 Vector3D<Real_v> const &direction)
{
  using namespace ::vecgeom::HypeTypes;
  using Bool_v = typename vecCore::Mask_v<Real_v>;
  Bool_v innerHypeSurf(false), outerHypeSurf(false), zSurf(false);
  Bool_v done(false);
  Real_v rho2  = point.Perp2();
  Real_v radI2 = RadiusHypeSq<Real_v, true>(hype, point.z());
  Real_v radO2 = RadiusHypeSq<Real_v, false>(hype, point.z());

  Bool_v in(false);
  zSurf = ((rho2 - hype.fEndOuterRadius2) < kTolerance) && ((hype.fEndInnerRadius2 - rho2) < kTolerance) &&
          (Abs(Abs(point.z()) - hype.fDz) < kTolerance);
  in |= (zSurf && (point.z() * direction.z() < Real_v(0.)));

  done |= zSurf;
  if (vecCore::MaskFull(done)) return in;

  outerHypeSurf |= (!zSurf && (Abs((radO2) - (rho2)) < hype.outerRadToleranceLevel));
  in |= (!done && outerHypeSurf && IsPointMovingInsideOuterSurface<Real_v>(hype, point, direction));

  // if (hype.InnerSurfaceExists()) {
  if (checkInnerSurfaceTreatment<hypeType>(hype)) {
    done |= (!zSurf && outerHypeSurf);
    if (vecCore::MaskFull(done)) return in;

    innerHypeSurf |= (!zSurf && !outerHypeSurf && (Abs((radI2) - (rho2)) < hype.innerRadToleranceLevel));
    in |= (!done && !zSurf && innerHypeSurf && IsPointMovingInsideInnerSurface<Real_v>(hype, point, direction));
    done |= (!zSurf && innerHypeSurf);
    if (vecCore::MaskFull(done)) return in;
  }
  return in;
}

template <typename Real_v, typename hypeType, bool ForDistToIn>
VECCORE_ATT_HOST_DEVICE
typename vecCore::Mask_v<Real_v> GetPointOfIntersectionWithZPlane(UnplacedStruct_t const &hype,
                                                                  Vector3D<Real_v> const &point,
                                                                  Vector3D<Real_v> const &direction, Real_v &zDist)
{
  using namespace ::vecgeom::HypeTypes;
  zDist = (Sign(ForDistToIn ? point.z() : direction.z()) * hype.fDz - point.z()) / NonZero(direction.z());

  auto r2 = (point + zDist * direction).Perp2();
  // if (!hype.InnerSurfaceExists())
  if (!checkInnerSurfaceTreatment<hypeType>(hype))
    return (r2 < hype.fEndOuterRadius2);
  else
    return ((r2 < hype.fEndOuterRadius2) && (r2 > hype.fEndInnerRadius2));
}

template <typename Real_v>
VECCORE_ATT_HOST_DEVICE
typename vecCore::Mask_v<Real_v> IsPointMovingOutsideOuterSurface(UnplacedStruct_t const &hype,
                                                                  Vector3D<Real_v> const &point,
                                                                  Vector3D<Real_v> const &direction)
{

  using Bool_v = typename vecCore::Mask_v<Real_v>;
  Bool_v out(false);

  Real_v pz = point.z();
  Real_v vz = direction.z();
  vecCore__MaskedAssignFunc(pz, vz < Real_v(0.), -pz);
  vecCore__MaskedAssignFunc(vz, vz < Real_v(0.), -vz);
  Vector3D<Real_v> normHere(point.x(), point.y(), -point.z() * hype.fTOut2);
  out = (normHere.Dot(direction) > Real_v(0.));
  return out;
}

template <typename Real_v>
VECCORE_ATT_HOST_DEVICE
typename vecCore::Mask_v<Real_v> IsPointMovingOutsideInnerSurface(UnplacedStruct_t const &hype,
                                                                  Vector3D<Real_v> const &point,
                                                                  Vector3D<Real_v> const &direction)
{

  Real_v pz = point.z();
  Real_v vz = direction.z();
  vecCore__MaskedAssignFunc(pz, vz < Real_v(0.), -pz);
  vecCore__MaskedAssignFunc(vz, vz < Real_v(0.), -vz);
  Vector3D<Real_v> normHere(-point.x(), -point.y(), point.z() * hype.fTIn2);
  return (normHere.Dot(direction) > Real_v(0.));
}

template <typename Real_v>
VECCORE_ATT_HOST_DEVICE
typename vecCore::Mask_v<Real_v> IsPointOnOuterSurfaceAndMovingOutside(UnplacedStruct_t const &hype,
                                                                       Vector3D<Real_v> const &point,
                                                                       Vector3D<Real_v> const &direction)
{

  using Bool_v = typename vecCore::Mask_v<Real_v>;
  Real_v rho2  = point.x() * point.x() + point.y() * point.y();
  Real_v absZ  = Abs(point.z());
  Real_v radO2 = RadiusHypeSq<Real_v, false>(hype, point.z());
  Bool_v out(false), outerHypeSurf(false);
  outerHypeSurf = (Abs((radO2) - (rho2)) < hype.outerRadToleranceLevel) && (absZ >= Real_v(0.)) && (absZ < hype.fDz);
  out           = outerHypeSurf && IsPointMovingOutsideOuterSurface<Real_v>(hype, point, direction);
  return out;
}

template <typename Real_v, typename hypeType>
VECCORE_ATT_HOST_DEVICE
typename vecCore::Mask_v<Real_v> IsPointOnInnerSurfaceAndMovingOutside(UnplacedStruct_t const &hype,
                                                                       Vector3D<Real_v> const &point,
                                                                       Vector3D<Real_v> const &direction)
{
  using namespace ::vecgeom::HypeTypes;
  using Bool_v = typename vecCore::Mask_v<Real_v>;
  Real_v rho2  = point.x() * point.x() + point.y() * point.y();
  Real_v absZ  = Abs(point.z());
  Real_v radI2 = RadiusHypeSq<Real_v, true>(hype, point.z());
  Bool_v out(false), innerHypeSurf(false);
  // if (hype.InnerSurfaceExists()) {
  if (checkInnerSurfaceTreatment<hypeType>(hype)) {
    innerHypeSurf = (Abs((radI2) - (rho2)) < hype.innerRadToleranceLevel) && (absZ >= Real_v(0.)) && (absZ < hype.fDz);
    out           = innerHypeSurf && HypeUtilities::IsPointMovingOutsideInnerSurface<Real_v>(hype, point, direction);
  }
  return out;
}

template <typename Real_v, typename hypeType>
VECCORE_ATT_HOST_DEVICE
typename vecCore::Mask_v<Real_v> IsPointOnSurfaceAndMovingOutside(UnplacedStruct_t const &hype,
                                                                  Vector3D<Real_v> const &point,
                                                                  Vector3D<Real_v> const &direction)
{
  using namespace ::vecgeom::HypeTypes;
  using Bool_v = typename vecCore::Mask_v<Real_v>;
  Bool_v innerHypeSurf(false), outerHypeSurf(false), zSurf(false);
  Bool_v done(false);

  Real_v rho2  = point.x() * point.x() + point.y() * point.y();
  Real_v radI2 = RadiusHypeSq<Real_v, true>(hype, point.z());
  Real_v radO2 = RadiusHypeSq<Real_v, false>(hype, point.z());

  Bool_v out(false);
  //  zSurf = ((hype.fEndOuterRadius2 - rho2) < kTolerance) && ((rho2 - hype.fEndInnerRadius2) < kTolerance) &&
  //          (Abs(Abs(point.z()) - hype.fDz) < kTolerance);
  zSurf = ((rho2 - hype.fEndOuterRadius2) < kTolerance) && ((hype.fEndInnerRadius2 - rho2) < kTolerance) &&
          (Abs(Abs(point.z()) - hype.fDz) < kTolerance);

  out |= (zSurf && (point.z() * direction.z() > Real_v(0.)));
  // done |= zSurf;
  done = out;
  if (vecCore::MaskFull(done)) return out;

  outerHypeSurf |= !done && (Abs(radO2 - rho2) < hype.outerRadToleranceLevel);
  // out |= (!done && !zSurf && outerHypeSurf &&
  out |= (outerHypeSurf && HypeUtilities::IsPointMovingOutsideOuterSurface<Real_v>(hype, point, direction));

  // done |= (!zSurf && outerHypeSurf);
  done |= out;
  if (vecCore::MaskFull(done)) return out;

  // if (hype.InnerSurfaceExists()) {
  if (checkInnerSurfaceTreatment<hypeType>(hype)) {
    // innerHypeSurf |= (!done && !zSurf && !outerHypeSurf && (Abs((radI2) - (rho2)) < hype.innerRadToleranceLevel));
    innerHypeSurf |= (!done && (Abs(radI2 - rho2) < hype.innerRadToleranceLevel));
    // out |= (!done && !zSurf && innerHypeSurf &&
    out |= (innerHypeSurf && HypeUtilities::IsPointMovingOutsideInnerSurface<Real_v>(hype, point, direction));
    // done |= (!zSurf && innerHypeSurf);
    done |= out;
    if (vecCore::MaskFull(done)) return out;
  }

  return out;
}

template <typename Real_v>
VECCORE_ATT_HOST_DEVICE
Real_v ApproxDistOutside(Real_v pr, Real_v pz, Precision r0, Precision tanPhi)
{
  Real_v r1 = Sqrt(r0 * r0 + tanPhi * tanPhi * pz * pz);
  Real_v z1 = pz;
  Real_v r2 = pr;
  Real_v z2 = Sqrt((pr * pr - r0 * r0) / (tanPhi * tanPhi));
  Real_v dz = z2 - z1;
  Real_v dr = r2 - r1;
  Real_v r3 = Sqrt(dr * dr + dz * dz);
  auto mask = r3 < vecCore::NumericLimits<Real_v>::Min();
  return vecCore::Blend(mask, (r2 - r1), (r2 - r1) * dz / r3);
}

template <typename Real_v>
VECCORE_ATT_HOST_DEVICE
Real_v ApproxDistInside(Real_v pr, Real_v pz, Precision r0, Precision tan2Phi)
{
  using Bool_v = typename vecCore::Mask_v<Real_v>;
  Bool_v done(false);
  Real_v ret(0.);
  Real_v tan2Phi_v(tan2Phi);
  vecCore__MaskedAssignFunc(ret, (tan2Phi_v < vecCore::NumericLimits<Real_v>::Min()), r0 - pr);
  done |= (tan2Phi_v < vecCore::NumericLimits<Real_v>::Min());
  if (vecCore::MaskFull(done)) return ret;

  Real_v rh  = Sqrt(r0 * r0 + pz * pz * tan2Phi_v);
  Real_v dr  = -rh;
  Real_v dz  = pz * tan2Phi_v;
  Real_v len = Sqrt(dr * dr + dz * dz);

  vecCore__MaskedAssignFunc(ret, !done, Abs((pr - rh) * dr) / len);
  return ret;
}

} // namespace HypeUtilities

/* This class  is basically constructed to allow partial specialization
 * for Scalar Backend.
 */
template <class Real_v, bool ForDistToIn, bool ForInnerSurface>
class HypeHelpers {

public:
  HypeHelpers() {}
  ~HypeHelpers() {}

  VECCORE_ATT_HOST_DEVICE
  static typename vecCore::Mask_v<Real_v> GetPointOfIntersectionWithHyperbolicSurface(HypeStruct<Precision> const &hype,
                                                                                      Vector3D<Real_v> const &point,
                                                                                      Vector3D<Real_v> const &direction,
                                                                                      Real_v &dist)
  {

    using Bool_v = typename vecCore::Mask_v<Real_v>;

    if (ForInnerSurface) {
      Real_v a     = direction.Perp2() - hype.fTIn2 * direction.z() * direction.z();
      Real_v b     = (direction.x() * point.x() + direction.y() * point.y() - hype.fTIn2 * direction.z() * point.z());
      Real_v c     = point.Perp2() - hype.fTIn2 * point.z() * point.z() - hype.fRmin2;
      Bool_v exist = (b * b - a * c > Real_v(0.));
      if (ForDistToIn) {
        vecCore__MaskedAssignFunc(dist, exist && b < Real_v(0.), ((-b + Sqrt(b * b - a * c)) / (a)));
        vecCore__MaskedAssignFunc(dist, exist && b >= Real_v(0.), ((c) / (-b - Sqrt(b * b - a * c))));

      } else {
        vecCore__MaskedAssignFunc(dist, exist && b > Real_v(0.), ((-b - Sqrt(b * b - a * c)) / (a)));
        vecCore__MaskedAssignFunc(dist, exist && b <= Real_v(0.), ((c) / (-b + Sqrt(b * b - a * c))));
      }

    } else {
      Real_v a     = direction.Perp2() - hype.fTOut2 * direction.z() * direction.z();
      Real_v b     = (direction.x() * point.x() + direction.y() * point.y() - hype.fTOut2 * direction.z() * point.z());
      Real_v c     = point.Perp2() - hype.fTOut2 * point.z() * point.z() - hype.fRmax2;
      Bool_v exist = (b * b - a * c > Real_v(0.));
      if (ForDistToIn) {
        vecCore__MaskedAssignFunc(dist, exist && b >= Real_v(0.), ((-b - Sqrt(b * b - a * c)) / (a)));
        vecCore__MaskedAssignFunc(dist, exist && b < Real_v(0.), ((c) / (-b + Sqrt(b * b - a * c))));
      } else {
        vecCore__MaskedAssignFunc(dist, exist && b < Real_v(0.), ((-b + Sqrt(b * b - a * c)) / (a)));
        vecCore__MaskedAssignFunc(dist, exist && b >= Real_v(0.), ((c) / (-b - Sqrt(b * b - a * c))));
      }
    }

    vecCore__MaskedAssignFunc(dist, dist < Real_v(0.), InfinityLength<Real_v>());

    Real_v newPtZ = point.z() + dist * direction.z();

    return (Abs(newPtZ) <= hype.fDz);
  }
};

template <bool ForDistToIn, bool ForInnerSurface>
class HypeHelpers<Precision, ForDistToIn, ForInnerSurface> {
public:
  HypeHelpers() {}
  ~HypeHelpers() {}

  VECCORE_ATT_HOST_DEVICE
  static bool GetPointOfIntersectionWithHyperbolicSurface(HypeStruct<Precision> const &hype,
                                                          Vector3D<Precision> const &point,
                                                          Vector3D<Precision> const &direction, Precision &dist)
  {
    if (ForInnerSurface) {
      Precision a = direction.Perp2() - hype.fTIn2 * direction.z() * direction.z();
      Precision b = (direction.x() * point.x() + direction.y() * point.y() - hype.fTIn2 * direction.z() * point.z());
      Precision c = point.Perp2() - hype.fTIn2 * point.z() * point.z() - hype.fRmin2;
      bool exist  = (b * b - a * c > 0.);
      if (exist) {
        if (ForDistToIn) {
          if (b < 0.)
            dist = ((-b + Sqrt(b * b - a * c)) / (a));
          else
            dist = ((c) / (-b - Sqrt(b * b - a * c)));
        } else {

          if (b > 0.)
            dist = ((-b - Sqrt(b * b - a * c)) / (a));
          else
            dist = ((c) / (-b + Sqrt(b * b - a * c)));
        }
      } else
        return false;

    } else {
      Precision a = direction.Perp2() - hype.fTOut2 * direction.z() * direction.z();
      Precision b = (direction.x() * point.x() + direction.y() * point.y() - hype.fTOut2 * direction.z() * point.z());
      Precision c = point.Perp2() - hype.fTOut2 * point.z() * point.z() - hype.fRmax2;
      bool exist  = (b * b - a * c > 0.);

      if (exist) {
        if (ForDistToIn) {
          if (b >= 0.)
            dist = ((-b - Sqrt(b * b - a * c)) / (a));
          else
            dist = ((c) / (-b + Sqrt(b * b - a * c)));
        } else {

          if (b < 0.)
            dist = ((-b + Sqrt(b * b - a * c)) / a);
          else
            dist = (c / (-b - Sqrt(b * b - a * c)));
        }
      } else
        return false;
    }
    if (dist < 0.) dist = kInfLength;
    // vecCore__MaskedAssignFunc(dist, dist < 0.0, InfinityLength<Real_v>());

    Precision newPtZ = point.z() + dist * direction.z();

    return (Abs(newPtZ) <= hype.fDz);
  }
};

} // namespace VECGEOM_IMPL_NAMESPACE
} // namespace vecgeom

#endif /* VOLUMES_HYPEUTILITIES_H_ */