collision_scene.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018-2020, University of Edinburgh, University of Oxford
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef EXOTICA_CORE_COLLISION_SCENE_H_
31 #define EXOTICA_CORE_COLLISION_SCENE_H_
32 
33 #include <Eigen/Dense>
34 
35 #include <sstream>
36 #include <string>
37 #include <tuple>
38 #include <unordered_map>
39 #include <unordered_set>
40 
41 #include <exotica_core/factory.h>
43 #include <exotica_core/object.h>
44 
45 #define REGISTER_COLLISION_SCENE_TYPE(TYPE, DERIV) EXOTICA_CORE_REGISTER(exotica::CollisionScene, TYPE, DERIV)
46 namespace exotica
47 {
48 class Scene;
49 
51 {
52 public:
57  {
58  if (this != &other)
59  {
60  entries_ = other.entries_;
61  }
62  return *this;
63  }
64  inline void clear() { entries_.clear(); }
65  inline bool hasEntry(const std::string& name) const { return entries_.find(name) == entries_.end(); }
66  inline void setEntry(const std::string& name1, const std::string& name2) { entries_[name1].insert(name2); }
67  inline void getAllEntryNames(std::vector<std::string>& names) const
68  {
69  names.clear();
70  for (auto& it : entries_)
71  {
72  names.push_back(it.first);
73  }
74  }
75  inline size_t getNumberOfEntries() const { return entries_.size(); }
76  inline bool getAllowedCollision(const std::string& name1, const std::string& name2) const
77  {
78  auto it = entries_.find(name1);
79  if (it == entries_.end()) return true;
80  return it->second.find(name2) == it->second.end();
81  }
82 
83 private:
84  std::unordered_map<std::string, std::unordered_set<std::string>> entries_;
85 };
86 
88 {
90 
91  ContinuousCollisionProxy() : e1(nullptr), e2(nullptr), in_collision(false), time_of_contact(-1) {}
92  std::shared_ptr<KinematicElement> e1;
93  std::shared_ptr<KinematicElement> e2;
98 
99  // Contact information, filled in if in continuous contact:
100  double penetration_depth = 0.0;
101  Eigen::Vector3d contact_normal;
102  Eigen::Vector3d contact_pos; // In world frame
103 
104  inline std::string Print() const
105  {
106  std::stringstream ss;
107  if (e1 && e2)
108  {
109  ss << "ContinuousCollisionProxy: '" << e1->segment.getName() << "' - '" << e2->segment.getName() << " in_collision: " << in_collision << " time_of_contact " << time_of_contact << " depth: " << penetration_depth;
110  }
111  else
112  {
113  ss << "ContinuousCollisionProxy (empty)";
114  }
115  return ss.str();
116  }
117 };
118 
120 {
122 
123  CollisionProxy() : e1(nullptr), e2(nullptr), distance(0) {}
124  std::shared_ptr<KinematicElement> e1;
125  std::shared_ptr<KinematicElement> e2;
126  Eigen::Vector3d contact1;
127  Eigen::Vector3d normal1;
128  Eigen::Vector3d contact2;
129  Eigen::Vector3d normal2;
130  double distance;
131 
132  inline std::string Print() const
133  {
134  std::stringstream ss;
135  if (e1 && e2)
136  {
137  ss << "Proxy: '" << e1->segment.getName() << "' - '" << e2->segment.getName() << "', c1: " << contact1.transpose() << " c2: " << contact2.transpose() << " n1: " << normal1.transpose() << " n2: " << normal2.transpose() << " d: " << distance;
138  }
139  else
140  {
141  ss << "Proxy (empty)";
142  }
143  return ss.str();
144  }
145 };
146 
148 class CollisionScene : public Object, public Uncopyable, public virtual InstantiableBase
149 {
150 public:
152  virtual ~CollisionScene() {}
154  virtual void InstantiateBase(const Initializer& init);
155 
157  virtual void Setup() {}
163  virtual bool IsAllowedToCollide(const std::string& o1, const std::string& o2, const bool& self);
164 
168  virtual bool IsStateValid(bool self = true, double safe_distance = 0.0) = 0;
169 
174  virtual bool IsCollisionFree(const std::string& o1, const std::string& o2, double safe_distance = 0.0) { ThrowPretty("Not implemented!"); }
179  virtual std::vector<CollisionProxy> GetCollisionDistance(bool self) { ThrowPretty("Not implemented!"); }
184  virtual std::vector<CollisionProxy> GetCollisionDistance(const std::string& o1, const std::string& o2) { ThrowPretty("Not implemented!"); }
188  virtual std::vector<CollisionProxy> GetCollisionDistance(const std::string& o1, const bool& self) { ThrowPretty("Not implemented!"); }
193  virtual std::vector<CollisionProxy> GetCollisionDistance(const std::string& o1, const bool& self, const bool& disable_collision_scene_update) { ThrowPretty("Not implemented!"); }
198  virtual std::vector<CollisionProxy> GetCollisionDistance(const std::vector<std::string>& objects, const bool& self) { ThrowPretty("Not implemented!"); }
201  virtual std::vector<CollisionProxy> GetRobotToRobotCollisionDistance(double check_margin) { ThrowPretty("Not implemented!"); }
204  virtual std::vector<CollisionProxy> GetRobotToWorldCollisionDistance(double check_margin) { ThrowPretty("Not implemented!"); }
207  virtual std::vector<std::string> GetCollisionWorldLinks() = 0;
208 
211  virtual std::vector<std::string> GetCollisionRobotLinks() = 0;
212 
221  virtual ContinuousCollisionProxy ContinuousCollisionCheck(const std::string& o1, const KDL::Frame& tf1_beg, const KDL::Frame& tf1_end, const std::string& o2, const KDL::Frame& tf2_beg, const KDL::Frame& tf2_end) { ThrowPretty("Not implemented!"); }
225  virtual std::vector<ContinuousCollisionProxy> ContinuousCollisionCast(const std::vector<std::vector<std::tuple<std::string, Eigen::Isometry3d, Eigen::Isometry3d>>>& motion_transforms) { ThrowPretty("Not implemented!"); }
228  virtual Eigen::Vector3d GetTranslation(const std::string& name) = 0;
229 
231  {
232  acm_ = acm;
233  }
234 
235  bool GetAlwaysExternallyUpdatedCollisionScene() const { return always_externally_updated_collision_scene_; }
237  {
238  always_externally_updated_collision_scene_ = value;
239  }
240 
241  double GetRobotLinkScale() const { return robot_link_scale_; }
242  void SetRobotLinkScale(const double scale)
243  {
244  if (scale < 0.0)
245  ThrowPretty("Link scaling needs to be greater than or equal to 0");
246  robot_link_scale_ = scale;
247  needs_update_of_collision_objects_ = true;
248  }
249 
250  double GetWorldLinkScale() const { return world_link_scale_; }
251  void SetWorldLinkScale(const double scale)
252  {
253  if (scale < 0.0)
254  ThrowPretty("Link scaling needs to be greater than or equal to 0");
255  world_link_scale_ = scale;
256  needs_update_of_collision_objects_ = true;
257  }
258 
259  double GetRobotLinkPadding() const { return robot_link_padding_; }
260  void SetRobotLinkPadding(const double padding)
261  {
262  if (padding < 0.0)
263  HIGHLIGHT_NAMED("SetRobotLinkPadding", "Generally, padding should be positive.");
264  robot_link_padding_ = padding;
265  needs_update_of_collision_objects_ = true;
266  }
267 
268  double GetWorldLinkPadding() const { return world_link_padding_; }
269  void SetWorldLinkPadding(const double padding)
270  {
271  if (padding < 0.0)
272  HIGHLIGHT_NAMED("SetRobotLinkPadding", "Generally, padding should be positive.");
273  world_link_padding_ = padding;
274  needs_update_of_collision_objects_ = true;
275  }
276 
277  bool GetReplacePrimitiveShapesWithMeshes() const { return replace_primitive_shapes_with_meshes_; }
278  void SetReplacePrimitiveShapesWithMeshes(const bool value)
279  {
280  replace_primitive_shapes_with_meshes_ = value;
281  needs_update_of_collision_objects_ = true;
282  }
283 
286  virtual void UpdateCollisionObjects(const std::map<std::string, std::weak_ptr<KinematicElement>>& objects) = 0;
287 
289  virtual void UpdateCollisionObjectTransforms() = 0;
290 
291  bool get_replace_cylinders_with_capsules() const { return replace_cylinders_with_capsules_; }
292  void set_replace_cylinders_with_capsules(const bool value)
293  {
294  replace_cylinders_with_capsules_ = value;
295  needs_update_of_collision_objects_ = true;
296  }
297 
298  bool debug_ = false;
299 
301  void AssignScene(std::shared_ptr<Scene> scene)
302  {
303  scene_ = scene;
304  }
305 
306 protected:
308  bool needs_update_of_collision_objects_ = true;
309 
311  std::weak_ptr<Scene> scene_;
312 
315 
317  bool always_externally_updated_collision_scene_ = false;
318 
320  double robot_link_scale_ = 1.0;
321 
323  double world_link_scale_ = 1.0;
324 
326  double robot_link_padding_ = 0.0;
327 
329  double world_link_padding_ = 0.0;
330 
332  bool replace_primitive_shapes_with_meshes_ = false;
333 
335  bool replace_cylinders_with_capsules_ = false;
336 
338  std::string robot_link_replacement_config_ = "";
339 };
340 
341 typedef std::shared_ptr<CollisionScene> CollisionScenePtr;
342 } // namespace exotica
343 
344 #endif // EXOTICA_CORE_COLLISION_SCENE_H_
virtual std::vector< CollisionProxy > GetRobotToRobotCollisionDistance(double check_margin)
Gets the closest distances between links within the robot that are closer than check_margin.
bool GetAlwaysExternallyUpdatedCollisionScene() const
virtual std::vector< CollisionProxy > GetCollisionDistance(bool self)
Computes collision distances.
void SetWorldLinkPadding(const double padding)
std::weak_ptr< Scene > scene_
Stores a pointer to the Scene which owns this CollisionScene.
The class of collision scene.
void SetRobotLinkPadding(const double padding)
std::string Print() const
void AssignScene(std::shared_ptr< Scene > scene)
Sets a scene pointer to the CollisionScene for access to methods.
virtual ContinuousCollisionProxy ContinuousCollisionCheck(const std::string &o1, const KDL::Frame &tf1_beg, const KDL::Frame &tf1_end, const std::string &o2, const KDL::Frame &tf2_beg, const KDL::Frame &tf2_end)
Performs a continuous collision check between two objects with a linear interpolation between two giv...
double GetRobotLinkScale() const
virtual std::vector< ContinuousCollisionProxy > ContinuousCollisionCast(const std::vector< std::vector< std::tuple< std::string, Eigen::Isometry3d, Eigen::Isometry3d >>> &motion_transforms)
Performs a continuous collision check by casting the active objects passed in against the static envi...
#define ThrowPretty(m)
Definition: exception.h:36
void SetACM(const AllowedCollisionMatrix &acm)
AllowedCollisionMatrix & operator=(const AllowedCollisionMatrix &other)
std::shared_ptr< KinematicElement > e1
void SetWorldLinkScale(const double scale)
EIGEN_MAKE_ALIGNED_OPERATOR_NEW ContinuousCollisionProxy()
virtual std::vector< CollisionProxy > GetCollisionDistance(const std::vector< std::string > &objects, const bool &self)
Gets the closest distance of any collision object which is allowed to collide with any collision obje...
bool hasEntry(const std::string &name) const
void SetAlwaysExternallyUpdatedCollisionScene(const bool value)
Eigen::Vector3d contact1
void set_replace_cylinders_with_capsules(const bool value)
Eigen::Vector3d contact2
bool get_replace_cylinders_with_capsules() const
bool GetReplacePrimitiveShapesWithMeshes() const
The class of EXOTica Scene.
Definition: scene.h:69
void setEntry(const std::string &name1, const std::string &name2)
virtual std::vector< CollisionProxy > GetCollisionDistance(const std::string &o1, const std::string &o2)
Computes collision distances between two objects.
virtual std::vector< CollisionProxy > GetCollisionDistance(const std::string &o1, const bool &self)
Gets the closest distance of any collision object which is allowed to collide with any collision obje...
double GetRobotLinkPadding() const
#define HIGHLIGHT_NAMED(name, x)
Definition: printable.h:62
virtual std::vector< CollisionProxy > GetRobotToWorldCollisionDistance(double check_margin)
Gets the closest distances between links of the robot and the environment that are closer than check_...
std::shared_ptr< CollisionScene > CollisionScenePtr
AllowedCollisionMatrix acm_
The allowed collision matrix.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW CollisionProxy()
double GetWorldLinkScale() const
virtual bool IsCollisionFree(const std::string &o1, const std::string &o2, double safe_distance=0.0)
Checks if two objects are in collision.
virtual std::vector< CollisionProxy > GetCollisionDistance(const std::string &o1, const bool &self, const bool &disable_collision_scene_update)
Gets the closest distance of any collision object which is allowed to collide with any collision obje...
bool getAllowedCollision(const std::string &name1, const std::string &name2) const
std::shared_ptr< KinematicElement > e2
void getAllEntryNames(std::vector< std::string > &names) const
double distance(const urdf::Pose &transform)
virtual void Setup()
Setup additional construction that requires initialiser parameter.
void SetRobotLinkScale(const double scale)
std::shared_ptr< KinematicElement > e2
std::shared_ptr< KinematicElement > e1
std::unordered_map< std::string, std::unordered_set< std::string > > entries_
AllowedCollisionMatrix(const AllowedCollisionMatrix &acm)
void SetReplacePrimitiveShapesWithMeshes(const bool value)
double GetWorldLinkPadding() const


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Sat Apr 10 2021 02:34:49