File: SExtruImplementation.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 (284 lines) | stat: -rw-r--r-- 9,031 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
#ifndef VECGEOM_VOLUMES_KERNEL_SEXTRUIMPLEMENTATION_H_
#define VECGEOM_VOLUMES_KERNEL_SEXTRUIMPLEMENTATION_H_

#include "VecGeom/base/Vector3D.h"
#include "VecGeom/volumes/PolygonalShell.h"
#include "VecGeom/volumes/kernel/BoxImplementation.h"

namespace vecgeom {

VECGEOM_DEVICE_FORWARD_DECLARE(struct SExtruImplementation;);
VECGEOM_DEVICE_DECLARE_CONV(struct, SExtruImplementation);

inline namespace VECGEOM_IMPL_NAMESPACE {

class PlacedSExtru;
class PolygonalShell;
class UnplacedSExtruVolume;

struct SExtruImplementation {

  using PlacedShape_t    = PlacedSExtru;
  using UnplacedStruct_t = PolygonalShell;
  using UnplacedVolume_t = UnplacedSExtruVolume;

  VECCORE_ATT_HOST_DEVICE
  static void PrintType()
  {
    //
  }

  template <typename Stream>
  static void PrintType(Stream &st, int transC = translation::kGeneric, int rotC = rotation::kGeneric)
  {
    st << "SpecializedSExtru<" << transC << "," << rotC << "\n";
  }

  template <typename Stream>
  static void PrintImplementationType(Stream &st)
  {
    (void)st;
    // st << "SExtruImplementation<" << transCodeT << "," << rotCodeT << ">";
  }

  template <typename Stream>
  static void PrintUnplacedType(Stream &st)
  {
    (void)st;
    // TODO: this is wrong
    // st << "UnplacedSExtruVolume";
  }

  template <typename Real_v, typename Bool_v>
  VECGEOM_FORCE_INLINE
  VECCORE_ATT_HOST_DEVICE
  static void Contains(UnplacedStruct_t const &unplaced, Vector3D<Real_v> const &p, Bool_v &inside)
  {
    inside    = Bool_v(false);
    auto done = p.z() > Real_v(unplaced.fUpperZ);
    done |= p.z() < Real_v(unplaced.fLowerZ);
    if (vecCore::MaskFull(done)) return;
    if (unplaced.fPolygon.IsConvex())
      inside = !done && unplaced.fPolygon.ContainsConvex(p);
    else
      inside = !done && unplaced.fPolygon.Contains(p);
  }

  template <typename Real_v, typename Inside_t>
  VECGEOM_FORCE_INLINE
  VECCORE_ATT_HOST_DEVICE
  static void Inside(UnplacedStruct_t const &unplaced, Vector3D<Real_v> const &point, Inside_t &inside)
  {
    // this is a quick / non-optimized ans scalar only implementation:
    if (point.z() > unplaced.fUpperZ + kTolerance) {
      inside = vecgeom::kOutside;
      return;
    }
    if (point.z() < unplaced.fLowerZ - kTolerance) {
      inside = vecgeom::kOutside;
      return;
    }

    // check conditions for surface first
    using Bool_v = vecCore::Mask_v<Real_v>;
    Bool_v onZ   = Abs(point.z() - unplaced.fUpperZ) < kTolerance;
    onZ |= Abs(point.z() - unplaced.fLowerZ) < kTolerance;

    if (unplaced.fPolygon.IsConvex()) {
      inside                                         = unplaced.fPolygon.InsideConvex(point);
      if (onZ && inside != vecgeom::kOutside) inside = vecgeom::kSurface;
      return;
    }

    if (onZ) {
      if (unplaced.fPolygon.Contains(point)) {
        inside = vecgeom::kSurface;
        return;
      }
    }

    // not on z-surface --> check other surface with safety for moment
    if (unplaced.fLowerZ <= point.z() && point.z() <= unplaced.fUpperZ) {
      int unused;
      auto s = unplaced.fPolygon.SafetySqr(point, unused);
      if (s < kTolerance * kTolerance) {
        inside = vecgeom::kSurface;
        return;
      }
    }

    Bool_v c;
    Contains(unplaced, point, c);

    if (c)
      inside = vecgeom::kInside;
    else
      inside = vecgeom::kOutside;
    return;
  }

  template <typename Real_v>
  VECGEOM_FORCE_INLINE
  VECCORE_ATT_HOST_DEVICE
  static void DistanceToIn(UnplacedStruct_t const &polyshell, Vector3D<Real_v> const &p, Vector3D<Real_v> const &dir,
                           Real_v const & /*stepMax*/, Real_v &distance)
  {
    if (polyshell.fPolygon.IsConvex()) {
      distance = polyshell.DistanceToInConvex(p, dir);
      return;
    }
    distance = Real_v(kInfLength);
    // consider adding bounding box check

    // check collision with +z or -z
    const auto s = vecCore::Blend(dir.z() > Real_v(0.), p.z() - polyshell.fLowerZ, polyshell.fUpperZ - p.z());

    const auto canhit = s < Real_v(kTolerance);
    if (!vecCore::MaskEmpty(canhit)) {
      const auto dist = -s / Abs(dir.z());
      // propagate
      const auto xInters = p.x() + dist * dir.x();
      const auto yInters = p.y() + dist * dir.y();

      const auto hits = polyshell.fPolygon.Contains(Vector3D<Real_v>(xInters, yInters, Real_v(0.)));

      vecCore::MaskedAssign(distance, hits, dist);
      if (vecCore::MaskFull(hits)) {
        return;
      }
    }

    // check collision with polyshell
    vecCore__MaskedAssignFunc(distance, distance == Real_v(kInfLength), polyshell.DistanceToIn(p, dir));
    return;
  }

  template <typename Real_v>
  VECGEOM_FORCE_INLINE
  VECCORE_ATT_HOST_DEVICE
  static void DistanceToOut(UnplacedStruct_t const &polyshell, Vector3D<Real_v> const &p, Vector3D<Real_v> const &dir,
                            Real_v const & /* stepMax */, Real_v &distance)
  {
    if (polyshell.fPolygon.IsConvex()) {
      distance = polyshell.DistanceToOutConvex(p, dir);
      return;
    }
    distance = Real_v(-1.);
    // or do a hit check; if not then it has to be the z planes
    const auto dshell   = polyshell.DistanceToOut(p, dir);
    const auto hitshell = dshell < Real_v(kInfLength);
    if (vecCore::MaskFull(hitshell)) {
      distance = dshell;
      return;
    }
    const auto correctZ = vecCore::Blend(dir.z() > Real_v(0.), Real_v(polyshell.fUpperZ), Real_v(polyshell.fLowerZ));
    distance            = (correctZ - p.z()) / dir.z();
    return;
  }

  template <typename Real_v>
  VECGEOM_FORCE_INLINE
  VECCORE_ATT_HOST_DEVICE
  static void SafetyToIn(UnplacedStruct_t const &polyshell, Vector3D<Real_v> const &point, Real_v &safety)
  {
    if (polyshell.fPolygon.IsConvex()) {
      Real_v safeZ = vecCore::math::Max(polyshell.fLowerZ - point.z(), point.z() - polyshell.fUpperZ);
      safety       = vecCore::math::Max(safeZ, polyshell.fPolygon.SafetyConvex(point, false));
      return;
    }

    Vector3D<Precision> aMin, aMax;
    polyshell.Extent(aMin, aMax);

    using Bool_v = vecCore::Mask_v<Real_v>;
    Bool_v isInExtent;
    ABBoxImplementation::ABBoxContainsKernelGeneric(aMin, aMax, point, isInExtent);

    // no one is in --> return precise safety to box
    if (vecCore::MaskEmpty(isInExtent)) {
      const auto ssqr = ABBoxImplementation::ABBoxSafetySqr(aMin, aMax, point);
      if (ssqr <= 0.) {
        safety = 0.;
        return;
      }
      safety = std::sqrt(ssqr);
      return;
    }

    const auto zSafety1 = polyshell.fLowerZ - point.z();
    const auto zSafety2 = polyshell.fUpperZ - point.z();
    if (Abs(zSafety1) < kTolerance || Abs(zSafety2) < kTolerance) {
      // on the z - entering surface:
      // need more careful treatment
      bool c;
      Contains(polyshell, point, c);
      if (c) {
        safety = 0.;
        return;
      }
    }
    int unused;
    safety = std::sqrt(polyshell.fPolygon.SafetySqr(point, unused));
  }

  template <typename Real_v>
  VECGEOM_FORCE_INLINE
  VECCORE_ATT_HOST_DEVICE
  static void SafetyToOut(UnplacedStruct_t const &polyshell, Vector3D<Real_v> const &point, Real_v &safety)
  {
    int unused;
    if (polyshell.fPolygon.IsConvex()) {
      Real_v safeZ = vecCore::math::Min(point.z() - polyshell.fLowerZ, polyshell.fUpperZ - point.z());
      safety       = vecCore::math::Min(safeZ, polyshell.fPolygon.SafetyConvex(point, true));
      return;
    }
    safety = std::sqrt(polyshell.fPolygon.SafetySqr(point, unused));
    safety = Min(safety, polyshell.fUpperZ - point.z());
    safety = Min(safety, point.z() - polyshell.fLowerZ);
  }

  template <typename Real_v>
  VECGEOM_FORCE_INLINE
  VECCORE_ATT_HOST_DEVICE
  static Vector3D<Real_v> NormalKernel(UnplacedStruct_t const &unplaced, Vector3D<Real_v> const &point,
                                       typename vecCore::Mask_v<Real_v> &valid)
  {
    // very rough implementation
    // not doing any sort of vector addition for normals on corners etc.
    valid = false;
    Vector3D<Real_v> normal(0., 0., 0.);

    // check conditions for surface first
    using Bool_v    = vecCore::Mask_v<Real_v>;
    Bool_v onUpperZ = Abs(point.z() - unplaced.fUpperZ) < kTolerance;
    Bool_v onLowerZ = Abs(point.z() - unplaced.fLowerZ) < kTolerance;

    if (onUpperZ || onLowerZ) {
      if (unplaced.fPolygon.Contains(point)) {
        valid = true;
        if (onUpperZ)
          normal = Vector3D<Real_v>(0., 0., 1);
        else {
          normal = Vector3D<Real_v>(0., 0., -1.);
        }
        return normal;
      }
    }

    // not on z-surface --> check other surface with safety for moment
    if (unplaced.fLowerZ <= point.z() && point.z() <= unplaced.fUpperZ) {
      int surfaceindex;
      auto s = unplaced.fPolygon.SafetySqr(point, surfaceindex);
      normal = Vector3D<Real_v>(-unplaced.fPolygon.fA[surfaceindex], -unplaced.fPolygon.fB[surfaceindex], 0.);
      if (s < kTolerance * kTolerance) {
        valid = true;
      }
    }
    return normal;
  }

}; // End struct SExtruImplementation
}
} // end namespaces

#endif