NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
tgCompoundRigidInfo.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
7  * under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * http://www.apache.org/licenses/LICENSE-2.0.
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language
16  * governing permissions and limitations under the License.
17 */
18 
25 #include "tgCompoundRigidInfo.h"
26 
27 tgCompoundRigidInfo::tgCompoundRigidInfo() : m_compoundShape(NULL), m_rigidBody(NULL)
28 {
29 }
30 
31 tgModel* tgCompoundRigidInfo::createModel(tgWorld& world)
32 {
33  // @todo: make tgModel a true composite and use that here
34  tgModel* result = new tgModel();
35  for(int i = 0; i < m_rigids.size(); i++) {
36  result->addChild(m_rigids[i]->createModel(world));
37  }
38  return result;
39 }
40 
42 {
43  m_rigids.push_back(&rigid);
44 }
45 
47 {
48  btVector3 sum = btVector3(0.0, 0.0, 0.0);
49  for (int ii = 0; ii < m_rigids.size(); ii++)
50  {
51  /* const */ tgRigidInfo * const rigid = m_rigids[ii];
52  sum += (rigid->getCenterOfMass() * rigid->getMass());
53  }
54  const double totalMass = getMass();
55  return
56  (totalMass == 0.0) ? btVector3(0.0, 0.0, 0.0) : (sum / totalMass);
57 }
58 
59 
60 btCompoundShape* tgCompoundRigidInfo::createCompoundShape(tgWorld& world) const
61 {
62  if (m_compoundShape == 0)
63  {
64  // Deallocated in the destructor
65  m_compoundShape = new btCompoundShape(&world);
66 
67  const btVector3 com = getCenterOfMass();
68 
69  for (int ii = 0; ii < m_rigids.size(); ii++)
70  {
71  tgRigidInfo* const rigid = m_rigids[ii];
72  btTransform t = rigid->getTransform();
73  t.setOrigin(t.getOrigin() - com);
74  m_compoundShape->addChildShape(t, rigid->getCollisionShape(world));
75  }
76 
77  // Add the collision shape to the array so we can delete it later
78  tgWorldBulletPhysicsImpl& bulletWorld =
81  }
82  return m_compoundShape;
83 }
84 
85 btCollisionShape* tgCompoundRigidInfo::getCollisionShape(tgWorld& world) const
86 {
87  if (m_compoundShape == 0)
88  {
90  }
91  return m_compoundShape;
92 }
93 
95 {
96  btTransform t;
97  t.setIdentity();
98  t.setOrigin(getCenterOfMass());
99  return t;
100 }
101 
103 {
105  double mass = 0.0;
106  for (int ii = 0; ii < m_rigids.size(); ii++)
107  {
108  mass += m_rigids[ii]->getMass();
109  }
110  return mass;
111 }
112 
113 void tgCompoundRigidInfo::setRigidBody(btRigidBody* const rigidBody)
114 {
115  m_rigidBody = rigidBody;
116  // Set the rigid body for all components
118  for (int ii = 0; ii < m_rigids.size(); ii++) {
119  m_rigids[ii]->setRigidBody(rigidBody);
120  }
121 }
122 
123 std::set<tgRigidInfo*> tgCompoundRigidInfo::getLeafRigids()
124 {
125  std::set<tgRigidInfo*> leaves;
126  for (int ii = 0; ii < m_rigids.size(); ii++) {
127  tgRigidInfo * const rigid = m_rigids[ii];
128  if (rigid->isCompound())
129  {
130  // Insert the leaves from the compound recursively
131  const std::set<tgRigidInfo*> compoundLeaves =
132  rigid->getLeafRigids();
133  leaves.insert(compoundLeaves.begin(), compoundLeaves.end());
134  }
135  else
136  {
137  leaves.insert(rigid);
138  }
139  }
140  return leaves;
141 }
142 
143 bool tgCompoundRigidInfo::containsNode(const btVector3& nodeVector) const
144 {
146  for (int ii = 0; ii < m_rigids.size(); ii++)
147  {
148  if (m_rigids[ii]->containsNode(nodeVector))
149  {
150  return true;
151  }
152  }
153  return false;
154 }
155 
157 {
159  for (int ii = 0; ii < m_rigids.size(); ii++)
160  {
161  if (m_rigids[ii]->sharesNodesWith(other)) {
162  return true;
163  }
164  }
165  return false;
166 }
167 
168 std::set<btVector3> tgCompoundRigidInfo::getContainedNodes() const
169 {
171  std::set<btVector3> contained;
172  for (int ii = 0; ii < m_rigids.size(); ii++)
173 {
174  const std::set<btVector3> nodes = m_rigids[ii]->getContainedNodes();
175  contained.insert(nodes.begin(), nodes.end());
176  }
177  return contained;
178 }
179 
bool isCompound() const
Definition: tgRigidInfo.h:256
std::vector< tgRigidInfo * > m_rigids
virtual bool containsNode(const btVector3 &nodeVector) const
void addChild(tgModel *pChild)
Definition: tgModel.cpp:126
virtual bool sharesNodesWith(const tgRigidInfo &other) const
Definition of class tgCompoundRigidInfo.
virtual double getMass() const
virtual btCollisionShape * getCollisionShape(tgWorld &world) const =0
virtual std::set< tgRigidInfo * > getLeafRigids()=0
std::set< btVector3 > getContainedNodes() const
virtual btVector3 getCenterOfMass() const
virtual btTransform getTransform() const
virtual btVector3 getCenterOfMass() const =0
tgWorldImpl & implementation() const
Definition: tgWorld.h:97
virtual void setRigidBody(btRigidBody *const rigidBody)
virtual double getMass() const =0
virtual std::set< tgRigidInfo * > getLeafRigids()
void addRigid(tgRigidInfo &rigid)
void addCollisionShape(btCollisionShape *pShape)
virtual btTransform getTransform() const =0
btCompoundShape * m_compoundShape
virtual btCollisionShape * getCollisionShape(tgWorld &world) const
btCompoundShape * createCompoundShape(tgWorld &world) const