Ocean
Loading...
Searching...
No Matches
PixelPosition.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_PIXEL_POSITION_H
9#define META_OCEAN_CV_PIXEL_POSITION_H
10
11#include "ocean/cv/CV.h"
12
14
15#include "ocean/math/Numeric.h"
16#include "ocean/math/Vector2.h"
17
18namespace Ocean
19{
20
21namespace CV
22{
23
24// Forward declaration.
25template <typename T> class PixelPositionT;
26
27/**
28 * Definition of the default PixelPosition object with a data type allowing only positive coordinate values.
29 * @see PixelPositionT
30 * @ingroup cv
31 */
33
34/**
35 * Definition of a PixelPosition object with a data type allowing positive and negative coordinate values.
36 * @see PixelPositionT
37 * @ingroup cv
38 */
40
41/**
42 * Definition of a vector holding pixel positions (with positive coordinate values).
43 * @see PixelPosition
44 * @ingroup cv
45 */
46using PixelPositions = std::vector<PixelPosition>;
47
48/**
49 * Definition of a vector holding pixel positions (with positive and negative coordinate values).
50 * @see PixelPositionI
51 * @ingroup cv
52 */
53using PixelPositionsI = std::vector<PixelPositionI>;
54
55/**
56 * This class implements a 2D pixel position with pixel precision.
57 * @tparam T The data type that is used to store the elements of a pixel coordinate
58 * @see PixelPosition, PixelPositionI
59 * @ingroup cv
60 */
61template <typename T>
63{
64 public:
65
66 /**
67 * Definition of individual rough directions.
68 */
69 enum RoughPixelDirection : uint32_t
70 {
71 /// Invalid direction.
73 /// Vertical direction.
75 /// Horizontal direction.
77 /// Vertical direction.
79 };
80
81 public:
82
83 /**
84 * Creates an invalid pixel position object with invalid coordinates.
85 */
87
88 /**
89 * Copy constructor.
90 * @param position The position to copy
91 */
92 inline PixelPositionT(const PixelPositionT<T>& position);
93
94 /**
95 * Creates a new coordinate object by two given coordinate values.
96 * @param x Horizontal position in pixel
97 * @param y Vertical position in pixel
98 */
99 inline PixelPositionT(const T& x, const T& y);
100
101 /**
102 * Returns the horizontal coordinate position of this object.
103 * @return Horizontal coordinate position in pixel
104 * @see y().
105 */
106 inline T x() const;
107
108 /**
109 * Returns the vertical coordinate position of this object.
110 * @return Vertical coordinate position in pixel
111 * @see x().
112 */
113 inline T y() const;
114
115 /**
116 * Returns the horizontal coordinate position of this object.
117 * @return Horizontal coordinate position in pixel
118 * @see y().
119 */
120 inline T& x();
121
122 /**
123 * Returns the vertical coordinate position of this object.
124 * @return Vertical coordinate position in pixel
125 * @see x().
126 */
127 inline T& y();
128
129 /**
130 * Sets the two coordinate values of this object.
131 * @param x Horizontal coordinate value to set, in pixel
132 * @param y Vertical coordinate value to be set, in pixel
133 */
134 inline void setPosition(const T& x, const T& y);
135
136 /**
137 * Returns the square difference between two pixel positions.
138 * @param position Second position to determine the difference for
139 * @return Square difference in pixel
140 */
141 inline unsigned int sqrDistance(const PixelPositionT<T>& position) const;
142
143 /**
144 * Returns the position of the pixel neighbor to this position.
145 * @param pixelDirection The direction in which the neighbor will be located, must be valid
146 * @return The position of the neighbor pixel
147 */
149
150 /**
151 * Returns the pixel position north to this position.
152 * @return North pixel position
153 */
154 inline PixelPositionT<T> north() const;
155
156 /**
157 * Returns the pixel position north west to this position.
158 * @return North west pixel position
159 */
161
162 /**
163 * Returns the pixel position west to this position.
164 * @return West pixel position
165 */
166 inline PixelPositionT<T> west() const;
167
168 /**
169 * Returns the pixel position south west to this position.
170 * @return South west pixel position
171 */
173
174 /**
175 * Returns the pixel position south to this position.
176 * @return South pixel position
177 */
178 inline PixelPositionT<T> south() const;
179
180 /**
181 * Returns the pixel position south east to this position.
182 * @return South east pixel position
183 */
185
186 /**
187 * Returns the pixel position east to this position.
188 * @return East pixel position
189 */
190 inline PixelPositionT<T> east() const;
191
192 /**
193 * Returns the pixel position north east to this position.
194 * @return North east pixel position
195 */
197
198 /**
199 * Returns this position divided by two.
200 * @return Half position
201 */
202 inline PixelPositionT<T> half() const;
203
204 /**
205 * Returns this position multiplied by two.
206 * @return Double position
207 */
208 inline PixelPositionT<T> twice() const;
209
210 /**
211 * Returns a sub-pixel accuracy vector of this pixel position.
212 * @return Vector object
213 */
214 inline Vector2 vector() const;
215
216 /**
217 * Returns whether this pixel position object holds two valid parameters.
218 * @return True, if so
219 */
220 inline bool isValid() const;
221
222 /**
223 * Returns whether this pixel position is equal to a second pixel position or is the direct neighbor in an 8-neighborhood.
224 * @param position Second pixel position to check
225 * @return True, if so
226 */
227 inline bool inArea9(const PixelPositionT<T>& position) const;
228
229 /**
230 * Returns whether this pixel position is the direct neighbor to a second pixel position in an 4-neighborhood.
231 * @param position Second pixel position to check
232 * @return True, if so
233 */
234 inline bool isNeighbor4(const PixelPositionT<T>& position) const;
235
236 /**
237 * Returns whether this pixel position is the direct neighbor to a second pixel position in an 8-neighborhood.
238 * @param position Second pixel position to check
239 * @return True, if so
240 */
241 inline bool isNeighbor8(const PixelPositionT<T>& position) const;
242
243 /**
244 * Returns the index of this position inside a frame with given width.
245 * The index is determined according to a frame stored in row aligned order.<br>
246 * The result is determined by y() * width + x().<br>
247 * @param width The width to be used for index determination
248 * @return Resulting index
249 */
250 inline T index(const unsigned int width) const;
251
252 /**
253 * Copy assignment operator.
254 * @param position The position to copy
255 */
257
258 /**
259 * Adds two pixel positions and returns the result as a new pixel position object.
260 * @param position Second pixel position to add
261 * @return Resulting pixel position sum
262 */
263 inline PixelPositionT<T> operator+(const PixelPositionT<T>& position) const;
264
265 /**
266 * Add a second pixel position to this position object.
267 * @param position Second pixel position to add
268 * @return Reference to this changed position object
269 */
271
272 /**
273 * Subtracts two pixel positions and returns the result as a new pixel position object.
274 * @param position Second pixel position to subtract
275 * @return Resulting pixel position
276 */
277 inline PixelPositionT<T> operator-(const PixelPositionT<T>& position) const;
278
279 /**
280 * Subtracts a second pixel position from this position object.
281 * @param position Second pixel position to subtract
282 * @return Reference to this changed position object
283 */
285
286 /**
287 * Multiplies this pixel position by a scalar and returns the new resulting position.
288 * @param factor The multiplication factor, with range (-infinity, infinity)
289 * @return The resulting new position
290 */
291 inline PixelPositionT<T> operator*(const T factor) const;
292
293 /**
294 * Multiplies this pixel position by a scalar.
295 * @param factor The multiplication factor, with range (-infinity, infinity)
296 * @return The reference to this modified position
297 */
298 inline PixelPositionT<T>& operator*=(const T factor);
299
300 /**
301 * Divides this pixel position by a scalar and returns the new resulting position.
302 * @param factor The division factor, with range (-infinity, infinity) \ {0}
303 * @return The resulting new position
304 */
305 inline PixelPositionT<T> operator/(const T factor) const;
306
307 /**
308 * Divides this pixel position by a scalar.
309 * @param factor The division factor, with range (-infinity, infinity) \ {0}
310 * @return The reference to this modified position
311 */
312 inline PixelPositionT<T>& operator/=(const T factor);
313
314 /**
315 * Compares two pixel position objects.
316 * @param position Second pixel position object to be compared
317 * @return True, if this one is lesser than the right one
318 */
319 inline bool operator<(const PixelPositionT<T>& position) const;
320
321 /**
322 * Returns whether two pixel position objects are equal.
323 * @param position Second pixel position object to be compared
324 * @return True, if so
325 */
326 inline bool operator==(const PixelPositionT<T>& position) const;
327
328 /**
329 * Returns whether two pixel position objects are not equal.
330 * @param position Second pixel position object to be compared
331 * @return True, if so
332 */
333 inline bool operator!=(const PixelPositionT<T>& position) const;
334
335 /**
336 * Returns whether this pixel position object holds two valid parameters.
337 * @return True, if so
338 */
339 explicit inline operator bool() const;
340
341 /**
342 * Hash function.
343 * @param pixelPosition The pixel position for which the hash value will be determined
344 * @return The resulting hash value
345 */
346 inline size_t operator()(const PixelPositionT<T>& pixelPosition) const;
347
348 /**
349 * Returns the pixel direction of two successive pixels in a dense contour.
350 * @param pixel0 First pixel
351 * @param pixel1 Following pixel
352 * @return Resulting pixel direction, pixel0 + direction == pixel1
353 */
355
356 /**
357 * Returns the rough pixel direction of two successive pixels in a dense contour.
358 * @param pixel0 First pixel
359 * @param pixel1 Following pixel
360 * @return Resulting rough pixel direction
361 */
363
364 /**
365 * Converts a pixel position into a 2D vector.
366 * @param pixelPosition Pixel position to be converted
367 * @return Resulting 2D vector
368 */
370
371 /**
372 * Converts several pixel positions to 2D vectors.
373 * @param pixelPositions Pixel positions to be converted
374 * @return Resulting 2D vectors
375 */
377
378 /**
379 * Converts a 2D vector into a pixel position.
380 * The pixel positions are rounded.
381 * @param value The value to be converted, with range [0, infinity)x[0, infinity)
382 * @return Resulting pixel position
383 */
384 static inline PixelPositionT<T> vector2pixelPosition(const Vector2& value);
385
386 /**
387 * Converts several 2D vectors into pixel positions.
388 * The pixel positions are rounded.
389 * @param values The values to be converted, with range [0, infinity)x[0, infinity)
390 * @return Resulting pixel positions
391 */
392 static inline std::vector<PixelPositionT<T>> vectors2pixelPositions(const Vectors2& values);
393
394 /**
395 * Converts several 2D vectors into pixel positions.
396 * The pixel positions are rounded and clipped to the given frame dimension.
397 * @param values The values to be converted, with range [0, infinity)x[0, infinity)
398 * @param width The width of the clipping area, in pixel
399 * @param height The height of the clipping area, in pixel
400 * @return Resulting pixel positions
401 */
402 static inline std::vector<PixelPositionT<T>> vectors2pixelPositions(const Vectors2& values, const unsigned int width, const unsigned int height);
403
404 /**
405 * Converts pixels positions with a data type T to pixel positions with another data type.
406 * Beware: This function does not handle out-of-range issues. Thus, ensure that the target data types can covers the locations of the source positions.
407 * @param pixelPositions The pixel positions to convert
408 * @return The resulting converted pixel positions
409 * @tparam TTarget Data type of the target pixel positions
410 */
411 template <typename TTarget>
412 static inline std::vector<PixelPositionT<TTarget>> pixelPositions2pixelPositions(const std::vector<PixelPositionT<T>>& pixelPositions);
413
414 protected:
415
416 /// Horizontal coordinate value of this object, in pixel.
417 T x_;
418
419 /// Vertical coordinate value of this object, in pixel.
420 T y_;
421};
422
423template <>
425 x_(NumericT<int>::minValue()),
426 y_(NumericT<int>::minValue())
427{
428 // nothing to do here
429}
430
431template <typename T>
433 x_(T(-1)),
434 y_(T(-1))
435{
436 // nothing to do here
437}
438
439template <typename T>
441 x_(position.x_),
442 y_(position.y_)
443{
444 // nothing to do here
445}
446
447template <typename T>
448inline PixelPositionT<T>::PixelPositionT(const T& x, const T& y) :
449 x_(x),
450 y_(y)
451{
452 // nothing to do here
453}
454
455template <typename T>
456inline T PixelPositionT<T>::x() const
457{
458 return x_;
459}
460
461template <typename T>
463{
464 return x_;
465}
466
467template <typename T>
468inline T PixelPositionT<T>::y() const
469{
470 return y_;
471}
472
473template <typename T>
475{
476 return y_;
477}
478
479template <typename T>
480inline void PixelPositionT<T>::setPosition(const T& x, const T& y)
481{
482 x_ = x;
483 y_ = y;
484}
485
486template <typename T>
487inline unsigned int PixelPositionT<T>::sqrDistance(const PixelPositionT<T>& position) const
488{
489 static_assert(sizeof(T) <= sizeof(int), "Invalid template type T");
490
491 const int xd = int(x_) - int(position.x_);
492 const int yd = int(y_) - int(position.y_);
493
494 return xd * xd + yd * yd;
495}
496
497template <typename T>
499{
500 /*
501 * X---------------------
502 * | |
503 * | NW N NE |
504 * | |
505 * | W P E |
506 * | |
507 * | SW S SE |
508 * | |
509 * ---------------------
510 */
511
512 switch (pixelDirection)
513 {
514 case PD_NORTH:
515 return CV::PixelPositionT<T>(x_, y_ - 1);
516
517 case PD_NORTH_WEST:
518 return CV::PixelPositionT<T>(x_ - 1, y_ - 1);
519
520 case PD_WEST:
521 return CV::PixelPositionT<T>(x_ - 1, y_);
522
523 case PD_SOUTH_WEST:
524 return CV::PixelPositionT<T>(x_ - 1, y_ + 1);
525
526 case PD_SOUTH:
527 return CV::PixelPositionT<T>(x_, y_ + 1);
528
529 case PD_SOUTH_EAST:
530 return CV::PixelPositionT<T>(x_ + 1, y_ + 1);
531
532 case PD_EAST:
533 return CV::PixelPositionT<T>(x_ + 1, y_);
534
535 case PD_NORTH_EAST:
536 return CV::PixelPositionT<T>(x_ + 1, y_ - 1);
537
538 case PD_INVALID:
539 default:
540 ocean_assert(false && "Invalid pixel direction!");
541 return *this;
542 }
543}
544
545template <typename T>
547{
548 return PixelPositionT<T>(x_, y_ - T(1));
549}
550
551template <typename T>
553{
554 return PixelPositionT<T>(x_ - T(1), y_ - T(1));
555}
556
557template <typename T>
559{
560 return PixelPositionT<T>(x_ - T(1), y_);
561}
562
563template <typename T>
565{
566 return PixelPositionT<T>(x_ - T(1), y_ + T(1));
567}
568
569template <typename T>
571{
572 return PixelPositionT<T>(x_, y_ + T(1));
573}
574
575template <typename T>
577{
578 return PixelPositionT<T>(x_ + T(1), y_ + T(1));
579}
580
581template <typename T>
583{
584 return PixelPositionT<T>(x_ + T(1), y_);
585}
586
587template <typename T>
589{
590 return PixelPositionT<T>(x_ + T(1), y_ - T(1));
591}
592
593template <typename T>
595{
596 return PixelPositionT<T>(x_ / T(2), y_ / T(2));
597}
598
599template <typename T>
601{
602 return PixelPositionT<T>(x_ << 1u, y_ << 1u);
603}
604
605template <typename T>
607{
608 return Vector2(Scalar(x_), Scalar(y_));
609}
610
611template <>
613{
614 return x_ != (uint32_t)(-1) && y_ != (uint32_t)(-1);
615}
616
617template <>
619{
620 return x_ != (uint64_t)(-1) && y_ != (uint64_t)(-1);
621}
622
623template <>
625{
627}
628
629template <>
631{
633}
634
635template <typename T>
636inline bool PixelPositionT<T>::inArea9(const PixelPositionT<T>& position) const
637{
638 ocean_assert(isValid());
639
640 const T differenceX = x_ - position.x_;
641 const T differenceY = y_ - position.y_;
642
643 return (differenceX == T(1) || differenceX == T(0) || differenceX == T(-1))
644 && (differenceY == T(1) || differenceY == T(0) || differenceY == T(-1));
645}
646
647template <typename T>
648inline bool PixelPositionT<T>::isNeighbor4(const PixelPositionT<T>& position) const
649{
650 ocean_assert(isValid());
651
652 const T differenceX = x_ - position.x_;
653 const T differenceY = y_ - position.y_;
654
655 return (differenceX == T(0) && (differenceY == T(1) || differenceY == T(-1)))
656 || (differenceY == T(0) && (differenceX == T(1) || differenceX == T(-1)));
657}
658
659template <typename T>
660inline bool PixelPositionT<T>::isNeighbor8(const PixelPositionT<T>& position) const
661{
662 ocean_assert(isValid());
663
664 const T differenceX = x_ - position.x_;
665 const T differenceY = y_ - position.y_;
666
667 return (differenceX != T(0) || differenceY != T(0))
668 && (differenceX == T(1) || differenceX == T(0) || differenceX == T(-1))
669 && (differenceY == T(1) || differenceY == T(0) || differenceY == T(-1));
670}
671
672template <typename T>
673inline T PixelPositionT<T>::index(const unsigned int width) const
674{
675 ocean_assert(isValid());
676 return y_ * T(width) + x_;
677}
678
679template <typename T>
681{
682 if (this == &position)
683 {
684 return *this;
685 }
686
687 x_ = position.x_;
688 y_ = position.y_;
689
690 return *this;
691}
692
693template <typename T>
695{
696 ocean_assert(isValid() && position.isValid());
697
698 return PixelPositionT<T>(x_ + position.x_, y_ + position.y_);
699}
700
701template <typename T>
703{
704 ocean_assert(isValid() && position.isValid());
705
706 x_ += position.x_;
707 y_ += position.y_;
708 return *this;
709}
710
711template <typename T>
713{
714 ocean_assert(isValid() && position.isValid());
715
716 return PixelPositionT<T>(x_ - position.x_, y_ - position.y_);
717}
718
719template <typename T>
721{
722 ocean_assert(isValid() && position.isValid());
723
724 x_ -= position.x_;
725 y_ -= position.y_;
726 return *this;
727}
728
729template <typename T>
731{
732 ocean_assert(isValid());
733
734 return PixelPositionT<T>(x_ * factor, y_ * factor);
735}
736
737template <typename T>
739{
740 ocean_assert(isValid());
741
742 x_ *= factor;
743 y_ *= factor;
744
745 return *this;
746}
747
748template <typename T>
750{
751 ocean_assert(isValid());
752 ocean_assert(factor != T(0));
753
754 return PixelPositionT<T>(x_ / factor, y_ / factor);
755}
756
757template <typename T>
759{
760 ocean_assert(isValid());
761 ocean_assert(factor != T(0));
762
763 x_ /= factor;
764 y_ /= factor;
765
766 return *this;
767}
768
769template <typename T>
770inline bool PixelPositionT<T>::operator<(const PixelPositionT<T>& position) const
771{
772 return (y_ < position.y_) || (y_ == position.y_ && x_ < position.x_);
773}
774
775template <typename T>
776inline bool PixelPositionT<T>::operator==(const PixelPositionT<T>& position) const
777{
778 return x_ == position.x_ && y_ == position.y_;
779}
780
781template <typename T>
782inline bool PixelPositionT<T>::operator!=(const PixelPositionT<T>& position) const
783{
784 return !(*this == position);
785}
786
787template <typename T>
789{
790 return isValid();
791}
792
793template <typename T>
794inline size_t PixelPositionT<T>::operator()(const PixelPositionT<T>& pixelPosition) const
795{
796 size_t seed = std::hash<T>{}(pixelPosition.x());
797 seed ^= std::hash<T>{}(pixelPosition.y()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
798
799 return seed;
800}
801
802template <typename T>
804{
805 ocean_assert(pixel0.isValid() && pixel1.isValid());
806 ocean_assert(pixel0.isNeighbor8(pixel1));
807
808 const unsigned int parameter = (0x0000FFFFu & (pixel1.x() - pixel0.x())) | ((pixel1.y() - pixel0.y()) << 16u);
809
810 // the low 16 bit may have value 0x0000 (same), 0x0001 (east) or 0xFFFF (west)
811 // the high 16 bit may have value 0x0000 (same), 0x0001 (south) or 0xFFFF (north)
812
813 switch (parameter)
814 {
815 // north
816 case 0xFFFF0000u:
817 ocean_assert(pixel0.north() == pixel1);
818 return PD_NORTH;
819
820 // north west
821 case 0xFFFFFFFFu:
822 ocean_assert(pixel0.northWest() == pixel1);
823 return PD_NORTH_WEST;
824
825 // west
826 case 0x0000FFFFu:
827 ocean_assert(pixel0.west() == pixel1);
828 return PD_WEST;
829
830 // south west
831 case 0x0001FFFFu:
832 ocean_assert(pixel0.southWest() == pixel1);
833 return PD_SOUTH_WEST;
834
835 // south
836 case 0x00010000u:
837 ocean_assert(pixel0.south() == pixel1);
838 return PD_SOUTH;
839
840 // south east
841 case 0x00010001u:
842 ocean_assert(pixel0.southEast() == pixel1);
843 return PD_SOUTH_EAST;
844
845 // east
846 case 0x00000001u:
847 ocean_assert(pixel0.east() == pixel1);
848 return PD_EAST;
849
850 // north east
851 case 0xFFFF0001u:
852 ocean_assert(pixel0.northEast() == pixel1);
853 return PD_NORTH_EAST;
854 }
855
856 ocean_assert(false && "Invalid direction");
857 return PD_INVALID;
858}
859
860template <typename T>
862{
863 ocean_assert(pixel0.isValid() && pixel1.isValid());
864 ocean_assert(pixel0.isNeighbor8(pixel1));
865
866 const unsigned int parameter = (0x0000FFFFu & (pixel1.x() - pixel0.x())) | ((pixel1.y() - pixel0.y()) << 16u);
867
868 // the low 16 bit may have value 0x0000 (same), 0x0001 (east) or 0xFFFF (west)
869 // the high 16 bit may have value 0x0000 (same), 0x0001 (south) or 0xFFFF (north)
870
871 switch (parameter)
872 {
873 // north
874 case 0xFFFF0000u:
875 // south
876 case 0x00010000u:
877 ocean_assert(pixel0.north() == pixel1 || pixel0.south() == pixel1);
878 return RPD_VERTICAL;
879
880 // west
881 case 0x0000FFFFu:
882 // east
883 case 0x00000001u:
884 ocean_assert(pixel0.west() == pixel1 || pixel0.east() == pixel1);
885 return RPD_HORIZONTAL;
886
887 // north west
888 case 0xFFFFFFFFu:
889 // north east
890 case 0xFFFF0001u:
891 // south west
892 case 0x0001FFFFu:
893 // south east
894 case 0x00010001u:
895 ocean_assert(pixel0.northWest() == pixel1 || pixel0.northEast() == pixel1 || pixel0.southWest() == pixel1 || pixel0.southEast() == pixel1);
896 return RPD_DIAGONAL;
897 }
898
899 ocean_assert(false && "Invalid direction");
900 return RPD_INVALID;
901}
902
903template <typename T>
905{
906 ocean_assert(pixelPosition.isValid());
907 return Vector2(Scalar(pixelPosition.x()), Scalar(pixelPosition.y()));
908}
909
910template <typename T>
912{
913 Vectors2 result;
914 result.reserve(pixelPositions.size());
915
916 for (typename std::vector<PixelPositionT<T>>::const_iterator i = pixelPositions.begin(); i != pixelPositions.end(); ++i)
917 {
918 ocean_assert(i->isValid());
919 result.push_back(Vector2(Scalar(i->x()), Scalar(i->y())));
920 }
921
922 return result;
923}
924
925template <>
927{
928 ocean_assert(value.x() >= Scalar(0) && value.x() < Scalar(NumericT<unsigned int>::maxValue()));
929 ocean_assert(value.y() >= Scalar(0) && value.y() < Scalar(NumericT<unsigned int>::maxValue()));
930
931 return PixelPositionT<unsigned int>((unsigned int)(value.x() + Scalar(0.5)), (unsigned int)(value.y() + Scalar(0.5)));
932}
933
934template <>
936{
937 ocean_assert(value.x() > Scalar(NumericT<int>::minValue()) && value.x() <= Scalar(NumericT<int>::maxValue()));
938 ocean_assert(value.y() > Scalar(NumericT<int>::minValue()) && value.y() <= Scalar(NumericT<int>::maxValue()));
939
940 return PixelPositionT<int>(Numeric::round32(value.x()), Numeric::round32(value.y()));
941}
942
943template <typename T>
944inline std::vector<PixelPositionT<T>> PixelPositionT<T>::vectors2pixelPositions(const Vectors2& values)
945{
946 std::vector<PixelPositionT<T>> result;
947 result.reserve(values.size());
948
949 for (Vectors2::const_iterator i = values.begin(); i != values.end(); ++i)
950 {
951 result.push_back(vector2pixelPosition(*i));
952 }
953
954 return result;
955}
956
957template <typename T>
958inline std::vector<PixelPositionT<T>> PixelPositionT<T>::vectors2pixelPositions(const Vectors2& values, const unsigned int width, const unsigned int height)
959{
960 static_assert(sizeof(T) <= sizeof(int), "Invalid template type T");
961
962 std::vector<PixelPositionT<T>> result;
963 result.reserve(values.size());
964
965 for (Vectors2::const_iterator i = values.begin(); i != values.end(); ++i)
966 {
967 result.emplace_back(T(minmax(0, int(i->x() + Scalar(0.5)), int(width - 1))), T(minmax(0, int(i->y() + Scalar(0.5)), int(height - 1u))));
968 }
969
970 return result;
971}
972
973template <typename T>
974template <typename TTarget>
975inline std::vector<PixelPositionT<TTarget>> PixelPositionT<T>::pixelPositions2pixelPositions(const std::vector<PixelPositionT<T>>& pixelPositions)
976{
977 std::vector<PixelPositionT<TTarget>> result;
978 result.reserve(pixelPositions.size());
979
980 for (typename std::vector<PixelPositionT<T>>::const_iterator i = pixelPositions.begin(); i != pixelPositions.end(); ++i)
981 {
982 result.emplace_back(TTarget(i->x()), TTarget(i->y()));
983 }
984
985 return result;
986}
987
988template <typename T>
989std::ostream& operator<<(std::ostream& stream, const PixelPositionT<T>& pixelPosition)
990{
991 stream << "[" << pixelPosition.x() << ", " << pixelPosition.y() << "]";
992
993 return stream;
994}
995
996template <bool tActive, typename T>
997MessageObject<tActive>& operator<<(MessageObject<tActive>& messageObject, const PixelPositionT<T>& pixelPosition)
998{
999 return messageObject << "[" << pixelPosition.x() << ", " << pixelPosition.y() << "]";
1000}
1001
1002template <bool tActive, typename T>
1003MessageObject<tActive>& operator<<(MessageObject<tActive>&& messageObject, const PixelPositionT<T>& pixelPosition)
1004{
1005 return messageObject << "[" << pixelPosition.x() << ", " << pixelPosition.y() << "]";
1006}
1007
1008}
1009
1010}
1011
1012#endif // META_OCEAN_CV_PIXEL_POSITION_H
T & x()
Returns the horizontal coordinate position of this object.
Definition PixelPosition.h:462
PixelPositionT< T > west() const
Returns the pixel position west to this position.
Definition PixelPosition.h:558
PixelPositionT< T > operator/(const T factor) const
Divides this pixel position by a scalar and returns the new resulting position.
Definition PixelPosition.h:749
static Vectors2 pixelPositions2vectors(const std::vector< PixelPositionT< T > > &pixelPositions)
Converts several pixel positions to 2D vectors.
Definition PixelPosition.h:911
PixelPositionT(const T &x, const T &y)
Creates a new coordinate object by two given coordinate values.
Definition PixelPosition.h:448
bool operator<(const PixelPositionT< T > &position) const
Compares two pixel position objects.
Definition PixelPosition.h:770
PixelPositionT(const PixelPositionT< T > &position)
Copy constructor.
Definition PixelPosition.h:440
PixelPositionT< T > & operator*=(const T factor)
Multiplies this pixel position by a scalar.
Definition PixelPosition.h:738
static PixelPositionT< T > vector2pixelPosition(const Vector2 &value)
Converts a 2D vector into a pixel position.
T x_
Horizontal coordinate value of this object, in pixel.
Definition PixelPosition.h:417
T index(const unsigned int width) const
Returns the index of this position inside a frame with given width.
Definition PixelPosition.h:673
PixelPositionT< T > & operator/=(const T factor)
Divides this pixel position by a scalar.
Definition PixelPosition.h:758
static std::vector< PixelPositionT< T > > vectors2pixelPositions(const Vectors2 &values, const unsigned int width, const unsigned int height)
Converts several 2D vectors into pixel positions.
Definition PixelPosition.h:958
PixelPositionT< T > & operator+=(const PixelPositionT< T > &position)
Add a second pixel position to this position object.
Definition PixelPosition.h:702
PixelPositionT< T > southWest() const
Returns the pixel position south west to this position.
Definition PixelPosition.h:564
bool inArea9(const PixelPositionT< T > &position) const
Returns whether this pixel position is equal to a second pixel position or is the direct neighbor in ...
Definition PixelPosition.h:636
PixelPositionT< T > operator+(const PixelPositionT< T > &position) const
Adds two pixel positions and returns the result as a new pixel position object.
Definition PixelPosition.h:694
T & y()
Returns the vertical coordinate position of this object.
Definition PixelPosition.h:474
static Vector2 pixelPosition2vector(const PixelPositionT< T > &pixelPosition)
Converts a pixel position into a 2D vector.
Definition PixelPosition.h:904
bool isNeighbor8(const PixelPositionT< T > &position) const
Returns whether this pixel position is the direct neighbor to a second pixel position in an 8-neighbo...
Definition PixelPosition.h:660
T y_
Vertical coordinate value of this object, in pixel.
Definition PixelPosition.h:420
static std::vector< PixelPositionT< TTarget > > pixelPositions2pixelPositions(const std::vector< PixelPositionT< T > > &pixelPositions)
Converts pixels positions with a data type T to pixel positions with another data type.
Definition PixelPosition.h:975
PixelPositionT< T > north() const
Returns the pixel position north to this position.
Definition PixelPosition.h:546
Vector2 vector() const
Returns a sub-pixel accuracy vector of this pixel position.
Definition PixelPosition.h:606
bool isValid() const
Returns whether this pixel position object holds two valid parameters.
CV::PixelPositionT< T > neighbor(const CV::PixelDirection pixelDirection) const
Returns the position of the pixel neighbor to this position.
Definition PixelPosition.h:498
PixelPositionT< T > operator*(const T factor) const
Multiplies this pixel position by a scalar and returns the new resulting position.
Definition PixelPosition.h:730
PixelPositionT()
Creates an invalid pixel position object with invalid coordinates.
Definition PixelPosition.h:432
PixelPositionT< T > east() const
Returns the pixel position east to this position.
Definition PixelPosition.h:582
T y() const
Returns the vertical coordinate position of this object.
Definition PixelPosition.h:468
static RoughPixelDirection roughDirection(const PixelPositionT< T > &pixel0, const PixelPositionT< T > &pixel1)
Returns the rough pixel direction of two successive pixels in a dense contour.
Definition PixelPosition.h:861
bool isNeighbor4(const PixelPositionT< T > &position) const
Returns whether this pixel position is the direct neighbor to a second pixel position in an 4-neighbo...
Definition PixelPosition.h:648
void setPosition(const T &x, const T &y)
Sets the two coordinate values of this object.
Definition PixelPosition.h:480
PixelPositionT< T > & operator=(const PixelPositionT< T > &position)
Copy assignment operator.
Definition PixelPosition.h:680
size_t operator()(const PixelPositionT< T > &pixelPosition) const
Hash function.
Definition PixelPosition.h:794
PixelPositionT< T > southEast() const
Returns the pixel position south east to this position.
Definition PixelPosition.h:576
unsigned int sqrDistance(const PixelPositionT< T > &position) const
Returns the square difference between two pixel positions.
Definition PixelPosition.h:487
bool operator==(const PixelPositionT< T > &position) const
Returns whether two pixel position objects are equal.
Definition PixelPosition.h:776
PixelPositionT< T > & operator-=(const PixelPositionT< T > &position)
Subtracts a second pixel position from this position object.
Definition PixelPosition.h:720
T x() const
Returns the horizontal coordinate position of this object.
Definition PixelPosition.h:456
PixelPositionT< T > northWest() const
Returns the pixel position north west to this position.
Definition PixelPosition.h:552
RoughPixelDirection
Definition of individual rough directions.
Definition PixelPosition.h:70
@ RPD_DIAGONAL
Vertical direction.
Definition PixelPosition.h:78
@ RPD_INVALID
Invalid direction.
Definition PixelPosition.h:72
@ RPD_HORIZONTAL
Horizontal direction.
Definition PixelPosition.h:76
@ RPD_VERTICAL
Vertical direction.
Definition PixelPosition.h:74
PixelPositionT< T > operator-(const PixelPositionT< T > &position) const
Subtracts two pixel positions and returns the result as a new pixel position object.
Definition PixelPosition.h:712
static std::vector< PixelPositionT< T > > vectors2pixelPositions(const Vectors2 &values)
Converts several 2D vectors into pixel positions.
Definition PixelPosition.h:944
PixelPositionT< T > south() const
Returns the pixel position south to this position.
Definition PixelPosition.h:570
PixelPositionT< T > northEast() const
Returns the pixel position north east to this position.
Definition PixelPosition.h:588
PixelPositionT< T > half() const
Returns this position divided by two.
Definition PixelPosition.h:594
bool operator!=(const PixelPositionT< T > &position) const
Returns whether two pixel position objects are not equal.
Definition PixelPosition.h:782
PixelPositionT< T > twice() const
Returns this position multiplied by two.
Definition PixelPosition.h:600
static PixelDirection direction(const PixelPositionT< T > &pixel0, const PixelPositionT< T > &pixel1)
Returns the pixel direction of two successive pixels in a dense contour.
Definition PixelPosition.h:803
Messenger object, one object for each message.
Definition Messenger.h:427
This class provides basic numeric functionalities.
Definition Numeric.h:57
static constexpr T minValue()
Returns the min scalar value.
Definition Numeric.h:3253
static constexpr int32_t round32(const T value)
Returns the rounded 32 bit integer value of a given value.
Definition Numeric.h:2067
const T & x() const noexcept
Returns the x value.
Definition Vector2.h:710
const T & y() const noexcept
Returns the y value.
Definition Vector2.h:722
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:927
std::vector< PixelPositionI > PixelPositionsI
Definition of a vector holding pixel positions (with positive and negative coordinate values).
Definition PixelPosition.h:53
std::vector< PixelPosition > PixelPositions
Definition of a vector holding pixel positions (with positive coordinate values).
Definition PixelPosition.h:46
PixelDirection
Definition of individual directions with pixel accuracy.
Definition CV.h:85
@ PD_WEST
Definition CV.h:93
@ PD_NORTH_EAST
Definition CV.h:103
@ PD_NORTH_WEST
Definition CV.h:91
@ PD_EAST
Definition CV.h:101
@ PD_SOUTH_WEST
Definition CV.h:95
@ PD_SOUTH
Definition CV.h:97
@ PD_SOUTH_EAST
Definition CV.h:99
@ PD_INVALID
Definition CV.h:87
@ PD_NORTH
Definition CV.h:89
float Scalar
Definition of a scalar type.
Definition Math.h:129
std::vector< Vector2 > Vectors2
Definition of a vector holding Vector2 objects.
Definition Vector2.h:64
VectorT2< Scalar > Vector2
Definition of a 2D vector.
Definition Vector2.h:28
std::ostream & operator<<(std::ostream &stream, const PixelPositionT< T > &pixelPosition)
Definition PixelPosition.h:989
The namespace covering the entire Ocean framework.
Definition Accessor.h:15