$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends 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/algebra/vec.hh>
37 # include <mln/metal/converts_to.hh>
38 
39 
40 namespace mln
41 {
42 
44  template <typename G, typename C> struct point;
45  namespace literal {
46  struct zero_t;
47  struct one_t;
48  }
50 
51 
56  template <typename G, typename C>
57  struct dpoint : public Gdpoint< dpoint<G,C> >,
58  public internal::mutable_coord_impl_< G::dim, C, dpoint<G,C> >
59  {
63  enum { dim = G::dim };
64 
66  typedef G grid;
67 
69  typedef point<G,C> psite;
70 
72  typedef point<G,C> site;
73 
75  typedef C coord;
76 
79 
83  C operator[](unsigned i) const;
84 
88  C& operator[](unsigned i);
89 
91  dpoint();
92 
94  template <typename C2>
95  dpoint(const algebra::vec<dim,C2>& v);
96 
99  dpoint(C ind);
100  dpoint(C row, C col);
101  dpoint(C sli, C row, C col);
103 
105  dpoint(const literal::zero_t&);
107  // Works only in 1D:
108  dpoint(const literal::one_t&);
111 
113  template <typename F>
114  dpoint(const Function_v2v<F>& f);
115 
117  void set_all(C c);
118 
120  template <typename Q>
121  operator mln::algebra::vec<dpoint<G,C>::dim, Q>() const;
122 
124  vec to_vec() const;
125 
126  protected:
128  };
129 
130 
131 # ifndef MLN_INCLUDE_ONLY
132 
133  template <typename G, typename C>
134  inline
135  C dpoint<G,C>::operator[](unsigned i) const
136  {
137  assert(i < dim);
138  return coord_[i];
139  }
140 
141  template <typename G, typename C>
142  inline
143  C& dpoint<G,C>::operator[](unsigned i)
144  {
145  assert(i < dim);
146  return coord_[i];
147  }
148 
149  template <typename G, typename C>
150  inline
152  {
153  }
154 
155  template <typename G, typename C>
156  template <typename C2>
157  inline
158  dpoint<G,C>::dpoint(const algebra::vec<dim,C2>& v)
159  {
160  // FIXME: to be improved.
161  if (dim < 3)
162  coord_ = v;
163  else
164  {
165  unsigned j = 0;
166  for (unsigned i = dim - 2; i < dim; ++i)
167  coord_[i] = static_cast<C>(v[j++]);
168  for (unsigned i = 2; i < dim; ++i, ++j)
169  coord_[i-j] = static_cast<C>(v[j]);
170  }
171  }
172 
173  template <typename G, typename C>
174  inline
175  dpoint<G,C>::dpoint(C ind)
176  {
177  metal::bool_<(dim == 1)>::check();
178  coord_[0] = ind;
179  }
180 
181  template <typename G, typename C>
182  inline
183  dpoint<G,C>::dpoint(C row, C col)
184  {
185  metal::bool_<(dim == 2)>::check();
186  coord_[0] = row;
187  coord_[1] = col;
188  }
189 
190  template <typename G, typename C>
191  inline
192  dpoint<G,C>::dpoint(C sli, C row, C col)
193  {
194  metal::bool_<(dim == 3)>::check();
195  coord_[0] = sli;
196  coord_[1] = row;
197  coord_[2] = col;
198  }
199 
200  template <typename G, typename C>
201  inline
202  dpoint<G,C>::dpoint(const literal::zero_t&)
203  {
204  coord_.set_all(0);
205  }
206 
207  template <typename G, typename C>
208  inline
209  dpoint<G,C>&
210  dpoint<G,C>::operator=(const literal::zero_t&)
211  {
212  coord_.set_all(0);
213  return *this;
214  }
215 
216  template <typename G, typename C>
217  inline
218  dpoint<G,C>::dpoint(const literal::one_t&)
219  {
220  metal::bool_<(dim == 1)>::check();
221  coord_[0] = 1;
222  }
223 
224  template <typename G, typename C>
225  inline
226  dpoint<G,C>&
227  dpoint<G,C>::operator=(const literal::one_t&)
228  {
229  metal::bool_<(dim == 1)>::check();
230  coord_[0] = 1;
231  return *this;
232  }
233 
234  template <typename G, typename C>
235  template <typename F>
236  inline
237  dpoint<G,C>::dpoint(const Function_v2v<F>& f_)
238  {
239  mlc_converts_to(mln_result(F), C)::check();
240  const F& f = exact(f_);
241  for (unsigned i = 0; i < dim; ++i)
242  coord_[i] = static_cast<C>(f(i));
243  }
244 
245  template <typename G, typename C>
246  inline
247  void dpoint<G,C>::set_all(C c)
248  {
249  for (unsigned i = 0; i < dim; ++i)
250  coord_[i] = c;
251  }
252 
253  template <typename G, typename C>
254  template <typename Q>
255  inline
256  dpoint<G,C>::operator mln::algebra::vec<dpoint<G,C>::dim, Q> () const
257  {
258  return to_vec();
259  }
260 
261  template <typename G, typename C>
262  inline
263  typename dpoint<G,C>::vec
264  dpoint<G,C>::to_vec() const
265  {
266  algebra::vec<G::dim, float> tmp;
267 
268  // FIXME: to be improved.
269  if (dim == 1)
270  tmp[0] = coord_[0];
271  else
272  {
273  unsigned j = 0;
274  for (unsigned i = dim - 2; i < dim; ++i)
275  tmp[j++] = coord_[i];
276  for (unsigned i = 2; i < dim; ++i, ++j)
277  tmp[j] = coord_[i-j];
278  }
279 
280  return tmp;
281  }
282 
283 # endif // ! MLN_INCLUDE_ONLY
284 
285 } // end of namespace mln
286 
287 
288 #endif // ! MLN_CORE_DPOINT_HH