NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
tgRod.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 // This module
26 #include "tgRod.h"
27 #include "tgModelVisitor.h"
28 // The Bullet Physics library
29 #include "BulletDynamics/Dynamics/btRigidBody.h"
30 #include "btBulletDynamicsCommon.h"
31 // The C++ Standard Library
32 #include <cassert>
33 #include <stdexcept>
34 
35 tgRod::Config::Config(double r, double d,
36  double f, double rf, double res) :
37  radius(r),
38  density(d),
39  friction(f),
40  rollFriction(rf),
41  restitution(res)
42 {
43  if (density < 0.0) { throw std::range_error("Negative density"); }
44  if (radius < 0.0) { throw std::range_error("Negative radius"); }
45  if (friction < 0.0) { throw std::range_error("Negative friction"); }
46  if (rollFriction < 0.0) { throw std::range_error("Negative roll friction"); }
47  if (restitution < 0.0) { throw std::range_error("Negative restitution"); }
48  if (friction > 1.0) { throw std::range_error("Friction > 1"); }
49  if (rollFriction > 1.0) { throw std::range_error("Roll Friction > 1"); }
50  if (restitution > 1.0) { throw std::range_error("Restitution > 1"); }
51  // Postcondition
52  assert(density >= 0.0);
53  assert(radius >= 0.0);
54  assert((friction >= 0.0) && (friction <= 1.0));
55  assert((rollFriction >= 0.0) && (rollFriction <= 1.0));
56  assert((restitution >= 0.0) && (restitution <= 1.0));
57 }
58 
59 tgRod::tgRod(btRigidBody* pRigidBody,
60  const tgTags& tags,
61  const double length) :
62  tgModel(tags),
63  m_pRigidBody(pRigidBody),
64  m_mass((m_pRigidBody->getInvMass() > 0.0) ?
65  1.0 / (m_pRigidBody->getInvMass()) :
66  0.0), // The object is static
67  m_length(length)
68 {
69  if (pRigidBody == NULL)
70  {
71  throw std::invalid_argument("Pointer to btRigidBody is NULL");
72  }
73 
74  // Postcondition
75  assert(invariant());
76  assert(m_pRigidBody == pRigidBody);
77 }
78 
80 
81 void tgRod::onVisit(const tgModelVisitor& v) const
82 {
83  v.render(*this);
84 
85 }
86 
88 {
89  // World should delete the body
90  m_pRigidBody = NULL;
92 
93  // Postcondition
94  // This does not preserve the invariant
95 }
96 
97 btVector3 tgRod::centerOfMass() const
98 {
99  // Precondition
100  assert(m_pRigidBody->getMotionState() != NULL);
101 
102  btTransform transform;
103  m_pRigidBody->getMotionState()->getWorldTransform(transform);
104  const btVector3& result = transform.getOrigin();
105 
106  // Return a copy
107  return result;
108 }
109 
110 bool tgRod::invariant() const
111 {
112  return
113  (m_pRigidBody != NULL) &&
114  (m_mass >= 0.0) &&
115  (m_length >= 0.0);
116 }
Config(double r=0.5, double d=1.0, double f=0.5, double rf=0.0, double res=0.0)
Definition: tgRod.cpp:35
virtual void teardown()
Definition: tgModel.cpp:73
const double rollFriction
Definition: tgRod.h:76
const double density
Definition: tgRod.h:68
const double radius
Definition: tgRod.h:65
btVector3 centerOfMass() const
Definition: tgRod.cpp:97
virtual void render(const tgRod &rod) const
Contains the definition of interface class tgModelVisitor.
double length() const
Definition: tgRod.h:104
virtual void teardown()
Definition: tgRod.cpp:87
const double friction
Definition: tgRod.h:72
const double restitution
Definition: tgRod.h:80
Contains the definition of class tgRod $Id$.
virtual ~tgRod()
Definition: tgRod.cpp:79
Definition: tgTags.h:43
virtual void onVisit(const tgModelVisitor &v) const
Definition: tgRod.cpp:81