Collision Checker
rectangle_obb.cc
Go to the documentation of this file.
2 
3 #include <fcl/narrowphase/collision.h>
4 #include <fcl/narrowphase/collision_object.h>
7 
9 
10 namespace collision {
11 
13  void) const {
14  return new fcl::Box<FCL_PRECISION>(r_x() * 2, r_y() * 2, FCL_HEIGHT);
15 }
17  const std::shared_ptr<fcl::CollisionGeometry<FCL_PRECISION>> &col_geom)
18  const {
21  this->center(), this->local_x_axis()));
22 }
23 
24 bool RectangleOBB::rayTrace(const Eigen::Vector2d &point1,
25  const Eigen::Vector2d &point2,
26  std::vector<LineSegment> &intersect) const {
27  LineSegment ray_segment(point1, point2);
28 
29  std::vector<Eigen::Vector2d> inters1;
30  bool res = false;
31  for (auto &segm : segments_) {
32  res = segm.intersect(ray_segment, inters1) || res;
33  }
34  collision::raytrace::rayTracePostprocess(point1, point2, inters1, intersect,
35  this);
36 
37  return res;
38 }
39 
40 RectangleOBB *RectangleOBB::clone() const { return new RectangleOBB(*this); }
41 
42 ShapeType RectangleOBB::type() const { return type_; }
43 
44 void RectangleOBB::print(std::ostringstream &stream) const {
45  stream << "OBB Rectangle: center: (" << center_x() << "/" << center_y()
46  << "), r: (" << r_(0) << "|" << r_(1) << ") "
47  << "Local coordinate axes: (" << local_axes_(0, 0) << ","
48  << local_axes_(1, 0) << "), (" << local_axes_(0, 1) << ","
49  << local_axes_(1, 1) << ")" << std::endl;
50 }
51 
52 Eigen::Matrix2d RectangleOBB::local_axes() const { return local_axes_; }
53 
54 Eigen::Vector2d RectangleOBB::local_x_axis() const {
55  return local_axes_.col(0);
56 }
57 
58 Eigen::Vector2d RectangleOBB::local_y_axis() const {
59  return local_axes_.col(1);
60 }
61 
62 Eigen::Vector2d RectangleOBB::r() const { return r_; }
63 
64 double RectangleOBB::r(int i) const {
65  switch (i) {
66  case 0:
67  return r_(0);
68  case 1:
69  return r_(1);
70  default:
71  throw "Rectangle_OBB: Not a valid index for r";
72  }
73 }
74 
75 double RectangleOBB::r_x() const { return r_(0); }
76 
77 double RectangleOBB::r_y() const { return r_(1); }
78 
79 void RectangleOBB::set_local_x_axis(const Eigen::Vector2d &x_axis) {
80  local_axes_.col(0) = x_axis;
82  is_fastAABB_cached_ = false;
83  is_orientation_cached_ = false;
84 }
85 
86 void RectangleOBB::set_local_y_axis(const Eigen::Vector2d &y_axis) {
87  local_axes_.col(1) = y_axis;
89  is_fastAABB_cached_ = false;
90  is_orientation_cached_ = false;
91 }
92 
93 void RectangleOBB::set_r_x(double _r_x) {
94  r_(0) = _r_x;
96  is_fastAABB_cached_ = false;
97 }
98 
99 void RectangleOBB::set_r_y(double _r_y) {
100  r_(1) = _r_y;
102  is_fastAABB_cached_ = false;
103 }
104 
106  if (is_orientation_cached_)
107  return cached_orientation_;
108  else
109  compute_orientation();
110  return cached_orientation_;
111 }
112 
113 void RectangleOBB::compute_orientation() const {
114  Eigen::Matrix2d temp_matrix;
115  temp_matrix.col(0) = local_x_axis();
116  temp_matrix.col(1) = Eigen::Vector2d(1, 0);
117  double dot = temp_matrix.col(0).dot(temp_matrix.col(1));
118  double det = temp_matrix.determinant();
119  cached_orientation_ =
120  -1 * std::atan2(det, dot); // atan2(y, x) or atan2(sin, cos)
121  is_orientation_cached_ = true;
122 }
123 
124 double RectangleOBB::squareDisToPoint(const Eigen::Vector2d &p) const {
125  double sq_dis = 0.0;
127  Eigen::Vector2d t = local_axes_.transpose() * (p - center_);
128 
129  for (int i = 0; i < 2; i++) {
130  if (t(i) < -r_(i)) {
131  sq_dis += pow(t(i) + r_(i), 2);
132  } else if (t(i) > r_(i)) {
133  sq_dis += pow(t(i) - r_(i), 2);
134  }
135  }
136  return sq_dis;
137 }
138 
139 #if ENABLE_SERIALIZER
140 
141 namespace serialize {
142 ICollisionObjectExport *exportObject(const collision::RectangleOBB &);
143 }
144 
145 serialize::ICollisionObjectExport *RectangleOBB::exportThis(void) const {
146  return serialize::exportObject(*this);
147 }
148 #endif
149 
150 } // namespace collision
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 orientation() const
static fcl::Transform3< FCL_PRECISION > fcl_get_3d_rotation_translation(const Eigen::Vector2d &pos, const Eigen::Vector2d &x_axis)
Definition: fcl_transform.h:27
fcl::CollisionObject< FCL_PRECISION > * createFCLCollisionObject(const std::shared_ptr< fcl::CollisionGeometry< FCL_PRECISION >> &) const override
double squareDisToPoint(const Eigen::Vector2d &p) const
double det(double a, double b, double c, double d)
bool rayTracePostprocess(const Eigen::Vector2d &point1, const Eigen::Vector2d &point2, std::vector< Eigen::Vector2d > inters1, std::vector< LineSegment > &intersect, const collision::CollisionObject *obj)
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
Eigen::Vector2d r() const
void set_local_y_axis(const Eigen::Vector2d &y_axis)
ShapeType
Definition: shape.h:13
void set_r_x(double _r_x)
virtual RectangleOBB * clone() const
void print(std::ostringstream &stream) const
Print all parameters of the shape.
int serialize(const test::BroadphaseFailureCCObj &bf_object, std::ostream &output_stream, const char *format=SERIALIZER_DEFAULT_FORMAT)
double center_x() const
Definition: shape.cc:18
double center_y() const
Definition: shape.cc:20
Eigen::Vector2d local_x_axis() const
#define FCL_HEIGHT
Definition: fcl_decl.h:10
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
bool rayTrace(const Eigen::Vector2d &point1, const Eigen::Vector2d &point2, std::vector< LineSegment > &intersect) const override