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 * @ingroup ioserialization
44 */
45class OCEAN_IO_SERIALIZATION_EXPORT DataSample
46{
48
49 public:
50
51 /**
52 * Creates a new data sample.
53 */
54 DataSample() = default;
55
56 /**
57 * Creates a new data sample with a playback timestamp and data timestamp.
58 * @param dataTimestamp The data timestamp of the sample
59 * @param sampleCreationTimestamp The timestamp when the sample was created, this timestamp is not serialized and is only used to automatically determine the playback timestamp
60 */
61 explicit inline DataSample(const DataTimestamp& dataTimestamp, const Timestamp sampleCreationTimestamp = Timestamp(true));
62
63 /**
64 * Destructs a data sample.
65 */
66 virtual ~DataSample() = default;
67
68 /**
69 * 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.
70 * @return The playback timestamp, in seconds, with range [0, infinity)
71 */
72 inline double playbackTimestamp() const;
73
74 /**
75 * Returns the timestamp of the data, e.g., the timestamp of the camera frame when it was captured.
76 * @return The data timestamp
77 */
78 inline const DataTimestamp& dataTimestamp() const;
79
80 /**
81 * Reads the sample from an input bitstream.
82 * @param inputBitstream The input bitstream from which the sample will be read
83 * @return True, if succeeded
84 */
85 virtual bool readSample(InputBitstream& inputBitstream);
86
87 /**
88 * Writes the sample to an output bitstream.
89 * @param outputBitstream The output bitstream to which the sample will be written
90 * @return True, if succeeded
91 */
92 virtual bool writeSample(OutputBitstream& outputBitstream) const;
93
94 /**
95 * Returns the type of the sample, mainly describing the content of the sample, not the source of the sample.
96 * @return The sample type
97 */
98 virtual const std::string& type() const = 0;
99
100 /**
101 * Configures the playback timestamp based on when serialization started.
102 * This calculates the relative playback time as the difference between when this sample was created and when serialization began.
103 * @param serializationStartTimestamp The timestamp when serialization started
104 */
105 inline void configurePlaybackTimestamp(const Timestamp& serializationStartTimestamp);
106
107 protected:
108
109 /**
110 * Reads a 4x4 homogenous matrix with float precision from an input bitstream.
111 * @param inputBitstream The input bitstream from which the matrix will be read
112 * @param homogenousMatrix The resulting homogenous matrix
113 * @return True, if succeeded
114 */
115 static bool readHomogenousMatrix4F(InputBitstream& inputBitstream, HomogenousMatrixF4& homogenousMatrix);
116
117 /**
118 * Writes a 4x4 homogenous matrix with float precision to an output bitstream.
119 * @param outputBitstream The output bitstream to which the matrix will be written
120 * @param homogenousMatrix The homogenous matrix to write
121 * @return True, if succeeded
122 */
123 static bool writeHomogenousMatrix4F(OutputBitstream& outputBitstream, const HomogenousMatrixF4& homogenousMatrix);
124
125 /**
126 * Reads a quaternion with float precision from an input bitstream.
127 * @param inputBitstream The input bitstream from which the quaternion will be read
128 * @param quaternion The resulting quaternion
129 * @return True, if succeeded
130 */
131 static bool readQuaternionF(InputBitstream& inputBitstream, QuaternionF& quaternion);
132
133 /**
134 * Writes a quaternion with float precision to an output bitstream.
135 * @param outputBitstream The output bitstream to which the quaternion will be written
136 * @param quaternion The quaternion to write
137 * @return True, if succeeded
138 */
139 static bool writeQuaternionF(OutputBitstream& outputBitstream, const QuaternionF& quaternion);
140
141 /**
142 * Reads a 3D vector with float precision from an input bitstream.
143 * @param inputBitstream The input bitstream from which the vector will be read
144 * @param vector The resulting vector
145 * @return True, if succeeded
146 */
147 static bool readVectorF3(InputBitstream& inputBitstream, VectorF3& vector);
148
149 /**
150 * Writes a 3D vector with float precision to an output bitstream.
151 * @param outputBitstream The output bitstream to which the vector will be written
152 * @param vector The vector to write
153 * @return True, if succeeded
154 */
155 static bool writeVectorF3(OutputBitstream& outputBitstream, const VectorF3& vector);
156
157 protected:
158
159 /// 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.
160 double playbackTimestamp_ = NumericD::minValue();
161
162 /// The timestamp of the data, e.g., the timestamp of the camera frame when it was captured.
164
165 private:
166
167 /// The unix timestamp when the sample was created, this timestamp is not serialized and is only used to automatically determine the playback timestamp.
168 Timestamp sampleCreationTimestamp_ = Timestamp(true);
169};
170
171inline DataSample::DataSample(const DataTimestamp& dataTimestamp, const Timestamp sampleCreationTimestamp) :
172 dataTimestamp_(dataTimestamp),
173 sampleCreationTimestamp_(sampleCreationTimestamp)
174{
175 ocean_assert(sampleCreationTimestamp_.isValid());
176}
177
178inline double DataSample::playbackTimestamp() const
179{
180 return playbackTimestamp_;
181}
182
184{
185 return dataTimestamp_;
186}
187
188inline void DataSample::configurePlaybackTimestamp(const Timestamp& serializationStartTimestamp)
189{
190 ocean_assert(serializationStartTimestamp.isValid());
191 ocean_assert(sampleCreationTimestamp_.isValid());
192
193 playbackTimestamp_ = double(sampleCreationTimestamp_ - serializationStartTimestamp);
194}
195
196inline bool DataSample::readHomogenousMatrix4F(InputBitstream& inputBitstream, HomogenousMatrixF4& homogenousMatrix)
197{
198 return inputBitstream.read(homogenousMatrix(), sizeof(float) * 16);
199}
200
201inline bool DataSample::writeHomogenousMatrix4F(OutputBitstream& outputBitstream, const HomogenousMatrixF4& homogenousMatrix)
202{
203 return outputBitstream.write(homogenousMatrix(), sizeof(float) * 16);
204}
205
206inline bool DataSample::readQuaternionF(InputBitstream& inputBitstream, QuaternionF& quaternion)
207{
208 return inputBitstream.read(quaternion(), sizeof(float) * 4);
209}
210
211inline bool DataSample::writeQuaternionF(OutputBitstream& outputBitstream, const QuaternionF& quaternion)
212{
213 return outputBitstream.write(quaternion(), sizeof(float) * 4);
214}
215
216inline bool DataSample::readVectorF3(InputBitstream& inputBitstream, VectorF3& vector)
217{
218 return inputBitstream.read(vector(), sizeof(float) * 3);
219}
220
221inline bool DataSample::writeVectorF3(OutputBitstream& outputBitstream, const VectorF3& vector)
222{
223 return outputBitstream.write(vector(), sizeof(float) * 3);
224}
225
226}
227
228}
229
230}
231
232#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:46
static bool writeQuaternionF(OutputBitstream &outputBitstream, const QuaternionF &quaternion)
Writes a quaternion with float precision to an output bitstream.
Definition DataSample.h:211
static bool readHomogenousMatrix4F(InputBitstream &inputBitstream, HomogenousMatrixF4 &homogenousMatrix)
Reads a 4x4 homogenous matrix with float precision from an input bitstream.
Definition DataSample.h:196
DataSample()=default
Creates a new data sample.
void configurePlaybackTimestamp(const Timestamp &serializationStartTimestamp)
Configures the playback timestamp based on when serialization started.
Definition DataSample.h:188
DataTimestamp dataTimestamp_
The timestamp of the data, e.g., the timestamp of the camera frame when it was captured.
Definition DataSample.h:163
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:168
static bool writeVectorF3(OutputBitstream &outputBitstream, const VectorF3 &vector)
Writes a 3D vector with float precision to an output bitstream.
Definition DataSample.h:221
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:183
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:178
static bool readVectorF3(InputBitstream &inputBitstream, VectorF3 &vector)
Reads a 3D vector with float precision from an input bitstream.
Definition DataSample.h:216
double playbackTimestamp_
The relative timestamp of this sample at which the sample will be played back, in relation to the mom...
Definition DataSample.h:160
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:206
static bool writeHomogenousMatrix4F(OutputBitstream &outputBitstream, const HomogenousMatrixF4 &homogenousMatrix)
Writes a 4x4 homogenous matrix with float precision to an output bitstream.
Definition DataSample.h:201
This class implements a data timestamp which can hold either a double or int64_t value.
Definition DataTimestamp.h:29
This class implements an output data serializer.
Definition OutputDataSerializer.h:29
This class implements a unit quaternion rotation.
Definition Quaternion.h:100
This class implements a timestamp.
Definition Timestamp.h:63
bool isValid() const
Returns whether the timestamp holds a valid time.
Definition Timestamp.h:559
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