tesseract  5.0.0
tesseract::POLY_BLOCK Class Reference

#include <polyblk.h>

Public Member Functions

 POLY_BLOCK ()=default
 
 POLY_BLOCK (const TBOX &tbox, PolyBlockType type)
 
 POLY_BLOCK (ICOORDELT_LIST *points, PolyBlockType type)
 
 ~POLY_BLOCK ()=default
 
TBOXbounding_box ()
 
ICOORDELT_LIST * points ()
 
PolyBlockType isA () const
 
bool IsText () const
 
POLY_BLOCK::compute_bb

Compute the bounding box from the outline points.

void compute_bb ()
 
POLY_BLOCK::rotate

Rotate the POLY_BLOCK.

Parameters
rotationcos, sin of angle
void rotate (FCOORD rotation)
 
POLY_BLOCK::winding_number

Return the winding number of the outline around the given point.

Parameters
pointpoint to wind around
bool contains (POLY_BLOCK *other)
 
int16_t winding_number (const ICOORD &test_pt)
 

POLY_BLOCK::reflect_in_y_axis

Reflect the coords of the polygon in the y-axis. (Flip the sign of x.)

void reflect_in_y_axis ()
 
void move (ICOORD shift)
 
void plot (ScrollView *window, int32_t num)
 
void fill (ScrollView *window, ScrollView::Color colour)
 
bool overlap (POLY_BLOCK *other)
 
static ScrollView::Color ColorForPolyBlockType (PolyBlockType type)
 Returns a color to draw the given type. More...
 

Detailed Description

Definition at line 30 of file polyblk.h.

Constructor & Destructor Documentation

◆ POLY_BLOCK() [1/3]

tesseract::POLY_BLOCK::POLY_BLOCK ( )
default

◆ POLY_BLOCK() [2/3]

tesseract::POLY_BLOCK::POLY_BLOCK ( const TBOX tbox,
PolyBlockType  type 
)

Definition at line 50 of file polyblk.cpp.

50  {
51  vertices.clear();
52  ICOORDELT_IT v = &vertices;
53  v.move_to_first();
54  v.add_to_end(new ICOORDELT(tbox.left(), tbox.top()));
55  v.add_to_end(new ICOORDELT(tbox.left(), tbox.bottom()));
56  v.add_to_end(new ICOORDELT(tbox.right(), tbox.bottom()));
57  v.add_to_end(new ICOORDELT(tbox.right(), tbox.top()));
58  compute_bb();
59  type = t;
60 }

◆ POLY_BLOCK() [3/3]

tesseract::POLY_BLOCK::POLY_BLOCK ( ICOORDELT_LIST *  points,
PolyBlockType  type 
)

Definition at line 39 of file polyblk.cpp.

39  {
40  ICOORDELT_IT v = &vertices;
41 
42  vertices.clear();
43  v.move_to_first();
44  v.add_list_before(points);
45  compute_bb();
46  type = t;
47 }
ICOORDELT_LIST * points()
Definition: polyblk.h:42

◆ ~POLY_BLOCK()

tesseract::POLY_BLOCK::~POLY_BLOCK ( )
default

Member Function Documentation

◆ bounding_box()

TBOX* tesseract::POLY_BLOCK::bounding_box ( )
inline

Definition at line 38 of file polyblk.h.

38  { // access function
39  return &box;
40  }

◆ ColorForPolyBlockType()

ScrollView::Color tesseract::POLY_BLOCK::ColorForPolyBlockType ( PolyBlockType  type)
static

Returns a color to draw the given type.

Definition at line 389 of file polyblk.cpp.

389  {
390  // Keep kPBColors in sync with PolyBlockType.
391  const ScrollView::Color kPBColors[PT_COUNT] = {
392  ScrollView::WHITE, // Type is not yet known. Keep as the 1st element.
393  ScrollView::BLUE, // Text that lives inside a column.
394  ScrollView::CYAN, // Text that spans more than one column.
395  ScrollView::MEDIUM_BLUE, // Text that is in a cross-column pull-out
396  // region.
397  ScrollView::AQUAMARINE, // Partition belonging to an equation region.
398  ScrollView::SKY_BLUE, // Partition belonging to an inline equation
399  // region.
400  ScrollView::MAGENTA, // Partition belonging to a table region.
401  ScrollView::GREEN, // Text-line runs vertically.
402  ScrollView::LIGHT_BLUE, // Text that belongs to an image.
403  ScrollView::RED, // Image that lives inside a column.
404  ScrollView::YELLOW, // Image that spans more than one column.
405  ScrollView::ORANGE, // Image in a cross-column pull-out region.
406  ScrollView::BROWN, // Horizontal Line.
407  ScrollView::DARK_GREEN, // Vertical Line.
408  ScrollView::GREY // Lies outside of any column.
409  };
410  if (type < PT_COUNT) {
411  return kPBColors[type];
412  }
413  return ScrollView::WHITE;
414 }

◆ compute_bb()

void tesseract::POLY_BLOCK::compute_bb ( )

Definition at line 68 of file polyblk.cpp.

68  { // constructor
69  ICOORD ibl, itr; // integer bb
70  ICOORD botleft; // bounding box
71  ICOORD topright;
72  ICOORD pos; // current pos;
73  ICOORDELT_IT pts = &vertices; // iterator
74 
75  botleft = *pts.data();
76  topright = botleft;
77  do {
78  pos = *pts.data();
79  if (pos.x() < botleft.x()) {
80  // get bounding box
81  botleft = ICOORD(pos.x(), botleft.y());
82  }
83  if (pos.y() < botleft.y()) {
84  botleft = ICOORD(botleft.x(), pos.y());
85  }
86  if (pos.x() > topright.x()) {
87  topright = ICOORD(pos.x(), topright.y());
88  }
89  if (pos.y() > topright.y()) {
90  topright = ICOORD(topright.x(), pos.y());
91  }
92  pts.forward();
93  } while (!pts.at_first());
94  ibl = ICOORD(botleft.x(), botleft.y());
95  itr = ICOORD(topright.x(), topright.y());
96  box = TBOX(ibl, itr);
97 }
@ TBOX

◆ contains()

bool tesseract::POLY_BLOCK::contains ( POLY_BLOCK other)
Returns
true if other is inside this.

Definition at line 143 of file polyblk.cpp.

143  {
144  int16_t count; // winding count
145  ICOORDELT_IT it = &vertices; // iterator
146  ICOORD vertex;
147 
148  if (!box.overlap(*(other->bounding_box()))) {
149  return false; // can't be contained
150  }
151 
152  /* check that no vertex of this is inside other */
153 
154  do {
155  vertex = *it.data();
156  // get winding number
157  count = other->winding_number(vertex);
158  if (count != INTERSECTING) {
159  if (count != 0) {
160  return false;
161  }
162  }
163  it.forward();
164  } while (!it.at_first());
165 
166  /* check that all vertices of other are inside this */
167 
168  // switch lists
169  it.set_to_list(other->points());
170  do {
171  vertex = *it.data();
172  // try other way round
173  count = winding_number(vertex);
174  if (count != INTERSECTING) {
175  if (count == 0) {
176  return false;
177  }
178  }
179  it.forward();
180  } while (!it.at_first());
181  return true;
182 }
#define INTERSECTING
Definition: polyblk.cpp:35
int16_t winding_number(const ICOORD &test_pt)
Definition: polyblk.cpp:106
bool overlap(const TBOX &box) const
Definition: rect.h:363

◆ fill()

void tesseract::POLY_BLOCK::fill ( ScrollView window,
ScrollView::Color  colour 
)

Definition at line 272 of file polyblk.cpp.

272  {
273  ICOORDELT_IT s_it;
274 
275  std::unique_ptr<PB_LINE_IT> lines(new PB_LINE_IT(this));
276  window->Pen(colour);
277 
278  for (auto y = this->bounding_box()->bottom(); y <= this->bounding_box()->top(); y++) {
279  const std::unique_ptr</*non-const*/ ICOORDELT_LIST> segments(lines->get_line(y));
280  if (!segments->empty()) {
281  s_it.set_to_list(segments.get());
282  for (s_it.mark_cycle_pt(); !s_it.cycled_list(); s_it.forward()) {
283  // Note different use of ICOORDELT, x coord is x coord of pixel
284  // at the start of line segment, y coord is length of line segment
285  // Last pixel is start pixel + length.
286  auto width = s_it.data()->y();
287  window->SetCursor(s_it.data()->x(), y);
288  window->DrawTo(s_it.data()->x() + static_cast<float>(width), y);
289  }
290  }
291  }
292 }
TBOX * bounding_box()
Definition: polyblk.h:38
TDimension top() const
Definition: rect.h:68

◆ isA()

PolyBlockType tesseract::POLY_BLOCK::isA ( ) const
inline

Definition at line 48 of file polyblk.h.

48  {
49  return type;
50  }

◆ IsText()

bool tesseract::POLY_BLOCK::IsText ( ) const
inline

Definition at line 52 of file polyblk.h.

52  {
53  return PTIsTextType(type);
54  }
bool PTIsTextType(PolyBlockType type)
Definition: publictypes.h:82

◆ move()

void tesseract::POLY_BLOCK::move ( ICOORD  shift)

POLY_BLOCK::move

Move the POLY_BLOCK.

Parameters
shiftx,y translation vector

Definition at line 233 of file polyblk.cpp.

233  {
234  ICOORDELT *pt; // current point
235  ICOORDELT_IT pts = &vertices; // iterator
236 
237  do {
238  pt = pts.data();
239  *pt += shift;
240  pts.forward();
241  } while (!pts.at_first());
242  compute_bb();
243 }

◆ overlap()

bool tesseract::POLY_BLOCK::overlap ( POLY_BLOCK other)
Returns
true if the polygons of other and this overlap.

Definition at line 296 of file polyblk.cpp.

296  {
297  int16_t count; // winding count
298  ICOORDELT_IT it = &vertices; // iterator
299  ICOORD vertex;
300 
301  if (!box.overlap(*(other->bounding_box()))) {
302  return false; // can't be any overlap.
303  }
304 
305  /* see if a vertex of this is inside other */
306 
307  do {
308  vertex = *it.data();
309  // get winding number
310  count = other->winding_number(vertex);
311  if (count != INTERSECTING) {
312  if (count != 0) {
313  return true;
314  }
315  }
316  it.forward();
317  } while (!it.at_first());
318 
319  /* see if a vertex of other is inside this */
320 
321  // switch lists
322  it.set_to_list(other->points());
323  do {
324  vertex = *it.data();
325  // try other way round
326  count = winding_number(vertex);
327  if (count != INTERSECTING) {
328  if (count != 0) {
329  return true;
330  }
331  }
332  it.forward();
333  } while (!it.at_first());
334  return false;
335 }

◆ plot()

void tesseract::POLY_BLOCK::plot ( ScrollView window,
int32_t  num 
)

Definition at line 246 of file polyblk.cpp.

246  {
247  ICOORDELT_IT v = &vertices;
248 
249  window->Pen(ColorForPolyBlockType(type));
250 
251  v.move_to_first();
252 
253  if (num > 0) {
254  window->TextAttributes("Times", 80, false, false, false);
255  char temp_buff[34];
256 # if !defined(_WIN32) || defined(__MINGW32__)
257  snprintf(temp_buff, sizeof(temp_buff), "%" PRId32, num);
258 # else
259  _ltoa(num, temp_buff, 10);
260 # endif
261  window->Text(v.data()->x(), v.data()->y(), temp_buff);
262  }
263 
264  window->SetCursor(v.data()->x(), v.data()->y());
265  for (v.mark_cycle_pt(); !v.cycled_list(); v.forward()) {
266  window->DrawTo(v.data()->x(), v.data()->y());
267  }
268  v.move_to_first();
269  window->DrawTo(v.data()->x(), v.data()->y());
270 }
static ScrollView::Color ColorForPolyBlockType(PolyBlockType type)
Returns a color to draw the given type.
Definition: polyblk.cpp:389

◆ points()

ICOORDELT_LIST* tesseract::POLY_BLOCK::points ( )
inline

Definition at line 42 of file polyblk.h.

42  { // access function
43  return &vertices;
44  }

◆ reflect_in_y_axis()

void tesseract::POLY_BLOCK::reflect_in_y_axis ( )

Definition at line 214 of file polyblk.cpp.

214  {
215  ICOORDELT *pt; // current point
216  ICOORDELT_IT pts = &vertices; // Iterator.
217 
218  do {
219  pt = pts.data();
220  pt->set_x(-pt->x());
221  pts.forward();
222  } while (!pts.at_first());
223  compute_bb();
224 }

◆ rotate()

void tesseract::POLY_BLOCK::rotate ( FCOORD  rotation)

Definition at line 191 of file polyblk.cpp.

191  {
192  FCOORD pos; // current pos;
193  ICOORDELT *pt; // current point
194  ICOORDELT_IT pts = &vertices; // iterator
195 
196  do {
197  pt = pts.data();
198  pos.set_x(pt->x());
199  pos.set_y(pt->y());
200  pos.rotate(rotation);
201  pt->set_x(static_cast<TDimension>(floor(pos.x() + 0.5)));
202  pt->set_y(static_cast<TDimension>(floor(pos.y() + 0.5)));
203  pts.forward();
204  } while (!pts.at_first());
205  compute_bb();
206 }
int16_t TDimension
Definition: tesstypes.h:32

◆ winding_number()

int16_t tesseract::POLY_BLOCK::winding_number ( const ICOORD test_pt)

Definition at line 106 of file polyblk.cpp.

106  {
107  int16_t count; // winding count
108  ICOORD pt; // current point
109  ICOORD vec; // point to current point
110  ICOORD vvec; // current point to next point
111  int32_t cross; // cross product
112  ICOORDELT_IT it = &vertices; // iterator
113 
114  count = 0;
115  do {
116  pt = *it.data();
117  vec = pt - point;
118  vvec = *it.data_relative(1) - pt;
119  // crossing the line
120  if (vec.y() <= 0 && vec.y() + vvec.y() > 0) {
121  cross = vec * vvec; // cross product
122  if (cross > 0) {
123  count++; // crossing right half
124  } else if (cross == 0) {
125  return INTERSECTING; // going through point
126  }
127  } else if (vec.y() > 0 && vec.y() + vvec.y() <= 0) {
128  cross = vec * vvec;
129  if (cross < 0) {
130  count--; // crossing back
131  } else if (cross == 0) {
132  return INTERSECTING; // illegal
133  }
134  } else if (vec.y() == 0 && vec.x() == 0) {
135  return INTERSECTING;
136  }
137  it.forward();
138  } while (!it.at_first());
139  return count; // winding number
140 }

The documentation for this class was generated from the following files: