00001 //------------------------------------------------------------------------------ 00002 // This file is part of the OpenStructure project <www.openstructure.org> 00003 // 00004 // Copyright (C) 2008-2010 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 00073 // value to value conversion 00074 00075 // declaration 00076 template <typename V, typename R> 00077 DLLEXPORT_OST_IMG_BASE R Val2Val(const V& v); 00078 00079 // specialization 00080 template <> 00081 inline 00082 Real Val2Val<Complex, Real>(const Complex& c) {return std::abs(c);} 00083 00084 template <> 00085 inline 00086 Word Val2Val<Complex, Word>(const Complex& c) {return static_cast<Word>(std::abs(c));} 00087 00088 // generic case 00089 template <typename V, typename R> 00090 inline 00091 R Val2Val(const V& v) {return static_cast<R>(v);} 00092 00093 00094 template <typename T> 00095 DLLEXPORT_OST_IMG_BASE DataType Val2Type(); 00096 00097 template<> 00098 inline 00099 DataType Val2Type<Complex>() {return COMPLEX;} 00100 00101 template <> 00102 inline 00103 DataType Val2Type<Real>() {return REAL;} 00104 00105 template <> 00106 inline 00107 DataType Val2Type<Word>() {return WORD;} 00108 00109 00110 template <typename T> 00111 DLLEXPORT_OST_IMG_BASE String Val2String(); 00112 00113 template<> 00114 inline 00115 String Val2String<Complex>() {return "COMPLEX";} 00116 00117 template <> 00118 inline 00119 String Val2String<Real>() {return "REAL";} 00120 00121 template <> 00122 inline 00123 String Val2String<Word>() {return "WORD";} 00124 00125 00126 }} // namespace 00127 00128 #endif 00129
1.5.8