Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-26 10:34:13

0001 //===-- kernel/TrapezoidImplementation.h ----------------------------*- C++ -*-===//
0002 //===--------------------------------------------------------------------------===//
0003 /// @file TrapezoidImplementation.h
0004 /// @brief Navigation kernels for the trapezoid solid.
0005 /// @author Guilherme Lima (lima@fnal.gov)
0006 //===--------------------------------------------------------------------------===//
0007 
0008 /// History notes:
0009 /// 2014-05-20: Created from USolids' UTrap algorithms (G. Lima)
0010 /// 2016-07-22: Revision and migration to the backend structure (G. Lima)
0011 
0012 #ifndef VECGEOM_VOLUMES_KERNEL_TRAPEZOIDIMPLEMENTATION_H_
0013 #define VECGEOM_VOLUMES_KERNEL_TRAPEZOIDIMPLEMENTATION_H_
0014 
0015 #include "VecGeom/base/Vector3D.h"
0016 #include "VecGeom/volumes/SurfaceHitView.h"
0017 #include "VecGeom/volumes/TrapezoidStruct.h"
0018 #include "VecGeom/volumes/kernel/GenericKernels.h"
0019 #include <VecCore/VecCore>
0020 
0021 #include <cstdio>
0022 
0023 namespace vecgeom {
0024 
0025 VECGEOM_DEVICE_FORWARD_DECLARE(struct TrapezoidImplementation;);
0026 VECGEOM_DEVICE_DECLARE_CONV(struct, TrapezoidImplementation);
0027 
0028 inline namespace VECGEOM_IMPL_NAMESPACE {
0029 
0030 class PlacedTrapezoid;
0031 class UnplacedTrapezoid;
0032 
0033 /// @brief Implements navigation kernels for `UnplacedTrapezoid`.
0034 /// @details The runtime data provides the z half-length and four cached
0035 /// outward side planes. Signed side-plane distances are negative inside,
0036 /// positive outside, and tolerance bands are resolved by each navigation entry
0037 /// point according to the queried convention.
0038 struct TrapezoidImplementation {
0039 
0040   using PlacedShape_t    = PlacedTrapezoid;
0041   using UnplacedStruct_t = TrapezoidStruct<Precision>;
0042   using UnplacedVolume_t = UnplacedTrapezoid;
0043 #ifndef VECGEOM_PLANESHELL
0044   using TrapSidePlane = TrapezoidStruct<Precision>::TrapSidePlane;
0045 #endif
0046 
0047   /// @brief Evaluate side-plane signed distances and direction projections.
0048   /// @tparam Real_v Floating-point scalar type.
0049   /// @param unplaced Trapezoid data containing the cached side planes.
0050   /// @param point Local start point.
0051   /// @param dir Unit local direction.
0052   /// @param[out] pdist Signed distance to each side plane; positive is outside.
0053   /// @param[out] proj Projection of @p dir on each outward side-plane normal.
0054   /// @param[out] vdist Plane-crossing distance `-pdist/proj` for each side.
0055   template <typename Real_v>
0056   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void EvaluateTrack(UnplacedStruct_t const &unplaced,
0057                                                                          Vector3D<Real_v> const &point,
0058                                                                          Vector3D<Real_v> const &dir, Real_v *pdist,
0059                                                                          Real_v *proj, Real_v *vdist)
0060   {
0061 #ifdef VECGEOM_PLANESHELL
0062     auto const *fPlanes = unplaced.GetPlanes();
0063     for (unsigned int i = 0; i < 4; ++i) {
0064       pdist[i] = fPlanes->fA[i] * point.x() + fPlanes->fB[i] * point.y() + fPlanes->fC[i] * point.z() + fPlanes->fD[i];
0065       proj[i]  = fPlanes->fA[i] * dir.x() + fPlanes->fB[i] * dir.y() + fPlanes->fC[i] * dir.z();
0066       vdist[i] = -pdist[i] / NonZero(proj[i]);
0067     }
0068 #else
0069     TrapSidePlane const *fPlanes = unplaced.GetPlanes();
0070     // loop over side planes - find pdist,proj for each side plane
0071     // auto-vectorizable part of loop
0072     for (unsigned int i = 0; i < 4; ++i) {
0073       // Note: normal vector is pointing outside the volume (convention), therefore
0074       // pdist>0 if point is outside  and  pdist<0 means inside
0075       pdist[i] = fPlanes[i].fA * point.x() + fPlanes[i].fB * point.y() + fPlanes[i].fC * point.z() + fPlanes[i].fD;
0076 
0077       // proj is projection of dir over the normal vector of side plane, hence
0078       // proj > 0 if pointing ~same direction as normal and proj<0 if ~opposite to normal
0079       proj[i] = fPlanes[i].fA * dir.x() + fPlanes[i].fB * dir.y() + fPlanes[i].fC * dir.z();
0080 
0081       vdist[i] = -pdist[i] / NonZero(proj[i]);
0082     }
0083 #endif
0084   }
0085 
0086   /// @brief Test whether a local point is contained in or on the trapezoid.
0087   /// @tparam Real_v Floating-point scalar type.
0088   /// @param unplaced Trapezoid data.
0089   /// @param point Local point to test.
0090   /// @param[out] inside Set to true unless @p point is outside a tolerated limiting plane.
0091   template <typename Real_v>
0092   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void Contains(UnplacedStruct_t const &unplaced,
0093                                                                     Vector3D<Real_v> const &point, bool &inside)
0094   {
0095     bool unused = false, outside = false;
0096     GenericKernelForContainsAndInside<Real_v, false>(unplaced, point, unused, outside);
0097     inside = !outside;
0098   }
0099 
0100   /// @brief Classify a local point as inside, outside, or surface.
0101   /// @tparam Real_v Floating-point scalar type.
0102   /// @param unplaced Trapezoid data.
0103   /// @param point Local point to classify.
0104   /// @param[out] inside Set to `kInside`, `kOutside`, or `kSurface`.
0105   template <typename Real_v>
0106   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void Inside(UnplacedStruct_t const &unplaced,
0107                                                                   Vector3D<Real_v> const &point, Inside_t &inside)
0108   {
0109     bool completelyInside = false, completelyOutside = false;
0110     GenericKernelForContainsAndInside<Real_v, true>(unplaced, point, completelyInside, completelyOutside);
0111 
0112     inside = Inside_t(EInside::kSurface);
0113     if (completelyOutside) inside = Inside_t(EInside::kOutside);
0114     if (completelyInside) inside = Inside_t(EInside::kInside);
0115   }
0116 
0117   /// @brief Shared classification helper for containment and inside queries.
0118   /// @tparam Real_v Floating-point scalar type.
0119   /// @tparam ForInside When true, also compute the strict-inside flag.
0120   /// @param unplaced Trapezoid data.
0121   /// @param point Local point to classify.
0122   /// @param[out] completelyInside Set when the point is separated from all limiting planes by the inside tolerance.
0123   /// @param[out] completelyOutside Set when the point is outside any limiting plane beyond tolerance.
0124   template <typename Real_v, bool ForInside>
0125   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void GenericKernelForContainsAndInside(
0126       UnplacedStruct_t const &unplaced, Vector3D<Real_v> const &point, bool &completelyInside, bool &completelyOutside)
0127   {
0128     // z-region
0129     completelyOutside = Abs(point[2]) > MakePlusTolerant<true>(unplaced.fDz);
0130     if (ForInside) {
0131       completelyInside = Abs(point[2]) < MakeMinusTolerant<true>(unplaced.fDz);
0132     }
0133     if (ForInside && completelyOutside) return;
0134 
0135     Real_v dist[4];
0136 #ifdef VECGEOM_PLANESHELL
0137     if (!ForInside) {
0138       unplaced.GetPlanes()->GenericKernelForContainsAndInside<Real_v, true>(point, completelyInside, completelyOutside);
0139       return;
0140     }
0141     unplaced.GetPlanes()->DistanceToPoint(point, dist);
0142 #else
0143     // here for PLANESHELL=OFF (disabled)
0144     TrapSidePlane const *fPlanes = unplaced.GetPlanes();
0145     for (unsigned int i = 0; i < 4; ++i) {
0146       dist[i] = fPlanes[i].fA * point.x() + fPlanes[i].fB * point.y() + fPlanes[i].fC * point.z() + fPlanes[i].fD;
0147     }
0148 
0149 #endif
0150     if (ForInside) {
0151       // The largest signed side-plane distance decides both classification bounds.
0152       const Real_v maxSide = Max(Max(dist[0], dist[1]), Max(dist[2], dist[3]));
0153       completelyOutside    = maxSide > Real_v(MakePlusTolerant<true>(0.));
0154       completelyInside     = completelyInside && maxSide < Real_v(MakeMinusTolerant<true>(0.));
0155     } else {
0156       for (unsigned int i = 0; i < 4; ++i)
0157         completelyOutside = completelyOutside || dist[i] > Real_v(MakePlusTolerant<true>(0.));
0158     }
0159   }
0160 
0161   /// @brief Compute the first entry distance from outside the trapezoid.
0162   /// @tparam Real_v Floating-point scalar type.
0163   /// @param unplaced Trapezoid data.
0164   /// @param point Local start point.
0165   /// @param dir Unit local direction.
0166   /// @param stepMax Unused by this implementation.
0167   /// @param[out] distance Entry distance, `kInfLength` on miss, or `-1` for wrong-side candidates.
0168   ///
0169   /// @details For each limiting component (z slab and side planes), the
0170   /// algorithm computes the ray interval for which the track is inside that
0171   /// component. The entry point is the largest lower bound (`smin`), and the
0172   /// exit point is the smallest upper bound (`smax`). If the largest lower
0173   /// bound exceeds the smallest upper bound, the trajectory misses the shape.
0174   template <typename Real_v>
0175   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void DistanceToIn(UnplacedStruct_t const &unplaced,
0176                                                                         Vector3D<Real_v> const &point,
0177                                                                         Vector3D<Real_v> const &dir,
0178                                                                         Real_v const &stepMax, Real_v &distance)
0179   {
0180     (void)stepMax;
0181     distance = kInfLength;
0182 
0183     //
0184     // Step 1: find range of distances along dir between Z-planes (smin, smax)
0185     //
0186 
0187     // step 1.a) input particle is moving away --> return infinity
0188     Real_v signZdir = Sign(dir.z());
0189     Real_v max      = signZdir * unplaced.fDz - point.z(); // z-dist to farthest z-plane
0190 
0191     // done = done || (dir.z()>0.0 && max < MakePlusTolerant<true>(0.));  // check if moving away towards +z
0192     // done = done || (dir.z()<0.0 && max > MakeMinusTolerant<true>(0.)); // check if moving away towards -z
0193     bool done = signZdir * max < Real_v(MakePlusTolerant<true>(0.0)); // if outside + moving away towards +/-z
0194 
0195     // if all particles moving away, we're done
0196     if (done) return;
0197 
0198     // Step 1.b) General case:
0199     //   smax,smin are range of distances within z-range, taking direction into account.
0200     //   smin<smax - smax is positive, but smin may be either positive or negative
0201     Real_v invdir = Real_v(1.0) / NonZero(dir.z()); // convert distances from z to dir
0202     Real_v smax   = max * invdir;
0203     Real_v smin   = -(signZdir * unplaced.fDz + point.z()) * invdir;
0204 
0205     //
0206     // Step 2: find distances for intersections with side planes.
0207     //
0208 
0209 #ifdef VECGEOM_PLANESHELL
0210     // If disttoplanes is such that smin < dist < smax, then distance=disttoplanes
0211     Real_v disttoplanes = unplaced.GetPlanes()->DistanceToIn(point, dir, smin, smax);
0212     if (!done) distance = disttoplanes;
0213 
0214 #else
0215 
0216     // here for VECGEOM_PLANESHELL_DISABLE
0217 
0218     // loop over side planes - find pdist,Comp for each side plane
0219     Real_v pdist[4], comp[4], vdist[4];
0220     // EvaluateTrack<Real_v>(unplaced, point, dir, pdist, comp, vdist);
0221 
0222     // auto-vectorizable part of loop
0223     TrapSidePlane const *fPlanes = unplaced.GetPlanes();
0224     for (unsigned int i = 0; i < 4; ++i) {
0225       // Note: normal vector is pointing outside the volume (convention), therefore
0226       // pdist>0 if point is outside  and  pdist<0 means inside
0227       pdist[i] = fPlanes[i].fA * point.x() + fPlanes[i].fB * point.y() + fPlanes[i].fC * point.z() + fPlanes[i].fD;
0228 
0229       // Comp is projection of dir over the normal vector of side plane, hence
0230       // Comp > 0 if pointing ~same direction as normal and Comp<0 if ~opposite to normal
0231       comp[i] = fPlanes[i].fA * dir.x() + fPlanes[i].fB * dir.y() + fPlanes[i].fC * dir.z();
0232 
0233       vdist[i] = -pdist[i] / NonZero(comp[i]);
0234     }
0235 
0236     // check special cases
0237     for (int i = 0; i < 4; ++i) {
0238       // points fully outside a plane and moving away or parallel to that plane
0239       done = done || (pdist[i] > Real_v(MakePlusTolerant<true>(0.)) && comp[i] >= Real_v(0.));
0240       // points at a plane surface and exiting
0241       done = done || (pdist[i] > Real_v(MakeMinusTolerant<true>(0.)) && comp[i] > Real_v(0.));
0242     }
0243     // if all particles moving away, we're done
0244     if (done) return;
0245 
0246     // this part does not auto-vectorize
0247     for (unsigned int i = 0; i < 4; ++i) {
0248       // if outside and moving away, return infinity
0249       bool posPoint = pdist[i] > Real_v(MakeMinusTolerant<true>(0.));
0250       bool posDir   = comp[i] > 0;
0251 
0252       // check if trajectory will intercept plane within current range (smin,smax), otherwise track misses shape
0253       bool interceptFromInside  = (!posPoint && posDir);
0254       bool interceptFromOutside = (posPoint && !posDir);
0255 
0256       //.. If dist is such that smin < dist < smax, then adjust either smin or smax
0257       if (interceptFromInside && vdist[i] < smax) smax = vdist[i];
0258       if (interceptFromOutside && vdist[i] > smin) smin = vdist[i];
0259     }
0260 
0261     if (!done && smin <= smax) distance = smin;
0262     if (distance < Real_v(MakeMinusTolerant<true>(0.0))) distance = Real_v(-1.);
0263 #endif
0264   }
0265 
0266   /// @brief Compute the first exit distance from inside or on the trapezoid surface.
0267   /// @tparam Real_v Floating-point scalar type.
0268   /// @param unplaced Trapezoid data.
0269   /// @param point Local start point.
0270   /// @param dir Unit local direction.
0271   /// @param stepMax Unused by this implementation.
0272   /// @param[out] distance Exit distance, zero for an immediate tolerated exit, or `-1` for outside input.
0273   /// @param hitInfo Optional exit-normal destination and supporting-plane result.
0274   ///
0275   /// @details The z planes and side planes are checked with distance and
0276   /// direction tolerances. A start already outside any limiting plane reports
0277   /// `-1`; a tolerated surface start moving outward reports zero.
0278   template <typename Real_v>
0279   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void DistanceToOut(UnplacedStruct_t const &unplaced,
0280                                                                          Vector3D<Real_v> const &point,
0281                                                                          Vector3D<Real_v> const &dir,
0282                                                                          Real_v const &stepMax, Real_v &distance,
0283                                                                          SurfaceHitView<Real_v> *hitInfo = nullptr)
0284   {
0285     (void)stepMax;
0286     const Real_v distanceTolerance  = kToleranceDist<Real_v>;
0287     const Real_v directionTolerance = kToleranceStrict<Real_v> * Real_v(unplaced.fInvProjectionScale);
0288     int exitFace                    = -1;
0289     if (hitInfo) hitInfo->Clear();
0290 
0291     // step 0: if point is outside any plane --> return -1, otherwise initialize at Infinity
0292     bool outside = Abs(point.z()) > unplaced.fDz + distanceTolerance;
0293     distance     = InfinityLength<Real_v>();
0294 
0295     //
0296     // Step 1: find range of distances along dir between Z-planes (smin, smax)
0297     //
0298 
0299     if (Abs(dir.z()) > directionTolerance) {
0300       if (Abs(point.z()) - unplaced.fDz >= -distanceTolerance && point.z() * dir.z() > Real_v(0.)) {
0301         distance = Real_v(0.);
0302         exitFace = point.z() < Real_v(0.) ? 4 : 5;
0303       } else {
0304         // The surface-exit case above leaves a forward z-plane intersection here.
0305         distance = (Sign(dir.z()) * unplaced.fDz - point.z()) / dir.z();
0306         exitFace = dir.z() < Real_v(0.) ? 4 : 5;
0307       }
0308     }
0309 
0310     //
0311     // Step 2: find distances for intersections with side planes.
0312     //
0313 
0314 #if defined(VECGEOM_PLANESHELL) && defined(VECGEOM_VC) && defined(VECGEOM_QUADRILATERAL_ACCELERATION)
0315     unplaced.GetPlanes()->DistanceToOut(point, dir, distanceTolerance, directionTolerance, outside, distance,
0316                                         hitInfo ? &exitFace : nullptr);
0317 #else
0318 #ifdef VECGEOM_PLANESHELL
0319     auto const *planes = unplaced.GetPlanes();
0320     // The two y-side planes have no x component by construction.
0321     for (unsigned int i = 0; i < 2; ++i) {
0322       Real_v pdist = planes->fB[i] * point.y() + planes->fC[i] * point.z() + planes->fD[i];
0323       Real_v proj  = planes->fB[i] * dir.y() + planes->fC[i] * dir.z();
0324 
0325       outside |= pdist > distanceTolerance;
0326       if (pdist < -distanceTolerance) {
0327         if (proj > directionTolerance) {
0328           Real_v vdist = -pdist / proj;
0329           if (vdist < distance) {
0330             distance = vdist;
0331             exitFace = i;
0332           }
0333         }
0334       } else if (pdist <= distanceTolerance && proj > directionTolerance) {
0335         distance = Real_v(0.);
0336         exitFace = i;
0337       }
0338     }
0339     for (unsigned int i = 2; i < 4; ++i) {
0340       Real_v pdist = planes->fA[i] * point.x() + planes->fB[i] * point.y() + planes->fC[i] * point.z() + planes->fD[i];
0341       Real_v proj  = planes->fA[i] * dir.x() + planes->fB[i] * dir.y() + planes->fC[i] * dir.z();
0342 
0343       outside |= pdist > distanceTolerance;
0344       if (pdist < -distanceTolerance) {
0345         if (proj > directionTolerance) {
0346           Real_v vdist = -pdist / proj;
0347           if (vdist < distance) {
0348             distance = vdist;
0349             exitFace = i;
0350           }
0351         }
0352       } else if (pdist <= distanceTolerance && proj > directionTolerance) {
0353         distance = Real_v(0.);
0354         exitFace = i;
0355       }
0356     }
0357 #else
0358     Real_v pdist[4], proj[4], vdist[4];
0359     EvaluateTrack<Real_v>(unplaced, point, dir, pdist, proj, vdist);
0360 
0361     for (unsigned int i = 0; i < 4; ++i) {
0362       outside |= pdist[i] > distanceTolerance;
0363       if (pdist[i] < -distanceTolerance) {
0364         if (proj[i] > directionTolerance && vdist[i] < distance) {
0365           distance = vdist[i];
0366           exitFace = i;
0367         }
0368       } else if (pdist[i] <= distanceTolerance && proj[i] > directionTolerance) {
0369         distance = Real_v(0.);
0370         exitFace = i;
0371       }
0372     }
0373 #endif
0374 #endif
0375 
0376     if (outside) {
0377       distance = Real_v(-1.);
0378       return;
0379     }
0380     if (hitInfo && exitFace >= 0) {
0381       hitInfo->SetLeftBehind(true);
0382       if (hitInfo->WantsNormal()) hitInfo->SetNormal(unplaced.normals[exitFace]);
0383     }
0384   }
0385 
0386   /// @brief Compute safety from an exterior point to the trapezoid.
0387   /// @tparam Real_v Floating-point scalar type.
0388   /// @param unplaced Trapezoid data.
0389   /// @param point Local point.
0390   /// @param[out] safety Conservative distance to the nearest entry boundary.
0391   template <typename Real_v>
0392   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void SafetyToIn(UnplacedStruct_t const &unplaced,
0393                                                                       Vector3D<Real_v> const &point, Real_v &safety)
0394   {
0395     safety = Abs(point.z()) - unplaced.fDz;
0396 
0397 #ifdef VECGEOM_PLANESHELL
0398     // Get safety over side planes
0399     unplaced.GetPlanes()->SafetyToIn(point, safety);
0400 #else
0401     // Loop over side planes
0402     TrapSidePlane const *fPlanes = unplaced.GetPlanes();
0403     Real_v dist[4];
0404     for (int i = 0; i < 4; ++i) {
0405       dist[i] = fPlanes[i].fA * point.x() + fPlanes[i].fB * point.y() + fPlanes[i].fC * point.z() + fPlanes[i].fD;
0406     }
0407 
0408     Real_v safmax = Max(Max(dist[0], dist[1]), Max(dist[2], dist[3]));
0409     if (safmax > safety) safety = safmax;
0410 #endif
0411   }
0412 
0413   /// @brief Compute safety from an interior point to leave the trapezoid.
0414   /// @tparam Real_v Floating-point scalar type.
0415   /// @param unplaced Trapezoid data.
0416   /// @param point Local point.
0417   /// @param[out] safety Conservative distance to the nearest exit boundary; negative for outside points.
0418   template <typename Real_v>
0419   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void SafetyToOut(UnplacedStruct_t const &unplaced,
0420                                                                        Vector3D<Real_v> const &point, Real_v &safety)
0421   {
0422     // If point is outside (wrong-side) --> safety to negative value
0423     safety = unplaced.fDz - Abs(point.z());
0424 
0425 #ifdef VECGEOM_PLANESHELL
0426     // Get safety over side planes
0427     unplaced.GetPlanes()->SafetyToOut(point, safety);
0428 #else
0429     // Loop over side planes
0430     TrapSidePlane const *fPlanes = unplaced.GetPlanes();
0431 
0432     // auto-vectorizable loop
0433     Real_v dist[4];
0434     for (int i = 0; i < 4; ++i) {
0435       dist[i] = -(fPlanes[i].fA * point.x() + fPlanes[i].fB * point.y() + fPlanes[i].fC * point.z() + fPlanes[i].fD);
0436     }
0437 
0438     Real_v safmin = Min(Min(dist[0], dist[1]), Min(dist[2], dist[3]));
0439     if (safmin < safety) safety = safmin;
0440 #endif
0441   }
0442 
0443   /// @brief Compute a trapezoid surface normal.
0444   /// @tparam Real_v Floating-point scalar type.
0445   /// @param unplaced Trapezoid data.
0446   /// @param point Local point.
0447   /// @param[out] valid Set when @p point is close enough to a limiting surface.
0448   /// @return Outward normal for the closest limiting plane, or a summed edge/corner normal.
0449   ///
0450   /// @details Side and z planes are compared using the local surface tolerance.
0451   /// Points on multiple surfaces return the normalized sum of the contributing
0452   /// outward normals.
0453   template <typename Real_v>
0454   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static Vector3D<Real_v> NormalKernel(UnplacedStruct_t const &unplaced,
0455                                                                                     Vector3D<Real_v> const &point,
0456                                                                                     bool &valid)
0457   {
0458 
0459     VECGEOM_CONST Precision delta = 1000. * kTolerance;
0460     Vector3D<Real_v> normal, cornerNormal;
0461     Real_v safety = InfinityLength<Real_v>();
0462     bool edge     = false;
0463 
0464 #ifdef VECGEOM_PLANESHELL
0465     // Get normal from side planes -- PlaneShell case
0466     safety = unplaced.GetPlanes()->NormalKernel(point, normal, edge);
0467 
0468 #else
0469     // Loop over side planes; reuse the accumulator declared above.
0470     unsigned char surfaces = 0;
0471     // vectorizable loop
0472     TrapSidePlane const *fPlanes = unplaced.GetPlanes();
0473     Real_v dist[4];
0474     for (int i = 0; i < 4; ++i) {
0475       dist[i] = Abs(fPlanes[i].fA * point.x() + fPlanes[i].fB * point.y() + fPlanes[i].fC * point.z() + fPlanes[i].fD);
0476       // If closest update normal
0477       if (dist[i] < safety) {
0478         normal = unplaced.normals[i];
0479         safety = dist[i];
0480       }
0481       // If on surface add to separate vector
0482       if (dist[i] < kTolerance) {
0483         surfaces++;
0484         cornerNormal += unplaced.normals[i];
0485       }
0486     }
0487 
0488     if (surfaces > 1) {
0489       // The point is on the edge - do not normalize the vector
0490       normal = cornerNormal;
0491       edge   = true;
0492     }
0493 #endif
0494 
0495     // check if normal is valid w.r.t. z-planes, and define normals based on safety (see above)
0496     Real_v safz = Abs(Abs(point[2]) - unplaced.fDz);
0497     if (edge && safz < kTolerance) {
0498       // The point is on a corner
0499       normal += Vector3D<Real_v>(0., 0., Sign(point.z()));
0500     } else {
0501       if (safz < safety) {
0502         normal.Set(0., 0., Sign(point.z()));
0503         safety = safz;
0504       }
0505     }
0506     valid = Abs(safety) <= delta;
0507     // returned vector must be normalized
0508     normal.Normalize();
0509     return normal;
0510   }
0511 };
0512 
0513 } // namespace VECGEOM_IMPL_NAMESPACE
0514 } // namespace vecgeom
0515 
0516 #endif // VECGEOM_VOLUMES_KERNEL_TRAPEZOIDIMPLEMENTATION_H_