Collision Checker
shape_group.cc
Go to the documentation of this file.
2 
5 
6 #include <limits>
7 
8 namespace collision {
9 
12  ShapeGroupPtr sg_new(new ShapeGroup());
13  for (auto el : this->shapes_) {
14  if (el->BVCheck(aabb)) {
15  sg_new->addToGroup(el);
16  }
17  }
18  return sg_new;
19 }
20 
21 std::shared_ptr<const collision::RectangleAABB> ShapeGroup::getAABB() const {
22  if (shapes_.size() == 0)
23  return RectangleAABBConstPtr(new RectangleAABB(0, 0));
24  else {
25  double min_x = std::numeric_limits<double>::max();
26  double max_x = std::numeric_limits<double>::min();
27  double min_y = std::numeric_limits<double>::max();
28  double max_y = std::numeric_limits<double>::min();
29  for (auto el : shapes_) {
30  auto cur_aabb = el->getAABB();
31  double cur_min_x = cur_aabb->min()(0);
32  double cur_min_y = cur_aabb->min()(1);
33  double cur_max_x = cur_aabb->max()(0);
34  double cur_max_y = cur_aabb->max()(1);
35  min_x = std::min(min_x, cur_min_x);
36  min_y = std::min(min_y, cur_min_y);
37  max_x = std::max(max_x, cur_max_x);
38  max_y = std::max(max_y, cur_max_y);
39  }
40  Eigen::Vector2d center((min_x + max_x) / 2, (min_y + max_y) / 2);
41  double radius_x = (max_x - min_x) / 2;
42  double radius_y = (max_y - min_y) / 2;
43  return RectangleAABBConstPtr(new RectangleAABB(radius_x, radius_y, center));
44  }
45 }
46 
48  std::vector<fcl::CollisionObject<FCL_PRECISION> *> &obj_vec) const {
49  // obj_vec.clear();
50  for (auto i : shapes_) {
51  const FCLCollisionObject *i_obj = get_fcl_object_ptr(i.get());
53  i_obj->getCollisionObject_fcl().get(); // TODO: thread-safety
54  if (obj)
55  obj_vec.push_back(obj);
56  else
57  return 1;
58  }
59  return 0;
60 }
61 
73 std::vector<std::set<int>> ShapeGroup::overlapMap(const ShapeGroup &sg) const {
74  int first_el_count = shapes_.size();
75  std::vector<std::set<int>> ret(first_el_count);
76  std::vector<std::pair<int, int>> collid_list = overlap(sg);
77  for (auto el : collid_list) {
78  int first_val = el.first;
79  if (first_val > -1 && first_val < first_el_count) {
80  ret[first_val].insert(el.second);
81  }
82  }
83  return ret;
84 }
85 
91 int ShapeGroup::elementCount(void) const { return shapes_.size(); }
92 
93 bool ShapeGroup::rayTrace(const Eigen::Vector2d &point1,
94  const Eigen::Vector2d &point2,
95  std::vector<LineSegment> &intersect) const {
96  bool res = false;
97  for (auto &obj : shapes_) {
98  res = obj->rayTrace(point1, point2, intersect) || res;
99  }
100  return res;
101 }
102 
111 std::vector<std::pair<int, int>> ShapeGroup::overlap(
112  const ShapeGroup &sg) const {
113  const FCLCollisionObjectGroup *fcl_gr_ptr_this =
115  const FCLCollisionObjectGroup *fcl_gr_ptr_sg = get_fcl_object_group_ptr(&sg);
116  if (!fcl_gr_ptr_this || !fcl_gr_ptr_sg) {
117  throw;
118  }
119  std::vector<std::pair<int, int>> ret;
120  fcl_primitive_queries::fcl_group_overlap(*fcl_gr_ptr_this, *fcl_gr_ptr_sg,
121  ret);
122  return ret;
123 }
124 
125 int ShapeGroup::addToIndex(const CollisionObject &obj, int idx) {
126  auto el = shapes_map_.find(&obj);
127  if (el != shapes_map_.end()) {
128  el->second.push_back(idx);
129  } else {
130  std::list<int> new_list;
131  new_list.push_back(idx);
132  shapes_map_.emplace(&obj, new_list);
133  }
134  return 0;
135 }
136 
138  std::list<int> &retlist) const {
139  auto el = shapes_map_.find(pObj);
140  if (el != shapes_map_.end()) {
141  retlist.insert(retlist.end(), el->second.begin(), el->second.end());
142  return 0;
143  } else {
144  return -1;
145  }
146 }
147 
148 void ShapeGroup::print(std::ostringstream &stream) const {}
149 
150 void ShapeGroup::toString(std::ostringstream &stream) const {
151  stream << "ShapeGroup "
152  << "shapes: ";
153  for (auto &shape : shapes_) {
154  shape->toString(stream);
155  }
156  stream << "\\ShapeGroup " << std::endl;
157 }
158 
160  int time_idx, CollisionObjectConstPtr shared_ptr_this) const {
161  return shared_ptr_this;
162 }
163 
177  shapes_.push_back(shape);
178  addToIndex(*shape, shapes_.size() - 1);
180 }
181 
187 std::vector<ShapeConstPtr> ShapeGroup::unpack() const { return shapes_; }
188 
190  std::unordered_map<const CollisionObject *,
191  std::list<CollisionObjectConstPtr>> &parent_map,
192  CollisionObjectConstPtr parent) const {
193  for (auto &shape : shapes_) {
194  shape->addParentMap(parent_map, parent);
195  }
196 }
197 
199  std::unordered_map<const CollisionObject *,
200  std::list<CollisionObjectConstPtr>> &parent_map) const {
201  for (auto &shape : shapes_) {
202  shape->addParentMap(parent_map, shared_from_this());
203  }
204 }
205 #if ENABLE_SERIALIZER
206 
207 namespace serialize {
208 ICollisionObjectExport *exportObject(const collision::ShapeGroup &);
209 }
210 
211 serialize::ICollisionObjectExport *ShapeGroup::exportThis(void) const {
212  return serialize::exportObject(*this);
213 }
214 #endif
215 
216 } // namespace collision
int getCollisionObjects(std::vector< fcl::CollisionObject< FCL_PRECISION > *> &obj_vec) const override
Definition: shape_group.cc:47
std::vector< std::set< int > > overlapMap(const ShapeGroup &sg) const
Returns lists of all objects from the other ShapeGroup that collide with each object of this ShapeGro...
Definition: shape_group.cc:73
virtual CollisionObjectConstPtr timeSlice(int time_idx, CollisionObjectConstPtr shared_ptr_this) const
Definition: shape_group.cc:159
std::shared_ptr< const Shape > ShapeConstPtr
std::vector< ShapeConstPtr > unpack() const
Returns Vector of all shapes contained inside the ShapeGroup.
Definition: shape_group.cc:187
std::shared_ptr< const RectangleAABB > RectangleAABBConstPtr
std::shared_ptr< const collision::RectangleAABB > getAABB() const
Definition: shape_group.cc:21
ShapeGroup can contain simple shapes.
Definition: shape_group.h:33
bool rayTrace(const Eigen::Vector2d &point1, const Eigen::Vector2d &point2, std::vector< LineSegment > &intersect) const
Definition: shape_group.cc:93
std::shared_ptr< const CollisionObject > CollisionObjectConstPtr
const FCLCollisionObject * get_fcl_object_ptr(const CollisionObject *obj)
Definition: fcl_helpers.h:40
void toString(std::ostringstream &stream) const
Definition: shape_group.cc:150
int elementCount(void) const
Returns count of contained objects.
Definition: shape_group.cc:91
ShapeGroupConstPtr windowQuery(collision::RectangleAABBConstPtr aabb) const
Definition: shape_group.cc:10
std::shared_ptr< fcl::CollisionObject< FCL_PRECISION > > getCollisionObject_fcl(void) const
std::shared_ptr< const ShapeGroup > ShapeGroupConstPtr
Definition: polygon.h:20
std::vector< std::pair< int, int > > overlap(const ShapeGroup &sg) const
Returns Vector of pairs of indexes of the intersecting objects within the ShapeGroup and another Shap...
Definition: shape_group.cc:111
virtual void print(std::ostringstream &stream) const
Definition: shape_group.cc:148
std::shared_ptr< ShapeGroup > ShapeGroupPtr
Definition: polygon.h:17
Axis-aligned rectangle.
int queryContainedObjectIndexList(const CollisionObject *, std::list< int > &retlist) const override
Definition: shape_group.cc:137
int serialize(const test::BroadphaseFailureCCObj &bf_object, std::ostream &output_stream, const char *format=SERIALIZER_DEFAULT_FORMAT)
int fcl_group_overlap(const FCLCollisionObjectGroup &group_first, const FCLCollisionObjectGroup &group_second, std::vector< std::pair< int, int >> &retlist, std::vector< std::pair< int, int >> *missed_col_naive_in=0)
Base class for CollisionObjects and some of their groups.
void addToGroup(ShapeConstPtr shape)
Adds a simple shape to the ShapeGroup.
Definition: shape_group.cc:176
const FCLCollisionObjectGroup * get_fcl_object_group_ptr(const CollisionObject *obj)
Definition: fcl_helpers.h:27
virtual void addParentMap(std::unordered_map< const CollisionObject *, std::list< CollisionObjectConstPtr >> &parent_map) const
Definition: shape_group.cc:198