Ocean
MathUtilities.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_MATH_MATH_UTILITIES_H
9 #define META_OCEAN_MATH_MATH_UTILITIES_H
10 
11 #include "ocean/math/Math.h"
15 
16 #include "ocean/base/Worker.h"
17 
18 namespace Ocean
19 {
20 
21 /**
22  * This class implements utility functions for the math library.
23  * @ingroup math
24  */
26 {
27  public:
28 
29  /**
30  * Transforms several objects (e.g., 3D vector or 4D vectors) by a transformation object (e.g., a 3x3 matrix or 4x4 matrix) using the multiplication operator.
31  * @param transformation The transformation to be used (e.g., a matrix)
32  * @param objects The object to be transformed (e.g. vectors)
33  * @param results The resulting transformed objects, the size of the vector will be adjusted to match the size of the provided objects
34  * @param worker Optional worker object to distribute the computation
35  * @param minimalObjectsPerThread Minimal number of objects assigned to one internal thread, with range [1, infinity)
36  */
37  template <typename TTransformation, typename TObject>
38  static inline void transform(const TTransformation& transformation, const std::vector<TObject>& objects, std::vector<TObject>& results, Worker* worker = nullptr, const unsigned int minimalObjectsPerThread = 1000u);
39 
40  /**
41  * Encodes a float value to four uint8 values.
42  * @param value The float value to decompose, with range [0, 1]
43  * @param decomposedValues The resulting four uint8 values, must be valid
44  */
45  static inline void encodeFloatToUint8(const float value, uint8_t* decomposedValues);
46 
47  /**
48  * Decodes four uint8 values into a float value
49  * @param decomposedValues The four uint8 values to decompose, must be valid
50  * @return The resulting decomposed float value, with range [0, 1]
51  */
52  static inline float decodeFloatFromUint8(const uint8_t* decomposedValues);
53 
54  protected:
55 
56  /**
57  * Transforms a subset of several objects (e.g., 3D vector or 4D vectors) by a transformation object (e.g., a 3x3 matrix or 4x4 matrix) using the multiplication operator.
58  * @param transformation The transformation to be used (e.g., a matrix)
59  * @param objects The object to be transformed (e.g. vectors)
60  * @param results The resulting transformed objects, the size of the vector will be adjusted to match the size of the provided objects
61  * @param firstObject The first object to be handled
62  * @param numberObjects The number of objects to be handled
63  */
64  template <typename TTransformation, typename TObject>
65  static inline void transformSubset(const TTransformation* transformation, const TObject* objects, TObject* results, const unsigned int firstObject, const unsigned int numberObjects);
66 };
67 
68 template <typename TTransformation, typename TObject>
69 inline void MathUtilities::transform(const TTransformation& transformation, const std::vector<TObject>& objects, std::vector<TObject>& results, Worker* worker, const unsigned int minimalObjectsPerThread)
70 {
71  ocean_assert(minimalObjectsPerThread >= 1u);
72  results.resize(objects.size());
73 
74  if (worker)
75  {
76  worker->executeFunction(Worker::Function::createStatic(transformSubset<TTransformation, TObject>, &transformation, objects.data(), results.data(), 0u, 0u), 0u, (unsigned int)objects.size(), 3u, 4u, minimalObjectsPerThread);
77  }
78  else
79  {
80  transformSubset<TTransformation, TObject>(&transformation, objects.data(), results.data(), 0u, (unsigned int)objects.size());
81  }
82 }
83 
84 inline void MathUtilities::encodeFloatToUint8(const float value, uint8_t* decomposedValues)
85 {
86  ocean_assert(0.0f <= value && value <= 1.0f);
87  ocean_assert(decomposedValues != nullptr);
88 
89  const float value1 = value * 255.0f;
90  const float value2 = value * 255.0f * 255.0f; // 255^2
91  const float value3 = value * 255.0f * 255.0f * 255.0f; // 255^3
92 
93  const int int0 = int(value1);
94  const int int1 = int((value1 - float(int(value1))) * 255.0f);
95  const int int2 = int((value2 - float(int(value2))) * 255.0f);
96  const int int3 = int((value3 - float(int(value3))) * 255.0f);
97 
98  ocean_assert(int0 >= 0 && int0 <= 255);
99  ocean_assert(int1 >= 0 && int1 <= 255);
100  ocean_assert(int2 >= 0 && int2 <= 255);
101  ocean_assert(int3 >= 0 && int3 <= 255);
102 
103  decomposedValues[0] = uint8_t(int0);
104  decomposedValues[1] = uint8_t(int1);
105  decomposedValues[2] = uint8_t(int2);
106  decomposedValues[3] = uint8_t(int3);
107 }
108 
109 float MathUtilities::decodeFloatFromUint8(const uint8_t* decomposedValues)
110 {
111  ocean_assert(decomposedValues != nullptr);
112 
113  return float(decomposedValues[0]) / 255.0f
114  + float(decomposedValues[1]) / (255.0f * 255.0f)
115  + float(decomposedValues[2]) / (255.0f * 255.0f * 255.0f)
116  + float(decomposedValues[3]) / (255.0f * 255.0f * 255.0f * 255.0f);
117 }
118 
119 template <>
120 inline void MathUtilities::transformSubset<SquareMatrixF3, VectorF3>(const SquareMatrixF3* transformation, const VectorF3* objects, VectorF3* results, const unsigned int firstObject, const unsigned int numberObjects)
121 {
122  ocean_assert(transformation != nullptr && objects != nullptr && results != nullptr);
123 
124  SquareMatrixF3::multiply(*transformation, objects + firstObject, results + firstObject, size_t(numberObjects));
125 }
126 
127 template <>
128 inline void MathUtilities::transformSubset<SquareMatrixD4, VectorD4>(const SquareMatrixD4* transformation, const VectorD4* objects, VectorD4* results, const unsigned int firstObject, const unsigned int numberObjects)
129 {
130  ocean_assert(transformation != nullptr && objects != nullptr && results != nullptr);
131 
132  SquareMatrixD4::multiply(*transformation, objects + firstObject, results + firstObject, size_t(numberObjects));
133 }
134 
135 template <>
136 inline void MathUtilities::transformSubset<SquareMatrixF4, VectorF4>(const SquareMatrixF4* transformation, const VectorF4* objects, VectorF4* results, const unsigned int firstObject, const unsigned int numberObjects)
137 {
138  ocean_assert(transformation != nullptr && objects != nullptr && results != nullptr);
139 
140  SquareMatrixF4::multiply(*transformation, objects + firstObject, results + firstObject, size_t(numberObjects));
141 }
142 
143 template <typename TTransformation, typename TObject>
144 inline void MathUtilities::transformSubset(const TTransformation* transformation, const TObject* objects, TObject* results, const unsigned int firstObject, const unsigned int numberObjects)
145 {
146  ocean_assert(transformation != nullptr && objects != nullptr && results != nullptr);
147 
148  for (unsigned int n = firstObject; n < firstObject + numberObjects; ++n)
149  {
150  results[n] = *transformation * objects[n];
151  }
152 }
153 
154 }
155 
156 #endif // META_OCEAN_MATH_MATH_UTILITIES_H
static Caller< void > createStatic(typename StaticFunctionPointerMaker< void, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass >::Type function)
Creates a new caller container for a static function with no function parameter.
Definition: Caller.h:2876
This class implements utility functions for the math library.
Definition: MathUtilities.h:26
static void transformSubset(const TTransformation *transformation, const TObject *objects, TObject *results, const unsigned int firstObject, const unsigned int numberObjects)
Transforms a subset of several objects (e.g., 3D vector or 4D vectors) by a transformation object (e....
Definition: MathUtilities.h:144
static void transform(const TTransformation &transformation, const std::vector< TObject > &objects, std::vector< TObject > &results, Worker *worker=nullptr, const unsigned int minimalObjectsPerThread=1000u)
Transforms several objects (e.g., 3D vector or 4D vectors) by a transformation object (e....
Definition: MathUtilities.h:69
static void encodeFloatToUint8(const float value, uint8_t *decomposedValues)
Encodes a float value to four uint8 values.
Definition: MathUtilities.h:84
static float decodeFloatFromUint8(const uint8_t *decomposedValues)
Decodes four uint8 values into a float value.
Definition: MathUtilities.h:109
This class implements a 3x3 square matrix.
Definition: SquareMatrix3.h:88
bool multiply(const VectorT2< T > &vector, VectorT2< T > &result) const
Multiplies a 2D vector with this matrix (from the right).
Definition: SquareMatrix3.h:1708
This class implements a 4x4 square matrix.
Definition: SquareMatrix4.h:85
static void multiply(const SquareMatrixT4< T > &matrix, const VectorT4< T > *vectors, VectorT4< T > *results, const size_t number)
Multiplies several 4D vectors with a given matrix.
Definition: SquareMatrix4.h:2561
This class implements a vector with three elements.
Definition: Vector3.h:97
This class implements a vector with four elements.
Definition: Vector4.h:97
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
bool executeFunction(const Function &function, const unsigned int first, const unsigned int size, const unsigned int firstIndex=(unsigned int)(-1), const unsigned int sizeIndex=(unsigned int)(-1), const unsigned int minimalIterations=1u, const unsigned int threadIndex=(unsigned int)(-1))
Executes a callback function separable by two function parameters.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15