1 #include "vtkImageData.h"
2 #include "vtkImageCast.h"
5 #include "vtkPolyDataMapper.h"
6 #include "vtkPolyData.h"
7 #include "vtkProperty.h"
8 #include "vtkFloatArray.h"
10 #include "vtkDataSetMapper.h"
11 #include "vtkCellArray.h"
20 Surface::Surface(vtkImageData* imageData, int ZHeight, std::string iColor)
22 surfaceResult= vtkActor::New();
25 //Original image type this case is an unsigned char (3)
26 imageType=imageData->GetScalarType();
28 //substracting the image
29 createSurface(imageData);
34 if(surfaceResult!=NULL)surfaceResult->Delete();
37 //----------------------------------------------------------------------------
39 //----------------------------------------------------------------------------
43 Calculate the new image and save it in the attribute imageResult
44 it is used if the user had given the imageData
46 void Surface::createSurface(vtkImageData* imageData)
48 //dimensions of the image (extent)
50 //setting the dimensionality (1d or 2d or 3d )
55 //getting the information from the original image
56 imageData->GetSpacing(spc);
57 imageData->GetExtent(ext);
60 newDim[0]=ext[1]-ext[0]+1;
61 newDim[1]=ext[3]-ext[2]+1;
62 newDim[2]=1;// in general it is ext[5]-ext[4]+1
65 //initializing the image that represents the substracted image
66 //initialize(newDim,spc);
72 Setting the values for the
74 void Surface::surface(vtkImageData* imageData)
79 vtkPoints* surfacePoints = vtkPoints::New();
80 vtkCellArray* surfaceCells = vtkCellArray::New();
83 if(imageType == VTK_CHAR)
85 // pointers to get into the image
86 char* dataImagePointer=NULL;
88 // we start where the image starts
89 dataImagePointer=(char*)imageData->GetScalarPointer(0,0,0);
95 imageData->GetExtent(ext);
104 //walking in the image
106 double sum1=0,sum2=0,sum3=0,sum4=0;
114 // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
115 dataImagePointer=(char*)imageData->GetScalarPointer(i,j,k);
117 sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
118 sum1=sum1/(3*VTK_CHAR_MAX);
120 surfacePoints->InsertPoint(counter, i, j, sum1*height);
127 //This cycle creates the cells of the surface
135 surfaceCells->InsertNextCell(3);
136 surfaceCells->InsertCellPoint(n);
137 surfaceCells->InsertCellPoint(n+1);
138 surfaceCells->InsertCellPoint(n+sy+1);
140 surfaceCells->InsertNextCell(3);
141 surfaceCells->InsertCellPoint(n);
142 surfaceCells->InsertCellPoint(n+sy+1);
143 surfaceCells->InsertCellPoint(n+sy);
158 else if(imageType == VTK_SIGNED_CHAR)
160 // pointers to get into the image
161 signed char* dataImagePointer=NULL;
163 // we start where the image starts
164 dataImagePointer=(signed char*)imageData->GetScalarPointer(0,0,0);
170 imageData->GetExtent(ext);
179 //walking in the image
181 double sum1=0,sum2=0,sum3=0,sum4=0;
189 // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
190 dataImagePointer=(signed char*)imageData->GetScalarPointer(i,j,k);
192 sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
193 sum1=sum1/(3*VTK_SIGNED_CHAR_MAX);
195 surfacePoints->InsertPoint(counter, i, j, sum1*height);
202 //This cycle creates the cells of the surface
210 surfaceCells->InsertNextCell(3);
211 surfaceCells->InsertCellPoint(n);
212 surfaceCells->InsertCellPoint(n+1);
213 surfaceCells->InsertCellPoint(n+sy+1);
215 surfaceCells->InsertNextCell(3);
216 surfaceCells->InsertCellPoint(n);
217 surfaceCells->InsertCellPoint(n+sy+1);
218 surfaceCells->InsertCellPoint(n+sy);
234 else if(imageType == VTK_UNSIGNED_CHAR)
236 // pointers to get into the image
237 unsigned char* dataImagePointer=NULL;
239 // we start where the image starts
240 dataImagePointer=(unsigned char*)imageData->GetScalarPointer(0,0,0);
246 imageData->GetExtent(ext);
255 //walking in the image
257 double sum1=0,sum2=0,sum3=0,sum4=0;
265 // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
266 dataImagePointer=(unsigned char*)imageData->GetScalarPointer(i,j,k);
268 sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
269 sum1=sum1/(3*VTK_UNSIGNED_CHAR_MAX);
271 surfacePoints->InsertPoint(counter, i, j, sum1*height);
278 //This cycle creates the cells of the surface
286 surfaceCells->InsertNextCell(3);
287 surfaceCells->InsertCellPoint(n);
288 surfaceCells->InsertCellPoint(n+1);
289 surfaceCells->InsertCellPoint(n+sy+1);
291 surfaceCells->InsertNextCell(3);
292 surfaceCells->InsertCellPoint(n);
293 surfaceCells->InsertCellPoint(n+sy+1);
294 surfaceCells->InsertCellPoint(n+sy);
309 else if(imageType == VTK_SHORT)
311 // pointers to get into the image
312 short* dataImagePointer=NULL;
314 // we start where the image starts
315 dataImagePointer=(short*)imageData->GetScalarPointer(0,0,0);
321 imageData->GetExtent(ext);
330 //walking in the image
332 double sum1=0,sum2=0,sum3=0,sum4=0;
340 // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
341 dataImagePointer=(short*)imageData->GetScalarPointer(i,j,k);
343 sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
344 sum1=sum1/(3*VTK_SHORT_MAX);
346 surfacePoints->InsertPoint(counter, i, j, sum1*height);
353 //This cycle creates the cells of the surface
361 surfaceCells->InsertNextCell(3);
362 surfaceCells->InsertCellPoint(n);
363 surfaceCells->InsertCellPoint(n+1);
364 surfaceCells->InsertCellPoint(n+sy+1);
366 surfaceCells->InsertNextCell(3);
367 surfaceCells->InsertCellPoint(n);
368 surfaceCells->InsertCellPoint(n+sy+1);
369 surfaceCells->InsertCellPoint(n+sy);
385 else if(imageType == VTK_UNSIGNED_SHORT)
387 // pointers to get into the image
388 unsigned short* dataImagePointer=NULL;
390 // we start where the image starts
391 dataImagePointer=(unsigned short*)imageData->GetScalarPointer(0,0,0);
397 imageData->GetExtent(ext);
406 //walking in the image
408 double sum1=0,sum2=0,sum3=0,sum4=0;
416 // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
417 dataImagePointer=(unsigned short*)imageData->GetScalarPointer(i,j,k);
419 sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
420 sum1=sum1/(3*VTK_UNSIGNED_SHORT_MAX);
422 surfacePoints->InsertPoint(counter, i, j, sum1*height);
429 //This cycle creates the cells of the surface
437 surfaceCells->InsertNextCell(3);
438 surfaceCells->InsertCellPoint(n);
439 surfaceCells->InsertCellPoint(n+1);
440 surfaceCells->InsertCellPoint(n+sy+1);
442 surfaceCells->InsertNextCell(3);
443 surfaceCells->InsertCellPoint(n);
444 surfaceCells->InsertCellPoint(n+sy+1);
445 surfaceCells->InsertCellPoint(n+sy);
461 else if(imageType == VTK_INT)
463 // pointers to get into the image
464 int* dataImagePointer=NULL;
466 // we start where the image starts
467 dataImagePointer=(int*)imageData->GetScalarPointer(0,0,0);
473 imageData->GetExtent(ext);
482 //walking in the image
484 double sum1=0,sum2=0,sum3=0,sum4=0;
492 // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
493 dataImagePointer=(int*)imageData->GetScalarPointer(i,j,k);
495 sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
496 sum1=sum1/(3*VTK_INT_MAX);
498 surfacePoints->InsertPoint(counter, i, j, sum1*height);
505 //This cycle creates the cells of the surface
513 surfaceCells->InsertNextCell(3);
514 surfaceCells->InsertCellPoint(n);
515 surfaceCells->InsertCellPoint(n+1);
516 surfaceCells->InsertCellPoint(n+sy+1);
518 surfaceCells->InsertNextCell(3);
519 surfaceCells->InsertCellPoint(n);
520 surfaceCells->InsertCellPoint(n+sy+1);
521 surfaceCells->InsertCellPoint(n+sy);
537 else if(imageType == VTK_UNSIGNED_INT)
539 // pointers to get into the image
540 unsigned int* dataImagePointer=NULL;
542 // we start where the image starts
543 dataImagePointer=(unsigned int*)imageData->GetScalarPointer(0,0,0);
549 imageData->GetExtent(ext);
558 //walking in the image
560 double sum1=0,sum2=0,sum3=0,sum4=0;
568 // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
569 dataImagePointer=(unsigned int*)imageData->GetScalarPointer(i,j,k);
571 sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
572 sum1=sum1/(3*VTK_UNSIGNED_INT_MAX);
574 surfacePoints->InsertPoint(counter, i, j, sum1*height);
581 //This cycle creates the cells of the surface
589 surfaceCells->InsertNextCell(3);
590 surfaceCells->InsertCellPoint(n);
591 surfaceCells->InsertCellPoint(n+1);
592 surfaceCells->InsertCellPoint(n+sy+1);
594 surfaceCells->InsertNextCell(3);
595 surfaceCells->InsertCellPoint(n);
596 surfaceCells->InsertCellPoint(n+sy+1);
597 surfaceCells->InsertCellPoint(n+sy);
613 else if(imageType == VTK_LONG)
615 // pointers to get into the image
616 long* dataImagePointer=NULL;
618 // we start where the image starts
619 dataImagePointer=(long*)imageData->GetScalarPointer(0,0,0);
625 imageData->GetExtent(ext);
634 //walking in the image
636 double sum1=0,sum2=0,sum3=0,sum4=0;
644 // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
645 dataImagePointer=(long*)imageData->GetScalarPointer(i,j,k);
647 sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
648 sum1=sum1/(3*VTK_LONG_MAX);
650 surfacePoints->InsertPoint(counter, i, j, sum1*height);
657 //This cycle creates the cells of the surface
665 surfaceCells->InsertNextCell(3);
666 surfaceCells->InsertCellPoint(n);
667 surfaceCells->InsertCellPoint(n+1);
668 surfaceCells->InsertCellPoint(n+sy+1);
670 surfaceCells->InsertNextCell(3);
671 surfaceCells->InsertCellPoint(n);
672 surfaceCells->InsertCellPoint(n+sy+1);
673 surfaceCells->InsertCellPoint(n+sy);
689 else if(imageType == VTK_UNSIGNED_LONG)
691 // pointers to get into the image
692 unsigned long* dataImagePointer=NULL;
694 // we start where the image starts
695 dataImagePointer=(unsigned long*)imageData->GetScalarPointer(0,0,0);
701 imageData->GetExtent(ext);
710 //walking in the image
712 double sum1=0,sum2=0,sum3=0,sum4=0;
720 // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
721 dataImagePointer=(unsigned long*)imageData->GetScalarPointer(i,j,k);
723 sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
724 sum1=sum1/(3*VTK_UNSIGNED_LONG_MAX);
726 surfacePoints->InsertPoint(counter, i, j, sum1*height);
733 //This cycle creates the cells of the surface
741 surfaceCells->InsertNextCell(3);
742 surfaceCells->InsertCellPoint(n);
743 surfaceCells->InsertCellPoint(n+1);
744 surfaceCells->InsertCellPoint(n+sy+1);
746 surfaceCells->InsertNextCell(3);
747 surfaceCells->InsertCellPoint(n);
748 surfaceCells->InsertCellPoint(n+sy+1);
749 surfaceCells->InsertCellPoint(n+sy);
765 else if(imageType == VTK_FLOAT)
767 // pointers to get into the image
768 float* dataImagePointer=NULL;
770 // we start where the image starts
771 dataImagePointer=(float*)imageData->GetScalarPointer(0,0,0);
777 imageData->GetExtent(ext);
786 //walking in the image
788 double sum1=0,sum2=0,sum3=0,sum4=0;
796 // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
797 dataImagePointer=(float*)imageData->GetScalarPointer(i,j,k);
799 sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
800 sum1=sum1/(3*VTK_FLOAT_MAX);
802 surfacePoints->InsertPoint(counter, i, j, sum1*height);
809 //This cycle creates the cells of the surface
817 surfaceCells->InsertNextCell(3);
818 surfaceCells->InsertCellPoint(n);
819 surfaceCells->InsertCellPoint(n+1);
820 surfaceCells->InsertCellPoint(n+sy+1);
822 surfaceCells->InsertNextCell(3);
823 surfaceCells->InsertCellPoint(n);
824 surfaceCells->InsertCellPoint(n+sy+1);
825 surfaceCells->InsertCellPoint(n+sy);
841 else if(imageType == VTK_DOUBLE)
843 std::cout << "Got inside Double" << std::endl;
844 // pointers to get into the image
845 double* dataImagePointer=NULL;
847 // we start where the image starts
848 dataImagePointer=(double*)imageData->GetScalarPointer(0,0,0);
854 imageData->GetExtent(ext);
863 //walking in the image
865 double sum1=0,sum2=0,sum3=0,sum4=0;
873 // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
874 dataImagePointer=(double*)imageData->GetScalarPointer(i,j,k);
876 sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
877 sum1=sum1/(3*VTK_DOUBLE_MAX);
879 surfacePoints->InsertPoint(counter, i, j, sum1*height);
886 //This cycle creates the cells of the surface
894 surfaceCells->InsertNextCell(3);
895 surfaceCells->InsertCellPoint(n);
896 surfaceCells->InsertCellPoint(n+1);
897 surfaceCells->InsertCellPoint(n+sy+1);
899 surfaceCells->InsertNextCell(3);
900 surfaceCells->InsertCellPoint(n);
901 surfaceCells->InsertCellPoint(n+sy+1);
902 surfaceCells->InsertCellPoint(n+sy);
919 vtkPolyData* surfaceData = vtkPolyData::New();
920 surfaceData->SetPolys(surfaceCells);
921 surfaceData->SetPoints(surfacePoints);
922 surfaceData->Update();
924 vtkPolyDataMapper* surfaceMapper = vtkPolyDataMapper::New();
925 surfaceMapper->SetInput(surfaceData);
926 surfaceMapper->Update();
928 surfaceResult->SetMapper(surfaceMapper);
929 surfaceResult->GetProperty()->SetOpacity(1.0);
933 surfaceResult->GetProperty()->SetColor(1,0,0);
935 else if (color == "BLUE")
937 surfaceResult->GetProperty()->SetColor(0,0,1);
939 else if (color == "GREEN")
941 surfaceResult->GetProperty()->SetColor(0,1,0);
945 surfaceResult->GetProperty()->SetColor(1,1,1);
949 Returns the filtered image
951 vtkActor* Surface::getSurface()
953 return surfaceResult;