21 using tesseract::ColPartition_LIST;
37 void ExpectPartition(
const TBOX& box) {
39 gsearch.SetUniqueMode(
true);
40 gsearch.StartFullSearch();
43 while ((part = gsearch.NextFullSearch()) !=
nullptr) {
53 void ExpectPartitionCount(
int expected_count) {
55 gsearch.SetUniqueMode(
true);
56 gsearch.StartFullSearch();
59 while ((part = gsearch.NextFullSearch()) !=
nullptr) {
62 EXPECT_EQ(expected_count,
count);
66 class TableFinderTest :
public testing::Test {
69 std::locale::global(std::locale(
""));
70 free_boxes_it_.set_to_list(&free_boxes_);
71 finder_.reset(
new TestableTableFinder());
74 finder_->set_global_median_xheight(5);
75 finder_->set_global_median_blob_width(5);
79 if (partition_.get() !=
nullptr) partition_->DeleteBoxes();
80 DeletePartitionListBoxes();
81 finder_.reset(
nullptr);
84 void MakePartition(
int x_min,
int y_min,
int x_max,
int y_max) {
85 MakePartition(x_min, y_min, x_max, y_max, 0, 0);
88 void MakePartition(
int x_min,
int y_min,
int x_max,
int y_max,
89 int first_column,
int last_column) {
90 if (partition_.get() !=
nullptr) partition_->DeleteBoxes();
95 partition_->set_first_column(first_column);
96 partition_->set_last_column(last_column);
100 finder_->InsertTextPartition(part);
101 free_boxes_it_.add_after_then_move(part);
104 void InsertLeaderPartition(
int x_min,
int y_min,
int x_max,
int y_max) {
105 InsertLeaderPartition(x_min, y_min, x_max, y_max, 0, 0);
108 void InsertLeaderPartition(
int x_min,
int y_min,
int x_max,
int y_max,
109 int first_column,
int last_column) {
116 finder_->InsertLeaderPartition(part);
117 free_boxes_it_.add_after_then_move(part);
120 void DeletePartitionListBoxes() {
121 for (free_boxes_it_.mark_cycle_pt(); !free_boxes_it_.cycled_list();
122 free_boxes_it_.forward()) {
128 std::unique_ptr<TestableTableFinder> finder_;
129 std::unique_ptr<ColPartition> partition_;
132 tesseract::ColPartition_CLIST free_boxes_;
133 tesseract::ColPartition_C_IT free_boxes_it_;
136 TEST_F(TableFinderTest, GapInXProjectionNoGap) {
138 for (
int i = 0; i < 100; ++i) data[i] = 10;
139 EXPECT_FALSE(finder_->GapInXProjection(data, 100));
142 TEST_F(TableFinderTest, GapInXProjectionEdgeGap) {
144 for (
int i = 0; i < 10; ++i) data[i] = 2;
145 for (
int i = 10; i < 90; ++i) data[i] = 10;
146 for (
int i = 90; i < 100; ++i) data[i] = 2;
147 EXPECT_FALSE(finder_->GapInXProjection(data, 100));
150 TEST_F(TableFinderTest, GapInXProjectionExists) {
152 for (
int i = 0; i < 10; ++i) data[i] = 10;
153 for (
int i = 10; i < 90; ++i) data[i] = 2;
154 for (
int i = 90; i < 100; ++i) data[i] = 10;
155 EXPECT_TRUE(finder_->GapInXProjection(data, 100));
158 TEST_F(TableFinderTest, HasLeaderAdjacentOverlapping) {
159 InsertLeaderPartition(90, 0, 150, 5);
160 MakePartition(0, 0, 100, 10);
161 EXPECT_TRUE(finder_->HasLeaderAdjacent(*partition_));
162 MakePartition(0, 25, 100, 40);
163 EXPECT_FALSE(finder_->HasLeaderAdjacent(*partition_));
164 MakePartition(145, 0, 200, 20);
165 EXPECT_TRUE(finder_->HasLeaderAdjacent(*partition_));
166 MakePartition(40, 0, 50, 4);
167 EXPECT_TRUE(finder_->HasLeaderAdjacent(*partition_));
170 TEST_F(TableFinderTest, HasLeaderAdjacentNoOverlap) {
171 InsertLeaderPartition(90, 10, 150, 15);
172 MakePartition(0, 10, 85, 20);
173 EXPECT_TRUE(finder_->HasLeaderAdjacent(*partition_));
174 MakePartition(0, 25, 100, 40);
175 EXPECT_FALSE(finder_->HasLeaderAdjacent(*partition_));
176 MakePartition(0, 0, 100, 10);
177 EXPECT_FALSE(finder_->HasLeaderAdjacent(*partition_));
183 TEST_F(TableFinderTest, HasLeaderAdjacentPreservesColumns) {
184 InsertLeaderPartition(90, 0, 150, 5, 1, 2);
185 MakePartition(0, 0, 85, 10, 0, 0);
186 EXPECT_FALSE(finder_->HasLeaderAdjacent(*partition_));
187 MakePartition(0, 0, 100, 10, 0, 1);
188 EXPECT_TRUE(finder_->HasLeaderAdjacent(*partition_));
189 MakePartition(0, 0, 200, 10, 0, 5);
190 EXPECT_TRUE(finder_->HasLeaderAdjacent(*partition_));
191 MakePartition(155, 0, 200, 10, 5, 5);
192 EXPECT_FALSE(finder_->HasLeaderAdjacent(*partition_));
197 TEST_F(TableFinderTest, SplitAndInsertFragmentedPartitionsBasicPass) {
198 finder_->set_global_median_blob_width(3);
199 finder_->set_global_median_xheight(10);
201 TBOX part_box(10, 5, 100, 15);
208 TBOX blob_box = part_box;
209 for (
int i = 10; i <= 20; i += 5) {
214 for (
int i = 35; i <= 55; i += 5) {
219 for (
int i = 80; i <= 95; i += 5) {
227 InsertTextPartition(all);
230 finder_->SplitAndInsertFragmentedTextPartition(fragment_me);
231 finder_->ExpectPartition(
TBOX(11, 5, 24, 15));
232 finder_->ExpectPartition(
TBOX(36, 5, 59, 15));
233 finder_->ExpectPartition(
TBOX(81, 5, 99, 15));
234 finder_->ExpectPartitionCount(3);
237 TEST_F(TableFinderTest, SplitAndInsertFragmentedPartitionsBasicFail) {
238 finder_->set_global_median_blob_width(3);
239 finder_->set_global_median_xheight(10);
241 TBOX part_box(10, 5, 100, 15);
248 TBOX blob_box = part_box;
249 for (
int i = 10; i <= 95; i += 5) {
257 InsertTextPartition(all);
260 finder_->SplitAndInsertFragmentedTextPartition(fragment_me);
261 finder_->ExpectPartition(
TBOX(11, 5, 99, 15));
262 finder_->ExpectPartitionCount(1);