• 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       dpoint1d dp(all_to(bdr_));
00308 
00309       vb_.pmin() = b_.pmin() - dp;
00310       vb_.pmax() = b_.pmax() + dp;
00311     }
00312 
00313     template <typename T>
00314     inline
00315     void
00316     data< image1d<T> >::allocate_()
00317     {
00318       update_vb_();
00319       unsigned
00320         ni = vb_.len(0);
00321       buffer_ = new T[ni];
00322       array_ = buffer_ - vb_.pmin().ind();
00323       mln_postcondition(vb_.len(0) == b_.len(0) + 2 * bdr_);
00324     }
00325 
00326     template <typename T>
00327     inline
00328     void
00329     data< image1d<T> >::deallocate_()
00330     {
00331       if (buffer_)
00332       {
00333         delete[] buffer_;
00334         buffer_ = 0;
00335       }
00336     }
00337 
00338 
00339     template <typename T>
00340     inline
00341     void
00342     data< image1d<T> >::swap_(data< image1d<T> >& other_)
00343     {
00344 
00345       data< image1d<T> > self_ = *this;
00346       *this = other_;
00347       other_ = self_;
00348 
00349     }
00350 
00351     template <typename T>
00352     inline
00353     void
00354     data< image1d<T> >::reallocate_(unsigned new_border)
00355     {
00356       data< image1d<T> >& tmp = *(new data< image1d<T> >(this->b_, new_border));
00357       this->swap_(tmp);
00358     }
00359 
00360   } // end of namespace mln::internal
00361 
00362 
00363   // image1d<T>
00364 
00365   template <typename T>
00366   inline
00367   image1d<T>::image1d()
00368   {
00369   }
00370 
00371   template <typename T>
00372   inline
00373   image1d<T>::image1d(const box1d& b, unsigned bdr)
00374   {
00375     init_(b, bdr);
00376   }
00377 
00378   template <typename T>
00379   inline
00380   image1d<T>::image1d(unsigned ninds, unsigned bdr)
00381   {
00382     mln_precondition(ninds != 0);
00383     init_(make::box1d(ninds), bdr);
00384   }
00385 
00386   template <typename T>
00387   inline
00388   void
00389   image1d<T>::init_(const box1d& b, unsigned bdr)
00390   {
00391     mln_precondition(! this->is_valid());
00392     this->data_ = new internal::data< image1d<T> >(b, bdr);
00393   }
00394 
00395   template <typename T>
00396   inline
00397   const box1d&
00398   image1d<T>::domain() const
00399   {
00400     mln_precondition(this->is_valid());
00401     return this->data_->b_;
00402   }
00403 
00404   template <typename T>
00405   inline
00406   const box1d&
00407   image1d<T>::bbox() const
00408   {
00409     mln_precondition(this->is_valid());
00410     return this->data_->b_;
00411   }
00412 
00413   template <typename T>
00414   inline
00415   unsigned
00416   image1d<T>::border() const
00417   {
00418     mln_precondition(this->is_valid());
00419     return this->data_->bdr_;
00420   }
00421 
00422   template <typename T>
00423   inline
00424   unsigned
00425   image1d<T>::nelements() const
00426   {
00427     mln_precondition(this->is_valid());
00428     return this->data_->vb_.nsites();
00429   }
00430 
00431   template <typename T>
00432   inline
00433   bool
00434   image1d<T>::has(const point1d& p) const
00435   {
00436     mln_precondition(this->is_valid());
00437     return this->data_->vb_.has(p);
00438   }
00439 
00440   template <typename T>
00441   inline
00442   const T&
00443   image1d<T>::operator()(const point1d& p) const
00444   {
00445     mln_precondition(this->has(p));
00446     return this->data_->array_[p.ind()];
00447   }
00448 
00449   template <typename T>
00450   inline
00451   T&
00452   image1d<T>::operator()(const point1d& p)
00453   {
00454     mln_precondition(this->has(p));
00455     return this->data_->array_[p.ind()];
00456   }
00457 
00458   template <typename T>
00459   inline
00460   const T&
00461   image1d<T>::at_(def::coord index) const
00462   {
00463     mln_precondition(this->has(point1d(index)));
00464     return this->data_->array_[index];
00465   }
00466 
00467   template <typename T>
00468   inline
00469   unsigned
00470   image1d<T>::ninds() const
00471   {
00472     mln_precondition(this->is_valid());
00473     return this->data_->b_.len(0);
00474   }
00475 
00476   template <typename T>
00477   inline
00478   T&
00479   image1d<T>::at_(def::coord index)
00480   {
00481     mln_precondition(this->has(point1d(index)));
00482     return this->data_->array_[index];
00483   }
00484 
00485 
00486   template <typename T>
00487   inline
00488   const T&
00489   image1d<T>::element(unsigned i) const
00490   {
00491     mln_precondition(i < nelements());
00492     return this->data_->buffer_[i];
00493   }
00494 
00495   template <typename T>
00496   inline
00497   T&
00498   image1d<T>::element(unsigned i)
00499   {
00500     mln_precondition(i < nelements());
00501     return this->data_->buffer_[i];
00502   }
00503 
00504   template <typename T>
00505   inline
00506   const T*
00507   image1d<T>::buffer() const
00508   {
00509     mln_precondition(this->is_valid());
00510     return this->data_->buffer_;
00511   }
00512 
00513   template <typename T>
00514   inline
00515   T*
00516   image1d<T>::buffer()
00517   {
00518     mln_precondition(this->is_valid());
00519     return this->data_->buffer_;
00520   }
00521 
00522   template <typename T>
00523   inline
00524   int
00525   image1d<T>::delta_index(const dpoint1d& dp) const
00526   {
00527     mln_precondition(this->is_valid());
00528     int o = dp[0];
00529     return o;
00530   }
00531 
00532   template <typename T>
00533   inline
00534   point1d
00535   image1d<T>::point_at_index(unsigned i) const
00536   {
00537     mln_precondition(i < nelements());
00538     def::coord ind = static_cast<def::coord>(i + this->data_->vb_.min_ind());
00539     point1d p = point1d(ind);
00540     mln_postcondition(& this->operator()(p) == this->data_->buffer_ + i);
00541     return p;
00542   }
00543 
00544   template <typename T>
00545   inline
00546   void
00547   image1d<T>::resize_(unsigned new_border)
00548   {
00549     this->data_->reallocate_(new_border);
00550   }
00551 
00552 # endif // ! MLN_INCLUDE_ONLY
00553 
00554 } // end of namespace mln
00555 
00556 
00557 
00558 # include <mln/core/trait/pixter.hh>
00559 # include <mln/core/dpoints_pixter.hh>
00560 # include <mln/core/pixter1d.hh>
00561 # include <mln/core/w_window.hh>
00562 
00563 
00564 
00565 namespace mln
00566 {
00567 
00568 
00569   namespace convert
00570   {
00571 
00572     namespace over_load
00573     {
00574 
00575       // histo::array -> image1d.
00576       template <typename V, typename T>
00577       inline
00578       void
00579       from_to_(const histo::array<V>& from, image1d<T>& to)
00580       {
00581         // FIXME: The code should looks like:
00582 
00583 //      box1d b(point1d(mln_min(V)), point1d(mln_max(V)));
00584 //      ima.init_(b, 0);
00585 //      for_all(v)
00586 //        from_to(h(v), ima.at_( index_of(v) ));
00587         to.init_(make::box1d(from.nvalues()));
00588         for (unsigned i = 0; i < from.nvalues(); ++i)
00589           from_to(from[i], to(point1d(i)));
00590       }
00591 
00592       // util::array -> image1d.
00593       template <typename V, typename T>
00594       inline
00595       void
00596       from_to_(const util::array<V>& from, image1d<T>& to)
00597       {
00598         to.init_(make::box1d(from.nelements()));
00599         for (unsigned i = 0; i < from.nelements(); ++i)
00600           from_to(from[i], to(point1d(i)));
00601       }
00602 
00603     } // end of namespace mln::convert::over_load
00604 
00605   } // end of namespace mln::convert
00606 
00607 
00608   namespace trait
00609   {
00610 
00611     // pixter
00612 
00613     template <typename T>
00614     struct fwd_pixter< image1d<T> >
00615     {
00616       typedef fwd_pixter1d< image1d<T> > ret;
00617     };
00618 
00619     template <typename T>
00620     struct fwd_pixter< const image1d<T> >
00621     {
00622       typedef fwd_pixter1d< const image1d<T> > ret;
00623     };
00624 
00625     template <typename T>
00626     struct bkd_pixter< image1d<T> >
00627     {
00628       typedef bkd_pixter1d< image1d<T> > ret;
00629     };
00630 
00631     template <typename T>
00632     struct bkd_pixter< const image1d<T> >
00633     {
00634       typedef bkd_pixter1d< const image1d<T> > ret;
00635     };
00636 
00637     // qixter
00638 
00639     template <typename T, typename W>
00640     struct fwd_qixter< image1d<T>, W >
00641     {
00642       typedef dpoints_fwd_pixter< image1d<T> > ret;
00643     };
00644 
00645     template <typename T, typename W>
00646     struct fwd_qixter< const image1d<T>, W >
00647     {
00648       typedef dpoints_fwd_pixter< const image1d<T> > ret;
00649     };
00650 
00651     template <typename T, typename W>
00652     struct bkd_qixter< image1d<T>, W >
00653     {
00654       typedef dpoints_bkd_pixter< image1d<T> > ret;
00655     };
00656 
00657     template <typename T, typename W>
00658     struct bkd_qixter< const image1d<T>, W >
00659     {
00660       typedef dpoints_bkd_pixter< const image1d<T> > ret;
00661     };
00662 
00663     // nixter
00664 
00665     template <typename T, typename W>
00666     struct fwd_nixter< image1d<T>, W >
00667     {
00668       typedef dpoints_fwd_pixter< image1d<T> > ret;
00669     };
00670 
00671     template <typename T, typename W>
00672     struct fwd_nixter< const image1d<T>, W >
00673     {
00674       typedef dpoints_fwd_pixter< const image1d<T> > ret;
00675     };
00676 
00677     template <typename T, typename W>
00678     struct bkd_nixter< image1d<T>, W >
00679     {
00680       typedef dpoints_bkd_pixter< image1d<T> > ret;
00681     };
00682 
00683     template <typename T, typename W>
00684     struct bkd_nixter< const image1d<T>, W >
00685     {
00686       typedef dpoints_bkd_pixter< const image1d<T> > ret;
00687     };
00688 
00689   } // end of namespace mln::trait
00690 
00691 } // end of namespace mln
00692 
00693 # include <mln/make/image.hh>
00694 
00695 #endif // ! MLN_CORE_IMAGE_IMAGE1D_HH

Generated on Fri Oct 19 2012 04:15:54 for Milena (Olena) by  doxygen 1.7.1