File: Parameterization_mesh_feature_extractor.h

package info (click to toggle)
cgal 3.2.1-2
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 47,752 kB
  • ctags: 72,510
  • sloc: cpp: 397,707; ansic: 10,393; sh: 4,232; makefile: 3,713; perl: 394; sed: 9
file content (386 lines) | stat: -rw-r--r-- 12,491 bytes parent folder | download
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Copyright (c) 2005  INRIA (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org); you may redistribute it under
// the terms of the Q Public License version 1.0.
// See the file LICENSE.QPL distributed with CGAL.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.2-branch/Surface_mesh_parameterization/include/CGAL/Parameterization_mesh_feature_extractor.h $
// $Id: Parameterization_mesh_feature_extractor.h 29020 2006-03-03 09:33:29Z lsaboret $
// 
//
// Author(s)     : Laurent Saboret, Pierre Alliez, Bruno Levy

#ifndef CGAL_PARAMETERIZATION_MESH_FEATURE_EXTRACTOR_H
#define CGAL_PARAMETERIZATION_MESH_FEATURE_EXTRACTOR_H

#include <CGAL/basic.h>

#include <list>
#include <vector>

CGAL_BEGIN_NAMESPACE


/// The class Parameterization_mesh_feature_extractor
/// computes features (genus, borders, ...) of a 3D surface,
/// model of the ParameterizationMesh_3 concept.

template<class ParameterizationMesh_3>      //< 3D surface
class Parameterization_mesh_feature_extractor
{
// Public types
public:
    /// Export ParameterizationMesh_3 template parameter.
    typedef ParameterizationMesh_3          Adaptor;

    /// Type representing a border = STL container of vertex handles.
    typedef std::list<typename Adaptor::Vertex_handle>
                                            Border;
    /// Type representing the list of all borders of the mesh
    /// = STL container of Border elements.
    typedef std::vector<Border*>            Skeleton;

// Private types
private:
    // Mesh_Adaptor_3 subtypes:
    typedef typename Adaptor::NT            NT;
    typedef typename Adaptor::Point_2       Point_2;
    typedef typename Adaptor::Point_3       Point_3;
    typedef typename Adaptor::Vector_2      Vector_2;
    typedef typename Adaptor::Vector_3      Vector_3;
    typedef typename Adaptor::Facet         Facet;
    typedef typename Adaptor::Facet_handle  Facet_handle;
    typedef typename Adaptor::Facet_const_handle
                                            Facet_const_handle;
    typedef typename Adaptor::Facet_iterator Facet_iterator;
    typedef typename Adaptor::Facet_const_iterator
                                            Facet_const_iterator;
    typedef typename Adaptor::Vertex        Vertex;
    typedef typename Adaptor::Vertex_handle Vertex_handle;
    typedef typename Adaptor::Vertex_const_handle
                                            Vertex_const_handle;
    typedef typename Adaptor::Vertex_iterator Vertex_iterator;
    typedef typename Adaptor::Vertex_const_iterator
                                            Vertex_const_iterator;
    typedef typename Adaptor::Border_vertex_iterator
                                            Border_vertex_iterator;
    typedef typename Adaptor::Border_vertex_const_iterator
                                            Border_vertex_const_iterator;
    typedef typename Adaptor::Vertex_around_facet_circulator
                                            Vertex_around_facet_circulator;
    typedef typename Adaptor::Vertex_around_facet_const_circulator
                                            Vertex_around_facet_const_circulator;
    typedef typename Adaptor::Vertex_around_vertex_circulator
                                            Vertex_around_vertex_circulator;
    typedef typename Adaptor::Vertex_around_vertex_const_circulator
                                            Vertex_around_vertex_const_circulator;

// Public operations
public:

    /// Constructor.
    ///
    /// CAUTION: This class caches the result of feature extractions
    /// => The caller must NOT modify 'mesh' during the
    /// Parameterization_mesh_feature_extractor life cycle.
    Parameterization_mesh_feature_extractor(Adaptor& mesh)
        // Store reference to adapted mesh 
      : m_mesh_adaptor(mesh)
    {
        // m_mesh_adaptor features are not yet computed
        m_nb_connex_components = -1;
        m_nb_borders = -1;
        m_genus = -1;
    }
    virtual ~Parameterization_mesh_feature_extractor() {}

    /// Get number of borders.
    int get_nb_borders()
    {
        // At first call, extract borders and put longest one first
        if (m_nb_borders == -1)
            extract_borders();

        return m_nb_borders;
    }
    /// Get extracted borders.
    /// The longest border is the first one.
    const Skeleton& get_borders()
    {
        // At first call, extract borders and put longest one first
        if (m_nb_borders == -1)
            extract_borders();

        return m_skeleton;
    }
    /// Get longest border.
    const Border& get_longest_border()
    {
        // At first call, extract borders and put longest one first
        if (m_nb_borders == -1)
            extract_borders();

        return *(m_skeleton[0]);
    }

    /// Get # of connected components.
    int get_nb_connex_components()
    {
        // At first call, count the number of connex components
        if (m_nb_connex_components == -1)
            count_connex_components();

        return m_nb_connex_components;
    }

    /// Get the genus.
    int get_genus()
    {
        // At first call, compute the genus
        if (m_genus == -1)
            compute_genus();

        return m_genus;
    }

// Private operations
private:

    /// Extract borders and put longest one first.
    /// Result is in m_nb_borders and m_skeleton.
    void extract_borders()
    {
        assert(m_skeleton.size() == 0);

        // Tag all vertices as unprocessed
        const int tag_free = 0;
        const int tag_done = 1;
        for (Vertex_iterator it = m_mesh_adaptor.mesh_vertices_begin();
             it != m_mesh_adaptor.mesh_vertices_end();
             it++)
        {
             m_mesh_adaptor.set_vertex_tag(it, tag_free);
        }

        // find all closed borders
        while (add_border(tag_free,tag_done)) {}

        // #borders
        m_nb_borders = m_skeleton.size();

        // put longest border first if required
        if (m_nb_borders>1)
        {
            int index = get_index_longest_border();
            Border *tmp = m_skeleton[0];
            m_skeleton[0] = m_skeleton[index];
            m_skeleton[index] = tmp;
        }
    }

    /// Add closed border.
    bool add_border(int tag_free, int tag_done)
    {
        // Find a border tagged as "free" and tag it as "processed"
        // Return an empty list if not found
        std::list<Vertex_handle> border = find_free_border(tag_free, tag_done);
        if(border.empty())
            return false;

        // add one border to list
        Border *pNewBorder = new Border;
        *pNewBorder = border;
        m_skeleton.push_back(pNewBorder);

        return true;
    }

    /// Find a border tagged as "free" and tag it as "processed".
    /// Return an empty list if not found.
    std::list<Vertex_handle> find_free_border(int tag_free, int tag_done)
    {
        std::list<Vertex_handle> border;    // returned list

        // get any border vertex with "free" tag
        Vertex_handle seed_vertex = NULL;
        for (Vertex_iterator pVertex = m_mesh_adaptor.mesh_vertices_begin();
             pVertex != m_mesh_adaptor.mesh_vertices_end();
             pVertex++)
        {
            if (m_mesh_adaptor.is_vertex_on_border(pVertex) &&
                m_mesh_adaptor.get_vertex_tag(pVertex) == tag_free)
            {
                seed_vertex = pVertex;
                break;
            }
        }
        if (seed_vertex == NULL)
            return border;                  // return empty list

        // Get the border containing seed_vertex
        border = m_mesh_adaptor.get_border(seed_vertex);

        // Tag border vertices as "processed"
        typename std::list<Vertex_handle>::iterator it;
        for(it = border.begin(); it != border.end(); it++)
            m_mesh_adaptor.set_vertex_tag(*it, tag_done);

        return border;
    }

    /// Get index of the longest border.
    int get_index_longest_border() const
    {
        int index = 0;
        double max = 0.0;

        // #borders
        int nb = m_skeleton.size();

        for(int i=0;i<nb;i++)
        {
            const Border *pBorder = m_skeleton[i];
            double length = len(*pBorder);
            if (length > max)
            {
                index = i;
                max = length;
            }
        }

        return index;
    }

    /// Compute  total len of a border.
    double len(const Border& border) const
    {
        double len = 0.0;
        typename std::list<typename Adaptor::Vertex_handle>::const_iterator it;
        for(it = border.begin(); it != border.end(); it++)
        {
            // Get next iterator (looping)
            typename std::list<typename Adaptor::Vertex_handle>::const_iterator next = it;
            next++;
            if (next == border.end())
                next = border.begin();

            Vector_3 v = m_mesh_adaptor.get_vertex_position(*next)
                       - m_mesh_adaptor.get_vertex_position(*it);
            len += std::sqrt(v*v);
        }
        return len;
    }

    /// Count # of connected components.
    /// Result is in m_nb_connex_components.
    void count_connex_components()
    {
        m_nb_connex_components = 0;

        const int tag_free = 0;
        const int tag_done = 1;
        for (Vertex_iterator it = m_mesh_adaptor.mesh_vertices_begin();
             it != m_mesh_adaptor.mesh_vertices_end();
             it++)
        {
             m_mesh_adaptor.set_vertex_tag(it, tag_free);
        }

        Vertex_handle seed_vertex = NULL;
        while((seed_vertex = get_any_vertex_tag(tag_free)) != NULL)
        {
            m_nb_connex_components++;
            tag_component(seed_vertex, tag_free, tag_done);
        }
    }

    /// Get any vertex with tag.
    Vertex_handle get_any_vertex_tag(int tag)
    {
        for (Vertex_iterator it = m_mesh_adaptor.mesh_vertices_begin();
             it != m_mesh_adaptor.mesh_vertices_end();
             it++)
        {
            if (m_mesh_adaptor.get_vertex_tag(it) == tag)
                return it;
        }

        return NULL;
    }

    /// Tag component.
    void tag_component(Vertex_handle pSeedVertex,
                       const int tag_free,
                       const int tag_done)
    {
        assert(m_mesh_adaptor.get_vertex_tag(pSeedVertex) == tag_free);

        std::list<Vertex_handle> vertices;
        vertices.push_front(pSeedVertex);

        while (!vertices.empty())
        {
            Vertex_handle pVertex = vertices.front();
            vertices.pop_front();

            // Stop if already done
            if (m_mesh_adaptor.get_vertex_tag(pVertex) == tag_done)
                continue;

            m_mesh_adaptor.set_vertex_tag(pVertex, tag_done);

            Vertex_around_vertex_circulator cir, cir_end;
            cir     = m_mesh_adaptor.vertices_around_vertex_begin(pVertex);
            cir_end = cir;
            CGAL_For_all(cir,cir_end)
                if (m_mesh_adaptor.get_vertex_tag(cir) == tag_free)
                    vertices.push_front(cir);
        }
    }

    /// Compute the genus. Result is in m_genus.
    ///
    /// Implementation note:
    ///  G = (2*C + E - B - F - V)/2 with
    ///  G : genus
    ///  C : # of connected components
    ///  E : # of edges
    ///  B : # of borders
    ///  F : # of facets
    ///  V : # of vertices
    void compute_genus()
    {
        int c = get_nb_connex_components();
        int b = get_nb_borders();
        int v = m_mesh_adaptor.count_mesh_vertices();
        int e = m_mesh_adaptor.count_mesh_halfedges()/2;
        int f = m_mesh_adaptor.count_mesh_facets();

        m_genus = (2*c+e-b-f-v)/2;
    }

// Fields
private:

    /// Pointer to mesh to parse
    Adaptor&    m_mesh_adaptor;

    /// m_mesh_adaptor features:
    int         m_nb_borders;
    Skeleton    m_skeleton;             ///< List of borders of m_mesh_adaptor
    int         m_nb_connex_components;
    int         m_genus;

}; // Parameterization_mesh_feature_extractor


CGAL_END_NAMESPACE

#endif // CGAL_SURFACE_MESH_PARAMETERIZATION_MESH_FEATURE_EXTRACTOR_H