opensurgsim
ElementContactFilter.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013-2016, 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_ELEMENTCONTACTFILTER_H
17 #define SURGSIM_COLLISION_ELEMENTCONTACTFILTER_H
18 
19 #include "SurgSim/Collision/ContactFilter.h"
20 #include "SurgSim/DataStructures/Location.h"
21 #include "SurgSim/Framework/LockedContainer.h"
22 
23 namespace SurgSim
24 {
25 
26 namespace Physics
27 {
28 class PhysicsManagerState;
29 }
30 
31 namespace Collision
32 {
33 class CollisionPair;
34 class Representation;
35 struct Contact;
36 
37 SURGSIM_STATIC_REGISTRATION(ElementContactFilter);
38 
42 {
43 public:
44  explicit ElementContactFilter(const std::string& name);
45 
47 
48  bool doInitialize() override;
49 
50  bool doWakeUp() override;
51 
56  void setFilter(const std::shared_ptr<Framework::Component>& other, const std::vector<size_t>& indices);
57 
61  const std::vector<size_t>& getFilter(const std::shared_ptr<Framework::Component>& other) const;
62 
65  void setRepresentation(const std::shared_ptr<SurgSim::Framework::Component>& val);
66 
68  std::shared_ptr<SurgSim::Collision::Representation> getRepresentation() const;
69 
70 protected:
71 
72  typedef std::vector<std::pair<std::shared_ptr<SurgSim::Framework::Component>, std::vector<size_t>>> FilterMapType;
73 
74  void setFilterElements(const FilterMapType& filterElements);
75 
76  FilterMapType getFilterElements();
77 
78  void doFilterContacts(
79  const std::shared_ptr<Physics::PhysicsManagerState>& state,
80  const std::shared_ptr<CollisionPair>& pair) override;
81 
82 
83  void doUpdate(double dt) override;
84 
85 private:
86 
87  std::shared_ptr<SurgSim::Framework::Logger> m_logger;
88 
90  std::shared_ptr<Collision::Representation> m_representation;
91 
92  mutable boost::mutex m_writeMutex;
93  std::unordered_map<Framework::Component*, std::vector<size_t>> m_writeBuffer;
94 
95  std::unordered_map<Framework::Component*, std::vector<size_t>> m_filters;
96 
101  void executeFilter(
102  const std::shared_ptr<CollisionPair>& pair,
103  size_t pairIndex,
104  const std::vector<size_t>& filter);
105 };
106 
113 template <class T>
114 const T& pairAt(const std::pair<T, T>& p, size_t i)
115 {
116  SURGSIM_ASSERT(i == 0 || i == 1) << "Index for pair must be 0 or 1.";
117  return (i == 0) ? p.first : p.second;
118 };
119 
126 template <class T>
127 T& pairAt(std::pair<T, T>& p, size_t i) // NOLINT
128 {
129  SURGSIM_ASSERT(i == 0 || i == 1) << "Index for pair must be 0 or 1.";
130  return (i == 0) ? p.first : p.second;
131 };
132 
133 }
134 }
135 
136 #endif
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
Given a DeformableCollisionRepresentation this filter can remove contacts on specific elements of tha...
Definition: ElementContactFilter.h:41
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
Base class to implement a contact filter, the job of this class is to be executed by the ContactFilte...
Definition: ContactFilter.h:40