contrib/rpl/rgrl/rgrl_mask.h
Go to the documentation of this file.
00001 #ifndef rgrl_mask_h_
00002 #define rgrl_mask_h_
00003 //:
00004 // \file
00005 // \brief  hierarchy of mask of various shapes.
00006 //         Disregarding the shape, each mask also provides
00007 //         a bounding box.  Denoting the upper left corner as x0,
00008 //         and bottom right as x1 (in 2D case), the bounding box
00009 //         is defined by a tight interval [x0, x1] on all dimensions.
00010 //
00011 // \author Charlene Tsai
00012 // \date   Oct 2003
00013 // \verbatim
00014 // Modifications
00015 // Oct. 2006  Gehua Yang (RPI) - move rgrl_mask_3d_image into separate file
00016 // \endverbatim
00017 
00018 #include <vcl_iosfwd.h>
00019 #include <vnl/vnl_vector.h>
00020 #include <vil/vil_image_view.h>
00021 
00022 #include <rgrl/rgrl_object.h>
00023 #include <rgrl/rgrl_macros.h>
00024 
00025 // Defines a ROI (termed mask) which is required by certain feature_set
00026 // to outline region where operations are performed (e.g. registration).
00027 
00028 class rgrl_mask_box;
00029 
00030 //: base class of roi which is a pure virtual class
00031 class rgrl_mask
00032   : public rgrl_object
00033 {
00034  public:
00035   rgrl_mask( unsigned dim=0 ) : x0_(dim, 0.0), x1_(dim, 0.0) { }
00036 
00037   virtual ~rgrl_mask(){}
00038 
00039   //: True if a point is inside the region
00040   virtual bool inside( vnl_vector<double> const& ) const = 0;
00041 
00042   // Defines type-related functions
00043   rgrl_type_macro( rgrl_mask, rgrl_object );
00044 
00045   //: The lower coordinate of the bounding box.
00046   vnl_vector<double> const& x0() const
00047   { return x0_; }
00048 
00049   //: The upper coordinate of the bounding box.
00050   vnl_vector<double> const& x1() const
00051   { return x1_; }
00052 
00053   rgrl_mask_box bounding_box() const;
00054 
00055  protected:
00056   vnl_vector<double> x0_, x1_;
00057 };
00058 
00059 
00060 //: this is a wrapper on the binary mask image. The image is define for the x-y plane.
00061 class rgrl_mask_2d_image
00062   : public rgrl_mask
00063 {
00064  public:
00065   rgrl_mask_2d_image( const vil_image_view<vxl_byte>& in_mask,
00066                       int org_x = 0, int org_y = 0);
00067 
00068   //: True if a point is inside the region
00069   bool inside( vnl_vector<double> const& pt ) const;
00070 
00071   // Defines type-related functions
00072   rgrl_type_macro( rgrl_mask_2d_image, rgrl_mask );
00073 
00074  private:
00075   void update_bounding_box();
00076 
00077  private:
00078   vil_image_view<vxl_byte> mask_image_;
00079   int org_x_, org_y_;
00080 };
00081 
00082 //: This is a wrapper on the circular region or sphere
00083 class rgrl_mask_sphere
00084   : public rgrl_mask
00085 {
00086  public:
00087   //: Null initialization
00088   //
00089   // Initializes the region to the sphere centered at (0,0,...) with
00090   // radius set to 0.
00091   //
00092   rgrl_mask_sphere( unsigned dim );
00093 
00094   rgrl_mask_sphere( const vnl_vector<double>& in_center,
00095                     double in_radius );
00096 
00097   //: True if a point is inside the region
00098   bool inside( vnl_vector<double> const& pt ) const;
00099 
00100   void set_center( vnl_vector<double> const& pt );
00101 
00102   void set_radius( double radius );
00103 
00104   // Defines type-related functions
00105   rgrl_type_macro( rgrl_mask_sphere, rgrl_mask );
00106 
00107  private:
00108   void update_bounding_box();
00109 
00110  private:
00111   vnl_vector<double> center_;
00112   double radius_sqr_;
00113 };
00114 
00115 
00116 //: this is a wrapper on the box
00117 class rgrl_mask_box
00118   : public rgrl_mask
00119 {
00120  public:
00121   //: Null initialization
00122   //
00123   // Initializes the region to the box from (0,0,...) to (0,0,...)
00124   //
00125   rgrl_mask_box( unsigned dim );
00126 
00127   //: Initialize the region to a box from \a x0 to \a x1.
00128   rgrl_mask_box( vnl_vector<double> const& x0, vnl_vector<double> const& x1 );
00129 
00130   //: True if a point is inside the region
00131   bool inside( vnl_vector<double> const& pt ) const;
00132 
00133   //: Set the lower coordinate of the box.
00134   void set_x0( vnl_vector<double> const& v );
00135 
00136   //: Set the upper coordinate of the box.
00137   void set_x1( vnl_vector<double> const& v );
00138 
00139   //: True iff all the components are equal.
00140   bool operator==( const rgrl_mask_box& other ) const;
00141 
00142   //: True iff some of the components are not equal.
00143   bool operator!=( const rgrl_mask_box& other ) const;
00144 
00145   // Defines type-related functions
00146   rgrl_type_macro( rgrl_mask_box, rgrl_mask );
00147 };
00148 
00149 //: An output operator for displaying a mask_box
00150 vcl_ostream& operator<<(vcl_ostream& os, const rgrl_mask_box& box);
00151 
00152 //: An output operator for displaying a mask_box
00153 vcl_istream& operator>>(vcl_istream& is, rgrl_mask_box& box);
00154 
00155 //--------------------------------
00156 //               Utility functions
00157 
00158 //: Intersection Box A with Box B (make it within the range of B).
00159 rgrl_mask_box
00160 rgrl_mask_box_intersection( rgrl_mask_box const& a, rgrl_mask_box const& b );
00161 
00162 
00163 #endif