/usr/include/ITK-4.5/itkTransform.hxx is in libinsighttoolkit4-dev 4.5.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*=========================================================================
*
* 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.
*
*=========================================================================*/
#ifndef __itkTransform_hxx
#define __itkTransform_hxx
#include "itkTransform.h"
#include "itkCrossHelper.h"
#include "vnl/algo/vnl_matrix_inverse.h"
namespace itk
{
/**
* Constructor
*/
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
Transform<TScalar, NInputDimensions, NOutputDimensions>
::Transform() :
m_Parameters(1),
m_FixedParameters(1)
#ifdef ITKV3_COMPATIBILITY
, m_SharedLocalJacobian(NOutputDimensions, 1)
#endif
{
itkWarningMacro(
<< "Using default transform constructor. Should specify NOutputDims and NParameters as args to constructor.");
}
/**
* Constructor
*/
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
Transform<TScalar, NInputDimensions, NOutputDimensions>
::Transform(NumberOfParametersType numberOfParameters) :
m_Parameters(numberOfParameters),
m_FixedParameters(numberOfParameters)
#ifdef ITKV3_COMPATIBILITY
, m_SharedLocalJacobian(NOutputDimensions, numberOfParameters)
#endif
{
}
/**
* GenerateName
*/
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
std::string Transform<TScalar, NInputDimensions, NOutputDimensions>
::GetTransformTypeAsString() const
{
std::ostringstream n;
n << GetNameOfClass();
n << "_";
n << this->GetTransformTypeAsString(static_cast<TScalar *>(0) );
n << "_" << this->GetInputSpaceDimension() << "_" << this->GetOutputSpaceDimension();
return n.str();
}
/**
* Clone
*/
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
typename LightObject::Pointer
Transform<TScalar, NInputDimensions, NOutputDimensions>
::InternalClone() const
{
// Default implementation just copies the parameters from
// this to new transform.
typename LightObject::Pointer loPtr = Superclass::InternalClone();
typename Self::Pointer rval =
dynamic_cast<Self *>(loPtr.GetPointer());
if(rval.IsNull())
{
itkExceptionMacro(<< "downcast to type "
<< this->GetNameOfClass()
<< " failed.");
}
rval->SetFixedParameters(this->GetFixedParameters());
rval->SetParameters(this->GetParameters());
return loPtr;
}
/**
* UpdateTransformParameters
*/
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
void
Transform<TScalar, NInputDimensions, NOutputDimensions>
::UpdateTransformParameters( const DerivativeType & update, TScalar factor )
{
NumberOfParametersType numberOfParameters = this->GetNumberOfParameters();
if( update.Size() != numberOfParameters )
{
itkExceptionMacro("Parameter update size, " << update.Size() << ", must "
" be same as transform parameter size, "
<< numberOfParameters << std::endl);
}
/* Make sure m_Parameters is updated to reflect the current values in
* the transform's other parameter-related variables. This is effective for
* managing the parallel variables used for storing parameter data,
* but inefficient. However for small global transforms, shouldn't be
* too bad. Dense-field transform will want to make sure m_Parameters
* is always updated whenever the transform is changed, so GetParameters
* can be skipped in their implementations of UpdateTransformParameters. */
this->GetParameters();
if( factor == 1.0 )
{
for( NumberOfParametersType k = 0; k < numberOfParameters; k++ )
{
this->m_Parameters[k] += update[k];
}
}
else
{
for( NumberOfParametersType k = 0; k < numberOfParameters; k++ )
{
this->m_Parameters[k] += update[k] * factor;
}
}
/* Call SetParameters with the updated parameters.
* SetParameters in most transforms is used to assign the input params
* to member variables, possibly with some processing. The member variables
* are then used in TransformPoint.
* In the case of dense-field transforms that are updated in blocks from
* a threaded implementation, SetParameters doesn't do this, and is
* optimized to not copy the input parameters when == m_Parameters.
*/
this->SetParameters( this->m_Parameters );
/* Call Modified, following behavior of other transform when their
* parameters change, e.g. MatrixOffsetTransformBase */
this->Modified();
}
/**
* Transform vector
*/
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
typename Transform<TScalar, NInputDimensions, NOutputDimensions>::OutputVectorType
Transform<TScalar, NInputDimensions, NOutputDimensions>
::TransformVector( const InputVectorType& vector, const InputPointType & point ) const
{
JacobianType jacobian;
this->ComputeJacobianWithRespectToPosition( point, jacobian );
OutputVectorType result;
for( unsigned int i = 0; i < NOutputDimensions; i++ )
{
result[i] = NumericTraits<ScalarType>::Zero;
for( unsigned int j = 0; j < NInputDimensions; j++ )
{
result[i] += jacobian[i][j] * vector[j];
}
}
return result;
}
/**
* Transform vector
*/
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
typename Transform<TScalar, NInputDimensions, NOutputDimensions>::OutputVnlVectorType
Transform<TScalar, NInputDimensions, NOutputDimensions>
::TransformVector( const InputVnlVectorType& vector, const InputPointType & point ) const
{
JacobianType jacobian;
this->ComputeJacobianWithRespectToPosition( point, jacobian );
OutputVnlVectorType result;
for( unsigned int i = 0; i < NOutputDimensions; i++ )
{
result[i] = NumericTraits<ScalarType>::Zero;
for( unsigned int j = 0; j < NInputDimensions; j++ )
{
result[i] += jacobian[i][j] * vector[j];
}
}
return result;
}
/**
* Transform vector
*/
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
typename Transform<TScalar, NInputDimensions, NOutputDimensions>::OutputVectorPixelType
Transform<TScalar, NInputDimensions, NOutputDimensions>
::TransformVector( const InputVectorPixelType& vector, const InputPointType & point ) const
{
if ( vector.GetSize() != NInputDimensions )
{
itkExceptionMacro( "Input Vector is not of size NInputDimensions = " << NInputDimensions << std::endl );
}
JacobianType jacobian;
this->ComputeJacobianWithRespectToPosition( point, jacobian );
OutputVectorPixelType result;
result.SetSize( NOutputDimensions );
for( unsigned int i = 0; i < NOutputDimensions; i++ )
{
result[i] = NumericTraits<ScalarType>::Zero;
for( unsigned int j = 0; j < NInputDimensions; j++ )
{
result[i] += jacobian[i][j] * vector[j];
}
}
return result;
}
/**
* Transform covariant vector
*/
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
typename Transform<TScalar, NInputDimensions, NOutputDimensions>::OutputCovariantVectorType
Transform<TScalar, NInputDimensions, NOutputDimensions>
::TransformCovariantVector( const InputCovariantVectorType& vector, const InputPointType & point ) const
{
JacobianType jacobian;
this->ComputeInverseJacobianWithRespectToPosition( point, jacobian );
OutputCovariantVectorType result;
for( unsigned int i = 0; i < NOutputDimensions; i++ )
{
result[i] = NumericTraits<ScalarType>::Zero;
for( unsigned int j = 0; j < NInputDimensions; j++ )
{
result[i] += jacobian[j][i] * vector[j];
}
}
return result;
}
/**
* Transform covariant vector
*/
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
typename Transform<TScalar, NInputDimensions, NOutputDimensions>::OutputVectorPixelType
Transform<TScalar, NInputDimensions, NOutputDimensions>
::TransformCovariantVector( const InputVectorPixelType& vector, const InputPointType & point ) const
{
if ( vector.GetSize() != NInputDimensions )
{
itkExceptionMacro( "Input Vector is not of size NInputDimensions = " << NInputDimensions << std::endl );
}
JacobianType jacobian;
this->ComputeInverseJacobianWithRespectToPosition( point, jacobian );
OutputVectorPixelType result;
result.SetSize( NOutputDimensions );
for( unsigned int i = 0; i < NOutputDimensions; i++ )
{
result[i] = NumericTraits<ScalarType>::Zero;
for( unsigned int j = 0; j < NInputDimensions; j++ )
{
result[i] += jacobian[j][i] * vector[j];
}
}
return result;
}
/**
* Transform tensor
*/
template <typename TScalar, unsigned int NInputDimensions, unsigned int NOutputDimensions>
typename Transform<TScalar, NInputDimensions, NOutputDimensions>::OutputDiffusionTensor3DType
Transform<TScalar, NInputDimensions, NOutputDimensions>
::TransformDiffusionTensor3D( const InputDiffusionTensor3DType& inputTensor, const InputPointType & point ) const
{
JacobianType invJacobian;
this->ComputeInverseJacobianWithRespectToPosition( point, invJacobian );
OutputDiffusionTensor3DType result
= this->PreservationOfPrincipalDirectionDiffusionTensor3DReorientation( inputTensor, invJacobian );
return result;
}
/**
* Transform tensor
*/
template <typename TScalar, unsigned int NInputDimensions, unsigned int NOutputDimensions>
typename Transform<TScalar, NInputDimensions, NOutputDimensions>::OutputVectorPixelType
Transform<TScalar, NInputDimensions, NOutputDimensions>
::TransformDiffusionTensor3D( const InputVectorPixelType & inputTensor, const InputPointType & point ) const
{
if (inputTensor.GetSize() != 6 )
{
itkExceptionMacro( "Input DiffusionTensor3D does not have 6 elements" << std::endl );
}
InputDiffusionTensor3DType inTensor;
for (unsigned int i=0; i<5; i++)
{
inTensor[i] = inputTensor[i];
}
OutputDiffusionTensor3DType outTensor = this->TransformDiffusionTensor3D( inTensor, point );
OutputVectorPixelType outputTensor;
outputTensor.SetSize( 6 );
for (unsigned int i=0; i<5; i++)
{
outputTensor[i] = outTensor[i];
}
return outputTensor;
}
/**
* Transform tensor
*/
template <typename TScalar, unsigned int NInputDimensions, unsigned int NOutputDimensions>
typename Transform<TScalar, NInputDimensions, NOutputDimensions>::OutputDiffusionTensor3DType
Transform<TScalar, NInputDimensions, NOutputDimensions>
::PreservationOfPrincipalDirectionDiffusionTensor3DReorientation( const InputDiffusionTensor3DType inputTensor,
const JacobianType jacobian ) const
{
Matrix<TScalar,3,3> matrix;
//typename MatrixType3D dMatrix;
matrix.Fill(0.0);
//dMatrix.Fill(0.0);
for( unsigned int i = 0; i < 3; i++ )
{
matrix(i, i) = 1.0;
//dMatrix(i, i) = 1.0;
}
for( unsigned int i = 0; i < NInputDimensions; i++ )
{
for( unsigned int j = 0; j < NOutputDimensions; j++ )
{
if( (i < 3) && (j < 3) )
{
matrix(i, j) = jacobian(i, j);
//dMatrix(i, j) = this->GetDirectionChangeMatrix()(i, j);
}
}
}
typename InputDiffusionTensor3DType::EigenValuesArrayType eigenValues;
typename InputDiffusionTensor3DType::EigenVectorsMatrixType eigenVectors;
inputTensor.ComputeEigenAnalysis( eigenValues, eigenVectors );
Vector<TScalar,3> ev1;
Vector<TScalar,3> ev2;
Vector<TScalar,3> ev3;
for( unsigned int i = 0; i < 3; i++ )
{
ev1[i] = eigenVectors(2, i);
ev2[i] = eigenVectors(1, i);
}
// Account for image direction changes between moving and fixed spaces
//ev1 = matrix * dMatrix * ev1;
ev1 = matrix * ev1;
ev1.Normalize();
// Get aspect of rotated e2 that is perpendicular to rotated e1
//ev2 = matrix * dMatrix * ev2;
ev2 = matrix * ev2;
double dp = ev2 * ev1;
if( dp < 0 )
{
ev2 = ev2 * (-1.0);
dp = dp * (-1.0);
}
ev2 = ev2 - ev1 * dp;
ev2.Normalize();
CrossHelper< Vector<TScalar,3> > vectorCross;
ev3 = vectorCross( ev1, ev2 );
// Outer product matrices
Matrix<TScalar,3,3> e1;
Matrix<TScalar,3,3> e2;
Matrix<TScalar,3,3> e3;
for( unsigned int i = 0; i < 3; i++ )
{
for( unsigned int j = 0; j < 3; j++ )
{
e1(i, j) = eigenValues[2] * ev1[i] * ev1[j];
e2(i, j) = eigenValues[1] * ev2[i] * ev2[j];
e3(i, j) = eigenValues[0] * ev3[i] * ev3[j];
}
}
Matrix<TScalar,3,3> rotated = e1 + e2 + e3;
OutputDiffusionTensor3DType result; // Converted vector
result[0] = rotated(0, 0);
result[1] = rotated(0, 1);
result[2] = rotated(0, 2);
result[3] = rotated(1, 1);
result[4] = rotated(1, 2);
result[5] = rotated(2, 2);
return result;
}
/**
* Transform tensor
*/
template <typename TScalar, unsigned int NInputDimensions, unsigned int NOutputDimensions>
typename Transform<TScalar, NInputDimensions, NOutputDimensions>::OutputSymmetricSecondRankTensorType
Transform<TScalar, NInputDimensions, NOutputDimensions>
::TransformSymmetricSecondRankTensor( const InputSymmetricSecondRankTensorType& inputTensor, const InputPointType & point ) const
{
JacobianType jacobian;
this->ComputeJacobianWithRespectToPosition( point, jacobian );
JacobianType invJacobian;
this->ComputeInverseJacobianWithRespectToPosition( point, invJacobian );
JacobianType tensor;
tensor.SetSize( NInputDimensions, NInputDimensions );
for( unsigned int i = 0; i < NInputDimensions; i++ )
{
for( unsigned int j = 0; j < NInputDimensions; j++ )
{
tensor(i, j) = inputTensor(i, j);
}
}
JacobianType outTensor = jacobian * tensor * invJacobian;
OutputSymmetricSecondRankTensorType outputTensor;
for( unsigned int i = 0; i < NOutputDimensions; i++ )
{
for( unsigned int j = 0; j < NOutputDimensions; j++ )
{
outputTensor(i, j) = outTensor(i, j);
}
}
return outputTensor;
}
/**
* Transform tensor
*/
template <typename TScalar, unsigned int NInputDimensions, unsigned int NOutputDimensions>
typename Transform<TScalar, NInputDimensions, NOutputDimensions>::OutputVectorPixelType
Transform<TScalar, NInputDimensions, NOutputDimensions>
::TransformSymmetricSecondRankTensor( const InputVectorPixelType& inputTensor, const InputPointType & point ) const
{
if (inputTensor.GetSize() != (NInputDimensions*NInputDimensions) )
{
itkExceptionMacro( "Input DiffusionTensor3D does not have " << NInputDimensions*NInputDimensions << " elements" << std::endl );
}
JacobianType jacobian;
this->ComputeJacobianWithRespectToPosition( point, jacobian );
JacobianType invJacobian;
this->ComputeInverseJacobianWithRespectToPosition( point, invJacobian );
JacobianType tensor;
tensor.SetSize( NInputDimensions, NInputDimensions );
for( unsigned int i = 0; i < NInputDimensions; i++ )
{
for( unsigned int j = 0; j < NInputDimensions; j++ )
{
tensor(i, j) = inputTensor[j + NInputDimensions*i];
}
}
JacobianType outTensor = jacobian * tensor * invJacobian;
OutputVectorPixelType outputTensor;
outputTensor.SetSize( NOutputDimensions*NOutputDimensions );
for( unsigned int i = 0; i < NOutputDimensions; i++ )
{
for( unsigned int j = 0; j < NOutputDimensions; j++ )
{
outputTensor[j + NOutputDimensions*i] = outTensor(i, j);
}
}
return outputTensor;
}
/**
* ComputeInverseJacobianWithRespectToPosition
*/
template <typename TScalar,
unsigned int NInputDimensions,
unsigned int NOutputDimensions>
void
Transform<TScalar, NInputDimensions, NOutputDimensions>
::ComputeInverseJacobianWithRespectToPosition( const InputPointType & pnt, JacobianType & jacobian ) const
{
JacobianType forward_jacobian;
this->ComputeJacobianWithRespectToPosition( pnt, forward_jacobian );
jacobian.SetSize(NInputDimensions, NOutputDimensions);
vnl_svd<typename JacobianType::ValueType> svd( forward_jacobian );
for( unsigned int i = 0; i < jacobian.rows(); i++ )
{
for( unsigned int j = 0; j < jacobian.cols(); j++ )
{
jacobian(i, j) = svd.inverse() (i, j);
}
}
}
} // end namespace itk
#endif
|