Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
point_site.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_POINT_SITE_HH
27 # define MLN_CORE_CONCEPT_POINT_SITE_HH
28 
34 # include <mln/core/concept/object.hh>
35 # include <mln/core/concept/delta_point_site.hh>
36 # include <mln/core/grids.hh>
37 # include <mln/trait/all.hh>
38 
39 
40 
41 namespace mln
42 {
43 
44  // Fwd decls.
45  template <typename E> struct Point_Site;
46 
47 
48 
49  namespace trait
50  {
51 
52  template < typename P, typename D >
53  struct set_binary_< op::plus,
54  mln::Point_Site, P, mln::Delta_Point_Site, D >
55  {
56  typedef mln_point(P) ret;
57  };
58 
59  template < typename P, typename D >
60  struct set_binary_< op::minus,
61  mln::Point_Site, P, mln::Delta_Point_Site, D >
62  {
63  typedef mln_point(P) ret;
64  };
65 
66  template < typename L, typename R >
67  struct set_binary_< op::minus,
68  mln::Point_Site, L, mln::Point_Site, R >
69  {
70  typedef mln_dpoint(L) ret;
71  };
72 
73  } // end of namespace mln::trait
74 
75 
76 
77 
79  template <>
80  struct Point_Site<void>
81  {
82  typedef Object<void> super;
83  };
84 
85 
105  template <typename E>
106  struct Point_Site : public Object<E>
107  {
108  typedef Point_Site<void> category;
109 
110  /*
111  enum { dim };
112  typedef mesh;
113 
114  typedef point;
115  typedef dpoint;
116  typedef coord;
117 
118  const point& to_point() const;
119 
120  coord operator[](unsigned i) const;
121  */
122 
123  protected:
124  Point_Site();
125  };
126 
127 
142  template <typename L, typename R>
143  bool operator==(const Point_Site<L>& lhs, const Point_Site<R>& rhs);
144 
145 
167  template <typename L, typename R>
168  mln_dpoint(L)
169  operator-(const Point_Site<L>& lhs, const Point_Site<R>& rhs);
170 
171 
185  template <typename P, typename D>
186  mln_point(P)
187  operator+(const Point_Site<P>& p, const Delta_Point_Site<D>& dp);
189 
190 
204  template <typename P, typename D>
205  mln_point(P)
206  operator-(const Point_Site<P>& p, const Delta_Point_Site<D>& dp);
208 
209 
219  template <typename P>
220  std::ostream& operator<<(std::ostream& ostr, const Point_Site<P>& p);
221 
222 
223 
224 
225 # ifndef MLN_INCLUDE_ONLY
226 
227  template <typename E>
228  inline
229  Point_Site<E>::Point_Site()
230  {
231  int dim = E::dim;
232  mln_invariant(dim > 0);
233  dim = 0;
234  typedef mln_mesh(E) mesh;
235  typedef mln_point(E) point;
236  typedef mln_dpoint(E) dpoint;
237  typedef mln_coord(E) coord;
238  const point& (E::*m1)() const = & E::to_point;
239  m1 = 0;
240  coord (E::*m2)(unsigned i) const = & E::operator[];
241  m2 = 0;
242  }
243 
244 
245  // Operators.
246 
247  // FIXME: Remove, or factor in a lower class (Théo removed it from
248  // the cleanup-2008 branch).
249  template <typename L, typename R>
250  inline
251  bool operator==(const Point_Site<L>& lhs_, const Point_Site<R>& rhs_)
252  {
253  mln::metal::bool_<(int(L::dim) == int(R::dim))>::check();
254  const L& lhs = exact(lhs_);
255  const R& rhs = exact(rhs_);
256  for (unsigned i = 0; i < L::dim; ++i)
257  if (lhs[i] != rhs[i])
258  return false;
259  return true;
260  }
261 
262  // FIXME: Remove, or factor in a lower class (Théo removed it from
263  // the cleanup-2008 branch).
264  template <typename L, typename R>
265  inline
266  mln_dpoint(L) // FIXME: promote!
267  operator-(const Point_Site<L>& lhs_, const Point_Site<R>& rhs_)
268  {
269  mln::metal::bool_<(int(L::dim) == int(R::dim))>::check();
270  const L& lhs = exact(lhs_);
271  const R& rhs = exact(rhs_);
272  mln_dpoint(L) tmp;
273  for (unsigned i = 0; i < L::dim; ++i)
274  tmp[i] = lhs[i] - rhs[i];
275  mln_postcondition(rhs_ + tmp == lhs_);
276  return tmp;
277  }
278 
279  template <typename P, typename D>
280  inline
281  mln_point(P) // FIXME: promote!
282  operator+(const Point_Site<P>& p_, const Delta_Point_Site<D>& dp_)
283  {
284  mln::metal::bool_<(int(P::dim) == int(D::dim))>::check();
285  const P& p = exact(p_);
286  const D& dp = exact(dp_);
287  mln_point(P) tmp;
288  for (unsigned i = 0; i < P::dim; ++i)
289  tmp[i] = p[i] + dp[i];
290  return tmp;
291  }
292 
293  template <typename P, typename D>
294  inline
295  mln_point(P) // FIXME: promote!
296  operator-(const Point_Site<P>& p_, const Delta_Point_Site<D>& dp_)
297  {
298  mln::metal::bool_<(int(P::dim) == int(D::dim))>::check();
299  const P& p = exact(p_);
300  const D& dp = exact(dp_);
301  mln_point(P) tmp;
302  for (unsigned i = 0; i < P::dim; ++i)
303  tmp[i] = p[i] - dp[i];
304  return tmp;
305  }
306 
307  // FIXME: We shall not rely on a point object associted to the point
308  // site! (Such an object does not always exist.)
309  template <typename P>
310  inline
311  std::ostream& operator<<(std::ostream& ostr, const Point_Site<P>& p_)
312  {
313  const P& p = exact(p_);
314  ostr << '(';
315  for (unsigned i = 0; i < P::dim; ++i)
316  ostr << p[i] << (i == P::dim - 1 ? ')' : ',');
317  return ostr;
318  }
319 
320 # endif // ! MLN_INCLUDE_ONLY
321 
322 } // end of namespace mln
323 
324 
325 #endif // ! MLN_CORE_CONCEPT_POINT_SITE_HH