Skip to content
Merged
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
28 changes: 25 additions & 3 deletions include/fcl/narrowphase/collision_object-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,36 @@ void CollisionObject<S>::computeAABB()
if(t.linear().isIdentity())
{
aabb = translate(cgeom->aabb_local, t.translation());
return;
}
else

const Vector3<S> center = t * cgeom->aabb_center;

// Bound the rotated local AABB. Rotating a box with half-widths h by R and
// taking the axis-aligned bound of the result gives half-widths |R| h. This
// is the center/extent form of Arvo's method for bounding a transformed box
// (Graphics Gems, 1990); see Ericson, Real-Time Collision Detection,
// section 4.2.6.
const Vector3<S> h_local =
(cgeom->aabb_local.max_ - cgeom->aabb_local.min_) * S(0.5);
const Vector3<S> delta = t.linear().cwiseAbs() * h_local;
// Only delta can go non-finite: h_local subtracts two extents that may both
// sit at the scalar type's limit, which overflows. center transforms a single
// stored value, and the bound below transforms that same value, so testing it
// would not change which bound a geometry gets.
if(delta.allFinite())
{
Vector3<S> center = t * cgeom->aabb_center;
Vector3<S> delta = Vector3<S>::Constant(cgeom->aabb_radius);
aabb.min_ = center - delta;
aabb.max_ = center + delta;
return;
}

// Half spaces and planes report an unbounded local AABB as extents at the
// limits of the scalar type, which overflow the half-width above.
// Use a sphere-based bound instead.
const Vector3<S> sphere_delta = Vector3<S>::Constant(cgeom->aabb_radius);
aabb.min_ = center - sphere_delta;
aabb.max_ = center + sphere_delta;
}

//==============================================================================
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ set(tests
test_fcl_capsule_capsule.cpp
test_fcl_cylinder_half_space.cpp
test_fcl_collision.cpp
test_fcl_collision_object.cpp
test_fcl_constant_eps.cpp
test_fcl_distance.cpp
test_fcl_frontlist.cpp
Expand Down
65 changes: 41 additions & 24 deletions test/broadphase/test_broadphase_SaP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,30 @@ PairSet FindOverlappingPairs(
}

// SaP tracks each object as an interval per axis and splices the interval's
// endpoints into sorted lists as objects move. CollisionObject uses a tight
// local AABB at the identity orientation and a conservative, radius-sized AABB
// at any non-identity orientation. Moving between those two representations
// exercises all four endpoint directions on every axis.
// endpoints into sorted lists as objects move. Rotating an object can change
// the AABB SaP uses to define its intervals. Below, we consider the intervals
// for a box in identity rotation and an adversarial rotation that will cause
// the AABB to expand different amounts in each axis.
GTEST_TEST(BroadPhaseSaP, UpdateWithChangingExtents) {
// The unequal dimensions make the identity AABB strictly smaller than the
// conservative rotated AABB on every axis. Also, the box has the obnoxious
// AABB computation for non-identity orientations (see below).
auto shape = std::make_shared<Box<double>>(20.0, 1.0, 1.0);
// A 60-degree rotation about (1, 1, 1) grows this box's AABB half-extents
// from (2, 3/2, 1) to (5/2, 8/3, 7/3). The distinct growth on each axis
// makes it possible for this test to detect accidental coupling of axes.
auto shape = std::make_shared<Box<double>>(4.0, 3.0, 2.0);

std::vector<std::unique_ptr<CollisionObject<double>>> objects;
std::vector<CollisionObject<double>*> raw;
// Note: 7 is an arbitrary number of overlapping boxes. With a separation of
// 3 meters and a length of 20 meters (along the x-axis), we guarantee that
// the first and last boxes will not be overlapping.
for (int i = 0; i < 7; ++i) {
// Each non-origin box is initially separated from the origin box along one
// distinct axis. Rotation grows the corresponding extent enough to create
// an overlap. Thus, each axis independently affects an observable pair.
const Vector3<double> translations[] = {
{0.0, 0.0, 0.0},
{4.5, 0.0, 0.0},
{0.0, 4.0, 0.0},
{0.0, 0.0, 3.0},
};
for (const Vector3<double>& translation : translations) {
Transform3<double> X_WB = Transform3<double>::Identity();
X_WB.translation() = Vector3<double>(3.0 * i, 0.5 * i, 0.0);
X_WB.translation() = translation;
objects.push_back(
std::make_unique<CollisionObject<double>>(shape, X_WB));
raw.push_back(objects.back().get());
Expand All @@ -117,29 +123,40 @@ GTEST_TEST(BroadPhaseSaP, UpdateWithChangingExtents) {
sap.setup();
reference.setup();
const PairSet reference_set = FindOverlappingPairs(&reference, raw);
EXPECT_GT(reference_set.size(), 0);
EXPECT_TRUE(reference_set.empty());
EXPECT_EQ(FindOverlappingPairs(&sap, raw), reference_set);

// Test the cases where internal endpoints *expand*. Any non-identity
// orientation selects the conservative AABB (the bounding box of the bounding
// sphere of the geometry-local bounding box of the box); we'll use a
// 45-degree rotation to make it as bad as possible (even if the upstream
// representation of Box AABB eventually changes). From the original
// identity orientation, all boxes should get bigger.
// Test the cases where internal endpoints *expand*. From the original
// identity orientation, all boxes should get bigger on every axis.
for (std::size_t i = 0; i < raw.size(); ++i) {
const AABB<double> identity_aabb = raw[i]->getAABB();
raw[i]->setRotation(AngleAxis<double>(M_PI / 4, Vector3<double>::UnitZ())
.toRotationMatrix());
const Vector3<double> identity_size =
identity_aabb.max_ - identity_aabb.min_;
raw[i]->setRotation(
AngleAxis<double>(M_PI / 3,
Vector3<double>(1.0, 1.0, 1.0).normalized())
.toRotationMatrix());
raw[i]->computeAABB();
const Vector3<double> rotated_size =
raw[i]->getAABB().max_ - raw[i]->getAABB().min_;
// Show expansion of the AABB.
for (int axis = 0; axis < 3; ++axis) {
EXPECT_LT(raw[i]->getAABB().min_[axis], identity_aabb.min_[axis]);
EXPECT_GT(raw[i]->getAABB().max_[axis], identity_aabb.max_[axis]);
}
// Show that the interval sizes are still unique along each axis.
EXPECT_LT(rotated_size[0] - identity_size[0],
rotated_size[1] - identity_size[1]);
EXPECT_LT(rotated_size[1] - identity_size[1],
rotated_size[2] - identity_size[2]);
}
sap.update();
reference.update();
PairSet expanded_set = FindOverlappingPairs(&reference, raw);
EXPECT_GT(expanded_set.size(), reference_set.size());
const PairSet expanded_set = FindOverlappingPairs(&reference, raw);
const PairSet expected_expanded_set = {
{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3},
};
EXPECT_EQ(expanded_set, expected_expanded_set);
EXPECT_EQ(FindOverlappingPairs(&sap, raw), expanded_set) << "expand";

// Test the case where the endpoints *contract*. Returning to identity
Expand Down
238 changes: 238 additions & 0 deletions test/test_fcl_collision_object.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
#include <cmath>
#include <limits>
#include <memory>

#include <gtest/gtest.h>

#include "eigen_matrix_compare.h"
#include "fcl/common/types.h"
#include "fcl/geometry/shape/box.h"
#include "fcl/geometry/shape/halfspace.h"
#include "fcl/narrowphase/collision_object.h"

namespace fcl {
namespace {

// Room for the rounding of a rotation applied to a point of the given
// magnitude. The bound is exact, so a corner can land a couple of units in
// the last place outside the box it touches.
template <typename S>
S Tolerance(S magnitude) {
return S(64) * std::numeric_limits<S>::epsilon() * std::max(S(1), magnitude);
}

template <typename S>
std::shared_ptr<Box<S>> MakeBox(const Vector3<S>& half_widths) {
return std::make_shared<Box<S>>(2 * half_widths[0], 2 * half_widths[1],
2 * half_widths[2]);
}

// An unrotated geometry keeps its local box, moved by the translation.
template <typename S>
void TestIdentityRotation() {
const Vector3<S> half_widths(S(10), S(5), S(0.05));
Transform3<S> X_WG = Transform3<S>::Identity();
X_WG.translation() = Vector3<S>(S(1), S(2), S(3));

const CollisionObject<S> object(MakeBox(half_widths), X_WG);

const S tolerance = Tolerance<S>(half_widths.norm());
EXPECT_TRUE(CompareMatrices(object.getAABB().min_,
X_WG.translation() - half_widths, tolerance));
EXPECT_TRUE(CompareMatrices(object.getAABB().max_,
X_WG.translation() + half_widths, tolerance));
}

// A rotated box is bounded by the rotated local box, whose half-widths are
// |R| h. For a thin box turned 45 degrees about z, that is much tighter on
// the thin axis than the cube of half-width aabb_radius that bounds the
// circumscribing sphere.
template <typename S>
void TestRotatedBoxMatchesAnalyticBound() {
const Vector3<S> half_widths(S(10), S(5), S(0.05));
const auto box = MakeBox(half_widths);
Transform3<S> X_WG = Transform3<S>::Identity();
X_WG.linear() = AngleAxis<S>(constants<S>::pi() / 4, Vector3<S>::UnitZ())
.toRotationMatrix();
X_WG.translation() = Vector3<S>(S(1), S(2), S(3));

const CollisionObject<S> object(box, X_WG);

// Rotating about z mixes the first two half-widths through cos(45 degrees)
// and leaves the third alone.
const S mixed = (half_widths[0] + half_widths[1]) / std::sqrt(S(2));
const Vector3<S> expected(mixed, mixed, half_widths[2]);

const S tolerance = Tolerance<S>(half_widths.norm());
EXPECT_TRUE(CompareMatrices(object.getAABB().min_,
X_WG.translation() - expected, tolerance));
EXPECT_TRUE(CompareMatrices(object.getAABB().max_,
X_WG.translation() + expected, tolerance));

}

// Rotating by 120 degrees about the (1, 1, 1) diagonal permutes the axes, so
// the bound's half-widths are the geometry's half-widths permuted. This is an
// exact case whose rotation is all zeros and ones.
template <typename S>
void TestAxisPermutingRotation() {
const Vector3<S> half_widths(S(4), S(2), S(0.5));
Transform3<S> X_WG = Transform3<S>::Identity();
X_WG.linear() =
AngleAxis<S>(2 * constants<S>::pi() / 3,
Vector3<S>(S(1), S(1), S(1)).normalized())
.toRotationMatrix();

const CollisionObject<S> object(MakeBox(half_widths), X_WG);

const Vector3<S> expected(half_widths[2], half_widths[0], half_widths[1]);
const S tolerance = Tolerance<S>(half_widths.norm());
EXPECT_TRUE(CompareMatrices(object.getAABB().min_, -expected, tolerance));
EXPECT_TRUE(CompareMatrices(object.getAABB().max_, expected, tolerance));
}

// A posed box, described by the pieces the test builds it from.
template <typename S>
struct PosedBox {
const char* description;
Vector3<S> half_widths;
Vector3<S> rotation_axis;
S rotation_angle;
Vector3<S> translation;
};

// Whatever the pose, the bound must contain the geometry.
template <typename S>
void TestBoundContainsGeometry() {
const S kQuarterTurn = constants<S>::pi() / 2;
const PosedBox<S> cases[] = {
// A thin plate turned within its own plane, which is what convex
// decomposition produces and how such pieces usually rest.
{"plate turned in plane",
{S(1), S(0.5), S(0.01)},
{S(0), S(0), S(1)},
constants<S>::pi() / 4,
{S(0), S(0), S(0)}},
// The same plate under a rotation that mixes all three axes.
{"plate, general rotation",
{S(1), S(0.5), S(0.01)},
{S(1), S(2), S(3)},
S(0.7),
{S(0.25), S(-0.5), S(2)}},
// An elongated rod, the other extreme shape.
{"rod",
{S(5), S(0.05), S(0.05)},
{S(0), S(1), S(0)},
constants<S>::pi() / 6,
{S(0), S(0), S(0)}},
// A cube on its diagonal, where the two bounds are closest together.
{"cube on its diagonal",
{S(1), S(1), S(1)},
{S(1), S(1), S(1)},
2 * constants<S>::pi() / 3,
{S(0), S(0), S(0)}},
// A quarter turn leaves exact zeros in the rotation.
{"quarter turn, near-degenerate extent",
{S(2), S(2), S(1e-6)},
{S(1), S(0), S(0)},
kQuarterTurn,
{S(0), S(0), S(0)}},
// Far from the origin, where the center term dominates the half-width.
{"far from the origin",
{S(0.5), S(3), S(0.25)},
{S(0), S(0), S(1)},
S(0.2),
{S(100), S(-50), S(7)}},
};

for (const PosedBox<S>& test_case : cases) {
SCOPED_TRACE(test_case.description);
const auto box = MakeBox(test_case.half_widths);
Transform3<S> X_WG = Transform3<S>::Identity();
X_WG.linear() = AngleAxis<S>(test_case.rotation_angle,
test_case.rotation_axis.normalized())
.toRotationMatrix();
X_WG.translation() = test_case.translation;

const CollisionObject<S> object(box, X_WG);
const AABB<S>& aabb = object.getAABB();
const S tolerance = Tolerance<S>(test_case.half_widths.norm() +
test_case.translation.norm());

for (int corner = 0; corner < 8; ++corner) {
const Vector3<S> p_GC(
(corner & 1) ? test_case.half_widths[0] : -test_case.half_widths[0],
(corner & 2) ? test_case.half_widths[1] : -test_case.half_widths[1],
(corner & 4) ? test_case.half_widths[2] : -test_case.half_widths[2]);
const Vector3<S> p_WC = X_WG * p_GC;
EXPECT_TRUE(((p_WC - aabb.min_).array() >= -tolerance).all())
<< "corner " << corner;
EXPECT_TRUE(((aabb.max_ - p_WC).array() >= -tolerance).all())
<< "corner " << corner;
}
}
}

template <typename S>
void TestUnboundedGeometry() {
const auto halfspace =
std::make_shared<Halfspace<S>>(Vector3<S>(0, 0, 1), S(0));

// The first rotation is about the half space's own normal, which leaves
// rows of exact zeros in the rotation; the second mixes all three axes.
const Matrix3<S> rotations[] = {
AngleAxis<S>(constants<S>::pi() / 4, Vector3<S>::UnitZ())
.toRotationMatrix(),
AngleAxis<S>(S(0.7), Vector3<S>(S(1), S(2), S(3)).normalized())
.toRotationMatrix()};

for (const Matrix3<S>& rotation : rotations) {
Transform3<S> X_WG = Transform3<S>::Identity();
X_WG.linear() = rotation;
const CollisionObject<S> object(halfspace, X_WG);
const AABB<S>& aabb = object.getAABB();

for (int i = 0; i < 3; ++i) {
ASSERT_FALSE(std::isnan(aabb.min_[i])) << "axis " << i;
ASSERT_FALSE(std::isnan(aabb.max_[i])) << "axis " << i;
EXPECT_TRUE(std::isinf(aabb.min_[i]) && aabb.min_[i] < S(0))
<< "axis " << i;
EXPECT_TRUE(std::isinf(aabb.max_[i]) && aabb.max_[i] > S(0))
<< "axis " << i;
}
}
}

GTEST_TEST(CollisionObjectAABB, IdentityRotation) {
TestIdentityRotation<double>();
TestIdentityRotation<float>();
}

GTEST_TEST(CollisionObjectAABB, RotatedBoxMatchesAnalyticBound) {
TestRotatedBoxMatchesAnalyticBound<double>();
TestRotatedBoxMatchesAnalyticBound<float>();
}

GTEST_TEST(CollisionObjectAABB, AxisPermutingRotation) {
TestAxisPermutingRotation<double>();
TestAxisPermutingRotation<float>();
}

GTEST_TEST(CollisionObjectAABB, BoundContainsGeometry) {
TestBoundContainsGeometry<double>();
TestBoundContainsGeometry<float>();
}

GTEST_TEST(CollisionObjectAABB, UnboundedGeometry) {
TestUnboundedGeometry<double>();
TestUnboundedGeometry<float>();
}

} // namespace
} // namespace fcl

//==============================================================================
int main(int argc, char* argv[]) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Loading