|
| 1 | +#include <tesseract/common/macros.h> |
| 2 | +TESSERACT_COMMON_IGNORE_WARNINGS_PUSH |
| 3 | +#include <gtest/gtest.h> |
| 4 | +#include <Eigen/Geometry> |
| 5 | +#include <memory> |
| 6 | +#include <stdexcept> |
| 7 | +#include <vector> |
| 8 | +TESSERACT_COMMON_IGNORE_WARNINGS_POP |
| 9 | + |
| 10 | +#include <tesseract/collision/coal/coal_cast_managers.h> |
| 11 | +#include <tesseract/collision/coal/coal_discrete_managers.h> |
| 12 | +#include <tesseract/geometry/impl/box.h> |
| 13 | + |
| 14 | +using namespace tesseract::collision; |
| 15 | + |
| 16 | +namespace |
| 17 | +{ |
| 18 | +/** @brief Add a unit box collision object at the origin */ |
| 19 | +template <typename ManagerType> |
| 20 | +void addBox(ManagerType& checker, const tesseract::common::LinkId& id) |
| 21 | +{ |
| 22 | + CollisionShapePtr box = std::make_shared<tesseract::geometry::Box>(1, 1, 1); |
| 23 | + CollisionShapesConst shapes{ box }; |
| 24 | + tesseract::common::VectorIsometry3d poses{ Eigen::Isometry3d::Identity() }; |
| 25 | + checker.addCollisionObject(id, 0, shapes, poses); |
| 26 | +} |
| 27 | + |
| 28 | +/** @brief Move two boxes into contact with a single one-pose parallel-array call and confirm the broadphase sees it */ |
| 29 | +template <typename ManagerType> |
| 30 | +void runOnePoseArraySetterTest() |
| 31 | +{ |
| 32 | + ManagerType checker; |
| 33 | + addBox(checker, tesseract::common::LinkId("box_a")); |
| 34 | + addBox(checker, tesseract::common::LinkId("box_b")); |
| 35 | + checker.setActiveCollisionObjects({ tesseract::common::LinkId("box_a"), tesseract::common::LinkId("box_b") }); |
| 36 | + checker.setDefaultCollisionMargin(0.0); |
| 37 | + |
| 38 | + // Apart |
| 39 | + const std::vector<tesseract::common::LinkId> ids{ "box_a", "box_b" }; |
| 40 | + Eigen::Isometry3d far_a{ Eigen::Isometry3d::Identity() }; |
| 41 | + far_a.translation() = Eigen::Vector3d(-10, 0, 0); |
| 42 | + Eigen::Isometry3d far_b{ Eigen::Isometry3d::Identity() }; |
| 43 | + far_b.translation() = Eigen::Vector3d(10, 0, 0); |
| 44 | + checker.setCollisionObjectsTransform(ids, tesseract::common::VectorIsometry3d{ far_a, far_b }); |
| 45 | + |
| 46 | + ContactResultMap result; |
| 47 | + checker.contactTest(result, ContactRequest(ContactTestType::ALL)); |
| 48 | + EXPECT_TRUE(result.empty()); |
| 49 | + |
| 50 | + // Overlapping, set in one call |
| 51 | + Eigen::Isometry3d near_a{ Eigen::Isometry3d::Identity() }; |
| 52 | + near_a.translation() = Eigen::Vector3d(-0.25, 0, 0); |
| 53 | + Eigen::Isometry3d near_b{ Eigen::Isometry3d::Identity() }; |
| 54 | + near_b.translation() = Eigen::Vector3d(0.25, 0, 0); |
| 55 | + checker.setCollisionObjectsTransform(ids, tesseract::common::VectorIsometry3d{ near_a, near_b }); |
| 56 | + |
| 57 | + result.clear(); |
| 58 | + checker.contactTest(result, ContactRequest(ContactTestType::ALL)); |
| 59 | + EXPECT_FALSE(result.empty()); |
| 60 | + |
| 61 | + EXPECT_TRUE(checker.getCollisionObjectsTransform(tesseract::common::LinkId("box_a")).isApprox(near_a, 1e-8)); |
| 62 | + EXPECT_TRUE(checker.getCollisionObjectsTransform(tesseract::common::LinkId("box_b")).isApprox(near_b, 1e-8)); |
| 63 | +} |
| 64 | +} // namespace |
| 65 | + |
| 66 | +// Two boxes are moved into contact with a single parallel-array call. If the array setter does not apply its |
| 67 | +// updates to the broadphase, contactTest sees the stale poses and reports nothing: contactTest does not refresh |
| 68 | +// the broadphase itself. |
| 69 | +TEST(CoalBatchedTransformUnit, DiscreteArraySetterUpdatesBroadphase) // NOLINT |
| 70 | +{ |
| 71 | + runOnePoseArraySetterTest<tesseract_collision_coal::CoalDiscreteBVHManager>(); |
| 72 | +} |
| 73 | + |
| 74 | +// A cast sweep set through the array setter must produce the same contact as the same sweep set one object at a time. |
| 75 | +TEST(CoalBatchedTransformUnit, CastArraySetterUpdatesBroadphase) // NOLINT |
| 76 | +{ |
| 77 | + tesseract_collision_coal::CoalCastBVHManager checker; |
| 78 | + addBox(checker, tesseract::common::LinkId("box_a")); |
| 79 | + addBox(checker, tesseract::common::LinkId("box_b")); |
| 80 | + checker.setActiveCollisionObjects({ tesseract::common::LinkId("box_a") }); |
| 81 | + checker.setDefaultCollisionMargin(0.0); |
| 82 | + |
| 83 | + Eigen::Isometry3d start{ Eigen::Isometry3d::Identity() }; |
| 84 | + start.translation() = Eigen::Vector3d(-5, 0, 0); |
| 85 | + Eigen::Isometry3d end{ Eigen::Isometry3d::Identity() }; |
| 86 | + end.translation() = Eigen::Vector3d(5, 0, 0); |
| 87 | + |
| 88 | + const std::vector<tesseract::common::LinkId> ids{ "box_a" }; |
| 89 | + checker.setCollisionObjectsTransform( |
| 90 | + ids, tesseract::common::VectorIsometry3d{ start }, tesseract::common::VectorIsometry3d{ end }); |
| 91 | + |
| 92 | + ContactResultMap result; |
| 93 | + checker.contactTest(result, ContactRequest(ContactTestType::ALL)); |
| 94 | + EXPECT_FALSE(result.empty()); // the swept box passes through box_b at the origin |
| 95 | +} |
| 96 | + |
| 97 | +// The cast manager's one-pose array form positions objects without a sweep; it must apply its updates to the |
| 98 | +// broadphase just like the discrete form, with a zero-length sweep reporting the same overlap. |
| 99 | +TEST(CoalBatchedTransformUnit, CastOnePoseArraySetterUpdatesBroadphase) // NOLINT |
| 100 | +{ |
| 101 | + runOnePoseArraySetterTest<tesseract_collision_coal::CoalCastBVHManager>(); |
| 102 | +} |
| 103 | + |
| 104 | +// Unregistered ids are skipped, exactly as the single-object setter skips them. |
| 105 | +TEST(CoalBatchedTransformUnit, DiscreteArraySetterSkipsUnknownIds) // NOLINT |
| 106 | +{ |
| 107 | + tesseract_collision_coal::CoalDiscreteBVHManager checker; |
| 108 | + addBox(checker, tesseract::common::LinkId("box_a")); |
| 109 | + |
| 110 | + Eigen::Isometry3d pose{ Eigen::Isometry3d::Identity() }; |
| 111 | + pose.translation() = Eigen::Vector3d(1, 2, 3); |
| 112 | + const std::vector<tesseract::common::LinkId> ids{ "box_a", "does_not_exist" }; |
| 113 | + |
| 114 | + EXPECT_NO_THROW(checker.setCollisionObjectsTransform( // NOLINT |
| 115 | + ids, |
| 116 | + tesseract::common::VectorIsometry3d{ pose, Eigen::Isometry3d::Identity() })); |
| 117 | + EXPECT_TRUE(checker.getCollisionObjectsTransform(tesseract::common::LinkId("box_a")).isApprox(pose, 1e-8)); |
| 118 | +} |
| 119 | + |
| 120 | +// The size check lives in the base class; an override must not drop it. |
| 121 | +TEST(CoalBatchedTransformUnit, ArraySetterSizeMismatchThrows) // NOLINT |
| 122 | +{ |
| 123 | + tesseract_collision_coal::CoalDiscreteBVHManager discrete; |
| 124 | + tesseract_collision_coal::CoalCastBVHManager cast; |
| 125 | + const std::vector<tesseract::common::LinkId> ids{ "box_a", "box_b" }; |
| 126 | + const tesseract::common::VectorIsometry3d poses{ Eigen::Isometry3d::Identity() }; |
| 127 | + |
| 128 | + EXPECT_THROW(discrete.setCollisionObjectsTransform(ids, poses), std::runtime_error); // NOLINT |
| 129 | + EXPECT_THROW(cast.setCollisionObjectsTransform(ids, poses), std::runtime_error); // NOLINT |
| 130 | + EXPECT_THROW(cast.setCollisionObjectsTransform(ids, poses, poses), std::runtime_error); // NOLINT |
| 131 | +} |
| 132 | + |
| 133 | +int main(int argc, char** argv) |
| 134 | +{ |
| 135 | + testing::InitGoogleTest(&argc, argv); |
| 136 | + return RUN_ALL_TESTS(); |
| 137 | +} |
0 commit comments