All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 48 of file bbgrid.h.

Constructor & Destructor Documentation

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

238  : grid_(grid), unique_mode_(false),
239  previous_return_(NULL), next_return_(NULL) {
240  }
#define NULL
Definition: host.h:144

Member Function Documentation

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

243  {
244  return x_;
245  }
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 246 of file bbgrid.h.

246  {
247  return y_;
248  }
template<class BBC , class BBC_CLIST , class BBC_C_IT >
BBC * tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::NextFullSearch ( )

Definition at line 678 of file bbgrid.h.

678  {
679  int x;
680  int y;
681  do {
682  while (it_.cycled_list()) {
683  ++x_;
684  if (x_ >= grid_->gridwidth_) {
685  --y_;
686  if (y_ < 0)
687  return CommonEnd();
688  x_ = 0;
689  }
690  SetIterator();
691  }
692  CommonNext();
693  TBOX box = previous_return_->bounding_box();
694  grid_->GridCoords(box.left(), box.bottom(), &x, &y);
695  } while (x != x_ || y != y_);
696  return previous_return_;
697 }
inT16 left() const
Definition: rect.h:68
inT16 bottom() const
Definition: rect.h:61
Definition: rect.h:30
template<class BBC , class BBC_CLIST , class BBC_C_IT >
BBC * tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::NextRadSearch ( )

Definition at line 716 of file bbgrid.h.

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

Definition at line 845 of file bbgrid.h.

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

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

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

Definition at line 869 of file bbgrid.h.

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

Definition at line 895 of file bbgrid.h.

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

265  {
266  TBOX box = previous_return_->bounding_box();
267  int x_center = (box.left()+box.right())/2;
268  int y_center = (box.top()+box.bottom())/2;
269  int grid_x, grid_y;
270  grid_->GridCoords(x_center, y_center, &grid_x, &grid_y);
271  return (x_ == grid_x) && (y_ == grid_y);
272  }
inT16 right() const
Definition: rect.h:75
inT16 left() const
Definition: rect.h:68
inT16 bottom() const
Definition: rect.h:61
Definition: rect.h:30
inT16 top() const
Definition: rect.h:54
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 254 of file bbgrid.h.

254  {
255  unique_mode_ = mode;
256  }
CMD_EVENTS mode
Definition: pgedit.cpp:116
template<class BBC , class BBC_CLIST , class BBC_C_IT >
void tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::StartFullSearch ( )

Definition at line 668 of file bbgrid.h.

668  {
669  // Full search uses x_ and y_ as the current grid
670  // cell being searched.
671  CommonStart(grid_->bleft_.x(), grid_->tright_.y());
672 }
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 701 of file bbgrid.h.

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

833  {
834  // Rect search records the xmin in x_origin_, the ymin in y_origin_
835  // and the xmax in max_radius_.
836  // The search proceeds left to right, top to bottom.
837  rect_ = rect;
838  CommonStart(rect.left(), rect.top());
839  grid_->GridCoords(rect.right(), rect.bottom(), // - rect.height(),
840  &max_radius_, &y_origin_);
841 }
inT16 right() const
Definition: rect.h:75
inT16 left() const
Definition: rect.h:68
inT16 bottom() const
Definition: rect.h:61
inT16 top() const
Definition: rect.h:54
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 749 of file bbgrid.h.

750  {
751  // Right search records the x in x_origin_, the ymax in y_origin_
752  // and the size of the vertical strip to search in radius_.
753  // To guarantee finding overlapping objects of upto twice the
754  // given size, double the height.
755  radius_ = ((ymax - ymin) * 2 + grid_->gridsize_ - 1) / grid_->gridsize_;
756  rad_index_ = 0;
757  CommonStart(x, ymax);
758 }
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 791 of file bbgrid.h.

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

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