DART  6.6.2
BasicNodeManager.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2018, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  * https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  * Redistribution and use in source and binary forms, with or
10  * without modification, are permitted provided that the following
11  * conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef DART_DYNAMICS_DETAIL_BASICNODEMANAGER_HPP_
34 #define DART_DYNAMICS_DETAIL_BASICNODEMANAGER_HPP_
35 
36 #include <map>
37 #include <typeindex>
38 #include <unordered_set>
39 
41 #include "dart/common/Empty.hpp"
42 #include "dart/dynamics/Node.hpp"
43 
44 namespace dart {
45 namespace dynamics {
46 namespace detail {
47 
49 {
50 public:
51 
52  using NodeMap = std::map< std::type_index, std::vector<Node*> >;
53  using NodeDestructorSet = std::unordered_set<NodeDestructorPtr>;
54  using NodeNameMgrMap = std::map< std::type_index, common::NameManager<Node*> >;
55  using SpecializedTreeNodes = std::map<std::type_index, std::vector<NodeMap::iterator>*>;
56 
59 
63 
65  template <class NodeType>
66  std::size_t getNumNodes() const;
67 
69  template <class NodeType>
70  NodeType* getNode(std::size_t index);
71 
73  template <class NodeType>
74  const NodeType* getNode(std::size_t index) const;
75 
77  template <class NodeType>
78  static constexpr bool isSpecializedForNode();
79 
80 protected:
81 
82  template <class T> struct type { };
83 
86 
89 
90 };
91 
93 {
94 public:
95 
98 
101  template <class NodeType>
102  std::size_t getNumNodes(std::size_t treeIndex) const;
103 
106  template <class NodeType>
107  NodeType* getNode(std::size_t treeIndex, std::size_t nodeIndex);
108 
111  template <class NodeType>
112  const NodeType* getNode(std::size_t treeIndex, std::size_t nodeIndex) const;
113 
115  template <class NodeType>
116  NodeType* getNode(const std::string& name);
117 
119  template <class NodeType>
120  const NodeType* getNode(const std::string& name) const;
121 
122 protected:
123 
126 
128  std::vector<NodeMap> mTreeNodeMaps;
129 
137 
138 };
139 
140 //==============================================================================
141 template <class NodeType>
143 {
144  NodeMap::const_iterator it = mNodeMap.find(typeid(NodeType));
145  if(mNodeMap.end() == it)
146  return 0;
147 
148  return it->second.size();
149 }
150 
151 //==============================================================================
152 template <class NodeType>
154 {
155  NodeMap::const_iterator it = mNodeMap.find(typeid(NodeType));
156  if(mNodeMap.end() == it)
157  return nullptr;
158 
159  return static_cast<NodeType*>(
160  getVectorObjectIfAvailable(index, it->second));
161 }
162 
163 //==============================================================================
164 template <class NodeType>
165 const NodeType* BasicNodeManagerForBodyNode::getNode(std::size_t index) const
166 {
167  return const_cast<BasicNodeManagerForBodyNode*>(this)->getNode<NodeType>(index);
168 }
169 
170 //==============================================================================
171 template <class NodeType>
173 {
174  // When invoked through a BasicNodeManager, this should always return false.
175  return false;
176 }
177 
178 //==============================================================================
179 template <class NodeType>
180 std::size_t BasicNodeManagerForSkeleton::getNumNodes(std::size_t treeIndex) const
181 {
182  if(treeIndex >= mTreeNodeMaps.size())
183  {
184  dterr << "[Skeleton::getNumNodes<" << typeid(NodeType).name() << ">] "
185  << "Requested tree index (" << treeIndex << "), but there are only ("
186  << mTreeNodeMaps.size() << ") trees available\n";
187  assert(false);
188  return 0;
189  }
190 
191  const NodeMap& nodeMap = mTreeNodeMaps[treeIndex];
192  NodeMap::const_iterator it = nodeMap.find(typeid(NodeType));
193  if(nodeMap.end() == it)
194  return 0;
195 
196  return it->second.size();
197 }
198 
199 //==============================================================================
200 template <class NodeType>
202  std::size_t treeIndex, std::size_t nodeIndex)
203 {
204  if(treeIndex >= mTreeNodeMaps.size())
205  {
206  dterr << "[Skeleton::getNode<" << typeid(NodeType).name() << ">] "
207  << "Requested tree index (" << treeIndex << "), but there are only ("
208  << mTreeNodeMaps.size() << ") trees available\n";
209  assert(false);
210  return nullptr;
211  }
212 
213  const NodeMap& nodeMap = mTreeNodeMaps[treeIndex];
214  NodeMap::const_iterator it = nodeMap.find(typeid(NodeType));
215  if(nodeMap.end() == it)
216  {
217  dterr << "[Skeleton::getNode<" << typeid(NodeType).name() << ">] "
218  << "Requested index (" << nodeIndex << ") within tree (" << treeIndex
219  << "), but there are no Nodes of the requested type in this tree\n";
220  assert(false);
221  return nullptr;
222  }
223 
224  if(nodeIndex >= it->second.size())
225  {
226  dterr << "[Skeleton::getNode<" << typeid(NodeType).name() << ">] "
227  << "Requested index (" << nodeIndex << ") within tree (" << treeIndex
228  << "), but there are only (" << it->second.size() << ") Nodes of the "
229  << "requested type within that tree\n";
230  assert(false);
231  return nullptr;
232  }
233 
234  return static_cast<NodeType*>(it->second[nodeIndex]);
235 }
236 
237 //==============================================================================
238 template <class NodeType>
240  std::size_t treeIndex, std::size_t nodeIndex) const
241 {
242  return const_cast<BasicNodeManagerForSkeleton*>(this)->getNode<NodeType>(
243  treeIndex, nodeIndex);
244 }
245 
246 //==============================================================================
247 template <class NodeType>
248 NodeType* BasicNodeManagerForSkeleton::getNode(const std::string& name)
249 {
250  NodeNameMgrMap::const_iterator it = mNodeNameMgrMap.find(typeid(NodeType));
251 
252  if(mNodeNameMgrMap.end() == it)
253  return nullptr;
254 
255  return static_cast<NodeType*>(it->second.getObject(name));
256 }
257 
258 //==============================================================================
259 template <class NodeType>
261  const std::string& name) const
262 {
263  return const_cast<BasicNodeManagerForSkeleton*>(
264  this)->getNode<NodeType>(name);
265 }
266 
267 //==============================================================================
268 #define DART_BAKE_SPECIALIZED_NODE_IRREGULAR( TypeName, AspectName, PluralAspectName )\
269  inline std::size_t getNum ## PluralAspectName () const\
270  { return getNumNodes< TypeName >(); }\
271  inline TypeName * get ## AspectName (std::size_t index)\
272  { return getNode< TypeName >(index); }\
273  inline const TypeName * get ## AspectName (std::size_t index) const\
274  { return getNode< TypeName >(index); }
275 
276 //==============================================================================
277 #define DART_BAKE_SPECIALIZED_NODE( AspectName )\
278  DART_BAKE_SPECIALIZED_NODE_IRREGULAR( AspectName, AspectName, AspectName ## s )
279 
280 //==============================================================================
281 #define DART_BAKE_SPECIALIZED_NODE_SKEL_IRREGULAR( TypeName, AspectName, PluralAspectName )\
282  DART_BAKE_SPECIALIZED_NODE_IRREGULAR( TypeName, AspectName, PluralAspectName )\
283  inline std::size_t getNum ## PluralAspectName (std::size_t treeIndex) const\
284  { return getNumNodes< TypeName >(treeIndex); }\
285  inline TypeName * get ## AspectName (std::size_t treeIndex, std::size_t nodeIndex)\
286  { return getNode< TypeName >(treeIndex, nodeIndex); }\
287  inline const TypeName * get ## AspectName (std::size_t treeIndex, std::size_t nodeIndex) const\
288  { return getNode< TypeName >(treeIndex, nodeIndex); }\
289  \
290  inline TypeName * get ## AspectName (const std::string& name)\
291  { return getNode< TypeName >(name); }\
292  inline const TypeName * get ## AspectName (const std::string& name) const\
293  { return getNode< TypeName >(name); }
294 
295 //==============================================================================
296 #define DART_BAKE_SPECIALIZED_NODE_SKEL( AspectName )\
297  DART_BAKE_SPECIALIZED_NODE_SKEL_IRREGULAR( AspectName, AspectName, AspectName ## s)
298 
299 //==============================================================================
300 #define DART_BAKE_SPECIALIZED_NODE_IRREGULAR_DECLARATIONS( TypeName, AspectName, PluralAspectName )\
301  std::size_t getNum ## PluralAspectName () const;\
302  TypeName * get ## AspectName (std::size_t index);\
303  const TypeName * get ## AspectName (std::size_t index) const;
304 
305 //==============================================================================
306 #define DART_BAKE_SPECIALIZED_NODE_DECLARATIONS( AspectName )\
307  DART_BAKE_SPECIALIZED_NODE_IRREGULAR_DECLARATIONS( AspectName, AspectName, AspectName ## s )
308 
309 //==============================================================================
310 #define DART_BAKE_SPECIALIZED_NODE_SKEL_IRREGULAR_DECLARATIONS( TypeName, AspectName, PluralAspectName )\
311  DART_BAKE_SPECIALIZED_NODE_IRREGULAR_DECLARATIONS( TypeName, AspectName, PluralAspectName )\
312  std::size_t getNum ## PluralAspectName (std::size_t treeIndex) const;\
313  TypeName * get ## AspectName (std::size_t treeIndex, std::size_t nodeIndex);\
314  const TypeName * get ## AspectName (std::size_t treeIndex, std::size_t nodeIndex) const;\
315  \
316  TypeName * get ## AspectName (const std::string& name);\
317  const TypeName * get ## AspectName (const std::string& name) const;
318 
319 //==============================================================================
320 #define DART_BAKE_SPECIALIZED_NODE_SKEL_DECLARATIONS( AspectName )\
321  DART_BAKE_SPECIALIZED_NODE_SKEL_IRREGULAR_DECLARATIONS( AspectName, AspectName, AspectName ## s )
322 
323 //==============================================================================
324 #define DART_BAKE_SPECIALIZED_NODE_IRREGULAR_DEFINITIONS( ClassName, TypeName, AspectName, PluralAspectName )\
325  std::size_t ClassName :: getNum ## PluralAspectName () const\
326  { return getNumNodes< TypeName >(); }\
327  TypeName * ClassName :: get ## AspectName (std::size_t index)\
328  { return getNode< TypeName >(index); }\
329  const TypeName * ClassName :: get ## AspectName (std::size_t index) const\
330  { return getNode< TypeName >(index); }
331 
332 //==============================================================================
333 #define DART_BAKE_SPECIALIZED_NODE_DEFINITIONS( ClassName, AspectName )\
334  DART_BAKE_SPECIALIZED_NODE_IRREGULAR_DEFINITIONS( ClassName, AspectName, AspectName, AspectName ## s )
335 
336 //==============================================================================
337 #define DART_BAKE_SPECIALIZED_NODE_SKEL_IRREGULAR_DEFINITIONS( ClassName, TypeName, AspectName, PluralAspectName )\
338  DART_BAKE_SPECIALIZED_NODE_IRREGULAR_DEFINITIONS( ClassName, TypeName, AspectName, PluralAspectName )\
339  std::size_t ClassName :: getNum ## PluralAspectName (std::size_t treeIndex) const\
340  { return getNumNodes< TypeName >(treeIndex); }\
341  TypeName * ClassName :: get ## AspectName (std::size_t treeIndex, std::size_t nodeIndex)\
342  { return getNode< TypeName >(treeIndex, nodeIndex); }\
343  const TypeName * ClassName :: get ## AspectName (std::size_t treeIndex, std::size_t nodeIndex) const\
344  { return getNode< TypeName >(treeIndex, nodeIndex); }\
345  \
346  TypeName * ClassName :: get ## AspectName (const std::string& name)\
347  { return getNode< TypeName >(name); }\
348  const TypeName * ClassName :: get ## AspectName (const std::string& name) const\
349  { return getNode< TypeName >(name); }
350 
351 //==============================================================================
352 #define DART_BAKE_SPECIALIZED_NODE_SKEL_DEFINITIONS( ClassName, AspectName )\
353  DART_BAKE_SPECIALIZED_NODE_SKEL_IRREGULAR_DEFINITIONS( ClassName, AspectName, AspectName, AspectName ## s )
354 
355 } // namespace detail
356 } // namespace dynamics
357 } // namespace dart
358 
359 #endif // DART_DYNAMICS_DETAIL_BASICNODEMANAGER_HPP_
#define dterr
Output an error message.
Definition: Console.hpp:49
std::string * name
Definition: SkelParser.cpp:1642
std::size_t index
Definition: SkelParser.cpp:1617
Definition: BasicNodeManager.hpp:49
BasicNodeManagerForBodyNode()=default
Default constructor.
std::unordered_set< NodeDestructorPtr > NodeDestructorSet
Definition: BasicNodeManager.hpp:53
NodeDestructorSet mNodeDestructors
A set for storing the Node destructors.
Definition: BasicNodeManager.hpp:88
NodeType * getNode(std::size_t index)
Get the Node of the specified type and the specified index.
Definition: BasicNodeManager.hpp:153
std::map< std::type_index, std::vector< NodeMap::iterator > * > SpecializedTreeNodes
Definition: BasicNodeManager.hpp:55
static constexpr bool isSpecializedForNode()
Check if this Manager is specialized for a specific type of Node.
Definition: BasicNodeManager.hpp:172
NodeMap mNodeMap
Map that retrieves the Nodes of a specified type.
Definition: BasicNodeManager.hpp:85
std::map< std::type_index, std::vector< Node * > > NodeMap
Definition: BasicNodeManager.hpp:52
std::size_t getNumNodes() const
Get the number of Nodes corresponding to the specified type.
Definition: BasicNodeManager.hpp:142
BasicNodeManagerForBodyNode(const BasicNodeManagerForBodyNode &)=delete
Delete copy constructors and assignment operators.
BasicNodeManagerForBodyNode & operator=(const BasicNodeManagerForBodyNode &)=delete
std::map< std::type_index, common::NameManager< Node * > > NodeNameMgrMap
Definition: BasicNodeManager.hpp:54
Definition: BasicNodeManager.hpp:93
NodeType * getNode(std::size_t treeIndex, std::size_t nodeIndex)
Get the nodeIndexth Node of the specified type within the tree of treeIndex.
Definition: BasicNodeManager.hpp:201
std::vector< NodeMap > mTreeNodeMaps
A NodeMap for each tree to allow tree Nodes to be accessed independently.
Definition: BasicNodeManager.hpp:128
SpecializedTreeNodes mSpecializedTreeNodes
A map that allows SpecializedNodeManagers to have a direct iterator to the tree-wise storage of its s...
Definition: BasicNodeManager.hpp:136
NodeNameMgrMap mNodeNameMgrMap
NameManager for tracking Nodes.
Definition: BasicNodeManager.hpp:125
static T getVectorObjectIfAvailable(std::size_t index, const std::vector< T > &vec)
Definition: StlHelpers.hpp:51
Definition: BulletCollisionDetector.cpp:63