File: EmbreeNavigator.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 (311 lines) | stat: -rw-r--r-- 12,090 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
/*
 * EmbreeNavigator.h
 *
 *  Created on: May 18, 2018
 *      Author: swenzel
 */

#ifndef VECGEOM_NAVIGATION_EMBREENAVIGATOR_H_
#define VECGEOM_NAVIGATION_EMBREENAVIGATOR_H_

#include "VecGeom/base/Global.h"

#include "VecGeom/volumes/PlacedVolume.h"
#include "VecGeom/base/Vector3D.h"
#include "VecGeom/management/GeoManager.h"
#include "VecGeom/navigation/NavigationState.h"
#include "VecGeom/base/Transformation3D.h"
#include "VecGeom/management/EmbreeManager.h"
#include "VecGeom/navigation/VNavigator.h"
#include "VecGeom/navigation/HybridSafetyEstimator.h"
#include "VecGeom/navigation/SimpleABBoxNavigator.h"

#include <vector>
#include <stack>

namespace vecgeom {
inline namespace VECGEOM_IMPL_NAMESPACE {

extern double g_step;
extern LogicalVolume const *g_lvol;
extern VPlacedVolume const *g_pvol;
extern VPlacedVolume const *g_lastexited;
extern Vector3D<double> const *g_pos;
extern Vector3D<double> const *g_dir;
extern Vector3D<float> const *g_normals;
extern int g_count;
extern std::vector<int> *g_geomIDs;
extern EmbreeManager::BoxIdDistancePair_t *g_hitlist;

// A navigator using Intel Embree as the underlying acceleration library to
// exclude hit targets quickly
template <bool MotherIsConvex = false>
class EmbreeNavigator : public VNavigatorHelper<EmbreeNavigator<MotherIsConvex>, MotherIsConvex> {

private:
  EmbreeManager &fAccelerationManager;
  EmbreeNavigator()
      : VNavigatorHelper<EmbreeNavigator<MotherIsConvex>, MotherIsConvex>(SimpleABBoxSafetyEstimator::Instance()),
        fAccelerationManager(EmbreeManager::Instance())
  {
  }

  static VPlacedVolume const *LookupDaughter(LogicalVolume const *lvol, int const daughterIndex)
  {
    return lvol->GetDaughters()[daughterIndex];
  }

  // a simple sort class (based on insertionsort)
  template <typename T> //, typename Cmp>
  static void insertionsort(T *arr, unsigned int N)
  {
    for (unsigned short i = 1; i < N; ++i) {
      T value    = arr[i];
      short hole = i;

      for (; hole > 0 && value.second < arr[hole - 1].second; --hole)
        arr[hole] = arr[hole - 1];

      arr[hole] = value;
    }
  }

  /**
   * Returns hitlist of daughter candidates (pairs of [daughter index, step to bounding box]) crossed by ray.
   */
  size_t GetHitCandidates_v(EmbreeManager::EmbreeAccelerationStructure const &accstructure,
                            Vector3D<Precision> const &point, Vector3D<Precision> const &dir,
                            EmbreeManager::BoxIdDistancePair_t *hitlist, float step) const
  {
    if (accstructure.fNumberObjects == 0) return false;
    // we need to setup an Embree ray
    RTCRayHit ray;
    ray.ray.flags = 0;
    ray.ray.org_x = point.x();
    ray.ray.org_y = point.y();
    ray.ray.org_z = point.z();
    ray.ray.dir_x = dir.x();
    ray.ray.dir_y = dir.y();
    ray.ray.dir_z = dir.z();
    ray.ray.tnear = 0.f;
    ray.ray.tfar  = 1E20f; // step is the current limit

    g_normals = accstructure.fNormals;
    g_hitlist = hitlist;
    g_count   = 0;
    g_geomIDs->clear(); // = geom_seen;

    RTCIntersectContext context;
    // we can't do a real capture ... so we do it via global variables
    auto hitFilter = [](const RTCFilterFunctionNArguments *args) {
      // check if a hit in this Embree structure leads to a hit
      // in the real geometry
      const auto hit = (RTCHit *)args->hit;
      int *hitvalid  = args->valid;
      const auto id  = hit->geomID;
      // if (g_geomIDs[id]) {
      //  hitvalid[0] = 0;
      //  return;
      // }
      // g_geomIDs[id] = true;
      if (std::find(g_geomIDs->begin(), g_geomIDs->end(), id) != g_geomIDs->end()) {
        hitvalid[0] = 0;
        return;
      }
      g_geomIDs->push_back(id);

      const auto ray      = (RTCRay *)args->ray;
      const auto normalID = id * 6 + hit->primID;
      const auto normal   = g_normals[normalID];
      const bool backface = ray->dir_x * normal.x() + ray->dir_y * normal.y() + ray->dir_z * normal.z() > 0;
      float dist          = backface ? -1.f : ray->tfar;

      // no need to sort twice !!! (in principle this thing is giving sorted inters??)

      // std::cerr << "putting " << id << " " << dist << " " << ray->tfar << "\n";
      g_hitlist[g_count++] = HybridManager2::BoxIdDistancePair_t(id, dist);
      // we strictly take all hits
      hitvalid[0] = 0;
    };

    rtcInitIntersectContext(&context);
    context.filter = hitFilter;
    rtcIntersect1(accstructure.fScene, &context, &ray);

    // at this moment we have the result
    return g_count;
  }

public:
  // a generic looper function that
  // given an acceleration structure (an aligned bounding box hierarchy),
  // a hit-query will be performed, the intersected boxes sorted, looped over
  // and a user hook called for processing
  // the user hook needs to indicate with a boolean return value whether to continue looping (false)
  // or whether we are done (true) and can exit

  // FIXME: might be generic enough to work for all possible kinds of BVH structures
  // FIXME: offer various sorting directions, etc.
  template <typename AccStructure, typename Func>
  VECGEOM_FORCE_INLINE
  void BVHSortedIntersectionsLooper(AccStructure const &accstructure, Vector3D<Precision> const &localpoint,
                                    Vector3D<Precision> const &localdir, float stepmax, Func &&userhook) const
  {
    // The following construct reserves stackspace for objects
    // of type IdDistPair_t WITHOUT initializing those objects
    using IdDistPair_t = HybridManager2::BoxIdDistancePair_t;
    char stackspace[VECGEOM_MAXFACETS * sizeof(IdDistPair_t)];
    IdDistPair_t *hitlist = reinterpret_cast<IdDistPair_t *>(&stackspace);

    auto ncandidates = GetHitCandidates_v(accstructure, localpoint, localdir, hitlist, stepmax);
    // sort candidates according to their bounding volume hit distance
    insertionsort(hitlist, ncandidates);

    // for (int c = 0; c < ncandidates; ++c) {
    //	std::cerr << "CAND " << 0 << hitlist[c].first << " " << hitlist[c].second << "\n";
    // }

    for (size_t index = 0; index < ncandidates; ++index) {
      auto hitbox = hitlist[index];
      // here we got the hit candidates
      // now we execute user specific code to process this "hitbox"
      auto done = userhook(hitbox);
      if (done) break;
    }
  }

  VECGEOM_FORCE_INLINE
  virtual bool CheckDaughterIntersections(LogicalVolume const *lvol, Vector3D<Precision> const &localpoint,
                                          Vector3D<Precision> const &localdir, NavigationState const *in_state,
                                          NavigationState * /*out_state*/, Precision &step,
                                          VPlacedVolume const *&hitcandidate) const override
  {
    if (lvol->GetDaughtersp()->size() == 0) return false;
    auto &accstructure = *fAccelerationManager.GetAccStructure(lvol);

    BVHSortedIntersectionsLooper(
        accstructure, localpoint, localdir, step, [&](HybridManager2::BoxIdDistancePair_t hitbox) {
          // only consider those hitboxes which are within potential reach of this step
          if (!(step < hitbox.second)) {
            VPlacedVolume const *candidate = LookupDaughter(lvol, hitbox.first);
            Precision ddistance            = candidate->DistanceToIn(localpoint, localdir, step);
            const auto valid               = !IsInf(ddistance) && ddistance < step &&
                               !((ddistance <= 0.) && in_state && in_state->GetLastExited() == candidate);
            hitcandidate = valid ? candidate : hitcandidate;
            step         = valid ? ddistance : step;
            return false; // not yet done; need to continue in looper
          }
          return true; // mark done in this case
        });
    return false;
  }

  //  VECGEOM_FORCE_INLINE
  //  virtual bool CheckDaughterIntersections(LogicalVolume const *lvol, Vector3D<Precision> const &localpoint,
  //                                          Vector3D<Precision> const &localdir, NavigationState const *in_state,
  //                                          NavigationState * /*out_state*/, Precision &step,
  //                                          VPlacedVolume const *&hitcandidate) const override
  //  {
  //	bool stackspace[VECGEOM_MAXFACETS * sizeof(bool)]; // alternatives are a thread_local static vector?
  //
  //    if (lvol->GetDaughtersp()->size() == 0) return false;
  //    const auto &accstructure = *fAccelerationManager.GetAccStructure(lvol);
  //
  //    for (int i = 0; i < lvol->GetDaughtersp()->size(); ++i) {
  //      stackspace[i] = false;
  //    }
  //
  //    // we need to setup an Embree ray
  //    RTCRayHit ray;
  //    ray.ray.flags = 0;
  //    ray.ray.org_x = localpoint.x();
  //    ray.ray.org_y = localpoint.y();
  //    ray.ray.org_z = localpoint.z();
  //    ray.ray.dir_x = localdir.x();
  //    ray.ray.dir_y = localdir.y();
  //    ray.ray.dir_z = localdir.z();
  //    ray.ray.tnear = 0.f;
  //    ray.ray.tfar  = 1E20; // step is the current limit
  //
  //    g_pos = &localpoint;
  //    g_dir = &localdir;
  //    g_step = step;
  //    g_lastexited = in_state ? in_state->GetLastExited() : nullptr;
  //    g_normals = accstructure.fNormals;
  //
  //    g_lvol = lvol;
  //    g_pvol = nullptr;
  //
  //    // NOT THREAD SAFE and VERY DANGEROUS; I WOULD INSTEAD VERY MUCH
  //    // LIKE TO BE ABLE TO CAPTURE THINGS IN THE FILTER LAMBDA
  //    g_geomIDs = stackspace;
  //    RTCIntersectContext context;
  //
  //    static int counter = 0;
  //    std::cerr << "# " << counter++ << "\n";
  //
  //    // we can't do a real capture ... so we do it via global variables
  //    auto hitFilter = [](const RTCFilterFunctionNArguments *args) {
  //      // check if a hit in this Embree structure leads to a hit
  //      // in the real geometry
  //
  //      assert(args->N == 1);
  //      const auto hit = (RTCHit *)args->hit;
  //      const auto id  = hit->geomID;
  //      int *hitvalid  = args->valid;
  //
  //      // if geometryID (aka bounding volume mesh) already treated we can exit
  //      if (g_geomIDs[id] == true) {
  //        hitvalid[0] = 0;
  //        return;
  //      }
  //      g_geomIDs[id] = true;
  //
  //      const auto ray = (RTCRay *)args->ray;
  //      std::cout << id << " "
  //                << " " << hit->primID << " " << ray->tfar << " (" << hit->Ng_x << "," << hit->Ng_y << "," <<
  //                hit->Ng_z
  //                << ") ";
  //      const auto normalID = id * 12 + hit->primID;
  //      const auto normal   = g_normals[normalID];
  //      const bool backface = ray->dir_x * normal.x() + ray->dir_y * normal.y() + ray->dir_z * normal.z() < 0;
  //      std::cout << "backface " << backface << "\n";
  //
  //      // this is assuming we get the hits in increasing distance order which somehow is not true???
  //      //if (!backface && g_step < ray->tfar) {
  //        // we can return early here (and not invalidating the hit) --> we are done!!
  //       // return;
  //      //}
  //
  //      const auto candidate = LookupDaughter(g_lvol, id);
  //      const auto ddistance = candidate->DistanceToIn(*g_pos, *g_dir, g_step);
  //      const auto valid = !IsInf(ddistance) && ddistance < g_step && !((ddistance <= 0.) && g_lastexited ==
  //      candidate);
  //      g_pvol           = valid ? candidate : g_pvol;
  //      g_step           = valid ? ddistance : g_step;
  //      hitvalid[0]      = 0;
  //    };
  //
  //    rtcInitIntersectContext(&context);
  //    context.filter = hitFilter;
  //    rtcIntersect1(accstructure.fScene, &context, &ray);
  //
  //    // at this moment we have the result
  //    hitcandidate = g_pvol;
  //    step = g_step;
  //    return false;
  //  }

  static VNavigator *Instance()
  {
    static EmbreeNavigator instance;
    return &instance;
  }

  static constexpr const char *gClassNameString = "EmbreeNavigator";
  typedef SimpleABBoxSafetyEstimator SafetyEstimator_t;
};
} // namespace VECGEOM_IMPL_NAMESPACE
} // namespace vecgeom

#endif /* VECGEOM_NAVIGATION_EMBREENAVIGATOR_H_ */