Helios++
Helios software for LiDAR simulations
SyncFileWriter.h
1 #pragma once
2 
3 #include <Measurement.h>
4 #include <mutex>
5 #include <string>
6 #include <Trajectory.h>
7 
15 private:
19  std::string path;
23  std::mutex mutex;
24 public:
25  // *** CONSTRUCTION / DESTRUCTION *** //
26  // ************************************ //
32  SyncFileWriter(const std::string &path) : path(path) {};
33  virtual ~SyncFileWriter(){}
34 
35  // *** W R I T E *** //
36  // ******************* //
43  void write(
44  Measurement const &m,
45  glm::dvec3 const shift = glm::dvec3(0, 0, 0)
46  ){
47  // Get the mutex to have exclusive access
48  std::lock_guard<std::mutex> lock(mutex);
49 
50  // Write data function
51  try {
52  _write(m, shift);
53  }
54  catch(std::exception &e){
55  std::stringstream ss;
56  ss << "SyncFileWriter failed to write measurement. EXCEPTION: \n\t"
57  << e.what();
58  logging::WARN(ss.str());
59  }
60  }
68  virtual void _write(Measurement const &m, glm::dvec3 const & shift) = 0;
69 
75  void write(Trajectory const &t){
76  // Get the mutex to have eclusive access
77  std::lock_guard<std::mutex> lock(mutex);
78 
79  // Write data function
80  try{
81  _write(t);
82  }
83  catch(std::exception &e){
84  std::stringstream ss;
85  ss << "SyncFileWriter failed to write trajectory. EXCEPTION: \n\t"
86  << e.what();
87  logging::WARN(ss.str());
88  }
89  }
90 
95  virtual void _write(Trajectory const &t) = 0;
96 
97 
108  void write(
109  std::vector<double> const &fullwave,
110  int fullwaveIndex,
111  double minTime,
112  double maxTime,
113  glm::dvec3 const & beamOrigin,
114  glm::dvec3 const & beamDir,
115  long gpsTime
116  ){
117  // Get the mutex to have exclusive access
118  std::lock_guard<std::mutex> lock(mutex);
119 
120  // Write data function
121  try {
122  _write(
123  fullwave,
124  fullwaveIndex,
125  minTime,
126  maxTime,
127  beamOrigin,
128  beamDir,
129  gpsTime
130  );
131  }
132  catch(std::exception &e){
133  std::stringstream ss;
134  ss << "SyncFileWriter failed to write fullwave. EXCEPTION: \n\t"
135  << e.what();
136  logging::WARN(ss.str());
137  }
138  }
139 
151  virtual void _write(
152  std::vector<double> const &fullwave,
153  int fullwaveIndex,
154  double minTime,
155  double maxTime,
156  glm::dvec3 const & beamOrigin,
157  glm::dvec3 const & beamDir,
158  long gpsTime
159  ) = 0;
160 
161  // *** F I N I S H *** //
162  // ********************* //
167  virtual void finish() {}
168 
169  // *** GETTERS and SETTERS *** //
170  // ***************************** //
175  inline std::string getPath(){return path;}
176 };
SyncFileWriter(const std::string &path)
Instantiate a SyncFileWriter which writes to file at given path.
Definition: SyncFileWriter.h:32
virtual void _write(Measurement const &m, glm::dvec3 const &shift)=0
Abstract write function for Measurement. Must be overridden by children classes.
std::string getPath()
Obtain the path to the file.
Definition: SyncFileWriter.h:175
Class representing a concrete trajectory definition.
Definition: Trajectory.h:8
void write(Trajectory const &t)
Synchronously write trajectory to file.
Definition: SyncFileWriter.h:75
void write(Measurement const &m, glm::dvec3 const shift=glm::dvec3(0, 0, 0))
Synchronously write Measurement to file.
Definition: SyncFileWriter.h:43
Class representing a measurement.
Definition: Measurement.h:12
std::string path
Path to file to be written.
Definition: SyncFileWriter.h:19
void write(std::vector< double > const &fullwave, int fullwaveIndex, double minTime, double maxTime, glm::dvec3 const &beamOrigin, glm::dvec3 const &beamDir, long gpsTime)
Synchronously write Fullwave to file.
Definition: SyncFileWriter.h:108
std::mutex mutex
Mutex to synchronize concurrent write operations.
Definition: SyncFileWriter.h:23
Abstract class defining common behavior for all synchronous file writers.
Definition: SyncFileWriter.h:14
virtual void finish()
Finish the writing so all writing operations are performed and all buffers are closed.
Definition: SyncFileWriter.h:167