contrib/brl/bseg/bbgm/bbgm_image_of.h
Go to the documentation of this file.
00001 // This is brl/bseg/bbgm/bbgm_image_of.h
00002 #ifndef bbgm_image_of_h_
00003 #define bbgm_image_of_h_
00004 //:
00005 // \file
00006 // \brief An image of distributions (templated)
00007 // \author Matt Leotta (mleotta@lems.brown.edu)
00008 // \date January 27, 2006
00009 
00010 #include <vcl_typeinfo.h>
00011 #include <vbl/vbl_array_2d.h>
00012 #include <vbl/vbl_ref_count.h>
00013 #include <vsl/vsl_binary_loader.h>
00014 
00015 //: base class for images of distributions
00016 class bbgm_image_base : public vbl_ref_count
00017 {
00018   public:
00019     virtual ~bbgm_image_base(){}
00020 
00021     //: return the type_info for the distribution type
00022     virtual const vcl_type_info& dist_typeid() const=0;
00023 
00024     //: Binary save self to stream.
00025     virtual void b_write(vsl_b_ostream &os) const=0;
00026 
00027     //: Binary load self from stream.
00028     virtual void b_read(vsl_b_istream &is)=0;
00029 
00030     virtual vcl_string is_a() const=0;
00031 
00032     virtual bbgm_image_base* clone() const = 0;
00033 };
00034 
00035 
00036 //: An image of distributions
00037 template<class dist>
00038 class bbgm_image_of : public bbgm_image_base
00039 {
00040  public:
00041   //: Constructor
00042   bbgm_image_of<dist>() {}
00043   bbgm_image_of<dist>(unsigned int ni, unsigned int nj,
00044                       const dist& model) : data_(nj,ni,model) {}
00045 
00046   //: return the type_info for the distribution type
00047   virtual const vcl_type_info& dist_typeid() const { return typeid(dist); }
00048 
00049   //: Return the width of the image
00050   unsigned int ni() const { return data_.cols(); }
00051 
00052   //: Return the height
00053   unsigned int nj() const { return data_.rows(); }
00054 
00055   //: resize to ni x nj
00056   // If already correct size, this function returns quickly
00057   void set_size(unsigned ni, unsigned nj) { data_.resize(nj,ni); }
00058 
00059   //: Read only access to the distributions
00060   const dist& operator() (unsigned int i, unsigned int j) const
00061   { return data_(j,i); }
00062 
00063   //: Access to the distributions
00064   dist& operator() (unsigned int i, unsigned int j)
00065   { return data_(j,i); }
00066 
00067   //: Set the distribution at (i,j) to a copy of d
00068   void set(unsigned int i, unsigned int j, const dist& d)
00069   { data_(j,i) = d; }
00070 
00071   //: An iterator over the distribution in the image
00072   class iterator
00073   {
00074     public:
00075       iterator(dist* ptr) : ptr_(ptr) {}
00076       iterator(const iterator& other) : ptr_(other.ptr_) {}
00077       iterator& operator++() { ++ptr_; return *this; }
00078       dist& operator*(){ return *ptr_; }
00079       dist* operator -> () { return ptr_; }
00080       bool operator==(const iterator& other) { return ptr_ == other.ptr_; }
00081       bool operator!=(const iterator& other) { return ptr_ != other.ptr_; }
00082 
00083     private:
00084       dist* ptr_;
00085   };
00086 
00087   class const_iterator
00088   {
00089     public:
00090       const_iterator(const dist* ptr) : ptr_(ptr) {}
00091       const_iterator(const const_iterator& other) : ptr_(other.ptr_) {}
00092       const_iterator& operator++() { ++ptr_; return *this; }
00093       const dist& operator*(){ return *ptr_; }
00094       const dist* operator -> () { return ptr_; }
00095       bool operator==(const const_iterator& other) { return ptr_ == other.ptr_; }
00096       bool operator!=(const const_iterator& other) { return ptr_ != other.ptr_; }
00097 
00098     private:
00099       const dist* ptr_;
00100   };
00101 
00102   //: Return an iterator to the first element
00103   iterator begin() { return iterator(data_[0]); }
00104   //: Return an iterator to one past the last element
00105   iterator end() { return iterator(data_[0]+data_.size()); }
00106   //: Return a const iterator to the first element
00107   const_iterator begin() const { return const_iterator(data_[0]); }
00108   //: Return a const iterator to one past the last element
00109   const_iterator end() const { return const_iterator(data_[0]+data_.size()); }
00110 
00111   //===========================================================================
00112   // Binary I/O Methods
00113 
00114   //: Return a string name
00115   // \note this is probably not portable
00116   virtual vcl_string is_a() const;
00117 
00118   virtual bbgm_image_base* clone() const;
00119 
00120   //: Return IO version number;
00121   short version() const;
00122 
00123   //: Binary save self to stream.
00124   virtual void b_write(vsl_b_ostream &os) const;
00125 
00126   //: Binary load self from stream.
00127   virtual void b_read(vsl_b_istream &is);
00128 
00129  private:
00130   //: the data
00131    vbl_array_2d<dist> data_;
00132 };
00133 
00134 
00135 //: Add an instance to the binary loader
00136 void vsl_add_to_binary_loader(bbgm_image_base const& b);
00137 
00138 
00139 #endif // bbgm_image_of_h_