NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgWorld.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 
19 
26 // This module
27 #include "tgWorld.h"
28 // This application
30 #include "terrain/tgBoxGround.h"
31 // The C++ Standard Library
32 #include <cassert>
33 #include <stdexcept>
34 
35 tgWorld::Config::Config(double g, double ws) :
36 gravity(g),
37 worldSize(ws)
38 {
39  if (ws <= 0.0)
40  {
41  throw std::invalid_argument("worldSize is not postive");
42  }
43 }
44 
50  m_config(),
51  m_pGround(new tgBoxGround()),
52  m_pImpl(new tgWorldBulletPhysicsImpl(m_config, (tgBulletGround*)m_pGround))
53 {
54  // Postcondition
55  assert(invariant());
56 }
57 
63  m_config(config),
64  m_pGround(new tgBoxGround()),
65  m_pImpl(new tgWorldBulletPhysicsImpl(m_config, (tgBulletGround*)m_pGround))
66 {
67  // Postcondition
68  assert(invariant());
69 }
70 
75 tgWorld::tgWorld(const tgWorld::Config& config, tgGround* ground) :
76  m_config(config),
77  m_pGround(ground),
78  m_pImpl(new tgWorldBulletPhysicsImpl(m_config, (tgBulletGround*)m_pGround))
79 {
80  // Postcondition
81  assert(invariant());
82 }
83 
85 {
86  delete m_pImpl;
87  delete m_pGround;
88 }
89 
91 {
92  delete m_pImpl;
93  m_pImpl = new tgWorldBulletPhysicsImpl(m_config, (tgBulletGround*)m_pGround);
94  // Postcondition
95  assert(invariant());
96 }
97 
98 void tgWorld::reset(const tgWorld::Config& config)
99 {
100  // Update the config
101  m_config = config;
102  // Reset as usual
103  reset();
104 
105  // Postcondition
106  assert(invariant());
107 }
108 
109 void tgWorld::reset(tgGround * ground)
110 {
111  delete m_pGround;
112 
113  m_pGround = ground;
114 
115  // Reset as usual
116  reset();
117 }
118 
119 void tgWorld::step(double dt) const
120 {
121  if (dt <= 0.0)
122  {
123  throw std::invalid_argument("dt is not postive");
124  }
125  else
126  {
127  // Forward to the implementation
128  m_pImpl->step(dt);
129  }
130 }
131 
132 // Add a function that returns the amount of gravity in the world.
133 // This is useful for calculating the forces applied by rigid bodies
134 // inside models (e.g., ForcePlateModel.)
136 {
137  // return the value from the config.
138  return m_config.gravity;
139 }
140 
141 bool tgWorld::invariant() const
142 {
143  return m_pImpl != 0;
144 }
~tgWorld()
Definition: tgWorld.cpp:84
Contains the definition of class tgWorldBulletPhysicsImpl.
double gravity
Definition: tgWorld.h:53
void step(double dt) const
Definition: tgWorld.cpp:119
double getWorldGravity() const
Definition: tgWorld.cpp:135
Contains the definition of class tgWorld $Id$.
Contains the definition of class tgBoxGround.
tgWorld()
Definition: tgWorld.cpp:49
void reset()
Definition: tgWorld.cpp:90
virtual void step(double dt)=0