514 |
s042372 |
1 |
/* ----------------------------------------------------------------------- *
|
572 |
jab |
2 |
* This file is part of GEL, http://www.imm.dtu.dk/GEL
|
|
|
3 |
* Copyright (C) the authors and DTU Informatics
|
|
|
4 |
* For license and list of authors, see ../../doc/intro.pdf
|
|
|
5 |
* ----------------------------------------------------------------------- */
|
514 |
s042372 |
6 |
|
|
|
7 |
#include "curvature.h"
|
|
|
8 |
|
|
|
9 |
#include <iostream>
|
601 |
jab |
10 |
#include "../CGLA/eigensolution.h"
|
|
|
11 |
#include "../CGLA/Vec2d.h"
|
|
|
12 |
#include "../CGLA/Vec3d.h"
|
|
|
13 |
#include "../CGLA/Mat3x3d.h"
|
|
|
14 |
#include "../CGLA/Mat2x2d.h"
|
|
|
15 |
#include "../CGLA/Mat2x3d.h"
|
514 |
s042372 |
16 |
|
601 |
jab |
17 |
#include "Manifold.h"
|
|
|
18 |
#include "AttributeVector.h"
|
|
|
19 |
#include "x3d_save.h"
|
|
|
20 |
#include "x3d_load.h"
|
|
|
21 |
#include "obj_load.h"
|
|
|
22 |
#include "mesh_optimization.h"
|
514 |
s042372 |
23 |
|
601 |
jab |
24 |
#include "../LinAlg/Matrix.h"
|
|
|
25 |
#include "../LinAlg/Vector.h"
|
|
|
26 |
#include "../LinAlg/LapackFunc.h"
|
514 |
s042372 |
27 |
|
|
|
28 |
using namespace std;
|
|
|
29 |
|
|
|
30 |
using namespace LinAlg;
|
|
|
31 |
using namespace CGLA;
|
|
|
32 |
using namespace HMesh;
|
|
|
33 |
|
|
|
34 |
namespace HMesh
|
|
|
35 |
{
|
|
|
36 |
namespace
|
|
|
37 |
{
|
|
|
38 |
//double scal = 0.001;
|
|
|
39 |
//double vector_scal = 0.001;
|
|
|
40 |
|
|
|
41 |
template<class T>
|
|
|
42 |
void smooth_something_on_mesh(const Manifold& m, VertexAttributeVector<T>& vec, int smooth_steps)
|
|
|
43 |
{
|
|
|
44 |
for(int iter=0;iter<smooth_steps;++iter){
|
586 |
jab |
45 |
VertexAttributeVector<T> new_vec(m.allocated_vertices());
|
514 |
s042372 |
46 |
for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
|
|
|
47 |
new_vec[*v] = vec[*v];
|
587 |
jab |
48 |
for(Walker w = m.walker(*v); !w.full_circle(); w = w.circulate_vertex_cw()){
|
514 |
s042372 |
49 |
new_vec[*v] += vec[w.vertex()];
|
|
|
50 |
}
|
|
|
51 |
new_vec[*v] /= (valency(m, *v) + 1.0);
|
|
|
52 |
}
|
|
|
53 |
swap(vec,new_vec);
|
|
|
54 |
}
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
double voronoi_area(const Manifold& m, VertexID v)
|
|
|
59 |
{
|
|
|
60 |
double area_mixed = 0;
|
|
|
61 |
//For each triangle T from the 1-ring neighborhood of x
|
587 |
jab |
62 |
for(Walker w = m.walker(v); !w.full_circle(); w = w.circulate_vertex_cw()){
|
514 |
s042372 |
63 |
double f_area = area(m, w.face());
|
|
|
64 |
|
566 |
jab |
65 |
Vec3d v0(m.pos(v));
|
514 |
s042372 |
66 |
Vec3d v1(m.pos(w.vertex()));
|
|
|
67 |
Vec3d v2(m.pos(w.next().vertex()));
|
|
|
68 |
|
|
|
69 |
double a0 = acos(dot(v1-v0, v2-v0)/(length(v1-v0)*length(v2-v0)));
|
|
|
70 |
double a1 = acos(dot(v2-v1, v0-v1)/(length(v2-v1)*length(v0-v1)));
|
|
|
71 |
double a2 = acos(dot(v0-v2, v1-v2)/(length(v0-v2)*length(v1-v2)));
|
|
|
72 |
|
|
|
73 |
if(a0>(M_PI/2.0) && a1>(M_PI/2.0) && a2>(M_PI/2.0)) // f is non-obtuse
|
|
|
74 |
{
|
|
|
75 |
// Add Voronoi formula (see Section 3.3)
|
|
|
76 |
area_mixed += (1.0/8) *
|
|
|
77 |
((1.0/tan(a1)) * sqr_length(v2-v0) +
|
|
|
78 |
(1.0/tan(a2)) * sqr_length(v1-v0));
|
|
|
79 |
}
|
|
|
80 |
else // Voronoi inappropriate
|
|
|
81 |
{
|
|
|
82 |
// Add either area(f)/4 or area(f)/2
|
|
|
83 |
if(a0>M_PI/2.0)// the angle of f at x is obtuse
|
|
|
84 |
area_mixed += f_area/2;
|
|
|
85 |
else
|
|
|
86 |
area_mixed += f_area/4;
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
return area_mixed;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
double barycentric_area(const Manifold& m, VertexID v)
|
|
|
93 |
{
|
|
|
94 |
double barea = 0;
|
|
|
95 |
//For each triangle T from the 1-ring neighborhood of x
|
587 |
jab |
96 |
for(Walker w = m.walker(v); !w.full_circle(); w = w.circulate_vertex_cw()){
|
514 |
s042372 |
97 |
barea += area(m, w.face())/3.0;
|
|
|
98 |
}
|
|
|
99 |
return barea;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
void unnormalized_mean_curvature_normal(const Manifold& m, VertexID v, Vec3d& curv_normal, double& w_sum)
|
|
|
103 |
{
|
|
|
104 |
if(boundary(m, v))
|
|
|
105 |
return;
|
|
|
106 |
|
|
|
107 |
Vec3d vertex(m.pos(v));
|
|
|
108 |
curv_normal = Vec3d(0);
|
|
|
109 |
w_sum = 0;
|
587 |
jab |
110 |
for(Walker walker = m.walker(v); !walker.full_circle(); walker = walker.circulate_vertex_ccw()){
|
514 |
s042372 |
111 |
Vec3d nbr(m.pos(walker.vertex()));
|
|
|
112 |
Vec3d left(m.pos(walker.next().vertex()));
|
566 |
jab |
113 |
Vec3d right(m.pos(walker.opp().next().vertex()));
|
514 |
s042372 |
114 |
|
566 |
jab |
115 |
double d_left = dot(cond_normalize(nbr-left),cond_normalize(vertex-left));
|
|
|
116 |
double d_right = dot(cond_normalize(nbr-right),cond_normalize(vertex-right));
|
514 |
s042372 |
117 |
double a_left = acos(min(1.0, max(-1.0, d_left)));
|
|
|
118 |
double a_right = acos(min(1.0, max(-1.0, d_right)));
|
|
|
119 |
|
566 |
jab |
120 |
double w = 1.0/(1e-300+tan(a_left));
|
|
|
121 |
w += 1.0/(1e-300+tan(a_right));
|
|
|
122 |
// double w = sin(a_left + a_right) / (1e-300 + sin(a_left)*sin(a_right));
|
|
|
123 |
curv_normal += w * (nbr-vertex);
|
514 |
s042372 |
124 |
w_sum += w;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
Vec3d mean_curvature_normal(const Manifold& m, VertexID v)
|
|
|
130 |
{
|
|
|
131 |
Vec3d curv_normal;
|
|
|
132 |
double w_sum;
|
|
|
133 |
unnormalized_mean_curvature_normal(m, v, curv_normal, w_sum);
|
|
|
134 |
|
|
|
135 |
return curv_normal / (4*voronoi_area(m, v));
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
double sum_curvatures(const Manifold& m, VertexAttributeVector<double>& curvature)
|
|
|
139 |
{
|
|
|
140 |
double sum = 0;
|
|
|
141 |
for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
|
|
|
142 |
if(boundary(m, *v))
|
|
|
143 |
continue;
|
|
|
144 |
sum += curvature[*v] * voronoi_area(m, *v);
|
|
|
145 |
}
|
|
|
146 |
return sum;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
double gaussian_curvature_angle_defect(const Manifold& m, VertexID v)
|
|
|
151 |
{
|
|
|
152 |
if(boundary(m, v))
|
|
|
153 |
return 0;
|
|
|
154 |
|
587 |
jab |
155 |
Vec3d vertex(m.pos(v));
|
514 |
s042372 |
156 |
vector<Vec3d> edges;
|
587 |
jab |
157 |
for(Walker w = m.walker(v); !w.full_circle(); w = w.circulate_vertex_cw()){
|
514 |
s042372 |
158 |
Vec3d e(normalize(m.pos(w.vertex()) - vertex));
|
|
|
159 |
edges.push_back(e);
|
|
|
160 |
}
|
585 |
jab |
161 |
size_t N=edges.size();
|
514 |
s042372 |
162 |
double angle_sum = 0;
|
607 |
jrf |
163 |
for(size_t i = 0; i < N; ++i)
|
514 |
s042372 |
164 |
{
|
|
|
165 |
double dot_prod =
|
632 |
janba |
166 |
std::max(-1.0, std::min(1.0, dot(edges[i],edges[(i+1)%N])));
|
514 |
s042372 |
167 |
angle_sum += acos(dot_prod);
|
|
|
168 |
}
|
|
|
169 |
return (2*M_PI - angle_sum)/voronoi_area(m, v);
|
|
|
170 |
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
Mat3x3d curvature_tensor(const Manifold& m, HalfEdgeID h)
|
|
|
174 |
{
|
|
|
175 |
if(boundary(m, h))
|
|
|
176 |
return Mat3x3d(0);
|
|
|
177 |
|
587 |
jab |
178 |
Walker w = m.walker(h);
|
514 |
s042372 |
179 |
Vec3d edge(m.pos(w.vertex()) - m.pos(w.opp().vertex()));
|
|
|
180 |
double edge_len = length(edge);
|
|
|
181 |
edge /= edge_len;
|
|
|
182 |
|
|
|
183 |
Vec3d h_norm(normal(m, w.face()));
|
|
|
184 |
Vec3d h_opp_norm(normal(m, w.opp().face()));
|
|
|
185 |
|
|
|
186 |
Vec3d nc = cross(h_norm, h_opp_norm);
|
|
|
187 |
|
|
|
188 |
double sign = (dot(nc, edge) >= 0) ? 1 : -1;
|
|
|
189 |
double beta = asin(nc.length());
|
|
|
190 |
|
|
|
191 |
Mat3x3d mat;
|
|
|
192 |
outer_product(edge, edge, mat);
|
|
|
193 |
return sign * edge_len * beta * mat;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
Mat3x3d curvature_tensor_from_edges(const Manifold& m, VertexID v)
|
|
|
197 |
{
|
|
|
198 |
Mat3x3d curv_tensor(0);
|
|
|
199 |
|
|
|
200 |
if(boundary(m, v))
|
|
|
201 |
return curv_tensor;
|
|
|
202 |
|
587 |
jab |
203 |
for(Walker w = m.walker(v); !w.full_circle(); w = w.circulate_vertex_cw())
|
514 |
s042372 |
204 |
curv_tensor += 0.5*curvature_tensor(m, w.halfedge());
|
|
|
205 |
|
|
|
206 |
curv_tensor /= voronoi_area(m, v);
|
|
|
207 |
|
|
|
208 |
return curv_tensor;
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
|
|
|
212 |
void curvature_tensor_paraboloid(const Manifold& m, VertexID v, Mat2x2d& curv_tensor, Mat3x3d& frame)
|
|
|
213 |
{
|
|
|
214 |
if(boundary(m, v))
|
|
|
215 |
return;
|
|
|
216 |
// First estimate the normal and compute a transformation matrix
|
|
|
217 |
// which takes us into tangent plane coordinates.
|
|
|
218 |
Vec3d Norm = Vec3d(normal(m, v));
|
|
|
219 |
Vec3d X,Y;
|
|
|
220 |
orthogonal(Norm,X,Y);
|
|
|
221 |
frame = Mat3x3d(X,Y,Norm);
|
|
|
222 |
Vec3d centre(m.pos(v));
|
|
|
223 |
|
|
|
224 |
vector<Vec3d> points;
|
587 |
jab |
225 |
for(Walker w = m.walker(v); !w.full_circle(); w = w.circulate_vertex_cw())
|
514 |
s042372 |
226 |
points.push_back(Vec3d(m.pos(w.vertex())));
|
|
|
227 |
|
591 |
jab |
228 |
int N = int(points.size());
|
514 |
s042372 |
229 |
|
|
|
230 |
CVector b(N);
|
|
|
231 |
// Compute the matrix of parameter values
|
|
|
232 |
CMatrix PMat(N, 3);
|
|
|
233 |
for(int i = 0; i < N; ++i){
|
|
|
234 |
Vec3d p = frame * (points[i]-centre);
|
|
|
235 |
b[i] = p[2];
|
|
|
236 |
|
|
|
237 |
PMat.set(i,0,0.5*sqr(p[0]));
|
|
|
238 |
PMat.set(i,1,p[0]*p[1]);
|
|
|
239 |
PMat.set(i,2,0.5*sqr(p[1]));
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
// Compute the coefficients of the polynomial surface
|
|
|
243 |
CVector x(3);
|
|
|
244 |
x = LinearLSSolve(PMat,b);
|
|
|
245 |
if(isnan(x[0])) cout << __LINE__ << " " << PMat << b << endl ;
|
|
|
246 |
|
|
|
247 |
// Finally compute the shape tensor from the coefficients
|
|
|
248 |
// using the first and second fundamental forms.
|
646 |
janba |
249 |
curv_tensor = - Mat2x2d(x[0],x[1],x[1],x[2]);
|
514 |
s042372 |
250 |
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
void curvature_tensors_from_edges(const Manifold& m, VertexAttributeVector<Mat3x3d>& curvature_tensors)
|
|
|
254 |
{
|
|
|
255 |
for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v)
|
|
|
256 |
curvature_tensors[*v] = curvature_tensor_from_edges(m, *v);
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
void smooth_curvature_tensors(const Manifold& m, VertexAttributeVector<Mat3x3d>& curvature_tensors)
|
|
|
260 |
{
|
586 |
jab |
261 |
assert(curvature_tensors.size() == m.allocated_vertices());
|
|
|
262 |
VertexAttributeVector<Mat3x3d> tmp_curvature_tensors(m.allocated_vertices());
|
514 |
s042372 |
263 |
double tmp_area;
|
|
|
264 |
|
|
|
265 |
for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
|
|
|
266 |
if(boundary(m, *v))
|
|
|
267 |
continue;
|
|
|
268 |
double a = voronoi_area(m, *v);
|
|
|
269 |
tmp_curvature_tensors[*v] = curvature_tensors[*v] * a;
|
|
|
270 |
tmp_area = a;
|
587 |
jab |
271 |
for(Walker w = m.walker(*v); !w.full_circle(); w = w.circulate_vertex_cw()){
|
514 |
s042372 |
272 |
if(!boundary(m, w.vertex())){
|
|
|
273 |
double a = voronoi_area(m, w.vertex());
|
|
|
274 |
tmp_curvature_tensors[*v] += curvature_tensors[w.vertex()]*a;
|
|
|
275 |
tmp_area += a;
|
|
|
276 |
}
|
|
|
277 |
tmp_curvature_tensors[*v] /= tmp_area;
|
|
|
278 |
}
|
|
|
279 |
}
|
631 |
janba |
280 |
curvature_tensors = move(tmp_curvature_tensors);
|
514 |
s042372 |
281 |
}
|
|
|
282 |
|
|
|
283 |
void gaussian_curvature_angle_defects(const Manifold& m, VertexAttributeVector<double>& curvature, int smooth_steps)
|
|
|
284 |
{
|
|
|
285 |
for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v)
|
|
|
286 |
curvature[*v] = gaussian_curvature_angle_defect(m, *v);
|
|
|
287 |
|
|
|
288 |
smooth_something_on_mesh(m, curvature, smooth_steps);
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
void mean_curvatures(const Manifold& m, VertexAttributeVector<double>& curvature, int smooth_steps)
|
|
|
292 |
{
|
543 |
jab |
293 |
for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v)
|
|
|
294 |
if(!boundary(m,*v))
|
|
|
295 |
{
|
566 |
jab |
296 |
Vec3d N = -mean_curvature_normal(m, *v);
|
543 |
jab |
297 |
curvature[*v] = length(N) * sign(dot(N,Vec3d(normal(m, *v))));
|
|
|
298 |
}
|
514 |
s042372 |
299 |
smooth_something_on_mesh(m, curvature, smooth_steps);
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
|
|
|
303 |
void curvature_paraboloids( const Manifold& m,
|
|
|
304 |
VertexAttributeVector<Vec3d>& min_curv_direction,
|
|
|
305 |
VertexAttributeVector<Vec3d>& max_curv_direction,
|
646 |
janba |
306 |
VertexAttributeVector<Vec2d>& curvature)
|
514 |
s042372 |
307 |
{
|
|
|
308 |
|
|
|
309 |
for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
|
|
|
310 |
Mat2x2d tensor;
|
|
|
311 |
Mat3x3d frame;
|
|
|
312 |
curvature_tensor_paraboloid(m, *v, tensor, frame);
|
|
|
313 |
|
|
|
314 |
Mat2x2d Q,L;
|
|
|
315 |
int s = power_eigensolution(tensor, Q, L);
|
|
|
316 |
|
|
|
317 |
if(s < 2)
|
|
|
318 |
cout << tensor << Q << L << endl;
|
|
|
319 |
|
|
|
320 |
int max_idx = 0;
|
|
|
321 |
int min_idx = 1;
|
|
|
322 |
|
646 |
janba |
323 |
if(L[max_idx][max_idx]<L[min_idx][min_idx]) swap(max_idx, min_idx);
|
514 |
s042372 |
324 |
|
|
|
325 |
Mat3x3d frame_t = transpose(frame);
|
|
|
326 |
|
566 |
jab |
327 |
max_curv_direction[*v] = cond_normalize(frame_t * Vec3d(Q[max_idx][0], Q[max_idx][1], 0));
|
514 |
s042372 |
328 |
|
566 |
jab |
329 |
min_curv_direction[*v] = cond_normalize(frame_t * Vec3d(Q[min_idx][0], Q[min_idx][1], 0));
|
514 |
s042372 |
330 |
|
646 |
janba |
331 |
curvature[*v][0] = L[min_idx][min_idx];
|
|
|
332 |
curvature[*v][1] = L[max_idx][max_idx];
|
514 |
s042372 |
333 |
}
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
|
|
|
337 |
void curvature_from_tensors(const Manifold& m,
|
|
|
338 |
const VertexAttributeVector<Mat3x3d>& curvature_tensors,
|
|
|
339 |
VertexAttributeVector<Vec3d>& min_curv_direction,
|
|
|
340 |
VertexAttributeVector<Vec3d>& max_curv_direction,
|
646 |
janba |
341 |
VertexAttributeVector<Vec2d>& curvature)
|
514 |
s042372 |
342 |
{
|
586 |
jab |
343 |
assert(curvature_tensors.size() == m.allocated_vertices());
|
514 |
s042372 |
344 |
|
|
|
345 |
double max_val = -1e30;
|
|
|
346 |
for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
|
|
|
347 |
Mat3x3d C,Q,L;
|
|
|
348 |
C = curvature_tensors[*v];
|
|
|
349 |
int s = power_eigensolution(C, Q, L);
|
|
|
350 |
Vec3d dmin, dmax;
|
|
|
351 |
if(s == 0)
|
|
|
352 |
{
|
|
|
353 |
Vec3d n(normal(m, *v));
|
|
|
354 |
orthogonal(n, dmin, dmax);
|
646 |
janba |
355 |
curvature[*v] = Vec2d(0);
|
514 |
s042372 |
356 |
cout << " rank 0 " << endl;
|
|
|
357 |
}
|
|
|
358 |
else if(s == 1)
|
|
|
359 |
{
|
|
|
360 |
Vec3d n(normal(m, *v));
|
|
|
361 |
dmin = normalize(Q[0]);
|
|
|
362 |
dmax = cross(n, dmin);
|
646 |
janba |
363 |
curvature[*v] = Vec2d(0);
|
514 |
s042372 |
364 |
cout << " rank 1 " << endl;
|
|
|
365 |
}
|
|
|
366 |
else
|
|
|
367 |
{
|
646 |
janba |
368 |
Vec2d l(fabs(L[0][0]), fabs(L[1][1]));
|
514 |
s042372 |
369 |
|
646 |
janba |
370 |
int max_idx = 0;
|
|
|
371 |
int min_idx = 1;
|
514 |
s042372 |
372 |
|
|
|
373 |
if(l[max_idx] < l[min_idx]) swap(max_idx, min_idx);
|
646 |
janba |
374 |
|
514 |
s042372 |
375 |
// Yes - the biggest eigenvalue corresponds to the min direction
|
|
|
376 |
// and vice versa.
|
|
|
377 |
dmin = normalize(Q[max_idx]);
|
|
|
378 |
dmax = normalize(Q[min_idx]);
|
|
|
379 |
|
646 |
janba |
380 |
curvature[*v][0] = L[min_idx][min_idx];
|
|
|
381 |
curvature[*v][1] = L[max_idx][max_idx];
|
514 |
s042372 |
382 |
|
|
|
383 |
}
|
|
|
384 |
min_curv_direction[*v] = dmin;
|
|
|
385 |
max_curv_direction[*v] = dmax;
|
646 |
janba |
386 |
max_val = max(fabs(curvature[*v][1]), max_val);
|
514 |
s042372 |
387 |
|
|
|
388 |
}
|
|
|
389 |
//scal = 1.0/max_val;
|
|
|
390 |
}
|
|
|
391 |
}
|
|
|
392 |
|