Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math: Fix Math::Intersection::sphereFrustum #482

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/Magnum/Math/Intersection.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ template<class T> bool aabbFrustum(const Vector3<T>& aabbCenter, const Vector3<T

Checks for each plane of the frustum whether the sphere is behind the plane
(the points distance larger than the sphere's radius) using

@ref Distance::pointPlaneScaled().
*/
template<class T> bool sphereFrustum(const Vector3<T>& sphereCenter, T sphereRadius, const Frustum<T>& frustum);
Expand Down Expand Up @@ -441,19 +442,16 @@ template<class T> bool aabbFrustum(const Vector3<T>& aabbCenter, const Vector3<T
}

template<class T> bool sphereFrustum(const Vector3<T>& sphereCenter, const T sphereRadius, const Frustum<T>& frustum) {
const T radiusSq = sphereRadius*sphereRadius;

for(const Vector4<T>& plane: frustum) {
/* The sphere is in front of one of the frustum planes (normals point
outwards) */
if(Distance::pointPlaneScaled<T>(sphereCenter, plane) < -radiusSq)
if(Distance::pointPlane<T>(sphereCenter, plane) >= sphereRadius)
return false;
}

return true;
}


template<class T> bool pointCone(const Vector3<T>& point, const Vector3<T>& coneOrigin, const Vector3<T>& coneNormal, const Rad<T> coneAngle) {
const T tanAngleSqPlusOne = Math::pow<2>(Math::tan(coneAngle*T(0.5))) + T(1);
return pointCone(point, coneOrigin, coneNormal, tanAngleSqPlusOne);
Expand Down
24 changes: 20 additions & 4 deletions src/Magnum/Math/Test/DistanceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,26 @@ void DistanceTest::lineSegmentPoint3D() {
}

void DistanceTest::pointPlane() {
Vector3 point{0.0f, 0.0f, 0.0f};
Vector4 plane{3.0f, 0.0f, 4.0f, 5.0f};

CORRADE_COMPARE(Distance::pointPlane(point, plane), 1.0f);
{
Vector4 plane{3.0f, 0.0f, 4.0f, 5.0f};
CORRADE_COMPARE(Distance::pointPlane({}, plane), 1.0f);
}
{
/* Origin plane with normal {0, 1, 0} */
Vector4 plane{0.0f, 1.0f, 0.0f, 0.0f};
/* Point lies in front of plane, 2 units away */
CORRADE_COMPARE(Distance::pointPlane({0.0f, 2.0f, 0.0f}, plane), 2.0f);
/* Point lies in begind the plane, 2 units away */
CORRADE_COMPARE(Distance::pointPlane({0.0f, -2.0f, 0.0f}, plane), -2.0f);
}
{
/* Origin plane offset by 1 normal with normal {0, 1, 0} */
Vector4 plane{0.0f, 1.0f, 0.0f, -1.0f};
/* Point lies in front of plane, 2 units away */
CORRADE_COMPARE(Distance::pointPlane({0.0f, 2.0f, 0.0f}, plane), 1.0f);
/* Point lies in begind the plane, 2 units away */
CORRADE_COMPARE(Distance::pointPlane({0.0f, -2.0f, 0.0f}, plane), -3.0f);
}
}

void DistanceTest::pointPlaneScaled() {
Expand Down
56 changes: 43 additions & 13 deletions src/Magnum/Math/Test/IntersectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ void IntersectionTest::rangeFrustum() {

void IntersectionTest::aabbFrustum() {
const Frustum frustum{
{1.0f, 0.0f, 0.0f, 0.0f},
{-1.0f, 0.0f, 0.0f, 5.0f},
{0.0f, 1.0f, 0.0f, 0.0f},
{1.0f, 0.0f, 0.0f, 0.0f},
{0.0f, -1.0f, 0.0f, 1.0f},
{0.0f, 0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, -1.0f, 10.0f}};
{0.0f, 1.0f, 0.0f, 0.0f},
{0.0f, 0.0f, -1.0f, 10.0f},
{0.0f, 0.0f, 1.0f, 0.0f}};

/* Fully inside */
CORRADE_VERIFY(Intersection::aabbFrustum(Vector3{0.0f}, Vector3{1.0f}, frustum));
Expand All @@ -204,20 +204,50 @@ void IntersectionTest::aabbFrustum() {
}

void IntersectionTest::sphereFrustum() {
/* Frustum spanning [(-10, -10, -10), (0, 0, 0)] */
const Frustum frustum{
{-1.0f, 0.0f, 0.0f, -10.0f},
{1.0f, 0.0f, 0.0f, 0.0f},
{-1.0f, 0.0f, 0.0f, 10.0f},
{0.0f, -0.5f, 0.0f, -5.0f}, /* Intentionally not normalized */
{0.0f, 1.0f, 0.0f, 0.0f},
{0.0f, -1.0f, 0.0f, 10.0f},
{0.0f, 0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, -1.0f, 10.0f}};
Copy link
Owner

@mosra mosra Nov 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, so if there's multiple ways how to represent the same plane and the planes can be in any order, maybe the test could be made instanced with testing a few equivalent variants and expecting the same points get always the same result? That could harden it quite nicely.

And that could be done for all *Frustum tests -- I see you already updated planeFrustum(), so the same instancing change could be done to all.

EDIT: and while you're at it, I think including arbitrary (instanced) transformation in both the frustum planes and the points could make sense as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instanced test for permutating the plane order sounds like it would get out of hand in the test results pretty quickly, I think a loop with iteration description is probably enough?

Re the transformation: Do you mean offsetting/transforming the entire space, frustum and points, sphere's etc by a certain transform?


/* Sphere on edge */
CORRADE_VERIFY(Intersection::sphereFrustum({0.0f, 0.0f, -1.0f}, 1.5f, frustum));
{0.0f, 0.0f, -2.0f, -20.0f}}; /* Intentionally not normalized */

/* Sphere overlapping each face by 0.5 */
CORRADE_VERIFY(Intersection::sphereFrustum({ 1.0f, 0.0f, 0.0f}, 1.5f, frustum));
CORRADE_VERIFY(Intersection::sphereFrustum({-11.0f, 0.0f, 0.0f}, 1.5f, frustum));
CORRADE_VERIFY(Intersection::sphereFrustum({ 0.0f, 1.0f, 0.0f}, 1.5f, frustum));
CORRADE_VERIFY(Intersection::sphereFrustum({ 0.0f,-11.0f, 0.0f}, 1.5f, frustum));
CORRADE_VERIFY(Intersection::sphereFrustum({ 0.0f, 0.0f, 1.0f}, 1.5f, frustum));
CORRADE_VERIFY(Intersection::sphereFrustum({ 0.0f, 0.0f,-11.0f}, 1.5f, frustum));
/* Sphere touching each face */
CORRADE_VERIFY(!Intersection::sphereFrustum({ 1.0f, 0.0f, 0.0f}, 1.0f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({-11.0f, 0.0f, 0.0f}, 1.0f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({ 0.0f, 1.0f, 0.0f}, 1.0f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({ 0.0f,-11.0f, 0.0f}, 1.0f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({ 0.0f, 0.0f, 1.0f}, 1.0f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({ 0.0f, 0.0f,-11.0f}, 1.0f, frustum));
/* Sphere inside */
CORRADE_VERIFY(Intersection::sphereFrustum({5.5f, 5.5f, 5.5f}, 1.5f, frustum));
/* Sphere outside */
CORRADE_VERIFY(!Intersection::sphereFrustum({0.0f, 0.0f, 100.0f}, 0.5f, frustum));
CORRADE_VERIFY(Intersection::sphereFrustum({-1.0f, -2.0f, -3.0f}, 1.5f, frustum));
/* Sphere outside each face */
CORRADE_VERIFY(!Intersection::sphereFrustum({ 1.0f, 0.0f, 0.0f}, 0.5f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({-11.0f, 0.0f, 0.0f}, 0.5f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({ 0.0f, 1.0f, 0.0f}, 0.5f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({ 0.0f,-11.0f, 0.0f}, 0.5f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({ 0.0f, 0.0f, 1.0f}, 0.5f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({ 0.0f, 0.0f,-11.0f}, 0.5f, frustum));

/* Some "Corner" cases, sphere touching corner, but shouldn't intersect */
CORRADE_VERIFY(!Intersection::sphereFrustum({1.1f, 1.1f, 1.1f}, 1.1f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({-11.1f, -11.1f, -11.1f}, 1.1f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({1.1f, -11.1f, 1.1f}, 1.1f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({-11.1f, 1.1f, -11.1f}, 1.1f, frustum));

CORRADE_VERIFY(!Intersection::sphereFrustum({-11.1f, 1.1f, 1.1f}, 1.1f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({1.1f, -11.1f, -11.1f}, 1.1f, frustum));

CORRADE_VERIFY(!Intersection::sphereFrustum({1.1f, 1.1f, -11.1f}, 1.1f, frustum));
CORRADE_VERIFY(!Intersection::sphereFrustum({-11.1f, -11.1f, 1.1f}, 1.1f, frustum));
}

void IntersectionTest::pointCone() {
Expand Down