3#include <vtkCellData.h>
4#include <vtkDoubleArray.h>
6#include <vtkInformation.h>
7#include <vtkInformationVector.h>
9#include <vtkObjectFactory.h>
10#include <vtkPointData.h>
12#include <vtkStreamingDemandDrivenPipeline.h>
13#include <vtkUnstructuredGrid.h>
26 this->Superclass::PrintSelf(os, indent);
28 os << indent <<
"File Name: " << (this->FileName ? this->FileName :
"(none)")
38 this->SetNumberOfInputPorts(0);
39 this->SetNumberOfOutputPorts(1);
42static int countVertsData(
const std::string &line) {
43 std::istringstream ss(line);
54static int countCellsData(
const std::string &line) {
55 std::istringstream ss(line);
67 return nbFields - sizeCell;
70static int processLineVert(vtkIdType curLine,
73 std::vector<vtkNew<vtkDoubleArray>> &vertScalars,
74 const vtkIdType nbVertsData) {
77 std::istringstream ss(line);
82 points->InsertNextPoint(x, y, z);
85 for(vtkIdType i = 0; i < nbVertsData; i++) {
88 vertScalars[i]->SetTuple1(curLine, scalar);
94static int processLineCell(vtkIdType curLine,
96 vtkUnstructuredGrid *mesh,
97 std::vector<vtkNew<vtkDoubleArray>> &cellScalars,
98 const vtkIdType nbVerts,
99 const vtkIdType nbCellsData,
102 vtkNew<vtkIdList> cellVerts{};
103 std::istringstream ss(line);
105 for(
int j = 0; j < nbCellVerts; j++) {
108 cellVerts->InsertNextId(
id);
110 switch(nbCellVerts) {
112 mesh->InsertNextCell(VTK_LINE, cellVerts);
115 mesh->InsertNextCell(VTK_TRIANGLE, cellVerts);
118 mesh->InsertNextCell(VTK_TETRA, cellVerts);
121 dbg.
printErr(
"Unsupported cell type having " + std::to_string(nbCellVerts)
127 for(vtkIdType i = 0; i < nbCellsData; i++) {
131 cellScalars[i]->SetTuple1(curLine - nbVerts, scalar);
138 vtkInformationVector **
ttkNotUsed(inputVector),
139 vtkInformationVector *outputVector) {
140 std::ifstream offFile(FileName, ios::in);
143 this->
printErr(
"Can't read file: '" + std::string{FileName} +
"'");
147 std::string FileType;
150 if(FileType !=
"OFF") {
151 this->
printErr(
"Bad format for file: '" + std::string{FileName} +
"'");
157 vtkIdType nbVerts{}, nbCells{}, nbVertsData{}, nbCellsData{};
160 offFile >> nbVerts >> nbCells;
161 std::getline(offFile, line);
171 std::getline(offFile, line);
172 nbVertsData = countVertsData(line);
175 std::vector<vtkNew<vtkDoubleArray>> vertScalars{};
176 vertScalars.resize(nbVertsData);
177 for(vtkIdType i = 0; i < nbVertsData; i++) {
178 vertScalars[i]->SetNumberOfComponents(1);
179 vertScalars[i]->SetNumberOfTuples(nbVerts);
180 const std::string name =
"VertScalarField_" + std::to_string(i);
181 vertScalars[i]->SetName(name.c_str());
184 vtkNew<vtkPoints> points{};
188 (curLine = processLineVert(curLine, line, points, vertScalars, nbVertsData))
190 std::getline(offFile, line);
194 vtkNew<vtkUnstructuredGrid> mesh{};
195 mesh->SetPoints(points);
196 for(
const auto &scalarArray : vertScalars) {
197 mesh->GetPointData()->AddArray(scalarArray);
201 std::getline(offFile, line);
202 nbCellsData = countCellsData(line);
205 std::vector<vtkNew<vtkDoubleArray>> cellScalars{};
206 cellScalars.resize(nbCellsData);
207 for(vtkIdType i = 0; i < nbCellsData; i++) {
208 cellScalars[i]->SetNumberOfComponents(1);
209 cellScalars[i]->SetNumberOfTuples(nbCells);
210 const std::string name =
"CellScalarField_" + std::to_string(i);
211 cellScalars[i]->SetName(name.c_str());
216 while((curLine = processLineCell(
217 curLine, line, mesh, cellScalars, nbVerts, nbCellsData, *
this))
218 < nbVerts + nbCells) {
219 std::getline(offFile, line);
224 for(
const auto &scalarArray : cellScalars) {
225 mesh->GetCellData()->AddArray(scalarArray);
229 this->
printMsg(
"Read " + std::to_string(mesh->GetNumberOfPoints())
231 this->
printMsg(
"Read " + std::to_string(mesh->GetNumberOfCells())
236 vtkInformation *outInfo = outputVector->GetInformationObject(0);
237 outInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES(), 1);
240 vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
241 outInfo->Get(vtkDataObject::DATA_OBJECT()));
243 output->ShallowCopy(mesh);
#define ttkNotUsed(x)
Mark function/method parameters that are not used in the function body at all.
ttkOFFReader - Object File Format Reader
void PrintSelf(std::ostream &os, vtkIndent indent) override
int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
Minimalist debugging class.
void setDebugMsgPrefix(const std::string &prefix)
int printMsg(const std::string &msg, const debug::Priority &priority=debug::Priority::INFO, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cout) const
int printErr(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
vtkStandardNewMacro(ttkOFFReader)