Skip to content

Commit

Permalink
Merge pull request #1195 from robotology/simplify-shapes-collision-vi…
Browse files Browse the repository at this point in the history
…sual

idyntree-model-simplify-shapes: Add the possibility to simplify only collisions or visuals
  • Loading branch information
traversaro authored Sep 17, 2024
2 parents 147fb75 + 2429fd4 commit bbd8b08
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
15 changes: 13 additions & 2 deletions src/solid-shapes/include/iDynTree/ModelTransformersSolidShapes.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ namespace iDynTree
class Model;
class SensorsList;

enum ApproximateSolidShapesWithPrimitiveShapeConversionType {
enum class ApproximateSolidShapesWithPrimitiveShapeConversionType {
ConvertSolidShapesWithEnclosingAxisAlignedBoundingBoxes
};

/**
* Enum class representing which shape should be approximated.
* Namely teh visual, collision or both shapes.
*/
enum class ApproximateSolidShapesWithPrimitiveShapeShapesToApproximate {
VisualShapes,
CollisionShapes,
BothShapes
};

struct ApproximateSolidShapesWithPrimitiveShapeOptions
{
ApproximateSolidShapesWithPrimitiveShapeConversionType conversionType = ConvertSolidShapesWithEnclosingAxisAlignedBoundingBoxes;
ApproximateSolidShapesWithPrimitiveShapeConversionType conversionType = ApproximateSolidShapesWithPrimitiveShapeConversionType::ConvertSolidShapesWithEnclosingAxisAlignedBoundingBoxes;
ApproximateSolidShapesWithPrimitiveShapeShapesToApproximate shapesToApproximate = ApproximateSolidShapesWithPrimitiveShapeShapesToApproximate::BothShapes;
};

/**
Expand Down
15 changes: 11 additions & 4 deletions src/solid-shapes/src/ModelTransformersSolidShapes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bool approximateSolidShapesWithBoundingBoxes(const iDynTree::ModelSolidShapes& i

bool approximateSolidShapesWithPrimitiveShape(const Model& inputModel,
Model& outputModel,
ApproximateSolidShapesWithPrimitiveShapeOptions /*options*/)
ApproximateSolidShapesWithPrimitiveShapeOptions options)
{
bool retValue = true;

Expand All @@ -50,10 +50,17 @@ bool approximateSolidShapesWithPrimitiveShape(const Model& inputModel,
// that approximates the solid shapes via the iDynTree::computeBoundingBoxFromShape function

// Approximate visual shapes
retValue = retValue && approximateSolidShapesWithBoundingBoxes(inputModel.visualSolidShapes(), outputModel.visualSolidShapes());
if (options.shapesToApproximate == ApproximateSolidShapesWithPrimitiveShapeShapesToApproximate::VisualShapes ||
options.shapesToApproximate == ApproximateSolidShapesWithPrimitiveShapeShapesToApproximate::BothShapes)
{
retValue = retValue && approximateSolidShapesWithBoundingBoxes(inputModel.visualSolidShapes(), outputModel.visualSolidShapes());
}

// Approximate collision shapes
retValue = retValue && approximateSolidShapesWithBoundingBoxes(inputModel.collisionSolidShapes(), outputModel.collisionSolidShapes());
if (options.shapesToApproximate == ApproximateSolidShapesWithPrimitiveShapeShapesToApproximate::CollisionShapes ||
options.shapesToApproximate == ApproximateSolidShapesWithPrimitiveShapeShapesToApproximate::BothShapes)
{
retValue = retValue && approximateSolidShapesWithBoundingBoxes(inputModel.collisionSolidShapes(), outputModel.collisionSolidShapes());
}

return retValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void checkThatOneSphereIsApproximatedToABox()
// Compute the simplified model
iDynTree::Model oneBoxModel;
ApproximateSolidShapesWithPrimitiveShapeOptions options = ApproximateSolidShapesWithPrimitiveShapeOptions();
options.conversionType = ConvertSolidShapesWithEnclosingAxisAlignedBoundingBoxes;
options.conversionType = ApproximateSolidShapesWithPrimitiveShapeConversionType::ConvertSolidShapesWithEnclosingAxisAlignedBoundingBoxes;
bool ok = approximateSolidShapesWithPrimitiveShape(oneSphereModel, oneBoxModel);
ASSERT_IS_TRUE(ok);

Expand Down
30 changes: 28 additions & 2 deletions src/tools/idyntree-model-simplify-shapes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ void addOptions(cmdline::parser &cmd)
cmd.add<std::string>("output-model", 'o',
"Output simplified model.",
true);

// Specify which shapes need to be approximated
cmd.add<std::string>("shapes-approximation", 's',
"Specify which shapes need to be approximated. Supported values are: visual, collision, both.",
false, "both");
}

int main(int argc, char** argv)
Expand All @@ -40,6 +45,14 @@ int main(int argc, char** argv)
// Read model
const std::string& modelPath = cmd.get<std::string>("model");
const std::string& outputModelPath = cmd.get<std::string>("output-model");
const std::string& shapesApproximation = cmd.get<std::string>("shapes-approximation");

// check if the shapes approximation is valid
if (shapesApproximation != "visual" && shapesApproximation != "collision" && shapesApproximation != "both")
{
std::cerr << "Invalid shapes approximation value: " << shapesApproximation << " . Supported values are: visual, collision, both." << std::endl;
return EXIT_FAILURE;
}

iDynTree::ModelLoader mdlLoader;
bool ok = mdlLoader.loadModelFromFile(modelPath);
Expand All @@ -53,9 +66,22 @@ int main(int argc, char** argv)
// Simplify the model
iDynTree::ApproximateSolidShapesWithPrimitiveShapeOptions options =
iDynTree::ApproximateSolidShapesWithPrimitiveShapeOptions();
options.conversionType = iDynTree::ConvertSolidShapesWithEnclosingAxisAlignedBoundingBoxes;
options.conversionType = iDynTree::ApproximateSolidShapesWithPrimitiveShapeConversionType::ConvertSolidShapesWithEnclosingAxisAlignedBoundingBoxes;
if (shapesApproximation == "visual")
{
options.shapesToApproximate = iDynTree::ApproximateSolidShapesWithPrimitiveShapeShapesToApproximate::VisualShapes;
}
else if (shapesApproximation == "collision")
{
options.shapesToApproximate = iDynTree::ApproximateSolidShapesWithPrimitiveShapeShapesToApproximate::CollisionShapes;
}
else if (shapesApproximation == "both")
{
options.shapesToApproximate = iDynTree::ApproximateSolidShapesWithPrimitiveShapeShapesToApproximate::BothShapes;
}

iDynTree::Model simplifiedModel;
ok = approximateSolidShapesWithPrimitiveShape(mdlLoader.model(), simplifiedModel);
ok = approximateSolidShapesWithPrimitiveShape(mdlLoader.model(), simplifiedModel, options);

if (!ok)
{
Expand Down

0 comments on commit bbd8b08

Please sign in to comment.