• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

image1d.hh

00001 // Copyright (C) 2007, 2008, 2009, 2011 EPITA Research and Development
00002 // Laboratory (LRDE)
00003 //
00004 // This file is part of Olena.
00005 //
00006 // Olena is free software: you can redistribute it and/or modify it under
00007 // the terms of the GNU General Public License as published by the Free
00008 // Software Foundation, version 2 of the License.
00009 //
00010 // Olena is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software project without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to produce
00022 // an executable, this file does not by itself cause the resulting
00023 // executable to be covered by the GNU General Public License.  This
00024 // exception does not however invalidate any other reasons why the
00025 // executable file might be covered by the GNU General Public License.
00026 
00027 #ifndef MLN_CORE_IMAGE_IMAGE1D_HH
00028 # define MLN_CORE_IMAGE_IMAGE1D_HH
00029 
00035 
00036 # include <mln/core/internal/fixme.hh>
00037 # include <mln/core/internal/image_primary.hh>
00038 # include <mln/core/alias/box1d.hh>
00039 
00040 # include <mln/border/thickness.hh>
00041 # include <mln/value/set.hh>
00042 # include <mln/fun/i2v/all_to.hh>
00043 
00044 
00045 // FIXME:
00046 
00047 // # include <mln/core/pixter1d.hh>
00048 // # include <mln/core/dpoints_pixter.hh>
00049 
00050 
00051 namespace mln
00052 {
00053 
00054   // Forward declaration.
00055   template <typename T> struct image1d;
00056 
00057 
00058   namespace internal
00059   {
00060 
00062     template <typename T>
00063     struct data< image1d<T> >
00064     {
00065       data(const box1d& b, unsigned bdr);
00066       ~data();
00067 
00068       T*  buffer_;
00069       T* array_;
00070 
00071       box1d b_;  // theoretical box
00072       unsigned bdr_;
00073       box1d vb_; // virtual box, i.e., box including the virtual border
00074 
00075       void update_vb_();
00076       void allocate_();
00077       void deallocate_();
00078       void swap_ (data< image1d<T> >& other_);
00079       void reallocate_(unsigned new_border);
00080     };
00081 
00082   } // end of namespace mln::internal
00083 
00084 
00085 
00086   namespace trait
00087   {
00088 
00089     template <typename T>
00090     struct image_< image1d<T> > : default_image_< T, image1d<T> >
00091     {
00092       // misc
00093       typedef trait::image::category::primary category;
00094       typedef trait::image::speed::fastest    speed;
00095       typedef trait::image::size::regular     size;
00096 
00097       // value
00098       typedef trait::image::vw_io::none                    vw_io;
00099       typedef trait::image::vw_set::none                   vw_set;
00100       typedef trait::image::value_access::direct           value_access;
00101       typedef trait::image::value_storage::one_block       value_storage;
00102       typedef trait::image::value_browsing::site_wise_only value_browsing;
00103       typedef trait::image::value_alignment::with_grid     value_alignment;
00104       typedef trait::image::value_io::read_write           value_io;
00105 
00106       // site / domain
00107       typedef trait::image::pw_io::read_write        pw_io;
00108       typedef trait::image::localization::basic_grid localization;
00109       typedef trait::image::dimension::one_d         dimension;
00110 
00111       // extended domain
00112       typedef trait::image::ext_domain::extendable ext_domain;
00113       typedef trait::image::ext_value::multiple    ext_value;
00114       typedef trait::image::ext_io::read_write     ext_io;
00115     };
00116 
00117   } // end of namespace mln::trait
00118 
00119 
00120 
00121   // Forward declaration.
00122   template <typename T> struct image1d;
00123 
00124 
00125 
00126   namespace convert
00127   {
00128 
00129     namespace over_load
00130     {
00131 
00132       // histo::array -> image1d.
00133       template <typename V, typename T>
00134       void from_to_(const histo::array<V>& from, image1d<T>& to);
00135 
00136       // util::array -> image1d.
00137       template <typename V, typename T>
00138       void from_to_(const util::array<V>& from, image1d<T>& to);
00139 
00140     } // end of namespace mln::convert::over_load
00141 
00142   } // end of namespace mln::convert
00143 
00144 
00145 
00153   //
00154   template <typename T>
00155   struct image1d :
00156     public internal::image_primary< T, box1d, image1d<T> >
00157   {
00158     typedef internal::image_primary< T, mln::box1d, image1d<T> > super_;
00159 
00161     typedef T         value;
00162 
00164     typedef const T& rvalue;
00165 
00167     typedef T&       lvalue;
00168 
00170     typedef image1d< tag::value_<T> > skeleton;
00171 
00172 
00174     image1d();
00175 
00178     image1d(unsigned ninds, unsigned bdr = border::thickness);
00179 
00181     image1d(const box1d& b, unsigned bdr = border::thickness);
00182 
00183 
00185     void init_(const box1d& b, unsigned bdr = border::thickness);
00186 
00187 
00189     bool has(const point1d& p) const;
00190 
00192     const box1d& domain() const;
00193 
00195     const box1d& bbox() const;
00196 
00198     const box1d& vbbox() const;
00199 
00201     unsigned border() const;
00202 
00204     const T& operator()(const point1d& p) const;
00205 
00207     T& operator()(const point1d& p);
00208 
00209 
00210     // Specific methods:
00211     // -----------------
00212 
00214     const T& at_(def::coord index) const;
00215 
00217     T& at_(def::coord index);
00218 
00220     unsigned ninds() const;
00221 
00222 
00223 
00225 
00226     // Give the index of a point.
00227     using super_::index_of_point;
00228 
00230     int delta_index(const dpoint1d& dp) const;
00231 
00233     point1d point_at_index(unsigned i) const;
00234 
00236     const T* buffer() const;
00237 
00239     T* buffer();
00240 
00243     const T& element(unsigned i) const;
00244 
00247     T& element(unsigned i);
00248 
00250     unsigned nelements() const;
00251 
00252 
00253 
00255     void resize_(unsigned new_border);
00256 
00257   };
00258 
00259   template <typename T, typename J>
00260   void init_(tag::image_t, mln::image1d<T>& target, const J& model);
00261 
00262 # ifndef MLN_INCLUDE_ONLY
00263 
00264   // init_
00265   template <typename T>
00266   inline
00267   void init_(tag::border_t, unsigned& b, const image1d<T>& model)
00268   {
00269     b = model.border();
00270   }
00271 
00272   template <typename T, typename J>
00273   inline
00274   void init_(tag::image_t, image1d<T>& target, const J& model)
00275   {
00276     box1d b;
00277     init_(tag::bbox, b, model);
00278     unsigned bdr;
00279     init_(tag::border, bdr, model);
00280     target.init_(b, bdr);
00281   }
00282 
00283   // internal::data< image1d<T> >
00284 
00285   namespace internal
00286   {
00287 
00288     template <typename T>
00289     inline
00290     data< image1d<T> >::data(const box1d& b, unsigned bdr)
00291       : buffer_(0),
00292         array_ (0),
00293         b_     (b),
00294         bdr_   (bdr)
00295     {
00296       allocate_();
00297     }
00298 
00299     template <typename T>
00300     inline
00301     data< image1d<T> >::~data()
00302     {
00303       deallocate_();
00304     }
00305 
00306     template <typename T>
00307     inline
00308     void
00309     data< image1d<T> >::update_vb_()
00310     {
00311       dpoint1d dp(all_to(bdr_));
00312 
00313       vb_.pmin() = b_.pmin() - dp;
00314       vb_.pmax() = b_.pmax() + dp;
00315     }
00316 
00317     template <typename T>
00318     inline
00319     void
00320     data< image1d<T> >::allocate_()
00321     {
00322       update_vb_();
00323       unsigned
00324         ni = vb_.len(0);
00325       buffer_ = new T[ni];
00326       array_ = buffer_ - vb_.pmin().ind();
00327       mln_postcondition(vb_.len(0) == b_.len(0) + 2 * bdr_);
00328     }
00329 
00330     template <typename T>
00331     inline
00332     void
00333     data< image1d<T> >::deallocate_()
00334     {
00335       if (buffer_)
00336       {
00337         delete[] buffer_;
00338         buffer_ = 0;
00339       }
00340     }
00341 
00342 
00343     template <typename T>
00344     inline
00345     void
00346     data< image1d<T> >::swap_(data< image1d<T> >& other_)
00347     {
00348 
00349       data< image1d<T> > self_ = *this;
00350       *this = other_;
00351       other_ = self_;
00352 
00353     }
00354 
00355     template <typename T>
00356     inline
00357     void
00358     data< image1d<T> >::reallocate_(unsigned new_border)
00359     {
00360       data< image1d<T> >& tmp = *(new data< image1d<T> >(this->b_, new_border));
00361       this->swap_(tmp);
00362     }
00363 
00364   } // end of namespace mln::internal
00365 
00366 
00367   // image1d<T>
00368 
00369   template <typename T>
00370   inline
00371   image1d<T>::image1d()
00372   {
00373   }
00374 
00375   template <typename T>
00376   inline
00377   image1d<T>::image1d(const box1d& b, unsigned bdr)
00378   {
00379     init_(b, bdr);
00380   }
00381 
00382   template <typename T>
00383   inline
00384   image1d<T>::image1d(unsigned ninds, unsigned bdr)
00385   {
00386     mln_precondition(ninds != 0);
00387     init_(make::box1d(ninds), bdr);
00388   }
00389 
00390   template <typename T>
00391   inline
00392   void
00393   image1d<T>::init_(const box1d& b, unsigned bdr)
00394   {
00395     mln_precondition(! this->is_valid());
00396     this->data_ = new internal::data< image1d<T> >(b, bdr);
00397   }
00398 
00399   template <typename T>
00400   inline
00401   const box1d&
00402   image1d<T>::domain() const
00403   {
00404     mln_precondition(this->is_valid());
00405     return this->data_->b_;
00406   }
00407 
00408   template <typename T>
00409   inline
00410   const box1d&
00411   image1d<T>::bbox() const
00412   {
00413     mln_precondition(this->is_valid());
00414     return this->data_->b_;
00415   }
00416 
00417   template <typename T>
00418   inline
00419   const box1d&
00420   image1d<T>::vbbox() const
00421   {
00422     mln_precondition(this->is_valid());
00423     return this->data_->vb_;
00424   }
00425 
00426   template <typename T>
00427   inline
00428   unsigned
00429   image1d<T>::border() const
00430   {
00431     mln_precondition(this->is_valid());
00432     return this->data_->bdr_;
00433   }
00434 
00435   template <typename T>
00436   inline
00437   unsigned
00438   image1d<T>::nelements() const
00439   {
00440     mln_precondition(this->is_valid());
00441     return this->data_->vb_.nsites();
00442   }
00443 
00444   template <typename T>
00445   inline
00446   bool
00447   image1d<T>::has(const point1d& p) const
00448   {
00449     mln_precondition(this->is_valid());
00450     return this->data_->vb_.has(p);
00451   }
00452 
00453   template <typename T>
00454   inline
00455   const T&
00456   image1d<T>::operator()(const point1d& p) const
00457   {
00458     mln_precondition(this->has(p));
00459     return this->data_->array_[p.ind()];
00460   }
00461 
00462   template <typename T>
00463   inline
00464   T&
00465   image1d<T>::operator()(const point1d& p)
00466   {
00467     mln_precondition(this->has(p));
00468     return this->data_->array_[p.ind()];
00469   }
00470 
00471   template <typename T>
00472   inline
00473   const T&
00474   image1d<T>::at_(def::coord index) const
00475   {
00476     mln_precondition(this->has(point1d(index)));
00477     return this->data_->array_[index];
00478   }
00479 
00480   template <typename T>
00481   inline
00482   unsigned
00483   image1d<T>::ninds() const
00484   {
00485     mln_precondition(this->is_valid());
00486     return this->data_->b_.len(0);
00487   }
00488 
00489   template <typename T>
00490   inline
00491   T&
00492   image1d<T>::at_(def::coord index)
00493   {
00494     mln_precondition(this->has(point1d(index)));
00495     return this->data_->array_[index];
00496   }
00497 
00498 
00499   template <typename T>
00500   inline
00501   const T&
00502   image1d<T>::element(unsigned i) const
00503   {
00504     mln_precondition(i < nelements());
00505     return this->data_->buffer_[i];
00506   }
00507 
00508   template <typename T>
00509   inline
00510   T&
00511   image1d<T>::element(unsigned i)
00512   {
00513     mln_precondition(i < nelements());
00514     return this->data_->buffer_[i];
00515   }
00516 
00517   template <typename T>
00518   inline
00519   const T*
00520   image1d<T>::buffer() const
00521   {
00522     mln_precondition(this->is_valid());
00523     return this->data_->buffer_;
00524   }
00525 
00526   template <typename T>
00527   inline
00528   T*
00529   image1d<T>::buffer()
00530   {
00531     mln_precondition(this->is_valid());
00532     return this->data_->buffer_;
00533   }
00534 
00535   template <typename T>
00536   inline
00537   int
00538   image1d<T>::delta_index(const dpoint1d& dp) const
00539   {
00540     mln_precondition(this->is_valid());
00541     int o = dp[0];
00542     return o;
00543   }
00544 
00545   template <typename T>
00546   inline
00547   point1d
00548   image1d<T>::point_at_index(unsigned i) const
00549   {
00550     mln_precondition(i < nelements());
00551     def::coord ind = static_cast<def::coord>(i + this->data_->vb_.min_ind());
00552     point1d p = point1d(ind);
00553     mln_postcondition(& this->operator()(p) == this->data_->buffer_ + i);
00554     return p;
00555   }
00556 
00557   template <typename T>
00558   inline
00559   void
00560   image1d<T>::resize_(unsigned new_border)
00561   {
00562     this->data_->reallocate_(new_border);
00563   }
00564 
00565 # endif // ! MLN_INCLUDE_ONLY
00566 
00567 } // end of namespace mln
00568 
00569 
00570 
00571 # include <mln/core/trait/pixter.hh>
00572 # include <mln/core/dpoints_pixter.hh>
00573 # include <mln/core/pixter1d.hh>
00574 # include <mln/core/w_window.hh>
00575 
00576 
00577 
00578 namespace mln
00579 {
00580 
00581 
00582   namespace convert
00583   {
00584 
00585     namespace over_load
00586     {
00587 
00588       // histo::array -> image1d.
00589       template <typename V, typename T>
00590       inline
00591       void
00592       from_to_(const histo::array<V>& from, image1d<T>& to)
00593       {
00594         // FIXME: The code should looks like:
00595 
00596 //      box1d b(point1d(mln_min(V)), point1d(mln_max(V)));
00597 //      ima.init_(b, 0);
00598 //      for_all(v)
00599 //        from_to(h(v), ima.at_( index_of(v) ));
00600         to.init_(make::box1d(from.nvalues()));
00601         for (unsigned i = 0; i < from.nvalues(); ++i)
00602           from_to(from[i], to(point1d(i)));
00603       }
00604 
00605       // util::array -> image1d.
00606       template <typename V, typename T>
00607       inline
00608       void
00609       from_to_(const util::array<V>& from, image1d<T>& to)
00610       {
00611         to.init_(make::box1d(from.nelements()));
00612         for (unsigned i = 0; i < from.nelements(); ++i)
00613           from_to(from[i], to(point1d(i)));
00614       }
00615 
00616     } // end of namespace mln::convert::over_load
00617 
00618   } // end of namespace mln::convert
00619 
00620 
00621   namespace trait
00622   {
00623 
00624     // pixter
00625 
00626     template <typename T>
00627     struct fwd_pixter< image1d<T> >
00628     {
00629       typedef fwd_pixter1d< image1d<T> > ret;
00630     };
00631 
00632     template <typename T>
00633     struct fwd_pixter< const image1d<T> >
00634     {
00635       typedef fwd_pixter1d< const image1d<T> > ret;
00636     };
00637 
00638     template <typename T>
00639     struct bkd_pixter< image1d<T> >
00640     {
00641       typedef bkd_pixter1d< image1d<T> > ret;
00642     };
00643 
00644     template <typename T>
00645     struct bkd_pixter< const image1d<T> >
00646     {
00647       typedef bkd_pixter1d< const image1d<T> > ret;
00648     };
00649 
00650     // qixter
00651 
00652     template <typename T, typename W>
00653     struct fwd_qixter< image1d<T>, W >
00654     {
00655       typedef dpoints_fwd_pixter< image1d<T> > ret;
00656     };
00657 
00658     template <typename T, typename W>
00659     struct fwd_qixter< const image1d<T>, W >
00660     {
00661       typedef dpoints_fwd_pixter< const image1d<T> > ret;
00662     };
00663 
00664     template <typename T, typename W>
00665     struct bkd_qixter< image1d<T>, W >
00666     {
00667       typedef dpoints_bkd_pixter< image1d<T> > ret;
00668     };
00669 
00670     template <typename T, typename W>
00671     struct bkd_qixter< const image1d<T>, W >
00672     {
00673       typedef dpoints_bkd_pixter< const image1d<T> > ret;
00674     };
00675 
00676     // nixter
00677 
00678     template <typename T, typename W>
00679     struct fwd_nixter< image1d<T>, W >
00680     {
00681       typedef dpoints_fwd_pixter< image1d<T> > ret;
00682     };
00683 
00684     template <typename T, typename W>
00685     struct fwd_nixter< const image1d<T>, W >
00686     {
00687       typedef dpoints_fwd_pixter< const image1d<T> > ret;
00688     };
00689 
00690     template <typename T, typename W>
00691     struct bkd_nixter< image1d<T>, W >
00692     {
00693       typedef dpoints_bkd_pixter< image1d<T> > ret;
00694     };
00695 
00696     template <typename T, typename W>
00697     struct bkd_nixter< const image1d<T>, W >
00698     {
00699       typedef dpoints_bkd_pixter< const image1d<T> > ret;
00700     };
00701 
00702   } // end of namespace mln::trait
00703 
00704 } // end of namespace mln
00705 
00706 # include <mln/make/image.hh>
00707 
00708 #endif // ! MLN_CORE_IMAGE_IMAGE1D_HH

Generated on Tue Oct 4 2011 15:23:56 for Milena (Olena) by  doxygen 1.7.1