NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgSimulation.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 "tgSimulation.h"
28 // This application
29 #include "tgModel.h"
30 #include "tgSimView.h"
31 #include "tgSimViewGraphics.h"
32 #include "tgWorld.h"
33 // The Bullet Physics Library
34 #include "LinearMath/btQuickprof.h"
35 
36 // The C++ Standard Library
37 #include <stdexcept>
38 
40  m_view(view)
41 {
42  m_view.bindToSimulation(*this);
43 
44  m_view.setup();
45 
46  // Postcondition
47  assert(invariant());
48 }
49 
50 tgSimulation::~tgSimulation()
51 {
52  teardown();
53  m_view.releaseFromSimulation();
54  for (int i = 0; i < m_models.size(); i++)
55  {
56  delete m_models[i];
57  }
58 }
59 
61 {
62  // Precondition
63  if (pModel == NULL)
64  {
65  throw std::invalid_argument("NULL pointer to tgModel");
66  }
67  else
68  {
69 
70  pModel->setup(m_view.world());
71  m_models.push_back(pModel);
72  }
73 
74  // Postcondition
75  assert(invariant());
76  assert(!m_models.empty());
77 }
78 
80 {
81 #ifndef BT_NO_PROFILE
82  BT_PROFILE("tgSimulation::onVisit");
83 #endif //BT_NO_PROFILE
84  // Removed sending the visitor to the world since it wasn't used
85  // Write a worldVisitor if its necessary
86  for (int i = 0; i < m_models.size(); i++) {
87  m_models[i]->onVisit(r);
88  }
89 }
90 
92 {
93 
94  teardown();
95 
96  m_view.setup();
97  for (int i = 0; i != m_models.size(); i++)
98  {
99 
100  m_models[i]->setup(m_view.world());
101  }
102 }
103 
108 {
109  return m_view.world();
110 }
111 
112 void tgSimulation::step(double dt) const
113 {
114 // Trying to profile here creates trouble for tgLinearString - this is outside of the profile loop
115 
116  if (dt <= 0)
117  {
118  throw std::invalid_argument("dt for step is not positive");
119  }
120  else
121  {
122  // Step the world.
123  // This can be done before or after stepping the models.
124  m_view.world().step(dt);
125 
126  // Step the models
127  for (int i = 0; i < m_models.size(); i++)
128  {
129  m_models[i]->step(dt);
130  }
131  }
132 }
133 
134 void tgSimulation::teardown() const
135 {
136  const size_t n = m_models.size();
137  for (int i = 0; i < n; i++)
138  {
139  tgModel * const pModel = m_models[i];
140  assert(pModel != NULL);
141 
142  pModel->teardown();
143  }
144  // Reset the world after the models - models need world info for
145  // their onTeardown() functions
146  m_view.world().reset();
147  // Postcondition
148  assert(invariant());
149 }
150 
151 void tgSimulation::run() const
152 {
153  m_view.run();
154 }
155 
156 void tgSimulation::run(int steps) const
157 {
158  m_view.run(steps);
159 }
160 
161 bool tgSimulation::invariant() const
162 {
163  return true;
164 }
virtual void teardown()
Definition: tgModel.cpp:68
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:57
void bindToSimulation(tgSimulation &simulation)
Definition: tgSimView.cpp:77
void releaseFromSimulation()
Definition: tgSimView.cpp:96
tgWorld & getWorld() const
void addModel(tgModel *pModel)
Contains the definition of class tgModel.
Contains the definition of class tgSimulation.
Contains the definition of class tgSimViewGraphics.
virtual void setup()
Definition: tgSimView.cpp:109
void step(double dt) const
Definition: tgWorld.cpp:109
Contains the definition of class tgWorld $Id$.
tgSimulation(tgSimView &view)
void onVisit(const tgModelVisitor &r) const
void run() const
tgWorld & world()
Definition: tgSimView.h:65
Contains the definition of class tgSimView.
virtual void run()
Definition: tgSimView.cpp:132
void step(double dt) const
void reset()
Definition: tgWorld.cpp:90