]> Creatis software - creaRigidRegistration.git/blob - lib/MyGridOnImageGenerator.cpp
Class that generates a grid over an image in order to visualize the deformation field...
[creaRigidRegistration.git] / lib / MyGridOnImageGenerator.cpp
1 //----------------------------------------------------------------------------
2 // Class definition include
3 //----------------------------------------------------------------------------
4
5 #include "MyGridOnImageGenerator.h"
6
7 //----------------------------------------------------------------------------
8 // Class implementation
9 //----------------------------------------------------------------------------
10 //----------------------------------------------------------------------------
11 // Builder
12 //----------------------------------------------------------------------------
13
14 MyGridOnImageGenerator::MyGridOnImageGenerator( vtkImageData* nImage, double nScpX, double nScpY, double nScpZ)
15 {
16         spcX = nScpX;
17         spcY = nScpY;
18         spcZ = nScpZ;
19         image = nImage;
20 }
21
22 //----------------------------------------------------------------------------
23 // Destructor
24 //----------------------------------------------------------------------------
25
26 MyGridOnImageGenerator::~MyGridOnImageGenerator( )
27 {
28
29 }
30 //----------------------------------------------------------------------------
31 // Methods
32 //----------------------------------------------------------------------------
33
34
35 void MyGridOnImageGenerator::setScpX(double nScpX)
36 {
37         spcX = nScpX;
38 }
39
40 void MyGridOnImageGenerator::setScpY(double nScpY)
41 {
42         spcY = nScpY;
43 }
44
45 void MyGridOnImageGenerator::setScpZ(double nScpZ)
46 {
47         spcZ = nScpZ;
48 }
49
50
51 void MyGridOnImageGenerator::setImage(vtkImageData* nImage)
52 {
53         image = nImage;
54 }
55
56 vtkImageData* MyGridOnImageGenerator::getGridOnImage( )
57 {
58         //Variables
59         double spc[3];
60         int dims[3];
61         int sizeX = 0, sizeY = 0, sizeZ = 0;
62         double minMaxValue[2];
63         double minValue = 0, maxValue = 0;
64         int spcVoxelX = 0, spcVoxelY = 0, spcVoxelZ = 0;
65
66         vtkImageData* gridImage = vtkImageData::New();
67         gridImage->ShallowCopy(image);
68         gridImage->Update();
69
70         //Get image spacing, size and mix and max values
71         gridImage->GetSpacing(spc);
72         gridImage->GetDimensions(dims);
73         sizeX = dims[0];
74         sizeY = dims[1];
75         sizeZ = dims[2];
76         gridImage->GetScalarRange(minMaxValue);
77         minValue = minMaxValue[0];
78         maxValue = minMaxValue[1];
79
80         //Grid spacing in voxels
81         spcVoxelX = spcX / spc[0];
82         spcVoxelY = spcY / spc[1];
83         spcVoxelZ = spcZ / spc[2];
84
85         std::cout << "spcVoxelX:" << spcVoxelX << ", spcVoxelY:" << spcVoxelY << ", spcVoxelZ:" << spcVoxelZ <<std::endl;
86         std::cout << "minValue:" << minValue << ", maxValue:" << maxValue << std::endl;
87         std::cout << "sizeX:" << sizeX << ", sizeY:" << sizeY << ", sizeZ:" << sizeZ <<std::endl;
88
89         for(int px = 0; px < sizeX; px++)
90         {
91                 for(int py = 0; py < sizeY; py++)
92                 {
93                         for(int pz = 0; pz < sizeZ; pz++)
94                         {
95                                 if(px % spcVoxelX == 0 || py % spcVoxelY == 0 || pz % spcVoxelZ == 0)
96                                 {
97                                         //std::cout << "In" ;
98                                         unsigned char* pointer = (unsigned char*) gridImage->GetScalarPointer(px, py, pz);
99                                         if( (*pointer - minValue) < (maxValue - *pointer) )
100                                                 *pointer = maxValue;
101                                         else
102                                                 *pointer = minValue;
103                                 }
104                         }
105                 }
106         }
107
108
109         return gridImage;
110 }
111