tesseract  5.0.0-alpha-619-ge9db
rect_test.cc
Go to the documentation of this file.
1 // (C) Copyright 2017, Google Inc.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 // http://www.apache.org/licenses/LICENSE-2.0
6 // Unless required by applicable law or agreed to in writing, software
7 // distributed under the License is distributed on an "AS IS" BASIS,
8 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9 // See the License for the specific language governing permissions and
10 // limitations under the License.
11 
12 #include "rect.h"
13 
14 #include "include_gunit.h"
15 
16 namespace {
17 
18 class TBOXTest : public testing::Test {
19  public:
20  void SetUp() {
21  std::locale::global(std::locale(""));
22  }
23 
24  void TearDown() {}
25 };
26 
27 TEST_F(TBOXTest, OverlapInside) {
28  TBOX a(10, 10, 20, 20);
29  TBOX b(11, 11, 12, 12);
30 
31  EXPECT_TRUE(a.overlap(b));
32  EXPECT_TRUE(b.overlap(a));
33  EXPECT_DOUBLE_EQ(0.01, a.overlap_fraction(b));
34  EXPECT_DOUBLE_EQ(1.0, b.overlap_fraction(a));
35 }
36 
37 TEST_F(TBOXTest, OverlapBoolCorners) {
38  TBOX mid(10, 10, 30, 30);
39  TBOX bottom_left(5, 5, 15, 15);
40  TBOX top_left(5, 25, 15, 35);
41  // other corners covered by symmetry
42 
43  EXPECT_TRUE(mid.overlap(bottom_left));
44  EXPECT_TRUE(bottom_left.overlap(mid));
45  EXPECT_TRUE(mid.overlap(top_left));
46  EXPECT_TRUE(top_left.overlap(mid));
47 }
48 
49 TEST_F(TBOXTest, OverlapFractionCorners) {
50  TBOX mid(10, 10, 30, 30);
51  TBOX bottom_left(5, 5, 15, 15);
52  TBOX top_left(5, 25, 15, 35);
53  // other corners covered by symmetry
54 
55  EXPECT_DOUBLE_EQ((5.0 * 5.0) / (20.0 * 20.0),
56  mid.overlap_fraction(bottom_left));
57  EXPECT_DOUBLE_EQ((5.0 * 5.0) / (10.0 * 10.0),
58  bottom_left.overlap_fraction(mid));
59  EXPECT_DOUBLE_EQ((5.0 * 5.0) / (20.0 * 20.0), mid.overlap_fraction(top_left));
60  EXPECT_DOUBLE_EQ((5.0 * 5.0) / (10.0 * 10.0), top_left.overlap_fraction(mid));
61 }
62 
63 TEST_F(TBOXTest, OverlapBoolSides) {
64  TBOX mid(10, 10, 30, 30);
65  TBOX left(5, 15, 15, 25);
66  TBOX bottom(15, 5, 25, 15);
67  // other sides covered by symmetry
68 
69  EXPECT_TRUE(mid.overlap(left));
70  EXPECT_TRUE(left.overlap(mid));
71  EXPECT_TRUE(mid.overlap(bottom));
72  EXPECT_TRUE(bottom.overlap(mid));
73 }
74 
75 TEST_F(TBOXTest, OverlapFractionSides) {
76  TBOX mid(10, 10, 30, 30);
77  TBOX left(5, 15, 15, 25);
78  TBOX bottom(15, 5, 25, 15);
79  // other sides covered by symmetry
80 
81  EXPECT_DOUBLE_EQ((5.0 * 10.0) / (20.0 * 20.0), mid.overlap_fraction(left));
82  EXPECT_DOUBLE_EQ((5.0 * 10.0) / (10.0 * 10.0), left.overlap_fraction(mid));
83  EXPECT_DOUBLE_EQ((5.0 * 10.0) / (20.0 * 20.0), mid.overlap_fraction(bottom));
84  EXPECT_DOUBLE_EQ((5.0 * 10.0) / (10.0 * 10.0), bottom.overlap_fraction(mid));
85 }
86 
87 TEST_F(TBOXTest, OverlapBoolSpan) {
88  TBOX mid(10, 10, 30, 30);
89  TBOX vertical(15, 5, 25, 35);
90  TBOX horizontal(5, 15, 35, 25);
91  // other sides covered by symmetry in other test cases
92 
93  EXPECT_TRUE(mid.overlap(vertical));
94  EXPECT_TRUE(vertical.overlap(mid));
95  EXPECT_TRUE(mid.overlap(horizontal));
96  EXPECT_TRUE(horizontal.overlap(mid));
97 }
98 
99 TEST_F(TBOXTest, OverlapFractionSpan) {
100  TBOX mid(10, 10, 30, 30);
101  TBOX vertical(15, 5, 25, 35);
102  TBOX horizontal(5, 15, 35, 25);
103  // other sides covered by symmetry in other test cases
104 
105  EXPECT_DOUBLE_EQ((10.0 * 20.0) / (20.0 * 20.0),
106  mid.overlap_fraction(vertical));
107  EXPECT_DOUBLE_EQ((10.0 * 20.0) / (10.0 * 30.0),
108  vertical.overlap_fraction(mid));
109  EXPECT_DOUBLE_EQ((20.0 * 10.0) / (20.0 * 20.0),
110  mid.overlap_fraction(horizontal));
111  EXPECT_DOUBLE_EQ((20.0 * 10.0) / (30.0 * 10.0),
112  horizontal.overlap_fraction(mid));
113 }
114 
115 // TODO(nbeato): pretty much all cases
116 TEST_F(TBOXTest, OverlapOutsideTests) {
117  TBOX mid(10, 10, 30, 30);
118  TBOX left(0, 15, 5, 25);
119 
120  EXPECT_FALSE(mid.overlap(left));
121  EXPECT_FALSE(left.overlap(mid));
122  EXPECT_DOUBLE_EQ(0.0, mid.overlap_fraction(left));
123  EXPECT_DOUBLE_EQ(0.0, left.overlap_fraction(mid));
124 }
125 
126 TEST_F(TBOXTest, OverlapXFraction) {
127  TBOX a(10, 10, 20, 20);
128  TBOX b(12, 100, 26, 200);
129  TBOX c(0, 0, 100, 100);
130  TBOX d(0, 0, 1, 1);
131 
132  EXPECT_DOUBLE_EQ(8.0 / 10.0, a.x_overlap_fraction(b));
133  EXPECT_DOUBLE_EQ(8.0 / 14.0, b.x_overlap_fraction(a));
134  EXPECT_DOUBLE_EQ(1.0, a.x_overlap_fraction(c));
135  EXPECT_DOUBLE_EQ(10.0 / 100.0, c.x_overlap_fraction(a));
136  EXPECT_DOUBLE_EQ(0.0, a.x_overlap_fraction(d));
137  EXPECT_DOUBLE_EQ(0.0, d.x_overlap_fraction(a));
138 }
139 
140 TEST_F(TBOXTest, OverlapYFraction) {
141  TBOX a(10, 10, 20, 20);
142  TBOX b(100, 12, 200, 26);
143  TBOX c(0, 0, 100, 100);
144  TBOX d(0, 0, 1, 1);
145 
146  EXPECT_DOUBLE_EQ(8.0 / 10.0, a.y_overlap_fraction(b));
147  EXPECT_DOUBLE_EQ(8.0 / 14.0, b.y_overlap_fraction(a));
148  EXPECT_DOUBLE_EQ(1.0, a.y_overlap_fraction(c));
149  EXPECT_DOUBLE_EQ(10.0 / 100.0, c.y_overlap_fraction(a));
150  EXPECT_DOUBLE_EQ(0.0, a.y_overlap_fraction(d));
151  EXPECT_DOUBLE_EQ(0.0, d.y_overlap_fraction(a));
152 }
153 
154 TEST_F(TBOXTest, OverlapXFractionZeroSize) {
155  TBOX zero(10, 10, 10, 10);
156  TBOX big(0, 0, 100, 100);
157  TBOX small(0, 0, 1, 1);
158 
159  EXPECT_DOUBLE_EQ(1.0, zero.x_overlap_fraction(big));
160  EXPECT_DOUBLE_EQ(0.0, big.x_overlap_fraction(zero));
161  EXPECT_DOUBLE_EQ(0.0, zero.x_overlap_fraction(small));
162  EXPECT_DOUBLE_EQ(0.0, small.x_overlap_fraction(zero));
163 }
164 
165 TEST_F(TBOXTest, OverlapYFractionZeroSize) {
166  TBOX zero(10, 10, 10, 10);
167  TBOX big(0, 0, 100, 100);
168  TBOX small(0, 0, 1, 1);
169 
170  EXPECT_DOUBLE_EQ(1.0, zero.y_overlap_fraction(big));
171  EXPECT_DOUBLE_EQ(0.0, big.y_overlap_fraction(zero));
172  EXPECT_DOUBLE_EQ(0.0, zero.y_overlap_fraction(small));
173  EXPECT_DOUBLE_EQ(0.0, small.y_overlap_fraction(zero));
174 }
175 
176 } // namespace
include_gunit.h
tesseract::TEST_F
TEST_F(EquationFinderTest, IdentifySpecialText)
Definition: equationdetect_test.cc:181
rect.h
TBOX
Definition: rect.h:33