8 #ifndef META_OCEAN_MATH_BOX_2_H
9 #define META_OCEAN_MATH_BOX_2_H
22 template <
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;
461 template <
typename T>
469 template <
typename T>
471 lower_(min(first.x(), second.x()), min(first.y(), second.y())),
472 higher_(max(first.x(), second.x()), max(first.y(), second.y()))
477 template <
typename T>
479 lower_(center.x() - width * T(0.5), center.y() - height * T(0.5)),
480 higher_(center.x() + width * T(0.5), center.y() + height * T(0.5))
485 template <
typename T>
488 higher_(topLeft.x() + width, topLeft.y() + height)
493 template <
typename T>
495 lower_(min(left, right), min(top, bottom)),
496 higher_(max(left, right), max(top, bottom))
501 template <
typename T>
506 for (
typename std::vector<
VectorT2<T>>::const_iterator i = points.begin(); i != points.end(); ++i)
530 template <
typename T>
537 ocean_assert(triangle.isValid());
539 *
this += triangle.point0();
540 *
this += triangle.point1();
541 *
this += triangle.point2();
545 template <
typename T>
550 const VectorT2<T>*
const pointsEnd = points + number;
552 while (points != pointsEnd)
578 template <
typename T>
583 *
this += triangle.
point0();
584 *
this += triangle.
point1();
585 *
this += triangle.
point2();
588 template <
typename T>
591 ocean_assert(isValid());
595 template <
typename T>
598 ocean_assert(isValid());
602 template <
typename T>
605 ocean_assert(isValid());
609 template <
typename T>
612 ocean_assert(isValid());
616 template <
typename T>
619 ocean_assert(isValid());
623 template <
typename T>
626 ocean_assert(isValid());
630 template <
typename T>
633 ocean_assert(isValid());
634 return higher_.x() - lower_.x();
637 template <
typename T>
640 ocean_assert(isValid());
641 return higher_.y() - lower_.y();
644 template <
typename T>
647 ocean_assert(isValid());
648 return VectorT2<T>((higher_.x() + lower_.x()) * T(0.5), (higher_.y() + lower_.y()) * T(0.5));
651 template <
typename T>
654 ocean_assert(isValid());
655 return higher_.x() - lower_.x();
658 template <
typename T>
661 ocean_assert(isValid());
662 return higher_.y() - lower_.y();
665 template <
typename T>
668 ocean_assert(isValid());
669 return xDimension() * yDimension();
672 template <
typename T>
675 return point.
x() >= lower_.x() && point.
x() <= higher_.x() && point.
y() >= lower_.y() && point.
y() <= higher_.y();
678 template <
typename T>
681 ocean_assert(epsilon >= 0);
683 return point.
x() + epsilon >= lower_.x()
684 && point.
x() - epsilon <= higher_.x()
685 && point.
y() + epsilon >= lower_.y()
686 && point.
y() - epsilon <= higher_.y();
689 template <
typename T>
692 return isInside(box.
lower()) && isInside(box.
higher());
695 template <
typename T>
698 return !(lower_.x() > box.
higher_.x() || box.
lower_.x() > higher_.x()
699 || lower_.y() > box.
higher_.y() || box.
lower_.y() > higher_.y());
702 template <
typename T>
705 const T xLower = max(lower_.x(), box.
lower_.x());
706 const T yLower = max(lower_.y(), box.
lower_.y());
707 const T xHigher = min(higher_.x(), box.
higher_.x());
708 const T yHigher = min(higher_.y(), box.
higher_.y());
710 if (xLower > xHigher || yLower > yHigher)
718 template <
typename T>
721 ocean_assert(isValid());
729 return BoxT2<T>(roundedLeft, roundedTop, roundedRight, roundedBottom);
732 template <
typename T>
735 ocean_assert(index <= 3u);
749 ocean_assert(index == 0u);
753 template <
typename T>
756 return lower_.x() == higher_.x() && lower_.y() == higher_.y();
759 template <
typename T>
762 return lower_.isEqual(box.
lower_, epsilon) && higher_.isEqual(box.
higher_, epsilon);
765 template <
typename T>
768 return lower_.x() <= higher_.x() && lower_.y() <= higher_.y();
771 template <
typename T>
779 template <
typename T>
784 result += triangle.
point0();
785 result += triangle.
point1();
786 result += triangle.
point2();
791 template <
typename T>
800 template <
typename T>
804 result += signedBorder;
809 template <
typename T>
814 lower_.x() = min(lower_.x(), right.
lower_.x());
815 lower_.y() = min(lower_.y(), right.
lower_.y());
816 higher_.x() = max(higher_.x(), right.
higher_.x());
817 higher_.y() = max(higher_.y(), right.
higher_.y());
822 template <
typename T>
825 *
this += triangle.
point0();
826 *
this += triangle.
point1();
827 *
this += triangle.
point2();
832 template <
typename T>
835 lower_.x() = min(lower_.x(), point.
x());
836 lower_.y() = min(lower_.y(), point.
y());
837 higher_.x() = max(higher_.x(), point.
x());
838 higher_.y() = max(higher_.y(), point.
y());
843 template <
typename T>
846 ocean_assert(isValid());
848 if (signedBorder >= 0)
850 lower_.x() -= signedBorder;
851 lower_.y() -= signedBorder;
853 higher_.x() += signedBorder;
854 higher_.y() += signedBorder;
860 lower_.x() = std::min(lower_.x() - signedBorder, currentCenter.
x());
861 lower_.y() = std::min(lower_.y() - signedBorder, currentCenter.
y());
863 higher_.x() = std::max(currentCenter.
x(), higher_.x() + signedBorder);
864 higher_.y() = std::max(currentCenter.
y(), higher_.y() + signedBorder);
870 template <
typename T>
873 ocean_assert(isValid());
875 return BoxT2<T>(lower_ * factor, higher_ * factor);
878 template <
typename T>
881 ocean_assert(isValid());
883 *
this = *
this * factor;
887 template <
typename T>
890 ocean_assert(isValid() && !transformation.
isSingular());
894 result += transformation *
VectorT2<T>(left(), bottom());
895 result += transformation *
VectorT2<T>(right(), bottom());
896 result += transformation *
VectorT2<T>(right(), top());
897 result += transformation *
VectorT2<T>(left(), top());
902 template <
typename T>
905 *
this = *
this * transformation;
909 template <
typename T>
915 template <
typename T>
918 return !(*
this == right);
921 template <
typename T>
927 template <
typename T>
928 bool 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
930 ocean_assert(isValid());
931 ocean_assert(constraintLeft <= constraintRight && constraintTop <= constraintBottom);
935 intersectionLeft = constraintLeft;
936 intersectionTop = constraintTop;
938 intersectionWidth = (
unsigned int)(constraintRight - constraintLeft) + 1u;
939 intersectionHeight = (
unsigned int)(constraintBottom - constraintTop) + 1u;
944 if (left() > T(constraintRight) || top() > T(constraintBottom) || right() < T(constraintLeft) || bottom() < T(constraintTop))
955 ocean_assert(intersectionLeft <= subRegionRight);
956 ocean_assert(intersectionTop <= subRegionBottom);
958 intersectionWidth = (
unsigned int)(subRegionRight - intersectionLeft) + 1u;
959 intersectionHeight = (
unsigned int)(subRegionBottom - intersectionTop) + 1u;
961 ocean_assert(T(intersectionLeft) <= max(T(constraintLeft), left()));
962 ocean_assert(T(intersectionTop) <= max(T(constraintTop), top()));
963 ocean_assert(T(intersectionLeft) + T(intersectionWidth) >= min(T(constraintRight), right()));
964 ocean_assert(T(intersectionTop) + T(intersectionHeight) >= min(T(constraintBottom), bottom()));
969 template <
typename T>
970 bool BoxT2<T>::box2integer(
const unsigned int maximalWidth,
const unsigned int maximalHeight,
unsigned int& intersectionLeft,
unsigned int& intersectionTop,
unsigned int& intersectionWidth,
unsigned int& intersectionHeight)
const
972 ocean_assert(isValid());
974 if (maximalWidth == 0u || maximalHeight == 0u)
981 intersectionLeft = 0u;
982 intersectionTop = 0u;
983 intersectionWidth = maximalWidth;
984 intersectionHeight = maximalHeight;
987 if (left() >= T(maximalWidth) || top() >= T(maximalHeight) || right() < 0 || bottom() < 0)
992 intersectionLeft =
minmax(0,
int(left()),
int(maximalWidth - 1u));
993 intersectionTop =
minmax(0,
int(top()),
int(maximalHeight - 1u));
994 const unsigned int intersectionRight =
minmax(
int(intersectionLeft),
int(right()),
int(maximalWidth - 1u));
995 const unsigned int intersectionBottom =
minmax(
int(intersectionTop),
int(bottom()),
int(maximalHeight - 1u));
997 intersectionWidth = intersectionRight - intersectionLeft + 1u;
998 intersectionHeight = intersectionBottom - intersectionTop + 1u;
1003 template <
typename T>
1004 bool 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
1006 if (!box2integer(maximalWidth, maximalHeight, intersectionLeft, intersectionTop, intersectionWidth, intersectionHeight))
1011 if (extraBorder == 0u)
1016 ocean_assert(intersectionWidth >= 0u);
1017 ocean_assert(intersectionHeight >= 0u);
1019 unsigned int intersectionRight = intersectionLeft + intersectionWidth - 1u;
1020 unsigned int intersectionBottom = intersectionTop + intersectionHeight - 1u;
1022 ocean_assert(intersectionRight < maximalWidth);
1023 ocean_assert(intersectionTop < maximalHeight);
1025 intersectionRight = min(intersectionRight + extraBorder, maximalWidth - 1u);
1026 intersectionBottom = min(intersectionBottom + extraBorder, maximalHeight - 1u);
1028 intersectionLeft = max(0,
int(intersectionLeft) -
int(extraBorder));
1029 intersectionTop = max(0,
int(intersectionTop) -
int(extraBorder));
1031 intersectionWidth = intersectionRight - intersectionLeft + 1u;
1032 intersectionHeight = intersectionBottom - intersectionTop + 1u;
BoxT2< T > operator*(const SquareMatrixT3< T > &transformation) const
Returns a new bounding box enclosing the transformed corners of this bounding box.
Definition: Box2.h:888
T xDimension() const
Returns the dimension in x axis, which could e.g.
Definition: Box2.h:652
BoxT2< T > & operator+=(const BoxT2< T > &right)
Adds a box to this box.
Definition: Box2.h:810
VectorT2< T > corner(const unsigned int index) const
Returns one of the four corners of this 2D box.
Definition: Box2.h:733
VectorT2< T > lower_
Lower box corner.
Definition: Box2.h:455
BoxT2< T > & operator+=(const VectorT2< T > &point)
Extends this box by another 2D point.
Definition: Box2.h:833
BoxT2< T > & operator+=(const TriangleT2< T > &triangle)
Adds a triangle (the three points of the triangle) to this box.
Definition: Box2.h:823
BoxT2(const TriangleT2< T > &triangle)
Creates a new box object that covers a given triangle.
Definition: Box2.h:579
BoxT2< T > rounded() const
Returns this box with corners rounded to integer locations.
Definition: Box2.h:719
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:760
const VectorT2< T > & higher() const
Returns the higher corner of this box.
Definition: Box2.h:596
bool isValid() const
Returns whether the box holds valid parameters.
Definition: Box2.h:766
BoxT2< T > operator+(const VectorT2< T > &point) const
Extends this box by another 2D point and returns the result.
Definition: Box2.h:792
BoxT2< T > operator+(const BoxT2< T > &right) const
Returns the combined box given by this one and a second box.
Definition: Box2.h:772
T width() const
Returns the width of the box.
Definition: Box2.h:631
BoxT2()
Creates an invalid box object.
Definition: Box2.h:462
bool operator==(const BoxT2< T > &right) const
Returns whether two boxes are identical.
Definition: Box2.h:910
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:486
BoxT2< T > operator+(const T signedBorder) const
Extends this box by adding a border around the box.
Definition: Box2.h:801
T Type
Definition of the used data type.
Definition: Box2.h:74
const T & left() const
Returns the horizontal position of the lower corner of this box.
Definition: Box2.h:603
T yDimension() const
Returns the dimension in x axis, which could e.g.
Definition: Box2.h:659
const T & top() const
Returns the vertical position of the lower corner of this box.
Definition: Box2.h:610
bool operator!=(const BoxT2< T > &right) const
Returns whether two boxes are not identical.
Definition: Box2.h:916
VectorT2< T > higher_
Higher box corner.
Definition: Box2.h:458
const VectorT2< T > & lower() const
Returns the lower corner of this box.
Definition: Box2.h:589
BoxT2< T > & operator+=(const T signedBorder)
Extends this box by adding a border around the box.
Definition: Box2.h:844
BoxT2< T > operator*(const T factor) const
Returns a new bounding box with scaled dimensions.
Definition: Box2.h:871
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:494
bool isInside(const VectorT2< T > &point) const
Returns whether a given point is inside the box.
Definition: Box2.h:673
BoxT2(const std::vector< VectorT2< T >> &points)
Creates a new box object by a set of points.
Definition: Box2.h:502
BoxT2< T > & operator*=(const T factor)
Scales this bounding box with a scalar factor.
Definition: Box2.h:879
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:679
BoxT2< T > operator+(const TriangleT2< T > &triangle) const
Extends this box by a triangle (by the three points of the triangle).
Definition: Box2.h:780
BoxT2< T > & operator*=(const SquareMatrixT3< T > &transformation)
Transforms this bounding box with a transformation matrix.
Definition: Box2.h:903
bool isInside(const BoxT2< T > &box) const
Returns whether a given box is entirely inside this box.
Definition: Box2.h:690
BoxT2< T > intersection(const BoxT2< T > &box) const
Returns the intersection of two boxes.
Definition: Box2.h:703
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:478
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:928
BoxT2(const VectorT2< T > *points, const size_t number)
Creates a new box object by a set of points.
Definition: Box2.h:546
bool intersects(const BoxT2< T > &box) const
Returns whether a given box intersects this one.
Definition: Box2.h:696
T area() const
Returns the area covered by this box.
Definition: Box2.h:666
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:970
bool isPoint() const
Returns whether the box holds exactly one point.
Definition: Box2.h:754
VectorT2< T > center() const
Returns the center of this box.
Definition: Box2.h:645
BoxT2(const VectorT2< T > &first, const VectorT2< T > &second)
Creates a new box object by two opposite corner positions.
Definition: Box2.h:470
BoxT2(const std::vector< TriangleT2< T >> &triangles)
Creates a new box object by a set of triangles.
Definition: Box2.h:531
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:1004
T height() const
Returns the height of the box.
Definition: Box2.h:638
const T & right() const
Returns the horizontal position of the higher corner of this box.
Definition: Box2.h:617
const T & bottom() const
Returns the vertical position of the higher corner of this box.
Definition: Box2.h:624
This class provides basic numeric functionalities.
Definition: Numeric.h:57
This class implements a 3x3 square matrix.
Definition: SquareMatrix3.h:88
bool isSingular() const
Returns whether this matrix is singular (and thus cannot be inverted).
Definition: SquareMatrix3.h:1341
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
This class implements a vector with two elements.
Definition: Vector2.h:96
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
BoxT2< double > BoxD2
Instantiation of the BoxT2 template class using a double precision float data type.
Definition: Box2.h:36
std::vector< Box2 > Boxes2
Definition of a vector holding Box2 objects.
Definition: Box2.h:58
BoxT2< float > BoxF2
Instantiation of the BoxT2 template class using a single precision float data type.
Definition: Box2.h:43
std::vector< BoxT2< T > > BoxesT2
Definition of a typename alias for vectors with BoxT2 objects.
Definition: Box2.h:51
BoxT2< Scalar > Box2
Definition of the Box2 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single or...
Definition: Box2.h:22
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15