-
Notifications
You must be signed in to change notification settings - Fork 0
/
half_edge_mesh.hpp
89 lines (73 loc) · 2.41 KB
/
half_edge_mesh.hpp
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
#ifndef HALF_EDGE_MESH_HPP
#define HALF_EDGE_MESH_HPP
#include "soup_mesh.hpp"
#include "glm/glm.hpp"
#include <functional>
#include <memory>
#include <vector>
#include <unordered_map>
#include <string>
namespace cg
{
class HalfEdgeMesh
{
public:
struct HalfEdge;
struct Face
{
HalfEdge* edge{nullptr};
};
struct Vertex
{
HalfEdge* edge{nullptr};
glm::vec3 position{0.f};
glm::vec3 normal{0.f};
glm::vec2 texture_coordinate{0.f};
};
struct HalfEdge
{
HalfEdge* next_edge{nullptr};
HalfEdge* companion_edge{nullptr};
Face* face{nullptr};
Vertex* next_vertex{nullptr};
};
/// Returns next half edge in a face loop around current->face or nullptr if one is reached.
/// Throws invalid_argument when called with nullptr.
static HalfEdge* face_loop_next(HalfEdge* current);
/// Returns next half edge in a vertex loop around current->next_vertex or nullptr if one is reached.
/// Throws invalid_argument when called with nullptr.
static HalfEdge* vertex_loop_next(HalfEdge* current);
/// Returns nullptr, if one is reached.
/// Throws when called with nullptr.
static HalfEdge* face_loop_prev(HalfEdge* current);
/// Reverse vertex loop using a full face loop.
/// Returns nullptr, if a boundary is reached.
/// Throws when called with nullptr.
static HalfEdge* vertex_loop_prev(HalfEdge* current);
/// Returns the number of vertices the given face has.
/// Throws invalid_argument when called with nullptr.
static int vertex_count(Face* face);
/// Uses multiple half edge collapses to simplify the mesh until the given proportion of edges is removed
/// or the mesh cannot be further simplified.
/// Returns the achieved proportion of removed edges.
//float half_edge_simplify(float factor);
explicit HalfEdgeMesh() = delete;
explicit HalfEdgeMesh(const SoupMesh& soup);
SoupMesh toSoupMesh() const;
private:
using EdgeKey = std::pair<Vertex*, Vertex*>;
struct Hasher
{
size_t operator()(const EdgeKey& key) const
{
size_t first = std::hash<Vertex*>{}(key.first) << (sizeof(size_t) * 4);
return first ^ std::hash<Vertex*>{}(key.second);
}
};
// HalfEdge structure
std::unordered_map<EdgeKey, std::unique_ptr<HalfEdge>, Hasher> half_edges;
std::vector<std::unique_ptr<Face>> faces;
std::vector<std::unique_ptr<Vertex>> vertices;
};
}
#endif // HALF_EDGE_MESH_HPP