Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
arith/div.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_ARITH_DIV_HH
27 # define MLN_ARITH_DIV_HH
28 
34 
35 # include <mln/arith/includes.hh>
36 
37 // Specializations are in:
38 # include <mln/arith/div.spe.hh>
39 
40 
41 namespace mln
42 {
43 
44 
45  namespace trait
46  {
47 
48  template <typename L, typename R>
49  struct set_binary_< op::div, Image, L, Image, R >
50  {
51  typedef mln_trait_op_div(mln_value(L), mln_value(R)) value;
52  typedef mln_ch_value(L, value) ret;
53  };
54 
55  template <typename I, typename S>
56  struct set_binary_< op::div, Image, I, mln::value::Scalar, S >
57  {
58  typedef mln_trait_op_div(mln_value(I), S) value;
59  typedef mln_ch_value(I, value) ret;
60  };
61 
62  } // end of namespace mln::trait
63 
64 
65 
66  template <typename L, typename R>
67  mln_trait_op_div(L,R)
68  operator/(const Image<L>& lhs, const Image<R>& rhs);
69 
70  template <typename L, typename R>
71  L&
72  operator/=(Image<L>& lhs, const Image<R>& rhs);
73 
74 
75  template <typename I, typename S>
76  mln_trait_op_div(I,S)
77  operator/(const Image<I>& ima, const value::Scalar<S>& s);
78 
79  template <typename I, typename S>
80  I&
81  operator/=(Image<I>& ima, const value::Scalar<S>& s);
82 
83 
84 
85  namespace arith
86  {
87 
89 
96  template <typename L, typename R, typename O>
97  void div(const Image<L>& lhs, const Image<R>& rhs, Image<O>& output);
98 
99 
101 
108  template <typename I, typename V, typename O>
109  void div_cst(const Image<I>& input, const V& val, Image<O>& output);
110 
111 
113 
123  template <typename L, typename R>
124  void div_inplace(Image<L>& lhs, const Image<R>& rhs);
125 
126 
127  } // end of namespace mln::arith
128 
129 
130 
131 
132 # ifndef MLN_INCLUDE_ONLY
133 
134 
135  template <typename L, typename R>
136  inline
137  mln_trait_op_div(L,R)
138  operator/(const Image<L>& lhs, const Image<R>& rhs)
139  {
140  mln_precondition(exact(rhs).domain() == exact(lhs).domain());
141  mln_trait_op_div(L,R) tmp;
142  initialize(tmp, lhs);
143  arith::div(lhs, rhs, tmp);
144  return tmp;
145  }
146 
147  template <typename L, typename R>
148  inline
149  L&
150  operator/=(Image<L>& lhs, const Image<R>& rhs)
151  {
152  mln_precondition(exact(rhs).domain() == exact(lhs).domain());
153  arith::div_inplace(lhs, rhs);
154  return exact(lhs);
155  }
156 
157 
158  template <typename I, typename S>
159  inline
160  mln_trait_op_div(I,S)
161  operator/(const Image<I>& ima, const value::Scalar<S>& s)
162  {
163  mln_precondition(exact(ima).is_valid());
164  mln_precondition(s != 0);
165  mln_trait_op_div(I,S) tmp;
166  initialize(tmp, ima);
167  arith::div_cst(ima, exact(s), tmp);
168  return tmp;
169  }
170 
171  template <typename I, typename S>
172  inline
173  I&
174  operator/=(Image<I>& ima, const value::Scalar<S>& s)
175  {
176  mln_precondition(exact(ima).is_valid());
177  arith::div_cst(ima, exact(s), ima);
178  return exact(ima);
179  }
180 
181 
182 
183  namespace arith
184  {
185 
186  namespace impl
187  {
188 
189  namespace generic
190  {
191 
192  template <typename L, typename R, typename O>
193  inline
194  void div_(const L& lhs, const R& rhs, O& output)
195  {
196  trace::entering("arith::impl::generic::div_");
197 
198  mln_piter(L) p(lhs.domain());
199  for_all(p)
200  output(p) = lhs(p) / rhs(p);
201 
202  trace::exiting("arith::impl::generic::div_");
203  }
204 
205  template <typename L, typename R>
206  inline
207  void div_inplace_(L& lhs, const R& rhs)
208  {
209  trace::entering("arith::impl::generic::div_inplace_");
210 
211  mln_piter(R) p(rhs.domain());
212  for_all(p)
213  lhs(p) /= rhs(p);
214 
215  trace::exiting("arith::impl::generic::div_inplace_");
216  }
217 
218  } // end of namespace mln::arith::impl::generic
219 
220  } // end of namespace mln::arith::impl
221 
222 
223  // Facades.
224 
225  template <typename L, typename R, typename O>
226  inline
227  void div(const Image<L>& lhs, const Image<R>& rhs, Image<O>& output)
228  {
229  trace::entering("arith::div");
230 
231  mln_precondition(exact(rhs).domain() == exact(lhs).domain());
232  mln_precondition(exact(output).domain() == exact(lhs).domain());
233  impl::div_(mln_trait_image_speed(L)(), exact(lhs),
234  mln_trait_image_speed(R)(), exact(rhs),
235  mln_trait_image_speed(O)(), exact(output));
236 
237  trace::exiting("arith::div");
238  }
239 
240  template <typename I, typename V, typename O>
241  inline
242  void div_cst(const Image<I>& input, const V& val, Image<O>& output)
243  {
244  trace::entering("arith::div_cst");
245 
246  mln_precondition(exact(output).domain() == exact(input).domain());
247  div(input, pw::cst(val) | exact(input).domain(), output);
248  // Calls the previous version.
249 
250  trace::exiting("arith::div_cst");
251  }
252 
253  template <typename L, typename R>
254  inline
255  void div_inplace(Image<L>& lhs, const Image<R>& rhs)
256  {
257  trace::entering("arith::div_inplace");
258 
259  mln_precondition(exact(rhs).domain() <= exact(lhs).domain());
260  impl::div_inplace_(mln_trait_image_speed(L)(), exact(lhs),
261  mln_trait_image_speed(R)(), exact(rhs));
262 
263  trace::exiting("arith::div_inplace");
264  }
265 
266  template <typename I, typename V>
267  inline
268  void div_cst_inplace(Image<I>& input, const V& val)
269  {
270  trace::entering("arith::div_cst_inplace");
271 
272  mln_precondition(exact(input).is_valid());
273  div_inplace(input, pw::cst(val) | exact(input).domain());
274  // Calls the previous version.
275 
276  trace::exiting("arith::div_cst_inplace");
277  }
278 
279  } // end of namespace mln::arith
280 
281 # endif // ! MLN_INCLUDE_ONLY
282 
283 } // end of namespace mln
284 
285 
286 #endif // ! MLN_ARITH_DIV_HH