Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
tr_image.hh
1 // Copyright (C) 2007, 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_IMAGE_IMORPH_TR_IMAGE_HH
27 # define MLN_CORE_IMAGE_IMORPH_TR_IMAGE_HH
28 
33 
34 # include <cmath>
35 
36 # include <mln/core/internal/image_identity.hh>
37 # include <mln/algebra/vec.hh>
38 # include <mln/value/set.hh>
39 
40 
41 namespace mln
42 {
43 
44  // Forward declaration.
45  template <typename S, typename I, typename T> struct tr_image;
46 
47  namespace internal
48  {
49 
51  template <typename S, typename I, typename T>
52  struct data< tr_image<S,I,T> >
53  {
54  data(const S& s, const I& ima, const T& tr);
55 
56  I ima_;
57  T tr_;
58  S s_;
59  };
60 
61  } // end of namespace mln::internal
62 
63  namespace trait
64  {
65 
66  template <typename S, typename I, typename T>
67  struct image_< tr_image<S,I,T> >
68  : public image_<I> // Same as I except...
69  {
70  // ...these changes.
71  typedef trait::image::value_io::read_only value_io;
72  typedef mln_internal_trait_image_speed_from(I) speed; // Un-fastest.
73  };
74 
75  } // end of namespace mln::trait
76 
77 
81  //
82  template <typename S, typename I, typename T>
83  struct tr_image :
84  public mln::internal::image_identity< I, S, tr_image<S,I,T> >
85  {
86 
88  typedef
89  mln::internal::image_identity< I, S, tr_image<S,I,T> > super_;
90 
92  typedef mln_psite(I) psite;
93 
95  typedef mln_site(I) site;
96 
98  typedef mln_value(I) value;
99 
101  typedef mln_value(I) lvalue; // FIXME: Depends on lvalue presence in I.
102 
104  typedef mln_value(I) rvalue;
105 
107  typedef tr_image< S, tag::image_<I>, T> skeleton;
108 
109 
111  tr_image(const S& s, const I& ima, const T& tr);
112  /* FIXME: What's the purpose of this ctor? AFAIK, morphers
113  objects (and images in general) cannot have their structure /
114  core data altered after they're built. Here, there's a
115  (partial) exception: this morpher provides set_tr(), but has no
116  set_ima(). I (Roland) don't see the point in keeping this ctor
117  if we do not provide set_ima(). */
118  tr_image();
119 
121  void init_(const S& s, const I& ima, const T& tr);
122 
124  bool is_valid() const;
125 
127  using super_::has;
128 
129  enum { dim_ = site::dim };
130  typedef mln::algebra::vec<dim_, float> vec_t;
131 
133  bool has(const vec_t& v) const;
134 
137  //using super_::operator();
138 
139  mln_value(I) operator()(const psite& p) const;
140  mln_value(I) operator()(const psite& p);
141 
143  void set_tr(T& tr);
144 
146  const T& tr() const;
147 
149  const S& domain() const;
150  };
151 
152 
153 
154 # ifndef MLN_INCLUDE_ONLY
155 
156  namespace internal
157  {
158 
159  // internal::data< tr_image<I,S> >
160 
161  template <typename S, typename I, typename T>
162  inline
163  data< tr_image<S,I,T> >::data(const S& s, const I& ima, const T& tr)
164  : ima_(ima),
165  tr_(tr),
166  s_(s)
167  {
168  }
169 
170  } // end of namespace mln::internal
171 
172  template <typename S, typename I, typename T>
173  inline
174  tr_image<S,I,T>::tr_image(const S& s, const I& ima, const T& tr)
175  {
176  init_(s, ima, tr);
177  }
178 
179  template <typename S, typename I, typename T>
180  inline
181  void
182  tr_image<S,I,T>::init_(const S& s, const I& ima, const T& tr)
183  {
184  mln_precondition(ima.is_valid());
185  this->data_ = new internal::data< tr_image<S,I,T> >(s, ima, tr);
186  }
187 
188  template <typename S, typename I, typename T>
189  inline
190  bool
192  {
193  mln_invariant(this->delegatee_()->is_valid());
194  return true;
195  }
196 
197  template <typename S, typename I, typename T>
198  inline
199  bool
200  tr_image<S,I,T>::has(const vec_t& v) const
201  {
202  mln_psite(I) p;
203  algebra::vec<site::dim, float> v2 = this->data_->tr_.inv()(v);
204  for (unsigned i = 0; i < site::dim; ++i)
205  p[i] = static_cast<int>(v2[i]);
206  return this->delegatee_().has(p);
207  }
208 
209 
210  template <typename S, typename I, typename T>
211  inline
212  mln_value(I)
213  tr_image<S,I,T>::operator()(const psite& p) const
214  {
215  algebra::vec<site::dim, float> v = p.to_vec();
216  return this->data_->ima_(this->data_->tr_.inv()(v));
217  }
218 
219  template <typename S, typename I, typename T>
220  inline
221  mln_value(I)
222  tr_image<S,I,T>::operator()(const psite& p)
223  {
224  algebra::vec<site::dim, float> v = p.to_vec();
225  return this->data_->ima_(this->data_->tr_.inv()(v));
226  }
227 
228  template <typename S, typename I, typename T>
229  inline
230  void
232  {
233  this->data_->tr_ = tr;
234  }
235 
236  template <typename S, typename I, typename T>
237  inline
238  const T&
240  {
241  return this->data_->tr_;
242  }
243 
244  template <typename S, typename I, typename T>
245  inline
246  const S&
248  {
249  return this->data_->s_;
250  }
251 
252 
253  template <typename S, typename I, typename T>
254  inline
256  transposed_image(const Site_Set<S>& s, const Image<I>& ima,
257  const Function_v2v<T>& t)
258  {
259  tr_image<S,I,T> tmp(exact(s), exact(ima), exact(t));
260  return tmp;
261  }
262 
263 # endif // ! MLN_INCLUDE_ONLY
264 
265 } // end of namespace mln
266 
267 
268 #endif // ! MLN_CORE_IMAGE_IMORPH_TR_IMAGE_HH