17 #ifndef _itkSparseKernelTransform_txx 18 #define _itkSparseKernelTransform_txx 19 #include "itkSparseKernelTransform.h" 22 #include <itkTimeProbe.h> 23 #include <itkTimeProbesCollectorBase.h> 25 #include "Eigen/Sparse" 26 #include "Eigen/SparseLU" 37 template <
class TScalarType,
unsigned int NDimensions>
38 SparseKernelTransform<TScalarType, NDimensions>::
39 SparseKernelTransform():Superclass(1)
48 m_I = IMatrixType::Identity();
59 Eigen::setNbThreads(8);
65 template <
class TScalarType,
unsigned int NDimensions>
66 SparseKernelTransform<TScalarType, NDimensions>::
67 ~SparseKernelTransform()
71 template<
class TScalarType,
unsigned int NDimensions>
72 inline void SparseKernelTransform<TScalarType,
73 NDimensions>::ComputeJacobianWithRespectToParameters(
76 template <
class TScalarType,
unsigned int NDimensions>
81 itkDebugMacro(
"setting SourceLandmarks to " << landmarks );
107 template <
class TScalarType,
unsigned int NDimensions>
112 itkDebugMacro(
"setting TargetLandmarks to " << landmarks );
130 template <
class TScalarType,
unsigned int NDimensions>
138 itkWarningMacro(<<
"ComputeG() should be reimplemented in the subclass !!");
145 template <
class TScalarType,
unsigned int NDimensions>
151 for(
unsigned d = 0; d < NDimensions; d++)
167 template <
class TScalarType,
unsigned int NDimensions>
171 OutputPointType & result )
const 178 for(
unsigned int lnd=0; lnd < numberOfLandmarks; lnd++ )
181 for(
unsigned int dim=0; dim < NDimensions; dim++ )
183 for(
unsigned int odim=0; odim < NDimensions; odim++ )
185 result[ odim ] += Gmatrix(dim, odim ) *
m_DMatrix(dim,lnd);
197 template <
class TScalarType,
unsigned int NDimensions>
212 vt->Value() = tp->Value() - sp->Value();
223 template <
class TScalarType,
unsigned int NDimensions>
227 itk::TimeProbe clock;
253 throw std::runtime_error(
"LMatrix failed to decompose ...!");
257 m_WMatrix = WMatrixType::Zero(NDimensions*(numberOfLandmarks+NDimensions+1), 1);
269 std::cout <<
"#iterations: " << solver.
iterations() << std::endl;
270 std::cout <<
"estimated error: " << solver.
error() << std::endl;
272 std::cout << solver.
error() << std::endl;
275 throw std::runtime_error(
"solving sparse system failed ...!");
279 std::cout <<
"Computing Wmatrix:" << std::endl;
280 std::cout <<
"Mean: " << clock.GetMean() << std::endl;
281 std::cout <<
"Total: " << clock.GetTotal() << std::endl;
349 template <
class TScalarType,
unsigned int NDimensions>
364 NDimensions*(numberOfLandmarks+NDimensions+1) );
375 for (
typename KMatrixType::InnerIterator it(
m_KMatrix,k); it; ++it)
383 for (
typename PMatrixType::InnerIterator it(
m_PMatrix,p); it; ++it)
386 m_LMatrix.
insert(it.row(), NDimensions*numberOfLandmarks + it.col()) = it.value() ;
390 m_LMatrix.
insert(NDimensions*numberOfLandmarks + it.col(), it.row()) = it.value() ;
415 template <
class TScalarType,
unsigned int NDimensions>
424 NDimensions * numberOfLandmarks );
438 PointsIterator p2 = p1;
449 for(
unsigned int d = 0; d < NDimensions; d++)
467 for(
unsigned int ii = 0 ; ii < NDimensions; ii++)
468 for(
unsigned int jj = 0 ; jj < NDimensions; jj++)
510 template <
class TScalarType,
unsigned int NDimensions>
528 NDimensions*(NDimensions+1) );
529 for (
unsigned int i = 0; i < numberOfLandmarks; i++)
532 for (
unsigned int j = 0; j < NDimensions; j++)
535 for(
unsigned int d = 0 ; d < NDimensions; d++)
543 for(
unsigned int d = 0 ; d < NDimensions; d++)
561 template <
class TScalarType,
unsigned int NDimensions>
569 typename VectorSetType::ConstIterator displacement =
574 m_YMatrix = YMatrixType::Zero(NDimensions*(numberOfLandmarks+NDimensions+1), 1);
576 for (
unsigned int i = 0; i < numberOfLandmarks; i++)
578 for (
unsigned int j = 0; j < NDimensions; j++)
580 m_YMatrix(i*NDimensions+j, 0) = displacement.Value()[j];
586 for (
unsigned int i = 0; i < NDimensions*(NDimensions+1); i++)
588 m_YMatrix(numberOfLandmarks*NDimensions+i, 0) = 0;
597 template <
class TScalarType,
unsigned int NDimensions>
605 m_DMatrix = DMatrixType::Zero(NDimensions,numberOfLandmarks);
609 for(
unsigned int lnd=0; lnd < numberOfLandmarks; lnd++ )
611 for(
unsigned int dim=0; dim < NDimensions; dim++ )
619 m_AMatrix = AMatrixType::Zero(NDimensions, NDimensions);
620 for(
unsigned int j=0; j < NDimensions; j++ )
622 for(
unsigned int i=0; i < NDimensions; i++ )
629 m_BVector = BMatrixType::Zero(NDimensions,1);
630 for(
unsigned int k=0; k < NDimensions; k++ )
653 template <
class TScalarType,
unsigned int NDimensions>
654 typename SparseKernelTransform<TScalarType, NDimensions>::OutputPointType
659 OutputPointType result;
661 typedef typename OutputPointType::ValueType ValueType;
663 result.Fill( NumericTraits< ValueType >::Zero );
668 for(
unsigned int j=0; j < NDimensions; j++ )
670 for(
unsigned int i=0; i < NDimensions; i++ )
672 result[i] +=
m_AMatrix(i,j) * thisPoint[j];
679 for(
unsigned int k=0; k < NDimensions; k++ )
681 result[k] +=
m_BVector(k) + thisPoint[k];
742 template <
class TScalarType,
unsigned int NDimensions>
756 template <
class TScalarType,
unsigned int NDimensions>
762 typename PointsContainer::Pointer landmarks = PointsContainer::New();
763 const unsigned int numberOfLandmarks = parameters.Size() / NDimensions;
764 landmarks->Reserve( numberOfLandmarks );
766 PointsIterator itr = landmarks->Begin();
767 PointsIterator end = landmarks->End();
771 unsigned int pcounter = 0;
774 for(
unsigned int dim=0; dim<NDimensions; dim++)
776 landMark[ dim ] = parameters[ pcounter ];
779 itr.Value() = landMark;
804 template <
class TScalarType,
unsigned int NDimensions>
809 typename PointsContainer::Pointer landmarks = PointsContainer::New();
810 const unsigned int numberOfLandmarks = parameters.Size() / NDimensions;
812 landmarks->Reserve( numberOfLandmarks );
814 PointsIterator itr = landmarks->Begin();
815 PointsIterator end = landmarks->End();
819 unsigned int pcounter = 0;
822 for(
unsigned int dim=0; dim<NDimensions; dim++)
824 landMark[ dim ] = parameters[ pcounter ];
827 itr.Value() = landMark;
848 template <
class TScalarType,
unsigned int NDimensions>
858 unsigned int pcounter = 0;
862 for(
unsigned int dim=0; dim<NDimensions; dim++)
864 this->m_Parameters[ pcounter ] = landmark[ dim ];
876 template <
class TScalarType,
unsigned int NDimensions>
882 return this->m_Parameters;
890 template <
class TScalarType,
unsigned int NDimensions>
900 unsigned int pcounter = 0;
904 for(
unsigned int dim=0; dim<NDimensions; dim++)
906 this->m_FixedParameters[ pcounter ] = landmark[ dim ];
912 return this->m_FixedParameters;
918 template <
class TScalarType,
unsigned int NDimensions>
921 PrintSelf(std::ostream& os, Indent indent)
const 923 Superclass::PrintSelf(os,indent);
926 os << indent <<
"SourceLandmarks: " << std::endl;
931 os << indent <<
"TargetLandmarks: " << std::endl;
936 os << indent <<
"Displacements: " << std::endl;
939 os << indent <<
"Stiffness: " <<
m_Stiffness << std::endl;
A bi conjugate gradient stabilized solver for sparse square problems.
BiCGSTAB< _MatrixType, _Preconditioner > & compute(const MatrixType &A)
Preconditioner & preconditioner()
Scalar & insert(Index row, Index col)
ComputationInfo info() const
const internal::solve_retval< BiCGSTAB< _MatrixType, _Preconditioner >, Rhs > solve(const MatrixBase< Rhs > &b) const