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()))
471 lower_(center.x() - width * T(0.5), center.y() - height * T(0.5)),
472 higher_(center.x() + width * T(0.5), center.y() + height * T(0.5))
480 higher_(topLeft.x() + width, topLeft.y() + height)
487 lower_(min(left, right), min(top, bottom)),
488 higher_(max(left, right), max(top, bottom))
498 for (
typename std::vector<
VectorT2<T>>::const_iterator i = points.begin(); i != points.end(); ++i)
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();
583 ocean_assert(isValid());
590 ocean_assert(isValid());
597 ocean_assert(isValid());
604 ocean_assert(isValid());
611 ocean_assert(isValid());
618 ocean_assert(isValid());
625 ocean_assert(isValid());
626 return higher_.x() - lower_.x();
632 ocean_assert(isValid());
633 return higher_.y() - lower_.y();
639 ocean_assert(isValid());
640 return VectorT2<T>((higher_.x() + lower_.x()) * T(0.5), (higher_.y() + lower_.y()) * T(0.5));
646 ocean_assert(isValid());
647 return higher_.x() - lower_.x();
653 ocean_assert(isValid());
654 return higher_.y() - lower_.y();
660 ocean_assert(isValid());
661 return xDimension() * yDimension();
667 return point.
x() >= lower_.x() && point.
x() <= higher_.x() && point.
y() >= lower_.y() && point.
y() <= higher_.y();
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();
684 return isInside(box.
lower()) && isInside(box.
higher());
690 return !(lower_.x() > box.
higher_.x() || box.
lower_.x() > higher_.x()
691 || lower_.y() > box.
higher_.y() || box.
lower_.y() > higher_.y());
697 const T xLower = max(lower_.x(), box.
lower_.x());
698 const T yLower = max(lower_.y(), box.
lower_.y());
699 const T xHigher = min(higher_.x(), box.
higher_.x());
700 const T yHigher = min(higher_.y(), box.
higher_.y());
702 if (xLower > xHigher || yLower > yHigher)
713 ocean_assert(isValid());
721 return BoxT2<T>(roundedLeft, roundedTop, roundedRight, roundedBottom);
727 ocean_assert(index <= 3u);
744 ocean_assert(index == 0u);
751 return lower_.x() == higher_.x() && lower_.y() == higher_.y();
757 return lower_.isEqual(box.
lower_, epsilon) && higher_.isEqual(box.
higher_, epsilon);
763 return lower_.x() <= higher_.x() && lower_.y() <= higher_.y();
779 result += triangle.
point0();
780 result += triangle.
point1();
781 result += triangle.
point2();
799 result += signedBorder;
809 lower_.x() = min(lower_.x(), right.
lower_.x());
810 lower_.y() = min(lower_.y(), right.
lower_.y());
811 higher_.x() = max(higher_.x(), right.
higher_.x());
812 higher_.y() = max(higher_.y(), right.
higher_.y());
820 *
this += triangle.
point0();
821 *
this += triangle.
point1();
822 *
this += triangle.
point2();
830 lower_.x() = min(lower_.x(), point.
x());
831 lower_.y() = min(lower_.y(), point.
y());
832 higher_.x() = max(higher_.x(), point.
x());
833 higher_.y() = max(higher_.y(), point.
y());
841 ocean_assert(isValid());
843 if (signedBorder >= 0)
845 lower_.x() -= signedBorder;
846 lower_.y() -= signedBorder;
848 higher_.x() += signedBorder;
849 higher_.y() += signedBorder;
855 lower_.x() = std::min(lower_.x() - signedBorder, currentCenter.
x());
856 lower_.y() = std::min(lower_.y() - signedBorder, currentCenter.
y());
858 higher_.x() = std::max(currentCenter.
x(), higher_.x() + signedBorder);
859 higher_.y() = std::max(currentCenter.
y(), higher_.y() + signedBorder);
868 ocean_assert(isValid());
870 return BoxT2<T>(lower_ * factor, higher_ * factor);
876 ocean_assert(isValid());
878 *
this = *
this * factor;
885 ocean_assert(isValid() && !transformation.
isSingular());
889 result += transformation *
VectorT2<T>(left(), bottom());
890 result += transformation *
VectorT2<T>(right(), bottom());
891 result += transformation *
VectorT2<T>(right(), top());
892 result += transformation *
VectorT2<T>(left(), top());
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
925 ocean_assert(isValid());
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
967 ocean_assert(isValid());
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;
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< 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: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
T width() const
Returns the width of the box.
Definition Box2.h:623
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 T & left() const
Returns the horizontal position of the lower corner of this box.
Definition Box2.h:595
T yDimension() const
Returns the dimension in y axis, which could e.g.
Definition Box2.h:651
const T & top() const
Returns the vertical position of the lower corner of this box.
Definition Box2.h:602
bool operator!=(const BoxT2< T > &right) const
Returns whether two boxes are not identical.
Definition Box2.h:911
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: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
T Type
Definition of the used data 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< T > center() const
Returns the center of this box.
Definition Box2.h:637
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
T height() const
Returns the height of the box.
Definition Box2.h:630
const T & right() const
Returns the horizontal position of the higher corner of this box.
Definition Box2.h:609
const T & bottom() const
Returns the vertical position of the higher corner of this box.
Definition Box2.h:616
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
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: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:710
const T & y() const noexcept
Returns the y value.
Definition Vector2.h:722
static VectorT2< T > minValue()
Returns a 2D vector with all elements set to NumericT::minValue().
Definition Vector2.h:926
static VectorT2< T > maxValue()
Returns a 2D vector with all elements set to NumericT::maxValue().
Definition Vector2.h:932
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
std::vector< Box2 > Boxes2
Definition of a vector holding Box2 objects.
Definition Box2.h:58
The namespace covering the entire Ocean framework.
Definition Accessor.h:15