]> Creatis software - creaRigidRegistration.git/blob - lib/Surface.cxx
#3221 creaRigidRegistration Feature New Normal - vtk8itk4wx3-mingw64
[creaRigidRegistration.git] / lib / Surface.cxx
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 #include "vtkImageData.h"
28 #include "vtkImageCast.h"
29 #include "vtkActor.h"
30 #include "vtkPolyDataMapper.h"
31 #include "vtkPolyData.h"
32 #include "vtkProperty.h"
33 #include "vtkFloatArray.h"
34 #include "vtkType.h"
35 #include "vtkDataSetMapper.h"
36
37 #include "Surface.h"
38
39 #include <iostream>
40 #include <fstream>
41 #include <string>
42 #include <vector>
43
44 Surface::Surface(vtkImageData* imageData, int ZHeight, std::string iColor)
45 {
46         surfaceResult= vtkActor::New();
47         height=ZHeight;
48         color=iColor;   
49         //Original image type this case is an unsigned char (3)
50         imageType=imageData->GetScalarType();
51
52         //substracting the image
53         createSurface(imageData);
54 }
55
56 Surface::~Surface()
57 {
58         if(surfaceResult!=NULL)surfaceResult->Delete();
59 }
60
61 //----------------------------------------------------------------------------
62 // Methods
63 //----------------------------------------------------------------------------
64
65
66 /*
67 Calculate the new image and save it in the attribute imageResult
68 it is used if the user had given the imageData
69 */
70 void Surface::createSurface(vtkImageData* imageData)
71 {
72         //dimensions of the image (extent)
73         int ext[6];
74         //setting the dimensionality (1d or 2d or 3d )
75     int newDim[3];
76         //image spacing
77     double spc[3];
78   
79         //getting the information from the original image
80         imageData->GetSpacing(spc);
81         imageData->GetExtent(ext);
82         
83         //this a 2d image
84         newDim[0]=ext[1]-ext[0]+1;
85     newDim[1]=ext[3]-ext[2]+1;
86     newDim[2]=1;// in general it is ext[5]-ext[4]+1
87
88
89         //initializing the image that represents the substracted image
90         //initialize(newDim,spc);
91         //Time to substract
92         surface(imageData);     
93 }
94
95 /*
96          Setting the values for the
97 */
98 void Surface::surface(vtkImageData* imageData)
99 {
100         /*
101                 images pointers
102         */
103         surfacePoints = vtkPoints::New();
104         surfaceCells = vtkCellArray::New();
105         
106         if(imageType == VTK_CHAR)
107         {
108                 // pointers to get into the image
109                 char* dataImagePointer=NULL;
110                 
111                 char max = VTK_CHAR_MAX;
112
113                 surfaceByType(dataImagePointer, imageData, max);
114         }               
115         else if(imageType == VTK_SIGNED_CHAR)
116         {
117                 // pointers to get into the image
118                 signed char* dataImagePointer=NULL;
119
120                 signed char max = VTK_SIGNED_CHAR_MAX;
121
122                 surfaceByType(dataImagePointer, imageData, max);        
123         }
124         else if(imageType == VTK_UNSIGNED_CHAR)
125         {
126                 // pointers to get into the image
127                 unsigned char* dataImagePointer=NULL;
128
129                 unsigned char max = VTK_UNSIGNED_CHAR_MAX;
130
131                 surfaceByType(dataImagePointer, imageData, max);        
132         }
133         else if(imageType == VTK_SHORT)
134         {
135                 // pointers to get into the image
136                 short* dataImagePointer=NULL;
137
138                 short max = VTK_SHORT_MAX;
139
140                 surfaceByType(dataImagePointer, imageData, max);        
141         }
142         else if(imageType == VTK_UNSIGNED_SHORT)
143         {
144                 // pointers to get into the image
145                 unsigned short* dataImagePointer=NULL;
146
147                 unsigned short max = VTK_UNSIGNED_SHORT_MAX;
148
149                 surfaceByType(dataImagePointer, imageData, max);        
150         }
151         else if(imageType == VTK_INT)
152         {
153                 // pointers to get into the image
154                 int* dataImagePointer=NULL;
155                 
156                 int max = VTK_INT_MAX;
157
158                 surfaceByType(dataImagePointer, imageData, max);
159         }
160         else if(imageType == VTK_UNSIGNED_INT)
161         {
162                 // pointers to get into the image
163                 unsigned int* dataImagePointer=NULL;
164                 
165                 unsigned int max = VTK_UNSIGNED_INT_MAX;
166
167                 surfaceByType(dataImagePointer, imageData, max);
168         }
169         else if(imageType == VTK_LONG)
170         {
171                 // pointers to get into the image
172                 long* dataImagePointer=NULL;
173                 
174                 long max = VTK_LONG_MAX;
175
176                 surfaceByType(dataImagePointer, imageData, max);
177         }
178         else if(imageType == VTK_UNSIGNED_LONG)
179         {
180                 // pointers to get into the image
181                 unsigned long* dataImagePointer=NULL;
182
183                 unsigned long max = VTK_UNSIGNED_LONG_MAX;
184
185                 surfaceByType(dataImagePointer, imageData, max);        
186         }
187         else if(imageType == VTK_FLOAT)
188         {               
189                 // pointers to get into the image
190                 float* dataImagePointer=NULL;
191
192                 float max = VTK_FLOAT_MAX;
193
194                 surfaceByType(dataImagePointer, imageData, max);        
195         }
196         else if(imageType == VTK_DOUBLE)
197         {
198                 // pointers to get into the image
199                 double* dataImagePointer=NULL;
200
201                 double max = VTK_DOUBLE_MAX;
202
203                 surfaceByType(dataImagePointer, imageData, max);
204         }
205
206         vtkPolyData* surfaceData = vtkPolyData::New();
207         surfaceData->SetPolys(surfaceCells);
208         surfaceData->SetPoints(surfacePoints);
209 //EED 2017-01-01 Migration VTK7
210 #if VTK_MAJOR_VERSION <= 5
211         surfaceData->Update();
212 #else
213         // ..
214 #endif
215
216         vtkPolyDataMapper* surfaceMapper = vtkPolyDataMapper::New();
217 //EED 2017-01-01 Migration VTK7
218 #if VTK_MAJOR_VERSION <= 5
219         surfaceMapper->SetInput(surfaceData);
220 #else
221         surfaceMapper->SetInputData(surfaceData);
222 #endif
223         surfaceMapper->Update();
224
225     surfaceResult->SetMapper(surfaceMapper);
226         surfaceResult->GetProperty()->SetOpacity(1.0);
227
228         if (color == "RED")
229         {
230                 surfaceResult->GetProperty()->SetColor(1,0,0);
231         }
232         else if (color == "BLUE")
233         {
234                 surfaceResult->GetProperty()->SetColor(0,0,1);
235         }
236         else if (color == "GREEN")
237         {
238                 surfaceResult->GetProperty()->SetColor(0,1,0);
239         }
240         else
241         {
242                 surfaceResult->GetProperty()->SetColor(1,1,1);
243         }
244 }
245
246 /*
247 Template for constructing the surface by image type
248 */
249 template <class T>
250 void Surface::surfaceByType(T* dataImagePointer, vtkImageData* imageData, T max)
251 {
252         int counter=0;
253         
254         // we start where the  image starts
255         dataImagePointer=(T*)imageData->GetScalarPointer(0,0,0);        
256         
257         /*
258          Image Size
259         */
260         int ext[6];
261         imageData->GetExtent(ext);
262         int sx,sy,sz;
263         sx=ext[1]-ext[0]+1;
264         sy=ext[3]-ext[2]+1;
265         sz=ext[5]-ext[4]+1;
266
267         //-----------------
268         //A3
269         //-----------------
270         //walking in the image
271         int i=0,j=0,k=0;
272         double sum1=0;  
273         //double sum2=0,sum3=0,sum4=0;  // JPR : unused
274         for(i=0;i<sx;i++)
275         {
276                 for(j=0;j<sy;j++)
277                 {
278                         for(k=0;k<sz;k++)
279                         {
280                                 
281                                 // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
282                                 dataImagePointer=(T*)imageData->GetScalarPointer(i,j,k);                                
283
284                                 sum1=(T)(dataImagePointer[0]) + (T)(dataImagePointer[1]) + (T)(dataImagePointer[2]);
285                                 sum1=sum1/(3*max);                              
286
287                                 surfacePoints->InsertPoint(counter, i, j, sum1*height);                         
288
289                                 counter ++;
290                         }
291                 }
292         }
293
294         //This cycle creates the cells of the surface
295         int n =0;       
296         for(i=0;i<sx-1;i++)
297         {
298                 for(j=0;j<sy-1;j++)
299                 {
300                         for(k=0;k<sz;k++)
301                         {
302                                 surfaceCells->InsertNextCell(3);
303                                 surfaceCells->InsertCellPoint(n);
304                                 surfaceCells->InsertCellPoint(n+1);
305                                 surfaceCells->InsertCellPoint(n+sy+1);
306
307                                 surfaceCells->InsertNextCell(3);
308                                 surfaceCells->InsertCellPoint(n);
309                                 surfaceCells->InsertCellPoint(n+sy+1);
310                                 surfaceCells->InsertCellPoint(n+sy);
311
312                                 if(j<sy-2)
313                                 {
314                                         n++;
315                                 }
316                                 else
317                                 {
318                                         n=n+2;
319                                 }
320                                 
321                         }
322                 }
323         }
324 }
325
326 /*
327 Returns the filtered image
328 */
329 vtkActor* Surface::getSurface()
330 {
331         return surfaceResult;
332 }
333