tesseract v5.3.3.20231005
tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT > Class Template Reference

#include <bbgrid.h>

Public Member Functions

 GridSearch (BBGrid< BBC, BBC_CLIST, BBC_C_IT > *grid)
 
int GridX () const
 
int GridY () const
 
void SetUniqueMode (bool mode)
 
bool ReturnedSeedElement () const
 
void StartFullSearch ()
 
BBC * NextFullSearch ()
 
void StartRadSearch (int x, int y, int max_radius)
 
BBC * NextRadSearch ()
 
void StartSideSearch (int x, int ymin, int ymax)
 
BBC * NextSideSearch (bool right_to_left)
 
void StartVerticalSearch (int xmin, int xmax, int y)
 
BBC * NextVerticalSearch (bool top_to_bottom)
 
void StartRectSearch (const TBOX &rect)
 
BBC * NextRectSearch ()
 
void RemoveBBox ()
 
void RepositionIterator ()
 

Detailed Description

template<class BBC, class BBC_CLIST, class BBC_C_IT>
class tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >

Definition at line 233 of file bbgrid.h.

Constructor & Destructor Documentation

◆ GridSearch()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::GridSearch ( BBGrid< BBC, BBC_CLIST, BBC_C_IT > *  grid)
inline

Definition at line 235 of file bbgrid.h.

235: grid_(grid) {}

Member Function Documentation

◆ GridX()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
int tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::GridX ( ) const
inline

Definition at line 238 of file bbgrid.h.

238 {
239 return x_;
240 }

◆ GridY()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
int tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::GridY ( ) const
inline

Definition at line 241 of file bbgrid.h.

241 {
242 return y_;
243 }

◆ NextFullSearch()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
BBC * tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::NextFullSearch

Definition at line 711 of file bbgrid.h.

711 {
712 int x;
713 int y;
714 do {
715 while (it_.cycled_list()) {
716 ++x_;
717 if (x_ >= grid_->gridwidth_) {
718 --y_;
719 if (y_ < 0) {
720 return CommonEnd();
721 }
722 x_ = 0;
723 }
724 SetIterator();
725 }
726 CommonNext();
727 TBOX box = previous_return_->bounding_box();
728 grid_->GridCoords(box.left(), box.bottom(), &x, &y);
729 } while (x != x_ || y != y_);
730 return previous_return_;
731}
@ TBOX
const double y

◆ NextRadSearch()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
BBC * tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::NextRadSearch

Definition at line 749 of file bbgrid.h.

749 {
750 for (;;) {
751 while (it_.cycled_list()) {
752 ++rad_index_;
753 if (rad_index_ >= radius_) {
754 ++rad_dir_;
755 rad_index_ = 0;
756 if (rad_dir_ >= 4) {
757 ++radius_;
758 if (radius_ > max_radius_) {
759 return CommonEnd();
760 }
761 rad_dir_ = 0;
762 }
763 }
764 ICOORD offset = C_OUTLINE::chain_step(rad_dir_);
765 offset *= radius_ - rad_index_;
766 offset += C_OUTLINE::chain_step(rad_dir_ + 1) * rad_index_;
767 x_ = x_origin_ + offset.x();
768 y_ = y_origin_ + offset.y();
769 if (x_ >= 0 && x_ < grid_->gridwidth_ && y_ >= 0 && y_ < grid_->gridheight_) {
770 SetIterator();
771 }
772 }
773 CommonNext();
774 if (!unique_mode_) {
775 break;
776 }
777 auto inserted = returns_.insert(previous_return_);
778 if (inserted.second) {
779 break;
780 }
781 }
782 return previous_return_;
783}
static ICOORD chain_step(int chaindir)
Definition: coutln.cpp:1058

◆ NextRectSearch()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
BBC * tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::NextRectSearch

Definition at line 896 of file bbgrid.h.

896 {
897 for (;;) {
898 while (it_.cycled_list()) {
899 ++x_;
900 if (x_ > max_radius_) {
901 --y_;
902 x_ = x_origin_;
903 if (y_ < y_origin_) {
904 return CommonEnd();
905 }
906 }
907 SetIterator();
908 }
909 CommonNext();
910 if (!rect_.overlap(previous_return_->bounding_box())) {
911 continue;
912 }
913 if (!unique_mode_) {
914 break;
915 }
916 auto inserted = returns_.insert(previous_return_);
917 if (inserted.second) {
918 break;
919 }
920 }
921 return previous_return_;
922}
bool overlap(const TBOX &box) const
Definition: rect.h:363

◆ NextSideSearch()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
BBC * tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::NextSideSearch ( bool  right_to_left)

Definition at line 802 of file bbgrid.h.

802 {
803 for (;;) {
804 while (it_.cycled_list()) {
805 ++rad_index_;
806 if (rad_index_ > radius_) {
807 if (right_to_left) {
808 --x_;
809 } else {
810 ++x_;
811 }
812 rad_index_ = 0;
813 if (x_ < 0 || x_ >= grid_->gridwidth_) {
814 return CommonEnd();
815 }
816 }
817 y_ = y_origin_ - rad_index_;
818 if (y_ >= 0 && y_ < grid_->gridheight_) {
819 SetIterator();
820 }
821 }
822 CommonNext();
823 if (!unique_mode_) {
824 break;
825 }
826 auto inserted = returns_.insert(previous_return_);
827 if (inserted.second) {
828 break;
829 }
830 }
831 return previous_return_;
832}

◆ NextVerticalSearch()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
BBC * tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::NextVerticalSearch ( bool  top_to_bottom)

Definition at line 849 of file bbgrid.h.

849 {
850 for (;;) {
851 while (it_.cycled_list()) {
852 ++rad_index_;
853 if (rad_index_ > radius_) {
854 if (top_to_bottom) {
855 --y_;
856 } else {
857 ++y_;
858 }
859 rad_index_ = 0;
860 if (y_ < 0 || y_ >= grid_->gridheight_) {
861 return CommonEnd();
862 }
863 }
864 x_ = x_origin_ + rad_index_;
865 if (x_ >= 0 && x_ < grid_->gridwidth_) {
866 SetIterator();
867 }
868 }
869 CommonNext();
870 if (!unique_mode_) {
871 break;
872 }
873 auto inserted = returns_.insert(previous_return_);
874 if (inserted.second) {
875 break;
876 }
877 }
878 return previous_return_;
879}

◆ RemoveBBox()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
void tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::RemoveBBox

Definition at line 928 of file bbgrid.h.

928 {
929 if (previous_return_ != nullptr) {
930 // Remove all instances of previous_return_ from the list, so the iterator
931 // remains valid after removal from the rest of the grid cells.
932 // if previous_return_ is not on the list, then it has been removed already.
933 BBC *prev_data = nullptr;
934 BBC *new_previous_return = nullptr;
935 it_.move_to_first();
936 for (it_.mark_cycle_pt(); !it_.cycled_list();) {
937 if (it_.data() == previous_return_) {
938 new_previous_return = prev_data;
939 it_.extract();
940 it_.forward();
941 next_return_ = it_.cycled_list() ? nullptr : it_.data();
942 } else {
943 prev_data = it_.data();
944 it_.forward();
945 }
946 }
947 grid_->RemoveBBox(previous_return_);
948 previous_return_ = new_previous_return;
950 }
951}
void RepositionIterator()
Definition: bbgrid.h:954

◆ RepositionIterator()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
void tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::RepositionIterator

Definition at line 954 of file bbgrid.h.

954 {
955 // Something was deleted, so we have little choice but to clear the
956 // returns list.
957 returns_.clear();
958 // Reset the iterator back to one past the previous return.
959 // If the previous_return_ is no longer in the list, then
960 // next_return_ serves as a backup.
961 it_.move_to_first();
962 // Special case, the first element was removed and reposition
963 // iterator was called. In this case, the data is fine, but the
964 // cycle point is not. Detect it and return.
965 if (!it_.empty() && it_.data() == next_return_) {
966 it_.mark_cycle_pt();
967 return;
968 }
969 for (it_.mark_cycle_pt(); !it_.cycled_list(); it_.forward()) {
970 if (it_.data() == previous_return_ || it_.data_relative(1) == next_return_) {
971 CommonNext();
972 return;
973 }
974 }
975 // We ran off the end of the list. Move to a new cell next time.
976 previous_return_ = nullptr;
977 next_return_ = nullptr;
978}

◆ ReturnedSeedElement()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
bool tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::ReturnedSeedElement ( ) const
inline

Definition at line 260 of file bbgrid.h.

260 {
261 TBOX box = previous_return_->bounding_box();
262 int x_center = (box.left() + box.right()) / 2;
263 int y_center = (box.top() + box.bottom()) / 2;
264 int grid_x, grid_y;
265 grid_->GridCoords(x_center, y_center, &grid_x, &grid_y);
266 return (x_ == grid_x) && (y_ == grid_y);
267 }

◆ SetUniqueMode()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
void tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::SetUniqueMode ( bool  mode)
inline

Definition at line 249 of file bbgrid.h.

249 {
250 unique_mode_ = mode;
251 }

◆ StartFullSearch()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
void tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::StartFullSearch

Definition at line 701 of file bbgrid.h.

701 {
702 // Full search uses x_ and y_ as the current grid
703 // cell being searched.
704 CommonStart(grid_->bleft_.x(), grid_->tright_.y());
705}

◆ StartRadSearch()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
void tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::StartRadSearch ( int  x,
int  y,
int  max_radius 
)

Definition at line 735 of file bbgrid.h.

735 {
736 // Rad search uses x_origin_ and y_origin_ as the center of the circle.
737 // The radius_ is the radius of the (diamond-shaped) circle and
738 // rad_index/rad_dir_ combine to determine the position around it.
739 max_radius_ = max_radius;
740 radius_ = 0;
741 rad_index_ = 0;
742 rad_dir_ = 3;
743 CommonStart(x, y);
744}

◆ StartRectSearch()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
void tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::StartRectSearch ( const TBOX rect)

Definition at line 884 of file bbgrid.h.

884 {
885 // Rect search records the xmin in x_origin_, the ymin in y_origin_
886 // and the xmax in max_radius_.
887 // The search proceeds left to right, top to bottom.
888 rect_ = rect;
889 CommonStart(rect.left(), rect.top());
890 grid_->GridCoords(rect.right(), rect.bottom(), // - rect.height(),
891 &max_radius_, &y_origin_);
892}

◆ StartSideSearch()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
void tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::StartSideSearch ( int  x,
int  ymin,
int  ymax 
)

Definition at line 788 of file bbgrid.h.

788 {
789 // Right search records the x in x_origin_, the ymax in y_origin_
790 // and the size of the vertical strip to search in radius_.
791 // To guarantee finding overlapping objects of up to twice the
792 // given size, double the height.
793 radius_ = ((ymax - ymin) * 2 + grid_->gridsize_ - 1) / grid_->gridsize_;
794 rad_index_ = 0;
795 CommonStart(x, ymax);
796}

◆ StartVerticalSearch()

template<class BBC , class BBC_CLIST , class BBC_C_IT >
void tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::StartVerticalSearch ( int  xmin,
int  xmax,
int  y 
)

Definition at line 837 of file bbgrid.h.

837 {
838 // Right search records the xmin in x_origin_, the y in y_origin_
839 // and the size of the horizontal strip to search in radius_.
840 radius_ = (xmax - xmin + grid_->gridsize_ - 1) / grid_->gridsize_;
841 rad_index_ = 0;
842 CommonStart(xmin, y);
843}

The documentation for this class was generated from the following file: