]> Creatis software - creaMaracasVisu.git/blob - lib/Kernel/VTKObjects/SurfaceRenderer/surfacerenderingimagestencilexport.cxx
Support #1768 CREATIS Licence insertion
[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     vtkPolyData* polydata = (vtkPolyData*)this->GetInput();
54
55     vtkSmartPointer<vtkPolyDataToImageStencil> polytostencil = vtkSmartPointer<vtkPolyDataToImageStencil>::New();
56     polytostencil->SetInput(polydata);
57     polytostencil->Update();
58
59     double *bounds = polydata->GetBounds();
60     vtkSmartPointer<vtkImageData> imagein = vtkSmartPointer<vtkImageData>::New();
61
62     imagein->SetExtent(bounds[0] - 1, bounds[1] + 1, bounds[2] - 1, bounds[3] + 1, bounds[4] - 1, bounds[5] + 1);
63     imagein->SetScalarTypeToUnsignedShort();    
64     imagein->AllocateScalars();
65
66
67     int* extent = imagein->GetExtent();
68
69     for (int x = extent[0]; x <= extent[1]; x++){
70         for (int y = extent[2]; y <= extent[3]; y++){
71             for (int z  =extent[4]; z <= extent[5]; z++){
72                 unsigned short* pixel = static_cast<unsigned short*>(imagein->GetScalarPointer(x,y,z));
73                 *pixel = 0;
74             }
75         }
76     }
77
78     vtkSmartPointer<vtkImageStencil> stencil = vtkSmartPointer<vtkImageStencil>::New();
79     stencil->SetInput(imagein);
80     stencil->SetStencil(polytostencil->GetOutput());
81     stencil->ReverseStencilOn();
82     stencil->SetBackgroundValue(128);
83     stencil->Update();
84
85     if(ImageOutput){
86         ImageOutput->Delete();
87     }
88     ImageOutput = vtkImageData::New();
89     ImageOutput->DeepCopy(stencil->GetOutput());
90
91 }
92
93 vtkImageData* SurfaceRenderingImageStencilExport::GetOutput(){
94     return ImageOutput;
95 }