tesseract  4.0.0-1-g2a2b
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 49 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 238 of file bbgrid.h.

239  : grid_(grid), unique_mode_(false),
240  previous_return_(nullptr), next_return_(nullptr) {
241  }

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 244 of file bbgrid.h.

244  {
245  return x_;
246  }

◆ 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 247 of file bbgrid.h.

247  {
248  return y_;
249  }

◆ NextFullSearch()

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

Definition at line 677 of file bbgrid.h.

677  {
678  int x;
679  int y;
680  do {
681  while (it_.cycled_list()) {
682  ++x_;
683  if (x_ >= grid_->gridwidth_) {
684  --y_;
685  if (y_ < 0)
686  return CommonEnd();
687  x_ = 0;
688  }
689  SetIterator();
690  }
691  CommonNext();
692  TBOX box = previous_return_->bounding_box();
693  grid_->GridCoords(box.left(), box.bottom(), &x, &y);
694  } while (x != x_ || y != y_);
695  return previous_return_;
696 }
Definition: rect.h:34
int16_t left() const
Definition: rect.h:72
int16_t bottom() const
Definition: rect.h:65

◆ NextRadSearch()

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

Definition at line 715 of file bbgrid.h.

715  {
716  do {
717  while (it_.cycled_list()) {
718  ++rad_index_;
719  if (rad_index_ >= radius_) {
720  ++rad_dir_;
721  rad_index_ = 0;
722  if (rad_dir_ >= 4) {
723  ++radius_;
724  if (radius_ > max_radius_)
725  return CommonEnd();
726  rad_dir_ = 0;
727  }
728  }
729  ICOORD offset = C_OUTLINE::chain_step(rad_dir_);
730  offset *= radius_ - rad_index_;
731  offset += C_OUTLINE::chain_step(rad_dir_ + 1) * rad_index_;
732  x_ = x_origin_ + offset.x();
733  y_ = y_origin_ + offset.y();
734  if (x_ >= 0 && x_ < grid_->gridwidth_ &&
735  y_ >= 0 && y_ < grid_->gridheight_)
736  SetIterator();
737  }
738  CommonNext();
739  } while (unique_mode_ && returns_.find(previous_return_) != returns_.end());
740  if (unique_mode_)
741  returns_.insert(previous_return_);
742  return previous_return_;
743 }
int16_t y() const
access_function
Definition: points.h:57
static ICOORD chain_step(int chaindir)
Definition: coutln.cpp:1050
integer coordinate
Definition: points.h:32
int16_t x() const
access function
Definition: points.h:53

◆ NextRectSearch()

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

Definition at line 844 of file bbgrid.h.

844  {
845  do {
846  while (it_.cycled_list()) {
847  ++x_;
848  if (x_ > max_radius_) {
849  --y_;
850  x_ = x_origin_;
851  if (y_ < y_origin_)
852  return CommonEnd();
853  }
854  SetIterator();
855  }
856  CommonNext();
857  } while (!rect_.overlap(previous_return_->bounding_box()) ||
858  (unique_mode_ && returns_.find(previous_return_) != returns_.end()));
859  if (unique_mode_)
860  returns_.insert(previous_return_);
861  return previous_return_;
862 }
bool overlap(const TBOX &box) const
Definition: rect.h:355

◆ 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 763 of file bbgrid.h.

763  {
764  do {
765  while (it_.cycled_list()) {
766  ++rad_index_;
767  if (rad_index_ > radius_) {
768  if (right_to_left)
769  --x_;
770  else
771  ++x_;
772  rad_index_ = 0;
773  if (x_ < 0 || x_ >= grid_->gridwidth_)
774  return CommonEnd();
775  }
776  y_ = y_origin_ - rad_index_;
777  if (y_ >= 0 && y_ < grid_->gridheight_)
778  SetIterator();
779  }
780  CommonNext();
781  } while (unique_mode_ && returns_.find(previous_return_) != returns_.end());
782  if (unique_mode_)
783  returns_.insert(previous_return_);
784  return previous_return_;
785 }

◆ 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 804 of file bbgrid.h.

805  {
806  do {
807  while (it_.cycled_list()) {
808  ++rad_index_;
809  if (rad_index_ > radius_) {
810  if (top_to_bottom)
811  --y_;
812  else
813  ++y_;
814  rad_index_ = 0;
815  if (y_ < 0 || y_ >= grid_->gridheight_)
816  return CommonEnd();
817  }
818  x_ = x_origin_ + rad_index_;
819  if (x_ >= 0 && x_ < grid_->gridwidth_)
820  SetIterator();
821  }
822  CommonNext();
823  } while (unique_mode_ && returns_.find(previous_return_) != returns_.end());
824  if (unique_mode_)
825  returns_.insert(previous_return_);
826  return previous_return_;
827 }

◆ RemoveBBox()

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

Definition at line 868 of file bbgrid.h.

868  {
869  if (previous_return_ != nullptr) {
870  // Remove all instances of previous_return_ from the list, so the iterator
871  // remains valid after removal from the rest of the grid cells.
872  // if previous_return_ is not on the list, then it has been removed already.
873  BBC* prev_data = nullptr;
874  BBC* new_previous_return = nullptr;
875  it_.move_to_first();
876  for (it_.mark_cycle_pt(); !it_.cycled_list();) {
877  if (it_.data() == previous_return_) {
878  new_previous_return = prev_data;
879  it_.extract();
880  it_.forward();
881  next_return_ = it_.cycled_list() ? nullptr : it_.data();
882  } else {
883  prev_data = it_.data();
884  it_.forward();
885  }
886  }
887  grid_->RemoveBBox(previous_return_);
888  previous_return_ = new_previous_return;
890  }
891 }
void RepositionIterator()
Definition: bbgrid.h:894

◆ RepositionIterator()

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

Definition at line 894 of file bbgrid.h.

894  {
895  // Something was deleted, so we have little choice but to clear the
896  // returns list.
897  returns_.clear();
898  // Reset the iterator back to one past the previous return.
899  // If the previous_return_ is no longer in the list, then
900  // next_return_ serves as a backup.
901  it_.move_to_first();
902  // Special case, the first element was removed and reposition
903  // iterator was called. In this case, the data is fine, but the
904  // cycle point is not. Detect it and return.
905  if (!it_.empty() && it_.data() == next_return_) {
906  it_.mark_cycle_pt();
907  return;
908  }
909  for (it_.mark_cycle_pt(); !it_.cycled_list(); it_.forward()) {
910  if (it_.data() == previous_return_ ||
911  it_.data_relative(1) == next_return_) {
912  CommonNext();
913  return;
914  }
915  }
916  // We ran off the end of the list. Move to a new cell next time.
917  previous_return_ = nullptr;
918  next_return_ = nullptr;
919 }

◆ 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 266 of file bbgrid.h.

266  {
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;
270  int grid_x, grid_y;
271  grid_->GridCoords(x_center, y_center, &grid_x, &grid_y);
272  return (x_ == grid_x) && (y_ == grid_y);
273  }
Definition: rect.h:34
int16_t left() const
Definition: rect.h:72
int16_t top() const
Definition: rect.h:58
int16_t right() const
Definition: rect.h:79
int16_t bottom() const
Definition: rect.h:65

◆ 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 255 of file bbgrid.h.

255  {
256  unique_mode_ = mode;
257  }

◆ StartFullSearch()

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

Definition at line 667 of file bbgrid.h.

667  {
668  // Full search uses x_ and y_ as the current grid
669  // cell being searched.
670  CommonStart(grid_->bleft_.x(), grid_->tright_.y());
671 }

◆ 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 700 of file bbgrid.h.

701  {
702  // Rad search uses x_origin_ and y_origin_ as the center of the circle.
703  // The radius_ is the radius of the (diamond-shaped) circle and
704  // rad_index/rad_dir_ combine to determine the position around it.
705  max_radius_ = max_radius;
706  radius_ = 0;
707  rad_index_ = 0;
708  rad_dir_ = 3;
709  CommonStart(x, y);
710 }

◆ 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 832 of file bbgrid.h.

832  {
833  // Rect search records the xmin in x_origin_, the ymin in y_origin_
834  // and the xmax in max_radius_.
835  // The search proceeds left to right, top to bottom.
836  rect_ = rect;
837  CommonStart(rect.left(), rect.top());
838  grid_->GridCoords(rect.right(), rect.bottom(), // - rect.height(),
839  &max_radius_, &y_origin_);
840 }
int16_t left() const
Definition: rect.h:72
int16_t top() const
Definition: rect.h:58
int16_t right() const
Definition: rect.h:79
int16_t bottom() const
Definition: rect.h:65

◆ 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 748 of file bbgrid.h.

749  {
750  // Right search records the x in x_origin_, the ymax in y_origin_
751  // and the size of the vertical strip to search in radius_.
752  // To guarantee finding overlapping objects of up to twice the
753  // given size, double the height.
754  radius_ = ((ymax - ymin) * 2 + grid_->gridsize_ - 1) / grid_->gridsize_;
755  rad_index_ = 0;
756  CommonStart(x, ymax);
757 }

◆ 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 790 of file bbgrid.h.

792  {
793  // Right search records the xmin in x_origin_, the y in y_origin_
794  // and the size of the horizontal strip to search in radius_.
795  radius_ = (xmax - xmin + grid_->gridsize_ - 1) / grid_->gridsize_;
796  rad_index_ = 0;
797  CommonStart(xmin, y);
798 }

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