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

center.hh

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

Generated on Fri Sep 16 2011 16:33:24 for Milena (Olena) by  doxygen 1.7.1