Tesseract  3.02
tesseract::GridSearch< BBC, BBC_CLIST, BBC_C_IT > Class Template Reference

#include <bbgrid.h>

List of all members.

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 232 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.

      : grid_(grid), unique_mode_(false),
        previous_return_(NULL), next_return_(NULL) {
  }

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.

                    {
    return x_;
  }
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.

                    {
    return y_;
  }
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.

                                                          {
  int x;
  int y;
  do {
    while (it_.cycled_list()) {
      ++x_;
      if (x_ >= grid_->gridwidth_) {
        --y_;
        if (y_ < 0)
          return CommonEnd();
        x_ = 0;
      }
      SetIterator();
    }
    CommonNext();
    TBOX box = previous_return_->bounding_box();
    grid_->GridCoords(box.left(), box.bottom(), &x, &y);
  } while (x != x_ || y != y_);
  return previous_return_;
}
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.

                                                         {
  do {
    while (it_.cycled_list()) {
      ++rad_index_;
      if (rad_index_ >= radius_) {
        ++rad_dir_;
        rad_index_ = 0;
        if (rad_dir_ >= 4) {
          ++radius_;
          if (radius_ > max_radius_)
            return CommonEnd();
          rad_dir_ = 0;
        }
      }
      ICOORD offset = C_OUTLINE::chain_step(rad_dir_);
      offset *= radius_ - rad_index_;
      offset += C_OUTLINE::chain_step(rad_dir_ + 1) * rad_index_;
      x_ = x_origin_ + offset.x();
      y_ = y_origin_ + offset.y();
      if (x_ >= 0 && x_ < grid_->gridwidth_ &&
          y_ >= 0 && y_ < grid_->gridheight_)
        SetIterator();
    }
    CommonNext();
  } while (unique_mode_ &&
           !returns_.add_sorted(SortByBoxLeft<BBC>, true, previous_return_));
  return previous_return_;
}
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.

                                                          {
  do {
    while (it_.cycled_list()) {
      ++x_;
      if (x_ > max_radius_) {
        --y_;
        x_ = x_origin_;
        if (y_ < y_origin_)
          return CommonEnd();
      }
      SetIterator();
    }
    CommonNext();
  } while (!rect_.overlap(previous_return_->bounding_box()) ||
           (unique_mode_ &&
            !returns_.add_sorted(SortByBoxLeft<BBC>, true, previous_return_)));
  return previous_return_;
}
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.

                                                                            {
  do {
    while (it_.cycled_list()) {
      ++rad_index_;
      if (rad_index_ > radius_) {
        if (right_to_left)
          --x_;
        else
          ++x_;
        rad_index_ = 0;
        if (x_ < 0 || x_ >= grid_->gridwidth_)
          return CommonEnd();
      }
      y_ = y_origin_ - rad_index_;
      if (y_ >= 0 && y_ < grid_->gridheight_)
        SetIterator();
    }
    CommonNext();
  } while (unique_mode_ &&
           !returns_.add_sorted(SortByBoxLeft<BBC>, true, previous_return_));
  return previous_return_;
}
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.

                        {
  do {
    while (it_.cycled_list()) {
      ++rad_index_;
      if (rad_index_ > radius_) {
        if (top_to_bottom)
          --y_;
        else
          ++y_;
        rad_index_ = 0;
        if (y_ < 0 || y_ >= grid_->gridheight_)
          return CommonEnd();
      }
      x_ = x_origin_ + rad_index_;
      if (x_ >= 0 && x_ < grid_->gridwidth_)
        SetIterator();
    }
    CommonNext();
  } while (unique_mode_ &&
           !returns_.add_sorted(SortByBoxLeft<BBC>, true, previous_return_));
  return previous_return_;
}
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.

                                                      {
  if (previous_return_ != NULL) {
    // Remove all instances of previous_return_ from the list, so the iterator
    // remains valid after removal from the rest of the grid cells.
    // if previous_return_ is not on the list, then it has been removed already.
    BBC* prev_data = NULL;
    BBC* new_previous_return = NULL;
    it_.move_to_first();
    for (it_.mark_cycle_pt(); !it_.cycled_list();) {
      if (it_.data() ==  previous_return_) {
        new_previous_return = prev_data;
        it_.extract();
        it_.forward();
        next_return_ = it_.cycled_list() ? NULL : it_.data();
      } else {
        prev_data = it_.data();
        it_.forward();
      }
    }
    grid_->RemoveBBox(previous_return_);
    previous_return_ = new_previous_return;
    RepositionIterator();
  }
}
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.

                                                              {
  // Something was deleted, so we have little choice but to clear the
  // returns list.
  returns_.shallow_clear();
  // Reset the iterator back to one past the previous return.
  // If the previous_return_ is no longer in the list, then
  // next_return_ serves as a backup.
  it_.move_to_first();
  // Special case, the first element was removed and reposition
  // iterator was called. In this case, the data is fine, but the
  // cycle point is not. Detect it and return.
  if (!it_.empty() && it_.data() == next_return_) {
    it_.mark_cycle_pt();
    return;
  }
  for (it_.mark_cycle_pt(); !it_.cycled_list(); it_.forward()) {
    if (it_.data() == previous_return_ ||
        it_.data_relative(1) == next_return_) {
      CommonNext();
      return;
    }
  }
  // We ran off the end of the list. Move to a new cell next time.
  previous_return_ = NULL;
  next_return_ = NULL;
}
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.

                                   {
    TBOX box = previous_return_->bounding_box();
    int x_center = (box.left()+box.right())/2;
    int y_center = (box.top()+box.bottom())/2;
    int grid_x, grid_y;
    grid_->GridCoords(x_center, y_center, &grid_x, &grid_y);
    return (x_ == grid_x) && (y_ == grid_y);
  }
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.

                                {
    unique_mode_ = mode;
  }
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.

                                                           {
  // Full search uses x_ and y_ as the current grid
  // cell being searched.
  CommonStart(grid_->bleft_.x(), grid_->tright_.y());
}
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.

                                                                          {
  // Rad search uses x_origin_ and y_origin_ as the center of the circle.
  // The radius_ is the radius of the (diamond-shaped) circle and
  // rad_index/rad_dir_ combine to determine the position around it.
  max_radius_ = max_radius;
  radius_ = 0;
  rad_index_ = 0;
  rad_dir_ = 3;
  CommonStart(x, y);
}
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.

                                                                           {
  // Rect search records the xmin in x_origin_, the ymin in y_origin_
  // and the xmax in max_radius_.
  // The search proceeds left to right, top to bottom.
  rect_ = rect;
  CommonStart(rect.left(), rect.top());
  grid_->GridCoords(rect.right(), rect.bottom(),  // - rect.height(),
                    &max_radius_, &y_origin_);
}
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.

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

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

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