Skip to content

Commit

Permalink
Added a function to create an empty body from shape type. (#137)
Browse files Browse the repository at this point in the history
This allows more efficient body construction when scale, padding or pose should also be set during the construction.
  • Loading branch information
peci1 authored May 4, 2020
1 parent 3f9eed4 commit a40825a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
3 changes: 3 additions & 0 deletions include/geometric_shapes/body_operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@

namespace bodies
{
/** \brief Create a body from a given shape */
Body* createEmptyBodyFromShapeType(const shapes::ShapeType& shapeType);

/** \brief Create a body from a given shape */
Body* createBodyFromShape(const shapes::Shape* shape);

Expand Down
53 changes: 33 additions & 20 deletions src/body_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,40 @@
#include <console_bridge/console.h>
#include <Eigen/Geometry>

bodies::Body* bodies::createEmptyBodyFromShapeType(const shapes::ShapeType& shapeType)
{
Body* body = nullptr;

switch (shapeType)
{
case shapes::BOX:
body = new bodies::Box();
break;
case shapes::SPHERE:
body = new bodies::Sphere();
break;
case shapes::CYLINDER:
body = new bodies::Cylinder();
break;
case shapes::MESH:
body = new bodies::ConvexMesh();
break;
default:
CONSOLE_BRIDGE_logError("Creating body from shape: Unknown shape type %d", (int)shapeType);
break;
}
return body;
}

bodies::Body* bodies::createBodyFromShape(const shapes::Shape* shape)
{
Body* body = nullptr;

if (shape)
switch (shape->type)
{
case shapes::BOX:
body = new bodies::Box(shape);
break;
case shapes::SPHERE:
body = new bodies::Sphere(shape);
break;
case shapes::CYLINDER:
body = new bodies::Cylinder(shape);
break;
case shapes::MESH:
body = new bodies::ConvexMesh(shape);
break;
default:
CONSOLE_BRIDGE_logError("Creating body from shape: Unknown shape type %d", (int)shape->type);
break;
}
{
body = createEmptyBodyFromShapeType(shape->type);
body->setDimensions(shape);
}

return body;
}
Expand Down Expand Up @@ -106,7 +117,7 @@ Body* constructBodyFromMsgHelper(const T& shape_msg, const geometry_msgs::Pose&

if (shape)
{
Body* body = createBodyFromShape(shape);
Body* body = createEmptyBodyFromShapeType(shape->type);
if (body)
{
Eigen::Quaterniond q(pose.orientation.w, pose.orientation.x, pose.orientation.y, pose.orientation.z);
Expand All @@ -116,7 +127,9 @@ Body* constructBodyFromMsgHelper(const T& shape_msg, const geometry_msgs::Pose&
q = Eigen::Quaterniond(1.0, 0.0, 0.0, 0.0);
}
Eigen::Isometry3d af(Eigen::Translation3d(pose.position.x, pose.position.y, pose.position.z) * q);
body->setPose(af);
body->setPoseDirty(af);
body->setDimensionsDirty(shape);
body->updateInternalData();
return body;
}
}
Expand Down

0 comments on commit a40825a

Please sign in to comment.