Rev Author Line No. Line
178 kaklik 1 #ifndef FUNCTIONS_H
2 #define FUNCTIONS_H
3  
4 #include <boost/array.hpp>
5 #include <cmath>
6 #include <complex>
7 #include <functional>
8 #include "mimasexception.h"
9  
10 namespace mimas {
11  
12 #ifndef sgn
13 #define sgn(x) ((x<0)?-1:((x>0)?1:0))
14 #endif
15  
16 /// Absolute value.
17 template< typename T >
18 struct _abs: public std::unary_function< T, T >
19 {
20 /** Compute absolute value.
21 @param x A number.
22 @return Absolute value of \c x. */
23 T operator()( const T &x ) const { return std::abs( x ); }
24 };
25  
26 /** Fast square.
27 Compute square of values between -511 and +511 using a precomputed
28 table. */
29 template< typename T >
30 struct _fastsqr: public std::unary_function< T, int >
31 {
32 /// Constructor.
33 _fastsqr(void) {
34 for ( int i=0; i<(signed)table.size(); i++ )
35 table[i] = i * i;
36 }
37 /// Function.
38 int operator()( const T &x ) const {
39 assert( x > -(signed)table.size() && x < (signed)table.size() );
40 return table[ x < 0 ? -x : x ];
41 }
42 /// Table with precomputed values.
43 boost::array< int, 512 > table;
44 };
45  
46 /// Square.
47 template< typename T >
48 struct _sqr: public std::unary_function< T, T >
49 {
50 T operator()( const T &x ) const { return x * x; }
51 };
52  
53 /// Square root.
54 template<typename T>
55 struct _sqrt: public std::unary_function< T, T >
56 {
57 /** Compute square root.
58 @param x A number.
59 @return Square root of \c x. */
60 T operator()( const T &x ) const { return (T)std::sqrt( (float)x ); }
61 };
62  
63 /// Thresholding function.
64 template< typename T >
65 struct _threshold: public std::binary_function< T, T, T >
66 {
67 /** Compare value with threshold.
68 @param x The value to be considered.
69 @param y The threshold to compare with.
70 @return Default-value, if \c x is lower than \c y. Value of \c x
71 otherwise. */
72 T operator()( const T &x, const T &y ) const { return x < y ? T() : x; }
73 };
74  
75 /// Thresholding function.
76 template< typename T >
77 struct _tobinary: public std::binary_function< T, T, bool >
78 {
79 /** Compare value with threshold.
80 @param x The value to be considered.
81 @param y The threshold to compare with.
82 @return A boolean, which is indicating, wether \c x is greater or equal
83 to \c y. */
84 bool operator()( const T &x, const T &y ) const { return x >= y; }
85 };
86  
87  
88 /// Convert boolean-pixel to bilevel-pixel.
89 template< typename T >
90 struct _bilevel: public std::binary_function< T, T, T > {
91 _bilevel( T _val1, T _val2 ): val1(_val1), val2(_val2) {}
92 /** Compare value with threshold and map to {val1,val2}.
93 @param x The value to be considered.
94 @param y The threshold to compare with.
95 @return Bilevel-pixel, which is either \c val1 or \c val2. */
96 T operator()( const T &x, const T &y ) const { return x >= y ? val2 : val1; }
97 T val1;
98 T val2;
99 };
100  
101 /// Thresholding function with 2 levels
102 template< typename T >
103 struct _bilevel_double: public std::unary_function< T, T> {
104 T val1, val2, min, max;
105 _bilevel_double( T _val1, T _val2, T _min, T _max ): val1(_val1), val2(_val2), min(_min), max(_max){}
106 /** Compare value with threshold levels . If value is between min
107 and max, the output is val2, else it's val1.
108 @param x The value to be considered.
109 @return Bilevel-pixel, which is either \c val1 or \c val2. */
110 T operator()( const T &x ) const { return (x >= min && x <= max) ? val2 : val1;
111 }
112 };
113  
114 /// Linear companding function.
115 template< typename T >
116 struct _normalise: public std::unary_function< T, T >
117 {
118 ///
119 _normalise( T _minval, T _maxval, T _val1, T _val2 ) {
120 if ( _maxval > _minval ) {
121 factor = (double)( _val2 - _val1 ) / ( _maxval - _minval );
122 offset = _val1 - _minval * factor;
123 } else {
124 factor = 0.0;
125 offset = _val1;
126 };
127 }
128 T operator()( const T &x ) const {
129 // Scale each pixel-value: ( pixel - minval ) * factor + val1.
130 // pixel * factor - minval * factor + val1
131 return (T)( x * factor + offset );
132 }
133 double factor;
134 double offset;
135 };
136  
137 /// Take norm of a real or complex value.
138 template< typename T1, typename T2 >
139 struct _norm: public std::unary_function< T2, T1 >
140 {
141 T1 operator()( const T2 &x ) const { return std::norm( x ); }
142 };
143  
144 /// The argument of a complex value.
145 template< typename T1, typename T2 >
146 struct _arg: public std::unary_function< T2, T1 >
147 {
148 T1 operator()( const T2 &x ) const { return std::arg( x ); }
149 };
150  
151 /// Complex conjugate.
152 template< typename T >
153 struct _conj: public std::unary_function< T, T >
154 {
155 T operator()( const T &x ) const { return std::conj( x ); }
156 };
157  
158 /// Compute logarithm.
159 template< typename T >
160 struct _log: public std::unary_function< T, T >
161 {
162 T operator()( const T &x ) const { return log( x ); }
163 };
164  
165 /** Compute sum of squares.
166 The sum of squares can be computed with the multiplication- and
167 plus-operator as well, but it would require allocation of one temporary
168 array. */
169 template< typename T >
170 struct _sumsquares: public std::binary_function< T, T, T > {
171 /** Compute sum of squares.
172 @param x First value.
173 @param y Second value.
174 @return x^2+y^2 */
175 T operator()( const T &x, const T &y ) const { return x * x + y * y; }
176 };
177  
178 /// Compute angle.
179 template< typename T >
180 struct _orientation: public std::binary_function< T, T, T > {
181 /** Compute sum of squares.
182 @param y y-component
183 @param x x-component
184 @return atan2( y, x ) */
185 T operator()( const T &y, const T &x ) const { return atan2( y, x ); }
186 };
187  
188 };
189  
190 #endif