Skip to content

Commit 5b88d09

Browse files
Return std::optional to indicate mesh AABB cannot be computed
* Also fix `_sdfMesh` to be `const &` Signed-off-by: Gabriel Pacheco <[email protected]>
1 parent e3fdde0 commit 5b88d09

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

include/gz/sim/Util.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ namespace gz
339339
/// \brief Compute the axis-aligned bounding box of a mesh.
340340
/// \param _sdfMesh Mesh SDF DOM.
341341
/// \return The AABB of the mesh in its local frame.
342-
GZ_SIM_VISIBLE math::AxisAlignedBox meshAxisAlignedBox(
343-
const sdf::Mesh _sdfMesh);
342+
GZ_SIM_VISIBLE std::optional<math::AxisAlignedBox> meshAxisAlignedBox(
343+
const sdf::Mesh &_sdfMesh);
344344

345345
/// \brief Environment variable holding resource paths.
346346
const std::string kResourcePathEnv{"GZ_SIM_RESOURCE_PATH"};

src/Util.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -989,15 +989,16 @@ const common::Mesh *optimizeMesh(const sdf::Mesh &_meshSdf,
989989
return optimizedMesh;
990990
}
991991

992-
math::AxisAlignedBox meshAxisAlignedBox(sdf::Mesh _sdfMesh)
992+
std::optional<math::AxisAlignedBox> meshAxisAlignedBox(
993+
const sdf::Mesh &_sdfMesh)
993994
{
994995
auto mesh = loadMesh(_sdfMesh);
995996
if (!mesh)
996997
{
997-
gzwarn << "Mesh could not be loaded. Invalidating its bounding box."
998+
gzwarn << "Mesh could not be loaded. Bounding box cannot be computed."
998999
<< std::endl;
9991000

1000-
return math::AxisAlignedBox();
1001+
return std::nullopt;
10011002
}
10021003

10031004
// Get the mesh's bounding box

src/Util_TEST.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,14 +1052,14 @@ TEST_F(UtilTest, MeshAxisAlignedBoundingBox)
10521052
{
10531053
sdf::Mesh meshSdf;
10541054
math::AxisAlignedBox emptyBox, aab;
1055-
EXPECT_EQ(emptyBox, meshAxisAlignedBox(meshSdf));
1055+
EXPECT_FALSE(meshAxisAlignedBox(meshSdf).has_value());
10561056

10571057
meshSdf.SetUri("invalid_uri");
10581058
meshSdf.SetFilePath("invalid_filepath");
1059-
EXPECT_EQ(emptyBox, meshAxisAlignedBox(meshSdf));
1059+
EXPECT_FALSE(meshAxisAlignedBox(meshSdf).has_value());
10601060

10611061
meshSdf.SetUri("name://unit_box");
1062-
aab = meshAxisAlignedBox(meshSdf);
1062+
aab = meshAxisAlignedBox(meshSdf).value();
10631063
EXPECT_NE(emptyBox, aab);
10641064
EXPECT_EQ(aab.Size(), math::Vector3d::One);
10651065
EXPECT_EQ(aab.Min(), math::Vector3d(-0.5, -0.5, -0.5));
@@ -1068,7 +1068,7 @@ TEST_F(UtilTest, MeshAxisAlignedBoundingBox)
10681068

10691069
// Validate scaling using the unit box mesh
10701070
meshSdf.SetScale(math::Vector3d(2, 3, 4));
1071-
aab = meshAxisAlignedBox(meshSdf);
1071+
aab = meshAxisAlignedBox(meshSdf).value();
10721072
EXPECT_NE(emptyBox, aab);
10731073
EXPECT_EQ(aab.Size(), math::Vector3d(2, 3, 4));
10741074
EXPECT_EQ(aab.Min(), math::Vector3d(-1, -1.5, -2));
@@ -1080,7 +1080,7 @@ TEST_F(UtilTest, MeshAxisAlignedBoundingBox)
10801080
std::string filePath = common::joinPaths(std::string(PROJECT_SOURCE_PATH),
10811081
"test", "media", "duck.dae");
10821082
meshSdf.SetFilePath(filePath);
1083-
aab = meshAxisAlignedBox(meshSdf);
1083+
aab = meshAxisAlignedBox(meshSdf).value();
10841084
EXPECT_NE(emptyBox, aab);
10851085

10861086
// Expected values obtained from the mesh file using Blender:

0 commit comments

Comments
 (0)