VTKWriter.cc 3.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
//== INCLUDES =================================================================

#include <fstream>
#include <limits>

#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/IO/BinaryHelper.hh>
#include <OpenMesh/Core/IO/writer/VTKWriter.hh>
#include <OpenMesh/Core/IO/IOManager.hh>
#include <OpenMesh/Core/System/omstream.hh>
#include <OpenMesh/Core/Utils/color_cast.hh>

//=== NAMESPACES ==============================================================

namespace OpenMesh {
namespace IO {

//=== INSTANTIATE =============================================================

_VTKWriter_  __VTKWriterinstance;
_VTKWriter_& VTKWriter() { return __VTKWriterinstance; }

//=== IMPLEMENTATION ==========================================================

_VTKWriter_::_VTKWriter_() { IOManager().register_module(this); }

//-----------------------------------------------------------------------------

bool _VTKWriter_::write(const std::string& _filename, BaseExporter& _be, Options _opt, std::streamsize _precision) const
{
    std::ofstream out(_filename.c_str());

    if (!out) {
        omerr() << "[VTKWriter] : cannot open file " << _filename << std::endl;
        return false;
    }

    return write(out, _be, _opt, _precision);
}

//-----------------------------------------------------------------------------

bool _VTKWriter_::write(std::ostream& _out, BaseExporter& _be, Options _opt, std::streamsize _precision) const
{
    unsigned int idx;
    Vec3f v, n;
    Vec2f t;
    VertexHandle vh;
    OpenMesh::Vec3f c;
    OpenMesh::Vec4f cA;

    // check exporter features
    if (!check(_be, _opt)) {
        return false;
    }

    // check writer features
    if (!_opt.is_empty()) {
        omlog() << "[VTKWriter] : writer does not support any options\n";
        return false;
    }

    omlog() << "[VTKWriter] : write file\n";
    _out.precision(_precision);

    std::vector<VertexHandle> vhandles;
    size_t polygon_table_size = 0;
    size_t nf = _be.n_faces();
    for (size_t i = 0; i < nf; ++i) {
        polygon_table_size += _be.get_vhandles(FaceHandle(int(i)), vhandles);
    }
    polygon_table_size += nf;

    // header
    _out << "# vtk DataFile Version 3.0\n";
    _out << "Generated by OpenMesh\n";
    _out << "ASCII\n";
    _out << "DATASET POLYDATA\n";

    // points
    _out << "POINTS " << _be.n_vertices() << " float\n";
    size_t nv = _be.n_vertices();
    for (size_t i = 0; i < nv; ++i) {
        Vec3f v = _be.point(VertexHandle(int(i)));
        _out << v[0] << ' ' << v[1] << ' ' << v[2] << std::endl;
    }

    // faces
    _out << "POLYGONS " << nf << ' ' << polygon_table_size << std::endl;
    for (size_t i = 0; i < nf; ++i) {
        _be.get_vhandles(FaceHandle(int(i)), vhandles);

        _out << vhandles.size() << ' ';
        for (size_t j = 0; j < vhandles.size(); ++j) {
            _out << " " << vhandles[j].idx();
        }
        _out << std::endl;
    }

    return true;
}

//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================