Skip to content

Commit

Permalink
add enum, expand sdf doc
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 committed Mar 14, 2024
1 parent 37a2ab3 commit 64d7c97
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 17 deletions.
27 changes: 25 additions & 2 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,14 +72,26 @@ 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 Simplification() const;
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.
public: void SetSimplification(const std::string &_simplifcation);
/// \return True if the _simplificationStr parameter matched a known
/// mesh simplification method. False if the mesh simplification method
/// could not be set.
public: bool SetSimplification(const std::string &_simplifcationStr);

/// \brief Get the mesh's URI.
/// \return The URI of the mesh data.
Expand Down
2 changes: 1 addition & 1 deletion sdf/1.11/mesh_shape.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<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", "convex_decomposition". Default value is an empty string which means no mesh simplification.
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>

Expand Down
44 changes: 38 additions & 6 deletions src/Mesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@

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: std::string simplification;
public: MeshSimplification simplification = MeshSimplification::NONE;

/// \brief The mesh's URI.
public: std::string uri = "";
Expand Down Expand Up @@ -93,8 +103,7 @@ Errors Mesh::Load(ElementPtr _sdf, const ParserConfig &_config)
// Simplify
if (_sdf->HasAttribute("simplification"))
{
this->dataPtr->simplification = _sdf->Get<std::string>("simplification",
this->dataPtr->simplification).first;
this->SetSimplification(_sdf->Get<std::string>("simplification", "").first);
}

if (_sdf->HasElement("uri"))
Expand Down Expand Up @@ -151,13 +160,36 @@ sdf::ElementPtr Mesh::Element() const
}

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

//////////////////////////////////////////////////
void Mesh::SetSimplification(const std::string &_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 174 in src/Mesh.cc

View check run for this annotation

Codecov / codecov/patch

src/Mesh.cc#L174

Added line #L174 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 188 in src/Mesh.cc

View check run for this annotation

Codecov / codecov/patch

src/Mesh.cc#L188

Added line #L188 was not covered by tests
}

//////////////////////////////////////////////////
void Mesh::SetSimplification(MeshSimplification _simplification)
{
this->dataPtr->simplification = _simplification;
}
Expand Down Expand Up @@ -268,7 +300,7 @@ sdf::ElementPtr Mesh::ToElement(sdf::Errors &_errors) const

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

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

EXPECT_EQ(std::string(), mesh.Simplification());
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 @@ -54,7 +55,8 @@ TEST(DOMMesh, MoveConstructor)
mesh.SetFilePath("/pear");

sdf::Mesh mesh2(std::move(mesh));
EXPECT_EQ("convex_hull", mesh2.Simplification());
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 @@ -74,7 +76,8 @@ TEST(DOMMesh, CopyConstructor)
mesh.SetFilePath("/pear");

sdf::Mesh mesh2(mesh);
EXPECT_EQ("convex_hull", mesh2.Simplification());
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 @@ -95,7 +98,8 @@ TEST(DOMMesh, CopyAssignmentOperator)

sdf::Mesh mesh2;
mesh2 = mesh;
EXPECT_EQ("convex_hull", mesh2.Simplification());
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 @@ -116,7 +120,8 @@ TEST(DOMMesh, MoveAssignmentOperator)

sdf::Mesh mesh2;
mesh2 = std::move(mesh);
EXPECT_EQ("convex_hull", mesh2.Simplification());
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 @@ -149,9 +154,14 @@ TEST(DOMMesh, Set)
sdf::Mesh mesh;
EXPECT_EQ(nullptr, mesh.Element());

EXPECT_EQ(std::string(), mesh.Simplification());
EXPECT_EQ(std::string(), mesh.SimplificationStr());
mesh.SetSimplification("convex_hull");
EXPECT_EQ("convex_hull", mesh.Simplification());
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");
Expand Down Expand Up @@ -321,6 +331,7 @@ 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());
Expand Down Expand Up @@ -361,6 +372,7 @@ 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());
Expand Down
4 changes: 3 additions & 1 deletion test/integration/geometry_dom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ 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->Simplification());
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());
Expand Down

0 comments on commit 64d7c97

Please sign in to comment.