8#ifndef META_OCEAN_MATH_BOX_2_H
9#define META_OCEAN_MATH_BOX_2_H
22template <
typename T>
class BoxT2;
168 inline const T&
top()
const;
404 explicit inline operator bool()
const;
420 bool box2integer(
const int constraintLeft,
const int constraintTop,
const int constraintRight,
const int constraintBottom,
int& intersectionLeft,
int& intersectionTop,
unsigned int& intersectionWidth,
unsigned int& intersectionHeight)
const;
435 bool box2integer(
const unsigned int maximalWidth,
const unsigned int maximalHeight,
unsigned int& intersectionLeft,
unsigned int& intersectionTop,
unsigned int& intersectionWidth,
unsigned int& intersectionHeight)
const;
450 bool box2integer(
const unsigned int maximalWidth,
const unsigned int maximalHeight,
const unsigned int extraBorder,
unsigned int& intersectionLeft,
unsigned int& intersectionTop,
unsigned int& intersectionWidth,
unsigned int& intersectionHeight)
const;
463 lower_(min(first.x(), second.x()), min(first.y(), second.y())),
464 higher_(max(first.x(), second.x()), max(first.y(), second.y()))
498 for (
typename std::vector<
VectorT2<T>>::const_iterator i = points.begin(); i != points.end(); ++i)
500 if (i->x() < lower_.x())
507 higher_.x() = i->x();
517 higher_.y() = i->y();
529 ocean_assert(triangle.isValid());
531 *
this += triangle.point0();
532 *
this += triangle.point1();
533 *
this += triangle.point2();
542 const VectorT2<T>*
const pointsEnd = points + number;
544 while (points != pointsEnd)
575 *
this += triangle.
point0();
576 *
this += triangle.
point1();
577 *
this += triangle.
point2();
673 ocean_assert(epsilon >= 0);
675 return point.
x() + epsilon >=
lower_.x()
676 && point.
x() - epsilon <=
higher_.x()
677 && point.
y() + epsilon >=
lower_.y()
678 && point.
y() - epsilon <=
higher_.y();
702 if (xLower > xHigher || yLower > yHigher)
721 return BoxT2<T>(roundedLeft, roundedTop, roundedRight, roundedBottom);
727 ocean_assert(index <= 3u);
744 ocean_assert(index == 0u);
779 result += triangle.
point0();
780 result += triangle.
point1();
781 result += triangle.
point2();
799 result += signedBorder;
807 ocean_assert(
right.isValid());
820 *
this += triangle.
point0();
821 *
this += triangle.
point1();
822 *
this += triangle.
point2();
843 if (signedBorder >= 0)
845 lower_.x() -= signedBorder;
846 lower_.y() -= signedBorder;
855 lower_.x() = std::min(
lower_.x() - signedBorder, currentCenter.
x());
856 lower_.y() = std::min(
lower_.y() - signedBorder, currentCenter.
y());
878 *
this = *
this * factor;
900 *
this = *
this * transformation;
913 return !(*
this ==
right);
923bool BoxT2<T>::box2integer(
const int constraintLeft,
const int constraintTop,
const int constraintRight,
const int constraintBottom,
int& intersectionLeft,
int& intersectionTop,
unsigned int& intersectionWidth,
unsigned int& intersectionHeight)
const
926 ocean_assert(constraintLeft <= constraintRight && constraintTop <= constraintBottom);
930 intersectionLeft = constraintLeft;
931 intersectionTop = constraintTop;
933 intersectionWidth = (
unsigned int)(constraintRight - constraintLeft) + 1u;
934 intersectionHeight = (
unsigned int)(constraintBottom - constraintTop) + 1u;
939 if (
left() > T(constraintRight) ||
top() > T(constraintBottom) ||
right() < T(constraintLeft) ||
bottom() < T(constraintTop))
950 ocean_assert(intersectionLeft <= subRegionRight);
951 ocean_assert(intersectionTop <= subRegionBottom);
953 intersectionWidth = (
unsigned int)(subRegionRight - intersectionLeft) + 1u;
954 intersectionHeight = (
unsigned int)(subRegionBottom - intersectionTop) + 1u;
956 ocean_assert(T(intersectionLeft) <= max(T(constraintLeft),
left()));
957 ocean_assert(T(intersectionTop) <= max(T(constraintTop),
top()));
958 ocean_assert(T(intersectionLeft) + T(intersectionWidth) >= min(T(constraintRight),
right()));
959 ocean_assert(T(intersectionTop) + T(intersectionHeight) >= min(T(constraintBottom),
bottom()));
965bool BoxT2<T>::box2integer(
const unsigned int maximalWidth,
const unsigned int maximalHeight,
unsigned int& intersectionLeft,
unsigned int& intersectionTop,
unsigned int& intersectionWidth,
unsigned int& intersectionHeight)
const
969 if (maximalWidth == 0u || maximalHeight == 0u)
976 intersectionLeft = 0u;
977 intersectionTop = 0u;
978 intersectionWidth = maximalWidth;
979 intersectionHeight = maximalHeight;
984 if (
left() >= T(maximalWidth) ||
top() >= T(maximalHeight) ||
right() < 0 ||
bottom() < 0)
989 intersectionLeft =
minmax(0,
int(
left()),
int(maximalWidth - 1u));
990 intersectionTop =
minmax(0,
int(
top()),
int(maximalHeight - 1u));
991 const unsigned int intersectionRight =
minmax(
int(intersectionLeft),
int(
right()),
int(maximalWidth - 1u));
992 const unsigned int intersectionBottom =
minmax(
int(intersectionTop),
int(
bottom()),
int(maximalHeight - 1u));
994 intersectionWidth = intersectionRight - intersectionLeft + 1u;
995 intersectionHeight = intersectionBottom - intersectionTop + 1u;
1000template <
typename T>
1001bool BoxT2<T>::box2integer(
const unsigned int maximalWidth,
const unsigned int maximalHeight,
const unsigned int extraBorder,
unsigned int& intersectionLeft,
unsigned int& intersectionTop,
unsigned int& intersectionWidth,
unsigned int& intersectionHeight)
const
1003 if (!
box2integer(maximalWidth, maximalHeight, intersectionLeft, intersectionTop, intersectionWidth, intersectionHeight))
1008 if (extraBorder == 0u)
1013 ocean_assert(intersectionWidth >= 0u);
1014 ocean_assert(intersectionHeight >= 0u);
1016 unsigned int intersectionRight = intersectionLeft + intersectionWidth - 1u;
1017 unsigned int intersectionBottom = intersectionTop + intersectionHeight - 1u;
1019 ocean_assert(intersectionRight < maximalWidth);
1020 ocean_assert(intersectionBottom < maximalHeight);
1022 intersectionRight = min(intersectionRight + extraBorder, maximalWidth - 1u);
1023 intersectionBottom = min(intersectionBottom + extraBorder, maximalHeight - 1u);
1025 intersectionLeft = max(0,
int(intersectionLeft) -
int(extraBorder));
1026 intersectionTop = max(0,
int(intersectionTop) -
int(extraBorder));
1028 intersectionWidth = intersectionRight - intersectionLeft + 1u;
1029 intersectionHeight = intersectionBottom - intersectionTop + 1u;
This class implements an axis aligned 2D box object.
Definition Box2.h:68
BoxT2< T > operator*(const SquareMatrixT3< T > &transformation) const
Returns a new bounding box enclosing the transformed corners of this bounding box.
Definition Box2.h:883
T xDimension() const
Returns the dimension in x axis, which could e.g.
Definition Box2.h:644
BoxT2< T > & operator+=(const BoxT2< T > &right)
Adds a box to this box.
Definition Box2.h:805
VectorT2< T > corner(const unsigned int index) const
Returns one of the four corners of this 2D box.
Definition Box2.h:725
VectorT2< Scalar > lower_
Definition Box2.h:455
BoxT2< T > & operator+=(const VectorT2< T > &point)
Extends this box by another 2D point.
Definition Box2.h:828
BoxT2< T > & operator+=(const TriangleT2< T > &triangle)
Adds a triangle (the three points of the triangle) to this box.
Definition Box2.h:818
BoxT2(const TriangleT2< T > &triangle)
Creates a new box object that covers a given triangle.
Definition Box2.h:571
BoxT2< T > rounded() const
Returns this box with corners rounded to integer locations.
Definition Box2.h:711
bool isEqual(const BoxT2< T > &box, const T epsilon=NumericT< T >::eps()) const
Returns whether two box objects are equal up to an epsilon.
Definition Box2.h:755
BoxT2()=default
Creates an invalid box object.
const VectorT2< T > & higher() const
Returns the higher corner of this box.
Definition Box2.h:588
bool isValid() const
Returns whether the box holds valid parameters.
Definition Box2.h:761
BoxT2< T > operator+(const VectorT2< T > &point) const
Extends this box by another 2D point and returns the result.
Definition Box2.h:787
BoxT2< T > operator+(const BoxT2< T > &right) const
Returns the combined box given by this one and a second box.
Definition Box2.h:767
bool operator==(const BoxT2< T > &right) const
Returns whether two boxes are identical.
Definition Box2.h:905
BoxT2(const T width, const T height, const VectorT2< T > &topLeft)
Creates a new box object by the center position and the box's dimension.
Definition Box2.h:478
BoxT2< T > operator+(const T signedBorder) const
Extends this box by adding a border around the box.
Definition Box2.h:796
const Scalar & left() const
T yDimension() const
Returns the dimension in y axis, which could e.g.
Definition Box2.h:651
const Scalar & top() const
bool operator!=(const BoxT2< T > &right) const
Returns whether two boxes are not identical.
Definition Box2.h:911
VectorT2< Scalar > higher_
Definition Box2.h:458
const VectorT2< T > & lower() const
Returns the lower corner of this box.
Definition Box2.h:581
BoxT2< T > & operator+=(const T signedBorder)
Extends this box by adding a border around the box.
Definition Box2.h:839
BoxT2< T > operator*(const T factor) const
Returns a new bounding box with scaled dimensions.
Definition Box2.h:866
BoxT2(const std::vector< TriangleT2< T > > &triangles)
Creates a new box object by a set of triangles.
Definition Box2.h:523
BoxT2(const T left, const T top, const T right, const T bottom)
Creates a new box object by the four borders.
Definition Box2.h:486
bool isInside(const VectorT2< T > &point) const
Returns whether a given point is inside the box.
Definition Box2.h:665
BoxT2< T > & operator*=(const T factor)
Scales this bounding box with a scalar factor.
Definition Box2.h:874
bool isInside(const VectorT2< T > &point, const T &epsilon) const
Returns whether a given point is inside the box including a thin epsilon boundary.
Definition Box2.h:671
BoxT2< T > operator+(const TriangleT2< T > &triangle) const
Extends this box by a triangle (by the three points of the triangle).
Definition Box2.h:775
BoxT2< T > & operator*=(const SquareMatrixT3< T > &transformation)
Transforms this bounding box with a transformation matrix.
Definition Box2.h:898
bool isInside(const BoxT2< T > &box) const
Returns whether a given box is entirely inside this box.
Definition Box2.h:682
BoxT2< T > intersection(const BoxT2< T > &box) const
Returns the intersection of two boxes.
Definition Box2.h:695
BoxT2(const VectorT2< T > ¢er, const T width, const T height)
Creates a new box object by the center position and the box's dimension.
Definition Box2.h:470
bool box2integer(const int constraintLeft, const int constraintTop, const int constraintRight, const int constraintBottom, int &intersectionLeft, int &intersectionTop, unsigned int &intersectionWidth, unsigned int &intersectionHeight) const
Calculates the intersection of this bounding box (with floating point accuracy) and a second bounding...
Definition Box2.h:923
Scalar Type
Definition Box2.h:74
BoxT2(const VectorT2< T > *points, const size_t number)
Creates a new box object by a set of points.
Definition Box2.h:538
bool intersects(const BoxT2< T > &box) const
Returns whether a given box intersects this one.
Definition Box2.h:688
T area() const
Returns the area covered by this box.
Definition Box2.h:658
bool box2integer(const unsigned int maximalWidth, const unsigned int maximalHeight, unsigned int &intersectionLeft, unsigned int &intersectionTop, unsigned int &intersectionWidth, unsigned int &intersectionHeight) const
Calculates the intersection of this bounding box (with floating point accuracy) and a second bounding...
Definition Box2.h:965
bool isPoint() const
Returns whether the box holds exactly one point.
Definition Box2.h:749
VectorT2< Scalar > center() const
BoxT2(const VectorT2< T > &first, const VectorT2< T > &second)
Creates a new box object by two opposite corner positions.
Definition Box2.h:462
bool box2integer(const unsigned int maximalWidth, const unsigned int maximalHeight, const unsigned int extraBorder, unsigned int &intersectionLeft, unsigned int &intersectionTop, unsigned int &intersectionWidth, unsigned int &intersectionHeight) const
Calculates the intersection of this bounding box (with floating point accuracy) and a second bounding...
Definition Box2.h:1001
const Scalar & right() const
const Scalar & bottom() const
BoxT2(const std::vector< VectorT2< T > > &points)
Creates a new box object by a set of points.
Definition Box2.h:494
This class provides basic numeric functionalities.
Definition Numeric.h:57
static constexpr int32_t round32(const T value)
Returns the rounded 32 bit integer value of a given value.
Definition Numeric.h:2073
static constexpr T eps()
Returns a small epsilon.
static T floor(const T value)
Returns the largest integer value that is not greater than the given value.
Definition Numeric.h:2035
This class implements a 3x3 square matrix.
Definition SquareMatrix3.h:89
bool isSingular() const
Returns whether this matrix is singular (and thus cannot be inverted).
Definition SquareMatrix3.h:1342
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:416
const VectorT2< T > & point0() const
Returns the first triangle corner.
Definition Triangle2.h:404
const VectorT2< T > & point1() const
Returns the second triangle corner.
Definition Triangle2.h:410
This class implements a vector with two elements.
Definition Vector2.h:96
const T & x() const noexcept
Returns the x value.
Definition Vector2.h:703
const T & y() const noexcept
Returns the y value.
Definition Vector2.h:715
static VectorT2< T > minValue()
Returns a 2D vector with all elements set to NumericT::minValue().
Definition Vector2.h:905
static VectorT2< T > maxValue()
Returns a 2D vector with all elements set to NumericT::maxValue().
Definition Vector2.h:911
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:973
std::vector< BoxT2< T > > BoxesT2
Definition of a typename alias for vectors with BoxT2 objects.
Definition Box2.h:51
BoxT2< float > BoxF2
Instantiation of the BoxT2 template class using a single precision float data type.
Definition Box2.h:43
std::vector< Box2 > Boxes2
Definition of a vector holding Box2 objects.
Definition Box2.h:58
BoxT2< Scalar > Box2
Definition of the Box2 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single or...
Definition Box2.h:29
BoxT2< double > BoxD2
Instantiation of the BoxT2 template class using a double precision float data type.
Definition Box2.h:36
The namespace covering the entire Ocean framework.
Definition Accessor.h:15