-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmoothing.cc
More file actions
212 lines (172 loc) · 8.5 KB
/
Smoothing.cc
File metadata and controls
212 lines (172 loc) · 8.5 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "Smoothing.hh"
double constant_edge_weight(TriMesh& mesh_, OpenMesh::EPropHandleT<double> edge_weight_, TriMesh::EdgeHandle eh) {
/*
Function for the constant weight.
*/
return 1.0;
}
double cotan_edge_weight(TriMesh& mesh_, OpenMesh::EPropHandleT<double> edge_weight_, TriMesh::EdgeHandle eh) {
/*
Function for the constant weight.
*/
return mesh_.property(edge_weight_, eh);
}
double area_vertex_weight(TriMesh& mesh_, OpenMesh::VPropHandleT<double> vertex_weight_, TriMesh::VertexHandle vh) {
/*
Function for the constant weight.
*/
return mesh_.property(vertex_weight_, vh);;
}
Eigen::SparseMatrix<double> Smoothing::get_M_matrix(double (*edge_weight)(TriMesh& mesh_, OpenMesh::EPropHandleT<double> edge_weight_, TriMesh::EdgeHandle eh)){
Eigen::SparseMatrix<double> M(mesh_.n_vertices(), mesh_.n_vertices());
std::vector<Eigen::Triplet<double>> tripletList2;
tripletList2.reserve(7*mesh_.n_vertices());
for (TriMesh::VertexIter v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it) {
double total = 0.0;
TriMesh::VertexFaceIter vf_it = mesh_.vf_iter(*v_it);
TriMesh::VertexHandle next;
while (vf_it.is_valid()) {
TriMesh::FaceVertexIter fv_it = mesh_.fv_iter(*vf_it);
TriMesh::VertexHandle v0 = *fv_it;
TriMesh::VertexHandle v1 = *(++fv_it);
TriMesh::VertexHandle v2 = *(++fv_it);
if(v0 == *v_it) {
next = v1;
} else if (v1 == *v_it) {
next = v2;
} else {
next = v0;
}
TriMesh::HalfedgeHandle heh = mesh_.find_halfedge(*v_it, next);
if (heh.is_valid())
{
TriMesh::EdgeHandle eh = mesh_.edge_handle(heh);
double weight = edge_weight(mesh_, edge_weight_, eh);
total += weight;
tripletList2.push_back(Eigen::Triplet<double>((*v_it).idx(), next.idx(), weight));
}
++vf_it;
}
tripletList2.push_back(Eigen::Triplet<double>((*v_it).idx(), (*v_it).idx(), -total));
}
M.setFromTriplets(tripletList2.begin(), tripletList2.end());
return M;
}
Eigen::SparseMatrix<double> Smoothing::get_D_matrix(double (*vertex_weight)(TriMesh& mesh_, OpenMesh::VPropHandleT<double> vertex_weight_, TriMesh::VertexHandle vh)){
Eigen::SparseMatrix<double> D(mesh_.n_vertices(), mesh_.n_vertices());
std::vector<Eigen::Triplet<double>> tripletList;
tripletList.reserve(mesh_.n_vertices());
for (TriMesh::VertexIter v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it) {
tripletList.push_back(Eigen::Triplet<double>((*v_it).idx(), (*v_it).idx(), vertex_weight(mesh_, vertex_weight_, *v_it)));
}
D.setFromTriplets(tripletList.begin(), tripletList.end());
return D;
}
Eigen::SparseMatrix<double> Smoothing::get_inverse_D_matrix(double (*vertex_weight)(TriMesh& mesh_, OpenMesh::VPropHandleT<double> vertex_weight_, TriMesh::VertexHandle vh)){
Eigen::SparseMatrix<double> D(mesh_.n_vertices(), mesh_.n_vertices());
std::vector<Eigen::Triplet<double>> tripletList(mesh_.n_vertices());
for (TriMesh::VertexIter v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it) {
tripletList.push_back(Eigen::Triplet<double>((*v_it).idx(), (*v_it).idx(), 1/vertex_weight(mesh_, vertex_weight_, *v_it)));
}
D.setFromTriplets(tripletList.begin(), tripletList.end());
return D;
}
Eigen::MatrixXd Smoothing::get_vertices_matrix(){
Eigen::MatrixXd vertices(mesh_.n_vertices(), 3);
// Loop over all vertices and extract their (x, y, z) coordinates
for (TriMesh::VertexIter v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it)
{
Smoothing::Point p = mesh_.point(*v_it);
vertices.row(v_it->idx()) << p[0], p[1], p[2];
}
return vertices;
}
void Smoothing::set_vertices(Eigen::MatrixXd vertices){
for (TriMesh::VertexIter v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it)
{
if(!mesh_.is_boundary(*v_it)){
mesh_.set_point(*v_it, Smoothing::Point(vertices(v_it->idx(), 0), vertices(v_it->idx(), 1), vertices(v_it->idx(), 2)));
}
}
}
Eigen::MatrixXd Smoothing::explicit_smoothing(int _iterations, double constant, double (*edge_weight)(TriMesh&, OpenMesh::EPropHandleT<double>, TriMesh::EdgeHandle)) {
Eigen::MatrixXd vertices = get_vertices_matrix();
for (int iter = 0; iter < _iterations; ++iter) {
calc_edges_weights();
// TODO parallel
for (TriMesh::VertexIter v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it){
TriMesh::VertexHandle v_i = *v_it;
if(!mesh_.is_boundary(v_i)) {
double total_weight = 0;
Smoothing::Point p = Smoothing::Smoothing::Point(0);
for (TriMesh::VertexVertexIter vv_it=mesh_.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) {
TriMesh::VertexHandle v_j = *vv_it;
TriMesh::HalfedgeHandle heh = mesh_.find_halfedge(v_i, v_j);
if (heh.is_valid())
{
TriMesh::EdgeHandle eh = mesh_.edge_handle(heh);
double weight = edge_weight(mesh_, edge_weight_, eh);
total_weight += weight;
p += weight*(mesh_.point(v_j) - mesh_.point(v_i));
}
}
if (total_weight != 0) {
p *= constant/total_weight;
p += mesh_.point(v_i);
vertices.row(v_it->idx()) << p[0], p[1], p[2];
}
}
}
set_vertices(vertices);
}
mesh_.update_normals();
return vertices;
}
void Smoothing::implicit_smoothing(double timestep, double diffuse, double (*edge_weight)(TriMesh& mesh_, OpenMesh::EPropHandleT<double> edge_weight_, TriMesh::EdgeHandle eh), double (*vertex_weight)(TriMesh& mesh_, OpenMesh::VPropHandleT<double> vertex_weight_, TriMesh::VertexHandle vh)){
calc_weights();
Eigen::SparseMatrix<double> M = get_M_matrix(edge_weight);
Eigen::SparseMatrix<double> D_inv = get_inverse_D_matrix(vertex_weight);
Eigen::MatrixXd vertices = get_vertices_matrix();
Eigen::SimplicialLDLT<Eigen::SparseMatrix<double>> solver;
solver.compute(D_inv - timestep*diffuse*M);
vertices = solver.solve(D_inv*vertices);
set_vertices(vertices);
mesh_.update_normals();
}
void Smoothing::feature_enhancement(int iterations, int coeff, double diffuse, double (*edge_weight)(TriMesh& mesh_, OpenMesh::EPropHandleT<double> edge_weight_, TriMesh::EdgeHandle eh)) {
Eigen::MatrixXd p_in = get_vertices_matrix();
Eigen::MatrixXd p_out = explicit_smoothing(iterations, diffuse, edge_weight);
p_out += coeff*(p_in - p_out);
set_vertices(p_out);
mesh_.update_normals();
}
// ======================================================================
// EXERCISE 1.1
// ========================================================================
void Smoothing::uniform_smooth(const int _iterations) {
explicit_smoothing(_iterations, 0.5, &constant_edge_weight);
}
// ======================================================================
// EXERCISE 1.2
// ========================================================================
void Smoothing::cotan_laplacian_smooth(const int _iterations) {
explicit_smoothing(_iterations, 0.5, &cotan_edge_weight);
}
// ======================================================================
// EXERCISE 2
// ========================================================================
void Smoothing::implicit_smooth(const double _timestep) {
implicit_smoothing(_timestep, 0.5, &cotan_edge_weight, &area_vertex_weight);
}
// ======================================================================
// EXERCISE 3.1
// ========================================================================
void Smoothing::uniform_laplacian_enhance_feature(const int _iterations, const int _coefficient) {
feature_enhancement(_iterations, _coefficient, 0.5, &constant_edge_weight);
}
// ======================================================================
// EXERCISE 3.2
// ========================================================================
void Smoothing::cotan_laplacian_enhance_feature(const int _iterations, const int _coefficient) {
feature_enhancement(_iterations, _coefficient, 0.5, &cotan_edge_weight);
}