TTK
Loading...
Searching...
No Matches
ttkOFFWriter.cpp
Go to the documentation of this file.
1#include <ttkOFFWriter.h>
2
3#include <vtkCell.h>
4#include <vtkCellData.h>
5#include <vtkDataArray.h>
6#include <vtkDataSet.h>
7#include <vtkObjectFactory.h>
8#include <vtkPointData.h>
9
11
12// Public
13// {{{
14
15void ttkOFFWriter::PrintSelf(std::ostream &os, vtkIndent indent) {
16 this->Superclass::PrintSelf(os, indent);
17
18 os << indent << "File Name: " << (this->Filename ? this->Filename : "(none)")
19 << std::endl;
20}
21
22// }}}
23// Protected
24// {{{
25
27 this->setDebugMsgPrefix("OFFWriter");
28}
29
31
33
34 std::ofstream f(Filename, ios::out);
35
36 if(!f.fail()) {
37 Stream = std::move(f);
38 } else {
39 return -1;
40 }
41
42 return 0;
43}
44
46
47 vtkDataSet *dataSet = vtkDataSet::SafeDownCast(this->GetInput());
48
49 if(!dataSet)
50 return;
51
52 if(this->OpenFile() == -1) {
53 this->printErr("Could not open file `" + std::string{FileName} + "' :(");
54 return;
55 }
56
57 Stream << "OFF" << std::endl;
58 Stream << dataSet->GetNumberOfPoints() << " " << dataSet->GetNumberOfCells()
59 << " 0" << std::endl;
60
61 double p[3];
62 for(vtkIdType i = 0; i < dataSet->GetNumberOfPoints(); i++) {
63 dataSet->GetPoint(i, p);
64 Stream << p[0] << " " << p[1] << " " << p[2] << " ";
65
66 // by default, store everything
67 // use the field selector to select a subset
68 for(int j = 0; j < dataSet->GetPointData()->GetNumberOfArrays(); j++) {
69 vtkDataArray *array = dataSet->GetPointData()->GetArray(j);
70 for(int k = 0; k < array->GetNumberOfComponents(); k++) {
71 Stream << array->GetComponent(i, k) << " ";
72 }
73 }
74
75 Stream << std::endl;
76 }
77
78 for(vtkIdType i = 0; i < dataSet->GetNumberOfCells(); i++) {
79 vtkCell *c = dataSet->GetCell(i);
80
81 Stream << c->GetNumberOfPoints() << " ";
82 for(int j = 0; j < c->GetNumberOfPoints(); j++) {
83 Stream << c->GetPointId(j) << " ";
84 }
85
86 // by default, store everything
87 // use the field selector to select a subset
88 for(int j = 0; j < dataSet->GetCellData()->GetNumberOfArrays(); j++) {
89 vtkDataArray *array = dataSet->GetCellData()->GetArray(j);
90 for(int k = 0; k < array->GetNumberOfComponents(); k++) {
91 Stream << array->GetComponent(i, k) << " ";
92 }
93 }
94
95 Stream << std::endl;
96 }
97}
98
99// }}}
ttkOFFWriter - Object File Format Writer
std::ofstream Stream
void WriteData() override
char * Filename
~ttkOFFWriter() override
void PrintSelf(std::ostream &os, vtkIndent indent) override
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
vtkStandardNewMacro(ttkOFFWriter)