NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
tgSimView.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 "tgModelVisitor.h"
29 #include "tgSimView.h"
30 // The C++ Standard Library
31 #include <cassert>
32 #include <iostream>
33 #include <stdexcept>
34 
36  double stepSize,
37  double renderRate) :
38  m_world(world),
39  m_pSimulation(NULL),
40  m_pModelVisitor(NULL),
41  m_stepSize(stepSize),
42  m_renderRate(renderRate),
43  m_renderTime(0.0),
44  m_initialized(false)
45 {
46  if (m_stepSize < 0.0)
47  {
48  throw std::invalid_argument("stepSize is not positive");
49  }
50  else if (renderRate < m_stepSize)
51  {
52  throw std::invalid_argument("renderRate is less than stepSize");
53  }
54 
55  // Postcondition
56  assert(invariant());
57  assert(m_pSimulation == NULL);
58  assert(m_pModelVisitor == NULL);
59  assert(m_stepSize == stepSize);
60  assert(m_renderRate == renderRate);
61  assert(m_renderTime == 0.0);
62  assert(!m_initialized);
63 }
64 
65 tgSimView::~tgSimView()
66 {
67  if (m_pSimulation != NULL)
68  {
69  // The tgSimView has been passed to a tgSimulation
70  teardown();
71  }
72  delete m_pModelVisitor;
73 }
74 
75 
77 {
78  if (m_pSimulation != NULL)
79  {
80  throw
81  std::invalid_argument("The view already belongs to a simulation.");
82  }
83  else
84  {
85  m_pSimulation = &simulation;
86  tgWorld& world = simulation.getWorld();
87  bindToWorld(world);
88  }
89 
90  // Postcondition
91  assert(invariant());
92  assert(m_pSimulation == &simulation);
93 }
94 
96 {
97  // The destructor that calls this must not fail, so don't assert or throw
98  // on a precondition
99  m_pSimulation = NULL;
100  // The destructor that calls this must not fail, so don't assert a
101  // postcondition
102 }
103 
105 {
106 }
107 
108 void tgSimView::setup()
109 {
110  assert(m_pSimulation != NULL);
111  // Just note that this function was called.
112  // tgSimViewGraphics needs to know for now.
113  m_initialized = true;
114 
115  // Postcondition
116  assert(invariant());
117  assert(m_initialized);
118 }
119 
120 void tgSimView::teardown()
121 {
122  // Just note that this function was called.
123  // tgSimViewGraphics needs to know for now.
124  m_initialized = false;
125 
126  // Postcondition
127  assert(invariant());
128  assert(!m_initialized);
129 }
130 
132  {
133  // This would normally run forever, but this is just for testing
134  run(10);
135  }
136 
137 void tgSimView::run(int steps)
138 {
139  if (m_pSimulation != NULL)
140  {
141  // The tgSimView has been passed to a tgSimulation
142  std::cout << "SimView::run("<<steps<<")" << std::endl;
143  // This would normally run forever, but this is just for testing
144  m_renderTime = 0;
145  double totalTime = 0.0;
146  for (int i = 0; i < steps; i++) {
149  totalTime += m_stepSize;
150 
151  if (m_renderTime >= m_renderRate) {
152  render();
153  //std::cout << totalTime << std::endl;
154  m_renderTime = 0;
155  }
156  }
157  }
158 }
159 
160 void tgSimView::render() const
161 {
162  if ((m_pSimulation != NULL) && (m_pModelVisitor != NULL))
163  {
164  // The tgSimView has been passed to a tgSimulation
165  m_pSimulation->onVisit(*m_pModelVisitor);
166  }
167 }
168 
169 void tgSimView::render(const tgModelVisitor& r) const
170 {
171  if (m_pSimulation != NULL)
172  {
173  // The tgSimView has been passed to a tgSimulation
174  m_pSimulation->onVisit(r);
175  }
176 }
177 
178 // tgSimulation handles calling teardown and setup on this,
179 // since it knows when the new world is available
180 void tgSimView::reset()
181 {
182  if (m_pSimulation != NULL)
183  {
184  // The tgSimView has been passed to a tgSimulation
185  m_pSimulation->reset();
186  }
187 }
188 
189 void tgSimView::setRenderRate(double renderRate)
190 {
191  m_renderRate = (renderRate > m_stepSize) ? renderRate : m_stepSize;
192 
193  // Postcondition
194  assert(invariant());
195 }
196 
197 void tgSimView::setStepSize(double stepSize)
198 {
199  if (stepSize <= 0.0)
200  {
201  throw std::invalid_argument("stepSize is not positive");
202  }
203  else
204  {
205  m_stepSize = stepSize;
206  // Assure that the render rate is no less than the new step size
208  }
209 
210  // Postcondition
211  assert(invariant());
212  assert((stepSize <= 0.0) || (m_stepSize == stepSize));
213 }
214 
215 bool tgSimView::invariant() const
216 {
217  return
218  (m_stepSize >= 0.0) &&
219  (m_renderRate >= m_stepSize) &&
220  (m_renderTime >= 0.0);
221 }
222 
void bindToWorld(tgWorld &world)
Definition: tgSimView.cpp:104
double m_stepSize
Definition: tgSimView.h:169
double m_renderTime
Definition: tgSimView.h:181
void bindToSimulation(tgSimulation &simulation)
Definition: tgSimView.cpp:76
tgModelVisitor * m_pModelVisitor
Definition: tgSimView.h:162
void releaseFromSimulation()
Definition: tgSimView.cpp:95
tgWorld & getWorld() const
Contains the definition of class tgSimulation $Id$.
void setRenderRate(double renderRate)
Definition: tgSimView.cpp:189
Contains the definition of interface class tgModelVisitor.
void setStepSize(double stepSize)
Definition: tgSimView.cpp:197
double m_renderRate
Definition: tgSimView.h:175
tgSimulation * m_pSimulation
Definition: tgSimView.h:155
tgWorld & world()
Definition: tgSimView.h:64
Contains the definition of class tgSimView $Id$.
virtual void run()
Definition: tgSimView.cpp:131
tgSimView(tgWorld &world, double stepSize=1.0/120.0, double renderRate=1.0/60.0)
Definition: tgSimView.cpp:35
void step(double dt) const