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

center.hh

00001 // Copyright (C) 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_ACCU_CENTER_HH
00027 # define MLN_ACCU_CENTER_HH
00028 
00034 
00035 # include <mln/accu/internal/base.hh>
00036 # include <mln/accu/shape/bbox.hh>
00037 
00038 namespace mln
00039 {
00040 
00041   namespace accu
00042   {
00043 
00044 
00052     //
00053     template <typename P, typename V = typename P::vec>
00054     struct center
00055       : public mln::accu::internal::base<V, center<P,V> >
00056     {
00057       typedef P argument;
00058       typedef V result;
00059 
00060       center();
00061 
00064       void init();
00065       void take(const argument& t);
00066       void take(const center<P,V>& other);
00068 
00070       V to_result() const;
00071       operator P() const;
00072 
00074       bool is_valid() const;
00075 
00076     protected:
00077       algebra::vec<P::dim, mln_sum(mln_coord(P))> center_;
00078       unsigned nsites_;
00079     };
00080 
00081     namespace meta
00082     {
00083 
00085       struct center : public Meta_Accumulator< center >
00086       {
00087 
00088         template <typename P>
00089         struct with
00090         {
00091           typedef accu::center<P> ret;
00092         };
00093 
00094       };
00095 
00096     } // end of namespace mln::accu::meta
00097 
00098 
00099 
00100 # ifndef MLN_INCLUDE_ONLY
00101 
00102     template <typename P, typename V>
00103     inline
00104     center<P,V>::center()
00105     {
00106       init();
00107     }
00108 
00109     template <typename P, typename V>
00110     inline
00111     void
00112     center<P,V>::init()
00113     {
00114       center_ = literal::zero;
00115       nsites_ = 0;
00116     }
00117 
00118     template <typename P, typename V>
00119     inline
00120     void center<P,V>::take(const argument& t)
00121     {
00122       center_ += t.to_vec();
00123       ++nsites_;
00124     }
00125 
00126     template <typename P, typename V>
00127     inline
00128     void
00129     center<P,V>::take(const center<P,V>& other)
00130     {
00131       center_ += other.center_;
00132       nsites_ += other.nsites_;
00133     }
00134 
00135     template <typename P, typename V>
00136     inline
00137     V
00138     center<P,V>::to_result() const
00139     {
00140       // mln_precondition(is_valid());
00141       if (! is_valid())
00142         return V();
00143       return center_ / nsites_;
00144     }
00145 
00146     template <typename P, typename V>
00147     inline
00148     center<P,V>::operator P() const
00149     {
00150       return P(to_result());
00151     }
00152 
00153     template <typename P, typename V>
00154     inline
00155     bool
00156     center<P,V>::is_valid() const
00157     {
00158       return nsites_ > 0;
00159     }
00160 
00161 # endif // ! MLN_INCLUDE_ONLY
00162 
00163 
00164   } // end of namespace mln::accu
00165 
00166 } // end of namespace mln
00167 
00168 
00169 #endif // ! MLN_ACCU_CENTER_HH

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