Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-16 09:21:47

0001 #ifndef ROOT_GEOM_BVH2_EXTRA
0002 
0003 namespace bvh::v2::extra {
0004 
0005 // reusable geometry kernels used in multiple places
0006 // for interaction with BVH2 structures
0007 
0008 // determines if a point is inside the bounding box
0009 template <typename T>
0010 bool contains(bvh::v2::BBox<T, 3> const &box, bvh::v2::Vec<T, 3> const &p)
0011 {
0012    auto min = box.min;
0013    auto max = box.max;
0014    return (p[0] >= min[0] && p[0] <= max[0]) && (p[1] >= min[1] && p[1] <= max[1]) &&
0015           (p[2] >= min[2] && p[2] <= max[2]);
0016 }
0017 
0018 // determines the largest squared distance of point to any of the bounding box corners
0019 template <typename T>
0020 auto RmaxSqToNode(bvh::v2::BBox<T, 3> const &box, bvh::v2::Vec<T, 3> const &p)
0021 {
0022    // construct the 8 corners to get the maximal distance
0023    const auto minCorner = box.min;
0024    const auto maxCorner = box.max;
0025    using Vec3 = bvh::v2::Vec<T, 3>;
0026    // these are the corners of the bounding box
0027    const std::array<bvh::v2::Vec<T, 3>, 8> corners{
0028       Vec3{minCorner[0], minCorner[1], minCorner[2]}, Vec3{minCorner[0], minCorner[1], maxCorner[2]},
0029       Vec3{minCorner[0], maxCorner[1], minCorner[2]}, Vec3{minCorner[0], maxCorner[1], maxCorner[2]},
0030       Vec3{maxCorner[0], minCorner[1], minCorner[2]}, Vec3{maxCorner[0], minCorner[1], maxCorner[2]},
0031       Vec3{maxCorner[0], maxCorner[1], minCorner[2]}, Vec3{maxCorner[0], maxCorner[1], maxCorner[2]}};
0032 
0033    T Rmax_sq{0};
0034    for (const auto &corner : corners) {
0035       float R_sq = 0.;
0036       const auto dx = corner[0] - p[0];
0037       R_sq += dx * dx;
0038       const auto dy = corner[1] - p[1];
0039       R_sq += dy * dy;
0040       const auto dz = corner[2] - p[2];
0041       R_sq += dz * dz;
0042       Rmax_sq = std::max(Rmax_sq, R_sq);
0043    }
0044    return Rmax_sq;
0045 };
0046 
0047 // determines the minimum squared distance of point to a bounding box ("safey square")
0048 template <typename T>
0049 auto SafetySqToNode(bvh::v2::BBox<T, 3> const &box, bvh::v2::Vec<T, 3> const &p)
0050 {
0051    T sqDist{0.0};
0052    for (int i = 0; i < 3; i++) {
0053       T v = p[i];
0054       if (v < box.min[i]) {
0055          sqDist += (box.min[i] - v) * (box.min[i] - v);
0056       } else if (v > box.max[i]) {
0057          sqDist += (v - box.max[i]) * (v - box.max[i]);
0058       }
0059    }
0060    return sqDist;
0061 };
0062 
0063 } // namespace bvh::v2::extra
0064 
0065 #endif