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
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* 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
*
* https://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_h
#define itkKernelTransform_h
#include "itkTransform.h"
#include "itkMatrix.h"
#include "itkPointSet.h"
#include <deque>
#include <cmath>
#include "vnl/vnl_matrix_fixed.h"
#include "vnl/vnl_matrix.h"
#include "vnl/vnl_vector.h"
#include "vnl/vnl_vector_fixed.h"
#include "vnl/algo/vnl_svd.h"
namespace itk
{
/**
* \class KernelTransform
* Intended to be a base class for elastic body spline and thin plate spline.
* This is implemented in as straightforward a manner as possible from the
* IEEE TMI paper by Davis, Khotanzad, Flamig, and Harms, Vol. 16,
* No. 3 June 1997. Notation closely follows their paper, so if you have it
* in front of you, this code will make a lot more sense.
*
* KernelTransform:
* Provides support for defining source and target landmarks
* Defines a number of data types used in the computations
* Defines the mathematical framework used to compute all splines,
* so that subclasses need only provide a kernel specific to
* that spline
*
* This formulation allows the stiffness of the spline to
* be adjusted, allowing the spline to vary from interpolating the
* landmarks to approximating the landmarks. This part of the
* formulation is based on the short paper by R. Sprengel, K. Rohr,
* H. Stiehl. "Thin-Plate Spline Approximation for Image
* Registration". In 18th International Conference of the IEEE
* Engineering in Medicine and Biology Society. 1996.
*
*
* \ingroup ITKTransform
*/
template <typename TParametersValueType, unsigned int VDimension>
class ITK_TEMPLATE_EXPORT KernelTransform : public Transform<TParametersValueType, VDimension, VDimension>
{
public:
ITK_DISALLOW_COPY_AND_MOVE(KernelTransform);
/** Standard class type aliases. */
using Self = KernelTransform;
using Superclass = Transform<TParametersValueType, VDimension, VDimension>;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;
/** \see LightObject::GetNameOfClass() */
itkOverrideGetNameOfClassMacro(KernelTransform);
/** New macro for creation of through a Smart Pointer */
itkNewMacro(Self);
/** Dimension of the domain space. */
static constexpr unsigned int SpaceDimension = VDimension;
/** Scalar type. */
using typename Superclass::ScalarType;
/** Parameters type. */
using typename Superclass::FixedParametersType;
using typename Superclass::ParametersType;
/** Jacobian types. */
using typename Superclass::JacobianType;
using typename Superclass::JacobianPositionType;
using typename Superclass::InverseJacobianPositionType;
/** Transform category type. */
using typename Superclass::TransformCategoryEnum;
/** Standard coordinate point type for this class. */
using typename Superclass::InputPointType;
using typename Superclass::OutputPointType;
/** Standard vector type for this class. */
using typename Superclass::InputVectorType;
using typename Superclass::OutputVectorType;
/** Standard covariant vector type for this class */
using typename Superclass::InputCovariantVectorType;
using typename Superclass::OutputCovariantVectorType;
/** Standard vnl_vector type for this class. */
using typename Superclass::InputVnlVectorType;
using typename Superclass::OutputVnlVectorType;
/** The number of parameters defining this transform. */
using typename Superclass::NumberOfParametersType;
/** PointList type alias. This type is used for maintaining lists of points,
* specifically, the source and target landmark lists. */
using PointSetTraitsType =
DefaultStaticMeshTraits<TParametersValueType, VDimension, VDimension, TParametersValueType, TParametersValueType>;
using PointSetType = PointSet<InputPointType, VDimension, PointSetTraitsType>;
using PointSetPointer = typename PointSetType::Pointer;
using PointsContainer = typename PointSetType::PointsContainer;
using PointsIterator = typename PointSetType::PointsContainerIterator;
using PointsConstIterator = typename PointSetType::PointsContainerConstIterator;
using PointIdentifier = typename PointSetType::PointIdentifier;
/** VectorSet type alias. */
using VectorSetType = itk::VectorContainer<SizeValueType, InputVectorType>;
using VectorSetPointer = typename VectorSetType::Pointer;
/** Get/Set the source landmarks list, which we will denote \f$ p \f$. */
itkGetModifiableObjectMacro(SourceLandmarks, PointSetType); // NOTE: This is used to circumvent the SetTargetLandmarks
virtual void
SetSourceLandmarks(PointSetType *);
/** Get the target landmarks list, which we will denote \f$ q \f$. */
itkGetModifiableObjectMacro(TargetLandmarks, PointSetType); // NOTE: This is used to circumvent the SetTargetLandmarks
virtual void
SetTargetLandmarks(PointSetType *);
/** Get the displacements list, which we will denote \f$ d \f$,
* where \f$ d_i = q_i - p_i \f$. */
itkGetModifiableObjectMacro(Displacements, VectorSetType);
/** Compute W matrix. */
void
ComputeWMatrix();
/** Compute the position of point in the new space */
OutputPointType
TransformPoint(const InputPointType & thisPoint) const override;
/** These vector transforms are not implemented for this transform */
using Superclass::TransformVector;
OutputVectorType
TransformVector(const InputVectorType &) const override
{
itkExceptionMacro("TransformVector(const InputVectorType &) is not implemented for KernelTransform");
}
OutputVnlVectorType
TransformVector(const InputVnlVectorType &) const override
{
itkExceptionMacro("TransformVector(const InputVnlVectorType &) is not implemented for KernelTransform");
}
/** Method to transform a CovariantVector. */
using Superclass::TransformCovariantVector;
OutputCovariantVectorType
TransformCovariantVector(const InputCovariantVectorType &) const override
{
itkExceptionMacro(
<< "TransformCovariantVector(const InputCovariantVectorType &) is not implemented for KernelTransform");
}
/** 'I' (identity) matrix type alias. */
using IMatrixType = vnl_matrix_fixed<TParametersValueType, VDimension, VDimension>;
/** Compute the Jacobian Matrix of the transformation at one point */
void
ComputeJacobianWithRespectToParameters(const InputPointType & p, JacobianType & jacobian) const override;
void
ComputeJacobianWithRespectToPosition(const InputPointType &, JacobianPositionType &) const override
{
itkExceptionMacro("ComputeJacobianWithRespectToPosition not yet implemented "
"for "
<< this->GetNameOfClass());
}
using Superclass::ComputeJacobianWithRespectToPosition;
/** Set the Transformation Parameters and update the internal transformation.
* The parameters represent the source landmarks. Each landmark point is
* represented by VDimension doubles. All the landmarks are concatenated to
* form one flat Array<double>. */
void
SetParameters(const ParametersType &) override;
/** Set Transform Fixed Parameters:
* To support the transform file writer this function was
* added to set the target landmarks similar to the
* SetParameters function setting the source landmarks
*/
void
SetFixedParameters(const FixedParametersType &) override;
/** Update the Parameters array from the landmarks coordinates. */
virtual void
UpdateParameters() const;
/** Get the Transformation Parameters - Gets the Source Landmarks */
const ParametersType &
GetParameters() const override;
/** Get Transform Fixed Parameters - Gets the Target Landmarks */
const FixedParametersType &
GetFixedParameters() const override;
/** This transform is not linear, because the transformation of a linear
* combination of points is not equal to the linear combination of the
* transformations of individual points */
TransformCategoryEnum
GetTransformCategory() const override
{
return Self::TransformCategoryEnum::Spline;
}
/** Stiffness of the spline. A stiffness of zero results in the
* standard interpolating spline. A non-zero stiffness allows the
* spline to approximate rather than interpolate the landmarks.
* Stiffness values are usually rather small, typically in the range
* of 0.001 to 0.1. The approximating spline formulation is based on
* the short paper by R. Sprengel, K. Rohr, H. Stiehl. "Thin-Plate
* Spline Approximation for Image Registration". In 18th
* International Conference of the IEEE Engineering in Medicine and
* Biology Society. 1996.
*/
itkSetClampMacro(Stiffness, double, 0.0, NumericTraits<double>::max());
itkGetConstMacro(Stiffness, double);
protected:
KernelTransform();
~KernelTransform() override = default;
void
PrintSelf(std::ostream & os, Indent indent) const override;
public:
/** 'G' matrix type alias. */
using GMatrixType = vnl_matrix_fixed<TParametersValueType, VDimension, VDimension>;
/** 'L' matrix type alias. */
using LMatrixType = vnl_matrix<TParametersValueType>;
/** 'K' matrix type alias. */
using KMatrixType = vnl_matrix<TParametersValueType>;
/** 'P' matrix type alias. */
using PMatrixType = vnl_matrix<TParametersValueType>;
/** 'Y' matrix type alias. */
using YMatrixType = vnl_matrix<TParametersValueType>;
/** 'W' matrix type alias. */
using WMatrixType = vnl_matrix<TParametersValueType>;
/** 'D' matrix type alias. Deformation component */
using DMatrixType = vnl_matrix<TParametersValueType>;
/** 'A' matrix type alias. Rotational part of the Affine component */
using AMatrixType = vnl_matrix_fixed<TParametersValueType, VDimension, VDimension>;
/** 'B' matrix type alias. Translational part of the Affine component */
using BMatrixType = vnl_vector_fixed<TParametersValueType, VDimension>;
/** Row matrix type alias. */
using RowMatrixType = vnl_matrix_fixed<TParametersValueType, 1, VDimension>;
/** Column matrix type alias. */
using ColumnMatrixType = vnl_matrix_fixed<TParametersValueType, VDimension, 1>;
protected:
/** Compute G(x)
* This is essentially the kernel of the transform.
* By overriding this method, we can obtain (among others):
* Elastic body spline
* Thin plate spline
* Volume spline */
virtual void
ComputeG(const InputVectorType & landmarkVector, GMatrixType & gmatrix) const;
/** Compute a G(x) for a point to itself (i.e. for the block diagonal
* elements of the matrix K. Parameter indicates for which landmark
* the reflexive G is to be computed. The default implementation for
* the reflexive contribution is a diagonal matrix where the diagonal
* elements are the stiffness of the spline.
*
* \warning this method is not thread-safe. However this method is called
* only through ComputeWMatrix() that is itself normally called from a single
* thread during the initialization of the Transform. */
virtual const GMatrixType & ComputeReflexiveG(PointsIterator) const;
/** Compute the contribution of the landmarks weighted by the kernel function
to the global deformation of the space */
virtual void
ComputeDeformationContribution(const InputPointType & thisPoint, OutputPointType & result) const;
/** Compute K matrix. */
void
ComputeK();
/** Compute L matrix. */
void
ComputeL();
/** Compute P matrix. */
void
ComputeP();
/** Compute Y matrix. */
void
ComputeY();
/** Compute displacements \f$ q_i - p_i \f$. */
void
ComputeD();
/** Reorganize the components of W into
D (deformable), A (rotation part of affine)
and B (translational part of affine ) components.
\warning This method release the memory of the W Matrix */
void
ReorganizeW();
/** Stiffness parameter */
double m_Stiffness{};
/** The list of displacements.
* d[i] = q[i] - p[i]; */
VectorSetPointer m_Displacements{};
/** The L matrix. */
LMatrixType m_LMatrix{};
/** The K matrix. */
KMatrixType m_KMatrix{};
/** The P matrix. */
PMatrixType m_PMatrix{};
/** The Y matrix. */
YMatrixType m_YMatrix{};
/** The W matrix. */
WMatrixType m_WMatrix{};
/** The Deformation matrix.
This is an auxiliary matrix that will hold the
Deformation (non-affine) part of the transform.
Those are the coefficients that will multiply the
Kernel function */
DMatrixType m_DMatrix{};
/** Rotational/Shearing part of the Affine component of the Transformation */
AMatrixType m_AMatrix{};
/** Translational part of the Affine component of the Transformation */
BMatrixType m_BVector{};
/** The G matrix.
* It is made mutable because m_GMatrix was made an ivar
* only to avoid copying the matrix at return time */
mutable GMatrixType m_GMatrix{};
/** Has the W matrix been computed? */
bool m_WMatrixComputed{};
/** Identity matrix. */
IMatrixType m_I{};
/** The list of source landmarks, denoted 'p'. */
PointSetPointer m_SourceLandmarks{};
/** The list of target landmarks, denoted 'q'. */
PointSetPointer m_TargetLandmarks{};
private:
};
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
# include "itkKernelTransform.hxx"
#endif
#endif // itkKernelTransform_h
|