se2ez
collision.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef SE2EZ_CORE_BOX2D_COLLISION_
4 #define SE2EZ_CORE_BOX2D_COLLISION_
5 
6 #include <mutex>
7 
9 #include <se2ez/core/collision.h>
10 
11 namespace se2ez
12 {
13  /** \cond IGNORE */
17  SE2EZ_CLASS_FORWARD(Box2DListener)
19  /** \endcond */
20 
21  /** \class se2ez::Box2DWorldPtr
22  \brief A shared pointer wrapper for se2ez::Box2DWorld. */
23 
24  /** \class se2ez::Box2DWorldConstPtr
25  \brief A const shared pointer wrapper for se2ez::Box2DWorld. */
26 
27  /** \class se2ez::Box2DShapePtr
28  \brief A shared pointer wrapper for se2ez::Box2DShape. */
29 
30  /** \class se2ez::Box2DShapeConstPtr
31  \brief A const shared pointer wrapper for se2ez::Box2DShape. */
32 
33  /** \class se2ez::Box2DCollisionManagerPtr
34  \brief A shared pointer wrapper for se2ez::Box2DCollisionManager. */
35 
36  /** \class se2ez::Box2DCollisionManagerConstPtr
37  \brief A const shared pointer wrapper for se2ez::Box2DCollisionManager. */
38 
39  /** \brief A collisions manager that uses Box2D as the underlying collision checking engine.
40  */
42  {
43  public:
44  /** \brief Constructor.
45  * \param[in] robot Robot to construct collision manager for.
46  */
47  Box2DCollisionManager(const RobotPtr &robot);
48 
49  /** \brief Destructor.
50  */
51  virtual ~Box2DCollisionManager();
52 
53  /** \brief Compiles internal data structures for collision checking. Needs to be called if the robot
54  * is updated.
55  */
56  void compile() override;
57 
58  // non-copyable
61 
62  /** \brief Check if a state is in collision.
63  * \param[in] state State to check for collisions using an allowed collision map.
64  * \return Returns true if state is in collision, false otherwise.
65  */
66  bool collide(StatePtr &state) override;
67 
68  /** \brief Check if a state is in collision.
69  * \param[in] state State to check for collisions using an allowed collision map.
70  * \param[out] frames What frames are in collision.
71  * \return Returns true if state is in collision, false otherwise.
72  */
73  bool collide(StatePtr &state, std::set<std::string> &frames) override;
74 
75  /** \brief Computes the signed distance of a scene to collision.
76  * \param[in] state State to computed signed distance for.
77  * \return Returns signed distance.
78  */
79  SignedDistance distance(StatePtr &state) override;
80 
81  /** \brief Computes the signed distance of a scene to collision.
82  * \param[in] state State to computed signed distance for.
83  * \param[out] frames Distance of frame to collision.
84  * \return Returns signed distance.
85  */
86  SignedDistance distance(StatePtr &state, SignedDistanceMap &frames) override;
87 
88  private:
89  /** \brief Updates internal world structure based upon robot state.
90  * \param[in] state State to update to.
91  */
92  void update(StatePtr &state);
93 
94  /** \brief Callback function for checking contacts. Takes in as argument the two fixtures that are a
95  * part of the collision, as well as the contact information. Returns true if iteration should
96  * continue, false if it should terminate early.
97  */
99 
100  /** \brief Check all Box2D contacts in the world, and call a callback function for each.
101  * \param[in] callback Callback function to call for each collision.
102  * \return True if checked all without terminating, false if terminated early.
103  */
104  bool checkContacts(const ContactCallback &callback);
105 
106  /** \brief Callback function for checking all potential collisions. Takes in as argument the two
107  * fixtures that are a part of the collision. Returns true if iteration should continue, false if it
108  * should terminate early.
109  */
111 
112  /** \brief Check all potential collisions in the world, and call a callback function for each.
113  * \param[in] callback Callback function to call for each pair.
114  * \return True if checked all without terminating, false if terminated early.
115  */
116  bool checkPotentials(const PotentialCallback &callback);
117 
118  /** \brief Compute distance from a contact.
119  * \param[in] contact Contact.
120  * \return distance between shapes in contact.
121  */
122  SignedDistance computeDistance(const Box2DContact &contact) const;
123 
124  /** \brief Compute distances between two Box2DShapes.
125  * \param[in] fa First shape.
126  * \param[in] fb Second shape.
127  * \return distance between shapes.
128  */
129  SignedDistance computeDistance(const Box2DShape *fa, const Box2DShape *fb) const;
130 
131  const RobotPtr robot_; ///< Robot model used for collision checking.
132  Box2DWorldPtr world_{nullptr}; ///< Box2D world model.
133  std::map<std::string, std::vector<Box2DShapePtr>> shapes_; ///< Map of frames to Box2D shapes.
134  std::mutex mutex_; ///< Mutex for collision checking.
135  };
136 } // namespace se2ez
137 
138 #endif
A shared pointer wrapper for se2ez::State.
An abstract class that all collision managers must implement.
Definition: collision.h:70
std::mutex mutex_
Mutex for collision checking.
Definition: collision.h:134
A shared pointer wrapper for se2ez::Box2DWorld.
A collisions manager that uses Box2D as the underlying collision checking engine. ...
Definition: collision.h:41
A class that contains all information about the signed distance of a frame to another.
Definition: collision.h:75
const RobotPtr robot_
Robot model used for collision checking.
Definition: collision.h:131
std::map< std::string, std::vector< Box2DShapePtr > > shapes_
Map of frames to Box2D shapes.
Definition: collision.h:133
Container class for Box2D&#39;s contact representation.
Definition: collision.cpp:42
A shared pointer wrapper for se2ez::Robot.
Container class that converts se2ez::Geometry into a Box2D geometry.
Definition: collision.cpp:66
Main namespace.
Definition: collision.h:11
Container class for Box2D&#39;s world representation.
Definition: collision.cpp:20
#define SE2EZ_CLASS_FORWARD(C)
Definition: class_forward.h:9