Ocean
Loading...
Searching...
No Matches
PointTrack.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_POINT_TRACK_H
9#define META_OCEAN_TRACKING_SLAM_POINT_TRACK_H
10
12
15
16namespace Ocean
17{
18
19namespace Tracking
20{
21
22namespace SLAM
23{
24
25// Forward declaration.
26class PointTrack;
27
28/**
29 * Definition of an unordered map mapping object point ids to point tracks.
30 * @ingroup trackingslam
31 */
32using PointTrackMap = std::unordered_map<Index32, PointTrack>;
33
34/**
35 * This class implements a point track which stores continuous 2D observations of a 3D object point over consecutive frames.
36 * A point track maintains a sequence of 2D image points observed in consecutive frames, starting from a first frame index.
37 * @ingroup trackingslam
38 */
39class OCEAN_TRACKING_SLAM_EXPORT PointTrack
40{
41 public:
42
43 /**
44 * Creates a new point track with a single observation.
45 * @param firstFrameIndex The index of the first frame in which the point is observed, with range [0, infinity)
46 * @param imagePoint The 2D image point observation in the first frame
47 */
48 inline PointTrack(const Index32 firstFrameIndex, const Vector2& imagePoint);
49
50 /**
51 * Creates a new point track from existing observations.
52 * @param firstFrameIndex The index of the first frame in which the point is observed, with range [0, infinity)
53 * @param imagePoints The 2D image point observations for consecutive frames starting at firstFrameIndex, will be moved, must not be empty
54 */
55 explicit inline PointTrack(const Index32 firstFrameIndex, Vectors2&& imagePoints);
56
57 /**
58 * Adds a new observation to this point track.
59 * The observation must be for the next consecutive frame (lastFrameIndex() + 1).
60 * @param frameIndex The index of the frame for which the observation will be added, must be lastFrameIndex() + 1
61 * @param imagePoint The 2D image point observation
62 */
63 inline void addObservation(const Index32 frameIndex, const Vector2& imagePoint);
64
65 /**
66 * Returns whether this point track has an observation for a given frame index.
67 * @param frameIndex The index of the frame to check
68 * @param imagePoint Optional resulting 2D image point of the observation, nullptr if not of interest
69 * @return True, if an observation exists for the given frame index
70 */
71 inline bool hasObservation(const Index32 frameIndex, Vector2* imagePoint = nullptr) const;
72
73 /**
74 * Returns the observation for a given frame index.
75 * @param frameIndex The index of the frame for which the observation will be returned, with range [firstFrameIndex(), lastFrameIndex()]
76 * @return The 2D image point observation for the given frame
77 */
78 inline const Vector2& observation(const Index32 frameIndex) const;
79
80 /**
81 * Returns the last observation of this point track.
82 * @return The 2D image point of the last observation
83 */
84 inline const Vector2& lastImagePoint() const;
85
86 /**
87 * Returns the index of the first frame in which the point was observed.
88 * @return The first frame index
89 */
90 inline Index32 firstFrameIndex() const;
91
92 /**
93 * Returns the index of the last frame in which the point was observed.
94 * @return The last frame index
95 */
96 inline Index32 lastFrameIndex() const;
97
98 /**
99 * Returns the index of the next expected frame for a new observation.
100 * @return The next frame index, which is lastFrameIndex() + 1
101 */
102 inline Index32 nextFrameIndex() const;
103
104 /**
105 * Returns all 2D image point observations of this point track.
106 * @return The vector of image points for consecutive frames starting at firstFrameIndex()
107 */
108 inline const Vectors2& imagePoints() const;
109
110 /**
111 * Returns the number of observations in this point track.
112 * @return The number of observations, with range [1, infinity)
113 */
114 inline size_t numberObservations() const;
115
116 /**
117 * Returns the number of observations up to and including a given frame index.
118 * @param frameIndex The frame index up to which to count observations
119 * @return The number of observations, 0 if the frame index is outside the track's range
120 */
121 inline size_t numberObservationsUntil(const Index32 frameIndex) const;
122
123 /**
124 * Returns whether this point track is valid.
125 * A valid point track has a valid first frame index and at least one observation.
126 * @return True, if valid
127 */
128 inline bool isValid() const;
129
130 /**
131 * Determines the percentile track length for a set of point tracks up to a given frame index.
132 * @param frameIndex The frame index up to which to consider observations
133 * @param pointTrackMap The map of point tracks to analyze
134 * @param minimalTracks The minimal number of tracks required to compute the percentile, with range [1, infinity)
135 * @param percentile The percentile to compute, with range [0.0, 1.0]
136 * @return The percentile track length, 0 if there are fewer than minimalTracks tracks
137 */
138 static size_t determineTracksLengthUntil(const Index32 frameIndex, const PointTrackMap& pointTrackMap, const size_t minimalTracks, const double percentile);
139
140 /**
141 * Extracts 2D-2D correspondences from point tracks that span between two frame indices.
142 * @param firstFrameIndex The index of the first frame
143 * @param lastFrameIndex The index of the last frame, must be > firstFrameIndex
144 * @param pointTrackMap The map of point tracks to extract correspondences from
145 * @param firstImagePoints The resulting image points in the first frame
146 * @param lastImagePoints The resulting corresponding image points in the last frame
147 * @param objectPointIds The resulting object point ids for each correspondence
148 */
149 static void extractCorrespondences(const Index32 firstFrameIndex, const Index32 lastFrameIndex, const PointTrackMap& pointTrackMap, Vectors2& firstImagePoints, Vectors2& lastImagePoints, Indices32& objectPointIds);
150
151 /**
152 * Determines the percentile viewing angle between ray directions from two camera poses.
153 * @param camera The camera profile defining the projection, must be valid
154 * @param world_T_camera0 The transformation from the first camera to the world, must be valid
155 * @param world_T_camera1 The transformation from the second camera to the world, must be valid
156 * @param imagePoints0 The image points in the first camera frame
157 * @param imagePoints1 The corresponding image points in the second camera frame, must have the same size as imagePoints0
158 * @param validIndices The indices of valid correspondences to consider, must not be empty
159 * @param percentile The percentile to compute, with range [0.0, 1.0]
160 * @return The percentile viewing angle in radians
161 */
162 static Scalar determineViewingAngle(const AnyCamera& camera, const HomogenousMatrix4& world_T_camera0, const HomogenousMatrix4& world_T_camera1, const Vectors2& imagePoints0, const Vectors2& imagePoints1, const Indices32& validIndices, const double percentile);
163
164 protected:
165
166 /// The index of the first frame in which the point was observed, -1 if invalid.
167 Index32 firstFrameIndex_ = Index32(-1);
168
169 /// The 2D image point observations for consecutive frames starting at firstFrameIndex_.
171};
172
173inline PointTrack::PointTrack(const Index32 firstFrameIndex, const Vector2& imagePoint) :
174 firstFrameIndex_(firstFrameIndex)
175{
176 ocean_assert(firstFrameIndex_ != Index32(-1));
177
178 imagePoints_.reserve(32);
179 imagePoints_.emplace_back(imagePoint);
180
181 ocean_assert(isValid());
182}
183
184inline PointTrack::PointTrack(const Index32 firstFrameIndex, Vectors2&& imagePoints) :
185 firstFrameIndex_(firstFrameIndex),
186 imagePoints_(std::move(imagePoints))
187{
188 ocean_assert(isValid());
189}
190
191inline void PointTrack::addObservation(const Index32 frameIndex, const Vector2& imagePoint)
192{
193 ocean_assert(isValid());
194
195 ocean_assert_and_suppress_unused(frameIndex == lastFrameIndex() + 1u, frameIndex);
196
197 imagePoints_.push_back(imagePoint);
198}
199
200inline bool PointTrack::hasObservation(const Index32 frameIndex, Vector2* imagePoint) const
201{
202 ocean_assert(isValid());
203
204 if (frameIndex < firstFrameIndex_ || frameIndex > lastFrameIndex())
205 {
206 return false;
207 }
208
209 if (imagePoint != nullptr)
210 {
211 *imagePoint = imagePoints_[frameIndex - firstFrameIndex_];
212 }
213
214 return true;
215}
216
217inline const Vector2& PointTrack::observation(const Index32 frameIndex) const
218{
219 ocean_assert(isValid());
220 ocean_assert(firstFrameIndex_ <= frameIndex && frameIndex <= lastFrameIndex());
221
222 return imagePoints_[frameIndex - firstFrameIndex_];
223}
224
226{
227 ocean_assert(isValid());
228
229 return imagePoints_.back();
230}
231
233{
234 return firstFrameIndex_;
235}
236
238{
239 ocean_assert(isValid());
240
241 return firstFrameIndex_ + Index32(imagePoints_.size()) - 1u;
242}
243
245{
246 ocean_assert(isValid());
247
248 return firstFrameIndex_ + Index32(imagePoints_.size());
249}
250
252{
253 ocean_assert(isValid());
254
255 return imagePoints_;
256}
257
259{
260 ocean_assert(isValid());
261
262 return imagePoints_.size();
263}
264
265inline size_t PointTrack::numberObservationsUntil(const Index32 frameIndex) const
266{
267 ocean_assert(isValid());
268
269 if (firstFrameIndex_ > frameIndex || lastFrameIndex() < frameIndex)
270 {
271 return 0;
272 }
273
274 ocean_assert(firstFrameIndex_ <= frameIndex && frameIndex <= lastFrameIndex());
275
276 return size_t(frameIndex - firstFrameIndex_ + 1u);
277}
278
279inline bool PointTrack::isValid() const
280{
281 ocean_assert(firstFrameIndex_ == Index32(-1) || !imagePoints_.empty());
282
283 return firstFrameIndex_ != Index32(-1);
284}
285
286}
287
288}
289
290}
291
292#endif // META_OCEAN_TRACKING_SLAM_POINT_TRACK_H
This class implements the abstract base class for all AnyCamera objects.
Definition AnyCamera.h:131
This class implements a point track which stores continuous 2D observations of a 3D object point over...
Definition PointTrack.h:40
Index32 firstFrameIndex() const
Returns the index of the first frame in which the point was observed.
Definition PointTrack.h:232
size_t numberObservations() const
Returns the number of observations in this point track.
Definition PointTrack.h:258
const Vector2 & lastImagePoint() const
Returns the last observation of this point track.
Definition PointTrack.h:225
Vectors2 imagePoints_
The 2D image point observations for consecutive frames starting at firstFrameIndex_.
Definition PointTrack.h:170
Index32 nextFrameIndex() const
Returns the index of the next expected frame for a new observation.
Definition PointTrack.h:244
PointTrack(const Index32 firstFrameIndex, const Vector2 &imagePoint)
Creates a new point track with a single observation.
Definition PointTrack.h:173
Index32 firstFrameIndex_
The index of the first frame in which the point was observed, -1 if invalid.
Definition PointTrack.h:167
Index32 lastFrameIndex() const
Returns the index of the last frame in which the point was observed.
Definition PointTrack.h:237
const Vectors2 & imagePoints() const
Returns all 2D image point observations of this point track.
Definition PointTrack.h:251
size_t numberObservationsUntil(const Index32 frameIndex) const
Returns the number of observations up to and including a given frame index.
Definition PointTrack.h:265
bool isValid() const
Returns whether this point track is valid.
Definition PointTrack.h:279
static size_t determineTracksLengthUntil(const Index32 frameIndex, const PointTrackMap &pointTrackMap, const size_t minimalTracks, const double percentile)
Determines the percentile track length for a set of point tracks up to a given frame index.
static Scalar determineViewingAngle(const AnyCamera &camera, const HomogenousMatrix4 &world_T_camera0, const HomogenousMatrix4 &world_T_camera1, const Vectors2 &imagePoints0, const Vectors2 &imagePoints1, const Indices32 &validIndices, const double percentile)
Determines the percentile viewing angle between ray directions from two camera poses.
void addObservation(const Index32 frameIndex, const Vector2 &imagePoint)
Adds a new observation to this point track.
Definition PointTrack.h:191
bool hasObservation(const Index32 frameIndex, Vector2 *imagePoint=nullptr) const
Returns whether this point track has an observation for a given frame index.
Definition PointTrack.h:200
const Vector2 & observation(const Index32 frameIndex) const
Returns the observation for a given frame index.
Definition PointTrack.h:217
static void extractCorrespondences(const Index32 firstFrameIndex, const Index32 lastFrameIndex, const PointTrackMap &pointTrackMap, Vectors2 &firstImagePoints, Vectors2 &lastImagePoints, Indices32 &objectPointIds)
Extracts 2D-2D correspondences from point tracks that span between two frame indices.
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::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::unordered_map< Index32, PointTrack > PointTrackMap
Definition of an unordered map mapping object point ids to point tracks.
Definition PointTrack.h:32
The namespace covering the entire Ocean framework.
Definition Accessor.h:15