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
|
/*
* BooleanImplementation.h
*/
#ifndef BOOLEANINTERSECTIONIMPLEMENTATION_H_
#define BOOLEANINTERSECTIONIMPLEMENTATION_H_
#include "VecGeom/base/Global.h"
#include "VecGeom/base/Vector3D.h"
#include "VecGeom/volumes/BooleanStruct.h"
#include <VecCore/VecCore>
namespace vecgeom {
inline namespace VECGEOM_IMPL_NAMESPACE {
/**
* partial template specialization for UNION implementation
*/
template <>
struct BooleanImplementation<kIntersection> {
using PlacedShape_t = PlacedBooleanVolume<kIntersection>;
using UnplacedVolume_t = UnplacedBooleanVolume<kIntersection>;
using UnplacedStruct_t = BooleanStruct;
VECCORE_ATT_HOST_DEVICE
static void PrintType()
{ /* printf("SpecializedBooleanVolume<%i, %i, %i>", kIntersection, transCodeT, rotCodeT); */
}
template <typename Stream>
static void PrintType(Stream &s)
{
// s << "SpecializedBooleanVolume<kIntersection"
// << "," << transCodeT << "," << rotCodeT << ">";
}
template <typename Stream>
static void PrintType(Stream &st, int transCodeT = translation::kGeneric, int rotCodeT = rotation::kGeneric)
{
st << "SpecializedBooleanVolume<kIntersection" << transCodeT << "," << rotCodeT << ">";
}
template <typename Stream>
static void PrintImplementationType(Stream &s)
{
// s << "BooleanImplementation<kIntersection"
// << "," << transCodeT << "," << rotCodeT << ">";
}
template <typename Stream>
static void PrintUnplacedType(Stream &s)
{
// s << "UnplacedBooleanVolume";
}
template <typename Real_v, typename Bool_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void Contains(BooleanStruct const &unplaced, Vector3D<Real_v> const &point, Bool_v &inside)
{
const auto insideA = unplaced.fLeftVolume->Contains(point);
const auto insideB = unplaced.fRightVolume->Contains(point);
inside = insideA && insideB;
}
template <typename Real_v, typename Inside_t>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void Inside(BooleanStruct const &unplaced, Vector3D<Real_v> const &point, Inside_t &inside)
{
// now use the Inside functionality of left and right components
// algorithm taken from Geant4 implementation
VPlacedVolume const *const fPtrSolidA = unplaced.fLeftVolume;
VPlacedVolume const *const fPtrSolidB = unplaced.fRightVolume;
const auto positionA = fPtrSolidA->Inside(point);
if (positionA == EInside::kOutside) {
inside = EInside::kOutside;
return;
}
const auto positionB = fPtrSolidB->Inside(point);
if (positionA == EInside::kInside && positionB == EInside::kInside) {
inside = EInside::kInside;
return;
} else {
if ((positionA == EInside::kInside && positionB == EInside::kSurface) ||
(positionB == EInside::kInside && positionA == EInside::kSurface) ||
(positionA == EInside::kSurface && positionB == EInside::kSurface)) {
inside = EInside::kSurface;
return;
} else {
inside = EInside::kOutside;
return;
}
}
}
template <typename Real_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void DistanceToIn(BooleanStruct const &unplaced, Vector3D<Real_v> const &point, Vector3D<Real_v> const &dir,
Real_v const &stepMax, Real_v &distance)
{
Vector3D<Real_v> hitpoint = point;
auto inleft = unplaced.fLeftVolume->Contains(hitpoint);
auto inright = unplaced.fRightVolume->Contains(hitpoint);
Real_v d1 = 0.;
Real_v d2 = 0.;
Real_v snext = 0.0;
// just a pre-check before entering main algorithm
if (inleft && inright) {
d1 = unplaced.fLeftVolume->PlacedDistanceToOut(hitpoint, dir, stepMax);
d2 = unplaced.fRightVolume->PlacedDistanceToOut(hitpoint, dir, stepMax);
// if we are close to a boundary continue
if (d1 < 2 * kTolerance) inleft = false; // Backend::kFalse;
if (d2 < 2 * kTolerance) inright = false; // Backend::kFalse;
// otherwise exit
if (inleft && inright) {
// TODO: WE are inside both so should return a negative number
distance = 0.0;
return;
}
}
// main loop
while (1) {
d1 = d2 = 0;
if (!inleft) {
d1 = unplaced.fLeftVolume->DistanceToIn(hitpoint, dir);
d1 = Max(d1, kTolerance);
if (d1 > 1E20) {
distance = kInfLength;
return;
}
}
if (!inright) {
d2 = unplaced.fRightVolume->DistanceToIn(hitpoint, dir);
d2 = Max(d2, kTolerance);
if (d2 > 1E20) {
distance = kInfLength;
return;
}
}
if (d1 > d2) {
// propagate to left shape
snext += d1;
inleft = true; // Backend::kTrue;
hitpoint += d1 * dir;
// check if propagated point is inside right shape
// check is done with a little push
inright = unplaced.fRightVolume->Contains(hitpoint + kTolerance * dir);
if (inright) {
distance = snext;
return;
}
// here inleft=true, inright=false
} else {
// propagate to right shape
snext += d2;
inright = true; // Backend::kTrue;
hitpoint += d2 * dir;
// check if propagated point is inside left shape
inleft = unplaced.fLeftVolume->Contains(hitpoint + kTolerance * dir);
if (inleft) {
distance = snext;
return;
}
}
// here inleft=false, inright=true
} // end while loop
distance = snext;
return;
}
template <typename Real_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void DistanceToOut(BooleanStruct const &unplaced, Vector3D<Real_v> const &point,
Vector3D<Real_v> const &direction, Real_v const &stepMax, Real_v &distance)
{
distance = Min(unplaced.fLeftVolume->DistanceToOut(point, direction),
unplaced.fRightVolume->PlacedDistanceToOut(point, direction));
}
template <typename Real_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void SafetyToIn(BooleanStruct const &unplaced, Vector3D<Real_v> const &point, Real_v &safety)
{
// This is the Geant4 algorithm
// TODO: ROOT seems to produce better safeties
const auto insideA = unplaced.fLeftVolume->Contains(point);
const auto insideB = unplaced.fRightVolume->Contains(point);
if (!insideA && insideB) {
safety = unplaced.fLeftVolume->SafetyToIn(point);
} else {
if (!insideB && insideA) {
safety = unplaced.fRightVolume->SafetyToIn(point);
} else {
safety = Min(unplaced.fLeftVolume->SafetyToIn(point), unplaced.fRightVolume->SafetyToIn(point));
}
}
return;
}
template <typename Real_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void SafetyToOut(BooleanStruct const &unplaced, Vector3D<Real_v> const &point, Real_v &safety)
{
safety = Min(
// TODO: could fail if left volume is placed shape
unplaced.fLeftVolume->SafetyToOut(point),
// TODO: consider introducing PlacedSafetyToOut function
unplaced.fRightVolume->SafetyToOut(unplaced.fRightVolume->GetTransformation()->Transform(point)));
vecCore::MaskedAssign(safety, safety < Real_v(0.), Real_v(0.));
}
template <typename Real_v, typename Bool_v>
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
static void NormalKernel(BooleanStruct const &unplaced, Vector3D<Real_v> const &point, Vector3D<Real_v> &normal,
Bool_v &valid)
{
Vector3D<Real_v> localNorm;
Vector3D<Real_v> localPoint;
valid = false; // Backend::kFalse;
VPlacedVolume const *const fPtrSolidA = unplaced.fLeftVolume;
VPlacedVolume const *const fPtrSolidB = unplaced.fRightVolume;
Real_v safetyA, safetyB;
if (fPtrSolidA->Contains(point)) {
fPtrSolidA->GetTransformation()->Transform(point, localPoint);
safetyA = fPtrSolidA->SafetyToOut(localPoint);
} else {
safetyA = fPtrSolidA->SafetyToIn(point);
}
if (fPtrSolidB->Contains(point)) {
fPtrSolidB->GetTransformation()->Transform(point, localPoint);
safetyB = fPtrSolidB->SafetyToOut(localPoint);
} else {
safetyB = fPtrSolidB->SafetyToIn(point);
}
const auto onA = safetyA < safetyB;
if (vecCore::MaskFull(onA)) {
fPtrSolidA->GetTransformation()->Transform(point, localPoint);
valid = fPtrSolidA->Normal(localPoint, localNorm);
fPtrSolidA->GetTransformation()->InverseTransformDirection(localNorm, normal);
return;
} else {
// if (vecCore::MaskEmpty(onA)) { // to use real mask operation when supporting vectors
fPtrSolidB->GetTransformation()->Transform(point, localPoint);
valid = fPtrSolidB->Normal(localPoint, localNorm);
fPtrSolidB->GetTransformation()->InverseTransformDirection(localNorm, normal);
return;
}
// Some particles are on A, some on B. We never arrive here in the scalar case
// If the interface to Normal will support the vector case, we have to write code here.
return;
}
}; // End struct BooleanImplementation
} // namespace VECGEOM_IMPL_NAMESPACE
} // namespace vecgeom
#endif /* BooleanImplementation_H_ */
|