-
Notifications
You must be signed in to change notification settings - Fork 6
/
pctPolynomialMLPFunction.txx
182 lines (157 loc) · 5.46 KB
/
pctPolynomialMLPFunction.txx
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
namespace pct
{
PolynomialMLPFunction
::PolynomialMLPFunction()
{
// We operate a change of origin, u0 is always 0
m_u0=0.;
m_ScalarTest = -1.;
m_CanBeVectorised = true;
}
PolynomialMLPFunction
::PolynomialMLPFunction(int const polydeg)
{
// We operate a change of origin, u0 is always 0
PolynomialMLPFunction();
m_PolynomialDegree = polydeg;
}
void
PolynomialMLPFunction
::SetPolynomialDegree(const int polydeg)
{
m_PolynomialDegree = polydeg;
m_PolynomialDegreePlusThree = m_PolynomialDegree+3;
switch (polydeg)
{
case 0:
m_bm.reserve(Functor::PolynomialMLP::bm_0.size());
std::copy(Functor::PolynomialMLP::bm_0.begin(),Functor::PolynomialMLP::bm_0.end(),std::back_inserter(m_bm));
break;
case 1:
m_bm.reserve(Functor::PolynomialMLP::bm_1.size());
std::copy(Functor::PolynomialMLP::bm_1.begin(),Functor::PolynomialMLP::bm_1.end(),std::back_inserter(m_bm));
break;
case 2:
m_bm.reserve(Functor::PolynomialMLP::bm_2.size());
std::copy(Functor::PolynomialMLP::bm_2.begin(),Functor::PolynomialMLP::bm_2.end(),std::back_inserter(m_bm));
break;
case 3:
m_bm.reserve(Functor::PolynomialMLP::bm_3.size());
std::copy(Functor::PolynomialMLP::bm_3.begin(),Functor::PolynomialMLP::bm_3.end(),std::back_inserter(m_bm));
break;
case 4:
m_bm.reserve(Functor::PolynomialMLP::bm_4.size());
std::copy(Functor::PolynomialMLP::bm_4.begin(),Functor::PolynomialMLP::bm_4.end(),std::back_inserter(m_bm));
break;
case 5:
m_bm.reserve(Functor::PolynomialMLP::bm_5.size());
std::copy(Functor::PolynomialMLP::bm_5.begin(),Functor::PolynomialMLP::bm_5.end(),std::back_inserter(m_bm));
break;
default:
itkWarningMacro( << "Allowed values for polydeg are 0-5. Received " << m_PolynomialDegree << ". Using default (5).");
std::copy(Functor::PolynomialMLP::bm_5.begin(),Functor::PolynomialMLP::bm_5.end(),std::back_inserter(m_bm));
break;
}
}
void
PolynomialMLPFunction
::Init(const VectorType posIn, const VectorType posOut, const VectorType dirIn, const VectorType dirOut)
{
m_uOrigin = posIn[2];
m_u2 = posOut[2]-m_uOrigin;
// Parameters vectors
m_x0[0] = posIn[0];
#if ITK_VERSION_MAJOR <= 4
m_x0[1] = vcl_atan(dirIn[0]); //dirIn[2] is implicitely 1.
#else
m_x0[1] = std::atan(dirIn[0]); //dirIn[2] is implicitely 1.
#endif
m_x2[0] = posOut[0];
#if ITK_VERSION_MAJOR <= 4
m_x2[1] = vcl_atan(dirOut[0]); //dirOut[2] is implicitely 1.
#else
m_x2[1] = std::atan(dirOut[0]); //dirOut[2] is implicitely 1.
#endif
m_y0[0] = posIn[1];
#if ITK_VERSION_MAJOR <= 4
m_y0[1] = vcl_atan(dirIn[1]); //dirIn[2] is implicitely 1.
#else
m_y0[1] = std::atan(dirIn[1]); //dirIn[2] is implicitely 1.
#endif
m_y2[0] = posOut[1];
#if ITK_VERSION_MAJOR <= 4
m_y2[1] = vcl_atan(dirOut[1]); //dirOut[2] is implicitely 1.
#else
m_y2[1] = std::atan(dirOut[1]); //dirOut[2] is implicitely 1.
#endif
const double A = Functor::PolynomialMLP::FactorsABCD::GetA(m_u2, m_bm);
const double B = Functor::PolynomialMLP::FactorsABCD::GetB(m_u2, m_bm);
const double C = Functor::PolynomialMLP::FactorsABCD::GetC(m_u2, m_bm);
const double D = Functor::PolynomialMLP::FactorsABCD::GetD(m_u2, m_bm);
Functor::PolynomialMLP::CoefficientsC::GetValue(m_c_x, m_u2, m_x0, m_x2, A, B, C, D);
Functor::PolynomialMLP::CoefficientsC::GetValue(m_c_y, m_u2, m_y0, m_y2, A, B, C, D);
m_dm_x[0] = m_x0[0];
m_dm_x[1] = m_x0[1];
m_dm_x[2] = m_c_x[0]*m_bm[0]/2;
for(int i = 3; i != m_PolynomialDegree+3; i++)
{
m_dm_x[i] = (m_c_x[0]*m_bm[i-2] + m_c_x[1]*m_bm[i-3]) / i / (i-1);
}
m_dm_x[m_PolynomialDegree+3] = m_c_x[1] * m_bm[m_PolynomialDegree] / (m_PolynomialDegree+2) / (m_PolynomialDegree+3);
m_dm_y[0] = m_y0[0];
m_dm_y[1] = m_y0[1];
m_dm_y[2] = m_c_y[0]*m_bm[0]/2;
for(int i = 3; i != m_PolynomialDegree+3; i++)
{
m_dm_y[i] = (m_c_y[0]*m_bm[i-2] + m_c_y[1]*m_bm[i-3]) / i / (i-1);
}
m_dm_y[m_PolynomialDegree+3] = m_c_y[1] * m_bm[m_PolynomialDegree] / (m_PolynomialDegree+2) / (m_PolynomialDegree+3);
}
// vectorised version
void
PolynomialMLPFunction
::Evaluate( std::vector<double> u, std::vector<double> &x, std::vector<double> &y )
{
for(auto& element : u)
element -= m_uOrigin;
std::fill(x.begin(), x.end(), 0.);
std::fill(y.begin(), y.end(), 0.);
#ifdef MLP_TIMING
m_EvaluateProbe1.Start();
#endif
for(int i = 0; i != m_PolynomialDegreePlusThree; i++)
{
for(auto& element : x)
element += m_dm_x[m_PolynomialDegreePlusThree-i];
std::transform(x.begin(), x.end(), u.begin(), x.begin(), std::multiplies<double>() );
for(auto& element : y)
element += m_dm_y[m_PolynomialDegreePlusThree-i];
std::transform(y.begin(), y.end(), u.begin(), y.begin(), std::multiplies<double>() );
}
for(auto& element : x)
element += m_dm_x[0];
for(auto& element : y)
element += m_dm_y[0];
#ifdef MLP_TIMING
m_EvaluateProbe1.Stop();
#endif
}
void
PolynomialMLPFunction
::EvaluateError( const double u, itk::Matrix<double, 2, 2> &error )
{
itkGenericExceptionMacro("The method PolynomialMLPFunction::EvaluateError is not implemented at the moment");
}
#ifdef MLP_TIMING
void
PolynomialMLPFunction
::PrintTiming(std::ostream& os)
{
os << "PolynomialMLPFunction timing:" << std::endl;
os << " EvaluateProbe1: " << m_EvaluateProbe1.GetTotal()
<< ' ' << m_EvaluateProbe1.GetUnit() << std::endl;
// os << " EvaluateProbe2: " << m_EvaluateProbe2.GetTotal()
// << ' ' << m_EvaluateProbe2.GetUnit() << std::endl;
}
#endif
}