Ocean
RMVFeatureMap.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_RMV_FEATURE_MAP_H
9 #define META_OCEAN_TRACKING_RMV_RMV_FEATURE_MAP_H
10 
11 #include "ocean/tracking/rmv/RMV.h"
13 
14 #include "ocean/base/Frame.h"
15 
16 #include "ocean/math/Box3.h"
18 
19 #include <vector>
20 
21 namespace Ocean
22 {
23 
24 namespace Tracking
25 {
26 
27 namespace RMV
28 {
29 
30 /**
31  * This class implements a feature map.
32  * @ingroup trackingrmv
33  */
34 class OCEAN_TRACKING_RMV_EXPORT RMVFeatureMap
35 {
36  public:
37 
38  /**
39  * Creates an empty feature map.
40  */
42 
43  /**
44  * Returns the object positions of the registered map features.
45  * @return Object positions
46  * @see initializationObjectPoints().
47  */
48  inline const Vectors3& objectPoints() const;
49 
50  /**
51  * Returns the object points to be used during initialization iterations.
52  * If no explicit initialization object points are defined the standard feature map object points are returned.
53  * @return Initialization object points
54  * @see objectPoints().
55  */
56  inline const Vectors3& initializationObjectPoints() const;
57 
58  /**
59  * Returns the indices of the most recently strong object points.
60  * @return Indices of the most recently strong object points
61  */
62  inline const Indices32& recentStrongObjectPointIndices() const;
63 
64  /**
65  * Returns the indices of the most recently semi-strong object points.
66  * @return Indices of the most recently semi-strong object points
67  */
68  inline const Indices32& recentSemiStrongObjectPointIndices() const;
69 
70  /**
71  * Returns the indices of the most recently used object points.
72  * @return Indices of the most recently used object points
73  */
74  inline const Indices32& recentUsedObjectPointIndices() const;
75 
76  /**
77  * Returns the most recently used object points.
78  * Additional this function can return a maximal amount of object points only.
79  * @param maxNumber The maximal number of requested object points, with range [1, infinity)
80  * @return Most recently used object points
81  */
82  inline Vectors3 recentUsedObjectPoints(const size_t maxNumber = size_t(-1)) const;
83 
84  /**
85  * Sets or changes the indices of the most recently object points.
86  * @param strongObjectPointIndices Indices of all strong object points, may be a subset of the used object points, with ascending order
87  * @param semiStrongObjectPointIndices Indices of all semi-strong object points, may be a subset of the used object points, the intersection with the strong features must be empty, with ascending order
88  * @param usedObjectPointIndices Indices of all (valid and therefore) use object points, with ascending order
89  */
90  inline void setMostRecentObjectPointIndices(Indices32&& strongObjectPointIndices, Indices32&& semiStrongObjectPointIndices, Indices32&& usedObjectPointIndices);
91 
92  /**
93  * Removes / clears the indices of the most recently object points.
94  * The indices of all strong, semi-strong and used object points will be released.<br>
95  */
96  inline void clearMostRecentObjectPointIndices();
97 
98  /**
99  * Returns the bounding box of this map.
100  * @return Bounding box
101  * @see initializationBoundingBox().
102  */
103  inline const Box3& boundingBox() const;
104 
105  /**
106  * Returns the initialization camera if defined.
107  * @return Initialization camera object
108  */
109  inline const PinholeCamera& initializationCamera() const;
110 
111  /**
112  * Returns the detector type for the normal feature map.
113  * @return Detector type
114  */
115  inline RMVFeatureDetector::DetectorType detectorType() const;
116 
117  /**
118  * Returns the detector type for the initialization features.
119  * @return Initialization detector type
120  */
121  inline RMVFeatureDetector::DetectorType initializationDetectorType() const;
122 
123  /**
124  * Returns the bounding box of the initialization feature map.
125  * The bounding box covers all strong features used for initialization.<br>
126  * If no explicit initialization features are specified the bounding box of the standard features is returned.<br>
127  * @return Initialization bounding box
128  * @see boundingBox().
129  */
130  inline const Box3& initializationBoundingBox() const;
131 
132  /**
133  * Sets or replaces the features for this feature map by a given set of 3D features points.
134  * @param points The points to be used as new feature map, can be nullptr if number is zero
135  * @param number The number of features to be set, with range [0, infinity)
136  * @param pinholeCamera The pinhole camera profile which will be used during the tracking
137  * @param detectorType The type of the detector which has been used to detect the provided feature points
138  */
139  void setFeatures(const Vector3* points, const size_t number, const PinholeCamera& pinholeCamera, const RMVFeatureDetector::DetectorType detectorType);
140 
141  /**
142  * Sets or replaces the features for this feature map by a given tracking pattern.
143  * @param pattern The tracking pattern from which unique and strong feature points will be extracted and stored as feature map, must be valid
144  * @param dimension The dimension of the pattern in the world coordinate system, with range (0, infinity)x[0, infinity)x[0]
145  * @param pinholeCamera The pinhole camera profile which will be used during tracking
146  * @param numberFeatures The maximal number of feature points which will be extracted from the provided pattern, with range [10, infinity)
147  * @param detectorType Detector type the feature points has been detected with
148  * @param worker Optional worker object to distribute the computation
149  * @return True, if succeeded
150  */
151  bool setFeatures(const Frame& pattern, const Vector3& dimension, const PinholeCamera& pinholeCamera, const size_t numberFeatures, const RMVFeatureDetector::DetectorType detectorType, Worker* worker = nullptr);
152 
153  /**
154  * Sets or replaces the initialization features for this feature map by a given set of 3D features points.
155  * These features are used for initialization only and thus should be a small subset of strong features.<br>
156  * If no initialization features are specified the normal feature map points are used during initialization.
157  * @param objectPoints The 3D object points to be used as new feature map during initialization iterations, can be nullptr if 'number' is zero
158  * @param number The number of initialization features to be set, with range [0, infinity)
159  * @param initializationCamera Specific initialization camera
160  * @param initializationDetectorType Specific detector type for the initialization
161  */
162  void setInitializationFeatures(const Vector3* objectPoints, const size_t number, const PinholeCamera& initializationCamera, const RMVFeatureDetector::DetectorType initializationDetectorType);
163 
164  /**
165  * Sets or replaces the initialization features for this feature map by a given set of 3D features points.
166  * These features are used for initialization only and thus should be a small subset of strong features.<br>
167  * If no initialization features are specified the normal feature map points are used during initialization.
168  * @param objectPoints The 3D object points to be used as new feature map during initialization iterations, will be moved
169  * @param initializationCamera Specific initialization camera
170  * @param initializationDetectorType Specific detector type for the initialization
171  */
172  void setInitializationFeatures(Vectors3&& objectPoints, const PinholeCamera& initializationCamera, const RMVFeatureDetector::DetectorType initializationDetectorType);
173 
174  /**
175  * Sets or replaces the features that will be used during the initialization only for this feature map by a given tracking pattern.
176  * @param pattern The pattern frame the tracker will detect and track, must be valid
177  * @param dimension The dimension of the pattern in the world coordinate system, with range (0, infinity)x[0, infinity)x[0]
178  * @param pinholeCamera The pinhole camera profile which will be used during the tracking
179  * @param numberInitializationObjectPoints The number of 3D object points that will be used during the initialization, with range [1, infinity)
180  * @param initializationDetectorType The detector type which will be used during the initialization, may be different from the detector type which will be used after a successful initialization
181  * @param worker Optional worker object to distribute the computation
182  * @return True, if succeeded
183  */
184  bool setInitializationFeatures(const Frame& pattern, const Vector3& dimension, const PinholeCamera& pinholeCamera, const size_t numberInitializationObjectPoints, const RMVFeatureDetector::DetectorType& initializationDetectorType, Worker* worker = nullptr);
185 
186  /**
187  * Removes all registered map feature points.
188  */
189  void clear();
190 
191  /**
192  * Returns whether this feature map holds no feature points.
193  * @return True, if so
194  */
195  inline bool isEmpty() const;
196 
197  /**
198  * Returns whether this feature map holds at least one feature point.
199  * @return True, if so
200  */
201  explicit inline operator bool() const;
202 
203  protected:
204 
205  /// Object points defining the feature map.
207 
208  /// Object points defining the initialization feature map.
210 
211  /// Indices of the strongest object points from the most recently tracking iteration.
213 
214  /// Indices of the semi-strongest object points from the most recently tracking iteration.
216 
217  /// Indices of the used object points from the most recently tracking iteration.
219 
220  /// Bounding box covering all feature points.
222 
223  /// Bounding box covering all initialization feature points, if existing.
225 
226  /// Standard camera.
228 
229  /// PinholeCamera object explicitly used for camera initialization, if defined.
231 
232  /// Detector type used for the normal feature map features.
234 
235  /// Detector type explicitly used for initialization features.
237 };
238 
240 {
241  return mapObjectPoints;
242 }
243 
245 {
247 }
248 
250 {
252 }
253 
255 {
257 }
258 
260 {
261  ocean_assert(mapRecentUsedObjectPointIndices.size() < mapObjectPoints.size());
263 }
264 
265 inline Vectors3 RMVFeatureMap::recentUsedObjectPoints(const size_t maxNumber) const
266 {
267  ocean_assert(maxNumber != 0);
268 
269  const size_t number = min(mapRecentUsedObjectPointIndices.size(), maxNumber);
270 
271  Vectors3 result(number);
272 
273  for (size_t n = 0; n < number; ++n)
274  {
275  ocean_assert(mapRecentUsedObjectPointIndices[n] < mapObjectPoints.size());
277  }
278 
279  return result;
280 }
281 
282 inline void RMVFeatureMap::setMostRecentObjectPointIndices(Indices32&& strongObjectPointIndices, Indices32&& semiStrongObjectPointIndices, Indices32&& usedObjectPointIndices)
283 {
284 
285 #ifdef OCEAN_DEBUG
286 
287  // check whether the given strong and semi-strong features have no index in common
288  for (Indices32::const_iterator i = strongObjectPointIndices.begin(); i != strongObjectPointIndices.end(); ++i)
289  {
290  bool found = false;
291 
292  for (Indices32::const_iterator iS = semiStrongObjectPointIndices.begin(); iS != semiStrongObjectPointIndices.end(); ++iS)
293  if (*i == *iS)
294  {
295  found = true;
296  break;
297  }
298 
299  ocean_assert(!found);
300  }
301 
302  // check whether the order of the index is correct
303 
304  for (size_t n = 1; n < strongObjectPointIndices.size(); ++n)
305  ocean_assert(strongObjectPointIndices[n - 1] < strongObjectPointIndices[n]);
306 
307  for (size_t n = 1; n < semiStrongObjectPointIndices.size(); ++n)
308  ocean_assert(semiStrongObjectPointIndices[n - 1] < semiStrongObjectPointIndices[n]);
309 
310  for (size_t n = 1; n < usedObjectPointIndices.size(); ++n)
311  ocean_assert(usedObjectPointIndices[n - 1] < usedObjectPointIndices[n]);
312 
313 #endif // OCEAN_DEBUG
314 
315  mapRecentStrongObjectPointIndices = std::move(strongObjectPointIndices);
316  mapRecentSemiStrongObjectPointIndices = std::move(semiStrongObjectPointIndices);
317  mapRecentUsedObjectPointIndices = std::move(usedObjectPointIndices);
318 }
319 
321 {
325 }
326 
327 inline const Box3& RMVFeatureMap::boundingBox() const
328 {
329  return mapBoundingBox;
330 }
331 
333 {
334  ocean_assert(mapCamera || mapInitializationCamera);
336 }
337 
339 {
341 }
342 
344 {
345  return mapDetectorType;
346 }
347 
349 {
351 }
352 
353 inline bool RMVFeatureMap::isEmpty() const
354 {
355  return mapObjectPoints.empty();
356 }
357 
358 inline RMVFeatureMap::operator bool() const
359 {
360  return !mapObjectPoints.empty();
361 }
362 
363 }
364 
365 }
366 
367 }
368 
369 #endif // META_OCEAN_TRACKING_RMV_RMV_FEATURE_MAP_H
bool isValid() const
Returns whether the bounding box is valid.
This class implements Ocean's image class.
Definition: Frame.h:1760
DetectorType
Definition of individual feature detectors.
Definition: RMVFeatureDetector.h:41
This class implements a feature map.
Definition: RMVFeatureMap.h:35
const Box3 & initializationBoundingBox() const
Returns the bounding box of the initialization feature map.
Definition: RMVFeatureMap.h:348
void setMostRecentObjectPointIndices(Indices32 &&strongObjectPointIndices, Indices32 &&semiStrongObjectPointIndices, Indices32 &&usedObjectPointIndices)
Sets or changes the indices of the most recently object points.
Definition: RMVFeatureMap.h:282
Vectors3 recentUsedObjectPoints(const size_t maxNumber=size_t(-1)) const
Returns the most recently used object points.
Definition: RMVFeatureMap.h:265
const Box3 & boundingBox() const
Returns the bounding box of this map.
Definition: RMVFeatureMap.h:327
RMVFeatureDetector::DetectorType mapInitializationDetectorType
Detector type explicitly used for initialization features.
Definition: RMVFeatureMap.h:236
PinholeCamera mapCamera
Standard camera.
Definition: RMVFeatureMap.h:227
void setInitializationFeatures(Vectors3 &&objectPoints, const PinholeCamera &initializationCamera, const RMVFeatureDetector::DetectorType initializationDetectorType)
Sets or replaces the initialization features for this feature map by a given set of 3D features point...
const Indices32 & recentUsedObjectPointIndices() const
Returns the indices of the most recently used object points.
Definition: RMVFeatureMap.h:259
Box3 mapInitializationBoundingBox
Bounding box covering all initialization feature points, if existing.
Definition: RMVFeatureMap.h:224
Box3 mapBoundingBox
Bounding box covering all feature points.
Definition: RMVFeatureMap.h:221
bool isEmpty() const
Returns whether this feature map holds no feature points.
Definition: RMVFeatureMap.h:353
Indices32 mapRecentSemiStrongObjectPointIndices
Indices of the semi-strongest object points from the most recently tracking iteration.
Definition: RMVFeatureMap.h:215
void clearMostRecentObjectPointIndices()
Removes / clears the indices of the most recently object points.
Definition: RMVFeatureMap.h:320
Vectors3 mapInitializationObjectPoints
Object points defining the initialization feature map.
Definition: RMVFeatureMap.h:209
bool setFeatures(const Frame &pattern, const Vector3 &dimension, const PinholeCamera &pinholeCamera, const size_t numberFeatures, const RMVFeatureDetector::DetectorType detectorType, Worker *worker=nullptr)
Sets or replaces the features for this feature map by a given tracking pattern.
const PinholeCamera & initializationCamera() const
Returns the initialization camera if defined.
Definition: RMVFeatureMap.h:332
const Indices32 & recentStrongObjectPointIndices() const
Returns the indices of the most recently strong object points.
Definition: RMVFeatureMap.h:249
const Indices32 & recentSemiStrongObjectPointIndices() const
Returns the indices of the most recently semi-strong object points.
Definition: RMVFeatureMap.h:254
RMVFeatureDetector::DetectorType mapDetectorType
Detector type used for the normal feature map features.
Definition: RMVFeatureMap.h:233
Indices32 mapRecentStrongObjectPointIndices
Indices of the strongest object points from the most recently tracking iteration.
Definition: RMVFeatureMap.h:212
void setInitializationFeatures(const Vector3 *objectPoints, const size_t number, const PinholeCamera &initializationCamera, const RMVFeatureDetector::DetectorType initializationDetectorType)
Sets or replaces the initialization features for this feature map by a given set of 3D features point...
const Vectors3 & initializationObjectPoints() const
Returns the object points to be used during initialization iterations.
Definition: RMVFeatureMap.h:244
bool setInitializationFeatures(const Frame &pattern, const Vector3 &dimension, const PinholeCamera &pinholeCamera, const size_t numberInitializationObjectPoints, const RMVFeatureDetector::DetectorType &initializationDetectorType, Worker *worker=nullptr)
Sets or replaces the features that will be used during the initialization only for this feature map b...
PinholeCamera mapInitializationCamera
PinholeCamera object explicitly used for camera initialization, if defined.
Definition: RMVFeatureMap.h:230
RMVFeatureDetector::DetectorType detectorType() const
Returns the detector type for the normal feature map.
Definition: RMVFeatureMap.h:343
Vectors3 mapObjectPoints
Object points defining the feature map.
Definition: RMVFeatureMap.h:206
void clear()
Removes all registered map feature points.
Indices32 mapRecentUsedObjectPointIndices
Indices of the used object points from the most recently tracking iteration.
Definition: RMVFeatureMap.h:218
RMVFeatureDetector::DetectorType initializationDetectorType() const
Returns the detector type for the initialization features.
Definition: RMVFeatureMap.h:338
RMVFeatureMap()
Creates an empty feature map.
void setFeatures(const Vector3 *points, const size_t number, const PinholeCamera &pinholeCamera, const RMVFeatureDetector::DetectorType detectorType)
Sets or replaces the features for this feature map by a given set of 3D features points.
const Vectors3 & objectPoints() const
Returns the object positions of the registered map features.
Definition: RMVFeatureMap.h:239
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
std::vector< Index32 > Indices32
Definition of a vector holding 32 bit index values.
Definition: Base.h:96
std::vector< Vector3 > Vectors3
Definition of a vector holding Vector3 objects.
Definition: Vector3.h:65
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15