ccWaveform.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #ifndef CC_WAVEFORM_HEADER
  2. #define CC_WAVEFORM_HEADER
  3. //Local
  4. #include "qCC_db.h"
  5. #include "ccGLMatrix.h"
  6. #include "ccSerializableObject.h"
  7. //CCCoreLib
  8. #include <CCGeom.h>
  9. //system
  10. #include <cstdint>
  11. #include <cstdlib>
  12. //! Waveform descriptor
  13. class QCC_DB_LIB_API WaveformDescriptor : public ccSerializableObject
  14. {
  15. public:
  16. //! Default constructor
  17. WaveformDescriptor();
  18. //! Difference operator
  19. bool operator != (const WaveformDescriptor& d) const;
  20. //! Equivalence operator
  21. bool operator == (const WaveformDescriptor& d) const { return !(*this != d); }
  22. //inherited from ccSerializableObject
  23. bool isSerializable() const override { return true; }
  24. bool toFile(QFile& out, short dataVersion) const override;
  25. bool fromFile(QFile& in, short dataVersion, int flags, LoadedIDMap& oldToNewIDMap) override;
  26. short minimumFileVersion() const override;
  27. uint32_t numberOfSamples; //!< Number of samples
  28. uint32_t samplingRate_ps; //!< Sampling rate in pico seconds
  29. double digitizerGain; //!< Digitizer gain (VOLTS = OFFSET + GAIN * Raw_Waveform_Amplitude)
  30. double digitizerOffset; //!< Digitizer offset (VOLTS = OFFSET + GAIN * Raw_Waveform_Amplitude)
  31. uint8_t bitsPerSample; //!< Number of bits per sample
  32. };
  33. //! Waveform
  34. /** \warning Waveforms do not own their data!
  35. **/
  36. class QCC_DB_LIB_API ccWaveform : public ccSerializableObject
  37. {
  38. public:
  39. //! Default constructor
  40. ccWaveform(uint8_t descriptorID = 0);
  41. //! Destructor
  42. ~ccWaveform() override = default;
  43. //! Returns the associated descriptor (ID)
  44. /** \warning A value of zero indicates that there is no associated waveform data.
  45. **/
  46. inline uint8_t descriptorID() const { return m_descriptorID; }
  47. //! Sets the associated descriptor (ID)
  48. inline void setDescriptorID(uint8_t id) { m_descriptorID = id; }
  49. //! Describes the waveform data
  50. void setDataDescription(uint64_t dataOffset, uint32_t byteCount);
  51. //! Returns the (raw) value of a given sample
  52. uint32_t getRawSample(uint32_t i, const WaveformDescriptor& descriptor, const uint8_t* dataStorage) const;
  53. //! Returns the (real) value of a given sample (in volts)
  54. double getSample(uint32_t i, const WaveformDescriptor& descriptor, const uint8_t* dataStorage) const;
  55. //! Returns the range of (real) samples
  56. double getRange(double& minVal, double& maxVal, const WaveformDescriptor& descriptor, const uint8_t* dataStorage) const;
  57. //! Decodes the samples and store them in a vector
  58. bool decodeSamples(std::vector<double>& values, const WaveformDescriptor& descriptor, const uint8_t* dataStorage) const;
  59. //! Exports (real) samples to an ASCII file
  60. bool toASCII(const QString& filename, const WaveformDescriptor& descriptor, const uint8_t* dataStorage) const;
  61. //! Helper: exports a series of values as an ASCII file
  62. static bool ToASCII(const QString& filename, std::vector<double>& values, uint32_t samplingRate_ps);
  63. //! Returns the sample position in 3D
  64. CCVector3 getSamplePos(float i, const CCVector3& P0, const WaveformDescriptor& descriptor) const;
  65. //! Returns the number of allocated bytes
  66. inline uint32_t byteCount() const { return m_byteCount; }
  67. //! Returns the byte offset to waveform data
  68. inline uint64_t dataOffset() const { return m_dataOffset; }
  69. //! Sets the byte offset to waveform data
  70. inline void setDataOffset(uint64_t offset) { m_dataOffset = offset; }
  71. //! Gives access to the internal data
  72. inline const uint8_t* data(const uint8_t* dataStorage) const { return dataStorage + m_dataOffset; }
  73. //! Sets the beam direction
  74. inline void setBeamDir(const CCVector3f& dir) { m_beamDir = dir; }
  75. //! Returns the beam direction
  76. inline const CCVector3f& beamDir() const { return m_beamDir; }
  77. //! Set the echo time (in picoseconds)
  78. inline void setEchoTime_ps(float time_ps) { m_echoTime_ps = time_ps; }
  79. //! Returns the echo time (in picoseconds)
  80. inline float echoTime_ps() const { return m_echoTime_ps; }
  81. //! Applies a rigid transformation (on the beam direction)
  82. void applyRigidTransformation(const ccGLMatrix& trans);
  83. //! Returns the return index
  84. uint8_t returnIndex() const { return m_returnIndex; }
  85. //! Sets the return index
  86. void setReturnIndex(uint8_t index) { m_returnIndex = index; }
  87. //inherited from ccSerializableObject
  88. bool isSerializable() const override { return true; }
  89. bool toFile(QFile& out, short dataVersion) const override;
  90. bool fromFile(QFile& in, short dataVersion, int flags, LoadedIDMap& oldToNewIDMap) override;
  91. short minimumFileVersion() const override;
  92. protected: //members
  93. //! Waveform packet size in bytes
  94. /** \warning Not necessarily equal to the number of samples!
  95. **/
  96. uint32_t m_byteCount;
  97. //! Byte offset to waveform data
  98. uint64_t m_dataOffset;
  99. //! Laser beam direction
  100. /** Parametric line equation for extrapolating points along the associated waveform:
  101. X = X0 + X(t)
  102. Y = Y0 + Y(t)
  103. Z = Z0 + Z(t)
  104. **/
  105. CCVector3f m_beamDir;
  106. //! Return Point location (in picoseconds)
  107. /** The offset in picoseconds from the first digitized value to the location
  108. within the waveform packet that the associated return pulse was detected.
  109. **/
  110. float m_echoTime_ps;
  111. //! Wave Packet descriptor index
  112. /** \warning A value of zero indicates that there is no associated waveform data.
  113. **/
  114. uint8_t m_descriptorID;
  115. //! Return index
  116. uint8_t m_returnIndex;
  117. };
  118. //! Waveform proxy
  119. /** For easier access to the waveform data
  120. **/
  121. class QCC_DB_LIB_API ccWaveformProxy
  122. {
  123. public:
  124. //! Default constructor
  125. ccWaveformProxy(const ccWaveform& w, const WaveformDescriptor& d, const uint8_t* storage)
  126. : m_w(w)
  127. , m_d(d)
  128. , m_storage(storage)
  129. {}
  130. //! Returns whether the waveform (proxy) is valid or not
  131. inline bool isValid() const { return m_storage && m_w.descriptorID() != 0 && m_d.numberOfSamples != 0; }
  132. //! Returns the associated descriptor (ID)
  133. /** \warning A value of zero indicates that there is no associated waveform data.
  134. **/
  135. inline uint8_t descriptorID() const { return m_w.descriptorID(); }
  136. //! Returns the (raw) value of a given sample
  137. inline uint32_t getRawSample(uint32_t i) const { return m_w.getRawSample(i, m_d, m_storage); }
  138. //! Returns the (real) value of a given sample (in volts)
  139. inline double getSample(uint32_t i) const { return m_w.getSample(i, m_d, m_storage); }
  140. //! Returns the range of (real) samples
  141. inline double getRange(double& minVal, double& maxVal) const { return m_w.getRange(minVal, maxVal, m_d, m_storage); }
  142. //! Decodes the samples and store them in a vector
  143. inline bool decodeSamples(std::vector<double>& values) const { return m_w.decodeSamples(values, m_d, m_storage); }
  144. //! Exports (real) samples to an ASCII file
  145. inline bool toASCII(const QString& filename) const { return m_w.toASCII(filename, m_d, m_storage); }
  146. //! Returns the sample position in 3D
  147. inline CCVector3 getSamplePos(float i, const CCVector3& P0) const { return m_w.getSamplePos(i, P0, m_d); }
  148. //! Returns the number of allocated bytes
  149. inline uint32_t byteCount() const { return m_w.byteCount(); }
  150. //! Gives access to the internal data
  151. inline const uint8_t* data() const { return m_w.data(m_storage); }
  152. //! Returns the beam direction
  153. inline const CCVector3f& beamDir() const { return m_w.beamDir(); }
  154. //! Returns the echo time (in picoseconds)
  155. inline float echoTime_ps() const { return m_w.echoTime_ps(); }
  156. //! Returns the number of samples
  157. inline uint32_t numberOfSamples() const { return m_d.numberOfSamples; }
  158. //! Returns the descriptor
  159. inline const WaveformDescriptor& descriptor() const { return m_d; }
  160. //! Returns the waveform
  161. inline const ccWaveform& waveform() const { return m_w; }
  162. protected: //members
  163. //! Associated ccWaveform instance
  164. const ccWaveform& m_w;
  165. //! Associated descriptor
  166. const WaveformDescriptor& m_d;
  167. //! Associated storage data
  168. const uint8_t* m_storage;
  169. };
  170. #endif //CC_WAVEFORM_HEADER