Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:14:04

0001 /*
0002  * TBooleanMinusImplementation.h
0003  *
0004  *  Created on: Aug 13, 2014
0005  *      Author: swenzel
0006  */
0007 
0008 #ifndef TBOOLEANMINUSIMPLEMENTATION_H_
0009 #define TBOOLEANMINUSIMPLEMENTATION_H_
0010 
0011 #include "VecGeom/base/Vector3D.h"
0012 #include "VecGeom/volumes/TUnplacedBooleanMinusVolume.h"
0013 
0014 namespace VECGEOM_NAMESPACE {
0015 
0016 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0017 struct TBooleanMinusImplementation {
0018 
0019   static const int transC = transCodeT;
0020   static const int rotC   = rotCodeT;
0021 
0022   //
0023   template <typename Backend>
0024   VECGEOM_FORCE_INLINE
0025   VECCORE_ATT_HOST_DEVICE
0026   static void UnplacedContains(TUnplacedBooleanMinusVolume const &unplaced,
0027                                Vector3D<typename Backend::precision_v> const &localPoint,
0028                                typename Backend::bool_v &inside);
0029 
0030   template <typename Backend>
0031   VECGEOM_FORCE_INLINE
0032   VECCORE_ATT_HOST_DEVICE
0033   static void Contains(TUnplacedBooleanMinusVolume const &unplaced, Transformation3D const &transformation,
0034                        Vector3D<typename Backend::precision_v> const &point,
0035                        Vector3D<typename Backend::precision_v> &localPoint, typename Backend::bool_v &inside);
0036 
0037   template <typename Backend>
0038   VECGEOM_FORCE_INLINE
0039   VECCORE_ATT_HOST_DEVICE
0040   static void Inside(TUnplacedBooleanMinusVolume const &unplaced, Transformation3D const &transformation,
0041                      Vector3D<typename Backend::precision_v> const &point, typename Backend::inside_v &inside);
0042 
0043   template <typename Backend>
0044   VECGEOM_FORCE_INLINE
0045   VECCORE_ATT_HOST_DEVICE
0046   static void DistanceToIn(TUnplacedBooleanMinusVolume const &unplaced, Transformation3D const &transformation,
0047                            Vector3D<typename Backend::precision_v> const &point,
0048                            Vector3D<typename Backend::precision_v> const &direction,
0049                            typename Backend::precision_v const &stepMax, typename Backend::precision_v &distance);
0050 
0051   template <typename Backend>
0052   VECGEOM_FORCE_INLINE
0053   VECCORE_ATT_HOST_DEVICE
0054   static void DistanceToOut(TUnplacedBooleanMinusVolume const &unplaced,
0055                             Vector3D<typename Backend::precision_v> const &point,
0056                             Vector3D<typename Backend::precision_v> const &direction,
0057                             typename Backend::precision_v const &stepMax, typename Backend::precision_v &distance);
0058 
0059   template <typename Backend>
0060   VECGEOM_FORCE_INLINE
0061   VECCORE_ATT_HOST_DEVICE
0062   static void SafetyToIn(TUnplacedBooleanMinusVolume const &unplaced, Transformation3D const &transformation,
0063                          Vector3D<typename Backend::precision_v> const &point, typename Backend::precision_v &safety);
0064 
0065   template <typename Backend>
0066   VECGEOM_FORCE_INLINE
0067   VECCORE_ATT_HOST_DEVICE
0068   static void SafetyToOut(TUnplacedBooleanMinusVolume const &unplaced,
0069                           Vector3D<typename Backend::precision_v> const &point, typename Backend::precision_v &safety);
0070 
0071   template <typename Backend>
0072   VECGEOM_FORCE_INLINE
0073   VECCORE_ATT_HOST_DEVICE
0074   static void ContainsKernel(TUnplacedBooleanMinusVolume const &unplaced,
0075                              Vector3D<typename Backend::precision_v> const &point, typename Backend::bool_v &inside);
0076 
0077   template <typename Backend>
0078   VECGEOM_FORCE_INLINE
0079   VECCORE_ATT_HOST_DEVICE
0080   static void InsideKernel(TUnplacedBooleanMinusVolume const &unplaced,
0081                            Vector3D<typename Backend::precision_v> const &point, typename Backend::inside_v &inside);
0082 
0083   template <typename Backend>
0084   VECGEOM_FORCE_INLINE
0085   VECCORE_ATT_HOST_DEVICE
0086   static void DistanceToInKernel(TUnplacedBooleanMinusVolume const &unplaced,
0087                                  Vector3D<typename Backend::precision_v> const &point,
0088                                  Vector3D<typename Backend::precision_v> const &direction,
0089                                  typename Backend::precision_v const &stepMax, typename Backend::precision_v &distance);
0090 
0091   template <typename Backend>
0092   VECGEOM_FORCE_INLINE
0093   VECCORE_ATT_HOST_DEVICE
0094   static void DistanceToOutKernel(TUnplacedBooleanMinusVolume const &unplaced,
0095                                   Vector3D<typename Backend::precision_v> const &point,
0096                                   Vector3D<typename Backend::precision_v> const &direction,
0097                                   typename Backend::precision_v const &stepMax,
0098                                   typename Backend::precision_v &distance);
0099 
0100   template <typename Backend>
0101   VECGEOM_FORCE_INLINE
0102   VECCORE_ATT_HOST_DEVICE
0103   static void SafetyToInKernel(TUnplacedBooleanMinusVolume const &unplaced,
0104                                Vector3D<typename Backend::precision_v> const &point,
0105                                typename Backend::precision_v &safety);
0106 
0107   template <typename Backend>
0108   VECGEOM_FORCE_INLINE
0109   VECCORE_ATT_HOST_DEVICE
0110   static void SafetyToOutKernel(TUnplacedBooleanMinusVolume const &unplaced,
0111                                 Vector3D<typename Backend::precision_v> const &point,
0112                                 typename Backend::precision_v &safety);
0113 
0114   template <typename Backend>
0115   VECGEOM_FORCE_INLINE
0116   VECCORE_ATT_HOST_DEVICE
0117   static void NormalKernel(TUnplacedBooleanMinusVolume const &unplaced,
0118                            Vector3D<typename Backend::precision_v> const &point,
0119                            Vector3D<typename Backend::precision_v> &normal, typename Backend::bool_v &valid);
0120 
0121 }; // End struct TBooleanMinusImplementation
0122 
0123 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0124 template <typename Backend>
0125 VECCORE_ATT_HOST_DEVICE
0126 void TBooleanMinusImplementation<LeftPlacedType_t, RightPlacedType_t, transCodeT, rotCodeT>::UnplacedContains(
0127     TUnplacedBooleanMinusVolume const &unplaced, Vector3D<typename Backend::precision_v> const &localPoint,
0128     typename Backend::bool_v &inside)
0129 {
0130 
0131   ContainsKernel<Backend>(unplaced, localPoint, inside);
0132 }
0133 
0134 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0135 template <typename Backend>
0136 VECCORE_ATT_HOST_DEVICE
0137 void TBooleanMinusImplementation<LeftPlacedType_t, RightPlacedType_t, transCodeT, rotCodeT>::Contains(
0138     TUnplacedBooleanMinusVolume const &unplaced, Transformation3D const &transformation,
0139     Vector3D<typename Backend::precision_v> const &point, Vector3D<typename Backend::precision_v> &localPoint,
0140     typename Backend::bool_v &inside)
0141 {
0142 
0143   localPoint = transformation.Transform<transCodeT, rotCodeT>(point);
0144   UnplacedContains<Backend>(unplaced, localPoint, inside);
0145 }
0146 
0147 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0148 template <typename Backend>
0149 VECCORE_ATT_HOST_DEVICE
0150 void TBooleanMinusImplementation<LeftPlacedType_t, RightPlacedType_t, transCodeT, rotCodeT>::Inside(
0151     TUnplacedBooleanMinusVolume const &unplaced, Transformation3D const &transformation,
0152     Vector3D<typename Backend::precision_v> const &point, typename Backend::inside_v &inside)
0153 {
0154 
0155   InsideKernel<Backend>(unplaced, transformation.Transform<transCodeT, rotCodeT>(point), inside);
0156 }
0157 
0158 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0159 template <typename Backend>
0160 VECCORE_ATT_HOST_DEVICE
0161 void TBooleanMinusImplementation<LeftPlacedType_t, RightPlacedType_t, transCodeT, rotCodeT>::DistanceToIn(
0162     TUnplacedBooleanMinusVolume const &unplaced, Transformation3D const &transformation,
0163     Vector3D<typename Backend::precision_v> const &point, Vector3D<typename Backend::precision_v> const &direction,
0164     typename Backend::precision_v const &stepMax, typename Backend::precision_v &distance)
0165 {
0166 
0167   DistanceToInKernel<Backend>(unplaced, transformation.Transform<transCodeT, rotCodeT>(point),
0168                               transformation.TransformDirection<rotCodeT>(direction), stepMax, distance);
0169 }
0170 
0171 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0172 template <typename Backend>
0173 VECCORE_ATT_HOST_DEVICE
0174 void TBooleanMinusImplementation<LeftPlacedType_t, RightPlacedType_t, transCodeT, rotCodeT>::DistanceToOut(
0175     TUnplacedBooleanMinusVolume const &unplaced, Vector3D<typename Backend::precision_v> const &point,
0176     Vector3D<typename Backend::precision_v> const &direction, typename Backend::precision_v const &stepMax,
0177     typename Backend::precision_v &distance)
0178 {
0179 
0180   DistanceToOutKernel<Backend>(unplaced, point, direction, stepMax, distance);
0181 }
0182 
0183 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0184 template <typename Backend>
0185 VECGEOM_FORCE_INLINE
0186 VECCORE_ATT_HOST_DEVICE
0187 void TBooleanMinusImplementation<LeftPlacedType_t, RightPlacedType_t, transCodeT, rotCodeT>::SafetyToIn(
0188     TUnplacedBooleanMinusVolume const &unplaced, Transformation3D const &transformation,
0189     Vector3D<typename Backend::precision_v> const &point, typename Backend::precision_v &safety)
0190 {
0191 
0192   SafetyToInKernel<Backend>(unplaced, transformation.Transform<transCodeT, rotCodeT>(point), safety);
0193 }
0194 
0195 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0196 template <class Backend>
0197 VECGEOM_FORCE_INLINE
0198 VECCORE_ATT_HOST_DEVICE
0199 void TBooleanMinusImplementation<LeftPlacedType_t, RightPlacedType_t, transCodeT, rotCodeT>::SafetyToOut(
0200     TUnplacedBooleanMinusVolume const &unplaced, Vector3D<typename Backend::precision_v> const &point,
0201     typename Backend::precision_v &safety)
0202 {
0203 
0204   SafetyToOutKernel<Backend>(unplaced, point, safety);
0205 }
0206 
0207 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0208 template <typename Backend>
0209 VECCORE_ATT_HOST_DEVICE
0210 void TBooleanMinusImplementation<LeftPlacedType_t, RightPlacedType_t, transCodeT, rotCodeT>::ContainsKernel(
0211     TUnplacedBooleanMinusVolume const &unplaced, Vector3D<typename Backend::precision_v> const &localPoint,
0212     typename Backend::bool_v &inside)
0213 {
0214 
0215   // now just use the Contains functionality
0216   // of Unplaced and its left and right components
0217   // Find if a subtraction of two shapes contains a given point
0218 
0219   // have to figure this out
0220   Vector3D<typename Backend::precision_v> tmp;
0221   LeftPlacedType_t::Implementation::template Contains<Backend>(
0222       *((LeftPlacedType_t *)unplaced.fLeftVolume)->GetUnplacedVolume(), *unplaced.fLeftVolume->transformation(),
0223       localPoint, tmp, inside);
0224 
0225   //   TUnplacedBooleanMinusVolume const &unplaced,
0226   //        Transformation3D const &transformation,
0227   //        Vector3D<typename Backend::precision_v> const &point,
0228   //        Vector3D<typename Backend::precision_v> &localPoint,
0229   //        typename Backend::bool_v &inside
0230   //
0231 
0232   if (vecCore::MaskEmpty(inside)) return;
0233 
0234   typename Backend::bool_v rightInside;
0235   RightPlacedType_t::Implementation::template Contains<Backend>(
0236       *((RightPlacedType_t *)unplaced.fRightVolume)->GetUnplacedVolume(), *unplaced.fRightVolume->transformation(),
0237       localPoint, tmp, rightInside);
0238 
0239   inside &= !rightInside;
0240 }
0241 
0242 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0243 template <typename Backend>
0244 VECCORE_ATT_HOST_DEVICE
0245 void TBooleanMinusImplementation<LeftPlacedType_t, RightPlacedType_t, transCodeT, rotCodeT>::InsideKernel(
0246     TUnplacedBooleanMinusVolume const &unplaced, Vector3D<typename Backend::precision_v> const &point,
0247     typename Backend::inside_v &inside)
0248 {
0249 
0250   // now use the Inside functionality of left and right components
0251 
0252   // going to be a bit more complicated due to Surface states
0253 }
0254 
0255 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0256 template <typename Backend>
0257 VECCORE_ATT_HOST_DEVICE
0258 void TBooleanMinusImplementation<LeftPlacedType_t, RightPlacedType_t, transCodeT, rotCodeT>::DistanceToInKernel(
0259     TUnplacedBooleanMinusVolume const &unplaced, Vector3D<typename Backend::precision_v> const &point,
0260     Vector3D<typename Backend::precision_v> const &direction, typename Backend::precision_v const &stepMax,
0261     typename Backend::precision_v &distance)
0262 {
0263 
0264   typedef typename Backend::precision_v Float_t;
0265   typedef typename Backend::bool_v Bool_t;
0266 
0267   // TOBEDONE: ASK Andrei about the while loop
0268   // Compute distance from a given point outside to the shape.
0269   //  Int_t i;
0270   //  Float_t d1, d2, snxt=0.;
0271   //  fRightMat->MasterToLocal(point, &local[0]);
0272   //  fLeftMat->MasterToLocalVect(dir, &ldir[0]);
0273   //  fRightMat->MasterToLocalVect(dir, &rdir[0]);
0274   //
0275   //  // check if inside '-'
0276   //  Bool_t insideRight = unplaced.fRightVolume->Contains(point);
0277   //  // epsilon is used to push across boundaries
0278   //  Precision epsil(0.);
0279   //
0280   //  // we should never subtract a volume such that B - A > 0
0281   //
0282   //  // what does this while loop do?
0283   //  if ( ! vecCore::MaskEmpty( insideRight ) ) {
0284   //    // propagate to outside of '- / RightShape'
0285   //    d1 = unplaced.fRightVolume->DistanceToOut( point, direction, stepMax);
0286   //    snxt += d1+epsil;
0287   //    hitpoint += (d1+1E-8)*direction;
0288   //
0289   //    epsil = 1.E-8;
0290   //    // now master outside 'B'; check if inside 'A'
0291   //    Bool_t insideLeft =
0292   //    if (unplaced.fLeftVolume->Contains(&local[0])) return snxt;
0293   //  }
0294   //
0295   //  // if outside of both we do a max operation
0296   //  // master outside '-' and outside '+' ;  find distances to both
0297   //        node->SetSelected(1);
0298   //        fLeftMat->MasterToLocal(&master[0], &local[0]);
0299   //        d2 = fLeft->DistFromOutside(&local[0], &ldir[0], iact, step, safe);
0300   //        if (d2>1E20) return TGeoShape::Big();
0301   //
0302   //        fRightMat->MasterToLocal(&master[0], &local[0]);
0303   //        d1 = fRight->DistFromOutside(&local[0], &rdir[0], iact, step, safe);
0304   //        if (d2<d1-TGeoShape::Tolerance()) {
0305   //           snxt += d2+epsil;
0306   //           return snxt;
0307   //        }
0308   //        // propagate to '-'
0309   //        snxt += d1+epsil;
0310   //        for (i=0; i<3; i++) master[i] += (d1+1E-8)*dir[i];
0311   //        epsil = 1.E-8;
0312   //        // now inside '-' and not inside '+'
0313   //        fRightMat->MasterToLocal(&master[0], &local[0]);
0314   //        inside = kTRUE;
0315   //     }
0316 }
0317 #include <iostream>
0318 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0319 template <typename Backend>
0320 VECCORE_ATT_HOST_DEVICE
0321 void TBooleanMinusImplementation<LeftPlacedType_t, RightPlacedType_t, transCodeT, rotCodeT>::DistanceToOutKernel(
0322     TUnplacedBooleanMinusVolume const &unplaced, Vector3D<typename Backend::precision_v> const &point,
0323     Vector3D<typename Backend::precision_v> const &direction, typename Backend::precision_v const &stepMax,
0324     typename Backend::precision_v &distance)
0325 {
0326 
0327   typedef typename Backend::precision_v Float_t;
0328   typedef typename Backend::bool_v Bool_t;
0329 
0330   // how can we force to inline this ?
0331   // Left is an unplaced shape; but it could be a complicated one
0332 
0333   // what happens if LeftType is VPlacedVolume itself?
0334   // For the SOA case: is it better to push SOA down or do ONE loop around?
0335   // distance = unplaced.fLeftVolume->Unplaced_t::LeftType::DistanceToOut( point, direction, stepMax );
0336   // Float_t dinright = unplaced.fRightVolume->Unplaced_t::RightType::DistanceToIn( point, direction, stepMax );
0337 
0338   // we need a template specialization for this in case we have LeftType or RightType equals to
0339   // VPlacedVolume
0340 
0341   LeftPlacedType_t::Implementation::template DistanceToOut<Backend>(
0342       *((LeftPlacedType_t *)unplaced.fLeftVolume)->GetUnplacedVolume(), point, direction, stepMax, distance);
0343   Float_t dinright(kInfLength);
0344   RightPlacedType_t::Implementation::template DistanceToIn<Backend>(
0345       *((RightPlacedType_t *)unplaced.fRightVolume)->GetUnplacedVolume(), *unplaced.fRightVolume->transformation(),
0346       point, direction, stepMax, dinright);
0347   distance = Min(distance, dinright);
0348   return;
0349 }
0350 
0351 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0352 template <typename Backend>
0353 VECCORE_ATT_HOST_DEVICE
0354 void TBooleanMinusImplementation<LeftPlacedType_t, RightPlacedType_t, transCodeT, rotCodeT>::SafetyToInKernel(
0355     TUnplacedBooleanMinusVolume const &unplaced, Vector3D<typename Backend::precision_v> const &point,
0356     typename Backend::precision_v &safety)
0357 {
0358 
0359   typedef typename Backend::precision_v Float_t;
0360   typedef typename Backend::bool_v Bool_t;
0361 
0362   // this is a very crude algorithm which as very many "virtual" surfaces
0363   // which could return a very small safety
0364   //  Bool_t  inleft   = unplaced.fLeftVolume->Contains(point);
0365   //  Float_t safleft  = unplaced.fLeftVolume->SafetyToIn(point);
0366   //  Bool_t  inright  = unplaced.fRightVolume->Contains(point);
0367   //  Float_t safright = unplaced.fRightVolume->SafetyToOut(point);
0368   //  Bool_t done(Backend::kFalse);
0369   //  vecCore::MaskedAssign(safety ,  inleft && inright, safright);
0370   //  done |= inleft && inright;
0371   //  vecCore:::MaskedAssign(safright,  !done && inleft, Min(safleft), &safety);
0372   //  done |= inleft;
0373   //  vecCore:::MaskedAssign(safright,  !done && inright, Max(safleft), &safety);
0374 }
0375 
0376 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0377 template <typename Backend>
0378 VECCORE_ATT_HOST_DEVICE
0379 void TBooleanMinusImplementation<LeftPlacedType_t, RightPlacedType_t, transCodeT, rotCodeT>::SafetyToOutKernel(
0380     TUnplacedBooleanMinusVolume const &unplaced, Vector3D<typename Backend::precision_v> const &point,
0381     typename Backend::precision_v &safety)
0382 {
0383 
0384   typedef typename Backend::bool_v Bool_t;
0385   typedef typename Backend::precision_v Float_t;
0386 
0387   LeftPlacedType_t::Implementation::template SafetyToOut<Backend>(
0388       *((LeftPlacedType_t *)unplaced.fLeftVolume)->GetUnplacedVolume(), point, safety);
0389   Float_t safetyright(kInfLength);
0390   RightPlacedType_t::Implementation::template SafetyToIn<Backend>(
0391       *((RightPlacedType_t *)unplaced.fRightVolume)->GetUnplacedVolume(), *unplaced.fRightVolume->transformation(),
0392       point, safetyright);
0393   safety = Min(safety, safetyright);
0394   return;
0395 }
0396 
0397 template <typename LeftPlacedType_t, typename RightPlacedType_t, TranslationCode transCodeT, RotationCode rotCodeT>
0398 template <typename Backend>
0399 VECCORE_ATT_HOST_DEVICE
0400 void TBooleanMinusImplementation<LeftPlacedType_t, RightPlacedType_t, transCodeT, rotCodeT>::NormalKernel(
0401     TUnplacedBooleanMinusVolume const &unplaced, Vector3D<typename Backend::precision_v> const &point,
0402     Vector3D<typename Backend::precision_v> &normal, typename Backend::bool_v &valid)
0403 {
0404   // TBDONE
0405 }
0406 
0407 } // End global namespace
0408 
0409 #endif /* TBOOLEANMINUSIMPLEMENTATION_H_ */