Ocean
Loading...
Searching...
No Matches
DataSample.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_IO_SERIALIZATION_DATA_SAMPLE_H
9#define META_OCEAN_IO_SERIALIZATION_DATA_SAMPLE_H
10
13
15
16#include "ocean/io/Bitstream.h"
17
20#include "ocean/math/Vector3.h"
21
22namespace Ocean
23{
24
25namespace IO
26{
27
28namespace Serialization
29{
30
31// Forward declaration.
32class DataSample;
33
34/**
35 * Definition of a unique pointer holding a DataSample.
36 * @see DataSample.
37 * @ingroup ioserialization
38 */
39using UniqueDataSample = std::unique_ptr<DataSample>;
40
41/**
42 * This class implements a base class for data samples.
43 * A data sample represents a single unit of data that can be serialized and deserialized through bitstreams.<br>
44 * Each sample contains two timestamps: a data timestamp representing when the data was originally captured (e.g., the camera frame timestamp), and a playback timestamp representing the relative time offset from when serialization started.<br>
45 * Derived classes must implement the type() function to identify the sample type and should override readSample()/writeSample() to serialize their specific data.<br>
46 * The class also provides helper functions for reading/writing common mathematical types like HomogenousMatrix4, Quaternion, and Vector3 with float precision.
47 * @ingroup ioserialization
48 */
49class OCEAN_IO_SERIALIZATION_EXPORT DataSample
50{
52
53 public:
54
55 /**
56 * Creates a new data sample.
57 */
58 DataSample() = default;
59
60 /**
61 * Creates a new data sample with a playback timestamp and data timestamp.
62 * @param dataTimestamp The data timestamp of the sample
63 * @param sampleCreationTimestamp The timestamp when the sample was created, this timestamp is not serialized and is only used to automatically determine the playback timestamp
64 */
65 explicit inline DataSample(const DataTimestamp& dataTimestamp, const Timestamp sampleCreationTimestamp = Timestamp(true));
66
67 /**
68 * Destructs a data sample.
69 */
70 virtual ~DataSample() = default;
71
72 /**
73 * Returns the relative timestamp of this sample at which the sample will be played back, in relation to the moment in time when the serialization started.
74 * @return The playback timestamp, in seconds, with range [0, infinity)
75 */
76 inline double playbackTimestamp() const;
77
78 /**
79 * Returns the timestamp of the data, e.g., the timestamp of the camera frame when it was captured.
80 * @return The data timestamp
81 */
82 inline const DataTimestamp& dataTimestamp() const;
83
84 /**
85 * Reads the sample from an input bitstream.
86 * @param inputBitstream The input bitstream from which the sample will be read
87 * @return True, if succeeded
88 */
89 virtual bool readSample(InputBitstream& inputBitstream);
90
91 /**
92 * Writes the sample to an output bitstream.
93 * @param outputBitstream The output bitstream to which the sample will be written
94 * @return True, if succeeded
95 */
96 virtual bool writeSample(OutputBitstream& outputBitstream) const;
97
98 /**
99 * Returns the type of the sample, mainly describing the content of the sample, not the source of the sample.
100 * @return The sample type
101 */
102 virtual const std::string& type() const = 0;
103
104 /**
105 * Configures the playback timestamp based on when serialization started.
106 * This calculates the relative playback time as the difference between when this sample was created and when serialization began.
107 * @param serializationStartTimestamp The timestamp when serialization started
108 */
109 inline void configurePlaybackTimestamp(const Timestamp& serializationStartTimestamp);
110
111 protected:
112
113 /**
114 * Reads a 4x4 homogenous matrix with float precision from an input bitstream.
115 * @param inputBitstream The input bitstream from which the matrix will be read
116 * @param homogenousMatrix The resulting homogenous matrix
117 * @return True, if succeeded
118 */
119 static bool readHomogenousMatrix4F(InputBitstream& inputBitstream, HomogenousMatrixF4& homogenousMatrix);
120
121 /**
122 * Writes a 4x4 homogenous matrix with float precision to an output bitstream.
123 * @param outputBitstream The output bitstream to which the matrix will be written
124 * @param homogenousMatrix The homogenous matrix to write
125 * @return True, if succeeded
126 */
127 static bool writeHomogenousMatrix4F(OutputBitstream& outputBitstream, const HomogenousMatrixF4& homogenousMatrix);
128
129 /**
130 * Reads a quaternion with float precision from an input bitstream.
131 * @param inputBitstream The input bitstream from which the quaternion will be read
132 * @param quaternion The resulting quaternion
133 * @return True, if succeeded
134 */
135 static bool readQuaternionF(InputBitstream& inputBitstream, QuaternionF& quaternion);
136
137 /**
138 * Writes a quaternion with float precision to an output bitstream.
139 * @param outputBitstream The output bitstream to which the quaternion will be written
140 * @param quaternion The quaternion to write
141 * @return True, if succeeded
142 */
143 static bool writeQuaternionF(OutputBitstream& outputBitstream, const QuaternionF& quaternion);
144
145 /**
146 * Reads a 3D vector with float precision from an input bitstream.
147 * @param inputBitstream The input bitstream from which the vector will be read
148 * @param vector The resulting vector
149 * @return True, if succeeded
150 */
151 static bool readVectorF3(InputBitstream& inputBitstream, VectorF3& vector);
152
153 /**
154 * Writes a 3D vector with float precision to an output bitstream.
155 * @param outputBitstream The output bitstream to which the vector will be written
156 * @param vector The vector to write
157 * @return True, if succeeded
158 */
159 static bool writeVectorF3(OutputBitstream& outputBitstream, const VectorF3& vector);
160
161 protected:
162
163 /// The relative timestamp of this sample at which the sample will be played back, in relation to the moment in time when the serialization started, in seconds.
164 double playbackTimestamp_ = NumericD::minValue();
165
166 /// The timestamp of the data, e.g., the timestamp of the camera frame when it was captured.
168
169 private:
170
171 /// The unix timestamp when the sample was created, this timestamp is not serialized and is only used to automatically determine the playback timestamp.
172 Timestamp sampleCreationTimestamp_ = Timestamp(true);
173};
174
175inline DataSample::DataSample(const DataTimestamp& dataTimestamp, const Timestamp sampleCreationTimestamp) :
176 dataTimestamp_(dataTimestamp),
177 sampleCreationTimestamp_(sampleCreationTimestamp)
178{
179 ocean_assert(sampleCreationTimestamp_.isValid());
180}
181
182inline double DataSample::playbackTimestamp() const
183{
184 return playbackTimestamp_;
185}
186
188{
189 return dataTimestamp_;
190}
191
192inline void DataSample::configurePlaybackTimestamp(const Timestamp& serializationStartTimestamp)
193{
194 ocean_assert(serializationStartTimestamp.isValid());
195 ocean_assert(sampleCreationTimestamp_.isValid());
196
197 playbackTimestamp_ = double(sampleCreationTimestamp_ - serializationStartTimestamp);
198}
199
200inline bool DataSample::readHomogenousMatrix4F(InputBitstream& inputBitstream, HomogenousMatrixF4& homogenousMatrix)
201{
202 return inputBitstream.read(homogenousMatrix(), sizeof(float) * 16);
203}
204
205inline bool DataSample::writeHomogenousMatrix4F(OutputBitstream& outputBitstream, const HomogenousMatrixF4& homogenousMatrix)
206{
207 return outputBitstream.write(homogenousMatrix(), sizeof(float) * 16);
208}
209
210inline bool DataSample::readQuaternionF(InputBitstream& inputBitstream, QuaternionF& quaternion)
211{
212 return inputBitstream.read(quaternion(), sizeof(float) * 4);
213}
214
215inline bool DataSample::writeQuaternionF(OutputBitstream& outputBitstream, const QuaternionF& quaternion)
216{
217 return outputBitstream.write(quaternion(), sizeof(float) * 4);
218}
219
220inline bool DataSample::readVectorF3(InputBitstream& inputBitstream, VectorF3& vector)
221{
222 return inputBitstream.read(vector(), sizeof(float) * 3);
223}
224
225inline bool DataSample::writeVectorF3(OutputBitstream& outputBitstream, const VectorF3& vector)
226{
227 return outputBitstream.write(vector(), sizeof(float) * 3);
228}
229
230}
231
232}
233
234}
235
236#endif // META_OCEAN_IO_SERIALIZATION_DATA_SAMPLE_H
This class implements a 4x4 homogeneous transformation matrix using floating point values with the pr...
Definition HomogenousMatrix4.h:110
This class implements an input bitstream.
Definition Bitstream.h:51
bool read(T &value)
Reads a value from the bitstream and moves the internal position inside the bitstream accordingly.
This class implements an output bitstream.
Definition Bitstream.h:215
bool write(const T &value)
Writes a data object to the stream and moves the internal position inside the bitstream accordingly.
This class implements a base class for data samples.
Definition DataSample.h:50
static bool writeQuaternionF(OutputBitstream &outputBitstream, const QuaternionF &quaternion)
Writes a quaternion with float precision to an output bitstream.
Definition DataSample.h:215
static bool readHomogenousMatrix4F(InputBitstream &inputBitstream, HomogenousMatrixF4 &homogenousMatrix)
Reads a 4x4 homogenous matrix with float precision from an input bitstream.
Definition DataSample.h:200
DataSample()=default
Creates a new data sample.
void configurePlaybackTimestamp(const Timestamp &serializationStartTimestamp)
Configures the playback timestamp based on when serialization started.
Definition DataSample.h:192
DataTimestamp dataTimestamp_
The timestamp of the data, e.g., the timestamp of the camera frame when it was captured.
Definition DataSample.h:167
virtual ~DataSample()=default
Destructs a data sample.
virtual bool readSample(InputBitstream &inputBitstream)
Reads the sample from an input bitstream.
Timestamp sampleCreationTimestamp_
The unix timestamp when the sample was created, this timestamp is not serialized and is only used to ...
Definition DataSample.h:172
static bool writeVectorF3(OutputBitstream &outputBitstream, const VectorF3 &vector)
Writes a 3D vector with float precision to an output bitstream.
Definition DataSample.h:225
const DataTimestamp & dataTimestamp() const
Returns the timestamp of the data, e.g., the timestamp of the camera frame when it was captured.
Definition DataSample.h:187
virtual bool writeSample(OutputBitstream &outputBitstream) const
Writes the sample to an output bitstream.
double playbackTimestamp() const
Returns the relative timestamp of this sample at which the sample will be played back,...
Definition DataSample.h:182
static bool readVectorF3(InputBitstream &inputBitstream, VectorF3 &vector)
Reads a 3D vector with float precision from an input bitstream.
Definition DataSample.h:220
double playbackTimestamp_
The relative timestamp of this sample at which the sample will be played back, in relation to the mom...
Definition DataSample.h:164
virtual const std::string & type() const =0
Returns the type of the sample, mainly describing the content of the sample, not the source of the sa...
static bool readQuaternionF(InputBitstream &inputBitstream, QuaternionF &quaternion)
Reads a quaternion with float precision from an input bitstream.
Definition DataSample.h:210
static bool writeHomogenousMatrix4F(OutputBitstream &outputBitstream, const HomogenousMatrixF4 &homogenousMatrix)
Writes a 4x4 homogenous matrix with float precision to an output bitstream.
Definition DataSample.h:205
This class implements a data timestamp which can hold either a double or int64_t value.
Definition DataTimestamp.h:32
This class implements an output data serializer.
Definition OutputDataSerializer.h:32
This class implements a unit quaternion rotation.
Definition Quaternion.h:100
This class implements a timestamp.
Definition Timestamp.h:64
bool isValid() const
Returns whether the timestamp holds a valid time.
Definition Timestamp.h:646
This class implements a vector with three elements.
Definition Vector3.h:97
std::unique_ptr< DataSample > UniqueDataSample
Definition of a unique pointer holding a DataSample.
Definition DataSample.h:39
The namespace covering the entire Ocean framework.
Definition Accessor.h:15