Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

rsdl_bins.h

Go to the documentation of this file.
00001 #ifndef rsdl_bins_h_
00002 #define rsdl_bins_h_
00003 //:
00004 // \file
00005 // \author Amitha Perera
00006 // \date   Feb 2003
00007 //
00008 // Provide an n-dimensional bin structure for fast point retrieval.
00009 
00010 #include <vcl_vector.h>
00011 #include <vnl/vnl_vector_fixed.h>
00012 
00013 // these two classes are helper classes for rsdl_bins, and should ideally
00014 // be declared in rsdl_bins and defined only in the .txx, but MSVC6 and 7
00015 // don't handle nested templated classes well. So, they become external
00016 // classes.
00017 //
00018 template<unsigned N, typename C, typename V>
00019 struct rsdl_bins_point_dist_entry;
00020 template<unsigned N, typename C, typename V>
00021 struct rsdl_bins_bin_entry_type;
00022 
00023 
00024 //: N-dimensional bin structure for fast point retrieval.
00025 //
00026 // This data structure stores point locations in larger bins so that
00027 // neighbourhood queries can be performed efficiently by not having to
00028 // consider the whole data set.
00029 //
00030 // The points are of type vnl_vector_fixed<\a CoordType, \a N>. With
00031 // every point is associated a value, of type \a ValueType.
00032 //
00033 // The structure will store any point location, including those
00034 // outside the range of the underlying bin storage. Such points are
00035 // stored in the nearest available bin.
00036 //
00037 // Location equality is performed to within a tolerance (see
00038 // set_distance_tolerance). This affects get_value, for
00039 // example.
00040 //
00041 template<unsigned N, typename CoordType, typename ValueType>
00042 class rsdl_bins
00043 {
00044  public:
00045   //:
00046   typedef CoordType                       coord_type;
00047 
00048   //: The type of data object associated with each location.
00049   typedef ValueType                       value_type;
00050 
00051   //: The location object type.
00052   typedef vnl_vector_fixed<CoordType,N>   point_type;
00053 
00054  public:
00055   //:
00056   // Construct bins of size \a bin_sizes with enough bins to span \a
00057   // min_coord to \a max_coord.
00058   //
00059   // The equality comparison tolerance is set to 0.
00060   //
00061   rsdl_bins( point_type const& min_coord,
00062              point_type const& max_coord,
00063              point_type const& bin_sizes );
00064 
00065   //: Remove all points from the bins.
00066   void clear();
00067 
00068   //: Update the tolerance used in equality checking.
00069   //
00070   // Two points with Euclidean distance <= \a tol will compare equal.
00071   //
00072   void set_distance_tolerance( coord_type const& tol );
00073 
00074   //: Adds the point \a pt with value \a val.
00075   void add_point( point_type const& pt, value_type const& val );
00076 
00077   //: Retrieves the value at \a pt, if possible.
00078   //
00079   // If a point equal to \a pt, to within the current tolerance,
00080   // is found, \a val will be set to the associated value, and the
00081   // function will return true. Otherwise, the function will return
00082   // false.
00083   //
00084   // If multiple points compare equal, one will be arbitrarily
00085   // selected.
00086   //
00087   bool get_value( point_type const& pt, value_type& val ) const;
00088 
00089   //: Return the values of the \a n nearest neighbours.
00090   //
00091   // If the structure has fewer than \a n points, it will return all
00092   // the values stored.
00093   //
00094   void n_nearest( point_type const& pt,
00095                   unsigned n,
00096                   vcl_vector< value_type >& values ) const;
00097 
00098   //: Return the values and locations of the \a n nearest neighbours.
00099   //
00100   // If the structure has fewer than \a n points, it will return all
00101   // the points stored.
00102   //
00103   void n_nearest( point_type const& pt,
00104                   unsigned n,
00105                   vcl_vector< point_type >& points,
00106                   vcl_vector< value_type >& values  ) const;
00107 
00108   //: Check if there is at least one point within \a radius of \a pt.
00109   //
00110   bool is_any_point_within_radius( point_type const& pt,
00111                                    coord_type const& radius ) const;
00112 
00113   //: Return values of all the points within \a radius of \a pt.
00114   //
00115   void points_within_radius( point_type const& pt,
00116                              coord_type const& radius,
00117                              vcl_vector< value_type >& values ) const;
00118 
00119   //: Return values and locations of all the points within \a radius of \a pt.
00120   //
00121   void points_within_radius( point_type const& pt,
00122                              coord_type const& radius,
00123                              vcl_vector< point_type >& points,
00124                              vcl_vector< value_type >& values  ) const;
00125 
00126   //: Return values of all the points within the bounding box.
00127   //
00128   // The boundaries of the bounding box are included in the region
00129   // searched.
00130   //
00131   void points_in_bounding_box( point_type const& min_pt,
00132                                point_type const& max_pt,
00133                                vcl_vector< value_type >& values ) const;
00134 
00135   //: Return values and locations of all the points within the bounding box.
00136   //
00137   // The boundaries of the bounding box are included in the region
00138   // searched.
00139   //
00140   void points_in_bounding_box( point_type const& min_pt,
00141                                point_type const& max_pt,
00142                                vcl_vector< point_type >& points,
00143                                vcl_vector< value_type >& values  ) const;
00144   // INTERNALS
00145  public:
00146   typedef rsdl_bins_bin_entry_type<N,CoordType,ValueType> bin_entry_type;
00147 
00148   // See comment in .txx
00149   typedef rsdl_bins_point_dist_entry<N,CoordType,ValueType> point_dist_entry;
00150 
00151   //:
00152   // Data stored at each bin
00153   //
00154   typedef vcl_vector< bin_entry_type >  bin_type;
00155 
00156   //:
00157   // The type of an index into the bins_ storage structure.
00158   //
00159   typedef typename bin_type::size_type bin_index_type;
00160 
00161   //:
00162   // A vector of indices into the bins_ storage structure.
00163   //
00164   typedef vcl_vector< bin_index_type > bin_index_vector;
00165 
00166   //:
00167   // Converts the coordinate \a x to a bin coordinate for dimension \a
00168   // d. Requires that min_pt_[d] and bin_size_[d] are initialized. The
00169   // resulting coordinate is not necessarily within the allocated
00170   // range.
00171   //
00172   int coord_to_bin( coord_type x, unsigned d ) const;
00173 
00174   //:
00175   // Converts the point to a set of bin indices.
00176   //
00177   void point_to_bin( point_type const& pt, int ind[N] ) const;
00178 
00179   //:
00180   // Convert the point into an index into the bin data structure bins_.
00181   //
00182   bin_index_type bin_index( point_type const& pt ) const;
00183 
00184   //:
00185   // Convert the bin coordinates into an index into the bin data
00186   // structure bins_.
00187   //
00188   bin_index_type bin_index( int bin[N] ) const;
00189 
00190   //:
00191   // The list of possible bin indices that could hold pt given the
00192   // current distance tolerance.
00193   bin_index_vector bin_indices( point_type const& pt ) const;
00194 
00195 
00196   //:
00197   // Implementation of n_nearest. See the documentation for that.
00198   //
00199   // This version will add the results to values, and, if points is
00200   // not null, to points.
00201   //
00202   void  n_nearest_impl( point_type const& pt,
00203                         unsigned n,
00204                         vcl_vector< value_type >& values,
00205                         vcl_vector< point_type >* points ) const;
00206 
00207   //:
00208   // Implementation of points_within_radius. See the documentation for
00209   // that.
00210   //
00211   // This version will add the results to values, and, if points is
00212   // not null, to points.
00213   //
00214   void points_within_radius_impl( point_type const& pt,
00215                                   coord_type const& radius,
00216                                   vcl_vector< value_type >& values,
00217                                   vcl_vector< point_type >* points ) const;
00218 
00219   //:
00220   // Implementation of points_in_bounding_box. See the documentation
00221   // for that.
00222   //
00223   // This version will add the results to values, and, if points is
00224   // not null, to points.
00225   //
00226   void points_in_bounding_box_impl( point_type const& min_pt,
00227                                     point_type const& max_pt,
00228                                     vcl_vector< value_type >& values,
00229                                     vcl_vector< point_type >* points ) const;
00230 
00231   // documentation in .txx
00232   unsigned scan_region( int lo[N], int hi[N], int cur[N], unsigned dim,
00233                         bin_index_vector& indices ) const;
00234 
00235   // documentation in .txx
00236   unsigned scan_bdy( int lo[N], int hi[N], int cur[N], unsigned dim,
00237                      bin_index_vector& indices ) const;
00238 
00239 
00240  private:
00241   //:
00242   // The number of bins along each dimension. The numbers are ints and
00243   // not unsigned ints because the logic for off-the-bottom is easier
00244   // if we allow -ve indices.
00245   //
00246   int size_[N];
00247 
00248   //:
00249   // The size of the bins along each dimension.
00250   //
00251   coord_type bin_size_[N];
00252 
00253   //:
00254   // The offsets for bin(0,0,0)
00255   coord_type min_pt_[N];
00256 
00257   //:
00258   // Distance tolerance used to compare if two points are the same.
00259   //
00260   coord_type dist_tol_;
00261 
00262   //:
00263   // Storage for all the bins.
00264   vcl_vector< bin_type > bins_;
00265 };
00266 
00267 
00268 //:
00269 // This is just a (location,value) pair that has a
00270 // equal-within-threshold function. Used in rsdl_bins to store the
00271 // points in the bins.
00272 //
00273 template<unsigned N, typename CoordType, typename ValueType>
00274 struct rsdl_bins_bin_entry_type
00275 {
00276   // For some reason, MSVC7 doesn't like rsdl_bins<N,CoordType,ValueType>::coord_type.
00277   // It complains that rsdl_bins is undefined, but doesn't complain about the same 
00278   // declaration in rsdl_bins_point_dist_entry (in the .txx). Go figure.
00279 
00280   typedef CoordType                       coord_type;
00281   typedef ValueType                       value_type;
00282   typedef vnl_vector_fixed<CoordType,N>   point_type;
00283 
00284   rsdl_bins_bin_entry_type( point_type const& pt, const value_type val );
00285 
00286   inline bool equal( point_type const& pt, double tol_sqr ) const;
00287 
00288   point_type point_;
00289   value_type value_;
00290 };
00291 
00292 
00293 #endif // rsdl_bins_h_

Generated on Thu Jan 10 14:49:34 2008 for contrib/rpl/rsdl by  doxygen 1.4.4