tesseract  4.0.0-1-g2a2b
polyblk.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: polyblk.cpp (Formerly poly_block.c)
3  * Description: Polygonal blocks
4  *
5  * (C) Copyright 1993, Hewlett-Packard Ltd.
6  ** Licensed under the Apache License, Version 2.0 (the "License");
7  ** you may not use this file except in compliance with the License.
8  ** You may obtain a copy of the License at
9  ** http://www.apache.org/licenses/LICENSE-2.0
10  ** Unless required by applicable law or agreed to in writing, software
11  ** distributed under the License is distributed on an "AS IS" BASIS,
12  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  ** See the License for the specific language governing permissions and
14  ** limitations under the License.
15  *
16  **********************************************************************/
17 
18 #include "polyblk.h"
19 #include <cctype>
20 #include <cmath>
21 #include <cstdio>
22 #include <memory> // std::unique_ptr
23 #include "elst.h"
24 
25 // Include automatically generated configuration file if running autoconf.
26 #ifdef HAVE_CONFIG_H
27 #include "config_auto.h"
28 #endif
29 
30 #define INTERSECTING INT16_MAX
31 
32 int lessthan(const void *first, const void *second);
33 
34 POLY_BLOCK::POLY_BLOCK(ICOORDELT_LIST *points, PolyBlockType t) {
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 }
43 
44 // Initialize from box coordinates.
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 }
56 
63 void POLY_BLOCK::compute_bb() { //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 }
90 
91 
99 int16_t POLY_BLOCK::winding_number(const ICOORD &point) {
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 }
134 
135 
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 }
174 
175 
183 void POLY_BLOCK::rotate(FCOORD rotation) {
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 }
200 
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 }
219 
220 
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 }
240 
241 
242 #ifndef GRAPHICS_DISABLED
243 void POLY_BLOCK::plot(ScrollView* window, int32_t num) {
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 }
268 
269 
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 }
298 #endif
299 
300 
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 }
339 
340 
341 ICOORDELT_LIST *PB_LINE_IT::get_line(int16_t y) {
342  ICOORDELT_IT v, r;
343  ICOORDELT_LIST *result;
344  ICOORDELT *x, *current, *previous;
345  float fy = y + 0.5f;
346  result = new ICOORDELT_LIST ();
347  r.set_to_list (result);
348  v.set_to_list (block->points ());
349 
350  for (v.mark_cycle_pt (); !v.cycled_list (); v.forward ()) {
351  if (((v.data_relative (-1)->y () > y) && (v.data ()->y () <= y))
352  || ((v.data_relative (-1)->y () <= y) && (v.data ()->y () > y))) {
353  previous = v.data_relative (-1);
354  current = v.data ();
355  float fx = 0.5f + previous->x() +
356  (current->x() - previous->x()) * (fy - previous->y()) /
357  (current->y() - previous->y());
358  x = new ICOORDELT(static_cast<int16_t>(fx), 0);
359  r.add_to_end (x);
360  }
361  }
362 
363  if (!r.empty ()) {
364  r.sort (lessthan);
365  for (r.mark_cycle_pt (); !r.cycled_list (); r.forward ())
366  x = r.data ();
367  for (r.mark_cycle_pt (); !r.cycled_list (); r.forward ()) {
368  r.data ()->set_y (r.data_relative (1)->x () - r.data ()->x ());
369  r.forward ();
370  delete (r.extract ());
371  }
372  }
373 
374  return result;
375 }
376 
377 
378 int lessthan(const void *first, const void *second) {
379  const ICOORDELT *p1 = *reinterpret_cast<const ICOORDELT* const*>(first);
380  const ICOORDELT *p2 = *reinterpret_cast<const ICOORDELT* const*>(second);
381 
382  if (p1->x () < p2->x ())
383  return (-1);
384  else if (p1->x () > p2->x ())
385  return (1);
386  else
387  return (0);
388 }
389 
390 #ifndef GRAPHICS_DISABLED
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 }
416 #endif // GRAPHICS_DISABLED
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 set_x(int16_t xin)
rewrite function
Definition: points.h:62
int count(LIST var_list)
Definition: oldlist.cpp:98
void rotate(const FCOORD vec)
Definition: points.h:764
int16_t y() const
access_function
Definition: points.h:57
Definition: rect.h:34
PolyBlockType
Definition: publictypes.h:53
void rotate(FCOORD rotation)
Definition: polyblk.cpp:183
void set_x(float xin)
rewrite function
Definition: points.h:215
void SetCursor(int x, int y)
Definition: scrollview.cpp:521
int lessthan(const void *first, const void *second)
Definition: polyblk.cpp:378
#define INTERSECTING
Definition: polyblk.cpp:30
static ScrollView::Color ColorForPolyBlockType(PolyBlockType type)
Returns a color to draw the given type.
Definition: polyblk.cpp:392
int16_t left() const
Definition: rect.h:72
int16_t top() const
Definition: rect.h:58
void fill(ScrollView *window, ScrollView::Color colour)
Definition: polyblk.cpp:270
Definition: capi.h:101
bool contains(POLY_BLOCK *other)
Definition: polyblk.cpp:137
integer coordinate
Definition: points.h:32
int16_t x() const
access function
Definition: points.h:53
TBOX * bounding_box()
Definition: polyblk.h:35
ICOORDELT_LIST * get_line(int16_t y)
Definition: polyblk.cpp:341
void Text(int x, int y, const char *mystring)
Definition: scrollview.cpp:654
void reflect_in_y_axis()
Definition: polyblk.cpp:207
bool overlap(POLY_BLOCK *other)
Definition: polyblk.cpp:302
void compute_bb()
Definition: polyblk.cpp:63
bool overlap(const TBOX &box) const
Definition: rect.h:355
int16_t winding_number(const ICOORD &test_pt)
Definition: polyblk.cpp:99
Definition: points.h:189
POLY_BLOCK()=default
int16_t right() const
Definition: rect.h:79
float x() const
Definition: points.h:208
ICOORDELT_LIST * points()
Definition: polyblk.h:39
void Pen(Color color)
Definition: scrollview.cpp:722
void set_y(int16_t yin)
rewrite function
Definition: points.h:66
void plot(ScrollView *window, int32_t num)
Definition: polyblk.cpp:243
int16_t bottom() const
Definition: rect.h:65
void set_y(float yin)
rewrite function
Definition: points.h:219
float y() const
Definition: points.h:211
void move(ICOORD shift)
Definition: polyblk.cpp:228