TTK
Loading...
Searching...
No Matches
ttkCinemaProductReader.cpp
Go to the documentation of this file.
2
3#include <vtkInformation.h>
4
5#include <vtkDoubleArray.h>
6#include <vtkFieldData.h>
7#include <vtkImageData.h>
8#include <vtkMultiBlockDataSet.h>
9#include <vtkStringArray.h>
10#include <vtkTable.h>
11#include <vtkXMLGenericDataObjectReader.h>
12
14
16 this->setDebugMsgPrefix("CinemaProductReader");
17 this->SetNumberOfInputPorts(1);
18 this->SetNumberOfOutputPorts(1);
19}
21
23 vtkInformation *info) {
24 if(port == 0)
25 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTable");
26 else
27 return 0;
28 return 1;
29}
30
32 vtkInformation *info) {
33 if(port == 0)
34 info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkMultiBlockDataSet");
35 else
36 return 0;
37 return 1;
38}
39
40template <class readerT>
41vtkSmartPointer<vtkDataObject> readFileLocal_(const std::string &pathToFile,
42 vtkNew<readerT> &reader) {
43 reader->SetFileName(pathToFile.data());
44 reader->Update();
45 if(reader->GetErrorCode() != 0)
46 return nullptr;
47
48 auto result
49 = vtkSmartPointer<vtkDataObject>::Take(reader->GetOutput()->NewInstance());
50 result->ShallowCopy(reader->GetOutput());
51 return result;
52}
53
55 ttkCinemaProductReader::readFileLocal(const std::string &pathToFile) {
56
57 if(pathToFile.substr(pathToFile.length() - 4, 4).compare(".ttk") == 0) {
58 this->topologicalCompressionReader->SetDebugLevel(this->debugLevel_);
59 return readFileLocal_(pathToFile, this->topologicalCompressionReader);
60 } else if(pathToFile.substr(pathToFile.size() - 4) == ".tif"
61 || pathToFile.substr(pathToFile.size() - 5) == ".tiff") {
62 return readFileLocal_(pathToFile, this->tiffReader);
63 } else if(pathToFile.substr(pathToFile.length() - 4, 4).compare(".png")
64 == 0) {
65 return readFileLocal_(pathToFile, this->pngReader);
66 } else {
67 // Check if dataset is XML encoded
68 std::ifstream is(pathToFile.data());
69 char prefix[10] = "";
70 is.get(prefix, 10);
71 bool const isXML = std::string(prefix).compare("<VTKFile ") == 0
72 || std::string(prefix).compare("<?xml ver") == 0;
73
74 if(isXML)
75 // If isXML use vtkXMLGenericDataObjectReader
76 return readFileLocal_(pathToFile, this->xmlGenericDataObjectReader);
77 else
78 // Otherwise use vtkGenericDataObjectReader
79 return readFileLocal_(pathToFile, this->genericDataObjectReader);
80 }
81
82 return nullptr;
83}
84
86 vtkFieldData *fd) {
87 auto objectAsMB = vtkMultiBlockDataSet::SafeDownCast(object);
88 if(objectAsMB) {
89 for(size_t i = 0, j = objectAsMB->GetNumberOfBlocks(); i < j; i++)
90 addFieldDataRecursively(objectAsMB->GetBlock(i), fd);
91 } else {
92 auto ofd = object->GetFieldData();
93 for(size_t i = 0, j = fd->GetNumberOfArrays(); i < j; i++)
94 ofd->AddArray(fd->GetAbstractArray(i));
95 }
96
97 return 1;
98}
99
101 vtkInformationVector **inputVector,
102 vtkInformationVector *outputVector) {
103 ttk::Timer timer;
104
105 // get input
106 auto inputTable = vtkTable::GetData(inputVector[0]);
107
108 auto outputMB = vtkMultiBlockDataSet::GetData(outputVector);
109
110 size_t const n = inputTable->GetNumberOfRows();
111 size_t const m = inputTable->GetNumberOfColumns();
112 // Determine number of files
113 this->printMsg(
114 {{"#Files", std::to_string(n)}, {"FILE Column", this->FilepathColumnName}});
116
117 // Read Data
118 {
119 // Get FILE column
120 auto paths = inputTable->GetColumnByName(this->FilepathColumnName.data());
121 if(!paths) {
122 this->printErr("Table does not have column '" + this->FilepathColumnName
123 + "'.");
124 return 0;
125 }
126
127 // For each row
128 for(size_t i = 0; i < n; i++) {
129
130 // initialize timer for individual file
131 ttk::Timer fileTimer;
132
133 // get filepath
134 auto path = paths->GetVariantValue(i).ToString();
135 auto file = path.substr(path.find_last_of("/") + 1);
136
137 // print progress
138 this->printMsg("Reading (" + std::to_string(i + 1) + "/"
139 + std::to_string(n) + "): \"" + file + "\"",
141
142 // read local file
143 {
144 std::ifstream const infile(path.data());
145 bool const exists = infile.good();
146 if(!exists) {
147 this->printErr("File does not exist.");
148 return 0;
149 }
150
151 auto readerOutput = this->readFileLocal(path);
152 if(!readerOutput) {
153 this->printErr("Unable to read file.");
154 return 0;
155 }
156
157 outputMB->SetBlock(i, readerOutput);
158 }
159
160 // augment data products with row data
161 {
162 auto block = outputMB->GetBlock(i);
163 auto fieldData = block->GetFieldData();
164 for(size_t j = 0; j < m; j++) {
165 auto columnName = inputTable->GetColumnName(j);
166
167 // always write FILE column
168 if(!fieldData->HasArray(columnName)
169 || columnName == this->FilepathColumnName) {
170 if(inputTable->GetColumn(j)->IsNumeric()) {
172 c->SetName(columnName);
173 c->SetNumberOfValues(1);
174 c->SetValue(0, inputTable->GetValue(i, j).ToDouble());
175 fieldData->AddArray(c);
176 } else {
178 c->SetName(columnName);
179 c->SetNumberOfValues(1);
180 c->SetValue(0, inputTable->GetValue(i, j).ToString());
181 fieldData->AddArray(c);
182 }
183 }
184 }
185
186 if(this->AddFieldDataRecursively)
187 this->addFieldDataRecursively(block, fieldData);
188
189 this->printMsg("Reading (" + std::to_string(i + 1) + "/"
190 + std::to_string(n) + "): \"" + file + "\"",
191 1, fileTimer.getElapsedTime());
192 }
193 }
194 }
195
196 // print stats
198 this->printMsg("Complete (#products: " + std::to_string(n) + ")", 1,
199 timer.getElapsedTime());
201
202 return 1;
203}
#define ttkNotUsed(x)
Mark function/method parameters that are not used in the function body at all.
Definition BaseClass.h:47
TTK VTK-filter that reads the data products that are referenced in a vtkTable.
int addFieldDataRecursively(vtkDataObject *object, vtkFieldData *fd)
vtkSmartPointer< vtkDataObject > readFileLocal(const std::string &pathToFile)
int FillInputPortInformation(int port, vtkInformation *info) override
int FillOutputPortInformation(int port, vtkInformation *info) override
~ttkCinemaProductReader() override
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
int debugLevel_
Definition Debug.h:379
void setDebugMsgPrefix(const std::string &prefix)
Definition Debug.h:364
int printErr(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
Definition Debug.h:149
double getElapsedTime()
Definition Timer.h:15
vtkStandardNewMacro(ttkCinemaProductReader)
vtkSmartPointer< vtkDataObject > readFileLocal_(const std::string &pathToFile, vtkNew< readerT > &reader)
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)