Go to the documentation of this file.00001 #ifndef rgrl_feature_set_location_txx_
00002 #define rgrl_feature_set_location_txx_
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 #include "rgrl_feature_set_location.h"
00014 
00015 #include <rgrl/rgrl_mask.h>
00016 #include <rsdl/rsdl_kd_tree.h>
00017 
00018 #include <vnl/vnl_vector_fixed.h>
00019 #include <vcl_cassert.h>
00020 
00021 template<unsigned N>
00022 rgrl_feature_set_location<N>::
00023 rgrl_feature_set_location( feature_vector const& features,
00024                            rgrl_feature_set_label const& label)
00025   :
00026   rgrl_feature_set( features, label ),
00027   bounding_box_( N )
00028 {
00029   
00030   
00031   typedef vnl_vector_fixed<double, N> point_type;
00032   point_type min;
00033   point_type max;
00034 
00035   if ( features.empty() )
00036   {
00037     min.fill( 0 );
00038     max.fill( 0 );
00039   }
00040   else
00041   {
00042     feature_vector::const_iterator itr = features.begin();
00043     
00044     feature_type_ = &typeid(*(*itr));
00045     min = (*itr)->location();
00046     max = min;
00047     for ( ; itr != features.end(); ++itr ) {
00048       vnl_vector<double> const& loc = (*itr)->location();
00049       assert( loc.size() == N );
00050       for ( unsigned i=0; i < N; ++i ) {
00051         if ( loc[i] < min[i] )    min[i] = loc[i];
00052         if ( loc[i] > max[i] )    max[i] = loc[i];
00053       }
00054     }
00055   }
00056   bounding_box_.set_x0( min.as_ref() );
00057   bounding_box_.set_x1( max.as_ref() );
00058 
00059   
00060   
00061   
00062   vcl_vector<rsdl_point> search_pts;
00063   search_pts.reserve( features.size() );
00064   for ( feature_vector::const_iterator itr = features.begin(); itr != features.end(); ++itr ) {
00065     search_pts.push_back( rsdl_point((*itr)->location()) );
00066   }
00067   kd_tree_ = new rsdl_kd_tree( search_pts );
00068 }
00069 
00070 
00071 template<unsigned N>
00072 rgrl_feature_set_location<N>::
00073 ~rgrl_feature_set_location()
00074 {
00075 }
00076 
00077 
00078 template<unsigned N>
00079 void
00080 rgrl_feature_set_location<N>::
00081 features_in_region( feature_vector& results, rgrl_mask_box const& roi ) const
00082 {
00083   assert( roi.x0().size() == N );
00084 
00085   
00086   rsdl_point min_point( roi.x0() );
00087   rsdl_point max_point( roi.x1() );
00088   rsdl_bounding_box box(min_point, max_point);
00089 
00090   
00091   clear_temp_storage();
00092   kd_tree_->points_in_bounding_box( box, temp_points_, temp_point_indices_ );
00093 
00094   
00095   
00096   vcl_size_t num_pts = temp_point_indices_.size();
00097   for (vcl_size_t i = 0; i<num_pts; i++ )
00098     results.push_back( fea_vec_[temp_point_indices_[i]] );
00099 }
00100 
00101 template<unsigned N>
00102 void
00103 rgrl_feature_set_location<N>::
00104 features_within_radius( feature_vector& results, vnl_vector<double> const& center, double radius ) const
00105 {
00106   
00107   clear_temp_storage();
00108   kd_tree_->points_in_radius(  center, radius, temp_points_, temp_point_indices_ );
00109 
00110   
00111   
00112   vcl_size_t num_pts = temp_point_indices_.size();
00113   results.reserve( num_pts );
00114   for (vcl_size_t i = 0; i<num_pts; i++ )
00115     results.push_back( fea_vec_[temp_point_indices_[i]] );
00116 }
00117 
00118 template<unsigned N>
00119 rgrl_feature_sptr
00120 rgrl_feature_set_location<N>::
00121 nearest_feature( rgrl_feature_sptr const& feature ) const
00122 {
00123   feature_vector results;
00124   this->k_nearest_features( results, feature->location(), 1 );
00125   assert( results.size() == 1 );
00126   return results[0];
00127 }
00128 
00129 
00130 template<unsigned N>
00131 rgrl_feature_sptr
00132 rgrl_feature_set_location<N>::
00133 nearest_feature( const vnl_vector<double>& loc ) const
00134 {
00135   feature_vector results;
00136   this->k_nearest_features( results, loc, 1 );
00137   assert( results.size() == 1 );
00138   return results[0];
00139 }
00140 
00141 
00142 template<unsigned N>
00143 void
00144 rgrl_feature_set_location<N>::
00145 features_within_radius( feature_vector& results, rgrl_feature_sptr const& feature, double distance ) const
00146 {
00147   rsdl_point query_point(feature->location());
00148   clear_temp_storage();
00149   kd_tree_->points_in_radius( query_point, distance, temp_points_, temp_point_indices_ );
00150 
00151   
00152   
00153   vcl_size_t num_pts = temp_point_indices_.size();
00154   for (vcl_size_t i = 0; i<num_pts; i++ )
00155     results.push_back( fea_vec_[temp_point_indices_[i]] );
00156 }
00157 
00158 
00159 template<unsigned N>
00160 void
00161 rgrl_feature_set_location<N>::
00162 k_nearest_features( feature_vector& results, rgrl_feature_sptr const& feature, unsigned int k ) const
00163 {
00164   rsdl_point query_point(feature->location());
00165   clear_temp_storage();
00166   kd_tree_->n_nearest( query_point, k, temp_points_, temp_point_indices_ );
00167 
00168   
00169   
00170   vcl_size_t num_pts = temp_point_indices_.size();
00171   for (vcl_size_t i = 0; i<num_pts; i++ )
00172     results.push_back( fea_vec_[temp_point_indices_[i]] );
00173 }
00174 
00175 
00176 template<unsigned N>
00177 void
00178 rgrl_feature_set_location<N>::
00179 k_nearest_features( feature_vector& results, const vnl_vector<double> & loc, unsigned int k ) const
00180 {
00181   rsdl_point query_point(loc);
00182   clear_temp_storage();
00183   kd_tree_->n_nearest( query_point, k, temp_points_, temp_point_indices_ );
00184 
00185   
00186   
00187   vcl_size_t num_pts = temp_point_indices_.size();
00188   for (vcl_size_t i = 0; i<num_pts; i++ )
00189     results.push_back( fea_vec_[temp_point_indices_[i]] );
00190 }
00191 
00192 template<unsigned N>
00193 rgrl_mask_box
00194 rgrl_feature_set_location<N>::
00195 bounding_box() const
00196 {
00197   return bounding_box_;
00198 }
00199 
00200 template<unsigned N>
00201 const vcl_type_info&
00202 rgrl_feature_set_location<N>::
00203 type() const
00204 {
00205   return *feature_type_;
00206 }
00207 
00208 #endif // rgrl_feature_set_location_txx_