ranges.hh 12.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
#pragma once

#include <cstddef>
#include <vector>

#include "iterators.hh"

namespace polymesh
{
10
11
// ================= COLLECTION =================

Philip Trettner's avatar
Philip Trettner committed
12
template <class mesh_ptr, class tag, class iterator>
Philip Trettner's avatar
Philip Trettner committed
13
struct smart_collection
14
{
Philip Trettner's avatar
Philip Trettner committed
15
    template <typename AttrT>
Philip Trettner's avatar
Philip Trettner committed
16
    using attribute = typename primitive<tag>::template attribute<AttrT>;
17

Philip Trettner's avatar
Philip Trettner committed
18
    /// Number of primitives, INCLUDING those marked for deletion
19
    /// O(1) computation
Philip Trettner's avatar
Philip Trettner committed
20
    int size() const;
Philip Trettner's avatar
Philip Trettner committed
21
22

    /// Ensures that a given number of primitives can be stored without reallocation
Philip Trettner's avatar
Philip Trettner committed
23
    void reserve(int capacity) const;
24

Philip Trettner's avatar
Philip Trettner committed
25
26
27
28
29
30
31
32
    /// Creates a new vertex attribute
    template <class PropT>
    attribute<PropT> make_attribute(PropT const& def_value = PropT());

    // Iteration:
    iterator begin() const;
    iterator end() const;

Philip Trettner's avatar
Philip Trettner committed
33
protected:
Philip Trettner's avatar
Philip Trettner committed
34
    /// Backreference to mesh
Philip Trettner's avatar
Philip Trettner committed
35
    mesh_ptr mesh;
Philip Trettner's avatar
Philip Trettner committed
36
37
38
39

    friend class Mesh;
};

Philip Trettner's avatar
Philip Trettner committed
40
/// Collection of all vertices of a mesh
Philip Trettner's avatar
Philip Trettner committed
41
/// Basically a smart std::vector
Philip Trettner's avatar
Philip Trettner committed
42
43
template <class iterator>
struct vertex_collection : smart_collection<Mesh*, vertex_tag, iterator>
Philip Trettner's avatar
Philip Trettner committed
44
{
45
46
47
48
    /// Adds a new vertex and returns its handle
    /// Does NOT invalidate any iterator!
    vertex_handle add() const;

49
50
51
    /// Removes a vertex (and all adjacent faces and edges)
    /// (marks them as removed, compactify mesh to actually remove them)
    void remove(vertex_handle v) const;
52
53
};

Philip Trettner's avatar
Philip Trettner committed
54
/// Collection of all faces of a mesh
55
/// Basically a smart std::vector
Philip Trettner's avatar
Philip Trettner committed
56
57
template <class iterator>
struct face_collection : smart_collection<Mesh*, face_tag, iterator>
58
59
60
61
62
{
    /// Adds a face consisting of N vertices
    /// The vertices must already be sorted in CCW order
    /// (note: trying to add already existing halfedges triggers assertions)
    template <size_t N>
Philip Trettner's avatar
Philip Trettner committed
63
    face_handle add(const vertex_handle (&v_handles)[N]) const;
Philip Trettner's avatar
Philip Trettner committed
64
65
66
    face_handle add(vertex_handle v0, vertex_handle v1, vertex_handle v2) const;
    face_handle add(vertex_handle v0, vertex_handle v1, vertex_handle v2, vertex_handle v3) const;
    face_handle add(std::vector<vertex_handle> const& v_handles) const;
67
    face_handle add(vertex_handle const* v_handles, int vcnt) const;
Philip Trettner's avatar
Philip Trettner committed
68
    template <size_t N>
Philip Trettner's avatar
Philip Trettner committed
69
    face_handle add(const halfedge_handle (&half_loop)[N]) const;
Philip Trettner's avatar
Philip Trettner committed
70
71
72
    face_handle add(halfedge_handle h0, halfedge_handle h1, halfedge_handle h2) const;
    face_handle add(halfedge_handle h0, halfedge_handle h1, halfedge_handle h2, halfedge_handle h3) const;
    face_handle add(std::vector<halfedge_handle> const& half_loop) const;
73
    face_handle add(halfedge_handle const* half_loop, int vcnt) const;
74

75
76
77
    /// Removes a face (adjacent edges and vertices are NOT removed)
    /// (marks it as removed, compactify mesh to actually remove it)
    void remove(face_handle f) const;
78
79
};

Philip Trettner's avatar
Philip Trettner committed
80
/// Collection of all edges of a mesh
81
/// Basically a smart std::vector
Philip Trettner's avatar
Philip Trettner committed
82
83
template <class iterator>
struct edge_collection : smart_collection<Mesh*, edge_tag, iterator>
84
{
Philip Trettner's avatar
Philip Trettner committed
85
86
87
88
    /// Adds an edge between two existing, distinct vertices
    /// if edge already exists, returns it
    edge_handle add_or_get(vertex_handle v_from, vertex_handle v_to);

Philip Trettner's avatar
Philip Trettner committed
89
90
    // TODO: find

91
92
93
    /// Removes an edge (and both adjacent faces, vertices are NOT removed)
    /// (marks them as removed, compactify mesh to actually remove them)
    void remove(edge_handle e) const;
Philip Trettner's avatar
Philip Trettner committed
94
};
Philip Trettner's avatar
Philip Trettner committed
95

Philip Trettner's avatar
Philip Trettner committed
96
97
/// Collection of all half-edges of a mesh
/// Basically a smart std::vector
Philip Trettner's avatar
Philip Trettner committed
98
99
template <class iterator>
struct halfedge_collection : smart_collection<Mesh*, halfedge_tag, iterator>
Philip Trettner's avatar
Philip Trettner committed
100
101
102
103
104
{
    /// Adds an half-edge between two existing, distinct vertices
    /// if half-edge already exists, returns it
    /// (always adds opposite half-edge as well)
    halfedge_handle add_or_get(vertex_handle v_from, vertex_handle v_to);
Philip Trettner's avatar
Philip Trettner committed
105

Philip Trettner's avatar
Philip Trettner committed
106
107
108
109
110
    // TODO: find

    /// Removes the edge and both half-edges belonging to it (and both adjacent faces, vertices are NOT removed)
    /// (marks them as removed, compactify mesh to actually remove them)
    void remove_edge(halfedge_handle h) const;
111
112
};

Philip Trettner's avatar
Philip Trettner committed
113
// vertices
114

Philip Trettner's avatar
Philip Trettner committed
115
116
117
struct all_vertex_collection : vertex_collection<primitive<vertex_tag>::all_iterator>
{
};
118

Philip Trettner's avatar
Philip Trettner committed
119
120
121
struct all_vertex_const_collection : smart_collection<Mesh const*, vertex_tag, primitive<vertex_tag>::all_iterator>
{
};
122

Philip Trettner's avatar
Philip Trettner committed
123
124
struct valid_vertex_collection : vertex_collection<primitive<vertex_tag>::valid_iterator>
{
125
126
};

Philip Trettner's avatar
Philip Trettner committed
127
struct valid_vertex_const_collection : smart_collection<Mesh const*, vertex_tag, primitive<vertex_tag>::valid_iterator>
128
{
Philip Trettner's avatar
Philip Trettner committed
129
};
130

Philip Trettner's avatar
Philip Trettner committed
131
// faces
132

Philip Trettner's avatar
Philip Trettner committed
133
134
struct all_face_collection : face_collection<primitive<face_tag>::all_iterator>
{
135
136
};

Philip Trettner's avatar
Philip Trettner committed
137
struct all_face_const_collection : smart_collection<Mesh const*, face_tag, primitive<face_tag>::all_iterator>
138
{
Philip Trettner's avatar
Philip Trettner committed
139
};
140

Philip Trettner's avatar
Philip Trettner committed
141
142
143
struct valid_face_collection : face_collection<primitive<face_tag>::valid_iterator>
{
};
144

Philip Trettner's avatar
Philip Trettner committed
145
146
147
struct valid_face_const_collection : smart_collection<Mesh const*, face_tag, primitive<face_tag>::valid_iterator>
{
};
Philip Trettner's avatar
Philip Trettner committed
148

Philip Trettner's avatar
Philip Trettner committed
149
// edges
150

Philip Trettner's avatar
Philip Trettner committed
151
152
153
struct all_edge_collection : edge_collection<primitive<edge_tag>::all_iterator>
{
};
Philip Trettner's avatar
Philip Trettner committed
154

Philip Trettner's avatar
Philip Trettner committed
155
156
struct all_edge_const_collection : smart_collection<Mesh const*, edge_tag, primitive<edge_tag>::all_iterator>
{
157
158
};

Philip Trettner's avatar
Philip Trettner committed
159
struct valid_edge_collection : edge_collection<primitive<edge_tag>::valid_iterator>
160
{
Philip Trettner's avatar
Philip Trettner committed
161
};
162

Philip Trettner's avatar
Philip Trettner committed
163
164
165
struct valid_edge_const_collection : smart_collection<Mesh const*, edge_tag, primitive<edge_tag>::valid_iterator>
{
};
166

Philip Trettner's avatar
Philip Trettner committed
167
// half-edges
168

Philip Trettner's avatar
Philip Trettner committed
169
170
struct all_halfedge_collection : halfedge_collection<primitive<halfedge_tag>::all_iterator>
{
171
172
};

Philip Trettner's avatar
Philip Trettner committed
173
struct all_halfedge_const_collection : smart_collection<Mesh const*, halfedge_tag, primitive<halfedge_tag>::all_iterator>
174
{
Philip Trettner's avatar
Philip Trettner committed
175
};
176

Philip Trettner's avatar
Philip Trettner committed
177
178
179
struct valid_halfedge_collection : halfedge_collection<primitive<halfedge_tag>::valid_iterator>
{
};
180

Philip Trettner's avatar
Philip Trettner committed
181
182
struct valid_halfedge_const_collection : smart_collection<Mesh const*, halfedge_tag, primitive<halfedge_tag>::valid_iterator>
{
183
};
Philip Trettner's avatar
Philip Trettner committed
184

185
186
// ================= RINGS =================

Philip Trettner's avatar
Philip Trettner committed
187
188
// rings

Philip Trettner's avatar
Philip Trettner committed
189
190
191
192
193
194
195
196
/// all vertices belonging to a face
struct face_vertex_ring
{
    face_handle face;

    /// Number of vertices
    /// O(result) computation
    int size() const;
197
198
    /// Returns true if handle is contained in this ring
    bool contains(vertex_handle v) const;
Philip Trettner's avatar
Philip Trettner committed
199
200

    // Iteration:
201
202
    face_vertex_circulator begin() const { return {face.any_halfedge(), false}; }
    face_vertex_circulator end() const { return {face.any_halfedge(), true}; }
Philip Trettner's avatar
Philip Trettner committed
203
204
};

205
206
207
208
209
210
211
212
/// all halfedges belonging to a face
struct face_halfedge_ring
{
    face_handle face;

    /// Number of vertices
    /// O(result) computation
    int size() const;
213
214
    /// Returns true if handle is contained in this ring
    bool contains(halfedge_handle h) const;
215
216

    // Iteration:
217
218
    face_halfedge_circulator begin() const { return {face.any_halfedge(), false}; }
    face_halfedge_circulator end() const { return {face.any_halfedge(), true}; }
219
220
221
222
223
224
225
226
227
228
};

/// all edges belonging to a face
struct face_edge_ring
{
    face_handle face;

    /// Number of vertices
    /// O(result) computation
    int size() const;
229
230
    /// Returns true if handle is contained in this ring
    bool contains(edge_handle e) const;
231
232

    // Iteration:
233
234
    face_edge_circulator begin() const { return {face.any_halfedge(), false}; }
    face_edge_circulator end() const { return {face.any_halfedge(), true}; }
235
236
237
238
239
240
241
242
243
244
};

/// all adjacent faces belonging to a face
struct face_face_ring
{
    face_handle face;

    /// Number of vertices
    /// O(result) computation
    int size() const;
245
246
    /// Returns true if handle is contained in this ring
    bool contains(face_handle f) const;
247
248

    // Iteration:
249
250
    face_face_circulator begin() const { return {face.any_halfedge(), false}; }
    face_face_circulator end() const { return {face.any_halfedge(), true}; }
251
252
253
254
255
256
257
258
259
260
};

/// all outgoing half-edges from a vertex
struct vertex_halfedge_out_ring
{
    vertex_handle vertex;

    /// Number of vertices
    /// O(result) computation
    int size() const;
261
262
    /// Returns true if handle is contained in this ring
    bool contains(halfedge_handle h) const;
263
264

    // Iteration:
265
266
    vertex_halfedge_out_circulator begin() const { return {vertex.any_outgoing_halfedge(), vertex.is_isolated()}; }
    vertex_halfedge_out_circulator end() const { return {vertex.any_outgoing_halfedge(), true}; }
267
268
269
270
271
272
273
274
275
276
};

/// all incoming half-edges from a vertex
struct vertex_halfedge_in_ring
{
    vertex_handle vertex;

    /// Number of vertices
    /// O(result) computation
    int size() const;
277
278
    /// Returns true if handle is contained in this ring
    bool contains(halfedge_handle h) const;
279
280

    // Iteration:
281
282
    vertex_halfedge_in_circulator begin() const { return {vertex.any_outgoing_halfedge(), vertex.is_isolated()}; }
    vertex_halfedge_in_circulator end() const { return {vertex.any_outgoing_halfedge(), true}; }
283
284
285
286
287
288
289
290
291
292
};

/// all adjacent vertices of a vertex
struct vertex_vertex_ring
{
    vertex_handle vertex;

    /// Number of vertices
    /// O(result) computation
    int size() const;
293
294
    /// Returns true if handle is contained in this ring
    bool contains(vertex_handle v) const;
295
296

    // Iteration:
297
298
    vertex_vertex_circulator begin() const { return {vertex.any_outgoing_halfedge(), vertex.is_isolated()}; }
    vertex_vertex_circulator end() const { return {vertex.any_outgoing_halfedge(), true}; }
299
300
301
302
303
304
305
306
307
308
};

/// all adjacent edges of a vertex
struct vertex_edge_ring
{
    vertex_handle vertex;

    /// Number of vertices
    /// O(result) computation
    int size() const;
309
310
    /// Returns true if handle is contained in this ring
    bool contains(edge_handle e) const;
311
312

    // Iteration:
313
314
    vertex_edge_circulator begin() const { return {vertex.any_outgoing_halfedge(), vertex.is_isolated()}; }
    vertex_edge_circulator end() const { return {vertex.any_outgoing_halfedge(), true}; }
315
316
317
318
319
320
321
322
323
324
};

/// all adjacent faces of a vertex (INCLUDES invalid ones for boundaries)
struct vertex_face_ring
{
    vertex_handle vertex;

    /// Number of vertices
    /// O(result) computation
    int size() const;
325
326
    /// Returns true if handle is contained in this ring
    bool contains(face_handle f) const;
327
328

    // Iteration:
329
330
    vertex_face_circulator begin() const { return {vertex.any_outgoing_halfedge(), vertex.is_isolated()}; }
    vertex_face_circulator end() const { return {vertex.any_outgoing_halfedge(), true}; }
331
332
};

Philip Trettner's avatar
Philip Trettner committed
333
334
335
336
337
338
339
340
341
342
343
344
345

/// ======== IMPLEMENTATION ========

inline int face_vertex_ring::size() const
{
    auto cnt = 0;
    for (auto v : *this)
    {
        (void)v; // unused
        cnt++;
    }
    return cnt;
}
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
inline int face_edge_ring::size() const
{
    auto cnt = 0;
    for (auto v : *this)
    {
        (void)v; // unused
        cnt++;
    }
    return cnt;
}
inline int face_halfedge_ring::size() const
{
    auto cnt = 0;
    for (auto v : *this)
    {
        (void)v; // unused
        cnt++;
    }
    return cnt;
}
inline int face_face_ring::size() const
{
    auto cnt = 0;
    for (auto v : *this)
    {
        (void)v; // unused
        cnt++;
    }
    return cnt;
}

inline int vertex_halfedge_out_ring::size() const
{
    auto cnt = 0;
    for (auto v : *this)
    {
        (void)v; // unused
        cnt++;
    }
    return cnt;
}

inline int vertex_halfedge_in_ring::size() const
{
    auto cnt = 0;
    for (auto v : *this)
    {
        (void)v; // unused
        cnt++;
    }
    return cnt;
}

inline int vertex_vertex_ring::size() const
{
    auto cnt = 0;
    for (auto v : *this)
    {
        (void)v; // unused
        cnt++;
    }
    return cnt;
}

inline int vertex_edge_ring::size() const
{
    auto cnt = 0;
    for (auto v : *this)
    {
        (void)v; // unused
        cnt++;
    }
    return cnt;
}

inline int vertex_face_ring::size() const
{
    auto cnt = 0;
    for (auto v : *this)
    {
        (void)v; // unused
        cnt++;
    }
    return cnt;
}
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502

inline bool face_vertex_ring::contains(vertex_handle v) const
{
    for (auto v2 : *this)
        if (v == v2)
            return true;
    return false;
}

inline bool face_edge_ring::contains(edge_handle e) const
{
    for (auto e2 : *this)
        if (e == e2)
            return true;
    return false;
}

inline bool face_halfedge_ring::contains(halfedge_handle h) const
{
    for (auto h2 : *this)
        if (h == h2)
            return true;
    return false;
}

inline bool face_face_ring::contains(face_handle f) const
{
    for (auto f2 : *this)
        if (f == f2)
            return true;
    return false;
}

inline bool vertex_halfedge_out_ring::contains(halfedge_handle h) const
{
    for (auto h2 : *this)
        if (h == h2)
            return true;
    return false;
}

inline bool vertex_halfedge_in_ring::contains(halfedge_handle h) const
{
    for (auto h2 : *this)
        if (h == h2)
            return true;
    return false;
}

inline bool vertex_vertex_ring::contains(vertex_handle v) const
{
    for (auto v2 : *this)
        if (v == v2)
            return true;
    return false;
}

inline bool vertex_edge_ring::contains(edge_handle e) const
{
    for (auto e2 : *this)
        if (e == e2)
            return true;
    return false;
}

inline bool vertex_face_ring::contains(face_handle f) const
{
    for (auto f2 : *this)
        if (f == f2)
            return true;
    return false;
}
503
}