Ocean
Loading...
Searching...
No Matches
FramePyramidManager.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_FRAME_PYRAMID_MANAGER_H
9#define META_OCEAN_TRACKING_SLAM_FRAME_PYRAMID_MANAGER_H
10
12
13#include "ocean/base/Lock.h"
15
17
18namespace Ocean
19{
20
21namespace Tracking
22{
23
24namespace SLAM
25{
26
27/**
28 * This class manages a pool of frame pyramids for efficient reuse.
29 * The manager provides thread-safe access to frame pyramids with automatic lifetime management.
30 * @ingroup trackingslam
31 */
32class OCEAN_TRACKING_SLAM_EXPORT FramePyramidManager
33{
34 public:
35
36 /**
37 * This class implements a scoped pyramid object providing automatic lifetime management.
38 * The pyramid is automatically released when the scoped object is destroyed.
39 */
41 {
42 friend class FramePyramidManager;
43
44 public:
45
46 /**
47 * Creates an invalid scoped pyramid object.
48 */
49 ScopedPyramid() = default;
50
51 /**
52 * Move constructor.
53 * @param scopedPyramid The scoped pyramid object to move
54 */
55 inline ScopedPyramid(ScopedPyramid&& scopedPyramid) noexcept;
56
57 /**
58 * Destructs the scoped pyramid and releases the pyramid.
59 */
60 inline ~ScopedPyramid();
61
62 /**
63 * Returns the frame index of this pyramid.
64 * @return The frame index
65 */
66 inline Index32 frameIndex() const;
67
68 /**
69 * Returns a const reference to the pyramid.
70 * @return The pyramid object
71 */
72 inline const CV::FramePyramid& pyramid() const;
73
74 /**
75 * Returns the finest layer of the pyramid.
76 * @return The finest layer
77 */
78 inline const Frame& finestLayer() const;
79
80 /**
81 * Returns a reference to the pyramid.
82 * @return The pyramid object
83 */
84 inline CV::FramePyramid& pyramid();
85
86 /**
87 * Returns the finest layer of the pyramid.
88 * @return The finest layer
89 */
90 inline Frame& finestLayer();
91
92 /**
93 * Explicitly releases the pyramid before the scoped object is destroyed.
94 */
95 void release();
96
97 /**
98 * Returns whether this scoped pyramid is valid.
99 * @return True, if so
100 */
101 inline bool isValid() const;
102
103 /**
104 * Returns whether this scoped pyramid is valid.
105 * @return True, if so
106 */
107 inline operator bool() const;
108
109 /**
110 * Returns a const reference to the pyramid.
111 * @return The pyramid object
112 */
113 inline const CV::FramePyramid& operator*() const;
114
115 /**
116 * Returns a reference to the pyramid.
117 * @return The pyramid object
118 */
119 inline CV::FramePyramid& operator*();
120
121 /**
122 * Returns a const pointer to the pyramid.
123 * @return The pyramid object
124 */
125 inline const CV::FramePyramid* operator->() const;
126
127 /**
128 * Returns a pointer to the pyramid.
129 * @return The pyramid object
130 */
131 inline CV::FramePyramid* operator->();
132
133 /**
134 * Move assignment operator.
135 * @param scopedPyramid The scoped pyramid object to move
136 * @return Reference to this object
137 */
138 ScopedPyramid& operator=(ScopedPyramid&& scopedPyramid) noexcept;
139
140 protected:
141
142 /**
143 * Creates a new scoped pyramid object.
144 * @param owner The owner manager
145 * @param pyramid The pyramid object
146 * @param frameIndex The frame index
147 */
148 inline ScopedPyramid(FramePyramidManager& owner, CV::FramePyramid& pyramid, const Index32 frameIndex);
149
150 /**
151 * Disabled copy constructor.
152 * @param scopedPyramid The scoped pyramid object to copy
153 */
154 ScopedPyramid(const ScopedPyramid& scopedPyramid) = delete;
155
156 /**
157 * Disabled copy assignment operator.
158 * @param scopedPyramid The scoped pyramid object to copy
159 * @return Reference to this object
160 */
161 ScopedPyramid& operator=(const ScopedPyramid& scopedPyramid) = delete;
162
163 protected:
164
165 /// The owner manager.
166 FramePyramidManager* owner_ = nullptr;
167
168 /// The pyramid object.
169 CV::FramePyramid* pyramid_ = nullptr;
170
171 /// The frame index.
172 Index32 frameIndex_ = Index32(-1);
173 };
174
175 protected:
176
177 /**
178 * This class implements a pyramid object with usage counter.
179 */
180 class Object
181 {
182 public:
183
184 /// The frame index, -1 if unused.
185 Index32 frameIndex_ = Index32(-1);
186
187 /// The usage counter.
188 unsigned int usageCounter_ = 0u;
189
190 /// The actual frame pyramid.
192 };
193
194 /// Definition of a shared pointer to an object.
195 using SharedObject = std::shared_ptr<Object>;
196
197 /// Definition of a static vector holding object pointers.
199
200 public:
201
202 /**
203 * Creates a new manager object.
204 */
206
207 /**
208 * Creates a new pyramid for a given frame index.
209 * @param frameIndex The frame index for which a new pyramid will be created, must be valid
210 * @return The scoped pyramid object
211 */
213
214 /**
215 * Returns an existing pyramid for a given frame index.
216 * @param frameIndex The frame index for which the pyramid will be returned, must be valid
217 * @return The scoped pyramid object, invalid if the pyramid does not exist
218 */
220
221 /**
222 * Returns the latest pyramid.
223 * @return The scoped pyramid object, invalid if no latest pyramid exists
224 */
226
227 /**
228 * Updates the latest pyramid to a given frame index.
229 * The pyramid for the given frame index must exist.
230 * @param frameIndex The frame index of the pyramid to be set as latest, must be valid
231 */
232 void updateLatest(const Index32 frameIndex);
233
234 /**
235 * Unlocks a pyramid for a given frame index.
236 * @param frameIndex The frame index of the pyramid to unlock, must be valid
237 */
238 void unlockPyramid(const Index32 frameIndex);
239
240 /**
241 * Returns the number of currently used pyramids.
242 * @return The number of pyramids
243 */
244 inline size_t size() const;
245
246 /**
247 * Determines the ideal number of pyramid layers for given parameters.
248 * @param width The width of the frame in pixels, with range [1, infinity)
249 * @param height The height of the frame in pixels, with range [1, infinity)
250 * @param patchSize The size of the patch in pixels, with range [1, infinity)
251 * @param maximalTrackingDistance The maximal tracking distance as fraction of the frame resolution, with range (0, infinity)
252 * @param coarseLayerRadius The search radius on the coarsest pyramid layer, with range [1, infinity)
253 * @return The ideal number of pyramid layers
254 */
255 static unsigned int idealPyramidLayers(const unsigned int width, const unsigned int height, const unsigned int patchSize, const float maximalTrackingDistance, const unsigned int coarseLayerRadius);
256
257 protected:
258
259 /// The frame index of the latest pyramid, -1 if invalid.
260 Index32 latestFrameIndex_ = Index32(-1);
261
262 /// The vector of used pyramid objects.
264
265 /// The vector of free pyramid objects.
267
268 /// The lock object.
269 mutable Lock lock_;
270};
271
272inline size_t FramePyramidManager::size() const
273{
274 const ScopedLock scopedLock(lock_);
275
276 return usedObjects_.size();
277}
278
280{
281 *this = std::move(scopedPyramid);
282}
283
288
290{
291 return frameIndex_;
292}
293
295{
296 ocean_assert(pyramid_);
297 return *pyramid_;
298}
299
301{
302 ocean_assert(pyramid_);
303 return pyramid_->finestLayer();
304}
305
307{
308 ocean_assert(pyramid_);
309 return *pyramid_;
310}
311
313{
314 return pyramid_->finestLayer();
315}
316
318{
319 return pyramid_ != nullptr;
320}
321
322inline FramePyramidManager::ScopedPyramid::operator bool() const
323{
324 return isValid();
325}
326
328{
329 ocean_assert(pyramid_);
330 return *pyramid_;
331}
332
334{
335 ocean_assert(pyramid_);
336 return *pyramid_;
337}
338
340{
341 ocean_assert(pyramid_);
342 return pyramid_;
343}
344
346{
347 ocean_assert(pyramid_);
348 return pyramid_;
349}
350
352 owner_(&owner),
353 pyramid_(&pyramid),
354 frameIndex_(frameIndex)
355{
356 // nothing to do here
357}
358
359}
360
361}
362
363}
364
365#endif // META_OCEAN_TRACKING_SLAM_FRAME_PYRAMID_MANAGER_H
This class implements a frame pyramid.
Definition FramePyramid.h:46
This class implements Ocean's image class.
Definition Frame.h:1879
This class implements a recursive lock object.
Definition Lock.h:31
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:147
size_t size() const
Returns the size of this vector.
Definition StaticVector.h:390
This class implements a pyramid object with usage counter.
Definition FramePyramidManager.h:181
CV::FramePyramid framePyramid_
The actual frame pyramid.
Definition FramePyramidManager.h:191
This class implements a scoped pyramid object providing automatic lifetime management.
Definition FramePyramidManager.h:41
ScopedPyramid(const ScopedPyramid &scopedPyramid)=delete
Disabled copy constructor.
const CV::FramePyramid & pyramid() const
Returns a const reference to the pyramid.
Definition FramePyramidManager.h:294
void release()
Explicitly releases the pyramid before the scoped object is destroyed.
const Frame & finestLayer() const
Returns the finest layer of the pyramid.
Definition FramePyramidManager.h:300
bool isValid() const
Returns whether this scoped pyramid is valid.
Definition FramePyramidManager.h:317
ScopedPyramid()=default
Creates an invalid scoped pyramid object.
const CV::FramePyramid & operator*() const
Returns a const reference to the pyramid.
Definition FramePyramidManager.h:327
~ScopedPyramid()
Destructs the scoped pyramid and releases the pyramid.
Definition FramePyramidManager.h:284
const CV::FramePyramid * operator->() const
Returns a const pointer to the pyramid.
Definition FramePyramidManager.h:339
ScopedPyramid & operator=(const ScopedPyramid &scopedPyramid)=delete
Disabled copy assignment operator.
Index32 frameIndex() const
Returns the frame index of this pyramid.
Definition FramePyramidManager.h:289
ScopedPyramid & operator=(ScopedPyramid &&scopedPyramid) noexcept
Move assignment operator.
This class manages a pool of frame pyramids for efficient reuse.
Definition FramePyramidManager.h:33
Objects freeObjects_
The vector of free pyramid objects.
Definition FramePyramidManager.h:266
Lock lock_
The lock object.
Definition FramePyramidManager.h:269
FramePyramidManager()=default
Creates a new manager object.
std::shared_ptr< Object > SharedObject
Definition of a shared pointer to an object.
Definition FramePyramidManager.h:195
size_t size() const
Returns the number of currently used pyramids.
Definition FramePyramidManager.h:272
Objects usedObjects_
The vector of used pyramid objects.
Definition FramePyramidManager.h:263
static unsigned int idealPyramidLayers(const unsigned int width, const unsigned int height, const unsigned int patchSize, const float maximalTrackingDistance, const unsigned int coarseLayerRadius)
Determines the ideal number of pyramid layers for given parameters.
ScopedPyramid newPyramid(const Index32 frameIndex)
Creates a new pyramid for a given frame index.
ScopedPyramid existingPyramid(const Index32 frameIndex)
Returns an existing pyramid for a given frame index.
void unlockPyramid(const Index32 frameIndex)
Unlocks a pyramid for a given frame index.
ScopedPyramid latestPyramid()
Returns the latest pyramid.
void updateLatest(const Index32 frameIndex)
Updates the latest pyramid to a given frame index.
uint32_t Index32
Definition of a 32 bit index value.
Definition Base.h:84
The namespace covering the entire Ocean framework.
Definition Accessor.h:15
AutomaticDifferentiationT< T1, TNumeric1 > operator*(const T2 &left, const AutomaticDifferentiationT< T1, TNumeric1 > &right)
Definition AutomaticDifferentiation.h:523