tesseract  4.00.00dev
POLY_BLOCK Class Reference

#include <polyblk.h>

Public Member Functions

 POLY_BLOCK ()
 
 POLY_BLOCK (const TBOX &box, PolyBlockType type)
 
 POLY_BLOCK (ICOORDELT_LIST *points, PolyBlockType type)
 
 ~POLY_BLOCK ()
 
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 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 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 28 of file polyblk.h.

Constructor & Destructor Documentation

◆ POLY_BLOCK() [1/3]

POLY_BLOCK::POLY_BLOCK ( )
inline

Definition at line 30 of file polyblk.h.

30  {
31  }

◆ POLY_BLOCK() [2/3]

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

Definition at line 48 of file polyblk.cpp.

48  {
49  vertices.clear();
50  ICOORDELT_IT v = &vertices;
51  v.move_to_first();
52  v.add_to_end(new ICOORDELT(box.left(), box.top()));
53  v.add_to_end(new ICOORDELT(box.left(), box.bottom()));
54  v.add_to_end(new ICOORDELT(box.right(), box.bottom()));
55  v.add_to_end(new ICOORDELT(box.right(), box.top()));
56  compute_bb();
57  type = t;
58 }
void compute_bb()
Definition: polyblk.cpp:66
inT16 left() const
Definition: rect.h:68
inT16 top() const
Definition: rect.h:54
inT16 right() const
Definition: rect.h:75
inT16 bottom() const
Definition: rect.h:61
double v[max]

◆ POLY_BLOCK() [3/3]

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

Definition at line 37 of file polyblk.cpp.

37  {
38  ICOORDELT_IT v = &vertices;
39 
40  vertices.clear();
41  v.move_to_first();
42  v.add_list_before(points);
43  compute_bb();
44  type = t;
45 }
ICOORDELT_LIST * points()
Definition: polyblk.h:42
void compute_bb()
Definition: polyblk.cpp:66
double v[max]

◆ ~POLY_BLOCK()

POLY_BLOCK::~POLY_BLOCK ( )
inline

Definition at line 35 of file polyblk.h.

35  {
36  }

Member Function Documentation

◆ bounding_box()

TBOX* POLY_BLOCK::bounding_box ( )
inline

Definition at line 38 of file polyblk.h.

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

◆ ColorForPolyBlockType()

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

Returns a color to draw the given type.

Definition at line 397 of file polyblk.cpp.

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

◆ compute_bb()

void POLY_BLOCK::compute_bb ( )

Definition at line 66 of file polyblk.cpp.

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

◆ contains()

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

Definition at line 140 of file polyblk.cpp.

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

◆ fill()

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

Definition at line 273 of file polyblk.cpp.

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

◆ isA()

PolyBlockType POLY_BLOCK::isA ( ) const
inline

Definition at line 48 of file polyblk.h.

48  {
49  return type;
50  }

◆ IsText()

bool 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:70

◆ move()

void POLY_BLOCK::move ( ICOORD  shift)

POLY_BLOCK::move

Move the POLY_BLOCK.

Parameters
shiftx,y translation vector

Definition at line 231 of file polyblk.cpp.

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

◆ overlap()

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

Definition at line 304 of file polyblk.cpp.

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

◆ plot()

void POLY_BLOCK::plot ( ScrollView window,
inT32  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(__UNIX__) || defined(MINGW)
257  sprintf(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 }
void SetCursor(int x, int y)
Definition: scrollview.cpp:525
static ScrollView::Color ColorForPolyBlockType(PolyBlockType type)
Returns a color to draw the given type.
Definition: polyblk.cpp:397
void Text(int x, int y, const char *mystring)
Definition: scrollview.cpp:658
double v[max]
void Pen(Color color)
Definition: scrollview.cpp:726
void DrawTo(int x, int y)
Definition: scrollview.cpp:531
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
Definition: scrollview.cpp:641

◆ points()

ICOORDELT_LIST* POLY_BLOCK::points ( )
inline

Definition at line 42 of file polyblk.h.

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

◆ reflect_in_y_axis()

void POLY_BLOCK::reflect_in_y_axis ( )

Definition at line 210 of file polyblk.cpp.

210  {
211  ICOORDELT *pt; // current point
212  ICOORDELT_IT pts = &vertices; // Iterator.
213 
214  do {
215  pt = pts.data();
216  pt->set_x(-pt->x());
217  pts.forward();
218  }
219  while (!pts.at_first());
220  compute_bb();
221 }
void set_x(inT16 xin)
rewrite function
Definition: points.h:61
void compute_bb()
Definition: polyblk.cpp:66
inT16 x() const
access function
Definition: points.h:52

◆ rotate()

void POLY_BLOCK::rotate ( FCOORD  rotation)

Definition at line 186 of file polyblk.cpp.

186  {
187  FCOORD pos; //current pos;
188  ICOORDELT *pt; //current point
189  ICOORDELT_IT pts = &vertices; //iterator
190 
191  do {
192  pt = pts.data ();
193  pos.set_x (pt->x ());
194  pos.set_y (pt->y ());
195  pos.rotate (rotation);
196  pt->set_x ((inT16) (floor (pos.x () + 0.5)));
197  pt->set_y ((inT16) (floor (pos.y () + 0.5)));
198  pts.forward ();
199  }
200  while (!pts.at_first ());
201  compute_bb();
202 }
Definition: points.h:189
void rotate(const FCOORD vec)
Definition: ipoints.h:471
void set_x(inT16 xin)
rewrite function
Definition: points.h:61
void compute_bb()
Definition: polyblk.cpp:66
inT16 x() const
access function
Definition: points.h:52
void set_x(float xin)
rewrite function
Definition: points.h:216
void set_y(float yin)
rewrite function
Definition: points.h:220
int16_t inT16
Definition: host.h:36
inT16 y() const
access_function
Definition: points.h:56
float y() const
Definition: points.h:212
float x() const
Definition: points.h:209
void set_y(inT16 yin)
rewrite function
Definition: points.h:65

◆ winding_number()

inT16 POLY_BLOCK::winding_number ( const ICOORD test_pt)

Definition at line 102 of file polyblk.cpp.

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

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