contrib/rpl/rgrl/rgrl_mask.cxx
Go to the documentation of this file.
00001 #include "rgrl_mask.h"
00002 //:
00003 // \file
00004 #include <vcl_cassert.h>
00005 // not used? #include <vcl_limits.h>
00006 #include <vnl/vnl_math.h>
00007 
00008 rgrl_mask_box
00009 rgrl_mask::
00010 bounding_box() const
00011 {
00012   rgrl_mask_box box( x0_, x1_ );
00013   return box;
00014 }
00015 
00016 //************* mask using a binary 2D image ********************
00017 rgrl_mask_2d_image::
00018 rgrl_mask_2d_image( const vil_image_view<vxl_byte>& in_mask,
00019                     int org_x, int org_y)
00020   : rgrl_mask( 2 ), mask_image_( in_mask ),
00021     org_x_( org_x ), org_y_( org_y )
00022 {
00023   update_bounding_box();
00024 }
00025 
00026 bool
00027 rgrl_mask_2d_image::
00028 inside( vnl_vector<double> const& pt ) const
00029 {
00030   double x = pt[0]-double(org_x_);
00031   double y = pt[1]-double(org_y_);
00032 
00033   // As the bounding box is tighter than image dim,
00034   // check w/ bounding box is sufficient
00035   //
00036   bool in_range = ( x0_[0] <= x ) && ( x <= x1_[0] ) &&
00037                   ( x0_[1] <= y ) && ( y <= x1_[1] );
00038 
00039   return in_range && mask_image_( vnl_math_rnd(x), vnl_math_rnd(y) )>0;
00040 }
00041 
00042 void
00043 rgrl_mask_2d_image::
00044 update_bounding_box()
00045 {
00046   // reset bounding box
00047   x0_[0] = double( mask_image_.ni() );
00048   x0_[1] = double( mask_image_.nj() );
00049   x1_[0] = 0.0;
00050   x1_[1] = 0.0;
00051 
00052   bool non_zero_pixel = false;
00053 
00054   for ( unsigned j=0; j<mask_image_.nj(); ++j )
00055     for ( unsigned i=0; i<mask_image_.ni(); ++i )
00056       if ( mask_image_(i,j) ) {
00057 
00058         if ( x0_[0] > double(i) )        x0_[0] = double(i);
00059         if ( x0_[1] > double(j) )        x0_[1] = double(j);
00060         if ( x1_[0] < double(i) )        x1_[0] = double(i);
00061         if ( x1_[1] < double(j) )        x1_[1] = double(j);
00062 
00063         non_zero_pixel = true;
00064       }
00065 
00066   // special case: no pixel is true
00067   if ( !non_zero_pixel ) {
00068     x0_.fill( 0.0 );
00069     x1_.fill( 0.0 );
00070   }
00071 }
00072 
00073 //******************* mask using a sphere *****************
00074 
00075 rgrl_mask_sphere::
00076 rgrl_mask_sphere( unsigned dim )
00077   : rgrl_mask( dim ), center_( dim, 0.0 ), radius_sqr_( 0 )
00078 {
00079 }
00080 
00081 rgrl_mask_sphere::
00082 rgrl_mask_sphere( const vnl_vector<double>& in_center,
00083                   double in_radius )
00084   : rgrl_mask( in_center.size() ),
00085     center_(in_center),
00086     radius_sqr_(in_radius*in_radius)
00087 {
00088   update_bounding_box();
00089 }
00090 
00091 bool
00092 rgrl_mask_sphere::
00093 inside( vnl_vector<double> const& pt ) const
00094 {
00095   assert( pt.size() == center_.size() );
00096 
00097   double sqr_sum = 0;
00098   for (unsigned int i = 0; i<pt.size(); ++i) {
00099     sqr_sum +=  vnl_math_sqr( pt[i] - center_[i] );
00100   }
00101   return sqr_sum < radius_sqr_ ;
00102 }
00103 
00104 void
00105 rgrl_mask_sphere::
00106 set_center( vnl_vector<double> const& pt )
00107 {
00108   assert( pt.size() == center_.size() );
00109   center_ = pt;
00110 
00111   update_bounding_box();
00112 }
00113 
00114 void
00115 rgrl_mask_sphere::
00116 set_radius( double radius )
00117 {
00118   radius_sqr_ = radius * radius;
00119 
00120   update_bounding_box();
00121 }
00122 
00123 void
00124 rgrl_mask_sphere::
00125 update_bounding_box()
00126 {
00127   // if ceter or radius not yet set
00128   if ( !center_.size() || !radius_sqr_ )
00129     return;
00130 
00131   const unsigned m = center_.size();
00132   x0_.set_size( m );
00133   x1_.set_size( m );
00134   double r = vcl_sqrt( radius_sqr_ );
00135   for ( unsigned i=0; i<m; ++i ) {
00136     x0_[i] = center_[i] - r;
00137     x1_[i] = center_[i] + r;
00138   }
00139 }
00140 
00141 //******************** mask using a box ***********************
00142 
00143 rgrl_mask_box::
00144 rgrl_mask_box( unsigned dim )
00145   : rgrl_mask( dim )
00146 {
00147 }
00148 
00149 rgrl_mask_box::
00150 rgrl_mask_box( vnl_vector<double> const& x0, vnl_vector<double> const& x1 )
00151   : rgrl_mask( x0.size() )
00152 {
00153   assert( x0.size() == x1.size() );
00154 
00155   //check
00156   for ( unsigned i=0; i<x0.size(); ++i )
00157     assert( x0[i] <= x1[i] );
00158 
00159   x0_ = x0;
00160   x1_ = x1;
00161 }
00162 
00163 bool
00164 rgrl_mask_box::
00165 inside( vnl_vector<double> const& pt ) const
00166 {
00167   assert( pt.size() == x1_.size() );
00168 
00169   bool inside = true;
00170   for (unsigned i =0; i<pt.size()&&inside; ++i) {
00171     inside = (x0_[i] <= pt[i] && pt[i] <= x1_[i]);
00172   }
00173   return inside;
00174 }
00175 
00176 void
00177 rgrl_mask_box::
00178 set_x0( vnl_vector<double> const& v )
00179 {
00180   assert( v.size() == x0_.size() || !x0_.size() );
00181   x0_ = v;
00182 }
00183 
00184 
00185 void
00186 rgrl_mask_box::
00187 set_x1( vnl_vector<double> const& v )
00188 {
00189   assert( v.size() == x1_.size() || !x1_.size() );
00190   x1_ = v;
00191 }
00192 
00193 bool
00194 rgrl_mask_box::
00195 operator==( const rgrl_mask_box& other ) const
00196 {
00197   return x0_ == other.x0_  &&
00198          x1_ == other.x1_;
00199 }
00200 
00201 bool
00202 rgrl_mask_box::
00203 operator!=( const rgrl_mask_box& other ) const
00204 {
00205   return !( *this == other );
00206 }
00207 
00208 vcl_ostream& operator<<(vcl_ostream& os, const rgrl_mask_box& box)
00209 {
00210   os<< box.x0().size() << "  ";
00211   if ( box.x0().size() )
00212     os << box.x0()<<"  "<<box.x1();
00213   return os;
00214 }
00215 
00216 vcl_istream& operator>>(vcl_istream& is, rgrl_mask_box& box)
00217 {
00218   int m = -1;
00219   is >> m;
00220 
00221   if ( m <= 0 ) return is;
00222 
00223   vnl_vector<double> x0(m), x1(m);
00224 
00225   is >> x0 >> x1;
00226   rgrl_mask_box temp_box( x0, x1 );
00227   box = temp_box;
00228   return is;
00229 }
00230 
00231 //--------------------------------
00232 //               Utility functions
00233 
00234 //: Intersection Box A with Box B (make it within the range of B). Then Box A has the result
00235 rgrl_mask_box
00236 rgrl_mask_box_intersection( rgrl_mask_box const& a, rgrl_mask_box const& b )
00237 {
00238   assert( a.x0().size() == b.x0().size() );
00239   assert( a.x1().size() == b.x1().size() );
00240 
00241   const unsigned m = a.x0().size();
00242   vnl_vector<double> new_x0=a.x0();
00243   vnl_vector<double> new_x1=a.x1();
00244   const vnl_vector<double>& b_x0=b.x0();
00245   const vnl_vector<double>& b_x1=b.x1();
00246 
00247   for ( unsigned d=0; d < m; ++d )
00248   {
00249     if ( new_x0[d] < b_x0[d] )  new_x0[d] = b_x0[d];
00250     if ( new_x0[d] > b_x1[d] )  new_x0[d] = b_x1[d];
00251 
00252     if ( new_x1[d] > b_x1[d] )  new_x1[d] = b_x1[d];
00253     if ( new_x1[d] < b_x0[d] )  new_x1[d] = b_x0[d];
00254   }
00255 
00256   return rgrl_mask_box( new_x0, new_x1 );
00257 }