8 #ifndef META_OCEAN_CV_PIXEL_BOUNDING_BOX_H
9 #define META_OCEAN_CV_PIXEL_BOUNDING_BOX_H
21 template <
typename T>
class PixelBoundingBoxT;
184 inline unsigned int size()
const;
315 explicit inline operator bool()
const;
332 template <
typename T>
342 template <
typename T>
344 left_(boundingBox.left_),
345 top_(boundingBox.top_),
346 right_(boundingBox.right_),
347 bottom_(boundingBox.bottom_)
352 template <
typename T>
362 template <
typename T>
369 for (
typename std::vector<
PixelPositionT<T>>::const_iterator i = points.begin(); i != points.end(); ++i)
375 template <
typename T>
385 template <
typename T>
389 right_(topLeft.x() + T(width - 1u)),
390 bottom_(topLeft.y() + T(height - 1u))
392 ocean_assert(
width >= 1u);
393 ocean_assert(
height >= 1u);
396 template <
typename T>
398 left_(std::min(point0.x(), point1.x())),
399 top_(std::min(point0.y(), point1.y())),
400 right_(std::max(point0.x(), point1.x())),
401 bottom_(std::max(point0.y(), point1.y()))
403 ocean_assert(
width() >= 1u);
404 ocean_assert(
height() >= 1u);
408 debugBoundingBox += point0;
409 debugBoundingBox += point1;
411 ocean_assert(*
this == debugBoundingBox);
415 template <
typename T>
418 ocean_assert(isValid());
422 template <
typename T>
425 ocean_assert(isValid());
429 template <
typename T>
432 ocean_assert(isValid());
436 template <
typename T>
439 ocean_assert(isValid());
440 return right_ + T(1);
443 template <
typename T>
446 ocean_assert(isValid());
450 template <
typename T>
453 ocean_assert(isValid());
454 return bottom_ + T(1);
457 template <
typename T>
463 template <
typename T>
469 template <
typename T>
475 template <
typename T>
481 template <
typename T>
484 ocean_assert(isValid());
485 return (
unsigned int)(right_ - left_) + 1u;
488 template <
typename T>
491 ocean_assert(isValid());
492 return (
unsigned int)(bottom_ - top_) + 1u;
495 template <
typename T>
503 return width() * height();
506 template <
typename T>
509 ocean_assert(isValid() && point.
isValid());
511 return point.
x() >= left_ && point.
y() >= top_ && point.
x() <= right_ && point.
y() <= bottom_;
514 template <
typename T>
517 ocean_assert(isValid() && boundingBox.
isValid());
519 return boundingBox.
left_ >= left_ && boundingBox.
top_ >= top_ && boundingBox.
right_ <= right_ && boundingBox.
bottom_ <= bottom_;
522 template <
typename T>
525 ocean_assert(isValid() && boundingBox.
isValid());
529 return !(left_ > boundingBox.
right_ || boundingBox.
left_ > right_
530 || top_ > boundingBox.
bottom_ || boundingBox.
top_ > bottom_);
533 template <
typename T>
536 ocean_assert(isValid() && boundingBox.
isValid());
538 if (left_ > boundingBox.
right_ + T(1) || boundingBox.
left_ > right_ + T(1)
539 || top_ > boundingBox.
bottom_ + T(1) || boundingBox.
top_ > bottom_ + T(1))
544 if (!useNeighborhood8)
548 if ((left_ == boundingBox.
right_ + T(1) || right_ + T(1) == boundingBox.
left_) && (top_ == boundingBox.
bottom_ + T(1) || bottom_ + T(1) == boundingBox.
top_))
557 template <
typename T>
560 static_assert(
sizeof(T) <=
sizeof(
int),
"Invalid template data type T");
562 ocean_assert(isValid());
564 ocean_assert(minLeft <= left() && minTop <= top());
565 ocean_assert(maxRight >= right() && maxBottom >= bottom());
567 ocean_assert(minLeft <= maxRight);
568 ocean_assert(minTop <= maxBottom);
571 max(
int(minTop),
int(top_) -
int(pixels)),
572 min(maxRight, right_ + T(pixels)),
573 min(maxBottom, bottom_ + T(pixels)));
576 template <
typename T>
579 return right_ >= left_ && bottom_ >= top_;
582 template <
typename T>
587 if (point.
x() < left_)
592 if (point.
x() > right_)
597 if (point.
y() < top_)
602 if (point.
y() > bottom_)
610 template <
typename T>
616 template <
typename T>
619 return !(*
this == box);
622 template <
typename T>
625 ocean_assert(isValid());
630 template <
typename T>
633 ocean_assert(isValid());
643 template <
typename T>
646 ocean_assert(isValid());
647 ocean_assert(factor != T(0));
652 template <
typename T>
655 ocean_assert(isValid());
656 ocean_assert(factor != T(0));
666 template <
typename T>
682 template <
typename T>
685 if (isValid() && box.
isValid())
693 template <
typename T>
696 size_t seed = std::hash<T>{}(boundingBox.
left());
697 seed ^= std::hash<T>{}(boundingBox.
top()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
698 seed ^= std::hash<T>{}(boundingBox.
right()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
699 seed ^= std::hash<T>{}(boundingBox.
bottom()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
704 template <
typename T>
PixelBoundingBoxT< T > & operator/=(const T factor)
Divides this bounding box by a specified scalar (divides left, top, right, and bottom location by the...
Definition: PixelBoundingBox.h:653
PixelBoundingBoxT(const PixelPositionT< T > &point)
Creates a bounding box covering one point only.
Definition: PixelBoundingBox.h:353
PixelBoundingBoxT< T > & operator*=(const T factor)
Multiplies this bounding box by a specified scalar (multiplies left, top, right, and bottom location ...
Definition: PixelBoundingBox.h:631
PixelBoundingBoxT(const std::vector< PixelPositionT< T >> &points)
Creates a bounding box covering several points.
Definition: PixelBoundingBox.h:363
bool operator==(const PixelBoundingBoxT< T > &box) const
Returns whether two bounding boxes are equal.
Definition: PixelBoundingBox.h:611
PixelBoundingBoxT(const PixelPositionT< T > &topLeft, const unsigned int width, const unsigned int height)
Creates a bounding box by a given top left position and a width and height of the bounding box.
Definition: PixelBoundingBox.h:386
bool operator!=(const PixelBoundingBoxT< T > &box) const
Returns whether two bounding boxes are not equal.
Definition: PixelBoundingBox.h:617
T left_
Left (including) pixel position of this bounding box.
Definition: PixelBoundingBox.h:320
PixelBoundingBoxT< T > operator/(const T factor) const
Divides this bounding box by a specified scalar (divides left, top, right, and bottom location by the...
Definition: PixelBoundingBox.h:644
T left() const
Returns the left (including) pixel position of this bounding box.
Definition: PixelBoundingBox.h:416
PixelBoundingBoxT< T > & operator=(const PixelBoundingBoxT< T > &other)=default
Assignment operator override to match copy constructor.
bool hasIntersection(const PixelBoundingBoxT< T > &boundingBox) const
Returns whether a given bounding box has an intersection with this bounding box.
Definition: PixelBoundingBox.h:523
PixelBoundingBoxT(const T left, const T top, const T right, const T bottom)
Creates a bounding box with specified dimension.
Definition: PixelBoundingBox.h:376
PixelBoundingBoxT< T > operator&&(const PixelBoundingBoxT< T > &box) const
Returns the intersection of two bounding boxes.
Definition: PixelBoundingBox.h:683
T right() const
Returns the right (including) pixel position of this bounding box.
Definition: PixelBoundingBox.h:430
T bottom() const
Returns the bottom (including) position of this bounding box.
Definition: PixelBoundingBox.h:444
unsigned int width() const
Returns the width (the number of horizontal including pixels) of this bounding box.
Definition: PixelBoundingBox.h:482
PixelBoundingBoxT(const PixelBoundingBoxT< T > &boundingBox)
Copy constructor.
Definition: PixelBoundingBox.h:343
PixelPositionT< T > bottomRight() const
Returns the bottom right corner of this bounding box.
Definition: PixelBoundingBox.h:476
T rightEnd() const
Returns the right (excluding) pixel position of this bounding box.
Definition: PixelBoundingBox.h:437
bool isValid() const
Returns whether this bounding box covers a valid pixel area.
Definition: PixelBoundingBox.h:577
bool isInside(const PixelBoundingBoxT< T > &boundingBox) const
Returns whether a given bounding box lies entirely inside this bounding box.
Definition: PixelBoundingBox.h:515
bool isInside(const PixelPositionT< T > &point) const
Returns whether a given point lies inside this bounding box.
Definition: PixelBoundingBox.h:507
PixelBoundingBoxT()
Creates a empty bounding box.
Definition: PixelBoundingBox.h:333
T bottomEnd() const
Returns the bottom (excluding) pixel position of this bounding box.
Definition: PixelBoundingBox.h:451
PixelBoundingBoxT< T > extended(const unsigned int pixels, const T minLeft, const T minTop, const T maxRight, const T maxBottom) const
Returns a new bounding box by extending this bounding box with a given number of pixel in each direct...
Definition: PixelBoundingBox.h:558
T top_
Top (including) pixel position of this bounding box.
Definition: PixelBoundingBox.h:323
PixelBoundingBoxT< T > operator||(const PixelBoundingBoxT< T > &box) const
Returns the union of two bounding boxes.
Definition: PixelBoundingBox.h:667
PixelPositionT< T > topLeft() const
Returns the top left corner of this bounding box.
Definition: PixelBoundingBox.h:458
unsigned int size() const
Returns the area (the number of including pixels) this bounding box covers.
Definition: PixelBoundingBox.h:496
PixelPositionT< T > bottomLeft() const
Returns the bottom left corner of this bounding box.
Definition: PixelBoundingBox.h:470
T bottom_
Bottom (including) pixel position of this bounding box.
Definition: PixelBoundingBox.h:329
PixelBoundingBoxT(const PixelPositionT< T > &point0, const PixelPositionT< T > &point1)
Creates a bounding box covering two points.
Definition: PixelBoundingBox.h:397
PixelPositionT< T > topRight() const
Returns the top right corner of this bounding box.
Definition: PixelBoundingBox.h:464
size_t operator()(const PixelBoundingBoxT< T > &boundingBox) const
Hash function.
Definition: PixelBoundingBox.h:694
T top() const
Returns the top (including) pixel position of this bounding box.
Definition: PixelBoundingBox.h:423
PixelBoundingBoxT< T > & operator+=(const PixelPositionT< T > &point)
Adds a new point to the bounding box.
Definition: PixelBoundingBox.h:583
PixelBoundingBoxT< T > operator*(const T factor) const
Multiplies this bounding box by a specified scalar (multiplies left, top, right, and bottom location ...
Definition: PixelBoundingBox.h:623
unsigned int height() const
Returns the height (the number of vertical including pixels) of this bounding box.
Definition: PixelBoundingBox.h:489
T right_
Right (including) pixel position of this bounding box.
Definition: PixelBoundingBox.h:326
bool isTouching(const PixelBoundingBoxT< T > &boundingBox, const bool useNeighborhood8) const
Returns whether a given bounding box is touching this bounding box.
Definition: PixelBoundingBox.h:534
This class implements a 2D pixel position with pixel precision.
Definition: PixelPosition.h:65
bool isValid() const
Returns whether this pixel position object holds two valid parameters.
T y() const
Returns the vertical coordinate position of this object.
Definition: PixelPosition.h:470
T x() const
Returns the horizontal coordinate position of this object.
Definition: PixelPosition.h:458
This class provides basic numeric functionalities.
Definition: Numeric.h:57
std::vector< PixelBoundingBoxI > PixelBoundingBoxesI
Definition of a vector holding bounding box objects with positive and negative coordinate values.
Definition: PixelBoundingBox.h:49
PixelBoundingBoxT< int > PixelBoundingBoxI
Definition of a PixelBoundingBox object with data type allowing positive and negative coordinate valu...
Definition: PixelBoundingBox.h:35
std::vector< PixelBoundingBox > PixelBoundingBoxes
Definition of a vector holding bounding box objects with only positive coordinate values.
Definition: PixelBoundingBox.h:42
PixelBoundingBoxT< unsigned int > PixelBoundingBox
Definition of the default PixelBoundingBox object with data type allowing only positive coordinate va...
Definition: PixelBoundingBox.h:21
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15