NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgBaseRigid.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 "tgBaseRigid.h"
28 #include "tgModelVisitor.h"
29 // The Bullet Physics library
30 #include "BulletDynamics/Dynamics/btRigidBody.h"
31 #include "btBulletDynamicsCommon.h"
32 // The C++ Standard Library
33 #include <cassert>
34 #include <stdexcept>
35 
36 tgBaseRigid::tgBaseRigid(btRigidBody* pRigidBody,
37  const tgTags& tags) :
38  tgModel(tags),
39  m_pRigidBody(pRigidBody),
40  m_mass((m_pRigidBody->getInvMass() > 0.0) ?
41  1.0 / (m_pRigidBody->getInvMass()) :
42  0.0) // The object is static
43 {
44  if (pRigidBody == NULL)
45  {
46  throw std::invalid_argument("Pointer to btRigidBody is NULL");
47  }
48 
49  // Postcondition
50  assert(invariant());
51  assert(m_pRigidBody == pRigidBody);
52 
53  // Supress compiler warning for bullet's unused variable
54  (void) btInfinityMask;
55 }
56 
58 
60 {
61  v.render(*this);
62 
63 }
64 
66 {
67  // World owns this
68  m_pRigidBody = NULL;
70  // Postcondition
71  // This does not preserve the invariant
72 }
73 
74 btVector3 tgBaseRigid::centerOfMass() const
75 {
76  // Precondition
77  assert(m_pRigidBody->getMotionState() != NULL);
78 
79  btTransform transform;
80  m_pRigidBody->getMotionState()->getWorldTransform(transform);
81  const btVector3& result = transform.getOrigin();
82 
83  // Return a copy
84  return result;
85 }
86 
87 btVector3 tgBaseRigid::orientation() const
88 {
89  //Precondition
90  assert(invariant());
91 
92  // get the orientation of this rod w.r.t. the world's refernce coorinate system
93  // oddly enough, there isn't a getEuler method for btQuaternion, which is
94  // returned by getOrientation from RigidBody, so convert it
95  // into a rotation matrix first.
96  btMatrix3x3 rot = btMatrix3x3(m_pRigidBody->getOrientation());
97  btScalar yaw = 0.0;
98  btScalar pitch = 0.0;
99  btScalar roll = 0.0;
100  rot.getEulerYPR(yaw, pitch, roll);
101  btVector3 *result = new btVector3(yaw, pitch, roll);
102  return *result;
103 }
104 
105 bool tgBaseRigid::invariant() const
106 {
107  return
108  (m_pRigidBody != NULL) &&
109  (m_mass >= 0.0);
110 }
virtual void teardown()
Definition: tgModel.cpp:68
btRigidBody * m_pRigidBody
Definition: tgBaseRigid.h:102
Create a box shape as an obstacle or add it to your tensegrity.
virtual void onVisit(const tgModelVisitor &v) const
Definition: tgBaseRigid.cpp:59
virtual ~tgBaseRigid()
Definition: tgBaseRigid.cpp:57
virtual btVector3 centerOfMass() const
Definition: tgBaseRigid.cpp:74
virtual void render(const tgRod &rod) const
Contains the definition of interface class tgModelVisitor.
const double m_mass
Definition: tgBaseRigid.h:105
virtual btVector3 orientation() const
Definition: tgBaseRigid.cpp:87
virtual void teardown()
Definition: tgBaseRigid.cpp:65
Definition: tgTags.h:44