tesseract  5.0.0-alpha-619-ge9db
TBOX Class Reference

#include <rect.h>

Public Member Functions

 TBOX ()
 
 TBOX (const ICOORD pt1, const ICOORD pt2)
 
 TBOX (int16_t left, int16_t bottom, int16_t right, int16_t top)
 
 TBOX (const FCOORD pt)
 
bool null_box () const
 
bool operator== (const TBOX &other) const
 
int16_t top () const
 
void set_top (int y)
 
int16_t bottom () const
 
void set_bottom (int y)
 
int16_t left () const
 
void set_left (int x)
 
int16_t right () const
 
void set_right (int x)
 
int x_middle () const
 
int y_middle () const
 
const ICOORDbotleft () const
 
ICOORD botright () const
 
ICOORD topleft () const
 
const ICOORDtopright () const
 
int16_t height () const
 
int16_t width () const
 
int32_t area () const
 
void pad (int xpad, int ypad)
 
void move_bottom_edge (const int16_t y)
 
void move_left_edge (const int16_t x)
 
void move_right_edge (const int16_t x)
 
void move_top_edge (const int16_t y)
 
void move (const ICOORD vec)
 
void move (const FCOORD vec)
 
void scale (const float f)
 
void scale (const FCOORD vec)
 
void rotate (const FCOORD &vec)
 
void rotate_large (const FCOORD &vec)
 
bool contains (const FCOORD pt) const
 
bool contains (const TBOX &box) const
 
bool overlap (const TBOX &box) const
 
bool major_overlap (const TBOX &box) const
 
bool x_overlap (const TBOX &box) const
 
int x_gap (const TBOX &box) const
 
int y_gap (const TBOX &box) const
 
bool major_x_overlap (const TBOX &box) const
 
bool y_overlap (const TBOX &box) const
 
bool major_y_overlap (const TBOX &box) const
 
double overlap_fraction (const TBOX &box) const
 
double x_overlap_fraction (const TBOX &box) const
 
double y_overlap_fraction (const TBOX &box) const
 
bool x_almost_equal (const TBOX &box, int tolerance) const
 
bool almost_equal (const TBOX &box, int tolerance) const
 
TBOX intersection (const TBOX &box) const
 
TBOX bounding_union (const TBOX &box) const
 
void set_to_given_coords (int x_min, int y_min, int x_max, int y_max)
 
void print () const
 
void print_to_str (STRING *str) const
 
void plot (ScrollView *fd) const
 
void plot (ScrollView *fd, ScrollView::Color fill_colour, ScrollView::Color border_colour) const
 
bool Serialize (FILE *fp) const
 
bool DeSerialize (bool swap, FILE *fp)
 

Friends

TBOXoperator+= (TBOX &, const TBOX &)
 
TBOXoperator&= (TBOX &, const TBOX &)
 

Detailed Description

Definition at line 33 of file rect.h.

Constructor & Destructor Documentation

◆ TBOX() [1/4]

TBOX::TBOX ( )
inline

Definition at line 35 of file rect.h.

35  :
36  TBOX (): // empty constructor making a null box
37  bot_left (INT16_MAX, INT16_MAX), top_right (-INT16_MAX, -INT16_MAX) {

◆ TBOX() [2/4]

TBOX::TBOX ( const ICOORD  pt1,
const ICOORD  pt2 
)

Definition at line 30 of file rect.cpp.

35  {
36  if (pt1.x () <= pt2.x ()) {
37  if (pt1.y () <= pt2.y ()) {
38  bot_left = pt1;
39  top_right = pt2;
40  }
41  else {
42  bot_left = ICOORD (pt1.x (), pt2.y ());
43  top_right = ICOORD (pt2.x (), pt1.y ());
44  }
45  }
46  else {
47  if (pt1.y () <= pt2.y ()) {
48  bot_left = ICOORD (pt2.x (), pt1.y ());
49  top_right = ICOORD (pt1.x (), pt2.y ());
50  }
51  else {
52  bot_left = pt2;
53  top_right = pt1;
54  }

◆ TBOX() [3/4]

TBOX::TBOX ( int16_t  left,
int16_t  bottom,
int16_t  right,
int16_t  top 
)

Definition at line 61 of file rect.cpp.

66  : bot_left(left, bottom), top_right(right, top) {

◆ TBOX() [4/4]

TBOX::TBOX ( const FCOORD  pt)
inline

Definition at line 316 of file rect.h.

320  {
321  bot_left = ICOORD(static_cast<int16_t>(std::floor(pt.x())),
322  static_cast<int16_t>(std::floor(pt.y())));
323  top_right = ICOORD(static_cast<int16_t>(std::ceil(pt.x())),

Member Function Documentation

◆ almost_equal()

bool TBOX::almost_equal ( const TBOX box,
int  tolerance 
) const

Definition at line 250 of file rect.cpp.

253  {
254  return (abs(left() - box.left()) <= tolerance &&
255  abs(right() - box.right()) <= tolerance);

◆ area()

int32_t TBOX::area ( ) const
inline

Definition at line 121 of file rect.h.

122  { // what is the area?
123  if (!null_box ())
124  return width () * height ();
125  else
126  return 0;

◆ botleft()

const ICOORD& TBOX::botleft ( ) const
inline

Definition at line 91 of file rect.h.

92  { // access function
93  return bot_left;

◆ botright()

ICOORD TBOX::botright ( ) const
inline

Definition at line 95 of file rect.h.

96  { // ~ access function
97  return ICOORD (top_right.x (), bot_left.y ());

◆ bottom()

int16_t TBOX::bottom ( ) const
inline

Definition at line 64 of file rect.h.

65  { // coord of bottom
66  return bot_left.y ();

◆ bounding_union()

TBOX TBOX::bounding_union ( const TBOX box) const

Definition at line 124 of file rect.cpp.

130  {
131  ICOORD bl; //bottom left
132  ICOORD tr; //top right
133 
134  if (box.bot_left.x () < bot_left.x ())
135  bl.set_x (box.bot_left.x ());
136  else
137  bl.set_x (bot_left.x ());
138 
139  if (box.top_right.x () > top_right.x ())
140  tr.set_x (box.top_right.x ());
141  else
142  tr.set_x (top_right.x ());
143 
144  if (box.bot_left.y () < bot_left.y ())
145  bl.set_y (box.bot_left.y ());
146  else
147  bl.set_y (bot_left.y ());
148 
149  if (box.top_right.y () > top_right.y ())

◆ contains() [1/2]

bool TBOX::contains ( const FCOORD  pt) const
inline

Definition at line 330 of file rect.h.

333  {
334  return ((pt.x () >= bot_left.x ()) &&

◆ contains() [2/2]

bool TBOX::contains ( const TBOX box) const
inline

Definition at line 341 of file rect.h.

◆ DeSerialize()

bool TBOX::DeSerialize ( bool  swap,
FILE *  fp 
)

Definition at line 186 of file rect.cpp.

◆ height()

int16_t TBOX::height ( ) const
inline

Definition at line 107 of file rect.h.

108  { // how high is it?
109  if (!null_box ())
110  return top_right.y () - bot_left.y ();
111  else
112  return 0;

◆ intersection()

TBOX TBOX::intersection ( const TBOX box) const

Definition at line 83 of file rect.cpp.

88  {
89  int16_t left;
90  int16_t bottom;
91  int16_t right;
92  int16_t top;
93  if (overlap (box)) {
94  if (box.bot_left.x () > bot_left.x ())
95  left = box.bot_left.x ();
96  else
97  left = bot_left.x ();
98 
99  if (box.top_right.x () < top_right.x ())
100  right = box.top_right.x ();
101  else
102  right = top_right.x ();
103 
104  if (box.bot_left.y () > bot_left.y ())
105  bottom = box.bot_left.y ();
106  else
107  bottom = bot_left.y ();
108 
109  if (box.top_right.y () < top_right.y ())
110  top = box.top_right.y ();
111  else
112  top = top_right.y ();
113  }
114  else {
115  left = INT16_MAX;
116  bottom = INT16_MAX;
117  top = -INT16_MAX;

◆ left()

int16_t TBOX::left ( ) const
inline

Definition at line 71 of file rect.h.

72  { // coord of left
73  return bot_left.x ();

◆ major_overlap()

bool TBOX::major_overlap ( const TBOX box) const
inline

Definition at line 362 of file rect.h.

369  {
370  int overlap = std::min(box.top_right.x(), top_right.x());
371  overlap -= std::max(box.bot_left.x(), bot_left.x());
372  overlap += overlap;
373  if (overlap < std::min(box.width(), width()))
374  return false;
375  overlap = std::min(box.top_right.y(), top_right.y());

◆ major_x_overlap()

bool TBOX::major_x_overlap ( const TBOX box) const
inline

Definition at line 403 of file rect.h.

412  {

◆ major_y_overlap()

bool TBOX::major_y_overlap ( const TBOX box) const
inline

Definition at line 428 of file rect.h.

428  {
429  return ((box.bot_left.y() <= top_right.y()) &&
430  (box.top_right.y() >= bot_left.y()));
431 }
432 
433 /**********************************************************************
434  * TBOX::major_y_overlap() Do two boxes overlap by more than half the
435  * height of the shorter box on the y-axis
436  *
437  **********************************************************************/

◆ move() [1/2]

void TBOX::move ( const FCOORD  vec)
inline

Definition at line 162 of file rect.h.

164  { // by float vector
165  bot_left.set_x(static_cast<int16_t>(std::floor(bot_left.x() + vec.x())));
166  // round left
167  bot_left.set_y(static_cast<int16_t>(std::floor(bot_left.y() + vec.y())));
168  // round down
169  top_right.set_x(static_cast<int16_t>(std::ceil(top_right.x() + vec.x())));
170  // round right
171  top_right.set_y(static_cast<int16_t>(std::ceil(top_right.y() + vec.y())));
172  // round up

◆ move() [2/2]

void TBOX::move ( const ICOORD  vec)
inline

Definition at line 156 of file rect.h.

158  { // by vector
159  bot_left += vec;
160  top_right += vec;

◆ move_bottom_edge()

void TBOX::move_bottom_edge ( const int16_t  y)
inline

Definition at line 136 of file rect.h.

138  { // by +/- y
139  bot_left += ICOORD (0, y);

◆ move_left_edge()

void TBOX::move_left_edge ( const int16_t  x)
inline

Definition at line 141 of file rect.h.

143  { // by +/- x
144  bot_left += ICOORD (x, 0);

◆ move_right_edge()

void TBOX::move_right_edge ( const int16_t  x)
inline

Definition at line 146 of file rect.h.

148  { // by +/- x
149  top_right += ICOORD (x, 0);

◆ move_top_edge()

void TBOX::move_top_edge ( const int16_t  y)
inline

Definition at line 151 of file rect.h.

153  { // by +/- y
154  top_right += ICOORD (0, y);

◆ null_box()

bool TBOX::null_box ( ) const
inline

Definition at line 49 of file rect.h.

50  { // Is box null
51  return ((left () >= right ()) || (top () <= bottom ()));

◆ operator==()

bool TBOX::operator== ( const TBOX other) const
inline

Definition at line 53 of file rect.h.

54  {
55  return bot_left == other.bot_left && top_right == other.top_right;

◆ overlap()

bool TBOX::overlap ( const TBOX box) const
inline

Definition at line 350 of file rect.h.

356  {

◆ overlap_fraction()

double TBOX::overlap_fraction ( const TBOX box) const
inline

Definition at line 381 of file rect.h.

◆ pad()

void TBOX::pad ( int  xpad,
int  ypad 
)
inline

Definition at line 130 of file rect.h.

131  {
132  ICOORD pad(xpad, ypad);
133  bot_left -= pad;
134  top_right += pad;

◆ plot() [1/2]

void TBOX::plot ( ScrollView fd) const
inline

Definition at line 285 of file rect.h.

287  { // where to paint
288  fd->Rectangle(bot_left.x (), bot_left.y (), top_right.x (),
289  top_right.y ());

◆ plot() [2/2]

void TBOX::plot ( ScrollView fd,
ScrollView::Color  fill_colour,
ScrollView::Color  border_colour 
) const

Definition at line 157 of file rect.cpp.

◆ print()

void TBOX::print ( ) const
inline

Definition at line 277 of file rect.h.

278  { // print
279  tprintf("Bounding box=(%d,%d)->(%d,%d)\n",
280  left(), bottom(), right(), top());

◆ print_to_str()

void TBOX::print_to_str ( STRING str) const

Definition at line 169 of file rect.cpp.

175  {
176  // "(%d,%d)->(%d,%d)", left(), bottom(), right(), top()

◆ right()

int16_t TBOX::right ( ) const
inline

Definition at line 78 of file rect.h.

79  { // coord of right
80  return top_right.x ();

◆ rotate()

void TBOX::rotate ( const FCOORD vec)
inline

Definition at line 196 of file rect.h.

197  { // by vector
198  bot_left.rotate (vec);
199  top_right.rotate (vec);
200  *this = TBOX (bot_left, top_right);

◆ rotate_large()

void TBOX::rotate_large ( const FCOORD vec)

Definition at line 69 of file rect.cpp.

72  {
73  ICOORD top_left(bot_left.x(), top_right.y());
74  ICOORD bottom_right(top_right.x(), bot_left.y());
75  top_left.rotate(vec);
76  bottom_right.rotate(vec);
77  rotate(vec);

◆ scale() [1/2]

void TBOX::scale ( const FCOORD  vec)
inline

Definition at line 185 of file rect.h.

187  { // by float vector
188  bot_left.set_x(static_cast<int16_t>(std::floor(bot_left.x() * vec.x())));
189  bot_left.set_y(static_cast<int16_t>(std::floor(bot_left.y() * vec.y())));
190  top_right.set_x(static_cast<int16_t>(std::ceil(top_right.x() * vec.x())));
191  top_right.set_y(static_cast<int16_t>(std::ceil(top_right.y() * vec.y())));

◆ scale() [2/2]

void TBOX::scale ( const float  f)
inline

Definition at line 174 of file rect.h.

176  { // by multiplier
177  // round left
178  bot_left.set_x(static_cast<int16_t>(std::floor(bot_left.x() * f)));
179  // round down
180  bot_left.set_y(static_cast<int16_t>(std::floor(bot_left.y() * f)));
181  // round right
182  top_right.set_x(static_cast<int16_t>(std::ceil(top_right.x() * f)));
183  // round up
184  top_right.set_y(static_cast<int16_t>(std::ceil(top_right.y() * f)));

◆ Serialize()

bool TBOX::Serialize ( FILE *  fp) const

Definition at line 179 of file rect.cpp.

◆ set_bottom()

void TBOX::set_bottom ( int  y)
inline

Definition at line 67 of file rect.h.

68  {
69  bot_left.set_y(y);

◆ set_left()

void TBOX::set_left ( int  x)
inline

Definition at line 74 of file rect.h.

75  {
76  bot_left.set_x(x);

◆ set_right()

void TBOX::set_right ( int  x)
inline

Definition at line 81 of file rect.h.

82  {
83  top_right.set_x(x);

◆ set_to_given_coords()

void TBOX::set_to_given_coords ( int  x_min,
int  y_min,
int  x_max,
int  y_max 
)
inline

Definition at line 270 of file rect.h.

271  {
272  bot_left.set_x(x_min);
273  bot_left.set_y(y_min);
274  top_right.set_x(x_max);
275  top_right.set_y(y_max);

◆ set_top()

void TBOX::set_top ( int  y)
inline

Definition at line 60 of file rect.h.

61  {
62  top_right.set_y(y);

◆ top()

int16_t TBOX::top ( ) const
inline

Definition at line 57 of file rect.h.

58  { // coord of top
59  return top_right.y ();

◆ topleft()

ICOORD TBOX::topleft ( ) const
inline

Definition at line 99 of file rect.h.

100  { // ~ access function
101  return ICOORD (bot_left.x (), top_right.y ());

◆ topright()

const ICOORD& TBOX::topright ( ) const
inline

Definition at line 103 of file rect.h.

104  { // access function
105  return top_right;

◆ width()

int16_t TBOX::width ( ) const
inline

Definition at line 114 of file rect.h.

115  { // how high is it?
116  if (!null_box ())
117  return top_right.x () - bot_left.x ();
118  else
119  return 0;

◆ x_almost_equal()

bool TBOX::x_almost_equal ( const TBOX box,
int  tolerance 
) const

Definition at line 245 of file rect.cpp.

◆ x_gap()

int TBOX::x_gap ( const TBOX box) const
inline

Definition at line 224 of file rect.h.

225  {
226  return std::max(bot_left.x(), box.bot_left.x()) -
227  std::min(top_right.x(), box.top_right.x());

◆ x_middle()

int TBOX::x_middle ( ) const
inline

Definition at line 84 of file rect.h.

85  {
86  return (bot_left.x() + top_right.x()) / 2;

◆ x_overlap()

bool TBOX::x_overlap ( const TBOX box) const
inline

Definition at line 393 of file rect.h.

◆ x_overlap_fraction()

double TBOX::x_overlap_fraction ( const TBOX box) const
inline

Definition at line 445 of file rect.h.

457  {
458  int low = std::max(left(), other.left());

◆ y_gap()

int TBOX::y_gap ( const TBOX box) const
inline

Definition at line 232 of file rect.h.

233  {
234  return std::max(bot_left.y(), box.bot_left.y()) -
235  std::min(top_right.y(), box.top_right.y());

◆ y_middle()

int TBOX::y_middle ( ) const
inline

Definition at line 87 of file rect.h.

88  {
89  return (bot_left.y() + top_right.y()) / 2;

◆ y_overlap()

bool TBOX::y_overlap ( const TBOX box) const
inline

Definition at line 418 of file rect.h.

◆ y_overlap_fraction()

double TBOX::y_overlap_fraction ( const TBOX box) const
inline

Definition at line 466 of file rect.h.

467  {
468  return std::max(0.0, static_cast<double>(high - low) / width);
469  }
470 }
471 
472 /**********************************************************************
473  * TBOX::y_overlap_fraction() Calculates the vertical overlap of the
474  * given boxes as a fraction of this boxes
475  * height.
476  *
477  **********************************************************************/
478 
479 inline double TBOX::y_overlap_fraction(const TBOX& other) const {

Friends And Related Function Documentation

◆ operator&=

TBOX& operator&= ( TBOX op1,
const TBOX op2 
)
friend

Definition at line 222 of file rect.cpp.

230  {
231  if (op1.overlap (op2)) {
232  if (op2.bot_left.x () > op1.bot_left.x ())
233  op1.bot_left.set_x (op2.bot_left.x ());
234 
235  if (op2.top_right.x () < op1.top_right.x ())
236  op1.top_right.set_x (op2.top_right.x ());
237 
238  if (op2.bot_left.y () > op1.bot_left.y ())
239  op1.bot_left.set_y (op2.bot_left.y ());
240 
241  if (op2.top_right.y () < op1.top_right.y ())
242  op1.top_right.set_y (op2.top_right.y ());
243  }

◆ operator+=

TBOX& operator+= ( TBOX op1,
const TBOX op2 
)
friend

Definition at line 198 of file rect.cpp.

207  {
208  if (op2.bot_left.x () < op1.bot_left.x ())
209  op1.bot_left.set_x (op2.bot_left.x ());
210 
211  if (op2.top_right.x () > op1.top_right.x ())
212  op1.top_right.set_x (op2.top_right.x ());
213 
214  if (op2.bot_left.y () < op1.bot_left.y ())

The documentation for this class was generated from the following files:
ICOORD::set_x
void set_x(int16_t xin)
rewrite function
Definition: points.h:60
TBOX::overlap
bool overlap(const TBOX &box) const
Definition: rect.h:350
FCOORD::y
float y() const
Definition: points.h:209
ICOORD
integer coordinate
Definition: points.h:30
ICOORD::rotate
void rotate(const FCOORD &vec)
Definition: points.h:522
FCOORD::x
float x() const
Definition: points.h:206
TBOX::top
int16_t top() const
Definition: rect.h:57
ICOORD::x
int16_t x() const
access function
Definition: points.h:51
ICOORD::set_y
void set_y(int16_t yin)
rewrite function
Definition: points.h:64
TBOX::rotate
void rotate(const FCOORD &vec)
Definition: rect.h:196
TBOX::height
int16_t height() const
Definition: rect.h:107
TBOX::null_box
bool null_box() const
Definition: rect.h:49
TBOX::width
int16_t width() const
Definition: rect.h:114
TBOX::bottom
int16_t bottom() const
Definition: rect.h:64
TBOX::pad
void pad(int xpad, int ypad)
Definition: rect.h:130
TBOX::left
int16_t left() const
Definition: rect.h:71
TBOX::right
int16_t right() const
Definition: rect.h:78
TBOX::y_overlap_fraction
double y_overlap_fraction(const TBOX &box) const
Definition: rect.h:466
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
TBOX::TBOX
TBOX()
Definition: rect.h:35
ScrollView::Rectangle
void Rectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:599
ICOORD::y
int16_t y() const
access_function
Definition: points.h:55
TBOX
Definition: rect.h:33