ranges.hh 12.4 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
template<class mesh_ptr, class tag, class iterator>
Philip Trettner's avatar
Philip Trettner committed
11
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
    /// O(1) computation
Philip Trettner's avatar
Philip Trettner committed
18
    int size() const { return iterator::primitive_size(*mesh); }
Philip Trettner's avatar
Philip Trettner committed
19
20

    /// Ensures that a given number of primitives can be stored without reallocation
Philip Trettner's avatar
Philip Trettner committed
21
    void reserve(int capacity) const { return primitive<tag>::reserve(*mesh, capacity); }
22

Philip Trettner's avatar
Philip Trettner committed
23
24
25
26
27
28
29
30
    /// 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
31
protected:
Philip Trettner's avatar
Philip Trettner committed
32
    /// Backreference to mesh
Philip Trettner's avatar
Philip Trettner committed
33
    mesh_ptr mesh;
Philip Trettner's avatar
Philip Trettner committed
34
35
36
37

    friend class Mesh;
};

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

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

Philip Trettner's avatar
Philip Trettner committed
52
/// Collection of all faces of a mesh
53
/// Basically a smart std::vector
Philip Trettner's avatar
Philip Trettner committed
54
55
template<class iterator>
struct face_collection : smart_collection<Mesh *, face_tag, iterator>
56
57
58
59
60
{
    /// 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
61
    face_handle add(const vertex_handle(&v_handles)[N]) const;
Philip Trettner's avatar
Philip Trettner committed
62
63
64
    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;
65
    face_handle add(vertex_handle const* v_handles, int vcnt) const;
Philip Trettner's avatar
Philip Trettner committed
66
    template <size_t N>
Philip Trettner's avatar
Philip Trettner committed
67
    face_handle add(const halfedge_handle(&half_loop)[N]) const;
Philip Trettner's avatar
Philip Trettner committed
68
69
70
    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;
71
    face_handle add(halfedge_handle const* half_loop, int vcnt) const;
72

73
74
75
    /// 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;
76
77
};

Philip Trettner's avatar
Philip Trettner committed
78
/// Collection of all edges of a mesh
79
/// Basically a smart std::vector
Philip Trettner's avatar
Philip Trettner committed
80
81
template<class iterator>
struct edge_collection : smart_collection<Mesh *, edge_tag, iterator>
82
{
Philip Trettner's avatar
Philip Trettner committed
83
84
85
86
    /// 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
87
88
    // TODO: find

89
90
91
    /// 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
92
};
Philip Trettner's avatar
Philip Trettner committed
93

Philip Trettner's avatar
Philip Trettner committed
94
95
96
97
98
99
100
101
102
/// Collection of all half-edges of a mesh
/// Basically a smart std::vector
template<class iterator>
struct halfedge_collection : smart_collection<Mesh *, halfedge_tag, iterator>
{
    /// 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
103

Philip Trettner's avatar
Philip Trettner committed
104
105
106
107
108
    // 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;
109
110
};

Philip Trettner's avatar
Philip Trettner committed
111
// vertices
112

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

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

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

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

Philip Trettner's avatar
Philip Trettner committed
129
// faces
130

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

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

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

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

Philip Trettner's avatar
Philip Trettner committed
147
// edges
148

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

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

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

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

Philip Trettner's avatar
Philip Trettner committed
165
// half-edges
166

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

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

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

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

Philip Trettner's avatar
Philip Trettner committed
183
184
// rings

Philip Trettner's avatar
Philip Trettner committed
185
186
187
188
189
190
191
192
/// all vertices belonging to a face
struct face_vertex_ring
{
    face_handle face;

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

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

201
202
203
204
205
206
207
208
/// all halfedges belonging to a face
struct face_halfedge_ring
{
    face_handle face;

    /// Number of vertices
    /// O(result) computation
    int size() const;
209
210
    /// Returns true if handle is contained in this ring
    bool contains(halfedge_handle h) const;
211
212
213
214
215
216
217
218
219
220
221
222
223
224

    // 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;
225
226
    /// Returns true if handle is contained in this ring
    bool contains(edge_handle e) const;
227
228
229
230
231
232
233
234
235
236
237
238
239
240

    // 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;
241
242
    /// Returns true if handle is contained in this ring
    bool contains(face_handle f) const;
243
244
245
246
247
248
249
250
251
252
253
254
255
256

    // 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;
257
258
    /// Returns true if handle is contained in this ring
    bool contains(halfedge_handle h) const;
259
260

    // Iteration:
261
    vertex_halfedge_out_circulator begin() const { return {vertex, vertex.is_isolated()}; }
262
263
264
265
266
267
268
269
270
271
272
    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;
273
274
    /// Returns true if handle is contained in this ring
    bool contains(halfedge_handle h) const;
275
276

    // Iteration:
277
    vertex_halfedge_in_circulator begin() const { return {vertex, vertex.is_isolated()}; }
278
279
280
281
282
283
284
285
286
287
288
    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;
289
290
    /// Returns true if handle is contained in this ring
    bool contains(vertex_handle v) const;
291
292

    // Iteration:
293
    vertex_vertex_circulator begin() const { return {vertex, vertex.is_isolated()}; }
294
295
296
297
298
299
300
301
302
303
304
    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;
305
306
    /// Returns true if handle is contained in this ring
    bool contains(edge_handle e) const;
307
308

    // Iteration:
309
    vertex_edge_circulator begin() const { return {vertex, vertex.is_isolated()}; }
310
311
312
313
314
315
316
317
318
319
320
    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;
321
322
    /// Returns true if handle is contained in this ring
    bool contains(face_handle f) const;
323
324

    // Iteration:
325
    vertex_face_circulator begin() const { return {vertex, vertex.is_isolated()}; }
326
327
328
    vertex_face_circulator end() const { return {vertex, true}; }
};

Philip Trettner's avatar
Philip Trettner committed
329
330
331
332
333
334
335
336
337
338
339
340
341

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

inline int face_vertex_ring::size() const
{
    auto cnt = 0;
    for (auto v : *this)
    {
        (void)v; // unused
        cnt++;
    }
    return cnt;
}
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
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
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;
}
427
428
429
430
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

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