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
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* 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.
*
*=========================================================================*/
#include "itkGaussianExponentialDiffeomorphicTransform.h"
#include "itkGaussianExponentialDiffeomorphicTransformParametersAdaptor.h"
#include "itkMath.h"
int itkGaussianExponentialDiffeomorphicTransformParametersAdaptorTest(int, char * [] )
{
const unsigned int SpaceDimension = 3;
typedef double CoordinateRepType;
typedef itk::GaussianExponentialDiffeomorphicTransform<CoordinateRepType, SpaceDimension> TransformType;
/**
* Define the transformation domain
*/
typedef TransformType::PointType PointType;
PointType origin;
origin.Fill( -5.0 );
typedef TransformType::SizeType SizeType;
SizeType size;
size.Fill( 65 );
typedef TransformType::SpacingType SpacingType;
SpacingType spacing;
spacing.Fill( 1.2 );
typedef TransformType::DirectionType DirectionType;
DirectionType direction;
direction.SetIdentity();
typedef TransformType::DisplacementFieldType DisplacementFieldType;
DisplacementFieldType::Pointer displacementField = DisplacementFieldType::New();
displacementField->SetOrigin( origin );
displacementField->SetSpacing( spacing );
displacementField->SetRegions( size );
displacementField->SetDirection( direction );
displacementField->Allocate();
TransformType::OutputVectorType zeroVector;
zeroVector.Fill( 0 );
displacementField->FillBuffer( zeroVector );
TransformType::OutputVectorType nonzeroVector;
nonzeroVector.Fill( 10.3 );
DisplacementFieldType::IndexType index;
index.Fill( 40 );
displacementField->SetPixel( index, nonzeroVector );
/**
* Instantiate a transform
*/
std::cout << "Initialize transform." << std::endl;
TransformType::Pointer transform = TransformType::New();
transform->SetConstantVelocityField( displacementField );
transform->IntegrateVelocityField();
TransformType::InputPointType point;
point.Fill( 50.0 );
TransformType::OutputPointType outputPointBeforeAdapt = transform->TransformPoint( point );
SpacingType spacingBefore = transform->GetConstantVelocityField()->GetSpacing();
SizeType sizeBefore = transform->GetConstantVelocityField()->GetLargestPossibleRegion().GetSize();
/**
* Instantiate the adaptor
* we keep the transform domain definition the same except we increase
* the size and decrease the spacing.
*/
std::cout << "Instantiate adaptor." << std::endl;
SpacingType requiredSpacing;
requiredSpacing.Fill( 0.6 );
SizeType requiredSize;
for( unsigned int d = 0; d < SpaceDimension; d++ )
{
requiredSize[d] = static_cast<SizeType::SizeValueType>
( ( spacing[d] * ( size[d] - 1 ) / requiredSpacing[d] ) + 1 );
}
typedef itk::GaussianExponentialDiffeomorphicTransformParametersAdaptor<TransformType> AdaptorType;
AdaptorType::Pointer adaptor = AdaptorType::New();
adaptor->SetTransform( transform );
adaptor->SetRequiredSize( requiredSize );
adaptor->SetRequiredSpacing( requiredSpacing );
adaptor->SetRequiredOrigin( displacementField->GetOrigin() );
adaptor->SetRequiredDirection( displacementField->GetDirection() );
float updateSmoothingVariance = 3.0;
float velocitySmoothingVariance = 0;
adaptor->SetGaussianSmoothingVarianceForTheUpdateField( updateSmoothingVariance );
adaptor->SetGaussianSmoothingVarianceForTheConstantVelocityField( velocitySmoothingVariance );
try
{
adaptor->AdaptTransformParameters();
}
catch(...)
{
std::cerr << "Error in adapting transform." << std::endl;
return EXIT_FAILURE;
}
SpacingType spacingAfter = transform->GetConstantVelocityField()->GetSpacing();
SizeType sizeAfter = transform->GetConstantVelocityField()->GetLargestPossibleRegion().GetSize();
std::cout << "Spacing: " << spacingBefore << "(before), " << spacingAfter << "(after)." << std::endl;
std::cout << "Size: " << sizeBefore << "(before), " << sizeAfter << "(after)." << std::endl;
TransformType::ParametersType fixedParameters = adaptor->GetRequiredFixedParameters();
std::cout << "Fixed parameters: " << fixedParameters << std::endl;
adaptor->SetRequiredFixedParameters( fixedParameters );
if( adaptor->GetRequiredSize() != transform->GetDisplacementField()->GetLargestPossibleRegion().GetSize() )
{
std::cerr << "required size conversion is incorrect." << std::endl;
return EXIT_FAILURE;
}
if( adaptor->GetRequiredSpacing() != transform->GetDisplacementField()->GetSpacing() )
{
std::cerr << "required spacing conversion is incorrect." << std::endl;
return EXIT_FAILURE;
}
if( adaptor->GetRequiredOrigin() != transform->GetDisplacementField()->GetOrigin() )
{
std::cerr << "required origin conversion is incorrect." << std::endl;
return EXIT_FAILURE;
}
if( adaptor->GetRequiredDirection() != transform->GetDisplacementField()->GetDirection() )
{
std::cerr << "required direction conversion is incorrect." << std::endl;
return EXIT_FAILURE;
}
if( itk::Math::NotAlmostEquals( adaptor->GetGaussianSmoothingVarianceForTheUpdateField(),
transform->GetGaussianSmoothingVarianceForTheUpdateField() ) )
{
std::cerr << "update field mesh conversion is incorrect." << std::endl;
return EXIT_FAILURE;
}
if( itk::Math::NotAlmostEquals( adaptor->GetGaussianSmoothingVarianceForTheConstantVelocityField(),
transform->GetGaussianSmoothingVarianceForTheConstantVelocityField() ) )
{
std::cerr << "total field mesh conversion is incorrect." << std::endl;
return EXIT_FAILURE;
}
TransformType::OutputPointType outputPointAfterAdapt = transform->TransformPoint( point );
std::cout << point << " to (before) " << outputPointBeforeAdapt << std::endl;
std::cout << point << " to (after) " << outputPointAfterAdapt << std::endl;
if( outputPointBeforeAdapt.EuclideanDistanceTo( outputPointAfterAdapt ) > 1e-6 )
{
std::cerr << "output points don't match up before and after adapt call." << std::endl;
return EXIT_FAILURE;
}
adaptor->Print( std::cout, 5 );
return EXIT_SUCCESS;
}
|