/usr/include/ITK-4.5/itkKernelTransform.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 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | /*=========================================================================
*
* 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 __itkKernelTransform_hxx
#define __itkKernelTransform_hxx
#include "itkKernelTransform.h"
namespace itk
{
/**
*
*/
template <typename TScalar, unsigned int NDimensions>
KernelTransform<TScalar, NDimensions>::KernelTransform() : Superclass(NDimensions)
// the second NDimensions is associated is provided as
// a tentative number for initializing the Jacobian.
// The matrix can be resized at run time so this number
// here is irrelevant. The correct size of the Jacobian
// will be NDimension X NDimension.NumberOfLandMarks.
{
this->m_I.set_identity();
this->m_SourceLandmarks = PointSetType::New();
this->m_TargetLandmarks = PointSetType::New();
this->m_Displacements = VectorSetType::New();
this->m_WMatrixComputed = false;
this->m_Stiffness = 0.0;
}
/**
*
*/
template <typename TScalar, unsigned int NDimensions>
KernelTransform<TScalar, NDimensions>::
~KernelTransform()
{
}
/**
*
*/
template <typename TScalar, unsigned int NDimensions>
void
KernelTransform<TScalar, NDimensions>::SetSourceLandmarks(PointSetType *landmarks)
{
itkDebugMacro("setting SourceLandmarks to " << landmarks);
if( this->m_SourceLandmarks != landmarks )
{
this->m_SourceLandmarks = landmarks;
this->UpdateParameters();
this->Modified();
}
}
/**
*
*/
template <typename TScalar, unsigned int NDimensions>
void
KernelTransform<TScalar, NDimensions>::SetTargetLandmarks(PointSetType *landmarks)
{
itkDebugMacro("setting TargetLandmarks to " << landmarks);
if( this->m_TargetLandmarks != landmarks )
{
this->m_TargetLandmarks = landmarks;
this->UpdateParameters();
this->Modified();
}
}
/**
*
*/
template <typename TScalar, unsigned int NDimensions>
void
KernelTransform<TScalar, NDimensions>::ComputeG( const InputVectorType &,
GMatrixType & itkNotUsed(gmatrix) ) const
{
itkExceptionMacro(<< "ComputeG(vector,gmatrix) must be reimplemented"
<< " in subclasses of KernelTransform.");
}
/**
*
*/
template <typename TScalar, unsigned int NDimensions>
const typename KernelTransform<TScalar, NDimensions>::GMatrixType
& KernelTransform<TScalar, NDimensions>::ComputeReflexiveG(PointsIterator) const
{
m_GMatrix.fill(NumericTraits<TScalar>::Zero);
m_GMatrix.fill_diagonal(m_Stiffness);
return m_GMatrix;
}
/**
* Default implementation of the the method. This can be overloaded
* in transforms whose kernel produce diagonal G matrices.
*/
template <typename TScalar, unsigned int NDimensions>
void
KernelTransform<TScalar, NDimensions>::ComputeDeformationContribution(const InputPointType & thisPoint,
OutputPointType & result) const
{
PointIdentifier numberOfLandmarks = this->m_SourceLandmarks
->GetNumberOfPoints();
PointsIterator sp = this->m_SourceLandmarks->GetPoints()->Begin();
GMatrixType Gmatrix;
for( unsigned int lnd = 0; lnd < numberOfLandmarks; lnd++ )
{
this->ComputeG(thisPoint - sp->Value(), Gmatrix);
for( unsigned int dim = 0; dim < NDimensions; dim++ )
{
for( unsigned int odim = 0; odim < NDimensions; odim++ )
{
result[odim] += Gmatrix(dim, odim) * m_DMatrix(dim, lnd);
}
}
++sp;
}
}
/**
*
*/
template <typename TScalar, unsigned int NDimensions>
void KernelTransform<TScalar, NDimensions>
::ComputeD(void)
{
PointIdentifier numberOfLandmarks = this->m_SourceLandmarks
->GetNumberOfPoints();
PointsIterator sp = this->m_SourceLandmarks->GetPoints()->Begin();
PointsIterator tp = this->m_TargetLandmarks->GetPoints()->Begin();
PointsIterator end = this->m_SourceLandmarks->GetPoints()->End();
this->m_Displacements->Reserve(numberOfLandmarks);
typename VectorSetType::Iterator vt = this->m_Displacements->Begin();
while( sp != end )
{
vt->Value() = tp->Value() - sp->Value();
++vt;
++sp;
++tp;
}
}
/**
*
*/
template <typename TScalar, unsigned int NDimensions>
void KernelTransform<TScalar, NDimensions>
::ComputeWMatrix(void)
{
typedef vnl_svd<TScalar> SVDSolverType;
this->ComputeL();
this->ComputeY();
SVDSolverType svd(this->m_LMatrix, 1e-8);
this->m_WMatrix = svd.solve(this->m_YMatrix);
this->ReorganizeW();
}
/**
*
*/
template <typename TScalar, unsigned int NDimensions>
void KernelTransform<TScalar, NDimensions>::ComputeL(void)
{
PointIdentifier numberOfLandmarks = this->m_SourceLandmarks
->GetNumberOfPoints();
vnl_matrix<TScalar> O2(NDimensions * ( NDimensions + 1 ),
NDimensions * ( NDimensions + 1 ), 0);
this->ComputeP();
this->ComputeK();
this->m_LMatrix.set_size(
NDimensions * ( numberOfLandmarks + NDimensions + 1 ),
NDimensions * ( numberOfLandmarks + NDimensions + 1 ) );
this->m_LMatrix.fill(0.0);
this->m_LMatrix.update(this->m_KMatrix, 0, 0);
this->m_LMatrix.update( this->m_PMatrix, 0, this->m_KMatrix.columns() );
this->m_LMatrix.update(this->m_PMatrix.transpose(),
this->m_KMatrix.rows(), 0);
this->m_LMatrix.update( O2, this->m_KMatrix.rows(),
this->m_KMatrix.columns() );
}
/**
*
*/
template <typename TScalar, unsigned int NDimensions>
void KernelTransform<TScalar, NDimensions>::ComputeK(void)
{
PointIdentifier numberOfLandmarks = this->m_SourceLandmarks
->GetNumberOfPoints();
GMatrixType G;
this->ComputeD();
this->m_KMatrix.set_size(NDimensions * numberOfLandmarks,
NDimensions * numberOfLandmarks);
this->m_KMatrix.fill(0.0);
PointsIterator p1 = this->m_SourceLandmarks->GetPoints()->Begin();
PointsIterator end = this->m_SourceLandmarks->GetPoints()->End();
// K matrix is symmetric, so only evaluate the upper triangle and
// store the values in bot the upper and lower triangle
unsigned int i = 0;
while( p1 != end )
{
PointsIterator p2 = p1; // start at the diagonal element
unsigned int j = i;
// Compute the block diagonal element, i.e. kernel for pi->pi
G = this->ComputeReflexiveG(p1);
this->m_KMatrix.update(G, i * NDimensions, i * NDimensions);
++p2;
++j;
// Compute the upper (and copy into lower) triangular part of K
while( p2 != end )
{
const InputVectorType s = p1.Value() - p2.Value();
this->ComputeG(s, G);
// write value in upper and lower triangle of matrix
this->m_KMatrix.update(G, i * NDimensions, j * NDimensions);
this->m_KMatrix.update(G, j * NDimensions, i * NDimensions);
++p2;
++j;
}
++p1;
++i;
}
}
/**
*
*/
template <typename TScalar, unsigned int NDimensions>
void KernelTransform<TScalar, NDimensions>::ComputeP()
{
PointIdentifier numberOfLandmarks = this->m_SourceLandmarks
->GetNumberOfPoints();
IMatrixType I;
IMatrixType temp;
InputPointType p;
p.Fill( NumericTraits<ScalarType>::Zero );
I.set_identity();
this->m_PMatrix.set_size( NDimensions * numberOfLandmarks,
NDimensions * ( NDimensions + 1 ) );
this->m_PMatrix.fill(0.0);
for( unsigned long i = 0; i < numberOfLandmarks; i++ )
{
this->m_SourceLandmarks->GetPoint(i, &p);
for( unsigned int j = 0; j < NDimensions; j++ )
{
temp = I * p[j];
this->m_PMatrix.update(temp, i * NDimensions, j * NDimensions);
}
this->m_PMatrix.update(I, i * NDimensions, NDimensions * NDimensions);
}
}
/**
*
*/
template <typename TScalar, unsigned int NDimensions>
void KernelTransform<TScalar, NDimensions>::ComputeY(void)
{
PointIdentifier numberOfLandmarks = this->m_SourceLandmarks
->GetNumberOfPoints();
typename VectorSetType::ConstIterator displacement =
this->m_Displacements->Begin();
this->m_YMatrix.set_size(NDimensions * ( numberOfLandmarks + NDimensions + 1 ), 1);
this->m_YMatrix.fill(0.0);
for( unsigned int i = 0; i < numberOfLandmarks; i++ )
{
for( unsigned int j = 0; j < NDimensions; j++ )
{
this->m_YMatrix.put(i * NDimensions + j, 0, displacement.Value()[j]);
}
++displacement;
}
for( unsigned int i = 0; i < NDimensions * ( NDimensions + 1 ); i++ )
{
this->m_YMatrix.put(numberOfLandmarks * NDimensions + i, 0, 0);
}
}
/**
*
*/
template <typename TScalar, unsigned int NDimensions>
void
KernelTransform<TScalar, NDimensions>
::ReorganizeW(void)
{
PointIdentifier numberOfLandmarks = this->m_SourceLandmarks
->GetNumberOfPoints();
// The deformable (non-affine) part of the registration goes here
this->m_DMatrix.set_size(NDimensions, numberOfLandmarks);
unsigned int ci = 0;
for( unsigned int lnd = 0; lnd < numberOfLandmarks; lnd++ )
{
for( unsigned int dim = 0; dim < NDimensions; dim++ )
{
this->m_DMatrix(dim, lnd) = this->m_WMatrix(ci++, 0);
}
}
// This matrix holds the rotational part of the Affine component
for( unsigned int j = 0; j < NDimensions; j++ )
{
for( unsigned int i = 0; i < NDimensions; i++ )
{
this->m_AMatrix(i, j) = this->m_WMatrix(ci++, 0);
}
}
// This vector holds the translational part of the Affine component
for( unsigned int k = 0; k < NDimensions; k++ )
{
this->m_BVector(k) = this->m_WMatrix(ci++, 0);
}
// release WMatrix memory by assigning a small one.
this->m_WMatrix = WMatrixType(1, 1);
}
/**
*
*/
template <typename TScalar, unsigned int NDimensions>
typename KernelTransform<TScalar, NDimensions>::OutputPointType
KernelTransform<TScalar, NDimensions>
::TransformPoint(const InputPointType & thisPoint) const
{
OutputPointType result;
typedef typename OutputPointType::ValueType ValueType;
result.Fill(NumericTraits<ValueType>::Zero);
//TODO: It is unclear if the following line is needed.
this->ComputeDeformationContribution(thisPoint, result);
// Add the rotational part of the Affine component
for( unsigned int j = 0; j < NDimensions; j++ )
{
for( unsigned int i = 0; i < NDimensions; i++ )
{
result[i] += this->m_AMatrix(i, j) * thisPoint[j];
}
}
// This vector holds the translational part of the Affine component
for( unsigned int k = 0; k < NDimensions; k++ )
{
result[k] += this->m_BVector(k) + thisPoint[k];
}
return result;
}
template <typename TScalar, unsigned int NDimensions>
void
KernelTransform<TScalar, NDimensions>
::ComputeJacobianWithRespectToParameters(const InputPointType &, JacobianType & jacobian) const
{
jacobian.Fill(0.0);
// FIXME: TODO
// The Jacobian should be computable in terms of the matrices
// used to Transform points...
itkExceptionMacro(<< "Get[Local]Jacobian must be implemented in subclasses"
<< " of KernelTransform.");
}
// Set the parameters
// NOTE that in this transformation both the Source and Target
// landmarks could be considered as parameters. It is assumed
// here that the Target landmarks are provided by the user and
// are not changed during the optimization process required for
// registration.
template <typename TScalar, unsigned int NDimensions>
void
KernelTransform<TScalar, NDimensions>::SetParameters(const ParametersType & parameters)
{
// Save parameters. Needed for proper operation of TransformUpdateParameters.
if( ¶meters != &(this->m_Parameters) )
{
this->m_Parameters = parameters;
}
typename PointsContainer::Pointer landmarks = PointsContainer::New();
const unsigned int numberOfLandmarks = parameters.Size() / NDimensions;
landmarks->Reserve(numberOfLandmarks);
PointsIterator itr = landmarks->Begin();
PointsIterator end = landmarks->End();
InputPointType landMark;
unsigned int pcounter = 0;
while( itr != end )
{
for( unsigned int dim = 0; dim < NDimensions; dim++ )
{
landMark[dim] = parameters[pcounter];
++pcounter;
}
itr.Value() = landMark;
++itr;
}
this->m_SourceLandmarks->SetPoints(landmarks);
// Modified is always called since we just have a pointer to the
// parameters and cannot know if the parameters have changed.
this->Modified();
}
// Set the fixed parameters
// Since the API of the SetParameters() function sets the
// source landmarks, this function was added to support the
// setting of the target landmarks, and allowing the Transform
// I/O mechanism to be supported.
template <typename TScalar, unsigned int NDimensions>
void
KernelTransform<TScalar, NDimensions>::SetFixedParameters(const ParametersType & parameters)
{
typename PointsContainer::Pointer landmarks = PointsContainer::New();
const unsigned int numberOfLandmarks = parameters.Size() / NDimensions;
landmarks->Reserve(numberOfLandmarks);
PointsIterator itr = landmarks->Begin();
PointsIterator end = landmarks->End();
InputPointType landMark;
unsigned int pcounter = 0;
while( itr != end )
{
for( unsigned int dim = 0; dim < NDimensions; dim++ )
{
landMark[dim] = parameters[pcounter];
++pcounter;
}
itr.Value() = landMark;
++itr;
}
this->m_TargetLandmarks->SetPoints(landmarks);
}
// Update parameters array
// They are the components of all the landmarks in the source space
template <typename TScalar, unsigned int NDimensions>
void
KernelTransform<TScalar, NDimensions>::UpdateParameters(void) const
{
this->m_Parameters = ParametersType(this->m_SourceLandmarks
->GetNumberOfPoints()
* NDimensions);
PointsIterator itr = this->m_SourceLandmarks->GetPoints()->Begin();
PointsIterator end = this->m_SourceLandmarks->GetPoints()->End();
unsigned int pcounter = 0;
while( itr != end )
{
InputPointType landmark = itr.Value();
for( unsigned int dim = 0; dim < NDimensions; dim++ )
{
this->m_Parameters[pcounter] = landmark[dim];
++pcounter;
}
++itr;
}
}
// Get the parameters
// They are the components of all the landmarks in the source space
template <typename TScalar, unsigned int NDimensions>
const typename KernelTransform<TScalar, NDimensions>::ParametersType
& KernelTransform<TScalar, NDimensions>::GetParameters(void) const
{
this->UpdateParameters();
return this->m_Parameters;
}
// Get the fixed parameters
// This returns the target landmark locations
// This was added to support the Transform Reader/Writer mechanism
template <typename TScalar, unsigned int NDimensions>
const typename KernelTransform<TScalar, NDimensions>::ParametersType
& KernelTransform<TScalar, NDimensions>::GetFixedParameters(void) const
{
this->m_FixedParameters = ParametersType(this->m_TargetLandmarks
->GetNumberOfPoints()
* NDimensions);
PointsIterator itr = this->m_TargetLandmarks->GetPoints()->Begin();
PointsIterator end = this->m_TargetLandmarks->GetPoints()->End();
unsigned int pcounter = 0;
while( itr != end )
{
InputPointType landmark = itr.Value();
for( unsigned int dim = 0; dim < NDimensions; dim++ )
{
this->m_FixedParameters[pcounter] = landmark[dim];
++pcounter;
}
++itr;
}
return this->m_FixedParameters;
}
template <typename TScalar, unsigned int NDimensions>
void
KernelTransform<TScalar, NDimensions>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
if( this->m_SourceLandmarks )
{
os << indent << "SourceLandmarks: " << std::endl;
this->m_SourceLandmarks->Print( os, indent.GetNextIndent() );
}
if( this->m_TargetLandmarks )
{
os << indent << "TargetLandmarks: " << std::endl;
this->m_TargetLandmarks->Print( os, indent.GetNextIndent() );
}
if( this->m_Displacements )
{
os << indent << "Displacements: " << std::endl;
this->m_Displacements->Print( os, indent.GetNextIndent() );
}
os << indent << "Stiffness: " << this->m_Stiffness << std::endl;
}
} // namespace itk
#endif
|