26 #ifndef MLN_CORE_W_WINDOW_HH
27 # define MLN_CORE_W_WINDOW_HH
37 # include <mln/core/internal/weighted_window_base.hh>
38 # include <mln/core/concept/image.hh>
39 # include <mln/core/site_set/box.hh>
40 # include <mln/core/window.hh>
41 # include <mln/core/dpsites_piter.hh>
43 # include <mln/value/ops.hh>
44 # include <mln/util/ord.hh>
45 # include <mln/geom/bbox.hh>
46 # include <mln/literal/zero.hh>
47 # include <mln/convert/to.hh>
54 template <
typename D,
typename W>
struct w_window;
55 template <
typename It,
typename W>
struct with_w_;
64 template <
typename I,
typename D,
typename W>
66 from_to_(
const Image<I>& from, w_window<D,W>&
to);
68 template <
typename D,
typename W,
typename I>
70 from_to_(
const w_window<D,W>& from, Image<I>&
to);
72 template <
typename V,
unsigned S,
typename D,
typename W>
74 from_to_(
const V (&weight)[S], w_window<D,W>&
to);
84 template <
typename D,
typename W>
85 struct window_< mln::
w_window<D,W> > : window_< mln::window<D> >
99 template <
typename D,
typename W>
100 struct w_window :
public internal::weighted_window_base< mln::window<D>,
111 typedef with_w_< dpsites_fwd_piter< w_window<D, W> >, W >
fwd_qiter;
114 typedef with_w_< dpsites_bkd_piter< w_window<D, W> >, W >
bkd_qiter;
126 W
w(
unsigned i)
const;
129 const std::vector<W>&
weights()
const;
157 template <
typename D,
typename W>
158 std::ostream& operator<<(std::ostream& ostr, const w_window<D,W>& w_win);
163 template <
typename D,
typename Wl,
typename Wr>
169 template <
typename It,
typename W>
170 struct with_w_ :
public It
173 template <
typename Ds,
typename P>
174 with_w_(
const Ds& ds,
const P& p);
179 const std::vector<W> wei_;
184 # ifndef MLN_INCLUDE_ONLY
189 template <
typename It,
typename W>
190 template <
typename Ds,
typename P>
192 with_w_<It,W>::with_w_(
const Ds& ds,
199 template <
typename It,
typename W>
202 with_w_<It,W>::w()
const
204 mln_precondition(this->i_ < wei_.size());
205 return wei_[this->i_];
211 template <
typename D,
typename W>
217 template <
typename D,
typename W>
225 template <
typename D,
typename W>
227 const std::vector<D>&
230 return win_.std_vector();
233 template <
typename D,
typename W>
235 const std::vector<W>&
241 template <
typename D,
typename W>
246 mln_precondition(i < wei_.size());
247 mln_invariant(wei_.size() == win_.size());
251 template <
typename D,
typename W>
256 mln_invariant(wei_.size() == win_.size());
257 mln_precondition(! win_.has(d));
264 std::map<D, W, util::ord<D> > memo_wei_;
265 for (
unsigned i = 0; i < win_.size(); ++i)
266 memo_wei_[win_.dp(i)] = wei_[i];
273 wei_.resize(win_.size());
274 for (
unsigned i = 0; i < win_.size(); ++i)
275 wei_[i] = memo_wei_[win_.dp(i)];
277 mln_invariant(wei_.size() == win_.size());
281 template <
typename D,
typename W>
286 if (! win_.is_symmetric())
293 template <
typename D,
typename W>
299 unsigned n = this->size();
300 for (
unsigned i = 0; i < n; ++i)
301 tmp.
insert(this->w(i), - this->dp(i));
305 template <
typename D,
typename W>
323 template <
typename I,
typename D,
typename W>
327 mlc_converts_to(mln_deduce(I, psite, delta), D)::check();
328 mlc_converts_to(mln_value(I), W)::check();
329 const I& ima = exact(from_);
331 mln_value(I) zero = literal::zero;
332 mln_piter(I) p(ima.domain());
335 to.insert(ima(p), convert::to<D>(p));
338 template <typename D, typename W, typename I>
342 typedef mln_site(I) P;
343 mlc_converts_to(D, mln_delta(P))::check();
344 mlc_converts_to(W, mln_value(I))::check();
346 I& ima = exact(ima_);
347 mln_precondition(! ima.is_valid());
348 mln_precondition(w_win.is_valid());
350 ima.init_(geom::bbox(w_win));
353 mln_value(I) zero = literal::zero;
354 mln_piter(I) p(ima.domain());
359 unsigned n = w_win.size();
360 for (
unsigned i = 0; i < n; ++i)
361 ima(convert::to<P>(w_win.dp(i))) = w_win.w(i);
387 template <typename V,
unsigned S, typename D, typename W>
389 from_to_(const V (&weight)[S],
w_window<D,W>& to)
391 mlc_bool(S != 0)::check();
392 mlc_converts_to(V, W)::check();
394 s = mlc_root(d,S)::value / 2 };
395 metal::bool_<(mlc_pow_int(2 * s + 1, d) == S)>::check();
397 typedef mln_site(D) P;
398 box<P> b(all_to(-s), all_to(+s));
399 mln_fwd_piter(
box<P>) p(b);
401 V zero = literal::zero;
404 if (weight[i] != zero)
405 to.insert(weight[i], convert::to<D>(p));
418 template <
typename D,
typename W>
420 std::ostream& operator<<(std::ostream& ostr, const w_window<D,W>& w_win)
423 for (
unsigned i = 0; i < w_win.win().size(); ++i)
424 ostr << w_win.dp(i) <<
':' << w_win.w(i) <<
' ';
428 template <
typename D,
typename Wl,
typename Wr>
432 if (lhs.size() != rhs.size())
434 if (lhs.
win() != rhs.
win())
436 const std::vector<Wl>& wl = lhs.
weights();
437 const std::vector<Wr>& wr = rhs.
weights();
438 mln_assertion(wl.size() == wr.size());
439 for (
unsigned i = 0; i < wl.size(); ++i)
445 # endif // ! MLN_INCLUDE_ONLY
450 # include <mln/make/w_window.hh>
453 #endif // ! MLN_CORE_W_WINDOW_HH