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