File: NewSimpleNavigator.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 (259 lines) | stat: -rw-r--r-- 11,067 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
/*
 * NewSimpleNavigator.h
 *
 *  Created on: 17.09.2015
 *      Author: swenzel
 */

#ifndef NAVIGATION_NEWSIMPLENAVIGATOR_H_
#define NAVIGATION_NEWSIMPLENAVIGATOR_H_

#include "VNavigator.h"
#include "SimpleSafetyEstimator.h"

namespace vecgeom {
inline namespace VECGEOM_IMPL_NAMESPACE {

// A very basic implementation of a navigator ( brute force which scales linearly with the number of daughters )
template <bool MotherIsConvex = false>
class NewSimpleNavigator : public VNavigatorHelper<class NewSimpleNavigator<MotherIsConvex>, MotherIsConvex> {

private:
  VECCORE_ATT_DEVICE
  NewSimpleNavigator()
      : VNavigatorHelper<class NewSimpleNavigator<MotherIsConvex>, MotherIsConvex>(SimpleSafetyEstimator::Instance()) {
  } VECCORE_ATT_DEVICE virtual ~NewSimpleNavigator() {}

public:
  VECGEOM_FORCE_INLINE
  VECCORE_ATT_HOST_DEVICE
  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
  {
    // iterate over all daughters
    auto *daughters = lvol->GetDaughtersp();
    auto ndaughters = daughters->size();
    for (decltype(ndaughters) d = 0; d < ndaughters; ++d) {
      auto daughter = daughters->operator[](d);
//    previous distance becomes step estimate, distance to daughter returned in workspace
// SW: this makes the navigation more robust and it appears that I have to
// put this at the moment since not all shapes respond yet with a negative distance if
// the point is actually inside the daughter
#ifdef CHECKCONTAINS
      bool contains = daughter->Contains(localpoint);
      if (!contains) {
#endif
        Precision ddistance = daughter->DistanceToIn(localpoint, localdir, step);

        // if distance is negative; we are inside that daughter and should relocate
        // unless distance is minus infinity
        const bool valid = (ddistance < step && !IsInf(ddistance)) &&
                           !((ddistance <= 0.) && in_state && in_state->GetLastExited() == daughter);
        hitcandidate = valid ? daughter : hitcandidate;
        step         = valid ? ddistance : step;
#ifdef CHECKCONTAINS
      } else {
        std::cerr << " INDA "
                  << " contained in daughter " << daughter << " - inside = " << daughter->Inside(localpoint)
                  << " , distToIn(p,v,s) = " << daughter->DistanceToIn(localpoint, localdir, step) << " \n";

        std::cerr << " INDA ";
        step         = -1.;
        hitcandidate = daughter;
        break;
      }
#endif
    }
    return false;
  }

  VECGEOM_FORCE_INLINE
  VECCORE_ATT_HOST_DEVICE
  virtual bool CheckDaughterIntersections(LogicalVolume const *lvol, Vector3D<Precision> const &localpoint,
                                          Vector3D<Precision> const &localdir, VPlacedVolume const *blocked,
                                          Precision &step, VPlacedVolume const *&hitcandidate) const override
  {
    //  New Implementation JA 2021.03.18
    static const double kMinExitingCos = 1.e-3;
    VPlacedVolume const *excludedVol   = nullptr;
    if (blocked) {
      Vector3D<Precision> normal;
      blocked->Normal(localpoint, normal);
      if (normal.Dot(localdir) >= kMinExitingCos) {
        excludedVol = blocked;
      }
    }

    // iterate over all daughters
    auto *daughters = lvol->GetDaughtersp();
    auto ndaughters = daughters->size();
    for (decltype(ndaughters) d = 0; d < ndaughters; ++d) {
      auto daughter = daughters->operator[](d);
//    previous distance becomes step estimate, distance to daughter returned in workspace
// SW: this makes the navigation more robust and it appears that I have to
// put this at the moment since not all shapes respond yet with a negative distance if
// the point is actually inside the daughter
#ifdef CHECKCONTAINS
      bool contains = daughter->Contains(localpoint);
      if (!contains) {
#endif
        if (daughter != excludedVol) {
          Precision ddistance = daughter->DistanceToIn(localpoint, localdir, step);

          // if distance is negative; we are inside that daughter and should relocate
          // unless distance is minus infinity
          const bool valid = (ddistance < step && !IsInf(ddistance));
          hitcandidate     = valid ? daughter : hitcandidate;
          step             = valid ? ddistance : step;
        }
#ifdef CHECKCONTAINS
      } else {
        std::cerr << " INDA: contained in daughter " << daughter << " - inside = " << daughter->Inside(localpoint)
                  << " , distToIn(p,v,s) = " << daughter->DistanceToIn(localpoint, localdir, step) << " \n";
        step         = -1.;
        hitcandidate = daughter;
        break;
      }
#endif
    }
    // assert(false); --- Was not implemented before
    return false;
  }

  // Vector specialization for NewSimpleNavigator
  // TODO: unify with scalar use case
  template <typename T, unsigned int ChunkSize>
  VECCORE_ATT_HOST_DEVICE
  static void DaughterIntersectionsLooper(VNavigator const * /*nav*/, LogicalVolume const *lvol,
                                          Vector3D<T> const &localpoint, Vector3D<T> const &localdir,
                                          NavigationState const* const* /*in_states*/, NavigationState ** /*out_states*/,
                                          unsigned int from_index, Precision *out_steps,
                                          VPlacedVolume const *hitcandidates[ChunkSize])
  {
    // dispatch to vector implementation
    // iterate over all daughters
    // using Real_v = typename Backend::Real_v;
    // using Bool_v = Real_v::Mask;
    T step(vecCore::FromPtr<T>(out_steps + from_index));
    const auto *daughters = lvol->GetDaughtersp();
    auto ndaughters       = daughters->size();
    for (decltype(ndaughters) d = 0; d < ndaughters; ++d) {
      auto daughter = daughters->operator[](d);
      // SW: this makes the navigation more robust and it appears that I have to
      // put this at the moment since not all shapes respond yet with a negative distance if
      // the point is actually inside the daughter
      //  #ifdef CHECKCONTAINS
      //        Bool_v contains = daughter->Contains(localpoint);
      //        if (!contains) {
      //  #endif
      const T ddistance = daughter->DistanceToIn(localpoint, localdir, step);

      // if distance is negative; we are inside that daughter and should relocate
      // unless distance is minus infinity
      const auto valid = (ddistance < step && !IsInf(ddistance));

      // serial treatment of hit candidate until I find a better way
      // we might do this using a cast to a double vector with subsequent masked assignment
      if (!vecCore::MaskEmpty(valid)) {
        for (unsigned int i = 0 /*valid.firstOne()*/; i < ChunkSize; ++i) {
          hitcandidates[i] = vecCore::MaskLaneAt(valid, i) ? daughter : hitcandidates[i];
        }

        vecCore::MaskedAssign(step, valid, ddistance);
        //  #ifdef CHECKCONTAINS
        //        } else {
        //          std::cerr << " INDA ";
        //          step = -1.;
        //          hitcandidate = daughter;
        //          break;
        //        }
        //  #endif
      }
    }
    vecCore::Store(step, out_steps + from_index);
  }

  // Vector specialization for NewSimpleNavigator
  // TODO: unify with scalar use case
  template <typename T, unsigned int ChunkSize>
  VECCORE_ATT_HOST_DEVICE
  static void DaughterIntersectionsLooper(VNavigator const * /*nav*/, LogicalVolume const *lvol,
                                          Vector3D<T> const &localpoint, Vector3D<T> const &localdir,
                                          NavStatePool const &/*in_states*/, NavStatePool &/*out_states*/,
                                          unsigned int from_index, Precision *out_steps,
                                          VPlacedVolume const *hitcandidates[ChunkSize])
  {
    // dispatch to vector implementation
    // iterate over all daughters
    // using Real_v = typename Backend::Real_v;
    // using Bool_v = Real_v::Mask;
    T step(vecCore::FromPtr<T>(out_steps + from_index));
    const auto *daughters = lvol->GetDaughtersp();
    auto ndaughters       = daughters->size();
    for (decltype(ndaughters) d = 0; d < ndaughters; ++d) {
      auto daughter = daughters->operator[](d);
      // SW: this makes the navigation more robust and it appears that I have to
      // put this at the moment since not all shapes respond yet with a negative distance if
      // the point is actually inside the daughter
      //  #ifdef CHECKCONTAINS
      //        Bool_v contains = daughter->Contains(localpoint);
      //        if (!contains) {
      //  #endif
      const T ddistance = daughter->DistanceToIn(localpoint, localdir, step);

      // if distance is negative; we are inside that daughter and should relocate
      // unless distance is minus infinity
      const auto valid = (ddistance < step && !IsInf(ddistance));

      // serial treatment of hit candidate until I find a better way
      // we might do this using a cast to a double vector with subsequent masked assignment
      if (!vecCore::MaskEmpty(valid)) {
        for (unsigned int i = 0 /*valid.firstOne()*/; i < ChunkSize; ++i) {
          hitcandidates[i] = vecCore::MaskLaneAt(valid, i) ? daughter : hitcandidates[i];
        }

        vecCore::MaskedAssign(step, valid, ddistance);
        //  #ifdef CHECKCONTAINS
        //        } else {
        //          std::cerr << " INDA ";
        //          step = -1.;
        //          hitcandidate = daughter;
        //          break;
        //        }
        //  #endif
      }
    }
    vecCore::Store(step, out_steps + from_index);
  }

  template <typename T, unsigned int ChunkSize>
  VECCORE_ATT_HOST_DEVICE
  static void DaughterIntersectionsLooper(VNavigator const *nav, LogicalVolume const *lvol,
                                          Vector3D<T> const &localpoint, Vector3D<T> const &localdir,
                                          NavigationState const *const *in_states, unsigned int from_index,
                                          Precision *out_steps, VPlacedVolume const *hitcandidates[ChunkSize])
  {
    NewSimpleNavigator<MotherIsConvex>::template DaughterIntersectionsLooper<T, ChunkSize>(
        nav, lvol, localpoint, localdir, in_states, nullptr, from_index, out_steps, hitcandidates);
  }

#ifndef VECCORE_CUDA
  static VNavigator *Instance()
  {
    static NewSimpleNavigator instance;
    return &instance;
  }
#else
  VECCORE_ATT_DEVICE
  static VNavigator *Instance();
#endif

  static constexpr const char *gClassNameString = "NewSimpleNavigator";
  typedef SimpleSafetyEstimator SafetyEstimator_t;
}; // end of class
}
} // end namespace

#endif /* NAVIGATION_NEWSIMPLENAVIGATOR_H_ */