contrib/rpl/rgrl/rgrl_mask_3d_image.cxx
Go to the documentation of this file.
00001 #include "rgrl_mask_3d_image.h"
00002 //:
00003 // \file
00004 
00005 //: ************* mask using a binary 3D image ********************
00006 rgrl_mask_3d_image::
00007 rgrl_mask_3d_image( const vil3d_image_view< vxl_byte > & in_mask,
00008                     int org_x, int org_y, int org_z )
00009   : rgrl_mask( 3 ), mask_image_( in_mask ),
00010     org_x_( org_x ), org_y_( org_y ), org_z_( org_z )
00011 {
00012 }
00013 
00014 bool
00015 rgrl_mask_3d_image::
00016 inside( vnl_vector< double > const& pt ) const
00017 {
00018   double x = pt[0]-double(org_x_);
00019   double y = pt[1]-double(org_y_);
00020   double z = pt[2]-double(org_z_);
00021 
00022   // As the bounding box is tighter than image dim,
00023   // check w/ bounding box is sufficient
00024   //
00025   bool in_range = ( x0_[0] <= x ) && ( x <= x1_[0] ) &&
00026                   ( x0_[1] <= y ) && ( y <= x1_[1] ) &&
00027                   ( x0_[2] <= z ) && ( z <= x1_[2] );
00028 
00029   return in_range && mask_image_( (unsigned int)x, (unsigned int)y, (unsigned int)z ) > 0 ;
00030 }
00031 
00032 void
00033 rgrl_mask_3d_image::
00034 update_bounding_box()
00035 {
00036   // reset bounding box
00037   x0_[0] = double( mask_image_.ni() );
00038   x0_[1] = double( mask_image_.nj() );
00039   x0_[2] = double( mask_image_.nk() );
00040   x1_[0] = 0.0;
00041   x1_[1] = 0.0;
00042   x1_[2] = 0.0;
00043 
00044   bool non_zero_pixel = false;
00045 
00046   for ( unsigned k=0; k<mask_image_.nk(); ++k )
00047     for ( unsigned j=0; j<mask_image_.nj(); ++j )
00048       for ( unsigned i=0; i<mask_image_.ni(); ++i )
00049         if ( mask_image_(i,j,k) ) {
00050           if ( x0_[0] > double(i) )        x0_[0] = double(i);
00051           if ( x0_[1] > double(j) )        x0_[1] = double(j);
00052           if ( x0_[2] > double(k) )        x0_[2] = double(k);
00053           if ( x1_[0] < double(i) )        x1_[0] = double(i);
00054           if ( x1_[1] < double(j) )        x1_[1] = double(j);
00055           if ( x1_[2] < double(k) )        x1_[2] = double(k);
00056           non_zero_pixel = true;
00057         }
00058 
00059   // special case: no pixel is true
00060   if ( !non_zero_pixel ) {
00061     x0_.fill( 0.0 );
00062     x1_.fill( 0.0 );
00063   }
00064 }
00065