00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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 }
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
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 }
00177
00178 }
00179
00180
00181 #endif // ! MLN_ACCU_CENTER_HH