Tesseract 3.01
/data/source/tesseract-ocr/textord/bbgrid.h
Go to the documentation of this file.
00001 
00002 // File:        bbgrid.h
00003 // Description: Class to hold BLOBNBOXs in a grid for fast access
00004 //              to neighbours.
00005 // Author:      Ray Smith
00006 // Created:     Wed Jun 06 17:22:01 PDT 2007
00007 //
00008 // (C) Copyright 2007, Google Inc.
00009 // Licensed under the Apache License, Version 2.0 (the "License");
00010 // you may not use this file except in compliance with the License.
00011 // You may obtain a copy of the License at
00012 // http://www.apache.org/licenses/LICENSE-2.0
00013 // Unless required by applicable law or agreed to in writing, software
00014 // distributed under the License is distributed on an "AS IS" BASIS,
00015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016 // See the License for the specific language governing permissions and
00017 // limitations under the License.
00018 //
00020 
00021 #ifndef TESSERACT_TEXTORD_BBGRID_H__
00022 #define TESSERACT_TEXTORD_BBGRID_H__
00023 
00024 #include "clst.h"
00025 #include "coutln.h"
00026 #include "rect.h"
00027 #include "scrollview.h"
00028 
00029 // Some code is dependent upon leptonica. If you don't have it,
00030 // you don't get this functionality.
00031 #ifdef HAVE_CONFIG_H
00032 #include "config_auto.h"
00033 #endif
00034 
00035 #include "allheaders.h"
00036 
00037 class BLOCK;
00038 
00039 namespace tesseract {
00040 
00041 // Helper function to return a scaled Pix with one pixel per grid cell,
00042 // set (black) where the given outline enters the corresponding grid cell,
00043 // and clear where the outline does not touch the grid cell.
00044 // Also returns the grid coords of the bottom-left of the Pix, in *left
00045 // and *bottom, which corresponds to (0, 0) on the Pix.
00046 // Note that the Pix is used upside-down, with (0, 0) being the bottom-left.
00047 Pix* TraceOutlineOnReducedPix(C_OUTLINE* outline, int gridsize,
00048                               ICOORD bleft, int* left, int* bottom);
00049 // As TraceOutlineOnReducedPix above, but on a BLOCK instead of a C_OUTLINE.
00050 Pix* TraceBlockOnReducedPix(BLOCK* block, int gridsize,
00051                             ICOORD bleft, int* left, int* bottom);
00052 
00053 template<class BBC, class BBC_CLIST, class BBC_C_IT> class GridSearch;
00054 
00055 // The GridBase class is the base class for BBGrid and IntGrid.
00056 // It holds the geometry and scale of the grid.
00057 class GridBase {
00058  public:
00059   GridBase();
00060   GridBase(int gridsize, const ICOORD& bleft, const ICOORD& tright);
00061   virtual ~GridBase();
00062 
00063   // (Re)Initialize the grid. The gridsize is the size in pixels of each cell,
00064   // and bleft, tright are the bounding box of everything to go in it.
00065   void Init(int gridsize, const ICOORD& bleft, const ICOORD& tright);
00066 
00067   // Simple accessors.
00068   int gridsize() const {
00069     return gridsize_;
00070   }
00071   int gridwidth() const {
00072     return gridwidth_;
00073   }
00074   int gridheight() const {
00075     return gridheight_;
00076   }
00077   const ICOORD& bleft() const {
00078     return bleft_;
00079   }
00080   const ICOORD& tright() const {
00081     return tright_;
00082   }
00083   // Compute the given grid coordinates from image coords.
00084   void GridCoords(int x, int y, int* grid_x, int* grid_y) const;
00085 
00086   // Clip the given grid coordinates to fit within the grid.
00087   void ClipGridCoords(int* x, int* y) const;
00088 
00089  protected:
00090   // TODO(rays) Make these private and migrate to the accessors in subclasses.
00091   int gridsize_;     // Pixel size of each grid cell.
00092   int gridwidth_;    // Size of the grid in cells.
00093   int gridheight_;
00094   int gridbuckets_;  // Total cells in grid.
00095   ICOORD bleft_;     // Pixel coords of bottom-left of grid.
00096   ICOORD tright_;    // Pixel coords of top-right of grid.
00097 
00098  private:
00099 };
00100 
00101 // The IntGrid maintains a single int for each cell in a grid.
00102 class IntGrid : public GridBase {
00103  public:
00104   IntGrid();
00105   IntGrid(int gridsize, const ICOORD& bleft, const ICOORD& tright);
00106   virtual ~IntGrid();
00107 
00108   // (Re)Initialize the grid. The gridsize is the size in pixels of each cell,
00109   // and bleft, tright are the bounding box of everything to go in it.
00110   void Init(int gridsize, const ICOORD& bleft, const ICOORD& tright);
00111 
00112   // Clear all the ints in the grid to zero.
00113   void Clear();
00114 
00115   // Rotate the grid by rotation, keeping cell contents.
00116   // rotation must be a multiple of 90 degrees.
00117   // NOTE: due to partial cells, cell coverage in the rotated grid will be
00118   // inexact. This is why there is no Rotate for the generic BBGrid.
00119   void Rotate(const FCOORD& rotation);
00120 
00121   // Returns a new IntGrid containing values equal to the sum of all the
00122   // neighbouring cells. The returned grid must be deleted after use.
00123   IntGrid* NeighbourhoodSum() const;
00124 
00125   int GridCellValue(int grid_x, int grid_y) const {
00126     ASSERT_HOST(grid_x >= 0 && grid_x < gridwidth());
00127     ASSERT_HOST(grid_y >= 0 && grid_y < gridheight());
00128     return grid_[grid_y * gridwidth_ + grid_x];
00129   }
00130   void SetGridCell(int grid_x, int grid_y, int value) {
00131     ASSERT_HOST(grid_x >= 0 && grid_x < gridwidth());
00132     ASSERT_HOST(grid_y >= 0 && grid_y < gridheight());
00133     grid_[grid_y * gridwidth_ + grid_x] = value;
00134   }
00135 
00136  private:
00137   int* grid_;  // 2-d array of ints.
00138 };
00139 
00140 // The BBGrid class holds C_LISTs of template classes BBC (bounding box class)
00141 // in a grid for fast neighbour access.
00142 // The BBC class must have a member const TBOX& bounding_box() const.
00143 // The BBC class must have been CLISTIZEH'ed elsewhere to make the
00144 // list class BBC_CLIST and the iterator BBC_C_IT.
00145 // Use of C_LISTs enables BBCs to exist in multiple cells simultaneously.
00146 // As a consequence, ownership of BBCs is assumed to be elsewhere and
00147 // persistent for at least the life of the BBGrid, or at least until Clear is
00148 // called which removes all references to inserted objects without actually
00149 // deleting them.
00150 // Most uses derive a class from a specific instantiation of BBGrid,
00151 // thereby making most of the ugly template notation go away.
00152 // The friend class GridSearch, with the same template arguments, is
00153 // used to search a grid efficiently in one of several search patterns.
00154 template<class BBC, class BBC_CLIST, class BBC_C_IT> class BBGrid
00155   : public GridBase {
00156   friend class GridSearch<BBC, BBC_CLIST, BBC_C_IT>;
00157  public:
00158   BBGrid();
00159   BBGrid(int gridsize, const ICOORD& bleft, const ICOORD& tright);
00160   virtual ~BBGrid();
00161 
00162   // (Re)Initialize the grid. The gridsize is the size in pixels of each cell,
00163   // and bleft, tright are the bounding box of everything to go in it.
00164   void Init(int gridsize, const ICOORD& bleft, const ICOORD& tright);
00165 
00166   // Empty all the lists but leave the grid itself intact.
00167   void Clear();
00168   // Deallocate the data in the lists but otherwise leave the lists and the grid
00169   // intact.
00170   void ClearGridData(void (*free_method)(BBC*));
00171 
00172   // Insert a bbox into the appropriate place in the grid.
00173   // If h_spread, then all cells covered horizontally by the box are
00174   // used, otherwise, just the bottom-left. Similarly for v_spread.
00175   // WARNING: InsertBBox may invalidate an active GridSearch. Call
00176   // RepositionIterator() on any GridSearches that are active on this grid.
00177   void InsertBBox(bool h_spread, bool v_spread, BBC* bbox);
00178 
00179   // Using a pix from TraceOutlineOnReducedPix or TraceBlockOnReducedPix, in
00180   // which each pixel corresponds to a grid cell, insert a bbox into every
00181   // place in the grid where the corresponding pixel is 1. The Pix is handled
00182   // upside-down to match the Tesseract coordinate system. (As created by
00183   // TraceOutlineOnReducedPix or TraceBlockOnReducedPix.)
00184   // (0, 0) in the pix corresponds to (left, bottom) in the
00185   // grid (in grid coords), and the pix works up the grid from there.
00186   // WARNING: InsertPixPtBBox may invalidate an active GridSearch. Call
00187   // RepositionIterator() on any GridSearches that are active on this grid.
00188   void InsertPixPtBBox(int left, int bottom, Pix* pix, BBC* bbox);
00189 
00190   // Remove the bbox from the grid.
00191   // WARNING: Any GridSearch operating on this grid could be invalidated!
00192   // If a GridSearch is operating, call GridSearch::RemoveBBox() instead.
00193   void RemoveBBox(BBC* bbox);
00194 
00195   // Returns true if the given rectangle has no overlapping elements.
00196   bool RectangleEmpty(const TBOX& rect);
00197 
00198   // Returns an IntGrid showing the number of elements in each cell.
00199   // Returned IntGrid must be deleted after use.
00200   IntGrid* CountCellElements();
00201 
00202   // Make a window of an appropriate size to display things in the grid.
00203   ScrollView* MakeWindow(int x, int y, const char* window_name);
00204 
00205   // Display the bounding boxes of the BLOBNBOXes in this grid.
00206   // Use of this function requires an additional member of the BBC class:
00207   // ScrollView::Color BBC::BoxColor() const.
00208   void DisplayBoxes(ScrollView* window);
00209 
00210   // ASSERT_HOST that every cell contains no more than one copy of each entry.
00211   void AssertNoDuplicates();
00212 
00213   // Handle a click event in a display window.
00214   virtual void HandleClick(int x, int y);
00215 
00216  protected:
00217   BBC_CLIST* grid_;  // 2-d array of CLISTS of BBC elements.
00218 
00219  private:
00220 };
00221 
00222 // The GridSearch class enables neighbourhood searching on a BBGrid.
00223 template<class BBC, class BBC_CLIST, class BBC_C_IT> class GridSearch {
00224  public:
00225   GridSearch(BBGrid<BBC, BBC_CLIST, BBC_C_IT>* grid)
00226       : grid_(grid), unique_mode_(false),
00227         previous_return_(NULL), next_return_(NULL) {
00228   }
00229 
00230   // Get the grid x, y coords of the most recently returned BBC.
00231   int GridX() const {
00232     return x_;
00233   }
00234   int GridY() const {
00235     return y_;
00236   }
00237 
00238   // Sets the search mode to return a box only once.
00239   // Efficiency warning: Implementation currently uses a squared-order
00240   // search in the number of returned elements. Use only where a small
00241   // number of elements are spread over a wide area, eg ColPartitions.
00242   void SetUniqueMode(bool mode) {
00243     unique_mode_ = mode;
00244   }
00245   // TODO(rays) Replace calls to ReturnedSeedElement with SetUniqueMode.
00246   // It only works if the search includes the bottom-left corner.
00247   // Apart from full search, all other searches return a box several
00248   // times if the box is inserted with h_spread or v_spread.
00249   // This method will return true for only one occurrence of each box
00250   // that was inserted with both h_spread and v_spread as true.
00251   // It will usually return false for boxes that were not inserted with
00252   // both h_spread=true and v_spread=true
00253   bool ReturnedSeedElement() const {
00254     TBOX box = previous_return_->bounding_box();
00255     int x_center = (box.left()+box.right())/2;
00256     int y_center = (box.top()+box.bottom())/2;
00257     int grid_x, grid_y;
00258     grid_->GridCoords(x_center, y_center, &grid_x, &grid_y);
00259     return (x_ == grid_x) && (y_ == grid_y);
00260   }
00261 
00262   // Various searching iterations... Note that these iterations
00263   // all share data members, so you can't run more than one iteration
00264   // in parallel in a single GridSearch instance, but multiple instances
00265   // can search the same BBGrid in parallel.
00266   // Note that all the searches can return blobs that may not exactly
00267   // match the search conditions, since they return everything in the
00268   // covered grid cells. It is up to the caller to check for
00269   // appropriateness.
00270   // TODO(rays) NextRectSearch only returns valid elements. Make the other
00271   // searches test before return also and remove the tests from code
00272   // that uses GridSearch.
00273 
00274   // Start a new full search. Will iterate all stored blobs, from the top.
00275   // If the blobs have been inserted using InsertBBox, (not InsertPixPtBBox)
00276   // then the full search guarantees to return each blob in the grid once.
00277   // Other searches may return a blob more than once if they have been
00278   // inserted using h_spread or v_spread.
00279   void StartFullSearch();
00280   // Return the next bbox in the search or NULL if done.
00281   BBC* NextFullSearch();
00282 
00283   // Start a new radius search. Will search in a spiral upto a
00284   // given maximum radius in grid cells from the given center in pixels.
00285   void StartRadSearch(int x, int y, int max_radius);
00286   // Return the next bbox in the radius search or NULL if the
00287   // maximum radius has been reached.
00288   BBC* NextRadSearch();
00289 
00290   // Start a new left or right-looking search. Will search to the side
00291   // for a box that vertically overlaps the given vertical line segment.
00292   // CAVEAT: This search returns all blobs from the cells to the side
00293   // of the start, and somewhat below, since there is no guarantee
00294   // that there may not be a taller object in a lower cell. The
00295   // blobs returned will include all those that vertically overlap and
00296   // are no more than twice as high, but may also include some that do
00297   // not overlap and some that are more than twice as high.
00298   void StartSideSearch(int x, int ymin, int ymax);
00299   // Return the next bbox in the side search or NULL if the
00300   // edge has been reached. Searches left to right or right to left
00301   // according to the flag.
00302   BBC* NextSideSearch(bool right_to_left);
00303 
00304   // Start a vertical-looking search. Will search up or down
00305   // for a box that horizontally overlaps the given line segment.
00306   void StartVerticalSearch(int xmin, int xmax, int y);
00307   // Return the next bbox in the vertical search or NULL if the
00308   // edge has been reached. Searches top to bottom or bottom to top
00309   // according to the flag.
00310   BBC* NextVerticalSearch(bool top_to_bottom);
00311 
00312   // Start a rectangular search. Will search for a box that overlaps the
00313   // given rectangle.
00314   void StartRectSearch(const TBOX& rect);
00315   // Return the next bbox in the rectangular search or NULL if complete.
00316   BBC* NextRectSearch();
00317 
00318   // Remove the last returned BBC. Will not invalidate this. May invalidate
00319   // any other concurrent GridSearch on the same grid. If any others are
00320   // in use, call RepositionIterator on those, to continue without harm.
00321   void RemoveBBox();
00322   void RepositionIterator();
00323 
00324  private:
00325   // Factored out helper to start a search.
00326   void CommonStart(int x, int y);
00327   // Factored out helper to complete a next search.
00328   BBC* CommonNext();
00329   // Factored out final return when search is exhausted.
00330   BBC* CommonEnd();
00331   // Factored out function to set the iterator to the current x_, y_
00332   // grid coords and mark the cycle pt.
00333   void SetIterator();
00334 
00335  private:
00336   // The grid we are searching.
00337   BBGrid<BBC, BBC_CLIST, BBC_C_IT>* grid_;
00338   // For executing a search. The different search algorithms use these in
00339   // different ways, but most use x_origin_ and y_origin_ as the start position.
00340   int x_origin_;
00341   int y_origin_;
00342   int max_radius_;
00343   int radius_;
00344   int rad_index_;
00345   int rad_dir_;
00346   TBOX rect_;
00347   int x_;  // The current location in grid coords, of the current search.
00348   int y_;
00349   bool unique_mode_;
00350   BBC* previous_return_;  // Previous return from Next*.
00351   BBC* next_return_;  // Current value of it_.data() used for repositioning.
00352   // An iterator over the list at (x_, y_) in the grid_.
00353   BBC_C_IT it_;
00354   // List of unique returned elements used when unique_mode_ is true.
00355   BBC_CLIST returns_;
00356 };
00357 
00358 // Sort function to sort a BBC by bounding_box().left().
00359 template<class BBC>
00360 int SortByBoxLeft(const void* void1, const void* void2) {
00361   // The void*s are actually doubly indirected, so get rid of one level.
00362   const BBC* p1 = *reinterpret_cast<const BBC* const *>(void1);
00363   const BBC* p2 = *reinterpret_cast<const BBC* const *>(void2);
00364   int result = p1->bounding_box().left() - p2->bounding_box().left();
00365   if (result != 0)
00366     return result;
00367   result = p1->bounding_box().right() - p2->bounding_box().right();
00368   if (result != 0)
00369     return result;
00370   result = p1->bounding_box().bottom() - p2->bounding_box().bottom();
00371   if (result != 0)
00372     return result;
00373   return p1->bounding_box().top() - p2->bounding_box().top();
00374 }
00375 
00376 // Sort function to sort a BBC by bounding_box().bottom().
00377 template<class BBC>
00378 int SortByBoxBottom(const void* void1, const void* void2) {
00379   // The void*s are actually doubly indirected, so get rid of one level.
00380   const BBC* p1 = *reinterpret_cast<const BBC* const *>(void1);
00381   const BBC* p2 = *reinterpret_cast<const BBC* const *>(void2);
00382   int result = p1->bounding_box().bottom() - p2->bounding_box().bottom();
00383   if (result != 0)
00384     return result;
00385   result =  p1->bounding_box().top() - p2->bounding_box().top();
00386   if (result != 0)
00387     return result;
00388   result = p1->bounding_box().left() - p2->bounding_box().left();
00389   if (result != 0)
00390     return result;
00391   return p1->bounding_box().right() - p2->bounding_box().right();
00392 }
00393 
00395 // BBGrid IMPLEMENTATION.
00397 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00398 BBGrid<BBC, BBC_CLIST, BBC_C_IT>::BBGrid() : grid_(NULL) {
00399 }
00400 
00401 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00402 BBGrid<BBC, BBC_CLIST, BBC_C_IT>::BBGrid(
00403   int gridsize, const ICOORD& bleft, const ICOORD& tright)
00404     : grid_(NULL) {
00405   Init(gridsize, bleft, tright);
00406 }
00407 
00408 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00409 BBGrid<BBC, BBC_CLIST, BBC_C_IT>::~BBGrid() {
00410   if (grid_ != NULL)
00411     delete [] grid_;
00412 }
00413 
00414 // (Re)Initialize the grid. The gridsize is the size in pixels of each cell,
00415 // and bleft, tright are the bounding box of everything to go in it.
00416 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00417 void BBGrid<BBC, BBC_CLIST, BBC_C_IT>::Init(int gridsize,
00418                                             const ICOORD& bleft,
00419                                             const ICOORD& tright) {
00420   GridBase::Init(gridsize, bleft, tright);
00421   if (grid_ != NULL)
00422     delete [] grid_;
00423   grid_ = new BBC_CLIST[gridbuckets_];
00424 }
00425 
00426 // Clear all lists, but leave the array of lists present.
00427 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00428 void BBGrid<BBC, BBC_CLIST, BBC_C_IT>::Clear() {
00429   for (int i = 0; i < gridbuckets_; ++i) {
00430     grid_[i].shallow_clear();
00431   }
00432 }
00433 
00434 // Deallocate the data in the lists but otherwise leave the lists and the grid
00435 // intact.
00436 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00437 void BBGrid<BBC, BBC_CLIST, BBC_C_IT>::ClearGridData(
00438     void (*free_method)(BBC*)) {
00439   if (grid_ == NULL) return;
00440   GridSearch<BBC, BBC_CLIST, BBC_C_IT> search(this);
00441   search.StartFullSearch();
00442   BBC* bb;
00443   BBC_CLIST bb_list;
00444   BBC_C_IT it(&bb_list);
00445   while ((bb = search.NextFullSearch()) != NULL) {
00446     it.add_after_then_move(bb);
00447   }
00448   for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
00449     free_method(it.data());
00450   }
00451 }
00452 
00453 // Insert a bbox into the appropriate place in the grid.
00454 // If h_spread, then all cells covered horizontally by the box are
00455 // used, otherwise, just the bottom-left. Similarly for v_spread.
00456 // WARNING: InsertBBox may invalidate an active GridSearch. Call
00457 // RepositionIterator() on any GridSearches that are active on this grid.
00458 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00459 void BBGrid<BBC, BBC_CLIST, BBC_C_IT>::InsertBBox(bool h_spread, bool v_spread,
00460                                                   BBC* bbox) {
00461   TBOX box = bbox->bounding_box();
00462   int start_x, start_y, end_x, end_y;
00463   GridCoords(box.left(), box.bottom(), &start_x, &start_y);
00464   GridCoords(box.right(), box.top(), &end_x, &end_y);
00465   if (!h_spread)
00466     end_x = start_x;
00467   if (!v_spread)
00468     end_y = start_y;
00469   int grid_index = start_y * gridwidth_;
00470   for (int y = start_y; y <= end_y; ++y, grid_index += gridwidth_) {
00471     for (int x = start_x; x <= end_x; ++x) {
00472       grid_[grid_index + x].add_sorted(SortByBoxLeft<BBC>, true, bbox);
00473     }
00474   }
00475 }
00476 
00477 // Using a pix from TraceOutlineOnReducedPix or TraceBlockOnReducedPix, in
00478 // which each pixel corresponds to a grid cell, insert a bbox into every
00479 // place in the grid where the corresponding pixel is 1. The Pix is handled
00480 // upside-down to match the Tesseract coordinate system. (As created by
00481 // TraceOutlineOnReducedPix or TraceBlockOnReducedPix.)
00482 // (0, 0) in the pix corresponds to (left, bottom) in the
00483 // grid (in grid coords), and the pix works up the grid from there.
00484 // WARNING: InsertPixPtBBox may invalidate an active GridSearch. Call
00485 // RepositionIterator() on any GridSearches that are active on this grid.
00486 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00487 void BBGrid<BBC, BBC_CLIST, BBC_C_IT>::InsertPixPtBBox(int left, int bottom,
00488                                                        Pix* pix, BBC* bbox) {
00489   int width = pixGetWidth(pix);
00490   int height = pixGetHeight(pix);
00491   for (int y = 0; y < height; ++y) {
00492     l_uint32* data = pixGetData(pix) + y * pixGetWpl(pix);
00493     for (int x = 0; x < width; ++x) {
00494       if (GET_DATA_BIT(data, x)) {
00495         grid_[(bottom + y) * gridwidth_ + x + left].
00496           add_sorted(SortByBoxLeft<BBC>, true, bbox);
00497       }
00498     }
00499   }
00500 }
00501 
00502 // Remove the bbox from the grid.
00503 // WARNING: Any GridSearch operating on this grid could be invalidated!
00504 // If a GridSearch is operating, call GridSearch::RemoveBBox() instead.
00505 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00506 void BBGrid<BBC, BBC_CLIST, BBC_C_IT>::RemoveBBox(BBC* bbox) {
00507   TBOX box = bbox->bounding_box();
00508   int start_x, start_y, end_x, end_y;
00509   GridCoords(box.left(), box.bottom(), &start_x, &start_y);
00510   GridCoords(box.right(), box.top(), &end_x, &end_y);
00511   int grid_index = start_y * gridwidth_;
00512   for (int y = start_y; y <= end_y; ++y, grid_index += gridwidth_) {
00513     for (int x = start_x; x <= end_x; ++x) {
00514       BBC_C_IT it(&grid_[grid_index + x]);
00515       for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
00516         if (it.data() == bbox)
00517           it.extract();
00518       }
00519     }
00520   }
00521 }
00522 
00523 // Returns true if the given rectangle has no overlapping elements.
00524 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00525 bool BBGrid<BBC, BBC_CLIST, BBC_C_IT>::RectangleEmpty(const TBOX& rect) {
00526   GridSearch<BBC, BBC_CLIST, BBC_C_IT> rsearch(this);
00527   rsearch.StartRectSearch(rect);
00528   return rsearch.NextRectSearch() == NULL;
00529 }
00530 
00531 // Returns an IntGrid showing the number of elements in each cell.
00532 // Returned IntGrid must be deleted after use.
00533 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00534 IntGrid* BBGrid<BBC, BBC_CLIST, BBC_C_IT>::CountCellElements() {
00535   IntGrid* intgrid = new IntGrid(gridsize(), bleft(), tright());
00536   for (int y = 0; y < gridheight(); ++y) {
00537     for (int x = 0; x < gridwidth(); ++x) {
00538       int cell_count = grid_[y * gridwidth() + x].length();
00539       intgrid->SetGridCell(x, y, cell_count);
00540     }
00541   }
00542   return intgrid;
00543 }
00544 
00545 
00546 template<class G> class TabEventHandler : public SVEventHandler {
00547  public:
00548   explicit TabEventHandler(G* grid) : grid_(grid) {
00549   }
00550   void Notify(const SVEvent* sv_event) {
00551     if (sv_event->type == SVET_CLICK) {
00552       grid_->HandleClick(sv_event->x, sv_event->y);
00553     }
00554   }
00555  private:
00556   G* grid_;
00557 };
00558 
00559 // Make a window of an appropriate size to display things in the grid.
00560 // Position the window at the given x,y.
00561 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00562 ScrollView* BBGrid<BBC, BBC_CLIST, BBC_C_IT>::MakeWindow(
00563     int x, int y, const char* window_name) {
00564   ScrollView* tab_win = NULL;
00565 #ifndef GRAPHICS_DISABLED
00566   tab_win = new ScrollView(window_name, x, y,
00567                            tright_.x() - bleft_.x(),
00568                            tright_.y() - bleft_.y(),
00569                            tright_.x() - bleft_.x(),
00570                            tright_.y() - bleft_.y(),
00571                            true);
00572   TabEventHandler<BBGrid<BBC, BBC_CLIST, BBC_C_IT> >* handler =
00573     new TabEventHandler<BBGrid<BBC, BBC_CLIST, BBC_C_IT> >(this);
00574   tab_win->AddEventHandler(handler);
00575   tab_win->Pen(ScrollView::GREY);
00576   tab_win->Rectangle(0, 0, tright_.x() - bleft_.x(), tright_.y() - bleft_.y());
00577 #endif
00578   return tab_win;
00579 }
00580 
00581 // Create a window at (x,y) and display the bounding boxes of the
00582 // BLOBNBOXes in this grid.
00583 // Use of this function requires an additional member of the BBC class:
00584 // ScrollView::Color BBC::BoxColor() const.
00585 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00586 void BBGrid<BBC, BBC_CLIST, BBC_C_IT>::DisplayBoxes(ScrollView* tab_win) {
00587 #ifndef GRAPHICS_DISABLED
00588   tab_win->Pen(ScrollView::BLUE);
00589   tab_win->Brush(ScrollView::NONE);
00590 
00591   // For every bbox in the grid, display it.
00592   GridSearch<BBC, BBC_CLIST, BBC_C_IT> gsearch(this);
00593   gsearch.StartFullSearch();
00594   BBC* bbox;
00595   while ((bbox = gsearch.NextFullSearch()) != NULL) {
00596     TBOX box = bbox->bounding_box();
00597     int left_x = box.left();
00598     int right_x = box.right();
00599     int top_y = box.top();
00600     int bottom_y = box.bottom();
00601     ScrollView::Color box_color = bbox->BoxColor();
00602     tab_win->Pen(box_color);
00603     tab_win->Rectangle(left_x, bottom_y, right_x, top_y);
00604   }
00605   tab_win->Update();
00606 #endif
00607 }
00608 
00609 // ASSERT_HOST that every cell contains no more than one copy of each entry.
00610 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00611 void BBGrid<BBC, BBC_CLIST, BBC_C_IT>::AssertNoDuplicates() {
00612   // Process all grid cells.
00613   for (int i = gridwidth_ * gridheight_ - 1; i >= 0; --i) {
00614     // Iterate over all elements excent the last.
00615     for (BBC_C_IT it(&grid_[i]); !it.at_last(); it.forward()) {
00616       BBC* ptr = it.data();
00617       BBC_C_IT it2(it);
00618       // None of the rest of the elements in the list should equal ptr.
00619       for (it2.forward(); !it2.at_first(); it2.forward()) {
00620         ASSERT_HOST(it2.data() != ptr);
00621       }
00622     }
00623   }
00624 }
00625 
00626 // Handle a click event in a display window.
00627 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00628 void BBGrid<BBC, BBC_CLIST, BBC_C_IT>::HandleClick(int x, int y) {
00629   tprintf("Click at (%d, %d)\n", x, y);
00630 }
00631 
00633 // GridSearch IMPLEMENTATION.
00635 
00636 // Start a new full search. Will iterate all stored blobs.
00637 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00638 void GridSearch<BBC, BBC_CLIST, BBC_C_IT>::StartFullSearch() {
00639   // Full search uses x_ and y_ as the current grid
00640   // cell being searched.
00641   CommonStart(grid_->bleft_.x(), grid_->tright_.y());
00642 }
00643 
00644 // Return the next bbox in the search or NULL if done.
00645 // The other searches will return a box that overlaps the grid cell
00646 // thereby duplicating boxes, but NextFullSearch only returns each box once.
00647 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00648 BBC* GridSearch<BBC, BBC_CLIST, BBC_C_IT>::NextFullSearch() {
00649   int x;
00650   int y;
00651   do {
00652     while (it_.cycled_list()) {
00653       ++x_;
00654       if (x_ >= grid_->gridwidth_) {
00655         --y_;
00656         if (y_ < 0)
00657           return CommonEnd();
00658         x_ = 0;
00659       }
00660       SetIterator();
00661     }
00662     CommonNext();
00663     TBOX box = previous_return_->bounding_box();
00664     grid_->GridCoords(box.left(), box.bottom(), &x, &y);
00665   } while (x != x_ || y != y_);
00666   return previous_return_;
00667 }
00668 
00669 // Start a new radius search.
00670 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00671 void GridSearch<BBC, BBC_CLIST, BBC_C_IT>::StartRadSearch(int x, int y,
00672                                                           int max_radius) {
00673   // Rad search uses x_origin_ and y_origin_ as the center of the circle.
00674   // The radius_ is the radius of the (diamond-shaped) circle and
00675   // rad_index/rad_dir_ combine to determine the position around it.
00676   max_radius_ = max_radius;
00677   radius_ = 0;
00678   rad_index_ = 0;
00679   rad_dir_ = 3;
00680   CommonStart(x, y);
00681 }
00682 
00683 // Return the next bbox in the radius search or NULL if the
00684 // maximum radius has been reached.
00685 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00686 BBC* GridSearch<BBC, BBC_CLIST, BBC_C_IT>::NextRadSearch() {
00687   do {
00688     while (it_.cycled_list()) {
00689       ++rad_index_;
00690       if (rad_index_ >= radius_) {
00691         ++rad_dir_;
00692         rad_index_ = 0;
00693         if (rad_dir_ >= 4) {
00694           ++radius_;
00695           if (radius_ > max_radius_)
00696             return CommonEnd();
00697           rad_dir_ = 0;
00698         }
00699       }
00700       ICOORD offset = C_OUTLINE::chain_step(rad_dir_);
00701       offset *= radius_ - rad_index_;
00702       offset += C_OUTLINE::chain_step(rad_dir_ + 1) * rad_index_;
00703       x_ = x_origin_ + offset.x();
00704       y_ = y_origin_ + offset.y();
00705       if (x_ >= 0 && x_ < grid_->gridwidth_ &&
00706           y_ >= 0 && y_ < grid_->gridheight_)
00707         SetIterator();
00708     }
00709     CommonNext();
00710   } while (unique_mode_ &&
00711            !returns_.add_sorted(SortByBoxLeft<BBC>, true, previous_return_));
00712   return previous_return_;
00713 }
00714 
00715 // Start a new left or right-looking search. Will search to the side
00716 // for a box that vertically overlaps the given vertical line segment.
00717 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00718 void GridSearch<BBC, BBC_CLIST, BBC_C_IT>::StartSideSearch(int x,
00719                                                            int ymin, int ymax) {
00720   // Right search records the x in x_origin_, the ymax in y_origin_
00721   // and the size of the vertical strip to search in radius_.
00722   // To guarantee finding overlapping objects of upto twice the
00723   // given size, double the height.
00724   radius_ = ((ymax - ymin) * 2 + grid_->gridsize_ - 1) / grid_->gridsize_;
00725   rad_index_ = 0;
00726   CommonStart(x, ymax);
00727 }
00728 
00729 // Return the next bbox in the side search or NULL if the
00730 // edge has been reached. Searches left to right or right to left
00731 // according to the flag.
00732 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00733 BBC* GridSearch<BBC, BBC_CLIST, BBC_C_IT>::NextSideSearch(bool right_to_left) {
00734   do {
00735     while (it_.cycled_list()) {
00736       ++rad_index_;
00737       if (rad_index_ > radius_) {
00738         if (right_to_left)
00739           --x_;
00740         else
00741           ++x_;
00742         rad_index_ = 0;
00743         if (x_ < 0 || x_ >= grid_->gridwidth_)
00744           return CommonEnd();
00745       }
00746       y_ = y_origin_ - rad_index_;
00747       if (y_ >= 0 && y_ < grid_->gridheight_)
00748         SetIterator();
00749     }
00750     CommonNext();
00751   } while (unique_mode_ &&
00752            !returns_.add_sorted(SortByBoxLeft<BBC>, true, previous_return_));
00753   return previous_return_;
00754 }
00755 
00756 // Start a vertical-looking search. Will search up or down
00757 // for a box that horizontally overlaps the given line segment.
00758 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00759 void GridSearch<BBC, BBC_CLIST, BBC_C_IT>::StartVerticalSearch(int xmin,
00760                                                                int xmax,
00761                                                                int y) {
00762   // Right search records the xmin in x_origin_, the y in y_origin_
00763   // and the size of the horizontal strip to search in radius_.
00764   radius_ = (xmax - xmin + grid_->gridsize_ - 1) / grid_->gridsize_;
00765   rad_index_ = 0;
00766   CommonStart(xmin, y);
00767 }
00768 
00769 // Return the next bbox in the vertical search or NULL if the
00770 // edge has been reached. Searches top to bottom or bottom to top
00771 // according to the flag.
00772 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00773 BBC* GridSearch<BBC, BBC_CLIST, BBC_C_IT>::NextVerticalSearch(
00774     bool top_to_bottom) {
00775   do {
00776     while (it_.cycled_list()) {
00777       ++rad_index_;
00778       if (rad_index_ > radius_) {
00779         if (top_to_bottom)
00780           --y_;
00781         else
00782           ++y_;
00783         rad_index_ = 0;
00784         if (y_ < 0 || y_ >= grid_->gridheight_)
00785           return CommonEnd();
00786       }
00787       x_ = x_origin_ + rad_index_;
00788       if (x_ >= 0 && x_ < grid_->gridwidth_)
00789         SetIterator();
00790     }
00791     CommonNext();
00792   } while (unique_mode_ &&
00793            !returns_.add_sorted(SortByBoxLeft<BBC>, true, previous_return_));
00794   return previous_return_;
00795 }
00796 
00797 // Start a rectangular search. Will search for a box that overlaps the
00798 // given rectangle.
00799 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00800 void GridSearch<BBC, BBC_CLIST, BBC_C_IT>::StartRectSearch(const TBOX& rect) {
00801   // Rect search records the xmin in x_origin_, the ymin in y_origin_
00802   // and the xmax in max_radius_.
00803   // The search proceeds left to right, top to bottom.
00804   rect_ = rect;
00805   CommonStart(rect.left(), rect.top());
00806   grid_->GridCoords(rect.right(), rect.bottom(),  // - rect.height(),
00807                     &max_radius_, &y_origin_);
00808 }
00809 
00810 // Return the next bbox in the rectangular search or NULL if complete.
00811 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00812 BBC* GridSearch<BBC, BBC_CLIST, BBC_C_IT>::NextRectSearch() {
00813   do {
00814     while (it_.cycled_list()) {
00815       ++x_;
00816       if (x_ > max_radius_) {
00817         --y_;
00818         x_ = x_origin_;
00819         if (y_ < y_origin_)
00820           return CommonEnd();
00821       }
00822       SetIterator();
00823     }
00824     CommonNext();
00825   } while (!rect_.overlap(previous_return_->bounding_box()) ||
00826            (unique_mode_ &&
00827             !returns_.add_sorted(SortByBoxLeft<BBC>, true, previous_return_)));
00828   return previous_return_;
00829 }
00830 
00831 // Remove the last returned BBC. Will not invalidate this. May invalidate
00832 // any other concurrent GridSearch on the same grid. If any others are
00833 // in use, call RepositionIterator on those, to continue without harm.
00834 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00835 void GridSearch<BBC, BBC_CLIST, BBC_C_IT>::RemoveBBox() {
00836   if (previous_return_ != NULL) {
00837     // Remove all instances of previous_return_ from the list, so the iterator
00838     // remains valid after removal from the rest of the grid cells.
00839     // if previous_return_ is not on the list, then it has been removed already.
00840     BBC* prev_data = NULL;
00841     BBC* new_previous_return = NULL;
00842     it_.move_to_first();
00843     for (it_.mark_cycle_pt(); !it_.cycled_list();) {
00844       if (it_.data() ==  previous_return_) {
00845         new_previous_return = prev_data;
00846         it_.extract();
00847         it_.forward();
00848         next_return_ = it_.cycled_list() ? NULL : it_.data();
00849       } else {
00850         prev_data = it_.data();
00851         it_.forward();
00852       }
00853     }
00854     grid_->RemoveBBox(previous_return_);
00855     previous_return_ = new_previous_return;
00856     RepositionIterator();
00857   }
00858 }
00859 
00860 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00861 void GridSearch<BBC, BBC_CLIST, BBC_C_IT>::RepositionIterator() {
00862   // Reset the iterator back to one past the previous return.
00863   // If the previous_return_ is no longer in the list, then
00864   // next_return_ serves as a backup.
00865   it_.move_to_first();
00866   // Special case, the first element was removed and reposition
00867   // iterator was called. In this case, the data is fine, but the
00868   // cycle point is not. Detect it and return.
00869   if (!it_.empty() && it_.data() == next_return_) {
00870     it_.mark_cycle_pt();
00871     return;
00872   }
00873   for (it_.mark_cycle_pt(); !it_.cycled_list(); it_.forward()) {
00874     if (it_.data() == previous_return_ ||
00875         it_.data_relative(1) == next_return_) {
00876       CommonNext();
00877       return;
00878     }
00879   }
00880   // We ran off the end of the list. Move to a new cell next time.
00881   previous_return_ = NULL;
00882   next_return_ = NULL;
00883 }
00884 
00885 // Factored out helper to start a search.
00886 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00887 void GridSearch<BBC, BBC_CLIST, BBC_C_IT>::CommonStart(int x, int y) {
00888   grid_->GridCoords(x, y, &x_origin_, &y_origin_);
00889   x_ = x_origin_;
00890   y_ = y_origin_;
00891   SetIterator();
00892   previous_return_ = NULL;
00893   next_return_ = it_.empty() ? NULL : it_.data();
00894   returns_.shallow_clear();
00895 }
00896 
00897 // Factored out helper to complete a next search.
00898 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00899 BBC* GridSearch<BBC, BBC_CLIST, BBC_C_IT>::CommonNext() {
00900   previous_return_ = it_.data();
00901   it_.forward();
00902   next_return_ = it_.cycled_list() ? NULL : it_.data();
00903   return previous_return_;
00904 }
00905 
00906 // Factored out final return when search is exhausted.
00907 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00908 BBC* GridSearch<BBC, BBC_CLIST, BBC_C_IT>::CommonEnd() {
00909   previous_return_ = NULL;
00910   next_return_ = NULL;
00911   return NULL;
00912 }
00913 
00914 // Factored out function to set the iterator to the current x_, y_
00915 // grid coords and mark the cycle pt.
00916 template<class BBC, class BBC_CLIST, class BBC_C_IT>
00917 void GridSearch<BBC, BBC_CLIST, BBC_C_IT>::SetIterator() {
00918   it_= &(grid_->grid_[y_ * grid_->gridwidth_ + x_]);
00919   it_.mark_cycle_pt();
00920 }
00921 
00922 }  // namespace tesseract.
00923 
00924 #endif  // TESSERACT_TEXTORD_BBGRID_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines