Choreonoid  1.8
CollisionDetector.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_UTIL_COLLISION_DETECTOR_H
7 #define CNOID_UTIL_COLLISION_DETECTOR_H
8 
9 #include "Collision.h"
10 #include "Referenced.h"
11 #include <cnoid/stdx/optional>
12 #include <cstdint>
13 #include "exportdecl.h"
14 
15 namespace cnoid {
16 
17 class SgNode;
18 class CollisionPair;
19 
20 class CNOID_EXPORT CollisionDetector : public Referenced
21 {
22 public:
23  typedef intptr_t GeometryHandle;
24 
25  static bool registerFactory(const std::string& name, std::function<CollisionDetector*()> factory);
26  static int numFactories();
27  static std::string factoryName(int factoryIndex);
28  static int factoryIndex(const std::string& name);
29  static CollisionDetector* create(int factoryIndex);
30 
31  virtual ~CollisionDetector();
32  virtual const char* name() const = 0;
33 
39  virtual CollisionDetector* clone() const = 0;
40 
41  virtual void clearGeometries() = 0;
42  virtual int numGeometries() const = 0;
43 
47  virtual stdx::optional<GeometryHandle> addGeometry(SgNode* geometry) = 0;
48  virtual void setCustomObject(GeometryHandle geometry, Referenced* object) = 0;
49  virtual void setGeometryStatic(GeometryHandle geometry, bool isStatic = true) = 0;
50  virtual void ignoreGeometryPair(GeometryHandle geometry1, GeometryHandle geometry2, bool ignore = true) = 0;
51  virtual bool makeReady() = 0;
52 
53  virtual void updatePosition(GeometryHandle geometry, const Isometry3& position) = 0;
54  virtual void updatePositions(
55  std::function<void(Referenced* object, Isometry3*& out_position)> positionQuery) = 0;
56 
57  virtual void detectCollisions(std::function<void(const CollisionPair& collisionPair)> callback) = 0;
58 };
59 
61 
62 
64 {
65 public:
66  virtual double detectDistance(
68  Vector3& out_point1, Vector3& out_point2) = 0;
69 };
70 
71 
73 {
74  typedef CollisionDetector::GeometryHandle GeometryHandle;
75  GeometryHandle geometries_[2];
76  Referenced* objects_[2];
77  CollisionArray collisions_;
78 
79 public:
81  CollisionPair(GeometryHandle geometry1, GeometryHandle geometry2){
82  geometries_[0] = geometry1;
83  geometries_[1] = geometry2;
84  }
85  CollisionPair(GeometryHandle geometry1, Referenced* object1, GeometryHandle geometry2, Referenced* object2){
86  geometries_[0] = geometry1;
87  geometries_[1] = geometry2;
88  objects_[0] = object1;
89  objects_[1] = object2;
90  }
91  GeometryHandle& geometry(int i) { return geometries_[i]; };
92  const GeometryHandle geometry(int i) const { return geometries_[i]; };
93  const GeometryHandle* geometries() const { return geometries_; }
94  Referenced*& object(int i){ return objects_[i]; };
95  Referenced* object(int i) const { return objects_[i]; };
96  CollisionArray& collisions() { return collisions_; }
97  const CollisionArray& collisions() const { return collisions_; }
98  void addCollision(const Collision& c){ collisions_.push_back(c); }
99  Collision& newCollision() { collisions_.resize(collisions_.size() + 1); return collisions_.back(); }
100  void clearCollisions(){ collisions_.clear(); }
101  bool empty() const { return collisions_.empty(); }
102  int numCollisions() const { return collisions_.size(); }
103 };
104 
105 }
106 
107 #endif
cnoid::CollisionArray
std::vector< Collision > CollisionArray
Definition: Collision.h:27
Referenced.h
cnoid::CollisionPair::addCollision
void addCollision(const Collision &c)
Definition: CollisionDetector.h:98
cnoid::Vector3
Eigen::Vector3d Vector3
Definition: EigenTypes.h:57
cnoid::CollisionPair::collisions
CollisionArray & collisions()
Definition: CollisionDetector.h:96
cnoid::CollisionPair::CollisionPair
CollisionPair()
Definition: CollisionDetector.h:80
cnoid::Isometry3
Eigen::Isometry3d Isometry3
Definition: EigenTypes.h:73
cnoid::CollisionPair::empty
bool empty() const
Definition: CollisionDetector.h:101
cnoid::CollisionPair::collisions
const CollisionArray & collisions() const
Definition: CollisionDetector.h:97
cnoid::CollisionPair::object
Referenced *& object(int i)
Definition: CollisionDetector.h:94
Collision.h
cnoid::ref_ptr< CollisionDetector >
cnoid::CollisionPair::geometry
const GeometryHandle geometry(int i) const
Definition: CollisionDetector.h:92
cnoid::CollisionDetectorDistanceAPI
Definition: CollisionDetector.h:63
cnoid::CollisionDetectorDistanceAPI::detectDistance
virtual double detectDistance(CollisionDetector::GeometryHandle geometry1, CollisionDetector::GeometryHandle geometry2, Vector3 &out_point1, Vector3 &out_point2)=0
cnoid::CollisionPair::newCollision
Collision & newCollision()
Definition: CollisionDetector.h:99
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Collision
Definition: Collision.h:14
cnoid::CollisionDetectorPtr
ref_ptr< CollisionDetector > CollisionDetectorPtr
Definition: CollisionDetector.h:60
cnoid::CollisionPair::clearCollisions
void clearCollisions()
Definition: CollisionDetector.h:100
cnoid::CollisionDetector
Definition: CollisionDetector.h:20
cnoid::CollisionPair
Definition: CollisionDetector.h:72
cnoid::Referenced
Definition: Referenced.h:54
cnoid::CollisionPair::CollisionPair
CollisionPair(GeometryHandle geometry1, Referenced *object1, GeometryHandle geometry2, Referenced *object2)
Definition: CollisionDetector.h:85
cnoid::CollisionDetector::GeometryHandle
intptr_t GeometryHandle
Definition: CollisionDetector.h:23
cnoid::CollisionPair::geometry
GeometryHandle & geometry(int i)
Definition: CollisionDetector.h:91
cnoid::CollisionPair::object
Referenced * object(int i) const
Definition: CollisionDetector.h:95
cnoid::CollisionPair::geometries
const GeometryHandle * geometries() const
Definition: CollisionDetector.h:93
cnoid::CollisionPair::numCollisions
int numCollisions() const
Definition: CollisionDetector.h:102
cnoid::CollisionPair::CollisionPair
CollisionPair(GeometryHandle geometry1, GeometryHandle geometry2)
Definition: CollisionDetector.h:81
cnoid::SgNode
Definition: SceneGraph.h:157