/src/tesseract/src/wordrec/outlines.h
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * File: outlines.h |
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 | | #ifndef OUTLINES_H |
21 | | #define OUTLINES_H |
22 | | |
23 | | #include <cmath> // for abs |
24 | | #include "blobs.h" // for TPOINT |
25 | | #include "params.h" // for IntParam |
26 | | #include "wordrec.h" // for Wordrec |
27 | | |
28 | | /*---------------------------------------------------------------------- |
29 | | C o n s t a n t s |
30 | | ----------------------------------------------------------------------*/ |
31 | | constexpr int LARGE_DISTANCE = 100000; /* Used for closest dist */ |
32 | | constexpr int MIN_BLOB_SIZE = 10; /* Big units */ |
33 | | constexpr double MAX_ASPECT_RATIO = 2.5; /* Widest character */ |
34 | | |
35 | | /*---------------------------------------------------------------------- |
36 | | M a c r o s |
37 | | ----------------------------------------------------------------------*/ |
38 | | /********************************************************************** |
39 | | * same_point |
40 | | * |
41 | | * Return true if the point values are the same. The parameters must |
42 | | * be of type POINT. |
43 | | **********************************************************************/ |
44 | | #define same_point(p1, p2) \ |
45 | 716M | ((abs(p1.x - p2.x) < chop_same_distance) && (abs(p1.y - p2.y) < chop_same_distance)) |
46 | | |
47 | | /********************************************************************** |
48 | | * dist_square |
49 | | * |
50 | | * Return the square of the distance between these two points. The |
51 | | * parameters must be of type POINT. |
52 | | **********************************************************************/ |
53 | | |
54 | | template <typename Point> |
55 | 88.3M | inline constexpr auto dist_square(const Point &p1, const Point &p2) { |
56 | 88.3M | return (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y); |
57 | 88.3M | } |
58 | | |
59 | | /********************************************************************** |
60 | | * closest |
61 | | * |
62 | | * The expression provides the EDGEPT that is closest to the point in |
63 | | * question. All three parameters must be of type EDGEPT. |
64 | | **********************************************************************/ |
65 | | |
66 | | template <typename Edgept> |
67 | 23.4M | inline Edgept *closest(Edgept *test_p, Edgept *p1, Edgept *p2) { |
68 | 23.4M | if (!p1) return p2; |
69 | 23.4M | if (!p2) return p1; |
70 | 23.4M | return dist_square(test_p->pos, p1->pos) < dist_square(test_p->pos, p2->pos) ? p1 : p2; |
71 | 23.4M | } |
72 | | |
73 | | /********************************************************************** |
74 | | * edgept_dist |
75 | | * |
76 | | * Return the distance (squared) between the two edge points. |
77 | | **********************************************************************/ |
78 | | |
79 | | template <typename Edgept> |
80 | 41.4M | inline constexpr auto edgept_dist(const Edgept *p1, const Edgept *p2) { |
81 | 41.4M | return dist_square(p1->pos, p2->pos); |
82 | 41.4M | } |
83 | | |
84 | | /********************************************************************** |
85 | | * is_exterior_point |
86 | | * |
87 | | * Return true if the point supplied is an exterior projection from the |
88 | | * outline. |
89 | | **********************************************************************/ |
90 | | |
91 | | #define is_exterior_point(edge, point) \ |
92 | 48.7M | (same_point(edge->prev->pos, point->pos) || same_point(edge->next->pos, point->pos) || \ |
93 | 48.7M | (angle_change(edge->prev, edge, edge->next) - angle_change(edge->prev, edge, point) > 20)) |
94 | | |
95 | | /********************************************************************** |
96 | | * is_equal |
97 | | * |
98 | | * Return true if the POINTs are equal. |
99 | | **********************************************************************/ |
100 | | |
101 | | template <typename Point> |
102 | | inline constexpr bool is_equal(const Point &p1, const Point &p2) { |
103 | | return p1.x == p2.x && p1.y == p2.y; |
104 | | } |
105 | | |
106 | | /********************************************************************** |
107 | | * is_on_line |
108 | | * |
109 | | * Return true if the point is on the line segment between the two end |
110 | | * points. The two end points are included as part of the line. The |
111 | | * parameters must be of type POINT. |
112 | | **********************************************************************/ |
113 | | |
114 | | template <typename T> |
115 | 39.4M | inline constexpr bool within_range(T x, T x0, T x1) { |
116 | 39.4M | return (x0 <= x && x <= x1) || (x1 <= x && x <= x0); |
117 | 39.4M | } |
118 | | |
119 | | template <typename Point> |
120 | 27.6M | inline constexpr bool is_on_line(const Point &p, const Point &p0, const Point &p1) { |
121 | 27.6M | return within_range(p.x, p0.x, p1.x) && within_range(p.y, p0.y, p1.y); |
122 | 27.6M | } |
123 | | |
124 | | #endif |