Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2006, 2007, 2008, 2009, 2011 EPITA Research and 00002 // Development Laboratory (LRDE) 00003 // 00004 // This file is part of Olena. 00005 // 00006 // Olena is free software: you can redistribute it and/or modify it under 00007 // the terms of the GNU General Public License as published by the Free 00008 // Software Foundation, version 2 of the License. 00009 // 00010 // Olena is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00017 // 00018 // As a special exception, you may use this file as part of a free 00019 // software project without restriction. Specifically, if other files 00020 // instantiate templates or use macros or inline functions from this 00021 // file, or you compile this file and link it with other files to produce 00022 // an executable, this file does not by itself cause the resulting 00023 // executable to be covered by the GNU General Public License. This 00024 // exception does not however invalidate any other reasons why the 00025 // executable file might be covered by the GNU General Public License. 00026 00027 #ifndef MLN_VALUE_INTERNAL_GRAY_F_HH 00028 # define MLN_VALUE_INTERNAL_GRAY_F_HH 00029 00033 00034 00035 # include <iostream> 00036 00037 # include <mln/value/ops.hh> 00038 00039 # include <mln/core/contract.hh> 00040 # include <mln/metal/math/pow.hh> 00041 # include <mln/metal/bexpr.hh> 00042 # include <mln/literal/ops.hh> 00043 00044 # include <mln/value/float01_f.hh> 00045 # include <mln/trait/value_.hh> 00046 00047 # include <mln/value/graylevel_f.hh> 00048 00049 namespace mln 00050 { 00051 00052 namespace literal 00053 { 00055 struct black_t; 00056 struct medium_gray_t; 00057 struct white_t; 00059 } 00060 namespace value 00061 { 00063 namespace internal 00064 { 00065 template <unsigned n> class gray_; 00066 struct gray_f; 00067 } 00068 00069 struct float01_f; 00071 } 00072 00073 00074 00075 namespace trait 00076 { 00077 00078 00079 template < template <class, class> class Name> 00080 struct set_precise_binary_< Name, mln::value::internal::gray_f, mln::value::internal::gray_f > 00081 { 00082 typedef mln::value::internal::gray_f ret; 00083 }; 00084 00085 template <> 00086 struct set_precise_binary_< op::greater, mln::value::internal::gray_f, mln::value::internal::gray_f > 00087 { 00088 typedef bool ret; 00089 }; 00090 00091 template <> 00092 struct set_precise_binary_< op::eq, mln::value::internal::gray_f, mln::value::internal::gray_f > 00093 { 00094 typedef bool ret; 00095 }; 00096 00097 // Necessary?? 00098 // template <typename F> 00099 // struct set_binary_< op::eq, 00100 // mln::value::Floating, mln::value::internal::gray_f, 00101 // mln::value::Floating, F > 00102 // { 00103 // typedef bool ret; 00104 // }; 00105 00107 template <typename T> struct value_; 00108 00109 // 'gray_f' as a value. 00110 template <> 00111 struct value_<mln::value::internal::gray_f> 00112 { 00113 private: 00114 typedef mln::value::internal::gray_f self_; 00115 typedef float equiv_; 00116 00117 public: 00118 00119 enum { 00120 nbits = mln_nbits(equiv_), 00121 card = 0 00122 }; 00123 00124 typedef trait::value::nature::floating nature; 00125 typedef trait::value::kind::gray kind; 00126 typedef mln_trait_value_quant_(equiv_) quant; 00127 00128 static equiv_ min() { return 0; } 00129 static equiv_ max() { return 1; } 00130 static equiv_ epsilon() { return mln_epsilon(equiv_); } 00131 00132 typedef float sum; 00133 }; 00134 00135 } // end of namespace mln::trait 00136 00137 00138 00139 namespace value 00140 { 00141 00142 namespace internal 00143 { 00144 00146 struct gray_f 00147 : 00148 public Floating< gray_f >, 00149 00150 public internal::value_like_< float, // Equivalent. 00151 float, // Encoding. 00152 gray_f, // Interoperation. 00153 gray_f > // Exact. 00154 { 00156 gray_f(); 00157 00159 gray_f(const gray_f& rhs); 00160 gray_f& operator=(const gray_f& rhs); 00162 00164 template <unsigned n> 00165 gray_f(const gray_<n>& rhs); 00166 template <unsigned n> 00167 gray_f& operator=(const gray_<n>& rhs); 00169 00171 gray_f(float val); 00172 gray_f& operator=(float val); 00174 00176 gray_f(const graylevel_f& val); 00177 gray_f& operator=(const graylevel_f& val); 00179 00181 template <unsigned m> 00182 operator graylevel<m>() const; 00183 00185 template <unsigned m> 00186 operator gray_<m>() const; 00187 00189 operator graylevel_f() const; 00190 00192 float value() const; 00193 }; 00194 00195 // Operators. 00196 00198 std::ostream& operator<<(std::ostream& ostr, const gray_f& g); 00199 00200 } // end of namespace mln::value::internal 00201 00202 } // end of namespace mln::value 00203 00204 } // end of namespace mln 00205 00206 00207 # include <mln/value/internal/gray_.hh> 00208 00209 00210 namespace mln 00211 { 00212 00213 namespace value 00214 { 00215 00216 namespace internal 00217 { 00218 00219 # ifndef MLN_INCLUDE_ONLY 00220 00221 // gray_f. 00222 00223 inline 00224 gray_f::gray_f() 00225 { 00226 } 00227 00228 inline 00229 gray_f::gray_f(const gray_f& g) 00230 : Floating<gray_f>() 00231 { 00232 this->v_ = g.v_; 00233 } 00234 00235 inline 00236 gray_f& 00237 gray_f::operator=(const gray_f& g) 00238 { 00239 this->v_ = g.v_; 00240 return *this; 00241 } 00242 00243 template <unsigned n> 00244 gray_f::gray_f(const gray_<n>& rhs) 00245 { 00246 static const float denom = float(metal::math::pow_int<2, n>::value) - 1.f; 00247 this->v_ = float(rhs.value()) / denom; 00248 } 00249 00250 template <unsigned n> 00251 gray_f& 00252 gray_f::operator=(const gray_<n>& rhs) 00253 { 00254 static const float denom = float(metal::math::pow_int<2, n>::value) - 1.f; 00255 this->v_ = float(rhs.value()) / denom; 00256 return *this; 00257 } 00258 00259 inline 00260 gray_f::gray_f(float val) 00261 { 00262 this->v_ = val; 00263 } 00264 00265 inline 00266 gray_f& 00267 gray_f::operator=(float val) 00268 { 00269 this->v_ = val; 00270 return *this; 00271 } 00272 00273 inline 00274 gray_f::gray_f(const graylevel_f& rhs) 00275 { 00276 this->v_ = rhs.value(); 00277 } 00278 00279 inline 00280 gray_f& 00281 gray_f::operator=(const graylevel_f& rhs) 00282 { 00283 this->v_ = rhs.value(); 00284 return *this; 00285 } 00286 00287 template <unsigned m> 00288 inline 00289 gray_f::operator graylevel<m>() const 00290 { 00291 return graylevel<m>(int(round(this->v_ * (mlc_pow_int(2, m) - 1)))); 00292 } 00293 00294 00295 template <unsigned m> 00296 inline 00297 gray_f::operator gray_<m>() const 00298 { 00299 return gray_<m>(int(round(this->v_ * (mlc_pow_int(2, m) - 1)))); 00300 } 00301 00302 inline 00303 gray_f::operator graylevel_f() const 00304 { 00305 return graylevel_f(this->v_); 00306 } 00307 00308 inline 00309 float 00310 gray_f::value() const 00311 { 00312 return this->v_; 00313 } 00314 00315 // Operators. 00316 00317 inline 00318 std::ostream& operator<<(std::ostream& ostr, const gray_f& g) 00319 { 00320 return ostr << g.value() << "/gl_f"; // FIXME: Be more explicit! 00321 } 00322 00323 # endif // ! MLN_INCLUDE_ONLY 00324 00325 } // end of namespace mln::value::internal 00326 00327 00328 // Graylevel_F operators. 00329 00330 // Op glf == Int 00331 00332 template <typename I> 00333 inline 00334 bool 00335 operator==(const Integer<I>& lhs, const graylevel_f& rhs) 00336 { 00337 return rhs.value() == exact(lhs); 00338 } 00339 00340 // Op glf == glf 00341 inline 00342 bool 00343 operator==(const graylevel_f& lhs, const graylevel_f& rhs) 00344 { 00345 return rhs.value() == lhs.value(); 00346 } 00347 00348 // Op glf + glf 00349 inline 00350 mln_trait_op_plus_(graylevel_f, graylevel_f) 00351 operator+(const graylevel_f& lhs, const graylevel_f& rhs) 00352 { 00353 return lhs.value() + rhs.value(); 00354 } 00355 00356 // Op glf + gl<n> 00357 template <unsigned n> 00358 inline 00359 mln_trait_op_plus(graylevel_f, graylevel<n>) 00360 operator+(const graylevel_f& lhs, const graylevel<n>& rhs) 00361 { 00362 return lhs.value() + graylevel_f(rhs).value(); 00363 } 00364 00365 // Op gl<n> + glf 00366 template <unsigned n> 00367 inline 00368 mln_trait_op_plus(graylevel_f, graylevel<n>) 00369 operator+(const graylevel<n>& lhs, const graylevel_f& rhs) 00370 { 00371 return rhs.value() + graylevel_f(lhs).value(); 00372 } 00373 00374 00375 // Op glf + Another type 00376 template <typename I> 00377 inline 00378 void 00379 operator+(const graylevel_f& lhs, const I& i) 00380 { 00381 (void) lhs; 00382 (void) i; 00383 typename Object<I>::wrong_use_of_graylevel_f___Please_use_the__to_enc__method a; 00384 } 00385 00386 00387 // Op Another type + glf 00388 template <typename I> 00389 inline 00390 void 00391 operator+(const I& i, const graylevel_f& rhs) 00392 { 00393 (void) i; 00394 (void) rhs; 00395 typename Object<I>::wrong_use_of_graylevel_f___Please_use_the__to_enc__method a; 00396 } 00397 00398 00399 // Op glf - Another type 00400 template <typename I> 00401 inline 00402 void 00403 operator-(const graylevel_f& lhs, const I& i) 00404 { 00405 (void) lhs; 00406 (void) i; 00407 typename Object<I>::wrong_use_of_graylevel_f___Please_use_the__to_enc__method a; 00408 } 00409 00410 00411 // Op Another type - glf 00412 template <typename I> 00413 inline 00414 void 00415 operator-(const I& i, const graylevel_f& rhs) 00416 { 00417 (void) i; 00418 (void) rhs; 00419 typename Object<I>::wrong_use_of_graylevel_f___Please_use_the__to_enc__method a; 00420 } 00421 00422 // Op glf - glf 00423 00424 inline 00425 mln_trait_op_minus_(graylevel_f, graylevel_f) 00426 operator-(const graylevel_f& lhs, const graylevel_f& rhs) 00427 { 00428 return lhs.value() - rhs.value(); 00429 } 00430 00431 // Op glf * glf 00432 inline 00433 mln_trait_op_times_(graylevel_f, graylevel_f) 00434 operator*(const graylevel_f& lhs, const graylevel_f& rhs) 00435 { 00436 return lhs.value() * rhs.value(); 00437 } 00438 00439 // Op symm glf * Int 00440 00441 template <typename I> 00442 inline 00443 mln_trait_op_times(graylevel_f, I) 00444 operator*(const graylevel_f& lhs, const Integer<I>& rhs) 00445 { 00446 return lhs.value() * int(exact(rhs)); 00447 } 00448 00449 template <typename I> 00450 inline 00451 mln_trait_op_times(I, graylevel_f) 00452 operator*(const Integer<I>& lhs, const graylevel_f& rhs) 00453 { 00454 return rhs.value() * int(exact(lhs)); 00455 } 00456 00457 // Op symm glf * Float 00458 00459 template <typename F> 00460 inline 00461 mln_trait_op_times(graylevel_f, F) 00462 operator*(const graylevel_f& lhs, const Floating<F>& rhs) 00463 { 00464 return lhs.value() * exact(rhs); 00465 } 00466 00467 template <typename F> 00468 inline 00469 mln_trait_op_times(F, graylevel_f) 00470 operator*(const Floating<F>& lhs, const graylevel_f& rhs) 00471 { 00472 return rhs.value() * exact(lhs); 00473 } 00474 00475 00476 00477 // Op * scalar 00478 template <typename S> 00479 inline 00480 mln_trait_op_times(graylevel_f, S) 00481 operator*(const graylevel_f& lhs, const scalar_<S>& rhs) 00482 { 00483 return lhs.value() * rhs.to_equiv(); 00484 } 00485 00486 // Op / scalar 00487 template <typename S> 00488 inline 00489 mln_trait_op_div(graylevel_f, S) 00490 operator/(const graylevel_f& lhs, const scalar_<S>& rhs) 00491 { 00492 mln_precondition(rhs.to_equiv() != S(0)); 00493 return lhs.value() / rhs.to_equiv(); 00494 } 00495 00496 } // end of namespace mln::value 00497 00498 } // end of namespace mln 00499 00500 #endif // ! MLN_VALUE_INTERNAL_GRAY_F_HH