Robowflex  v0.1
Making MoveIt Easy
geometry.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef ROBOWFLEX_GEOMETRY_
4 #define ROBOWFLEX_GEOMETRY_
5 
6 #include <Eigen/Core>
7 #include <Eigen/Geometry>
8 
9 #include <geometric_shapes/shapes.h>
10 #include <geometric_shapes/bodies.h>
11 
12 #include <shape_msgs/SolidPrimitive.h>
13 #include <shape_msgs/Mesh.h>
14 
15 #include <moveit/robot_model/aabb.h>
16 
19 
20 namespace robowflex
21 {
22  /** \cond IGNORE */
23  ROBOWFLEX_CLASS_FORWARD(Geometry);
24  /** \endcond */
25 
26  /** \class robowflex::GeometryPtr
27  \brief A shared pointer wrapper for robowflex::Geometry. */
28 
29  /** \class robowflex::GeometryConstPtr
30  \brief A const shared pointer wrapper for robowflex::Geometry. */
31 
32  /** \brief A class that manages both solid and mesh geometry for various parts of the motion planning
33  * system.
34  */
35  class Geometry
36  {
37  public:
38  /** \brief Supported shape types.
39  */
40  class ShapeType
41  {
42  public:
43  enum Type
44  {
45  BOX = 0, ///< Solid primitive box. Uses three dimensions (x, y, z).
46  SPHERE = 1, ///< Solid primitive sphere. Uses one dimension (radius).
47  CYLINDER = 2, ///< Solid primitive cylinder. Uses two dimensions (height, radius).
48  CONE = 3, ///< Solid primitive cone. Uses two dimensions (height, radius).
49  MESH = 4 ///< Mesh. Dimensions scale along x, y, z.
50  };
51 
52  static const unsigned int MAX; ///< Maximum value of ShapeType.
53  static const std::vector<std::string> STRINGS; ///< Mapping of ShapeType to string.
54 
55  static Type toType(const std::string &str); ///< Converts a string into a ShapeType.
56  static const std::string &toString(Type type); ///< Converts a ShapeType to its string.
57  };
58 
59  /** \brief Create a sphere.
60  * \param[in] radius The radius of the sphere.
61  * \return The created sphere.
62  */
63  static GeometryPtr makeSphere(double radius);
64 
65  /** \brief Create a box.
66  * \param[in] x The box's x dimension.
67  * \param[in] y The boy's y dimension.
68  * \param[in] z The boz's z dimension.
69  * \return The created box.
70  */
71  static GeometryPtr makeBox(double x, double y, double z);
72 
73  /** \brief Create a box.
74  * \param[in] dimensions The XYZ dimensions of the box.
75  * \return The created box.
76  */
77  static GeometryPtr makeBox(const Eigen::Vector3d &dimensions);
78 
79  /** \brief Create a cylinder.
80  * \param[in] radius The radius of the cylinder.
81  * \param[in] length The length of the cylinder.
82  * \return The created cylinder.
83  */
84  static GeometryPtr makeCylinder(double radius, double length);
85 
86  /** \brief Create a cone.
87  * \param[in] radius The radius of the base of the cone.
88  * \param[in] length The height of the cone.
89  * \return The created cone.
90  */
91  static GeometryPtr makeCone(double radius, double length);
92 
93  /** \brief Create a solid primitive.
94  * \param[in] msg The solid primitive message to load.
95  * \return The created shape.
96  */
97  static GeometryPtr makeSolidPrimitive(const shape_msgs::SolidPrimitive &msg);
98 
99  /** \brief Create a mesh from resource file.
100  * \param[in] resource The resource to load for the mesh.
101  * \param[in] scale The scale of the mesh.
102  * \return The created mesh.
103  */
104  static GeometryPtr makeMesh(const std::string &resource, const Eigen::Vector3d &scale = {1, 1, 1});
105 
106  /** \brief Create a mesh from triangles represented as vertices .
107  * \param[in] vertices The vertices that will create the mesh.
108  * \return The created mesh.
109  */
110  static GeometryPtr makeMesh(const EigenSTL::vector_Vector3d &vertices);
111 
112  /** \brief Constructor.
113  * Builds and loads the specified geometry.
114  * \param[in] type Type of the geometry to create.
115  * \param[in] dimensions Dimensions of the geometry to load.
116  * \param[in] resource If \a type is ShapeType::MESH, then resource or vertices must be specified
117  * \param[in] vertices List of vertices that form the mesh.
118  * as the mesh file to load.
119  */
120  Geometry(ShapeType::Type type, const Eigen::Vector3d &dimensions, const std::string &resource = "",
121  const EigenSTL::vector_Vector3d &vertices = EigenSTL::vector_Vector3d{});
122 
123  /** \brief Constructor.
124  * Builds and loads the specified geometry from a MoveIt shape.
125  * \param[in] shape Shape to construct geometry from.
126  */
127  Geometry(const shapes::Shape &shape);
128 
129  /** \brief Constructor.
130  * Builds and loads the specified geometry from a MoveIt shape message.
131  * \param[in] msg Shape message to construct geometry from.
132  */
133  Geometry(const shape_msgs::SolidPrimitive &msg);
134 
135  /** \brief Constructor.
136  * Builds and loads the specified geometry from a MoveIt shape message.
137  * \param[in] msg Shape message to construct mesh geometry from.
138  */
139  Geometry(const shape_msgs::Mesh &msg);
140 
141  // non-copyable
142  Geometry(const Geometry &) = delete;
143  Geometry &operator=(const Geometry &) = delete;
144 
145  /** \brief Checks if the geometry contains a \a point.
146  * \param[in] point The point to check, in the geometry's frame.
147  * \return True if the geometry contains the point, false otherwise.
148  */
149  bool contains(const Eigen::Vector3d &point) const;
150 
151  /** \brief Tries to sample a point in the geometry.
152  * \param[in] attempts Number of attempts to sample.
153  * \return The sampled point and true, or the 0 vector and false on failure.
154  */
155  std::pair<bool, Eigen::Vector3d> sample(const unsigned int attempts = 50) const;
156 
157  /** \brief Checks if the geometry is a mesh geometry.
158  * \return True if the \a type_ is a mesh (ShapeType::MESH).
159  */
160  bool isMesh() const;
161 
162  /** \brief Gets the message form of solid primitive geometry (all but ShapeType::MESH).
163  * \return The message.
164  */
165  const shape_msgs::SolidPrimitive getSolidMsg() const;
166 
167  /** \brief Gets the message form of mesh geometry.
168  * \return The message.
169  */
170  const shape_msgs::Mesh getMeshMsg() const;
171 
172  /** \brief Gets the underlying shape.
173  * \return The shape.
174  */
175  const shapes::ShapePtr &getShape() const;
176 
177  /** \brief Gets the underlying body.
178  * \return The body.
179  */
180  const bodies::BodyPtr &getBody() const;
181 
182  /** \brief Gets the type of the geometry.
183  * \return The type of geometry.
184  */
185  ShapeType::Type getType() const;
186 
187  /** \brief Gets the mesh resource of the geometry.
188  * \return The mesh resource of geometry.
189  */
190  const std::string &getResource() const;
191 
192  /** \brief Gets the Vertices of the primitive.
193  * \return The mesh resource of geometry.
194  */
195  const EigenSTL::vector_Vector3d &getVertices() const;
196 
197  /** \brief Gets the dimensions of the geometry.
198  * \return The dimensions of geometry.
199  */
200  const Eigen::Vector3d &getDimensions() const;
201 
202  /** \brief Compute the AABB (axis-aligned bounding box) of the geometry at a given pose.
203  * \param[in] pose Pose to compute AABB of geometry at.
204  * \return The AABB.
205  */
206  Eigen::AlignedBox3d getAABB(const RobotPose &pose = RobotPose::Identity()) const;
207 
208  private:
209  /** \brief Loads a shape from the set \a type_ and \a dimensions_, and \a resource_ if a
210  * mesh. \return A pointer to a newly allocated shape.
211  */
212  shapes::Shape *loadShape() const;
213 
214  /** \brief Loads a body from the loaded \a shape_.
215  * \return A pointer to a newly allocated body.
216  */
217  bodies::Body *loadBody() const;
218 
219  ShapeType::Type type_{ShapeType::Type::BOX}; ///< Geometry Type.
220  Eigen::Vector3d dimensions_{Eigen::Vector3d::Ones()}; ///< Dimensions to scale geometry.
221  EigenSTL::vector_Vector3d vertices_{{}}; ///< Vertices of the primitive
222  std::string resource_{""}; ///< Resource locator for MESH types.
223  shapes::ShapePtr shape_{nullptr}; ///< Loaded shape.
224  bodies::BodyPtr body_{nullptr}; ///< Body operation.
225  };
226 } // namespace robowflex
227 
228 #endif
#define ROBOWFLEX_CLASS_FORWARD(C)
Macro that forward declares a class and defines two shared ptrs types:
Definition: class_forward.h:16
A shared pointer wrapper for robowflex::Geometry.
Supported shape types.
Definition: geometry.h:41
static const std::string & toString(Type type)
Converts a ShapeType to its string.
Definition: geometry.cpp:35
@ CYLINDER
Solid primitive cylinder. Uses two dimensions (height, radius).
Definition: geometry.h:47
@ MESH
Mesh. Dimensions scale along x, y, z.
Definition: geometry.h:49
@ CONE
Solid primitive cone. Uses two dimensions (height, radius).
Definition: geometry.h:48
@ BOX
Solid primitive box. Uses three dimensions (x, y, z).
Definition: geometry.h:45
@ SPHERE
Solid primitive sphere. Uses one dimension (radius).
Definition: geometry.h:46
static Type toType(const std::string &str)
Converts a string into a ShapeType.
Definition: geometry.cpp:14
static const std::vector< std::string > STRINGS
Mapping of ShapeType to string.
Definition: geometry.h:53
static const unsigned int MAX
Maximum value of ShapeType.
Definition: geometry.h:52
A class that manages both solid and mesh geometry for various parts of the motion planning system.
Definition: geometry.h:36
const std::string & getResource() const
Gets the mesh resource of the geometry.
Definition: geometry.cpp:289
static GeometryPtr makeMesh(const std::string &resource, const Eigen::Vector3d &scale={1, 1, 1})
Create a mesh from resource file.
Definition: geometry.cpp:91
Geometry & operator=(const Geometry &)=delete
Geometry(ShapeType::Type type, const Eigen::Vector3d &dimensions, const std::string &resource="", const EigenSTL::vector_Vector3d &vertices=EigenSTL::vector_Vector3d{})
Constructor. Builds and loads the specified geometry.
Definition: geometry.cpp:101
Eigen::Vector3d dimensions_
Dimensions to scale geometry.
Definition: geometry.h:220
static GeometryPtr makeSphere(double radius)
Create a sphere.
Definition: geometry.cpp:43
static GeometryPtr makeBox(double x, double y, double z)
Create a box.
Definition: geometry.cpp:48
const EigenSTL::vector_Vector3d & getVertices() const
Gets the Vertices of the primitive.
Definition: geometry.cpp:294
const shape_msgs::Mesh getMeshMsg() const
Gets the message form of mesh geometry.
Definition: geometry.cpp:265
shapes::ShapePtr shape_
Loaded shape.
Definition: geometry.h:223
static GeometryPtr makeCone(double radius, double length)
Create a cone.
Definition: geometry.cpp:63
const Eigen::Vector3d & getDimensions() const
Gets the dimensions of the geometry.
Definition: geometry.cpp:299
bool isMesh() const
Checks if the geometry is a mesh geometry.
Definition: geometry.cpp:251
const shape_msgs::SolidPrimitive getSolidMsg() const
Gets the message form of solid primitive geometry (all but ShapeType::MESH).
Definition: geometry.cpp:256
Eigen::AlignedBox3d getAABB(const RobotPose &pose=RobotPose::Identity()) const
Compute the AABB (axis-aligned bounding box) of the geometry at a given pose.
Definition: geometry.cpp:304
const shapes::ShapePtr & getShape() const
Gets the underlying shape.
Definition: geometry.cpp:274
Geometry(const Geometry &)=delete
ShapeType::Type getType() const
Gets the type of the geometry.
Definition: geometry.cpp:284
bodies::BodyPtr body_
Body operation.
Definition: geometry.h:224
std::pair< bool, Eigen::Vector3d > sample(const unsigned int attempts=50) const
Tries to sample a point in the geometry.
Definition: geometry.cpp:240
shapes::Shape * loadShape() const
Loads a shape from the set type_ and dimensions_, and resource_ if a mesh.
Definition: geometry.cpp:170
bodies::Body * loadBody() const
Loads a body from the loaded shape_.
Definition: geometry.cpp:207
EigenSTL::vector_Vector3d vertices_
Vertices of the primitive.
Definition: geometry.h:221
static GeometryPtr makeCylinder(double radius, double length)
Create a cylinder.
Definition: geometry.cpp:58
std::string resource_
Resource locator for MESH types.
Definition: geometry.h:222
ShapeType::Type type_
Geometry Type.
Definition: geometry.h:219
const bodies::BodyPtr & getBody() const
Gets the underlying body.
Definition: geometry.cpp:279
static GeometryPtr makeSolidPrimitive(const shape_msgs::SolidPrimitive &msg)
Create a solid primitive.
Definition: geometry.cpp:68
bool contains(const Eigen::Vector3d &point) const
Checks if the geometry contains a point.
Definition: geometry.cpp:235
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
std::decay< decltype(std::declval< moveit::core::Transforms >().getTransform("")) >::type RobotPose
A pose (point in SE(3)) used in various functions. Defined from what MoveIt! uses.
Definition: adapter.h:24