DART 6.13.2
Loading...
Searching...
No Matches
BodyNode.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2022, 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_BODYNODE_HPP_
34#define DART_DYNAMICS_DETAIL_BODYNODE_HPP_
35
36#include <utility>
37
40
41namespace dart {
42namespace dynamics {
43
44//==============================================================================
45template <class JointType>
47 BodyNode* _newParent, const typename JointType::Properties& _joint)
48{
49 if (nullptr == _newParent)
50 return getSkeleton()->template moveBodyNodeTree<JointType>(
51 this, getSkeleton(), nullptr, _joint);
52 else
53 return getSkeleton()->template moveBodyNodeTree<JointType>(
54 this, _newParent->getSkeleton(), _newParent, _joint);
55}
56
57//==============================================================================
58template <class JointType>
60 const SkeletonPtr& _newSkeleton,
61 BodyNode* _newParent,
62 const typename JointType::Properties& _joint)
63{
64 return getSkeleton()->template moveBodyNodeTree<JointType>(
65 this, _newSkeleton, _newParent, _joint);
66}
67
68//==============================================================================
69template <class JointType>
71 const std::string& _skeletonName,
72 const typename JointType::Properties& _joint)
73{
75 skel->setName(_skeletonName);
76 moveTo<JointType>(skel, nullptr, _joint);
77 return skel;
78}
79
80//==============================================================================
81template <class JointType>
83 const typename JointType::Properties& _joint)
84{
85 return moveTo<JointType>(getParentBodyNode(), _joint);
86}
87
88//==============================================================================
89template <class JointType>
90std::pair<JointType*, BodyNode*> BodyNode::copyTo(
91 BodyNode* _newParent,
92 const typename JointType::Properties& _joint,
93 bool _recursive)
94{
95 if (nullptr == _newParent)
96 return getSkeleton()->cloneBodyNodeTree<JointType>(
97 this, getSkeleton(), nullptr, _joint, _recursive);
98 else
99 return getSkeleton()->cloneBodyNodeTree<JointType>(
100 this, _newParent->getSkeleton(), _newParent, _joint, _recursive);
101}
102
103//==============================================================================
104template <class JointType>
105std::pair<JointType*, BodyNode*> BodyNode::copyTo(
106 const SkeletonPtr& _newSkeleton,
107 BodyNode* _newParent,
108 const typename JointType::Properties& _joint,
109 bool _recursive) const
110{
111 return getSkeleton()->cloneBodyNodeTree<JointType>(
112 this, _newSkeleton, _newParent, _joint, _recursive);
113}
114
115//==============================================================================
116template <class JointType>
118 const std::string& _skeletonName,
119 const typename JointType::Properties& _joint,
120 bool _recursive) const
121{
123 skel->setName(_skeletonName);
124 copyTo<JointType>(skel, nullptr, _joint, _recursive);
125 return skel;
126}
127
128//==============================================================================
129template <class JointType, class NodeType>
130std::pair<JointType*, NodeType*> BodyNode::createChildJointAndBodyNodePair(
131 const typename JointType::Properties& _jointProperties,
132 const typename NodeType::Properties& _bodyProperties)
133{
134 return getSkeleton()->createJointAndBodyNodePair<JointType, NodeType>(
135 this, _jointProperties, _bodyProperties);
136}
137
138//==============================================================================
139template <class NodeType, typename... Args>
140NodeType* BodyNode::createNode(Args&&... args)
141{
142 NodeType* node = new NodeType(this, std::forward<Args>(args)...);
143 node->attach();
144
145 return node;
146}
147
148//==============================================================================
149template <class ShapeNodeProperties>
151 ShapeNodeProperties properties, bool automaticName)
152{
153 if (automaticName)
154 {
155 properties.mName
156 = getName() + "_ShapeNode_" + std::to_string(getNumShapeNodes());
157 }
158
159 return createNode<ShapeNode>(properties);
160}
161
162//==============================================================================
163template <class ShapeType>
164ShapeNode* BodyNode::createShapeNode(const std::shared_ptr<ShapeType>& shape)
165{
167 properties.mShape = shape;
168
169 return createShapeNode(properties, true);
170}
171
172//==============================================================================
173template <class ShapeType, class StringType>
175 const std::shared_ptr<ShapeType>& shape, StringType&& name)
176{
178 properties.mShape = shape;
179 properties.mName = std::forward<StringType>(name);
180
181 return createShapeNode(properties, false);
182}
183
184//==============================================================================
185template <class... Aspects>
187{
188 return createShapeNodeWith<Aspects...>(
189 shape, getName() + "_ShapeNode_" + std::to_string(getNumShapeNodes()));
190}
191
192//==============================================================================
193template <class... Aspects>
195 const ShapePtr& shape, const std::string& name)
196{
197 auto shapeNode = createShapeNode(shape, name);
198
199 common::createAspects<ShapeNode, Aspects...>(shapeNode);
200
201 return shapeNode;
202}
203
204//==============================================================================
205template <class AspectT>
207{
208 auto count = 0u;
209 auto numShapeNode = getNumShapeNodes();
210
211 for (auto i = 0u; i < numShapeNode; ++i)
212 {
213 if (getShapeNode(i)->has<AspectT>())
214 ++count;
215 }
216
217 return count;
218}
219
220//==============================================================================
221template <class AspectT>
222const std::vector<ShapeNode*> BodyNode::getShapeNodesWith()
223{
224 std::vector<ShapeNode*> shapeNodes;
225
226 auto numShapeNode = getNumShapeNodes();
227
228 for (auto i = 0u; i < numShapeNode; ++i)
229 {
230 auto shapeNode = getShapeNode(i);
231
232 if (shapeNode->has<AspectT>())
233 shapeNodes.push_back(shapeNode);
234 }
235
236 return shapeNodes;
237}
238
239//==============================================================================
240template <class AspectT>
241const std::vector<const ShapeNode*> BodyNode::getShapeNodesWith() const
242{
243 std::vector<const ShapeNode*> shapeNodes;
244
245 auto numShapeNode = getNumShapeNodes();
246
247 for (auto i = 0u; i < numShapeNode; ++i)
248 {
249 const auto shapeNode = getShapeNode(i);
250
251 if (shapeNode->has<AspectT>())
252 shapeNodes.push_back(shapeNode);
253 }
254
255 return shapeNodes;
256}
257
258//==============================================================================
259template <class AspectT>
261{
262 std::size_t count = 0;
263 ShapeNode* found = nullptr;
264 eachShapeNodeWith<AspectT>([&](ShapeNode* shapeNode) -> bool {
265 if (count++ == index)
266 {
267 found = shapeNode;
268 return false;
269 }
270 return true;
271 });
272
273 return found;
274}
275
276//==============================================================================
277template <class AspectT>
279{
280 std::size_t count = 0;
281 const ShapeNode* found = nullptr;
282 eachShapeNodeWith<AspectT>([&](const ShapeNode* shapeNode) -> bool {
283 if (count++ == index)
284 {
285 found = shapeNode;
286 return false;
287 }
288 return true;
289 });
290
291 return found;
292}
293
294//==============================================================================
295template <class AspectT>
297{
298 eachShapeNodeWith<AspectT>([](ShapeNode* shapeNode) { shapeNode->remove(); });
299}
300
301//==============================================================================
302template <typename AspectT, typename Func>
303void BodyNode::eachShapeNodeWith(Func func) const
304{
305 if constexpr (std::is_same_v<
306 std::invoke_result_t<Func, const ShapeNode*>,
307 bool>)
308 {
309 for (auto i = 0u; i < getNumShapeNodes(); ++i)
310 {
311 const ShapeNode* shapeNode = getShapeNode(i);
312 if (shapeNode->has<AspectT>())
313 {
314 if (!func(shapeNode))
315 return;
316 }
317 }
318 }
319 else
320 {
321 for (auto i = 0u; i < getNumShapeNodes(); ++i)
322 {
323 const ShapeNode* shapeNode = getShapeNode(i);
324 if (shapeNode->has<AspectT>())
325 {
326 func(shapeNode);
327 }
328 }
329 }
330}
331
332//==============================================================================
333template <typename AspectT, typename Func>
335{
336 if constexpr (std::is_same_v<std::invoke_result_t<Func, ShapeNode*>, bool>)
337 {
338 for (auto i = 0u; i < getNumShapeNodes(); ++i)
339 {
340 ShapeNode* shapeNode = getShapeNode(i);
341 if (shapeNode->has<AspectT>())
342 {
343 if (!func(shapeNode))
344 return;
345 }
346 }
347 }
348 else
349 {
350 for (auto i = 0u; i < getNumShapeNodes(); ++i)
351 {
352 ShapeNode* shapeNode = getShapeNode(i);
353 if (shapeNode->has<AspectT>())
354 {
355 func(shapeNode);
356 }
357 }
358 }
359}
360
361} // namespace dynamics
362} // namespace dart
363
364#endif // DART_DYNAMICS_DETAIL_BODYNODE_HPP_
BodyPropPtr properties
Definition SdfParser.cpp:80
std::string * name
Definition SkelParser.cpp:1698
std::size_t index
Definition SkelParser.cpp:1673
const AspectProperties & getAspectProperties() const
Definition EmbeddedAspect.hpp:408
Definition CompositeData.hpp:187
BodyNode class represents a single node of the skeleton.
Definition BodyNode.hpp:80
ShapeNode * getShapeNodeWith(std::size_t index)
Returns the index-th ShapeNode among the ShapeNodes that have a specific Aspect.
Definition BodyNode.hpp:260
const std::string & getName() const override
Return the name of this Entity.
Definition BodyNode.cpp:459
void removeAllShapeNodesWith()
Remove all ShapeNodes containing given Aspect from this BodyNode.
Definition BodyNode.hpp:296
ShapeNode * createShapeNode(ShapeNodeProperties properties, bool automaticName=true)
Create an ShapeNode attached to this BodyNode.
Definition BodyNode.hpp:150
SkeletonPtr getSkeleton() override
Return the Skeleton that this Node is attached to.
Definition BodyNode.cpp:857
std::size_t getNumShapeNodes() const
Definition BodyNode.cpp:953
NodeType * createNode(Args &&... args)
Create some Node type and attach it to this BodyNode.
Definition BodyNode.hpp:140
const std::vector< ShapeNode * > getShapeNodesWith()
Return the list of ShapeNodes containing given Aspect.
Definition BodyNode.hpp:222
JointType * changeParentJointType(const typename JointType::Properties &_joint=typename JointType::Properties())
Change the Joint type of this BodyNode's parent Joint.
Definition BodyNode.hpp:82
std::pair< JointType *, NodeType * > createChildJointAndBodyNodePair(const typename JointType::Properties &_jointProperties=typename JointType::Properties(), const typename NodeType::Properties &_bodyProperties=typename NodeType::Properties())
Create a Joint and BodyNode pair as a child of this BodyNode.
Definition BodyNode.hpp:130
SkeletonPtr split(const std::string &_skeletonName)
Remove this BodyNode and all of its children (recursively) from their current Skeleton and move them ...
Definition BodyNode.cpp:808
SkeletonPtr copyAs(const std::string &_skeletonName, bool _recursive=true) const
Create clones of this BodyNode and all of its children (recursively) and create a new Skeleton with t...
Definition BodyNode.cpp:846
bool moveTo(BodyNode *_newParent)
Remove this BodyNode and all of its children (recursively) from their current parent BodyNode,...
Definition BodyNode.cpp:784
void eachShapeNodeWith(Func func) const
Iterates all the ShapeNodes that has a specific aspect and invokes the callback function.
Definition BodyNode.hpp:303
ShapeNode * createShapeNodeWith(const ShapePtr &shape)
Create a ShapeNode with the specified Aspects and an automatically assigned name: <BodyNodeName>Shape...
Definition BodyNode.hpp:186
ShapeNode * getShapeNode(std::size_t index)
Definition BodyNode.cpp:953
BodyNode * getParentBodyNode()
Return the parent BodyNdoe of this BodyNode.
Definition BodyNode.cpp:881
std::size_t getNumShapeNodesWith() const
Return the number of ShapeNodes containing given Aspect in this BodyNode.
Definition BodyNode.hpp:206
std::pair< Joint *, BodyNode * > copyTo(BodyNode *_newParent, bool _recursive=true)
Create clones of this BodyNode and all of its children recursively (unless _recursive is set to false...
Definition BodyNode.cpp:818
Definition ShapeNode.hpp:49
static SkeletonPtr create(const std::string &_name="Skeleton")
Create a new Skeleton inside of a shared_ptr.
Definition Skeleton.cpp:384
void createAspects(T *)
Attach an arbitrary number of Aspects to the specified Composite type.
Definition Composite.hpp:145
std::shared_ptr< Shape > ShapePtr
Definition SmartPointer.hpp:81
std::shared_ptr< Skeleton > SkeletonPtr
Definition SmartPointer.hpp:60
Definition BulletCollisionDetector.cpp:60