DART  6.6.2
BodyNode.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_BODYNODE_HPP_
34 #define DART_DYNAMICS_DETAIL_BODYNODE_HPP_
35 
36 #include <utility>
37 
39 
40 namespace dart {
41 namespace dynamics {
42 
43 //==============================================================================
44 template <class JointType>
45 JointType* BodyNode::moveTo(BodyNode* _newParent,
46  const typename JointType::Properties& _joint)
47 {
48  if(nullptr == _newParent)
49  return getSkeleton()->moveBodyNodeTree<JointType>(
50  this, getSkeleton(), nullptr, _joint);
51  else
52  return getSkeleton()->moveBodyNodeTree<JointType>(
53  this, _newParent->getSkeleton(), _newParent, _joint);
54 }
55 
56 //==============================================================================
57 template <class JointType>
58 JointType* BodyNode::moveTo(
59  const SkeletonPtr& _newSkeleton, BodyNode* _newParent,
60  const typename JointType::Properties& _joint)
61 {
62  return getSkeleton()->moveBodyNodeTree<JointType>(
63  this, _newSkeleton, _newParent, _joint);
64 }
65 
66 //==============================================================================
67 template <class JointType>
68 SkeletonPtr BodyNode::split(const std::string& _skeletonName,
69  const typename JointType::Properties& _joint)
70 {
72  skel->setName(_skeletonName);
73  moveTo<JointType>(skel, nullptr, _joint);
74  return skel;
75 }
76 
77 //==============================================================================
78 template <class JointType>
80  const typename JointType::Properties& _joint)
81 {
82  return moveTo<JointType>(getParentBodyNode(), _joint);
83 }
84 
85 //==============================================================================
86 template <class JointType>
87 std::pair<JointType*, BodyNode*> BodyNode::copyTo(
88  BodyNode* _newParent,
89  const typename JointType::Properties& _joint,
90  bool _recursive)
91 {
92  if(nullptr == _newParent)
93  return getSkeleton()->cloneBodyNodeTree<JointType>(
94  this, getSkeleton(), nullptr, _joint, _recursive);
95  else
96  return getSkeleton()->cloneBodyNodeTree<JointType>(
97  this, _newParent->getSkeleton(), _newParent, _joint, _recursive);
98 }
99 
100 //==============================================================================
101 template <class JointType>
102 std::pair<JointType*, BodyNode*> BodyNode::copyTo(
103  const SkeletonPtr& _newSkeleton, BodyNode* _newParent,
104  const typename JointType::Properties& _joint,
105  bool _recursive) const
106 {
107  return getSkeleton()->cloneBodyNodeTree<JointType>(
108  this, _newSkeleton, _newParent, _joint, _recursive);
109 }
110 
111 //==============================================================================
112 template <class JointType>
113 SkeletonPtr BodyNode::copyAs(const std::string& _skeletonName,
114  const typename JointType::Properties& _joint, bool _recursive) const
115 {
117  skel->setName(_skeletonName);
118  copyTo<JointType>(skel, nullptr, _joint, _recursive);
119  return skel;
120 }
121 
122 //==============================================================================
123 template <class JointType, class NodeType>
124 std::pair<JointType*, NodeType*> BodyNode::createChildJointAndBodyNodePair(
125  const typename JointType::Properties& _jointProperties,
126  const typename NodeType::Properties& _bodyProperties)
127 {
128  return getSkeleton()->createJointAndBodyNodePair<JointType, NodeType>(
129  this, _jointProperties, _bodyProperties);
130 }
131 
132 //==============================================================================
133 template <class NodeType, typename ...Args>
134 NodeType* BodyNode::createNode(Args&&... args)
135 {
136  NodeType* node = new NodeType(this, std::forward<Args>(args)...);
137  node->attach();
138 
139  return node;
140 }
141 
142 //==============================================================================
143 template <class ShapeNodeProperties>
145  bool automaticName)
146 {
147  if(automaticName)
148  {
149  properties.mName = getName()+"_ShapeNode_"
150  +std::to_string(getNumShapeNodes());
151  }
152 
153  return createNode<ShapeNode>(properties);
154 }
155 
156 //==============================================================================
157 template <class ShapeType>
158 ShapeNode* BodyNode::createShapeNode(const std::shared_ptr<ShapeType>& shape)
159 {
161  properties.mShape = shape;
162 
163  return createShapeNode(properties, true);
164 }
165 
166 //==============================================================================
167 template <class ShapeType, class StringType>
169  const std::shared_ptr<ShapeType>& shape,
170  StringType&& name)
171 {
173  properties.mShape = shape;
174  properties.mName = std::forward<StringType>(name);
175 
176  return createShapeNode(properties, false);
177 }
178 
179 //==============================================================================
180 template <class... Aspects>
182 {
183  return createShapeNodeWith<Aspects...>(shape, getName()+"_ShapeNode_"
184  +std::to_string(getNumShapeNodes()));
185 }
186 
187 //==============================================================================
188 template <class... Aspects>
190  const ShapePtr& shape, const std::string& name)
191 {
192  auto shapeNode = createShapeNode(shape, name);
193 
194  common::createAspects<ShapeNode, Aspects...>(shapeNode);
195 
196  return shapeNode;
197 }
198 
199 //==============================================================================
200 template <class AspectT>
202 {
203  auto count = 0u;
204  auto numShapeNode = getNumShapeNodes();
205 
206  for (auto i = 0u; i < numShapeNode; ++i)
207  {
208  if (getShapeNode(i)->has<AspectT>())
209  ++count;
210  }
211 
212  return count;
213 }
214 
215 //==============================================================================
216 template <class AspectT>
217 const std::vector<ShapeNode*> BodyNode::getShapeNodesWith()
218 {
219  std::vector<ShapeNode*> shapeNodes;
220 
221  auto numShapeNode = getNumShapeNodes();
222 
223  for (auto i = 0u; i < numShapeNode; ++i)
224  {
225  auto shapeNode = getShapeNode(i);
226 
227  if (shapeNode->has<AspectT>())
228  shapeNodes.push_back(shapeNode);
229  }
230 
231  return shapeNodes;
232 }
233 
234 //==============================================================================
235 template <class AspectT>
236 const std::vector<const ShapeNode*> BodyNode::getShapeNodesWith() const
237 {
238  std::vector<const ShapeNode*> shapeNodes;
239 
240  auto numShapeNode = getNumShapeNodes();
241 
242  for (auto i = 0u; i < numShapeNode; ++i)
243  {
244  const auto shapeNode = getShapeNode(i);
245 
246  if (shapeNode->has<AspectT>())
247  shapeNodes.push_back(shapeNode);
248  }
249 
250  return shapeNodes;
251 }
252 
253 //==============================================================================
254 template <class AspectT>
256 {
257  auto shapeNodes = getShapeNodesWith<AspectT>();
258  for (auto shapeNode : shapeNodes)
259  shapeNode->remove();
260 }
261 
262 } // namespace dynamics
263 } // namespace dart
264 
265 #endif // DART_DYNAMICS_DETAIL_BODYNODE_HPP_
BodyPropPtr properties
Definition: SdfParser.cpp:80
std::string * name
Definition: SkelParser.cpp:1642
const AspectProperties & getAspectProperties() const
Definition: EmbeddedAspect.hpp:414
Definition: CompositeData.hpp:180
BodyNode class represents a single node of the skeleton.
Definition: BodyNode.hpp:78
const std::string & getName() const override
Get the name of this Node.
Definition: BodyNode.cpp:427
void removeAllShapeNodesWith()
Remove all ShapeNodes containing given Aspect from this BodyNode.
Definition: BodyNode.hpp:255
ShapeNode * createShapeNode(ShapeNodeProperties properties, bool automaticName=true)
Create an ShapeNode attached to this BodyNode.
Definition: BodyNode.hpp:144
SkeletonPtr getSkeleton() override
Return the Skeleton that this Node is attached to.
Definition: BodyNode.cpp:798
std::size_t getNumShapeNodes() const
Definition: BodyNode.cpp:894
NodeType * createNode(Args &&... args)
Create some Node type and attach it to this BodyNode.
Definition: BodyNode.hpp:134
const std::vector< ShapeNode * > getShapeNodesWith()
Return the list of ShapeNodes containing given Aspect.
Definition: BodyNode.hpp:217
JointType * changeParentJointType(const typename JointType::Properties &_joint=typename JointType::Properties())
Change the Joint type of this BodyNode's parent Joint.
Definition: BodyNode.hpp:79
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:124
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:750
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:787
bool moveTo(BodyNode *_newParent)
Remove this BodyNode and all of its children (recursively) from their current parent BodyNode,...
Definition: BodyNode.cpp:726
ShapeNode * createShapeNodeWith(const ShapePtr &shape)
Create a ShapeNode with the specified Aspects and an automatically assigned name: <BodyNodeName>Shape...
Definition: BodyNode.hpp:181
ShapeNode * getShapeNode(std::size_t index)
Definition: BodyNode.cpp:894
BodyNode * getParentBodyNode()
Return the parent BodyNdoe of this BodyNode.
Definition: BodyNode.cpp:822
std::size_t getNumShapeNodesWith() const
Return the number of ShapeNodes containing given Aspect in this BodyNode.
Definition: BodyNode.hpp:201
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:760
Definition: ShapeNode.hpp:49
static SkeletonPtr create(const std::string &_name="Skeleton")
Create a new Skeleton inside of a shared_ptr.
Definition: Skeleton.cpp:341
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:63