Debian Codegraph @

include/vigra/gaussians.hxx

raw symbols SCIP
1 /************************************************************************/cxx . . $ `<file>/include/vigra/gaussians.hxx`/
2 /* */
3 /* Copyright 1998-2004 by Ullrich Koethe */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* [email protected] or */
10 /* [email protected] */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35
36 #ifndef VIGRA_GAUSSIANS_HXX
37 #define VIGRA_GAUSSIANS_HXXcxx . . $ `include/vigra/gaussians.hxx:37:9`!
38
39 #include <cmath>
40 #include "config.hxx"cxx . . $ `<file>/include/vigra/config.hxx`/
41 #include "mathutil.hxx"cxx . . $ `<file>/include/vigra/mathutil.hxx`/
42 #include "array_vector.hxx"cxx . . $ `<file>/include/vigra/array_vector.hxx`/
43 #include "error.hxx"cxx . . $ `<file>/include/vigra/error.hxx`/
44
45 namespace vigracxx . . $ vigra/ {
46
47 #if 0
48 /** \addtogroup MathFunctions Mathematical Functions
49 */
50 //@{
51 #endif
52 /** The Gaussian function and its derivatives.
53
54 Implemented as a unary functor. Since it supports the <tt>radius()</tt> function
55 it can also be used as a kernel in \ref resamplingConvolveImage().
56
57 <b>\#include</b> \<vigra/gaussians.hxx\><br>
58 Namespace: vigra
59
60 \ingroup MathFunctions
61 */
62 template <class T = double>
63 class Gaussiancxx . . $ vigra/Gaussian#
64 {
65 public:
66
67 /** the value type if used as a kernel in \ref resamplingConvolveImage().
68 */
69 typedef T value_typecxx . . $ vigra/Gaussian#value_type#;
70 /** the functor's argument type
71 */
72 typedef T argument_typecxx . . $ vigra/Gaussian#argument_type#;
73 /** the functor's result type
74 */
75 typedef T result_typecxx . . $ vigra/Gaussian#result_type#;
76
77 /** Create functor for the given standard deviation <tt>sigma</tt> and
78 derivative order <i>n</i>. The functor then realizes the function
79
80 \f[ f_{\sigma,n}(x)=\frac{\partial^n}{\partial x^n}
81 \frac{1}{\sqrt{2\pi}\sigma}e^{-\frac{x^2}{2\sigma^2}}
82 \f]
83
84 Precondition:
85 \code
86 sigma > 0.0
87 \endcode
88 */
89 explicit Gaussiancxx . . $ vigra/Gaussian#`Gaussian<T>`(b93190170a9f96a8).(T sigma = 1.0, unsigned int derivativeOrder = 0)
90 : sigma_cxx . . $ vigra/Gaussian#sigma_.(sigma),
91 sigma2_cxx . . $ vigra/Gaussian#sigma2_.(T(-0.5 /cxx . . $ vigra/`operator/`(6f3a3930211914e8). sigma /cxx . . $ vigra/`operator/`(6f3a3930211914e8). sigma)),
92 norm_cxx . . $ vigra/Gaussian#norm_.(0.0),
93 order_cxx . . $ vigra/Gaussian#order_.(derivativeOrder),
94 hermitePolynomial_cxx . . $ vigra/Gaussian#hermitePolynomial_.(derivativeOrder / 2 + 1)
95 {
96 vigra_preconditioncxx . . $ `include/vigra/error.hxx:265:9`!(sigma_ > 0.0,
97 "Gaussian::Gaussian(): sigma > 0 required.");
98 switch(order_cxx . . $ vigra/Gaussian#order_.)
99 {
100 case 1:
101 case 2:
102 norm_cxx . . $ vigra/Gaussian#norm_. = T(-1.0 /cxx . . $ vigra/`operator/`(6f3a3930211914e8). (VIGRA_CSTDcxx . . $ `include/vigra/config.hxx:280:13`!::sqrt(2.0 * M_PIcxx . . $ `<external>/11263917310783833665/math.h:1121:10`!) *cxx . . $ vigra/`operator*`(6f3a3930211914e8). sqcxx . . $ vigra/sq(1aa255fb643c2591).(sigma) *cxx . . $ vigra/`operator*`(6f3a3930211914e8). sigma));
103 break;
104 case 3:
105 norm_cxx . . $ vigra/Gaussian#norm_. = T(1.0 /cxx . . $ vigra/`operator/`(6f3a3930211914e8). (VIGRA_CSTDcxx . . $ `include/vigra/config.hxx:280:13`!::sqrt(2.0 * M_PIcxx . . $ `<external>/11263917310783833665/math.h:1121:10`!) *cxx . . $ vigra/`operator*`(6f3a3930211914e8). sqcxx . . $ vigra/sq(1aa255fb643c2591).(sigma) *cxx . . $ vigra/`operator*`(6f3a3930211914e8). sqcxx . . $ vigra/sq(1aa255fb643c2591).(sigma) *cxx . . $ vigra/`operator*`(6f3a3930211914e8). sigma));
106 break;
107 default:
108 norm_cxx . . $ vigra/Gaussian#norm_. = T(1.0 / VIGRA_CSTDcxx . . $ `include/vigra/config.hxx:280:13`!::sqrt(2.0 * M_PIcxx . . $ `<external>/11263917310783833665/math.h:1121:10`!) /cxx . . $ vigra/`operator/`(6f3a3930211914e8). sigma);
109 }
110 calculateHermitePolynomialcxx . . $ vigra/Gaussian#calculateHermitePolynomial(49f6e7a06ebc5aa8).();
111 }
112
113 /** Function (functor) call.
114 */
115 result_typecxx . . $ vigra/Gaussian#result_type# operatorcxx . . $ vigra/Gaussian#`operator()`(2b3d2a6686d5a9c3).()(argument_typecxx . . $ vigra/Gaussian#argument_type# x) const;
116
117 /** Get the standard deviation of the Gaussian.
118 */
119 value_typecxx . . $ vigra/Gaussian#value_type# sigmacxx . . $ vigra/Gaussian#sigma(8f5363d873dd4781).() const
120 { return sigma_cxx . . $ vigra/Gaussian#sigma_.; }
121
122 /** Get the derivative order of the Gaussian.
123 */
124 unsigned int derivativeOrdercxx . . $ vigra/Gaussian#derivativeOrder(ddd8539a2fcdd4ce).() const
125 { return order_cxx . . $ vigra/Gaussian#order_.; }
126
127 /** Get the required filter radius for a discrete approximation of the Gaussian.
128 The radius is given as a multiple of the Gaussian's standard deviation
129 (default: <tt>sigma * (3 + 1/2 * derivativeOrder()</tt> -- the second term
130 accounts for the fact that the derivatives of the Gaussian become wider
131 with increasing order). The result is rounded to the next higher integer.
132 */
133 double radiuscxx . . $ vigra/Gaussian#radius(7e3a3c6b59947c6d).(double sigmaMultiple = 3.0) const
134 { return VIGRA_CSTDcxx . . $ `include/vigra/config.hxx:280:13`!::ceil(sigma_cxx . . $ vigra/Gaussian#sigma_. *cxx . . $ vigra/`operator*`(6f3a3930211914e8). (sigmaMultiple +cxx . . $ vigra/operator+(76177e72537f9555). 0.5 *cxx . . $ vigra/`operator*`(6f3a3930211914e8). derivativeOrdercxx . . $ vigra/Gaussian#derivativeOrder(ddd8539a2fcdd4ce).())); }
135
136 private:
137 void calculateHermitePolynomialcxx . . $ vigra/Gaussian#calculateHermitePolynomial(49f6e7a06ebc5aa8).();
138 T hornercxx . . $ vigra/Gaussian#horner(2b3d2a6686d5a9c3).(T x) const;
139
140 T sigma_cxx . . $ vigra/Gaussian#sigma_., sigma2_cxx . . $ vigra/Gaussian#sigma2_., norm_cxx . . $ vigra/Gaussian#norm_.;
141 unsigned int order_cxx . . $ vigra/Gaussian#order_.;
142 ArrayVectorcxx . . $ vigra/ArrayVector#<T> hermitePolynomial_cxx . . $ vigra/Gaussian#hermitePolynomial_.;
143 };
144
145 template <class T>
146 typename Gaussiancxx . . $ vigra/Gaussian#<T>::result_typecxx . . $ vigra/Gaussian#result_type#
147 Gaussiancxx . . $ vigra/Gaussian#<T>::operatorcxx . . $ vigra/Gaussian#`operator()`(2b3d2a6686d5a9c3).()(argument_typecxx . . $ vigra/Gaussian#argument_type# x) const
148 {
149 T x2 = x *cxx . . $ vigra/`operator*`(6f3a3930211914e8). x;
150 T g = norm_cxx . . $ vigra/Gaussian#norm_. *cxx . . $ vigra/`operator*`(6f3a3930211914e8). VIGRA_CSTDcxx . . $ `include/vigra/config.hxx:280:13`!::expcxx . . $ std/exp(a358fd9634d01384).(x2 *cxx . . $ vigra/`operator*`(6f3a3930211914e8). sigma2_cxx . . $ vigra/Gaussian#sigma2_.);
151 switch(order_cxx . . $ vigra/Gaussian#order_.)
152 {
153 case 0:
154 return detailcxx . . $ vigra/detail/::RequiresExplicitCastcxx . . $ vigra/detail/RequiresExplicitCast#<result_type>::cast(g);
155 case 1:
156 return detailcxx . . $ vigra/detail/::RequiresExplicitCastcxx . . $ vigra/detail/RequiresExplicitCast#<result_type>::cast(x *cxx . . $ vigra/`operator*`(6f3a3930211914e8). g);
157 case 2:
158 return detailcxx . . $ vigra/detail/::RequiresExplicitCastcxx . . $ vigra/detail/RequiresExplicitCast#<result_type>::cast((1.0 -cxx . . $ vigra/operator-(76177e72537f9555). sqcxx . . $ vigra/sq(1aa255fb643c2591).(x /cxx . . $ vigra/`operator/`(6f3a3930211914e8). sigma_cxx . . $ vigra/Gaussian#sigma_.)) *cxx . . $ vigra/`operator*`(6f3a3930211914e8). g);
159 case 3:
160 return detailcxx . . $ vigra/detail/::RequiresExplicitCastcxx . . $ vigra/detail/RequiresExplicitCast#<result_type>::cast((3.0 -cxx . . $ vigra/operator-(76177e72537f9555). sqcxx . . $ vigra/sq(1aa255fb643c2591).(x /cxx . . $ vigra/`operator/`(6f3a3930211914e8). sigma_cxx . . $ vigra/Gaussian#sigma_.)) *cxx . . $ vigra/`operator*`(6f3a3930211914e8). x *cxx . . $ vigra/`operator*`(6f3a3930211914e8). g);
161 default:
162 return order_cxx . . $ vigra/Gaussian#order_. % 2 == 0 ?
163 detailcxx . . $ vigra/detail/::RequiresExplicitCastcxx . . $ vigra/detail/RequiresExplicitCast#<result_type>::cast(g *cxx . . $ vigra/`operator*`(6f3a3930211914e8). hornercxx . . $ vigra/Gaussian#horner(2b3d2a6686d5a9c3).(x2))
164 : detailcxx . . $ vigra/detail/::RequiresExplicitCastcxx . . $ vigra/detail/RequiresExplicitCast#<result_type>::cast(x *cxx . . $ vigra/`operator*`(6f3a3930211914e8). g *cxx . . $ vigra/`operator*`(6f3a3930211914e8). hornercxx . . $ vigra/Gaussian#horner(2b3d2a6686d5a9c3).(x2));
165 }
166 }
167
168 template <class T>
169 T Gaussiancxx . . $ vigra/Gaussian#<T>::hornercxx . . $ vigra/Gaussian#horner(2b3d2a6686d5a9c3).(T x) const
170 {
171 int i = order_cxx . . $ vigra/Gaussian#order_. / 2;
172 T res = hermitePolynomial_cxx . . $ vigra/Gaussian#hermitePolynomial_.[i];
173 for(--i; i >= 0; --i)
174 res = x *cxx . . $ vigra/`operator*`(6f3a3930211914e8). res +cxx . . $ vigra/operator+(76177e72537f9555). hermitePolynomial_cxx . . $ vigra/Gaussian#hermitePolynomial_.[i];
175 return res;
176 }
177
178 template <class T>
179 void Gaussiancxx . . $ vigra/Gaussian#<T>::calculateHermitePolynomialcxx . . $ vigra/Gaussian#calculateHermitePolynomial(49f6e7a06ebc5aa8).()
180 {
181 if(order_cxx . . $ vigra/Gaussian#order_. == 0)
182 {
183 hermitePolynomial_cxx . . $ vigra/Gaussian#hermitePolynomial_.[0] = 1.0;
184 }
185 else if(order_cxx . . $ vigra/Gaussian#order_. == 1)
186 {
187 hermitePolynomial_cxx . . $ vigra/Gaussian#hermitePolynomial_.[0] = T(-1.0 /cxx . . $ vigra/`operator/`(6f3a3930211914e8). sigma_cxx . . $ vigra/Gaussian#sigma_. /cxx . . $ vigra/`operator/`(6f3a3930211914e8). sigma_cxx . . $ vigra/Gaussian#sigma_.);
188 }
189 else
190 {
191 // calculate Hermite polynomial for requested derivative
192 // recursively according to
193 // (0)
194 // h (x) = 1
195 //
196 // (1)
197 // h (x) = -x / s^2
198 //
199 // (n+1) (n) (n-1)
200 // h (x) = -1 / s^2 * [ x * h (x) + n * h (x) ]
201 //
202 T s2 = T(-1.0 /cxx . . $ vigra/`operator/`(6f3a3930211914e8). sigma_cxx . . $ vigra/Gaussian#sigma_. /cxx . . $ vigra/`operator/`(6f3a3930211914e8). sigma_cxx . . $ vigra/Gaussian#sigma_.);
203 ArrayVectorcxx . . $ vigra/ArrayVector#<T> hn(3*order_cxx . . $ vigra/Gaussian#order_.+3, 0.0);
204 typename ArrayVectorcxx . . $ vigra/ArrayVector#<T>::iterator hn0 = hn.begincxx . . $ vigra/ArrayVectorView#begin(1016ee5406d91ab9).(),
205 hn1 = hn0 +cxx . . $ vigra/operator+(76177e72537f9555). order_cxx . . $ vigra/Gaussian#order_.+cxx . . $ vigra/operator+(76177e72537f9555).1,
206 hn2 = hn1 +cxx . . $ vigra/operator+(76177e72537f9555). order_cxx . . $ vigra/Gaussian#order_.+cxx . . $ vigra/operator+(76177e72537f9555).1,
207 ht;
208 hn2[0] = 1.0;
209 hn1[1] = s2;
210 for(unsigned int i = 2; i <= order_cxx . . $ vigra/Gaussian#order_.; ++i)
211 {
212 hn0[0] = s2 *cxx . . $ vigra/`operator*`(6f3a3930211914e8). (i-1) *cxx . . $ vigra/`operator*`(6f3a3930211914e8). hn2[0];
213 for(unsigned int j = 1; j <= i; ++j)
214 hn0[j] = s2 *cxx . . $ vigra/`operator*`(6f3a3930211914e8). (hn1[j-1] +cxx . . $ vigra/operator+(76177e72537f9555). (i-1) *cxx . . $ vigra/`operator*`(6f3a3930211914e8). hn2[j]);
215 ht = hn2;
216 hn2 = hn1;
217 hn1 = hn0;
218 hn0 = ht;
219 }
220 // keep only non-zero coefficients of the polynomial
221 for(unsigned int i = 0; i <cxx . . $ vigra/`operator<`(b84b089163f6fc8). hermitePolynomial_cxx . . $ vigra/Gaussian#hermitePolynomial_..sizecxx . . $ vigra/ArrayVectorView#size(fe4d2d262d4164a8).(); ++i)
222 hermitePolynomial_cxx . . $ vigra/Gaussian#hermitePolynomial_.[i] = order_cxx . . $ vigra/Gaussian#order_. % 2 == 0 ?
223 hn1[2*i]
224 : hn1[2*i+1];
225 }
226 }
227
228
229 ////@}
230
231 } // namespace vigra
232
233
234 #endif /* VIGRA_GAUSSIANS_HXX */