Ocean
PixelTriangle.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_ADVANCED_PIXEL_TRIANGLE_H
9 #define META_OCEAN_CV_ADVANCED_PIXEL_TRIANGLE_H
10 
12 
13 #include "ocean/base/Utilities.h"
14 
15 #include "ocean/cv/PixelPosition.h"
16 
17 #include "ocean/math/Numeric.h"
18 #include "ocean/math/Triangle2.h"
19 
20 #include <vector>
21 
22 namespace Ocean
23 {
24 
25 namespace CV
26 {
27 
28 namespace Advanced
29 {
30 
31 // Forward declaration.
32 template <typename T> class PixelTriangleT;
33 
34 /**
35  * Definition of the default PixelTriangle object with a data type allowing only positive coordinate values.
36  * @see PixelTriangleT
37  * @ingroup cvadvanced
38  */
40 
41 /**
42  * Definition of a PixelTriangle object with a data type allowing positive and negative coordinate values.
43  * @see PixelTriangleT
44  * @ingroup cvadvanced
45  */
47 
48 /**
49  * Definition of a vector holding pixel triangles (with positive coordinate values).
50  * @see PixelTriangle
51  * @ingroup cvadvanced
52  */
53 typedef std::vector<PixelTriangle> PixelTriangles;
54 
55 /**
56  * Definition of a vector holding pixel triangles (with positive and negative coordinate values).
57  * @see PixelTriangleI
58  * @ingroup cvadvanced
59  */
60 typedef std::vector<PixelTriangleI> PixelTrianglesI;
61 
62 /**
63  * This class implements a 2D triangle with pixel precision.
64  * @tparam T The data type that is used to store the elements of a pixel coordinate
65  * @see PixelPosition, PixelPositionI
66  * @ingroup cvadvanced
67  */
68 template <typename T>
70 {
71  public:
72 
73  /**
74  * Creates an invalid triangle.
75  */
76  inline PixelTriangleT();
77 
78  /**
79  * Creates a triangle by three corners.
80  * @param point0 First corner point
81  * @param point1 Second corner point
82  * @param point2 Third corner point
83  */
85 
86  /**
87  * Creates a triangle by a given triangle with subpixel accuracy.
88  * The corners of the given triangle are rounded to the next matching pixel within a specified area with upper left corner at (0, 0).
89  * @param triangle Subpixel accuracy triangle to be converted to a pixel accuracy triangle
90  * @param width The width of the specified area in which the resulting triangle will be located, with range [1, infinity)
91  * @param height The height of the specified area in which the resulting triangle will be located, with range [1, infinity)
92  */
93  inline PixelTriangleT(const Triangle2& triangle, const unsigned int width, const unsigned int height);
94 
95  /**
96  * Returns the first corner point of this triangle.
97  * @return First corner point
98  */
99  inline const PixelPositionT<T>& point0() const;
100 
101  /**
102  * Returns the second corner point of this triangle.
103  * @return Second corner point
104  */
105  inline const PixelPositionT<T>& point1() const;
106 
107  /**
108  * Returns the third corner point of this triangle.
109  * @return Third corner point
110  */
111  inline const PixelPositionT<T>& point2() const;
112 
113  /**
114  * Returns the most left (including) position of this triangle.
115  * @return Left position
116  */
117  inline T left() const;
118 
119  /**
120  * Returns the most top (including) position of this triangle.
121  * @return Top position
122  */
123  inline T top() const;
124 
125  /**
126  * Returns the most right (including) position of this triangle.
127  * @return Right position
128  */
129  inline T right() const;
130 
131  /**
132  * Returns the most bottom (including) position of this triangle.
133  * @return Bottom position
134  */
135  inline T bottom() const;
136 
137  /**
138  * Returns whether this triangle holds three valid corner points.
139  * @return True, if so
140  */
141  inline bool isValid() const;
142 
143  /**
144  * Returns whether this triangle holds three valid corner points.
145  * @return True, if so
146  */
147  explicit inline operator bool() const;
148 
149  /**
150  * Shifts the corners of the triangle by a given offset (by adding the offset to each corner) and returns the shifted triangle.
151  * This triangle object must be valid.
152  * @param offset The offset to be added to each corner, must be valid
153  * @return The new shifted triangle
154  */
155  inline PixelTriangleT<T> operator+(const CV::PixelPositionT<T>& offset) const;
156 
157  /**
158  * Shifts the corners of this triangle by a given offset (by adding the offset to each corner).
159  * This triangle object must be valid.
160  * @param offset The offset to be added to each corner, must be valid
161  * @return The reference to this shifted triangle
162  */
163  inline PixelTriangleT<T>& operator+=(const CV::PixelPositionT<T>& offset);
164 
165  /**
166  * Shifts the corners of the triangle by a given offset (by subtracting the offset from each corner) and returns the shifted triangle.
167  * This triangle object must be valid.
168  * @param offset The offset to be subtracted from each corner, must be valid
169  * @return The new shifted triangle
170  */
171  inline PixelTriangleT<T> operator-(const CV::PixelPositionT<T>& offset) const;
172 
173  /**
174  * Shifts the corners of this triangle by a given offset (by subtracting the offset from each corner).
175  * This triangle object must be valid.
176  * @param offset The offset to be subtracted from each corner, must be valid
177  * @return The reference to this shifted triangle
178  */
179  inline PixelTriangleT<T>& operator-=(const CV::PixelPositionT<T>& offset);
180 
181  /**
182  * Returns individual triangle corners.
183  * @param index Index of the corner that is requested, with range [0, 2]
184  * @return Resulting triangle corner
185  */
186  inline const PixelPositionT<T>& operator[](const unsigned int index) const;
187 
188  /**
189  * Returns individual triangle corners.
190  * @param index Index of the corner that is requested, with range [0, 2]
191  * @return Resulting triangle corner
192  */
193  inline PixelPositionT<T>& operator[](const unsigned int index);
194 
195  private:
196 
197  /// Three triangle corners.
199 };
200 
201 template <typename T>
203 {
204  // nothing to do here
205 }
206 
207 template <typename T>
209 {
210  trianglePoints[0] = point0;
211  trianglePoints[1] = point1;
212  trianglePoints[2] = point2;
213 }
214 
215 template <typename T>
216 inline PixelTriangleT<T>::PixelTriangleT(const Triangle2& triangle, const unsigned int width, const unsigned int height)
217 {
218  ocean_assert(triangle.isValid());
219  ocean_assert(width >= 1u && height >= 1u);
220 
221  trianglePoints[0] = PixelPosition(minmax(0, Numeric::round32(triangle.point0().x()), int(width) - 1), minmax(0, Numeric::round32(triangle.point0().y()), int(height) - 1));
222  trianglePoints[1] = PixelPosition(minmax(0, Numeric::round32(triangle.point1().x()), int(width) - 1), minmax(0, Numeric::round32(triangle.point1().y()), int(height) - 1));
223  trianglePoints[2] = PixelPosition(minmax(0, Numeric::round32(triangle.point2().x()), int(width) - 1), minmax(0, Numeric::round32(triangle.point2().y()), int(height) - 1));
224 }
225 
226 template <typename T>
228 {
229  return trianglePoints[0];
230 }
231 
232 template <typename T>
234 {
235  return trianglePoints[1];
236 }
237 
238 template <typename T>
240 {
241  return trianglePoints[2];
242 }
243 
244 template <typename T>
245 inline T PixelTriangleT<T>::left() const
246 {
247  ocean_assert(isValid());
248  return min(trianglePoints[0].x(), min(trianglePoints[1].x(), trianglePoints[2].x()));
249 }
250 
251 template <typename T>
252 inline T PixelTriangleT<T>::top() const
253 {
254  ocean_assert(isValid());
255  return min(trianglePoints[0].y(), min(trianglePoints[1].y(), trianglePoints[2].y()));
256 }
257 
258 template <typename T>
259 inline T PixelTriangleT<T>::right() const
260 {
261  ocean_assert(isValid());
262  return max(trianglePoints[0].x(), max(trianglePoints[1].x(), trianglePoints[2].x()));
263 }
264 
265 template <typename T>
267 {
268  ocean_assert(isValid());
269  return max(trianglePoints[0].y(), max(trianglePoints[1].y(), trianglePoints[2].y()));
270 }
271 
272 template <typename T>
273 inline bool PixelTriangleT<T>::isValid() const
274 {
275  return trianglePoints[0].isValid() && trianglePoints[1].isValid() && trianglePoints[2].isValid();
276 }
277 
278 template <typename T>
279 inline PixelTriangleT<T>::operator bool() const
280 {
281  return isValid();
282 }
283 
284 template <typename T>
286 {
287  ocean_assert(isValid() && offset.isValid());
288  return PixelTriangleT<T>(trianglePoints[0] + offset, trianglePoints[1] + offset, trianglePoints[2] + offset);
289 }
290 
291 template <typename T>
293 {
294  ocean_assert(isValid() && offset.isValid());
295 
296  trianglePoints[0] += offset;
297  trianglePoints[1] += offset;
298  trianglePoints[2] += offset;
299 
300  return *this;
301 }
302 
303 template <typename T>
305 {
306  ocean_assert(isValid() && offset.isValid());
307  return PixelTriangleT<T>(trianglePoints[0] - offset, trianglePoints[1] - offset, trianglePoints[2] - offset);
308 }
309 
310 template <typename T>
312 {
313  ocean_assert(isValid() && offset.isValid());
314 
315  trianglePoints[0] -= offset;
316  trianglePoints[1] -= offset;
317  trianglePoints[2] -= offset;
318 
319  return *this;
320 }
321 
322 template <typename T>
323 inline const PixelPositionT<T>& PixelTriangleT<T>::operator[](const unsigned int index) const
324 {
325  ocean_assert(index <= 2u);
326  return trianglePoints[index];
327 }
328 
329 template <typename T>
330 inline PixelPositionT<T>& PixelTriangleT<T>::operator[](const unsigned int index)
331 {
332  ocean_assert(index <= 2u);
333  return trianglePoints[index];
334 }
335 
336 }
337 
338 }
339 
340 }
341 
342 #endif // META_OCEAN_CV_ADVANCED_PIXEL_TRIANGLE_H
This class implements a 2D triangle with pixel precision.
Definition: PixelTriangle.h:70
T right() const
Returns the most right (including) position of this triangle.
Definition: PixelTriangle.h:259
const PixelPositionT< T > & point1() const
Returns the second corner point of this triangle.
Definition: PixelTriangle.h:233
PixelTriangleT()
Creates an invalid triangle.
Definition: PixelTriangle.h:202
T bottom() const
Returns the most bottom (including) position of this triangle.
Definition: PixelTriangle.h:266
const PixelPositionT< T > & point0() const
Returns the first corner point of this triangle.
Definition: PixelTriangle.h:227
const PixelPositionT< T > & point2() const
Returns the third corner point of this triangle.
Definition: PixelTriangle.h:239
T top() const
Returns the most top (including) position of this triangle.
Definition: PixelTriangle.h:252
const PixelPositionT< T > & operator[](const unsigned int index) const
Returns individual triangle corners.
Definition: PixelTriangle.h:323
bool isValid() const
Returns whether this triangle holds three valid corner points.
Definition: PixelTriangle.h:273
PixelTriangleT< T > & operator+=(const CV::PixelPositionT< T > &offset)
Shifts the corners of this triangle by a given offset (by adding the offset to each corner).
Definition: PixelTriangle.h:292
PixelTriangleT< T > operator+(const CV::PixelPositionT< T > &offset) const
Shifts the corners of the triangle by a given offset (by adding the offset to each corner) and return...
Definition: PixelTriangle.h:285
T left() const
Returns the most left (including) position of this triangle.
Definition: PixelTriangle.h:245
PixelTriangleT< T > & operator-=(const CV::PixelPositionT< T > &offset)
Shifts the corners of this triangle by a given offset (by subtracting the offset from each corner).
Definition: PixelTriangle.h:311
PixelPositionT< T > trianglePoints[3]
Three triangle corners.
Definition: PixelTriangle.h:198
PixelTriangleT< T > operator-(const CV::PixelPositionT< T > &offset) const
Shifts the corners of the triangle by a given offset (by subtracting the offset from each corner) and...
Definition: PixelTriangle.h:304
This class implements a 2D pixel position with pixel precision.
Definition: PixelPosition.h:65
bool isValid() const
Returns whether this pixel position object holds two valid parameters.
static constexpr int32_t round32(const T value)
Returns the rounded 32 bit integer value of a given value.
Definition: Numeric.h:2064
This class implements a 2D triangle with Cartesian coordinates.
Definition: Triangle2.h:81
const VectorT2< T > & point2() const
Returns the third triangle corner.
Definition: Triangle2.h:415
const VectorT2< T > & point0() const
Returns the first triangle corner.
Definition: Triangle2.h:403
const VectorT2< T > & point1() const
Returns the second triangle corner.
Definition: Triangle2.h:409
bool isValid() const
Returns whether this triangle can provide valid barycentric coordinates (for 64 bit floating point va...
Definition: Triangle2.h:806
T minmax(const T &lowerBoundary, const T &value, const T &upperBoundary)
This function fits a given parameter into a specified value range.
Definition: base/Utilities.h:903
PixelPositionT< unsigned int > PixelPosition
Definition of the default PixelPosition object with a data type allowing only positive coordinate val...
Definition: PixelPosition.h:27
std::vector< PixelTriangle > PixelTriangles
Definition of a vector holding pixel triangles (with positive coordinate values).
Definition: PixelTriangle.h:53
std::vector< PixelTriangleI > PixelTrianglesI
Definition of a vector holding pixel triangles (with positive and negative coordinate values).
Definition: PixelTriangle.h:60
PixelTriangleT< unsigned int > PixelTriangle
Definition of the default PixelTriangle object with a data type allowing only positive coordinate val...
Definition: PixelTriangle.h:32
PixelTriangleT< int > PixelTriangleI
Definition of a PixelTriangle object with a data type allowing positive and negative coordinate value...
Definition: PixelTriangle.h:46
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15