NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgRigidInfo.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 
26 // This module
27 #include "tgRigidInfo.h"
28 // This application
29 #include "tgNode.h"
30 #include "tgNodes.h"
31 #include "tgPair.h"
32 #include "tgPairs.h"
33 // The NTRT Core Libary
34 #include "core/tgBulletUtil.h"
35 #include "core/tgTagSearch.h"
36 #include "tgUtil.h"
37 #include "core/tgBulletUtil.h"
38 #include "core/tgWorld.h"
39 
40 
41 // The Bullet Physics library
42 #include "btBulletDynamicsCommon.h"
43 #include "BulletSoftBody/btSoftRigidDynamicsWorld.h"
44 
45 tgRigidInfo* tgRigidInfo::createRigidInfo(const tgNode& node, const tgTagSearch& tagSearch)
46 {
47  // Our subclasses may not be able to create rigidInfos based on nodes. Also, the
48  // tags may not match our search. Make sure both work, or return 0.
49  tgRigidInfo* rigidInfo = 0;
50  if(tagSearch.matches(node.getTags())) {
51  rigidInfo = createRigidInfo(node);
52  }
53  return rigidInfo;
54 }
55 
56 std::vector<tgRigidInfo*> tgRigidInfo::createRigidInfos(const tgNodes& nodes, const tgTagSearch& tagSearch)
57 {
58  std::vector<tgRigidInfo*> result;
59  for(int i = 0; i < nodes.size(); i++) {
60  tgRigidInfo* r = createRigidInfo(nodes[i], tagSearch);
61  if(r != 0) {
62  r->addTags(nodes[i].getTags());
63  result.push_back(r);
64  }
65  }
66  return result;
67 }
68 
69 tgRigidInfo* tgRigidInfo::createRigidInfo(const tgPair& pair, const tgTagSearch& tagSearch)
70 {
71  // Our subclasses may not be able to create rigidInfos based on nodes. Also, the
72  // tags may not match our search. Make sure both work, or return 0.
73  tgRigidInfo* rigidInfo = 0;
74  if(tagSearch.matches(pair.getTags())) {
75  rigidInfo = createRigidInfo(pair);
76  }
77  return rigidInfo;
78 }
79 
80 std::vector<tgRigidInfo*> tgRigidInfo::createRigidInfos(const tgPairs& pairs, const tgTagSearch& tagSearch)
81 {
82  std::vector<tgRigidInfo*> result;
83  for(int i = 0; i < pairs.size(); i++) {
84  tgRigidInfo* r = createRigidInfo(pairs[i], tagSearch);
85  if(r != 0) {
86  result.push_back(r);
87  }
88  }
89  return result;
90 }
91 
92 
93 
94 void tgRigidInfo::initRigidBody(tgWorld& world)
95  {
96  if(!getRigidBody()) {
97 
98  // we want to do this based on group instead the rigid itself; otherwise we throw away autocompounding.
99  tgRigidInfo* rigid = getRigidInfoGroup();
100 
101  // If we're not using autocompounding, use the rigid body itself.
102  // NOTE: This means that auto-compounding can be silently skipped, which means that your parts may not be joined correctly. Do we want that?
103  if(rigid == 0) {
104  rigid = this;
105  }
106 
107  if (rigid->getRigidBody() == NULL) { // Init only if it doesn't have a btRigidBody (has already been initialized)
108 
109  double mass = rigid->getMass();
110  btTransform transform = rigid->getTransform();
111  btCollisionShape* shape = rigid->getCollisionShape(world);
112 
113  btRigidBody* body =
114  tgBulletUtil::createRigidBody(&tgBulletUtil::worldToDynamicsWorld(world),
115  mass,
116  transform,
117  shape);
118  body->setFlags(BT_ENABLE_GYROPSCOPIC_FORCE);
119  rigid->setRigidBody(body);
120  }
121  }
122  }
123 
125 {
126  btRigidBody* body = tgCast::cast<btCollisionObject, btRigidBody>(m_collisionObject);
127  return body;
128 }
129 
130 const btRigidBody* tgRigidInfo::getRigidBody() const {
131  const btRigidBody* body = tgCast::cast<btCollisionObject, btRigidBody>(m_collisionObject);
132  return body;
133 }
134 
135 void tgRigidInfo::setRigidBody(btRigidBody* rigidBody)
136 {
138  m_collisionObject = rigidBody;
139 }
140 
142 {
143  const std::set<btVector3> s1 = getContainedNodes();
144  const std::set<btVector3> s2 = other.getContainedNodes();
145 #if (0)
146  // This would have been preferable, but the compiler barfs
147  std::set<btVector3> intersection;
148  // Ignore the return value
149  std::set_intersection(s1.begin(), s1.end(),
150  s2.begin(), s2.end(),
151  intersection.begin());
152  return intersection.empty();
153 #else
154  std::set<btVector3>::const_iterator ii;
155  std::set<btVector3>::const_iterator ij;
156  for (ii = s1.begin(); ii != s1.end(); ++ii) {
157  for (ij = s2.begin(); ij != s2.end(); ++ij) {
158  if (*ii == *ij)
159  return true;
160  }
161  }
162  return false;
163 #endif
164 }
Contains the definition of class tgTagSearch.
Definition of abstract class tgRigidInfo.
virtual tgRigidInfo * getRigidInfoGroup()
Definition: tgRigidInfo.h:149
virtual bool sharesNodesWith(const tgRigidInfo &other) const
virtual void setRigidBody(btRigidBody *rigidBody)
static btDynamicsWorld & worldToDynamicsWorld(const tgWorld &world)
virtual btCollisionShape * getCollisionShape(tgWorld &world) const =0
Definition of class tgPair.
Definition of class tgPairs.
Definition of class tgNodes.
Definition of class tgNode.
virtual btRigidBody * getRigidBody()
btCollisionObject * m_collisionObject
Definition: tgRigidInfo.h:347
const bool matches(const tgTags &tags) const
Definition: tgTagSearch.h:62
Definition: tgPair.h:48
Contains the definition of class tgWorld $Id$.
Contains the definition of class tgBulletUtil.
Definition: tgNode.h:45
virtual double getMass() const =0
virtual btTransform getTransform() const =0
Rand seeding simular to the evolution and terrain classes.
virtual std::set< btVector3 > getContainedNodes() const =0