Ocean
io/Utilities.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_UTILITIES_H
9 #define META_OCEAN_IO_UTILITIES_H
10 
11 #include "ocean/io/IO.h"
12 
14 #include "ocean/math/Vector2.h"
15 #include "ocean/math/Vector3.h"
16 #include "ocean/math/Vector4.h"
17 
18 #include <vector>
19 
20 namespace Ocean
21 {
22 
23 namespace IO
24 {
25 
26 /**
27  * This class implements utility functions for input and output operations.
28  * @ingroup io
29  */
30 class OCEAN_IO_EXPORT Utilities
31 {
32  public:
33 
34  /**
35  * Definition of a vector holding 8 bit elements.
36  */
37  typedef std::vector<uint8_t> Buffer;
38 
39  public:
40 
41  /**
42  * Reads a file and copies the data into a buffer.
43  * In case the file exists but is empty, the resulting buffer will be empty but the function succeeds.
44  * @param filename The name of the file from which the data will be read, must be valid
45  * @param buffer The buffer receiving the data
46  * @return True, if succeeded
47  */
48  static bool readFile(const std::string& filename, Buffer& buffer);
49 
50  /**
51  * Encodes a 4x4 homogeneous matrix.
52  * The matrix will be stored with 64 bit precision.
53  * @param matrix The homogeneous matrix which will be encoded
54  * @param buffer The resulting buffer which will be extended so that already existing data stays untouched
55  */
56  static void encodeHomogenousMatrix4(const HomogenousMatrix4& matrix, Buffer& buffer);
57 
58  /**
59  * Encodes a 4x4 homogeneous matrix.
60  * The matrix will be stored with 64 bit precision.
61  * @param matrix The homogeneous matrix which will be encoded
62  * @param reservedHeaderSize The number of bytes which will be reserved for an optional header, so that the resulting buffer has a header followed by the payload data
63  * @return The resulting buffer storing the given matrix
64  */
65  static inline Buffer encodeHomogenousMatrix4(const HomogenousMatrix4& matrix, const size_t reservedHeaderSize = 0);
66 
67  /**
68  * Decodes a 4x4 homogeneous matrix.
69  * The buffer must provide the matrix with 64 bit precision.
70  * @param data The pointer to the buffer providing the matrix data, will be shifted by the number of consumed bytes afterwards
71  * @param size The size of the entire buffer, will be reduced by the number of consumed bytes afterwards
72  * @param matrix The resulting matrix
73  * @return True, if succeeded
74  */
75  static bool decodeHomogenousMatrix4(const uint8_t*& data, size_t& size, HomogenousMatrix4& matrix);
76 
77  /**
78  * Decodes a 4x4 homogeneous matrix.
79  * The buffer must provide the matrix with 64 bit precision.
80  * @param buffer The buffer providing the matrix data
81  * @param matrix The resulting matrix
82  * @return True, if succeeded
83  */
84  static inline bool decodeHomogenousMatrix4(const Buffer& buffer, HomogenousMatrix4& matrix);
85 
86  /**
87  * Encodes a set of given 2D vectors.
88  * The vectors will be stored with 64 bit precision.
89  * @param vectors The vectors to encode
90  * @param size The number of given vectors
91  * @param buffer The resulting buffer which will be extended so that already existing data stays untouched
92  */
93  static void encodeVectors2(const Vector2* vectors, const size_t size, Buffer& buffer);
94 
95  /**
96  * Encodes a set of given 2D vectors.
97  * The vectors will be stored with 64 bit precision.
98  * @param vectors The vectors to encode
99  * @param size The number of given vectors
100  * @param reservedHeaderSize The number of bytes which will be reserved for an optional header, so that the resulting buffer has a header followed by the payload data
101  * @return The resulting buffer storing the given vectors
102  */
103  static inline Buffer encodeVectors2(const Vector2* vectors, const size_t size, const size_t reservedHeaderSize = 0);
104 
105  /**
106  * Decodes a set of 2D vectors.
107  * The buffer must provide the vectors with 64 bit precision.
108  * @param data The pointer to the buffer providing the 2D vectors, will be shifted by the number of consumed bytes afterwards
109  * @param size The size of the entire buffer, will be reduced by the number of consumed bytes afterwards
110  * @param vectors The resulting vectors
111  * @return True, if succeeded
112  */
113  static bool decodeVectors2(const uint8_t*& data, size_t& size, Vectors2& vectors);
114 
115  /**
116  * Decodes a set of 2D vectors.
117  * The buffer must provide the vectors with 64 bit precision.
118  * @param buffer The buffer providing the 2D vectors
119  * @param vectors The resulting vectors
120  * @return True, if succeeded
121  */
122  static inline bool decodeVectors2(const Buffer& buffer, Vectors2& vectors);
123 
124  /**
125  * Encodes a set of given 3D vectors.
126  * The vectors will be stored with 64 bit precision.
127  * @param vectors The vectors to encode
128  * @param size The number of given vectors
129  * @param buffer The resulting buffer which will be extended so that already existing data stays untouched
130  */
131  static void encodeVectors3(const Vector3* vectors, const size_t size, Buffer& buffer);
132 
133  /**
134  * Encodes a set of given 3D vectors.
135  * The vectors will be stored with 64 bit precision.
136  * @param vectors The vectors to encode
137  * @param size The number of given vectors
138  * @param reservedHeaderSize The number of bytes which will be reserved for an optional header, so that the resulting buffer has a header followed by the payload data
139  * @return The resulting buffer storing the given vectors
140  */
141  static inline Buffer encodeVectors3(const Vector3* vectors, const size_t size, const size_t reservedHeaderSize = 0);
142 
143  /**
144  * Decodes a set of 3D vectors.
145  * The buffer must provide the vectors with 64 bit precision.
146  * @param data The pointer to the buffer providing the 3D vectors, will be shifted by the number of consumed bytes afterwards
147  * @param size The size of the entire buffer, will be reduced by the number of consumed bytes afterwards
148  * @param vectors The resulting vectors
149  * @return True, if succeeded
150  */
151  static bool decodeVectors3(const uint8_t*& data, size_t& size, Vectors3& vectors);
152 
153  /**
154  * Decodes a set of 3D vectors.
155  * The buffer must provide the vectors with 64 bit precision.
156  * @param buffer The buffer providing the 3D vectors
157  * @param vectors The resulting vectors
158  * @return True, if succeeded
159  */
160  static inline bool decodeVectors3(const Buffer& buffer, Vectors3& vectors);
161 
162  /**
163  * Encodes a set of given 4D vectors.
164  * The vectors will be stored with 64 bit precision.
165  * @param vectors The vectors to encode
166  * @param size The number of given vectors
167  * @param buffer The resulting buffer which will be extended so that already existing data stays untouched
168  */
169  static void encodeVectors4(const Vector4* vectors, const size_t size, Buffer& buffer);
170 
171  /**
172  * Encodes a set of given 4D vectors.
173  * The vectors will be stored with 64 bit precision.
174  * @param vectors The vectors to encode
175  * @param size The number of given vectors
176  * @param reservedHeaderSize The number of bytes which will be reserved for an optional header, so that the resulting buffer has a header followed by the payload data
177  * @return The resulting buffer storing the given vectors
178  */
179  static inline Buffer encodeVectors4(const Vector4* vectors, const size_t size, const size_t reservedHeaderSize = 0);
180 
181  /**
182  * Decodes a set of 4D vectors.
183  * The buffer must provide the vectors with 64 bit precision.
184  * @param data The pointer to the buffer providing the 4D vectors, will be shifted by the number of consumed bytes afterwards
185  * @param size The size of the entire buffer, will be reduced by the number of consumed bytes afterwards
186  * @param vectors The resulting vectors
187  * @return True, if succeeded
188  */
189  static bool decodeVectors4(const uint8_t*& data, size_t& size, Vectors4& vectors);
190 
191  /**
192  * Decodes a set of 4D vectors.
193  * The buffer must provide the vectors with 64 bit precision.
194  * @param buffer The buffer providing the 4D vectors
195  * @param vectors The resulting vectors
196  * @return True, if succeeded
197  */
198  static inline bool decodeVectors4(const Buffer& buffer, Vectors4& vectors);
199 };
200 
201 inline Utilities::Buffer Utilities::encodeHomogenousMatrix4(const HomogenousMatrix4& matrix, const size_t reservedHeaderSize)
202 {
203  Buffer result(reservedHeaderSize);
204 
205  encodeHomogenousMatrix4(matrix, result);
206 
207  return result;
208 }
209 
211 {
212  const uint8_t* data = buffer.data();
213  size_t size = buffer.size();
214 
215  return decodeHomogenousMatrix4(data, size, matrix);
216 }
217 
218 inline Utilities::Buffer Utilities::encodeVectors2(const Vector2* vectors, const size_t size, const size_t reservedHeaderSize)
219 {
220  Buffer result(reservedHeaderSize);
221 
222  encodeVectors2(vectors, size, result);
223 
224  return result;
225 }
226 
227 inline bool Utilities::decodeVectors2(const Buffer& buffer, Vectors2& vectors)
228 {
229  const uint8_t* data = buffer.data();
230  size_t size = buffer.size();
231 
232  return decodeVectors2(data, size, vectors);
233 }
234 
235 inline Utilities::Buffer Utilities::encodeVectors3(const Vector3* vectors, const size_t size, const size_t reservedHeaderSize)
236 {
237  Buffer result(reservedHeaderSize);
238 
239  encodeVectors3(vectors, size, result);
240 
241  return result;
242 }
243 
244 inline bool Utilities::decodeVectors3(const Buffer& buffer, Vectors3& vectors)
245 {
246  const uint8_t* data = buffer.data();
247  size_t size = buffer.size();
248 
249  return decodeVectors3(data, size, vectors);
250 }
251 
252 inline Utilities::Buffer Utilities::encodeVectors4(const Vector4* vectors, const size_t size, const size_t reservedHeaderSize)
253 {
254  Buffer result(reservedHeaderSize);
255 
256  encodeVectors4(vectors, size, result);
257 
258  return result;
259 }
260 
261 inline bool Utilities::decodeVectors4(const Buffer& buffer, Vectors4& vectors)
262 {
263  const uint8_t* data = buffer.data();
264  size_t size = buffer.size();
265 
266  return decodeVectors4(data, size, vectors);
267 }
268 
269 }
270 
271 }
272 
273 #endif // META_OCEAN_IO_UTILITIES_H
This class implements utility functions for input and output operations.
Definition: io/Utilities.h:31
static bool decodeVectors2(const uint8_t *&data, size_t &size, Vectors2 &vectors)
Decodes a set of 2D vectors.
static void encodeVectors3(const Vector3 *vectors, const size_t size, Buffer &buffer)
Encodes a set of given 3D vectors.
static bool decodeVectors4(const uint8_t *&data, size_t &size, Vectors4 &vectors)
Decodes a set of 4D vectors.
static bool decodeHomogenousMatrix4(const uint8_t *&data, size_t &size, HomogenousMatrix4 &matrix)
Decodes a 4x4 homogeneous matrix.
std::vector< uint8_t > Buffer
Definition of a vector holding 8 bit elements.
Definition: io/Utilities.h:37
static bool readFile(const std::string &filename, Buffer &buffer)
Reads a file and copies the data into a buffer.
static void encodeHomogenousMatrix4(const HomogenousMatrix4 &matrix, Buffer &buffer)
Encodes a 4x4 homogeneous matrix.
static void encodeVectors4(const Vector4 *vectors, const size_t size, Buffer &buffer)
Encodes a set of given 4D vectors.
static bool decodeVectors3(const uint8_t *&data, size_t &size, Vectors3 &vectors)
Decodes a set of 3D vectors.
static void encodeVectors2(const Vector2 *vectors, const size_t size, Buffer &buffer)
Encodes a set of given 2D vectors.
This class implements a vector with four elements.
Definition: Vector4.h:97
std::vector< Vector4 > Vectors4
Definition of a vector holding Vector4 objects.
Definition: Vector4.h:65
std::vector< Vector2 > Vectors2
Definition of a vector holding Vector2 objects.
Definition: Vector2.h:64
std::vector< Vector3 > Vectors3
Definition of a vector holding Vector3 objects.
Definition: Vector3.h:65
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15