Ocean
FrameCollection.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_MEDIA_FRAME_COLLECTION_H
9 #define META_OCEAN_MEDIA_FRAME_COLLECTION_H
10 
11 #include "ocean/media/Media.h"
12 
13 #include "ocean/base/Frame.h"
14 #include "ocean/base/RingMap.h"
15 #include "ocean/base/Timestamp.h"
16 
17 #include "ocean/math/AnyCamera.h"
18 
19 namespace Ocean
20 {
21 
22 namespace Media
23 {
24 
25 /**
26  * This class implements a simple buffer holding several frames combined with their timestamps and optional camera calibrations.
27  * @ingroup media
28  */
29 class OCEAN_MEDIA_EXPORT FrameCollection
30 {
31  protected:
32 
33  /**
34  * Definition of a pair combining a frame with a camera profile.
35  */
36  using FrameCameraPair = std::pair<FrameRef, SharedAnyCamera>;
37 
38  /**
39  * Definition of a ring map mapping timestamps to frame pairs.
40  */
41  using RingMap = RingMapT<Timestamp, FrameCameraPair, true /*thread-safe*/, true /*ordered keys*/>;
42 
43  public:
44 
45  /**
46  * Creates an empty frame collection able to hold one frame.
47  */
48  FrameCollection() = default;
49 
50  /**
51  * Move constructor.
52  * @param frameCollection Frame collection to move
53  */
54  FrameCollection(FrameCollection&& frameCollection) = default;
55 
56  /**
57  * Copy constructor.
58  * @param frameCollection Frame collection to copy
59  */
60  FrameCollection(const FrameCollection& frameCollection) = default;
61 
62  /**
63  * Creates a new frame collection.
64  * @param capacity Number of maximal frames this object can store [1, infinity)
65  */
66  explicit FrameCollection(const size_t capacity);
67 
68  /**
69  * Destructs a frame collection.
70  */
72 
73  /**
74  * Returns the capacity of this frame collection object.
75  * @return The frame collection object's capacity, with range [0, infinity)
76  */
77  inline size_t capacity() const;
78 
79  /**
80  * Returns the most recent frame.
81  * @param anyCamera Optional resulting camera if known; nullptr if not of interest
82  * @return Most recent frame
83  */
84  FrameRef recent(SharedAnyCamera* anyCamera = nullptr) const;
85 
86  /**
87  * Returns the frame with a specific timestamp.
88  * If no frame with the given timestamp exists, the most recent frame will be returned.
89  * @param timestamp The timestamp of the frame to return
90  * @param anyCamera Optional resulting camera if known; nullptr if not of interest
91  * @return Frame with the specific timestamp
92  */
93  FrameRef get(const Timestamp timestamp, SharedAnyCamera* anyCamera = nullptr) const;
94 
95  /**
96  * Returns whether a frame with specific timestamp is currently stored inside the frame collection.
97  * @param timestamp The timestamp to be checked
98  * @return True, if so
99  */
100  bool has(const Timestamp timestamp) const;
101 
102  /**
103  * Sets a new frame and overwrites the oldest frame.
104  * @param frame The frame to set, a copy will be created
105  * @param anyCamera Optional camera profile associated with the given frame, nullptr if unknown
106  * @return Reference to the stored frame, if succeeded
107  */
108  FrameRef set(const Frame& frame, SharedAnyCamera anyCamera = nullptr);
109 
110  /**
111  * Sets a new frame and overwrites the oldest frame.
112  * @param frame The frame to set
113  * @param anyCamera Optional camera profile associated with the given frame, nullptr if unknown
114  * @return Reference to the stored frame, if succeeded
115  */
116  FrameRef set(Frame&& frame, SharedAnyCamera anyCamera = nullptr);
117 
118  /**
119  * Sets or changes the capacity of this frame collection.
120  * In case the capacity is reduced, the oldest frames will be removed
121  * @param capacity The capacity to set, with range [1, infinity)
122  * @return True, if succeeded
123  */
124  bool setCapacity(const size_t capacity);
125 
126  /**
127  * Returns whether the frame collection is empty.
128  * @return True, if so
129  */
130  inline bool isNull() const;
131 
132  /**
133  * Removes all frames from this frame collection.
134  * The capacity will be kept.
135  */
136  void clear();
137 
138  /**
139  * Returns whether the frame collection is not empty.
140  * @return True, if so
141  */
142  explicit inline operator bool() const;
143 
144  /**
145  * Default move operator.
146  * @param frameCollection Frame collection to move
147  */
148  FrameCollection& operator=(FrameCollection&& frameCollection) = default;
149 
150  /**
151  * Default assign operator.
152  * @param frameCollection Frame collection to assign
153  */
154  FrameCollection& operator=(const FrameCollection& frameCollection) = default;
155 
156  protected:
157 
158  /// The ring map mapping timestamps to frame/camera pairs.
159  RingMap ringMap_ = RingMap(1);
160 };
161 
162 inline size_t FrameCollection::capacity() const
163 {
164  return ringMap_.capacity();
165 }
166 
167 inline bool FrameCollection::isNull() const
168 {
169  return ringMap_.isEmpty();
170 }
171 
172 inline FrameCollection::operator bool() const
173 {
174  return !ringMap_.isEmpty();
175 }
176 
177 }
178 
179 }
180 
181 #endif // META_OCEAN_MEDIA_FRAME_COLLECTION_H
This class implements Ocean's image class.
Definition: Frame.h:1760
This class implements a simple buffer holding several frames combined with their timestamps and optio...
Definition: FrameCollection.h:30
~FrameCollection()
Destructs a frame collection.
FrameCollection()=default
Creates an empty frame collection able to hold one frame.
FrameRef set(const Frame &frame, SharedAnyCamera anyCamera=nullptr)
Sets a new frame and overwrites the oldest frame.
RingMap ringMap_
The ring map mapping timestamps to frame/camera pairs.
Definition: FrameCollection.h:159
FrameCollection & operator=(FrameCollection &&frameCollection)=default
Default move operator.
bool has(const Timestamp timestamp) const
Returns whether a frame with specific timestamp is currently stored inside the frame collection.
std::pair< FrameRef, SharedAnyCamera > FrameCameraPair
Definition of a pair combining a frame with a camera profile.
Definition: FrameCollection.h:36
FrameCollection(FrameCollection &&frameCollection)=default
Move constructor.
FrameCollection & operator=(const FrameCollection &frameCollection)=default
Default assign operator.
bool setCapacity(const size_t capacity)
Sets or changes the capacity of this frame collection.
FrameCollection(const FrameCollection &frameCollection)=default
Copy constructor.
void clear()
Removes all frames from this frame collection.
FrameCollection(const size_t capacity)
Creates a new frame collection.
size_t capacity() const
Returns the capacity of this frame collection object.
Definition: FrameCollection.h:162
FrameRef get(const Timestamp timestamp, SharedAnyCamera *anyCamera=nullptr) const
Returns the frame with a specific timestamp.
bool isNull() const
Returns whether the frame collection is empty.
Definition: FrameCollection.h:167
FrameRef recent(SharedAnyCamera *anyCamera=nullptr) const
Returns the most recent frame.
FrameRef set(Frame &&frame, SharedAnyCamera anyCamera=nullptr)
Sets a new frame and overwrites the oldest frame.
This template class implements a object reference with an internal reference counter.
Definition: base/ObjectRef.h:58
size_t capacity() const
Returns the capacity of this storage container.
Definition: RingMap.h:287
bool isEmpty() const
Returns whether this ring map holds at least one element.
Definition: RingMap.h:597
This class implements a timestamp.
Definition: Timestamp.h:36
std::shared_ptr< AnyCamera > SharedAnyCamera
Definition of a shared pointer holding an AnyCamera object with Scalar precision.
Definition: AnyCamera.h:60
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15