]> Creatis software - creaRigidRegistration.git/blob - lib/MyGridOnImageGenerator.cpp
#3113 crea Rigid Registration Bug New Normal - branch vtk7itk4 compilation with...
[creaRigidRegistration.git] / lib / MyGridOnImageGenerator.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------      */                                                                    
26
27 //----------------------------------------------------------------------------
28 // Class definition include
29 //----------------------------------------------------------------------------
30
31 #include "MyGridOnImageGenerator.h"
32
33 //----------------------------------------------------------------------------
34 // Class implementation
35 //----------------------------------------------------------------------------
36 //----------------------------------------------------------------------------
37 // Builder
38 //----------------------------------------------------------------------------
39
40 MyGridOnImageGenerator::MyGridOnImageGenerator( vtkImageData* nImage, double nScpX, double nScpY, double nScpZ)
41 {
42         spcX = nScpX;
43         spcY = nScpY;
44         spcZ = nScpZ;
45         image = nImage;
46 }
47
48 //----------------------------------------------------------------------------
49 // Destructor
50 //----------------------------------------------------------------------------
51
52 MyGridOnImageGenerator::~MyGridOnImageGenerator( )
53 {
54
55 }
56 //----------------------------------------------------------------------------
57 // Methods
58 //----------------------------------------------------------------------------
59
60
61 void MyGridOnImageGenerator::setScpX(double nScpX)
62 {
63         spcX = nScpX;
64 }
65
66 void MyGridOnImageGenerator::setScpY(double nScpY)
67 {
68         spcY = nScpY;
69 }
70
71 void MyGridOnImageGenerator::setScpZ(double nScpZ)
72 {
73         spcZ = nScpZ;
74 }
75
76
77 void MyGridOnImageGenerator::setImage(vtkImageData* nImage)
78 {
79         image = nImage;
80 }
81
82 vtkImageData* MyGridOnImageGenerator::getGridOnImage( )
83 {
84         //Variables
85         double spc[3];
86         int dims[3];
87         int sizeX = 0, sizeY = 0, sizeZ = 0;
88         double minMaxValue[2];
89         double minValue = 0, maxValue = 0;
90         int spcVoxelX = 0, spcVoxelY = 0, spcVoxelZ = 0;
91
92         vtkImageData* gridImage = vtkImageData::New();
93         gridImage->ShallowCopy(image);
94 //EED 2017-01-01 Migration VTK7
95 #if VTK_MAJOR_VERSION <= 5
96         gridImage->Update();
97 #else
98         gridImage->Modified();
99 #endif
100
101         //Get image spacing, size and mix and max values
102         gridImage->GetSpacing(spc);
103         gridImage->GetDimensions(dims);
104         sizeX = dims[0];
105         sizeY = dims[1];
106         sizeZ = dims[2];
107         gridImage->GetScalarRange(minMaxValue);
108         minValue = minMaxValue[0];
109         maxValue = minMaxValue[1];
110
111         //Grid spacing in voxels
112         spcVoxelX = spcX / spc[0];
113         spcVoxelY = spcY / spc[1];
114         spcVoxelZ = spcZ / spc[2];
115
116         std::cout << "spcVoxelX:" << spcVoxelX << ", spcVoxelY:" << spcVoxelY << ", spcVoxelZ:" << spcVoxelZ <<std::endl;
117         std::cout << "minValue:" << minValue << ", maxValue:" << maxValue << std::endl;
118         std::cout << "sizeX:" << sizeX << ", sizeY:" << sizeY << ", sizeZ:" << sizeZ <<std::endl;
119
120         for(int px = 0; px < sizeX; px++)
121         {
122                 for(int py = 0; py < sizeY; py++)
123                 {
124                         for(int pz = 0; pz < sizeZ; pz++)
125                         {
126                                 if(px % spcVoxelX == 0 || py % spcVoxelY == 0 || pz % spcVoxelZ == 0)
127                                 {
128                                         //std::cout << "In" ;
129                                         unsigned char* pointer = (unsigned char*) gridImage->GetScalarPointer(px, py, pz);
130                                         if( (*pointer - minValue) < (maxValue - *pointer) )
131                                                 *pointer = maxValue;
132                                         else
133                                                 *pointer = minValue;
134                                 }
135                         }
136                 }
137         }
138
139
140         return gridImage;
141 }
142