Helios++
Helios software for LiDAR simulations
Simulation.h
1 #pragma once
2 #include <boost/asio/thread_pool.hpp>
3 #include <boost/asio/post.hpp>
4 
5 #include "Scanner.h"
6 #include "MeasurementsBuffer.h"
7 #include "Color4f.h"
8 #include "ThreadPool.h"
9 #include <SimulationCycleCallback.h>
10 #ifdef PYTHON_BINDING
11 #include <PySimulationCycleCallback.h>
12 #endif
13 
17 class Simulation {
18 protected:
19  // *** ATTRIBUTES *** //
20  // ******************** //
24  static const long NANOSECONDS_PER_SECOND = 1000000000;
25 
29  unsigned numSysThreads = std::thread::hardware_concurrency(); //may return 0 when not able to detect
35 
39  double mSimSpeedFactor = 1;
40 
45  std::shared_ptr<Scanner> mScanner = NULL;
46 
50  size_t simFrequency = 0;
55  std::mutex mutex;
60  std::condition_variable condvar;
66  std::shared_ptr<std::unique_lock<std::mutex>> pauseLock = nullptr;
67 
72  bool mStopped = false;
77  bool mPaused = false;
78 
82  long timeStart_ms = 0;
83 
84 public:
88  unsigned int mCurrentLegIndex = 0;
89 
94  bool exportToFile = true;
99  bool exitAtEnd = false;
103  bool finished = false;
104 
105  std::shared_ptr<MeasurementsBuffer> mbuffer = NULL;
106  std::shared_ptr<SimulationCycleCallback> callback = nullptr;
107 
108  // *** CONSTRUCTION / DESTRUCTION *** //
109  // ************************************ //
116  Simulation(unsigned numThreads, double deviceAccuracy);
117 
118  // *** M E T H O D S *** //
119  // *********************** //
123  virtual void doSimStep();
127  virtual void onLegComplete() = 0;
128 
132  void start();
140  void stop() {this->mStopped = true;}
145  void pause(bool pause);
149  void shutdown();
150 
151  // *** GETTERS and SETTERS *** //
152  // ***************************** //
165  void setSimSpeedFactor(double factor);
171  double getSimSpeedFactor() {return this->mSimSpeedFactor;}
172 
178  void setScanner(std::shared_ptr<Scanner> scanner);
184  std::shared_ptr<Scanner> getScanner() {return this->mScanner;}
185 
186 
192  bool isPaused() {return this->mPaused;}
198  bool isStopped() {return this->mStopped;}
204  size_t getSimFrequency() {return simFrequency;}
210  void setSimFrequency(size_t simFrequency)
211  {this->simFrequency = simFrequency;}
212 };
virtual void onLegComplete()=0
Handle leg completion.
std::mutex mutex
Mutex to handle simulation pause and iterations on a multi threading context.
Definition: Simulation.h:55
bool isStopped()
Check if simulation is stopped (true) or not (false)
Definition: Simulation.h:198
bool finished
Flag specifying if simulation has finished (true) or not (false)
Definition: Simulation.h:103
Class representing a simulation.
Definition: Simulation.h:17
void setSimFrequency(size_t simFrequency)
Set simulation frequency.
Definition: Simulation.h:210
unsigned int mCurrentLegIndex
Index of leg at current simulation stage.
Definition: Simulation.h:88
virtual void doSimStep()
Perform computations for current simulation step.
Definition: Simulation.cpp:23
std::condition_variable condvar
Condition variable tu handle simulation iterations on a multi threading context.
Definition: Simulation.h:60
size_t simFrequency
Simulation frequency. If it is 0 then no pause is possible.
Definition: Simulation.h:50
bool mPaused
Flag specifying if the simulation has been paused (true) or not (false)
Definition: Simulation.h:77
void shutdown()
Handle simulation shutdown.
Definition: Simulation.cpp:52
size_t getSimFrequency()
Obtain simulation frequency.
Definition: Simulation.h:204
Class representing a thread pool to deal with multi threading tasks.
Definition: ThreadPool.h:14
double getSimSpeedFactor()
Obtain simulation speed factor.
Definition: Simulation.h:171
static const long NANOSECONDS_PER_SECOND
How many nanoseconds there are in a second.
Definition: Simulation.h:24
void pause(bool pause)
Pause or unpause the simulation.
Definition: Simulation.cpp:38
std::shared_ptr< Scanner > getScanner()
Obtain simulation scanner.
Definition: Simulation.h:184
void start()
Start the simmulation.
Definition: Simulation.cpp:94
void setScanner(std::shared_ptr< Scanner > scanner)
Set scanner for the simulation.
Definition: Simulation.cpp:63
bool exportToFile
Flag specifying if simulation output must be exported to a file (true) or not (false) ...
Definition: Simulation.h:94
thread_pool threadPool
Thread pool.
Definition: Simulation.h:34
void setSimSpeedFactor(double factor)
Set the simulation speed factor.
Definition: Simulation.cpp:78
long timeStart_ms
Time corresponding to simulation start (milliseconds)
Definition: Simulation.h:82
unsigned numSysThreads
Number of threads available in the system.
Definition: Simulation.h:29
void stop()
Stop the simulation.
Definition: Simulation.h:140
std::shared_ptr< std::unique_lock< std::mutex > > pauseLock
Shared pointer to the lock used to handle the mutex for simulation pause purposes.
Definition: Simulation.h:66
bool mStopped
Flag specifying if the simulation has been stopped (true) or not (false)
Definition: Simulation.h:72
Simulation(unsigned numThreads, double deviceAccuracy)
Simulation constructor.
Definition: Simulation.cpp:14
double mSimSpeedFactor
Simulation speed factor.
Definition: Simulation.h:39
bool exitAtEnd
Flag specifying if simulation must end when current leg has been completed (true) or not (false) ...
Definition: Simulation.h:99
bool isPaused()
Check if simulation is paused (true) or not (false)
Definition: Simulation.h:192
std::shared_ptr< Scanner > mScanner
Scanner used by the simulation.
Definition: Simulation.h:45