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

image1d.hh

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

Generated on Thu Sep 8 2011 18:31:58 for Milena (Olena) by  doxygen 1.7.1