Ocean
Loading...
Searching...
No Matches
CameraPoses.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_CAMERA_POSES_H
9#define META_OCEAN_TRACKING_SLAM_CAMERA_POSES_H
10
14
15#include "ocean/math/Box3.h"
16
17namespace Ocean
18{
19
20namespace Tracking
21{
22
23namespace SLAM
24{
25
26/**
27 * This class implements a container for camera poses.
28 * The object is thread-safe.
29 * @ingroup trackingslam
30 */
31class OCEAN_TRACKING_SLAM_EXPORT CameraPoses
32{
33 public:
34
35 /**
36 * Definition of an unordered map mapping frame indices to camera poses.
37 */
38 using CameraPoseMap = std::unordered_map<Index32, SharedCameraPose>;
39
40 /// Invalid frame index constant.
41 static constexpr Index32 invalidFrameIndex_ = Index32(-1);
42
43 public:
44
45 /**
46 * Creates a new container object.
47 * Call nextFrame() to activate the container for the first time and set the current frame index to 0.
48 * @see nextFrame().
49 */
51
52 /**
53 * Returns the current frame index.
54 * @return The current frame index, with range [0, infinity)
55 */
56 inline Index32 frameIndex() const;
57
58 /**
59 * Returns the next frame index.
60 * This function can be called even if the container has not been activated yet.
61 * @return The next frame index, which is frameIndex() + 1, or 0 if the container has not been activated
62 */
63 inline Index32 nextFrameIndex() const;
64
65 /**
66 * Returns the index of the previous frame.
67 * @return The index of the previous frame, which is frameIndex() - 1, -1 if no previous frame exists
68 */
69 inline Index32 previousFrameIndex() const;
70
71 /**
72 * Returns the index of the last valid camera pose.
73 * @return The index of the last valid camera pose, with range [0, frameIndex()], -1 if no valid camera pose exists
74 */
75 inline Index32 lastValidPoseFrameIndex() const;
76
77 /**
78 * Returns the indices of all valid camera poses within a specified range.
79 * The function returns the indices of all valid camera poses within the range [max(0, lastFrameIndex - numberFrames + 1), lastFrameIndex].
80 * @param lastFrameIndex The last frame index for which the valid camera poses will be returned, with range [0, infinity)
81 * @param numberFrames The number of frames for which the valid camera poses will be returned, with range [1, infinity)
82 * @return The indices of all valid camera poses
83 */
84 Indices32 validPoseFrameIndices(const Index32 lastFrameIndex, const Index32 numberFrames) const;
85
86 /**
87 * Returns the camera pose for a specific frame index.
88 * @param frameIndex The index of the frame for which the camera pose will be returned, with range [0, infinity)
89 * @return The camera pose for the specified frame index, may be invalid
90 */
91 inline SharedCameraPose pose(const Index32 frameIndex) const;
92
93 /**
94 * Returns whether this container holds a valid camera pose for a specific frame index.
95 * @param frameIndex The index of the frame for which the camera pose will be returned, with range [0, infinity)
96 * @return True, if so
97 */
98 inline bool hasPose(const Index32 frameIndex) const;
99
100 /**
101 * Returns whether this container holds a valid camera pose for a specific frame index.
102 * @param frameIndex The index of the frame for which the camera pose will be returned, with range [0, infinity)
103 * @param cameraPose The resulting camera pose for the specified frame index, must be initialized with nullptr
104 * @return True, if so
105 */
106 bool hasPose(const Index32 frameIndex, SharedCameraPose& cameraPose) const;
107
108 /**
109 * Returns the transformation between camera and world for a specific frame index.
110 * The default camera points towards the negative z-space with y-axis upwards.
111 * @param frameIndex The index of the frame for which the transformation will be returned, with range [0, infinity)
112 * @return The camera-to-world transformation, invalid if no pose exists for the specified frame
113 */
114 inline HomogenousMatrix4 world_T_camera(const Index32 frameIndex) const;
115
116 /**
117 * Returns the transformation between world and flipped camera for a specific frame index.
118 * The default flipped camera points towards the positive z-space with y-axis downwards.
119 * @param frameIndex The index of the frame for which the transformation will be returned, with range [0, infinity)
120 * @return The world-to-flipped-camera transformation, invalid if no pose exists for the specified frame
121 */
122 inline HomogenousMatrix4 flippedCamera_T_world(const Index32 frameIndex) const;
123
124 /**
125 * Sets or updates the camera pose for a specific frame index.
126 * @param frameIndex The index of the frame for which the camera pose will be updated, with range [0, frameIndex()]
127 * @param cameraPose The new camera pose to set, must be valid
128 * @param mapVersion The version of the map associated with this pose
129 */
130 void setPose(const Index32 frameIndex, SharedCameraPose&& cameraPose, const Index32 mapVersion);
131
132 /**
133 * Increases the frame index by one and makes the current camera pose the previous camera pose.
134 * Further, this function needs to be called once before the first camera pose can be accessed.
135 */
136 void nextFrame();
137
138 /**
139 * Removes all poses.
140 * The frame index will be untouched.
141 */
143
144 /**
145 * Returns the 3D bounding box enclosing the positions/translations of all camera poses.
146 * @return The 3D bounding box
147 */
149
150 /**
151 * Returns the number of camera poses stored in this container.
152 * The number of poses may be smaller than the frame index, as the container holds only valid camera poses.
153 * @return The container's size, with range [0, infinity)
154 */
155 inline size_t size() const;
156
157 /**
158 * Returns whether this container holds no valid camera pose.
159 * @return True, if so
160 */
161 inline bool isEmpty() const;
162
163 /**
164 * Returns whether this container has been activated.
165 * The container is activated after the first call of nextFrame().
166 * @return True, if so
167 */
168 inline bool isValid() const;
169
170 protected:
171
172 /**
173 * Returns whether this container has been activated.
174 * The container is activated after the first call of nextFrame().
175 * This is a lock-free version of isValid().
176 * @return True, if so
177 */
178 inline bool isValidLockFree() const;
179
180 protected:
181
182 /// The current frame index, with range [0, infinity), -1 before the first call of nextFrame().
183 Index32 frameIndex_ = Index32(-1);
184
185 /// The frame index of the last valid camera pose, with range [0, frameIndex()], -1 if no valid camera pose exists.
186 Index32 lastValidPoseFrameIndex_ = Index32(-1);
187
188 /// The map mapping frame indices to camera poses.
190
191 /// The mutex of this object.
192 mutable Mutex mutex_;
193};
194
196{
197 const ReadLock readLock(mutex_);
198
199 ocean_assert(isValidLockFree());
200
201 return frameIndex_;
202}
203
205{
206 const ReadLock readLock(mutex_);
207
208 ocean_assert(frameIndex_ != Index32(-1) || frameIndex_ + 1u == 0u); // needs to work even if object is invalid
209
210 return Index32(frameIndex_ + 1u);
211}
212
214{
215 const ReadLock readLock(mutex_);
216
217 ocean_assert(isValidLockFree());
218
219 if (frameIndex_ == Index32(-1) || frameIndex_ == 0u)
220 {
221 return Index32(-1);
222 }
223
224 return Index32(frameIndex_ - 1u);
225}
226
228{
229 const ReadLock readLock(mutex_);
230
231 ocean_assert(isValidLockFree());
232
234}
235
236inline SharedCameraPose CameraPoses::pose(const Index32 frameIndex) const
237{
238 const ReadLock readLock(mutex_);
239
240 ocean_assert(isValidLockFree());
241
242 const CameraPoseMap::const_iterator iCameraPose = cameraPoseMap_.find(frameIndex);
243
244 if (iCameraPose == cameraPoseMap_.cend())
245 {
246 return nullptr;
247 }
248
249 return iCameraPose->second;
250}
251
252inline bool CameraPoses::hasPose(const Index32 frameIndex) const
253{
254 const ReadLock readLock(mutex_);
255
256 ocean_assert(isValidLockFree());
257
258 return cameraPoseMap_.find(frameIndex) != cameraPoseMap_.cend();
259}
260
262{
263 SharedCameraPose cameraPose;
264 if (!hasPose(frameIndex, cameraPose))
265 {
266 return HomogenousMatrix4(false);
267 }
268
269 return cameraPose->world_T_camera();
270}
271
273{
274 SharedCameraPose cameraPose;
275 if (!hasPose(frameIndex, cameraPose))
276 {
277 return HomogenousMatrix4(false);
278 }
279
280 return cameraPose->flippedCamera_T_world();
281}
282
283inline size_t CameraPoses::size() const
284{
285 const ReadLock readLock(mutex_);
286
287 ocean_assert(isValidLockFree());
288
289 return cameraPoseMap_.size();
290}
291
292inline bool CameraPoses::isEmpty() const
293{
294 const ReadLock readLock(mutex_);
295
296 return cameraPoseMap_.empty();
297}
298
299inline bool CameraPoses::isValid() const
300{
301 const ReadLock readLock(mutex_);
302
303 return isValidLockFree();
304}
305
307{
308 return frameIndex_ != Index32(-1);
309}
310
311}
312
313}
314
315}
316
317#endif // META_OCEAN_TRACKING_SLAM_CAMERA_POSES_H
This class implements a container for camera poses.
Definition CameraPoses.h:32
Index32 frameIndex_
The current frame index, with range [0, infinity), -1 before the first call of nextFrame().
Definition CameraPoses.h:183
Mutex mutex_
The mutex of this object.
Definition CameraPoses.h:192
size_t size() const
Returns the number of camera poses stored in this container.
Definition CameraPoses.h:283
bool hasPose(const Index32 frameIndex, SharedCameraPose &cameraPose) const
Returns whether this container holds a valid camera pose for a specific frame index.
bool hasPose(const Index32 frameIndex) const
Returns whether this container holds a valid camera pose for a specific frame index.
Definition CameraPoses.h:252
void removePoses()
Removes all poses.
CameraPoseMap cameraPoseMap_
The map mapping frame indices to camera poses.
Definition CameraPoses.h:189
bool isValidLockFree() const
Returns whether this container has been activated.
Definition CameraPoses.h:306
bool isValid() const
Returns whether this container has been activated.
Definition CameraPoses.h:299
CameraPoses()
Creates a new container object.
Indices32 validPoseFrameIndices(const Index32 lastFrameIndex, const Index32 numberFrames) const
Returns the indices of all valid camera poses within a specified range.
bool isEmpty() const
Returns whether this container holds no valid camera pose.
Definition CameraPoses.h:292
Index32 previousFrameIndex() const
Returns the index of the previous frame.
Definition CameraPoses.h:213
Index32 frameIndex() const
Returns the current frame index.
Definition CameraPoses.h:195
void setPose(const Index32 frameIndex, SharedCameraPose &&cameraPose, const Index32 mapVersion)
Sets or updates the camera pose for a specific frame index.
SharedCameraPose pose(const Index32 frameIndex) const
Returns the camera pose for a specific frame index.
Definition CameraPoses.h:236
Box3 boundingBox() const
Returns the 3D bounding box enclosing the positions/translations of all camera poses.
Index32 lastValidPoseFrameIndex_
The frame index of the last valid camera pose, with range [0, frameIndex()], -1 if no valid camera po...
Definition CameraPoses.h:186
void nextFrame()
Increases the frame index by one and makes the current camera pose the previous camera pose.
Index32 lastValidPoseFrameIndex() const
Returns the index of the last valid camera pose.
Definition CameraPoses.h:227
HomogenousMatrix4 flippedCamera_T_world(const Index32 frameIndex) const
Returns the transformation between world and flipped camera for a specific frame index.
Definition CameraPoses.h:272
std::unordered_map< Index32, SharedCameraPose > CameraPoseMap
Definition of an unordered map mapping frame indices to camera poses.
Definition CameraPoses.h:38
HomogenousMatrix4 world_T_camera(const Index32 frameIndex) const
Returns the transformation between camera and world for a specific frame index.
Definition CameraPoses.h:261
Index32 nextFrameIndex() const
Returns the next frame index.
Definition CameraPoses.h:204
This class implements a scoped read lock for a shared mutex.
Definition Mutex.h:261
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
HomogenousMatrixT4< Scalar > HomogenousMatrix4
Definition of the HomogenousMatrix4 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION flag eit...
Definition HomogenousMatrix4.h:44
std::shared_mutex Mutex
Definition of a mutex supporting read and write locks.
Definition Mutex.h:78
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