contrib/rpl/rgrl/rgrl_feature_set_bins.txx
Go to the documentation of this file.
00001 #ifndef rgrl_feature_set_bins_txx_
00002 #define rgrl_feature_set_bins_txx_
00003 //:
00004 // \file
00005 // \author Amitha Perera
00006 // \date   Feb 2003
00007 //
00008 // \verbatim
00009 //  Modifications:
00010 //   April 2004 Charlene: allow the use of kd_tree and user-defined bin_size.
00011 // \endverbatim
00012 
00013 #include "rgrl_feature_set_bins.h"
00014 
00015 #include <vcl_cassert.h>
00016 
00017 #include <rsdl/rsdl_bins.h>
00018 #include <rsdl/rsdl_bins.txx> // to avoid explicit instantiation
00019 
00020 #include <rgrl/rgrl_mask.h>
00021 
00022 
00023 template<unsigned N>
00024 rgrl_feature_set_bins<N>::
00025 rgrl_feature_set_bins( feature_vector const& features,
00026                        double bin_size,
00027                        rgrl_feature_set_label const& label)
00028   :
00029   rgrl_feature_set( features, label ),
00030   bounding_box_( N )
00031 {
00032   // Determine the extents of the data. (And the type.)
00033   //
00034   typedef typename bin_type::point_type point_type;
00035   point_type min;
00036   point_type max;
00037 
00038   if ( features.empty() )
00039   {
00040     min.fill( 0 );
00041     max.fill( bin_size );
00042   }
00043   else
00044   {
00045     feature_vector::const_iterator itr = features.begin();
00046     //feature_type_ = (*itr)->type_id();
00047     feature_type_ = &typeid(*(*itr));
00048     min = (*itr)->location();
00049     max = min;
00050     for ( ; itr != features.end(); ++itr ) {
00051       vnl_vector<double> const& loc = (*itr)->location();
00052       assert( loc.size() == N );
00053       for ( unsigned i=0; i < N; ++i ) {
00054         if ( loc[i] < min[i] )    min[i] = loc[i];
00055         if ( loc[i] > max[i] )    max[i] = loc[i];
00056       }
00057     }
00058   }
00059   bounding_box_.set_x0( min.as_ref() );
00060   bounding_box_.set_x1( max.as_ref() );
00061 
00062   // Now store the feature points in the chosen data structure
00063   //
00064   // Create the bins
00065   point_type bin_sizes;
00066   bin_sizes.fill( bin_size );
00067   bins_.reset( new bin_type( min, max, bin_sizes ) );
00068 
00069   // Add the data
00070   for ( feature_vector::const_iterator itr = features.begin(); itr != features.end(); ++itr ) {
00071     bins_->add_point( (*itr)->location(), *itr );
00072   }
00073 }
00074 
00075 
00076 template<unsigned N>
00077 rgrl_feature_set_bins<N>::
00078 ~rgrl_feature_set_bins()
00079 {
00080 }
00081 
00082 
00083 template<unsigned N>
00084 void
00085 rgrl_feature_set_bins<N>::
00086 features_in_region( feature_vector& results, rgrl_mask_box const& roi ) const
00087 {
00088   assert( roi.x0().size() == N );
00089   bins_->points_in_bounding_box( roi.x0(), roi.x1(), results );
00090 }
00091 
00092 template<unsigned N>
00093 void
00094 rgrl_feature_set_bins<N>::
00095 features_within_radius( feature_vector& results, vnl_vector<double> const& center, double radius ) const
00096 {
00097   bins_->points_within_radius( center, radius, results );
00098 }
00099 
00100 template<unsigned N>
00101 rgrl_feature_sptr
00102 rgrl_feature_set_bins<N>::
00103 nearest_feature( rgrl_feature_sptr const& feature ) const
00104 {
00105   feature_vector results;
00106   bins_->n_nearest( feature->location(), 1, results );
00107   assert( results.size() == 1 );
00108   return results[0];
00109 }
00110 
00111 
00112 template<unsigned N>
00113 rgrl_feature_sptr
00114 rgrl_feature_set_bins<N>::
00115 nearest_feature( const vnl_vector<double>& loc ) const
00116 {
00117   feature_vector results;
00118   bins_->n_nearest( loc, 1, results );
00119   assert( results.size() == 1 );
00120   return results[0];
00121 }
00122 
00123 
00124 template<unsigned N>
00125 void
00126 rgrl_feature_set_bins<N>::
00127 features_within_radius( feature_vector& results, rgrl_feature_sptr const& feature, double distance ) const
00128 {
00129   bins_->points_within_radius( feature->location(), distance, results );
00130 }
00131 
00132 //:  Return the k nearest features based on Euclidean distance.
00133 template<unsigned N>
00134 void
00135 rgrl_feature_set_bins<N>::
00136 k_nearest_features( feature_vector& results, rgrl_feature_sptr const& feature, unsigned int k ) const
00137 {
00138   bins_->n_nearest( feature->location(), k, results );
00139 }
00140 
00141 //:  Return the k nearest features based on Euclidean distance.
00142 template<unsigned N>
00143 void
00144 rgrl_feature_set_bins<N>::
00145 k_nearest_features( feature_vector& results, const vnl_vector<double> & loc, unsigned int k ) const
00146 {
00147   bins_->n_nearest( loc, k, results );
00148 }
00149 
00150 template<unsigned N>
00151 rgrl_mask_box
00152 rgrl_feature_set_bins<N>::
00153 bounding_box() const
00154 {
00155   return bounding_box_;
00156 }
00157 
00158 template<unsigned N>
00159 const vcl_type_info&
00160 rgrl_feature_set_bins<N>::
00161 type() const
00162 {
00163   return *feature_type_;
00164 }
00165 
00166 #endif // rgrl_feature_set_bins_txx_