8#ifndef META_OCEAN_CV_PIXEL_POSITION_H
9#define META_OCEAN_CV_PIXEL_POSITION_H
27template <
typename T>
class PixelPositionT;
252 inline T
index(
const unsigned int width)
const;
341 explicit inline operator bool()
const;
413 template <
typename TTarget>
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;
514 switch (pixelDirection)
544 ocean_assert(
false &&
"Invalid pixel direction!");
617 return x_ != (uint32_t)(-1) && y_ != (uint32_t)(-1);
623 return x_ != (uint64_t)(-1) && y_ != (uint64_t)(-1);
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));
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)));
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));
678 ocean_assert(isValid());
679 return y_ * T(width) + x_;
685 if (
this == &position)
699 ocean_assert(isValid() && position.
isValid());
707 ocean_assert(isValid() && position.
isValid());
717 ocean_assert(isValid() && position.
isValid());
725 ocean_assert(isValid() && position.
isValid());
735 ocean_assert(isValid());
743 ocean_assert(isValid());
754 ocean_assert(isValid());
755 ocean_assert(factor != T(0));
763 ocean_assert(isValid());
764 ocean_assert(factor != T(0));
775 return (y_ < position.
y_) || (y_ == position.
y_ && x_ < position.
x_);
781 return x_ == position.
x_ && y_ == position.
y_;
787 return !(*
this == position);
799 size_t seed = std::hash<T>{}(pixelPosition.
x());
800 seed ^= std::hash<T>{}(pixelPosition.
y()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
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");
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");
909 ocean_assert(pixelPosition.
isValid());
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());
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));
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))));
977template <
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()));
994 stream <<
"[" << pixelPosition.
x() <<
", " << pixelPosition.
y() <<
"]";
999template <
bool tActive,
typename T>
1002 return messageObject <<
"[" << pixelPosition.x() <<
", " << pixelPosition.y() <<
"]";
1005template <
bool tActive,
typename T>
1006MessageObject<tActive>&
operator<<(MessageObject<tActive>&& messageObject,
const PixelPositionT<T>& pixelPosition)
1008 return messageObject <<
"[" << pixelPosition.x() <<
", " << pixelPosition.y() <<
"]";
This class implements a 2D pixel position with pixel precision.
Definition PixelPosition.h:65
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
static Vectors2 pixelPositions2vectors(const std::vector< PixelPositionT< T > > &pixelPositions)
Converts several pixel positions to 2D vectors.
Definition PixelPosition.h:914
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
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: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
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
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 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
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
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
static constexpr int32_t round32(const T value)
Returns the rounded 32 bit integer value of a given value.
Definition Numeric.h:2064
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: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:34
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: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:992
The namespace covering the entire Ocean framework.
Definition Accessor.h:15