66 static vtkAbstractTransform*
CreateVTKTransformFromITK(vtkObject* loggerObject,
typename itk::TransformBaseTemplate<T>::Pointer transformItk,
67 double center_LocalRAS[3]=
nullptr);
80 itk::Object::Pointer& secondaryTransformItk,
int preferITKv3CompatibleTransforms,
bool initialize =
true,
double center_LocalRAS[3] =
nullptr);
83 typename itk::TransformBaseTemplate<T>::Pointer warpTransformItk,
typename itk::TransformBaseTemplate<T>::Pointer bulkTransformItk);
87 typename itk::DisplacementFieldTransform< T, 3 >::DisplacementFieldType::Pointer gridImage_Lps);
89 vtkOrientedGridTransform* grid_Ras);
95 typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS,
double center_LocalRAS[3]=
nullptr);
96 static bool SetITKLinearTransformFromVTK(vtkObject* loggerObject, itk::Object::Pointer& transformItk_LPS, vtkMatrix4x4* transformVtk_RAS,
97 double center_LocalRAS[3]=
nullptr);
101 typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS);
104 static bool SetITKv3BSplineFromVTK(vtkObject* loggerObject, itk::Object::Pointer& warpTransformItk, itk::Object::Pointer& bulkTransformItk,
105 vtkOrientedBSplineTransform* bsplineVtk,
bool alwaysAddBulkTransform);
106 static bool SetITKv4BSplineFromVTK(vtkObject* loggerObject, itk::Object::Pointer& warpTransformItk, vtkOrientedBSplineTransform* bsplineVtk);
110 typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS);
112 vtkThinPlateSplineTransform* transformVtk_RAS,
bool initialize =
true);
117 vtkOrientedBSplineTransform* bsplineVtk,
typename itk::TransformBaseTemplate<typename BSplineTransformType::ScalarType>::Pointer warpTransformItk);
119 vtkOrientedBSplineTransform* bsplineVtk,
typename itk::TransformBaseTemplate<T>::Pointer warpTransformItk);
122 typename itk::Transform< typename BSplineTransformType::ScalarType, VTKDimension, VTKDimension>::Pointer& warpTransformItk,
123 vtkOrientedBSplineTransform* bsplineVtk);
125 typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& warpTransformItk,
126 typename itk::Transform<T, VTKDimension, VTKDimension>::Pointer& bulkTransformItk, vtkOrientedBSplineTransform* bsplineVtk,
127 bool alwaysAddBulkTransform);
129 typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& warpTransformItk, vtkOrientedBSplineTransform* bsplineVtk);
136 itk::TransformFactory<InverseDisplacementFieldTransformFloatType>::RegisterTransform();
137 itk::TransformFactory<InverseDisplacementFieldTransformDoubleType>::RegisterTransform();
139 itk::TransformFactory<InverseBSplineTransformFloatITKv3Type>::RegisterTransform();
140 itk::TransformFactory<InverseBSplineTransformFloatITKv4Type>::RegisterTransform();
141 itk::TransformFactory<InverseBSplineTransformDoubleITKv3Type>::RegisterTransform();
142 itk::TransformFactory<InverseBSplineTransformDoubleITKv4Type>::RegisterTransform();
144 itk::TransformFactory<InverseThinPlateSplineTransformFloatType>::RegisterTransform();
145 itk::TransformFactory<InverseThinPlateSplineTransformDoubleType>::RegisterTransform();
147 typedef itk::ThinPlateSplineKernelTransform<float,3> ThinPlateSplineTransformFloatType;
151 itk::TransformFactory<ThinPlateSplineTransformFloatType>::RegisterTransform();
152 itk::TransformFactory<ThinPlateSplineTransformDoubleType>::RegisterTransform();
159 vtkMatrix4x4* transformVtk_RAS,
160 typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS,
161 double center_LocalRAS[3] )
164 typedef itk::MatrixOffsetTransformBase<T,D,D> LinearTransformType;
165 typedef itk::ScaleTransform<T, D> ScaleTransformType;
166 typedef itk::TranslationTransform<T, D> TranslateTransformType;
168 vtkSmartPointer<vtkMatrix4x4> transformVtk_LPS = vtkSmartPointer<vtkMatrix4x4>::New();
170 bool convertedToVtkMatrix=
false;
172 std::string itkTransformClassName = transformItk_LPS->GetNameOfClass();
191 if (itkTransformClassName.find(
"AffineTransform" ) != std::string::npos ||
192 itkTransformClassName ==
"MatrixOffsetTransformBase" ||
193 itkTransformClassName ==
"Rigid3DTransform" ||
194 itkTransformClassName ==
"Euler3DTransform" ||
195 itkTransformClassName ==
"CenteredEuler3DTransform" ||
196 itkTransformClassName ==
"QuaternionRigidTransform" ||
197 itkTransformClassName ==
"VersorTransform" ||
198 itkTransformClassName ==
"VersorRigid3DTransform" ||
199 itkTransformClassName ==
"ScaleSkewVersor3DTransform" ||
200 itkTransformClassName ==
"ScaleVersor3DTransform" ||
201 itkTransformClassName ==
"Similarity3DTransform" ||
202 itkTransformClassName ==
"ScaleTransform" ||
203 itkTransformClassName ==
"ScaleLogarithmicTransform")
205 typename LinearTransformType::Pointer dlt
206 =
static_cast<LinearTransformType*
>( transformItk_LPS.GetPointer() );
207 convertedToVtkMatrix=
true;
208 for (
unsigned int i=0; i < D; i++)
210 for (
unsigned int j=0; j < D; j++)
212 transformVtk_LPS->SetElement(i, j, dlt->GetMatrix()[i][j]);
214 transformVtk_LPS->SetElement(i, D, dlt->GetOffset()[i]);
219 auto center_LocalLPS = dlt->GetCenter();
220 center_LocalRAS[0] = -center_LocalLPS[0];
221 center_LocalRAS[1] = -center_LocalLPS[1];
222 center_LocalRAS[2] = center_LocalLPS[2];
227 if (itkTransformClassName ==
"IdentityTransform")
230 convertedToVtkMatrix=
true;
234 if (itkTransformClassName ==
"ScaleTransform")
236 typename ScaleTransformType::Pointer dst
237 =
static_cast<ScaleTransformType*
>( transformItk_LPS.GetPointer() );
238 convertedToVtkMatrix=
true;
239 for (
unsigned int i=0; i < D; i++)
241 transformVtk_LPS->SetElement(i, i, dst->GetScale()[i]);
246 if (itkTransformClassName ==
"TranslationTransform")
248 typename TranslateTransformType::Pointer dtt
249 =
static_cast<TranslateTransformType*
>( transformItk_LPS.GetPointer());
250 convertedToVtkMatrix=
true;
251 for (
unsigned int i=0; i < D; i++)
253 transformVtk_LPS->SetElement(i, D, dtt->GetOffset()[i]);
262 vtkSmartPointer<vtkMatrix4x4> lps2ras = vtkSmartPointer<vtkMatrix4x4>::New();
263 lps2ras->SetElement(0,0,-1);
264 lps2ras->SetElement(1,1,-1);
265 vtkMatrix4x4* ras2lps = lps2ras;
267 vtkMatrix4x4::Multiply4x4(lps2ras, transformVtk_LPS, transformVtk_LPS);
268 vtkMatrix4x4::Multiply4x4(transformVtk_LPS, ras2lps, transformVtk_RAS);
272 for (
int i = 0; i < 3; ++i)
277 double offset_RAS = transformVtk_RAS->GetElement(i, 3);
278 center_LocalRAS[i] = center_LocalRAS[i] + offset_RAS;
282 return convertedToVtkMatrix;
287 double center_LocalRAS[3] )
291 if (transformVtk_RAS==
nullptr)
293 vtkErrorWithObjectMacro(loggerObject,
"vtkITKTransformConverter::SetITKLinearTransformFromVTK failed: invalid input transform");
297 vtkSmartPointer<vtkMatrix4x4> lps2ras = vtkSmartPointer<vtkMatrix4x4>::New();
298 lps2ras->SetElement(0,0,-1);
299 lps2ras->SetElement(1,1,-1);
300 vtkMatrix4x4* ras2lps = lps2ras;
306 vtkSmartPointer<vtkMatrix4x4> vtkmat = vtkSmartPointer<vtkMatrix4x4>::New();
308 vtkMatrix4x4::Multiply4x4(ras2lps, transformVtk_RAS, vtkmat);
309 vtkMatrix4x4::Multiply4x4(vtkmat, lps2ras, vtkmat);
311 typedef AffineTransformType::MatrixType MatrixType;
312 typedef AffineTransformType::OutputVectorType OffsetType;
315 OffsetType itkoffset;
321 itkmat[i][j] = vtkmat->GetElement(i, j);
326 AffineTransformType::Pointer affine = AffineTransformType::New();
337 double center_LocalLPS[3] = { -center_LocalRAS[0], -center_LocalRAS[1], center_LocalRAS[2] };
342 vtkMath::Subtract(center_LocalLPS, itkoffset, center_LocalLPS);
343 affine->SetCenter(center_LocalLPS);
345 affine->SetMatrix(itkmat);
346 affine->SetOffset(itkoffset);
348 transformItk_LPS = affine;
374 vtkObject* loggerObject,
375 vtkOrientedBSplineTransform* bsplineVtk,
376 typename itk::TransformBaseTemplate<typename BSplineTransformType::ScalarType>::Pointer warpTransformItk)
381 typedef typename BSplineTransformType::ScalarType T;
382 if (bsplineVtk==
nullptr)
384 vtkErrorWithObjectMacro(loggerObject,
"vtkMRMLTransformStorageNode::SetVTKBSplineFromITKv4 failed: bsplineVtk is invalid");
387 bool isDoublePrecisionInput=
true;
388 if (
sizeof(T)==
sizeof(
float))
390 isDoublePrecisionInput=
false;
392 else if (
sizeof(T)==
sizeof(
double))
394 isDoublePrecisionInput=
true;
398 vtkErrorWithObjectMacro(loggerObject,
"Unsupported scalar type in BSpline transform file (only float and double are supported)");
402 typename BSplineTransformType::Pointer bsplineItk = BSplineTransformType::New();
403 std::string warpTransformItkName = warpTransformItk->GetNameOfClass();
404 std::string requestedWarpTransformItkName = bsplineItk->GetNameOfClass();
405 if (warpTransformItkName != requestedWarpTransformItkName)
409 if (warpTransformItk->GetOutputSpaceDimension() !=
VTKDimension)
411 vtkErrorWithObjectMacro(loggerObject,
"Unsupported number of dimensions in BSpline transform file (expected = "
412 <<
VTKDimension <<
", actual = " << warpTransformItk->GetOutputSpaceDimension() <<
")");
415 bsplineItk =
static_cast< BSplineTransformType*
>( warpTransformItk.GetPointer() );
423 const typename BSplineTransformType::CoefficientImageArray coefficientImages =
424 bsplineItk->GetCoefficientImages();
428 typename BSplineTransformType::MeshSizeType meshSize =
429 coefficientImages[0]->GetLargestPossibleRegion().GetSize();
432 typename BSplineTransformType::OriginType origin =
433 coefficientImages[0]->GetOrigin();
436 typename BSplineTransformType::SpacingType spacing =
437 coefficientImages[0]->GetSpacing();
440 typename BSplineTransformType::DirectionType direction =
441 coefficientImages[0]->GetDirection();
443 vtkNew<vtkMatrix4x4> gridDirectionMatrix_LPS;
446 for (
unsigned int column=0; column<
VTKDimension; column++)
448 gridDirectionMatrix_LPS->SetElement(row,column,direction[row][column]);
453 bsplineVtk->SetBorderModeToZero();
455 vtkNew<vtkImageData> bsplineCoefficients;
457 bsplineCoefficients->SetExtent(0, meshSize[0]-1, 0, meshSize[1]-1, 0, meshSize[2]-1);
458 bsplineCoefficients->SetSpacing(spacing[0], spacing[1], spacing[2]);
461 vtkNew<vtkMatrix4x4> lpsToRas;
462 lpsToRas->SetElement(0,0,-1);
463 lpsToRas->SetElement(1,1,-1);
465 vtkNew<vtkMatrix4x4> rasToLps;
466 rasToLps->SetElement(0,0,-1);
467 rasToLps->SetElement(1,1,-1);
469 vtkNew<vtkMatrix4x4> gridDirectionMatrix_RAS;
470 vtkMatrix4x4::Multiply4x4(
471 lpsToRas.GetPointer(),
472 gridDirectionMatrix_LPS.GetPointer(),
473 gridDirectionMatrix_RAS.GetPointer());
474 bsplineVtk->SetGridDirectionMatrix(gridDirectionMatrix_RAS.GetPointer());
477 double gridOrigin_RAS[3]={-origin[0], -origin[1], origin[2]};
478 bsplineCoefficients->SetOrigin(gridOrigin_RAS);
480 int bsplineCoefficientsScalarType=VTK_FLOAT;
481 if (isDoublePrecisionInput)
483 bsplineCoefficientsScalarType=VTK_DOUBLE;
486 bsplineCoefficients->AllocateScalars(bsplineCoefficientsScalarType, 3);
488 const unsigned int expectedNumberOfVectors = meshSize[0]*meshSize[1]*meshSize[2];
489 const unsigned int expectedNumberOfParameters = expectedNumberOfVectors*
VTKDimension;
490 const unsigned int actualNumberOfParameters = bsplineItk->GetNumberOfParameters();
492 if( actualNumberOfParameters != expectedNumberOfParameters )
494 vtkErrorWithObjectMacro(loggerObject,
"Mismatch in number of BSpline parameters in the transform file and the MRML node");
497 const T* itkBSplineParams_LPS =
static_cast<const T*
>(bsplineItk->GetParameters().data_block());
498 T* vtkBSplineParams_RAS=
static_cast<T*
>(bsplineCoefficients->GetScalarPointer());
499 for (
unsigned int i=0; i<expectedNumberOfVectors; i++)
501 *(vtkBSplineParams_RAS++) = - (*(itkBSplineParams_LPS ));
502 *(vtkBSplineParams_RAS++) = - (*(itkBSplineParams_LPS+expectedNumberOfVectors ));
503 *(vtkBSplineParams_RAS++) = (*(itkBSplineParams_LPS+expectedNumberOfVectors*2));
504 itkBSplineParams_LPS++;
506 bsplineVtk->SetCoefficientData(bsplineCoefficients.GetPointer());
514 vtkOrientedBSplineTransform* bsplineVtk,
515 typename itk::TransformBaseTemplate<T>::Pointer warpTransformItk,
typename itk::TransformBaseTemplate<T>::Pointer bulkTransformItk)
517 if (bsplineVtk==
nullptr)
519 vtkErrorWithObjectMacro(loggerObject,
"vtkMRMLTransformStorageNode::SetVTKBSplineFromITK failed: bsplineVtk is invalid");
523 bool inverse =
false;
535 vtkDebugWithObjectMacro(loggerObject,
"Not an ITKv3 BSpline transform");
540 if( bulkTransformItk )
542 std::string bulkTransformItkTransformName = bulkTransformItk->GetNameOfClass();
544 typedef itk::AffineTransform<T,3> BulkTransformType;
546 if (bulkTransformItkTransformName ==
"AffineTransform")
548 BulkTransformType* bulkItkAffine =
static_cast<BulkTransformType*
> (bulkTransformItk.GetPointer());
549 vtkNew<vtkMatrix4x4> bulkMatrix_LPS;
554 bulkMatrix_LPS->SetElement(i,j, bulkItkAffine->GetMatrix()[i][j]);
556 bulkMatrix_LPS->SetElement(i,
VTKDimension, bulkItkAffine->GetOffset()[i]);
558 vtkNew<vtkMatrix4x4> lpsToRas;
559 lpsToRas->SetElement(0,0,-1);
560 lpsToRas->SetElement(1,1,-1);
561 vtkNew<vtkMatrix4x4> rasToLps;
562 rasToLps->SetElement(0,0,-1);
563 rasToLps->SetElement(1,1,-1);
564 vtkNew<vtkMatrix4x4> bulkMatrix_RAS;
565 vtkMatrix4x4::Multiply4x4(lpsToRas.GetPointer(), bulkMatrix_LPS.GetPointer(), bulkMatrix_RAS.GetPointer());
566 vtkMatrix4x4::Multiply4x4(bulkMatrix_RAS.GetPointer(), rasToLps.GetPointer(), bulkMatrix_RAS.GetPointer());
567 bsplineVtk->SetBulkTransformMatrix(bulkMatrix_RAS.GetPointer());
569 else if (bulkTransformItkTransformName ==
"IdentityTransform")
575 vtkErrorWithObjectMacro(loggerObject,
"Cannot read the 2nd transform in BSplineTransform (expected AffineTransform_double_3_3 or IdentityTransform)" );
582 bsplineVtk->Inverse();
617 typename itk::Transform< typename BSplineTransformType::ScalarType,VTKDimension,VTKDimension>::Pointer& warpTransformItk,
618 vtkOrientedBSplineTransform* bsplineVtk)
620 typedef typename BSplineTransformType::ScalarType T;
621 if (bsplineVtk==
nullptr)
623 vtkErrorWithObjectMacro(loggerObject,
"vtkMRMLTransformStorageNode::SetITKBSplineFromVTK failed: bsplineVtk is invalid");
626 vtkImageData* bsplineCoefficients_RAS=bsplineVtk->GetCoefficientData();
627 if (bsplineCoefficients_RAS==
nullptr)
629 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file: coefficients are not specified");
633 typename BSplineTransformType::Pointer bsplineItk = BSplineTransformType::New();
634 warpTransformItk = bsplineItk;
641 typename BSplineTransformType::FixedParametersType transformFixedParamsItk;
643 transformFixedParamsItk.SetSize(numberOfFixedParameters);
645 int *gridExtent=bsplineCoefficients_RAS->GetExtent();
646 int gridSize[3]={gridExtent[1]-gridExtent[0]+1, gridExtent[3]-gridExtent[2]+1, gridExtent[5]-gridExtent[4]+1};
647 transformFixedParamsItk[0]=gridSize[0];
648 transformFixedParamsItk[1]=gridSize[1];
649 transformFixedParamsItk[2]=gridSize[2];
651 double* gridOrigin_RAS=bsplineCoefficients_RAS->GetOrigin();
652 double gridOrigin_LPS[3]={-gridOrigin_RAS[0], -gridOrigin_RAS[1], gridOrigin_RAS[2]};
653 transformFixedParamsItk[3]=gridOrigin_LPS[0];
654 transformFixedParamsItk[4]=gridOrigin_LPS[1];
655 transformFixedParamsItk[5]=gridOrigin_LPS[2];
657 double* gridSpacing=bsplineCoefficients_RAS->GetSpacing();
658 transformFixedParamsItk[6]=gridSpacing[0];
659 transformFixedParamsItk[7]=gridSpacing[1];
660 transformFixedParamsItk[8]=gridSpacing[2];
662 vtkNew<vtkMatrix4x4> gridDirectionMatrix_RAS;
663 if (bsplineVtk->GetGridDirectionMatrix()!=
nullptr)
665 gridDirectionMatrix_RAS->DeepCopy(bsplineVtk->GetGridDirectionMatrix());
667 vtkNew<vtkMatrix4x4> lpsToRas;
668 lpsToRas->SetElement(0,0,-1);
669 lpsToRas->SetElement(1,1,-1);
670 vtkNew<vtkMatrix4x4> rasToLps;
671 rasToLps->SetElement(0,0,-1);
672 rasToLps->SetElement(1,1,-1);
673 vtkNew<vtkMatrix4x4> gridDirectionMatrix_LPS;
674 vtkMatrix4x4::Multiply4x4(rasToLps.GetPointer(), gridDirectionMatrix_RAS.GetPointer(), gridDirectionMatrix_LPS.GetPointer());
678 for (
unsigned int column=0; column<
VTKDimension; column++)
680 transformFixedParamsItk[fpIndex++]=gridDirectionMatrix_LPS->GetElement(row,column);
684 bsplineItk->SetFixedParameters(transformFixedParamsItk);
688 const unsigned int expectedNumberOfVectors = gridSize[0]*gridSize[1]*gridSize[2];
689 const unsigned int expectedNumberOfParameters = expectedNumberOfVectors*
VTKDimension;
690 if( bsplineItk->GetNumberOfParameters() != expectedNumberOfParameters )
692 vtkErrorWithObjectMacro(loggerObject,
"Mismatch in number of BSpline parameters in the ITK transform and the VTK transform");
696 typename BSplineTransformType::ParametersType transformParamsItk(expectedNumberOfParameters);
697 T* itkBSplineParams_LPS =
static_cast<T*
>(transformParamsItk.data_block());
698 T* vtkBSplineParams_RAS=
static_cast<T*
>(bsplineCoefficients_RAS->GetScalarPointer());
699 double coefficientScale = bsplineVtk->GetDisplacementScale();
700 for (
unsigned int i=0; i<expectedNumberOfVectors; i++)
702 *(itkBSplineParams_LPS ) = -coefficientScale * (*(vtkBSplineParams_RAS++));
703 *(itkBSplineParams_LPS+expectedNumberOfVectors ) = -coefficientScale * (*(vtkBSplineParams_RAS++));
704 *(itkBSplineParams_LPS+expectedNumberOfVectors*2) = coefficientScale * (*(vtkBSplineParams_RAS++));
705 itkBSplineParams_LPS++;
708 bsplineItk->SetParameters(transformParamsItk);
714 typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& warpTransformItk,
715 typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& bulkTransformItk,
716 vtkOrientedBSplineTransform* bsplineVtk,
bool alwaysAddBulkTransform)
718 if (bsplineVtk==
nullptr)
720 vtkErrorWithObjectMacro(loggerObject,
"vtkMRMLTransformStorageNode::SetITKBSplineFromVTK failed: bsplineVtk is invalid");
724 bsplineVtk->Update();
725 bool itkTransformSetSuccessfully =
false;
726 if (bsplineVtk->GetInverseFlag())
734 itk::BSplineDeformableTransform< T, VTKDimension, VTKDimension > >(loggerObject, warpTransformItk, bsplineVtk);
736 if (!itkTransformSetSuccessfully)
738 vtkErrorWithObjectMacro(loggerObject,
"vtkMRMLTransformStorageNode::SetITKBSplineFromVTK failed: cannot determine BSpline parameters");
742 vtkMatrix4x4* bulkMatrix_RAS=bsplineVtk->GetBulkTransformMatrix();
743 if (bulkMatrix_RAS || alwaysAddBulkTransform)
745 vtkNew<vtkMatrix4x4> lpsToRas;
746 lpsToRas->SetElement(0,0,-1);
747 lpsToRas->SetElement(1,1,-1);
748 vtkNew<vtkMatrix4x4> rasToLps;
749 rasToLps->SetElement(0,0,-1);
750 rasToLps->SetElement(1,1,-1);
751 vtkNew<vtkMatrix4x4> bulkMatrix_LPS;
754 if (bulkMatrix_RAS!=
nullptr)
756 vtkMatrix4x4::Multiply4x4(rasToLps.GetPointer(), bulkMatrix_RAS, bulkMatrix_LPS.GetPointer());
757 vtkMatrix4x4::Multiply4x4(bulkMatrix_LPS.GetPointer(), lpsToRas.GetPointer(), bulkMatrix_LPS.GetPointer());
759 typedef itk::AffineTransform<T,VTKDimension> BulkTransformType;
760 typename BulkTransformType::Pointer affineItk = BulkTransformType::New();
761 bulkTransformItk = affineItk;
763 typename BulkTransformType::MatrixType affineMatrix;
764 typename BulkTransformType::OffsetType affineOffset;
769 affineMatrix[i][j]=bulkMatrix_LPS->GetElement(i,j);
771 affineOffset[i]=bulkMatrix_LPS->GetElement(i,
VTKDimension);
774 affineItk->SetMatrix(affineMatrix);
775 affineItk->SetOffset(affineOffset);
779 bulkTransformItk=
nullptr;
787 typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& warpTransformItk,
788 vtkOrientedBSplineTransform* bsplineVtk)
791 bsplineVtk->Update();
792 bool itkTransformSetSuccessfully =
false;
793 if (bsplineVtk->GetInverseFlag())
801 itk::BSplineTransform< T, VTKDimension, VTKDimension > >(loggerObject, warpTransformItk, bsplineVtk);
803 if (!itkTransformSetSuccessfully)
805 vtkErrorWithObjectMacro(loggerObject,
"vtkMRMLTransformStorageNode::SetITKv4BSplineFromVTKGeneric failed: cannot determine BSpline parameters");
813 itk::Object::Pointer& bulkTransformItk, vtkOrientedBSplineTransform* bsplineVtk,
bool alwaysAddBulkTransform)
815 if (bsplineVtk==
nullptr)
817 vtkErrorWithObjectMacro(loggerObject,
"Cannot retrieve BSpline transform from node");
821 vtkImageData* bsplineCoefficients=bsplineVtk->GetCoefficientData();
823 if (bsplineCoefficients==
nullptr)
825 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file: coefficients are not specified");
829 if (bsplineCoefficients->GetScalarType()==VTK_FLOAT)
831 typedef itk::Transform<float, VTKDimension, VTKDimension > ITKTransformType;
832 ITKTransformType::Pointer floatWarpTransformItk;
833 ITKTransformType::Pointer floatBulkTransformItk;
836 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file");
839 warpTransformItk = floatWarpTransformItk.GetPointer();
840 bulkTransformItk = floatBulkTransformItk.GetPointer();
842 else if (bsplineCoefficients->GetScalarType()==VTK_DOUBLE)
844 typedef itk::Transform<double, VTKDimension, VTKDimension > ITKTransformType;
845 ITKTransformType::Pointer doubleWarpTransformItk;
846 ITKTransformType::Pointer doubleBulkTransformItk;
849 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file");
852 warpTransformItk = doubleWarpTransformItk;
853 bulkTransformItk = doubleBulkTransformItk;
857 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file: only float and double coefficient types are supported");
866 itk::Object::Pointer& warpTransformItk, vtkOrientedBSplineTransform* bsplineVtk)
868 if (bsplineVtk==
nullptr)
870 vtkErrorWithObjectMacro(loggerObject,
"Cannot retrieve BSpline transform from node");
874 vtkImageData* bsplineCoefficients=bsplineVtk->GetCoefficientData();
876 if (bsplineCoefficients==
nullptr)
878 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file: coefficients are not specified");
882 if (bsplineCoefficients->GetScalarType()==VTK_FLOAT)
884 typedef itk::Transform<float, VTKDimension, VTKDimension > ITKTransformType;
885 ITKTransformType::Pointer floatWarpTransformItk;
888 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file");
891 warpTransformItk = floatWarpTransformItk.GetPointer();
893 else if (bsplineCoefficients->GetScalarType()==VTK_DOUBLE)
895 typedef itk::Transform<double, VTKDimension, VTKDimension > ITKTransformType;
896 ITKTransformType::Pointer doubleWarpTransformItk;
899 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file");
902 warpTransformItk = doubleWarpTransformItk;
906 vtkErrorWithObjectMacro(loggerObject,
"Cannot write BSpline transform to file: only float and double coefficient types are supported");
916 vtkOrientedGridTransform* transformVtk_RAS,
typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS)
918 typedef itk::DisplacementFieldTransform< T, 3 > DisplacementFieldTransformType;
921 if (!transformItk_LPS)
923 vtkErrorWithObjectMacro(loggerObject,
"Cannot set VTK oriented grid transform from ITK: the input transform is nullptr");
926 if (transformItk_LPS->GetOutputSpaceDimension() !=
VTKDimension)
928 vtkErrorWithObjectMacro(loggerObject,
"Unsupported number of dimensions in oriented grid transform file (expected = "
929 <<
VTKDimension <<
", actual = " << transformItk_LPS->GetOutputSpaceDimension() <<
")");
933 std::string transformItkClassName = transformItk_LPS->GetNameOfClass();
935 bool inverse =
false;
936 typename DisplacementFieldTransformType::DisplacementFieldType* gridImageItk_Lps =
nullptr;
937 if (transformItkClassName ==
"InverseDisplacementFieldTransform")
939 DisplacementFieldTransformType* inverseDisplacementFieldTransform =
static_cast<InverseDisplacementFieldTransformType*
>( transformItk_LPS.GetPointer() );
941 gridImageItk_Lps = inverseDisplacementFieldTransform->GetDisplacementField();
943 else if (transformItkClassName ==
"DisplacementFieldTransform")
945 DisplacementFieldTransformType* displacementFieldTransform =
static_cast<DisplacementFieldTransformType*
>( transformItk_LPS.GetPointer() );
947 gridImageItk_Lps = displacementFieldTransform->GetDisplacementField();
951 vtkDebugWithObjectMacro(loggerObject,
"Not a grid transform");
960 transformVtk_RAS->Inverse();
967 itk::Object::Pointer& transformItk_LPS, vtkOrientedGridTransform* transformVtk_RAS)
969 GridImageDoubleType::Pointer gridImageItk_Lps;
972 vtkErrorWithObjectMacro(loggerObject,
"vtkITKTransformConverter::SetITKOrientedGridTransformFromVTK failed: input transform is invalid");
976 transformVtk_RAS->Update();
977 if (transformVtk_RAS->GetInverseFlag())
980 gridTransformItk->SetDisplacementField(gridImageItk_Lps);
981 transformItk_LPS = gridTransformItk;
985 DisplacementFieldTransformDoubleType::Pointer gridTransformItk = DisplacementFieldTransformDoubleType::New();
986 gridTransformItk->SetDisplacementField(gridImageItk_Lps);
987 transformItk_LPS = gridTransformItk;
995 typename itk::DisplacementFieldTransform< T, 3 >::DisplacementFieldType::Pointer gridImage_Lps)
997 typedef itk::DisplacementFieldTransform< T, 3 > DisplacementFieldTransformType;
998 typedef typename DisplacementFieldTransformType::DisplacementFieldType GridImageType;
1000 vtkNew<vtkImageData> gridImage_Ras;
1003 gridImage_Ras->SetOrigin( -gridImage_Lps->GetOrigin()[0], -gridImage_Lps->GetOrigin()[1], gridImage_Lps->GetOrigin()[2] );
1006 gridImage_Ras->SetSpacing( gridImage_Lps->GetSpacing()[0], gridImage_Lps->GetSpacing()[1], gridImage_Lps->GetSpacing()[2] );
1009 vtkNew<vtkMatrix4x4> gridDirectionMatrix_LPS;
1012 for (
unsigned int column=0; column<
VTKDimension; column++)
1014 gridDirectionMatrix_LPS->SetElement(row,column,gridImage_Lps->GetDirection()(row,column));
1017 vtkNew<vtkMatrix4x4> lpsToRas;
1018 lpsToRas->SetElement(0,0,-1);
1019 lpsToRas->SetElement(1,1,-1);
1020 vtkNew<vtkMatrix4x4> gridDirectionMatrix_RAS;
1021 vtkMatrix4x4::Multiply4x4(lpsToRas.GetPointer(), gridDirectionMatrix_LPS.GetPointer(), gridDirectionMatrix_RAS.GetPointer());
1022 grid_Ras->SetGridDirectionMatrix(gridDirectionMatrix_RAS.GetPointer());
1025 typename GridImageType::SizeType size = gridImage_Lps->GetBufferedRegion().GetSize();
1026 gridImage_Ras->SetDimensions( size[0], size[1], size[2] );
1027 unsigned int numberOfScalarComponents = GridImageType::PixelType::Dimension;
1030 vtkErrorWithObjectMacro(loggerObject,
"Cannot load grid transform: the input displacement field expected to contain "
1031 <<
VTKDimension <<
" components but it actually contains " << numberOfScalarComponents );
1034 gridImage_Ras->AllocateScalars(VTK_DOUBLE, 3);
1036 double* displacementVectors_Ras =
reinterpret_cast<double*
>(gridImage_Ras->GetScalarPointer());
1037 itk::ImageRegionConstIterator<GridImageType> inputIt(gridImage_Lps, gridImage_Lps->GetRequestedRegion());
1038 inputIt.GoToBegin();
1039 while( !inputIt.IsAtEnd() )
1041 typename GridImageType::PixelType displacementVectorLps=inputIt.Get();
1042 *(displacementVectors_Ras++) = -displacementVectorLps[0];
1043 *(displacementVectors_Ras++) = -displacementVectorLps[1];
1044 *(displacementVectors_Ras++) = displacementVectorLps[2];
1048 grid_Ras->SetDisplacementGridData( gridImage_Ras.GetPointer() );
1051 grid_Ras->SetInterpolationModeToCubic();
1058 GridImageDoubleType::Pointer &gridImage_Lps, vtkOrientedGridTransform* grid_Ras)
1060 if (grid_Ras==
nullptr)
1062 vtkErrorWithObjectMacro(loggerObject,
"Cannot save grid transform: the input vtkOrientedGridTransform is invalid");
1069 vtkImageData* gridImage_Ras = grid_Ras->GetDisplacementGrid();
1070 if (gridImage_Ras==
nullptr)
1072 vtkErrorWithObjectMacro(loggerObject,
"Cannot save grid transform: the input vtkOrientedGridTransform does not contain a valid displacement grid");
1075 if (gridImage_Ras->GetNumberOfScalarComponents() !=
static_cast<int>(
VTKDimension))
1077 vtkErrorWithObjectMacro(loggerObject,
"Cannot save grid transform: the input vtkOrientedGridTransform expected to contain "
1078 <<
VTKDimension <<
" components but it actually contains " << gridImage_Ras->GetNumberOfScalarComponents() );
1082 gridImage_Lps = GridImageDoubleType::New();
1085 double* origin_Ras = gridImage_Ras->GetOrigin();
1086 double origin_Lps[3] = { -origin_Ras[0], -origin_Ras[1], origin_Ras[2] };
1087 gridImage_Lps->SetOrigin( origin_Lps );
1090 double* spacing = gridImage_Ras->GetSpacing();
1092 gridImage_Lps->SetSpacing( spacing );
1095 vtkNew<vtkMatrix4x4> gridDirectionMatrix_Ras;
1096 if (grid_Ras->GetGridDirectionMatrix()!=
nullptr)
1098 gridDirectionMatrix_Ras->DeepCopy(grid_Ras->GetGridDirectionMatrix());
1100 vtkNew<vtkMatrix4x4> rasToLps;
1101 rasToLps->SetElement(0,0,-1);
1102 rasToLps->SetElement(1,1,-1);
1103 vtkNew<vtkMatrix4x4> gridDirectionMatrix_Lps;
1104 vtkMatrix4x4::Multiply4x4(rasToLps.GetPointer(), gridDirectionMatrix_Ras.GetPointer(), gridDirectionMatrix_Lps.GetPointer());
1105 GridImageDoubleType::DirectionType gridDirectionMatrixItk_Lps;
1108 for (
unsigned int column=0; column<
VTKDimension; column++)
1110 gridDirectionMatrixItk_Lps(row,column) = gridDirectionMatrix_Lps->GetElement(row,column);
1113 gridImage_Lps->SetDirection(gridDirectionMatrixItk_Lps);
1116 GridImageDoubleType::IndexType start;
1117 start[0] = start[1] = start[2] = 0;
1118 int* Nijk = gridImage_Ras->GetDimensions();
1119 GridImageDoubleType::SizeType size;
1120 size[0] = Nijk[0]; size[1] = Nijk[1]; size[2] = Nijk[2];
1121 GridImageDoubleType::RegionType region;
1122 region.SetSize( size );
1123 region.SetIndex( start );
1124 gridImage_Lps->SetRegions( region );
1125 gridImage_Lps->Allocate();
1126 itk::ImageRegionIterator<GridImageDoubleType> gridImageIt_Lps(gridImage_Lps, region);
1127 gridImageIt_Lps.GoToBegin();
1128 GridImageDoubleType::PixelType displacementVectorLps;
1129 double displacementScale = grid_Ras->GetDisplacementScale();
1130 double displacementShift = grid_Ras->GetDisplacementShift();
1132 if (gridImage_Ras->GetScalarType()==VTK_DOUBLE)
1134 double* displacementVectors_Ras =
reinterpret_cast<double*
>(gridImage_Ras->GetScalarPointer());
1135 while( !gridImageIt_Lps.IsAtEnd() )
1137 displacementVectorLps[0] = -( displacementScale * (*(displacementVectors_Ras++)) + displacementShift );
1138 displacementVectorLps[1] = -( displacementScale * (*(displacementVectors_Ras++)) + displacementShift );
1139 displacementVectorLps[2] = ( displacementScale * (*(displacementVectors_Ras++)) + displacementShift );
1140 gridImageIt_Lps.Set(displacementVectorLps);
1144 else if (gridImage_Ras->GetScalarType()==VTK_FLOAT)
1146 float* displacementVectors_Ras =
reinterpret_cast<float*
>(gridImage_Ras->GetScalarPointer());
1147 while( !gridImageIt_Lps.IsAtEnd() )
1149 displacementVectorLps[0] = -( displacementScale * (*(displacementVectors_Ras++)) + displacementShift );
1150 displacementVectorLps[1] = -( displacementScale * (*(displacementVectors_Ras++)) + displacementShift );
1151 displacementVectorLps[2] = ( displacementScale * (*(displacementVectors_Ras++)) + displacementShift );
1152 gridImageIt_Lps.Set(displacementVectorLps);
1158 vtkErrorWithObjectMacro(loggerObject,
"Cannot save grid transform: only float and double scalar types are supported");
1167 vtkThinPlateSplineTransform* transformVtk_RAS,
typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS)
1169 typedef itk::ThinPlateSplineKernelTransform<T,3> ThinPlateSplineTransformType;
1172 if (transformVtk_RAS==
nullptr)
1174 vtkErrorWithObjectMacro(loggerObject,
"Cannot set VTK thin-plate spline transform from ITK: the output vtkThinPlateSplineTransform is invalid");
1178 if (!transformItk_LPS)
1180 vtkErrorWithObjectMacro(loggerObject,
"Cannot set VTK thin-plate spline transform from ITK: the input transform is nullptr");
1184 if (transformItk_LPS->GetOutputSpaceDimension() !=
VTKDimension)
1186 vtkErrorWithObjectMacro(loggerObject,
"Unsupported number of dimensions in thin-plate spline transform file (expected = "
1187 <<
VTKDimension <<
", actual = " << transformItk_LPS->GetOutputSpaceDimension() <<
")");
1191 std::string transformItkClassName = transformItk_LPS->GetNameOfClass();
1193 bool inverse =
false;
1194 typename ThinPlateSplineTransformType::PointSetType::Pointer sourceLandmarksItk_Lps;
1195 typename ThinPlateSplineTransformType::PointSetType::Pointer targetLandmarksItk_Lps;
1196 if (transformItkClassName ==
"InverseThinPlateSplineKernelTransform")
1198 ThinPlateSplineTransformType* inverseTpsTransform =
static_cast<InverseThinPlateSplineTransformType*
>( transformItk_LPS.GetPointer() );
1200 sourceLandmarksItk_Lps = inverseTpsTransform->GetSourceLandmarks();
1201 targetLandmarksItk_Lps = inverseTpsTransform->GetTargetLandmarks();
1203 else if (transformItkClassName ==
"ThinPlateSplineKernelTransform")
1205 ThinPlateSplineTransformType* tpsTransform =
static_cast<ThinPlateSplineTransformType*
>( transformItk_LPS.GetPointer() );
1207 sourceLandmarksItk_Lps = tpsTransform->GetSourceLandmarks();
1208 targetLandmarksItk_Lps = tpsTransform->GetTargetLandmarks();
1212 vtkDebugWithObjectMacro(loggerObject,
"Not a ThinPlateSpline transform");
1216 vtkNew<vtkPoints> sourceLandmarksVtk_Ras;
1217 unsigned int numberOfSourceLandmarks = sourceLandmarksItk_Lps->GetNumberOfPoints();
1218 for(
unsigned int i = 0; i < numberOfSourceLandmarks; i++)
1220 typename ThinPlateSplineTransformType::InputPointType pointItk_Lps;
1221 bool pointExists = sourceLandmarksItk_Lps->GetPoint(i, &pointItk_Lps);
1226 double pointVtk_Ras[3]={0};
1227 pointVtk_Ras[0] = -pointItk_Lps[0];
1228 pointVtk_Ras[1] = -pointItk_Lps[1];
1229 pointVtk_Ras[2] = pointItk_Lps[2];
1230 sourceLandmarksVtk_Ras->InsertNextPoint(pointVtk_Ras);
1233 vtkNew<vtkPoints> targetLandmarksVtk_Ras;
1234 unsigned int numberOfTargetLandmarks = targetLandmarksItk_Lps->GetNumberOfPoints();
1235 for(
unsigned int i = 0; i < numberOfTargetLandmarks; i++)
1237 typename ThinPlateSplineTransformType::InputPointType pointItk_Lps;
1238 bool pointExists = targetLandmarksItk_Lps->GetPoint(i, &pointItk_Lps);
1243 double pointVtk_Ras[3]={0};
1244 pointVtk_Ras[0] = -pointItk_Lps[0];
1245 pointVtk_Ras[1] = -pointItk_Lps[1];
1246 pointVtk_Ras[2] = pointItk_Lps[2];
1247 targetLandmarksVtk_Ras->InsertNextPoint(pointVtk_Ras);
1250 transformVtk_RAS->SetBasisToR();
1251 transformVtk_RAS->SetSourceLandmarks(sourceLandmarksVtk_Ras.GetPointer());
1252 transformVtk_RAS->SetTargetLandmarks(targetLandmarksVtk_Ras.GetPointer());
1256 transformVtk_RAS->Inverse();
1263 itk::Object::Pointer& transformItk_LPS, vtkThinPlateSplineTransform* transformVtk_RAS,
bool initialize )
1265 if (transformVtk_RAS==
nullptr)
1267 vtkErrorWithObjectMacro(loggerObject,
"Cannot set ITK thin-plate spline transform from VTK: the input vtkThinPlateSplineTransform is invalid");
1272 transformVtk_RAS->Update();
1274 if (transformVtk_RAS->GetBasis()!=VTK_RBF_R)
1276 vtkErrorWithObjectMacro(loggerObject,
"Cannot set ITK thin-plate spline transform from VTK: basis function must be R."
1277 " Call SetBasisToR() method of the vtkThinPlateSplineTransform object before attempting to write it to file.");
1281 ThinPlateSplineTransformDoubleType::PointSetType::Pointer sourceLandmarksItk_Lps = ThinPlateSplineTransformDoubleType::PointSetType::New();
1282 vtkPoints* sourceLandmarksVtk_Ras=transformVtk_RAS->GetSourceLandmarks();
1283 if (sourceLandmarksVtk_Ras!=
nullptr)
1285 for (
int i=0; i<sourceLandmarksVtk_Ras->GetNumberOfPoints(); i++)
1287 double posVtk_Ras[3]={0};
1288 sourceLandmarksVtk_Ras->GetPoint(i, posVtk_Ras);
1289 ThinPlateSplineTransformDoubleType::InputPointType posItk_Lps;
1290 posItk_Lps[0] = -posVtk_Ras[0];
1291 posItk_Lps[1] = -posVtk_Ras[1];
1292 posItk_Lps[2] = posVtk_Ras[2];
1293 sourceLandmarksItk_Lps->GetPoints()->InsertElement(i,posItk_Lps);
1296 ThinPlateSplineTransformDoubleType::PointSetType::Pointer targetLandmarksItk_Lps = ThinPlateSplineTransformDoubleType::PointSetType::New();
1297 vtkPoints* targetLandmarksVtk_Ras=transformVtk_RAS->GetTargetLandmarks();
1298 if (targetLandmarksVtk_Ras!=
nullptr)
1300 for (
int i=0; i<targetLandmarksVtk_Ras->GetNumberOfPoints(); i++)
1302 double posVtk_Ras[3]={0};
1303 targetLandmarksVtk_Ras->GetPoint(i, posVtk_Ras);
1304 ThinPlateSplineTransformDoubleType::InputPointType posItk_Lps;
1305 posItk_Lps[0] = -posVtk_Ras[0];
1306 posItk_Lps[1] = -posVtk_Ras[1];
1307 posItk_Lps[2] = posVtk_Ras[2];
1308 targetLandmarksItk_Lps->GetPoints()->InsertElement(i,posItk_Lps);
1312 if (transformVtk_RAS->GetInverseFlag())
1315 tpsTransformItk->SetSourceLandmarks(sourceLandmarksItk_Lps);
1316 tpsTransformItk->SetTargetLandmarks(targetLandmarksItk_Lps);
1319 tpsTransformItk->ComputeWMatrix();
1321 transformItk_LPS = tpsTransformItk;
1325 ThinPlateSplineTransformDoubleType::Pointer tpsTransformItk = ThinPlateSplineTransformDoubleType::New();
1326 tpsTransformItk->SetSourceLandmarks(sourceLandmarksItk_Lps);
1327 tpsTransformItk->SetTargetLandmarks(targetLandmarksItk_Lps);
1330 tpsTransformItk->ComputeWMatrix();
1332 transformItk_LPS = tpsTransformItk;
1341 vtkObject* loggerObject,
1342 typename itk::TransformBaseTemplate<T>::Pointer transformItk,
1343 double center_LocalRAS[3])
1345 bool conversionSuccess =
false;
1348 vtkNew<vtkMatrix4x4> transformMatrixVtk;
1350 if (conversionSuccess)
1352 vtkNew<vtkTransform> linearTransformVtk;
1353 linearTransformVtk->SetMatrix(transformMatrixVtk.GetPointer());
1354 linearTransformVtk->Register(
nullptr);
1355 return linearTransformVtk.GetPointer();
1358 vtkNew<vtkOrientedGridTransform> gridTransformVtk;
1360 if (conversionSuccess)
1362 gridTransformVtk->Register(
nullptr);
1363 return gridTransformVtk.GetPointer();
1366 vtkNew<vtkOrientedBSplineTransform> bsplineTransformVtk;
1368 if (conversionSuccess)
1370 bsplineTransformVtk->Register(
nullptr);
1371 return bsplineTransformVtk.GetPointer();
1374 vtkNew<vtkThinPlateSplineTransform> tpsTransformVtk;
1376 if (conversionSuccess)
1378 tpsTransformVtk->Register(
nullptr);
1379 return tpsTransformVtk.GetPointer();
1387 vtkAbstractTransform* transformVtk, itk::Object::Pointer& secondaryTransformItk,
int preferITKv3CompatibleTransforms,
bool initialize ,
1388 double center_LocalRAS[3] )
1390 typedef itk::CompositeTransform< double > CompositeTransformType;
1392 if (transformVtk==
nullptr)
1394 vtkErrorWithObjectMacro(loggerObject,
"CreateITKTransformFromVTK failed: invalid VTK transform");
1397 vtkNew<vtkCollection> transformList;
1399 if (transformList->GetNumberOfItems()==0)
1402 vtkNew<vtkTransform> identity;
1403 transformList->AddItem(identity.GetPointer());
1406 itk::Object::Pointer primaryTransformItk;
1407 if (transformList->GetNumberOfItems()==1)
1410 vtkObject* singleTransformVtk = transformList->GetItemAsObject(0);
1412 if (vtkHomogeneousTransform::SafeDownCast(singleTransformVtk))
1414 vtkHomogeneousTransform* linearTransformVtk = vtkHomogeneousTransform::SafeDownCast(singleTransformVtk);
1415 vtkMatrix4x4* transformMatrix = linearTransformVtk->GetMatrix();
1421 return primaryTransformItk;
1424 else if (vtkOrientedBSplineTransform::SafeDownCast(singleTransformVtk))
1426 vtkOrientedBSplineTransform* bsplineTransformVtk = vtkOrientedBSplineTransform::SafeDownCast(singleTransformVtk);
1427 vtkMatrix4x4* bulkMatrix = bsplineTransformVtk->GetBulkTransformMatrix();
1428 if (preferITKv3CompatibleTransforms || (bulkMatrix!=
nullptr && !
IsIdentityMatrix(bulkMatrix)))
1430 if (!
SetITKv3BSplineFromVTK(loggerObject, primaryTransformItk, secondaryTransformItk, bsplineTransformVtk, preferITKv3CompatibleTransforms))
1435 return primaryTransformItk;
1444 return primaryTransformItk;
1448 else if (vtkOrientedGridTransform::SafeDownCast(singleTransformVtk))
1450 vtkOrientedGridTransform* gridTransformVtk = vtkOrientedGridTransform::SafeDownCast(singleTransformVtk);
1456 return primaryTransformItk;
1459 else if (vtkThinPlateSplineTransform::SafeDownCast(singleTransformVtk))
1461 vtkThinPlateSplineTransform* tpsTransformVtk = vtkThinPlateSplineTransform::SafeDownCast(singleTransformVtk);
1467 return primaryTransformItk;
1471 if (singleTransformVtk==
nullptr)
1473 vtkErrorWithObjectMacro(loggerObject,
"vtkITKTransformConverter::CreateITKTransformFromVTK failed: invalid input transform");
1476 vtkErrorWithObjectMacro(loggerObject,
"vtkITKTransformConverter::CreateITKTransformFromVTK failed: conversion of transform type "
1477 << singleTransformVtk->GetClassName() <<
" is not supported");
1485 CompositeTransformType::Pointer compositeTransformItk = CompositeTransformType::New();
1486 primaryTransformItk = compositeTransformItk;
1488 for (
int transformIndex = transformList->GetNumberOfItems()-1; transformIndex>=0; --transformIndex)
1490 vtkAbstractTransform* singleTransformVtk = vtkAbstractTransform::SafeDownCast(transformList->GetItemAsObject(transformIndex));
1491 itk::Object::Pointer secondaryTransformItkTmp;
1495 itk::Object::Pointer singleTransformItk =
CreateITKTransformFromVTK(loggerObject, singleTransformVtk, secondaryTransformItkTmp,
false );
1496 if (secondaryTransformItkTmp.IsNotNull())
1498 vtkErrorWithObjectMacro(loggerObject,
"vtkITKTransformConverter::CreateITKTransformFromVTK failed:"
1499 " composite transforms cannot contain legacy transforms (that contains secondary transforms)."
1500 " Do not harden transforms on legacy ITK transforms to avoid this error.");
1504 if (singleTransformItk.IsNull()
1505 || std::string(singleTransformItk->GetNameOfClass()).find(
"Transform") == std::string::npos)
1507 vtkErrorWithObjectMacro(loggerObject,
"vtkITKTransformConverter::CreateITKTransformFromVTK failed:"
1508 " invalid element found while trying to create a composite transform");
1511 CompositeTransformType::TransformType::Pointer singleTransformItkTypeChecked =
1512 static_cast< CompositeTransformType::TransformType*
>( singleTransformItk.GetPointer() );
1513 compositeTransformItk->AddTransform(singleTransformItkTypeChecked.GetPointer());
1515 return primaryTransformItk;