]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkPolyDataWriterPlus.cxx
e960ef44dee4bef035987ca8425aece4efc86764
[bbtk.git] / packages / vtk / src / bbvtkPolyDataWriterPlus.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 #include "bbvtkPolyDataWriterPlus.h"
29 #include "bbvtkPackage.h"
30 namespace bbvtk
31 {
32
33 BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,PolyDataWriterPlus)
34 BBTK_BLACK_BOX_IMPLEMENTATION(PolyDataWriterPlus,bbtk::AtomicBlackBox);
35
36 void PolyDataWriterPlus::Process()
37 {
38 //      std::cout << "[" << this << "]" << "PolyDataWriterPlus::Process()..." << std::endl;
39         //Review the observers...
40
41         if (!HasObserver(OBS_PRE_WRITER_1) && bbGetInputInPrePersistObs1() != NULL)
42                 AddObserver(OBS_PRE_WRITER_1, bbGetInputInPrePersistObs1());
43  /// \TODO fix warning: NULL used in arithmetic // JPR
44         if (!HasObserver(OBS_POST_WRITER_1) == NULL && bbGetInputInPostPersistObs1() != NULL)
45                 AddObserver(OBS_POST_WRITER_1, bbGetInputInPostPersistObs1());
46
47         InvokeEvent(OBS_PRE_WRITER_1);
48
49         if (bbGetInputInPath().size()==0)
50         {
51                 std::cout << "Set InPath." << std::endl;
52                 return;
53         }
54
55         vtkPolyData* cosa;
56
57         if (bbGetInputIn()==NULL)
58         {
59                 if (bbGetInputIn2()==NULL)
60                 {
61                         std::cout << "Set In." << std::endl;
62                         return;
63                 } else {
64                         vtkProp3D* actor                        = bbGetInputIn2();
65                         vtkPolyDataMapper* mapper       = ((vtkPolyDataMapper*)((vtkActor*)actor)->GetMapper());
66                         cosa                                            = mapper->GetInput();
67                 }
68         } else {
69                 cosa = bbGetInputIn();
70         }
71
72         std::string nuevo_nombre = guessName(bbGetInputInPath());
73
74         vtkPolyData* entrada = cosa;
75 //      std::cout << "Before writing" << std::endl;
76 //      entrada->Print(std::cout);
77
78         std::vector< std::string > partes = StringSplit(nuevo_nombre, ".");
79         std::string extension = partes.at(partes.size()-1);
80
81 //      std::cout << "Extension (vtk, vtp o stl?) => " << extension << std::endl;
82
83         vtkDataSetSurfaceFilter* surfaceFilter = vtkDataSetSurfaceFilter::New();
84
85 //EED 2017-01-01 Migration VTK7
86 #if VTK_MAJOR_VERSION <= 5
87         surfaceFilter->SetInput(entrada);
88 #else
89         surfaceFilter->SetInputData(entrada);
90 #endif
91
92         surfaceFilter->Update();
93         //vtkPolyData* polydata = surfaceFilter->GetOutput();
94         entrada = surfaceFilter->GetOutput();
95
96         if (extension.compare("vtk") == 0)
97         {
98                 vtkPolyDataWriter* writer = vtkPolyDataWriter::New();
99
100 //EED 2017-01-01 Migration VTK7
101 #if VTK_MAJOR_VERSION <= 5
102                 writer->SetInput(entrada);
103 #else
104                 writer->SetInputData(entrada);
105 #endif
106
107                 writer->SetFileName(nuevo_nombre.data());
108                 writer->Write();
109                 writer->Delete();
110         }
111
112         if (extension.compare("vtp") == 0)
113         {
114                 vtkXMLPolyDataWriter* writer = vtkXMLPolyDataWriter::New();
115                 writer->SetFileName ( nuevo_nombre.data() );
116
117 //EED 2017-01-01 Migration VTK7
118 #if VTK_MAJOR_VERSION <= 5
119                 writer->SetInput ( entrada );
120 #else
121                 writer->SetInputData ( entrada );
122 #endif
123
124                 writer->Write();
125                 writer->Delete();
126         }
127
128         if (extension.compare("stl") == 0)
129         {
130                 vtkSTLWriter* writer = vtkSTLWriter::New();
131                 writer->SetFileName ( nuevo_nombre.data() );
132
133 //EED 2017-01-01 Migration VTK7
134 #if VTK_MAJOR_VERSION <= 5
135                 writer->SetInput ( entrada );
136 #else
137                 writer->SetInputData ( entrada );
138 #endif
139
140                 writer->Write();
141                 writer->Print(std::cout);
142                 writer->Delete();
143         }
144
145         std::cout << "Saving Ok!" << std::endl;
146
147         InvokeEvent(OBS_POST_WRITER_1);
148 }
149
150 void PolyDataWriterPlus::bbUserSetDefaultValues()
151 {
152
153 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
154 //    Here we initialize the input 'In' to 0
155     bbSetInputIn(NULL);
156         bbSetInputIn2(NULL);
157         bbSetInputInPrePersistObs1(NULL);
158         bbSetInputInPostPersistObs1(NULL);
159         std::string nada = "";
160         std::string prefixDefault = "Data";
161         bbSetInputInPath(nada);
162         bbSetInputInFilePrefix(prefixDefault);
163
164 }
165 void PolyDataWriterPlus::bbUserInitializeProcessing()
166 {
167
168 //  THE INITIALIZATION METHOD BODY :
169 //    Here does nothing
170 //    but this is where you should allocate the internal/output pointers
171 //    if any
172
173
174 }
175 void PolyDataWriterPlus::bbUserFinalizeProcessing()
176 {
177
178 //  THE FINALIZATION METHOD BODY :
179 //    Here does nothing
180 //    but this is where you should desallocate the internal/output pointers
181 //    if any
182         RemoveAllObservers();
183
184         if (bbGetInputInPrePersistObs1() != NULL && bbGetInputInPrePersistObs1()->GetReferenceCount() > 0)
185                 bbGetInputInPrePersistObs1()->SetReferenceCount(bbGetInputInPrePersistObs1()->GetReferenceCount()-1);
186         if (bbGetInputInPostPersistObs1() != NULL)
187                 bbGetInputInPostPersistObs1()->SetReferenceCount(bbGetInputInPostPersistObs1()->GetReferenceCount()-1);
188 }
189 }
190 // EO namespace bbvtk
191
192