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