forked from sp4cerat/Fast-Quadric-Mesh-Simplification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simplify.h
114 lines (91 loc) · 2.51 KB
/
Simplify.h
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
107
108
109
110
111
112
113
114
/*
* Adopted from https://github.com/sp4cerat/Fast-Quadric-Mesh-Simplification
*
* Mesh Simplification (C)2014 by Sven Forstmann in 2014, MIT License
*
*/
#ifndef SIMPLIFY_H
#define SIMPLIFY_H
#include <math.h>
#include <vector>
#include <map>
#include "core/os/os.h"
#include "core/math/vector3.h"
#include "core/reference.h"
#include "scene/resources/surface_tool.h"
#include "scene/resources/material.h"
#include "scene/resources/mesh.h"
#include "scene/3d/mesh_instance.h"
#include "scene/resources/mesh.h"
#include "scene/resources/mesh_library.h"
#include "scene/3d/visual_instance.h"
#include "scene/main/node.h"
#include "core/vector.h"
#include "symetric_matrix.h"
enum Attributes {
NONE,
NORMAL = 2,
TEXCOORD = 4,
COLOR = 8
};
struct Triangle
{
int v[3];
double err[4];
int deleted;
int dirty;
Vector3 n;
Vector3 uvs[3];
int attr;
};
struct Vertex
{
Vector3 p;
int tstart;
int tcount;
SymetricMatrix q;
int border;
};
struct VTRef
{
int tid;
int tvertex;
};
class ProceduralMesh : public Reference {
GDCLASS(ProceduralMesh, Reference)
public:
void add_triangle(Vector3 a, Vector3 b, Vector3 c);
Ref<SurfaceTool> get_surface();
//
// Main simplification function
//
// target_count : target nr. of triangles
// agressiveness : sharpness to increase the threshold.
// 5..8 are good numbers
// more iterations yield higher quality
//
void simplify_mesh(int target_count, double agressiveness = 7, bool verbose = false);
void simplify_mesh_lossless(bool verbose = false);
protected:
static void _bind_methods();
private:
std::vector<Triangle> triangles;
std::vector<Vertex> vertices;
std::vector<VTRef> refs;
std::map<Vector3, size_t> vertex_indices;
size_t get_index(Vector3 v);
// Check if a triangle flips when this edge is removed
bool flipped(Vector3 p, int i0, int i1, Vertex &v0, Vertex &v1, std::vector<int> &deleted);
void update_uvs(int i0, const Vertex &v, const Vector3 &p, std::vector<int> &deleted);
// Update triangle connections and edge error after a edge is collapsed
void update_triangles(int i0, Vertex &v, std::vector<int> &deleted, int &deleted_triangles);
// compact triangles, compute edge error and build reference list
void update_mesh(int iteration);
// Finally compact mesh before exiting
void compact_mesh();
// Error between vertex and Quadric
double vertex_error(SymetricMatrix q, double x, double y, double z);
// Error for one edge
double calculate_error(int id_v1, int id_v2, Vector3 &p_result);
};
#endif // !SIMPLIFY_H