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
|
/*=========================================================================
*
* 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 itkObjectToObjectMetric_h
#define itkObjectToObjectMetric_h
#include "itkObjectToObjectMetricBase.h"
#include "itkDisplacementFieldTransform.h"
#include "itkImage.h"
#include "itkObject.h"
#include "itkPointSet.h"
#include "itkTransform.h"
namespace itk
{
/**
* \class ObjectToObjectMetric
* \brief Computes similarity between regions of two objects.
*
* This class is templated over the dimensionality of the two input objects.
* This is the abstract templated base class for a hierarchy of similarity metrics
* that may, in derived classes, operate on meshes, images, etc.
* This class computes a value that measures the similarity between the two
* objects.
*
* Derived classes must provide implementations for:
* GetValue
* GetDerivative
* GetValueAndDerivative
* SupportsArbitraryVirtualDomainSamples
*
* Similarity is evaluated using fixed and moving transforms.
* Both transforms are initialized to an IdentityTransform, and can be
* set by the user using SetFixedTransform() and SetMovingTransform().
*
* Virtual Domain
*
* This class uses a virtual reference space. This space defines the resolution
* at which the evaluation is performed, as well as the physical coordinate
* system. This is useful for unbiased registration. The virtual domain is stored
* in the m_VirtualDomain member, but this is subject to change so the convenience
* methods GetVirtualSpacing(), GetVirtualDirection() and GetVirtualOrigin() should
* be used whenever possible to retrieve virtual domain information. The region over which
* metric evaluation is performed is taken from the virtual image buffered region.
*
* The user can define a virtual domain by calling either
* \c SetVirtualDomain or \c SetVirtualDomainFromImage. See these
* methods for details. Derived classes may automatically assign a virtual domain
* if the user has not assigned one by initialization time.
*
* If the virtual domain is left undefined by the user and by derived classes,
* then unit or zero values are returned for GetVirtualSpacing(),
* GetVirtualDirection() and GetVirtualOrigin(), as appropriate. The virtual region is left
* undefined and an attempt to retrieve it via GetVirtualRegion() will generate an exception.
* The m_VirtualImage member will be nullptr.
*
* During evaluation, derived classes should verify that points are within the virtual domain
* and thus valid, as appropriate for the needs of the metric. When points are deemed invalid
* the number of valid points returned by GetNumberOfValidPoints() should reflect this.
*
* \note Transform Optimization
* This hierarchy currently assumes only the moving transform is 'active',
* i.e. only the moving transform is being optimized when used in an optimizer.
* Methods relevant to transform optimization such as GetNumberOfParameters(),
* UpdateTransformParameters() are passed on to the active transform.
* The eventual goal however is to allow for either moving, fixed or both
* transforms to be active within a single metric.
*
* \ingroup ITKOptimizersv4
*/
template <unsigned int TFixedDimension,
unsigned int TMovingDimension,
typename TVirtualImage = Image<double, TFixedDimension>,
typename TParametersValueType = double>
class ITK_TEMPLATE_EXPORT ObjectToObjectMetric : public ObjectToObjectMetricBaseTemplate<TParametersValueType>
{
public:
ITK_DISALLOW_COPY_AND_MOVE(ObjectToObjectMetric);
/** Standard class type aliases. */
using Self = ObjectToObjectMetric;
using Superclass = ObjectToObjectMetricBaseTemplate<TParametersValueType>;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;
/** \see LightObject::GetNameOfClass() */
itkOverrideGetNameOfClassMacro(ObjectToObjectMetric);
/** Type used for representing object components */
using CoordinateRepresentationType = TParametersValueType;
/** Type for internal computations */
using InternalComputationValueType = TParametersValueType;
/** Type of the measure. */
using typename Superclass::MeasureType;
/** Type of object. */
using ObjectType = typename Superclass::Object;
/** Type of the derivative. */
using typename Superclass::DerivativeType;
using typename Superclass::DerivativeValueType;
/** Type of the parameters. */
using typename Superclass::ParametersType;
using typename Superclass::NumberOfParametersType;
using typename Superclass::GradientSourceEnum;
/** Dimension type */
using DimensionType = SizeValueType;
/** Object dimension accessors */
static constexpr DimensionType FixedDimension = TFixedDimension;
static constexpr DimensionType MovingDimension = TMovingDimension;
static constexpr DimensionType VirtualDimension = TVirtualImage::ImageDimension;
/** Types for the virtual domain */
using VirtualImageType = TVirtualImage;
using VirtualImagePointer = typename VirtualImageType::Pointer;
using VirtualImageConstPointer = typename VirtualImageType::ConstPointer;
using VirtualPixelType = typename VirtualImageType::PixelType;
using VirtualRegionType = typename VirtualImageType::RegionType;
using VirtualSizeType = typename VirtualRegionType::SizeType;
using VirtualSpacingType = typename VirtualImageType::SpacingType;
using VirtualOriginType = typename VirtualImageType::PointType;
using VirtualPointType = typename VirtualImageType::PointType;
using VirtualDirectionType = typename VirtualImageType::DirectionType;
using VirtualRadiusType = typename VirtualImageType::SizeType;
using VirtualIndexType = typename VirtualImageType::IndexType;
/** Point set in the virtual domain */
using VirtualPointSetType = PointSet<VirtualPixelType, Self::VirtualDimension>;
using VirtualPointSetPointer = typename VirtualPointSetType::Pointer;
/** Type of the Transform Base classes */
using MovingTransformType = Transform<TParametersValueType, TVirtualImage::ImageDimension, TMovingDimension>;
using FixedTransformType = Transform<TParametersValueType, TVirtualImage::ImageDimension, TFixedDimension>;
using FixedTransformPointer = typename FixedTransformType::Pointer;
using FixedInputPointType = typename FixedTransformType::InputPointType;
using FixedOutputPointType = typename FixedTransformType::OutputPointType;
using FixedTransformParametersType = typename FixedTransformType::ParametersType;
using MovingTransformPointer = typename MovingTransformType::Pointer;
using MovingInputPointType = typename MovingTransformType::InputPointType;
using MovingOutputPointType = typename MovingTransformType::OutputPointType;
using MovingTransformParametersType = typename MovingTransformType::ParametersType;
/** Jacobian type. This is the same for all transforms */
using JacobianType = typename FixedTransformType::JacobianType;
using FixedTransformJacobianType = typename FixedTransformType::JacobianType;
using MovingTransformJacobianType = typename MovingTransformType::JacobianType;
/** DisplacementFieldTransform types for working with local-support transforms */
using MovingDisplacementFieldTransformType =
DisplacementFieldTransform<CoordinateRepresentationType, Self::MovingDimension>;
void
Initialize() override;
NumberOfParametersType
GetNumberOfParameters() const override;
NumberOfParametersType
GetNumberOfLocalParameters() const override;
void
SetParameters(ParametersType & params) override;
const ParametersType &
GetParameters() const override;
bool
HasLocalSupport() const override;
void
UpdateTransformParameters(const DerivativeType & derivative, TParametersValueType factor) override;
/** Connect the fixed transform. */
itkSetObjectMacro(FixedTransform, FixedTransformType);
/** Get a pointer to the fixed transform. */
itkGetModifiableObjectMacro(FixedTransform, FixedTransformType);
/** Connect the moving transform. */
itkSetObjectMacro(MovingTransform, MovingTransformType);
/** Get a pointer to the moving transform. */
itkGetModifiableObjectMacro(MovingTransform, MovingTransformType);
/** Connect the moving transform using a backwards-compatible name.
* This assigns the input transform to the moving transform. */
void
SetTransform(MovingTransformType * transform);
/** Get the moving transform using a backwards-compatible name */
const MovingTransformType *
GetTransform();
/** Get the number of valid points after a call to evaluate the
* metric. */
itkGetConstMacro(NumberOfValidPoints, SizeValueType);
/** Define the virtual reference space. This space defines the resolution
* at which the registration is performed as well as the physical coordinate
* system. Useful for unbiased registration.
* This method will allocate \c m_VirtualImage with the passed
* information, with the pixel buffer left unallocated.
* Metric evaluation will be performed within the constraints of the virtual
* domain depending on implementation in derived classes.
* A default domain is created during initialization in derived
* classes according to their need.
* \param spacing spacing
* \param origin origin
* \param direction direction
* \param region region is used to set all image regions.
*
* \sa SetVirtualDomainFromImage
*/
void
SetVirtualDomain(const VirtualSpacingType & spacing,
const VirtualOriginType & origin,
const VirtualDirectionType & direction,
const VirtualRegionType & region);
/** Use a virtual domain image to define the virtual reference space.
* \sa SetVirtualDomain */
void
SetVirtualDomainFromImage(const VirtualImageType * virtualImage);
/** Returns a flag. True if arbitrary virtual domain points will
* always correspond to data points. False if not. For example,
* point-set metrics return false because only some virtual domain
* points will correspond to points within the point sets. */
virtual bool
SupportsArbitraryVirtualDomainSamples() const = 0;
/** Return a timestamp relating to the virtual domain.
* This returns the greater of the metric timestamp and the
* virtual domain image timestamp. This allows us to
* capture if the virtual domain image is changed by the user
* after being assigned to the metric. */
virtual const TimeStamp &
GetVirtualDomainTimeStamp() const;
/** Accessors for the virtual domain spacing.
* Returns unit spacing if a virtual domain is undefined. */
VirtualSpacingType
GetVirtualSpacing() const;
/** Accessor for virtual domain origin.
* Returns zero origin if a virtual domain is undefined. */
VirtualOriginType
GetVirtualOrigin() const;
/** Accessor for virtual domain direction.
* Returns unit direction if a virtual domain is undefined. */
VirtualDirectionType
GetVirtualDirection() const;
/** Return the virtual domain region, which is retrieved from
* the m_VirtualImage buffered region. */
const VirtualRegionType &
GetVirtualRegion() const;
itkGetModifiableObjectMacro(VirtualImage, VirtualImageType);
/** Computes an offset for accessing parameter data from a virtual domain
* index. Relevant for metrics with local-support transforms, to access
* parameter or derivative memory that is stored linearly in a 1D array.
* The result is the offset (1D array index) to the first of N parameters
* corresponding to the given virtual index, where N is the number of
* local parameters.
* \param index the virtual index to convert
* \param numberOfLocalParameters corresponding to the transform
**/
OffsetValueType
ComputeParameterOffsetFromVirtualIndex(const VirtualIndexType & index,
const NumberOfParametersType & numberOfLocalParameters) const;
/** Computes an offset for accessing parameter data from a virtual domain
* point. Relevant for metrics with local-support transforms, to access
* parameter or derivative memory that is stored linearly in a 1D array.
* The result is the offset (1D array index) to the first of N parameters
* corresponding to the given virtual index, where N is the number of
* local parameters.
* \param point the virtual point to convert
* \param numberOfLocalParameters corresponding to the transform
**/
OffsetValueType
ComputeParameterOffsetFromVirtualPoint(const VirtualPointType & point,
const NumberOfParametersType & numberOfLocalParameters) const;
/** Determine if a point is within the virtual domain.
* \note Returns true if the virtual domain has not been defined. This
* allows, for example, use in point set metrics where the virtual domain
* is implicitly defined by the point sets and transforms. */
bool
IsInsideVirtualDomain(const VirtualPointType & point) const;
bool
IsInsideVirtualDomain(const VirtualIndexType & index) const;
using MetricCategoryType = typename Superclass::MetricCategoryEnum;
/** Get metric category */
MetricCategoryType
GetMetricCategory() const override
{
return MetricCategoryType::OBJECT_METRIC;
}
protected:
ObjectToObjectMetric();
~ObjectToObjectMetric() override = default;
void
PrintSelf(std::ostream & os, Indent indent) const override;
/** Verify that virtual domain and displacement field are the same size
* and in the same physical space. */
virtual void
VerifyDisplacementFieldSizeAndPhysicalSpace();
bool
TransformPhysicalPointToVirtualIndex(const VirtualPointType &, VirtualIndexType &) const;
void
TransformVirtualIndexToPhysicalPoint(const VirtualIndexType &, VirtualPointType &) const;
/** If the moving transform is a DisplacementFieldTransform, return it.
* If the moving transform is a CompositeTransform, the routine will check if the
* first (last to be added) transform is a DisplacementFieldTransform, and if so return it.
* Otherwise, return nullptr. */
const MovingDisplacementFieldTransformType *
GetMovingDisplacementFieldTransform() const;
/** Check that the number of valid points is above a default
* minimum (zero). If not, then return false, and assign to 'value' a value
* indicating insufficient valid points were found during evaluation, and set
* the derivative to zero. A warning is also output.
* This functionality is provided as a separate method so derived classes
* can use it without hardcoding the details. */
bool
VerifyNumberOfValidPoints(MeasureType & value, DerivativeType & derivative) const;
/** Transforms */
FixedTransformPointer m_FixedTransform{};
MovingTransformPointer m_MovingTransform{};
VirtualImagePointer m_VirtualImage{};
/** Flag that is set when user provides a virtual domain, either via
* SetVirtualDomain() or SetVirtualDomainFromImage(). */
bool m_UserHasSetVirtualDomain{};
/** Store the number of points used during most recent value and derivative
* calculation.
* \sa VerifyNumberOfValidPoints() */
mutable SizeValueType m_NumberOfValidPoints{ 0 };
};
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
# include "itkObjectToObjectMetric.hxx"
#endif
#endif
|