Skip to content

Commit 0c79183

Browse files
Make link AABB computation from collision shapes public
This could be helpful if users want to explicetly compute the AABB from the link SDF collision shapes. For instance, the Physics system can fallback and use this method in case the selected engine does not support AABB calculation. Signed-off-by: Gabriel Pacheco <gabriel.fpacheco@gmail.com>
1 parent 2885b7a commit 0c79183

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

include/gz/sim/Link.hh

+5-3
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,16 @@ namespace gz
371371

372372
/// \brief Get the link's axis-aligned bounding box in the link frame.
373373
/// \param[in] _ecm Entity-component manager.
374-
/// \return Link's AxisAlignedBox in the link frame or nullopt
374+
/// \return Link's AxisAlignedBox in the link frame or nullopt if the
375+
/// link entity does not have a components::AxisAlignedBox component.
375376
/// \sa EnableBoundingBoxChecks
376377
public: std::optional<math::AxisAlignedBox> AxisAlignedBox(
377378
const EntityComponentManager &_ecm) const;
378379

379380
/// \brief Get the link's axis-aligned bounding box in the world frame.
380381
/// \param[in] _ecm Entity-component manager.
381-
/// \return Link's AxisAlignedBox in the world frame or nullopt
382+
/// \return Link's AxisAlignedBox in the world frame or nullopt if the
383+
/// link entity does not have a components::AxisAlignedBox component.
382384
/// \sa AxisAlignedBox
383385
public: std::optional<math::AxisAlignedBox> WorldAxisAlignedBox(
384386
const EntityComponentManager &_ecm) const;
@@ -392,7 +394,7 @@ namespace gz
392394
/// \return Link's axis-aligned bounding box in the link frame or
393395
/// nullopt if the link does not have collisions. It returns an
394396
/// invalid bounding box if all of the link collisions are empty.
395-
private: std::optional<math::AxisAlignedBox> ComputeAxisAlignedBox(
397+
public: std::optional<math::AxisAlignedBox> ComputeAxisAlignedBox(
396398
const EntityComponentManager &_ecm) const;
397399

398400
/// \brief Pointer to private data.

test/integration/link.cc

+15-13
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,7 @@ TEST_F(LinkIntegrationTest, AxisAlignedBoxInLink)
755755

756756
// LinkA - bounding box from collisions
757757
gz::sim::Link linkA(eLinkA);
758-
linkA.EnableBoundingBoxChecks(ecm);
759-
auto aabb = linkA.AxisAlignedBox(ecm);
758+
auto aabb = linkA.ComputeAxisAlignedBox(ecm);
760759

761760
EXPECT_TRUE(aabb.has_value());
762761
EXPECT_EQ(-5.0, aabb->Min().X());
@@ -766,12 +765,20 @@ TEST_F(LinkIntegrationTest, AxisAlignedBoxInLink)
766765
EXPECT_EQ(4.0, aabb->Max().Y());
767766
EXPECT_EQ(2.0, aabb->Max().Z());
768767

768+
// By default, the bounding box checks are disabled.
769+
// Once enabled, the result must match the computed AABB.
770+
EXPECT_EQ(std::nullopt, linkA.AxisAlignedBox(ecm));
771+
linkA.EnableBoundingBoxChecks(ecm);
772+
EXPECT_EQ(aabb, linkA.AxisAlignedBox(ecm));
773+
769774
// If ECM is updated with new link AABB, the ECM value is returned
775+
// instead of recomputing the AABB from the collisions
770776
math::AxisAlignedBox newAabb(-math::Vector3d::One, math::Vector3d::One);
771777
ecm.SetComponentData<components::AxisAlignedBox>(eLinkA, newAabb);
778+
EXPECT_NE(newAabb, aabb.value());
772779
EXPECT_EQ(newAabb, linkA.AxisAlignedBox(ecm));
773780

774-
// If the bounding box checks are disabled and then enabled again, the
781+
// If the bounding box checks are disabled and then re-enabled, the
775782
// bounding box should be recalculated from the collisions
776783
linkA.EnableBoundingBoxChecks(ecm, false);
777784
EXPECT_EQ(std::nullopt, linkA.AxisAlignedBox(ecm));
@@ -805,8 +812,7 @@ TEST_F(LinkIntegrationTest, AxisAlignedBoxInLink)
805812

806813
// LinkB - bounding box from collisions
807814
gz::sim::Link linkB(eLinkB);
808-
linkB.EnableBoundingBoxChecks(ecm);
809-
aabb = linkB.AxisAlignedBox(ecm);
815+
aabb = linkB.ComputeAxisAlignedBox(ecm);
810816

811817
EXPECT_TRUE(aabb.has_value());
812818
EXPECT_EQ(-1.5, aabb->Min().X());
@@ -824,6 +830,7 @@ TEST_F(LinkIntegrationTest, AxisAlignedBoxInLink)
824830
// LinkC - No collisions (AABB should be invalid if checks are enabled)
825831
gz::sim::Link linkC(eLinkC);
826832
EXPECT_EQ(std::nullopt, linkC.AxisAlignedBox(ecm));
833+
EXPECT_EQ(std::nullopt, linkC.ComputeAxisAlignedBox(ecm));
827834
linkC.EnableBoundingBoxChecks(ecm);
828835
EXPECT_EQ(math::AxisAlignedBox(), linkC.AxisAlignedBox(ecm));
829836

@@ -844,6 +851,7 @@ TEST_F(LinkIntegrationTest, AxisAlignedBoxInLink)
844851
linkC = gz::sim::Link(eLinkC);
845852
linkC.EnableBoundingBoxChecks(ecm, false);
846853
EXPECT_EQ(std::nullopt, linkC.AxisAlignedBox(ecm));
854+
EXPECT_EQ(math::AxisAlignedBox(), linkC.ComputeAxisAlignedBox(ecm));
847855
linkC.EnableBoundingBoxChecks(ecm);
848856
EXPECT_EQ(math::AxisAlignedBox(), linkC.AxisAlignedBox(ecm));
849857

@@ -858,10 +866,7 @@ TEST_F(LinkIntegrationTest, AxisAlignedBoxInLink)
858866
// LinkC - Invalid mesh will be skiped with warning, bounding
859867
// box will be defined for the valid mesh only
860868
linkC = gz::sim::Link(eLinkC);
861-
linkC.EnableBoundingBoxChecks(ecm, false);
862-
EXPECT_EQ(std::nullopt, linkC.AxisAlignedBox(ecm));
863-
linkC.EnableBoundingBoxChecks(ecm);
864-
aabb = linkC.AxisAlignedBox(ecm);
869+
aabb = linkC.ComputeAxisAlignedBox(ecm);
865870

866871
EXPECT_TRUE(aabb.has_value());
867872
EXPECT_EQ(-0.5, aabb->Min().X());
@@ -878,10 +883,7 @@ TEST_F(LinkIntegrationTest, AxisAlignedBoxInLink)
878883
creator.SetParent(eCollisions[0], eLinkB);
879884

880885
linkC = gz::sim::Link(eLinkC);
881-
linkC.EnableBoundingBoxChecks(ecm, false);
882-
EXPECT_EQ(std::nullopt, linkC.AxisAlignedBox(ecm));
883-
linkC.EnableBoundingBoxChecks(ecm);
884-
aabb = linkC.AxisAlignedBox(ecm);
886+
aabb = linkC.ComputeAxisAlignedBox(ecm);
885887

886888
EXPECT_TRUE(aabb.has_value());
887889
EXPECT_EQ(-0.5, aabb->Min().X());

0 commit comments

Comments
 (0)