Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mesh optimization attribute to <mesh> #1382

Merged
merged 7 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions include/sdf/Mesh.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ namespace sdf
// Forward declarations.
class ParserConfig;

/// \brief Mesh simplification method
enum class MeshSimplification
{
/// \brief No mesh simplification
NONE,
/// \brief Convex hull
CONVEX_HULL,
/// \brief Convex decomposition
CONVEX_DECOMPOSITION
};

/// \brief Mesh represents a mesh shape, and is usually accessed through a
/// Geometry.
class SDFORMAT_VISIBLE Mesh
Expand All @@ -61,6 +72,27 @@ namespace sdf
/// an error code and message. An empty vector indicates no error.
public: Errors Load(sdf::ElementPtr _sdf, const ParserConfig &_config);

/// \brief Get the mesh's simplifcation method
/// \return The mesh simplification method.
/// MeshSimplification::NONE if no mesh simplificaton is done.
public: MeshSimplification Simplification() const;

/// \brief Get the mesh's simplifcation method
/// \return The mesh simplification method.
/// Empty string if no mesh simplificaton is done.
public: std::string SimplificationStr() const;

/// \brief Set the mesh simplification method.
/// \param[in] _simplifcation The mesh simplification method.
public: void SetSimplification(MeshSimplification _simplifcation);

/// \brief Set the mesh simplification method.
/// \param[in] _simplifcation The mesh simplification method.
/// \return True if the _simplificationStr parameter matched a known
/// mesh simplification method. False if the mesh simplification method
/// could not be set.
iche033 marked this conversation as resolved.
Show resolved Hide resolved
public: bool SetSimplification(const std::string &_simplifcationStr);

/// \brief Get the mesh's URI.
/// \return The URI of the mesh data.
public: std::string Uri() const;
Expand Down
7 changes: 7 additions & 0 deletions sdf/1.11/mesh_shape.sdf
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<element name="mesh" required="0">
<description>Mesh shape</description>

<attribute name="simplification" type="string" default="" required="0">
<description>
Set whether to simplify the mesh using one of the specified methods. Values include: "convex_hull" - a single convex hull that encapsulates the mesh, "convex_decomposition" - decompose the mesh into multiple convex hull meshes. Default value is an empty string which means no mesh simplification.
</description>
</attribute>

<element name="uri" type="string" default="__default__" required="1">
<description>Mesh uri</description>
</element>
Expand Down
7 changes: 7 additions & 0 deletions sdf/1.12/mesh_shape.sdf
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<element name="mesh" required="0">
<description>Mesh shape</description>

<attribute name="simplification" type="string" default="" required="0">
<description>
Set whether to simplify the mesh using one of the specified methods. Values include: "convex_hull" - a single convex hull that encapsulates the mesh, "convex_decomposition" - decompose the mesh into multiple convex hull meshes. Default value is an empty string which means no mesh simplification.
</description>
</attribute>

<element name="uri" type="string" default="__default__" required="1">
<description>Mesh uri</description>
</element>
Expand Down
60 changes: 60 additions & 0 deletions src/Mesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
* limitations under the License.
*
*/
#include <array>
#include <filesystem>
#include <optional>
#include <string_view>

#include <gz/math/Inertial.hh>
#include "sdf/CustomInertiaCalcProperties.hh"
Expand All @@ -27,9 +29,22 @@

using namespace sdf;

/// Mesh Simplification method strings. These should match the data in
/// `enum class MeshSimplification` located in Mesh.hh, and the size
/// template parameter should match the number of elements as well.
constexpr std::array<const std::string_view, 3> kMeshSimplificationStrs =
{
"",
"convex_hull",
"convex_decomposition"
};

// Private data class
class sdf::Mesh::Implementation
{
/// \brief Mesh simplification method
public: MeshSimplification simplification = MeshSimplification::NONE;

/// \brief The mesh's URI.
public: std::string uri = "";

Expand Down Expand Up @@ -87,6 +102,12 @@
return errors;
}

// Simplify
if (_sdf->HasAttribute("simplification"))
{
this->SetSimplification(_sdf->Get<std::string>("simplification", "").first);
}

if (_sdf->HasElement("uri"))
{
std::unordered_set<std::string> paths;
Expand Down Expand Up @@ -140,6 +161,41 @@
return this->dataPtr->sdf;
}

//////////////////////////////////////////////////
MeshSimplification Mesh::Simplification() const
{
return this->dataPtr->simplification;
}

//////////////////////////////////////////////////
std::string Mesh::SimplificationStr() const
{
size_t index = static_cast<int>(this->dataPtr->simplification);
if (index < kMeshSimplificationStrs.size())
return std::string(kMeshSimplificationStrs[index]);
return "";

Check warning on line 176 in src/Mesh.cc

View check run for this annotation

Codecov / codecov/patch

src/Mesh.cc#L176

Added line #L176 was not covered by tests
}

//////////////////////////////////////////////////
bool Mesh::SetSimplification(const std::string &_simplificationStr)
{
for (size_t i = 0; i < kMeshSimplificationStrs.size(); ++i)
{
if (_simplificationStr == kMeshSimplificationStrs[i])
{
this->dataPtr->simplification = static_cast<MeshSimplification>(i);
return true;
}
}
return false;

Check warning on line 190 in src/Mesh.cc

View check run for this annotation

Codecov / codecov/patch

src/Mesh.cc#L190

Added line #L190 was not covered by tests
}

//////////////////////////////////////////////////
void Mesh::SetSimplification(MeshSimplification _simplification)
{
this->dataPtr->simplification = _simplification;
}

//////////////////////////////////////////////////
std::string Mesh::Uri() const
{
Expand Down Expand Up @@ -244,6 +300,10 @@
sdf::ElementPtr elem(new sdf::Element);
sdf::initFile("mesh_shape.sdf", elem);

// Simplification
elem->GetAttribute("simplification")->Set<std::string>(
this->SimplificationStr());

// Uri
sdf::ElementPtr uriElem = elem->GetElement("uri", _errors);
uriElem->Set(_errors, this->Uri());
Expand Down
29 changes: 29 additions & 0 deletions src/Mesh_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ TEST(DOMMesh, Construction)
sdf::Mesh mesh;
EXPECT_EQ(nullptr, mesh.Element());

EXPECT_EQ(std::string(), mesh.SimplificationStr());
EXPECT_EQ(sdf::MeshSimplification::NONE, mesh.Simplification());
EXPECT_EQ(std::string(), mesh.FilePath());
EXPECT_EQ(std::string(), mesh.Uri());
EXPECT_EQ(std::string(), mesh.Submesh());
Expand All @@ -45,13 +47,16 @@ TEST(DOMMesh, Construction)
TEST(DOMMesh, MoveConstructor)
{
sdf::Mesh mesh;
mesh.SetSimplification("convex_hull");
mesh.SetUri("banana");
mesh.SetSubmesh("watermelon");
mesh.SetCenterSubmesh(true);
mesh.SetScale({0.5, 0.6, 0.7});
mesh.SetFilePath("/pear");

sdf::Mesh mesh2(std::move(mesh));
EXPECT_EQ("convex_hull", mesh2.SimplificationStr());
EXPECT_EQ(sdf::MeshSimplification::CONVEX_HULL, mesh2.Simplification());
EXPECT_EQ("banana", mesh2.Uri());
EXPECT_EQ("watermelon", mesh2.Submesh());
EXPECT_EQ(gz::math::Vector3d(0.5, 0.6, 0.7), mesh2.Scale());
Expand All @@ -63,13 +68,16 @@ TEST(DOMMesh, MoveConstructor)
TEST(DOMMesh, CopyConstructor)
{
sdf::Mesh mesh;
mesh.SetSimplification("convex_hull");
mesh.SetUri("banana");
mesh.SetSubmesh("watermelon");
mesh.SetCenterSubmesh(true);
mesh.SetScale({0.5, 0.6, 0.7});
mesh.SetFilePath("/pear");

sdf::Mesh mesh2(mesh);
EXPECT_EQ("convex_hull", mesh2.SimplificationStr());
EXPECT_EQ(sdf::MeshSimplification::CONVEX_HULL, mesh2.Simplification());
EXPECT_EQ("banana", mesh2.Uri());
EXPECT_EQ("watermelon", mesh2.Submesh());
EXPECT_EQ(gz::math::Vector3d(0.5, 0.6, 0.7), mesh2.Scale());
Expand All @@ -81,6 +89,7 @@ TEST(DOMMesh, CopyConstructor)
TEST(DOMMesh, CopyAssignmentOperator)
{
sdf::Mesh mesh;
mesh.SetSimplification("convex_hull");
mesh.SetUri("banana");
mesh.SetSubmesh("watermelon");
mesh.SetCenterSubmesh(true);
Expand All @@ -89,6 +98,8 @@ TEST(DOMMesh, CopyAssignmentOperator)

sdf::Mesh mesh2;
mesh2 = mesh;
EXPECT_EQ("convex_hull", mesh2.SimplificationStr());
EXPECT_EQ(sdf::MeshSimplification::CONVEX_HULL, mesh2.Simplification());
EXPECT_EQ("banana", mesh2.Uri());
EXPECT_EQ("watermelon", mesh2.Submesh());
EXPECT_EQ(gz::math::Vector3d(0.5, 0.6, 0.7), mesh2.Scale());
Expand All @@ -100,6 +111,7 @@ TEST(DOMMesh, CopyAssignmentOperator)
TEST(DOMMesh, MoveAssignmentOperator)
{
sdf::Mesh mesh;
mesh.SetSimplification("convex_hull");
mesh.SetUri("banana");
mesh.SetSubmesh("watermelon");
mesh.SetCenterSubmesh(true);
Expand All @@ -108,6 +120,8 @@ TEST(DOMMesh, MoveAssignmentOperator)

sdf::Mesh mesh2;
mesh2 = std::move(mesh);
EXPECT_EQ("convex_hull", mesh2.SimplificationStr());
EXPECT_EQ(sdf::MeshSimplification::CONVEX_HULL, mesh2.Simplification());
EXPECT_EQ("banana", mesh2.Uri());
EXPECT_EQ("watermelon", mesh2.Submesh());
EXPECT_EQ(gz::math::Vector3d(0.5, 0.6, 0.7), mesh2.Scale());
Expand Down Expand Up @@ -140,6 +154,15 @@ TEST(DOMMesh, Set)
sdf::Mesh mesh;
EXPECT_EQ(nullptr, mesh.Element());

EXPECT_EQ(std::string(), mesh.SimplificationStr());
mesh.SetSimplification("convex_hull");
EXPECT_EQ("convex_hull", mesh.SimplificationStr());
EXPECT_EQ(sdf::MeshSimplification::CONVEX_HULL, mesh.Simplification());
mesh.SetSimplification(sdf::MeshSimplification::CONVEX_DECOMPOSITION);
EXPECT_EQ("convex_decomposition", mesh.SimplificationStr());
EXPECT_EQ(sdf::MeshSimplification::CONVEX_DECOMPOSITION,
mesh.Simplification());

EXPECT_EQ(std::string(), mesh.Uri());
mesh.SetUri("http://myuri.com");
EXPECT_EQ("http://myuri.com", mesh.Uri());
Expand Down Expand Up @@ -296,6 +319,7 @@ TEST(DOMMesh, ToElement)
{
sdf::Mesh mesh;

mesh.SetSimplification("convex_hull");
mesh.SetUri("mesh-uri");
mesh.SetScale(gz::math::Vector3d(1, 2, 3));
mesh.SetSubmesh("submesh");
Expand All @@ -307,6 +331,8 @@ TEST(DOMMesh, ToElement)
sdf::Mesh mesh2;
mesh2.Load(elem);

EXPECT_EQ(mesh.SimplificationStr(), mesh2.SimplificationStr());
EXPECT_EQ(mesh.Simplification(), mesh2.Simplification());
EXPECT_EQ(mesh.Uri(), mesh2.Uri());
EXPECT_EQ(mesh.Scale(), mesh2.Scale());
EXPECT_EQ(mesh.Submesh(), mesh2.Submesh());
Expand All @@ -332,6 +358,7 @@ TEST(DOMMesh, ToElementErrorOutput)
sdf::Mesh mesh;
sdf::Errors errors;

mesh.SetSimplification("convex_hull");
mesh.SetUri("mesh-uri");
mesh.SetScale(gz::math::Vector3d(1, 2, 3));
mesh.SetSubmesh("submesh");
Expand All @@ -345,6 +372,8 @@ TEST(DOMMesh, ToElementErrorOutput)
errors = mesh2.Load(elem);
EXPECT_TRUE(errors.empty());

EXPECT_EQ(mesh.SimplificationStr(), mesh2.SimplificationStr());
EXPECT_EQ(mesh.Simplification(), mesh2.Simplification());
EXPECT_EQ(mesh.Uri(), mesh2.Uri());
EXPECT_EQ(mesh.Scale(), mesh2.Scale());
EXPECT_EQ(mesh.Submesh(), mesh2.Submesh());
Expand Down
4 changes: 4 additions & 0 deletions test/integration/geometry_dom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ TEST(DOMGeometry, Shapes)
EXPECT_EQ(sdf::GeometryType::MESH, meshCol->Geom()->Type());
const sdf::Mesh *meshColGeom = meshCol->Geom()->MeshShape();
ASSERT_NE(nullptr, meshColGeom);
EXPECT_EQ("convex_hull", meshColGeom->SimplificationStr());
EXPECT_EQ(sdf::MeshSimplification::CONVEX_HULL,
meshColGeom->Simplification());

EXPECT_EQ("https://fuel.gazebosim.org/1.0/an_org/models/a_model/mesh/"
"mesh.dae", meshColGeom->Uri());
EXPECT_TRUE(gz::math::Vector3d(0.1, 0.2, 0.3) ==
Expand Down
2 changes: 1 addition & 1 deletion test/sdf/shapes.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@

<collision name="mesh_col">
<geometry>
<mesh>
<mesh simplification="convex_hull">
<uri>https://fuel.gazebosim.org/1.0/an_org/models/a_model/mesh/mesh.dae</uri>
<submesh>
<name>my_submesh</name>
Expand Down
Loading