]> Creatis software - creaMaracasVisu.git/blob - lib/Kernel/VTKObjects/SurfaceRenderer/surfacerenderingimagestencilexport.cxx
#3109 creaMaracasVisu Bug New Normal - branch vtk7itk4 compilation with vtk7
[creaMaracasVisu.git] / lib / Kernel / VTKObjects / SurfaceRenderer / surfacerenderingimagestencilexport.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 #include "surfacerenderingimagestencilexport.h"
27
28 #include "vtkObjectFactory.h"
29 #include "vtkMetaImageWriter.h"
30
31 #include "math.h"
32
33 vtkStandardNewMacro(SurfaceRenderingImageStencilExport)
34
35 SurfaceRenderingImageStencilExport::SurfaceRenderingImageStencilExport()
36 {    
37     ImageOutput = 0;
38 }
39
40 SurfaceRenderingImageStencilExport::~SurfaceRenderingImageStencilExport(){
41
42     if(ImageOutput){
43         ImageOutput->Delete();
44     }
45 }
46
47 void SurfaceRenderingImageStencilExport::Update(){
48     this->Execute(0);
49 }
50
51 void SurfaceRenderingImageStencilExport::Execute(vtkImageData *data){
52
53     vtkSmartPointer<vtkPolyDataToImageStencil> polytostencil = vtkSmartPointer<vtkPolyDataToImageStencil>::New();
54
55 //EED 2017-01-01 Migration VTK7
56 #if VTK_MAJOR_VERSION <= 5
57     vtkPolyData* polydata = (vtkPolyData*)this->GetInput();
58     polytostencil->SetInput(polydata);
59 #else
60     vtkPolyData* polydata = (vtkPolyData*)this->GetInput();
61     polytostencil->SetInputData(polydata);
62 #endif
63     polytostencil->Update();
64
65     double *bounds = polydata->GetBounds();
66     vtkSmartPointer<vtkImageData> imagein = vtkSmartPointer<vtkImageData>::New();
67
68     imagein->SetExtent(bounds[0] - 1, bounds[1] + 1, bounds[2] - 1, bounds[3] + 1, bounds[4] - 1, bounds[5] + 1);
69 //EED 2017-01-01 Migration VTK7
70 #if VTK_MAJOR_VERSION <= 5
71     imagein->SetScalarTypeToUnsignedShort();    
72     imagein->AllocateScalars();
73 #else
74     imagein->AllocateScalars(VTK_UNSIGNED_SHORT,1);
75 #endif
76
77
78     int* extent = imagein->GetExtent();
79
80     for (int x = extent[0]; x <= extent[1]; x++){
81         for (int y = extent[2]; y <= extent[3]; y++){
82             for (int z  =extent[4]; z <= extent[5]; z++){
83                 unsigned short* pixel = static_cast<unsigned short*>(imagein->GetScalarPointer(x,y,z));
84                 *pixel = 0;
85             }
86         }
87     }
88
89     vtkSmartPointer<vtkImageStencil> stencil = vtkSmartPointer<vtkImageStencil>::New();
90
91 //EED 2017-01-01 Migration VTK7
92 #if VTK_MAJOR_VERSION <= 5
93     stencil->SetInput(imagein);
94     stencil->SetStencil(polytostencil->GetOutput());
95 #else
96     stencil->SetInputData(imagein);
97     stencil->SetStencilData(polytostencil->GetOutput());
98 #endif
99
100     stencil->ReverseStencilOn();
101     stencil->SetBackgroundValue(128);
102     stencil->Update();
103
104     if(ImageOutput){
105         ImageOutput->Delete();
106     }
107     ImageOutput = vtkImageData::New();
108     ImageOutput->DeepCopy(stencil->GetOutput());
109
110 }
111
112 vtkImageData* SurfaceRenderingImageStencilExport::GetOutput(){
113     return ImageOutput;
114 }