tesseract  5.0.0-alpha-619-ge9db
outlines.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * File: outlines.cpp (Formerly outlines.c)
4  * Description: Combinatorial Splitter
5  * Author: Mark Seaman, OCR Technology
6  *
7  * (c) Copyright 1989, Hewlett-Packard Company.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  *****************************************************************************/
19 /*----------------------------------------------------------------------
20  I n c l u d e s
21 ----------------------------------------------------------------------*/
22 #include "outlines.h"
23 #include "wordrec.h"
24 
25 namespace tesseract {
26 /*----------------------------------------------------------------------
27  F u n c t i o n s
28 ----------------------------------------------------------------------*/
29 /**********************************************************************
30  * near_point
31  *
32  * Find the point on a line segment that is closest to a point not on
33  * the line segment. Return that point in near_pt. Returns whether
34  * near_pt was newly created.
35  **********************************************************************/
36 bool Wordrec::near_point(EDGEPT *point,
37  EDGEPT *line_pt_0, EDGEPT *line_pt_1,
38  EDGEPT **near_pt) {
39  TPOINT p;
40 
41  float slope;
42  float intercept;
43 
44  float x0 = line_pt_0->pos.x;
45  float x1 = line_pt_1->pos.x;
46  float y0 = line_pt_0->pos.y;
47  float y1 = line_pt_1->pos.y;
48 
49  if (x0 == x1) {
50  /* Handle vertical line */
51  p.x = static_cast<int16_t>(x0);
52  p.y = point->pos.y;
53  }
54  else {
55  /* Slope and intercept */
56  slope = (y0 - y1) / (x0 - x1);
57  intercept = y1 - x1 * slope;
58 
59  /* Find perpendicular */
60  p.x = static_cast<int16_t>((point->pos.x + (point->pos.y - intercept) * slope) /
61  (slope * slope + 1));
62  p.y = static_cast<int16_t>(slope * p.x + intercept);
63  }
64 
65  if (is_on_line (p, line_pt_0->pos, line_pt_1->pos) &&
66  (!same_point (p, line_pt_0->pos)) && (!same_point (p, line_pt_1->pos))) {
67  /* Intersection on line */
68  *near_pt = make_edgept(p.x, p.y, line_pt_1, line_pt_0);
69  return true;
70  } else { /* Intersection not on line */
71  *near_pt = closest(point, line_pt_0, line_pt_1);
72  return false;
73  }
74 }
75 
76 } // namespace tesseract
TPOINT
Definition: blobs.h:49
same_point
#define same_point(p1, p2)
Definition: outlines.h:42
wordrec.h
is_on_line
#define is_on_line(p, p0, p1)
Definition: outlines.h:107
tesseract::Wordrec::near_point
bool near_point(EDGEPT *point, EDGEPT *line_pt_0, EDGEPT *line_pt_1, EDGEPT **near_pt)
Definition: outlines.cpp:50
TPOINT::x
int16_t x
Definition: blobs.h:91
TPOINT::y
int16_t y
Definition: blobs.h:92
make_edgept
EDGEPT * make_edgept(int x, int y, EDGEPT *next, EDGEPT *prev)
Definition: split.cpp:136
tesseract
Definition: baseapi.h:65
EDGEPT
Definition: blobs.h:97
outlines.h
closest
#define closest(test_p, p1, p2)
Definition: outlines.h:62
EDGEPT::pos
TPOINT pos
Definition: blobs.h:184