TTK
Loading...
Searching...
No Matches
ttkProgramBase.h
Go to the documentation of this file.
1
8
9#pragma once
10
11// VTK IO
12#include <vtkDataSet.h>
13#include <vtkDataSetAlgorithm.h>
14#include <vtkImageData.h>
15#include <vtkPolyData.h>
16#include <vtkSmartPointer.h>
17#include <vtkUnstructuredGrid.h>
18#include <vtkXMLImageDataReader.h>
19#include <vtkXMLImageDataWriter.h>
20#include <vtkXMLPolyDataReader.h>
21#include <vtkXMLPolyDataWriter.h>
22#include <vtkXMLUnstructuredGridReader.h>
23#include <vtkXMLUnstructuredGridWriter.h>
24
25// base code includes
26#include <ProgramBase.h>
27#include <ttkAlgorithm.h>
28
29#include <ttkProgramBaseModule.h>
30
31class TTKPROGRAMBASE_EXPORT ttkProgramBase : public ttk::ProgramBase {
32
33public:
35
36 vtkWrapper_ = nullptr;
37 };
38
39 ~ttkProgramBase() override = default;
40 ;
41
43 int execute() override;
44
45 vtkDataSet *getInput(const int &inputId) {
46 if((inputId < 0) || (inputId >= (int)inputs_.size()))
47 return nullptr;
48 return inputs_[inputId];
49 }
50
52 return inputs_.size();
53 };
54
55 int run() override {
56
57 if(!vtkWrapper_) {
58 return -1;
59 }
60
61 if(!ttkModule_)
62 return -2;
63
64 ttkModule_->setDebugLevel(debugLevel_);
65 ttkModule_->setThreadNumber(threadNumber_);
66
67 return execute();
68 }
69
71 int save() const override;
72
73 virtual int setTTKmodule(vtkDataSetAlgorithm *ttkModule) {
74
75 vtkWrapper_ = ttkModule;
76 ttkModule_ = (Debug *)ttkModule;
77
78 return 0;
79 }
80
81protected:
82 std::vector<vtkDataSet *> inputs_;
83 std::vector<vtkSmartPointer<vtkXMLImageDataReader>> imageDataReaders_;
84 std::vector<vtkSmartPointer<vtkXMLPolyDataReader>> polyDataReaders_;
85 std::vector<vtkSmartPointer<vtkXMLUnstructuredGridReader>>
87 vtkDataSetAlgorithm *vtkWrapper_;
88
89 template <class vtkReaderClass>
90 int load(const std::string &fileName,
91 std::vector<vtkSmartPointer<vtkReaderClass>> &readerList);
92
94 int load(const std::vector<std::string> &inputPaths) override;
95
96 template <class vtkWriterClass>
97 int save(const int &outputPortId) const;
98};
99
100template <class ttkModule>
102
103public:
106 vtkWrapper_ = (vtkDataSetAlgorithm *)ttkObject_.GetPointer();
107 ttkModule_ = (Debug *)ttkObject_.GetPointer();
108 }
109
110 int run() override {
111 return ttkProgramBase::run();
112 }
113
115};
116
117template <class vtkWriterClass>
118int ttkProgramBase::save(const int &outputPortId) const {
119
120 if(!vtkWrapper_)
121 return -1;
122
123 std::string extension;
124
125 if((vtkWrapper_->GetOutput(outputPortId)->GetDataObjectType()
126 == VTK_IMAGE_DATA)) {
127 // ||(vtkWrapper_->GetOutput(outputPortId)->GetDataObjectType()
128 // == TTK_IMAGE_DATA)){
129 extension = "vti";
130 }
131
132 if((vtkWrapper_->GetOutput(outputPortId)->GetDataObjectType()
133 == VTK_POLY_DATA)) {
134 // ||(vtkWrapper_->GetOutput(outputPortId)->GetDataObjectType()
135 // == TTK_POLY_DATA)){
136 extension = "vtp";
137 }
138
139 if((vtkWrapper_->GetOutput(outputPortId)->GetDataObjectType()
140 == VTK_UNSTRUCTURED_GRID)) {
141 // ||(vtkWrapper_->GetOutput(outputPortId)->GetDataObjectType()
142 // == TTK_UNSTRUCTURED_GRID)){
143 extension = "vtu";
144 }
145
146 std::stringstream fileName;
147 fileName << outputPath_ << "_port#" << outputPortId << "." << extension;
148
151 writer->SetFileName(fileName.str().data());
152 writer->SetInputData(vtkWrapper_->GetOutput(outputPortId));
153 std::stringstream msg;
154 msg << "[ttkProgramBase] Saving output file `" << fileName.str() << "'..."
155 << std::endl;
156 printMsg(msg.str());
157
158 writer->Write();
159
160 return 0;
161}
162
163template <class vtkReaderClass>
165 const std::string &fileName,
166 std::vector<vtkSmartPointer<vtkReaderClass>> &readerList) {
167
168 readerList.resize(readerList.size() + 1);
169 readerList.back() = vtkSmartPointer<vtkReaderClass>::New();
170
171 readerList.back()->SetFileName(fileName.data());
172
173 // handle debug messages
174 {
175 std::stringstream msg;
176 msg << "[ttkProgramBase] Reading input data..." << std::endl;
177 // choose where to display this message (std::cout, std::cerr, a file)
178 // choose the priority of this message (1, nearly always displayed,
179 // higher values mean lower priorities)
180 printMsg(msg.str());
181 }
182
183 readerList.back()->Update();
184 inputs_.push_back(readerList.back()->GetOutput());
185
186 if(!inputs_.back())
187 return -1;
188
189 if(!inputs_.back()->GetNumberOfPoints())
190 return -2;
191
192 if(!inputs_.back()->GetNumberOfCells())
193 return -3;
194
195 {
196 std::stringstream msg;
197 msg << "[ttkProgramBase] done! (read "
198 << inputs_.back()->GetNumberOfPoints() << " vertices, "
199 << inputs_.back()->GetNumberOfCells() << " cells)" << std::endl;
200 printMsg(msg.str());
201 }
202
203 return 0;
204}
Base VTK editor class for standalone programs. This class parses the the command line,...
std::vector< vtkSmartPointer< vtkXMLPolyDataReader > > polyDataReaders_
std::vector< vtkSmartPointer< vtkXMLImageDataReader > > imageDataReaders_
int load(const std::string &fileName, std::vector< vtkSmartPointer< vtkReaderClass > > &readerList)
vtkDataSet * getInput(const int &inputId)
int run() override
~ttkProgramBase() override=default
vtkDataSetAlgorithm * vtkWrapper_
std::vector< vtkSmartPointer< vtkXMLUnstructuredGridReader > > unstructuredGridReaders_
int save() const override
Save the output(s) of the TTK module.
std::vector< vtkDataSet * > inputs_
virtual int setTTKmodule(vtkDataSetAlgorithm *ttkModule)
Base editor class for standalone programs. This class parses the the command line,...
Definition ProgramBase.h:16
virtual int save() const =0
Save the output(s) of the TTK module.
std::string outputPath_
Definition ProgramBase.h:72
virtual int execute()=0
Execute your TTK module here.
virtual int load(const std::vector< std::string > &inputPaths)=0
Load a sequence of input data-sets.
int run() override
vtkSmartPointer< ttkModule > ttkObject_
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)