]> Creatis software - creaRigidRegistration.git/blob - lib/Surface.cxx
Feature #1766 Add licence terms for all files.
[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         surfaceData->Update();
210
211         vtkPolyDataMapper* surfaceMapper = vtkPolyDataMapper::New();
212         surfaceMapper->SetInput(surfaceData);
213         surfaceMapper->Update();
214
215     surfaceResult->SetMapper(surfaceMapper);
216         surfaceResult->GetProperty()->SetOpacity(1.0);
217
218         if (color == "RED")
219         {
220                 surfaceResult->GetProperty()->SetColor(1,0,0);
221         }
222         else if (color == "BLUE")
223         {
224                 surfaceResult->GetProperty()->SetColor(0,0,1);
225         }
226         else if (color == "GREEN")
227         {
228                 surfaceResult->GetProperty()->SetColor(0,1,0);
229         }
230         else
231         {
232                 surfaceResult->GetProperty()->SetColor(1,1,1);
233         }
234 }
235
236 /*
237 Template for constructing the surface by image type
238 */
239 template <class T>
240 void Surface::surfaceByType(T* dataImagePointer, vtkImageData* imageData, T max)
241 {
242         int counter=0;
243         
244         // we start where the  image starts
245         dataImagePointer=(T*)imageData->GetScalarPointer(0,0,0);        
246         
247         /*
248          Image Size
249         */
250         int ext[6];
251         imageData->GetExtent(ext);
252         int sx,sy,sz;
253         sx=ext[1]-ext[0]+1;
254         sy=ext[3]-ext[2]+1;
255         sz=ext[5]-ext[4]+1;
256
257         //-----------------
258         //A3
259         //-----------------
260         //walking in the image
261         int i=0,j=0,k=0;
262         double sum1=0;  
263         //double sum2=0,sum3=0,sum4=0;  // JPR : unused
264         for(i=0;i<sx;i++)
265         {
266                 for(j=0;j<sy;j++)
267                 {
268                         for(k=0;k<sz;k++)
269                         {
270                                 
271                                 // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
272                                 dataImagePointer=(T*)imageData->GetScalarPointer(i,j,k);                                
273
274                                 sum1=(T)(dataImagePointer[0]) + (T)(dataImagePointer[1]) + (T)(dataImagePointer[2]);
275                                 sum1=sum1/(3*max);                              
276
277                                 surfacePoints->InsertPoint(counter, i, j, sum1*height);                         
278
279                                 counter ++;
280                         }
281                 }
282         }
283
284         //This cycle creates the cells of the surface
285         int n =0;       
286         for(i=0;i<sx-1;i++)
287         {
288                 for(j=0;j<sy-1;j++)
289                 {
290                         for(k=0;k<sz;k++)
291                         {
292                                 surfaceCells->InsertNextCell(3);
293                                 surfaceCells->InsertCellPoint(n);
294                                 surfaceCells->InsertCellPoint(n+1);
295                                 surfaceCells->InsertCellPoint(n+sy+1);
296
297                                 surfaceCells->InsertNextCell(3);
298                                 surfaceCells->InsertCellPoint(n);
299                                 surfaceCells->InsertCellPoint(n+sy+1);
300                                 surfaceCells->InsertCellPoint(n+sy);
301
302                                 if(j<sy-2)
303                                 {
304                                         n++;
305                                 }
306                                 else
307                                 {
308                                         n=n+2;
309                                 }
310                                 
311                         }
312                 }
313         }
314 }
315
316 /*
317 Returns the filtered image
318 */
319 vtkActor* Surface::getSurface()
320 {
321         return surfaceResult;
322 }
323