NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgSphereInfo.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 
27 // This Module
28 #include "tgSphereInfo.h"
29 
30 // The NTRT Core library
32 
33 // The Bullet Physics Library
34 #include "LinearMath/btVector3.h"
35 #include "BulletCollision/CollisionShapes/btSphereShape.h"
36 
37 // The C++ Standard Library
38 #include <algorithm>
39 #include <stdexcept>
40 #include <cassert>
41 
42 // @todo: Need to take tags into account...
43 
45  m_node(),
46  m_config(config),
47  tgRigidInfo()
48 {}
49 
51  m_node(),
52  m_config(config),
53  tgRigidInfo(tags)
54 {}
55 
56 tgSphereInfo::tgSphereInfo(const tgSphere::Config& config, const tgNode& node) :
57  m_node(node),
58  m_config(config),
59  tgRigidInfo(node.getTags())
60 {}
61 
62 tgSphereInfo::tgSphereInfo(const tgSphere::Config& config, tgTags tags, const tgNode& node) :
63  m_node(node),
64  m_config(config),
65  tgRigidInfo( tags + node.getTags() )
66 {}
67 
69 {
70  return new tgSphereInfo(m_config, node);
71 }
72 
74 {
75  tgRigidInfo::initRigidBody(world);
76  assert(m_collisionObject != NULL);
77  getRigidBody()->setFriction(m_config.friction);
78  getRigidBody()->setRollingFriction(m_config.rollFriction);
79  getRigidBody()->setRestitution(m_config.restitution);
80 }
81 
82 tgModel* tgSphereInfo::createModel(tgWorld& world)
83 {
84  // @todo: handle tags -> model
85  // @todo: check to make sure the rigidBody has been built
86  // @todo: Do we need to use the group here?
87 
88  // Just in case it hasn't been done already...
89  initRigidBody(world);
90 
91  #if (0)
92  std::cout << "creating sphere with tags " << getTags() << std::endl;
93  #endif
94 
95  tgSphere* sphere = new tgSphere(getRigidBody(), getTags());
96 
97  return sphere;
98 }
99 
100 btCollisionShape* tgSphereInfo::getCollisionShape(tgWorld& world) const
101 {
102  if (m_collisionShape == NULL)
103  {
104  const double radius = m_config.radius;
106  new btSphereShape(radius);
107 
108  // Add the collision shape to the array so we can delete it later
109  tgWorldBulletPhysicsImpl& bulletWorld =
111  bulletWorld.addCollisionShape(m_collisionShape);
112  }
113  return m_collisionShape;
114 }
115 
116 double tgSphereInfo::getMass() const
117 {
118  const double radius = m_config.radius;
119  const double density = m_config.density;
120  const double volume = 4.0 / 3.0 * M_PI * radius * radius * radius;
121  return volume * density;
122 }
123 
124 btVector3
125 tgSphereInfo::getConnectionPoint(const btVector3& referencePoint,
126  const btVector3& destinationPoint) const
127 {
128  return getConnectionPoint(referencePoint, destinationPoint, 0);
129 }
130 
131 btVector3
132 tgSphereInfo::getConnectionPoint(const btVector3& referencePoint,
133  const btVector3& destinationPoint,
134  const double rotation) const
135 {
136  if (referencePoint == destinationPoint)
137  {
138  throw
139  std::invalid_argument("Destination point is the reference point.");
140  }
141 
142  if (rotation != 0)
143  {
144  std::cerr << "Rotation not yet supported due to lack of central axis" << std::endl;
145  }
146 
147  // Get the center point
148  const btVector3 startPoint = (getNode());
149  // Vector from reference point to destination point
150  const btVector3 refToDest =
151  (destinationPoint - referencePoint).normalize();
152 
153  // Project along the radius to the destination point
154 
155  const btVector3 surfaceVector = refToDest
156  * m_config.radius;
157 
158  // Return the the surface point closest to the reference point in the
159  // direction of the destination point.
160  return startPoint + surfaceVector;
161 }
162 
163 std::set<tgRigidInfo*> tgSphereInfo::getLeafRigids()
164 {
165  std::set<tgRigidInfo*> leaves;
166  leaves.insert(this);
167  return leaves;
168 }
169 
170 std::set<btVector3> tgSphereInfo::getContainedNodes() const {
171  std::set<btVector3> contained;
172  contained.insert(getNode());
173  contained.insert(getNode());
174  return contained;
175 }
const double density
Definition: tgSphere.h:71
Definition of class tgSphereInfo.
const btVector3 & getNode() const
Definition: tgSphereInfo.h:111
const double friction
Definition: tgSphere.h:75
Contains the definition of class tgWorldBulletPhysicsImpl.
const double rollFriction
Definition: tgSphere.h:79
tgSphereInfo(const tgSphere::Config &config)
virtual void initRigidBody(tgWorld &world)
tgRigidInfo * createRigidInfo(const tgNode &node)
virtual btCollisionShape * getCollisionShape(tgWorld &world) const
const double radius
Definition: tgSphere.h:68
virtual double getMass() const
virtual btRigidBody * getRigidBody()
btCollisionObject * m_collisionObject
Definition: tgRigidInfo.h:347
tgWorldImpl & implementation() const
Definition: tgWorld.h:102
virtual std::set< tgRigidInfo * > getLeafRigids()
Definition: tgNode.h:45
virtual btVector3 getConnectionPoint(const btVector3 &referencePoint, const btVector3 &destinationPoint) const
void addCollisionShape(btCollisionShape *pShape)
virtual std::set< btVector3 > getContainedNodes() const
const double restitution
Definition: tgSphere.h:83
Definition: tgTags.h:44
btCollisionShape * m_collisionShape
Definition: tgRigidInfo.h:332