]> Creatis software - creaMaracasVisu.git/blob - lib/Kernel/VTKObjects/SurfaceRenderer/surfacerenderingimagestencilexport.cxx
ec92d05ffd5d44b30da09565ba6e53ba74e9d7d9
[creaMaracasVisu.git] / lib / Kernel / VTKObjects / SurfaceRenderer / surfacerenderingimagestencilexport.cxx
1 #include "surfacerenderingimagestencilexport.h"
2
3 #include "vtkObjectFactory.h"
4 #include "vtkMetaImageWriter.h"
5
6 #include "math.h"
7
8 vtkStandardNewMacro(SurfaceRenderingImageStencilExport)
9
10 SurfaceRenderingImageStencilExport::SurfaceRenderingImageStencilExport()
11 {    
12     ImageOutput = 0;
13 }
14
15 SurfaceRenderingImageStencilExport::~SurfaceRenderingImageStencilExport(){
16
17     if(ImageOutput){
18         ImageOutput->Delete();
19     }
20 }
21
22 void SurfaceRenderingImageStencilExport::Update(){
23     this->Execute(0);
24 }
25
26 void SurfaceRenderingImageStencilExport::Execute(vtkImageData *data){
27
28     vtkPolyData* polydata = (vtkPolyData*)this->GetInput();
29
30     vtkSmartPointer<vtkPolyDataToImageStencil> polytostencil = vtkSmartPointer<vtkPolyDataToImageStencil>::New();
31     polytostencil->SetInput(polydata);
32     polytostencil->Update();
33
34     double *bounds = polydata->GetBounds();
35     vtkSmartPointer<vtkImageData> imagein = vtkSmartPointer<vtkImageData>::New();
36
37     imagein->SetExtent(bounds[0] - 1, bounds[1] + 1, bounds[2] - 1, bounds[3] + 1, bounds[4] - 1, bounds[5] + 1);
38     imagein->SetScalarTypeToUnsignedShort();    
39     imagein->AllocateScalars();
40
41
42     int* extent = imagein->GetExtent();
43
44     for (int x = extent[0]; x <= extent[1]; x++){
45         for (int y = extent[2]; y <= extent[3]; y++){
46             for (int z  =extent[4]; z <= extent[5]; z++){
47                 unsigned short* pixel = static_cast<unsigned short*>(imagein->GetScalarPointer(x,y,z));
48                 *pixel = 0;
49             }
50         }
51     }
52
53     vtkSmartPointer<vtkImageStencil> stencil = vtkSmartPointer<vtkImageStencil>::New();
54     stencil->SetInput(imagein);
55     stencil->SetStencil(polytostencil->GetOutput());
56     stencil->ReverseStencilOn();
57     stencil->SetBackgroundValue(128);
58     stencil->Update();
59
60     if(ImageOutput){
61         ImageOutput->Delete();
62     }
63     ImageOutput = vtkImageData::New();
64     ImageOutput->DeepCopy(stencil->GetOutput());
65
66 }
67
68 vtkImageData* SurfaceRenderingImageStencilExport::GetOutput(){
69     return ImageOutput;
70 }