contrib/brl/bseg/bbgm/bbgm_feature_image.h
Go to the documentation of this file.
00001 // This is brl/bseg/bbgm/bbgm_feature_image.h
00002 #ifndef bbgm_feature_image_h_
00003 #define bbgm_feature_image_h_
00004 
00005 //:
00006 // \file
00007 // \brief A templated image of features with associated probabilities
00008 // \author J.L. Mundy
00009 // \date May 3, 2008
00010 //
00011 // \verbatim
00012 //  Modifications
00013 // \endverbatim
00014 
00015 #include <vbl/vbl_array_2d.h>
00016 #include <vbl/vbl_ref_count.h>
00017 #include <vsl/vsl_binary_io.h>
00018 #include <vsl/vsl_binary_loader.h>
00019 #include <vcl_iostream.h>
00020 
00021 //: base class for images of features
00022 class bbgm_feature_image_base : public vbl_ref_count
00023 {
00024  public:
00025   virtual ~bbgm_feature_image_base(){}
00026 
00027   //: Binary save self to stream.
00028   virtual void b_write(vsl_b_ostream &os) const=0;
00029 
00030   //: Binary load self from stream.
00031   virtual void b_read(vsl_b_istream &is)=0;
00032 
00033   virtual vcl_string is_a() const=0;
00034 
00035   virtual bbgm_feature_image_base* clone() const = 0;
00036 
00037   virtual unsigned int ni() const = 0;
00038   virtual unsigned int nj() const = 0;
00039 };
00040 
00041 
00042 template<class f_type_>
00043 class bbgm_feature_image : public bbgm_feature_image_base
00044 {
00045  public:
00046   //: Constructor
00047   bbgm_feature_image<f_type_>() {}
00048   bbgm_feature_image<f_type_>(unsigned int ni, unsigned int nj): data_(nj,ni) {}
00049   bbgm_feature_image<f_type_>(unsigned int ni, unsigned int nj,
00050                               f_type_ const& feature) : data_(nj,ni,feature) {}
00051 
00052 
00053   //: Return the width of the image
00054   unsigned int ni() const { return data_.cols(); }
00055 
00056   //: Return the height
00057   unsigned int nj() const { return data_.rows(); }
00058 
00059   //: resize to ni x nj
00060   // If already correct size, this function returns quickly
00061   void set_size(unsigned ni, unsigned nj) { data_.resize(nj,ni); }
00062 
00063   //: Read only access to the features
00064   const f_type_& operator() (unsigned int i, unsigned int j) const
00065   { return data_(j,i); }
00066 
00067   //: Access to the features
00068   f_type_& operator() (unsigned int i, unsigned int j)
00069   { return data_(j,i); }
00070 
00071   //: Set the distribution at (i,j) to a copy of d
00072   void set(unsigned int i, unsigned int j, const f_type_& d)
00073   { data_(j,i) = d; }
00074 
00075   //: An iterator over the distribution in the image
00076   class iterator
00077   {
00078     public:
00079       iterator(f_type_* ptr) : ptr_(ptr) {}
00080       iterator(const iterator& other) : ptr_(other.ptr_) {}
00081       iterator& operator++() { ++ptr_; return *this; }
00082       f_type_& operator*(){ return *ptr_; }
00083       f_type_* operator -> () { return ptr_; }
00084       bool operator==(const iterator& other) { return ptr_ == other.ptr_; }
00085       bool operator!=(const iterator& other) { return ptr_ != other.ptr_; }
00086 
00087     private:
00088       f_type_* ptr_;
00089   };
00090 
00091   class const_iterator
00092   {
00093     public:
00094       const_iterator(const f_type_* ptr) : ptr_(ptr) {}
00095       const_iterator(const const_iterator& other) : ptr_(other.ptr_) {}
00096       const_iterator& operator++() { ++ptr_; return *this; }
00097       const f_type_& operator*(){ return *ptr_; }
00098       const f_type_* operator -> () { return ptr_; }
00099       bool operator==(const const_iterator& other) { return ptr_ == other.ptr_; }
00100       bool operator!=(const const_iterator& other) { return ptr_ != other.ptr_; }
00101 
00102     private:
00103       const f_type_* ptr_;
00104   };
00105 
00106   //: Return an iterator to the first element
00107   iterator begin() { return iterator(data_[0]); }
00108   //: Return an iterator to one past the last element
00109   iterator end() { return iterator(data_[0]+data_.size()); }
00110   //: Return a const iterator to the first element
00111   const_iterator begin() const { return const_iterator(data_[0]); }
00112   //: Return a const iterator to one past the last element
00113   const_iterator end() const { return const_iterator(data_[0]+data_.size()); }
00114 
00115   //===========================================================================
00116   // Binary I/O Methods
00117 
00118   //: Return a string name
00119   // \note this is probably not portable
00120   virtual vcl_string is_a() const;
00121 
00122   virtual bbgm_feature_image_base* clone() const;
00123 
00124   //: Return IO version number;
00125   short version() const;
00126 
00127   //: Binary save self to stream.
00128   virtual void b_write(vsl_b_ostream &os) const;
00129 
00130   //: Binary load self from stream.
00131   virtual void b_read(vsl_b_istream &is);
00132 
00133  private:
00134   //: the data
00135    vbl_array_2d<f_type_ > data_;
00136 };
00137 
00138 template <class f_type_>
00139 void vsl_print_summary(vcl_ostream& os,
00140                        const bbgm_feature_image<f_type_> & b)
00141 { os << "not yet implemented for bbgm_feature_image\n";}
00142 
00143 
00144 //: Binary save bbgm_feature_image
00145 template <class f_type_>
00146 void vsl_b_write(vsl_b_ostream &os, const bbgm_feature_image<f_type_>& b)
00147 {
00148   b.b_write(os);
00149 }
00150 
00151 //: Binary load bbgm_feature_image
00152 template <class f_type_>
00153 void vsl_b_read(vsl_b_istream &is, bbgm_feature_image<f_type_>& b)
00154 {
00155   bbgm_feature_image<f_type_> temp;
00156   temp.b_read(is);
00157   b = temp;
00158 }
00159 
00160 
00161 //: Add an instance to the binary loader
00162 void vsl_add_to_binary_loader(bbgm_feature_image_base const& b);
00163 
00164 
00165 #endif // bbgm_feature_image_h_