8 #ifndef META_OCEAN_CV_PIXEL_POSITION_H
9 #define META_OCEAN_CV_PIXEL_POSITION_H
27 template <
typename T>
class PixelPositionT;
252 inline T
index(
const unsigned int width)
const;
341 explicit inline operator bool()
const;
413 template <
typename TTarget>
433 template <
typename T>
441 template <
typename T>
449 template <
typename T>
457 template <
typename T>
463 template <
typename T>
469 template <
typename T>
475 template <
typename T>
481 template <
typename T>
488 template <
typename T>
491 static_assert(
sizeof(T) <=
sizeof(
int),
"Invalid template type T");
493 const int xd = int(x_) - int(position.
x_);
494 const int yd = int(y_) - int(position.
y_);
496 return xd * xd + yd * yd;
499 template <
typename T>
514 switch (pixelDirection)
544 ocean_assert(
false &&
"Invalid pixel direction!");
548 template <
typename T>
554 template <
typename T>
560 template <
typename T>
566 template <
typename T>
572 template <
typename T>
578 template <
typename T>
584 template <
typename T>
590 template <
typename T>
596 template <
typename T>
602 template <
typename T>
608 template <
typename T>
617 return x_ != (uint32_t)(-1) && y_ != (uint32_t)(-1);
623 return x_ != (uint64_t)(-1) && y_ != (uint64_t)(-1);
638 template <
typename T>
641 ocean_assert(isValid());
643 const T differenceX = x_ - position.
x_;
644 const T differenceY = y_ - position.
y_;
646 return (differenceX == T(1) || differenceX == T(0) || differenceX == T(-1))
647 && (differenceY == T(1) || differenceY == T(0) || differenceY == T(-1));
650 template <
typename T>
653 ocean_assert(isValid());
655 const T differenceX = x_ - position.
x_;
656 const T differenceY = y_ - position.
y_;
658 return (differenceX == T(0) && (differenceY == T(1) || differenceY == T(-1)))
659 || (differenceY == T(0) && (differenceX == T(1) || differenceX == T(-1)));
662 template <
typename T>
665 ocean_assert(isValid());
667 const T differenceX = x_ - position.
x_;
668 const T differenceY = y_ - position.
y_;
670 return (differenceX != T(0) || differenceY != T(0))
671 && (differenceX == T(1) || differenceX == T(0) || differenceX == T(-1))
672 && (differenceY == T(1) || differenceY == T(0) || differenceY == T(-1));
675 template <
typename T>
678 ocean_assert(isValid());
679 return y_ * T(width) + x_;
682 template <
typename T>
685 if (
this == &position)
696 template <
typename T>
699 ocean_assert(isValid() && position.
isValid());
704 template <
typename T>
707 ocean_assert(isValid() && position.
isValid());
714 template <
typename T>
717 ocean_assert(isValid() && position.
isValid());
722 template <
typename T>
725 ocean_assert(isValid() && position.
isValid());
732 template <
typename T>
735 ocean_assert(isValid());
740 template <
typename T>
743 ocean_assert(isValid());
751 template <
typename T>
754 ocean_assert(isValid());
755 ocean_assert(factor != T(0));
760 template <
typename T>
763 ocean_assert(isValid());
764 ocean_assert(factor != T(0));
772 template <
typename T>
775 return (y_ < position.
y_) || (y_ == position.
y_ && x_ < position.
x_);
778 template <
typename T>
781 return x_ == position.
x_ && y_ == position.
y_;
784 template <
typename T>
787 return !(*
this == position);
790 template <
typename T>
796 template <
typename T>
799 size_t seed = std::hash<T>{}(pixelPosition.
x());
800 seed ^= std::hash<T>{}(pixelPosition.
y()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
805 template <
typename T>
811 const unsigned int parameter = (0x0000FFFFu & (pixel1.
x() - pixel0.
x())) | ((pixel1.
y() - pixel0.
y()) << 16u);
820 ocean_assert(pixel0.
north() == pixel1);
825 ocean_assert(pixel0.
northWest() == pixel1);
830 ocean_assert(pixel0.
west() == pixel1);
835 ocean_assert(pixel0.
southWest() == pixel1);
840 ocean_assert(pixel0.
south() == pixel1);
845 ocean_assert(pixel0.
southEast() == pixel1);
850 ocean_assert(pixel0.
east() == pixel1);
855 ocean_assert(pixel0.
northEast() == pixel1);
859 ocean_assert(
false &&
"Invalid direction");
863 template <
typename T>
869 const unsigned int parameter = (0x0000FFFFu & (pixel1.
x() - pixel0.
x())) | ((pixel1.
y() - pixel0.
y()) << 16u);
880 ocean_assert(pixel0.
north() == pixel1 || pixel0.
south() == pixel1);
887 ocean_assert(pixel0.
west() == pixel1 || pixel0.
east() == pixel1);
888 return RPD_HORIZONTAL;
902 ocean_assert(
false &&
"Invalid direction");
906 template <
typename T>
909 ocean_assert(pixelPosition.
isValid());
913 template <
typename T>
917 result.reserve(pixelPositions.size());
919 for (
typename std::vector<
PixelPositionT<T>>::const_iterator i = pixelPositions.begin(); i != pixelPositions.end(); ++i)
921 ocean_assert(i->isValid());
946 template <
typename T>
949 std::vector<PixelPositionT<T>> result;
950 result.reserve(values.size());
952 for (Vectors2::const_iterator i = values.begin(); i != values.end(); ++i)
954 result.push_back(vector2pixelPosition(*i));
960 template <
typename T>
963 static_assert(
sizeof(T) <=
sizeof(
int),
"Invalid template type T");
965 std::vector<PixelPositionT<T>> result;
966 result.reserve(values.size());
968 for (Vectors2::const_iterator i = values.begin(); i != values.end(); ++i)
970 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))));
976 template <
typename T>
977 template <
typename TTarget>
980 std::vector<PixelPositionT<TTarget>> result;
981 result.reserve(pixelPositions.size());
983 for (
typename std::vector<
PixelPositionT<T>>::const_iterator i = pixelPositions.begin(); i != pixelPositions.end(); ++i)
985 result.emplace_back(TTarget(i->x()), TTarget(i->y()));
991 template <
typename T>
994 stream <<
"[" << pixelPosition.
x() <<
", " << pixelPosition.
y() <<
"]";
999 template <
bool tActive,
typename T>
1002 return messageObject <<
"[" << pixelPosition.x() <<
", " << pixelPosition.y() <<
"]";
1005 template <
bool tActive,
typename T>
1006 MessageObject<tActive>&
operator<<(MessageObject<tActive>&& messageObject,
const PixelPositionT<T>& pixelPosition)
1008 return messageObject <<
"[" << pixelPosition.x() <<
", " << pixelPosition.y() <<
"]";
T & x()
Returns the horizontal coordinate position of this object.
Definition: PixelPosition.h:464
PixelPositionT< T > west() const
Returns the pixel position west to this position.
Definition: PixelPosition.h:561
PixelPositionT< T > operator/(const T factor) const
Divides this pixel position by a scalar and returns the new resulting position.
Definition: PixelPosition.h:752
PixelPositionT(const T &x, const T &y)
Creates a new coordinate object by two given coordinate values.
Definition: PixelPosition.h:450
bool operator<(const PixelPositionT< T > &position) const
Compares two pixel position objects.
Definition: PixelPosition.h:773
PixelPositionT(const PixelPositionT< T > &position)
Copy constructor.
Definition: PixelPosition.h:442
PixelPositionT< T > & operator*=(const T factor)
Multiplies this pixel position by a scalar.
Definition: PixelPosition.h:741
T x_
Horizontal coordinate value of this object, in pixel.
Definition: PixelPosition.h:419
T index(const unsigned int width) const
Returns the index of this position inside a frame with given width.
Definition: PixelPosition.h:676
PixelPositionT< T > & operator/=(const T factor)
Divides this pixel position by a scalar.
Definition: PixelPosition.h:761
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:961
PixelPositionT< T > & operator+=(const PixelPositionT< T > &position)
Add a second pixel position to this position object.
Definition: PixelPosition.h:705
PixelPositionT< T > southWest() const
Returns the pixel position south west to this position.
Definition: PixelPosition.h:567
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:639
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:697
T & y()
Returns the vertical coordinate position of this object.
Definition: PixelPosition.h:476
static Vector2 pixelPosition2vector(const PixelPositionT< T > &pixelPosition)
Converts a pixel position into a 2D vector.
Definition: PixelPosition.h:907
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:663
T y_
Vertical coordinate value of this object, in pixel.
Definition: PixelPosition.h:422
RoughPixelDirection
Definition of individual rough directions.
Definition: PixelPosition.h:72
@ RPD_DIAGONAL
Vertical direction.
Definition: PixelPosition.h:80
@ RPD_INVALID
Invalid direction.
Definition: PixelPosition.h:74
@ RPD_HORIZONTAL
Horizontal direction.
Definition: PixelPosition.h:78
@ RPD_VERTICAL
Vertical direction.
Definition: PixelPosition.h:76
PixelPositionT< T > north() const
Returns the pixel position north to this position.
Definition: PixelPosition.h:549
Vector2 vector() const
Returns a sub-pixel accuracy vector of this pixel position.
Definition: PixelPosition.h:609
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:500
PixelPositionT< T > operator*(const T factor) const
Multiplies this pixel position by a scalar and returns the new resulting position.
Definition: PixelPosition.h:733
PixelPositionT()
Creates an invalid pixel position object with invalid coordinates.
Definition: PixelPosition.h:434
PixelPositionT< T > east() const
Returns the pixel position east to this position.
Definition: PixelPosition.h:585
T y() const
Returns the vertical coordinate position of this object.
Definition: PixelPosition.h:470
static Vectors2 pixelPositions2vectors(const std::vector< PixelPositionT< T >> &pixelPositions)
Converts several pixel positions to 2D vectors.
Definition: PixelPosition.h:914
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:864
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:651
void setPosition(const T &x, const T &y)
Sets the two coordinate values of this object.
Definition: PixelPosition.h:482
PixelPositionT< T > & operator=(const PixelPositionT< T > &position)
Copy assignment operator.
Definition: PixelPosition.h:683
size_t operator()(const PixelPositionT< T > &pixelPosition) const
Hash function.
Definition: PixelPosition.h:797
PixelPositionT< T > southEast() const
Returns the pixel position south east to this position.
Definition: PixelPosition.h:579
unsigned int sqrDistance(const PixelPositionT< T > &position) const
Returns the square difference between two pixel positions.
Definition: PixelPosition.h:489
bool operator==(const PixelPositionT< T > &position) const
Returns whether two pixel position objects are equal.
Definition: PixelPosition.h:779
static PixelPositionT< T > vector2pixelPosition(const Vector2 &value)
Converts a 2D vector into a pixel position.
PixelPositionT< T > & operator-=(const PixelPositionT< T > &position)
Subtracts a second pixel position from this position object.
Definition: PixelPosition.h:723
T x() const
Returns the horizontal coordinate position of this object.
Definition: PixelPosition.h:458
PixelPositionT< T > northWest() const
Returns the pixel position north west to this position.
Definition: PixelPosition.h:555
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:978
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:715
static std::vector< PixelPositionT< T > > vectors2pixelPositions(const Vectors2 &values)
Converts several 2D vectors into pixel positions.
Definition: PixelPosition.h:947
PixelPositionT< T > south() const
Returns the pixel position south to this position.
Definition: PixelPosition.h:573
PixelPositionT< T > northEast() const
Returns the pixel position north east to this position.
Definition: PixelPosition.h:591
PixelPositionT< T > half() const
Returns this position divided by two.
Definition: PixelPosition.h:597
bool operator!=(const PixelPositionT< T > &position) const
Returns whether two pixel position objects are not equal.
Definition: PixelPosition.h:785
PixelPositionT< T > twice() const
Returns this position multiplied by two.
Definition: PixelPosition.h:603
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:806
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:3250
const T & x() const noexcept
Returns the x value.
Definition: Vector2.h:698
const T & y() const noexcept
Returns the y value.
Definition: Vector2.h:710
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
std::vector< PixelPosition > PixelPositions
Definition of a vector holding pixel positions (with positive coordinate values).
Definition: PixelPosition.h:48
PixelPositionT< int > PixelPositionI
Definition of a PixelPosition object with a data type allowing positive and negative coordinate value...
Definition: PixelPosition.h:41
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< PixelPositionI > PixelPositionsI
Definition of a vector holding pixel positions (with positive and negative coordinate values).
Definition: PixelPosition.h:55
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:128
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:21
MessageObject< tActive > & operator<<(MessageObject< tActive > &&messageObject, const PixelPositionT< T > &pixelPosition)
Definition: PixelPosition.h:1006
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15