27 #ifndef MLN_CORE_W_WINDOW_HH
28 # define MLN_CORE_W_WINDOW_HH
38 # include <mln/core/internal/weighted_window_base.hh>
39 # include <mln/core/concept/image.hh>
40 # include <mln/core/site_set/box.hh>
41 # include <mln/core/window.hh>
42 # include <mln/core/dpsites_piter.hh>
44 # include <mln/value/ops.hh>
45 # include <mln/util/ord.hh>
46 # include <mln/geom/bbox.hh>
47 # include <mln/literal/zero.hh>
48 # include <mln/convert/to.hh>
55 template <
typename D,
typename W>
struct w_window;
56 template <
typename It,
typename W>
struct with_w_;
62 template <
typename D,
typename W>
63 struct window_< mln::
w_window<D,W> > : window_< mln::window<D> >
77 template <
typename D,
typename W>
104 W
w(
unsigned i)
const;
107 const std::vector<W>&
weights()
const;
135 template <
typename D,
typename W>
136 std::ostream& operator<<(std::ostream& ostr, const w_window<D,W>& w_win);
141 template <
typename D,
typename Wl,
typename Wr>
146 template <
typename D,
typename W,
typename I>
150 template <
typename V,
unsigned S,
typename D,
typename W>
157 template <
typename It,
typename W>
161 template <
typename Ds,
typename P>
172 # ifndef MLN_INCLUDE_ONLY
177 template <
typename It,
typename W>
178 template <
typename Ds,
typename P>
187 template <
typename It,
typename W>
192 mln_precondition(this->i_ < wei_.size());
193 return wei_[this->i_];
199 template <
typename D,
typename W>
205 template <
typename D,
typename W>
213 template <
typename D,
typename W>
215 const std::vector<D>&
218 return win_.std_vector();
221 template <
typename D,
typename W>
223 const std::vector<W>&
229 template <
typename D,
typename W>
234 mln_precondition(i < wei_.size());
235 mln_invariant(wei_.size() == win_.size());
239 template <
typename D,
typename W>
244 mln_invariant(wei_.size() == win_.size());
245 mln_precondition(! win_.has(d));
252 std::map<D, W, util::ord<D> > memo_wei_;
253 for (
unsigned i = 0; i < win_.size(); ++i)
254 memo_wei_[win_.dp(i)] = wei_[i];
261 wei_.resize(win_.size());
262 for (
unsigned i = 0; i < win_.size(); ++i)
263 wei_[i] = memo_wei_[win_.dp(i)];
265 mln_invariant(wei_.size() == win_.size());
269 template <
typename D,
typename W>
274 if (! win_.is_symmetric())
281 template <
typename D,
typename W>
287 unsigned n = this->size();
288 for (
unsigned i = 0; i < n; ++i)
289 tmp.insert(this->w(i), - this->
dp(i));
293 template <
typename D,
typename W>
305 template <
typename D,
typename W>
307 std::ostream& operator<<(std::ostream& ostr, const w_window<D,W>& w_win)
310 for (
unsigned i = 0; i < w_win.win().size(); ++i)
311 ostr << w_win.dp(i) <<
':' << w_win.w(i) <<
' ';
315 template <
typename D,
typename Wl,
typename Wr>
317 bool operator==(
const w_window<D,Wl>& lhs,
const w_window<D,Wr>&
rhs)
319 if (lhs.size() != rhs.size())
321 if (lhs.win() != rhs.win())
323 const std::vector<Wl>& wl = lhs.weights();
324 const std::vector<Wr>& wr = rhs.weights();
325 mln_assertion(wl.size() == wr.size());
326 for (
unsigned i = 0; i < wl.size(); ++i)
335 template <
typename D,
typename W,
typename I>
337 from_to_(
const w_window<D,W>& w_win, Image<I>& ima_)
339 typedef mln_site(I) P;
340 mlc_converts_to(D, mln_delta(P))::check();
341 mlc_converts_to(W, mln_value(I))::check();
343 I& ima =
exact(ima_);
344 mln_precondition(! ima.is_valid());
345 mln_precondition(w_win.is_valid());
350 mln_value(I) zero = literal::zero;
351 mln_piter(I)
p(ima.domain());
356 unsigned n = w_win.size();
357 for (
unsigned i = 0; i < n; ++i)
358 ima(
convert::to<P>(w_win.dp(i))) = w_win.w(i);
361 template <typename V,
unsigned S, typename D, typename W>
363 from_to_(const V (&weight)[S], w_window<D,W>& to)
365 mlc_bool(S != 0)::check();
366 mlc_converts_to(V, W)::check();
368 s = mlc_root(d,S)::
value / 2 };
369 metal::bool_<(mlc_pow_int(2 * s + 1, d) == S)>::check();
371 typedef mln_site(D) P;
373 mln_fwd_piter(
box<P>)
p(b);
375 V zero = literal::zero;
378 if (weight[i] != zero)
379 to.insert(weight[i], convert::to<D>(p));
385 # endif // ! MLN_INCLUDE_ONLY
390 # include <mln/make/w_window.hh>
393 #endif // ! MLN_CORE_W_WINDOW_HH