Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
595 jab 1
/* ----------------------------------------------------------------------- *
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
 * ----------------------------------------------------------------------- */
6
 
149 jab 7
#include "smooth.h"
8
 
595 jab 9
#include <vector>
10
#include <algorithm>
601 jab 11
#include "../CGLA/Mat3x3f.h"
12
#include "../CGLA/Vec3d.h"
149 jab 13
 
595 jab 14
#include "Manifold.h"
15
#include "AttributeVector.h"
16
 
150 jab 17
namespace HMesh
149 jab 18
{
595 jab 19
    using namespace std;
20
    using namespace CGLA;
149 jab 21
 
595 jab 22
    Vec3d laplacian(const Manifold& m, VertexID v)
23
    {
24
        Vec3d avg_pos(0);
25
        int n = 0;
149 jab 26
 
595 jab 27
        for(Walker w = m.walker(v); !w.full_circle(); w = w.circulate_vertex_cw()){
28
            avg_pos += m.pos(w.vertex());
29
            ++n;
30
        }
31
        return avg_pos / n - m.pos(v);
32
    }
149 jab 33
 
595 jab 34
    void laplacian_smooth(Manifold& m, float t)
35
    {
36
        VertexAttributeVector<Vec3d> pos(m.allocated_vertices());
149 jab 37
 
595 jab 38
        for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
39
            if(!boundary(m, *v))
40
                pos[*v] =  t * laplacian(m, *v) + m.pos(*v);
41
        }
149 jab 42
 
595 jab 43
        for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
44
            if(!boundary(m, *v))
45
                m.pos(*v) = pos[*v];
46
        }
47
    }
215 jab 48
 
595 jab 49
    void taubin_smooth(Manifold& m, int max_iter)
50
    {
51
        for(int iter = 0; iter < max_iter; ++iter) {
52
            VertexAttributeVector<Vec3d> lap(m.allocated_vertices());
215 jab 53
 
595 jab 54
            for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
55
                if(!boundary(m, *v))
56
                    lap[*v] =  laplacian(m, *v);
57
            }
215 jab 58
 
595 jab 59
            for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
60
                if(!boundary(m, *v))
61
                    m.pos(*v) += (iter%2 == 0) ? 0.5  * lap[*v] : -0.52 * lap[*v];
62
            }
63
        }
64
    }
215 jab 65
 
595 jab 66
    void face_neighbourhood(Manifold& m, FaceAttributeVector<int>& touched, FaceID f, vector<Vec3d>& nbrs)
67
    {	
68
        nbrs.push_back(normal(m, f));
69
        for(Walker wf = m.walker(f); !wf.full_circle(); wf = wf.circulate_face_cw()){
70
            for(Walker wv = m.walker(wf.vertex()); !wv.full_circle(); wv = wv.circulate_vertex_cw()){
71
                FaceID fn = wv.face();
72
                if(fn != InvalidFaceID && touched[fn] != touched[f]){
73
                    Vec3d norm = normal(m, fn);
74
                    if(!isnan(sqr_length(norm)))
75
                        nbrs.push_back(norm);
76
                    else
77
                        cout << "bad normal detected" << endl;
78
                    touched[fn] = touched[f];
79
                }
80
            }
81
        }
82
    }
215 jab 83
 
84
 
85
 
595 jab 86
    Vec3d filtered_normal(Manifold& m,  FaceAttributeVector<int>& touched, FaceID f)
87
    {
88
        const float sigma = .1f;
215 jab 89
 
595 jab 90
        vector<Vec3d> norms;
91
        face_neighbourhood(m, touched, f, norms);
92
        float min_dist_sum=1e32f;
93
        long int median=-1;
215 jab 94
 
595 jab 95
        for(size_t i=0;i<norms.size();++i)
96
        {
97
            float dist_sum = 0;
98
            for(size_t j=0;j<norms.size(); ++j)
99
                dist_sum += 1.0f - dot(norms[i], norms[j]);
100
            if(dist_sum < min_dist_sum)
101
            {
102
                min_dist_sum = dist_sum;
103
                median = i;
104
            }
105
        }
106
        assert(median != -1);
107
        Vec3d median_norm = norms[median];
108
        Vec3d avg_norm(0);
109
        for(size_t i=0;i<norms.size();++i)
110
        {
111
            float w = exp((dot(median_norm, norms[i])-1)/sigma);
112
            if(w<1e-2) w = 0;
113
            avg_norm += w*norms[i];
114
        }
115
        return normalize(avg_norm);
116
    }
215 jab 117
 
595 jab 118
    void fvm_smooth(HMesh::Manifold& m, int max_iter)
119
    {
120
        for(int iter = 0;iter<max_iter; ++iter)
121
        {
122
            FaceAttributeVector<int> touched(m.allocated_faces(), -1);
123
            FaceAttributeVector<Vec3d> filtered_norms(m.allocated_faces());
124
 
125
            int i = 0;
126
            for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f,++i){
127
                touched[*f] = i;
128
                filtered_norms[*f] = filtered_normal(m, touched, *f);
129
            }
130
 
131
            VertexAttributeVector<Vec3d> vertex_positions(m.allocated_vertices());
132
 
133
            for(int iter=0;iter<20;++iter)
134
            {
135
                for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
136
                    Vec3d move(0);
137
                    for(Walker w = m.walker(*v); !w.full_circle(); w = w.circulate_vertex_cw()){
138
                        FaceID f1 = w.face();
139
                        FaceID f2 = w.opp().face();
140
                        Vec3d dir = m.pos(w.vertex()) - m.pos(*v);
141
 
142
                        if(f1 != InvalidFaceID){
143
                            Vec3d n1 = filtered_norms[f1];
144
                            move += 0.05 * n1 * dot(n1, dir);
145
                        }
146
                        if(f2 != InvalidFaceID)
147
                        {
148
                            Vec3d n2 = filtered_norms[f2];
149
                            move += 0.05 * n2 * dot(n2, dir);
150
                        }
151
                    }
152
                    vertex_positions[*v] = m.pos(*v) + move;
153
                }
154
                for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v)
155
                    m.pos(*v) = vertex_positions[*v];
156
            }
157
        }
158
    }
159
 
160
 
149 jab 161
}