obj.hh 2.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
#pragma once

#include <iostream>
#include <string>

#include "../Mesh.hh"

namespace polymesh
{
10
11
12
13
template <class ScalarT>
void write_obj(std::string const& filename, Mesh const& mesh, vertex_attribute<std::array<ScalarT, 3>> const& position);
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
23
struct obj_writer
{
    obj_writer(std::string const& filename);
    obj_writer(std::ostream& out);
    ~obj_writer();

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

    // 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
46
template <class ScalarT>
47
48
49
50
51
52
53
struct obj_reader
{
    obj_reader(std::string const& filename, Mesh& mesh);
    obj_reader(std::istream& in, Mesh& mesh);

    // get properties of the obj
public:
54
55
56
    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; }
57

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

61
62
63
private:
    void parse(std::istream& in, Mesh& mesh);

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

    int n_error_faces = 0;
69
};
70
} // namespace polymesh