ElementContactFilter.h
Go to the documentation of this file.
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 
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 
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
SurgSim::Collision::ElementContactFilter::m_logger
std::shared_ptr< SurgSim::Framework::Logger > m_logger
Definition: ElementContactFilter.h:87
SURGSIM_ASSERT
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
SurgSim::Collision::ContactFilter
Base class to implement a contact filter, the job of this class is to be executed by the ContactFilte...
Definition: ContactFilter.h:41
SurgSim::Collision::ElementContactFilter::FilterMapType
std::vector< std::pair< std::shared_ptr< SurgSim::Framework::Component >, std::vector< size_t > > > FilterMapType
Definition: ElementContactFilter.h:72
SurgSim::Collision::ElementContactFilter::m_filters
std::unordered_map< Framework::Component *, std::vector< size_t > > m_filters
Definition: ElementContactFilter.h:95
SurgSim::Collision::ElementContactFilter::doInitialize
bool doInitialize() override
Interface to be implemented by derived classes.
Definition: ElementContactFilter.cpp:42
LockedContainer.h
SurgSim::Collision::ElementContactFilter::setFilter
void setFilter(const std::shared_ptr< Framework::Component > &other, const std::vector< size_t > &indices)
Set the filter for one representation, this means any contects on the given indices for that represen...
Definition: ElementContactFilter.cpp:74
SurgSim::Collision::pairAt
const T & pairAt(const std::pair< T, T > &p, size_t i)
Get member of pair data via indexed access, the members of the pair have to have the same type const ...
Definition: ElementContactFilter.h:114
SurgSim::Collision::ElementContactFilter::setRepresentation
void setRepresentation(const std::shared_ptr< SurgSim::Framework::Component > &val)
Sets the representation used for filtering, can only be used before initialization.
Definition: ElementContactFilter.cpp:121
SurgSim::Collision::ElementContactFilter::doWakeUp
bool doWakeUp() override
Interface to be implemented by derived classes.
Definition: ElementContactFilter.cpp:47
SurgSim::Collision::ElementContactFilter::SURGSIM_CLASSNAME
SURGSIM_CLASSNAME(SurgSim::Collision::ElementContactFilter)
SurgSim
Definition: CompoundShapeToGraphics.cpp:30
SurgSim::Collision::ElementContactFilter::m_representation
std::shared_ptr< Collision::Representation > m_representation
Representation whose contacts need to be filtered.
Definition: ElementContactFilter.h:90
SurgSim::Collision::ElementContactFilter::ElementContactFilter
ElementContactFilter(const std::string &name)
Definition: ElementContactFilter.cpp:31
SurgSim::Collision::ElementContactFilter::executeFilter
void executeFilter(const std::shared_ptr< CollisionPair > &pair, size_t pairIndex, const std::vector< size_t > &filter)
Run the filter over the side of the collision pair indicated by pairIndex.
Definition: ElementContactFilter.cpp:160
SurgSim::Collision::ElementContactFilter::setFilterElements
void setFilterElements(const FilterMapType &filterElements)
Definition: ElementContactFilter.cpp:94
ContactFilter.h
SurgSim::Collision::ElementContactFilter::m_writeBuffer
std::unordered_map< Framework::Component *, std::vector< size_t > > m_writeBuffer
Definition: ElementContactFilter.h:93
SurgSim::Collision::ElementContactFilter
Given a DeformableCollisionRepresentation this filter can remove contacts on specific elements of tha...
Definition: ElementContactFilter.h:42
SurgSim::Collision::ElementContactFilter::getFilterElements
FilterMapType getFilterElements()
Definition: ElementContactFilter.cpp:109
Location.h
SurgSim::Collision::ElementContactFilter::doFilterContacts
void doFilterContacts(const std::shared_ptr< Physics::PhysicsManagerState > &state, const std::shared_ptr< CollisionPair > &pair) override
Override this with the implementation of the filter, this has to be threadsafe, it will be called in ...
Definition: ElementContactFilter.cpp:133
string
string(TOUPPER ${DEVICE} DEVICE_UPPER_CASE) option(BUILD_DEVICE_$
Definition: CMakeLists.txt:38
SurgSim::Collision::ElementContactFilter::getRepresentation
std::shared_ptr< SurgSim::Collision::Representation > getRepresentation() const
Definition: ElementContactFilter.cpp:128
SurgSim::Collision::ElementContactFilter::getFilter
const std::vector< size_t > & getFilter(const std::shared_ptr< Framework::Component > &other) const
Query the filters for one specific representation.
Definition: ElementContactFilter.cpp:83
SurgSim::Collision::ElementContactFilter::m_writeMutex
boost::mutex m_writeMutex
Definition: ElementContactFilter.h:92
SurgSim::Collision::ElementContactFilter::doUpdate
void doUpdate(double dt) override
Definition: ElementContactFilter.cpp:154