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