Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
dpoint.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_DPOINT_HH
27 # define MLN_CORE_DPOINT_HH
28 
32 
33 # include <mln/core/def/coord.hh>
34 # include <mln/core/concept/gdpoint.hh>
35 # include <mln/core/internal/coord_impl.hh>
36 # include <mln/fun/i2v/all.hh>
37 # include <mln/algebra/vec.hh>
38 # include <mln/metal/converts_to.hh>
39 
40 
41 namespace mln
42 {
43 
45  template <typename G, typename C> struct point;
46  namespace literal {
47  struct zero_t;
48  struct one_t;
49  }
51 
52 
57  template <typename G, typename C>
58  struct dpoint : public Gdpoint< dpoint<G,C> >,
59  public internal::mutable_coord_impl_< G::dim, C, dpoint<G,C> >
60  {
64  enum { dim = G::dim };
65 
67  typedef G grid;
68 
70  typedef point<G,C> psite;
71 
73  typedef point<G,C> site;
74 
76  typedef C coord;
77 
79  typedef algebra::vec<G::dim, C> vec;
80 
84  C operator[](unsigned i) const;
85 
89  C& operator[](unsigned i);
90 
92  dpoint();
93 
95  template <typename C2>
96  dpoint(const algebra::vec<dim,C2>& v);
97 
100  dpoint(C ind);
101  dpoint(C row, C col);
102  dpoint(C sli, C row, C col);
104 
106  dpoint(const literal::zero_t&);
107  dpoint<G,C>& operator=(const literal::zero_t&);
108  // Works only in 1D:
109  dpoint(const literal::one_t&);
110  dpoint<G,C>& operator=(const literal::one_t&);
112 
114  template <typename F>
115  dpoint(const Function_v2v<F>& f);
116 
118  void set_all(C c);
119 
121  template <typename Q>
122  operator mln::algebra::vec<dpoint<G,C>::dim, Q>() const;
123 
125  vec to_vec() const;
126 
127  protected:
128  mln::algebra::vec<G::dim, C> coord_;
129  };
130 
131 
132 # ifndef MLN_INCLUDE_ONLY
133 
134  template <typename G, typename C>
135  inline
136  C dpoint<G,C>::operator[](unsigned i) const
137  {
138  assert(i < dim);
139  return coord_[i];
140  }
141 
142  template <typename G, typename C>
143  inline
144  C& dpoint<G,C>::operator[](unsigned i)
145  {
146  assert(i < dim);
147  return coord_[i];
148  }
149 
150  template <typename G, typename C>
151  inline
153  {
154  }
155 
156  template <typename G, typename C>
157  template <typename C2>
158  inline
159  dpoint<G,C>::dpoint(const algebra::vec<dim,C2>& v)
160  {
161  // FIXME: to be improved.
162  if (dim < 3)
163  coord_ = v;
164  else
165  {
166  unsigned j = 0;
167  for (unsigned i = dim - 2; i < dim; ++i)
168  coord_[i] = static_cast<C>(v[j++]);
169  for (unsigned i = 2; i < dim; ++i, ++j)
170  coord_[i-j] = static_cast<C>(v[j]);
171  }
172  }
173 
174  template <typename G, typename C>
175  inline
177  {
178  metal::bool_<(dim == 1)>::check();
179  coord_[0] = ind;
180  }
181 
182  template <typename G, typename C>
183  inline
184  dpoint<G,C>::dpoint(C row, C col)
185  {
186  metal::bool_<(dim == 2)>::check();
187  coord_[0] = row;
188  coord_[1] = col;
189  }
190 
191  template <typename G, typename C>
192  inline
193  dpoint<G,C>::dpoint(C sli, C row, C col)
194  {
195  metal::bool_<(dim == 3)>::check();
196  coord_[0] = sli;
197  coord_[1] = row;
198  coord_[2] = col;
199  }
200 
201  template <typename G, typename C>
202  inline
204  {
205  coord_.set_all(0);
206  }
207 
208  template <typename G, typename C>
209  inline
210  dpoint<G,C>&
212  {
213  coord_.set_all(0);
214  return *this;
215  }
216 
217  template <typename G, typename C>
218  inline
219  dpoint<G,C>::dpoint(const literal::one_t&)
220  {
221  metal::bool_<(dim == 1)>::check();
222  coord_[0] = 1;
223  }
224 
225  template <typename G, typename C>
226  inline
227  dpoint<G,C>&
228  dpoint<G,C>::operator=(const literal::one_t&)
229  {
230  metal::bool_<(dim == 1)>::check();
231  coord_[0] = 1;
232  return *this;
233  }
234 
235  template <typename G, typename C>
236  template <typename F>
237  inline
239  {
240  mlc_converts_to(mln_result(F), C)::check();
241  const F& f = exact(f_);
242  for (unsigned i = 0; i < dim; ++i)
243  coord_[i] = static_cast<C>(f(i));
244  }
245 
246  template <typename G, typename C>
247  inline
249  {
250  for (unsigned i = 0; i < dim; ++i)
251  coord_[i] = c;
252  }
253 
254  template <typename G, typename C>
255  template <typename Q>
256  inline
257  dpoint<G,C>::operator mln::algebra::vec<dpoint<G,C>::dim, Q> () const
258  {
259  return to_vec();
260  }
261 
262  template <typename G, typename C>
263  inline
264  typename dpoint<G,C>::vec
266  {
267  algebra::vec<G::dim, float> tmp;
268 
269  // FIXME: to be improved.
270  if (dim == 1)
271  tmp[0] = coord_[0];
272  else
273  {
274  unsigned j = 0;
275  for (unsigned i = dim - 2; i < dim; ++i)
276  tmp[j++] = coord_[i];
277  for (unsigned i = 2; i < dim; ++i, ++j)
278  tmp[j] = coord_[i-j];
279  }
280 
281  return tmp;
282  }
283 
284 # endif // ! MLN_INCLUDE_ONLY
285 
286 } // end of namespace mln
287 
288 
289 #endif // ! MLN_CORE_DPOINT_HH