OpenStructure
Loading...
Searching...
No Matches
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-2020 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
35
36namespace ost { namespace img {
37
38// randomization
39// declaration
40template <typename T>
42
43namespace {
44 boost::mt19937 RandomGenerator(time(NULL));
45 boost::uniform_01<boost::mt19937> UniformRandom(RandomGenerator);
46}
47
48// specialization
49template <>
50inline
52 Real r=UniformRandom();
53 Real p=UniformRandom()*2.0*M_PI;
54 return Complex(r*cos(p),r*sin(p));
55}
56
57template <>
58inline
60{
61 Real r=UniformRandom();
62 return static_cast<Word>(r*65536.0);
63}
64
65// definition of generic, ie non-complex case
66template <typename T>
67inline
68T Random() {
69 return UniformRandom();
70}
71
72typedef boost::variate_generator<boost::mt19937&, boost::uniform_int<> > UniformIntGenerator;
73
74inline
76{
77 boost::uniform_int<> dist(min,max);
78 return UniformIntGenerator(RandomGenerator, dist);
79}
80
81
82// value to value conversion
83
84// declaration
85template <typename V, typename R>
87
88// specialization
89template <>
90inline
91Real Val2Val<Complex, Real>(const Complex& c) {return std::abs(c);}
92
93template <>
94inline
95Word Val2Val<Complex, Word>(const Complex& c) {return static_cast<Word>(std::abs(c));}
96
97// generic case
98template <typename V, typename R>
99inline
100R Val2Val(const V& v) {return static_cast<R>(v);}
101
102
103template <typename T>
105
106template<>
107inline
109
110template <>
111inline
113
114template <>
115inline
117
118
119template <typename T>
121
122template<>
123inline
124String Val2String<Complex>() {return "COMPLEX";}
125
126template <>
127inline
128String Val2String<Real>() {return "REAL";}
129
130template <>
131inline
132String Val2String<Word>() {return "WORD";}
133
134
135}} // namespace
136
137#endif
138
#define DLLEXPORT_OST_IMG_BASE
float Real
Definition base.hh:44
std::complex< Real > Complex
Definition base.hh:51
std::string String
Definition base.hh:54
unsigned short Word
Definition base.hh:52
String Val2String< Real >()
Word Val2Val< Complex, Word >(const Complex &c)
Definition value_util.hh:95
DataType Val2Type< Complex >()
Complex Random< Complex >()
Definition value_util.hh:51
DLLEXPORT_OST_IMG_BASE DataType Val2Type()
DLLEXPORT_OST_IMG_BASE String Val2String()
DataType Val2Type< Real >()
Word Random< Word >()
Definition value_util.hh:59
Real Val2Val< Complex, Real >(const Complex &c)
Definition value_util.hh:91
UniformIntGenerator GetUniformIntGenerator(int min, int max)
Definition value_util.hh:75
String Val2String< Complex >()
DLLEXPORT_OST_IMG_BASE R Val2Val(const V &v)
DLLEXPORT_OST_IMG_BASE T Random()
Definition value_util.hh:68
boost::variate_generator< boost::mt19937 &, boost::uniform_int<> > UniformIntGenerator
Definition value_util.hh:72
DataType Val2Type< Word >()
String Val2String< Word >()
Definition base.dox:1