Ocean
Loading...
Searching...
No Matches
PoseCorrespondences.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_SLAM_POSE_CORRESPONDENCES_H
9#define META_OCEAN_TRACKING_SLAM_POSE_CORRESPONDENCES_H
10
15
17
20
23
24namespace Ocean
25{
26
27namespace Tracking
28{
29
30namespace SLAM
31{
32
33/**
34 * This class holds 2D-3D point correspondences for camera pose estimation.
35 * The class allows memory reuse across frames to minimize allocations.
36 * @ingroup trackingslam
37 */
38class OCEAN_TRACKING_SLAM_EXPORT PoseCorrespondences
39{
40 public:
41
42 /**
43 * Creates a new pose correspondences object.
44 */
46
47 /**
48 * Adds a correspondence.
49 * @param objectPoint The 3D object point in world coordinates
50 * @param imagePoint The 2D image point observation
51 * @param objectPointId The unique identifier of the object point
52 * @param precision The localization precision of the object point
53 * @param imagePointSqrDistance The squared distance between previous and current image point (for motion estimation), 0 if unknown
54 */
55 inline void addCorrespondence(const Vector3& objectPoint, const Vector2& imagePoint, const Index32 objectPointId, const LocalizedObjectPoint::LocalizationPrecision precision, const Scalar imagePointSqrDistance = 0);
56
57 /**
58 * Resets and prepares pose correspondences from tracking correspondences.
59 * This method clears all previous data and converts valid 2D-2D tracking correspondences into 2D-3D correspondences suitable for pose estimation by extracting object point positions and metadata.
60 * @param trackingCorrespondences The source tracking correspondences with valid flags set
61 */
62 void reset(const TrackingCorrespondences& trackingCorrespondences);
63
64 /**
65 * Returns the number of correspondences.
66 * @return The number of correspondences
67 */
68 inline size_t size() const;
69
70 /**
71 * Returns whether no correspondences exist.
72 * @return True, if empty
73 */
74 inline bool isEmpty() const;
75
76 /**
77 * Estimates the camera pose from the correspondences.
78 * The method first attempts to refine a previous camera pose using non-linear optimization.
79 * If no valid previous pose is available or optimization fails, it falls back to RANSAC-based P3P.
80 * @param camera The camera profile defining the projection, must be valid
81 * @param world_T_previousCamera The pose of the previous camera frame, invalid if unavailable
82 * @param minimalNumberCorrespondences The minimal number of inliers required, with range [5, infinity)
83 * @param randomGenerator A random generator for RANSAC
84 * @param maximalProjectionError The maximal projection error for inliers in pixels, with range [0, infinity)
85 * @param estimatorType The robust estimator type (must not be ET_SQUARE)
86 * @param gravityConstraints Optional gravity constraints, nullptr if unused
87 * @param robustError Optional resulting mean squared projection error of inliers, nullptr if not needed
88 * @return The estimated camera pose if successful, nullptr otherwise
89 */
90 SharedCameraPose determinePose(const AnyCamera& camera, const HomogenousMatrix4& world_T_previousCamera, const unsigned int minimalNumberCorrespondences, RandomGenerator& randomGenerator, const Scalar maximalProjectionError, const Geometry::Estimator::EstimatorType estimatorType, const Geometry::GravityConstraints* gravityConstraints, Scalar* robustError = nullptr);
91
92 protected:
93
94 /**
95 * Clears all data for reuse.
96 */
97 void clear();
98
99 /**
100 * Filters the arrays to contain only inlier correspondences.
101 * This method uses the indices in inlierIndices_ to filter the arrays, and populates outlierObjectPointIds_ with the IDs of removed correspondences.
102 * After calling this method, only the inlier correspondences remain.
103 */
105
106 /**
107 * Reserves memory for the expected number of correspondences.
108 * @param capacity The expected number of correspondences
109 */
110 void reserve(const size_t capacity);
111
112 public:
113
114 /// The 3D object points in world coordinates.
116
117 /// The 2D image point observations, one for each object point.
119
120 /// The unique identifiers of the object points, one for each object point.
122
123 /// The localization precisions of the object points, one for each object point.
125
126 /// The squared distances between previous and current image points (for motion estimation), one for each object point.
128
129 /// The IDs of object points that were outliers.
131
132 /// The IDs of object points that contributed precisely to the pose (for debugging/visualization).
134
135 /// The IDs of object points that did not contribute precisely to the pose (for debugging/visualization).
137
138 /// The map version at the time the correspondences were gathered.
139 Index32 mapVersion_ = 0u;
140
141 protected:
142
143 /// The indices of inlier correspondences (into the arrays).
145};
146
147inline void PoseCorrespondences::addCorrespondence(const Vector3& objectPoint, const Vector2& imagePoint, const Index32 objectPointId, const LocalizedObjectPoint::LocalizationPrecision precision, const Scalar imagePointSqrDistance)
148{
149 objectPoints_.push_back(objectPoint);
150 imagePoints_.push_back(imagePoint);
151 objectPointIds_.push_back(objectPointId);
152 precisions_.push_back(precision);
153 imagePointSqrDistances_.push_back(imagePointSqrDistance);
154}
155
156inline size_t PoseCorrespondences::size() const
157{
158 ocean_assert(objectPoints_.size() == imagePoints_.size());
159 ocean_assert(objectPoints_.size() == objectPointIds_.size());
160
161 return objectPoints_.size();
162}
163
165{
166 return objectPoints_.empty();
167}
168
169}
170
171}
172
173}
174
175#endif // META_OCEAN_TRACKING_SLAM_POSE_CORRESPONDENCES_H
This class implements the abstract base class for all AnyCamera objects.
Definition AnyCamera.h:131
EstimatorType
Definition of individual robust estimator types.
Definition Estimator.h:34
This class implements a container allowing to define gravity constraints during e....
Definition GravityConstraints.h:63
This class implements a generator for random numbers.
Definition RandomGenerator.h:42
LocalizationPrecision
Definition of possible localization precisions.
Definition LocalizedObjectPoint.h:59
std::vector< LocalizationPrecision > LocalizationPrecisions
Definition of a vector holding localization precisions.
Definition LocalizedObjectPoint.h:75
This class holds 2D-3D point correspondences for camera pose estimation.
Definition PoseCorrespondences.h:39
LocalizedObjectPoint::LocalizationPrecisions precisions_
The localization precisions of the object points, one for each object point.
Definition PoseCorrespondences.h:124
Scalars imagePointSqrDistances_
The squared distances between previous and current image points (for motion estimation),...
Definition PoseCorrespondences.h:127
void applyInlierSubset()
Filters the arrays to contain only inlier correspondences.
void clear()
Clears all data for reuse.
SharedCameraPose determinePose(const AnyCamera &camera, const HomogenousMatrix4 &world_T_previousCamera, const unsigned int minimalNumberCorrespondences, RandomGenerator &randomGenerator, const Scalar maximalProjectionError, const Geometry::Estimator::EstimatorType estimatorType, const Geometry::GravityConstraints *gravityConstraints, Scalar *robustError=nullptr)
Estimates the camera pose from the correspondences.
Vectors3 objectPoints_
The 3D object points in world coordinates.
Definition PoseCorrespondences.h:115
void addCorrespondence(const Vector3 &objectPoint, const Vector2 &imagePoint, const Index32 objectPointId, const LocalizedObjectPoint::LocalizationPrecision precision, const Scalar imagePointSqrDistance=0)
Adds a correspondence.
Definition PoseCorrespondences.h:147
PoseCorrespondences()
Creates a new pose correspondences object.
void reset(const TrackingCorrespondences &trackingCorrespondences)
Resets and prepares pose correspondences from tracking correspondences.
Indices32 objectPointIds_
The unique identifiers of the object points, one for each object point.
Definition PoseCorrespondences.h:121
size_t size() const
Returns the number of correspondences.
Definition PoseCorrespondences.h:156
Indices32 inlierIndices_
The indices of inlier correspondences (into the arrays).
Definition PoseCorrespondences.h:144
void reserve(const size_t capacity)
Reserves memory for the expected number of correspondences.
UnorderedIndexSet32 preciseObjectPointIds_
The IDs of object points that contributed precisely to the pose (for debugging/visualization).
Definition PoseCorrespondences.h:133
UnorderedIndexSet32 impreciseObjectPointIds_
The IDs of object points that did not contribute precisely to the pose (for debugging/visualization).
Definition PoseCorrespondences.h:136
Vectors2 imagePoints_
The 2D image point observations, one for each object point.
Definition PoseCorrespondences.h:118
bool isEmpty() const
Returns whether no correspondences exist.
Definition PoseCorrespondences.h:164
Indices32 outlierObjectPointIds_
The IDs of object points that were outliers.
Definition PoseCorrespondences.h:130
This class holds 2D-2D point correspondences for frame-to-frame tracking.
Definition TrackingCorrespondences.h:40
std::vector< Index32 > Indices32
Definition of a vector holding 32 bit index values.
Definition Base.h:96
uint32_t Index32
Definition of a 32 bit index value.
Definition Base.h:84
std::unordered_set< Index32 > UnorderedIndexSet32
Definition of an unordered_set holding 32 bit indices.
Definition Base.h:126
std::vector< Vector2 > Vectors2
Definition of a vector holding Vector2 objects.
Definition Vector2.h:64
float Scalar
Definition of a scalar type.
Definition Math.h:129
std::vector< Vector3 > Vectors3
Definition of a vector holding Vector3 objects.
Definition Vector3.h:65
std::vector< Scalar > Scalars
Definition of a vector holding Scalar objects.
Definition Math.h:145
std::shared_ptr< CameraPose > SharedCameraPose
Definition of a shared pointer holding a CameraPose object.
Definition CameraPose.h:36
The namespace covering the entire Ocean framework.
Definition Accessor.h:15