Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
gdpoint.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_GDPOINT_HH
27 # define MLN_CORE_CONCEPT_GDPOINT_HH
28 
32 
33 # include <mln/core/concept/object.hh>
34 # include <mln/core/grids.hh>
35 # include <mln/trait/all.hh>
36 # include <mln/value/scalar.hh>
37 # include <mln/debug/format.hh>
38 
39 
40 namespace mln
41 {
42 
43  // Forward declaration.
44  template <typename E> struct Gdpoint;
45 
46 
47 
48  namespace trait
49  {
50 
51  // FIXME: Add promotion.
52 
53  template < typename L, typename R >
54  struct set_binary_< op::plus,
55  mln::Gdpoint, L, mln::Gdpoint, R >
56  {
57  typedef L ret;
58  };
59 
60  template < typename L, typename R >
61  struct set_binary_< op::minus,
62  mln::Gdpoint, L, mln::Gdpoint, R >
63  {
64  typedef L ret;
65  };
66 
67  template < typename D, typename S >
68  struct set_binary_< op::times,
69  mln::Gdpoint, D,
70  mln::value::Scalar, S >
71  {
72  typedef D ret;
73  };
74 
75  template <typename D>
76  struct set_unary_< op::ord, mln::Gdpoint, D >
77  {
78  typedef mln::internal::ord_vec< D > ret;
79  };
80 
81  } // end of namespace mln::trait
82 
83 
84 
86  template <>
87  struct Gdpoint<void>
88  {
89  typedef Object<void> super;
90  };
91 
92 
94  template <typename E>
95  struct Gdpoint : public Object<E>
96  {
97  typedef Gdpoint<void> category;
98 
99  /*
100  typedef grid;
101  typedef vec;
102  const vec& to_vec() const;
103  */
104 
105  protected:
106  Gdpoint();
107  };
108 
109 
110 
111  // Operators.
112 
113  template <typename D>
114  std::ostream&
115  operator<<(std::ostream& ostr, const Gdpoint<D>& dp);
116 
117 
118  template <typename L, typename R>
119  bool
120  operator==(const Gdpoint<L>& lhs, const Gdpoint<R>& rhs);
121 
122 
123  template <typename L, typename R>
124  L // FIXME: promote!
125  operator+(const Gdpoint<L>& lhs, const Gdpoint<R>& rhs);
126 
127  template <typename L, typename R>
128  L // FIXME: promote!
129  operator-(const Gdpoint<L>& lhs, const Gdpoint<R>& rhs);
130 
131  template <typename D, typename S>
132  D // FIXME: promote!
133  operator*(const Gdpoint<D>& lhs, const value::Scalar<S>& rhs);
134 
135 
136 
137  namespace convert
138  {
139 
140  namespace over_load
141  {
142 
143  template <typename D>
144  void
145  from_to_(const Gdpoint<D>& from, mln_site(D)& to);
146 
147 // template <typename D, unsigned n, typename T>
148 // void
149 // from_to_(const Gdpoint<D>& from, algebra::vec<n,T>& to);
150 
151 // template <unsigned n, typename T, typename D>
152 // void
153 // from_to_(const algebra::vec<n,T>& from, Gdpoint<D>& to);
154 
155  } // end of namespace mln::convert::over_load
156 
157  } // end of namespace mln::convert
158 
159 
160 # ifndef MLN_INCLUDE_ONLY
161 
162  template <typename E>
163  inline
164  Gdpoint<E>::Gdpoint()
165  {
166  typedef mln_grid(E) grid;
167  typedef mln_vec(E) vec;
168  vec (E::*m)() const = & E::to_vec;
169  m = 0;
170  }
171 
172 
173  template <typename D>
174  inline
175  std::ostream& operator<<(std::ostream& ostr, const Gdpoint<D>& dp)
176  {
177  enum { n = D::dim };
178  ostr << '(';
179  for (unsigned i = 0; i < n; ++i)
180  ostr << debug::format(exact(dp)[i]) << (i == n - 1 ? ')' : ',');
181  return ostr;
182  }
183 
184 
185  template <typename L, typename R>
186  inline
187  bool operator==(const Gdpoint<L>& lhs, const Gdpoint<R>& rhs)
188  {
189  mlc_equal(mln_grid(L), mln_grid(R))::check();
190  return exact(lhs).to_vec() == exact(rhs).to_vec();
191  }
192 
193  template <typename L, typename R>
194  inline
195  L // FIXME: promote!
196  operator+(const Gdpoint<L>& lhs, const Gdpoint<R>& rhs)
197  {
198  mlc_equal(mln_grid(L), mln_grid(R))::check();
199  L tmp = exact(lhs).to_vec() + exact(rhs).to_vec();
200  return tmp;
201  }
202 
203  template <typename L, typename R>
204  inline
205  L // FIXME: promote!
206  operator-(const Gdpoint<L>& lhs, const Gdpoint<R>& rhs)
207  {
208  mlc_equal(mln_grid(L), mln_grid(R))::check();
209  L tmp = exact(lhs).to_vec() - exact(rhs).to_vec();
210  return tmp;
211  }
212 
213  template <typename D, typename S>
214  D // FIXME: promote!
215  operator*(const Gdpoint<D>& lhs, const value::Scalar<S>& rhs)
216  {
217  D tmp = exact(lhs).to_vec() * exact(rhs);
218  return tmp;
219  }
220 
221 
222  namespace convert
223  {
224 
225  namespace over_load
226  {
227 
228  template <typename D>
229  inline
230  void
231  from_to_(const Gdpoint<D>& dp_, mln_site(D)& p)
232  {
233  enum { n = D::dim };
234  const D& dp = exact(dp_);
235  for (unsigned i = 0; i < n; ++i)
236  p[i] = dp[i];
237  }
238 
239  } // end of namespace mln::convert::over_load
240 
241  } // end of namespace mln::convert
242 
243 # endif // ! MLN_INCLUDE_ONLY
244 
245 } // end of namespace mln
246 
247 
248 #endif // ! MLN_CORE_CONCEPT_GDPOINT_HH