$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
array3d.hh
1 // Copyright (C) 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_METAL_ARRAY3D_HH
27 # define MLN_METAL_ARRAY3D_HH
28 
29 # include <mln/core/concept/object.hh>
30 
31 # include <mln/trait/all.hh>
32 # include <mln/trait/value_.hh>
33 
34 # include <mln/value/ops.hh>
35 
36 namespace mln
37 {
38 
39  // Fwd decls.
40  namespace metal {
41  template <typename T, unsigned s, unsigned r, unsigned c> struct array3d;
42  }
43 
44  namespace trait
45  {
46 
47  template <typename T, unsigned s, unsigned r, unsigned c>
48  struct value_< mln::metal::array3d<T,s, r, c> >
49  {
50  typedef trait::value::nature::vectorial nature;
51  typedef trait::value::kind::data kind;
52 
53  enum {
54  nbits = s * r * c * mln_nbits(T),
55  card = s * r * c * mln_card(T)
56  };
57  typedef mln_value_quant_from_(card) quant;
58 
59  typedef metal::array3d<mln_sum(T), s, r, c> sum;
60  };
61 
62  } // end of namespace mln::trait
63 
64 
65  namespace metal
66  {
67 
68  template <typename T, unsigned s, unsigned r, unsigned c>
69  struct array3d : public Object< array3d<T, s, r, c> >
70  {
71 
72  //
73  // Constructors
74  //
75 
76  array3d();
77  array3d(T* ptr);
78 
79  // Copy
80 
81  array3d(const array3d<T, s, r, c>& rhs);
82  array3d<T, s, r, c>& operator=(const array3d<T, s, r, c>& rhs);
83 
84  // Operators
85 
86  template <class U>
87  array3d<T, s, r, c> operator*(U w);
88 
89  template <class U>
90  array3d<mln_trait_op_div(T,U), s, r, c>
91  operator/(U w);
92 
93  template <typename U>
94  array3d<mln_trait_op_plus(T,U), s, r, c>
95  operator+(const array3d<U, s, r, c>& rhs) const;
96  array3d<T, s, r, c>& operator+=(const array3d<T, s, r, c>& rhs);
97 
98  template <typename U>
99  array3d<mln_trait_op_minus(T,U), s, r, c>
100  operator-(const array3d<U, s, r, c>& rhs) const;
101  array3d<T, s, r, c>&
102  operator-=(const array3d<T, s, r, c>& rhs);
103 
104 
105  // dynamic accessors:
106 
107  T operator[](unsigned i) const {
108  mln_precondition(i < s * r * c);
109  return buffer_[i];
110  }
111  T& operator[](unsigned i) {
112  mln_precondition(i < s * r * c);
113  return buffer_[i];
114  }
115 
116  // static accessor
117 
118  template <unsigned sli, unsigned row, unsigned col>
119  T get() const {
120  return buffer_[sli * (row * col) + col * r + row];
121  }
122  template <unsigned sli, unsigned row, unsigned col>
123  T& get() {
124  return buffer_[sli * (row * col) + col * r + row];
125  }
126 
127 
128  template <unsigned sli, unsigned row, unsigned col>
129  T get_at() const {
130  mln_precondition(sli * (row * col) + col * r + row < s * r * c );
131  return buffer_[sli * (row * col) + col * r + row];
132  }
133  template <unsigned sli, unsigned row, unsigned col>
134  T& get_at() {
135  mln_precondition(sli * (row * col) + col * r + row < s * r * c );
136  return buffer_[sli * (row * col) + col * r + row];
137  }
138 
139  enum { length = s * r * c };
140  protected:
141 
142  T buffer_[s * r * c];
143  };
144 
145  }
146 
147  namespace trait
148  {
149 
150  // For unary traits.
151 
152  template < template <class> class Name,
153  unsigned s, unsigned r, unsigned c, typename T >
154  struct set_precise_unary_< Name, metal::array3d<T, s, r, c> >
155  {
156  typedef mln_trait_unary(Name, T) V;
157  typedef metal::array3d<V, s, r, c> ret;
158  };
159 
160  // For binary traits.
161 
162  template < template <class, class> class Name,
163  unsigned s, unsigned r, unsigned c, typename T,
164  typename U >
165  struct set_precise_binary_< Name,
166  metal::array3d<T, s, r, c>, metal::array3d<U, s, r, c> >
167  {
168  typedef mln_trait_binary(Name, T, U) V;
169  typedef metal::array3d<V, s, r, c> ret;
170  };
171 
172  template < unsigned s, unsigned r, unsigned c, typename T,
173  typename U >
174  struct set_precise_binary_< op::times,
175  metal::array3d<T, s, r, c>, metal::array3d<U, s, r, c> >
176  {
177  typedef mln_sum_product(T,U) ret;
178  };
179 
180  template < template <class, class> class Name,
181  unsigned s, unsigned r, unsigned c, typename T,
182  typename S >
183  struct set_precise_binary_< Name,
184  metal::array3d<T, s, r, c>, mln::value::scalar_<S> >
185  {
186  typedef mln_trait_binary(Name, T, S) V;
187  typedef metal::array3d<V, s, r, c> ret;
188  };
189 
190  template < template<class, class> class Name,
191  unsigned s, unsigned r, unsigned c, typename T,
192  typename S >
193  struct set_binary_< Name,
194  mln::Object, metal::array3d<T, s, r, c>,
195  mln::value::Scalar, S >
196  {
197  typedef mln_trait_binary(Name, T, S) V;
198  typedef metal::array3d<T, s, r, c> ret;
199  };
200 
201  } // end of namespace mln::trait
202 
203 
204  namespace metal
205  {
206 
207  //
208  // Constructors
209  //
210 
211  template <typename T, unsigned s, unsigned r, unsigned c>
212  array3d<T,s, r, c>::array3d()
213  {
214  }
215 
216  template <typename T, unsigned s, unsigned r, unsigned c>
217  array3d<T,s, r, c>::array3d(T* ptr)
218  {
219  for (unsigned i = 0; i < s * r * c; ++i)
220  buffer_[i] = *ptr++;
221  }
222 
223  // Copy
224 
225  template <typename T, unsigned s, unsigned r, unsigned c>
226  array3d<T,s, r, c>::array3d(const array3d<T, s, r, c>& rhs)
227  {
228  for (unsigned i = 0; i < s * r * c; ++i)
229  buffer_[i] = rhs[i];
230  }
231  template <typename T, unsigned s, unsigned r, unsigned c>
232  array3d<T, s, r, c>&
233  array3d<T,s, r, c>::operator=(const array3d<T, s, r, c>& rhs)
234  {
235  for (unsigned i = 0; i < s * r * c; ++i)
236  buffer_[i] = rhs[i];
237  return *this;
238  }
239 
240  // Operators
241 
242  template <typename T, unsigned s, unsigned r, unsigned c>
243  template <class U>
244  array3d<T, s, r, c>
246  {
247  //fixme mln_trait_op_mult<int,U>
248  array3d<T, s, r, c> tmp;
249  for (unsigned i = 0; i < s * r * c; ++i)
250  tmp[i] = this->buffer_[i] * w;
251  return tmp;
252  }
253 
254  template <typename T, unsigned s, unsigned r, unsigned c>
255  template <class U>
256  array3d<mln_trait_op_div(T,U), s, r, c>
258  {
259  array3d<T, s, r, c> tmp;
260  for (unsigned i = 0; i < s * r * c; ++i)
261  tmp[i] = this->buffer_[i] / w;
262  return tmp;
263  }
264 
265  template <typename T, unsigned s, unsigned r, unsigned c>
266  template <typename U>
267  array3d<mln_trait_op_plus(T,U), s, r, c>
268  array3d<T,s, r, c>::operator+(const array3d<U, s, r, c>& rhs) const
269  {
270  array3d<T, s, r, c> tmp;
271  for (unsigned i = 0; i < s * r * c; ++i)
272  tmp[i] = this->buffer_[i] + rhs.buffer_[i];
273  return tmp;
274  }
275  template <typename T, unsigned s, unsigned r, unsigned c>
276  array3d<T, s, r, c>&
277  array3d<T,s, r, c>::operator+=(const array3d<T, s, r, c>& rhs)
278  {
279  for (unsigned i = 0; i < s * r * c; ++i)
280  this->buffer_[i] += rhs.buffer_[i];
281  return *this;
282  }
283 
284  template <typename T, unsigned s, unsigned r, unsigned c>
285  template <typename U>
286  array3d<mln_trait_op_minus(T,U), s, r, c>
287  array3d<T,s, r, c>::operator-(const array3d<U, s, r, c>& rhs) const
288  {
289  array3d<T, s, r, c> tmp;
290  for (unsigned i = 0; i < s * r * c; ++i)
291  tmp[i] = this->buffer_[i] - rhs.buffer_[i];
292  return tmp;
293  }
294  template <typename T, unsigned s, unsigned r, unsigned c>
295  array3d<T, s, r, c>&
296  array3d<T,s, r, c>::operator-=(const array3d<T, s, r, c>& rhs)
297  {
298  for (unsigned i = 0; i < s * r * c; ++i)
299  this->buffer_[i] -= rhs.buffer_[i];
300  return *this;
301  }
302 
303  } // end of namespace metal
304 
305 } // end of namespace mln
306 
307 #endif // ! MLN_METAL_ARRAY3D_HH