File: itkExponentialLimiterFunction.hxx

package info (click to toggle)
elastix 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,480 kB
  • sloc: cpp: 68,403; lisp: 4,118; python: 1,013; xml: 182; sh: 177; makefile: 33
file content (153 lines) | stat: -rw-r--r-- 4,634 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*=========================================================================
 *
 *  Copyright UMC Utrecht and contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef itkExponentialLimiterFunction_hxx
#define itkExponentialLimiterFunction_hxx

#include "itkExponentialLimiterFunction.h"
#include <vnl/vnl_math.h>

namespace itk
{

/**
 * *************** Constructor ********************
 */

template <class TInput, unsigned int NDimension>
ExponentialLimiterFunction<TInput, NDimension>::ExponentialLimiterFunction()
{
  this->ComputeLimiterSettings();
} // end Constructor


/**
 * **************** Initialize ***********************
 */

template <class TInput, unsigned int NDimension>
void
ExponentialLimiterFunction<TInput, NDimension>::Initialize()
{
  this->ComputeLimiterSettings();
} // end Initialize()


/**
 * ******************** Evaluate ***********************
 */

template <class TInput, unsigned int NDimension>
auto
ExponentialLimiterFunction<TInput, NDimension>::Evaluate(const InputType & input) const -> OutputType
{
  /** Apply a soft limit if the input is larger than the UpperThreshold */
  const double diffU = static_cast<double>(input - this->m_UpperThreshold);
  if (diffU > 1e-10)
  {
    return static_cast<OutputType>(this->m_UTminUB * std::exp(this->m_UTminUBinv * diffU) + this->m_UpperBound);
  }

  /** Apply a soft limit if the input is smaller than the LowerThreshold */
  const double diffL = static_cast<double>(input - this->m_LowerThreshold);
  if (diffL < -1e-10)
  {
    return static_cast<OutputType>(this->m_LTminLB * std::exp(this->m_LTminLBinv * diffL) + this->m_LowerBound);
  }

  /** Leave the value as it is */
  return static_cast<OutputType>(input);
} // end Evaluate()


/**
 * *********************** Evaluate *************************
 */

template <class TInput, unsigned int NDimension>
auto
ExponentialLimiterFunction<TInput, NDimension>::Evaluate(const InputType & input, DerivativeType & derivative) const
  -> OutputType
{
  /** Apply a soft limit if the input is larger than the UpperThreshold */
  const double diffU = static_cast<double>(input - this->m_UpperThreshold);
  if (diffU > 1e-10)
  {
    const double temp = this->m_UTminUB * std::exp(this->m_UTminUBinv * diffU);
    const double gradientfactor = this->m_UTminUBinv * temp;
    for (unsigned int i = 0; i < Dimension; ++i)
    {
      derivative[i] = static_cast<DerivativeValueType>(derivative[i] * gradientfactor);
    }
    return static_cast<OutputType>(temp + this->m_UpperBound);
  }

  /** Apply a soft limit if the input is smaller than the LowerThreshold */
  const double diffL = static_cast<double>(input - this->m_LowerThreshold);
  if (diffL < -1e-10)
  {
    const double temp = this->m_LTminLB * std::exp(this->m_LTminLBinv * diffL);
    const double gradientfactor = this->m_LTminLBinv * temp;
    for (unsigned int i = 0; i < Dimension; ++i)
    {
      derivative[i] = static_cast<DerivativeValueType>(derivative[i] * gradientfactor);
    }
    return static_cast<OutputType>(temp + this->m_LowerBound);
  }

  /** Leave the value and derivative as they are */
  return static_cast<OutputType>(input);
} // end Evaluate()


/**
 * ******************** ComputeLimiterSettings ********************
 */

template <class TInput, unsigned int NDimension>
void
ExponentialLimiterFunction<TInput, NDimension>::ComputeLimiterSettings()
{
  this->m_UTminUB = static_cast<double>(this->m_UpperThreshold) - this->m_UpperBound;
  this->m_LTminLB = static_cast<double>(this->m_LowerThreshold) - this->m_LowerBound;

  if (this->m_UTminUB < -1e-10)
  {
    this->m_UTminUBinv = 1.0 / this->m_UTminUB;
  }
  else
  {
    /** The result is a hard limiter */
    this->m_UTminUB = 0.0;
    this->m_UTminUBinv = 0.0;
  }
  if (this->m_LTminLB > 1e-10)
  {
    this->m_LTminLBinv = 1.0 / this->m_LTminLB;
  }
  else
  {
    /** The result is a hard limiter */
    this->m_LTminLB = 0.0;
    this->m_LTminLBinv = 0.0;
  }
} // end ComputeLimiterSettings()


} // end namespace itk

#endif