File: itkBSplineTransformGTest.cxx

package info (click to toggle)
insighttoolkit5 5.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704,404 kB
  • sloc: cpp: 783,697; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 461; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (207 lines) | stat: -rw-r--r-- 8,502 bytes parent folder | download | duplicates (2)
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
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  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
 *
 *         https://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.
 *
 *=========================================================================*/

#define ITK_LEGACY_TEST
#include "itkGTest.h"
#include "itkBSplineTransform.h"

#include "itkImageRegionConstIterator.h"

namespace
{


// Method which checks that two BSpline are exactly equal
template <typename TParametersValueType, unsigned int VDimension, unsigned int VSplineOrder>
void
bspline_eq(const itk::BSplineTransform<TParametersValueType, VDimension, VSplineOrder> * bspline1,
           const itk::BSplineTransform<TParametersValueType, VDimension, VSplineOrder> * bspline2,
           const std::string &                                                           description = "",
           double                                                                        tolerance = 1e-15)
{
  using BSplineType = itk::BSplineTransform<TParametersValueType, VDimension, VSplineOrder>;
  // Compare Transform Domain interface

  EXPECT_EQ(bspline1->GetNumberOfFixedParameters(), bspline2->GetNumberOfFixedParameters()) << description;
  ITK_EXPECT_VECTOR_NEAR(bspline1->GetTransformDomainOrigin(), bspline2->GetTransformDomainOrigin(), tolerance)
    << description;
  EXPECT_EQ(bspline1->GetTransformDomainDirection(), bspline2->GetTransformDomainDirection()) << description;
  EXPECT_EQ(bspline1->GetTransformDomainMeshSize(), bspline2->GetTransformDomainMeshSize()) << description;
  ITK_EXPECT_VECTOR_NEAR(
    bspline1->GetTransformDomainPhysicalDimensions(), bspline2->GetTransformDomainPhysicalDimensions(), tolerance)
    << description;

  // check transform parameters interface
  ITK_EXPECT_VECTOR_NEAR(bspline1->GetParameters(), bspline2->GetParameters(), tolerance) << description;
  ITK_EXPECT_VECTOR_NEAR(bspline1->GetFixedParameters(), bspline2->GetFixedParameters(), tolerance) << description;

  ASSERT_EQ(bspline1->GetCoefficientImages().Size(), VDimension) << description;
  ASSERT_EQ(bspline2->GetCoefficientImages().Size(), VDimension) << description;

  for (unsigned int i = 0; i < VDimension; ++i)
  {
    using ImageType = typename BSplineType::ImageType;
    using ImageConstIterator = typename itk::ImageRegionConstIterator<ImageType>;

    typename ImageType::Pointer coeffImage1 = bspline1->GetCoefficientImages()[i];
    typename ImageType::Pointer coeffImage2 = bspline2->GetCoefficientImages()[i];

    ITK_EXPECT_VECTOR_NEAR(coeffImage1->GetOrigin(), coeffImage2->GetOrigin(), tolerance) << description;
    EXPECT_EQ(coeffImage1->GetDirection(), coeffImage2->GetDirection()) << description;
    ITK_EXPECT_VECTOR_NEAR(coeffImage1->GetSpacing(), coeffImage2->GetSpacing(), tolerance) << description;
    EXPECT_EQ(coeffImage1->GetLargestPossibleRegion(), coeffImage2->GetLargestPossibleRegion()) << description;
    EXPECT_EQ(coeffImage1->GetBufferedRegion(), coeffImage2->GetBufferedRegion()) << description;

    ImageConstIterator iter1(coeffImage1, coeffImage1->GetBufferedRegion());
    ImageConstIterator iter2(coeffImage2, coeffImage2->GetBufferedRegion());


    while (!iter1.IsAtEnd() && iter2.IsAtEnd())
    {
      EXPECT_EQ(iter1.Get(), iter2.Get()) << description << " Expected value at: " << iter1.GetIndex();
      ++iter1;
      ++iter2;
    }
  }
}

} // namespace

TEST(ITKBSplineTransform, Construction)
{

  using BSplineType = itk::BSplineTransform<double, 3, 3>;

  auto bspline1 = BSplineType::New();
  auto bspline2 = BSplineType::New();

  bspline_eq(bspline1.GetPointer(), bspline1.GetPointer());
  bspline_eq(bspline2.GetPointer(), bspline2.GetPointer());
  bspline_eq(bspline1.GetPointer(), bspline2.GetPointer());
  bspline_eq(bspline2.GetPointer(), bspline1.GetPointer());
}

TEST(ITKBSplineTransform, Copying_Clone)
{
  // This test sets up an non-trivial bspline transform then,
  // test various interface to set the parameters to the a new
  // transform. It validates that the result it the same as the initial

  using namespace itk::GTest::TypedefsAndConstructors::Dimension2;

  using BSplineType = itk::BSplineTransform<double, 2, 3>;
  using ImageType = typename BSplineType::ImageType;
  using ImageIterator = typename itk::ImageRegionIterator<ImageType>;

  typename BSplineType::CoefficientImageArray coeffImageArray;

  ASSERT_EQ(coeffImageArray.Size(), 2);

  SizeType      imageSize = itk::MakeSize(10, 10);
  DirectionType imageDirection; // filled with zeros
  imageDirection(0, 1) = -1;
  imageDirection(1, 0) = 1;

  VectorType imageSpacing = itk::MakeVector(1.1, 1.2);

  PointType imageOrigin = itk::MakePoint(0.9, 0.8);

  coeffImageArray[0] = ImageType::New();

  unsigned short px_value = 3;

  ImageType::Pointer coeffImage;
  for (unsigned int i = 0; i < Dimension; ++i)
  {
    coeffImageArray[i] = ImageType::New();
    coeffImage = coeffImageArray[i];

    coeffImage->SetRegions(RegionType(imageSize));

    coeffImage->SetSpacing(imageSpacing);
    coeffImage->SetOrigin(imageOrigin);
    coeffImage->SetDirection(imageDirection);

    coeffImage->Allocate();

    ImageIterator iter(coeffImage, coeffImage->GetBufferedRegion());


    while (!iter.IsAtEnd())
    {
      iter.Set(px_value++);
      ++iter;
    }
  }

  auto bspline1 = BSplineType::New();
  bspline1->SetCoefficientImages(coeffImageArray);

  bspline_eq(bspline1.GetPointer(), bspline1.GetPointer(), "Check after initialization by coefficient images.");

  ITK_EXPECT_VECTOR_NEAR(bspline1->GetTransformDomainOrigin(), itk::MakePoint(-0.3, 1.9), 1e-15);
  EXPECT_EQ(bspline1->GetTransformDomainDirection(), imageDirection);
  EXPECT_EQ(bspline1->GetTransformDomainMeshSize(), itk::MakeSize(7, 7));
  ITK_EXPECT_VECTOR_NEAR(bspline1->GetTransformDomainPhysicalDimensions(), itk::MakeVector(7.7, 8.4), 1e-15);

  auto bspline2 = BSplineType::New();
  bspline2->SetFixedParameters(bspline1->GetFixedParameters());
  bspline2->SetParameters(bspline1->GetParameters());

  bspline_eq(bspline1.GetPointer(), bspline2.GetPointer(), "Copying by parameters.");


  bspline2 = BSplineType::New();
  bspline2->SetTransformDomainOrigin(bspline1->GetTransformDomainOrigin());
  bspline2->SetTransformDomainPhysicalDimensions(bspline1->GetTransformDomainPhysicalDimensions());
  bspline2->SetTransformDomainDirection(bspline1->GetTransformDomainDirection());
  bspline2->SetTransformDomainMeshSize(bspline1->GetTransformDomainMeshSize());
  bspline2->SetParameters(bspline1->GetParameters());

  bspline_eq(bspline1.GetPointer(), bspline2.GetPointer(), "Set by transform domain then by parameters.");


  bspline2 = bspline1->Clone();
  bspline_eq(bspline1.GetPointer(), bspline2.GetPointer(), "Clone");
}


TEST(ITKBSplineTransform, NumberOfWeights)
{
  const auto testNumberOfWeights = [](const auto & bsplineTransform) {
    using BSplineTransformType = std::remove_reference_t<decltype(bsplineTransform)>;

    constexpr auto actualNumberOfWeights = BSplineTransformType::NumberOfWeights;

#ifndef ITK_LEGACY_REMOVE
    EXPECT_EQ(actualNumberOfWeights, bsplineTransform.GetNumberOfWeights());
#endif

    // Expect the actual value of `NumberOfWeights` to be equal to the value of `m_NumberOfWeights` from ITK 5.2 (before
    // the introduction of `BSplineTransform::NumberOfWeights`), as was originally initialized at
    // https://github.com/InsightSoftwareConsortium/ITK/blob/v5.2.0/Modules/Core/Common/include/itkBSplineInterpolationWeightFunction.hxx#L35
    EXPECT_EQ(actualNumberOfWeights,
              static_cast<unsigned int>(std::pow(static_cast<double>(BSplineTransformType::SplineOrder + 1),
                                                 static_cast<double>(BSplineTransformType::SpaceDimension))));
  };

  testNumberOfWeights(*itk::BSplineTransform<>::New());
  testNumberOfWeights(*itk::BSplineTransform<float>::New());
  testNumberOfWeights(*itk::BSplineTransform<float, 2>::New());
  testNumberOfWeights(*itk::BSplineTransform<float, 2, 2>::New());
}