Skip to content

Commit 8f818f6

Browse files
Add AxisAlignedBox getters for all relevant geometries
* Mesh is a special case since it forwards the calculations for the caller. If callback is not set, the return is nullptr * This has been done since this library does not depend on gz-common * Geometry types that do not have a volume, return nullptr.
1 parent 7ea246d commit 8f818f6

24 files changed

+427
-0
lines changed

include/sdf/Box.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <gz/math/Box.hh>
2323
#include <gz/math/Vector3.hh>
2424
#include <gz/math/Inertial.hh>
25+
#include <gz/math/AxisAlignedBox.hh>
2526
#include <gz/utils/ImplPtr.hh>
2627
#include <sdf/Error.hh>
2728
#include <sdf/Element.hh>
@@ -76,6 +77,10 @@ namespace sdf
7677
public: std::optional<gz::math::Inertiald>
7778
CalculateInertial(double _density);
7879

80+
/// \brief Get the Axis-aligned box for this Box.
81+
/// \return A gz::math::AxisAlignedBox object.
82+
public: gz::math::AxisAlignedBox AxisAlignedBox() const;
83+
7984
/// \brief Create and return an SDF element filled with data from this
8085
/// box.
8186
/// Note that parameter passing functionality is not captured with this

include/sdf/Capsule.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <gz/math/Capsule.hh>
2323
#include <gz/math/Inertial.hh>
24+
#include <gz/math/AxisAlignedBox.hh>
2425
#include <gz/utils/ImplPtr.hh>
2526
#include <sdf/Error.hh>
2627
#include <sdf/Element.hh>
@@ -83,6 +84,10 @@ namespace sdf
8384
public: std::optional<gz::math::Inertiald> CalculateInertial(
8485
double _density);
8586

87+
/// \brief Get the Axis-aligned box for this Capsule.
88+
/// \return A gz::math::AxisAlignedBox object.
89+
public: gz::math::AxisAlignedBox AxisAlignedBox() const;
90+
8691
/// \brief Create and return an SDF element filled with data from this
8792
/// capsule.
8893
/// Note that parameter passing functionality is not captured with this

include/sdf/Cone.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <gz/math/Cone.hh>
2525
#include <gz/math/Inertial.hh>
26+
#include <gz/math/AxisAlignedBox.hh>
2627
#include <gz/utils/ImplPtr.hh>
2728
#include <sdf/Error.hh>
2829
#include <sdf/Element.hh>
@@ -85,6 +86,10 @@ namespace sdf
8586
public: std::optional<gz::math::Inertiald>
8687
CalculateInertial(double _density);
8788

89+
/// \brief Get the Axis-aligned box for this Cone.
90+
/// \return A gz::math::AxisAlignedBox object.
91+
public: gz::math::AxisAlignedBox AxisAlignedBox() const;
92+
8893
/// \brief Create and return an SDF element filled with data from this
8994
/// cone.
9095
/// Note that parameter passing functionality is not captured with this

include/sdf/Cylinder.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <gz/math/Cylinder.hh>
2323
#include <gz/math/Inertial.hh>
24+
#include <gz/math/AxisAlignedBox.hh>
2425
#include <gz/utils/ImplPtr.hh>
2526
#include <sdf/Error.hh>
2627
#include <sdf/Element.hh>
@@ -83,6 +84,10 @@ namespace sdf
8384
public: std::optional<gz::math::Inertiald>
8485
CalculateInertial(double _density);
8586

87+
/// \brief Get the Axis-aligned box for this Cylinder.
88+
/// \return A gz::math::AxisAlignedBox object.
89+
public: gz::math::AxisAlignedBox AxisAlignedBox() const;
90+
8691
/// \brief Create and return an SDF element filled with data from this
8792
/// cylinder.
8893
/// Note that parameter passing functionality is not captured with this

include/sdf/Ellipsoid.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <gz/math/Inertial.hh>
2323
#include <gz/math/Ellipsoid.hh>
24+
#include <gz/math/AxisAlignedBox.hh>
2425
#include <gz/utils/ImplPtr.hh>
2526
#include <sdf/Error.hh>
2627
#include <sdf/Element.hh>
@@ -75,6 +76,10 @@ namespace sdf
7576
public: std::optional<gz::math::Inertiald>
7677
CalculateInertial(double _density);
7778

79+
/// \brief Get the Axis-aligned box for this Ellipsoid.
80+
/// \return A gz::math::AxisAlignedBox object.
81+
public: gz::math::AxisAlignedBox AxisAlignedBox() const;
82+
7883
/// \brief Create and return an SDF element filled with data from this
7984
/// ellipsoid.
8085
/// Note that parameter passing functionality is not captured with this

include/sdf/Geometry.hh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <gz/utils/ImplPtr.hh>
2424
#include <gz/math/Inertial.hh>
25+
#include <gz/math/AxisAlignedBox.hh>
2526
#include <sdf/Error.hh>
2627
#include <sdf/Element.hh>
2728
#include <sdf/config.hh>
@@ -239,6 +240,13 @@ namespace sdf
239240
sdf::Errors &_errors, const ParserConfig &_config,
240241
double _density, sdf::ElementPtr _autoInertiaParams);
241242

243+
/// \brief Calculate and return the AxisAlignedBox for the Geometry
244+
/// @param _meshAabbCalculator The function to calculate the AABB of a mesh
245+
/// @return std::optional with gz::math::AxisAlignedBox object or std::nullopt
246+
/// if the geometry type does not support AABB calculation
247+
public: std::optional<gz::math::AxisAlignedBox> AxisAlignedBox(
248+
Mesh::AxisAlignedBoxCalculator _meshAabbCalculator) const;
249+
242250
/// \brief Get a pointer to the SDF element that was used during
243251
/// load.
244252
/// \return SDF element pointer. The value will be nullptr if Load has

include/sdf/Mesh.hh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <gz/math/Vector3.hh>
2424
#include <gz/math/Inertial.hh>
25+
#include <gz/math/AxisAlignedBox.hh>
2526
#include <gz/utils/ImplPtr.hh>
2627
#include <sdf/CustomInertiaCalcProperties.hh>
2728
#include <sdf/Element.hh>
@@ -94,6 +95,9 @@ namespace sdf
9495
/// Geometry.
9596
class SDFORMAT_VISIBLE Mesh
9697
{
98+
public: using AxisAlignedBoxCalculator =
99+
std::function<gz::math::AxisAlignedBox(const sdf::Mesh &_sdfMesh)>;
100+
97101
/// \brief Constructor
98102
public: Mesh();
99103

@@ -206,6 +210,14 @@ namespace sdf
206210
const sdf::ElementPtr _autoInertiaParams,
207211
const ParserConfig &_config);
208212

213+
/// \brief Get the Axis-aligned box for this Mesh.
214+
/// \param[out] _errors A vector of Errors object. Each object
215+
/// would contain an error code and an error message.
216+
/// \param[in] _config Parser Configuration object.
217+
/// \return A gz::math::AxisAlignedBox object.
218+
public: std::optional<gz::math::AxisAlignedBox>
219+
AxisAlignedBox(AxisAlignedBoxCalculator _meshAabbCalculator) const;
220+
209221
/// \brief Get a pointer to the SDF element that was used during load.
210222
/// \return SDF element pointer. The value will be nullptr if Load has
211223
/// not been called.

include/sdf/Sphere.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <gz/math/Inertial.hh>
2323
#include <gz/math/Sphere.hh>
24+
#include <gz/math/AxisAlignedBox.hh>
2425
#include <gz/utils/ImplPtr.hh>
2526

2627
#include <sdf/Error.hh>
@@ -76,6 +77,10 @@ namespace sdf
7677
public: std::optional<gz::math::Inertiald>
7778
CalculateInertial(double _density);
7879

80+
/// \brief Get the Axis-aligned box for this Sphere.
81+
/// \return A gz::math::AxisAlignedBox object.
82+
public: gz::math::AxisAlignedBox AxisAlignedBox() const;
83+
7984
/// \brief Create and return an SDF element filled with data from this
8085
/// sphere.
8186
/// Note that parameter passing functionality is not captured with this

src/Box.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ std::optional<gz::math::Inertiald> Box::CalculateInertial(double _density)
140140
}
141141
}
142142

143+
/////////////////////////////////////////////////
144+
gz::math::AxisAlignedBox Box::AxisAlignedBox() const
145+
{
146+
auto halfSize = this->Size() / 2;
147+
return gz::math::AxisAlignedBox(-halfSize, halfSize);
148+
}
149+
143150
/////////////////////////////////////////////////
144151
sdf::ElementPtr Box::ToElement() const
145152
{

src/Box_TEST.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,15 @@ TEST(DOMBox, ToElementErrorOutput)
246246
// Check nothing has been printed
247247
EXPECT_TRUE(buffer.str().empty()) << buffer.str();
248248
}
249+
250+
/////////////////////////////////////////////////
251+
TEST(DOMBox, AxisAlignedBox)
252+
{
253+
sdf::Box box;
254+
box.SetSize(gz::math::Vector3d(1, 2, 3));
255+
256+
auto aabb = box.AxisAlignedBox();
257+
EXPECT_EQ(box.Size(), aabb.Size());
258+
EXPECT_EQ(gz::math::Vector3d(-0.5, -1, -1.5), aabb.Min());
259+
EXPECT_EQ(gz::math::Vector3d(0.5, 1, 1.5), aabb.Max());
260+
}

0 commit comments

Comments
 (0)