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
|
/*=========================================================================
*
* 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 <iostream>
#include "itkCenteredAffineTransform.h"
#include "itkImage.h"
typedef itk::Matrix<double, 2, 2> MatrixType;
typedef itk::Vector<double, 2> VectorType;
namespace
{
void PrintVector( const VectorType & v )
{
for( unsigned int i = 0; i < VectorType::Dimension; i++ )
{
std::cout << v[i] << ", ";
}
std::cout << std::endl;
}
}
int itkCenteredAffineTransformTest(int, char *[])
{
int any = 0; // Any errors detected in testing?
MatrixType matrix2;
VectorType vector2;
int i, j;
/* FIXME: This code exercises most of the methods but doesn't
actually check that the results are correct. */
/* Create a 2D identity transformation and show its parameters */
typedef itk::CenteredAffineTransform<double, 2> Affine2DType;
Affine2DType::Pointer id2 = Affine2DType::New();
matrix2 = id2->GetMatrix();
vector2 = id2->GetOffset();
std::cout << "Matrix from instantiating an identity transform:"
<< std::endl << matrix2;
std::cout << "Vector from instantiating an identity transform:"
<< std::endl;
PrintVector( vector2 );
/* Create and show a simple 2D transform from given parameters */
matrix2[0][0] = 1;
matrix2[0][1] = 2;
matrix2[1][0] = 3;
matrix2[1][1] = 4;
vector2[0] = 5;
vector2[1] = 6;
Affine2DType::Pointer aff2 = Affine2DType::New();
Affine2DType::Pointer inverse2 = Affine2DType::New();
aff2->SetMatrix( matrix2 );
aff2->SetOffset( vector2 );
for( i = 0; i < 2; i++ )
{
for( j = 0; j < 2; j++ )
{
matrix2[i][j] = 0.0;
}
vector2[i] = 0.0;
}
std::cout << "Instantiation of a given 2D transform:" << std::endl;
aff2->Print( std::cout );
aff2->GetInverse(inverse2);
std::cout << "Inverse matrix for the given transform:"
<< std::endl << inverse2->GetMatrix();
/* Set parameters of a 2D transform */
matrix2[0][0] = 6;
matrix2[0][1] = 5;
matrix2[1][0] = 4;
matrix2[1][1] = 3;
vector2[0] = 2;
vector2[1] = 1;
aff2->SetMatrix(matrix2);
aff2->SetOffset(vector2);
for( i = 0; i < 2; i++ )
{
for( j = 0; j < 2; j++ )
{
matrix2[i][j] = 0.0;
}
vector2[i] = 0.0;
}
matrix2 = aff2->GetMatrix();
vector2 = aff2->GetOffset();
std::cout << "Setting the matrix in an existing transform:"
<< std::endl << matrix2;
std::cout << "Setting the offset in an existing transform:"
<< std::endl;
PrintVector( vector2 );
/* Try composition of two transformations */
aff2->Compose( aff2 );
std::cout << "Result of a composition:" << std::endl;
aff2->Print( std::cout );
/* Compose with a translation */
VectorType trans;
trans[0] = 1;
trans[1] = 2;
aff2->Translate(trans);
std::cout << "Result of a translation:" << std::endl;
aff2->Print( std::cout );
/* Compose with an isotropic scaling */
aff2->Scale(.3, 1);
std::cout << "Result of isotropic scaling:" << std::endl;
aff2->Print( std::cout );
/* Compose with an anisotropic scaling */
VectorType scale;
scale[0] = .3;
scale[1] = .2;
aff2->Scale(scale);
std::cout << "Result of anisotropic scaling:" << std::endl;
aff2->Print( std::cout );
/* Compose with a general N-D rotation */
aff2->Rotate(0, 1, 0.57, 1);
std::cout << "Result of general rotation:" << std::endl;
aff2->Print( std::cout );
/* Compose with a 2-D rotation */
aff2->Rotate(0, 1, -0.57, 1);
std::cout << "Result of 2-D rotation:" << std::endl;
aff2->Print( std::cout );
/* Compose with a shear */
aff2->Shear(1, 0, .2);
std::cout << "Result of shear:" << std::endl;
aff2->Print( std::cout );
/* Transform a point */
itk::Point<double, 2> u2, v2;
u2[0] = 3;
u2[1] = 5;
v2 = aff2->TransformPoint(u2);
std::cout << "Transform a point:" << std::endl
<< v2[0] << " , " << v2[1] << std::endl;
// /* Back transform a point */
// v2 = aff2->BackTransform(u2);
// std::cout << "Back transform a point:" << std::endl
// << v2[0] << " , " << v2[1] << std::endl;
/* Transform a vnl_vector */
vnl_vector_fixed<double, 2> x2, y2;
x2[0] = 1;
x2[1] = 2;
y2 = aff2->TransformVector(x2);
std::cout << "Transform a vnl_vector:" << std::endl
<< y2[0] << " , " << y2[1] << std::endl;
// /* Back transform a vector */
// y2 = aff2->BackTransform(x2);
// std::cout << "Back transform a vnl_vector:" << std::endl
// << y2[0] << " , " << y2[1] << std::endl;
/* Transform a vector */
itk::Vector<double, 2> u3, v3;
u3[0] = 3;
u3[1] = 5;
v3 = aff2->TransformVector(u3);
std::cout << "Transform a vector:" << std::endl
<< v3[0] << " , " << v3[1] << std::endl;
// /* Back transform a vector */
// v3 = aff2->BackTransform(u3);
// std::cout << "Back transform a vector :" << std::endl
// << v3[0] << " , " << v3[1] << std::endl;
/* Transform a Covariant vector */
itk::Vector<double, 2> u4, v4;
u4[0] = 3;
u4[1] = 5;
v4 = aff2->TransformVector(u4);
std::cout << "Transform a Covariant vector:" << std::endl
<< v4[0] << " , " << v4[1] << std::endl;
// /* Back transform a vector */
// v4 = aff2->BackTransform(u4);
// std::cout << "Back transform a vector :" << std::endl
// << v4[0] << " , " << v4[1] << std::endl;
/* Create a 3D transform and rotate in 3D */
typedef itk::CenteredAffineTransform<double, 3> Affine3DType;
Affine3DType::Pointer aff3 = Affine3DType::New();
itk::Vector<double, 3> axis;
axis[0] = .707;
axis[1] = .707;
axis[2] = .707;
aff3->Rotate3D(axis, 1.0, 1);
std::cout << "Create and rotate a 3D transform:" << std::endl;
aff3->Print( std::cout );
/* Generate inverse transform */
Affine3DType::Pointer inv3 = Affine3DType::New();
if( !aff3->GetInverse(inv3) )
{
std::cout << "Cannot create inverse transformation" << std::endl;
}
std::cout << "Create an inverse transformation:" << std::endl;
inv3->Print( std::cout );
Affine3DType::Pointer inv4 =
dynamic_cast<Affine3DType *>(aff3->GetInverseTransform().GetPointer() );
if( !inv4 )
{
std::cout << "Cannot compute inverse transformation" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Create an inverse transformation:" << std::endl;
inv4->Print( std::cout );
/* Create an image for testing index<->physical transforms */
std::cout << "Creating image for testing index<->physical transforms"
<< std::endl;
double spacing[3] = {1.0, 2.0, 3.0};
double origin[3] = {4.0, 5.0, 6.0};
itk::Image<unsigned char, 3>::Pointer
image = itk::Image<unsigned char, 3>::New();
image->SetOrigin(origin);
image->SetSpacing(spacing);
/* Test output of ComputeJacobianWithRespectToParameters */
Affine3DType::Pointer jaff = Affine3DType::New();
const Affine3DType::MatrixType jaffMatrix = jaff->GetMatrix();
std::cout << "GetMatrix:" << std::endl;
std::cout << jaffMatrix << std::endl;
const Affine3DType::OffsetType jaffVector = jaff->GetOffset();
std::cout << "GetOffset:" << std::endl;
std::cout << jaffVector << std::endl;
Affine3DType::InputPointType jpoint;
jpoint[0] = 5.0;
jpoint[1] = 10.0;
jpoint[2] = 15.0;
Affine3DType::JacobianType jaffJacobian;
jaff->ComputeJacobianWithRespectToParameters( jpoint, jaffJacobian );
std::cout << "ComputeJacobianWithRespectToParameters: " << std::endl;
std::cout << jaffJacobian << std::endl;
/* Get the parameters */
Affine3DType::ParametersType parameters3D;
parameters3D = aff3->GetParameters();
std::cout << "Parameters 3D: " << parameters3D << std::endl;
/* Now set the parameters of another matrix */
jaff->SetParameters(parameters3D);
std::cout << "A transform after SetParameters:" << std::endl;
jaff->Print( std::cout );
return any;
}
|