Subversion Repositories gelsvn

Rev

Rev 646 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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