OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
value_util.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 // Copyright (C) 2003-2010 by the IPLT authors
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 3.0 of the License, or (at your option)
10 // any later version.
11 // This library is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 // details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this library; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 //------------------------------------------------------------------------------
20 
21 /*
22  templated value utilities
23 
24  Author: Ansgar Philippsen
25 */
26 
27 #ifndef IMG_VALUE_UTIL_H
28 #define IMG_VALUE_UTIL_H
29 
30 #include <time.h>
31 #include <boost/random.hpp>
32 #include <ost/img/data_types.hh>
33 
34 #include <ost/img/module_config.hh>
35 
36 namespace ost { namespace img {
37 
38 // randomization
39 // declaration
40 template <typename T>
42 
43 namespace {
44  boost::mt19937 RandomGenerator(time(NULL));
45  boost::uniform_01<boost::mt19937> UniformRandom(RandomGenerator);
46 }
47 
48 // specialization
49 template <>
50 inline
52  Real r=UniformRandom();
53  Real p=UniformRandom()*2.0*M_PI;
54  return Complex(r*cos(p),r*sin(p));
55 }
56 
57 template <>
58 inline
60 {
61  Real r=UniformRandom();
62  return static_cast<Word>(r*65536.0);
63 }
64 
65 // definition of generic, ie non-complex case
66 template <typename T>
67 inline
68 T Random() {
69  return UniformRandom();
70 }
71 
72 typedef boost::variate_generator<boost::mt19937&, boost::uniform_int<> > UniformIntGenerator;
73 
74 inline
76 {
77  boost::uniform_int<> dist(min,max);
78  return UniformIntGenerator(RandomGenerator, dist);
79 }
80 
81 
82 // value to value conversion
83 
84 // declaration
85 template <typename V, typename R>
86 DLLEXPORT_OST_IMG_BASE R Val2Val(const V& v);
87 
88 // specialization
89 template <>
90 inline
91 Real Val2Val<Complex, Real>(const Complex& c) {return std::abs(c);}
92 
93 template <>
94 inline
95 Word Val2Val<Complex, Word>(const Complex& c) {return static_cast<Word>(std::abs(c));}
96 
97 // generic case
98 template <typename V, typename R>
99 inline
100 R Val2Val(const V& v) {return static_cast<R>(v);}
101 
102 
103 template <typename T>
105 
106 template<>
107 inline
109 
110 template <>
111 inline
113 
114 template <>
115 inline
117 
118 
119 template <typename T>
121 
122 template<>
123 inline
124 String Val2String<Complex>() {return "COMPLEX";}
125 
126 template <>
127 inline
128 String Val2String<Real>() {return "REAL";}
129 
130 template <>
131 inline
132 String Val2String<Word>() {return "WORD";}
133 
134 
135 }} // namespace
136 
137 #endif
138 
unsigned short Word
Definition: base.hh:52
std::string String
Definition: base.hh:54
Word Val2Val< Complex, Word >(const Complex &c)
Definition: value_util.hh:95
float Real
Definition: base.hh:44
std::complex< Real > Complex
Definition: base.hh:51
DataType Val2Type< Word >()
Definition: value_util.hh:116
DLLEXPORT_OST_IMG_BASE R Val2Val(const V &v)
Definition: value_util.hh:100
Real Val2Val< Complex, Real >(const Complex &c)
Definition: value_util.hh:91
String Val2String< Complex >()
Definition: value_util.hh:124
DLLEXPORT_OST_IMG_BASE DataType Val2Type()
String Val2String< Word >()
Definition: value_util.hh:132
DataType Val2Type< Complex >()
Definition: value_util.hh:108
DLLEXPORT_OST_IMG_BASE String Val2String()
String Val2String< Real >()
Definition: value_util.hh:128
Complex Random< Complex >()
Definition: value_util.hh:51
Word Random< Word >()
Definition: value_util.hh:59
UniformIntGenerator GetUniformIntGenerator(int min, int max)
Definition: value_util.hh:75
DLLEXPORT_OST_IMG_BASE T Random()
Definition: value_util.hh:68
DataType Val2Type< Real >()
Definition: value_util.hh:112
#define DLLEXPORT_OST_IMG_BASE
boost::variate_generator< boost::mt19937 &, boost::uniform_int<> > UniformIntGenerator
Definition: value_util.hh:72