|
a |
|
b/src/reflectionMatrix.cxx |
|
|
1 |
|
|
|
2 |
#include <nanobind/nanobind.h> |
|
|
3 |
#include <nanobind/stl/vector.h> |
|
|
4 |
#include <nanobind/stl/string.h> |
|
|
5 |
#include <nanobind/stl/tuple.h> |
|
|
6 |
#include <nanobind/stl/list.h> |
|
|
7 |
#include <nanobind/ndarray.h> |
|
|
8 |
#include <nanobind/stl/shared_ptr.h> |
|
|
9 |
|
|
|
10 |
#include <exception> |
|
|
11 |
#include <vector> |
|
|
12 |
#include <string> |
|
|
13 |
|
|
|
14 |
#include "itkImage.h" |
|
|
15 |
#include "itkAffineTransform.h" |
|
|
16 |
#include "itkVectorImage.h" |
|
|
17 |
#include "itkImageRegionIteratorWithIndex.h" |
|
|
18 |
#include "itkImageMomentsCalculator.h" |
|
|
19 |
#include "itkTransformFileWriter.h" |
|
|
20 |
|
|
|
21 |
#include "antsImage.h" |
|
|
22 |
|
|
|
23 |
namespace nb = nanobind; |
|
|
24 |
using namespace nb::literals; |
|
|
25 |
|
|
|
26 |
template< class ImageType > |
|
|
27 |
int reflectionMatrixHelper( AntsImage<ImageType> & py_image, unsigned int axis, std::string filename ) |
|
|
28 |
{ |
|
|
29 |
typedef typename ImageType::Pointer ImagePointerType; |
|
|
30 |
typedef itk::AffineTransform<double, ImageType::ImageDimension> AffineTransformType; |
|
|
31 |
|
|
|
32 |
ImagePointerType image = py_image.ptr; |
|
|
33 |
|
|
|
34 |
typedef typename itk::ImageMomentsCalculator<ImageType> ImageCalculatorType; |
|
|
35 |
typename ImageCalculatorType::Pointer calculator = ImageCalculatorType::New(); |
|
|
36 |
calculator->SetImage( image ); |
|
|
37 |
|
|
|
38 |
typename ImageCalculatorType::VectorType fixed_center; |
|
|
39 |
fixed_center.Fill(0); |
|
|
40 |
|
|
|
41 |
calculator->Compute(); |
|
|
42 |
fixed_center = calculator->GetCenterOfGravity(); |
|
|
43 |
|
|
|
44 |
typename AffineTransformType::Pointer aff = AffineTransformType::New(); |
|
|
45 |
aff->SetIdentity(); |
|
|
46 |
|
|
|
47 |
typename AffineTransformType::ParametersType myoff = aff->GetFixedParameters(); |
|
|
48 |
for( unsigned int i = 0; i < ImageType::ImageDimension; i++ ) |
|
|
49 |
{ |
|
|
50 |
myoff[i] = fixed_center[i]; |
|
|
51 |
} |
|
|
52 |
typename AffineTransformType::MatrixType mymat = aff->GetMatrix(); |
|
|
53 |
if( axis < ImageType::ImageDimension ) |
|
|
54 |
{ |
|
|
55 |
mymat[axis][axis] = ( -1.0 ); |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
aff->SetFixedParameters( myoff ); |
|
|
59 |
aff->SetMatrix( mymat ); |
|
|
60 |
|
|
|
61 |
typedef itk::TransformFileWriter TransformWriterType; |
|
|
62 |
typename TransformWriterType::Pointer transformWriter = TransformWriterType::New(); |
|
|
63 |
transformWriter->SetInput( aff ); |
|
|
64 |
transformWriter->SetFileName( filename.c_str() ); |
|
|
65 |
transformWriter->Update(); |
|
|
66 |
|
|
|
67 |
return 1; |
|
|
68 |
} |
|
|
69 |
|
|
|
70 |
|
|
|
71 |
template <typename ImageType> |
|
|
72 |
int reflectionMatrixEntry( AntsImage<ImageType> & image, unsigned int axis, std::string filename) |
|
|
73 |
{ |
|
|
74 |
return reflectionMatrixHelper<ImageType>( image, axis, filename ); |
|
|
75 |
} |
|
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
void local_reflectionMatrix(nb::module_ &m) |
|
|
80 |
{ |
|
|
81 |
m.def("reflectionMatrix", &reflectionMatrixEntry<itk::Image<unsigned char, 2>>); |
|
|
82 |
m.def("reflectionMatrix", &reflectionMatrixEntry<itk::Image<unsigned char, 3>>); |
|
|
83 |
m.def("reflectionMatrix", &reflectionMatrixEntry<itk::Image<unsigned char, 4>>); |
|
|
84 |
m.def("reflectionMatrix", &reflectionMatrixEntry<itk::Image<unsigned int, 2>>); |
|
|
85 |
m.def("reflectionMatrix", &reflectionMatrixEntry<itk::Image<unsigned int, 3>>); |
|
|
86 |
m.def("reflectionMatrix", &reflectionMatrixEntry<itk::Image<unsigned int, 4>>); |
|
|
87 |
m.def("reflectionMatrix", &reflectionMatrixEntry<itk::Image<float, 2>>); |
|
|
88 |
m.def("reflectionMatrix", &reflectionMatrixEntry<itk::Image<float, 3>>); |
|
|
89 |
m.def("reflectionMatrix", &reflectionMatrixEntry<itk::Image<float, 4>>); |
|
|
90 |
m.def("reflectionMatrix", &reflectionMatrixEntry<itk::Image<double, 2>>); |
|
|
91 |
m.def("reflectionMatrix", &reflectionMatrixEntry<itk::Image<double, 3>>); |
|
|
92 |
m.def("reflectionMatrix", &reflectionMatrixEntry<itk::Image<double, 4>>); |
|
|
93 |
} |
|
|
94 |
|