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
|
/*=========================================================================
*
* 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 _itkWeightedCombinationTransform_hxx
#define _itkWeightedCombinationTransform_hxx
#include "itkWeightedCombinationTransform.h"
#include <numeric> // For iota.
namespace itk
{
/**
* ********************* Constructor ****************************
*/
template <class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
WeightedCombinationTransform<TScalarType, NInputDimensions, NOutputDimensions>::WeightedCombinationTransform()
: Superclass(OutputSpaceDimension)
{
this->m_SumOfWeights = 1.0;
this->m_NormalizeWeights = false;
this->m_HasNonZeroSpatialHessian = true;
this->m_HasNonZeroJacobianOfSpatialHessian = true;
} // end Constructor
/**
* ************************ SetParameters ***********************
*/
template <class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
void
WeightedCombinationTransform<TScalarType, NInputDimensions, NOutputDimensions>::SetParameters(
const ParametersType & param)
{
if (param.GetSize() != this->m_TransformContainer.size())
{
itkExceptionMacro("Number of parameters does not match the number of transforms set in the transform container.");
}
// this->m_Jacobian.set_size( OutputSpaceDimension, param.GetSize() );
this->m_Parameters = param;
this->m_SumOfWeights = param.sum();
if (this->m_SumOfWeights < 1e-10 && this->m_NormalizeWeights)
{
itkExceptionMacro("Sum of weights for WeightedCombinationTransform is smaller than 0.");
}
// Precompute the nonzerojacobianindices vector
const NumberOfParametersType nrParams = param.GetSize();
if (nrParams != this->m_NonZeroJacobianIndices.size())
{
this->m_NonZeroJacobianIndices.resize(nrParams);
std::iota(m_NonZeroJacobianIndices.begin(), m_NonZeroJacobianIndices.end(), 0u);
}
this->Modified();
} // end SetParameters()
/**
* ********************* TransformPoint ****************************
*/
template <class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
auto
WeightedCombinationTransform<TScalarType, NInputDimensions, NOutputDimensions>::TransformPoint(
const InputPointType & inputPoint) const -> OutputPointType
{
OutputPointType opp{};
OutputPointType tempopp;
const TransformContainerType & tc = this->m_TransformContainer;
const unsigned int N = tc.size();
const ParametersType & param = this->m_Parameters;
/** Calculate sum_i w_i T_i(x) */
for (unsigned int i = 0; i < N; ++i)
{
tempopp = tc[i]->TransformPoint(inputPoint);
const double w = param[i];
for (unsigned int d = 0; d < OutputSpaceDimension; ++d)
{
opp[d] += w * tempopp[d];
}
}
if (this->m_NormalizeWeights)
{
/** T(x) = \sum_i w_i T_i(x) / sum_i w_i */
for (unsigned int d = 0; d < OutputSpaceDimension; ++d)
{
opp[d] /= this->m_SumOfWeights;
}
}
else
{
/** T(x) = (1 - \sum_i w_i ) x + \sum_i w_i T_i(x) */
const double factor = 1.0 - this->m_SumOfWeights;
for (unsigned int d = 0; d < OutputSpaceDimension; ++d)
{
opp[d] += factor * inputPoint[d];
}
}
return opp;
} // end TransformPoint
/**
* ********************* GetJacobian ****************************
*/
template <class TScalarType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
void
WeightedCombinationTransform<TScalarType, NInputDimensions, NOutputDimensions>::GetJacobian(
const InputPointType & inputPoint,
JacobianType & jac,
NonZeroJacobianIndicesType & nzji) const
{
OutputPointType tempopp;
const TransformContainerType & tc = this->m_TransformContainer;
const unsigned int N = tc.size();
const ParametersType & param = this->m_Parameters;
jac.set_size(OutputSpaceDimension, N);
/** This transform has only nonzero jacobians. */
nzji = this->m_NonZeroJacobianIndices;
if (this->m_NormalizeWeights)
{
/** dT/dmu_i = ( T_i(x) - T(x) ) / ( \sum_i w_i ) */
OutputPointType opp{};
for (unsigned int i = 0; i < N; ++i)
{
tempopp = tc[i]->TransformPoint(inputPoint);
const double w = param[i];
for (unsigned int d = 0; d < OutputSpaceDimension; ++d)
{
opp[d] += w * tempopp[d];
jac(d, i) = tempopp[d];
}
}
for (unsigned int d = 0; d < OutputSpaceDimension; ++d)
{
opp[d] /= this->m_SumOfWeights;
}
for (unsigned int i = 0; i < N; ++i)
{
for (unsigned int d = 0; d < OutputSpaceDimension; ++d)
{
jac(d, i) = (jac(d, i) - opp[d]) / this->m_SumOfWeights;
}
}
}
else
{
/** dT/dmu_i = T_i(x) - x */
for (unsigned int i = 0; i < N; ++i)
{
tempopp = tc[i]->TransformPoint(inputPoint);
for (unsigned int d = 0; d < OutputSpaceDimension; ++d)
{
jac(d, i) = tempopp[d] - inputPoint[d];
}
}
}
} // end GetJacobian()
} // end namespace itk
#endif
|