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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
|
/*=========================================================================
*
* 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.
*
*=========================================================================*/
// First include the header file to be tested:
#include "elxTransformIO.h"
#include "elxConversion.h"
#include "elxDefaultConstruct.h"
#include "elxElastixTemplate.h"
#include "elxGTestUtilities.h"
#include "../Core/Main/GTesting/elxCoreMainGTestUtilities.h"
#include "AdvancedAffineTransform/elxAdvancedAffineTransform.h"
#include "AdvancedBSplineTransform/elxAdvancedBSplineTransform.h"
#include "AffineDTITransform/elxAffineDTITransform.h"
#include "AffineLogStackTransform/elxAffineLogStackTransform.h"
#include "AffineLogTransform/elxAffineLogTransform.h"
#include "BSplineDeformableTransformWithDiffusion/elxBSplineTransformWithDiffusion.h"
#include "BSplineStackTransform/elxBSplineStackTransform.h"
#include "DeformationFieldTransform/elxDeformationFieldTransform.h"
#include "EulerStackTransform/elxEulerStackTransform.h"
#include "EulerTransform/elxEulerTransform.h"
#include "MultiBSplineTransformWithNormal/elxMultiBSplineTransformWithNormal.h"
#include "RecursiveBSplineTransform/elxRecursiveBSplineTransform.h"
#include "SimilarityTransform/elxSimilarityTransform.h"
#include "SplineKernelTransform/elxSplineKernelTransform.h"
#include "TranslationStackTransform/elxTranslationStackTransform.h"
#include "TranslationTransform/elxTranslationTransform.h"
#include "WeightedCombinationTransform/elxWeightedCombinationTransform.h"
#include <itkAffineTransform.h>
#include <itkBSplineTransform.h>
#include <itkCompositeTransform.h>
#include <itkEuler2DTransform.h>
#include <itkEuler3DTransform.h>
#include <itkSimilarity2DTransform.h>
#include <itkSimilarity3DTransform.h>
#include <itkTranslationTransform.h>
#include <itkImage.h>
#include <itkIndexRange.h>
#include <itkVector.h>
#include <itksys/SystemTools.hxx>
#include <cmath> // For M_PI_4.
#include <typeinfo>
#include <type_traits> // For extent, is_base_of and is_same
#include <gtest/gtest.h>
using ParameterValuesType = itk::ParameterFileParser::ParameterValuesType;
using ParameterMapType = itk::ParameterFileParser::ParameterMapType;
// Using-declarations:
using elx::CoreMainGTestUtilities::CheckNew;
using elx::GTestUtilities::CreateDefaultElastixObject;
using elx::GTestUtilities::ExpectAllKeysUnique;
using elx::GTestUtilities::GeneratePseudoRandomParameters;
using elx::GTestUtilities::MakeMergedMap;
namespace
{
template <unsigned NDimension>
using ElastixType = elx::ElastixTemplate<itk::Image<float, NDimension>, itk::Image<float, NDimension>>;
template <std::size_t N>
using ExpectedParameters = std::array<double, N>;
// All tests specific to a dimension.
template <unsigned NDimension>
struct WithDimension
{
template <template <typename> class TElastixTransform>
struct WithElastixTransform
{
using ElastixTransformType = TElastixTransform<ElastixType<NDimension>>;
template <typename TExpectedCorrespondingItkTransform>
static void
Expect_CorrespondingItkCompositeTransform()
{
using ItkCompositeTransformType = itk::CompositeTransform<double, NDimension>;
static_assert(
std::is_base_of_v<itk::Transform<double, NDimension, NDimension>, TExpectedCorrespondingItkTransform>,
"TExpectedCorrespondingItkTransform should be derived from the expected `itk::Transform` specialization!");
static_assert(!std::is_base_of_v<ItkCompositeTransformType, TExpectedCorrespondingItkTransform>,
"TExpectedCorrespondingItkTransform should not be derived from `itk::CompositeTransform`!");
const auto elxTransform = CheckNew<ElastixTransformType>();
EXPECT_EQ(elxTransform->elxGetClassName(),
elx::TransformIO::ConvertITKNameOfClassToElastixClassName(
elx::DefaultConstruct<TExpectedCorrespondingItkTransform>{}.GetNameOfClass()));
const auto compositeTransform = elx::TransformIO::ConvertToItkCompositeTransform(*elxTransform);
ASSERT_NE(compositeTransform, nullptr);
static_assert(std::is_same_v<const itk::SmartPointer<ItkCompositeTransformType>, decltype(compositeTransform)>,
"`ConvertToItkCompositeTransform` should have the expected `SmartPointer` return type!");
const auto & transformQueue = compositeTransform->GetTransformQueue();
ASSERT_EQ(transformQueue.size(), 1);
const auto & itkTransform = transformQueue.front();
ASSERT_NE(itkTransform, nullptr);
const auto & actualItkTransformTypeId = typeid(*itkTransform);
const auto & expectedItkTransformTypeId = typeid(TExpectedCorrespondingItkTransform);
ASSERT_EQ(std::string(actualItkTransformTypeId.name()), std::string(expectedItkTransformTypeId.name()));
EXPECT_EQ(actualItkTransformTypeId, expectedItkTransformTypeId);
}
static void
Expect_default_elastix_FixedParameters_empty()
{
const auto fixedParameters = CheckNew<ElastixTransformType>()->GetFixedParameters();
ASSERT_EQ(fixedParameters, vnl_vector<double>());
}
static void
Expect_default_elastix_FixedParameters_are_all_zero()
{
const auto fixedParameters = CheckNew<ElastixTransformType>()->GetFixedParameters();
for (const auto fixedParameter : fixedParameters)
{
EXPECT_EQ(fixedParameter, 0.0);
}
}
static void
Expect_default_elastix_Parameters_remain_the_same_when_set(const bool fixed)
{
SCOPED_TRACE(std::string("Function = ")
.append(__func__)
.append("\n ElastixTransformType = ")
.append(typeid(ElastixTransformType).name())
.append("\n fixed = ")
.append(elx::Conversion::BoolToString(fixed)));
const auto transform = CheckNew<ElastixTransformType>();
const auto parameters = elx::TransformIO::GetParameters(fixed, *transform);
elx::TransformIO::SetParameters(fixed, *transform, parameters);
ASSERT_EQ(elx::TransformIO::GetParameters(fixed, *transform), parameters);
}
template <typename TExpectedCorrespondingItkTransform>
static void
Test_copying_default_parameters(const bool fixed)
{
SCOPED_TRACE(std::string("Function = ")
.append(__func__)
.append("\n ElastixTransformType = ")
.append(typeid(ElastixTransformType).name())
.append("\n TExpectedCorrespondingItkTransform = ")
.append(typeid(TExpectedCorrespondingItkTransform).name())
.append("\n fixed = ")
.append(elx::Conversion::BoolToString(fixed)));
const auto elxTransform = CheckNew<ElastixTransformType>();
SCOPED_TRACE(fixed);
const auto compositeTransform = elx::TransformIO::ConvertToItkCompositeTransform(*elxTransform);
ASSERT_NE(compositeTransform, nullptr);
const auto & transformQueue = compositeTransform->GetTransformQueue();
ASSERT_EQ(transformQueue.size(), 1);
const auto & itkTransform = transformQueue.front();
ASSERT_NE(itkTransform, nullptr);
const auto parameters = elx::TransformIO::GetParameters(fixed, *elxTransform);
elx::TransformIO::SetParameters(fixed, *itkTransform, parameters);
ASSERT_EQ(elx::TransformIO::GetParameters(fixed, *itkTransform), parameters);
}
template <typename TExpectedCorrespondingItkTransform>
static void
Test_copying_parameters()
{
const auto elxTransform = CheckNew<ElastixTransformType>();
SCOPED_TRACE(elxTransform->elxGetClassName());
const auto compositeTransform = elx::TransformIO::ConvertToItkCompositeTransform(*elxTransform);
ASSERT_NE(compositeTransform, nullptr);
const auto & transformQueue = compositeTransform->GetTransformQueue();
ASSERT_EQ(transformQueue.size(), 1);
const auto & itkTransform = transformQueue.front();
ASSERT_NE(itkTransform, nullptr);
const auto & actualItkTransformTypeId = typeid(*itkTransform);
const auto & expectedItkTransformTypeId = typeid(TExpectedCorrespondingItkTransform);
ASSERT_EQ(std::string(actualItkTransformTypeId.name()), std::string(expectedItkTransformTypeId.name()));
EXPECT_EQ(actualItkTransformTypeId, expectedItkTransformTypeId);
auto parameters = elxTransform->GetParameters();
std::iota(std::begin(parameters), std::end(parameters), 1.0);
std::for_each(std::begin(parameters), std::end(parameters), [](double & x) { x /= 8; });
elxTransform->SetParameters(parameters);
ASSERT_EQ(elxTransform->GetParameters(), parameters);
auto fixedParameters = elxTransform->GetFixedParameters();
std::iota(std::begin(fixedParameters), std::end(fixedParameters), 1.0);
elxTransform->SetFixedParameters(fixedParameters);
ASSERT_EQ(elxTransform->GetFixedParameters(), fixedParameters);
itkTransform->SetParameters(parameters);
itkTransform->SetFixedParameters(fixedParameters);
ASSERT_EQ(itkTransform->GetParameters(), parameters);
ASSERT_EQ(itkTransform->GetFixedParameters(), fixedParameters);
}
static void
Test_CreateTransformParameterMap_for_default_transform(const ParameterMapType & expectedDerivedParameterMap)
{
SCOPED_TRACE(std::string("Function = ")
.append(__func__)
.append("\n ElastixTransformType = ")
.append(typeid(ElastixTransformType).name()));
const auto elxTransform = CheckNew<ElastixTransformType>();
const auto elastixObject = CreateDefaultElastixObject<ElastixType<NDimension>>();
// Note: SetElastix does not take or share the ownership of its argument!
elxTransform->SetElastix(elastixObject);
// BeforeAll() appears necessary to for MultiBSplineTransformWithNormal
// and AdvancedBSplineTransform to initialize the internal ITK
// transform of the elastix transform, by calling
// InitializeBSplineTransform()
elxTransform->BeforeAll();
// Overrule the default-constructors of BSplineTransformWithDiffusion
// and DeformationFieldTransform which do SetReadWriteTransformParameters(false)
elxTransform->SetReadWriteTransformParameters(true);
ParameterMapType actualParameterMap;
elxTransform->CreateTransformParameterMap(itk::OptimizerParameters<double>{}, actualParameterMap);
const std::string expectedImageDimension{ char{ '0' + NDimension } };
const std::string expectedInternalImagePixelType = "float";
const std::string expectedZero = "0";
const std::string expectedOne = "1";
const ParameterMapType expectedBaseParameterMap = {
{ "Direction", ParameterValuesType(NDimension * NDimension, expectedZero) },
{ "FixedImageDimension", { expectedImageDimension } },
{ "FixedInternalImagePixelType", { expectedInternalImagePixelType } },
{ "HowToCombineTransforms", { "Compose" } },
{ "Index", { ParameterValuesType(NDimension, expectedZero) } },
{ "InitialTransformParameterFileName", { "NoInitialTransform" } },
{ "MovingImageDimension", { expectedImageDimension } },
{ "MovingInternalImagePixelType", { expectedInternalImagePixelType } },
{ "NumberOfParameters", { expectedZero } },
{ "Origin", { ParameterValuesType(NDimension, expectedZero) } },
{ "Size", { ParameterValuesType(NDimension, expectedZero) } },
{ "Spacing", { ParameterValuesType(NDimension, expectedOne) } },
{ "Transform", { elxTransform->elxGetClassName() } },
{ "TransformParameters", {} },
{ "UseDirectionCosines", { "true" } }
};
ExpectAllKeysUnique(expectedDerivedParameterMap, expectedBaseParameterMap);
EXPECT_EQ(actualParameterMap, MakeMergedMap(expectedDerivedParameterMap, expectedBaseParameterMap));
}
static void
Test_CreateTransformParameterMap_double_precision()
{
// Use 0.3333333333333333... as test value.
static constexpr auto testValue = 1.0 / 3.0;
static constexpr auto expectedPrecision = 16;
static_assert(expectedPrecision == std::numeric_limits<double>::digits10 + 1,
"The expected precision for double floating point numbers");
const auto expectedString = "0." + std::string(expectedPrecision, '3');
const auto elastixObject = CheckNew<ElastixType<NDimension>>();
elastixObject->SetConfiguration(CheckNew<elx::Configuration>());
const auto imageContainer = elx::ElastixBase::DataObjectContainerType::New();
const auto image = itk::Image<float, NDimension>::New();
image->SetOrigin(itk::Point<double, NDimension>(testValue));
image->SetSpacing(itk::Vector<double, NDimension>(testValue));
imageContainer->push_back(image);
elastixObject->SetFixedImageContainer(imageContainer);
elastixObject->SetMovingImageContainer(imageContainer);
const auto elxTransform = CheckNew<ElastixTransformType>();
elxTransform->SetElastix(elastixObject);
elxTransform->BeforeAll();
ParameterMapType parameterMap;
elxTransform->CreateTransformParameterMap(itk::OptimizerParameters<double>(2U, testValue), parameterMap);
for (const auto key : { "TransformParameters", "Origin", "Spacing" })
{
const auto found = parameterMap.find(key);
ASSERT_NE(found, end(parameterMap));
for (const auto & actualString : found->second)
{
EXPECT_EQ(actualString, expectedString);
}
}
}
static void
Test_CreateTransformParameterMap_SetUseAddition()
{
const auto elxTransform = CheckNew<ElastixTransformType>();
const auto elastixObject = CreateDefaultElastixObject<ElastixType<NDimension>>();
// Note: SetElastix does not take or share the ownership of its argument!
elxTransform->SetElastix(elastixObject);
elxTransform->BeforeAll();
const auto expectHowToCombineTransforms = [&elxTransform](const char * const expectedParameterValue) {
ParameterMapType parameterMap;
elxTransform->CreateTransformParameterMap({}, parameterMap);
const auto found = parameterMap.find("HowToCombineTransforms");
ASSERT_NE(found, end(parameterMap));
EXPECT_EQ(found->second, ParameterValuesType{ expectedParameterValue });
};
expectHowToCombineTransforms("Compose");
elxTransform->SetUseAddition(true);
expectHowToCombineTransforms("Add");
elxTransform->SetUseAddition(false);
expectHowToCombineTransforms("Compose");
}
};
static void
Expect_default_AdvancedBSplineTransform_GetParameters_throws_ExceptionObject(const bool fixed)
{
const auto transform = CheckNew<elx::AdvancedBSplineTransform<ElastixType<NDimension>>>();
EXPECT_THROW(elx::TransformIO::GetParameters(fixed, *transform), itk::ExceptionObject);
}
template <template <typename> class TElastixTransform, std::size_t NExpectedParameters>
static void
Expect_default_elastix_Parameters_equal(const ExpectedParameters<NExpectedParameters> & expectedParameters)
{
using ElastixTransformType = TElastixTransform<ElastixType<NDimension>>;
SCOPED_TRACE(std::string("Function = ")
.append(__func__)
.append("\n ElastixTransformType = ")
.append(typeid(ElastixTransformType).name()));
const auto parameters = CheckNew<ElastixTransformType>()->GetParameters();
ASSERT_EQ(parameters, vnl_vector<double>(expectedParameters.data(), NExpectedParameters));
}
static void
Test_CreateTransformParameterMap_for_default_transform()
{
// Converts the specified string to an std::vector<std::string>. Each vector element contains a character of the
// specified string.
const auto toVectorOfStrings = [](const std::string & str) {
std::vector<std::string> result;
for (const char ch : str)
{
result.push_back({ ch });
}
return result;
};
// Concatenates n times the specified string.
const auto times = [](const unsigned n, const std::string & str) {
std::string result;
for (auto i = n; i > 0; --i)
{
result += str;
}
return result;
};
const std::string expectedFalse("false");
const std::string expectedZero("0");
const std::string expectedOne("1");
const ParameterValuesType expectedZeros(NDimension, "0");
const ParameterValuesType expectedOnes(NDimension, "1");
const auto expectedMatrixTranslation = toVectorOfStrings(times(NDimension, '1' + std::string(NDimension, '0')));
const auto expectedGridDirection =
toVectorOfStrings(times(NDimension - 1, '1' + std::string(NDimension, '0')) + '1');
const std::string expectedDeformationFieldFileName("DeformationFieldImage.mhd");
using namespace elx;
WithElastixTransform<AdvancedAffineTransformElastix>::Test_CreateTransformParameterMap_for_default_transform(
{ { "CenterOfRotationPoint", expectedZeros } });
WithElastixTransform<AdvancedBSplineTransform>::Test_CreateTransformParameterMap_for_default_transform(
{ { "BSplineTransformSplineOrder", { "3" } },
{ "GridDirection", expectedGridDirection },
{ "GridIndex", expectedZeros },
{ "GridOrigin", expectedZeros },
{ "GridSize", expectedZeros },
{ "GridSpacing", expectedOnes },
{ "UseCyclicTransform", { "false" } } });
WithElastixTransform<AffineDTITransformElastix>::Test_CreateTransformParameterMap_for_default_transform(
{ { "CenterOfRotationPoint", expectedZeros }, { "MatrixTranslation", expectedMatrixTranslation } });
WithElastixTransform<AffineLogStackTransform>::Test_CreateTransformParameterMap_for_default_transform(
{ { "CenterOfRotationPoint", ParameterValuesType(NDimension - 1, expectedZero) },
{ "NumberOfSubTransforms", { expectedZero } },
{ "StackOrigin", { expectedZero } },
{ "StackSpacing", { expectedOne } } });
WithElastixTransform<AffineLogTransformElastix>::Test_CreateTransformParameterMap_for_default_transform(
{ { "CenterOfRotationPoint", expectedZeros }, { "MatrixTranslation", expectedMatrixTranslation } });
const auto skippedTest = [] {
// Appears to crash when internally calling GetSubTransform(0) while m_SubTransformContainer is still empty.
WithElastixTransform<BSplineStackTransform>::Test_CreateTransformParameterMap_for_default_transform({});
};
(void)skippedTest;
WithElastixTransform<BSplineTransformWithDiffusion>::Test_CreateTransformParameterMap_for_default_transform(
{ { "DeformationFieldFileName", { expectedDeformationFieldFileName } },
{ "GridIndex", expectedZeros },
{ "GridOrigin", expectedZeros },
{ "GridSize", expectedZeros },
{ "GridSpacing", expectedOnes } });
WithElastixTransform<DeformationFieldTransform>::Test_CreateTransformParameterMap_for_default_transform(
{ { "DeformationFieldFileName", { expectedDeformationFieldFileName } },
{ "DeformationFieldInterpolationOrder", { expectedZero } } });
WithElastixTransform<EulerStackTransform>::Test_CreateTransformParameterMap_for_default_transform(
(NDimension == 4)
? ParameterMapType{ { "CenterOfRotationPoint", ParameterValuesType(NDimension - 1, expectedZero) },
{ "ComputeZYX", { expectedFalse } },
{ "NumberOfSubTransforms", { expectedZero } },
{ "StackOrigin", { expectedZero } },
{ "StackSpacing", { expectedOne } } }
: ParameterMapType{ { "CenterOfRotationPoint", ParameterValuesType(NDimension - 1, expectedZero) },
{ "NumberOfSubTransforms", { expectedZero } },
{ "StackOrigin", { expectedZero } },
{ "StackSpacing", { expectedOne } } });
WithElastixTransform<EulerTransformElastix>::Test_CreateTransformParameterMap_for_default_transform(
(NDimension == 3)
? ParameterMapType{ { "CenterOfRotationPoint", expectedZeros }, { "ComputeZYX", { expectedFalse } } }
: ParameterMapType{ { "CenterOfRotationPoint", expectedZeros } });
try
{
WithElastixTransform<MultiBSplineTransformWithNormal>::Test_CreateTransformParameterMap_for_default_transform(
{ { "GridIndex", expectedZeros },
{ "GridOrigin", expectedZeros },
{ "GridSize", expectedZeros },
{ "GridSpacing", expectedOnes },
{ "BSplineTransformSplineOrder", { "3" } },
{ "GridDirection", expectedGridDirection },
{ "MultiBSplineTransformWithNormalLabels", { itksys::SystemTools::GetCurrentWorkingDirectory() } } });
EXPECT_FALSE("MultiBSplineTransformWithNormal::CreateTransformParameterMap is expected to throw an exception!");
}
catch (const itk::ExceptionObject & exceptionObject)
{
// TODO Avoid this exception!
EXPECT_NE(std::strstr(exceptionObject.GetDescription(), "ERROR: Missing -labels argument!"), nullptr);
}
WithElastixTransform<RecursiveBSplineTransform>::Test_CreateTransformParameterMap_for_default_transform(
{ { "BSplineTransformSplineOrder", { "3" } },
{ "GridDirection", expectedGridDirection },
{ "GridIndex", expectedZeros },
{ "GridOrigin", expectedZeros },
{ "GridSize", expectedZeros },
{ "GridSpacing", expectedOnes },
{ "UseCyclicTransform", { expectedFalse } } });
WithElastixTransform<SimilarityTransformElastix>::Test_CreateTransformParameterMap_for_default_transform(
{ { "CenterOfRotationPoint", expectedZeros } });
WithElastixTransform<SplineKernelTransform>::Test_CreateTransformParameterMap_for_default_transform(
{ { "FixedImageLandmarks", {} },
{ "SplineKernelType", { "unknown" } },
{ "SplinePoissonRatio", { "0.3" } },
{ "SplineRelaxationFactor", { expectedZero } } });
WithElastixTransform<TranslationStackTransform>::Test_CreateTransformParameterMap_for_default_transform(
{ { "NumberOfSubTransforms", { expectedZero } },
{ "StackOrigin", { expectedZero } },
{ "StackSpacing", { expectedOne } } });
WithElastixTransform<TranslationTransformElastix>::Test_CreateTransformParameterMap_for_default_transform({});
WithElastixTransform<WeightedCombinationTransformElastix>::Test_CreateTransformParameterMap_for_default_transform(
{ { "NormalizeCombinationWeights", { expectedFalse } }, { "SubTransforms", {} } });
}
};
template <template <typename> class TElastixTransform>
void
Expect_default_elastix_FixedParameters_are_all_zero()
{
WithDimension<2>::WithElastixTransform<TElastixTransform>::Expect_default_elastix_FixedParameters_are_all_zero();
WithDimension<3>::WithElastixTransform<TElastixTransform>::Expect_default_elastix_FixedParameters_are_all_zero();
WithDimension<4>::WithElastixTransform<TElastixTransform>::Expect_default_elastix_FixedParameters_are_all_zero();
}
template <template <typename> class TElastixTransform>
void
Expect_default_elastix_FixedParameters_empty()
{
WithDimension<2>::WithElastixTransform<TElastixTransform>::Expect_default_elastix_FixedParameters_empty();
WithDimension<3>::WithElastixTransform<TElastixTransform>::Expect_default_elastix_FixedParameters_empty();
WithDimension<4>::WithElastixTransform<TElastixTransform>::Expect_default_elastix_FixedParameters_empty();
}
template <template <typename> class TElastixTransform>
void
Expect_default_elastix_Parameters_remain_the_same_when_set(const bool fixed)
{
WithDimension<2>::WithElastixTransform<TElastixTransform>::Expect_default_elastix_Parameters_remain_the_same_when_set(
fixed);
WithDimension<3>::WithElastixTransform<TElastixTransform>::Expect_default_elastix_Parameters_remain_the_same_when_set(
fixed);
WithDimension<4>::WithElastixTransform<TElastixTransform>::Expect_default_elastix_Parameters_remain_the_same_when_set(
fixed);
}
template <template <typename> class TElastixTransform, typename TITKTransform>
void
Expect_elx_TransformPoint_yields_same_point_as_ITK(const TITKTransform & itkTransform)
{
const auto Dimension = TITKTransform::SpaceDimension;
const auto elastixObject = CreateDefaultElastixObject<ElastixType<Dimension>>();
const auto elxTransform = CheckNew<TElastixTransform<ElastixType<Dimension>>>();
const std::string elxClassName = elxTransform->elxGetClassName();
const std::string itkNameOfClass = itkTransform.GetNameOfClass();
if (!((elxClassName == "BSplineTransform") && (itkNameOfClass == elxClassName)))
{
// Check that the elastix transform type corresponds with the ITK transform type.
EXPECT_EQ(elxClassName, elx::TransformIO::ConvertITKNameOfClassToElastixClassName(itkNameOfClass));
}
// Note: SetElastix does not take or share the ownership of its argument!
elxTransform->SetElastix(elastixObject);
// Necessary for AdvancedBSplineTransform, to avoid an exception, saying
// "No current transform set in the AdvancedCombinationTransform".
elxTransform->BeforeAll();
// SetFixedParameters before SetParameters, to avoid an exception from
// AdvancedBSplineTransform, saying "AdvancedBSplineDeformableTransform:
// Mismatched between parameters size 32 and region size 0"
elxTransform->SetFixedParameters(itkTransform.GetFixedParameters());
elxTransform->SetParameters(itkTransform.GetParameters());
unsigned numberOfTimesOutputDiffersFromInput{};
unsigned numberOfTimesOutputIsNonZero{};
using NumericLimits = std::numeric_limits<double>;
static constexpr double testInputValues[] = { NumericLimits::lowest(),
-2.0,
-1.0,
-0.5,
-NumericLimits::min(),
-0.0,
0.0,
NumericLimits::min(),
0.5,
1.0 - (2 * NumericLimits::epsilon()), // Note: 1.0 fails on BSpline!!!
1.0 + 1.0e-14,
2.0,
NumericLimits::max() };
static constexpr auto numberOfTestInputValues = std::extent_v<decltype(testInputValues)>;
// Use the test input values as coordinates.
for (const auto index : itk::ZeroBasedIndexRange<Dimension>(itk::Size<Dimension>::Filled(numberOfTestInputValues)))
{
itk::Point<double, Dimension> inputPoint;
std::transform(index.begin(), index.end(), inputPoint.begin(), [](const itk::SizeValueType value) {
return testInputValues[value];
});
const auto expectedOutputPoint = itkTransform.TransformPoint(inputPoint);
const auto actualOutputPoint = elxTransform->TransformPoint(inputPoint);
static_assert(std::is_same_v<decltype(actualOutputPoint), decltype(expectedOutputPoint)>,
"elxTransform->TransformPoint must have the expected return type!");
if (expectedOutputPoint != inputPoint)
{
++numberOfTimesOutputDiffersFromInput;
}
if (expectedOutputPoint != itk::Point<double, Dimension>())
{
++numberOfTimesOutputIsNonZero;
}
const auto pointToString = [](const itk::Point<double, Dimension> & point) {
std::ostringstream stream;
stream << point;
return stream.str();
};
if (pointToString(actualOutputPoint) != pointToString(expectedOutputPoint))
{
// Consider two points significantly different when they have different string representations.
EXPECT_EQ(actualOutputPoint, expectedOutputPoint) << " inputPoint = " << inputPoint;
}
}
// If the output point would always equal the input point, either the test
// or the transform might not make much sense.
EXPECT_GT(numberOfTimesOutputDiffersFromInput, 0U);
// If the output point would always be zero (0, 0, 0), again, either the test
// or the transform might not make much sense.
EXPECT_GT(numberOfTimesOutputIsNonZero, 0U);
}
} // namespace
// Tests that elx::TransformIO::ConvertToItkCompositeTransform(elxTransform) yields the expected corresponding
// `itk::CompositeTransform`.
GTEST_TEST(TransformIO, CorrespondingItkCompositeTransform)
{
WithDimension<2>::WithElastixTransform<
elx::AdvancedAffineTransformElastix>::Expect_CorrespondingItkCompositeTransform<itk::AffineTransform<double, 2>>();
WithDimension<3>::WithElastixTransform<
elx::AdvancedAffineTransformElastix>::Expect_CorrespondingItkCompositeTransform<itk::AffineTransform<double, 3>>();
WithDimension<4>::WithElastixTransform<
elx::AdvancedAffineTransformElastix>::Expect_CorrespondingItkCompositeTransform<itk::AffineTransform<double, 4>>();
// Note: This test fails for `elx::AdvancedBSplineTransform` (corresponding with `itk::BSplineTransform`), as it
// produces an `itk::ExceptionObject`: unknown file: error: C++ exception with description
// "<source-directory>\elastix\Common\Transforms\itkAdvancedCombinationTransform.hxx:223: ITK ERROR:
// AdvancedBSplineTransform(0000018BCE262040): No current transform set in the AdvancedCombinationTransform" thrown in
// the test body.
WithDimension<2>::WithElastixTransform<elx::TranslationTransformElastix>::Expect_CorrespondingItkCompositeTransform<
itk::TranslationTransform<double, 2>>();
WithDimension<3>::WithElastixTransform<elx::TranslationTransformElastix>::Expect_CorrespondingItkCompositeTransform<
itk::TranslationTransform<double, 3>>();
WithDimension<4>::WithElastixTransform<elx::TranslationTransformElastix>::Expect_CorrespondingItkCompositeTransform<
itk::TranslationTransform<double, 4>>();
WithDimension<2>::WithElastixTransform<elx::SimilarityTransformElastix>::Expect_CorrespondingItkCompositeTransform<
itk::Similarity2DTransform<double>>();
WithDimension<3>::WithElastixTransform<elx::SimilarityTransformElastix>::Expect_CorrespondingItkCompositeTransform<
itk::Similarity3DTransform<double>>();
WithDimension<2>::WithElastixTransform<elx::EulerTransformElastix>::Expect_CorrespondingItkCompositeTransform<
itk::Euler2DTransform<double>>();
WithDimension<3>::WithElastixTransform<elx::EulerTransformElastix>::Expect_CorrespondingItkCompositeTransform<
itk::Euler3DTransform<double>>();
}
GTEST_TEST(TransformIO, DefaultAdvancedBSplineTransformGetParametersThrowsExceptionObject)
{
for (const bool fixed : { false, true })
{
WithDimension<2>::Expect_default_AdvancedBSplineTransform_GetParameters_throws_ExceptionObject(fixed);
WithDimension<3>::Expect_default_AdvancedBSplineTransform_GetParameters_throws_ExceptionObject(fixed);
}
}
GTEST_TEST(TransformIO, DefaultElastixFixedParametersAreZeroOrEmpty)
{
using namespace elastix;
// Note: This test would fail for AdvancedBSplineTransform, which is related to the test
// DefaultAdvancedBSplineTransformGetParametersThrowsExceptionObject.
Expect_default_elastix_FixedParameters_are_all_zero<AdvancedAffineTransformElastix>();
Expect_default_elastix_FixedParameters_are_all_zero<EulerTransformElastix>();
Expect_default_elastix_FixedParameters_are_all_zero<SimilarityTransformElastix>();
Expect_default_elastix_FixedParameters_empty<TranslationTransformElastix>();
}
GTEST_TEST(TransformIO, DefaultElastixParameters)
{
using namespace elx;
// Note: This test would fail for AdvancedBSplineTransform, which is related to the test
// DefaultAdvancedBSplineTransformGetParametersThrowsExceptionObject.
WithDimension<2>::Expect_default_elastix_Parameters_equal<AdvancedAffineTransformElastix>(
ExpectedParameters<6>{ 1, 0, 0, 1, 0, 0 });
WithDimension<3>::Expect_default_elastix_Parameters_equal<AdvancedAffineTransformElastix>(
ExpectedParameters<12>{ 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 });
WithDimension<4>::Expect_default_elastix_Parameters_equal<AdvancedAffineTransformElastix>(
ExpectedParameters<20>{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 });
WithDimension<2>::Expect_default_elastix_Parameters_equal<EulerTransformElastix>(ExpectedParameters<3>{});
WithDimension<3>::Expect_default_elastix_Parameters_equal<EulerTransformElastix>(ExpectedParameters<6>{});
WithDimension<4>::Expect_default_elastix_Parameters_equal<EulerTransformElastix>(
ExpectedParameters<20>{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 });
WithDimension<2>::Expect_default_elastix_Parameters_equal<SimilarityTransformElastix>(
ExpectedParameters<4>{ 1, 0, 0, 0 });
WithDimension<3>::Expect_default_elastix_Parameters_equal<SimilarityTransformElastix>(
ExpectedParameters<7>{ 0, 0, 0, 0, 0, 0, 1 });
WithDimension<4>::Expect_default_elastix_Parameters_equal<SimilarityTransformElastix>(
ExpectedParameters<20>{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 });
WithDimension<2>::Expect_default_elastix_Parameters_equal<TranslationTransformElastix>(ExpectedParameters<2>{});
WithDimension<3>::Expect_default_elastix_Parameters_equal<TranslationTransformElastix>(ExpectedParameters<3>{});
WithDimension<4>::Expect_default_elastix_Parameters_equal<TranslationTransformElastix>(ExpectedParameters<4>{});
}
GTEST_TEST(TransformIO, DefaultElastixParametersRemainTheSameWhenSet)
{
for (const bool fixed : { false, true })
{
using namespace elx;
// Note: This test would fail for AdvancedBSplineTransform, which is related to the test
// DefaultAdvancedBSplineTransformGetParametersThrowsExceptionObject.
Expect_default_elastix_Parameters_remain_the_same_when_set<AdvancedAffineTransformElastix>(fixed);
Expect_default_elastix_Parameters_remain_the_same_when_set<EulerTransformElastix>(fixed);
Expect_default_elastix_Parameters_remain_the_same_when_set<SimilarityTransformElastix>(fixed);
Expect_default_elastix_Parameters_remain_the_same_when_set<TranslationTransformElastix>(fixed);
}
}
GTEST_TEST(TransformIO, CopyDefaultParametersToCorrespondingItkTransform)
{
for (const bool fixed : { false, true })
{
// Note: This test would fail for elx::AdvancedBSplineTransform, which is related to the test
// DefaultAdvancedBSplineTransformGetParametersThrowsExceptionObject.
WithDimension<2>::WithElastixTransform<elx::AdvancedAffineTransformElastix>::Test_copying_default_parameters<
itk::AffineTransform<double, 2>>(fixed);
WithDimension<3>::WithElastixTransform<elx::AdvancedAffineTransformElastix>::Test_copying_default_parameters<
itk::AffineTransform<double, 3>>(fixed);
WithDimension<4>::WithElastixTransform<elx::AdvancedAffineTransformElastix>::Test_copying_default_parameters<
itk::AffineTransform<double, 4>>(fixed);
WithDimension<2>::WithElastixTransform<elx::TranslationTransformElastix>::Test_copying_default_parameters<
itk::TranslationTransform<double, 2>>(fixed);
WithDimension<3>::WithElastixTransform<elx::TranslationTransformElastix>::Test_copying_default_parameters<
itk::TranslationTransform<double, 3>>(fixed);
WithDimension<4>::WithElastixTransform<elx::TranslationTransformElastix>::Test_copying_default_parameters<
itk::TranslationTransform<double, 4>>(fixed);
WithDimension<2>::WithElastixTransform<elx::SimilarityTransformElastix>::Test_copying_default_parameters<
itk::Similarity2DTransform<double>>(fixed);
WithDimension<3>::WithElastixTransform<elx::SimilarityTransformElastix>::Test_copying_default_parameters<
itk::Similarity3DTransform<double>>(fixed);
WithDimension<2>::WithElastixTransform<elx::SimilarityTransformElastix>::Test_copying_default_parameters<
itk::Euler2DTransform<double>>(fixed);
WithDimension<3>::WithElastixTransform<elx::EulerTransformElastix>::Test_copying_default_parameters<
itk::Euler3DTransform<double>>(fixed);
}
}
GTEST_TEST(TransformIO, CopyParametersToCorrespondingItkTransform)
{
WithDimension<2>::WithElastixTransform<elx::AdvancedAffineTransformElastix>::Test_copying_parameters<
itk::AffineTransform<double, 2>>();
WithDimension<3>::WithElastixTransform<elx::AdvancedAffineTransformElastix>::Test_copying_parameters<
itk::AffineTransform<double, 3>>();
WithDimension<2>::WithElastixTransform<elx::TranslationTransformElastix>::Test_copying_parameters<
itk::TranslationTransform<double, 2>>();
WithDimension<3>::WithElastixTransform<elx::TranslationTransformElastix>::Test_copying_parameters<
itk::TranslationTransform<double, 3>>();
WithDimension<2>::WithElastixTransform<elx::SimilarityTransformElastix>::Test_copying_parameters<
itk::Similarity2DTransform<double>>();
WithDimension<3>::WithElastixTransform<elx::SimilarityTransformElastix>::Test_copying_parameters<
itk::Similarity3DTransform<double>>();
WithDimension<2>::WithElastixTransform<elx::EulerTransformElastix>::Test_copying_parameters<
itk::Euler2DTransform<double>>();
}
GTEST_TEST(Transform, CreateTransformParameterMapForDefaultTransform)
{
WithDimension<2>::Test_CreateTransformParameterMap_for_default_transform();
WithDimension<3>::Test_CreateTransformParameterMap_for_default_transform();
WithDimension<4>::Test_CreateTransformParameterMap_for_default_transform();
}
GTEST_TEST(Transform, CreateTransformParameterMapDoublePrecision)
{
// Checks two different transform types, just to be sure.
WithDimension<2>::WithElastixTransform<
elx::AdvancedAffineTransformElastix>::Test_CreateTransformParameterMap_double_precision();
WithDimension<3>::WithElastixTransform<
elx::TranslationTransformElastix>::Test_CreateTransformParameterMap_double_precision();
}
GTEST_TEST(Transform, CreateTransformParametersSetUseAddition)
{
// Checks two different transform types, just to be sure.
WithDimension<2>::WithElastixTransform<
elx::AdvancedAffineTransformElastix>::Test_CreateTransformParameterMap_SetUseAddition();
WithDimension<3>::WithElastixTransform<
elx::TranslationTransformElastix>::Test_CreateTransformParameterMap_SetUseAddition();
}
GTEST_TEST(Transform, TransformedPointSameAsITKTranslation2D)
{
static constexpr auto Dimension = 2U;
elx::DefaultConstruct<itk::TranslationTransform<double, Dimension>> itkTransform;
itkTransform.SetOffset(itk::MakeVector(1.0, 2.0));
Expect_elx_TransformPoint_yields_same_point_as_ITK<elx::TranslationTransformElastix>(itkTransform);
}
GTEST_TEST(Transform, TransformedPointSameAsITKTranslation3D)
{
static constexpr auto Dimension = 3U;
elx::DefaultConstruct<itk::TranslationTransform<double, Dimension>> itkTransform;
itkTransform.SetOffset(itk::MakeVector(1.0, 2.0, 3.0));
Expect_elx_TransformPoint_yields_same_point_as_ITK<elx::TranslationTransformElastix>(itkTransform);
}
GTEST_TEST(Transform, TransformedPointSameAsITKAffine2D)
{
static constexpr auto Dimension = 2U;
elx::DefaultConstruct<itk::AffineTransform<double, Dimension>> itkTransform;
itkTransform.SetTranslation(itk::MakeVector(1.0, 2.0));
itkTransform.Scale(itk::MakeVector(1.5, 1.75));
itkTransform.SetCenter(itk::MakePoint(0.5, 1.5));
itkTransform.Rotate2D(M_PI_4);
Expect_elx_TransformPoint_yields_same_point_as_ITK<elx::AdvancedAffineTransformElastix>(itkTransform);
}
GTEST_TEST(Transform, TransformedPointSameAsITKAffine3D)
{
static constexpr auto Dimension = 3U;
elx::DefaultConstruct<itk::AffineTransform<double, Dimension>> itkTransform;
itkTransform.SetTranslation(itk::MakeVector(1.0, 2.0, 3.0));
itkTransform.SetCenter(itk::MakePoint(3.0, 2.0, 1.0));
itkTransform.Scale(itk::MakeVector(1.25, 1.5, 1.75));
itkTransform.Rotate3D(itk::Vector<double, Dimension>(1.0), M_PI_4);
Expect_elx_TransformPoint_yields_same_point_as_ITK<elx::AdvancedAffineTransformElastix>(itkTransform);
}
GTEST_TEST(Transform, TransformedPointSameAsITKEuler2D)
{
elx::DefaultConstruct<itk::Euler2DTransform<double>> itkTransform;
itkTransform.SetTranslation(itk::MakeVector(1.0, 2.0));
itkTransform.SetCenter(itk::MakePoint(0.5, 1.5));
itkTransform.SetAngle(M_PI_4);
Expect_elx_TransformPoint_yields_same_point_as_ITK<elx::EulerTransformElastix>(itkTransform);
}
GTEST_TEST(Transform, TransformedPointSameAsITKEuler3D)
{
for (const bool computeZYX : { false, true })
{
elx::DefaultConstruct<itk::Euler3DTransform<double>> itkTransform;
itkTransform.SetTranslation(itk::MakeVector(1.0, 2.0, 3.0));
itkTransform.SetCenter(itk::MakePoint(3.0, 2.0, 1.0));
itkTransform.SetRotation(M_PI_2, M_PI_4, M_PI_4 / 2.0);
itkTransform.SetComputeZYX(computeZYX);
Expect_elx_TransformPoint_yields_same_point_as_ITK<elx::EulerTransformElastix>(itkTransform);
}
}
GTEST_TEST(Transform, TransformedPointSameAsITKSimilarity2D)
{
elx::DefaultConstruct<itk::Similarity2DTransform<double>> itkTransform;
itkTransform.SetScale(0.75);
itkTransform.SetTranslation(itk::MakeVector(1.0, 2.0));
itkTransform.SetCenter(itk::MakePoint(0.5, 1.5));
itkTransform.SetAngle(M_PI_4);
Expect_elx_TransformPoint_yields_same_point_as_ITK<elx::SimilarityTransformElastix>(itkTransform);
}
GTEST_TEST(Transform, TransformedPointSameAsITKSimilarity3D)
{
elx::DefaultConstruct<itk::Similarity3DTransform<double>> itkTransform;
itkTransform.SetScale(0.75);
itkTransform.SetTranslation(itk::MakeVector(1.0, 2.0, 3.0));
itkTransform.SetCenter(itk::MakePoint(3.0, 2.0, 1.0));
itkTransform.SetRotation(itk::Vector<double, 3>(1.0), M_PI_4);
Expect_elx_TransformPoint_yields_same_point_as_ITK<elx::SimilarityTransformElastix>(itkTransform);
}
GTEST_TEST(Transform, TransformedPointSameAsITKBSpline2D)
{
elx::DefaultConstruct<itk::BSplineTransform<double, 2>> itkTransform;
itkTransform.SetParameters(GeneratePseudoRandomParameters(itkTransform.GetParameters().size(), -1.0));
Expect_elx_TransformPoint_yields_same_point_as_ITK<elx::AdvancedBSplineTransform>(itkTransform);
Expect_elx_TransformPoint_yields_same_point_as_ITK<elx::RecursiveBSplineTransform>(itkTransform);
}
GTEST_TEST(Transform, TransformedPointSameAsITKBSpline3D)
{
elx::DefaultConstruct<itk::BSplineTransform<double, 3>> itkTransform;
itkTransform.SetParameters(GeneratePseudoRandomParameters(itkTransform.GetParameters().size(), -1.0));
Expect_elx_TransformPoint_yields_same_point_as_ITK<elx::AdvancedBSplineTransform>(itkTransform);
Expect_elx_TransformPoint_yields_same_point_as_ITK<elx::RecursiveBSplineTransform>(itkTransform);
}
|