Collision Checker
rectangle_aabb.cc
Go to the documentation of this file.
1 #include <fcl/narrowphase/collision.h>
2 #include <fcl/narrowphase/collision_object.h>
5 
7 
9 
10 namespace collision {
11 
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 {
20  col_geom,
22  this->center()));
23 }
24 
25 bool RectangleAABB::rayTrace(const Eigen::Vector2d &point1,
26  const Eigen::Vector2d &point2,
27  std::vector<LineSegment> &intersect) const {
28  LineSegment obj_segment(point1, point2);
29 
30  std::vector<Eigen::Vector2d> inters1;
31  bool res = false;
32  for (auto &segm : segments_) {
33  res = segm.intersect(obj_segment, inters1) || res;
34  }
35  collision::raytrace::rayTracePostprocess(point1, point2, inters1, intersect,
36  this);
37 
38  return res;
39 }
40 
41 RectangleAABB *RectangleAABB::clone() const { return new RectangleAABB(*this); }
42 
44  center_ = copy.center(); // not needed? It is already set in Shape(copy)
45  radius_ = copy.radius();
46  r_ = copy.r();
47  min_ = copy.min();
48  max_ = copy.max();
49  segments_ = copy.segments();
50 }
51 
52 ShapeType RectangleAABB::type() const { return type_; }
53 
54 void RectangleAABB::print(std::ostringstream &stream) const {
55  stream << "AABB Rectangle: center: (" << center_x() << "/" << center_y()
56  << ") r: (" << r_(0) << "|" << r_(1) << ") "
57  << "min: (" << min_(0) << "|" << min_(1) << ") "
58  << "max: (" << max_(0) << "|" << max_(1) << ")" << std::endl;
59 }
60 
61 Eigen::Vector2d RectangleAABB::min() const { return min_; }
62 
63 Eigen::Vector2d RectangleAABB::max() const { return max_; }
64 
65 Eigen::Vector2d RectangleAABB::r() const { return r_; }
66 
67 double RectangleAABB::r(int i) const {
68  switch (i) {
69  case 0:
70  return r_(0);
71  case 1:
72  return r_(1);
73  default:
74  throw "Rectangle_OBB: Not a valid index for r";
75  }
76 }
77 
78 void RectangleAABB::set_r(const Eigen::Vector2d &_r) {
79  r_ = _r;
80  min_ = center_ - r_;
81  max_ = center_ + r_;
83 }
84 
85 double RectangleAABB::r_x() const { return r_(0); }
86 
87 double RectangleAABB::r_y() const { return r_(1); }
88 
89 void RectangleAABB::set_center(const Eigen::Vector2d &_center) {
90  center_ = _center;
91  min_ = center_ - r_;
92  max_ = center_ + r_;
94 }
95 
96 void RectangleAABB::set_r_x(double _r_x) {
97  r_(0) = _r_x;
98  min_(0) = center_(0) - r_(0);
99  max_(0) = center_(0) + r_(0);
101 }
102 
103 void RectangleAABB::set_r_y(double _r_y) {
104  r_(1) = _r_y;
105  min_(1) = center_(1) - r_(1);
106  max_(1) = center_(1) + r_(1);
108 }
109 
110 void RectangleAABB::set_all(double r_x, double r_y, double center_x,
111  double center_y) {
112  center_(0) = center_x;
113  r_(0) = r_x;
114  min_(0) = center_x - r_x;
115  max_(0) = center_x + r_x;
116 
117  center_(1) = center_y;
118  r_(1) = r_y;
119  min_(1) = center_y - r_y;
120  max_(1) = center_y + r_y;
122 }
123 
124 double RectangleAABB::squareDisToPoint(const Eigen::Vector2d &p) const {
125  double sq_dis = 0.0;
126  for (int i = 0; i < 2; i++) {
127  if (p(i) < min_(i)) {
128  sq_dis += pow(min_(i) - p(i), 2);
129  } else if (p(i) > max_(i)) {
130  sq_dis += pow(p(i) - max_(i), 2);
131  }
132  }
133  return sq_dis;
134 }
135 
136 #if ENABLE_SERIALIZER
137 
138 namespace serialize {
139 ICollisionObjectExport *exportObject(const collision::RectangleAABB &);
140 }
141 
142 serialize::ICollisionObjectExport *RectangleAABB::exportThis(void) const {
143  return serialize::exportObject(*this);
144 }
145 #endif
146 
147 } // namespace collision
double radius_
Definition: shape.h:73
void set_r(const Eigen::Vector2d &_r)
double squareDisToPoint(const Eigen::Vector2d &p) const
Eigen::Vector2d r() const
void print(std::ostringstream &stream) const
Print all parameters of the shape.
bool rayTrace(const Eigen::Vector2d &point1, const Eigen::Vector2d &point2, std::vector< LineSegment > &intersect) const override
bool rayTracePostprocess(const Eigen::Vector2d &point1, const Eigen::Vector2d &point2, std::vector< Eigen::Vector2d > inters1, std::vector< LineSegment > &intersect, const collision::CollisionObject *obj)
static fcl::Transform3< FCL_PRECISION > fcl_get_3d_translation(const Eigen::Vector2d &pos)
Definition: fcl_transform.h:21
RectangleAABB * clone() const
fcl::CollisionObject< FCL_PRECISION > * createFCLCollisionObject(const std::shared_ptr< fcl::CollisionGeometry< FCL_PRECISION >> &col_geom) const override
Eigen::Vector2d min() const
fcl::CollisionGeometry< FCL_PRECISION > * createFCLCollisionGeometry(void) const override
void set_r_x(double _r_x)
void set_r_y(double _r_y)
ShapeType type() const
Get shape type.
void set_all(double r_x, double r_y, double center_x, double center_y)
double radius() const
Get radius.
Definition: shape.cc:27
Axis-aligned rectangle.
ShapeType
Definition: shape.h:13
EIGEN_MAKE_ALIGNED_OPERATOR_NEW RectangleAABB(double _rx, double _ry, const Eigen::Vector2d &_center=Eigen::Vector2d(0, 0))
Base prototype for the shape of an obstacle.
Definition: shape.h:25
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
std::vector< LineSegment > segments(void) const
double center_y() const
Definition: shape.cc:20
#define FCL_HEIGHT
Definition: fcl_decl.h:10
Eigen::Vector2d center_
Definition: shape.h:72
Eigen::Vector2d max() const
Eigen::Vector2d center() const
Get geometric center of shape.
Definition: shape.cc:16
void set_center(const Eigen::Vector2d &_center)