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  Vec3f v, n;
44  Vec2f t;
45  VertexHandle vh;
47  OpenMesh::Vec4f cA;
48 
49  // check exporter features
50  if (!check(_be, _opt)) {
51  return false;
52  }
53 
54  // check writer features
55  if (!_opt.is_empty()) {
56  omlog() << "[VTKWriter] : writer does not support any options\n";
57  return false;
58  }
59 
60  omlog() << "[VTKWriter] : write file\n";
61  _out.precision(_precision);
62 
63  std::vector<VertexHandle> vhandles;
64  size_t polygon_table_size = 0;
65  size_t nf = _be.n_faces();
66  for (size_t i = 0; i < nf; ++i) {
67  polygon_table_size += _be.get_vhandles(FaceHandle(int(i)), vhandles);
68  }
69  polygon_table_size += nf;
70 
71  // header
72  _out << "# vtk DataFile Version 3.0\n";
73  _out << "Generated by OpenMesh\n";
74  _out << "ASCII\n";
75  _out << "DATASET POLYDATA\n";
76 
77  // points
78  _out << "POINTS " << _be.n_vertices() << " float\n";
79  size_t nv = _be.n_vertices();
80  for (size_t i = 0; i < nv; ++i) {
81  Vec3f v = _be.point(VertexHandle(int(i)));
82  _out << v[0] << ' ' << v[1] << ' ' << v[2] << '\n';
83  }
84 
85  // faces
86  _out << "POLYGONS " << nf << ' ' << polygon_table_size << '\n';
87  for (size_t i = 0; i < nf; ++i) {
88  _be.get_vhandles(FaceHandle(int(i)), vhandles);
89 
90  _out << vhandles.size() << ' ';
91  for (size_t j = 0; j < vhandles.size(); ++j) {
92  _out << " " << vhandles[j].idx();
93  }
94  _out << '\n';
95  }
96 
97  return true;
98 }
99 
100 //=============================================================================
101 } // namespace IO
102 } // namespace OpenMesh
103 //=============================================================================
Handle for a vertex entity.
Definition: Handles.hh:120
_VTKWriter_ __VTKWriterinstance
Declare the single entity of the OBJ writer.
Definition: VTKWriter.cc:18
bool register_module(BaseReader *_bl)
Definition: IOManager.hh:217
_IOManager_ & IOManager()
Definition: IOManager.cc:72
bool is_empty(void) const
Returns true if all bits are zero.
Definition: Options.hh:151
Handle for a face entity.
Definition: Handles.hh:141
Set options for reader/writer modules.
Definition: Options.hh:90
bool write(const std::string &, BaseExporter &, Options, std::streamsize _precision=6) const
Definition: VTKWriter.cc:27