]> 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 "vtkMetaImageWriter.h"
29
30 #include "math.h"
31
32
33 vtkStandardNewMacro(SurfaceRenderingImageStencilExport)
34
35 SurfaceRenderingImageStencilExport::SurfaceRenderingImageStencilExport()
36 {    
37     ImageOutput = 0;
38 }
39
40 SurfaceRenderingImageStencilExport::~SurfaceRenderingImageStencilExport()
41 {
42
43     if(ImageOutput)
44         {
45         ImageOutput->Delete();
46     }
47 }
48
49
50
51 //EED 2017-01-01 Migration VTK7
52 #if VTK_MAJOR_VERSION <= 5
53
54         void SurfaceRenderingImageStencilExport::Update(){
55             this->Execute(0);
56         }
57
58
59         // This method is much too long, and has to be broken up!
60         // Furthermore we are loosing the normals !!!
61         void SurfaceRenderingImageStencilExport::Execute(vtkImageData *data){
62         {
63
64     vtkSmartPointer<vtkPolyDataToImageStencil> polytostencil = vtkSmartPointer<vtkPolyDataToImageStencil>::New();
65
66     vtkPolyData* polydata = (vtkPolyData*)this->GetInput();
67     polytostencil->SetInput(polydata);
68     polytostencil->Update();
69
70     double *bounds = polydata->GetBounds();
71     vtkSmartPointer<vtkImageData> imagein = vtkSmartPointer<vtkImageData>::New();
72
73     imagein->SetExtent(bounds[0] - 1, bounds[1] + 1, bounds[2] - 1, bounds[3] + 1, bounds[4] - 1, bounds[5] + 1);
74     imagein->SetScalarTypeToUnsignedShort();    
75     imagein->AllocateScalars();
76
77     int* extent = imagein->GetExtent();
78
79         int x, y, z;
80     for (x = extent[0]; x <= extent[1]; x++){
81         for (y = extent[2]; y <= extent[3]; y++){
82             for (z  =extent[4]; z <= extent[5]; z++){
83                 unsigned short* pixel = static_cast<unsigned short*>(imagein->GetScalarPointer(x,y,z));
84                 *pixel = 0;
85             }  // for x
86         } // for y 
87     } // for z
88
89     vtkSmartPointer<vtkImageStencil> stencil = vtkSmartPointer<vtkImageStencil>::New();
90
91     stencil->SetInput(imagein);
92     stencil->SetStencil(polytostencil->GetOutput());
93
94     stencil->ReverseStencilOn();
95     stencil->SetBackgroundValue(128);
96     stencil->Update();
97
98     if(ImageOutput)
99         {
100         ImageOutput->Delete();
101     }
102     ImageOutput = vtkImageData::New();
103     ImageOutput->DeepCopy(stencil->GetOutput());
104
105         }
106
107 #else
108
109 int SurfaceRenderingImageStencilExport::RequestData(  vtkInformation *vtkNotUsed(request),  vtkInformationVector **inputVector,  vtkInformationVector *outputVector)
110 {
111
112         // get the info objects
113         vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
114         vtkInformation *outInfo = outputVector->GetInformationObject(0);
115
116         // get the input and output
117         vtkPolyData *input = vtkPolyData::SafeDownCast(    inInfo->Get(vtkDataObject::DATA_OBJECT()));
118         vtkPolyData *output = vtkPolyData::SafeDownCast(    outInfo->Get(vtkDataObject::DATA_OBJECT()));
119
120
121     vtkSmartPointer<vtkPolyDataToImageStencil> polytostencil = vtkSmartPointer<vtkPolyDataToImageStencil>::New();
122
123 //    vtkPolyData* polydata = (vtkPolyData*)this->GetInput();
124     vtkPolyData* polydata = input;
125     polytostencil->SetInputData(polydata);
126     polytostencil->Update();
127
128     double *bounds = polydata->GetBounds();
129     vtkSmartPointer<vtkImageData> imagein = vtkSmartPointer<vtkImageData>::New();
130
131     imagein->SetExtent(bounds[0] - 1, bounds[1] + 1, bounds[2] - 1, bounds[3] + 1, bounds[4] - 1, bounds[5] + 1);
132     imagein->AllocateScalars(VTK_UNSIGNED_SHORT,1);
133
134
135     int* extent = imagein->GetExtent();
136
137         int x,y,z;
138     for (x = extent[0]; x <= extent[1]; x++){
139         for (y = extent[2]; y <= extent[3]; y++){
140             for (z  =extent[4]; z <= extent[5]; z++){
141                 unsigned short* pixel = static_cast<unsigned short*>(imagein->GetScalarPointer(x,y,z));
142                 *pixel = 0;
143             } // for z
144         }  // for y
145     }  // for z
146
147     vtkSmartPointer<vtkImageStencil> stencil = vtkSmartPointer<vtkImageStencil>::New();
148
149     stencil->SetInputData(imagein);
150     stencil->SetStencilData(polytostencil->GetOutput());
151
152     stencil->ReverseStencilOn();
153     stencil->SetBackgroundValue(128);
154     stencil->Update();
155
156     if(ImageOutput)
157         {
158         ImageOutput->Delete();
159     }
160     ImageOutput = vtkImageData::New();
161     ImageOutput->DeepCopy(stencil->GetOutput());
162
163         return 1;
164 }
165
166 #endif
167
168
169
170
171
172
173
174 //-------------------------------------------------------------------------------------------
175
176 vtkImageData* SurfaceRenderingImageStencilExport::GetOutputData()
177 {
178     return ImageOutput;
179 }