Helios++
Helios software for LiDAR simulations
NoiseSource.h
1 #pragma once
2 
3 #include <string>
4 #include <ostream>
5 #include <sstream>
6 
16 template <typename RealType>
18 public:
19 protected:
20  // *** ATTRIBUTES *** //
21  // ******************** //
28  RealType clipMin = 0.0;
33  RealType clipMax = 1.0;
37  bool clipEnabled = false;
65  unsigned long fixedLifespan = 1L;
71  unsigned long fixedRemainingUses = 0L;
77  RealType fixedValue = 0;
78 
79 public:
80  // *** CONSTRUCTION *** //
81  // ********************** //
85  void build(){this->fixedValue = noiseFunction();}
86 
87  // *** CLIPPING FUNCTIONS *** //
88  // **************************** //
94  inline double getClipMin()
95  {return clipMin;}
102  inline NoiseSource & setClipMin(RealType clipMin)
103  {this->clipMin = clipMin; return *this;}
109  inline double getClipMax()
110  {return clipMax;}
117  inline NoiseSource & setClipMax(RealType clipMax)
118  {this->clipMax = clipMax; return *this;}
124  inline bool isClipEnabled(){return clipEnabled;}
133  {this->clipEnabled = clipEnabled; return *this;}
134 protected:
142  inline RealType clip(RealType v);
143 
144 public:
145  // *** NOISE CONFIGURATION FUNCTIONS *** //
146  // *************************************** //
152  {return this->fixedLifespan != 1L;}
158  unsigned long getFixedLifespan()
159  {return this->fixedLifespan;}
169  {this->fixedLifespan = fixedLifespan; return *this;}
180  {return this->fixedRemainingUses;}
186  NoiseSource & setFixedValueRemainingUses(unsigned long remainingUses)
187  {fixedRemainingUses = remainingUses; return *this;}
194 
195 
196  // *** NOISE OBTAINMENT FUNCTIONS *** //
197  // ************************************ //
207  RealType next();
208 
213  virtual RealType noiseFunction() = 0;
214 
215  // *** STREAM OPERATORS *** //
216  // ************************** //
220  template<typename _RealType>
221  friend std::ostream& operator << (
222  std::ostream &out,
223  NoiseSource<_RealType> const &ns
224  );
225 };
226 
227 
228 
229 
230 // *** CLASS IMPLEMENTATION *** //
231 // ********************************* //
232 
233 // *** CLIPPING FUNCTIONS *** //
234 // **************************** //
235 template <typename RealType>
236 RealType NoiseSource<RealType>::clip(RealType v){
237  if(isClipEnabled()){
238  if(v > getClipMax()) return getClipMax();
239  if(v < getClipMin()) return getClipMin();
240  }
241 
242  return v;
243 }
244 
245 // *** NOISE CONFIGURATION FUNCTIONS *** //
246 // *************************************** //
247 template <typename RealType>
249  fixedValue = noiseFunction();
250  fixedRemainingUses = fixedLifespan;
251  return *this;
252 }
253 
254 
255 // *** NOISE OBTAINMENT FUNCTIONS *** //
256 // ************************************ //
257 template <typename RealType>
259  // Handle fixed values
260  if(fixedLifespan != 1L){
261  if(fixedLifespan == 0L) return fixedValue;
262  if(fixedRemainingUses > 0L){
263  fixedRemainingUses--;
264  return fixedValue;
265  }
266  }
267 
268  // Obtain noise value
269  fixedValue = clip(noiseFunction());
270 
271  // Update remaining uses if necessary
272  if(fixedLifespan > 1L){
273  fixedRemainingUses = fixedLifespan-1;
274  }
275 
276  // Return noise value
277  return fixedValue;
278 }
279 
280 // *** STREAM OPERATORS *** //
281 // ************************** //
282 template<typename RealType>
283 std::ostream& operator <<(std::ostream &out, NoiseSource<RealType> const &ns){
284  std::stringstream ss;
285  ss << "Noise source ("<<&ns<<"):\n"
286  << "\tclipMin = " << ns.clipMin << "\n"
287  << "\tclipMax = " << ns.clipMax << "\n"
288  << "\tclipEnabled = " << ns.clipEnabled << "\n"
289  << "\tfixedLifespan = " << ns.fixedLifespan << "\n"
290  << "\tfixedRemainingUses = " << ns.fixedRemainingUses << "\n"
291  << "\tfixedValue = " << ns.fixedValue << "\n";
292  out << ss.str();
293  return out;
294 }
Class to handle a noise source.
Definition: NoiseSource.h:17
RealType next()
Obtain the next default noise value.
Definition: NoiseSource.h:258
NoiseSource & setFixedLifespan(unsigned long fixedLifespan)
Set the fixed value lifespan.
Definition: NoiseSource.h:168
NoiseSource & setFixedValueRemainingUses(unsigned long remainingUses)
Update remaining uses count for current fixed value.
Definition: NoiseSource.h:186
unsigned long getFixedLifespan()
Obtain the fixed value lifespan.
Definition: NoiseSource.h:158
bool isFixedValueEnabled()
Check if fixed value usage is enabled or not.
Definition: NoiseSource.h:151
NoiseSource & fixedRenew()
Forces a renewal of fixed value and its remaining uses.
Definition: NoiseSource.h:248
RealType clip(RealType v)
If clipping is enabled, it will clip received value. If clipping is not enabled, received value will ...
Definition: NoiseSource.h:236
unsigned long getFixedValueRemainingUses()
Obtain the remaining uses of current fixed value.
Definition: NoiseSource.h:179
virtual RealType noiseFunction()=0
Function which computes noise values.
double getClipMax()
Obtain the clip max value.
Definition: NoiseSource.h:109
NoiseSource & setClipMax(RealType clipMax)
Set the clip max value.
Definition: NoiseSource.h:117
NoiseSource & setClipEnabled(bool clipEnabled)
Enable clipping by setting it to true or disable it by setting to false.
Definition: NoiseSource.h:132
RealType clipMin
All noise values which are less than clipMin will be clipped to clipMin if clipping is enabled.
Definition: NoiseSource.h:28
unsigned long fixedLifespan
Specify the how many times a fixed value can be used before being renewed.
Definition: NoiseSource.h:65
unsigned long fixedRemainingUses
How many remaining uses the fixed value has.
Definition: NoiseSource.h:71
RealType fixedValue
The fixed value.
Definition: NoiseSource.h:77
bool clipEnabled
True when clipping is enabled, False otherwise.
Definition: NoiseSource.h:37
double getClipMin()
Obtain the clip min value.
Definition: NoiseSource.h:94
friend std::ostream & operator<<(std::ostream &out, NoiseSource< _RealType > const &ns)
Output stream behavior.
bool isClipEnabled()
Check if clipping is enabled or not.
Definition: NoiseSource.h:124
RealType clipMax
All noise values which are greater than clipMax will be clipped to clipMaxx if clipping is enabled.
Definition: NoiseSource.h:33
void build()
Common behavior for all NoiseSource constructors.
Definition: NoiseSource.h:85
NoiseSource & setClipMin(RealType clipMin)
Set the clip min value.
Definition: NoiseSource.h:102