File: itkAffineLogTransform.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 (271 lines) | stat: -rw-r--r-- 7,189 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*=========================================================================
 *
 *  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 itkAffineLogTransform_hxx
#define itkAffineLogTransform_hxx

#include <vnl/vnl_matrix_exp.h>
#include "itkMath.h"
#include "itkAffineLogTransform.h"

namespace itk
{

// Constructor with default arguments
template <class TScalarType, unsigned int Dimension>
AffineLogTransform<TScalarType, Dimension>::AffineLogTransform()
  : Superclass(ParametersDimension)
{
  this->m_MatrixLogDomain.Fill(ScalarType{});
  this->PrecomputeJacobianOfSpatialJacobian();
}


// Constructor with default arguments
template <class TScalarType, unsigned int Dimension>
AffineLogTransform<TScalarType, Dimension>::AffineLogTransform(const MatrixType &      matrix,
                                                               const OutputPointType & offset)
{
  this->SetMatrix(matrix);

  OffsetType off;
  for (unsigned int i = 0; i < Dimension; ++i)
  {
    off[i] = offset[i];
  }
  this->SetOffset(off);

  this->PrecomputeJacobianOfSpatialJacobian();
}


// Constructor with arguments
template <class TScalarType, unsigned int Dimension>
AffineLogTransform<TScalarType, Dimension>::AffineLogTransform(unsigned int spaceDimension,
                                                               unsigned int parametersDimension)
  : Superclass(spaceDimension, parametersDimension)
{
  this->m_MatrixLogDomain.Fill(ScalarType{});
  this->PrecomputeJacobianOfSpatialJacobian();
}


// Set Parameters
template <class TScalarType, unsigned int Dimension>
void
AffineLogTransform<TScalarType, Dimension>::SetParameters(const ParametersType & parameters)
{
  itkDebugMacro("Setting parameters " << parameters);
  unsigned int k = 0; // Dummy loop index

  MatrixType exponentMatrix;

  for (unsigned int i = 0; i < Dimension; ++i)
  {
    for (unsigned int j = 0; j < Dimension; ++j)
    {
      this->m_MatrixLogDomain(i, j) = parameters[k];
      k += 1;
    }
  }

  exponentMatrix = vnl_matrix_exp(this->m_MatrixLogDomain.GetVnlMatrix());

  this->PrecomputeJacobianOfSpatialJacobian();

  this->SetVarMatrix(exponentMatrix);

  OutputVectorType off;

  for (unsigned int i = 0; i < Dimension; ++i)
  {
    off[i] = parameters[k];
    k += 1;
  }

  this->SetVarTranslation(off);
  this->ComputeOffset();

  // Modified is always called since we just have a pointer to the
  // parameters and cannot know if the parameters have changed.

  this->Modified();
  itkDebugMacro("After setting parameters ");
}


// Get Parameters
template <class TScalarType, unsigned int Dimension>
auto
AffineLogTransform<TScalarType, Dimension>::GetParameters() const -> const ParametersType &
{
  unsigned int k = 0; // Dummy loop index

  for (unsigned int i = 0; i < Dimension; ++i)
  {
    for (unsigned int j = 0; j < Dimension; ++j)
    {
      this->m_Parameters[k] = this->m_MatrixLogDomain(i, j);
      k += 1;
    }
  }

  for (unsigned int j = 0; j < Dimension; ++j)
  {
    this->m_Parameters[k] = this->GetTranslation()[j];
    k += 1;
  }

  return this->m_Parameters;
}

// SetIdentity
template <class TScalarType, unsigned int Dimension>
void
AffineLogTransform<TScalarType, Dimension>::SetIdentity()
{
  Superclass::SetIdentity();
  this->m_MatrixLogDomain.Fill(ScalarType{});
  this->PrecomputeJacobianOfSpatialJacobian();
}


// Get Jacobian
template <class TScalarType, unsigned int Dimension>
void
AffineLogTransform<TScalarType, Dimension>::GetJacobian(const InputPointType &       p,
                                                        JacobianType &               j,
                                                        NonZeroJacobianIndicesType & nzji) const
{
  unsigned int d = Dimension;

  j.set_size(d, ParametersDimension);
  j.fill(ScalarType{});

  const JacobianOfSpatialJacobianType & jsj = this->m_JacobianOfSpatialJacobian;
  const InputVectorType                 pp = p - this->GetCenter();
  for (unsigned int dim = 0; dim < d * d; ++dim)
  {
    const InputVectorType column = jsj[dim] * pp;
    for (unsigned int i = 0; i < d; ++i)
    {
      j(i, dim) = column[i];
    }
  }

  // compute derivatives for the translation part
  const unsigned int blockOffset = d * d;
  for (unsigned int dim = 0; dim < Dimension; ++dim)
  {
    j[dim][blockOffset + dim] = 1.0;
  }

  nzji = this->m_NonZeroJacobianIndices;
}


// Precompute Jacobian of Spatial Jacobian
template <class TScalarType, unsigned int Dimension>
void
AffineLogTransform<TScalarType, Dimension>::PrecomputeJacobianOfSpatialJacobian()
{
  unsigned int d = Dimension;

  /** The Jacobian of spatial Jacobian is constant over inputspace, so is precomputed */
  JacobianOfSpatialJacobianType & jsj = this->m_JacobianOfSpatialJacobian;

  jsj.resize(ParametersDimension);

  vnl_matrix<ScalarType> dA(d, d);

  vnl_matrix<ScalarType> dummymatrix(d, d);

  vnl_matrix<ScalarType> A_bar(2 * d, 2 * d);

  vnl_matrix<ScalarType> B_bar(2 * d, 2 * d);

  dA.fill(ScalarType{});
  dummymatrix.fill(ScalarType{});
  A_bar.fill(ScalarType{});

  // Fill A_bar top left and bottom right with A
  for (unsigned int k = 0; k < d; ++k)
  {
    for (unsigned int l = 0; l < d; ++l)
    {
      A_bar(k, l) = this->m_MatrixLogDomain(k, l);
    }
  }
  for (unsigned int k = d; k < 2 * d; ++k)
  {
    for (unsigned int l = d; l < 2 * d; ++l)
    {
      A_bar(k, l) = this->m_MatrixLogDomain(k - d, l - d);
    }
  }

  unsigned int m = 0; // Dummy loop index

  // Non-translation derivatives
  for (unsigned int i = 0; i < d; ++i)
  {
    for (unsigned int j = 0; j < d; ++j)
    {
      dA(i, j) = 1;
      for (unsigned int k = 0; k < d; ++k)
      {
        for (unsigned int l = d; l < 2 * d; ++l)
        {
          A_bar(k, l) = dA(k, (l - d));
        }
      }
      B_bar = vnl_matrix_exp(A_bar);
      for (unsigned int k = 0; k < d; ++k)
      {
        for (unsigned int l = d; l < 2 * d; ++l)
        {
          dummymatrix(k, (l - d)) = B_bar(k, l);
        }
      }
      jsj[m] = dummymatrix;
      dA.fill(ScalarType{});
      m += 1;
    }
  }

  /** Translation parameters: */
  for (unsigned int par = d * d; par < ParametersDimension; ++par)
  {
    jsj[par].Fill(ScalarType{});
  }
}


// Print self
template <class TScalarType, unsigned int Dimension>
void
AffineLogTransform<TScalarType, Dimension>::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);

  os << indent << "parameters:" << this->m_Parameters << std::endl;
}


} // namespace itk

#endif // itkAffineLogTransform_hxx