/
vtkITKTransformConverter.h
1495 lines (1328 loc) · 61.1 KB
/
vtkITKTransformConverter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Visualization Toolkit
Module: $RCSfile: vtkITKTransformConverter.h,v $
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or https://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#ifndef __vtkITKTransformConverter_h
#define __vtkITKTransformConverter_h
#include "vtkMRMLTransformNode.h"
// VTK includes
#include <vtkImageData.h>
#include <vtkPoints.h>
#include <vtkThinPlateSplineTransform.h>
#include <vtkTransform.h>
// ITK includes
#include <itkAffineTransform.h>
#include <itkBSplineDeformableTransform.h> // ITKv3 style
#include <itkBSplineTransform.h> // ITKv4 style
#include <itkCompositeTransform.h>
#include <itkCompositeTransformIOHelper.h>
#include <itkDisplacementFieldTransform.h>
#include <itkIdentityTransform.h>
#include <itkTransformFileWriter.h>
#include <itkTransformFileReader.h>
#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>
#include <itkTranslationTransform.h>
#include <itkScaleTransform.h>
#include <itkTransformFactory.h>
#include <itkThinPlateSplineKernelTransform.h>
// Constants and typedefs
static const unsigned int VTKDimension = 3;
static const int BSPLINE_TRANSFORM_ORDER = 3;
typedef itk::TransformFileWriter TransformWriterType;
typedef itk::DisplacementFieldTransform< double, 3 > DisplacementFieldTransformDoubleType;
typedef DisplacementFieldTransformDoubleType::DisplacementFieldType GridImageDoubleType;
typedef itk::ThinPlateSplineKernelTransform<double,3> ThinPlateSplineTransformDoubleType;
#include "vtkITKTransformInverse.h"
class vtkITKTransformConverter
{
public:
static void RegisterInverseTransformTypes();
template<typename T>
static vtkAbstractTransform* CreateVTKTransformFromITK(vtkObject* loggerObject, typename itk::TransformBaseTemplate<T>::Pointer transformItk,
double center_RAS[3]=nullptr);
///
/// Create an ITK transform from a VTK transform.
/// secondaryTransformItk: Only for backward compatibility. For BSpline transforms that have an additive bulk component, the bulk component is saved
/// in the secondary transform.
/// preferITKv3CompatibleTransforms: If true then the BSpline transform will be created as a BSplineDeformableTransform and additive bulk transform component
/// is always written in the secondary transform. If false then the BSpline transform is written as a BSplineTransform (multiplicative bulk component
/// is saved in a composite transform).
/// If initialize is set to true then the transform is initialized to be readily usable.
/// Initialization takes a long time for kernel transforms with many points,
/// If a transform is created only to write it to file, initialization can be turned off to improve performance.
static itk::Object::Pointer CreateITKTransformFromVTK(vtkObject* loggerObject, vtkAbstractTransform* transformVtk,
itk::Object::Pointer& secondaryTransformItk, int preferITKv3CompatibleTransforms, bool initialize = true, double center_RAS[3] = nullptr);
template <typename T> static bool SetVTKBSplineFromITKv3Generic(vtkObject* loggerObject, vtkOrientedBSplineTransform* bsplineVtk,
typename itk::TransformBaseTemplate<T>::Pointer warpTransformItk, typename itk::TransformBaseTemplate<T>::Pointer bulkTransformItk);
template<typename T>
static bool SetVTKOrientedGridTransformFromITKImage(vtkObject* loggerObject, vtkOrientedGridTransform* grid_Ras,
typename itk::DisplacementFieldTransform< T, 3 >::DisplacementFieldType::Pointer gridImage_Lps);
static bool SetITKImageFromVTKOrientedGridTransform(vtkObject* loggerObject, GridImageDoubleType::Pointer &gridImage_Lps,
vtkOrientedGridTransform* grid_Ras);
protected:
template<typename T>
static bool SetVTKLinearTransformFromITK(vtkObject* loggerObject, vtkMatrix4x4* transformVtk_RAS,
typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS, double center_RAS[3]=nullptr);
static bool SetITKLinearTransformFromVTK(vtkObject* loggerObject, itk::Object::Pointer& transformItk_LPS, vtkMatrix4x4* transformVtk_RAS,
double center_RAS[3]=nullptr);
template<typename T>
static bool SetVTKOrientedGridTransformFromITK(vtkObject* loggerObject, vtkOrientedGridTransform* transformVtk_RAS,
typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS);
static bool SetITKOrientedGridTransformFromVTK(vtkObject* loggerObject, itk::Object::Pointer& transformItk_LPS, vtkOrientedGridTransform* transformVtk_RAS);
static bool SetITKv3BSplineFromVTK(vtkObject* loggerObject, itk::Object::Pointer& warpTransformItk, itk::Object::Pointer& bulkTransformItk,
vtkOrientedBSplineTransform* bsplineVtk, bool alwaysAddBulkTransform);
static bool SetITKv4BSplineFromVTK(vtkObject* loggerObject, itk::Object::Pointer& warpTransformItk, vtkOrientedBSplineTransform* bsplineVtk);
template<typename T>
static bool SetVTKThinPlateSplineTransformFromITK(vtkObject* loggerObject, vtkThinPlateSplineTransform* transformVtk_RAS,
typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS);
static bool SetITKThinPlateSplineTransformFromVTK(vtkObject* loggerObject, itk::Object::Pointer& transformItk_LPS,
vtkThinPlateSplineTransform* transformVtk_RAS, bool initialize = true);
static bool IsIdentityMatrix(vtkMatrix4x4 *matrix);
template <typename BSplineTransformType> static bool SetVTKBSplineParametersFromITKGeneric(vtkObject* loggerObject,
vtkOrientedBSplineTransform* bsplineVtk, typename itk::TransformBaseTemplate<typename BSplineTransformType::ScalarType>::Pointer warpTransformItk);
template <typename T> static bool SetVTKBSplineFromITKv4Generic(vtkObject* loggerObject,
vtkOrientedBSplineTransform* bsplineVtk, typename itk::TransformBaseTemplate<T>::Pointer warpTransformItk);
template <typename BSplineTransformType> static bool SetITKBSplineParametersFromVTKGeneric(vtkObject* loggerObject,
typename itk::Transform< typename BSplineTransformType::ScalarType, VTKDimension, VTKDimension>::Pointer& warpTransformItk,
vtkOrientedBSplineTransform* bsplineVtk);
template <typename T> static bool SetITKv3BSplineFromVTKGeneric(vtkObject* loggerObject,
typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& warpTransformItk,
typename itk::Transform<T, VTKDimension, VTKDimension>::Pointer& bulkTransformItk, vtkOrientedBSplineTransform* bsplineVtk,
bool alwaysAddBulkTransform);
template <typename T> static bool SetITKv4BSplineFromVTKGeneric(vtkObject* loggerObject,
typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& warpTransformItk, vtkOrientedBSplineTransform* bsplineVtk);
};
//----------------------------------------------------------------------------
void vtkITKTransformConverter::RegisterInverseTransformTypes()
{
itk::TransformFactory<InverseDisplacementFieldTransformFloatType>::RegisterTransform();
itk::TransformFactory<InverseDisplacementFieldTransformDoubleType>::RegisterTransform();
itk::TransformFactory<InverseBSplineTransformFloatITKv3Type>::RegisterTransform();
itk::TransformFactory<InverseBSplineTransformFloatITKv4Type>::RegisterTransform();
itk::TransformFactory<InverseBSplineTransformDoubleITKv3Type>::RegisterTransform();
itk::TransformFactory<InverseBSplineTransformDoubleITKv4Type>::RegisterTransform();
itk::TransformFactory<InverseThinPlateSplineTransformFloatType>::RegisterTransform();
itk::TransformFactory<InverseThinPlateSplineTransformDoubleType>::RegisterTransform();
typedef itk::ThinPlateSplineKernelTransform<float,3> ThinPlateSplineTransformFloatType;
typedef itk::ThinPlateSplineKernelTransform<double,3> ThinPlateSplineTransformDoubleType;
// by default they are not registered
itk::TransformFactory<ThinPlateSplineTransformFloatType>::RegisterTransform();
itk::TransformFactory<ThinPlateSplineTransformDoubleType>::RegisterTransform();
}
//----------------------------------------------------------------------------
template<typename T>
bool vtkITKTransformConverter::SetVTKLinearTransformFromITK(
vtkObject* /*loggerObject*/,
vtkMatrix4x4* transformVtk_RAS,
typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS,
double center_RAS[3] /*=nullptr*/)
{
static const unsigned int D = VTKDimension;
typedef itk::MatrixOffsetTransformBase<T,D,D> LinearTransformType;
typedef itk::ScaleTransform<T, D> ScaleTransformType;
typedef itk::TranslationTransform<T, D> TranslateTransformType;
vtkSmartPointer<vtkMatrix4x4> transformVtk_LPS = vtkSmartPointer<vtkMatrix4x4>::New();
bool convertedToVtkMatrix=false;
std::string itkTransformClassName = transformItk_LPS->GetNameOfClass();
// Linear transform of doubles or floats, dimension 3
// ITKIO transform libraries are build as shared and dynamic_cast
// can NOT be used with templated classes that are
// instantiated in a translation unit different than the one where they are
// defined. It will work only if the classes are explicitly instantiated
// and exported.
// To workaround the issue, instead of using dynamic_cast:
// (1) to ensure the objects are of the right type string comparison is done
// (2) static_cast is used instead of dynamic_cast.
// See InsightSoftwareConsortium/ITK@d1e9fe2
// and see https://stackoverflow.com/questions/8024010/why-do-template-class-functions-have-to-be-declared-in-the-same-translation-unit
//
// The disadvantage of this approach is that each supported class name has to be explicitly listed here and if the class hierarchy changes in ITK
// then the static cast may produce invalid results. Also, even if the transform class name is matching,
// we may cast the transform to a wrong type due to mismatch in dimensions (not 3) or data type (not double or float).
if (itkTransformClassName.find( "AffineTransform" ) != std::string::npos ||
itkTransformClassName == "MatrixOffsetTransformBase" ||
itkTransformClassName == "Rigid3DTransform" ||
itkTransformClassName == "Euler3DTransform" ||
itkTransformClassName == "CenteredEuler3DTransform" ||
itkTransformClassName == "QuaternionRigidTransform" ||
itkTransformClassName == "VersorTransform" ||
itkTransformClassName == "VersorRigid3DTransform" ||
itkTransformClassName == "ScaleSkewVersor3DTransform" ||
itkTransformClassName == "ScaleVersor3DTransform" ||
itkTransformClassName == "Similarity3DTransform" ||
itkTransformClassName == "ScaleTransform" ||
itkTransformClassName == "ScaleLogarithmicTransform")
{
typename LinearTransformType::Pointer dlt
= static_cast<LinearTransformType*>( transformItk_LPS.GetPointer() );
convertedToVtkMatrix=true;
for (unsigned int i=0; i < D; i++)
{
for (unsigned int j=0; j < D; j++)
{
transformVtk_LPS->SetElement(i, j, dlt->GetMatrix()[i][j]);
}
transformVtk_LPS->SetElement(i, D, dlt->GetOffset()[i]);
}
if (center_RAS)
{
auto center_LPS = dlt->GetCenter();
center_RAS[0] = -center_LPS[0];
center_RAS[1] = -center_LPS[1];
center_RAS[2] = center_LPS[2];
}
}
// Identity transform of doubles or floats, dimension 3
if (itkTransformClassName == "IdentityTransform")
{
// nothing to do, matrix is already the identity
convertedToVtkMatrix=true;
}
// Scale transform of doubles or floats, dimension 3
if (itkTransformClassName == "ScaleTransform")
{
typename ScaleTransformType::Pointer dst
= static_cast<ScaleTransformType*>( transformItk_LPS.GetPointer() );
convertedToVtkMatrix=true;
for (unsigned int i=0; i < D; i++)
{
transformVtk_LPS->SetElement(i, i, dst->GetScale()[i]);
}
}
// Translate transform of doubles or floats, dimension 3
if (itkTransformClassName == "TranslationTransform")
{
typename TranslateTransformType::Pointer dtt
= static_cast<TranslateTransformType*>( transformItk_LPS.GetPointer());
convertedToVtkMatrix=true;
for (unsigned int i=0; i < D; i++)
{
transformVtk_LPS->SetElement(i, D, dtt->GetOffset()[i]);
}
}
// Convert from LPS (ITK) to RAS (Slicer)
//
// Tras = lps2ras * Tlps * ras2lps
//
vtkSmartPointer<vtkMatrix4x4> lps2ras = vtkSmartPointer<vtkMatrix4x4>::New();
lps2ras->SetElement(0,0,-1);
lps2ras->SetElement(1,1,-1);
vtkMatrix4x4* ras2lps = lps2ras; // lps2ras is diagonal therefore the inverse is identical
vtkMatrix4x4::Multiply4x4(lps2ras, transformVtk_LPS, transformVtk_LPS);
vtkMatrix4x4::Multiply4x4(transformVtk_LPS, ras2lps, transformVtk_RAS);
return convertedToVtkMatrix;
}
//----------------------------------------------------------------------------
bool vtkITKTransformConverter::SetITKLinearTransformFromVTK(vtkObject* loggerObject, itk::Object::Pointer& transformItk_LPS, vtkMatrix4x4* transformVtk_RAS,
double center_RAS[3] /*=nullptr*/)
{
typedef itk::AffineTransform<double, VTKDimension> AffineTransformType;
if (transformVtk_RAS==nullptr)
{
vtkErrorWithObjectMacro(loggerObject,"vtkITKTransformConverter::SetITKLinearTransformFromVTK failed: invalid input transform");
return false;
}
vtkSmartPointer<vtkMatrix4x4> lps2ras = vtkSmartPointer<vtkMatrix4x4>::New();
lps2ras->SetElement(0,0,-1);
lps2ras->SetElement(1,1,-1);
vtkMatrix4x4* ras2lps = lps2ras; // lps2ras is diagonal therefore the inverse is identical
// Convert from RAS (Slicer) to LPS (ITK)
//
// Tlps = ras2lps * Tras * lps2ras
//
vtkSmartPointer<vtkMatrix4x4> vtkmat = vtkSmartPointer<vtkMatrix4x4>::New();
vtkMatrix4x4::Multiply4x4(ras2lps, transformVtk_RAS, vtkmat);
vtkMatrix4x4::Multiply4x4(vtkmat, lps2ras, vtkmat);
typedef AffineTransformType::MatrixType MatrixType;
typedef AffineTransformType::OutputVectorType OffsetType;
MatrixType itkmat;
OffsetType itkoffset;
for (unsigned int i=0; i < VTKDimension; i++)
{
for (unsigned int j=0; j < VTKDimension; j++)
{
itkmat[i][j] = vtkmat->GetElement(i, j);
}
itkoffset[i] = vtkmat->GetElement(i, VTKDimension);
}
AffineTransformType::Pointer affine = AffineTransformType::New();
affine->SetMatrix(itkmat);
affine->SetOffset(itkoffset);
if (center_RAS)
{
double center_LPS[3] = { -center_RAS[0], -center_RAS[1], center_RAS[2] };
affine->SetCenter(center_LPS);
}
transformItk_LPS = affine;
return true;
}
//----------------------------------------------------------------------------
bool vtkITKTransformConverter::IsIdentityMatrix(vtkMatrix4x4 *matrix)
{
static double identity[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
int i,j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if (matrix->GetElement(i,j) != identity[4*i+j])
{
return false;
}
}
}
return true;
}
//----------------------------------------------------------------------------
template <typename BSplineTransformType>
bool vtkITKTransformConverter::SetVTKBSplineParametersFromITKGeneric(
vtkObject* loggerObject,
vtkOrientedBSplineTransform* bsplineVtk,
typename itk::TransformBaseTemplate<typename BSplineTransformType::ScalarType>::Pointer warpTransformItk)
{
//
// this version uses the itk::BSplineTransform not the itk::BSplineDeformableTransform
//
typedef typename BSplineTransformType::ScalarType T;
if (bsplineVtk==nullptr)
{
vtkErrorWithObjectMacro(loggerObject, "vtkMRMLTransformStorageNode::SetVTKBSplineFromITKv4 failed: bsplineVtk is invalid");
return false;
}
bool isDoublePrecisionInput=true;
if (sizeof(T)==sizeof(float))
{
isDoublePrecisionInput=false;
}
else if (sizeof(T)==sizeof(double))
{
isDoublePrecisionInput=true;
}
else
{
vtkErrorWithObjectMacro(loggerObject,"Unsupported scalar type in BSpline transform file (only float and double are supported)");
return false;
}
typename BSplineTransformType::Pointer bsplineItk = BSplineTransformType::New();
std::string warpTransformItkName = warpTransformItk->GetNameOfClass();
std::string requestedWarpTransformItkName = bsplineItk->GetNameOfClass();
if (warpTransformItkName != requestedWarpTransformItkName)
{
return false;
}
if (warpTransformItk->GetOutputSpaceDimension() != VTKDimension)
{
vtkErrorWithObjectMacro(loggerObject, "Unsupported number of dimensions in BSpline transform file (expected = "
<< VTKDimension << ", actual = " << warpTransformItk->GetOutputSpaceDimension() << ")");
return false;
}
bsplineItk = static_cast< BSplineTransformType* >( warpTransformItk.GetPointer() );
// now get the fixed parameters and map them to the vtk analogs
// it turns out that for a BSplineTransform, the TransformDomain information
// is not populated when reading from a file, so instead we access these
// fields from one of the coefficient images (they are assumed to be
// identical)
const typename BSplineTransformType::CoefficientImageArray coefficientImages =
bsplineItk->GetCoefficientImages();
// * mesh size X, Y, Z (including the BSPLINE_TRANSFORM_ORDER=3 boundary nodes,
// 1 before and 2 after the grid)
typename BSplineTransformType::MeshSizeType meshSize =
coefficientImages[0]->GetLargestPossibleRegion().GetSize();
// * mesh origin X, Y, Z (position of the boundary node before the grid)
typename BSplineTransformType::OriginType origin =
coefficientImages[0]->GetOrigin();
// * mesh spacing X, Y, Z
typename BSplineTransformType::SpacingType spacing =
coefficientImages[0]->GetSpacing();
// * mesh direction 3x3 matrix (first row, second row, third row)
typename BSplineTransformType::DirectionType direction =
coefficientImages[0]->GetDirection();
vtkNew<vtkMatrix4x4> gridDirectionMatrix_LPS;
for (unsigned int row=0; row<VTKDimension; row++)
{
for (unsigned int column=0; column<VTKDimension; column++)
{
gridDirectionMatrix_LPS->SetElement(row,column,direction[row][column]);
}
}
// Set bspline grid and coefficients
bsplineVtk->SetBorderModeToZero();
vtkNew<vtkImageData> bsplineCoefficients;
bsplineCoefficients->SetExtent(0, meshSize[0]-1, 0, meshSize[1]-1, 0, meshSize[2]-1);
bsplineCoefficients->SetSpacing(spacing[0], spacing[1], spacing[2]);
// convert the direction matrix from LPS (itk) to RAS (slicer)
vtkNew<vtkMatrix4x4> lpsToRas;
lpsToRas->SetElement(0,0,-1);
lpsToRas->SetElement(1,1,-1);
vtkNew<vtkMatrix4x4> rasToLps;
rasToLps->SetElement(0,0,-1);
rasToLps->SetElement(1,1,-1);
vtkNew<vtkMatrix4x4> gridDirectionMatrix_RAS;
vtkMatrix4x4::Multiply4x4(
lpsToRas.GetPointer(),
gridDirectionMatrix_LPS.GetPointer(),
gridDirectionMatrix_RAS.GetPointer());
bsplineVtk->SetGridDirectionMatrix(gridDirectionMatrix_RAS.GetPointer());
// convert the origin from LPS (itk) to RAS (slicer)
double gridOrigin_RAS[3]={-origin[0], -origin[1], origin[2]};
bsplineCoefficients->SetOrigin(gridOrigin_RAS);
int bsplineCoefficientsScalarType=VTK_FLOAT;
if (isDoublePrecisionInput)
{
bsplineCoefficientsScalarType=VTK_DOUBLE;
}
bsplineCoefficients->AllocateScalars(bsplineCoefficientsScalarType, 3);
const unsigned int expectedNumberOfVectors = meshSize[0]*meshSize[1]*meshSize[2];
const unsigned int expectedNumberOfParameters = expectedNumberOfVectors*VTKDimension;
const unsigned int actualNumberOfParameters = bsplineItk->GetNumberOfParameters();
if( actualNumberOfParameters != expectedNumberOfParameters )
{
vtkErrorWithObjectMacro(loggerObject,"Mismatch in number of BSpline parameters in the transform file and the MRML node");
return false;
}
const T* itkBSplineParams_LPS = static_cast<const T*>(bsplineItk->GetParameters().data_block());
T* vtkBSplineParams_RAS=static_cast<T*>(bsplineCoefficients->GetScalarPointer());
for (unsigned int i=0; i<expectedNumberOfVectors; i++)
{
*(vtkBSplineParams_RAS++) = - (*(itkBSplineParams_LPS ));
*(vtkBSplineParams_RAS++) = - (*(itkBSplineParams_LPS+expectedNumberOfVectors ));
*(vtkBSplineParams_RAS++) = (*(itkBSplineParams_LPS+expectedNumberOfVectors*2));
itkBSplineParams_LPS++;
}
bsplineVtk->SetCoefficientData(bsplineCoefficients.GetPointer());
// Success
return true;
}
//----------------------------------------------------------------------------
template <typename T> bool vtkITKTransformConverter::SetVTKBSplineFromITKv3Generic(vtkObject* loggerObject,
vtkOrientedBSplineTransform* bsplineVtk,
typename itk::TransformBaseTemplate<T>::Pointer warpTransformItk, typename itk::TransformBaseTemplate<T>::Pointer bulkTransformItk)
{
if (bsplineVtk==nullptr)
{
vtkErrorWithObjectMacro(loggerObject, "vtkMRMLTransformStorageNode::SetVTKBSplineFromITK failed: bsplineVtk is invalid");
return false;
}
bool inverse = false;
// inverse class is derived from forward class, so it has to be checked first
if (SetVTKBSplineParametersFromITKGeneric< itk::InverseBSplineDeformableTransform< T, VTKDimension, VTKDimension > >(loggerObject, bsplineVtk, warpTransformItk))
{
inverse = true;
}
else if (SetVTKBSplineParametersFromITKGeneric< itk::BSplineDeformableTransform< T, VTKDimension, VTKDimension > >(loggerObject, bsplineVtk, warpTransformItk))
{
inverse = false;
}
else
{
vtkDebugWithObjectMacro(loggerObject, "Not an ITKv3 BSpline transform");
return false;
}
// Set the bulk transform
if( bulkTransformItk )
{
std::string bulkTransformItkTransformName = bulkTransformItk->GetNameOfClass();
typedef itk::AffineTransform<T,3> BulkTransformType;
if (bulkTransformItkTransformName == "AffineTransform")
{
BulkTransformType* bulkItkAffine = static_cast<BulkTransformType*> (bulkTransformItk.GetPointer());
vtkNew<vtkMatrix4x4> bulkMatrix_LPS;
for (unsigned int i=0; i < VTKDimension; i++)
{
for (unsigned int j=0; j < VTKDimension; j++)
{
bulkMatrix_LPS->SetElement(i,j, bulkItkAffine->GetMatrix()[i][j]);
}
bulkMatrix_LPS->SetElement(i,VTKDimension, bulkItkAffine->GetOffset()[i]);
}
vtkNew<vtkMatrix4x4> lpsToRas;
lpsToRas->SetElement(0,0,-1);
lpsToRas->SetElement(1,1,-1);
vtkNew<vtkMatrix4x4> rasToLps;
rasToLps->SetElement(0,0,-1);
rasToLps->SetElement(1,1,-1);
vtkNew<vtkMatrix4x4> bulkMatrix_RAS; // bulk_RAS = lpsToRas * bulk_LPS * rasToLps
vtkMatrix4x4::Multiply4x4(lpsToRas.GetPointer(), bulkMatrix_LPS.GetPointer(), bulkMatrix_RAS.GetPointer());
vtkMatrix4x4::Multiply4x4(bulkMatrix_RAS.GetPointer(), rasToLps.GetPointer(), bulkMatrix_RAS.GetPointer());
bsplineVtk->SetBulkTransformMatrix(bulkMatrix_RAS.GetPointer());
}
else if (bulkTransformItkTransformName == "IdentityTransform")
{
// bulk transform is identity, which is equivalent to no bulk transform
}
else
{
vtkErrorWithObjectMacro(loggerObject,"Cannot read the 2nd transform in BSplineTransform (expected AffineTransform_double_3_3 or IdentityTransform)" );
return false;
}
}
if (inverse)
{
bsplineVtk->Inverse();
}
// Success
return true;
}
//----------------------------------------------------------------------------
template <typename T> bool vtkITKTransformConverter::SetVTKBSplineFromITKv4Generic(vtkObject* loggerObject,
vtkOrientedBSplineTransform* bsplineVtk, typename itk::TransformBaseTemplate<T>::Pointer warpTransformItk)
{
bool inverse = false;
// inverse class is derived from forward class, so it has to be checked first
if (SetVTKBSplineParametersFromITKGeneric< itk::InverseBSplineTransform< T, VTKDimension, VTKDimension > >(loggerObject, bsplineVtk, warpTransformItk))
{
inverse = true;
}
else if (SetVTKBSplineParametersFromITKGeneric< itk::BSplineTransform< T, VTKDimension, VTKDimension > >(loggerObject, bsplineVtk, warpTransformItk))
{
inverse = false;
}
else
{
vtkDebugWithObjectMacro(loggerObject, "Not an ITKv4 BSpline transform");
return false;
}
if (inverse)
{
bsplineVtk->Inverse();
}
return true;
}
//----------------------------------------------------------------------------
template <typename BSplineTransformType> bool vtkITKTransformConverter::SetITKBSplineParametersFromVTKGeneric(vtkObject* loggerObject,
typename itk::Transform< typename BSplineTransformType::ScalarType,VTKDimension,VTKDimension>::Pointer& warpTransformItk,
vtkOrientedBSplineTransform* bsplineVtk)
{
typedef typename BSplineTransformType::ScalarType T;
if (bsplineVtk==nullptr)
{
vtkErrorWithObjectMacro(loggerObject, "vtkMRMLTransformStorageNode::SetITKBSplineFromVTK failed: bsplineVtk is invalid");
return false;
}
vtkImageData* bsplineCoefficients_RAS=bsplineVtk->GetCoefficientData();
if (bsplineCoefficients_RAS==nullptr)
{
vtkErrorWithObjectMacro(loggerObject, "Cannot write BSpline transform to file: coefficients are not specified");
return false;
}
typename BSplineTransformType::Pointer bsplineItk = BSplineTransformType::New();
warpTransformItk = bsplineItk;
// Fixed parameters:
// * mesh size X, Y, Z (including the BSPLINE_TRANSFORM_ORDER=3 boundary nodes, 1 before and 2 after the grid)
// * mesh origin X, Y, Z (position of the boundary node before the grid)
// * mesh spacing X, Y, Z
// * mesh direction 3x3 matrix (first row, second row, third row)
typename BSplineTransformType::FixedParametersType transformFixedParamsItk;
const unsigned int numberOfFixedParameters=VTKDimension*(VTKDimension+3);
transformFixedParamsItk.SetSize(numberOfFixedParameters);
int *gridExtent=bsplineCoefficients_RAS->GetExtent();
int gridSize[3]={gridExtent[1]-gridExtent[0]+1, gridExtent[3]-gridExtent[2]+1, gridExtent[5]-gridExtent[4]+1};
transformFixedParamsItk[0]=gridSize[0];
transformFixedParamsItk[1]=gridSize[1];
transformFixedParamsItk[2]=gridSize[2];
double* gridOrigin_RAS=bsplineCoefficients_RAS->GetOrigin();
double gridOrigin_LPS[3]={-gridOrigin_RAS[0], -gridOrigin_RAS[1], gridOrigin_RAS[2]};
transformFixedParamsItk[3]=gridOrigin_LPS[0];
transformFixedParamsItk[4]=gridOrigin_LPS[1];
transformFixedParamsItk[5]=gridOrigin_LPS[2];
double* gridSpacing=bsplineCoefficients_RAS->GetSpacing();
transformFixedParamsItk[6]=gridSpacing[0];
transformFixedParamsItk[7]=gridSpacing[1];
transformFixedParamsItk[8]=gridSpacing[2];
vtkNew<vtkMatrix4x4> gridDirectionMatrix_RAS;
if (bsplineVtk->GetGridDirectionMatrix()!=nullptr)
{
gridDirectionMatrix_RAS->DeepCopy(bsplineVtk->GetGridDirectionMatrix());
}
vtkNew<vtkMatrix4x4> lpsToRas;
lpsToRas->SetElement(0,0,-1);
lpsToRas->SetElement(1,1,-1);
vtkNew<vtkMatrix4x4> rasToLps;
rasToLps->SetElement(0,0,-1);
rasToLps->SetElement(1,1,-1);
vtkNew<vtkMatrix4x4> gridDirectionMatrix_LPS;
vtkMatrix4x4::Multiply4x4(rasToLps.GetPointer(), gridDirectionMatrix_RAS.GetPointer(), gridDirectionMatrix_LPS.GetPointer());
int fpIndex=9;
for (unsigned int row=0; row<VTKDimension; row++)
{
for (unsigned int column=0; column<VTKDimension; column++)
{
transformFixedParamsItk[fpIndex++]=gridDirectionMatrix_LPS->GetElement(row,column);
}
}
bsplineItk->SetFixedParameters(transformFixedParamsItk);
// BSpline coefficients
const unsigned int expectedNumberOfVectors = gridSize[0]*gridSize[1]*gridSize[2];
const unsigned int expectedNumberOfParameters = expectedNumberOfVectors*VTKDimension;
if( bsplineItk->GetNumberOfParameters() != expectedNumberOfParameters )
{
vtkErrorWithObjectMacro(loggerObject,"Mismatch in number of BSpline parameters in the ITK transform and the VTK transform");
return false;
}
typename BSplineTransformType::ParametersType transformParamsItk(expectedNumberOfParameters);
T* itkBSplineParams_LPS = static_cast<T*>(transformParamsItk.data_block());
T* vtkBSplineParams_RAS=static_cast<T*>(bsplineCoefficients_RAS->GetScalarPointer());
double coefficientScale = bsplineVtk->GetDisplacementScale();
for (unsigned int i=0; i<expectedNumberOfVectors; i++)
{
*(itkBSplineParams_LPS ) = -coefficientScale * (*(vtkBSplineParams_RAS++));
*(itkBSplineParams_LPS+expectedNumberOfVectors ) = -coefficientScale * (*(vtkBSplineParams_RAS++));
*(itkBSplineParams_LPS+expectedNumberOfVectors*2) = coefficientScale * (*(vtkBSplineParams_RAS++));
itkBSplineParams_LPS++;
}
bsplineItk->SetParameters(transformParamsItk);
return true;
}
//----------------------------------------------------------------------------
template <typename T> bool vtkITKTransformConverter::SetITKv3BSplineFromVTKGeneric(vtkObject* loggerObject,
typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& warpTransformItk,
typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& bulkTransformItk,
vtkOrientedBSplineTransform* bsplineVtk, bool alwaysAddBulkTransform)
{
if (bsplineVtk==nullptr)
{
vtkErrorWithObjectMacro(loggerObject, "vtkMRMLTransformStorageNode::SetITKBSplineFromVTK failed: bsplineVtk is invalid");
return false;
}
// Update is needed because it refreshes the inverse flag (the flag may be out-of-date if the transform depends on its inverse)
bsplineVtk->Update();
bool itkTransformSetSuccessfully = false;
if (bsplineVtk->GetInverseFlag())
{
itkTransformSetSuccessfully = SetITKBSplineParametersFromVTKGeneric<
itk::InverseBSplineDeformableTransform< T, VTKDimension, VTKDimension > >(loggerObject, warpTransformItk, bsplineVtk);
}
else
{
itkTransformSetSuccessfully = SetITKBSplineParametersFromVTKGeneric<
itk::BSplineDeformableTransform< T, VTKDimension, VTKDimension > >(loggerObject, warpTransformItk, bsplineVtk);
}
if (!itkTransformSetSuccessfully)
{
vtkErrorWithObjectMacro(loggerObject, "vtkMRMLTransformStorageNode::SetITKBSplineFromVTK failed: cannot determine BSpline parameters");
return false;
}
vtkMatrix4x4* bulkMatrix_RAS=bsplineVtk->GetBulkTransformMatrix();
if (bulkMatrix_RAS || alwaysAddBulkTransform)
{
vtkNew<vtkMatrix4x4> lpsToRas;
lpsToRas->SetElement(0,0,-1);
lpsToRas->SetElement(1,1,-1);
vtkNew<vtkMatrix4x4> rasToLps;
rasToLps->SetElement(0,0,-1);
rasToLps->SetElement(1,1,-1);
vtkNew<vtkMatrix4x4> bulkMatrix_LPS; // bulk_LPS = rasToLps * bulk_RAS * lpsToRas
// If bulk transform is available then use it, otherwise just write an identity matrix (we just write it because
// alwaysAddBulkTransform was requested, due to backward compatibility reasons)
if (bulkMatrix_RAS!=nullptr)
{
vtkMatrix4x4::Multiply4x4(rasToLps.GetPointer(), bulkMatrix_RAS, bulkMatrix_LPS.GetPointer());
vtkMatrix4x4::Multiply4x4(bulkMatrix_LPS.GetPointer(), lpsToRas.GetPointer(), bulkMatrix_LPS.GetPointer());
}
typedef itk::AffineTransform<T,VTKDimension> BulkTransformType;
typename BulkTransformType::Pointer affineItk = BulkTransformType::New();
bulkTransformItk = affineItk;
typename BulkTransformType::MatrixType affineMatrix;
typename BulkTransformType::OffsetType affineOffset;
for (unsigned int i=0; i < VTKDimension; i++)
{
for (unsigned int j=0; j < VTKDimension; j++)
{
affineMatrix[i][j]=bulkMatrix_LPS->GetElement(i,j);
}
affineOffset[i]=bulkMatrix_LPS->GetElement(i,VTKDimension);
}
affineItk->SetMatrix(affineMatrix);
affineItk->SetOffset(affineOffset);
}
else
{
bulkTransformItk=nullptr;
}
return true;
}
//----------------------------------------------------------------------------
template <typename T> bool vtkITKTransformConverter::SetITKv4BSplineFromVTKGeneric(vtkObject* loggerObject,
typename itk::Transform<T,VTKDimension,VTKDimension>::Pointer& warpTransformItk,
vtkOrientedBSplineTransform* bsplineVtk)
{
// Update is needed because it refreshes the inverse flag (the flag may be out-of-date if the transform depends on its inverse)
bsplineVtk->Update();
bool itkTransformSetSuccessfully = false;
if (bsplineVtk->GetInverseFlag())
{
itkTransformSetSuccessfully = SetITKBSplineParametersFromVTKGeneric<
itk::InverseBSplineTransform< T, VTKDimension, VTKDimension > >(loggerObject, warpTransformItk, bsplineVtk);
}
else
{
itkTransformSetSuccessfully = SetITKBSplineParametersFromVTKGeneric<
itk::BSplineTransform< T, VTKDimension, VTKDimension > >(loggerObject, warpTransformItk, bsplineVtk);
}
if (!itkTransformSetSuccessfully)
{
vtkErrorWithObjectMacro(loggerObject, "vtkMRMLTransformStorageNode::SetITKv4BSplineFromVTKGeneric failed: cannot determine BSpline parameters");
return false;
}
return true;
}
//----------------------------------------------------------------------------
bool vtkITKTransformConverter::SetITKv3BSplineFromVTK(vtkObject* loggerObject, itk::Object::Pointer& warpTransformItk,
itk::Object::Pointer& bulkTransformItk, vtkOrientedBSplineTransform* bsplineVtk, bool alwaysAddBulkTransform)
{
if (bsplineVtk==nullptr)
{
vtkErrorWithObjectMacro(loggerObject, "Cannot retrieve BSpline transform from node");
return false;
}
vtkImageData* bsplineCoefficients=bsplineVtk->GetCoefficientData();
if (bsplineCoefficients==nullptr)
{
vtkErrorWithObjectMacro(loggerObject, "Cannot write BSpline transform to file: coefficients are not specified");
return false;
}
if (bsplineCoefficients->GetScalarType()==VTK_FLOAT)
{
typedef itk::Transform<float, VTKDimension, VTKDimension > ITKTransformType;
ITKTransformType::Pointer floatWarpTransformItk;
ITKTransformType::Pointer floatBulkTransformItk;
if (!SetITKv3BSplineFromVTKGeneric<float>(loggerObject, floatWarpTransformItk, floatBulkTransformItk, bsplineVtk, alwaysAddBulkTransform))
{
vtkErrorWithObjectMacro(loggerObject, "Cannot write BSpline transform to file");
return false;
}
warpTransformItk = floatWarpTransformItk.GetPointer();
bulkTransformItk = floatBulkTransformItk.GetPointer();
}
else if (bsplineCoefficients->GetScalarType()==VTK_DOUBLE)
{
typedef itk::Transform<double, VTKDimension, VTKDimension > ITKTransformType;
ITKTransformType::Pointer doubleWarpTransformItk;
ITKTransformType::Pointer doubleBulkTransformItk;
if (!SetITKv3BSplineFromVTKGeneric<double>(loggerObject, doubleWarpTransformItk, doubleBulkTransformItk, bsplineVtk, alwaysAddBulkTransform))
{
vtkErrorWithObjectMacro(loggerObject, "Cannot write BSpline transform to file");
return false;
}
warpTransformItk = doubleWarpTransformItk;
bulkTransformItk = doubleBulkTransformItk;
}
else
{
vtkErrorWithObjectMacro(loggerObject, "Cannot write BSpline transform to file: only float and double coefficient types are supported");
return false;
}
return true;
}
//----------------------------------------------------------------------------
bool vtkITKTransformConverter::SetITKv4BSplineFromVTK(vtkObject* loggerObject,
itk::Object::Pointer& warpTransformItk, vtkOrientedBSplineTransform* bsplineVtk)
{
if (bsplineVtk==nullptr)
{
vtkErrorWithObjectMacro(loggerObject, "Cannot retrieve BSpline transform from node");
return false;
}
vtkImageData* bsplineCoefficients=bsplineVtk->GetCoefficientData();
if (bsplineCoefficients==nullptr)
{
vtkErrorWithObjectMacro(loggerObject, "Cannot write BSpline transform to file: coefficients are not specified");
return false;
}
if (bsplineCoefficients->GetScalarType()==VTK_FLOAT)
{
typedef itk::Transform<float, VTKDimension, VTKDimension > ITKTransformType;
ITKTransformType::Pointer floatWarpTransformItk;
if (!SetITKv4BSplineFromVTKGeneric<float>(loggerObject, floatWarpTransformItk, bsplineVtk))
{
vtkErrorWithObjectMacro(loggerObject, "Cannot write BSpline transform to file");
return false;
}
warpTransformItk = floatWarpTransformItk.GetPointer();
}
else if (bsplineCoefficients->GetScalarType()==VTK_DOUBLE)
{
typedef itk::Transform<double, VTKDimension, VTKDimension > ITKTransformType;
ITKTransformType::Pointer doubleWarpTransformItk;
if (!SetITKv4BSplineFromVTKGeneric<double>(loggerObject, doubleWarpTransformItk, bsplineVtk))
{
vtkErrorWithObjectMacro(loggerObject, "Cannot write BSpline transform to file");
return false;
}
warpTransformItk = doubleWarpTransformItk;
}
else
{
vtkErrorWithObjectMacro(loggerObject, "Cannot write BSpline transform to file: only float and double coefficient types are supported");
return false;
}
return true;
}
//----------------------------------------------------------------------------
template<typename T>
bool vtkITKTransformConverter::SetVTKOrientedGridTransformFromITK(vtkObject* loggerObject,
vtkOrientedGridTransform* transformVtk_RAS, typename itk::TransformBaseTemplate<T>::Pointer transformItk_LPS)
{
typedef itk::DisplacementFieldTransform< T, 3 > DisplacementFieldTransformType;
typedef itk::InverseDisplacementFieldTransform< T, 3 > InverseDisplacementFieldTransformType;
if (!transformItk_LPS)
{
vtkErrorWithObjectMacro(loggerObject, "Cannot set VTK oriented grid transform from ITK: the input transform is nullptr");
return false;
}
if (transformItk_LPS->GetOutputSpaceDimension() != VTKDimension)
{
vtkErrorWithObjectMacro(loggerObject, "Unsupported number of dimensions in oriented grid transform file (expected = "
<< VTKDimension << ", actual = " << transformItk_LPS->GetOutputSpaceDimension() << ")");
return false;
}
std::string transformItkClassName = transformItk_LPS->GetNameOfClass();
bool inverse = false;
typename DisplacementFieldTransformType::DisplacementFieldType* gridImageItk_Lps = nullptr;
if (transformItkClassName == "InverseDisplacementFieldTransform") // inverse class is derived from forward class, so it has to be checked first
{
DisplacementFieldTransformType* inverseDisplacementFieldTransform = static_cast<InverseDisplacementFieldTransformType*>( transformItk_LPS.GetPointer() );
inverse = true;
gridImageItk_Lps = inverseDisplacementFieldTransform->GetDisplacementField();
}
else if (transformItkClassName == "DisplacementFieldTransform")
{
DisplacementFieldTransformType* displacementFieldTransform = static_cast<DisplacementFieldTransformType*>( transformItk_LPS.GetPointer() );
inverse = false;
gridImageItk_Lps = displacementFieldTransform->GetDisplacementField();
}
else
{
vtkDebugWithObjectMacro(loggerObject, "Not a grid transform");
return false;
}
if (!SetVTKOrientedGridTransformFromITKImage<T>(loggerObject, transformVtk_RAS, gridImageItk_Lps))
{
return false;
}
if (inverse)
{
transformVtk_RAS->Inverse();
}
return true;
}
//----------------------------------------------------------------------------
bool vtkITKTransformConverter::SetITKOrientedGridTransformFromVTK(vtkObject* loggerObject,
itk::Object::Pointer& transformItk_LPS, vtkOrientedGridTransform* transformVtk_RAS)
{
GridImageDoubleType::Pointer gridImageItk_Lps;
if (!SetITKImageFromVTKOrientedGridTransform(loggerObject, gridImageItk_Lps, transformVtk_RAS))
{
vtkErrorWithObjectMacro(loggerObject, "vtkITKTransformConverter::SetITKOrientedGridTransformFromVTK failed: input transform is invalid");
return false;
}
// Update is needed because it refreshes the inverse flag (the flag may be out-of-date if the transform depends on its inverse)
transformVtk_RAS->Update();
if (transformVtk_RAS->GetInverseFlag())
{
InverseDisplacementFieldTransformDoubleType::Pointer gridTransformItk = InverseDisplacementFieldTransformDoubleType::New();
gridTransformItk->SetDisplacementField(gridImageItk_Lps);
transformItk_LPS = gridTransformItk;
}
else
{
DisplacementFieldTransformDoubleType::Pointer gridTransformItk = DisplacementFieldTransformDoubleType::New();
gridTransformItk->SetDisplacementField(gridImageItk_Lps);
transformItk_LPS = gridTransformItk;
}
return true;
}
//----------------------------------------------------------------------------
template<typename T>
bool vtkITKTransformConverter::SetVTKOrientedGridTransformFromITKImage(vtkObject* loggerObject, vtkOrientedGridTransform* grid_Ras,
typename itk::DisplacementFieldTransform< T, 3 >::DisplacementFieldType::Pointer gridImage_Lps)
{
typedef itk::DisplacementFieldTransform< T, 3 > DisplacementFieldTransformType;
typedef typename DisplacementFieldTransformType::DisplacementFieldType GridImageType;
vtkNew<vtkImageData> gridImage_Ras;
// Origin
gridImage_Ras->SetOrigin( -gridImage_Lps->GetOrigin()[0], -gridImage_Lps->GetOrigin()[1], gridImage_Lps->GetOrigin()[2] );
// Spacing
gridImage_Ras->SetSpacing( gridImage_Lps->GetSpacing()[0], gridImage_Lps->GetSpacing()[1], gridImage_Lps->GetSpacing()[2] );
// Direction
vtkNew<vtkMatrix4x4> gridDirectionMatrix_LPS;
for (unsigned int row=0; row<VTKDimension; row++)
{
for (unsigned int column=0; column<VTKDimension; column++)
{
gridDirectionMatrix_LPS->SetElement(row,column,gridImage_Lps->GetDirection()(row,column));
}
}
vtkNew<vtkMatrix4x4> lpsToRas;
lpsToRas->SetElement(0,0,-1);
lpsToRas->SetElement(1,1,-1);
vtkNew<vtkMatrix4x4> gridDirectionMatrix_RAS;
vtkMatrix4x4::Multiply4x4(lpsToRas.GetPointer(), gridDirectionMatrix_LPS.GetPointer(), gridDirectionMatrix_RAS.GetPointer());
grid_Ras->SetGridDirectionMatrix(gridDirectionMatrix_RAS.GetPointer());
// Vectors
typename GridImageType::SizeType size = gridImage_Lps->GetBufferedRegion().GetSize();