se2ez
geometry.cpp
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #include <iostream>
4 
5 #include <se2ez/core/math.h>
6 #include <se2ez/core/geometry.h>
8 
9 using namespace se2ez;
10 
11 const std::string Geometry::TYPE_STRINGS[] = {"box", "circle", "convex", "simple"};
12 
14 {
15  for (unsigned int i = 0; i <= TYPE_MAX; ++i)
16  if (type == TYPE_STRINGS[i])
17  return static_cast<Type>(i);
18 
19  throw std::invalid_argument("Not a valid Geometry type");
20 }
21 
23 {
24  if (type <= TYPE_MAX && type >= 0)
25  return TYPE_STRINGS[type];
26 
27  throw std::invalid_argument("Not a valid Geometry type");
28 }
29 
32 {
33 }
34 
35 Geometry::Geometry(Type type, const Eigen::VectorXd &dimensions, const Eigen::Isometry2d &offset,
36  const Eigen::Vector4d &color)
37  : type(type), dimensions(dimensions), offset(offset), isOffset(tf::transformDistance(offset) > 1e-8), color(color)
38 {
39  switch (type)
40  {
41  case BOX:
42  if (dimensions.size() != 2)
43  throw std::runtime_error("Wrong number of dimensions for box!");
44  break;
45  case CIRCLE:
46  if (dimensions.size() != 1)
47  throw std::runtime_error("Wrong number of dimensions for circle!");
48  break;
49  case CONVEX:
50  case SIMPLE:
51  throw std::runtime_error("Wrong constructor for shape type!");
52  default:
53  throw std::runtime_error("Shape type not implemented!");
54  }
55 }
56 
57 Geometry::Geometry(Type type, const Eigen::VectorXd &dimensions, const Eigen::Vector3d &offset,
58  const Eigen::Vector4d &color)
59  : Geometry(type, dimensions, tf::toIsometry(offset), color)
60 {
61 }
62 
64  const Eigen::Vector4d &color)
65  : type(type), points(points), offset(offset), isOffset(tf::transformDistance(offset) > 1e-8), color(color)
66 {
67  if (points.size() < 3)
68  throw std::runtime_error("Polygon must have at least 3 points!");
69 
70  // For convention, we counterclockwise order the vertices.
71  switch (type)
72  {
73  case CONVEX:
74  if (!geo::isConvex(*this))
75  throw std::runtime_error("Polygon is not convex!");
76 
78  break;
79 
80  case SIMPLE:
81  if (!geo::isSimple(*this))
82  throw std::runtime_error("Polygon is not simple!");
83 
85  break;
86 
87  case BOX:
88  case CIRCLE:
89  throw std::runtime_error("Wrong constructor for shape type!");
90  default:
91  throw std::runtime_error("Shape type not implemented!");
92  }
93 }
94 
96  const Eigen::Vector4d &color)
97  : Geometry(type, points, tf::toIsometry(offset), color)
98 {
99 }
100 
102 {
105 
106  switch (type)
107  {
108  case BOX:
109  case CIRCLE:
111  break;
112  case CONVEX:
113  case SIMPLE:
114  for (const auto &p : points)
115  ss << "(" << p(0) << "," << p(1) << ")";
116  break;
117  default:
118  throw std::runtime_error("Shape type not implemented!");
119  }
120 
121  return ss.str();
122 }
Eigen::Isometry2d offset
Offset of geometry from base frame.
Definition: geometry.h:120
Eigen::VectorXd dimensions
The geometry&#39;s dimensions.
Definition: geometry.h:118
A rectangular prism.
Definition: geometry.h:33
const Type type
Type of geometry.
Definition: geometry.h:117
bool isSimple(const Geometry &geometry)
Checks if a polygon is simple, that is, it has no holes.
Definition: polygon.cpp:75
std::string printFrame(const Eigen::Isometry2d &frame)
Returns a string "[x, y, t]" of the translation (x, y) and rotation (t) component of a transform...
Definition: math.cpp:79
A filled circle.
Definition: geometry.h:34
A convex polygon.
Definition: geometry.h:35
tf::EigenVector< Eigen::Vector2d > points
Point list for polygons.
Definition: geometry.h:119
static const unsigned int TYPE_MAX
Maximum value a geometry type can have.
Definition: geometry.h:40
static Type stringToType(const std::string &geometry)
Try to convert a string into a geometry type. Not case-sensitive.
Definition: geometry.cpp:13
double transformDistance(const Eigen::Isometry2d &t1, const Eigen::Isometry2d &t2=Eigen::Isometry2d::Identity(), double alpha=0.75)
Return the transform distance between two transformations, w is the weight of the euclidean distance...
Definition: math.cpp:36
Type
Type of geometry.
Definition: geometry.h:31
static const std::string & typeToString(const Type &type)
Converts a geometry type into its string representation.
Definition: geometry.cpp:22
A shared pointer wrapper for se2ez::Geometry.
Eigen::Isometry2d toIsometry(double x, double y, double theta)
Converts a translation (x, y) and rotation from X-axis (theta) into a transform.
Definition: math.cpp:11
T str(T... args)
A simple polygon (no holes).
Definition: geometry.h:36
void makeCounterClockwise(Geometry &geometry)
If not counterclockwise, reorders polygon vertices to be in counterclockwise orientation.
Definition: polygon.cpp:94
T size(T... args)
Geometry(const GeometryPtr &g)
Constructor.
Definition: geometry.cpp:30
std::string printVector(const Eigen::VectorXd &v, unsigned int precision=4)
Returns a string of a vector&#39;s contents.
Definition: math.cpp:90
bool isOffset
Is this geometry offset?
Definition: geometry.h:121
bool isConvex(const Geometry &geometry)
Checks if a polygon is convex.
Definition: polygon.cpp:68
Main namespace.
Definition: collision.h:11
static const std::string TYPE_STRINGS[]
String representation of geometry type.
Definition: geometry.h:43
std::string printGeometry() const
Returns a string representation of the geometry.
Definition: geometry.cpp:101
Eigen::Vector4d color
The color to display this geometry as.
Definition: geometry.h:122