Skip to content

Commit fc7bab1

Browse files
Update AxisAlignedBoxCalculator return type to be optional
Signed-off-by: Gabriel Pacheco <[email protected]>
1 parent 0c5b51f commit fc7bab1

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

Diff for: include/sdf/Mesh.hh

+3-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ namespace sdf
9696
class SDFORMAT_VISIBLE Mesh
9797
{
9898
public: using AxisAlignedBoxCalculator =
99-
std::function<gz::math::AxisAlignedBox(const sdf::Mesh &_sdfMesh)>;
99+
std::function<std::optional<gz::math::AxisAlignedBox>(
100+
const sdf::Mesh &_sdfMesh)>;
100101

101102
/// \brief Constructor
102103
public: Mesh();
@@ -214,7 +215,7 @@ namespace sdf
214215
/// \param[in] _aabbCalc A custom function that calculates the
215216
/// AxisAlignedBox for the Mesh.
216217
/// \return A gz::math::AxisAlignedBox object centered at this
217-
/// Mesh's origin.
218+
/// Mesh's origin or std::nullopt.
218219
public: std::optional<gz::math::AxisAlignedBox>
219220
AxisAlignedBox(const AxisAlignedBoxCalculator &_aabbCalc) const;
220221

Diff for: python/test/pyGeometry_TEST.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from gz_test_deps.math import AxisAlignedBox, Inertiald, MassMatrix3d, Pose3d, Vector3d, Vector2d
1919
import gz_test_deps.sdformat as sdf
2020
import math
21+
from typing import Optional
2122
import unittest
2223

2324

@@ -461,7 +462,7 @@ def test_axis_aligned_box(self):
461462

462463
self.assertEqual(None, geom.axis_aligned_box(None))
463464

464-
def mesh_aabb_calulator(sdf_mesh: Mesh) -> AxisAlignedBox:
465+
def mesh_aabb_calulator(sdf_mesh: Mesh) -> Optional[AxisAlignedBox]:
465466
return AxisAlignedBox(Vector3d(-1, -1, -1), Vector3d(1, 1, 1))
466467

467468
self.assertEqual(

Diff for: python/test/pyMesh_TEST.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from gz_test_deps.sdformat import Mesh, ConvexDecomposition
1717
from gz_test_deps.math import AxisAlignedBox, Vector3d
1818
import gz_test_deps.sdformat as sdf
19+
from typing import Optional
1920
import unittest
2021

2122

@@ -164,13 +165,21 @@ def test_axis_aligned_box(self):
164165
self.assertEqual(None, mesh.axis_aligned_box(None))
165166

166167
# Customly defined Mesh AABB calculator
167-
def mesh_aabb_calulator(sdf_mesh: Mesh) -> AxisAlignedBox:
168+
def mesh_aabb_calulator(sdf_mesh: Mesh) -> Optional[AxisAlignedBox]:
169+
if sdf_mesh.uri() == "no_mesh":
170+
return None
171+
168172
if sdf_mesh.uri() == "banana":
169173
# Banana mesh should return invalid AABB
170174
return AxisAlignedBox()
171175

172176
return AxisAlignedBox(Vector3d(-1, -1, -1), Vector3d(1, 1, 1))
173177

178+
mesh.set_uri("no_mesh")
179+
self.assertEqual(
180+
None,
181+
mesh.axis_aligned_box(mesh_aabb_calulator))
182+
174183
mesh.set_uri("banana")
175184
self.assertEqual(
176185
AxisAlignedBox(),

Diff for: src/Geometry_TEST.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,8 @@ TEST(DOMGeometry, AxisAlignedBox)
12951295
auto aabb = geom.AxisAlignedBox(nullptr);
12961296
EXPECT_FALSE(aabb.has_value());
12971297

1298-
auto meshAabbCalculator = [](const sdf::Mesh &) -> gz::math::AxisAlignedBox
1298+
auto meshAabbCalculator = [](const sdf::Mesh &) ->
1299+
std::optional<gz::math::AxisAlignedBox>
12991300
{
13001301
return gz::math::AxisAlignedBox(
13011302
gz::math::Vector3d(-1, -2, -3),

Diff for: src/Mesh_TEST.cc

+12-3
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,13 @@ TEST(DOMMesh, AxisAlignedBox)
443443
EXPECT_EQ(std::nullopt, mesh.AxisAlignedBox(nullptr));
444444

445445
auto customCalculator = [](
446-
const sdf::Mesh &_mesh) -> gz::math::AxisAlignedBox
446+
const sdf::Mesh &_mesh) -> std::optional<gz::math::AxisAlignedBox>
447447
{
448+
if(_mesh.Uri() == "no_mesh")
449+
{
450+
return std::nullopt;
451+
}
452+
448453
if (_mesh.Uri() == "banana")
449454
{
450455
// Invalid mesh leads to empty AABB
@@ -459,16 +464,20 @@ TEST(DOMMesh, AxisAlignedBox)
459464
);
460465
};
461466

462-
// Set to valid "apple" mesh
467+
// "apple" mesh should return valid aabb
463468
mesh.SetUri("apple");
464469
auto aabb = mesh.AxisAlignedBox(customCalculator);
465470
EXPECT_TRUE(aabb.has_value());
466471
EXPECT_EQ(gz::math::Vector3d(1, 2, 3), aabb->Min());
467472
EXPECT_EQ(gz::math::Vector3d(4, 5, 6), aabb->Max());
468473

469-
// Set to invalid "banana" mesh
474+
// "banana" mesh should return invalid aabb
470475
mesh.SetUri("banana");
471476
aabb = mesh.AxisAlignedBox(customCalculator);
472477
EXPECT_TRUE(aabb.has_value());
473478
EXPECT_EQ(gz::math::AxisAlignedBox(), aabb.value());
479+
480+
// "no_mesh" should return std::nullopt
481+
mesh.SetUri("no_mesh");
482+
EXPECT_EQ(std::nullopt, mesh.AxisAlignedBox(customCalculator));
474483
}

0 commit comments

Comments
 (0)