Ocean
Loading...
Searching...
No Matches
Marker.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_CV_CALIBRATION_MARKER_H
9#define META_OCEAN_CV_CALIBRATION_MARKER_H
10
12
13namespace Ocean
14{
15
16namespace CV
17{
18
19namespace Calibration
20{
21
22/**
23 * This class implements the base class for a marker in a calibration board.
24 * @ingroup cvcalibration
25 */
26class OCEAN_CV_CALIBRATION_EXPORT Marker
27{
28 public:
29
30 /**
31 * This class manages all possible layouts of markers.
32 */
33 class OCEAN_CV_CALIBRATION_EXPORT LayoutManager
34 {
35 friend class Marker;
36
37 protected:
38
39 /**
40 * Definition of an invalid marker id.
41 */
42 static constexpr size_t invalidMarkerId_ = size_t(-1);
43
44 /// The number of points in each row and column of the marker.
45 static constexpr size_t numberRowsColumns_ = 5;
46
47 /// The number of points in the marker.
48 static constexpr size_t numberPoints_ = numberRowsColumns_ * numberRowsColumns_;
49
50 public:
51
52 /**
53 * Definition of a marker layout storing one bit for each marker point.
54 * The marker layout is defined for a marker with positive sign (mainly black dot's on white background).<br>
55 * The layout of a negative marker can be derived by inverting the layout of the positive marker.
56 */
57 using Layout = std::array<uint8_t, numberPoints_>;
58
59 /**
60 * Definition of a vector holding marker layouts.
61 */
62 using Layouts = std::vector<Layout>;
63
64 public:
65
66 /**
67 * Returns the sign of point in a marker layout.
68 * @param markerId The id of the marker, with range [0, layouts().size() - 1]
69 * @param markerSign The sign of the marker; True, if the marker is a positive marker (mainly black dot's on white background); False, if the marker is a negative marker (mainly white dot's on black background)
70 * @param orientation The orientation of the marker, must be valid
71 * @param xUnoriented The horizontal position of the point in the marker, will be rotated due to 'orientation', with range [0, numberRowsColumns_ - 1]
72 * @param yUnoriented The vertical position of the point in the marker, will be rotated due to 'orientation', with range [0, numberRowsColumns_ - 1]
73 * @return True, if the point has a positive sign (black dot on white background); False, if the point has a negative sign (white dot on black background)
74 */
75 static bool layoutPointSign(const size_t markerId, const bool markerSign, const CV::PixelDirection& orientation, const size_t xUnoriented, const size_t yUnoriented);
76
77 /**
78 * Returns the sign of point in a marker layout.
79 * @param markerId The id of the marker, with range [0, layouts().size() - 1]
80 * @param markerSign The sign of the marker; True, if the marker is a positive marker (mainly black dot's on white background); False, if the marker is a negative marker (mainly white dot's on black background)
81 * @param orientation The orientation of the marker, must be valid
82 * @param indexInMarkerUnoriented The index of the point in the marker, will be rotated due to 'orientation', with range [0, numberPoints_ - 1]
83 * @return True, if the point has a positive sign (black dot on white background); False, if the point has a negative sign (white dot on black background)
84 */
85 static inline bool layoutPointSign(const size_t markerId, const bool markerSign, const CV::PixelDirection& orientation, const size_t indexInMarkerUnoriented);
86
87 /**
88 * Returns all possible marker layouts.
89 * Each marker layout is unique under rotation.
90 * @return The marker layouts
91 */
92 static const Layouts& layouts();
93
94 public:
95
96 /**
97 * Determines all unique marker layouts so that no layout can be rotated to another layout.
98 * @return The unique marker layouts
99 */
101
102 /**
103 * Returns whether a layout is similar to several other existing layouts.
104 * @param layouts The existing layouts to compare with, at least one
105 * @param layout The layout to check
106 * @param checkSelfSimilarity True, to check whether the layout is similar to itself; False, to skip the self similarity check
107 * @return True, if so
108 */
109 static bool isSimilar(const Layouts& layouts, const Layout& layout, const bool checkSelfSimilarity = true);
110
111 /**
112 * Returns whether two markers can be rotated in a way so that they are identical.
113 * @param layoutA The first layout to check
114 * @param layoutB The second layout to check
115 * @param checkIdentity True, to check whether both layouts are identical without rotation; False, to check whether the layout is rotated by 90, 180, or 270 degree
116 * @return True, if so
117 */
118 static inline bool isSimilar(const Layout& layoutA, const Layout& layoutB, const bool checkIdentity = true);
119
120 /**
121 * Returns whether a second layout is a rotated version of a first layout.
122 * @param layout The first layout
123 * @param rotatedLayout The second layout which can be rotated by a multiple of 90 degree
124 * @param checkIdentity True, to check whether both layouts are identical (no rotation); False, to check whether the layout is rotated by 90, 180, or 270 degree
125 * @return The direction in which the second layout is rotated compared to the first layout, PD_INVALID if the second layout is not a rotated version of the first layout
126 */
127 static CV::PixelDirection isRotated(const Layout& layout, const Layout& rotatedLayout, const bool checkIdentity = true);
128 };
129
130 /**
131 * Definition of a marker type.
132 * A marker type combines the id of a marker with the sign of a marker.
133 */
134 using MarkerType = uint32_t;
135
136 public:
137
138 /**
139 * Default constructor to creator a marker with invalid id and unknown sign.
140 */
141 Marker() = default;
142
143 /**
144 * Creates a new marker with valid marker id but unknown sign.
145 * @param markerId The id of the marker, must be valid
146 */
147 explicit inline Marker(const size_t markerId);
148
149 /**
150 * Creates a new marker with valid marker id and known sign.
151 * @param markerId The id of the marker, must be valid
152 * @param The sign of the marker; True, if the marker is a positive marker (mainly black dot's on white background); False, if the marker is a negative marker (mainly white dot's on black background)
153 */
154 inline Marker(const size_t markerId, const bool sign);
155
156 /**
157 * Returns the id of this marker.
158 * @return The marker's id
159 */
160 inline size_t markerId() const;
161
162 /**
163 * Returns the sign of this marker.
164 * @return The marker's sign; True, if the marker is a positive marker (mainly black dot's on white background); False, if the marker is a negative marker (mainly white dot's on black background)
165 */
166 inline bool sign() const;
167
168 /**
169 * Returns the type of this marker.
170 * @return The marker's type which is a combination of the marker id and the marker's sign
171 */
172 inline MarkerType markerType() const;
173
174 /**
175 * Sets the id of the marker.
176 * The id of the marker describes the marker layout but not the position of the marker within a calibration board.
177 * @param markerId The id of the marker to set
178 */
179 inline void setMarkerId(const size_t markerId);
180
181 /**
182 * Sets the sign of the marker.
183 * @param sign The sign of the marker to set; True, if the marker is a positive marker (mainly black dot's on white background); False, if the marker is a negative marker (mainly white dot's on black background)
184 */
185 inline void setSign(const bool sign);
186
187 /**
188 * Returns whether this marker has a known marker id.
189 * Having a valid marker id means that this marker has been associated with a unique marker layout.
190 * However, as every calibration board may be composed of markers with the same id, the marker id does not yet mean that this marker is associated with a unique position within a calibration board.
191 * @return True, if so
192 * @see hasMarkerCoordinate().
193 */
194 inline bool hasMarkerId() const;
195
196 /**
197 * Returns whether this marker has a known sign.
198 * @return True, if so
199 */
200 inline bool hasSign() const;
201
202 /**
203 * Returns whether this marker is associated with a valid id and a valid sign.
204 * @return True, if so
205 */
206 inline bool isValid() const;
207
208 /**
209 * Returns the number of rows and columns of a marker.
210 * @return The number of rows and columns
211 */
212 static constexpr size_t numberRowsColumns();
213
214 /**
215 * Returns the number of points each marker has.
216 * @return The marker's number of points
217 */
218 static constexpr size_t numberPoints();
219
220 /**
221 * Returns the number of unique marker ids.
222 * @return The number of unique markers, with range [1, infinity)
223 */
224 static inline size_t numberMarkerIds();
225
226 /**
227 * Returns an invalid marker id.
228 * @return Invalid marker id
229 */
230 static constexpr size_t invalidMarkerId();
231
232 private:
233
234 /// The id of the maker, invalid if unknown.
235 size_t markerId_ = invalidMarkerId();
236
237 /// The sign of the marker, 1 for markers with positive sign (mainly black dot's on white background); -1 for markers with negative sign (mainly white dot's on black background), 0 for invalid markers
238 int32_t sign_ = 0;
239};
240
241inline bool Marker::LayoutManager::layoutPointSign(const size_t markerId, const bool markerSign, const CV::PixelDirection& orientation, const size_t indexInMarkerUnoriented)
242{
243 ocean_assert(indexInMarkerUnoriented < numberPoints_);
244
245 const size_t xUnoriented = indexInMarkerUnoriented % numberRowsColumns_;
246 const size_t yUnoriented = indexInMarkerUnoriented / numberRowsColumns_;
247
248 return layoutPointSign(markerId, markerSign, orientation, xUnoriented, yUnoriented);
249}
250
251inline bool Marker::LayoutManager::isSimilar(const Layout& layoutA, const Layout& layoutB, const bool checkIdentity)
252{
253 const CV::PixelDirection rotation = isRotated(layoutA, layoutB, checkIdentity);
254
255 return rotation != CV::PD_INVALID;
256}
257
258inline Marker::Marker(const size_t markerId) :
260{
261 ocean_assert(markerId_ != invalidMarkerId());
262 ocean_assert(markerId_ < numberMarkerIds());
263}
264
265inline Marker::Marker(const size_t markerId, const bool sign) :
266 markerId_(markerId),
267 sign_(sign ? 1 : -1)
268{
269 ocean_assert(markerId_ != invalidMarkerId());
270 ocean_assert(markerId_ < numberMarkerIds());
271}
272
273size_t Marker::markerId() const
274{
275 return markerId_;
276}
277
278inline bool Marker::sign() const
279{
280 ocean_assert(sign_ != 0);
281
282 return sign_ > 0;
283}
284
286{
287 ocean_assert(isValid());
288
289 ocean_assert(markerId_ < size_t(1u << 31u));
290
291 const MarkerType uniqueMarkerType = MarkerType(uint32_t(markerId_) << 1u | (sign() ? 1u : 0u));
292
293 ocean_assert((uniqueMarkerType & 1u) == (sign() ? 1u : 0u));
294 ocean_assert(size_t(uniqueMarkerType >> 1u) == markerId_);
295
296 return uniqueMarkerType;
297}
298
299inline void Marker::setMarkerId(const size_t markerId)
300{
301 ocean_assert(markerId_ == invalidMarkerId());
302
304}
305
306inline void Marker::setSign(const bool sign)
307{
308 ocean_assert(sign_ == 0);
309
310 sign_ = sign ? 1 : -1;
311}
312
313inline bool Marker::hasMarkerId() const
314{
315 return markerId_ != invalidMarkerId();
316}
317
318inline bool Marker::hasSign() const
319{
320 return sign_ != 0;
321}
322
323inline bool Marker::isValid() const
324{
325 return markerId_ != invalidMarkerId() && sign_ != 0;
326}
327
329{
331}
332
333constexpr size_t Marker::numberPoints()
334{
336}
337
339{
340 return LayoutManager::layouts().size();
341}
342
343constexpr size_t Marker::invalidMarkerId()
344{
346}
347
348}
349
350}
351
352}
353
354#endif // META_OCEAN_CV_CALIBRATION_MARKER_H
This class manages all possible layouts of markers.
Definition Marker.h:34
static constexpr size_t invalidMarkerId_
Definition of an invalid marker id.
Definition Marker.h:42
std::vector< Layout > Layouts
Definition of a vector holding marker layouts.
Definition Marker.h:62
std::array< uint8_t, numberPoints_ > Layout
Definition of a marker layout storing one bit for each marker point.
Definition Marker.h:57
static CV::PixelDirection isRotated(const Layout &layout, const Layout &rotatedLayout, const bool checkIdentity=true)
Returns whether a second layout is a rotated version of a first layout.
static constexpr size_t numberRowsColumns_
The number of points in each row and column of the marker.
Definition Marker.h:45
static bool isSimilar(const Layouts &layouts, const Layout &layout, const bool checkSelfSimilarity=true)
Returns whether a layout is similar to several other existing layouts.
static const Layouts & layouts()
Returns all possible marker layouts.
static constexpr size_t numberPoints_
The number of points in the marker.
Definition Marker.h:48
static Layouts determineUniqueLayouts()
Determines all unique marker layouts so that no layout can be rotated to another layout.
static bool layoutPointSign(const size_t markerId, const bool markerSign, const CV::PixelDirection &orientation, const size_t xUnoriented, const size_t yUnoriented)
Returns the sign of point in a marker layout.
This class implements the base class for a marker in a calibration board.
Definition Marker.h:27
bool hasMarkerId() const
Returns whether this marker has a known marker id.
Definition Marker.h:313
bool sign() const
Returns the sign of this marker.
Definition Marker.h:278
size_t markerId() const
Returns the id of this marker.
Definition Marker.h:273
Marker()=default
Default constructor to creator a marker with invalid id and unknown sign.
bool isValid() const
Returns whether this marker is associated with a valid id and a valid sign.
Definition Marker.h:323
void setMarkerId(const size_t markerId)
Sets the id of the marker.
Definition Marker.h:299
uint32_t MarkerType
Definition of a marker type.
Definition Marker.h:134
int32_t sign_
The sign of the marker, 1 for markers with positive sign (mainly black dot's on white background); -1...
Definition Marker.h:238
size_t markerId_
The id of the maker, invalid if unknown.
Definition Marker.h:235
static constexpr size_t numberPoints()
Returns the number of points each marker has.
Definition Marker.h:333
MarkerType markerType() const
Returns the type of this marker.
Definition Marker.h:285
static constexpr size_t numberRowsColumns()
Returns the number of rows and columns of a marker.
Definition Marker.h:328
bool hasSign() const
Returns whether this marker has a known sign.
Definition Marker.h:318
void setSign(const bool sign)
Sets the sign of the marker.
Definition Marker.h:306
static constexpr size_t invalidMarkerId()
Returns an invalid marker id.
Definition Marker.h:343
static size_t numberMarkerIds()
Returns the number of unique marker ids.
Definition Marker.h:338
PixelDirection
Definition of individual directions with pixel accuracy.
Definition CV.h:85
@ PD_INVALID
Definition CV.h:87
The namespace covering the entire Ocean framework.
Definition Accessor.h:15