Ocean
Loading...
Searching...
No Matches
LocalizedObjectPoint.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_LOCALIZED_OBJECT_POINT_H
9#define META_OCEAN_TRACKING_SLAM_LOCALIZED_OBJECT_POINT_H
10
15
17
19
20#include "ocean/io/Bitstream.h"
21
24
25#include "ocean/math/Vector3.h"
26
27namespace Ocean
28{
29
30namespace Tracking
31{
32
33namespace SLAM
34{
35
36// Forward declaration.
37class LocalizedObjectPoint;
38
39/**
40 * Definition of an unordered map mapping object point ids to localized object points.
41 * @ingroup trackingslam
42 */
43using LocalizedObjectPointMap = std::unordered_map<Index32, LocalizedObjectPoint>;
44
45/**
46 * This class implements a localized 3D object point.
47 * In contrast to an unlocalized object point, a localized object point has a known 3D position and holds visual descriptors for observations of the object point.
48 * A localized object point may (not yet) have a valid known 3D location - e.g., because there have not been enough observation from individual viewing angles.
49 * @ingroup trackingslam
50 */
51class OCEAN_TRACKING_SLAM_EXPORT LocalizedObjectPoint
52{
53 public:
54
55 /**
56 * Definition of possible localization precisions.
57 */
58 enum LocalizationPrecision : uint8_t
59 {
60 /// An invalid localization precision.
61 LP_INVALID = 0u,
62 /// The localization precision could not yet be decided (e.g., because of too few observations).
64 /// The localization precision is low, because the object point has not been observed from enough different viewing angles.
66 /// The localization precision is medium, because the object point has been observed from quite narrow viewing angles.
68 /// The localization precision is high, because the object point has been observed from several different viewing angles.
69 LP_HIGH
70 };
71
72 /**
73 * Definition of a vector holding localization precisions.
74 */
75 using LocalizationPrecisions = std::vector<LocalizationPrecision>;
76
77 /**
78 * Definition of individual optimization results.
79 */
80 enum OptimizationResult : uint32_t
81 {
82 /// The optimization failed because the object point does not have enough observations.
84 /// The optimization failed because the object point location does not fit to all observations.
86 /// The optimization succeeded.
87 OR_SUCCEEDED
88 };
89
90 /**
91 * This class implements a container for correspondences between object points and image points which can be reused to reduce memory re-allocations.
92 */
93 class OCEAN_TRACKING_SLAM_EXPORT CorrespondenceData
94 {
95 public:
96
97 /**
98 * Applies the subset of used indices to filter the correspondence data.
99 * This function removes correspondences that are not in the used indices and populates the bad object point ids.
100 */
102
103 /**
104 * Resets this data object so that it can be re-used.
105 */
106 void reset();
107
108 /**
109 * Returns whether the subset has already been applied.
110 * @return True, if so
111 */
112 inline bool isSubsetApplied() const;
113
114 /**
115 * Returns whether this correspondence data object is empty.
116 * @return True, if so
117 */
118 inline bool isEmpty() const;
119
120 public:
121
122 /// The 3D object points.
124
125 /// The 2D image points corresponding to the object points.
127
128 /// The ids of the object points.
130
131 /// The localization precisions of the object points.
133
134 /// The individual squared distances between previous and current image points, one for each observation, in squared pixel; empty if correspondences are not based on frame-to-frame tracking.
136
137 /// The indices of the used correspondences after pose determination.
139
140 /// The ids of object points that were rejected during pose determination.
142
143 /// The ids of object points with precise localization used for the pose, for debugging and visualization.
145
146 /// The ids of object points with imprecise localization used for the pose, for debugging and visualization.
148 };
149
150 /**
151 * This class implements a thread-safe container for object point ids.
152 * The container allows adding object point ids from multiple threads and retrieving them atomically.
153 */
155 {
156 public:
157
158 /**
159 * Adds object point ids to this container.
160 * This function is thread-safe.
161 * @param objectPointIds The object point ids to add
162 */
163 inline void add(const Indices32& objectPointIds);
164
165 /**
166 * Returns and clears all object point ids from this container.
167 * This function is thread-safe.
168 * @return The object point ids that were stored in this container
169 */
170 inline UnorderedIndexSet32 objectPointIds();
171
172 /**
173 * Clears all object point ids from this container.
174 * This function is thread-safe.
175 */
176 inline void clear();
177
178 protected:
179
180 /// The set of object point ids.
182
183 /// The lock for thread-safe access.
185 };
186
187 protected:
188
189 /**
190 * Definition of an unordered map mapping camera indices to 2D observations.
191 */
192 using ObservationMap = std::unordered_map<Index32, Vector2>;
193
194 public:
195
196 /**
197 * Creates a new localized object point from an unlocalized object point.
198 * The resulting localized object point will not yet contain a valid position for the object point.
199 * @param pointTrack The unlocalized point track containing the observations, must be valid
200 */
201 explicit LocalizedObjectPoint(const PointTrack& pointTrack);
202
203 /**
204 * Creates a new localized object point from an unlocalized object point for which the 3D position is already known.
205 * @param pointTrack The unlocalized point track containing the observations, must be valid
206 * @param position The known 3D position of the object point, must be valid
207 * @param localizationPrecision The localization precision of the object point
208 * @param isBundleAdjusted True, if the position has been determined during a bundle adjustment; False, otherwise
209 */
210 inline LocalizedObjectPoint(const PointTrack& pointTrack, const Vector3& position, const LocalizationPrecision localizationPrecision, const bool isBundleAdjusted);
211
212 /**
213 * Adds a new observation of this object point for a given frame index.
214 * Complexity: O(1).
215 * @param frameIndex The index of the frame for which the observation will be added
216 * @param imagePoint The 2D image point of the observation
217 */
218 inline void addObservation(const Index32 frameIndex, const Vector2& imagePoint);
219
220 /**
221 * Adds all observations from a point track to this localized object point.
222 * @param pointTrack The point track containing the observations to add, must be valid
223 */
224 void addObservations(const PointTrack& pointTrack);
225
226 /**
227 * Removes the observation of this object point for a given frame index.
228 * Complexity: O(1).
229 * @param frameIndex The index of the frame for which the observation will be removed
230 */
231 inline void removeObservation(const Index32 frameIndex);
232
233 /**
234 * Returns whether this object point has an observation for a given frame index.
235 * Complexity: O(1).
236 * @param frameIndex The index of the frame for which the observation will be checked
237 * @param imagePoint Optional resulting 2D image point of the observation, nullptr if not of interest
238 * @return True, if so
239 */
240 inline bool hasObservation(const Index32& frameIndex, Vector2* imagePoint = nullptr) const;
241
242 /**
243 * Returns the observation of this object point for a given frame index.
244 * Only call this function if hasObservation() returns true.<br>
245 * Complexity: O(1).
246 * @param frameIndex The index of the frame for which the observation will be returned
247 * @return The object point's observation for the given frame index
248 */
249 inline Vector2 observation(const Index32 frameIndex) const;
250
251 /**
252 * Returns the last observation of this object point.
253 * @return The object point's last observation
254 */
255 inline Observation lastObservation() const;
256
257 /**
258 * Returns the frame index of the last observation of this object point.
259 * Complexity: O(1).
260 * @return The object point's last observation frame index, -1 if no observation exists
261 */
262 inline Index32 lastObservationFrameIndex() const;
263
264 /**
265 * Returns the number of observations of this object point.
266 * Complexity: O(1).
267 * @return The object point's number of observations, with range [1, infinity)
268 */
269 inline size_t numberObservations() const;
270
271 /**
272 * Returns the position of this object point.
273 * @return The object point's position, Vector3::minValue() if the position is (not yet) known
274 */
275 inline const Vector3& position() const;
276
277 /**
278 * Sets or updates the position of this object point.
279 * @param objectPoint The new position of this object point, must not be Vector3::minValue()
280 * @param isBundleAdjusted True, if the position has been determined during a bundle adjustment; False, otherwise
281 */
282 inline void setPosition(const Vector3& objectPoint, const bool isBundleAdjusted);
283
284 /**
285 * Returns whether the position of this object point has been determined during a bundle adjustment.
286 * @return True, if the position has been determined during a bundle adjustment; False, if the position has been determined by lower precision methods
287 */
288 inline bool isBundleAdjusted() const;
289
290 /**
291 * Returns the localization precision of this object point.
292 * @return The object point's localization precision
293 */
294 inline LocalizationPrecision localizationPrecision() const;
295
296 /**
297 * Sets or updates the localization precision of this object point.
298 * @param localizationPrecision The new localization precision of this object point
299 */
300 inline void setLocalizationPrecision(const LocalizationPrecision localizationPrecision);
301
302 /**
303 * Returns the visual descriptors of this object point.
304 * An object point may have several visual descriptors, e.g., one for individual observations.
305 * @return The object point's visual descriptors
306 */
307 inline const CV::Detector::FREAKDescriptors32& descriptors() const;
308
309 /**
310 * Adds a new visual descriptor to this object point.
311 * @param frameIndex The index of the frame from which the descriptor has been extracted, with range [0, infinity)
312 * @param descriptor The new visual descriptor to be added, must be valid
313 */
314 inline void addDescriptors(const Index32 frameIndex, const CV::Detector::FREAKDescriptor32& descriptor);
315
316 /**
317 * Returns whether this object point needs a visual descriptor.
318 * @param frameIndex The index of the current frame, with range [0, infinity)
319 * @return True, if so
320 */
321 inline bool needDescriptor(const Index32 frameIndex) const;
322
323 /**
324 * Optimizes this localized 3D object point which is visible in the current camera frame and in several previous camera frames.
325 * This function should be called for object points which are not precise enough anymore (e.g., because they are based on a low number of observations).
326 * @param mapVersion The version of the map to use for the optimization
327 * @param camera The camera profile defining the projection, must be valid
328 * @param cameraPoses The camera poses which have been determined so far, must be valid
329 * @param currentFrameIndex The index of the current frame in which the object point is visible, with range [0, infinity)
330 * @param minimalNumberObservations The minimal number of observations which are necessary to optimize the object point, with range [2, infinity)
331 * @param maximalProjectionError The maximal projection error in pixels, with range [0, infinity)
332 * @param estimatorType The estimator type to be used, must be valid
333 * @return The optimization result, OR_SUCCEEDED if the position was updated
334 */
335 inline OptimizationResult optimizeObjectPoint(const Index32 mapVersion, const AnyCamera& camera, const CameraPoses& cameraPoses, const Index32 currentFrameIndex, const size_t minimalNumberObservations, const Scalar maximalProjectionError, const Geometry::Estimator::EstimatorType estimatorType);
336
337 /**
338 * Determines the optimized 3D position of this localized object point visible in the current camera frame and in several previous camera frames.
339 * This function computes an optimized position without modifying the internal state of this object.
340 * @param mapVersion The version of the map to use for the optimization
341 * @param camera The camera profile defining the projection, must be valid
342 * @param cameraPoses The camera poses which have been determined so far, must be valid
343 * @param currentFrameIndex The index of the current frame in which the object point is visible, with range [0, infinity)
344 * @param minimalNumberObservations The minimal number of observations which are necessary to optimize the object point, with range [2, infinity)
345 * @param maximalProjectionError The maximal projection error in pixels, with range [0, infinity)
346 * @param estimatorType The estimator type to be used, must be valid
347 * @param optimizedPosition The resulting optimized 3D position of this object point
348 * @return The optimization result, OR_SUCCEEDED if the position was successfully optimized
349 */
350 OptimizationResult optimizedObjectPoint(const Index32 mapVersion, const AnyCamera& camera, const CameraPoses& cameraPoses, const Index32 currentFrameIndex, const size_t minimalNumberObservations, const Scalar maximalProjectionError, const Geometry::Estimator::EstimatorType estimatorType, Vector3& optimizedPosition) const;
351
352 /**
353 * Updates the localization precision of this object point based on its observations and the camera poses.
354 * The precision is determined from the covariance matrix of the object point's 3D position estimate.
355 * @param camera The camera profile defining the projection, must be valid
356 * @param cameraPose The camera poses which have been determined so far, must not be empty
357 * @return True, if the precision has changed; False, if the precision has not changed
358 */
359 bool updateLocalizedObjectPointUncertainty(const AnyCamera& camera, const CameraPoses& cameraPose);
360
361 /**
362 * Determines the median viewing angle of this object point.
363 * @param cameraPoses The camera poses which have been determined so far, must be valid
364 * @return The median viewing angle in radian, 0 if the angle could not be determined
365 */
367
368 /**
369 * Determines the quality of a camera pose for a specific frame by computing projection errors.
370 * @param camera The camera profile defining the projection, must be valid
371 * @param cameraPoses The camera poses which have been determined so far, must be valid
372 * @param frameIndex The index of the frame for which the camera pose quality will be determined, with range [0, infinity)
373 * @param localizedObjectPointMap The map of localized object points
374 * @param minError The resulting minimum projection error in pixels
375 * @param averageError The resulting average projection error in pixels
376 * @param maxError The resulting maximum projection error in pixels
377 * @return The number of observations used to determine the quality, 0 if the pose could not be evaluated
378 */
379 static size_t determineCameraPoseQuality(const AnyCamera& camera, const CameraPoses& cameraPoses, const Index32 frameIndex, const LocalizedObjectPointMap& localizedObjectPointMap, Scalar& minError, Scalar& averageError, Scalar& maxError);
380
381 /**
382 * Determines the quality of a camera pose for a specific frame by computing projection errors using an inverted flipped camera transformation.
383 * @param camera The camera profile defining the projection, must be valid
384 * @param flippedCamera_T_world The inverted flipped camera transformation, must be valid
385 * @param frameIndex The index of the frame for which the camera pose quality will be determined, with range [0, infinity)
386 * @param localizedObjectPointMap The map of localized object points
387 * @param minError The resulting minimum projection error in pixels
388 * @param averageError The resulting average projection error in pixels
389 * @param maxError The resulting maximum projection error in pixels
390 * @return The number of observations used to determine the quality, 0 if the pose could not be evaluated
391 */
392 static size_t determineCameraPoseQualityIF(const AnyCamera& camera, const HomogenousMatrix4& flippedCamera_T_world, const Index32 frameIndex, const LocalizedObjectPointMap& localizedObjectPointMap, Scalar& minError, Scalar& averageError, Scalar& maxError);
393
394 /**
395 * Determines the quality of object points for a specific frame by computing projection errors using an inverted flipped camera transformation.
396 * Object points are classified as valid or invalid based on whether their projection error is below the maximal threshold.
397 * @param camera The camera profile defining the projection, must be valid
398 * @param flippedCamera_T_world The inverted flipped camera transformation, must be valid
399 * @param frameIndex The index of the frame for which the object point quality will be determined, with range [0, infinity)
400 * @param localizedObjectPointMap The map of localized object points
401 * @param maximalProjectionError The maximal projection error in pixels, with range [0, infinity)
402 * @param validObjectPointIds The resulting ids of object points with projection errors below the threshold
403 * @param invalidObjectPointIds The resulting ids of object points with projection errors above the threshold
404 */
405 static void determineObjectPointQualityIF(const AnyCamera& camera, const HomogenousMatrix4& flippedCamera_T_world, const Index32 frameIndex, const LocalizedObjectPointMap& localizedObjectPointMap, const Scalar maximalProjectionError, Indices32& validObjectPointIds, Indices32& invalidObjectPointIds);
406
407 /**
408 * Determines the quality of bundle adjustment for a specific frame.
409 * @param frameIndex The index of the frame for which the bundle adjustment quality will be determined, with range [0, infinity)
410 * @param localizedObjectPointMap The map of localized object points
411 * @param bundleAdjustedObjectPoints The resulting number of object points that have been bundle adjusted
412 * @return The total number of object points visible in the frame
413 */
414 static size_t determineBundleAdjustmentQuality(const Index32 frameIndex, const LocalizedObjectPointMap& localizedObjectPointMap, size_t& bundleAdjustedObjectPoints);
415
416 /**
417 * Determines the median distance from the camera to the localized object points.
418 * @param cameraPoses The camera poses which have been determined so far, must be valid
419 * @param frameIndex The index of the frame for which the median distance will be determined, with range [0, infinity)
420 * @param localizedObjectPointMap The map of localized object points
421 * @param onlyTrackedObjectPoints True, to only consider object points that have an observation in the current frame; False, to consider all object points in front of the camera
422 * @return The median distance in world units, -1 if the median could not be determined
423 */
424 static Scalar determineMedianLocalizedObjectDistances(const CameraPoses& cameraPoses, const Index32 frameIndex, const LocalizedObjectPointMap& localizedObjectPointMap, const bool onlyTrackedObjectPoints);
425
426 /**
427 * Determines the number of tracked object points for a specific frame.
428 * @param cameraPoses The camera poses which have been determined so far, must be valid
429 * @param frameIndex The index of the frame for which the number of tracked object points will be determined, with range [0, infinity)
430 * @param localizedObjectPointMap The map of localized object points
431 * @return The number of tracked object points
432 */
433 static size_t determineNumberTrackedObjectPoints(const CameraPoses& cameraPoses, const Index32 frameIndex, const LocalizedObjectPointMap& localizedObjectPointMap);
434
435 /**
436 * Determines the number of tracked object points for a specific frame from a given set of object point ids.
437 * @param cameraPoses The camera poses which have been determined so far, must be valid
438 * @param frameIndex The index of the frame for which the number of tracked object points will be determined, with range [0, infinity)
439 * @param localizedObjectPointMap The map of localized object points
440 * @param objectPointIds The set of object point ids to check
441 * @return The number of tracked object points
442 */
443 static size_t determineNumberTrackedObjectPoints(const CameraPoses& cameraPoses, const Index32 frameIndex, const LocalizedObjectPointMap& localizedObjectPointMap, const UnorderedIndexSet32& objectPointIds);
444
445 /**
446 * Determines the localization precision of an object point based on camera poses using an inverted flipped camera transformation.
447 * The precision is estimated from the covariance matrix of the triangulated 3D point.
448 * @param camera The camera profile defining the projection, must be valid
449 * @param flippedCameras_T_world The inverted flipped camera transformations for each observation, with at least 2 elements
450 * @param objectPoint The 3D position of the object point
451 * @return The determined localization precision
452 */
453 static LocalizationPrecision determineLocalizedObjectPointUncertaintyIF(const AnyCamera& camera, const HomogenousMatrices4& flippedCameras_T_world, const Vector3& objectPoint);
454
455 /**
456 * Determines the localization precision of an object point based on its covariance matrix.
457 * The precision is determined by analyzing the eigenvalues of the covariance matrix.
458 * @param covarianceMatrix The covariance matrix of the object point's 3D position, must not be singular
459 * @return The determined localization precision
460 */
462
463 /**
464 * Determines the camera pose for a specific frame using the localized object points.
465 * This function uses pose estimation with an optional prior pose from the previous frame.
466 * @param camera The camera profile defining the projection, must be valid
467 * @param cameraPoses The camera poses which have been determined so far, must be valid
468 * @param frameIndex The index of the frame for which the camera pose will be determined, with range [0, infinity)
469 * @param localizedObjectPointMap The map of localized object points, with at least 4 elements
470 * @param randomGenerator The random generator to use for RANSAC
471 * @param estimatorType The estimator type to be used, must be valid
472 * @param correspondenceData The reusable correspondence data container, will be modified
473 * @param gravityConstraints Optional gravity constraints to apply during pose optimization, nullptr if not used
474 * @return The determined camera pose, nullptr if the pose could not be determined
475 */
476 static SharedCameraPose determineCameraPose(const AnyCamera& camera, const CameraPoses& cameraPoses, const Index32 frameIndex, const LocalizedObjectPointMap& localizedObjectPointMap, RandomGenerator& randomGenerator, const Geometry::Estimator::EstimatorType estimatorType, CorrespondenceData& correspondenceData, const Geometry::GravityConstraints* gravityConstraints);
477
478 /**
479 * Determines the camera pose for a specific frame using RANSAC-based P3P pose estimation.
480 * @param camera The camera profile defining the projection, must be valid
481 * @param frameIndex The index of the frame for which the camera pose will be determined, with range [0, infinity)
482 * @param localizedObjectPointMap The map of localized object points
483 * @param world_T_camera The resulting camera-to-world transformation
484 * @param randomGenerator The random generator to use for RANSAC
485 * @param minimalCorrespondences The minimal number of correspondences required, with range [4, infinity)
486 * @param maximalProjectionError The maximal projection error in pixels for inlier classification, with range [0, infinity)
487 * @param usedObjectPointIds The resulting ids of object points used as inliers
488 * @param sqrError The resulting squared average projection error
489 * @param gravityConstraints Optional gravity constraints to apply during pose optimization, nullptr if not used
490 * @return True, if a valid pose was determined; False, otherwise
491 */
492 static bool determineCameraPose(const AnyCamera& camera, const Index32 frameIndex, const LocalizedObjectPointMap& localizedObjectPointMap, HomogenousMatrix4& world_T_camera, RandomGenerator& randomGenerator, const size_t minimalCorrespondences, const Scalar maximalProjectionError, Indices32& usedObjectPointIds, Scalar& sqrError, const Geometry::GravityConstraints* gravityConstraints);
493
494 /**
495 * Translates a localization precision enum value to a human-readable string.
496 * @param localizationPrecision The localization precision to translate
497 * @return The human-readable string representation of the localization precision
498 */
499 static std::string translateLocalizationPrecision(const LocalizationPrecision localizationPrecision);
500
501 /**
502 * Serializes a map of localized object points to a bitstream.
503 * @param localizedObjectPointMap The map of localized object points to serialize
504 * @param outputBitstream The output bitstream to write to
505 * @return True, if the serialization succeeded; False, otherwise
506 */
507 static bool serialize(const LocalizedObjectPointMap& localizedObjectPointMap, IO::OutputBitstream& outputBitstream);
508
509 /**
510 * Serializes a single localized object point to a bitstream.
511 * @param localizedObjectPoint The localized object point to serialize
512 * @param outputBitstream The output bitstream to write to
513 * @return True, if the serialization succeeded; False, otherwise
514 */
515 static bool serialize(const LocalizedObjectPoint& localizedObjectPoint, IO::OutputBitstream& outputBitstream);
516
517 protected:
518
519 /// The 3D position of the object point.
520 Vector3 position_ = Vector3::minValue();
521
522 /// True, if the position of the object point has been determined during a Bundle Adjustment; False, if the position has been determine by lower precision methods
523 bool isBundleAdjusted_ = false;
524
525 /// The map mapping camera indices to 2D observations.
527
528 /// The index of the last observation, -1 if no observation exists.
529 Index32 lastObservationFrameIndex_ = Index32(-1);
530
531 /// The localization precision of the object point.
532 LocalizationPrecision localizationPrecision_ = LP_INVALID;
533
534 /// The index of the frame in which the last descriptor was added, -1 if no descriptor has been added yet.
535 Index32 lastDescriptorFrameIndex_ = Index32(-1);
536
537 /// The visual descriptors of the object point.
539
540 /// TODO, we need to separate visibile from invisible localized object points to optimize performance
541};
542
543inline LocalizedObjectPoint::LocalizedObjectPoint(const PointTrack& pointTrack, const Vector3& position, const LocalizationPrecision localizationPrecision, const bool isBundleAdjusted) :
544 LocalizedObjectPoint(pointTrack)
545{
549}
550
551inline void LocalizedObjectPoint::addObservation(const Index32 frameIndex, const Vector2& imagePoint)
552{
553 ocean_assert(observationMap_.find(frameIndex) == observationMap_.cend());
554 observationMap_.emplace(frameIndex, imagePoint);
555
556 ocean_assert(lastObservationFrameIndex_ != Index32(-1));
557 ocean_assert(frameIndex > lastObservationFrameIndex_);
558 lastObservationFrameIndex_ = frameIndex;
559}
560
562{
563 ocean_assert(observationMap_.find(frameIndex) != observationMap_.cend());
564 observationMap_.erase(frameIndex);
565
566 if (frameIndex != lastObservationFrameIndex_)
567 {
568 return;
569 }
570
571 // we removed the last observation, so we have to find the new latest frame index
572
574
575 if (observationMap_.empty())
576 {
577 return;
578 }
579
580 for (const ObservationMap::value_type& observationPair : observationMap_)
581 {
582 const Index32 candidateFrameIndex = observationPair.first;
583
584 if (lastObservationFrameIndex_ == Index32(-1) || candidateFrameIndex > lastObservationFrameIndex_)
585 {
586 lastObservationFrameIndex_ = candidateFrameIndex;
587 }
588 }
589}
590
591inline bool LocalizedObjectPoint::hasObservation(const Index32& frameIndex, Vector2* imagePoint) const
592{
593 const ObservationMap::const_iterator iObservation = observationMap_.find(frameIndex);
594
595 if (iObservation == observationMap_.cend())
596 {
597 return false;
598 }
599
600 if (imagePoint != nullptr)
601 {
602 *imagePoint = iObservation->second;
603 }
604
605 return true;
606}
607
609{
610 const ObservationMap::const_iterator iObservation = observationMap_.find(frameIndex);
611
612 ocean_assert(iObservation != observationMap_.cend());
613
614 return iObservation->second;
615}
616
618{
619 ocean_assert(lastObservationFrameIndex_ != Index32(-1));
620
621 const ObservationMap::const_iterator iObservation = observationMap_.find(lastObservationFrameIndex_);
622 ocean_assert(iObservation != observationMap_.cend());
623
624 return Observation(iObservation->first, iObservation->second);
625}
626
631
633{
634 return observationMap_.size();
635}
636
638{
639 return position_;
640}
641
642inline void LocalizedObjectPoint::setPosition(const Vector3& objectPoint, const bool isBundleAdjusted)
643{
644 ocean_assert(objectPoint != Vector3::minValue());
645
646 position_ = objectPoint;
648}
649
654
659
664
666{
667 ocean_assert(lastDescriptorFrameIndex_ == Index32(-1) || lastDescriptorFrameIndex_ < frameIndex);
668 ocean_assert(descriptor.isValid());
669
670 lastDescriptorFrameIndex_ = frameIndex;
671
672 descriptors_.push_back(descriptor);
673}
674
675inline bool LocalizedObjectPoint::needDescriptor(const Index32 frameIndex) const
676{
678 {
679 return false;
680 }
681
682 if (descriptors_.empty())
683 {
684 return true;
685 }
686
687 if (descriptors_.size() >= 10)
688 {
689 return false;
690 }
691
692 ocean_assert(lastDescriptorFrameIndex_ != Index32(-1));
693
694 // intervals between descriptors = basicInterval, 2 * basicInterval, 4 * basicInterval, ...
695
696 const unsigned int basicInterval = 30u; // 30 frames
697
698 const unsigned int intervalFactor = (unsigned int)(1u << (unsigned int)(descriptors_.size() - 1u));
699
700 const unsigned int nextFrameIndex = lastDescriptorFrameIndex_ + basicInterval * intervalFactor;
701
702 return frameIndex >= nextFrameIndex;
703}
704
705inline LocalizedObjectPoint::OptimizationResult LocalizedObjectPoint::optimizeObjectPoint(const Index32 mapVersion, const AnyCamera& camera, const CameraPoses& cameraPoses, const Index32 currentFrameIndex, const size_t minimalNumberObservations, const Scalar maximalProjectionError, const Geometry::Estimator::EstimatorType estimatorType)
706{
707 Vector3 optimizedPosition;
708 const OptimizationResult optimizationResult = optimizedObjectPoint(mapVersion, camera, cameraPoses, currentFrameIndex, minimalNumberObservations, maximalProjectionError, estimatorType, optimizedPosition);
709
710 if (optimizationResult == OR_SUCCEEDED)
711 {
712 position_ = optimizedPosition;
713 }
714
715 return optimizationResult;
716}
717
719{
720 return isBundleAdjusted_;
721}
722
724{
725 return usedIndices_.empty();
726}
727
729{
730 ocean_assert(!objectPoints_.empty() || (imagePoints_.empty() && objectPointIds_.empty() && localizationPrecisions_.empty() && imagePointSqrDistances_.empty() && usedIndices_.empty() && badObjectPointIds_.empty()));
731
732 return objectPoints_.empty();
733}
734
736{
737 const ScopedLock scopedLock(lock_);
738
739 objectPointIdSet_.insert(objectPointIds.begin(), objectPointIds.end());
740}
741
743{
744 const ScopedLock scopedLock(lock_);
745
746 UnorderedIndexSet32 result(std::move(objectPointIdSet_));
747 objectPointIdSet_.clear();
748
749 return result;
750}
751
753{
754 const ScopedLock scopedLock(lock_);
755
756 objectPointIdSet_.clear();
757}
758
759}
760
761}
762
763}
764
765#endif // META_OCEAN_TRACKING_SLAM_LOCALIZED_OBJECT_POINT_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 an output bitstream.
Definition Bitstream.h:215
This class implements a recursive lock object.
Definition Lock.h:31
This class implements a generator for random numbers.
Definition RandomGenerator.h:42
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:147
This class implements a container for camera poses.
Definition CameraPoses.h:32
This class implements a container for correspondences between object points and image points which ca...
Definition LocalizedObjectPoint.h:94
UnorderedIndexSet32 poseNotPreciseObjectPointIds_
The ids of object points with imprecise localization used for the pose, for debugging and visualizati...
Definition LocalizedObjectPoint.h:147
Vectors3 objectPoints_
The 3D object points.
Definition LocalizedObjectPoint.h:123
Scalars imagePointSqrDistances_
The individual squared distances between previous and current image points, one for each observation,...
Definition LocalizedObjectPoint.h:135
void applySubset()
Applies the subset of used indices to filter the correspondence data.
UnorderedIndexSet32 posePreciseObjectPointIds_
The ids of object points with precise localization used for the pose, for debugging and visualization...
Definition LocalizedObjectPoint.h:144
bool isEmpty() const
Returns whether this correspondence data object is empty.
Definition LocalizedObjectPoint.h:728
LocalizationPrecisions localizationPrecisions_
The localization precisions of the object points.
Definition LocalizedObjectPoint.h:132
bool isSubsetApplied() const
Returns whether the subset has already been applied.
Definition LocalizedObjectPoint.h:723
Indices32 badObjectPointIds_
The ids of object points that were rejected during pose determination.
Definition LocalizedObjectPoint.h:141
Indices32 usedIndices_
The indices of the used correspondences after pose determination.
Definition LocalizedObjectPoint.h:138
Indices32 objectPointIds_
The ids of the object points.
Definition LocalizedObjectPoint.h:129
Vectors2 imagePoints_
The 2D image points corresponding to the object points.
Definition LocalizedObjectPoint.h:126
void reset()
Resets this data object so that it can be re-used.
This class implements a thread-safe container for object point ids.
Definition LocalizedObjectPoint.h:155
UnorderedIndexSet32 objectPointIdSet_
The set of object point ids.
Definition LocalizedObjectPoint.h:181
UnorderedIndexSet32 objectPointIds()
Returns and clears all object point ids from this container.
Definition LocalizedObjectPoint.h:742
Lock lock_
The lock for thread-safe access.
Definition LocalizedObjectPoint.h:184
void add(const Indices32 &objectPointIds)
Adds object point ids to this container.
Definition LocalizedObjectPoint.h:735
void clear()
Clears all object point ids from this container.
Definition LocalizedObjectPoint.h:752
This class implements a localized 3D object point.
Definition LocalizedObjectPoint.h:52
LocalizedObjectPoint(const PointTrack &pointTrack)
Creates a new localized object point from an unlocalized object point.
static SharedCameraPose determineCameraPose(const AnyCamera &camera, const CameraPoses &cameraPoses, const Index32 frameIndex, const LocalizedObjectPointMap &localizedObjectPointMap, RandomGenerator &randomGenerator, const Geometry::Estimator::EstimatorType estimatorType, CorrespondenceData &correspondenceData, const Geometry::GravityConstraints *gravityConstraints)
Determines the camera pose for a specific frame using the localized object points.
static size_t determineNumberTrackedObjectPoints(const CameraPoses &cameraPoses, const Index32 frameIndex, const LocalizedObjectPointMap &localizedObjectPointMap)
Determines the number of tracked object points for a specific frame.
Index32 lastObservationFrameIndex_
The index of the last observation, -1 if no observation exists.
Definition LocalizedObjectPoint.h:529
Scalar determineMedianViewingAngle(const CameraPoses &cameraPoses) const
Determines the median viewing angle of this object point.
LocalizationPrecision localizationPrecision_
The localization precision of the object point.
Definition LocalizedObjectPoint.h:532
static size_t determineBundleAdjustmentQuality(const Index32 frameIndex, const LocalizedObjectPointMap &localizedObjectPointMap, size_t &bundleAdjustedObjectPoints)
Determines the quality of bundle adjustment for a specific frame.
static LocalizationPrecision determineLocalizedObjectPointUncertaintyIF(const AnyCamera &camera, const HomogenousMatrices4 &flippedCameras_T_world, const Vector3 &objectPoint)
Determines the localization precision of an object point based on camera poses using an inverted flip...
static size_t determineCameraPoseQualityIF(const AnyCamera &camera, const HomogenousMatrix4 &flippedCamera_T_world, const Index32 frameIndex, const LocalizedObjectPointMap &localizedObjectPointMap, Scalar &minError, Scalar &averageError, Scalar &maxError)
Determines the quality of a camera pose for a specific frame by computing projection errors using an ...
void addDescriptors(const Index32 frameIndex, const CV::Detector::FREAKDescriptor32 &descriptor)
Adds a new visual descriptor to this object point.
Definition LocalizedObjectPoint.h:665
bool isBundleAdjusted() const
Returns whether the position of this object point has been determined during a bundle adjustment.
Definition LocalizedObjectPoint.h:718
bool hasObservation(const Index32 &frameIndex, Vector2 *imagePoint=nullptr) const
Returns whether this object point has an observation for a given frame index.
Definition LocalizedObjectPoint.h:591
static Scalar determineMedianLocalizedObjectDistances(const CameraPoses &cameraPoses, const Index32 frameIndex, const LocalizedObjectPointMap &localizedObjectPointMap, const bool onlyTrackedObjectPoints)
Determines the median distance from the camera to the localized object points.
CV::Detector::FREAKDescriptors32 descriptors_
The visual descriptors of the object point.
Definition LocalizedObjectPoint.h:538
Index32 lastDescriptorFrameIndex_
The index of the frame in which the last descriptor was added, -1 if no descriptor has been added yet...
Definition LocalizedObjectPoint.h:535
Index32 lastObservationFrameIndex() const
Returns the frame index of the last observation of this object point.
Definition LocalizedObjectPoint.h:627
LocalizationPrecision
Definition of possible localization precisions.
Definition LocalizedObjectPoint.h:59
@ LP_MEDIUM
The localization precision is medium, because the object point has been observed from quite narrow vi...
Definition LocalizedObjectPoint.h:67
@ LP_UNKNOWN
The localization precision could not yet be decided (e.g., because of too few observations).
Definition LocalizedObjectPoint.h:63
@ LP_LOW
The localization precision is low, because the object point has not been observed from enough differe...
Definition LocalizedObjectPoint.h:65
bool isBundleAdjusted_
True, if the position of the object point has been determined during a Bundle Adjustment; False,...
Definition LocalizedObjectPoint.h:523
static void determineObjectPointQualityIF(const AnyCamera &camera, const HomogenousMatrix4 &flippedCamera_T_world, const Index32 frameIndex, const LocalizedObjectPointMap &localizedObjectPointMap, const Scalar maximalProjectionError, Indices32 &validObjectPointIds, Indices32 &invalidObjectPointIds)
Determines the quality of object points for a specific frame by computing projection errors using an ...
void addObservation(const Index32 frameIndex, const Vector2 &imagePoint)
Adds a new observation of this object point for a given frame index.
Definition LocalizedObjectPoint.h:551
static std::string translateLocalizationPrecision(const LocalizationPrecision localizationPrecision)
Translates a localization precision enum value to a human-readable string.
OptimizationResult optimizeObjectPoint(const Index32 mapVersion, const AnyCamera &camera, const CameraPoses &cameraPoses, const Index32 currentFrameIndex, const size_t minimalNumberObservations, const Scalar maximalProjectionError, const Geometry::Estimator::EstimatorType estimatorType)
Optimizes this localized 3D object point which is visible in the current camera frame and in several ...
Definition LocalizedObjectPoint.h:705
static size_t determineCameraPoseQuality(const AnyCamera &camera, const CameraPoses &cameraPoses, const Index32 frameIndex, const LocalizedObjectPointMap &localizedObjectPointMap, Scalar &minError, Scalar &averageError, Scalar &maxError)
Determines the quality of a camera pose for a specific frame by computing projection errors.
OptimizationResult optimizedObjectPoint(const Index32 mapVersion, const AnyCamera &camera, const CameraPoses &cameraPoses, const Index32 currentFrameIndex, const size_t minimalNumberObservations, const Scalar maximalProjectionError, const Geometry::Estimator::EstimatorType estimatorType, Vector3 &optimizedPosition) const
Determines the optimized 3D position of this localized object point visible in the current camera fra...
void removeObservation(const Index32 frameIndex)
Removes the observation of this object point for a given frame index.
Definition LocalizedObjectPoint.h:561
bool needDescriptor(const Index32 frameIndex) const
Returns whether this object point needs a visual descriptor.
Definition LocalizedObjectPoint.h:675
std::vector< LocalizationPrecision > LocalizationPrecisions
Definition of a vector holding localization precisions.
Definition LocalizedObjectPoint.h:75
OptimizationResult
Definition of individual optimization results.
Definition LocalizedObjectPoint.h:81
@ OR_INACCURATE
The optimization failed because the object point location does not fit to all observations.
Definition LocalizedObjectPoint.h:85
@ OR_SUCCEEDED
The optimization succeeded.
Definition LocalizedObjectPoint.h:87
@ OR_NOT_ENOUGH_OBSERVATIONS
The optimization failed because the object point does not have enough observations.
Definition LocalizedObjectPoint.h:83
Vector2 observation(const Index32 frameIndex) const
Returns the observation of this object point for a given frame index.
Definition LocalizedObjectPoint.h:608
void addObservations(const PointTrack &pointTrack)
Adds all observations from a point track to this localized object point.
static bool determineCameraPose(const AnyCamera &camera, const Index32 frameIndex, const LocalizedObjectPointMap &localizedObjectPointMap, HomogenousMatrix4 &world_T_camera, RandomGenerator &randomGenerator, const size_t minimalCorrespondences, const Scalar maximalProjectionError, Indices32 &usedObjectPointIds, Scalar &sqrError, const Geometry::GravityConstraints *gravityConstraints)
Determines the camera pose for a specific frame using RANSAC-based P3P pose estimation.
const Vector3 & position() const
Returns the position of this object point.
Definition LocalizedObjectPoint.h:637
void setPosition(const Vector3 &objectPoint, const bool isBundleAdjusted)
Sets or updates the position of this object point.
Definition LocalizedObjectPoint.h:642
static bool serialize(const LocalizedObjectPoint &localizedObjectPoint, IO::OutputBitstream &outputBitstream)
Serializes a single localized object point to a bitstream.
bool updateLocalizedObjectPointUncertainty(const AnyCamera &camera, const CameraPoses &cameraPose)
Updates the localization precision of this object point based on its observations and the camera pose...
LocalizationPrecision localizationPrecision() const
Returns the localization precision of this object point.
Definition LocalizedObjectPoint.h:650
static size_t determineNumberTrackedObjectPoints(const CameraPoses &cameraPoses, const Index32 frameIndex, const LocalizedObjectPointMap &localizedObjectPointMap, const UnorderedIndexSet32 &objectPointIds)
Determines the number of tracked object points for a specific frame from a given set of object point ...
void setLocalizationPrecision(const LocalizationPrecision localizationPrecision)
Sets or updates the localization precision of this object point.
Definition LocalizedObjectPoint.h:655
Observation lastObservation() const
Returns the last observation of this object point.
Definition LocalizedObjectPoint.h:617
std::unordered_map< Index32, Vector2 > ObservationMap
Definition of an unordered map mapping camera indices to 2D observations.
Definition LocalizedObjectPoint.h:192
Vector3 position_
The 3D position of the object point.
Definition LocalizedObjectPoint.h:520
static bool serialize(const LocalizedObjectPointMap &localizedObjectPointMap, IO::OutputBitstream &outputBitstream)
Serializes a map of localized object points to a bitstream.
const CV::Detector::FREAKDescriptors32 & descriptors() const
Returns the visual descriptors of this object point.
Definition LocalizedObjectPoint.h:660
ObservationMap observationMap_
The map mapping camera indices to 2D observations.
Definition LocalizedObjectPoint.h:526
size_t numberObservations() const
Returns the number of observations of this object point.
Definition LocalizedObjectPoint.h:632
static LocalizationPrecision determineLocalizedObjectPointUncertainty(const SquareMatrix3 &covarianceMatrix)
Determines the localization precision of an object point based on its covariance matrix.
This class implements an observation of a 3D feature point in a camera frame.
Definition Observation.h:39
This class implements a point track which stores continuous 2D observations of a 3D object point over...
Definition PointTrack.h:40
static VectorT3< Scalar > minValue()
Returns a 3D vector with all elements set to NumericT::minValue().
Definition Vector3.h:1059
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< HomogenousMatrix4 > HomogenousMatrices4
Definition of a vector holding HomogenousMatrix4 objects.
Definition HomogenousMatrix4.h:73
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::unordered_map< Index32, LocalizedObjectPoint > LocalizedObjectPointMap
Definition of an unordered map mapping object point ids to localized object points.
Definition LocalizedObjectPoint.h:43
std::shared_ptr< CameraPose > SharedCameraPose
Definition of a shared pointer holding a CameraPose object.
Definition CameraPose.h:36
std::vector< FREAKDescriptor32 > FREAKDescriptors32
Vector of 32-bytes long FREAK descriptors.
Definition FREAKDescriptor.h:69
FREAKDescriptorT< 32 > FREAKDescriptor32
Typedef for the 32-bytes long FREAK descriptor.
Definition FREAKDescriptor.h:66
The namespace covering the entire Ocean framework.
Definition Accessor.h:15