Developer Documentation
VTKWriter.cc
1 //== INCLUDES =================================================================
2 
3 #include <fstream>
4 #include <limits>
5 
6 #include <OpenMesh/Core/IO/BinaryHelper.hh>
7 #include <OpenMesh/Core/IO/writer/VTKWriter.hh>
8 #include <OpenMesh/Core/IO/IOManager.hh>
9 #include <OpenMesh/Core/Utils/color_cast.hh>
10 
11 //=== NAMESPACES ==============================================================
12 
13 namespace OpenMesh {
14 namespace IO {
15 
16 //=== INSTANTIATE =============================================================
17 
19 _VTKWriter_& VTKWriter() { return __VTKWriterinstance; }
20 
21 //=== IMPLEMENTATION ==========================================================
22 
23 _VTKWriter_::_VTKWriter_() { IOManager().register_module(this); }
24 
25 //-----------------------------------------------------------------------------
26 
27 bool _VTKWriter_::write(const std::string& _filename, BaseExporter& _be, Options _opt, std::streamsize _precision) const
28 {
29  std::ofstream out(_filename.c_str());
30 
31  if (!out) {
32  omerr() << "[VTKWriter] : cannot open file " << _filename << std::endl;
33  return false;
34  }
35 
36  return write(out, _be, _opt, _precision);
37 }
38 
39 //-----------------------------------------------------------------------------
40 
41 bool _VTKWriter_::write(std::ostream& _out, BaseExporter& _be, Options _opt, std::streamsize _precision) const
42 {
43  VertexHandle vh;
44 
45  // check exporter features
46  if (!check(_be, _opt)) {
47  return false;
48  }
49 
50  // check writer features
51  if (!_opt.is_empty()) {
52  omlog() << "[VTKWriter] : writer does not support any options\n";
53  return false;
54  }
55 
56  omlog() << "[VTKWriter] : write file\n";
57  _out.precision(_precision);
58 
59  std::vector<VertexHandle> vhandles;
60  size_t polygon_table_size = 0;
61  size_t nf = _be.n_faces();
62  for (size_t i = 0; i < nf; ++i) {
63  polygon_table_size += _be.get_vhandles(FaceHandle(int(i)), vhandles);
64  }
65  polygon_table_size += nf;
66 
67  // header
68  _out << "# vtk DataFile Version 3.0\n";
69  _out << "Generated by OpenMesh\n";
70  _out << "ASCII\n";
71  _out << "DATASET POLYDATA\n";
72 
73  // points
74  _out << "POINTS " << _be.n_vertices() << " float\n";
75  size_t nv = _be.n_vertices();
76  for (size_t i = 0; i < nv; ++i) {
77  const Vec3f v = _be.point(VertexHandle(int(i)));
78  _out << v[0] << ' ' << v[1] << ' ' << v[2] << '\n';
79  }
80 
81  // faces
82  _out << "POLYGONS " << nf << ' ' << polygon_table_size << '\n';
83  for (size_t i = 0; i < nf; ++i) {
84  _be.get_vhandles(FaceHandle(int(i)), vhandles);
85 
86  _out << vhandles.size() << ' ';
87  for (size_t j = 0; j < vhandles.size(); ++j) {
88  _out << " " << vhandles[j].idx();
89  }
90  _out << '\n';
91  }
92 
93  return true;
94 }
95 
96 //=============================================================================
97 } // namespace IO
98 } // namespace OpenMesh
99 //=============================================================================
Handle for a face entity.
Definition: Handles.hh:141
_VTKWriter_ __VTKWriterinstance
Declare the single entity of the OBJ writer.
Definition: VTKWriter.cc:18
bool is_empty(void) const
Returns true if all bits are zero.
Definition: Options.hh:151
Handle for a vertex entity.
Definition: Handles.hh:120
bool register_module(BaseReader *_bl)
Definition: IOManager.hh:217
bool write(const std::string &, BaseExporter &, Options, std::streamsize _precision=6) const override
Definition: VTKWriter.cc:27
Set options for reader/writer modules.
Definition: Options.hh:90
_IOManager_ & IOManager()
Definition: IOManager.cc:72