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

dpoint.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_DPOINT_HH
00027 # define MLN_CORE_DPOINT_HH
00028 
00032 
00033 # include <mln/core/def/coord.hh>
00034 # include <mln/core/concept/gdpoint.hh>
00035 # include <mln/core/internal/coord_impl.hh>
00036 # include <mln/fun/i2v/all.hh>
00037 # include <mln/algebra/vec.hh>
00038 # include <mln/metal/converts_to.hh>
00039 
00040 
00041 namespace mln
00042 {
00043 
00045   template <typename G, typename C> struct point;
00046   namespace literal {
00047     struct zero_t;
00048     struct one_t;
00049   }
00051 
00052 
00057   template <typename G, typename C>
00058   struct dpoint : public Gdpoint< dpoint<G,C> >,
00059                   public internal::mutable_coord_impl_< G::dim, C, dpoint<G,C> >
00060   {
00064     enum { dim = G::dim };
00065 
00067     typedef G grid;
00068 
00070     typedef point<G,C> psite;
00071 
00073     typedef point<G,C> site;
00074 
00076     typedef C coord;
00077 
00079     typedef algebra::vec<G::dim, C> vec;
00080 
00084     C  operator[](unsigned i) const;
00085 
00089     C& operator[](unsigned i);
00090 
00092     dpoint();
00093 
00095     template <typename C2>
00096     dpoint(const algebra::vec<dim,C2>& v);
00097 
00100     dpoint(C ind);
00101     dpoint(C row, C col);
00102     dpoint(C sli, C row, C col);
00104 
00106     dpoint(const literal::zero_t&);
00107     dpoint<G,C>& operator=(const literal::zero_t&);
00108     // Works only in 1D:
00109     dpoint(const literal::one_t&);
00110     dpoint<G,C>& operator=(const literal::one_t&);
00112 
00114     template <typename F>
00115     dpoint(const Function_v2v<F>& f);
00116 
00118     void set_all(C c);
00119 
00121     template <typename Q>
00122     operator mln::algebra::vec<dpoint<G,C>::dim, Q>() const;
00123 
00125     vec to_vec() const;
00126 
00127   protected:
00128     mln::algebra::vec<G::dim, C> coord_;
00129   };
00130 
00131 
00132 # ifndef MLN_INCLUDE_ONLY
00133 
00134   template <typename G, typename C>
00135   inline
00136   C dpoint<G,C>::operator[](unsigned i) const
00137   {
00138     assert(i < dim);
00139     return coord_[i];
00140   }
00141 
00142   template <typename G, typename C>
00143   inline
00144   C& dpoint<G,C>::operator[](unsigned i)
00145   {
00146     assert(i < dim);
00147     return coord_[i];
00148   }
00149 
00150   template <typename G, typename C>
00151   inline
00152   dpoint<G,C>::dpoint()
00153   {
00154   }
00155 
00156   template <typename G, typename C>
00157   template <typename C2>
00158   inline
00159   dpoint<G,C>::dpoint(const algebra::vec<dim,C2>& v)
00160   {
00161     // FIXME: to be improved.
00162     if (dim < 3)
00163       coord_ = v;
00164     else
00165     {
00166       unsigned j = 0;
00167       for (unsigned i = dim - 2; i < dim; ++i)
00168         coord_[i] = static_cast<C>(v[j++]);
00169       for (unsigned i = 2; i < dim; ++i, ++j)
00170         coord_[i-j] = static_cast<C>(v[j]);
00171     }
00172   }
00173 
00174   template <typename G, typename C>
00175   inline
00176   dpoint<G,C>::dpoint(C ind)
00177   {
00178     metal::bool_<(dim == 1)>::check();
00179     coord_[0] = ind;
00180   }
00181 
00182   template <typename G, typename C>
00183   inline
00184   dpoint<G,C>::dpoint(C row, C col)
00185   {
00186     metal::bool_<(dim == 2)>::check();
00187     coord_[0] = row;
00188     coord_[1] = col;
00189   }
00190 
00191   template <typename G, typename C>
00192   inline
00193   dpoint<G,C>::dpoint(C sli, C row, C col)
00194   {
00195     metal::bool_<(dim == 3)>::check();
00196     coord_[0] = sli;
00197     coord_[1] = row;
00198     coord_[2] = col;
00199   }
00200 
00201   template <typename G, typename C>
00202   inline
00203   dpoint<G,C>::dpoint(const literal::zero_t&)
00204   {
00205     coord_.set_all(0);
00206   }
00207 
00208   template <typename G, typename C>
00209   inline
00210   dpoint<G,C>&
00211   dpoint<G,C>::operator=(const literal::zero_t&)
00212   {
00213     coord_.set_all(0);
00214     return *this;
00215   }
00216 
00217   template <typename G, typename C>
00218   inline
00219   dpoint<G,C>::dpoint(const literal::one_t&)
00220   {
00221     metal::bool_<(dim == 1)>::check();
00222     coord_[0] = 1;
00223   }
00224 
00225   template <typename G, typename C>
00226   inline
00227   dpoint<G,C>&
00228   dpoint<G,C>::operator=(const literal::one_t&)
00229   {
00230     metal::bool_<(dim == 1)>::check();
00231     coord_[0] = 1;
00232     return *this;
00233   }
00234 
00235   template <typename G, typename C>
00236   template <typename F>
00237   inline
00238   dpoint<G,C>::dpoint(const Function_v2v<F>& f_)
00239   {
00240     mlc_converts_to(mln_result(F), C)::check();
00241     const F& f = exact(f_);
00242     for (unsigned i = 0; i < dim; ++i)
00243       coord_[i] = static_cast<C>(f(i));
00244   }
00245 
00246   template <typename G, typename C>
00247   inline
00248   void dpoint<G,C>::set_all(C c)
00249   {
00250     for (unsigned i = 0; i < dim; ++i)
00251       coord_[i] = c;
00252   }
00253 
00254   template <typename G, typename C>
00255   template <typename Q>
00256   inline
00257   dpoint<G,C>::operator mln::algebra::vec<dpoint<G,C>::dim, Q> () const
00258   {
00259     return to_vec();
00260   }
00261 
00262   template <typename G, typename C>
00263   inline
00264   typename dpoint<G,C>::vec
00265   dpoint<G,C>::to_vec() const
00266   {
00267     algebra::vec<G::dim, float> tmp;
00268 
00269     // FIXME: to be improved.
00270     if (dim == 1)
00271       tmp[0] = coord_[0];
00272     else
00273     {
00274       unsigned j = 0;
00275       for (unsigned i = dim - 2; i < dim; ++i)
00276         tmp[j++] = coord_[i];
00277       for (unsigned i = 2; i < dim; ++i, ++j)
00278         tmp[j] = coord_[i-j];
00279     }
00280 
00281     return tmp;
00282   }
00283 
00284 # endif // ! MLN_INCLUDE_ONLY
00285 
00286 } // end of namespace mln
00287 
00288 
00289 #endif // ! MLN_CORE_DPOINT_HH

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