Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
gpoint.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_CORE_CONCEPT_GPOINT_HH
27 # define MLN_CORE_CONCEPT_GPOINT_HH
28 
32 
33 
34 # include <mln/core/concept/site.hh>
35 # include <mln/core/concept/gdpoint.hh>
36 # include <mln/value/scalar.hh>
37 # include <mln/algebra/vec.hh>
38 # include <mln/util/ord.hh>
39 # include <mln/debug/format.hh>
40 
41 
42 namespace mln
43 {
44 
45  // Forward declaration.
46  template <typename E> struct Gpoint;
47 
48 
49 
50  namespace trait
51  {
52 
53  template < typename P, typename D >
54  struct set_binary_< op::plus,
55  mln::Gpoint, P, mln::Gdpoint, D >
56  {
57  typedef P ret;
58  };
59 
60  template < typename P, typename D >
61  struct set_binary_< op::minus,
62  mln::Gpoint, P, mln::Gdpoint, D >
63  {
64  typedef P ret;
65  };
66 
67  template < typename L, typename R >
68  struct set_binary_< op::minus,
69  mln::Gpoint, L, mln::Gpoint, R >
70  {
71  typedef mln_delta(L) ret;
72  };
73 
74  template < typename L, typename R >
75  struct set_binary_< op::times,
76  mln::Gpoint, L,
77  mln::Object, mln::value::scalar_<R> >
78  {
79  typedef L ret;
80  };
81 
82  template < typename L, typename R >
83  struct set_binary_< op::div,
84  mln::Gpoint, L,
85  mln::Object, mln::value::scalar_<R> >
86  {
87  typedef L ret;
88  };
89 
90  template <typename P>
91  struct set_unary_< op::ord, mln::Gpoint, P >
92  {
93  typedef mln::internal::ord_vec< P > ret;
94  };
95 
96  } // end of namespace mln::trait
97 
98 
99 
100  // Gpoint category flag type.
101  template <>
102  struct Gpoint<void>
103  {
104  typedef Site<void> super;
105  };
106 
107 
114  template <typename E>
115  struct Gpoint : public Site<E>
116  {
117  typedef Gpoint<void> category;
118 
119  /*
120  typedef grid;
121  typedef delta;
122  typedef vec;
123  const vec& to_vec() const
124  */
125  protected:
126  Gpoint();
127  };
128 
129 
130  namespace convert
131  {
132 
133  namespace over_load
134  {
135 
136  template <typename P>
137  void
138  from_to_(const Gpoint<P>& from, mln_delta(P)& to);
139 
140  template <typename P, unsigned n, typename T>
141  void
142  from_to_(const Gpoint<P>& from, algebra::vec<n,T>& to);
143 
144  template <unsigned n, typename T, typename P>
145  void
146  from_to_(const algebra::vec<n,T>& from, Gpoint<P>& to);
147 
148  } // end of namespace mln::convert::over_load
149 
150  } // end of namespace mln::convert
151 
152 
153 
156 
168  template <typename L, typename R>
169  bool operator==(const Gpoint<L>& lhs, const Gpoint<R>& rhs);
170 
171 
174 
192  template <typename L, typename R>
193  mln_delta(L)
194  operator-(const Gpoint<L>& lhs, const Gpoint<R>& rhs);
195 
196 
199 
209  template <typename P, typename D>
210  P
211  operator+(const Gpoint<P>& p, const Gdpoint<D>& dp);
212 
213 
214 
217 
227  template <typename P, typename D>
228  P
229  operator-(const Gpoint<P>& p, const Gdpoint<D>& dp);
230 
231 
233  //
234  template <typename P, typename S>
235  P
236  operator*(const Gpoint<P>& p, const value::scalar_<S>& s);
237 
238 
240 
247  template <typename P>
248  std::ostream& operator<<(std::ostream& ostr, const Gpoint<P>& p);
249 
250 
252 
261  template <typename P, typename D>
262  P& operator+=(Gpoint<P>& p, const Gdpoint<D>& dp);
263 
264 
266 
275  template <typename P, typename D>
276  P& operator-=(Gpoint<P>& p, const Gdpoint<D>& dp);
277 
279 
285  template <typename P, typename D>
286  P operator/(const Gpoint<P>& p, const value::scalar_<D>& dp);
287 
288  // FIXME : add operators and traits?
289 
290 
291 
292 # ifndef MLN_INCLUDE_ONLY
293 
294 
295  // Gpoint
296 
297  template <typename E>
298  inline
299  Gpoint<E>::Gpoint()
300  {
301  typedef mln_grid(E) grid;
302  typedef mln_delta(E) delta;
303  typedef mln_vec(E) vec;
304  vec (E::*m)() const = & E::to_vec;
305  m = 0;
306  }
307 
308 
309  // convert::from_to_
310 
311  namespace convert
312  {
313 
314  namespace over_load
315  {
316 
317  // Gpoint -> delta
318  template <typename P>
319  inline
320  void
321  from_to_(const Gpoint<P>& p_, mln_delta(P)& dp)
322  {
323  // Instead of "dp.to_vec() = exact(p).to_vec();" that
324  // does not compile (cause to_vec returns const), we
325  // have:
326  enum { n = P::dim };
327  const P& p = exact(p_);
328  for (unsigned i = 0; i < n; ++i)
329  dp[i] = p[i];
330  }
331 
332  // Gpoint -> algebra::vec.
333  template <typename P, unsigned n, typename T>
334  inline
335  void
336  from_to_(const Gpoint<P>& from_, algebra::vec<n,T>& to)
337  {
338  mlc_bool(n == P::dim)::check();
339  const P& from = exact(from_);
340  for (unsigned i = 0; i < n; ++i)
341  to[i] = static_cast< T >(from[i]); // FIXME: cast -> best effort...
342  }
343 
344  // algebra::vec -> Gpoint.
345  template <unsigned n, typename T, typename P>
346  inline
347  void
348  from_to_(const algebra::vec<n,T>& from, Gpoint<P>& to_)
349  {
350  mlc_bool(P::dim == n)::check();
351  P& to = exact(to_);
352  for (unsigned i = 0; i < n; ++i)
353  to[i] = static_cast< typename P::coord >(from[i]); // FIXME: cast -> best effort...
354  }
355 
356  } // end of namespace mln::convert::over_load
357 
358  } // end of namespace mln::convert
359 
360 
361  // Operators.
362 
363  template <typename L, typename R>
364  inline
365  bool operator==(const Gpoint<L>& lhs, const Gpoint<R>& rhs)
366  {
367  mlc_equal(mln_grid(L), mln_grid(R))::check();
368  return exact(lhs).to_vec() == exact(rhs).to_vec();
369  }
370 
371  template <typename L, typename R>
372  inline
373  mln_delta(L) // FIXME: promote!
374  operator-(const Gpoint<L>& lhs, const Gpoint<R>& rhs)
375  {
376  mlc_equal(mln_grid(L), mln_grid(R))::check();
377  mln_delta(L) tmp = exact(lhs).to_vec() - exact(rhs).to_vec();
378  mln_postcondition(rhs + tmp == lhs);
379  return tmp;
380  }
381 
382  template <typename P, typename D>
383  inline
384  P // FIXME: promote!
385  operator+(const Gpoint<P>& p, const Gdpoint<D>& dp)
386  {
387  mlc_equal(mln_grid(P), mln_grid(D))::check();
388  P tmp = exact(p).to_vec() + exact(dp).to_vec();
389  return tmp;
390  }
391 
392  template <typename P, typename D>
393  inline
394  P // FIXME: promote!
395  operator-(const Gpoint<P>& p, const Gdpoint<D>& dp)
396  {
397  mlc_equal(mln_grid(P), mln_grid(D))::check();
398  P tmp = exact(p).to_vec() - exact(dp).to_vec();
399  return tmp;
400  }
401 
402  template <typename P, typename S>
403  inline
404  P
405  operator*(const Gpoint<P>& p, const value::scalar_<S>& s)
406  {
407  S s_ = s.to_equiv();
408  const unsigned n = P::dim;
409  P tmp = exact(p);
410  for (unsigned i = 0; i < n; ++i)
411  tmp[i] *= s_;
412  return tmp;
413  }
414 
415  template <typename P>
416  inline
417  std::ostream& operator<<(std::ostream& ostr, const Gpoint<P>& p)
418  {
419  enum { n = P::dim };
420  ostr << '(';
421  for (unsigned i = 0; i < n; ++i)
422  ostr << debug::format(exact(p)[i]) << (i == n - 1 ? ')' : ',');
423  return ostr;
424  }
425 
426  template <typename P, typename D>
427  inline
428  P& operator+=(Gpoint<P>& p, const Gdpoint<D>& dp)
429  {
430  mlc_equal(mln_grid(P), mln_grid(D))::check();
431  return exact(p) = p + dp;
432  }
433 
434  template <typename P, typename D>
435  inline
436  P& operator-=(Gpoint<P>& p, const Gdpoint<D>& dp)
437  {
438  mlc_equal(mln_grid(P), mln_grid(D))::check();
439  return exact(p) = p - dp;
440  }
441 
442 
443  template <typename P, typename S>
444  inline
445  P
446  operator/(const Gpoint<P>& p, const value::scalar_<S>& s_)
447  {
448  S s = s_.to_equiv();
449  const unsigned n = P::dim;
450  P tmp = exact(p);
451  for (unsigned i = 0; i < n; ++i)
452  tmp[i] /= s;
453  return tmp;
454  }
455 
456 # endif // ! MLN_INCLUDE_ONLY
457 
458 } // end of namespace mln
459 
460 
461 
462 #endif // ! MLN_CORE_CONCEPT_GPOINT_HH