DART  6.6.2
SpecializedNodeManager.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_SPECIALIZEDNODEMANAGER_HPP_
34 #define DART_DYNAMICS_DETAIL_SPECIALIZEDNODEMANAGER_HPP_
35 
37 
38 namespace dart {
39 namespace dynamics {
40 
41 // This preprocessor token should only be used by the unittest that is
42 // responsible for checking that the specialized routines are being used to
43 // access specialized Aspects
44 #ifdef DART_UNITTEST_SPECIALIZED_NODE_ACCESS
45 bool usedSpecializedNodeAccess;
46 #endif // DART_UNITTEST_SPECIALIZED_NODE_ACCESS
47 
48 //==============================================================================
49 template <class SpecNode>
51 {
52  mNodeMap[typeid( SpecNode )] = std::vector<Node*>();
53  mSpecNodeIterator = mNodeMap.find(typeid( SpecNode ));
54 }
55 
56 //==============================================================================
57 template <class SpecNode>
58 template <class NodeType>
60 {
61  return _getNumNodes(type<NodeType>());
62 }
63 
64 //==============================================================================
65 template <class SpecNode>
66 template <class NodeType>
68 {
69  return _getNode(type<NodeType>(), index);
70 }
71 
72 //==============================================================================
73 template <class SpecNode>
74 template <class NodeType>
75 const NodeType* BodyNodeSpecializedFor<SpecNode>::getNode(std::size_t index) const
76 {
77  return const_cast<BodyNodeSpecializedFor<SpecNode>*>(this)->
78  _getNode(type<NodeType>(), index);
79 }
80 
81 //==============================================================================
82 template <class SpecNode>
83 template <class NodeType>
85 {
86  return _isSpecializedForNode(type<NodeType>());
87 }
88 
89 //==============================================================================
90 template <class SpecNode>
91 template <class NodeType>
93 {
94  return detail::BasicNodeManagerForBodyNode::getNumNodes<NodeType>();
95 }
96 
97 //==============================================================================
98 template <class SpecNode>
100 {
101 #ifdef DART_UNITTEST_SPECIALIZED_NODE_ACCESS
102  usedSpecializedNodeAccess = true;
103 #endif // DART_UNITTEST_SPECIALIZED_NODE_ACCESS
104 
105  return mSpecNodeIterator->second.size();
106 }
107 
108 //==============================================================================
109 template <class SpecNode>
110 template <class NodeType>
112 {
113  return detail::BasicNodeManagerForBodyNode::getNode<NodeType>(index);
114 }
115 
116 //==============================================================================
117 template <class SpecNode>
119 {
120 #ifdef DART_UNITTEST_SPECIALIZED_NODE_ACCESS
121  usedSpecializedNodeAccess = true;
122 #endif // DART_UNITTEST_SPECIALIZED_NODE_ACCESS
123 
124  return static_cast<SpecNode*>(
125  getVectorObjectIfAvailable(index, mSpecNodeIterator->second));
126 }
127 
128 //==============================================================================
129 template <class SpecNode>
130 template <class NodeType>
132 {
133  return false;
134 }
135 
136 //==============================================================================
137 template <class SpecNode>
139 {
140  return true;
141 }
142 
143 //==============================================================================
144 template <class SpecNode>
146 {
147  mSpecializedTreeNodes[typeid( SpecNode )] = &mTreeSpecNodeIterators;
148 
149  mNodeNameMgrMap[typeid( SpecNode )] = common::NameManager<Node*>();
150  mSpecNodeNameMgrIterator = mNodeNameMgrMap.find(typeid( SpecNode ));
151 }
152 
153 //==============================================================================
154 template <class SpecNode>
155 template <class NodeType>
157  std::size_t treeIndex) const
158 {
159  return _getNumNodes(type<NodeType>(), treeIndex);
160 }
161 
162 //==============================================================================
163 template <class SpecNode>
164 template <class NodeType>
166  std::size_t treeIndex, std::size_t nodeIndex)
167 {
168  return _getNode(type<NodeType>(), treeIndex, nodeIndex);
169 }
170 
171 //==============================================================================
172 template <class SpecNode>
173 template <class NodeType>
175  std::size_t treeIndex, std::size_t nodeIndex) const
176 {
177  return const_cast<SkeletonSpecializedFor<SpecNode>*>(this)->
178  _getNode(type<NodeType>(), treeIndex, nodeIndex);
179 }
180 
181 //==============================================================================
182 template <class SpecNode>
183 template <class NodeType>
185  const std::string& name)
186 {
187  return _getNode(type<NodeType>(), name);
188 }
189 
190 //==============================================================================
191 template <class SpecNode>
192 template <class NodeType>
194  const std::string& name) const
195 {
196  return const_cast<SkeletonSpecializedFor<SpecNode>*>(this)->
197  _getNode(type<NodeType>(), name);
198 }
199 
200 //==============================================================================
201 template <class SpecNode>
202 template <class NodeType>
204 {
205  return _isSpecializedForNode(type<NodeType>());
206 }
207 
208 //==============================================================================
209 template <class SpecNode>
210 template <class NodeType>
212  type<NodeType>, std::size_t treeIndex) const
213 {
214  return detail::BasicNodeManagerForSkeleton::getNumNodes<NodeType>(treeIndex);
215 }
216 
217 //==============================================================================
218 template <class SpecNode>
220  type<SpecNode>, std::size_t treeIndex) const
221 {
222 #ifdef DART_UNITTEST_SPECIALIZED_NODE_ACCESS
223  usedSpecializedNodeAccess = true;
224 #endif // DART_UNITTEST_SPECIALIZED_NODE_ACCESS
225 
226  if(treeIndex >= mTreeNodeMaps.size())
227  {
228  dterr << "[Skeleton::getNumNodes<" << typeid(SpecNode).name() << ">] "
229  << "Requested tree index (" << treeIndex << "), but there are only ("
230  << mTreeNodeMaps.size() << ") trees available\n";
231  assert(false);
232  return 0;
233  }
234 
235  return mTreeSpecNodeIterators[treeIndex]->second.size();
236 }
237 
238 //==============================================================================
239 template <class SpecNode>
240 template <class NodeType>
242  type<NodeType>, std::size_t treeIndex, std::size_t nodeIndex)
243 {
244  return detail::BasicNodeManagerForSkeleton::getNode<NodeType>(
245  treeIndex, nodeIndex);
246 }
247 
248 //==============================================================================
249 template <class SpecNode>
251  type<SpecNode>, std::size_t treeIndex, std::size_t nodeIndex)
252 {
253 #ifdef DART_UNITTEST_SPECIALIZED_NODE_ACCESS
254  usedSpecializedNodeAccess = true;
255 #endif // DART_UNITTEST_SPECIALIZED_NODE_ACCESS
256 
257  if(treeIndex >= mTreeNodeMaps.size())
258  {
259  dterr << "[Skeleton::getNode<" << typeid(SpecNode).name() << ">] "
260  << "Requested tree index (" << treeIndex << "), but there are only ("
261  << mTreeNodeMaps.size() << ") trees available\n";
262  assert(false);
263  return nullptr;
264  }
265 
266  NodeMap::iterator& it = mTreeSpecNodeIterators[treeIndex];
267 
268  if(nodeIndex >= it->second.size())
269  {
270  dterr << "[Skeleton::getNode<" << typeid(SpecNode).name() << ">] "
271  << "Requested index (" << nodeIndex << ") within tree (" << treeIndex
272  << "), but there are only (" << it->second.size() << ") Nodes of the "
273  << "requested type within that tree\n";
274  assert(false);
275  return nullptr;
276  }
277 
278  return static_cast<SpecNode*>(it->second[nodeIndex]);
279 }
280 
281 //==============================================================================
282 template <class SpecNode>
283 template <class NodeType>
285  type<NodeType>, const std::string& name)
286 {
287  return detail::BasicNodeManagerForSkeleton::getNode<NodeType>(name);
288 }
289 
290 //==============================================================================
291 template <class SpecNode>
293  type<SpecNode>, const std::string& name)
294 {
295 #ifdef DART_UNITTEST_SPECIALIZED_NODE_ACCESS
296  usedSpecializedNodeAccess = true;
297 #endif // DART_UNITTEST_SPECIALIZED_NODE_ACCESS
298 
299  return static_cast<SpecNode*>(
300  mSpecNodeNameMgrIterator->second.getObject(name));
301 }
302 
303 //==============================================================================
304 template <class SpecNode>
305 template <class NodeType>
308 {
309  return false;
310 }
311 
312 //==============================================================================
313 template <class SpecNode>
316 {
317  return true;
318 }
319 
320 } // namespace dynamics
321 } // namespace dart
322 
323 #endif // DART_DYNAMICS_DETAIL_SPECIALIZEDNODEMANAGER_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
class NameManager
Definition: NameManager.hpp:62
BodyNodeSpecializedFor allows classes that inherit BodyNode to have constant-time access to a specifi...
Definition: SpecializedNodeManager.hpp:57
Declaration of the variadic template.
Definition: SpecializedNodeManager.hpp:49
SkeletonSpecializedForNode allows classes that inherit Skeleton to have constant-time access to a spe...
Definition: SpecializedNodeManager.hpp:129
Declaration of the variadic template.
Definition: SpecializedNodeManager.hpp:120
static T getVectorObjectIfAvailable(std::size_t index, const std::vector< T > &vec)
Definition: StlHelpers.hpp:51
std::multimap< dart::dynamics::Shape *, SimpleFrameShapeDnD * >::iterator iterator
Definition: Viewer.cpp:622
Definition: BulletCollisionDetector.cpp:63