66 static vtkAbstractTransform*
CreateVTKTransformFromITK(vtkObject* loggerObject,
typename itk::TransformBaseTemplate<T>::Pointer transformItk);
79 itk::Object::Pointer& secondaryTransformItk,
int preferITKv3CompatibleTransforms,
bool initialize =
true);
82 typename itk::TransformBaseTemplate<T>::Pointer warpTransformItk,
typename itk::TransformBaseTemplate<T>::Pointer bulkTransformItk);
86 typename itk::DisplacementFieldTransform< T, 3 >::DisplacementFieldType::Pointer gridImage_Lps);
88 vtkOrientedGridTransform* grid_Ras);
94 typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS);
95 static bool SetITKLinearTransformFromVTK(vtkObject* loggerObject, itk::Object::Pointer& transformItk_LPS, vtkMatrix4x4* transformVtk_RAS);
99 typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS);
102 static bool SetITKv3BSplineFromVTK(vtkObject* loggerObject, itk::Object::Pointer& warpTransformItk, itk::Object::Pointer& bulkTransformItk,
103 vtkOrientedBSplineTransform* bsplineVtk,
bool alwaysAddBulkTransform);
104 static bool SetITKv4BSplineFromVTK(vtkObject* loggerObject, itk::Object::Pointer& warpTransformItk, vtkOrientedBSplineTransform* bsplineVtk);
108 typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS);
110 vtkThinPlateSplineTransform* transformVtk_RAS,
bool initialize =
true);
115 vtkOrientedBSplineTransform* bsplineVtk,
typename itk::TransformBaseTemplate<typename BSplineTransformType::ScalarType>::Pointer warpTransformItk);
117 vtkOrientedBSplineTransform* bsplineVtk,
typename itk::TransformBaseTemplate<T>::Pointer warpTransformItk);
120 typename itk::Transform< typename BSplineTransformType::ScalarType, VTKDimension, VTKDimension>::Pointer& warpTransformItk,
121 vtkOrientedBSplineTransform* bsplineVtk);
123 typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& warpTransformItk,
124 typename itk::Transform<T, VTKDimension, VTKDimension>::Pointer& bulkTransformItk, vtkOrientedBSplineTransform* bsplineVtk,
125 bool alwaysAddBulkTransform);
127 typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& warpTransformItk, vtkOrientedBSplineTransform* bsplineVtk);
134 itk::TransformFactory<InverseDisplacementFieldTransformFloatType>::RegisterTransform();
135 itk::TransformFactory<InverseDisplacementFieldTransformDoubleType>::RegisterTransform();
137 itk::TransformFactory<InverseBSplineTransformFloatITKv3Type>::RegisterTransform();
138 itk::TransformFactory<InverseBSplineTransformFloatITKv4Type>::RegisterTransform();
139 itk::TransformFactory<InverseBSplineTransformDoubleITKv3Type>::RegisterTransform();
140 itk::TransformFactory<InverseBSplineTransformDoubleITKv4Type>::RegisterTransform();
142 itk::TransformFactory<InverseThinPlateSplineTransformFloatType>::RegisterTransform();
143 itk::TransformFactory<InverseThinPlateSplineTransformDoubleType>::RegisterTransform();
145 typedef itk::ThinPlateSplineKernelTransform<float,3> ThinPlateSplineTransformFloatType;
149 itk::TransformFactory<ThinPlateSplineTransformFloatType>::RegisterTransform();
150 itk::TransformFactory<ThinPlateSplineTransformDoubleType>::RegisterTransform();
157 vtkMatrix4x4* transformVtk_RAS,
158 typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS)
161 typedef itk::MatrixOffsetTransformBase<T,D,D> LinearTransformType;
162 typedef itk::ScaleTransform<T, D> ScaleTransformType;
163 typedef itk::TranslationTransform<T, D> TranslateTransformType;
165 vtkSmartPointer<vtkMatrix4x4> transformVtk_LPS = vtkSmartPointer<vtkMatrix4x4>::New();
167 bool convertedToVtkMatrix=
false;
169 std::string itkTransformClassName = transformItk_LPS->GetNameOfClass();
188 if (itkTransformClassName.find(
"AffineTransform" ) != std::string::npos ||
189 itkTransformClassName ==
"MatrixOffsetTransformBase" ||
190 itkTransformClassName ==
"Rigid3DTransform" ||
191 itkTransformClassName ==
"Euler3DTransform" ||
192 itkTransformClassName ==
"CenteredEuler3DTransform" ||
193 itkTransformClassName ==
"QuaternionRigidTransform" ||
194 itkTransformClassName ==
"VersorTransform" ||
195 itkTransformClassName ==
"VersorRigid3DTransform" ||
196 itkTransformClassName ==
"ScaleSkewVersor3DTransform" ||
197 itkTransformClassName ==
"ScaleVersor3DTransform" ||
198 itkTransformClassName ==
"Similarity3DTransform" ||
199 itkTransformClassName ==
"ScaleTransform" ||
200 itkTransformClassName ==
"ScaleLogarithmicTransform")
202 typename LinearTransformType::Pointer dlt
203 =
static_cast<LinearTransformType*
>( transformItk_LPS.GetPointer() );
204 convertedToVtkMatrix=
true;
205 for (
unsigned int i=0; i < D; i++)
207 for (
unsigned int j=0; j < D; j++)
209 transformVtk_LPS->SetElement(i, j, dlt->GetMatrix()[i][j]);
211 transformVtk_LPS->SetElement(i, D, dlt->GetOffset()[i]);
216 if (itkTransformClassName ==
"IdentityTransform")
219 convertedToVtkMatrix=
true;
223 if (itkTransformClassName ==
"ScaleTransform")
225 typename ScaleTransformType::Pointer dst
226 =
static_cast<ScaleTransformType*
>( transformItk_LPS.GetPointer() );
227 convertedToVtkMatrix=
true;
228 for (
unsigned int i=0; i < D; i++)
230 transformVtk_LPS->SetElement(i, i, dst->GetScale()[i]);
235 if (itkTransformClassName ==
"TranslationTransform")
237 typename TranslateTransformType::Pointer dtt
238 =
static_cast<TranslateTransformType*
>( transformItk_LPS.GetPointer());
239 convertedToVtkMatrix=
true;
240 for (
unsigned int i=0; i < D; i++)
242 transformVtk_LPS->SetElement(i, D, dtt->GetOffset()[i]);
251 vtkSmartPointer<vtkMatrix4x4> lps2ras = vtkSmartPointer<vtkMatrix4x4>::New();
252 lps2ras->SetElement(0,0,-1);
253 lps2ras->SetElement(1,1,-1);
254 vtkMatrix4x4* ras2lps = lps2ras;
256 vtkMatrix4x4::Multiply4x4(lps2ras, transformVtk_LPS, transformVtk_LPS);
257 vtkMatrix4x4::Multiply4x4(transformVtk_LPS, ras2lps, transformVtk_RAS);
259 return convertedToVtkMatrix;
267 if (transformVtk_RAS==
nullptr)
269 vtkErrorWithObjectMacro(loggerObject,
"vtkITKTransformConverter::SetITKLinearTransformFromVTK failed: invalid input transform");
273 vtkSmartPointer<vtkMatrix4x4> lps2ras = vtkSmartPointer<vtkMatrix4x4>::New();
274 lps2ras->SetElement(0,0,-1);
275 lps2ras->SetElement(1,1,-1);
276 vtkMatrix4x4* ras2lps = lps2ras;
282 vtkSmartPointer<vtkMatrix4x4> vtkmat = vtkSmartPointer<vtkMatrix4x4>::New();
284 vtkMatrix4x4::Multiply4x4(ras2lps, transformVtk_RAS, vtkmat);
285 vtkMatrix4x4::Multiply4x4(vtkmat, lps2ras, vtkmat);
287 typedef AffineTransformType::MatrixType MatrixType;
288 typedef AffineTransformType::OutputVectorType OffsetType;
291 OffsetType itkoffset;
297 itkmat[i][j] = vtkmat->GetElement(i, j);
302 AffineTransformType::Pointer affine = AffineTransformType::New();
303 affine->SetMatrix(itkmat);
304 affine->SetOffset(itkoffset);
306 transformItk_LPS = affine;
332 vtkObject* loggerObject,
333 vtkOrientedBSplineTransform* bsplineVtk,
334 typename itk::TransformBaseTemplate<typename BSplineTransformType::ScalarType>::Pointer warpTransformItk)
339 typedef typename BSplineTransformType::ScalarType T;
340 if (bsplineVtk==
nullptr)
342 vtkErrorWithObjectMacro(loggerObject,
"vtkMRMLTransformStorageNode::SetVTKBSplineFromITKv4 failed: bsplineVtk is invalid");
345 bool isDoublePrecisionInput=
true;
346 if (
sizeof(T)==
sizeof(
float))
348 isDoublePrecisionInput=
false;
350 else if (
sizeof(T)==
sizeof(
double))
352 isDoublePrecisionInput=
true;
356 vtkErrorWithObjectMacro(loggerObject,
"Unsupported scalar type in BSpline transform file (only float and double are supported)");
360 typename BSplineTransformType::Pointer bsplineItk = BSplineTransformType::New();
361 std::string warpTransformItkName = warpTransformItk->GetNameOfClass();
362 std::string requestedWarpTransformItkName = bsplineItk->GetNameOfClass();
363 if (warpTransformItkName != requestedWarpTransformItkName)
367 if (warpTransformItk->GetOutputSpaceDimension() !=
VTKDimension)
369 vtkErrorWithObjectMacro(loggerObject,
"Unsupported number of dimensions in BSpline transform file (expected = "
370 <<
VTKDimension <<
", actual = " << warpTransformItk->GetOutputSpaceDimension() <<
")");
373 bsplineItk =
static_cast< BSplineTransformType*
>( warpTransformItk.GetPointer() );
381 const typename BSplineTransformType::CoefficientImageArray coefficientImages =
382 bsplineItk->GetCoefficientImages();
386 typename BSplineTransformType::MeshSizeType meshSize =
387 coefficientImages[0]->GetLargestPossibleRegion().GetSize();
390 typename BSplineTransformType::OriginType origin =
391 coefficientImages[0]->GetOrigin();
394 typename BSplineTransformType::SpacingType spacing =
395 coefficientImages[0]->GetSpacing();
398 typename BSplineTransformType::DirectionType direction =
399 coefficientImages[0]->GetDirection();
401 vtkNew<vtkMatrix4x4> gridDirectionMatrix_LPS;
404 for (
unsigned int column=0; column<
VTKDimension; column++)
406 gridDirectionMatrix_LPS->SetElement(row,column,direction[row][column]);
411 bsplineVtk->SetBorderModeToZero();
413 vtkNew<vtkImageData> bsplineCoefficients;
415 bsplineCoefficients->SetExtent(0, meshSize[0]-1, 0, meshSize[1]-1, 0, meshSize[2]-1);
416 bsplineCoefficients->SetSpacing(spacing[0], spacing[1], spacing[2]);
419 vtkNew<vtkMatrix4x4> lpsToRas;
420 lpsToRas->SetElement(0,0,-1);
421 lpsToRas->SetElement(1,1,-1);
423 vtkNew<vtkMatrix4x4> rasToLps;
424 rasToLps->SetElement(0,0,-1);
425 rasToLps->SetElement(1,1,-1);
427 vtkNew<vtkMatrix4x4> gridDirectionMatrix_RAS;
428 vtkMatrix4x4::Multiply4x4(
429 lpsToRas.GetPointer(),
430 gridDirectionMatrix_LPS.GetPointer(),
431 gridDirectionMatrix_RAS.GetPointer());
432 bsplineVtk->SetGridDirectionMatrix(gridDirectionMatrix_RAS.GetPointer());
435 double gridOrigin_RAS[3]={-origin[0], -origin[1], origin[2]};
436 bsplineCoefficients->SetOrigin(gridOrigin_RAS);
438 int bsplineCoefficientsScalarType=VTK_FLOAT;
439 if (isDoublePrecisionInput)
441 bsplineCoefficientsScalarType=VTK_DOUBLE;
444 bsplineCoefficients->AllocateScalars(bsplineCoefficientsScalarType, 3);
446 const unsigned int expectedNumberOfVectors = meshSize[0]*meshSize[1]*meshSize[2];
447 const unsigned int expectedNumberOfParameters = expectedNumberOfVectors*
VTKDimension;
448 const unsigned int actualNumberOfParameters = bsplineItk->GetNumberOfParameters();
450 if( actualNumberOfParameters != expectedNumberOfParameters )
452 vtkErrorWithObjectMacro(loggerObject,
"Mismatch in number of BSpline parameters in the transform file and the MRML node");
455 const T* itkBSplineParams_LPS =
static_cast<const T*
>(bsplineItk->GetParameters().data_block());
456 T* vtkBSplineParams_RAS=
static_cast<T*
>(bsplineCoefficients->GetScalarPointer());
457 for (
unsigned int i=0; i<expectedNumberOfVectors; i++)
459 *(vtkBSplineParams_RAS++) = - (*(itkBSplineParams_LPS ));
460 *(vtkBSplineParams_RAS++) = - (*(itkBSplineParams_LPS+expectedNumberOfVectors ));
461 *(vtkBSplineParams_RAS++) = (*(itkBSplineParams_LPS+expectedNumberOfVectors*2));
462 itkBSplineParams_LPS++;
464 bsplineVtk->SetCoefficientData(bsplineCoefficients.GetPointer());
472 vtkOrientedBSplineTransform* bsplineVtk,
473 typename itk::TransformBaseTemplate<T>::Pointer warpTransformItk,
typename itk::TransformBaseTemplate<T>::Pointer bulkTransformItk)
475 if (bsplineVtk==
nullptr)
477 vtkErrorWithObjectMacro(loggerObject,
"vtkMRMLTransformStorageNode::SetVTKBSplineFromITK failed: bsplineVtk is invalid");
481 bool inverse =
false;
493 vtkDebugWithObjectMacro(loggerObject,
"Not an ITKv3 BSpline transform");
498 if( bulkTransformItk )
500 std::string bulkTransformItkTransformName = bulkTransformItk->GetNameOfClass();
502 typedef itk::AffineTransform<T,3> BulkTransformType;
504 if (bulkTransformItkTransformName ==
"AffineTransform")
506 BulkTransformType* bulkItkAffine =
static_cast<BulkTransformType*
> (bulkTransformItk.GetPointer());
507 vtkNew<vtkMatrix4x4> bulkMatrix_LPS;
512 bulkMatrix_LPS->SetElement(i,j, bulkItkAffine->GetMatrix()[i][j]);
514 bulkMatrix_LPS->SetElement(i,
VTKDimension, bulkItkAffine->GetOffset()[i]);
516 vtkNew<vtkMatrix4x4> lpsToRas;
517 lpsToRas->SetElement(0,0,-1);
518 lpsToRas->SetElement(1,1,-1);
519 vtkNew<vtkMatrix4x4> rasToLps;
520 rasToLps->SetElement(0,0,-1);
521 rasToLps->SetElement(1,1,-1);
522 vtkNew<vtkMatrix4x4> bulkMatrix_RAS;
523 vtkMatrix4x4::Multiply4x4(lpsToRas.GetPointer(), bulkMatrix_LPS.GetPointer(), bulkMatrix_RAS.GetPointer());
524 vtkMatrix4x4::Multiply4x4(bulkMatrix_RAS.GetPointer(), rasToLps.GetPointer(), bulkMatrix_RAS.GetPointer());
525 bsplineVtk->SetBulkTransformMatrix(bulkMatrix_RAS.GetPointer());
527 else if (bulkTransformItkTransformName ==
"IdentityTransform")
533 vtkErrorWithObjectMacro(loggerObject,
"Cannot read the 2nd transform in BSplineTransform (expected AffineTransform_double_3_3 or IdentityTransform)" );
540 bsplineVtk->Inverse();
575 typename itk::Transform< typename BSplineTransformType::ScalarType,VTKDimension,VTKDimension>::Pointer& warpTransformItk,
576 vtkOrientedBSplineTransform* bsplineVtk)
578 typedef typename BSplineTransformType::ScalarType T;
579 if (bsplineVtk==
nullptr)
581 vtkErrorWithObjectMacro(loggerObject,
"vtkMRMLTransformStorageNode::SetITKBSplineFromVTK failed: bsplineVtk is invalid");
584 vtkImageData* bsplineCoefficients_RAS=bsplineVtk->GetCoefficientData();
585 if (bsplineCoefficients_RAS==
nullptr)
587 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file: coefficients are not specified");
591 typename BSplineTransformType::Pointer bsplineItk = BSplineTransformType::New();
592 warpTransformItk = bsplineItk;
599 typename BSplineTransformType::FixedParametersType transformFixedParamsItk;
601 transformFixedParamsItk.SetSize(numberOfFixedParameters);
603 int *gridExtent=bsplineCoefficients_RAS->GetExtent();
604 int gridSize[3]={gridExtent[1]-gridExtent[0]+1, gridExtent[3]-gridExtent[2]+1, gridExtent[5]-gridExtent[4]+1};
605 transformFixedParamsItk[0]=gridSize[0];
606 transformFixedParamsItk[1]=gridSize[1];
607 transformFixedParamsItk[2]=gridSize[2];
609 double* gridOrigin_RAS=bsplineCoefficients_RAS->GetOrigin();
610 double gridOrigin_LPS[3]={-gridOrigin_RAS[0], -gridOrigin_RAS[1], gridOrigin_RAS[2]};
611 transformFixedParamsItk[3]=gridOrigin_LPS[0];
612 transformFixedParamsItk[4]=gridOrigin_LPS[1];
613 transformFixedParamsItk[5]=gridOrigin_LPS[2];
615 double* gridSpacing=bsplineCoefficients_RAS->GetSpacing();
616 transformFixedParamsItk[6]=gridSpacing[0];
617 transformFixedParamsItk[7]=gridSpacing[1];
618 transformFixedParamsItk[8]=gridSpacing[2];
620 vtkNew<vtkMatrix4x4> gridDirectionMatrix_RAS;
621 if (bsplineVtk->GetGridDirectionMatrix()!=
nullptr)
623 gridDirectionMatrix_RAS->DeepCopy(bsplineVtk->GetGridDirectionMatrix());
625 vtkNew<vtkMatrix4x4> lpsToRas;
626 lpsToRas->SetElement(0,0,-1);
627 lpsToRas->SetElement(1,1,-1);
628 vtkNew<vtkMatrix4x4> rasToLps;
629 rasToLps->SetElement(0,0,-1);
630 rasToLps->SetElement(1,1,-1);
631 vtkNew<vtkMatrix4x4> gridDirectionMatrix_LPS;
632 vtkMatrix4x4::Multiply4x4(rasToLps.GetPointer(), gridDirectionMatrix_RAS.GetPointer(), gridDirectionMatrix_LPS.GetPointer());
636 for (
unsigned int column=0; column<
VTKDimension; column++)
638 transformFixedParamsItk[fpIndex++]=gridDirectionMatrix_LPS->GetElement(row,column);
642 bsplineItk->SetFixedParameters(transformFixedParamsItk);
646 const unsigned int expectedNumberOfVectors = gridSize[0]*gridSize[1]*gridSize[2];
647 const unsigned int expectedNumberOfParameters = expectedNumberOfVectors*
VTKDimension;
648 if( bsplineItk->GetNumberOfParameters() != expectedNumberOfParameters )
650 vtkErrorWithObjectMacro(loggerObject,
"Mismatch in number of BSpline parameters in the ITK transform and the VTK transform");
654 typename BSplineTransformType::ParametersType transformParamsItk(expectedNumberOfParameters);
655 T* itkBSplineParams_LPS =
static_cast<T*
>(transformParamsItk.data_block());
656 T* vtkBSplineParams_RAS=
static_cast<T*
>(bsplineCoefficients_RAS->GetScalarPointer());
657 double coefficientScale = bsplineVtk->GetDisplacementScale();
658 for (
unsigned int i=0; i<expectedNumberOfVectors; i++)
660 *(itkBSplineParams_LPS ) = -coefficientScale * (*(vtkBSplineParams_RAS++));
661 *(itkBSplineParams_LPS+expectedNumberOfVectors ) = -coefficientScale * (*(vtkBSplineParams_RAS++));
662 *(itkBSplineParams_LPS+expectedNumberOfVectors*2) = coefficientScale * (*(vtkBSplineParams_RAS++));
663 itkBSplineParams_LPS++;
666 bsplineItk->SetParameters(transformParamsItk);
672 typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& warpTransformItk,
673 typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& bulkTransformItk,
674 vtkOrientedBSplineTransform* bsplineVtk,
bool alwaysAddBulkTransform)
676 if (bsplineVtk==
nullptr)
678 vtkErrorWithObjectMacro(loggerObject,
"vtkMRMLTransformStorageNode::SetITKBSplineFromVTK failed: bsplineVtk is invalid");
682 bsplineVtk->Update();
683 bool itkTransformSetSuccessfully =
false;
684 if (bsplineVtk->GetInverseFlag())
692 itk::BSplineDeformableTransform< T, VTKDimension, VTKDimension > >(loggerObject, warpTransformItk, bsplineVtk);
694 if (!itkTransformSetSuccessfully)
696 vtkErrorWithObjectMacro(loggerObject,
"vtkMRMLTransformStorageNode::SetITKBSplineFromVTK failed: cannot determine BSpline parameters");
700 vtkMatrix4x4* bulkMatrix_RAS=bsplineVtk->GetBulkTransformMatrix();
701 if (bulkMatrix_RAS || alwaysAddBulkTransform)
703 vtkNew<vtkMatrix4x4> lpsToRas;
704 lpsToRas->SetElement(0,0,-1);
705 lpsToRas->SetElement(1,1,-1);
706 vtkNew<vtkMatrix4x4> rasToLps;
707 rasToLps->SetElement(0,0,-1);
708 rasToLps->SetElement(1,1,-1);
709 vtkNew<vtkMatrix4x4> bulkMatrix_LPS;
712 if (bulkMatrix_RAS!=
nullptr)
714 vtkMatrix4x4::Multiply4x4(rasToLps.GetPointer(), bulkMatrix_RAS, bulkMatrix_LPS.GetPointer());
715 vtkMatrix4x4::Multiply4x4(bulkMatrix_LPS.GetPointer(), lpsToRas.GetPointer(), bulkMatrix_LPS.GetPointer());
717 typedef itk::AffineTransform<T,VTKDimension> BulkTransformType;
718 typename BulkTransformType::Pointer affineItk = BulkTransformType::New();
719 bulkTransformItk = affineItk;
721 typename BulkTransformType::MatrixType affineMatrix;
722 typename BulkTransformType::OffsetType affineOffset;
727 affineMatrix[i][j]=bulkMatrix_LPS->GetElement(i,j);
729 affineOffset[i]=bulkMatrix_LPS->GetElement(i,
VTKDimension);
732 affineItk->SetMatrix(affineMatrix);
733 affineItk->SetOffset(affineOffset);
737 bulkTransformItk=
nullptr;
745 typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& warpTransformItk,
746 vtkOrientedBSplineTransform* bsplineVtk)
749 bsplineVtk->Update();
750 bool itkTransformSetSuccessfully =
false;
751 if (bsplineVtk->GetInverseFlag())
759 itk::BSplineTransform< T, VTKDimension, VTKDimension > >(loggerObject, warpTransformItk, bsplineVtk);
761 if (!itkTransformSetSuccessfully)
763 vtkErrorWithObjectMacro(loggerObject,
"vtkMRMLTransformStorageNode::SetITKv4BSplineFromVTKGeneric failed: cannot determine BSpline parameters");
771 itk::Object::Pointer& bulkTransformItk, vtkOrientedBSplineTransform* bsplineVtk,
bool alwaysAddBulkTransform)
773 if (bsplineVtk==
nullptr)
775 vtkErrorWithObjectMacro(loggerObject,
"Cannot retrieve BSpline transform from node");
779 vtkImageData* bsplineCoefficients=bsplineVtk->GetCoefficientData();
781 if (bsplineCoefficients==
nullptr)
783 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file: coefficients are not specified");
787 if (bsplineCoefficients->GetScalarType()==VTK_FLOAT)
789 typedef itk::Transform<float, VTKDimension, VTKDimension > ITKTransformType;
790 ITKTransformType::Pointer floatWarpTransformItk;
791 ITKTransformType::Pointer floatBulkTransformItk;
792 if (!SetITKv3BSplineFromVTKGeneric<float>(loggerObject, floatWarpTransformItk, floatBulkTransformItk, bsplineVtk, alwaysAddBulkTransform))
794 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file");
797 warpTransformItk = floatWarpTransformItk.GetPointer();
798 bulkTransformItk = floatBulkTransformItk.GetPointer();
800 else if (bsplineCoefficients->GetScalarType()==VTK_DOUBLE)
802 typedef itk::Transform<double, VTKDimension, VTKDimension > ITKTransformType;
803 ITKTransformType::Pointer doubleWarpTransformItk;
804 ITKTransformType::Pointer doubleBulkTransformItk;
805 if (!SetITKv3BSplineFromVTKGeneric<double>(loggerObject, doubleWarpTransformItk, doubleBulkTransformItk, bsplineVtk, alwaysAddBulkTransform))
807 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file");
810 warpTransformItk = doubleWarpTransformItk;
811 bulkTransformItk = doubleBulkTransformItk;
815 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file: only float and double coefficient types are supported");
824 itk::Object::Pointer& warpTransformItk, vtkOrientedBSplineTransform* bsplineVtk)
826 if (bsplineVtk==
nullptr)
828 vtkErrorWithObjectMacro(loggerObject,
"Cannot retrieve BSpline transform from node");
832 vtkImageData* bsplineCoefficients=bsplineVtk->GetCoefficientData();
834 if (bsplineCoefficients==
nullptr)
836 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file: coefficients are not specified");
840 if (bsplineCoefficients->GetScalarType()==VTK_FLOAT)
842 typedef itk::Transform<float, VTKDimension, VTKDimension > ITKTransformType;
843 ITKTransformType::Pointer floatWarpTransformItk;
844 if (!SetITKv4BSplineFromVTKGeneric<float>(loggerObject, floatWarpTransformItk, bsplineVtk))
846 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file");
849 warpTransformItk = floatWarpTransformItk.GetPointer();
851 else if (bsplineCoefficients->GetScalarType()==VTK_DOUBLE)
853 typedef itk::Transform<double, VTKDimension, VTKDimension > ITKTransformType;
854 ITKTransformType::Pointer doubleWarpTransformItk;
855 if (!SetITKv4BSplineFromVTKGeneric<double>(loggerObject, doubleWarpTransformItk, bsplineVtk))
857 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file");
860 warpTransformItk = doubleWarpTransformItk;
864 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file: only float and double coefficient types are supported");
874 vtkOrientedGridTransform* transformVtk_RAS,
typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS)
876 typedef itk::DisplacementFieldTransform< T, 3 > DisplacementFieldTransformType;
879 if (!transformItk_LPS)
881 vtkErrorWithObjectMacro(loggerObject,
"Cannot set VTK oriented grid transform from ITK: the input transform is nullptr");
884 if (transformItk_LPS->GetOutputSpaceDimension() !=
VTKDimension)
886 vtkErrorWithObjectMacro(loggerObject,
"Unsupported number of dimensions in oriented grid transform file (expected = "
887 <<
VTKDimension <<
", actual = " << transformItk_LPS->GetOutputSpaceDimension() <<
")");
891 std::string transformItkClassName = transformItk_LPS->GetNameOfClass();
893 bool inverse =
false;
894 typename DisplacementFieldTransformType::DisplacementFieldType* gridImageItk_Lps =
nullptr;
895 if (transformItkClassName ==
"InverseDisplacementFieldTransform")
897 DisplacementFieldTransformType* inverseDisplacementFieldTransform =
static_cast<InverseDisplacementFieldTransformType*
>( transformItk_LPS.GetPointer() );
899 gridImageItk_Lps = inverseDisplacementFieldTransform->GetDisplacementField();
901 else if (transformItkClassName ==
"DisplacementFieldTransform")
903 DisplacementFieldTransformType* displacementFieldTransform =
static_cast<DisplacementFieldTransformType*
>( transformItk_LPS.GetPointer() );
905 gridImageItk_Lps = displacementFieldTransform->GetDisplacementField();
909 vtkDebugWithObjectMacro(loggerObject,
"Not a grid transform");
912 if (!SetVTKOrientedGridTransformFromITKImage<T>(loggerObject, transformVtk_RAS, gridImageItk_Lps))
918 transformVtk_RAS->Inverse();
925 itk::Object::Pointer& transformItk_LPS, vtkOrientedGridTransform* transformVtk_RAS)
927 GridImageDoubleType::Pointer gridImageItk_Lps;
930 vtkErrorWithObjectMacro(loggerObject,
"vtkITKTransformConverter::SetITKOrientedGridTransformFromVTK failed: input transform is invalid");
934 transformVtk_RAS->Update();
935 if (transformVtk_RAS->GetInverseFlag())
938 gridTransformItk->SetDisplacementField(gridImageItk_Lps);
939 transformItk_LPS = gridTransformItk;
943 DisplacementFieldTransformDoubleType::Pointer gridTransformItk = DisplacementFieldTransformDoubleType::New();
944 gridTransformItk->SetDisplacementField(gridImageItk_Lps);
945 transformItk_LPS = gridTransformItk;
953 typename itk::DisplacementFieldTransform< T, 3 >::DisplacementFieldType::Pointer gridImage_Lps)
955 typedef itk::DisplacementFieldTransform< T, 3 > DisplacementFieldTransformType;
956 typedef typename DisplacementFieldTransformType::DisplacementFieldType GridImageType;
958 vtkNew<vtkImageData> gridImage_Ras;
961 gridImage_Ras->SetOrigin( -gridImage_Lps->GetOrigin()[0], -gridImage_Lps->GetOrigin()[1], gridImage_Lps->GetOrigin()[2] );
964 gridImage_Ras->SetSpacing( gridImage_Lps->GetSpacing()[0], gridImage_Lps->GetSpacing()[1], gridImage_Lps->GetSpacing()[2] );
967 vtkNew<vtkMatrix4x4> gridDirectionMatrix_LPS;
970 for (
unsigned int column=0; column<
VTKDimension; column++)
972 gridDirectionMatrix_LPS->SetElement(row,column,gridImage_Lps->GetDirection()(row,column));
975 vtkNew<vtkMatrix4x4> lpsToRas;
976 lpsToRas->SetElement(0,0,-1);
977 lpsToRas->SetElement(1,1,-1);
978 vtkNew<vtkMatrix4x4> gridDirectionMatrix_RAS;
979 vtkMatrix4x4::Multiply4x4(lpsToRas.GetPointer(), gridDirectionMatrix_LPS.GetPointer(), gridDirectionMatrix_RAS.GetPointer());
980 grid_Ras->SetGridDirectionMatrix(gridDirectionMatrix_RAS.GetPointer());
983 typename GridImageType::SizeType size = gridImage_Lps->GetBufferedRegion().GetSize();
984 gridImage_Ras->SetDimensions( size[0], size[1], size[2] );
985 unsigned int numberOfScalarComponents = GridImageType::PixelType::Dimension;
988 vtkErrorWithObjectMacro(loggerObject,
"Cannot load grid transform: the input displacement field expected to contain "
989 <<
VTKDimension <<
" components but it actually contains " << numberOfScalarComponents );
992 gridImage_Ras->AllocateScalars(VTK_DOUBLE, 3);
994 double* displacementVectors_Ras =
reinterpret_cast<double*
>(gridImage_Ras->GetScalarPointer());
995 itk::ImageRegionConstIterator<GridImageType> inputIt(gridImage_Lps, gridImage_Lps->GetRequestedRegion());
997 while( !inputIt.IsAtEnd() )
999 typename GridImageType::PixelType displacementVectorLps=inputIt.Get();
1000 *(displacementVectors_Ras++) = -displacementVectorLps[0];
1001 *(displacementVectors_Ras++) = -displacementVectorLps[1];
1002 *(displacementVectors_Ras++) = displacementVectorLps[2];
1006 grid_Ras->SetDisplacementGridData( gridImage_Ras.GetPointer() );
1009 grid_Ras->SetInterpolationModeToCubic();
1016 GridImageDoubleType::Pointer &gridImage_Lps, vtkOrientedGridTransform* grid_Ras)
1018 if (grid_Ras==
nullptr)
1020 vtkErrorWithObjectMacro(loggerObject,
"Cannot save grid transform: the input vtkOrientedGridTransform is invalid");
1027 vtkImageData* gridImage_Ras = grid_Ras->GetDisplacementGrid();
1028 if (gridImage_Ras==
nullptr)
1030 vtkErrorWithObjectMacro(loggerObject,
"Cannot save grid transform: the input vtkOrientedGridTransform does not contain a valid displacement grid");
1033 if (gridImage_Ras->GetNumberOfScalarComponents() !=
static_cast<int>(
VTKDimension))
1035 vtkErrorWithObjectMacro(loggerObject,
"Cannot save grid transform: the input vtkOrientedGridTransform expected to contain "
1036 <<
VTKDimension <<
" components but it actually contains " << gridImage_Ras->GetNumberOfScalarComponents() );
1040 gridImage_Lps = GridImageDoubleType::New();
1043 double* origin_Ras = gridImage_Ras->GetOrigin();
1044 double origin_Lps[3] = { -origin_Ras[0], -origin_Ras[1], origin_Ras[2] };
1045 gridImage_Lps->SetOrigin( origin_Lps );
1048 double* spacing = gridImage_Ras->GetSpacing();
1050 gridImage_Lps->SetSpacing( spacing );
1053 vtkNew<vtkMatrix4x4> gridDirectionMatrix_Ras;
1054 if (grid_Ras->GetGridDirectionMatrix()!=
nullptr)
1056 gridDirectionMatrix_Ras->DeepCopy(grid_Ras->GetGridDirectionMatrix());
1058 vtkNew<vtkMatrix4x4> rasToLps;
1059 rasToLps->SetElement(0,0,-1);
1060 rasToLps->SetElement(1,1,-1);
1061 vtkNew<vtkMatrix4x4> gridDirectionMatrix_Lps;
1062 vtkMatrix4x4::Multiply4x4(rasToLps.GetPointer(), gridDirectionMatrix_Ras.GetPointer(), gridDirectionMatrix_Lps.GetPointer());
1063 GridImageDoubleType::DirectionType gridDirectionMatrixItk_Lps;
1066 for (
unsigned int column=0; column<
VTKDimension; column++)
1068 gridDirectionMatrixItk_Lps(row,column) = gridDirectionMatrix_Lps->GetElement(row,column);
1071 gridImage_Lps->SetDirection(gridDirectionMatrixItk_Lps);
1074 GridImageDoubleType::IndexType start;
1075 start[0] = start[1] = start[2] = 0;
1076 int* Nijk = gridImage_Ras->GetDimensions();
1077 GridImageDoubleType::SizeType size;
1078 size[0] = Nijk[0]; size[1] = Nijk[1]; size[2] = Nijk[2];
1079 GridImageDoubleType::RegionType region;
1080 region.SetSize( size );
1081 region.SetIndex( start );
1082 gridImage_Lps->SetRegions( region );
1083 gridImage_Lps->Allocate();
1084 itk::ImageRegionIterator<GridImageDoubleType> gridImageIt_Lps(gridImage_Lps, region);
1085 gridImageIt_Lps.GoToBegin();
1086 GridImageDoubleType::PixelType displacementVectorLps;
1087 double displacementScale = grid_Ras->GetDisplacementScale();
1088 double displacementShift = grid_Ras->GetDisplacementShift();
1090 if (gridImage_Ras->GetScalarType()==VTK_DOUBLE)
1092 double* displacementVectors_Ras =
reinterpret_cast<double*
>(gridImage_Ras->GetScalarPointer());
1093 while( !gridImageIt_Lps.IsAtEnd() )
1095 displacementVectorLps[0] = -( displacementScale * (*(displacementVectors_Ras++)) + displacementShift );
1096 displacementVectorLps[1] = -( displacementScale * (*(displacementVectors_Ras++)) + displacementShift );
1097 displacementVectorLps[2] = ( displacementScale * (*(displacementVectors_Ras++)) + displacementShift );
1098 gridImageIt_Lps.Set(displacementVectorLps);
1102 else if (gridImage_Ras->GetScalarType()==VTK_FLOAT)
1104 float* displacementVectors_Ras =
reinterpret_cast<float*
>(gridImage_Ras->GetScalarPointer());
1105 while( !gridImageIt_Lps.IsAtEnd() )
1107 displacementVectorLps[0] = -( displacementScale * (*(displacementVectors_Ras++)) + displacementShift );
1108 displacementVectorLps[1] = -( displacementScale * (*(displacementVectors_Ras++)) + displacementShift );
1109 displacementVectorLps[2] = ( displacementScale * (*(displacementVectors_Ras++)) + displacementShift );
1110 gridImageIt_Lps.Set(displacementVectorLps);
1116 vtkErrorWithObjectMacro(loggerObject,
"Cannot save grid transform: only float and double scalar types are supported");
1125 vtkThinPlateSplineTransform* transformVtk_RAS,
typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS)
1127 typedef itk::ThinPlateSplineKernelTransform<T,3> ThinPlateSplineTransformType;
1130 if (transformVtk_RAS==
nullptr)
1132 vtkErrorWithObjectMacro(loggerObject,
"Cannot set VTK thin-plate spline transform from ITK: the output vtkThinPlateSplineTransform is invalid");
1136 if (!transformItk_LPS)
1138 vtkErrorWithObjectMacro(loggerObject,
"Cannot set VTK thin-plate spline transform from ITK: the input transform is nullptr");
1142 if (transformItk_LPS->GetOutputSpaceDimension() !=
VTKDimension)
1144 vtkErrorWithObjectMacro(loggerObject,
"Unsupported number of dimensions in thin-plate spline transform file (expected = "
1145 <<
VTKDimension <<
", actual = " << transformItk_LPS->GetOutputSpaceDimension() <<
")");
1149 std::string transformItkClassName = transformItk_LPS->GetNameOfClass();
1151 bool inverse =
false;
1152 typename ThinPlateSplineTransformType::PointSetType::Pointer sourceLandmarksItk_Lps;
1153 typename ThinPlateSplineTransformType::PointSetType::Pointer targetLandmarksItk_Lps;
1154 if (transformItkClassName ==
"InverseThinPlateSplineKernelTransform")
1156 ThinPlateSplineTransformType* inverseTpsTransform =
static_cast<InverseThinPlateSplineTransformType*
>( transformItk_LPS.GetPointer() );
1158 sourceLandmarksItk_Lps = inverseTpsTransform->GetSourceLandmarks();
1159 targetLandmarksItk_Lps = inverseTpsTransform->GetTargetLandmarks();
1161 else if (transformItkClassName ==
"ThinPlateSplineKernelTransform")
1163 ThinPlateSplineTransformType* tpsTransform =
static_cast<ThinPlateSplineTransformType*
>( transformItk_LPS.GetPointer() );
1165 sourceLandmarksItk_Lps = tpsTransform->GetSourceLandmarks();
1166 targetLandmarksItk_Lps = tpsTransform->GetTargetLandmarks();
1170 vtkDebugWithObjectMacro(loggerObject,
"Not a ThinPlateSpline transform");
1174 vtkNew<vtkPoints> sourceLandmarksVtk_Ras;
1175 unsigned int numberOfSourceLandmarks = sourceLandmarksItk_Lps->GetNumberOfPoints();
1176 for(
unsigned int i = 0; i < numberOfSourceLandmarks; i++)
1178 typename ThinPlateSplineTransformType::InputPointType pointItk_Lps;
1179 bool pointExists = sourceLandmarksItk_Lps->GetPoint(i, &pointItk_Lps);
1184 double pointVtk_Ras[3]={0};
1185 pointVtk_Ras[0] = -pointItk_Lps[0];
1186 pointVtk_Ras[1] = -pointItk_Lps[1];
1187 pointVtk_Ras[2] = pointItk_Lps[2];
1188 sourceLandmarksVtk_Ras->InsertNextPoint(pointVtk_Ras);
1191 vtkNew<vtkPoints> targetLandmarksVtk_Ras;
1192 unsigned int numberOfTargetLandmarks = targetLandmarksItk_Lps->GetNumberOfPoints();
1193 for(
unsigned int i = 0; i < numberOfTargetLandmarks; i++)
1195 typename ThinPlateSplineTransformType::InputPointType pointItk_Lps;
1196 bool pointExists = targetLandmarksItk_Lps->GetPoint(i, &pointItk_Lps);
1201 double pointVtk_Ras[3]={0};
1202 pointVtk_Ras[0] = -pointItk_Lps[0];
1203 pointVtk_Ras[1] = -pointItk_Lps[1];
1204 pointVtk_Ras[2] = pointItk_Lps[2];
1205 targetLandmarksVtk_Ras->InsertNextPoint(pointVtk_Ras);
1208 transformVtk_RAS->SetBasisToR();
1209 transformVtk_RAS->SetSourceLandmarks(sourceLandmarksVtk_Ras.GetPointer());
1210 transformVtk_RAS->SetTargetLandmarks(targetLandmarksVtk_Ras.GetPointer());
1214 transformVtk_RAS->Inverse();
1221 itk::Object::Pointer& transformItk_LPS, vtkThinPlateSplineTransform* transformVtk_RAS,
bool initialize )
1223 if (transformVtk_RAS==
nullptr)
1225 vtkErrorWithObjectMacro(loggerObject,
"Cannot set ITK thin-plate spline transform from VTK: the input vtkThinPlateSplineTransform is invalid");
1230 transformVtk_RAS->Update();
1232 if (transformVtk_RAS->GetBasis()!=VTK_RBF_R)
1234 vtkErrorWithObjectMacro(loggerObject,
"Cannot set ITK thin-plate spline transform from VTK: basis function must be R."
1235 " Call SetBasisToR() method of the vtkThinPlateSplineTransform object before attempting to write it to file.");
1239 ThinPlateSplineTransformDoubleType::PointSetType::Pointer sourceLandmarksItk_Lps = ThinPlateSplineTransformDoubleType::PointSetType::New();
1240 vtkPoints* sourceLandmarksVtk_Ras=transformVtk_RAS->GetSourceLandmarks();
1241 if (sourceLandmarksVtk_Ras!=
nullptr)
1243 for (
int i=0; i<sourceLandmarksVtk_Ras->GetNumberOfPoints(); i++)
1245 double posVtk_Ras[3]={0};
1246 sourceLandmarksVtk_Ras->GetPoint(i, posVtk_Ras);
1247 ThinPlateSplineTransformDoubleType::InputPointType posItk_Lps;
1248 posItk_Lps[0] = -posVtk_Ras[0];
1249 posItk_Lps[1] = -posVtk_Ras[1];
1250 posItk_Lps[2] = posVtk_Ras[2];
1251 sourceLandmarksItk_Lps->GetPoints()->InsertElement(i,posItk_Lps);
1254 ThinPlateSplineTransformDoubleType::PointSetType::Pointer targetLandmarksItk_Lps = ThinPlateSplineTransformDoubleType::PointSetType::New();
1255 vtkPoints* targetLandmarksVtk_Ras=transformVtk_RAS->GetTargetLandmarks();
1256 if (targetLandmarksVtk_Ras!=
nullptr)
1258 for (
int i=0; i<targetLandmarksVtk_Ras->GetNumberOfPoints(); i++)
1260 double posVtk_Ras[3]={0};
1261 targetLandmarksVtk_Ras->GetPoint(i, posVtk_Ras);
1262 ThinPlateSplineTransformDoubleType::InputPointType posItk_Lps;
1263 posItk_Lps[0] = -posVtk_Ras[0];
1264 posItk_Lps[1] = -posVtk_Ras[1];
1265 posItk_Lps[2] = posVtk_Ras[2];
1266 targetLandmarksItk_Lps->GetPoints()->InsertElement(i,posItk_Lps);
1270 if (transformVtk_RAS->GetInverseFlag())
1273 tpsTransformItk->SetSourceLandmarks(sourceLandmarksItk_Lps);
1274 tpsTransformItk->SetTargetLandmarks(targetLandmarksItk_Lps);
1277 tpsTransformItk->ComputeWMatrix();
1279 transformItk_LPS = tpsTransformItk;
1283 ThinPlateSplineTransformDoubleType::Pointer tpsTransformItk = ThinPlateSplineTransformDoubleType::New();
1284 tpsTransformItk->SetSourceLandmarks(sourceLandmarksItk_Lps);
1285 tpsTransformItk->SetTargetLandmarks(targetLandmarksItk_Lps);
1288 tpsTransformItk->ComputeWMatrix();
1290 transformItk_LPS = tpsTransformItk;
1299 vtkObject* loggerObject,
1300 typename itk::TransformBaseTemplate<T>::Pointer transformItk)
1302 bool conversionSuccess =
false;
1305 vtkNew<vtkMatrix4x4> transformMatrixVtk;
1306 conversionSuccess = SetVTKLinearTransformFromITK<T>(loggerObject, transformMatrixVtk.GetPointer(), transformItk);
1307 if (conversionSuccess)
1309 vtkNew<vtkTransform> linearTransformVtk;
1310 linearTransformVtk->SetMatrix(transformMatrixVtk.GetPointer());
1311 linearTransformVtk->Register(
nullptr);
1312 return linearTransformVtk.GetPointer();
1315 vtkNew<vtkOrientedGridTransform> gridTransformVtk;
1316 conversionSuccess = SetVTKOrientedGridTransformFromITK<T>(loggerObject, gridTransformVtk.GetPointer(), transformItk);
1317 if (conversionSuccess)
1319 gridTransformVtk->Register(
nullptr);
1320 return gridTransformVtk.GetPointer();
1323 vtkNew<vtkOrientedBSplineTransform> bsplineTransformVtk;
1324 conversionSuccess = SetVTKBSplineFromITKv4Generic<T>(loggerObject, bsplineTransformVtk.GetPointer(), transformItk);
1325 if (conversionSuccess)
1327 bsplineTransformVtk->Register(
nullptr);
1328 return bsplineTransformVtk.GetPointer();
1331 vtkNew<vtkThinPlateSplineTransform> tpsTransformVtk;
1332 conversionSuccess = SetVTKThinPlateSplineTransformFromITK<T>(loggerObject, tpsTransformVtk.GetPointer(), transformItk);
1333 if (conversionSuccess)
1335 tpsTransformVtk->Register(
nullptr);
1336 return tpsTransformVtk.GetPointer();
1344 vtkAbstractTransform* transformVtk, itk::Object::Pointer& secondaryTransformItk,
int preferITKv3CompatibleTransforms,
bool initialize )
1346 typedef itk::CompositeTransform< double > CompositeTransformType;
1348 if (transformVtk==
nullptr)
1350 vtkErrorWithObjectMacro(loggerObject,
"CreateITKTransformFromVTK failed: invalid VTK transform");
1353 vtkNew<vtkCollection> transformList;
1355 if (transformList->GetNumberOfItems()==0)
1358 vtkNew<vtkTransform> identity;
1359 transformList->AddItem(identity.GetPointer());
1362 itk::Object::Pointer primaryTransformItk;
1363 if (transformList->GetNumberOfItems()==1)
1366 vtkObject* singleTransformVtk = transformList->GetItemAsObject(0);
1368 if (vtkHomogeneousTransform::SafeDownCast(singleTransformVtk))
1370 vtkHomogeneousTransform* linearTransformVtk = vtkHomogeneousTransform::SafeDownCast(singleTransformVtk);
1371 vtkMatrix4x4* transformMatrix = linearTransformVtk->GetMatrix();
1377 return primaryTransformItk;
1380 else if (vtkOrientedBSplineTransform::SafeDownCast(singleTransformVtk))
1382 vtkOrientedBSplineTransform* bsplineTransformVtk = vtkOrientedBSplineTransform::SafeDownCast(singleTransformVtk);
1383 vtkMatrix4x4* bulkMatrix = bsplineTransformVtk->GetBulkTransformMatrix();
1384 if (preferITKv3CompatibleTransforms || (bulkMatrix!=
nullptr && !
IsIdentityMatrix(bulkMatrix)))
1386 if (!
SetITKv3BSplineFromVTK(loggerObject, primaryTransformItk, secondaryTransformItk, bsplineTransformVtk, preferITKv3CompatibleTransforms))
1391 return primaryTransformItk;
1400 return primaryTransformItk;
1404 else if (vtkOrientedGridTransform::SafeDownCast(singleTransformVtk))
1406 vtkOrientedGridTransform* gridTransformVtk = vtkOrientedGridTransform::SafeDownCast(singleTransformVtk);
1412 return primaryTransformItk;
1415 else if (vtkThinPlateSplineTransform::SafeDownCast(singleTransformVtk))
1417 vtkThinPlateSplineTransform* tpsTransformVtk = vtkThinPlateSplineTransform::SafeDownCast(singleTransformVtk);
1423 return primaryTransformItk;
1427 if (singleTransformVtk==
nullptr)
1429 vtkErrorWithObjectMacro(loggerObject,
"vtkITKTransformConverter::CreateITKTransformFromVTK failed: invalid input transform");
1432 vtkErrorWithObjectMacro(loggerObject,
"vtkITKTransformConverter::CreateITKTransformFromVTK failed: conversion of transform type "
1433 << singleTransformVtk->GetClassName() <<
" is not supported");
1441 CompositeTransformType::Pointer compositeTransformItk = CompositeTransformType::New();
1442 primaryTransformItk = compositeTransformItk;
1444 for (
int transformIndex = transformList->GetNumberOfItems()-1; transformIndex>=0; --transformIndex)
1446 vtkAbstractTransform* singleTransformVtk = vtkAbstractTransform::SafeDownCast(transformList->GetItemAsObject(transformIndex));
1447 itk::Object::Pointer secondaryTransformItkTmp;
1451 itk::Object::Pointer singleTransformItk =
CreateITKTransformFromVTK(loggerObject, singleTransformVtk, secondaryTransformItkTmp,
false );
1452 if (secondaryTransformItkTmp.IsNotNull())
1454 vtkErrorWithObjectMacro(loggerObject,
"vtkITKTransformConverter::CreateITKTransformFromVTK failed:"
1455 " composite transforms cannot contain legacy transforms (that contains secondary transforms)."
1456 " Do not harden transforms on legacy ITK transforms to avoid this error.");
1460 if (singleTransformItk.IsNull()
1461 || std::string(singleTransformItk->GetNameOfClass()).find(
"Transform") == std::string::npos)
1463 vtkErrorWithObjectMacro(loggerObject,
"vtkITKTransformConverter::CreateITKTransformFromVTK failed:"
1464 " invalid element found while trying to create a composite transform");
1467 CompositeTransformType::TransformType::Pointer singleTransformItkTypeChecked =
1468 static_cast< CompositeTransformType::TransformType*
>( singleTransformItk.GetPointer() );
1469 compositeTransformItk->AddTransform(singleTransformItkTypeChecked.GetPointer());
1471 return primaryTransformItk;