opensurgsim
CollisionPair.h
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_COLLISIONPAIR_H
17 #define SURGSIM_COLLISION_COLLISIONPAIR_H
18 
19 #include <list>
20 #include <memory>
21 
22 #include "SurgSim/Collision/Representation.h"
23 #include "SurgSim/DataStructures/Location.h"
24 #include "SurgSim/Math/Vector.h"
25 
26 
27 namespace SurgSim
28 {
29 namespace Collision
30 {
31 
37 struct Contact
38 {
39  Contact(const CollisionDetectionType& newType,
40  const double& newDepth,
41  const double& newTime,
42  const SurgSim::Math::Vector3d& newContact,
43  const SurgSim::Math::Vector3d& newNormal,
44  const std::pair<SurgSim::DataStructures::Location,
45  SurgSim::DataStructures::Location>& newPenetrationPoints) :
46  type(newType), depth(newDepth), time(newTime), contact(newContact),
47  normal(newNormal), penetrationPoints(newPenetrationPoints), force(SurgSim::Math::Vector3d::Zero())
48  {
49  }
50  std::shared_ptr<Contact> makeComplimentary()
51  {
52  auto complimentary = std::make_shared<Contact>(type, depth, time, contact,
53  -normal, std::make_pair(penetrationPoints.second, penetrationPoints.first));
54  complimentary->force = -force;
55  return complimentary;
56  }
57  bool operator==(const Contact& contact) const
58  {
59  return type == contact.type &&
60  std::abs(time - contact.time) < 1e-8 &&
61  penetrationPoints.first.isApprox(contact.penetrationPoints.first) &&
62  penetrationPoints.second.isApprox(contact.penetrationPoints.second) &&
63  normal.isApprox(contact.normal);
64  }
65  CollisionDetectionType type;
66  double depth;
67  double time;
71  SurgSim::DataStructures::Location> penetrationPoints;
73 };
74 
79 {
80 public:
82  CollisionPair();
83 
85  CollisionPair(const std::shared_ptr<Representation>& first,
86  const std::shared_ptr<Representation>& second);
87 
89  ~CollisionPair();
90 
94  void setRepresentations(const std::shared_ptr<Representation>& first,
95  const std::shared_ptr<Representation>& second);
96 
99  const std::pair<std::shared_ptr<Representation>, std::shared_ptr<Representation>>&
100  getRepresentations() const;
101 
104  CollisionDetectionType getType() const;
105 
107  std::shared_ptr<Representation> getFirst() const;
108 
110  std::shared_ptr<Representation> getSecond() const;
111 
113  bool hasContacts() const;
114 
121  void addCcdContact(const double& depth,
122  const double& time,
123  const SurgSim::Math::Vector3d& contactPoint,
125  const std::pair<SurgSim::DataStructures::Location,
127 
132  void addDcdContact(const double& depth,
133  const SurgSim::Math::Vector3d& normal,
134  const std::pair<SurgSim::DataStructures::Location,
136 
139  void addContact(const std::shared_ptr<Contact>& contact);
140 
142  void updateRepresentations();
143 
145  std::list<std::shared_ptr<Contact>>& getContacts();
146 
148  void clearContacts();
149 
151  void swapRepresentations();
152 
155  bool isSwapped() const;
156 
159  bool mayIntersect() const;
160 
161 private:
163  std::pair<std::shared_ptr<Representation>, std::shared_ptr<Representation>> m_representations;
164 
166  CollisionDetectionType m_type;
167 
169  std::list<std::shared_ptr<Contact>> m_contacts;
170 
171  bool m_isSwapped;
172 };
173 
174 
175 }; // namespace Collision
176 }; // namespace SurgSim
177 
178 template <typename charT, typename traits>
179 std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& out,
181 {
182  out << "Type: " << contact.type << std::endl;
183  out << "Depth: " << contact.depth << std::endl;
184  out << "Time: " << contact.time << std::endl;
185  out << "Contact: " << contact.contact.transpose() << std::endl;
186  out << "Normal: " << contact.normal.transpose() << std::endl;
187  out << "Force: " << contact.force.transpose() << std::endl;
188  out << "Penetration Point 1 :" << contact.penetrationPoints.first << std::endl;
189  out << "Penetration Point 2 :" << contact.penetrationPoints.second << std::endl;
190  return out;
191 }
192 #endif
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
A Location defines a local position w.r.t.
Definition: Location.h:39
SurgSim::Math::Vector3d force
The reaction force to correct this contact.
Definition: CollisionPair.h:72
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
SurgSim::Math::Vector3d normal
The normal on the contact point (normalized)
Definition: CollisionPair.h:69
Collision Pair class, it signifies a pair of items that should be checked with the collision algorith...
Definition: CollisionPair.h:78
double depth
What is the penetration depth for the representation.
Definition: CollisionPair.h:66
double time
What is the time of the collision, CCD only.
Definition: CollisionPair.h:67
std::pair< SurgSim::DataStructures::Location, SurgSim::DataStructures::Location > penetrationPoints
The deepest point inside the opposing object.
Definition: CollisionPair.h:71
Definitions of small fixed-size vector types.
CollisionDetectionType type
What collision algorithm class was used to get the contact.
Definition: CollisionPair.h:65
SurgSim::Math::Vector3d contact
The actual contact point, only used for CCD.
Definition: CollisionPair.h:68
Contact data structure used when two representations touch each other The convention is that if body ...
Definition: CollisionPair.h:37