GaussianErrorFunction.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Stefan Pszczolkowski Parraguez, Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_GaussianErrorFunction_H
21 #define MIRTK_GaussianErrorFunction_H
22 
23 #include "mirtk/RadialErrorFunction.h"
24 
25 
26 namespace mirtk {
27 
28 
29 /**
30  * Gaussian weighted registration error function
31  */
33 {
34  mirtkObjectMacro(GaussianErrorFunction);
35 
36  /// Variance of Gaussian function
37  mirtkPublicAttributeMacro(double, Variance);
38 
39 public:
40 
41  /// Constructor
42  ///
43  /// \param[in] sigma Standard deviation of Gaussian function.
44  GaussianErrorFunction(double sigma = 1.0)
45  :
46  _Variance(sigma * sigma)
47  {}
48 
49  /// Copy constructor
51  :
52  _Variance(other._Variance)
53  {}
54 
55  /// Destructor
57 
58  /// Copy construct a new instance
60  {
61  return new GaussianErrorFunction(*this);
62  }
63 
64  /// Type enumeration value
65  virtual TypeId Type() const
66  {
67  return Gaussian;
68  }
69 
70  /// Set parameter value from string
71  virtual bool Set(const char *name, const char *value)
72  {
73  if (strcmp(name, "sigma") == 0 ||
74  strcmp(name, "standard deviation") == 0) {
75  double sigma = .0;
76  FromString(value, sigma);
77  if (sigma == .0) return false;
78  _Variance = sigma * sigma;
79  return true;
80  } else if (strcmp(name, "variance") == 0) {
81  return FromString(value, _Variance) && _Variance > .0;
82  }
83  return false;
84  }
85 
86  // Import other overloads
88 
89  /// Get parameter key/value as string map
90  virtual ParameterList Parameter() const
91  {
92  ParameterList params;
93  Insert(params, "sigma", ToString(sqrt(_Variance)));
94  return params;
95  }
96 
97  /// Evaluate radial registration error
98  virtual double Value(double d) const
99  {
100  return exp(-.5 * d / _Variance);
101  }
102 
103  /// Evaluate derivative of radial registration error
104  virtual double Derivative(double d) const
105  {
106  return exp(-.5 * d / _Variance) / _Variance;
107  }
108 
109 };
110 
111 
112 } // namespace mirtk
113 
114 #endif // MIRTK_GaussianErrorFunction_H
virtual ParameterList Parameter() const
Get parameter key/value as string map.
GaussianErrorFunction(const GaussianErrorFunction &other)
Copy constructor.
TypeId
Enumeration of available error functions.
Array< Pair< string, string > > ParameterList
Ordered list of parameter name/value pairs.
Definition: Object.h:38
Definition: IOConfig.h:41
virtual double Value(double d) const
Evaluate radial registration error.
virtual bool Set(const char *name, const char *value)
Set parameter value from string.
string ToString(const EnergyMeasure &value, int w, char c, bool left)
Convert energy measure enumeration value to string.
bool FromString(const char *str, EnergyMeasure &value)
Convert energy measure string to enumeration value.
virtual TypeId Type() const
Type enumeration value.
virtual ParameterList Parameter() const
Get parameter name/value pairs.
Definition: Object.h:139
virtual RadialErrorFunction * NewInstance() const
Copy construct a new instance.
ParameterList & Insert(ParameterList &params, string name, T value)
Insert/replace value into/in parameters list.
Definition: Object.h:212
virtual double Derivative(double d) const
Evaluate derivative of radial registration error.