Subversion Repositories gelsvn

Rev

Rev 631 | Details | Compare with Previous | 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
 
631 janba 7
#include "dual.h"
595 jab 8
#include "subdivision.h"
9
 
10
#include <vector>
601 jab 11
#include "../CGLA/Vec3d.h"
595 jab 12
 
13
#include "Manifold.h"
14
#include "AttributeVector.h"
15
 
16
namespace HMesh
17
{
18
    using namespace std;
19
    using namespace CGLA;
631 janba 20
 
21
    void loop_split(Manifold& m_in, Manifold& m)
22
    {
23
        if(&m != &m_in)
24
            m = m_in;
25
 
26
        VertexAttributeVector<int> vtouched(m_in.allocated_vertices(), 0);
27
 
28
        vector<HalfEdgeID> hedges;
29
        for(HalfEdgeID h : m.halfedges())
30
            if(h<m.walker(h).opp().halfedge())
31
                hedges.push_back(h);
32
 
33
        for(HalfEdgeID h : hedges)
34
            vtouched[m.split_edge(h)] = 1;
35
 
36
 
37
        vector<FaceID> faces;
38
        for(FaceID fid : m.faces())
39
            faces.push_back(fid);
40
        for(FaceID fid : faces)
41
        {
42
            Walker w = m.walker(fid);
43
 
44
            if(vtouched[w.vertex()] == 0)
45
                w = w.next();
46
 
47
            assert(vtouched[w.vertex()] == 1);
48
 
49
            FaceID f = fid;
50
            VertexID v1, orig_vert = w.vertex();
51
            w = w.next();
52
            do
53
            {
54
                VertexID v0 = w.opp().vertex();
55
                w = w.next();
56
                v1  = w.vertex();
57
                w = w.next();
58
                assert(vtouched[v0] == 1);
59
                assert(vtouched[v1] == 1);
60
                f = m.split_face_by_edge(f, v0, v1);
61
            }
62
            while (v1 != orig_vert);
63
        }
64
    }
65
 
595 jab 66
    void cc_split(Manifold& m_in, Manifold& m_out)
67
    {
68
        const int Invalid = -1;
69
 
70
        vector<Vec3d> new_points;
71
        new_points.reserve(m_in.no_vertices());
72
 
73
        VertexAttributeVector<int> vtouched(m_in.allocated_vertices(), Invalid);
74
        HalfEdgeAttributeVector<int> htouched(m_in.allocated_halfedges(), Invalid);
75
 
76
        int npsize = 0;
77
        for(VertexIDIterator v = m_in.vertices_begin(); v != m_in.vertices_end(); ++v){       
78
            vtouched[*v] = npsize;
79
            new_points.push_back(m_in.pos(*v));
80
            ++npsize;
81
        }
82
 
83
        for(HalfEdgeIDIterator h = m_in.halfedges_begin(); h != m_in.halfedges_end(); ++h){
84
            if(htouched[*h] != Invalid)
85
                continue;
86
 
87
            Walker w = m_in.walker(*h);
88
            htouched[*h] = htouched[w.opp().halfedge()] = npsize;
89
            new_points.push_back((m_in.pos(w.vertex()) + m_in.pos(w.opp().vertex())) * 0.5f);
90
            ++npsize;
91
 
92
        }
93
        vector<int> indices;
94
        vector<int> faces;
95
 
96
        for(FaceIDIterator f = m_in.faces_begin(); f != m_in.faces_end(); ++f){           
97
            for(Walker w = m_in.walker(*f); !w.full_circle(); w = w.circulate_face_cw()){
98
                indices.push_back(npsize);
99
                indices.push_back(htouched[w.halfedge()]);
100
                indices.push_back(vtouched[w.vertex()]);
101
                indices.push_back(htouched[w.next().halfedge()]);
102
                faces.push_back(4);
103
            }
104
            new_points.push_back(centre(m_in, *f));
105
            ++npsize;
106
        }
107
 
108
        m_out.clear();
109
        m_out.build(npsize, reinterpret_cast<double*>(&new_points[0]), faces.size(), &faces[0], &indices[0]);
110
    }
111
 
631 janba 112
    void root3_subdivide(Manifold& m_in, Manifold& m)
595 jab 113
    {
631 janba 114
        if(&m != &m_in)
115
            m = m_in;
116
 
117
        VertexAttributeVector<int> vtouched(m.allocated_vertices(), 0);
118
        VertexAttributeVector<Vec3d> new_pos(m.allocated_vertices(), Vec3d(0));
119
 
120
        for (VertexIDIterator vid = m.vertices_begin(); vid != m.vertices_end(); ++vid) {
121
            int v = valency(m, *vid);
122
            double beta = (4.0 - 2.0 * cos(2.0 * M_PI / v))/(9.0*v);
123
            new_pos[*vid] = (1.0 - v * beta) * m.pos(*vid);
124
            for(Walker w = m.walker(*vid); !w.full_circle(); w = w.circulate_vertex_ccw())
595 jab 125
            {
631 janba 126
                new_pos[*vid] += beta * m.pos(w.vertex());
127
            }
128
        }
129
 
130
        vector<FaceID> faces;
131
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f)
132
            faces.push_back(*f);
133
        for(int i=0;i<faces.size(); ++i)
134
            vtouched[m.split_face_by_vertex(faces[i])] = 1;
135
 
136
        for(HalfEdgeIDIterator h = m.halfedges_begin(); h != m.halfedges_end(); ++h)
137
        {
138
            Walker w = m.walker(*h);
139
 
140
            if(vtouched[w.vertex()] == 0 && vtouched[w.opp().vertex()] == 0 &&
141
                precond_flip_edge(m, *h))
142
                m.flip_edge(*h);
143
        }
144
 
145
        for (VertexIDIterator vid = m.vertices_begin(); vid != m.vertices_end(); ++vid)
146
            if(vtouched[*vid] == 0)
147
                m.pos(*vid) = new_pos[*vid];
148
    }
666 janba 149
 
150
    void rootCC_subdivide(Manifold& m_in, Manifold& m)
151
    {
152
        if(&m != &m_in)
153
            m = m_in;
154
 
155
        VertexAttributeVector<int> vtouched(m.allocated_vertices(), 0);
156
        VertexAttributeVector<Vec3d> new_pos(m.allocated_vertices(), Vec3d(0));
157
 
158
        for (VertexIDIterator vid = m.vertices_begin(); vid != m.vertices_end(); ++vid) {
159
            int v = valency(m, *vid);
160
            double beta = (4.0 - 2.0 * cos(2.0 * M_PI / v))/(9.0*v);
161
            new_pos[*vid] = (1.0 - v * beta) * m.pos(*vid);
162
            for(Walker w = m.walker(*vid); !w.full_circle(); w = w.circulate_vertex_ccw())
163
            {
164
                new_pos[*vid] += beta * m.pos(w.vertex());
165
            }
166
        }
167
 
168
        vector<FaceID> faces;
169
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f)
170
            faces.push_back(*f);
171
        for(int i=0;i<faces.size(); ++i)
172
            vtouched[m.split_face_by_vertex(faces[i])] = 1;
173
 
174
        for(HalfEdgeIDIterator h = m.halfedges_begin(); h != m.halfedges_end(); ++h)
175
        {
176
            Walker w = m.walker(*h);
177
 
178
            if(vtouched[w.vertex()] == 0 && vtouched[w.opp().vertex()] == 0)
179
                m.merge_faces(w.face(), *h);
180
        }
181
 
182
        for (VertexIDIterator vid = m.vertices_begin(); vid != m.vertices_end(); ++vid)
183
            if(vtouched[*vid] == 0)
184
                m.pos(*vid) = new_pos[*vid];
185
    }
186
 
631 janba 187
 
188
    void butterfly_subdivide(Manifold& m_in, Manifold& m)
189
    {
190
        const float S[4][6] = {{5.0/12.0, -1.0/12.0, -1.0/12.0, 0, 0, 0},
191
            {3.0/8.0, 0, -1.0/8.0, 0, 0, 0},
192
            {0.35,
193
                0.03090169943749475,
194
                -0.08090169943749474,
195
                -0.08090169943749474,
196
                0.03090169943749468,0 },
197
            {0, 1.0f/8, -1.0f/8, 0, -1.0f/8, 1.0f/8}};
198
 
199
 
200
        if(&m != &m_in)
201
            m = m_in;
202
 
203
        HalfEdgeAttributeVector<Vec3d> new_vertices_pos(m.allocated_halfedges(), Vec3d(0.0));
204
        HalfEdgeAttributeVector<int> htouched(m.allocated_halfedges(), 0);
205
        VertexAttributeVector<int> vtouched(m.allocated_vertices(), 0);
206
 
207
        for(HalfEdgeIDIterator hid = m.halfedges_begin(); hid != m.halfedges_end(); ++hid)
208
            {
209
                Walker w = m.walker(*hid);
210
                VertexID v0 = w.opp().vertex();
211
 
212
                int K = valency(m, v0);
213
                int Ko = valency(m, w.vertex());
214
                if((K==6 && Ko==6)|| K != 6)
595 jab 215
                {
631 janba 216
                    Vec3d pos((K==6 ? 1.0 : 3.0/4.0) * m.pos(v0));
217
                    for(int k=0;k<K; ++k, w = w.circulate_vertex_ccw())
218
                    {
219
                        double s = (K<=6) ? S[K-3][k]:(0.25+cos((2.0*M_PI*k)/K)+0.5*cos((4.0*M_PI*k)/K))/K;
220
                        pos += s * m.pos(w.vertex());                        
221
                    }
222
                    new_vertices_pos[*hid] = pos;
223
                    htouched[*hid] = 1;
224
                }
225
            }
226
        loop_split(m, m);
227
 
228
        for(HalfEdgeIDIterator hid = m.halfedges_begin(); hid != m.halfedges_end(); ++hid)
229
            if(htouched[*hid])
230
            {
231
                VertexID v = m.walker(*hid).opp().vertex();
232
                if(vtouched[v] == 0)
233
                    m.pos(v) =Vec3d(0);
234
                m.pos(v) += new_vertices_pos[*hid];
235
                vtouched[v]+=1;
236
            }
237
        for(VertexIDIterator vid = m.vertices_begin(); vid != m.vertices_end(); ++vid)
238
            if(vtouched[*vid])
239
                m.pos(*vid) /= vtouched[*vid];
240
    }
241
 
242
    enum Subd {QUAD_SUBD, CC_SUBD, LOOP_SUBD, TRI_SUBD};
243
 
244
    void subd_smooth(Subd subd_method, Manifold& m)
245
    {
246
 
247
        VertexAttributeVector<Vec3d> new_vertices(m.no_vertices(), Vec3d(0,0,0));
248
 
249
        for(FaceIDIterator fid = m.faces_begin(); fid != m.faces_end(); ++fid)
250
        {
251
            FaceID f = *fid;
252
            for(Walker wlk = m.walker(f); !wlk.full_circle(); wlk = wlk.next())
253
            {
254
                float val = valency(m, wlk.vertex());
255
                float A,B;
256
 
257
                switch(subd_method)
258
                {
259
                    case QUAD_SUBD:
260
                        A = 1.0f / (4.0f * val);
261
                        B = 1.0f / (4.0f * val);
262
                        break;
263
                    case CC_SUBD:
264
                        A = (1.0f-3.0f/val)	* (1.0f/val);
265
                        B = sqr(1.0f/val);
266
                        break;
267
                    case TRI_SUBD:
268
                        A = 2.0f / (8.0f * val);
269
                        B = 3.0f / (8.0f * val);
270
                        break;
271
                    case LOOP_SUBD:
272
                        float w = 5.0f/8.0f - sqr(3.0f/8.0f + 0.25 * cos(2.0f*M_PI/val));
273
                        A = (1-2*w)/val;
274
                        B = w/val;
275
                        break;
276
                }
277
                for(Walker wlk2 = m.walker(f); !wlk2.full_circle(); wlk2 = wlk2.next())
278
                {
279
                    if(wlk2.vertex()==wlk.vertex())
280
                        new_vertices[wlk.vertex()] += A * m.pos(wlk2.vertex());
595 jab 281
                    else
631 janba 282
                        new_vertices[wlk.vertex()] += B * m.pos(wlk2.vertex());
595 jab 283
                }
284
 
285
            }
286
        }
631 janba 287
        for(VertexIDIterator vid = m.vertices_begin(); vid != m.vertices_end(); ++vid)
288
            m.pos(*vid) = new_vertices[*vid];
595 jab 289
    }
290
 
631 janba 291
    void cc_smooth(Manifold& m)
292
    {
293
        subd_smooth(CC_SUBD, m);
294
    }
295
 
296
    void loop_smooth(Manifold& m)
297
    {
298
        subd_smooth(LOOP_SUBD, m);
299
    }
595 jab 300
 
631 janba 301
 
595 jab 302
}