Collision Checker
rectangle_obb.h
Go to the documentation of this file.
1 #ifndef RECTANGLE_OBB_H_
2 #define RECTANGLE_OBB_H_
3 
4 #include <exception>
5 #include <iostream>
6 
9 
10 #include "collision/line_segment.h"
11 
12 #include "detail/aabb.h"
13 
14 namespace collision {
19 class RectangleOBB : public Shape {
20  public:
21  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
22 
23  inline void set_up_segments(void) {
24  // must be clockwise to be able to correctly construct BoostPolygon
25 
26  Eigen::Vector2d _v1 =
27  center() - r_x() * local_x_axis() + r_y() * local_y_axis();
28  Eigen::Vector2d _v2 =
29  center() + r_x() * local_x_axis() + r_y() * local_y_axis();
30  Eigen::Vector2d _v3 =
31  center() + r_x() * local_x_axis() - r_y() * local_y_axis();
32  Eigen::Vector2d _v4 =
33  center() - r_x() * local_x_axis() - r_y() * local_y_axis();
34 
35  compute_fastAABB(_v1, _v2, _v3, _v4);
36 
37  segments_.push_back(LineSegment(_v1, _v2));
38  segments_.push_back(LineSegment(_v2, _v3));
39  segments_.push_back(LineSegment(_v3, _v4));
40  segments_.push_back(LineSegment(_v4, _v1));
41  }
42 
43  inline void compute_fastAABB(const Eigen::Vector2d &_v1,
44  const Eigen::Vector2d &_v2,
45  const Eigen::Vector2d &_v3,
46  const Eigen::Vector2d &_v4) const {
47  double min_x = std::min(_v1(0), _v2(0));
48  double tmp = std::min(_v3(0), _v4(0));
49  fast_aabb_.x_min = std::min(min_x, tmp);
50 
51  double min_y = std::min(_v1(1), _v2(1));
52  tmp = std::min(_v3(1), _v4(1));
53  fast_aabb_.y_min = std::min(min_y, tmp);
54 
55  double max_x = std::max(_v1(0), _v2(0));
56  tmp = std::max(_v3(0), _v4(0));
57  fast_aabb_.x_max = std::max(max_x, tmp);
58 
59  double max_y = std::max(_v1(1), _v2(1));
60  tmp = std::max(_v3(1), _v4(1));
61  fast_aabb_.y_max = std::max(max_y, tmp);
62 
63  is_fastAABB_cached_ = true;
64  }
65 
66  inline void compute_fastAABB(void) const {
67  double min_x = std::min(segments_[0].point1().x, segments_[1].point1().x);
68  double tmp = std::min(segments_[2].point1().x, segments_[3].point1().x);
69  fast_aabb_.x_min = std::min(min_x, tmp);
70 
71  double min_y = std::min(segments_[0].point1().y, segments_[1].point1().y);
72  tmp = std::min(segments_[2].point1().y, segments_[3].point1().y);
73  fast_aabb_.y_min = std::min(min_y, tmp);
74 
75  double max_x = std::max(segments_[0].point1().x, segments_[1].point1().x);
76  tmp = std::max(segments_[2].point1().x, segments_[3].point1().x);
77  fast_aabb_.x_max = std::max(max_x, tmp);
78 
79  double max_y = std::max(segments_[0].point1().y, segments_[1].point1().y);
80  tmp = std::max(segments_[2].point1().y, segments_[3].point1().y);
81  fast_aabb_.y_max = std::max(max_y, tmp);
82 
83  is_fastAABB_cached_ = true;
84  }
85 
87  : RectangleOBB(obb.r_x(), obb.r_y(), obb.local_axes(), obb.center()){};
88 
89  RectangleOBB(double _r_x, double _r_y, Eigen::Matrix2d _local_axes,
90  const Eigen::Vector2d &_center = Eigen::Vector2d(0, 0))
91  : Shape(_center),
92  local_axes_(_local_axes),
93  r_(_r_x, _r_y),
94  is_fastAABB_cached_(false),
95  cached_orientation_(0),
96  is_orientation_cached_(false) {
98  }
99 
100  RectangleOBB(double _r_x, double _r_y, double angle,
101  const Eigen::Vector2d &_center = Eigen::Vector2d(0, 0))
102  : Shape(_center),
103  r_(_r_x, _r_y),
104  is_fastAABB_cached_(false),
105  cached_orientation_(angle),
106  is_orientation_cached_(true) {
107  local_axes_ << cos(angle), -sin(angle), sin(angle), cos(angle);
108 
109  set_up_segments();
110  }
111 
113  : Shape(copy), is_fastAABB_cached_(false) {
114  center_ = copy.center();
115  radius_ = copy.radius();
116  local_axes_ = copy.local_axes();
117  r_ = copy.r();
118  segments_ = copy.segments();
119  cached_orientation_ = copy.cached_orientation_;
120  is_orientation_cached_ = copy.is_orientation_cached_;
121  }
122 
123  double orientation() const;
124 
125  inline AABB getAABB_fast(void) const {
126  if (!is_fastAABB_cached_) {
128  }
129  return fast_aabb_;
130  }
131 
134  }
135 
136  bool rayTrace(const Eigen::Vector2d &point1, const Eigen::Vector2d &point2,
137  std::vector<LineSegment> &intersect) const override;
138 
139  virtual ~RectangleOBB() {}
140 
141 #if ENABLE_SERIALIZER
142  serialize::ICollisionObjectExport *exportThis(void) const override;
143 #endif
144 
145  virtual RectangleOBB *clone() const;
146 
147  void print(std::ostringstream &stream) const;
148 
150  void) const override;
152  const std::shared_ptr<fcl::CollisionGeometry<FCL_PRECISION>> &)
153  const override;
154 
155  ShapeType type() const;
156 
157  Eigen::Matrix2d local_axes() const;
158  Eigen::Vector2d local_x_axis() const;
159  Eigen::Vector2d local_y_axis() const;
160  Eigen::Vector2d r() const;
161  double r(int i) const;
162  double r_x() const;
163  double r_y() const;
164 
165  void set_local_x_axis(const Eigen::Vector2d &x_axis);
166  void set_local_y_axis(const Eigen::Vector2d &y_axis);
167  void set_r_x(double _r_x);
168  void set_r_y(double _r_y);
169 
170  //
171  // @brief Computes the square distance between a point and
172  // the rectangle boundary. From:
173  // C. Ericson, Real-Time Collision Detection, pp. 134, 2004
174  // @param p Point
175  //
176  double squareDisToPoint(const Eigen::Vector2d &p) const;
177 
178  std::vector<LineSegment> segments(void) const { return segments_; };
179 
180  private:
181  using Shape::center_;
182  using Shape::radius_;
183 
184  // Local x- and y-axis (column vectors)
185  // | x_1 x_2 |
186  // local_axis = | |
187  // | y_1 y_2 |
188  //
189  Eigen::Matrix2d local_axes_;
190 
192  Eigen::Vector2d r_;
193 
194  std::vector<LineSegment> segments_;
195 
196  static constexpr ShapeType type_ = TYPE_RECTANGLE_OBB;
197 
198  mutable bool is_fastAABB_cached_;
199 
200  mutable AABB fast_aabb_;
201 
202  mutable double cached_orientation_;
203  mutable bool is_orientation_cached_;
204 
205  void compute_orientation(void) const;
206 };
207 
208 } // namespace collision
209 
210 #endif
Eigen::Vector2d local_y_axis() const
void set_local_x_axis(const Eigen::Vector2d &x_axis)
ShapeType type() const
Get shape type.
Oriented rectangle.
Definition: rectangle_obb.h:19
double y_min
Definition: aabb.h:11
double orientation() const
double radius_
Definition: shape.h:73
fcl::CollisionObject< FCL_PRECISION > * createFCLCollisionObject(const std::shared_ptr< fcl::CollisionGeometry< FCL_PRECISION >> &) const override
double squareDisToPoint(const Eigen::Vector2d &p) const
RectangleOBB(double _r_x, double _r_y, double angle, const Eigen::Vector2d &_center=Eigen::Vector2d(0, 0))
EIGEN_MAKE_ALIGNED_OPERATOR_NEW void set_up_segments(void)
Definition: rectangle_obb.h:23
fcl::CollisionGeometry< FCL_PRECISION > * createFCLCollisionGeometry(void) const override
void set_r_y(double _r_y)
RectangleOBB(const detail::OBB &obb)
Definition: rectangle_obb.h:86
void compute_fastAABB(void) const
Definition: rectangle_obb.h:66
virtual CollisionObjectType getCollisionObjectType() const
Eigen::Vector2d r() const
double radius() const
Get radius.
Definition: shape.cc:27
void set_local_y_axis(const Eigen::Vector2d &y_axis)
ShapeType
Definition: shape.h:13
void set_r_x(double _r_x)
Base prototype for the shape of an obstacle.
Definition: shape.h:25
virtual RectangleOBB * clone() const
AABB getAABB_fast(void) const
void print(std::ostringstream &stream) const
Print all parameters of the shape.
double x_max
Definition: aabb.h:10
RectangleOBB(double _r_x, double _r_y, Eigen::Matrix2d _local_axes, const Eigen::Vector2d &_center=Eigen::Vector2d(0, 0))
Definition: rectangle_obb.h:89
double x_min
Definition: aabb.h:9
RectangleOBB(const RectangleOBB &copy)
std::vector< LineSegment > segments(void) const
Eigen::Vector2d local_x_axis() const
void compute_fastAABB(const Eigen::Vector2d &_v1, const Eigen::Vector2d &_v2, const Eigen::Vector2d &_v3, const Eigen::Vector2d &_v4) const
Definition: rectangle_obb.h:43
Eigen::Vector2d center_
Definition: shape.h:72
Eigen::Matrix2d local_axes() const
Eigen::Vector2d center() const
Get geometric center of shape.
Definition: shape.cc:16
double y_max
Definition: aabb.h:12
bool rayTrace(const Eigen::Vector2d &point1, const Eigen::Vector2d &point2, std::vector< LineSegment > &intersect) const override