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
|
//===-- kernel/MultiUnionImplementation.h ---------------------------*- C++ -*-===//
//===--------------------------------------------------------------------------===//
/// @file MultiUnionImplementation.h
/// @author Mihaela Gheata (mihaela.gheata@cern.ch)
#ifndef VECGEOM_VOLUMES_KERNEL_MULTIUNIONIMPLEMENTATION_H_
#define VECGEOM_VOLUMES_KERNEL_MULTIUNIONIMPLEMENTATION_H_
#include "VecGeom/base/Vector3D.h"
#include "VecGeom/volumes/MultiUnionStruct.h"
#include "VecGeom/volumes/kernel/GenericKernels.h"
#include <VecCore/VecCore>
#include <cstdio>
namespace vecgeom {
VECGEOM_DEVICE_FORWARD_DECLARE(struct MultiUnionImplementation;);
VECGEOM_DEVICE_DECLARE_CONV(struct, MultiUnionImplementation);
inline namespace VECGEOM_IMPL_NAMESPACE {
class PlacedMultiUnion;
struct MultiUnionStruct;
class UnplacedMultiUnion;
struct MultiUnionImplementation {
using PlacedShape_t = PlacedMultiUnion;
using UnplacedStruct_t = MultiUnionStruct;
using UnplacedVolume_t = UnplacedMultiUnion;
VECCORE_ATT_HOST_DEVICE
static void PrintType() {}
template <typename Stream>
static void PrintType(Stream &st, int transCodeT = translation::kGeneric, int rotCodeT = rotation::kGeneric)
{
st << "SpecializedMultiUnion<" << transCodeT << "," << rotCodeT << ">";
}
template <typename Stream>
static void PrintImplementationType(Stream &st)
{
(void)st;
}
template <typename Stream>
static void PrintUnplacedType(Stream &st)
{
(void)st;
}
template <typename Real_v, typename Bool_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void Contains(UnplacedStruct_t const &munion, Vector3D<Real_v> const &point, Bool_v &inside)
{
auto containshook = [&](size_t id) {
inside = munion.fVolumes[id]->Contains(point);
return inside;
};
HybridNavigator<> *boxNav = (HybridNavigator<> *)HybridNavigator<>::Instance();
boxNav->BVHContainsLooper(*munion.fNavHelper, point, containshook);
}
template <typename Real_v, typename Inside_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void Inside(UnplacedStruct_t const &munion, Vector3D<Real_v> const &point, Inside_v &inside)
{
inside = EInside::kOutside;
auto insidehook = [&](size_t id) {
auto inside_crt = munion.fVolumes[id]->Inside(point);
if (inside_crt == EInside::kInside) {
inside = EInside::kInside;
return true;
}
if (inside_crt == EInside::kSurface) inside = EInside::kSurface;
return false;
};
HybridNavigator<> *boxNav = (HybridNavigator<> *)HybridNavigator<>::Instance();
boxNav->BVHContainsLooper(*munion.fNavHelper, point, insidehook);
}
template <typename Real_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void InsideComponent(UnplacedStruct_t const &munion, Vector3D<Real_v> const &point, int &component)
{
component = -1;
auto insidehook = [&](size_t id) {
auto inside_crt = munion.fVolumes[id]->Inside(point);
if (inside_crt != EInside::kOutside) {
component = id;
return true;
}
return false;
};
HybridNavigator<> *boxNav = (HybridNavigator<> *)HybridNavigator<>::Instance();
boxNav->BVHContainsLooper(*munion.fNavHelper, point, insidehook);
}
template <typename Real_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void InsideCluster(UnplacedStruct_t const &munion, Vector3D<Real_v> const &point, int &component)
{
// loop cluster overlap candidates for current component, then update component
size_t *cluster = munion.fNeighbours[component];
size_t ncluster = munion.fNneighbours[component];
for (size_t i = 0; i < ncluster; ++i) {
if (munion.fVolumes[cluster[i]]->Inside(point) == EInside::kInside) {
component = cluster[i];
return;
}
}
component = -1;
}
template <typename Real_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void DistanceToIn(UnplacedStruct_t const &munion, Vector3D<Real_v> const &point,
Vector3D<Real_v> const &direction, Real_v const &stepMax, Real_v &distance)
{
// Check if the bounding box is hit
const Vector3D<Real_v> invdir(Real_v(1.0) / NonZero(direction.x()), Real_v(1.0) / NonZero(direction.y()),
Real_v(1.0) / NonZero(direction.z()));
Vector3D<int> sign;
sign[0] = invdir.x() < 0;
sign[1] = invdir.y() < 0;
sign[2] = invdir.z() < 0;
distance = BoxImplementation::IntersectCachedKernel2<Real_v, Real_v>(
&munion.fMinExtent, point, invdir, sign.x(), sign.y(), sign.z(), -kTolerance, InfinityLength<Real_v>());
if (distance >= stepMax) return;
distance = kInfLength;
// Lambda function to be called for each candidate selected by the bounding box navigator
auto userhook = [&](HybridManager2::BoxIdDistancePair_t hitbox) {
// Stop searching if the distance to the current box is bigger than the
// requested limit or than the current distance
if (hitbox.second > vecCore::math::Min(stepMax, distance)) return true;
// Compute distance to the cluster (in both ToIn or ToOut assumptions)
auto distance_crt = munion.fVolumes[hitbox.first]->DistanceToIn(point, direction, stepMax);
if (distance_crt < distance) distance = distance_crt;
return false;
};
HybridNavigator<> *boxNav = (HybridNavigator<> *)HybridNavigator<>::Instance();
// intersect ray with the BVH structure and use hook
boxNav->BVHSortedIntersectionsLooper(*munion.fNavHelper, point, direction, stepMax, userhook);
}
template <typename Real_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void DistanceToOut(UnplacedStruct_t const &munion, Vector3D<Real_v> const &point,
Vector3D<Real_v> const &direction, Real_v const &stepMax, Real_v &distance)
{
constexpr Real_v eps = 10 * kTolerance;
distance = -1.;
// Locate the component containing the point
int comp;
InsideComponent(munion, point, comp);
if (comp < 0) return; // Point not inside
// Compute distance to exit current component
distance = -eps;
Real_v dstep = 1.;
Vector3D<Real_v> pnew = point;
Vector3D<Real_v> local, ldir;
while (dstep > kTolerance && comp >= 0) {
size_t component = (size_t)comp;
munion.fVolumes[component]->GetTransformation()->Transform(pnew, local);
munion.fVolumes[component]->GetTransformation()->TransformDirection(direction, ldir);
dstep = munion.fVolumes[component]->DistanceToOut(local, ldir, stepMax);
assert(dstep < kInfLength);
distance += dstep + eps;
// If no neighbours, exit
if (!munion.fNneighbours[component]) return;
// Propagate to exit of current component
pnew += (dstep + eps) * direction;
// Try to relocate inside the cluster of neighbours
MultiUnionImplementation::InsideCluster(munion, pnew, comp);
}
}
template <typename Real_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void SafetyToInComp(UnplacedStruct_t const &munion, Vector3D<Real_v> const &point, Real_v &safety,
int &component)
{
safety = vecgeom::InfinityLength<Real_v>();
component = -1;
auto userhook = [&](HybridManager2::BoxIdDistancePair_t hitbox) {
// Stop searching if the safety to the current cluster is bigger than the
// current safety
if (hitbox.second > safety * safety) return true;
// Compute distance to the cluster
Real_v safetycrt = munion.fVolumes[hitbox.first]->SafetyToIn(point);
if (safetycrt > 0 && safetycrt < safety) {
safety = safetycrt;
component = hitbox.first;
}
return false;
};
HybridSafetyEstimator *safEstimator = (HybridSafetyEstimator *)HybridSafetyEstimator::Instance();
// Use the BVH structure and connect hook
safEstimator->BVHSortedSafetyLooper(*munion.fNavHelper, point, userhook, safety);
}
template <typename Real_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void SafetyToIn(UnplacedStruct_t const &munion, Vector3D<Real_v> const &point, Real_v &safety)
{
int comp;
InsideComponent(munion, point, comp);
if (comp > -1) {
safety = -1.;
return;
}
SafetyToInComp<Real_v>(munion, point, safety, comp);
}
template <typename Real_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void SafetyToOut(UnplacedStruct_t const &munion, Vector3D<Real_v> const &point, Real_v &safety)
{
// Locate the component containing the point
int comp;
MultiUnionImplementation::InsideComponent(munion, point, comp);
if (comp < 0) {
safety = -1.; // Point not inside
return;
}
// Compute safety to exit current component
Vector3D<Real_v> const local = munion.fVolumes[comp]->GetTransformation()->Transform(point);
safety = munion.fVolumes[comp]->SafetyToOut(local);
assert(safety > -kTolerance);
// Loop cluster of neighbours
size_t *cluster = munion.fNeighbours[comp];
size_t ncluster = munion.fNneighbours[comp];
for (size_t i = 0; i < ncluster; ++i) {
Vector3D<Real_v> const local = munion.fVolumes[cluster[i]]->GetTransformation()->Transform(point);
Real_v safetycrt = munion.fVolumes[cluster[i]]->SafetyToOut(local);
if (safetycrt > 0 && safetycrt < safety) safety = safetycrt;
}
}
template <typename Real_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static Vector3D<Real_v> NormalKernel(UnplacedStruct_t const &munion, Vector3D<Real_v> const &point,
typename vecCore::Mask_v<Real_v> &valid)
{
// Locate the component containing the point
int comp;
valid = false;
Vector3D<Real_v> direction;
InsideComponent(munion, point, comp);
// If component not found, locate closest one
Real_v safety;
if (comp < 0) SafetyToInComp(munion, point, safety, comp);
if (comp < 0) return direction;
Vector3D<Real_v> local = munion.fVolumes[comp]->GetTransformation()->Transform(point);
Vector3D<Real_v> ldir;
valid = munion.fVolumes[comp]->Normal(local, ldir);
if (valid) direction = munion.fVolumes[comp]->GetTransformation()->InverseTransformDirection(ldir);
return direction;
}
}; // end MultiUnionImplementation
} // namespace VECGEOM_IMPL_NAMESPACE
} // namespace vecgeom
#endif // VECGEOM_VOLUMES_KERNEL_MULTIUNIONIMPLEMENTATION_H_
|