All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
split.cpp
Go to the documentation of this file.
1 /* -*-C-*-
2  ********************************************************************************
3  *
4  * File: split.c (Formerly split.c)
5  * Description:
6  * Author: Mark Seaman, OCR Technology
7  * Created: Fri Oct 16 14:37:00 1987
8  * Modified: Fri May 17 16:27:49 1991 (Mark Seaman) marks@hpgrlt
9  * Language: C
10  * Package: N/A
11  * Status: Reusable Software Component
12  *
13  * (c) Copyright 1987, Hewlett-Packard Company.
14  ** Licensed under the Apache License, Version 2.0 (the "License");
15  ** you may not use this file except in compliance with the License.
16  ** You may obtain a copy of the License at
17  ** http://www.apache.org/licenses/LICENSE-2.0
18  ** Unless required by applicable law or agreed to in writing, software
19  ** distributed under the License is distributed on an "AS IS" BASIS,
20  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  ** See the License for the specific language governing permissions and
22  ** limitations under the License.
23  *
24  *************************************************************************/
25 /*----------------------------------------------------------------------
26  I n c l u d e s
27 ----------------------------------------------------------------------*/
28 #include "split.h"
29 #include "coutln.h"
30 #include "tprintf.h"
31 
32 #ifdef __UNIX__
33 #include <assert.h>
34 #endif
35 
36 /*----------------------------------------------------------------------
37  V a r i a b l e s
38 ----------------------------------------------------------------------*/
39 // Limit on the amount of penalty for the chop being off-center.
40 const int kCenterGradeCap = 25;
41 // Ridiculously large priority for splits that are no use.
42 const double kBadPriority = 999.0;
43 
44 BOOL_VAR(wordrec_display_splits, 0, "Display splits");
45 
46 // Returns the bounding box of all the points in the split.
48  return TBOX(
51 }
52 
53 // Hides the SPLIT so the outlines appear not to be cut by it.
54 void SPLIT::Hide() const {
55  EDGEPT* edgept = point1;
56  do {
57  edgept->Hide();
58  edgept = edgept->next;
59  } while (!edgept->EqualPos(*point2) && edgept != point1);
60  edgept = point2;
61  do {
62  edgept->Hide();
63  edgept = edgept->next;
64  } while (!edgept->EqualPos(*point1) && edgept != point2);
65 }
66 
67 // Undoes hide, so the outlines are cut by the SPLIT.
68 void SPLIT::Reveal() const {
69  EDGEPT* edgept = point1;
70  do {
71  edgept->Reveal();
72  edgept = edgept->next;
73  } while (!edgept->EqualPos(*point2) && edgept != point1);
74  edgept = point2;
75  do {
76  edgept->Reveal();
77  edgept = edgept->next;
78  } while (!edgept->EqualPos(*point1) && edgept != point2);
79 }
80 
81 // Compute a split priority based on the bounding boxes of the parts.
82 // The arguments here are config parameters defined in Wordrec. Add chop_
83 // to the beginning of the name.
84 float SPLIT::FullPriority(int xmin, int xmax, double overlap_knob,
85  int centered_maxwidth, double center_knob,
86  double width_change_knob) const {
87  TBOX box1 = Box12();
88  TBOX box2 = Box21();
89  int min_left = MIN(box1.left(), box2.left());
90  int max_right = MAX(box1.right(), box2.right());
91  if (xmin < min_left && xmax > max_right) return kBadPriority;
92 
93  float grade = 0.0f;
94  // grade_overlap.
95  int width1 = box1.width();
96  int width2 = box2.width();
97  int min_width = MIN(width1, width2);
98  int overlap = -box1.x_gap(box2);
99  if (overlap == min_width) {
100  grade += 100.0f; // Total overlap.
101  } else {
102  if (2 * overlap > min_width) overlap += 2 * overlap - min_width;
103  if (overlap > 0) grade += overlap_knob * overlap;
104  }
105  // grade_center_of_blob.
106  if (width1 <= centered_maxwidth || width2 <= centered_maxwidth) {
107  grade += MIN(kCenterGradeCap, center_knob * abs(width1 - width2));
108  }
109  // grade_width_change.
110  float width_change_grade = 20 - (max_right - min_left - MAX(width1, width2));
111  if (width_change_grade > 0.0f)
112  grade += width_change_grade * width_change_knob;
113  return grade;
114 }
115 
116 // Returns true if *this SPLIT appears OK in the sense that it does not cross
117 // any outlines and does not chop off any ridiculously small pieces.
118 bool SPLIT::IsHealthy(const TBLOB& blob, int min_points, int min_area) const {
119  return !IsLittleChunk(min_points, min_area) &&
121 }
122 
123 // Returns true if the split generates a small chunk in terms of either area
124 // or number of points.
125 bool SPLIT::IsLittleChunk(int min_points, int min_area) const {
126  if (point1->ShortNonCircularSegment(min_points, point2) &&
127  point1->SegmentArea(point2) < min_area) {
128  return true;
129  }
130  if (point2->ShortNonCircularSegment(min_points, point1) &&
131  point2->SegmentArea(point1) < min_area) {
132  return true;
133  }
134  return false;
135 }
136 
137 /**********************************************************************
138  * make_edgept
139  *
140  * Create an EDGEPT and hook it into an existing list of edge points.
141  **********************************************************************/
142 EDGEPT *make_edgept(int x, int y, EDGEPT *next, EDGEPT *prev) {
143  EDGEPT *this_edgept;
144  /* Create point */
145  this_edgept = new EDGEPT;
146  this_edgept->pos.x = x;
147  this_edgept->pos.y = y;
148  // Now deal with the src_outline steps.
149  C_OUTLINE* prev_ol = prev->src_outline;
150  if (prev_ol != NULL && prev->next == next) {
151  // Compute the fraction of the segment that is being cut.
152  FCOORD segment_vec(next->pos.x - prev->pos.x, next->pos.y - prev->pos.y);
153  FCOORD target_vec(x - prev->pos.x, y - prev->pos.y);
154  double cut_fraction = target_vec.length() / segment_vec.length();
155  // Get the start and end at the step level.
156  ICOORD step_start = prev_ol->position_at_index(prev->start_step);
157  int end_step = prev->start_step + prev->step_count;
158  int step_length = prev_ol->pathlength();
159  ICOORD step_end = prev_ol->position_at_index(end_step % step_length);
160  ICOORD step_vec = step_end - step_start;
161  double target_length = step_vec.length() * cut_fraction;
162  // Find the point on the segment that gives the length nearest to target.
163  int best_step = prev->start_step;
164  ICOORD total_step(0, 0);
165  double best_dist = target_length;
166  for (int s = prev->start_step; s < end_step; ++s) {
167  total_step += prev_ol->step(s % step_length);
168  double dist = fabs(target_length - total_step.length());
169  if (dist < best_dist) {
170  best_dist = dist;
171  best_step = s + 1;
172  }
173  }
174  // The new point is an intermediate point.
175  this_edgept->src_outline = prev_ol;
176  this_edgept->step_count = end_step - best_step;
177  this_edgept->start_step = best_step % step_length;
178  prev->step_count = best_step - prev->start_step;
179  } else {
180  // The new point is poly only.
181  this_edgept->src_outline = NULL;
182  this_edgept->step_count = 0;
183  this_edgept->start_step = 0;
184  }
185  /* Hook it up */
186  this_edgept->next = next;
187  this_edgept->prev = prev;
188  prev->next = this_edgept;
189  next->prev = this_edgept;
190  /* Set up vec entries */
191  this_edgept->vec.x = this_edgept->next->pos.x - x;
192  this_edgept->vec.y = this_edgept->next->pos.y - y;
193  this_edgept->prev->vec.x = x - this_edgept->prev->pos.x;
194  this_edgept->prev->vec.y = y - this_edgept->prev->pos.y;
195  return this_edgept;
196 }
197 
198 /**********************************************************************
199  * remove_edgept
200  *
201  * Remove a given EDGEPT from its list and delete it.
202  **********************************************************************/
203 void remove_edgept(EDGEPT *point) {
204  EDGEPT *prev = point->prev;
205  EDGEPT *next = point->next;
206  // Add point's steps onto prev's steps if they are from the same outline.
207  if (prev->src_outline == point->src_outline && prev->src_outline != NULL) {
208  prev->step_count += point->step_count;
209  }
210  prev->next = next;
211  next->prev = prev;
212  prev->vec.x = next->pos.x - prev->pos.x;
213  prev->vec.y = next->pos.y - prev->pos.y;
214  delete point;
215 }
216 
217 /**********************************************************************
218  * Print
219  *
220  * Shows the coordinates of both points in a split.
221  **********************************************************************/
222 void SPLIT::Print() const {
223  if (this != NULL) {
224  tprintf("(%d,%d)--(%d,%d)", point1->pos.x, point1->pos.y, point2->pos.x,
225  point2->pos.y);
226  }
227 }
228 
229 #ifndef GRAPHICS_DISABLED
230 // Draws the split in the given window.
231 void SPLIT::Mark(ScrollView* window) const {
232  window->Pen(ScrollView::GREEN);
233  window->Line(point1->pos.x, point1->pos.y, point2->pos.x, point2->pos.y);
234  window->UpdateWindow();
235 }
236 #endif
237 
238 // Creates two outlines out of one by splitting the original one in half.
239 // Inserts the resulting outlines into the given list.
240 void SPLIT::SplitOutlineList(TESSLINE* outlines) const {
241  SplitOutline();
242  while (outlines->next != NULL) outlines = outlines->next;
243 
244  outlines->next = new TESSLINE;
245  outlines->next->loop = point1;
246  outlines->next->ComputeBoundingBox();
247 
248  outlines = outlines->next;
249 
250  outlines->next = new TESSLINE;
251  outlines->next->loop = point2;
252  outlines->next->ComputeBoundingBox();
253 
254  outlines->next->next = NULL;
255 }
256 
257 // Makes a split between these two edge points, but does not affect the
258 // outlines to which they belong.
259 void SPLIT::SplitOutline() const {
260  EDGEPT* temp2 = point2->next;
261  EDGEPT* temp1 = point1->next;
262  /* Create two new points */
263  EDGEPT* new_point1 = make_edgept(point1->pos.x, point1->pos.y, temp1, point2);
264  EDGEPT* new_point2 = make_edgept(point2->pos.x, point2->pos.y, temp2, point1);
265  // point1 and 2 are now cross-over points, so they must have NULL
266  // src_outlines and give their src_outline information their new
267  // replacements.
268  new_point1->src_outline = point1->src_outline;
269  new_point1->start_step = point1->start_step;
270  new_point1->step_count = point1->step_count;
271  new_point2->src_outline = point2->src_outline;
272  new_point2->start_step = point2->start_step;
273  new_point2->step_count = point2->step_count;
275  point1->start_step = 0;
276  point1->step_count = 0;
278  point2->start_step = 0;
279  point2->step_count = 0;
280 }
281 
282 // Undoes the effect of SplitOutlineList, correcting the outlines for undoing
283 // the split, but possibly leaving some duplicate outlines.
284 void SPLIT::UnsplitOutlineList(TBLOB* blob) const {
285  /* Modify edge points */
286  UnsplitOutlines();
287 
288  TESSLINE* outline1 = new TESSLINE;
289  outline1->next = blob->outlines;
290  blob->outlines = outline1;
291  outline1->loop = point1;
292 
293  TESSLINE* outline2 = new TESSLINE;
294  outline2->next = blob->outlines;
295  blob->outlines = outline2;
296  outline2->loop = point2;
297 }
298 
299 // Removes the split that was put between these two points.
301  EDGEPT* tmp1 = point1->next;
302  EDGEPT* tmp2 = point2->next;
303 
304  tmp1->next->prev = point2;
305  tmp2->next->prev = point1;
306 
307  // tmp2 is coincident with point1. point1 takes tmp2's place as tmp2 is
308  // deleted.
309  point1->next = tmp2->next;
310  point1->src_outline = tmp2->src_outline;
311  point1->start_step = tmp2->start_step;
312  point1->step_count = tmp2->step_count;
313  // Likewise point2 takes tmp1's place.
314  point2->next = tmp1->next;
315  point2->src_outline = tmp1->src_outline;
316  point2->start_step = tmp1->start_step;
317  point2->step_count = tmp1->step_count;
318 
319  delete tmp1;
320  delete tmp2;
321 
322  point1->vec.x = point1->next->pos.x - point1->pos.x;
323  point1->vec.y = point1->next->pos.y - point1->pos.y;
324 
325  point2->vec.x = point2->next->pos.x - point2->pos.x;
326  point2->vec.y = point2->next->pos.y - point2->pos.y;
327 }
bool ShortNonCircularSegment(int min_points, const EDGEPT *end) const
Definition: blobs.h:135
Definition: blobs.h:261
void Print() const
Definition: split.cpp:222
void Reveal() const
Definition: split.cpp:68
void Pen(Color color)
Definition: scrollview.cpp:726
bool wordrec_display_splits
Definition: split.cpp:44
int SegmentArea(const EDGEPT *end) const
Definition: blobs.h:122
float FullPriority(int xmin, int xmax, double overlap_knob, int centered_maxwidth, double center_knob, double width_change_knob) const
Definition: split.cpp:84
#define MAX(x, y)
Definition: ndminx.h:24
bool SegmentCrossesOutline(const TPOINT &pt1, const TPOINT &pt2) const
Definition: blobs.h:316
const int kCenterGradeCap
Definition: split.cpp:40
int start_step
Definition: blobs.h:173
EDGEPT * prev
Definition: blobs.h:170
#define tprintf(...)
Definition: tprintf.h:31
#define MIN(x, y)
Definition: ndminx.h:28
void remove_edgept(EDGEPT *point)
Definition: split.cpp:203
TBOX Box21() const
Definition: split.h:46
inT16 y
Definition: blobs.h:72
void SplitOutlineList(TESSLINE *outlines) const
Definition: split.cpp:240
#define BOOL_VAR(name, val, comment)
Definition: params.h:280
void UnsplitOutlineList(TBLOB *blob) const
Definition: split.cpp:284
TESSLINE * next
Definition: blobs.h:258
inT32 pathlength() const
Definition: coutln.h:133
void ComputeBoundingBox()
Definition: blobs.cpp:225
inT16 right() const
Definition: rect.h:75
void Hide() const
Definition: split.cpp:54
float length() const
find length
Definition: points.h:78
bool IsLittleChunk(int min_points, int min_area) const
Definition: split.cpp:125
void Reveal()
Definition: blobs.h:150
EDGEPT * point2
Definition: split.h:104
VECTOR vec
Definition: blobs.h:164
const double kBadPriority
Definition: split.cpp:42
inT16 left() const
Definition: rect.h:68
void SplitOutline() const
Definition: split.cpp:259
EDGEPT * next
Definition: blobs.h:169
void Mark(ScrollView *window) const
Definition: split.cpp:231
float length() const
find length
Definition: points.h:230
void UpdateWindow()
Definition: scrollview.cpp:710
void UnsplitOutlines() const
Definition: split.cpp:300
inT16 x
Definition: blobs.h:71
EDGEPT * make_edgept(int x, int y, EDGEPT *next, EDGEPT *prev)
Definition: split.cpp:142
int step_count
Definition: blobs.h:174
TBOX bounding_box() const
Definition: split.cpp:47
integer coordinate
Definition: points.h:30
C_OUTLINE * src_outline
Definition: blobs.h:171
bool IsHealthy(const TBLOB &blob, int min_points, int min_area) const
Definition: split.cpp:118
inT16 width() const
Definition: rect.h:111
bool EqualPos(const EDGEPT &other) const
Definition: blobs.h:105
TPOINT pos
Definition: blobs.h:163
int x_gap(const TBOX &box) const
Definition: rect.h:217
Definition: blobs.h:76
Definition: rect.h:30
ICOORD step(int index) const
Definition: coutln.h:142
#define NULL
Definition: host.h:144
ICOORD position_at_index(int index) const
Definition: coutln.h:151
TBOX Box12() const
Definition: split.h:44
TESSLINE * outlines
Definition: blobs.h:377
void Hide()
Definition: blobs.h:147
EDGEPT * loop
Definition: blobs.h:257
void Line(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:538
Definition: points.h:189
EDGEPT * point1
Definition: split.h:103