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