contains_sphere_alignedbox.hpp

Back to Containment testing

pastel/geometry/containment/

#ifndef PASTELGEOMETRY_CONTAINS_SPHERE_ALIGNEDBOX_HPP
#define PASTELGEOMETRY_CONTAINS_SPHERE_ALIGNEDBOX_HPP

#include "pastel/geometry/containment/contains_sphere_alignedbox.h"

namespace Pastel
{

    template <typename Real, integer N>
    bool contains(
        const Sphere<Real, N>& outerSphere,
        const AlignedBox<Real, N>& innerBox)
    {
        Vector<Real, N> boxCenter =
            linear(innerBox.min(), innerBox.max(), 0.5);

        Vector<Real, N> boxRadius =
            innerBox.extent() / 2;

        Real cornerDistance2 =
            dot(abs(innerSphere.position() - boxCenter) + boxRadius);

        return cornerDistance2 <= innerSphere.radius() * innerSphere.radius();
    }

}

#endif