21 #ifndef TESSERACT_TEXTORD_BBGRID_H_ 22 #define TESSERACT_TEXTORD_BBGRID_H_ 24 #include <unordered_set> 31 #include "allheaders.h" 44 ICOORD bleft,
int* left,
int* bottom);
47 ICOORD bleft,
int* left,
int* bottom);
49 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
class GridSearch;
80 void GridCoords(
int x,
int y,
int* grid_x,
int* grid_y)
const;
115 void Rotate(
const FCOORD& rotation);
119 IntGrid* NeighbourhoodSum()
const;
132 bool RectMostlyOverThreshold(
const TBOX& rect,
int threshold)
const;
135 bool AnyZeroInRect(
const TBOX& rect)
const;
139 Pix* ThresholdToPix(
int threshold)
const;
159 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
class BBGrid 175 void ClearGridData(
void (*free_method)(BBC*));
182 void InsertBBox(
bool h_spread,
bool v_spread, BBC* bbox);
193 void InsertPixPtBBox(
int left,
int bottom, Pix* pix, BBC* bbox);
198 void RemoveBBox(BBC* bbox);
201 bool RectangleEmpty(
const TBOX& rect);
208 ScrollView* MakeWindow(
int x,
int y,
const char* window_name);
216 void AssertNoDuplicates();
219 virtual void HandleClick(
int x,
int y);
230 return reinterpret_cast<size_t>(ptr) /
sizeof(T);
236 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
class GridSearch {
239 : grid_(grid), unique_mode_(false),
240 previous_return_(NULL), next_return_(NULL) {
267 TBOX box = previous_return_->bounding_box();
268 int x_center = (box.
left()+box.
right())/2;
269 int y_center = (box.
top()+box.
bottom())/2;
271 grid_->GridCoords(x_center, y_center, &grid_x, &grid_y);
272 return (x_ == grid_x) && (y_ == grid_y);
292 void StartFullSearch();
294 BBC* NextFullSearch();
298 void StartRadSearch(
int x,
int y,
int max_radius);
301 BBC* NextRadSearch();
311 void StartSideSearch(
int x,
int ymin,
int ymax);
315 BBC* NextSideSearch(
bool right_to_left);
319 void StartVerticalSearch(
int xmin,
int xmax,
int y);
323 BBC* NextVerticalSearch(
bool top_to_bottom);
327 void StartRectSearch(
const TBOX& rect);
329 BBC* NextRectSearch();
335 void RepositionIterator();
339 void CommonStart(
int x,
int y);
363 BBC* previous_return_;
368 std::unordered_set<BBC*, PtrHash<BBC> > returns_;
375 const BBC* p1 = *
static_cast<const BBC*
const *
>(void1);
376 const BBC* p2 = *
static_cast<const BBC*
const *
>(void2);
377 int result = p1->bounding_box().left() - p2->bounding_box().left();
380 result = p1->bounding_box().right() - p2->bounding_box().right();
383 result = p1->bounding_box().bottom() - p2->bounding_box().bottom();
386 return p1->bounding_box().top() - p2->bounding_box().top();
393 const BBC* p1 = *
static_cast<const BBC*
const *
>(void1);
394 const BBC* p2 = *
static_cast<const BBC*
const *
>(void2);
395 int result = p2->bounding_box().right() - p1->bounding_box().right();
398 result = p2->bounding_box().left() - p1->bounding_box().left();
401 result = p1->bounding_box().bottom() - p2->bounding_box().bottom();
404 return p1->bounding_box().top() - p2->bounding_box().top();
411 const BBC* p1 = *
static_cast<const BBC*
const *
>(void1);
412 const BBC* p2 = *
static_cast<const BBC*
const *
>(void2);
413 int result = p1->bounding_box().bottom() - p2->bounding_box().bottom();
416 result = p1->bounding_box().top() - p2->bounding_box().top();
419 result = p1->bounding_box().left() - p2->bounding_box().left();
422 return p1->bounding_box().right() - p2->bounding_box().right();
428 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
432 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
434 int gridsize,
const ICOORD& bleft,
const ICOORD& tright)
436 Init(gridsize, bleft, tright);
439 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
447 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
451 GridBase::Init(gridsize, bleft, tright);
454 grid_ =
new BBC_CLIST[gridbuckets_];
458 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
460 for (
int i = 0; i < gridbuckets_; ++i) {
461 grid_[i].shallow_clear();
467 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
469 void (*free_method)(BBC*)) {
470 if (grid_ == NULL)
return;
475 BBC_C_IT it(&bb_list);
477 it.add_after_then_move(bb);
479 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
480 free_method(it.data());
489 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
492 TBOX box = bbox->bounding_box();
493 int start_x, start_y, end_x, end_y;
494 GridCoords(box.
left(), box.
bottom(), &start_x, &start_y);
495 GridCoords(box.
right(), box.
top(), &end_x, &end_y);
500 int grid_index = start_y * gridwidth_;
501 for (
int y = start_y; y <= end_y; ++y, grid_index += gridwidth_) {
502 for (
int x = start_x; x <= end_x; ++x) {
503 grid_[grid_index + x].add_sorted(SortByBoxLeft<BBC>,
true, bbox);
517 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
519 Pix* pix, BBC* bbox) {
520 int width = pixGetWidth(pix);
521 int height = pixGetHeight(pix);
522 for (
int y = 0; y < height; ++y) {
523 l_uint32* data = pixGetData(pix) + y * pixGetWpl(pix);
524 for (
int x = 0; x < width; ++x) {
525 if (GET_DATA_BIT(data, x)) {
526 grid_[(bottom + y) * gridwidth_ + x + left].
527 add_sorted(SortByBoxLeft<BBC>,
true, bbox);
536 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
538 TBOX box = bbox->bounding_box();
539 int start_x, start_y, end_x, end_y;
540 GridCoords(box.
left(), box.
bottom(), &start_x, &start_y);
541 GridCoords(box.
right(), box.
top(), &end_x, &end_y);
542 int grid_index = start_y * gridwidth_;
543 for (
int y = start_y; y <= end_y; ++y, grid_index += gridwidth_) {
544 for (
int x = start_x; x <= end_x; ++x) {
545 BBC_C_IT it(&grid_[grid_index + x]);
546 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
547 if (it.data() == bbox)
555 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
564 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
567 for (
int y = 0; y < gridheight(); ++y) {
568 for (
int x = 0; x < gridwidth(); ++x) {
569 int cell_count = grid_[y * gridwidth() + x].length();
583 grid_->HandleClick(sv_event->
x, sv_event->
y);
592 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
594 int x,
int y,
const char* window_name) {
596 #ifndef GRAPHICS_DISABLED 598 tright_.x() - bleft_.x(),
599 tright_.y() - bleft_.y(),
600 tright_.x() - bleft_.x(),
601 tright_.y() - bleft_.y(),
607 tab_win->
Rectangle(0, 0, tright_.x() - bleft_.x(), tright_.y() - bleft_.y());
616 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
618 #ifndef GRAPHICS_DISABLED 627 const TBOX& box = bbox->bounding_box();
628 int left_x = box.
left();
629 int right_x = box.
right();
630 int top_y = box.
top();
631 int bottom_y = box.
bottom();
633 tab_win->
Pen(box_color);
634 tab_win->
Rectangle(left_x, bottom_y, right_x, top_y);
641 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
644 for (
int i = gridwidth_ * gridheight_ - 1; i >= 0; --i) {
646 for (BBC_C_IT it(&grid_[i]); !it.at_last(); it.forward()) {
647 BBC* ptr = it.data();
650 for (it2.forward(); !it2.at_first(); it2.forward()) {
658 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
660 tprintf(
"Click at (%d, %d)\n", x, y);
668 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
672 CommonStart(grid_->bleft_.x(), grid_->tright_.y());
678 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
683 while (it_.cycled_list()) {
685 if (x_ >= grid_->gridwidth_) {
694 TBOX box = previous_return_->bounding_box();
695 grid_->GridCoords(box.
left(), box.
bottom(), &x, &y);
696 }
while (x != x_ || y != y_);
697 return previous_return_;
701 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
707 max_radius_ = max_radius;
716 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
719 while (it_.cycled_list()) {
721 if (rad_index_ >= radius_) {
726 if (radius_ > max_radius_)
732 offset *= radius_ - rad_index_;
734 x_ = x_origin_ + offset.
x();
735 y_ = y_origin_ + offset.
y();
736 if (x_ >= 0 && x_ < grid_->gridwidth_ &&
737 y_ >= 0 && y_ < grid_->gridheight_)
741 }
while (unique_mode_ && returns_.find(previous_return_) != returns_.end());
743 returns_.insert(previous_return_);
744 return previous_return_;
749 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
751 int ymin,
int ymax) {
756 radius_ = ((ymax - ymin) * 2 + grid_->gridsize_ - 1) / grid_->gridsize_;
758 CommonStart(x, ymax);
764 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
767 while (it_.cycled_list()) {
769 if (rad_index_ > radius_) {
775 if (x_ < 0 || x_ >= grid_->gridwidth_)
778 y_ = y_origin_ - rad_index_;
779 if (y_ >= 0 && y_ < grid_->gridheight_)
783 }
while (unique_mode_ && returns_.find(previous_return_) != returns_.end());
785 returns_.insert(previous_return_);
786 return previous_return_;
791 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
797 radius_ = (xmax - xmin + grid_->gridsize_ - 1) / grid_->gridsize_;
799 CommonStart(xmin, y);
805 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
807 bool top_to_bottom) {
809 while (it_.cycled_list()) {
811 if (rad_index_ > radius_) {
817 if (y_ < 0 || y_ >= grid_->gridheight_)
820 x_ = x_origin_ + rad_index_;
821 if (x_ >= 0 && x_ < grid_->gridwidth_)
825 }
while (unique_mode_ && returns_.find(previous_return_) != returns_.end());
827 returns_.insert(previous_return_);
828 return previous_return_;
833 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
839 CommonStart(rect.
left(), rect.
top());
841 &max_radius_, &y_origin_);
845 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
848 while (it_.cycled_list()) {
850 if (x_ > max_radius_) {
859 }
while (!rect_.overlap(previous_return_->bounding_box()) ||
860 (unique_mode_ && returns_.find(previous_return_) != returns_.end()));
862 returns_.insert(previous_return_);
863 return previous_return_;
869 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
871 if (previous_return_ != NULL) {
875 BBC* prev_data = NULL;
876 BBC* new_previous_return = NULL;
878 for (it_.mark_cycle_pt(); !it_.cycled_list();) {
879 if (it_.data() == previous_return_) {
880 new_previous_return = prev_data;
883 next_return_ = it_.cycled_list() ? NULL : it_.data();
885 prev_data = it_.data();
890 previous_return_ = new_previous_return;
891 RepositionIterator();
895 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
907 if (!it_.empty() && it_.data() == next_return_) {
911 for (it_.mark_cycle_pt(); !it_.cycled_list(); it_.forward()) {
912 if (it_.data() == previous_return_ ||
913 it_.data_relative(1) == next_return_) {
919 previous_return_ = NULL;
924 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
926 grid_->GridCoords(x, y, &x_origin_, &y_origin_);
930 previous_return_ = NULL;
931 next_return_ = it_.empty() ? NULL : it_.data();
936 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
938 previous_return_ = it_.data();
940 next_return_ = it_.cycled_list() ? NULL : it_.data();
941 return previous_return_;
945 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
947 previous_return_ = NULL;
954 template<
class BBC,
class BBC_CLIST,
class BBC_C_IT>
956 it_= &(grid_->grid_[y_ * grid_->gridwidth_ + x_]);
962 #endif // TESSERACT_TEXTORD_BBGRID_H_
size_t operator()(const T *ptr) const
bool ReturnedSeedElement() const
GridSearch(BBGrid< BBC, BBC_CLIST, BBC_C_IT > *grid)
const ICOORD & bleft() const
inT16 x() const
access function
static ICOORD chain_step(int chaindir)
int SortByBoxBottom(const void *void1, const void *void2)
void SetUniqueMode(bool mode)
LIST search(LIST list, void *key, int_compare is_equal)
int SortByBoxLeft(const void *void1, const void *void2)
void Rectangle(int x1, int y1, int x2, int y2)
Pix * TraceBlockOnReducedPix(BLOCK *block, int gridsize, ICOORD bleft, int *left, int *bottom)
inT16 y() const
access_function
const ICOORD & tright() const
void AddEventHandler(SVEventHandler *listener)
Add an Event Listener to this ScrollView Window.
Pix * TraceOutlineOnReducedPix(C_OUTLINE *outline, int gridsize, ICOORD bleft, int *left, int *bottom)
int GridCellValue(int grid_x, int grid_y) const
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
void Notify(const SVEvent *sv_event)
void StartRectSearch(const TBOX &rect)
void SetGridCell(int grid_x, int grid_y, int value)
int SortRightToLeft(const void *void1, const void *void2)
void ClipGridCoords(int *x, int *y) const
void GridCoords(int x, int y, int *grid_x, int *grid_y) const