obj.hh 2.2 KB
Newer Older
1
2
#pragma once

3
#include <iosfwd>
4
5
#include <string>

6
#include <polymesh/Mesh.hh>
7
8
9

namespace polymesh
{
10
template <class ScalarT>
11
void write_obj(std::string const& filename, vertex_attribute<std::array<ScalarT, 3>> const& position);
12
13
template <class ScalarT>
bool read_obj(std::string const& filename, Mesh& mesh, vertex_attribute<std::array<ScalarT, 3>>& position);
14

15
template <class ScalarT>
16
17
18
19
20
21
22
struct obj_writer
{
    obj_writer(std::string const& filename);
    obj_writer(std::ostream& out);
    ~obj_writer();

    void write_object_name(std::string object_name);
23
    void write_mesh(vertex_attribute<std::array<ScalarT, 4>> const& position,
24
25
                    halfedge_attribute<std::array<ScalarT, 3>> const* tex_coord = nullptr,
                    halfedge_attribute<std::array<ScalarT, 3>> const* normal = nullptr);
26
    void write_mesh(vertex_attribute<std::array<ScalarT, 3>> const& position,
27
28
                    vertex_attribute<std::array<ScalarT, 2>> const* tex_coord = nullptr,
                    vertex_attribute<std::array<ScalarT, 3>> const* normal = nullptr);
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

    // TODO: tex coords and normals as half-edge attributes

private:
    std::ostream* tmp_out = nullptr;
    std::ostream* out = nullptr;

    int vertex_idx = 1;
    int texture_idx = 1;
    int normal_idx = 1;
};

// clears the given mesh before adding data
// obj must be manifold
// no negative indices
44
template <class ScalarT>
45
46
47
48
49
50
51
struct obj_reader
{
    obj_reader(std::string const& filename, Mesh& mesh);
    obj_reader(std::istream& in, Mesh& mesh);

    // get properties of the obj
public:
52
53
54
    vertex_attribute<std::array<ScalarT, 4>> const& get_positions() const { return positions; }
    halfedge_attribute<std::array<ScalarT, 3>> const& get_tex_coords() const { return tex_coords; }
    halfedge_attribute<std::array<ScalarT, 3>> const& get_normals() const { return normals; }
55

56
57
58
    /// Number of faces that could not be added
    int error_faces() const { return n_error_faces; }

59
60
61
private:
    void parse(std::istream& in, Mesh& mesh);

62
63
64
    vertex_attribute<std::array<ScalarT, 4>> positions;
    halfedge_attribute<std::array<ScalarT, 3>> tex_coords;
    halfedge_attribute<std::array<ScalarT, 3>> normals;
65
66

    int n_error_faces = 0;
67
};
68
} // namespace polymesh