SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSSimHelper.cpp
Go to the documentation of this file.
1 #include "SENSSimHelper.h"
2 
4  SENSOrientation*& orientation,
5  SENSCamera*& camera,
6  const std::string& simDataDir,
7  std::function<void(void)> cameraParametersChangedCB)
8  : _gpsIn(gps),
9  _orientationIn(orientation),
10  _cameraIn(camera),
11  _gpsRef(gps),
12  _orientationRef(orientation),
13  _cameraRef(camera),
14  _simDataDir(Utils::unifySlashes(simDataDir)),
15  _cameraParametersChangedCB(cameraParametersChangedCB)
16 {
20  Utils::log("SENSSimHelper", "Could not create simDataDir!");
21 }
22 
24 {
25  resetRecorder();
27 }
28 
30 {
31  //restore input sensors and state
32  if (_gpsRef != _gpsIn)
33  {
34  if (_gpsRef && _gpsRef->isRunning())
35  _gpsRef->stop();
36  _gpsRef = _gpsIn;
37  }
38 
40  {
44  }
45 
46  if (_cameraRef != _cameraIn)
47  {
48  if (_cameraRef && _cameraRef->started())
49  _cameraRef->stop();
51  }
52 }
53 
55 {
56  if (!_recorder)
57  return false;
58 
59  //sensors should have been activated before
60  return _recorder->start();
61 }
62 
64 {
65  if (_recorder)
66  _recorder->stop();
67 }
68 
70 {
71  if (recordGps)
72  {
73  if (!recorder()->activateGps(_gpsRef))
75  }
76  else
77  {
78  if (!recorder()->deactivateGps())
80  }
81 }
82 
84 {
86  {
87  if (!recorder()->activateOrientation(_orientationRef))
89  }
90  else
91  {
92  if (!recorder()->deactivateOrientation())
94  }
95 }
96 
98 {
99  if (recordCamera)
100  {
101  if (!recorder()->activateCamera(_cameraRef))
103  }
104  else
105  {
106  if (!recorder()->deactivateOrientation())
108  }
109 }
110 
112 {
113  if (!_recorder)
114  _recorder = std::make_unique<SENSRecorder>(_simDataDir);
115  return _recorder.get();
116 }
117 
119 {
120  if (_recorder)
121  return _recorder->isRunning();
122  else
123  return false;
124 }
125 
127 {
128  if (_recorder)
129  _recorder.reset();
130 
131  recordGps = false;
132  recordOrientation = false;
133  recordCamera = false;
134 }
135 
136 bool SENSSimHelper::getRecorderErrors(std::vector<std::string>& errorMsgs)
137 {
138  if (_recorder)
139  {
140  std::string gpsError;
141  if (_recorder->getGpsHandlerError(gpsError))
142  errorMsgs.emplace_back(gpsError);
143  std::string orientationError;
144  if (_recorder->getOrientationHandlerError(orientationError))
145  errorMsgs.emplace_back(orientationError);
146  std::string cameraError;
147  if (_recorder->getCameraHandlerError(cameraError))
148  errorMsgs.emplace_back(cameraError);
149 
150  return errorMsgs.size() != 0;
151  }
152  else
153  return false;
154 }
155 
157 {
158  if (_simulator && _simulator->getGpsSensorPtr())
159  return true;
160  else
161  return false;
162 }
163 
165 {
166  if (_simulator && _simulator->getOrientationSensorPtr())
167  return true;
168  else
169  return false;
170 }
171 
173 {
174  if (_simulator && _simulator->getCameraSensorPtr())
175  return true;
176  else
177  return false;
178 }
179 
180 void SENSSimHelper::initSimulator(const std::string& simDataSet)
181 {
182  resetRecorder();
183  //stop running simulations
184  if (canSimGps() && simulateGps)
185  _simulator->getGpsSensorPtr()->stop();
187  _simulator->getOrientationSensorPtr()->stop();
188  if (canSimCamera() && simulateCamera)
189  _simulator->getCameraSensorPtr()->stop();
190 
191  simulateGps = false;
192  simulateOrientation = false;
193  simulateCamera = false;
197 
198  if (_simulator)
199  _simulator.reset();
200 
201  _simulator = std::make_unique<SENSSimulator>(_simDataDir + simDataSet);
202 }
203 
205 {
206  if (_simulator && _simulator->isRunning())
207  return true;
208  else
209  return false;
210 }
211 
213 {
214  if (_gpsRef && _gpsRef->isRunning())
215  _gpsRef->stop();
216 
217  if (canSimGps() && simulateGps)
218  _gpsRef = _simulator->getGpsSensorPtr();
219  else
220  _gpsRef = _gpsIn;
221 }
222 
224 {
227 
229  _orientationRef = _simulator->getOrientationSensorPtr();
230  else
232 }
233 
235 {
236  if (_cameraRef && _cameraRef->started())
237  _cameraRef->stop();
238 
239  if (canSimCamera() && simulateCamera)
240  _cameraRef = _simulator->getCameraSensorPtr();
241  else
243 
246 }
247 
248 bool SENSSimHelper::getSimulatorErrors(std::vector<std::string>& errorMsgs)
249 {
250  if (_simulator)
251  return _simulator->getSimulatorErrors(errorMsgs);
252  else
253  return false;
254 }
255 
257 {
258  //stop running sensors
259  if (_gpsRef && _gpsRef->isRunning())
260  _gpsRef->stop();
261 
264 
265  if (_cameraRef && _cameraRef->started())
266  _cameraRef->stop();
267 }
268 
270 {
271  //stop running sensors
272  stopSim();
273 
274  //start possible simulated sensors
275  if (canSimGps() && simulateGps)
276  {
277  _gpsRef = _simulator->getGpsSensorPtr();
278  _gpsRef->start();
279  }
280 
282  {
283  _orientationRef = _simulator->getOrientationSensorPtr();
285  }
286 
287  if (canSimCamera() && simulateCamera)
288  {
289  _cameraRef = _simulator->getCameraSensorPtr();
290  //there is only one prop:
291  const SENSCaptureProps& capProps = _cameraRef->captureProperties();
292  if (capProps.size())
293  {
294  const SENSCameraDeviceProps* currCamProps = &capProps.front();
295  auto streamConfig = currCamProps->streamConfigs().front();
296  //first start the camera, the intrinsic is valid afterwards
297  _cameraRef->start(currCamProps->deviceId(),
298  streamConfig,
299  true);
302  }
303  }
304 }
305 
307 {
308  if (_simulator && _simulator->isPaused())
309  return true;
310  else
311  return false;
312 }
313 
315 {
316  if (_simulator)
317  _simulator->pause();
318 }
319 
321 {
322  if (_simulator)
323  _simulator->resume();
324 }
325 
327 {
328  if (_simulator)
329  return _simulator->now();
330  else
331  return SENSClock::now();
332 }
334 {
335  if (_simulator)
336  return _simulator->passedTime();
337  else
338  return SENSMicroseconds(0);
339 }
std::chrono::microseconds SENSMicroseconds
Definition: SENS.h:58
std::chrono::high_resolution_clock::time_point SENSTimePt
Definition: SENS.h:57
const std::string & deviceId() const
Definition: SENSCamera.h:85
const std::vector< SENSCameraStreamConfig > & streamConfigs() const
Definition: SENSCamera.h:84
Pure abstract camera class.
Definition: SENSCamera.h:160
virtual const SENSCameraConfig & start(std::string deviceId, const SENSCameraStreamConfig &streamConfig, bool provideIntrinsics=true)=0
virtual bool started() const =0
virtual const SENSCaptureProps & captureProperties()=0
Get SENSCaptureProps which contains necessary information about all available camera devices and thei...
virtual void stop()=0
Stop a started camera device.
bool isRunning()
Definition: SENSGps.h:32
virtual bool start()=0
virtual void stop()=0
virtual void stop()=0
virtual bool start()=0
SENSRecorder * recorder()
bool canSimOrientation()
SENSCamera * _cameraIn
Definition: SENSSimHelper.h:68
SENSOrientation *& _orientationRef
Definition: SENSSimHelper.h:71
bool getRecorderErrors(std::vector< std::string > &errorMsgs)
SENSTimePt simTime()
SENSGps * _gpsIn
Definition: SENSSimHelper.h:66
void updateOrientationRecording()
SENSCamera *& _cameraRef
Definition: SENSSimHelper.h:72
bool getSimulatorErrors(std::vector< std::string > &errorMsgs)
std::string _simDataDir
Definition: SENSSimHelper.h:74
std::unique_ptr< SENSSimulator > _simulator
Definition: SENSSimHelper.h:63
void updateCameraSim()
void stopRecording()
void updateOrientationSim()
bool simulateOrientation
Definition: SENSSimHelper.h:56
SENSGps *& _gpsRef
Definition: SENSSimHelper.h:70
bool recordOrientation
Definition: SENSSimHelper.h:52
void initSimulator(const std::string &simDataSet)
std::unique_ptr< SENSRecorder > _recorder
Definition: SENSSimHelper.h:64
SENSMicroseconds passedSimTime()
void restoreInputSensors()
bool recorderIsRunning()
SENSOrientation * _orientationIn
Definition: SENSSimHelper.h:67
void updateGpsRecording()
std::function< void(void)> _cameraParametersChangedCB
Definition: SENSSimHelper.h:77
void updateCameraRecording()
bool startRecording()
SENSSimHelper(SENSGps *&gps, SENSOrientation *&orientation, SENSCamera *&camera, const std::string &simDataDir, std::function< void(void)> cameraParametersChangedCB)
const std::string & simDataDir() const
Definition: SENSSimHelper.h:49
Utils provides utilities for string & file handling, logging and math functions.
Definition: Averaged.h:22
string unifySlashes(const string &inputDir, bool withTrailingSlash)
Returns the inputDir string with unified forward slashes, e.g.: "dirA/dirB/".
Definition: Utils.cpp:367
bool dirExists(const string &path)
Returns true if a directory exists.
Definition: Utils.cpp:789
bool makeDir(const string &path)
Creates a directory with given path.
Definition: Utils.cpp:809
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1100