Representation.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013-2015, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_COLLISION_REPRESENTATION_H
17 #define SURGSIM_COLLISION_REPRESENTATION_H
18 
19 #include <boost/thread/mutex.hpp>
20 #include <list>
21 #include <memory>
22 #include <unordered_map>
23 #include <unordered_set>
24 
26 #include "SurgSim/Framework/Log.h"
28 #include "SurgSim/Math/Aabb.h"
29 #include "SurgSim/Math/Shape.h"
30 
31 namespace SurgSim
32 {
33 
34 namespace Math
35 {
36 class Shape;
37 };
38 
39 namespace Physics
40 {
41 class Representation;
42 };
43 
44 namespace Collision
45 {
46 struct Contact;
48 
49 typedef std::unordered_map<std::shared_ptr<SurgSim::Collision::Representation>,
50  std::list<std::shared_ptr<SurgSim::Collision::Contact>>> ContactMapType;
51 
53 enum CollisionDetectionType : SURGSIM_ENUM_TYPE;
54 
61 {
62 public:
65  explicit Representation(const std::string& name);
66 
68  virtual ~Representation();
69 
72  virtual int getShapeType() const = 0;
73 
76  void setCollisionDetectionType(CollisionDetectionType type);
77 
80  CollisionDetectionType getCollisionDetectionType() const;
81 
84  void setSelfCollisionDetectionType(CollisionDetectionType type);
85 
88  CollisionDetectionType getSelfCollisionDetectionType() const;
89 
92  virtual const std::shared_ptr<SurgSim::Math::Shape> getShape() const = 0;
93 
96  virtual std::shared_ptr<SurgSim::Math::Shape> getPosedShape();
97 
100 
105 
110  void addContact(const std::shared_ptr<Representation>& other,
111  const std::shared_ptr<SurgSim::Collision::Contact>& contact);
112 
116  bool collidedWith(const std::shared_ptr<Representation>& other);
117 
120  virtual void update(const double& dt);
121 
123  virtual void updateShapeData();
124 
126  virtual void updateDcdData();
127 
131  virtual void updateCcdData(double timeOfImpact);
132 
137  bool ignore(const std::string& fullName);
138 
143  bool ignore(const std::shared_ptr<Representation>& representation);
144 
150  void setIgnoring(const std::vector<std::string>& fullNames);
151 
155  bool isIgnoring(const std::string& fullName) const;
156 
160  bool isIgnoring(const std::shared_ptr<Representation>& representation) const;
161 
170  bool allow(const std::string& fullName);
171 
180  bool allow(const std::shared_ptr<Representation>& representation);
181 
187  void setAllowing(const std::vector<std::string>& fullNames);
188 
192  bool isAllowing(const std::string& fullName) const;
193 
197  bool isAllowing(const std::shared_ptr<Representation>& representation) const;
198 
200  Math::Aabbd getBoundingBox() const;
201 
202 protected:
205 
208  std::vector<std::string> getIgnoring() const;
209 
212  std::vector<std::string> getAllowing() const;
213 
214  void doRetire() override;
215 
217  void setPosedShapeMotion(const Math::PosedShapeMotion<std::shared_ptr<Math::Shape>>& posedShape);
218 
219  std::shared_ptr<Framework::Logger> m_logger;
220 
221 private:
223  CollisionDetectionType m_collisionDetectionType;
224 
226  CollisionDetectionType m_selfCollisionDetectionType;
227 
232 
234  boost::mutex m_collisionsMutex;
235 
238 
240  mutable boost::shared_mutex m_posedShapeMotionMutex;
241 
243  std::unordered_set<std::string> m_ignoring;
244 
246  std::unordered_set<std::string> m_allowing;
247 };
248 
249 }; // namespace Collision
250 }; // namespace SurgSim
251 
252 SURGSIM_SERIALIZABLE_ENUM(SurgSim::Collision::CollisionDetectionType,
253  (COLLISION_DETECTION_TYPE_NONE)
254  (COLLISION_DETECTION_TYPE_DISCRETE)
255  (COLLISION_DETECTION_TYPE_CONTINUOUS)
256  (MAX_COLLISION_DETECTION_TYPES))
257 
258 #endif
Shape.h
SurgSim::Collision::Representation::setSelfCollisionDetectionType
void setSelfCollisionDetectionType(CollisionDetectionType type)
Set the type of collision detection to use between this representation and itself.
Definition: Representation.cpp:57
SurgSim::Collision::Representation::addContact
void addContact(const std::shared_ptr< Representation > &other, const std::shared_ptr< SurgSim::Collision::Contact > &contact)
Add a contact with another representation.
Definition: Representation.cpp:122
SurgSim::Collision::Representation::allow
bool allow(const std::string &fullName)
Set a collision representation to allow Only collisions with "allowed" collision representation will ...
Definition: Representation.cpp:181
SurgSim::Collision::Representation::isAllowing
bool isAllowing(const std::string &fullName) const
Is the collision representation being allowed.
Definition: Representation.cpp:249
SurgSim::Collision::Representation::getCollisions
SurgSim::DataStructures::BufferedValue< ContactMapType > & getCollisions()
A map between collision representations and contacts.
Definition: Representation.cpp:117
SurgSim::Collision::Representation::m_logger
std::shared_ptr< Framework::Logger > m_logger
Definition: Representation.h:219
SurgSim::Collision::Representation::m_selfCollisionDetectionType
CollisionDetectionType m_selfCollisionDetectionType
The type of self collision detection.
Definition: Representation.h:226
SURGSIM_ENUM_TYPE
#define SURGSIM_ENUM_TYPE
Required type of enums used by SURGSIM_SERIALIZABLE_ENUM.
Definition: Accessible.h:261
SurgSim::Collision::ContactMapType
std::unordered_map< std::shared_ptr< SurgSim::Collision::Representation >, std::list< std::shared_ptr< SurgSim::Collision::Contact > > > ContactMapType
Definition: Representation.h:47
SurgSim::Collision::Representation::getSelfCollisionDetectionType
CollisionDetectionType getSelfCollisionDetectionType() const
Get the type of collision detection used between this representation and itself.
Definition: Representation.cpp:62
SurgSim::Math::PosedShapeMotion
PosedShapeMotion is embedding the motion of a PosedShape, providing a posed shape at 2 different inst...
Definition: Shape.h:145
SurgSim::Collision::Representation::invalidatePosedShapeMotion
void invalidatePosedShapeMotion()
Invalidate the cached posed shape motion.
Definition: Representation.cpp:110
SurgSim::Collision::Representation::updateDcdData
virtual void updateDcdData()
Update the data (the shape) in preparation for a DCD contact calculation.
Definition: Representation.cpp:292
SurgSim::Collision::Representation::getCollisionDetectionType
CollisionDetectionType getCollisionDetectionType() const
Get the type of collision detection used between this representation and other representations.
Definition: Representation.cpp:52
SurgSim::Collision::Representation::isIgnoring
bool isIgnoring(const std::string &fullName) const
Is the collision representation being ignored.
Definition: Representation.cpp:235
SurgSim::Collision::Representation::getAllowing
std::vector< std::string > getAllowing() const
Get the only collision representations that this representation is allowed to collide with.
Definition: Representation.cpp:274
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Collision::Representation::updateShapeData
virtual void updateShapeData()
Update the basic Shape's state from the physics state, so that the bounding box can correctly be dete...
Definition: Representation.cpp:302
SurgSim::Collision::Representation::m_collisionDetectionType
CollisionDetectionType m_collisionDetectionType
The type of collision detection.
Definition: Representation.h:223
SurgSim::Collision::Representation::update
virtual void update(const double &dt)
Update the representation.
Definition: Representation.cpp:136
Log.h
SurgSim::Collision::Representation::Representation
Representation(const std::string &name)
Constructor.
Definition: Representation.cpp:28
SurgSim::Collision::Representation
The type of collision detection.
Definition: Representation.h:60
SurgSim::Collision::Representation::getBoundingBox
Math::Aabbd getBoundingBox() const
Definition: Representation.cpp:286
SurgSim::Collision::Representation::ignore
bool ignore(const std::string &fullName)
Set a collision representation to ignore Collisions with this collision representation will not be de...
Definition: Representation.cpp:144
SurgSim::Collision::Representation::getShapeType
virtual int getShapeType() const =0
Get the shape type id.
SurgSim::Math::Aabbd
Eigen::AlignedBox< double, 3 > Aabbd
Wrapper around the Eigen type.
Definition: Aabb.h:30
SurgSim::Framework::Representation
Representations are manifestations of a SceneElement.
Definition: Representation.h:33
SurgSim::Collision::Representation::setCollisionDetectionType
void setCollisionDetectionType(CollisionDetectionType type)
Set the type of collision detection to use between this representation and other representations.
Definition: Representation.cpp:47
SurgSim::DataStructures::BufferedValue< ContactMapType >
SurgSim::Collision::Representation::doRetire
void doRetire() override
Interface to be implemented by derived classes Has a default implementation, does nothing.
Definition: Representation.cpp:279
SURGSIM_SERIALIZABLE_ENUM
SURGSIM_SERIALIZABLE_ENUM(SurgSim::Devices::HandType,(HANDTYPE_LEFT)(HANDTYPE_RIGHT))
SurgSim::Collision::Representation::m_ignoring
std::unordered_set< std::string > m_ignoring
Ignored collision representations.
Definition: Representation.h:243
SurgSim::Collision::Representation::m_collisions
SurgSim::DataStructures::BufferedValue< ContactMapType > m_collisions
A map which associates a list of contacts with each collision representation.
Definition: Representation.h:231
SurgSim::Collision::Representation::m_posedShapeMotionMutex
boost::shared_mutex m_posedShapeMotionMutex
Mutex to lock write access to m_posedShapeMotion.
Definition: Representation.h:240
BufferedValue.h
Representation.h
SurgSim::Collision::Representation::m_allowing
std::unordered_set< std::string > m_allowing
Allowed collision representations.
Definition: Representation.h:246
Aabb.h
SurgSim::Collision::Representation::getPosedShape
virtual std::shared_ptr< SurgSim::Math::Shape > getPosedShape()
Get the shape, posed.
Definition: Representation.cpp:81
SurgSim::Collision::Representation::getShape
virtual const std::shared_ptr< SurgSim::Math::Shape > getShape() const =0
Get the shape.
SurgSim::Collision::Representation::getPosedShapeMotion
const Math::PosedShapeMotion< std::shared_ptr< Math::Shape > > & getPosedShapeMotion() const
Definition: Representation.cpp:67
SurgSim::Collision::Representation::setAllowing
void setAllowing(const std::vector< std::string > &fullNames)
Set the only collision representations to allow collisions with Only Collisions with these collision ...
Definition: Representation.cpp:260
SurgSim::Collision::Representation::getIgnoring
std::vector< std::string > getIgnoring() const
Get the ignored collision representations.
Definition: Representation.cpp:230
SurgSim::Collision::Representation::collidedWith
bool collidedWith(const std::shared_ptr< Representation > &other)
Check whether this collision representation collided with another during the last update.
Definition: Representation.cpp:130
SurgSim::Collision::Representation::~Representation
virtual ~Representation()
Destructor.
Definition: Representation.cpp:42
SurgSim::Collision::Representation::m_collisionsMutex
boost::mutex m_collisionsMutex
Mutex to lock write access to m_collisions.
Definition: Representation.h:234
SurgSim::Collision::Representation::m_posedShapeMotion
Math::PosedShapeMotion< std::shared_ptr< Math::Shape > > m_posedShapeMotion
The shape transformed in space and defined through time, i.e. with 2 differents configurations.
Definition: Representation.h:237
SurgSim::Collision::Representation::setPosedShapeMotion
void setPosedShapeMotion(const Math::PosedShapeMotion< std::shared_ptr< Math::Shape >> &posedShape)
Definition: Representation.cpp:74
SurgSim::Collision::Representation::setIgnoring
void setIgnoring(const std::vector< std::string > &fullNames)
Set the collision representations to ignore Collisions with these collision representation will not b...
Definition: Representation.cpp:216
SurgSim::Collision::Representation::updateCcdData
virtual void updateCcdData(double timeOfImpact)
Update the data (the motionShape) in preparation for a CCD contact calcul ation.
Definition: Representation.cpp:297