Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 53 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 234 of file bbgrid.h.

235  : grid_(grid), unique_mode_(false),
236  previous_return_(NULL), next_return_(NULL) {
237  }
#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 240 of file bbgrid.h.

240  {
241  return x_;
242  }
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 243 of file bbgrid.h.

243  {
244  return y_;
245  }
template<class BBC , class BBC_CLIST , class BBC_C_IT >
BBC * tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::NextFullSearch ( )

Definition at line 675 of file bbgrid.h.

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

Definition at line 713 of file bbgrid.h.

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

Definition at line 839 of file bbgrid.h.

839  {
840  do {
841  while (it_.cycled_list()) {
842  ++x_;
843  if (x_ > max_radius_) {
844  --y_;
845  x_ = x_origin_;
846  if (y_ < y_origin_)
847  return CommonEnd();
848  }
849  SetIterator();
850  }
851  CommonNext();
852  } while (!rect_.overlap(previous_return_->bounding_box()) ||
853  (unique_mode_ &&
854  !returns_.add_sorted(SortByBoxLeft<BBC>, true, previous_return_)));
855  return previous_return_;
856 }
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 760 of file bbgrid.h.

760  {
761  do {
762  while (it_.cycled_list()) {
763  ++rad_index_;
764  if (rad_index_ > radius_) {
765  if (right_to_left)
766  --x_;
767  else
768  ++x_;
769  rad_index_ = 0;
770  if (x_ < 0 || x_ >= grid_->gridwidth_)
771  return CommonEnd();
772  }
773  y_ = y_origin_ - rad_index_;
774  if (y_ >= 0 && y_ < grid_->gridheight_)
775  SetIterator();
776  }
777  CommonNext();
778  } while (unique_mode_ &&
779  !returns_.add_sorted(SortByBoxLeft<BBC>, true, previous_return_));
780  return previous_return_;
781 }
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 800 of file bbgrid.h.

801  {
802  do {
803  while (it_.cycled_list()) {
804  ++rad_index_;
805  if (rad_index_ > radius_) {
806  if (top_to_bottom)
807  --y_;
808  else
809  ++y_;
810  rad_index_ = 0;
811  if (y_ < 0 || y_ >= grid_->gridheight_)
812  return CommonEnd();
813  }
814  x_ = x_origin_ + rad_index_;
815  if (x_ >= 0 && x_ < grid_->gridwidth_)
816  SetIterator();
817  }
818  CommonNext();
819  } while (unique_mode_ &&
820  !returns_.add_sorted(SortByBoxLeft<BBC>, true, previous_return_));
821  return previous_return_;
822 }
template<class BBC , class BBC_CLIST , class BBC_C_IT >
void tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::RemoveBBox ( )

Definition at line 862 of file bbgrid.h.

862  {
863  if (previous_return_ != NULL) {
864  // Remove all instances of previous_return_ from the list, so the iterator
865  // remains valid after removal from the rest of the grid cells.
866  // if previous_return_ is not on the list, then it has been removed already.
867  BBC* prev_data = NULL;
868  BBC* new_previous_return = NULL;
869  it_.move_to_first();
870  for (it_.mark_cycle_pt(); !it_.cycled_list();) {
871  if (it_.data() == previous_return_) {
872  new_previous_return = prev_data;
873  it_.extract();
874  it_.forward();
875  next_return_ = it_.cycled_list() ? NULL : it_.data();
876  } else {
877  prev_data = it_.data();
878  it_.forward();
879  }
880  }
881  grid_->RemoveBBox(previous_return_);
882  previous_return_ = new_previous_return;
884  }
885 }
void RepositionIterator()
Definition: bbgrid.h:888
#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 888 of file bbgrid.h.

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

262  {
263  TBOX box = previous_return_->bounding_box();
264  int x_center = (box.left()+box.right())/2;
265  int y_center = (box.top()+box.bottom())/2;
266  int grid_x, grid_y;
267  grid_->GridCoords(x_center, y_center, &grid_x, &grid_y);
268  return (x_ == grid_x) && (y_ == grid_y);
269  }
inT16 left() const
Definition: rect.h:67
Definition: rect.h:29
inT16 right() const
Definition: rect.h:74
inT16 top() const
Definition: rect.h:53
inT16 bottom() const
Definition: rect.h:60
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 251 of file bbgrid.h.

251  {
252  unique_mode_ = mode;
253  }
CMD_EVENTS mode
Definition: pgedit.cpp:115
template<class BBC , class BBC_CLIST , class BBC_C_IT >
void tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT >::StartFullSearch ( )

Definition at line 665 of file bbgrid.h.

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

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

827  {
828  // Rect search records the xmin in x_origin_, the ymin in y_origin_
829  // and the xmax in max_radius_.
830  // The search proceeds left to right, top to bottom.
831  rect_ = rect;
832  CommonStart(rect.left(), rect.top());
833  grid_->GridCoords(rect.right(), rect.bottom(), // - rect.height(),
834  &max_radius_, &y_origin_);
835 }
inT16 left() const
Definition: rect.h:67
inT16 right() const
Definition: rect.h:74
inT16 top() const
Definition: rect.h:53
inT16 bottom() const
Definition: rect.h:60
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 745 of file bbgrid.h.

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

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

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