22 #ifndef CRNRECT_HEADER
23 #define CRNRECT_HEADER
50 Rect() noexcept:bx(0),by(0),ex(0),ey(0),w(0),h(0), valid(
false) {}
61 inline Rect(
int begX,
int begY,
int endX,
int endY) noexcept:bx(begX),by(begY),ex(endX),ey(endY)
62 { w = ex - bx + 1; h = ey - by + 1; valid = (w >= 0) && (h >= 0); }
71 Rect(
int x,
int y) noexcept:bx(x),by(y),ex(x),ey(y),w(1),h(1),valid(
true) { }
79 Rect(
const Point2DInt &p) noexcept:bx(p.X),by(p.Y),ex(p.X),ey(p.Y),w(1),h(1),valid(
true) { }
83 virtual ~Rect()
override =
default;
94 inline bool IsValid() const noexcept {
return valid; }
142 unsigned int GetArea() const noexcept {
return valid ? (w * h) : 0; }
148 return (w*h == 1) ? 1 : (w + h) * 2 - 4;
243 bool Contains(
int x,
int y)
const noexcept;
264 Rect first_rct = *it_begin;
267 int t = first_rct.
GetTop();
269 ITER it = it_begin + 1;
275 t = std::min(t, rb.
GetTop());
279 return Rect(l, t, r, b);
290 template <
class ITER>
static int MedianWidth(ITER it_begin, ITER it_end)
292 auto w = std::vector<int>();
293 for (
auto it = it_begin; it != it_end; ++it)
294 w.push_back(it->GetWidth());
306 template <
class ITER>
static int MedianHeight(ITER it_begin, ITER it_end)
308 auto h = std::vector<int>();
309 for (
auto it = it_begin; it != it_end; ++it)
310 h.push_back(it->GetHeight());
322 template <
class ITER>
static double MeanWidth(ITER it_begin, ITER it_end)
325 double counter = 0.0;
329 cumul += ((*it).GetWidth());
333 return cumul / std::max(1.0, counter);
344 template <
class ITER>
static double MeanHeight(ITER it_begin, ITER it_end)
347 double counter = 0.0;
351 cumul += ((*it).GetHeight());
355 return cumul / std::max(1.0, counter);
369 std::vector<Rect> closest;
371 ITER it_ref = it_begin;
375 while (it_ref != it_end)
377 bool is_extremal =
true;
378 int border_ref = it_ref->GetLeft();
380 ITER it_cmp = it_begin;
382 while ((it_cmp != it_end) && (is_extremal))
384 if (it_cmp != it_ref)
386 if (border_ref > it_cmp->GetLeft())
393 closest.push_back(*it_ref);
400 while (it_ref != it_end)
402 bool is_extremal =
true;
403 int border_ref = it_ref->GetRight();
405 ITER it_cmp = it_begin;
407 while ((it_cmp != it_end) && (is_extremal))
409 if (it_cmp != it_ref)
411 if (border_ref < it_cmp->
GetRight())
418 closest.push_back(*it_ref);
425 while (it_ref != it_end)
427 bool is_extremal =
true;
428 int border_ref = it_ref->GetTop();
430 ITER it_cmp = it_begin;
432 while ((it_cmp != it_end) && (is_extremal))
434 if (it_cmp != it_ref)
436 if (border_ref > it_cmp->GetTop())
443 closest.push_back(*it_ref);
450 while (it_ref != it_end)
452 bool is_extremal =
true;
453 int border_ref = it_ref->GetBottom();
455 ITER it_cmp = it_begin;
457 while ((it_cmp != it_end) && (is_extremal))
459 if (it_cmp != it_ref)
468 closest.push_back(*it_ref);
485 template <
class ITER> std::vector<Rect>
FindIncluded(ITER it_begin, ITER it_end)
487 std::vector<Rect> included;
490 const auto r_top =
GetTop();
499 included.push_back(r_i);
515 template <
class ITER> std::vector<Rect>
FindIntersecting(ITER it_begin, ITER it_end,
double ratio = 0)
517 ratio = std::max(ratio, 0.0);
518 ratio = std::min(ratio, 1.0);
519 std::vector<Rect> touching;
526 Rect rct_inter = (*this)&rb;
527 if((rct_inter).IsValid())
529 if (rct_inter.
GetArea() > ratio * rb.GetArea())
531 touching.push_back(rb);
547 class Sorter:
public std::binary_function<const Rect&, const Rect&, bool>
632 class AreaSorter:
public std::binary_function<const Rect&, const Rect&, bool>
638 inline AreaSorter(
bool biggertosmaller =
false):big2low(biggertosmaller) {}
660 class HeightSorter:
public std::binary_function<const Rect&, const Rect&, bool>
666 inline HeightSorter(
bool tallertosmaller =
false):tall2small(tallertosmaller) {}
689 struct BitwiseCompare:
public std::binary_function<const Rect&, const Rect&, bool>
715 using Set = std::set<Rect, Rect::BitwiseCompare>;
723 class iterator:
public std::iterator<std::input_iterator_tag, const crn::Point2DInt>
736 iterator operator++(
int nouse) noexcept;
737 reference operator*() const noexcept {
return pos; }
758 class spiral_iterator:
public std::iterator<std::input_iterator_tag, const crn::Point2DInt>
778 int rectl = 0, rectt = 0, rectr = 0, rectb = 0;
782 int framel = 0, framer = 0, framet = 0, frameb = 0;
788 bool preproc =
false;
T MedianValue(const std::vector< T > &v)
Median value.
virtual ~Rect() override=default
virtual const iterator & operator++() noexcept
AreaSorter(bool biggertosmaller=false)
Constructor.
bool operator()(const Rect &r1, const Rect &r2) const
Comparison function.
Functor to sort rectangles regarding horizontal stretching values.
#define CRN_SERIALIZATION_CONSTRUCTOR(classname)
Defines a default constructor from xml element.
pointer operator->() const
Point2DInt GetBottomRight() const
Returns the bottom-right coordinates.
OrthogonalSorter(Direction sort_direction)
Constructor.
bool operator==(const spiral_iterator &other) const
#define CRN_ADD_RANGED_FOR_TO_CONST_POINTERS(TYPE)
Enables ranged for for smart pointers on a type.
Rect(const Point2DInt &p) noexcept
Rectangle constructor.
bool operator()(const Rect &r1, const Rect &r2) const
Comparison function.
Orientation
An enumeration of orientations.
spiral_iterator & operator=(const spiral_iterator &)=default
static Set EmptySet()
Creates an empty set of Rects.
bool operator()(const Rect &r1, const Rect &r2) const
Comparison function.
InclusionSorter()
Constructor.
int Overlap(const Rect &r, Orientation orientation) const
Compute overlap in a given orientation.
Functor to sort rectangles regarding surfaces.
iterator & operator=(const iterator &)=default
int GetBottom() const
Returns the bottommost coordinate.
Unintialized object error.
Functor to sort Rects in sets and maps.
bool operator()(const Rect &r1, const Rect &r2) const
Comparison function.
Direction
An enumeration of directions.
#define CRN_ADD_RANGED_FOR_TO_POINTERS(TYPE)
Enables ranged for for smart pointers on a type.
int SetHeight(int hei)
Changes the height of the rectangle.
spiral_iterator SBegin() const
Returns an spiral iterator to the first point of the rectangle.
bool operator()(const Rect &r1, const Rect &r2) const
Comparison function.
Rect operator|(const Rect &r) const
Computes the union of two rectangles.
int GetTop() const
Returns the topmost coordinate.
iterator cbegin() const
Returns an iterator to the first point of the rectangle.
int GetLeft() const
Returns the leftmost coordinate.
bool operator()(const Rect &r1, const Rect &r2) const
Comparison function.
Functor to sort rectangles regarding surfaces.
static std::vector< Rect > FindClosestsToBorder(ITER it_begin, ITER it_end, Direction drt)
Get rectangles closest to border in a direction from a collection of rectangles.
A UTF32 character string class.
void Deserialize(xml::Element &el)
Initializes the object from an XML element. Unsafe.
int SetBottom(int endY) noexcept
Changes the bottommost coordinate.
std::set< Rect, Rect::BitwiseCompare > Set
A set of Rects.
spiral_iterator SEnd() const
Returns an spiral iterator after the last point of the rectangle.
bool operator()(const Rect &r1, const Rect &r2) const
Comparison function.
HorizontalStretchingSorter()
Constructor.
int SetWidth(int wid)
Changes the width of the rectangle.
bool Contains(int x, int y) const noexcept
Checks if the rectangle contains a point.
const Rect & operator*=(double s)
Scales the rectangle.
xml::Element Serialize(xml::Element &parent) const
Dumps the object to an XML element. Unsafe.
Sorter(Direction sort_direction)
Constructor.
unsigned int GetArea() const noexcept
Returns the area of the rectangle.
unsigned int GetPerimeter() const noexcept
Returns the perimeter of the rectangle.
Rect & operator=(const Rect &)=default
Functor to sort rectangles regarding directions.
iterator end() const
Returns an iterator after the last point of the rectangle.
void operator&=(const Rect &r)
Keeps only the intersecting part with another rectangle.
bool IsValid() const noexcept
Returns whether the rect is valid.
const spiral_iterator & operator++()
Functor to sort rectangles by inclusion.
bool operator!=(const iterator &other) const noexcept
iterator begin() const
Returns an iterator to the first point of the rectangle.
Point2DInt GetCenter() const
Returns the center coordinates.
bool IsValid() const noexcept
iterator cend() const
Returns an iterator after the last point of the rectangle.
void Translate(int x, int y)
Translates the rectangle.
pointer operator->() const noexcept
int GetHeight() const
Returns the height of the rectangle.
Rect operator&(const Rect &r) const
Computes the intersection of two rectangles.
static int MedianWidth(ITER it_begin, ITER it_end)
Get median width value for a collection of rectangles.
Point2DInt GetTopLeft() const
Returns the top-left coordinates.
Rect() noexcept
Dummy constructor. DO NOT USE.
HeightSorter(bool tallertosmaller=false)
Constructor.
std::vector< Rect > FindIntersecting(ITER it_begin, ITER it_end, double ratio=0)
Get the rectangles intersecting significantly this from a collection.
bool operator==(const iterator &other) const noexcept
int SetRight(int endX) noexcept
Changes the rightmost coordinate.
int SetTop(int begY) noexcept
Changes the topmost coordinate.
bool operator!=(const spiral_iterator &other) const
static Rect SmallestRectEmbedding(ITER it_begin, ITER it_end)
Get the smallest rectangle embedding a collection of rectangles.
int GetWidth() const
Returns the width of the rectangle.
static double MeanWidth(ITER it_begin, ITER it_end)
Get average width value for a collection of rectangles.
static double MeanHeight(ITER it_begin, ITER it_end)
Get median height value for a collection of rectangles.
int GetCenterX() const
Returns the horizontal center coordinate.
Functor to sort rectangles regarding orthogonal directions.
#define CRN_DECLARE_CLASS_CONSTRUCTOR(classname)
Declares a class constructor.
Rect operator*(double s) const
Creates a scaled rectangle.
void operator|=(const Rect &r)
Merges with another rectangle.
String ToString() const
Dumps to a string.
CRN_ALIAS_SMART_PTR(ImageBW)
bool operator==(const Rect &r) const noexcept
Checks if two rectangles are identical.
Rect(int x, int y) noexcept
Rectangle constructor.
bool operator!=(const Rect &r) const noexcept
Checks if two rectangles are different.
static int MedianHeight(ITER it_begin, ITER it_end)
Get median height value for a collection of rectangles.
int GetRight() const
Returns the rightmost coordinate.
reference operator*() const
int GetCenterY() const
Returns the vertical center coordinate.
Spiral iterator for a Rect.
iterator(const Rect &r) noexcept
Rect(int begX, int begY, int endX, int endY) noexcept
Rectangle constructor.
int SetLeft(int begX) noexcept
Changes the leftmost coordinate.
std::vector< Rect > FindIncluded(ITER it_begin, ITER it_end)
Get the rectangles included in this.