tesseract  4.0.0-1-g2a2b
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 27 of file polyblk.h.

Constructor & Destructor Documentation

◆ POLY_BLOCK() [1/3]

POLY_BLOCK::POLY_BLOCK ( )
default

◆ POLY_BLOCK() [2/3]

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

Definition at line 45 of file polyblk.cpp.

45  {
46  vertices.clear();
47  ICOORDELT_IT v = &vertices;
48  v.move_to_first();
49  v.add_to_end(new ICOORDELT(tbox.left(), tbox.top()));
50  v.add_to_end(new ICOORDELT(tbox.left(), tbox.bottom()));
51  v.add_to_end(new ICOORDELT(tbox.right(), tbox.bottom()));
52  v.add_to_end(new ICOORDELT(tbox.right(), tbox.top()));
53  compute_bb();
54  type = t;
55 }
int16_t left() const
Definition: rect.h:72
int16_t top() const
Definition: rect.h:58
void compute_bb()
Definition: polyblk.cpp:63
int16_t right() const
Definition: rect.h:79
int16_t bottom() const
Definition: rect.h:65

◆ POLY_BLOCK() [3/3]

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

Definition at line 34 of file polyblk.cpp.

34  {
35  ICOORDELT_IT v = &vertices;
36 
37  vertices.clear();
38  v.move_to_first();
39  v.add_list_before(points);
40  compute_bb();
41  type = t;
42 }
void compute_bb()
Definition: polyblk.cpp:63
ICOORDELT_LIST * points()
Definition: polyblk.h:39

◆ ~POLY_BLOCK()

POLY_BLOCK::~POLY_BLOCK ( )
default

Member Function Documentation

◆ bounding_box()

TBOX* POLY_BLOCK::bounding_box ( )
inline

Definition at line 35 of file polyblk.h.

35  { // access function
36  return &box;
37  }

◆ ColorForPolyBlockType()

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

Returns a color to draw the given type.

Definition at line 392 of file polyblk.cpp.

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

◆ compute_bb()

void POLY_BLOCK::compute_bb ( )

Definition at line 63 of file polyblk.cpp.

63  { //constructor
64  ICOORD ibl, itr; //integer bb
65  ICOORD botleft; //bounding box
66  ICOORD topright;
67  ICOORD pos; //current pos;
68  ICOORDELT_IT pts = &vertices; //iterator
69 
70  botleft = *pts.data ();
71  topright = botleft;
72  do {
73  pos = *pts.data ();
74  if (pos.x () < botleft.x ())
75  //get bounding box
76  botleft = ICOORD (pos.x (), botleft.y ());
77  if (pos.y () < botleft.y ())
78  botleft = ICOORD (botleft.x (), pos.y ());
79  if (pos.x () > topright.x ())
80  topright = ICOORD (pos.x (), topright.y ());
81  if (pos.y () > topright.y ())
82  topright = ICOORD (topright.x (), pos.y ());
83  pts.forward ();
84  }
85  while (!pts.at_first ());
86  ibl = ICOORD (botleft.x (), botleft.y ());
87  itr = ICOORD (topright.x (), topright.y ());
88  box = TBOX (ibl, itr);
89 }
int16_t y() const
access_function
Definition: points.h:57
Definition: rect.h:34
integer coordinate
Definition: points.h:32
int16_t x() const
access function
Definition: points.h:53

◆ contains()

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

Definition at line 137 of file polyblk.cpp.

137  {
138  int16_t count; // winding count
139  ICOORDELT_IT it = &vertices; // iterator
140  ICOORD vertex;
141 
142  if (!box.overlap (*(other->bounding_box ())))
143  return false; // can't be contained
144 
145  /* check that no vertex of this is inside other */
146 
147  do {
148  vertex = *it.data ();
149  // get winding number
150  count = other->winding_number (vertex);
151  if (count != INTERSECTING)
152  if (count != 0)
153  return false;
154  it.forward ();
155  }
156  while (!it.at_first ());
157 
158  /* check that all vertices of other are inside this */
159 
160  //switch lists
161  it.set_to_list (other->points ());
162  do {
163  vertex = *it.data ();
164  //try other way round
165  count = winding_number (vertex);
166  if (count != INTERSECTING)
167  if (count == 0)
168  return false;
169  it.forward ();
170  }
171  while (!it.at_first ());
172  return true;
173 }
int count(LIST var_list)
Definition: oldlist.cpp:98
#define INTERSECTING
Definition: polyblk.cpp:30
integer coordinate
Definition: points.h:32
TBOX * bounding_box()
Definition: polyblk.h:35
bool overlap(const TBOX &box) const
Definition: rect.h:355
int16_t winding_number(const ICOORD &test_pt)
Definition: polyblk.cpp:99
ICOORDELT_LIST * points()
Definition: polyblk.h:39

◆ fill()

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

Definition at line 270 of file polyblk.cpp.

270  {
271  int16_t y;
272  int16_t width;
273  PB_LINE_IT *lines;
274  ICOORDELT_IT s_it;
275 
276  lines = new PB_LINE_IT (this);
277  window->Pen(colour);
278 
279  for (y = this->bounding_box ()->bottom ();
280  y <= this->bounding_box ()->top (); y++) {
281  const std::unique_ptr</*non-const*/ ICOORDELT_LIST> segments(
282  lines->get_line(y));
283  if (!segments->empty ()) {
284  s_it.set_to_list(segments.get());
285  for (s_it.mark_cycle_pt (); !s_it.cycled_list (); s_it.forward ()) {
286  // Note different use of ICOORDELT, x coord is x coord of pixel
287  // at the start of line segment, y coord is length of line segment
288  // Last pixel is start pixel + length.
289  width = s_it.data ()->y ();
290  window->SetCursor(s_it.data ()->x (), y);
291  window->DrawTo(s_it.data()->x() + static_cast<float>(width), y);
292  }
293  }
294  }
295 
296  delete lines;
297 }
void DrawTo(int x, int y)
Definition: scrollview.cpp:527
void SetCursor(int x, int y)
Definition: scrollview.cpp:521
int16_t top() const
Definition: rect.h:58
TBOX * bounding_box()
Definition: polyblk.h:35
ICOORDELT_LIST * get_line(int16_t y)
Definition: polyblk.cpp:341
void Pen(Color color)
Definition: scrollview.cpp:722

◆ isA()

PolyBlockType POLY_BLOCK::isA ( ) const
inline

Definition at line 45 of file polyblk.h.

45  {
46  return type;
47  }

◆ IsText()

bool POLY_BLOCK::IsText ( ) const
inline

Definition at line 49 of file polyblk.h.

49  {
50  return PTIsTextType(type);
51  }
bool PTIsTextType(PolyBlockType type)
Definition: publictypes.h:82

◆ move()

void POLY_BLOCK::move ( ICOORD  shift)

POLY_BLOCK::move

Move the POLY_BLOCK.

Parameters
shiftx,y translation vector

Definition at line 228 of file polyblk.cpp.

228  {
229  ICOORDELT *pt; //current point
230  ICOORDELT_IT pts = &vertices; //iterator
231 
232  do {
233  pt = pts.data ();
234  *pt += shift;
235  pts.forward ();
236  }
237  while (!pts.at_first ());
238  compute_bb();
239 }
void compute_bb()
Definition: polyblk.cpp:63

◆ overlap()

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

Definition at line 302 of file polyblk.cpp.

302  {
303  int16_t count; // winding count
304  ICOORDELT_IT it = &vertices; // iterator
305  ICOORD vertex;
306 
307  if (!box.overlap(*(other->bounding_box())))
308  return false; // can't be any overlap.
309 
310  /* see if a vertex of this is inside other */
311 
312  do {
313  vertex = *it.data ();
314  // get winding number
315  count = other->winding_number (vertex);
316  if (count != INTERSECTING)
317  if (count != 0)
318  return true;
319  it.forward ();
320  }
321  while (!it.at_first ());
322 
323  /* see if a vertex of other is inside this */
324 
325  // switch lists
326  it.set_to_list (other->points ());
327  do {
328  vertex = *it.data();
329  // try other way round
330  count = winding_number (vertex);
331  if (count != INTERSECTING)
332  if (count != 0)
333  return true;
334  it.forward ();
335  }
336  while (!it.at_first ());
337  return false;
338 }
int count(LIST var_list)
Definition: oldlist.cpp:98
#define INTERSECTING
Definition: polyblk.cpp:30
integer coordinate
Definition: points.h:32
TBOX * bounding_box()
Definition: polyblk.h:35
bool overlap(const TBOX &box) const
Definition: rect.h:355
int16_t winding_number(const ICOORD &test_pt)
Definition: polyblk.cpp:99
ICOORDELT_LIST * points()
Definition: polyblk.h:39

◆ plot()

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

Definition at line 243 of file polyblk.cpp.

243  {
244  ICOORDELT_IT v = &vertices;
245 
246  window->Pen(ColorForPolyBlockType(type));
247 
248  v.move_to_first ();
249 
250  if (num > 0) {
251  window->TextAttributes("Times", 80, false, false, false);
252  char temp_buff[34];
253 #if !defined(_WIN32) || defined(__MINGW32__)
254  snprintf(temp_buff, sizeof(temp_buff), "%" PRId32, num);
255 #else
256  ltoa (num, temp_buff, 10);
257 #endif
258  window->Text(v.data ()->x (), v.data ()->y (), temp_buff);
259  }
260 
261  window->SetCursor(v.data ()->x (), v.data ()->y ());
262  for (v.mark_cycle_pt (); !v.cycled_list (); v.forward ()) {
263  window->DrawTo(v.data ()->x (), v.data ()->y ());
264  }
265  v.move_to_first ();
266  window->DrawTo(v.data ()->x (), v.data ()->y ());
267 }
void DrawTo(int x, int y)
Definition: scrollview.cpp:527
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
Definition: scrollview.cpp:637
void SetCursor(int x, int y)
Definition: scrollview.cpp:521
static ScrollView::Color ColorForPolyBlockType(PolyBlockType type)
Returns a color to draw the given type.
Definition: polyblk.cpp:392
void Text(int x, int y, const char *mystring)
Definition: scrollview.cpp:654
void Pen(Color color)
Definition: scrollview.cpp:722

◆ points()

ICOORDELT_LIST* POLY_BLOCK::points ( )
inline

Definition at line 39 of file polyblk.h.

39  { // access function
40  return &vertices;
41  }

◆ reflect_in_y_axis()

void POLY_BLOCK::reflect_in_y_axis ( )

Definition at line 207 of file polyblk.cpp.

207  {
208  ICOORDELT *pt; // current point
209  ICOORDELT_IT pts = &vertices; // Iterator.
210 
211  do {
212  pt = pts.data();
213  pt->set_x(-pt->x());
214  pts.forward();
215  }
216  while (!pts.at_first());
217  compute_bb();
218 }
void set_x(int16_t xin)
rewrite function
Definition: points.h:62
int16_t x() const
access function
Definition: points.h:53
void compute_bb()
Definition: polyblk.cpp:63

◆ rotate()

void POLY_BLOCK::rotate ( FCOORD  rotation)

Definition at line 183 of file polyblk.cpp.

183  {
184  FCOORD pos; //current pos;
185  ICOORDELT *pt; //current point
186  ICOORDELT_IT pts = &vertices; //iterator
187 
188  do {
189  pt = pts.data ();
190  pos.set_x (pt->x ());
191  pos.set_y (pt->y ());
192  pos.rotate (rotation);
193  pt->set_x(static_cast<int16_t>(floor(pos.x() + 0.5)));
194  pt->set_y(static_cast<int16_t>(floor(pos.y() + 0.5)));
195  pts.forward ();
196  }
197  while (!pts.at_first ());
198  compute_bb();
199 }
void set_x(int16_t xin)
rewrite function
Definition: points.h:62
void rotate(const FCOORD vec)
Definition: points.h:764
int16_t y() const
access_function
Definition: points.h:57
void set_x(float xin)
rewrite function
Definition: points.h:215
int16_t x() const
access function
Definition: points.h:53
void compute_bb()
Definition: polyblk.cpp:63
Definition: points.h:189
float x() const
Definition: points.h:208
void set_y(int16_t yin)
rewrite function
Definition: points.h:66
void set_y(float yin)
rewrite function
Definition: points.h:219
float y() const
Definition: points.h:211

◆ winding_number()

int16_t POLY_BLOCK::winding_number ( const ICOORD test_pt)

Definition at line 99 of file polyblk.cpp.

99  {
100  int16_t count; //winding count
101  ICOORD pt; //current point
102  ICOORD vec; //point to current point
103  ICOORD vvec; //current point to next point
104  int32_t cross; //cross product
105  ICOORDELT_IT it = &vertices; //iterator
106 
107  count = 0;
108  do {
109  pt = *it.data ();
110  vec = pt - point;
111  vvec = *it.data_relative (1) - pt;
112  //crossing the line
113  if (vec.y () <= 0 && vec.y () + vvec.y () > 0) {
114  cross = vec * vvec; //cross product
115  if (cross > 0)
116  count++; //crossing right half
117  else if (cross == 0)
118  return INTERSECTING; //going through point
119  }
120  else if (vec.y () > 0 && vec.y () + vvec.y () <= 0) {
121  cross = vec * vvec;
122  if (cross < 0)
123  count--; //crossing back
124  else if (cross == 0)
125  return INTERSECTING; //illegal
126  }
127  else if (vec.y () == 0 && vec.x () == 0)
128  return INTERSECTING;
129  it.forward ();
130  }
131  while (!it.at_first ());
132  return count; //winding number
133 }
int count(LIST var_list)
Definition: oldlist.cpp:98
int16_t y() const
access_function
Definition: points.h:57
#define INTERSECTING
Definition: polyblk.cpp:30
integer coordinate
Definition: points.h:32
int16_t x() const
access function
Definition: points.h:53

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