00001 #ifndef rsdl_bins_2d_h_
00002 #define rsdl_bins_2d_h_
00003
00004 #include <vcl_iosfwd.h>
00005 #include <vnl/vnl_vector_fixed.h>
00006
00007 #include <vcl_vector.h>
00008 #include <vbl/vbl_array_2d.h>
00009
00010 template < class COORD_T, class VALUE_T >
00011 class rsdl_bins_2d_entry {
00012 public:
00013 rsdl_bins_2d_entry() {}
00014 rsdl_bins_2d_entry( const vnl_vector_fixed< COORD_T, 2 > & pt,
00015 const VALUE_T& val )
00016 : point_(pt), value_(val) {}
00017 bool operator== ( const rsdl_bins_2d_entry& right ) const
00018 { return point_ == right.point_ && value_ == right.value_; }
00019 vnl_vector_fixed<COORD_T, 2> point_;
00020 VALUE_T value_;
00021 };
00022
00023 template < class COORD_T, class VALUE_T >
00024 vcl_ostream& operator<< ( vcl_ostream& ostr,
00025 const vcl_vector< rsdl_bins_2d_entry< COORD_T, VALUE_T > > & entries );
00026
00027
00028 template < class COORD_T, class VALUE_T >
00029 class rsdl_bins_2d {
00030 public:
00031 typedef vnl_vector_fixed< COORD_T, 2 > point_type;
00032 typedef VALUE_T value_type;
00033
00034 public:
00035 rsdl_bins_2d();
00036 rsdl_bins_2d( const point_type & min_pt,
00037 const point_type & max_pt,
00038 const point_type & bin_sizes );
00039 ~rsdl_bins_2d();
00040
00041 void set_distance_tolerance( COORD_T tolerance )
00042 { dist_tolerance_sqr_ = tolerance * tolerance; }
00043
00044 void reset( const point_type & min_pt,
00045 const point_type & max_pt,
00046 const point_type & bin_sizes );
00047 void remove_all_points( );
00048
00049 void add_point( const point_type & pt, const value_type& value );
00050 bool get_value( const point_type & pt, value_type& value );
00051 bool change_point( const point_type & pt, const value_type& value );
00052 bool change_point( const point_type & pt, const value_type& old_val, const value_type& new_val );
00053 bool remove_point( const point_type & pt );
00054 bool remove_point( const point_type & pt, const value_type& value );
00055
00056 void n_nearest( const point_type & query_pt,
00057 int n,
00058 vcl_vector< value_type >& values ) const;
00059
00060 void n_nearest( const point_type & query_pt,
00061 int n,
00062 vcl_vector< point_type > & points,
00063 vcl_vector< value_type > & values ) const;
00064
00065 bool is_any_point_within_radius( const point_type & query_pt,
00066 COORD_T radius ) const;
00067
00068 void points_within_radius( const point_type & query_pt,
00069 COORD_T radius,
00070 vcl_vector< value_type >& values ) const;
00071
00072 void points_within_radius( const point_type & query_pt,
00073 COORD_T radius,
00074 vcl_vector< point_type > & points,
00075 vcl_vector< value_type >& values ) const;
00076
00077 bool is_any_point_in_bounding_box( const point_type & min_query_pt,
00078 const point_type & max_query_pt ) const;
00079
00080 void points_in_bounding_box( const point_type & min_query_pt,
00081 const point_type & max_query_pt,
00082 vcl_vector< value_type >& values ) const;
00083
00084 void points_in_bounding_box( const point_type & min_query_pt,
00085 const point_type & max_query_pt,
00086 vcl_vector< point_type > & points,
00087 vcl_vector< value_type > & values ) const;
00088
00089 void change_value( const value_type& old_val, const value_type& new_val );
00090
00091 point_type max_pt() {return max_pt_ ;}
00092
00093 point_type min_pt() {return min_pt_ ;}
00094
00095 point_type bin_sizes() {return bin_sizes_;}
00096
00097 unsigned num_pts() const;
00098
00099 private:
00100 void point_to_bin( COORD_T x, COORD_T y, int& bin_x, int& bin_y ) const;
00101 COORD_T min_sq_distance_to_bin( COORD_T x, COORD_T y, int bin_x, int bin_y ) const;
00102
00103 public:
00104 typedef rsdl_bins_2d_entry< COORD_T, VALUE_T > bin_entry_type_;
00105 typedef vcl_vector< bin_entry_type_ > bin_vector_type_;
00106
00107 private:
00108 vbl_array_2d< bin_vector_type_ > bins_;
00109
00110 point_type min_pt_;
00111 point_type max_pt_;
00112 point_type bin_sizes_;
00113
00114 int num_bins_x_, num_bins_y_;
00115
00116 COORD_T dist_tolerance_sqr_ ;
00117 };
00118
00119 #endif