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
|
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* 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 itkStackTransform_h
#define itkStackTransform_h
#include "itkAdvancedTransform.h"
#include "itkIndex.h"
#include <cassert>
namespace itk
{
/** \class StackTransform
* \brief Implements stack of transforms: one for every last dimension index.
*
* A list of transforms with dimension of Dimension - 1 is maintained:
* one for every last dimension index. This transform selects the right
* transform based on the last dimension index of the input point.
*
* \ingroup Transforms
*
*/
template <class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
class ITK_TEMPLATE_EXPORT StackTransform : public AdvancedTransform<TScalarType, NInputDimensions, NOutputDimensions>
{
public:
ITK_DISALLOW_COPY_AND_MOVE(StackTransform);
/** Standard class typedefs. */
using Self = StackTransform;
using Superclass = AdvancedTransform<TScalarType, NInputDimensions, NOutputDimensions>;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;
/** Run-time type information (and related methods). */
itkTypeMacro(StackTransform, AdvancedTransform);
/** (Reduced) dimension of the domain space. */
itkStaticConstMacro(InputSpaceDimension, unsigned int, NInputDimensions);
itkStaticConstMacro(OutputSpaceDimension, unsigned int, NOutputDimensions);
itkStaticConstMacro(ReducedInputSpaceDimension, unsigned int, NInputDimensions - 1);
itkStaticConstMacro(ReducedOutputSpaceDimension, unsigned int, NOutputDimensions - 1);
/** Typedefs from the Superclass. */
using typename Superclass::ScalarType;
using typename Superclass::ParametersType;
using typename Superclass::FixedParametersType;
using typename Superclass::NumberOfParametersType;
using typename Superclass::ParametersValueType;
using typename Superclass::JacobianType;
using typename Superclass::SpatialJacobianType;
using typename Superclass::JacobianOfSpatialJacobianType;
using typename Superclass::SpatialHessianType;
using typename Superclass::JacobianOfSpatialHessianType;
using typename Superclass::NonZeroJacobianIndicesType;
using typename Superclass::InputPointType;
using typename Superclass::InputVectorType;
using typename Superclass::OutputVectorType;
using typename Superclass::InputVnlVectorType;
using typename Superclass::OutputVnlVectorType;
using typename Superclass::OutputCovariantVectorType;
using typename Superclass::InputCovariantVectorType;
using typename Superclass::OutputPointType;
using typename Superclass::OutputVectorPixelType;
using typename Superclass::InputVectorPixelType;
/** Sub transform types, having a reduced dimension. */
using SubTransformType =
AdvancedTransform<TScalarType, Self::ReducedInputSpaceDimension, Self::ReducedOutputSpaceDimension>;
using SubTransformPointer = typename SubTransformType::Pointer;
using SubTransformJacobianType = typename SubTransformType::JacobianType;
/** Dimension - 1 point types. */
using SubTransformInputPointType = typename SubTransformType::InputPointType;
using SubTransformOutputPointType = typename SubTransformType::OutputPointType;
/** Array type for parameter vector instantiation. */
using ParametersArrayType = typename ParametersType::ArrayType;
/** Method to transform a point. */
OutputPointType
TransformPoint(const InputPointType & inputPoint) const override;
/** This returns a sparse version of the Jacobian of the transformation.
* In this class however, the Jacobian is not sparse.
* However, it is a useful function, since the Jacobian is passed
* by reference, which makes it threadsafe, unlike the normal
* GetJacobian function. */
void
GetJacobian(const InputPointType & inputPoint, JacobianType & jac, NonZeroJacobianIndicesType & nzji) const override;
/** Set the parameters. Checks if the number of parameters
* is correct and sets parameters of sub transforms. */
void
SetParameters(const ParametersType & param) override;
/** Get the parameters. Concatenates the parameters of the
* sub transforms. */
const ParametersType &
GetParameters() const override;
/** Set the fixed parameters. */
void
SetFixedParameters(const FixedParametersType & fixedParameters) override
{
const auto numberOfFixedParameters = fixedParameters.size();
if (numberOfFixedParameters < NumberOfGeneralFixedParametersOfStack)
{
itkExceptionMacro("The number of FixedParameters (" << numberOfFixedParameters << ") should be at least "
<< NumberOfGeneralFixedParametersOfStack);
}
if (Superclass::m_FixedParameters != fixedParameters)
{
Superclass::m_FixedParameters = fixedParameters;
CreateSubTransforms(FixedParametersType(fixedParameters.data_block() + NumberOfGeneralFixedParametersOfStack,
numberOfFixedParameters - NumberOfGeneralFixedParametersOfStack));
UpdateStackSpacingAndOrigin();
this->Modified();
}
}
/** Return the number of sub transforms that have been set. */
NumberOfParametersType
GetNumberOfParameters() const override
{
if (this->m_SubTransformContainer.empty())
{
return 0;
}
else
{
return this->m_SubTransformContainer.size() * m_SubTransformContainer[0]->GetNumberOfParameters();
}
}
/** Set/get number of transforms needed. */
void
SetNumberOfSubTransforms(const unsigned int num)
{
if (this->m_SubTransformContainer.size() != num)
{
this->m_SubTransformContainer.clear();
this->m_SubTransformContainer.resize(num);
this->Modified();
}
}
auto
GetNumberOfSubTransforms() const
{
return static_cast<unsigned>(m_SubTransformContainer.size());
}
/** Set/get stack transform parameters. */
itkSetMacro(StackSpacing, TScalarType);
itkGetConstMacro(StackSpacing, TScalarType);
itkSetMacro(StackOrigin, TScalarType);
itkGetConstMacro(StackOrigin, TScalarType);
/** Set the initial transform for sub transform i. */
void
SetSubTransform(unsigned int i, SubTransformType * transform)
{
this->m_SubTransformContainer[i] = transform;
this->Modified();
}
/** Sets the fixed parameters to the general fixed parameters of the stack + the fixed parameters of the first
* sub-transform (if any). */
void
UpdateFixedParameters()
{
const SubTransformType * const subTransform =
m_SubTransformContainer.empty() ? nullptr : m_SubTransformContainer.front();
this->UpdateFixedParametersInternally((subTransform == nullptr) ? FixedParametersType()
: subTransform->GetFixedParameters());
}
/** Set all sub transforms to transform. */
void
SetAllSubTransforms(const SubTransformType & transform)
{
const auto & fixedParametersOfSubTransform = transform.GetFixedParameters();
const auto & parametersOfSubTransform = transform.GetParameters();
UpdateFixedParametersInternally(fixedParametersOfSubTransform);
for (auto & subTransform : m_SubTransformContainer)
{
// Copy transform
SubTransformPointer transformcopy = dynamic_cast<SubTransformType *>(transform.CreateAnother().GetPointer());
transformcopy->SetFixedParameters(fixedParametersOfSubTransform);
transformcopy->SetParameters(parametersOfSubTransform);
// Set sub transform
subTransform = transformcopy;
}
}
/** Get a sub transform. */
SubTransformPointer
GetSubTransform(unsigned int i)
{
return this->m_SubTransformContainer[i];
}
/** Get number of nonzero Jacobian indices. */
NumberOfParametersType
GetNumberOfNonZeroJacobianIndices() const override;
protected:
StackTransform() = default;
~StackTransform() override = default;
// Indices of the general fixed parameters into the FixedParameters array, and the number of those parameters.
enum
{
IndexOfNumberOfSubTransforms,
IndexOfStackSpacing,
IndexOfStackOrigin,
NumberOfGeneralFixedParametersOfStack
};
void
CreateSubTransforms(const FixedParametersType & fixedParametersOfSubTransform)
{
assert(Superclass::m_FixedParameters.size() >= NumberOfGeneralFixedParametersOfStack);
const auto numberOfSubTransforms = Superclass::m_FixedParameters[IndexOfNumberOfSubTransforms];
if (numberOfSubTransforms >= 0.0 && numberOfSubTransforms <= UINT_MAX &&
static_cast<double>(static_cast<unsigned>(numberOfSubTransforms)) == numberOfSubTransforms)
{
m_SubTransformContainer.resize(static_cast<unsigned>(numberOfSubTransforms));
}
else
{
itkExceptionMacro("The FixedParameters element (" << numberOfSubTransforms
<< ") should be a valid number (the number of subtransforms).");
}
for (auto & subTransform : m_SubTransformContainer)
{
subTransform = this->CreateSubTransform();
subTransform->SetFixedParameters(fixedParametersOfSubTransform);
}
}
void
UpdateStackSpacingAndOrigin()
{
assert(Superclass::m_FixedParameters.size() >= NumberOfGeneralFixedParametersOfStack);
m_StackSpacing = Superclass::m_FixedParameters[IndexOfStackSpacing];
m_StackOrigin = Superclass::m_FixedParameters[IndexOfStackOrigin];
}
/** Sets the fixed parameters to the general fixed parameters of the stack + the specified fixed parameters of a
* sub-transform. */
virtual void
UpdateFixedParametersInternally(const FixedParametersType & fixedParametersOfSubTransform)
{
const auto numberOfFixedParametersOfSubTransform = fixedParametersOfSubTransform.size();
FixedParametersType & fixedParametersOfStack = this->Superclass::m_FixedParameters;
const auto minimumNumberOfFixedParametersOfStack =
NumberOfGeneralFixedParametersOfStack + numberOfFixedParametersOfSubTransform;
if (fixedParametersOfStack.size() < minimumNumberOfFixedParametersOfStack)
{
fixedParametersOfStack.set_size(minimumNumberOfFixedParametersOfStack);
}
fixedParametersOfStack[IndexOfNumberOfSubTransforms] = m_SubTransformContainer.size();
fixedParametersOfStack[IndexOfStackOrigin] = m_StackOrigin;
fixedParametersOfStack[IndexOfStackSpacing] = m_StackSpacing;
std::copy_n(fixedParametersOfSubTransform.begin(),
numberOfFixedParametersOfSubTransform,
fixedParametersOfStack.begin() + NumberOfGeneralFixedParametersOfStack);
}
private:
// Private using-declarations, to avoid `-Woverloaded-virtual` warnings from GCC (GCC 11.4).
using Superclass::TransformCovariantVector;
using Superclass::TransformVector;
/** Each override of this pure virtual member function should create a subtransform for the specific (derived) stack
* transform type. For example, for an `TranslationStackTransform` it should create an `AdvancedTranslationTransform`,
* and for an `EulerStackTransform` it should create an `EulerTransform`. */
virtual SubTransformPointer
CreateSubTransform() const = 0;
static constexpr const char * unimplementedOverrideMessage = "Not implemented for StackTransform";
/** These vector transforms are not implemented for this transform. */
OutputVectorType
TransformVector(const InputVectorType &) const override
{
itkExceptionMacro(<< unimplementedOverrideMessage);
}
OutputVnlVectorType
TransformVector(const InputVnlVectorType &) const override
{
itkExceptionMacro(<< unimplementedOverrideMessage);
}
OutputCovariantVectorType
TransformCovariantVector(const InputCovariantVectorType &) const override
{
itkExceptionMacro(<< unimplementedOverrideMessage);
}
/** Must be provided. */
void
GetSpatialJacobian(const InputPointType &, SpatialJacobianType &) const override
{
itkExceptionMacro(<< unimplementedOverrideMessage);
}
void
GetSpatialHessian(const InputPointType &, SpatialHessianType &) const override
{
itkExceptionMacro(<< unimplementedOverrideMessage);
}
void
GetJacobianOfSpatialJacobian(const InputPointType &,
JacobianOfSpatialJacobianType &,
NonZeroJacobianIndicesType &) const override
{
itkExceptionMacro(<< unimplementedOverrideMessage);
}
void
GetJacobianOfSpatialJacobian(const InputPointType &,
SpatialJacobianType &,
JacobianOfSpatialJacobianType &,
NonZeroJacobianIndicesType &) const override
{
itkExceptionMacro(<< unimplementedOverrideMessage);
}
void
GetJacobianOfSpatialHessian(const InputPointType &,
JacobianOfSpatialHessianType &,
NonZeroJacobianIndicesType &) const override
{
itkExceptionMacro(<< unimplementedOverrideMessage);
}
void
GetJacobianOfSpatialHessian(const InputPointType &,
SpatialHessianType &,
JacobianOfSpatialHessianType &,
NonZeroJacobianIndicesType &) const override
{
itkExceptionMacro(<< unimplementedOverrideMessage);
}
// Transform container
std::vector<SubTransformPointer> m_SubTransformContainer{};
// Stack spacing and origin of last dimension
TScalarType m_StackSpacing{ 1.0 };
TScalarType m_StackOrigin{ 0.0 };
};
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
# include "itkStackTransform.hxx"
#endif
#endif
|