$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
float01_.hh
1 // Copyright (C) 2006, 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_VALUE_FLOAT01__HH
27 # define MLN_VALUE_FLOAT01__HH
28 
34 # include <iostream>
35 # include <mln/core/contract.hh>
36 # include <mln/metal/math/pow.hh>
37 # include <mln/metal/bexpr.hh>
38 
39 # include <mln/value/int_u.hh>
40 # include <mln/value/concept/floating.hh>
41 # include <mln/value/internal/convert.hh>
42 # include <mln/value/float01.hh>
43 # include <mln/trait/value_.hh>
44 
45 
46 
47 namespace mln
48 {
49 
50  // Fwd decls.
51  namespace value {
52  class float01;
53  template <unsigned n> struct float01_;
54  }
55 
56 
57  namespace trait
58  {
59 
60  template <unsigned n>
61  struct value_< mln::value::float01_<n> >
62  {
63  enum constants_ {
64  dim = 1,
65  nbits = n,
66  card = mln_value_card_from_(nbits)
67  };
68 
69  typedef trait::value::nature::floating nature;
70  typedef trait::value::kind::data kind;
71  typedef mln_value_quant_from_(card) quant;
72 
73  static float min() { return 0.f; }
74  static float max() { return 1.f; }
75  static float epsilon() { return 0.f; }
76 
77  typedef float comp;
78 
79  typedef float sum;
80  };
81 
82  } // end of namespace trait
83 
84 
85  namespace value
86  {
87 
89  template <unsigned n>
90  struct float01_
91 
92  : public Floating< float01_<n> >,
93 
94  public internal::value_like_< float, // Equivalent. // FIXME: Why not float01?
95  mln_enc(int_u<n>), // Encoding.
96  float, // Interoperation.
97  float01_<n> > // Exact.
98  {
100  float01_();
101 
103  float01_(float val);
104 
111 
113  float01_<n>& operator=(float val);
114 
116  float value() const;
117 
119  void set_ind(unsigned long val);
120 
122  operator float() const;
123 
124  private:
125  typedef mln_enc(int_u<n>) enc_;
126  };
127 
128 
129  namespace internal
130  {
131 
132  template <unsigned n>
133  struct convert_< float01_<n> >
134  {
135  static float01_<n> value_at_index(unsigned i)
136  {
137  float01_<n> tmp;
138  tmp.set_ind(i);
139  return tmp;
140  }
141 
142  static unsigned index_of_value(const float01_<n>& v)
143  {
144  return v.to_enc();
145  }
146  };
147  }
148 
149 
151  template <unsigned n>
152  std::ostream& operator<<(std::ostream& ostr, const float01_<n>& f);
153 
154 
155  template <unsigned n, unsigned m>
156  bool approx_equal(const float01_<n>& lhs, const float01_<m>& rhs);
157 
158  template <unsigned n>
159  bool approx_equal(const float01_<n>& lhs, const float f);
160 
161 
162 
163 # ifndef MLN_INCLUDE_ONLY
164 
165  // Float01_<n>.
166 
167  template <unsigned n>
168  inline
170  {
171  }
172 
173  template <unsigned n>
174  inline
175  float01_<n>::float01_(float val)
176  {
177  mln_precondition(val >= 0.f);
178  mln_precondition(val <= 1.f);
179  this->v_ = static_cast<enc_>(val * (float(mln_max(enc_)) - 1.f)); // FIXME
180  }
181 
182  template <unsigned n>
183  inline
184  float
185  float01_<n>::value() const
186  {
187  return float(this->v_) / (float(mln_max(enc_)) - 1.f); // FIXME
188  }
189 
190  template <unsigned n>
191  inline
192  void
193  float01_<n>::set_ind(unsigned long val)
194  {
195  this->v_ = static_cast<enc_>(val);
196  }
197 
198  template <unsigned n>
199  inline
201  {
202  this->v_ = 0;
203  }
204 
205  template <unsigned n>
206  inline
207  float01_<n>&
209  {
210  this->v_ = 0;
211  return *this;
212  }
213 
214  template <unsigned n>
215  inline
217  {
218  this->v_ = 1;
219  }
220 
221  template <unsigned n>
222  inline
223  float01_<n>&
225  {
226  this->v_ = 1;
227  return *this;
228  }
229 
230  template <unsigned n>
231  inline
232  float01_<n>&
233  float01_<n>::operator=(float val)
234  {
235  mln_precondition(val >= 0.f);
236  mln_precondition(val <= 1.f);
237  this->v_ = static_cast<enc_>(val * (float(mln_max(enc_)) - 1.f)); // FIXME
238  return *this;
239  }
240 
241  template <unsigned n>
242  inline
243  float01_<n>::operator float() const
244  {
245  return float(this->v_) / (float(mln_max(enc_)) - 1.f);
246  }
247 
248 
249  // Operators.
250 
251  template <unsigned n>
252  inline
253  std::ostream& operator<<(std::ostream& ostr, const float01_<n>& f)
254  {
255  return ostr << f.value();
256  }
257 
258  template <unsigned n, unsigned m>
259  inline
260  bool approx_equal(const float01_<n>& lhs, const float01_<m>& rhs)
261  {
262  return float01(lhs) == float01(rhs);
263  }
264 
265  template <unsigned n>
266  inline
267  bool approx_equal(const float01_<n>& lhs, float f)
268  {
269  return float01(lhs) == float01_<n>(f);
270  }
271 
272 
273 # endif // ! MLN_INCLUDE_ONLY
274 
275  } // end of namespace mln::value
276 
277 } // end of namespace mln
278 
279 #endif // ! MLN_VALUE_FLOAT01__HH