00001 //------------------------------------------------------------------------------ 00002 // This file is part of the OpenStructure project <www.openstructure.org> 00003 // 00004 // Copyright (C) 2008-2011 by the OpenStructure authors 00005 // Copyright (C) 2003-2010 by the IPLT authors 00006 // 00007 // This library is free software; you can redistribute it and/or modify it under 00008 // the terms of the GNU Lesser General Public License as published by the Free 00009 // Software Foundation; either version 3.0 of the License, or (at your option) 00010 // any later version. 00011 // This library is distributed in the hope that it will be useful, but WITHOUT 00012 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00013 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00014 // details. 00015 // 00016 // You should have received a copy of the GNU Lesser General Public License 00017 // along with this library; if not, write to the Free Software Foundation, Inc., 00018 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 //------------------------------------------------------------------------------ 00020 00021 /* 00022 templated value utilities 00023 00024 Author: Ansgar Philippsen 00025 */ 00026 00027 #ifndef IMG_VALUE_UTIL_H 00028 #define IMG_VALUE_UTIL_H 00029 00030 #include <time.h> 00031 #include <boost/random.hpp> 00032 #include <ost/img/data_types.hh> 00033 00034 #include <ost/img/module_config.hh> 00035 00036 namespace ost { namespace img { 00037 00038 // randomization 00039 // declaration 00040 template <typename T> 00041 DLLEXPORT_OST_IMG_BASE T Random(); 00042 00043 namespace { 00044 boost::mt19937 RandomGenerator(time(NULL)); 00045 boost::uniform_01<boost::mt19937> UniformRandom(RandomGenerator); 00046 } 00047 00048 // specialization 00049 template <> 00050 inline 00051 Complex Random<Complex>() { 00052 Real r=UniformRandom(); 00053 Real p=UniformRandom()*2.0*M_PI; 00054 return Complex(r*cos(p),r*sin(p)); 00055 } 00056 00057 template <> 00058 inline 00059 Word Random<Word>() 00060 { 00061 Real r=UniformRandom(); 00062 return static_cast<Word>(r*65536.0); 00063 } 00064 00065 // definition of generic, ie non-complex case 00066 template <typename T> 00067 inline 00068 T Random() { 00069 return UniformRandom(); 00070 } 00071 00072 typedef boost::variate_generator<boost::mt19937&, boost::uniform_int<> > UniformIntGenerator; 00073 00074 inline 00075 UniformIntGenerator GetUniformIntGenerator(int min, int max) 00076 { 00077 boost::uniform_int<> dist(min,max); 00078 return UniformIntGenerator(RandomGenerator, dist); 00079 } 00080 00081 00082 // value to value conversion 00083 00084 // declaration 00085 template <typename V, typename R> 00086 DLLEXPORT_OST_IMG_BASE R Val2Val(const V& v); 00087 00088 // specialization 00089 template <> 00090 inline 00091 Real Val2Val<Complex, Real>(const Complex& c) {return std::abs(c);} 00092 00093 template <> 00094 inline 00095 Word Val2Val<Complex, Word>(const Complex& c) {return static_cast<Word>(std::abs(c));} 00096 00097 // generic case 00098 template <typename V, typename R> 00099 inline 00100 R Val2Val(const V& v) {return static_cast<R>(v);} 00101 00102 00103 template <typename T> 00104 DLLEXPORT_OST_IMG_BASE DataType Val2Type(); 00105 00106 template<> 00107 inline 00108 DataType Val2Type<Complex>() {return COMPLEX;} 00109 00110 template <> 00111 inline 00112 DataType Val2Type<Real>() {return REAL;} 00113 00114 template <> 00115 inline 00116 DataType Val2Type<Word>() {return WORD;} 00117 00118 00119 template <typename T> 00120 DLLEXPORT_OST_IMG_BASE String Val2String(); 00121 00122 template<> 00123 inline 00124 String Val2String<Complex>() {return "COMPLEX";} 00125 00126 template <> 00127 inline 00128 String Val2String<Real>() {return "REAL";} 00129 00130 template <> 00131 inline 00132 String Val2String<Word>() {return "WORD";} 00133 00134 00135 }} // namespace 00136 00137 #endif 00138