$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
gpoint.hh
1 // Copyright (C) 2007, 2008, 2009, 2011, 2012, 2013 EPITA Research and
2 // Development Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_CORE_CONCEPT_GPOINT_HH
28 # define MLN_CORE_CONCEPT_GPOINT_HH
29 
33 
34 
35 # include <mln/core/concept/site.hh>
36 # include <mln/core/concept/gdpoint.hh>
37 # include <mln/value/scalar.hh>
38 # include <mln/algebra/vec.hh>
39 # include <mln/util/ord.hh>
40 # include <mln/debug/format.hh>
41 
42 
43 namespace mln
44 {
45 
46  // Forward declaration.
47  template <typename E> struct Gpoint;
48 
49 
51  namespace trait
52  {
53 
54  template < typename P, typename D >
55  struct set_binary_< op::plus,
56  mln::Gpoint, P, mln::Gdpoint, D >
57  {
58  typedef P ret;
59  };
60 
61  template < typename P, typename D >
62  struct set_binary_< op::minus,
63  mln::Gpoint, P, mln::Gdpoint, D >
64  {
65  typedef P ret;
66  };
67 
68  template < typename L, typename R >
69  struct set_binary_< op::minus,
70  mln::Gpoint, L, mln::Gpoint, R >
71  {
72  typedef mln_delta(L) ret;
73  };
74 
75  template < typename L, typename R >
76  struct set_binary_< op::times,
77  mln::Gpoint, L,
78  mln::Object, mln::value::scalar_<R> >
79  {
80  typedef L ret;
81  };
82 
83  template < typename L, typename R >
84  struct set_binary_< op::div,
85  mln::Gpoint, L,
87  {
88  typedef L ret;
89  };
90 
91  template <typename P>
92  struct set_unary_< op::ord, mln::Gpoint, P >
93  {
95  };
96 
97  } // end of namespace mln::trait
99 
100 
103  template <>
104  struct Gpoint<void>
105  {
106  typedef Site<void> super;
107  };
109 
110 
121  template <typename E>
122  struct Gpoint : public Site<E>
123  {
125 
126  /*
127  typedef grid;
128  typedef delta;
129  typedef vec;
130  const vec& to_vec() const
131  */
132  protected:
133  Gpoint();
134  };
135 
136 
139 
151  template <typename L, typename R>
152  bool operator==(const Gpoint<L>& lhs, const Gpoint<R>& rhs);
153 
154 
157 
175  template <typename L, typename R>
176  mln_delta(L)
177  operator-(const Gpoint<L>& lhs, const Gpoint<R>& rhs);
178 
179 
182 
192  template <typename P, typename D>
193  P
194  operator+(const Gpoint<P>& p, const Gdpoint<D>& dp);
195 
196 
197 
200 
210  template <typename P, typename D>
211  P
212  operator-(const Gpoint<P>& p, const Gdpoint<D>& dp);
213 
214 
216  //
217  template <typename P, typename S>
218  P
219  operator*(const Gpoint<P>& p, const value::scalar_<S>& s);
220 
221 
223 
230  template <typename P>
231  std::ostream& operator<<(std::ostream& ostr, const Gpoint<P>& p);
232 
233 
235 
244  template <typename P, typename D>
245  P& operator+=(Gpoint<P>& p, const Gdpoint<D>& dp);
246 
247 
249 
258  template <typename P, typename D>
259  P& operator-=(Gpoint<P>& p, const Gdpoint<D>& dp);
260 
262 
268  template <typename P, typename D>
269  P operator/(const Gpoint<P>& p, const value::scalar_<D>& dp);
270 
271  // FIXME : add operators and traits?
272 
273 
275  template <typename P>
276  void
277  from_to_(const Gpoint<P>& from, mln_delta(P)& to);
278 
280  template <typename P, unsigned n, typename T>
281  void
282  from_to_(const Gpoint<P>& from, algebra::vec<n,T>& to);
283 
284 
285 
286 # ifndef MLN_INCLUDE_ONLY
287 
288 
289  // Gpoint
290 
291  template <typename E>
292  inline
294  {
295  typedef mln_grid(E) grid;
296  typedef mln_delta(E) delta;
297  typedef mln_vec(E) vec;
298  vec (E::*m)() const = & E::to_vec;
299  (void) m;
300  }
301 
302  // Operators.
303 
304  template <typename L, typename R>
305  inline
306  bool operator==(const Gpoint<L>& lhs, const Gpoint<R>& rhs)
307  {
308  mlc_equal(mln_grid(L), mln_grid(R))::check();
309  return exact(lhs).to_vec() == exact(rhs).to_vec();
310  }
311 
312  template <typename L, typename R>
313  inline
314  mln_delta(L) // FIXME: promote!
315  operator-(const Gpoint<L>& lhs, const Gpoint<R>& rhs)
316  {
317  mlc_equal(mln_grid(L), mln_grid(R))::check();
318  mln_delta(L) tmp = exact(lhs).to_vec() - exact(rhs).to_vec();
319  mln_postcondition(rhs + tmp == lhs);
320  return tmp;
321  }
322 
323  template <typename P, typename D>
324  inline
325  P // FIXME: promote!
326  operator+(const Gpoint<P>& p, const Gdpoint<D>& dp)
327  {
328  mlc_equal(mln_grid(P), mln_grid(D))::check();
329  P tmp = exact(p).to_vec() + exact(dp).to_vec();
330  return tmp;
331  }
332 
333  template <typename P, typename D>
334  inline
335  P // FIXME: promote!
336  operator-(const Gpoint<P>& p, const Gdpoint<D>& dp)
337  {
338  mlc_equal(mln_grid(P), mln_grid(D))::check();
339  P tmp = exact(p).to_vec() - exact(dp).to_vec();
340  return tmp;
341  }
342 
343  template <typename P, typename S>
344  inline
345  P
346  operator*(const Gpoint<P>& p, const value::scalar_<S>& s)
347  {
348  S s_ = s.to_equiv();
349  const unsigned n = P::dim;
350  P tmp = exact(p);
351  for (unsigned i = 0; i < n; ++i)
352  tmp[i] *= s_;
353  return tmp;
354  }
355 
356  template <typename P>
357  inline
358  std::ostream& operator<<(std::ostream& ostr, const Gpoint<P>& p)
359  {
360  enum { n = P::dim };
361  ostr << '(';
362  for (unsigned i = 0; i < n; ++i)
363  ostr << debug::format(exact(p)[i]) << (i == n - 1 ? ')' : ',');
364  return ostr;
365  }
366 
367  template <typename P, typename D>
368  inline
369  P& operator+=(Gpoint<P>& p, const Gdpoint<D>& dp)
370  {
371  mlc_equal(mln_grid(P), mln_grid(D))::check();
372  return exact(p) = p + dp;
373  }
374 
375  template <typename P, typename D>
376  inline
377  P& operator-=(Gpoint<P>& p, const Gdpoint<D>& dp)
378  {
379  mlc_equal(mln_grid(P), mln_grid(D))::check();
380  return exact(p) = p - dp;
381  }
382 
383 
384  template <typename P, typename S>
385  inline
386  P
387  operator/(const Gpoint<P>& p, const value::scalar_<S>& s_)
388  {
389  S s = s_.to_equiv();
390  const unsigned n = P::dim;
391  P tmp = exact(p);
392  for (unsigned i = 0; i < n; ++i)
393  tmp[i] /= s;
394  return tmp;
395  }
396 
397 
398 
399  // Conversions
400 
401  template <typename P>
402  inline
403  void
404  from_to_(const Gpoint<P>& p_, mln_delta(P)& dp)
405  {
406  // Instead of "dp.to_vec() = exact(p).to_vec();" that
407  // does not compile (cause to_vec returns const), we
408  // have:
409  enum { n = P::dim };
410  const P& p = exact(p_);
411  for (unsigned i = 0; i < n; ++i)
412  dp[i] = p[i];
413  }
414 
415  template <typename P, unsigned n, typename T>
416  inline
417  void
418  from_to_(const Gpoint<P>& from_, algebra::vec<n,T>& to)
419  {
420  mlc_bool(n == P::dim)::check();
421  const P& from = exact(from_);
422  for (unsigned i = 0; i < n; ++i)
423  to[i] = static_cast< T >(from[i]); // FIXME: cast -> best effort...
424  }
425 
426 
427 # endif // ! MLN_INCLUDE_ONLY
428 
429 } // end of namespace mln
430 
431 
432 
433 #endif // ! MLN_CORE_CONCEPT_GPOINT_HH