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

#include <cstddef>
#include <vector>

#include "iterators.hh"

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

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

    /// Ensures that a given number of primitives can be stored without reallocation
21
22
    void reserve(int capacity) const;

Philip Trettner's avatar
Philip Trettner committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    /// Creates a new vertex attribute
    template <class PropT>
    attribute<PropT> make_attribute(PropT const& def_value = PropT());

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

private:
    /// Backreference to mesh
    Mesh* mesh;

    friend class Mesh;
};

/// Collection of all vertices of a mesh, including deleted ones
/// Basically a smart std::vector
struct vertex_collection : smart_collection<vertex_tag, primitive<vertex_tag>::all_iterator>
{
42
43
44
45
    /// Adds a new vertex and returns its handle
    /// Does NOT invalidate any iterator!
    vertex_handle add() const;

46
47
48
    /// 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;
49
50
};

51
52
53
54
55
56
57
58
59
/// same as vertex_collection but const
struct const_vertex_collection
{
    Mesh const* mesh;

    /// Number of vertices, INCLUDING deleted/invalid ones
    /// O(1) computation
    int size() const;

60
    /// Creates a new vertex attribute
61
    template <class PropT>
62
63
    vertex_attribute<PropT> make_attribute(PropT const& def_value = PropT());

64
    // Iteration:
Philip Trettner's avatar
Philip Trettner committed
65
66
    all_vertex_iterator begin() const;
    all_vertex_iterator end() const;
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
/// Same as vertex_collection but only including valid, non-deleted vertices
/// (a bit slower than the normal collection)
/// (if mesh->is_compact(), identical to vertex_collection)
struct valid_vertex_collection
{
    Mesh const* mesh;

    /// Number of vertices, EXCLUDING deleted/invalid ones
    /// O(1) computation
    int size() const;

    // Iteration:
    valid_vertex_iterator begin() const;
    valid_vertex_iterator end() const;
};

/// Collection of all faces of a mesh, including deleted ones
/// Basically a smart std::vector
struct face_collection
{
    Mesh* mesh;

    /// Number of vertices, INCLUDING deleted/invalid ones
    /// O(1) computation
    int size() const;
    /// Ensures that a given number of faces can be stored without reallocation
    void reserve(int capacity) const;

    /// 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
101
102
103
104
    face_handle add(const vertex_handle (&v_handles)[N]) const;
    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;
105
    face_handle add(vertex_handle const* v_handles, int vcnt) const;
Philip Trettner's avatar
Philip Trettner committed
106
    template <size_t N>
Philip Trettner's avatar
Philip Trettner committed
107
108
109
110
    face_handle add(const halfedge_handle (&half_loop)[N]) const;
    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;
111
    face_handle add(halfedge_handle const* half_loop, int vcnt) const;
112

113
114
115
    /// 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;
116

117
    /// Creates a new face attribute
118
    template <class PropT>
119
    face_attribute<PropT> make_attribute(PropT const& def_value = PropT());
Philip Trettner's avatar
Philip Trettner committed
120

121
122
123
124
125
    // Iteration:
    face_iterator begin() const;
    face_iterator end() const;
};

126
127
128
129
130
131
132
133
134
/// same as face_collection but const
struct const_face_collection
{
    Mesh const* mesh;

    /// Number of faces, INCLUDING deleted/invalid ones
    /// O(1) computation
    int size() const;

135
    /// Creates a new face attribute
136
    template <class PropT>
137
138
    face_attribute<PropT> make_attribute(PropT const& def_value = PropT());

139
140
141
142
143
    // Iteration:
    face_iterator begin() const;
    face_iterator end() const;
};

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
/// Same as face_collection but only including valid, non-deleted faces
/// (a bit slower than the normal collection)
/// (if mesh->is_compact(), identical to face_collection)
struct valid_face_collection
{
    Mesh const* mesh;

    /// Number of faces, EXCLUDING deleted/invalid ones
    /// O(1) computation
    int size() const;

    // Iteration:
    valid_face_iterator begin() const;
    valid_face_iterator end() const;
};

/// Collection of all edges of a mesh, including deleted ones
/// Basically a smart std::vector
struct edge_collection
{
    Mesh* mesh;

    /// Number of vertices, INCLUDING deleted/invalid ones
    /// O(1) computation
    int size() const;
    /// Ensures that a given number of edges can be stored without reallocation
    void reserve(int capacity) const;

Philip Trettner's avatar
Philip Trettner committed
172
173
174
175
    /// 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);

176
177
178
    /// 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
179

180
    /// Creates a new edge attribute
181
    template <class PropT>
182
    edge_attribute<PropT> make_attribute(PropT const& def_value = PropT());
Philip Trettner's avatar
Philip Trettner committed
183

184
185
186
187
188
    // Iteration:
    edge_iterator begin() const;
    edge_iterator end() const;
};

189
190
191
192
193
194
195
196
197
/// same as edge_collection but const
struct const_edge_collection
{
    Mesh const* mesh;

    /// Number of edges, INCLUDING deleted/invalid ones
    /// O(1) computation
    int size() const;

198
    /// Creates a new edge attribute
199
    template <class PropT>
200
201
    edge_attribute<PropT> make_attribute(PropT const& def_value = PropT());

202
203
204
205
206
    // Iteration:
    edge_iterator begin() const;
    edge_iterator end() const;
};

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
/// Same as edge_collection but only including valid, non-deleted edges
/// (a bit slower than the normal collection)
/// (if mesh->is_compact(), identical to edge_collection)
struct valid_edge_collection
{
    Mesh const* mesh;

    /// Number of edges, EXCLUDING deleted/invalid ones
    /// O(1) computation
    int size() const;

    // Iteration:
    valid_edge_iterator begin() const;
    valid_edge_iterator end() const;
};

/// Collection of all half-edges of a mesh, including deleted ones
/// Basically a smart std::vector
struct halfedge_collection
{
    Mesh* mesh;

    /// Number of vertices, INCLUDING deleted/invalid ones
    /// O(1) computation
    int size() const;
    /// Ensures that a given number of half-edges can be stored without reallocation
    void reserve(int capacity) const;

Philip Trettner's avatar
Philip Trettner committed
235
236
237
238
239
    /// 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);

240
241
242
243
    /// 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;

244
    /// Creates a new half-edge attribute
245
    template <class PropT>
246
    halfedge_attribute<PropT> make_attribute(PropT const& def_value = PropT());
Philip Trettner's avatar
Philip Trettner committed
247

248
249
250
251
252
    // Iteration:
    halfedge_iterator begin() const;
    halfedge_iterator end() const;
};

253
254
255
256
257
258
259
260
261
/// same as halfedge_collection but const
struct const_halfedge_collection
{
    Mesh const* mesh;

    /// Number of halfedges, INCLUDING deleted/invalid ones
    /// O(1) computation
    int size() const;

262
    /// Creates a new half-edge attribute
263
    template <class PropT>
264
265
    halfedge_attribute<PropT> make_attribute(PropT const& def_value = PropT());

266
267
268
269
270
    // Iteration:
    halfedge_iterator begin() const;
    halfedge_iterator end() const;
};

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/// Same as halfedge_collection but only including valid, non-deleted halfedges
/// (a bit slower than the normal collection)
/// (if mesh->is_compact(), identical to halfedge_collection)
struct valid_halfedge_collection
{
    Mesh const* mesh;

    /// Number of halfedges, EXCLUDING deleted/invalid ones
    /// O(1) computation
    int size() const;

    // Iteration:
    valid_halfedge_iterator begin() const;
    valid_halfedge_iterator end() const;
};
Philip Trettner's avatar
Philip Trettner committed
286

Philip Trettner's avatar
Philip Trettner committed
287
288
289
290
291
292
293
294
/// all vertices belonging to a face
struct face_vertex_ring
{
    face_handle face;

    /// Number of vertices
    /// O(result) computation
    int size() const;
295
296
    /// Returns true if handle is contained in this ring
    bool contains(vertex_handle v) const;
Philip Trettner's avatar
Philip Trettner committed
297
298
299
300
301
302

    // Iteration:
    face_vertex_circulator begin() const { return {face, false}; }
    face_vertex_circulator end() const { return {face, true}; }
};

303
304
305
306
307
308
309
310
/// all halfedges belonging to a face
struct face_halfedge_ring
{
    face_handle face;

    /// Number of vertices
    /// O(result) computation
    int size() const;
311
312
    /// Returns true if handle is contained in this ring
    bool contains(halfedge_handle h) const;
313
314
315
316
317
318
319
320
321
322
323
324
325
326

    // Iteration:
    face_halfedge_circulator begin() const { return {face, false}; }
    face_halfedge_circulator end() const { return {face, true}; }
};

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

    /// Number of vertices
    /// O(result) computation
    int size() const;
327
328
    /// Returns true if handle is contained in this ring
    bool contains(edge_handle e) const;
329
330
331
332
333
334
335
336
337
338
339
340
341
342

    // Iteration:
    face_edge_circulator begin() const { return {face, false}; }
    face_edge_circulator end() const { return {face, true}; }
};

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

    /// Number of vertices
    /// O(result) computation
    int size() const;
343
344
    /// Returns true if handle is contained in this ring
    bool contains(face_handle f) const;
345
346
347
348
349
350
351
352
353
354
355
356
357
358

    // Iteration:
    face_face_circulator begin() const { return {face, false}; }
    face_face_circulator end() const { return {face, true}; }
};

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

    /// Number of vertices
    /// O(result) computation
    int size() const;
359
360
    /// Returns true if handle is contained in this ring
    bool contains(halfedge_handle h) const;
361
362

    // Iteration:
363
    vertex_halfedge_out_circulator begin() const { return {vertex, vertex.is_isolated()}; }
364
365
366
367
368
369
370
371
372
373
374
    vertex_halfedge_out_circulator end() const { return {vertex, true}; }
};

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

    /// Number of vertices
    /// O(result) computation
    int size() const;
375
376
    /// Returns true if handle is contained in this ring
    bool contains(halfedge_handle h) const;
377
378

    // Iteration:
379
    vertex_halfedge_in_circulator begin() const { return {vertex, vertex.is_isolated()}; }
380
381
382
383
384
385
386
387
388
389
390
    vertex_halfedge_in_circulator end() const { return {vertex, true}; }
};

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

    /// Number of vertices
    /// O(result) computation
    int size() const;
391
392
    /// Returns true if handle is contained in this ring
    bool contains(vertex_handle v) const;
393
394

    // Iteration:
395
    vertex_vertex_circulator begin() const { return {vertex, vertex.is_isolated()}; }
396
397
398
399
400
401
402
403
404
405
406
    vertex_vertex_circulator end() const { return {vertex, true}; }
};

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

    /// Number of vertices
    /// O(result) computation
    int size() const;
407
408
    /// Returns true if handle is contained in this ring
    bool contains(edge_handle e) const;
409
410

    // Iteration:
411
    vertex_edge_circulator begin() const { return {vertex, vertex.is_isolated()}; }
412
413
414
415
416
417
418
419
420
421
422
    vertex_edge_circulator end() const { return {vertex, true}; }
};

/// 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;
423
424
    /// Returns true if handle is contained in this ring
    bool contains(face_handle f) const;
425
426

    // Iteration:
427
    vertex_face_circulator begin() const { return {vertex, vertex.is_isolated()}; }
428
429
430
    vertex_face_circulator end() const { return {vertex, true}; }
};

Philip Trettner's avatar
Philip Trettner committed
431
432
433
434
435
436
437
438
439
440
441
442
443

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

inline int face_vertex_ring::size() const
{
    auto cnt = 0;
    for (auto v : *this)
    {
        (void)v; // unused
        cnt++;
    }
    return cnt;
}
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
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;
}
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600

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;
}
601
}