Ocean
RandomModelVariation.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_TRACKING_RMV_RANDOM_MODEL_VARIATION_H
9 #define META_OCEAN_TRACKING_RMV_RANDOM_MODEL_VARIATION_H
10 
11 #include "ocean/tracking/rmv/RMV.h"
12 
14 #include "ocean/base/Worker.h"
15 
16 #include "ocean/geometry/Error.h"
17 
20 
21 namespace Ocean
22 {
23 
24 namespace Tracking
25 {
26 
27 namespace RMV
28 {
29 
30 /**
31  * This class implements random model variation algorithms.
32  * @ingroup trackingrmv
33  */
34 class OCEAN_TRACKING_RMV_EXPORT RandomModelVariation
35 {
36  public:
37 
38  /**
39  * Returns the optimized camera pose for a given rough pose, a cloud of object points and a cloud of image points with a sufficient number of valid correspondences.<br>
40  * However, no explicit correspondences between the 3D object points and 2D image points are defined.<br>
41  * Therefore, the point order of the given points will be ignored.
42  *
43  * This function supports two individual modes:<br>
44  * The first mode (if tLessImagePoints is True) seeks a corresponding object point for each provided image point - thus, the number of image points must be smaller than the number of object points.<br>
45  * The second mode (if tLessImagePoints is False) seeks a corresponding image point for each provided object point - thus, the number of object points must be smaller than the number of image points.<br>
46  * Further, the amount of valid correspondences between image and object points must be defined to improve the accuracy of the pose determination.
47  * @param initialPoseIF The initial and rough inverted and flipped pose that will be improved by application of the random model variation approach
48  * @param pinholeCamera The pinhole camera profile defining the projection between 3D object points and 2D image points
49  * @param objectPoints Objects points to be used for pose determination, must be valid
50  * @param numberObjectPoints Number of object points to be used for pose determination, with range [numberImagePoints, infinity) if tLessImagePoints is True; with range [3, numberImagePoints) if tLessImagePoints is False
51  * @param imagePoints Image points to be used for pose determination, must be valid
52  * @param numberImagePoints Number of image points to be used for pose determination, with range [3, numberObjectPoints] if tLessImagePoints is True; with range [numberImagePoints, infinity) is tLessImagePoints is False
53  * @param numberValidCorrespondences Approximately number of valid correspondences between image points and object points, with range [3, min(numberImagePoints, numberObjectPoints)]
54  * @param randomGenerator Random generator object used as initialization for the local random generators
55  * @param poseIF The resulting inverted and flipped optimized pose
56  * @param errorDetermination Defines the applied error determination method to allowing different error quality results
57  * @param targetAverageSqrError The expected target average square pixel error for valid point correspondences to be reached before the calculation will stop, with range [0, infinity)
58  * @param maximalTranslationOffset Maximal translation offset between the initial pose and the final resulting pose for all three axes (should be approx. 3 times higher than the real expected value)
59  * @param maximalOrientationOffset Maximal orientation offset between the initial pose and the final resulting pose, in radian (should be approx. 3 times higher than the real expected value)
60  * @param timeout Maximal time for the algorithm to determine the pose, in seconds
61  * @param resultingSqrError Optional resulting average square pixel error, with range [0, infinity), must be -1 if specified
62  * @param correspondences Optional resulting point correspondences, for each index of an image point (first index) one corresponding object point index (second index) will be returned
63  * @param explicitStop Optional possibility to stop the matrix determination use a stop flag, True to stop the calculation explicitly
64  * @param worker Optional worker object to execute the function on several CPU cores concurrently
65  * @return True, if succeeded
66  * @tparam tLessImagePoints True, to find a corresponding object point for each given image point (as the number of image points is smaller than the number of object points); False, to find a corresponding image points for each object point
67  */
68  template <bool tLessImagePoints>
69  static bool optimizedPoseFromPointCloudsWithOneInitialPoseIF(const HomogenousMatrix4& initialPoseIF, const PinholeCamera& pinholeCamera, const Vector3* objectPoints, const size_t numberObjectPoints, const Vector2* imagePoints, const size_t numberImagePoints, const size_t numberValidCorrespondences, RandomGenerator& randomGenerator, HomogenousMatrix4& poseIF, const Geometry::Error::ErrorDetermination errorDetermination, const Scalar targetAverageSqrError = Scalar(4 * 4), const Vector3& maximalTranslationOffset = Vector3(Scalar(0.3), Scalar(0.3), Scalar(0.3)), const Scalar maximalOrientationOffset = Numeric::deg2rad(30), const double timeout = 1, Scalar* resultingSqrError = nullptr, IndexPairs32* correspondences = nullptr, bool* explicitStop = nullptr, Worker* worker = nullptr);
70 
71  /**
72  * Returns the optimized camera pose for several given rough pose candidates, a cloud of object points and a cloud of image points with a sufficient number of valid correspondences.<br>
73  * However, no explicit correspondences between the 3D object points and 2D image points are defined.<br>
74  * Therefore, the point order of the given points will be ignored.
75  *
76  * This function supports two individual modes:<br>
77  * The first mode (if tLessImagePoints is True) seeks a corresponding object point for each provided image point - thus, the number of image points must be smaller than the number of object points.<br>
78  * The second mode (if tLessImagePoints is False) seeks a corresponding image point for each provided object point - thus, the number of object points must be smaller than the number of image points.<br>
79  * Further, the amount of valid correspondences between image and object points must be defined to improve the accuracy of the pose determination.
80  *
81  * In the case a worker object provided, this function will execute the pose determination using several CPU cores concurrently.<br>
82  * Each CPU core will receive a different initial pose. The entire calculation stops if the first execute receives a valid result.<br>
83  * Thus, the number of given initial poses should be a multiple of the existing CPU cores.
84  * @param initialPosesIF Initial and rough inverted and flipped poses to be used for precise pose determination
85  * @param numberInitialPoses The number of given initial poses, with range [2, infinity)
86  * @param pinholeCamera The pinhole camera profile defining the projection between 3D object points and 2D image points
87  * @param objectPoints Objects points to be used for pose determination, must be valid
88  * @param numberObjectPoints Number of object points to be used for pose determination, with range [numberImagePoints, infinity) if tLessImagePoints is True; with range [3, numberImagePoints) if tLessImagePoints is False
89  * @param imagePoints Image points to be used for pose determination, must be valid
90  * @param numberImagePoints Number of image points to be used for pose determination, with range [3, numberObjectPoints] if tLessImagePoints is True; with range [numberImagePoints, infinity) is tLessImagePoints is False
91  * @param numberValidCorrespondences Approximately number of valid correspondences between image points and object points, with range [3, min(numberImagePoints, numberObjectPoints)]
92  * @param randomGenerator Random generator object used as initialization for the local random generators
93  * @param poseIF The one unique best matching (and optimized) inverted and flipped pose from the set of given poses
94  * @param errorDetermination Defines the applied error determination method to allowing different error quality results
95  * @param targetAverageSqrError The expected target average square pixel error for valid point correspondences to be reached before the calculation will stop, with range [0, infinity)
96  * @param maximalTranslationOffset Maximal translation offset between the initial pose and the final resulting pose for all three axes (should be approx. 3 times higher than the real expected value)
97  * @param maximalOrientationOffset Maximal orientation offset between the initial pose and the final resulting pose, in radian (should be approx. 3 times higher than the real expected value)
98  * @param timeout Maximal time for the algorithm to determine the pose, in seconds
99  * @param resultingSqrError Optional resulting average square pixel error, must be -1 if specified
100  * @param explicitStop Optional possibility to stop the matrix determination use a stop flag, True to stop the calculation explicitly
101  * @param worker Optional worker object to execute the function on several CPU cores concurrently
102  * @return True, if succeeded
103  * @tparam tLessImagePoints True, to find a corresponding object point for each given image point (as the number of image points is smaller than the number of object points); False, to find a corresponding image points for each object point
104  * @see Worker::threads().
105  */
106  template <bool tLessImagePoints>
107  static bool optimizedPoseFromPointCloudsWithSeveralInitialPosesIF(const HomogenousMatrix4* initialPosesIF, const size_t numberInitialPoses, const PinholeCamera& pinholeCamera, const Vector3* objectPoints, const size_t numberObjectPoints, const Vector2* imagePoints, const size_t numberImagePoints, const size_t numberValidCorrespondences, RandomGenerator& randomGenerator, HomogenousMatrix4& poseIF, const Geometry::Error::ErrorDetermination errorDetermination, const Scalar targetAverageSqrError = Scalar(4 * 4), const Vector3& maximalTranslationOffset = Vector3(Scalar(0.3), Scalar(0.3), Scalar(0.3)), const Scalar maximalOrientationOffset = Numeric::deg2rad(30), const double timeout = 1, Scalar* resultingSqrError = nullptr, bool* explicitStop = nullptr, Worker* worker = nullptr);
108 
109  private:
110 
111  /**
112  * Returns the extrinsic camera matrix by a rough pose, valid object points and image points with a sufficient number of valid correspondences.<br>
113  * However, no explicit correspondences between the 3D object points and 2D image points are defined.<br>
114  * Therefore, the point order of the given points will be ignored.
115 
116  * This function supports two individual modes:<br>
117  * The first mode (if tLessImagePoints is True) seeks a corresponding object point for each provided image point - thus, the number of image points must be smaller than the number of object points.<br>
118  * The second mode (if tLessImagePoints is False) seeks a corresponding image point for each provided object point - thus, the number of object points must be smaller than the number of image points.<br>
119  * Further, the amount of valid correspondences between image and object points must be defined to improve the accuracy of the pose determination.
120  * @param initialPoseIF The initial and rough inverted and flipped pose that will be improved by application of the random model variation approach
121  * @param pinholeCamera The pinhole camera profile defining the projection between 3D object points and 2D image points
122  * @param objectPoints Objects points to be used for pose determination, must be valid
123  * @param numberObjectPoints Number of object points to be used for pose determination, with range [numberImagePoints, infinity) if tLessImagePoints is True; with range [3, numberImagePoints) if tLessImagePoints is False
124  * @param imagePoints Image points to be used for pose determination, must be valid
125  * @param numberImagePoints Number of image points to be used for pose determination, with range [3, numberObjectPoints] if tLessImagePoints is True; with range [numberImagePoints, infinity) is tLessImagePoints is False
126  * @param numberValidCorrespondences Approximately number of valid correspondences between image points and object points, with range [3, min(numberImagePoints, numberObjectPoints)]
127  * @param randomGenerator Random generator object used as initialization for the local random generators
128  * @param poseIF The resulting optimized inverted and flipped pose
129  * @param errorDetermination Defines the applied error determination method to allowing different error quality results
130  * @param targetAverageSqrError The expected target average square pixel error for valid point correspondences to be reached before the calculation will stop, with range [0, infinity)
131  * @param maximalTranslationOffset Maximal translation offset between the initial pose and the final resulting pose for all three axes (should be approx. 3 times higher than the real expected value)
132  * @param maximalOrientationOffset Maximal orientation offset between the initial pose and the final resulting pose, in radian (should be approx. 3 times higher than the real expected value)
133  * @param timeout Maximal time for the algorithm to determine the pose, in seconds
134  * @param resultingSqrError Resulting average square pixel error
135  * @param correspondences Optional resulting point correspondences, for each index of an image point one corresponding object point index will be returned
136  * @param explicitStop Optional possibility to stop the matrix determination use a stop flag, True to stop the calculation explicitly
137  * @param lock Optional lock object to lock the resulting values during assignment for execution on several cpu cores
138  * @return True, if succeeded
139  * @tparam tLessImagePoints True, to find a corresponding object point for each given image point (as the number of image points is smaller than the number of object points); False, to find a corresponding image points for each object point
140  */
141  template <bool tLessImagePoints>
142  static bool optimizedPoseFromPointCloudsAbortableIF(const HomogenousMatrix4* initialPoseIF, const PinholeCamera* pinholeCamera, const Vector3* objectPoints, const size_t numberObjectPoints, const Vector2* imagePoints, const size_t numberImagePoints, const size_t numberValidCorrespondences, RandomGenerator* randomGenerator, HomogenousMatrix4* poseIF, const Geometry::Error::ErrorDetermination errorDetermination, const Scalar targetAverageSqrError = Scalar(4 * 4), Vector3 maximalTranslationOffset = Vector3(Scalar(0.3), Scalar(0.3), Scalar(0.3)), Scalar maximalOrientationOffset = Numeric::deg2rad(30), const double timeout = 1, Scalar* resultingSqrError = nullptr, IndexPairs32* correspondences = nullptr, bool* explicitStop = nullptr, Lock* lock = nullptr);
143 
144  /**
145  * Returns the optimized camera pose for a subset of several given rough pose candidates, a cloud of object points and a cloud of image points with a sufficient number of valid correspondences.<br>
146  * However, no explicit correspondences between the 3D object points and 2D image points are defined.<br>
147  * Therefore, the point order of the given points will be ignored.
148  *
149  * This function supports two individual modes:<br>
150  * The first mode (if tLessImagePoints is True) seeks a corresponding object point for each provided image point - thus, the number of image points must be smaller than the number of object points.<br>
151  * The second mode (if tLessImagePoints is False) seeks a corresponding image point for each provided object point - thus, the number of object points must be smaller than the number of image points.<br>
152  * Further, the amount of valid correspondences between image and object points must be defined to improve the accuracy of the pose determination.
153  * @param initialPosesIF The initial and rough inverted and flipped poses which will be improved by application of the random model variation approach
154  * @param firstInitialPose First initial pose to be handled, must be valid
155  * @param numberInitialPoses Number of initial poses to be handled, with range [1, infinity)
156  * @param pinholeCamera The pinhole camera profile defining the projection between 3D object points and 2D image points
157  * @param objectPoints Objects points to be used for pose determination, must be valid
158  * @param numberObjectPoints Number of object points to be used for pose determination, with range [numberImagePoints, infinity) if tLessImagePoints is True; with range [3, numberImagePoints) if tLessImagePoints is False
159  * @param imagePoints Image points to be used for pose determination, must be valid
160  * @param numberImagePoints Number of image points to be used for pose determination, with range [3, numberObjectPoints] if tLessImagePoints is True; with range [numberImagePoints, infinity) is tLessImagePoints is False
161  * @param numberValidCorrespondences Approximately number of valid correspondences between image points and object points, with range [3, min(numberImagePoints, numberObjectPoints)]
162  * @param randomGenerator Random generator object used as initialization for the local random generators
163  * @param poseIF The one unique best matching (and optimized) inverted and flipped pose from the set of given poses
164  * @param errorDetermination Defines the applied error determination method to allowing different error quality results
165  * @param targetAverageSqrError The expected target average square pixel error for valid point correspondences to be reached before the calculation will stop, with range [0, infinity)
166  * @param maximalTranslationOffset Maximal translation offset between the initial pose and the final resulting pose for all three axes (should be approx. 3 times higher than the real expected value)
167  * @param maximalOrientationOffset Maximal orientation offset between the initial pose and the final resulting pose, in radian (should be approx. 3 times higher than the real expected value)
168  * @param timeout Maximal time for the algorithm to determine the pose, in seconds
169  * @param resultingSqrError Resulting average square pixel error
170  * @param explicitStop Optional possibility to stop the matrix determination use a stop flag, True to stop the calculation explicitly
171  * @param lock Optional lock object assuring that only one valid result is returned
172  * @return True, if succeeded
173  * @tparam tLessImagePoints True, to find a corresponding object point for each given image point (as the number of image points is smaller than the number of object points); False, to find a corresponding image points for each object point
174  */
175  template <bool tLessImagePoints>
176  static bool optimizedPoseFromPointCloudsPoseIFSubset(const HomogenousMatrix4* initialPosesIF, const unsigned int firstInitialPose, const unsigned int numberInitialPoses, const PinholeCamera* pinholeCamera, const Vector3* objectPoints, const size_t numberObjectPoints, const Vector2* imagePoints, const size_t numberImagePoints, const size_t numberValidCorrespondences, RandomGenerator* randomGenerator, HomogenousMatrix4* poseIF, const Geometry::Error::ErrorDetermination errorDetermination, const Scalar targetAverageSqrError, Vector3 maximalTranslationOffset, Scalar maximalOrientationOffset, const double timeout, Scalar* resultingSqrError, bool* explicitStop, Lock* lock);
177 
178  /**
179  * Assigns a pose candidate to a target pose if the pose quality is better than the currently known pose quality.
180  * The pose itself is not used for error determination but the point clouds only.<br>
181  * This function can be thread-safe if a lock object is provided.
182  * @param smallPointGroup The smaller group of points, must be valid
183  * @param smallPointGroupNumber The number of points in the smaller group, with range [1, infinity)
184  * @param largePointGroup The larger group of points, must be valid
185  * @param largePointGroupNumber The number of points in the larger group, with range [smallPointGroupNumber, infinity)
186  * @param numberValidCorrespondences The expected number of points that have a corresponding point in the other point set, with range [1, smallPointGroupNumber]
187  * @param candidateSqrError The square error of the current candidate pose, with range [0, infinity)
188  * @param poseCandidateIF The inverted and flipped candidate pose
189  * @param poseIF The target pose (receiving the candidate pose if this pose is the best)
190  * @param errorDetermination The error determination to be used
191  * @param targetAverageSqrError The maximal square distance between two corresponding points so that they count as valid pair, with range [0, infinity)
192  * @param resultingSqrError Optional resulting square distance that can be used to decide the best result of parallel threads
193  * @param correspondences Optional resulting index pairs of correspondences between the two point groups, only for pairs with square distance better than 'targetAverageSqrError'
194  * @param explicitStop Optional stop flag that will be set if provided
195  * @param lock Optional lock object to make this function thread-safe
196  * @return True, if the pose candidate has been assigned as better pose
197  */
198  static bool assignBestPoseIF(const Vector2* smallPointGroup, const size_t smallPointGroupNumber, const Vector2* largePointGroup, const size_t largePointGroupNumber, const size_t numberValidCorrespondences, const Scalar candidateSqrError, const HomogenousMatrix4& poseCandidateIF, HomogenousMatrix4& poseIF, const Geometry::Error::ErrorDetermination errorDetermination, const Scalar targetAverageSqrError, Scalar* resultingSqrError, IndexPairs32* correspondences, bool* explicitStop, Lock* lock);
199 };
200 
201 }
202 
203 }
204 
205 }
206 
207 #endif // META_OCEAN_TRACKING_RMV_RANDOM_MODEL_VARIATION_H
ErrorDetermination
Definition of different error determination stages.
Definition: Error.h:41
This class implements a recursive lock object.
Definition: Lock.h:31
static constexpr T deg2rad(const T deg)
Converts deg to rad.
Definition: Numeric.h:3232
This class implements a generator for random numbers.
Definition: RandomGenerator.h:42
This class implements random model variation algorithms.
Definition: RandomModelVariation.h:35
static bool optimizedPoseFromPointCloudsWithOneInitialPoseIF(const HomogenousMatrix4 &initialPoseIF, const PinholeCamera &pinholeCamera, const Vector3 *objectPoints, const size_t numberObjectPoints, const Vector2 *imagePoints, const size_t numberImagePoints, const size_t numberValidCorrespondences, RandomGenerator &randomGenerator, HomogenousMatrix4 &poseIF, const Geometry::Error::ErrorDetermination errorDetermination, const Scalar targetAverageSqrError=Scalar(4 *4), const Vector3 &maximalTranslationOffset=Vector3(Scalar(0.3), Scalar(0.3), Scalar(0.3)), const Scalar maximalOrientationOffset=Numeric::deg2rad(30), const double timeout=1, Scalar *resultingSqrError=nullptr, IndexPairs32 *correspondences=nullptr, bool *explicitStop=nullptr, Worker *worker=nullptr)
Returns the optimized camera pose for a given rough pose, a cloud of object points and a cloud of ima...
static bool optimizedPoseFromPointCloudsAbortableIF(const HomogenousMatrix4 *initialPoseIF, const PinholeCamera *pinholeCamera, const Vector3 *objectPoints, const size_t numberObjectPoints, const Vector2 *imagePoints, const size_t numberImagePoints, const size_t numberValidCorrespondences, RandomGenerator *randomGenerator, HomogenousMatrix4 *poseIF, const Geometry::Error::ErrorDetermination errorDetermination, const Scalar targetAverageSqrError=Scalar(4 *4), Vector3 maximalTranslationOffset=Vector3(Scalar(0.3), Scalar(0.3), Scalar(0.3)), Scalar maximalOrientationOffset=Numeric::deg2rad(30), const double timeout=1, Scalar *resultingSqrError=nullptr, IndexPairs32 *correspondences=nullptr, bool *explicitStop=nullptr, Lock *lock=nullptr)
Returns the extrinsic camera matrix by a rough pose, valid object points and image points with a suff...
static bool optimizedPoseFromPointCloudsPoseIFSubset(const HomogenousMatrix4 *initialPosesIF, const unsigned int firstInitialPose, const unsigned int numberInitialPoses, const PinholeCamera *pinholeCamera, const Vector3 *objectPoints, const size_t numberObjectPoints, const Vector2 *imagePoints, const size_t numberImagePoints, const size_t numberValidCorrespondences, RandomGenerator *randomGenerator, HomogenousMatrix4 *poseIF, const Geometry::Error::ErrorDetermination errorDetermination, const Scalar targetAverageSqrError, Vector3 maximalTranslationOffset, Scalar maximalOrientationOffset, const double timeout, Scalar *resultingSqrError, bool *explicitStop, Lock *lock)
Returns the optimized camera pose for a subset of several given rough pose candidates,...
static bool assignBestPoseIF(const Vector2 *smallPointGroup, const size_t smallPointGroupNumber, const Vector2 *largePointGroup, const size_t largePointGroupNumber, const size_t numberValidCorrespondences, const Scalar candidateSqrError, const HomogenousMatrix4 &poseCandidateIF, HomogenousMatrix4 &poseIF, const Geometry::Error::ErrorDetermination errorDetermination, const Scalar targetAverageSqrError, Scalar *resultingSqrError, IndexPairs32 *correspondences, bool *explicitStop, Lock *lock)
Assigns a pose candidate to a target pose if the pose quality is better than the currently known pose...
static bool optimizedPoseFromPointCloudsWithSeveralInitialPosesIF(const HomogenousMatrix4 *initialPosesIF, const size_t numberInitialPoses, const PinholeCamera &pinholeCamera, const Vector3 *objectPoints, const size_t numberObjectPoints, const Vector2 *imagePoints, const size_t numberImagePoints, const size_t numberValidCorrespondences, RandomGenerator &randomGenerator, HomogenousMatrix4 &poseIF, const Geometry::Error::ErrorDetermination errorDetermination, const Scalar targetAverageSqrError=Scalar(4 *4), const Vector3 &maximalTranslationOffset=Vector3(Scalar(0.3), Scalar(0.3), Scalar(0.3)), const Scalar maximalOrientationOffset=Numeric::deg2rad(30), const double timeout=1, Scalar *resultingSqrError=nullptr, bool *explicitStop=nullptr, Worker *worker=nullptr)
Returns the optimized camera pose for several given rough pose candidates, a cloud of object points a...
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
std::vector< IndexPair32 > IndexPairs32
Definition of a vector holding 32 bit index pairs.
Definition: Base.h:144
float Scalar
Definition of a scalar type.
Definition: Math.h:128
VectorT3< Scalar > Vector3
Definition of a 3D vector.
Definition: Vector3.h:22
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15