-
Notifications
You must be signed in to change notification settings - Fork 23
/
SurfaceMesh.h
2708 lines (2473 loc) · 106 KB
/
SurfaceMesh.h
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
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
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
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
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2022 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
#pragma once
#include <lagrange/AttributeFwd.h>
#include <lagrange/utils/SharedSpan.h>
#include <lagrange/utils/function_ref.h>
#include <lagrange/utils/span.h>
#include <lagrange/utils/value_ptr.h>
#include <initializer_list>
#include <string_view>
#include <type_traits>
namespace lagrange {
/// @cond LA_INTERNAL_DOCS
/// Forward declarations
namespace internal {
template <typename T>
class weak_ptr;
}
/// @endcond
/// @defgroup group-surfacemesh SurfaceMesh
/// @ingroup module-core
/// Generic surface meshes.
/// @{
///
/// A general purpose polygonal mesh class.
///
/// @tparam Scalar_ %Mesh scalar type.
/// @tparam Index_ %Mesh index type.
///
/// @see <div> Related documentation:
///
/// - @ref group-surfacemesh-attr "Manipulating mesh attributes"
/// - @ref group-surfacemesh-utils "Mesh utility functions"
/// - @ref group-surfacemesh-attr-utils "Attribute utility functions"
/// - @ref group-surfacemesh-iterate "Attribute iterators"
/// - @ref group-surfacemesh-views "Eigen matrix view of mesh attributes"
///
/// Related headers:
///
/// - `#include <lagrange/Attribute.h>` for manipulating non-indexed mesh attributes (e.g. vertex
/// colors or facet normals).
/// - `#include <lagrange/IndexedAttribute.h>` for manipulating indexed mesh attributes (e.g. UVs).
/// - `#include <lagrange/views.h>` for Eigen matrix views over mesh attributes.
/// - `#include <lagrange/foreach_attribute.h>` for iterating over mesh attributes.
/// - `#include <lagrange/cast.h>` for casting between Scalar/Index types.
/// - `#include <lagrange/map_attribute.h>` for converting attributes between mesh element types
/// (e.g. vertex -> facet). </div>
///
template <typename Scalar_, typename Index_>
class SurfaceMesh
{
public:
/// %Mesh scalar type, used for vertex coordinates.
using Scalar = Scalar_;
/// %Mesh index type, used for facet indices.
using Index = Index_;
/// Signed index type corresponding to the mesh index type.
using SignedIndex = std::make_signed_t<Index>;
// Static assertions to prevent users from instantiating unsupported types.
static_assert(
std::is_same_v<Scalar, float> || std::is_same_v<Scalar, double>,
"SurfaceMesh's Scalar template parameter can only be float or double.");
static_assert(
std::is_same_v<Index, uint32_t> || std::is_same_v<Index, uint64_t>,
"SurfaceMesh's Index template parameter can only be uint32_t or uint64_t.");
public:
///
/// Callback function to set vertex coordinates.
///
/// @param[in] v Index of the vertex to set coordinates for (relative to the newly inserted
/// vertices, i.e. starts at 0).
/// @param[out] p Output coordinate buffer to write to. The output array will contain K
/// elements to write to, where K is the dimension of the mesh (3 by default).
///
/// @see add_vertices
///
using SetVertexCoordinatesFunction = function_ref<void(Index v, span<Scalar> p)>;
///
/// Callback function to set indices of a single facet. The facet size is fixed and known in
/// advance by the user.
///
/// @param[out] t Output index buffer to write to.
///
/// @see add_polygon
///
using SetSingleFacetIndicesFunction = function_ref<void(span<Index> t)>;
///
/// Callback function to set indices of a multiple facets.
///
/// @param[in] f Index of the facet whose size to compute (relative to the newly inserted
/// facets, starting with 0).
/// @param[out] t Output index buffer to write to. You can query the size of the current
/// output facet by calling t.size().
///
/// @see add_triangles, add_quads, add_polygons, add_hybrid
///
using SetMultiFacetsIndicesFunction = function_ref<void(Index f, span<Index> t)>;
///
/// Callback function to get a facet size (number of vertices in the facet).
///
/// @param[in] f Index of the facet whose size to compute (relative to the newly inserted
/// facets, starting with 0).
///
/// @see add_hybrid
///
using GetFacetsSizeFunction = function_ref<Index(Index f)>;
///
/// Callback function to get the vertex indices of an edge endpoints in a user-provided ordering
/// of a mesh edges.
///
/// @param[in] e Index of the edge being queried.
///
/// @return A pair of indices for the vertex endpoints.
///
using GetEdgeVertices = function_ref<std::array<Index, 2>(Index e)>;
public:
///
/// @name Mesh construction
/// @{
///
/// Default constructor.
///
/// @param[in] dimension Vertex dimension.
///
explicit SurfaceMesh(Index dimension = 3);
///
/// Default destructor.
///
~SurfaceMesh();
///
/// Move constructor.
///
/// @param other Instance to move from.
///
SurfaceMesh(SurfaceMesh&& other) noexcept;
///
/// Assignment move operator.
///
/// @param other Instance to move from.
///
/// @return The result of the assignment.
///
SurfaceMesh& operator=(SurfaceMesh&& other) noexcept;
///
/// Copy constructor.
///
/// @param[in] other Instance to copy from.
///
SurfaceMesh(const SurfaceMesh& other);
///
/// Assignment copy operator.
///
/// @param[in] other Instance to copy from.
///
/// @return The result of the copy.
///
SurfaceMesh& operator=(const SurfaceMesh& other);
///
/// Copy constructor stripping away non-reserved attributes. Will internally cast the scalar and
/// index type to the desired type. This static constructor is used internally to implement the
/// more high-level `cast` and `filter_attribute` function.
///
/// @note Should this function take an optional list of attribute ids to preserve, and/or
/// provide stable attribute ids?
///
/// @param[in] other Instance to copy from.
///
/// @tparam OtherScalar Other mesh scalar type.
/// @tparam OtherIndex Other mesh index type.
///
/// @return The result of the copy.
///
/// @see cast
/// @see filter_attributes
///
template <typename OtherScalar, typename OtherIndex>
static SurfaceMesh stripped_copy(const SurfaceMesh<OtherScalar, OtherIndex>& other);
///
/// Move constructor stripping away non-reserved attributes. Will internally cast the scalar and
/// index type to the desired type. This static constructor is used internally to implement the
/// more high-level `cast` and `filter_attribute` function.
///
/// @note Should this function take an optional list of attribute ids to preserve, and/or
/// provide stable attribute ids?
///
/// @param[in] other Instance to move from.
///
/// @tparam OtherScalar Other mesh scalar type.
/// @tparam OtherIndex Other mesh index type.
///
/// @return The result of the move.
///
/// @see cast
/// @see filter_attributes
///
template <typename OtherScalar, typename OtherIndex>
static SurfaceMesh stripped_move(SurfaceMesh<OtherScalar, OtherIndex>&& other);
///
/// Adds a vertex to the mesh.
///
/// @param[in] p Vertex coordinates with the same dimensionality as the mesh.
///
void add_vertex(span<const Scalar> p);
///
/// Adds a vertex to the mesh.
///
/// @param[in] p Vertex coordinates with the same dimensionality as the mesh.
///
void add_vertex(std::initializer_list<const Scalar> p);
///
/// Adds multiple vertices to the mesh.
///
/// @param[in] num_vertices Number of vertices to add.
/// @param[in] coordinates A contiguous array of point coordinates (num_vertices x
/// dimension).
///
void add_vertices(Index num_vertices, span<const Scalar> coordinates = {});
///
/// Adds multiple vertices to the mesh.
///
/// @param[in] num_vertices Number of vertices to add.
/// @param[in] coordinates A contiguous array of point coordinates (num_vertices x
/// dimension).
///
void add_vertices(Index num_vertices, std::initializer_list<const Scalar> coordinates);
///
/// Adds multiple vertices to the mesh.
///
/// @param[in] num_vertices Number of vertices to add.
/// @param[in] set_vertex_coordinates Function to set point coordinates of a given vertex.
///
void add_vertices(Index num_vertices, SetVertexCoordinatesFunction set_vertex_coordinates);
///
/// Adds a triangular facet to the mesh.
///
/// @param[in] v0 Index of the first vertex.
/// @param[in] v1 Index of the second vertex.
/// @param[in] v2 Index of the third vertex.
///
void add_triangle(Index v0, Index v1, Index v2);
///
/// Adds multiple triangular facets to the mesh.
///
/// @warning If the mesh contains edge/connectivity attributes, this function will throw an
/// exception if you pass an empty buffer of facet indices. This is because it is
/// impossible to update edge/connectivity information if the facet buffer is
/// directly modified by the user. Instead, the correct facet indices must be
/// provided when the facet is constructed.
///
/// @param[in] num_facets Number of facets to add.
/// @param[in] facet_indices A contiguous array of corner indices, where facet_indices[3*i+k]
/// is the index of the k-th corner of the i-th facet.
///
void add_triangles(Index num_facets, span<const Index> facet_indices = {});
///
/// Adds multiple triangular facets to the mesh.
///
/// @warning If the mesh contains edge/connectivity attributes, this function will throw an
/// exception if you pass an empty buffer of facet indices. This is because it is
/// impossible to update edge/connectivity information if the facet buffer is
/// directly modified by the user. Instead, the correct facet indices must be
/// provided when the facet is constructed.
///
/// @param[in] num_facets Number of facets to add.
/// @param[in] facet_indices A contiguous array of corner indices, where facet_indices[3*i+k]
/// is the index of the k-th corner of the i-th facet.
///
void add_triangles(Index num_facets, std::initializer_list<const Index> facet_indices);
///
/// Adds multiple triangular facets to the mesh.
///
/// @param[in] num_facets Number of facets to add.
/// @param[in] set_facets_indices Callable function to set vertex indices of a given facet.
///
void add_triangles(Index num_facets, SetMultiFacetsIndicesFunction set_facets_indices);
///
/// Adds a quadrilateral facet to the mesh.
///
/// @param[in] v0 Index of the first vertex.
/// @param[in] v1 Index of the second vertex.
/// @param[in] v2 Index of the third vertex.
/// @param[in] v3 Index of the fourth vertex.
///
void add_quad(Index v0, Index v1, Index v2, Index v3);
///
/// Adds multiple quadrilateral facets to the mesh.
///
/// @warning If the mesh contains edge/connectivity attributes, this function will throw an
/// exception if you pass an empty buffer of facet indices. This is because it is
/// impossible to update edge/connectivity information if the facet buffer is
/// directly modified by the user. Instead, the correct facet indices must be
/// provided when the facet is constructed.
///
/// @param[in] num_facets Number of facets to add.
/// @param[in] facet_indices A contiguous array of corner indices, where facet_indices[4*i+k]
/// is the index of the k-th corner of the i-th facet.
///
void add_quads(Index num_facets, span<const Index> facet_indices = {});
///
/// Adds multiple quadrilateral facets to the mesh.
///
/// @warning If the mesh contains edge/connectivity attributes, this function will throw an
/// exception if you pass an empty buffer of facet indices. This is because it is
/// impossible to update edge/connectivity information if the facet buffer is
/// directly modified by the user. Instead, the correct facet indices must be
/// provided when the facet is constructed.
///
/// @param[in] num_facets Number of facets to add.
/// @param[in] facet_indices A contiguous array of corner indices, where facet_indices[4*i+k]
/// is the index of the k-th corner of the i-th facet.
///
void add_quads(Index num_facets, std::initializer_list<const Index> facet_indices);
///
/// Adds multiple quadrilateral facets to the mesh.
///
/// @param[in] num_facets Number of facets to add.
/// @param[in] set_facets_indices Callable function to set vertex indices of a given facet.
///
void add_quads(Index num_facets, SetMultiFacetsIndicesFunction set_facets_indices);
///
/// Adds a single (uninitialized) polygonal facet to the mesh. Facet indices are set to 0.
///
/// @param[in] facet_size Number of vertices in the facet.
///
void add_polygon(Index facet_size);
///
/// Adds a single polygonal facet to the mesh.
///
/// @param[in] facet_indices A contiguous array of vertex indices in the facet.
///
void add_polygon(span<const Index> facet_indices);
///
/// Adds a single polygonal facet to the mesh.
///
/// @param[in] facet_indices A contiguous array of vertex indices in the facet.
///
void add_polygon(std::initializer_list<const Index> facet_indices);
///
/// Adds a single polygonal facet to the mesh.
///
/// @param[in] facet_size Number of vertices in the facet.
/// @param[in] set_facet_indices Callable function to retrieve facet indices.
///
void add_polygon(Index facet_size, SetSingleFacetIndicesFunction set_facet_indices);
///
/// Adds multiple polygonal facets of the same size to the mesh.
///
/// @warning If the mesh contains edge/connectivity attributes, this function will throw an
/// exception if you pass an empty buffer of facet indices. This is because it is
/// impossible to update edge/connectivity information if the facet buffer is
/// directly modified by the user. Instead, the correct facet indices must be
/// provided when the facet is constructed.
///
/// @param[in] num_facets Number of facets to add.
/// @param[in] facet_size Size of each facet to be added.
/// @param[in] facet_indices A contiguous array of corner indices, where
/// facet_indices[facet_size*i+k] is the index of the k-th corner of
/// the i-th facet.
///
void add_polygons(Index num_facets, Index facet_size, span<const Index> facet_indices = {});
///
/// Adds multiple polygonal facets of the same size to the mesh.
///
/// @warning If the mesh contains edge/connectivity attributes, this function will throw an
/// exception if you pass an empty buffer of facet indices. This is because it is
/// impossible to update edge/connectivity information if the facet buffer is
/// directly modified by the user. Instead, the correct facet indices must be
/// provided when the facet is constructed.
///
/// @param[in] num_facets Number of facets to add.
/// @param[in] facet_size Size of each facet to be added.
/// @param[in] facet_indices A contiguous array of corner indices, where
/// facet_indices[facet_size*i+k] is the index of the k-th corner of
/// the i-th facet.
///
void add_polygons(
Index num_facets,
Index facet_size,
std::initializer_list<const Index> facet_indices);
///
/// Adds multiple polygonal facets of the same size to the mesh.
///
/// @param[in] num_facets Number of facets to add.
/// @param[in] facet_size Size of each facet to be added.
/// @param[in] set_facets_indices Callable function to set vertex indices of a given facet.
///
void add_polygons(
Index num_facets,
Index facet_size,
SetMultiFacetsIndicesFunction set_facets_indices);
///
/// Adds multiple polygonal facets of different sizes to the mesh.
///
/// @warning If the mesh contains edge/connectivity attributes, this function will throw an
/// exception if you pass an empty buffer of facet indices. This is because it is
/// impossible to update edge/connectivity information if the facet buffer is
/// directly modified by the user. Instead, the correct facet indices must be
/// provided when the facet is constructed.
///
/// @param[in] facet_sizes A contiguous array representing the size of each facet to add.
/// @param[in] facet_indices A contiguous array of corner indices, where
/// facet_indices[sum(facet_sizes(j), j<=i) + k] is the index of the
/// k-th corner of the i-th facet.
///
void add_hybrid(span<const Index> facet_sizes, span<const Index> facet_indices = {});
///
/// Adds multiple polygonal facets of different sizes to the mesh.
///
/// @warning If the mesh contains edge/connectivity attributes, this function will throw an
/// exception if you pass an empty buffer of facet indices. This is because it is
/// impossible to update edge/connectivity information if the facet buffer is
/// directly modified by the user. Instead, the correct facet indices must be
/// provided when the facet is constructed.
///
/// @param[in] facet_sizes A contiguous array representing the size of each facet to add.
/// @param[in] facet_indices A contiguous array of corner indices, where
/// facet_indices[sum(facet_sizes(j), j<=i) + k] is the index of the
/// k-th corner of the i-th facet.
///
void add_hybrid(
std::initializer_list<const Index> facet_sizes,
std::initializer_list<const Index> facet_indices);
///
/// Adds multiple polygonal facets of different sizes to the mesh.
///
/// @param[in] num_facets Number of facets to add.
/// @param[in] facet_sizes Callable function to retrieve the size of each facet to be
/// added.
/// @param[in] set_facets_indices Callable function to set vertex indices of a given facet.
///
void add_hybrid(
Index num_facets,
GetFacetsSizeFunction facet_sizes,
SetMultiFacetsIndicesFunction set_facets_indices);
///
/// Removes a list of vertices. The set of vertices should be provided as a sorted list,
/// otherwise an exception is raised.
///
/// @note This function will remove any facet incident to a removed vertex.
///
/// @param[in] vertices_to_remove The vertices to remove.
///
void remove_vertices(span<const Index> vertices_to_remove);
///
/// Removes a list of vertices. The set of vertices should be provided as a sorted list,
/// otherwise an exception is raised.
///
/// @note This function will remove any facet incident to a removed vertex.
///
/// @param[in] vertices_to_remove The vertices to remove.
///
void remove_vertices(std::initializer_list<const Index> vertices_to_remove);
///
/// Removes a list of vertices, defined by a predicate function.
///
/// @note This function will remove any facet incident to a removed vertex.
///
/// @param[in] should_remove_func Function to determine if a vertex of a particular index
/// should be removed.
///
void remove_vertices(function_ref<bool(Index)> should_remove_func);
///
/// Removes a list of facets. The set of facets should be provided as a sorted list, otherwise
/// an exception is raised.
///
/// @note This function does _not_ remove any isolated vertex that occurs due to facet
/// removal.
///
/// @param[in] facets_to_remove The facets to remove.
///
void remove_facets(span<const Index> facets_to_remove);
///
/// Removes a list of facets. The set of facets should be provided as a sorted list, otherwise
/// an exception is raised.
///
/// @note This function does _not_ remove any isolated vertex that occurs due to facet
/// removal.
///
/// @param[in] facets_to_remove The facets to remove.
///
void remove_facets(std::initializer_list<const Index> facets_to_remove);
///
/// Removes a list of facets, defined by a predicate function.
///
/// @note This function does _not_ remove any isolated vertex that occurs due to facet
/// removal.
///
/// @param[in] should_remove_func Function to determine if a facet of a particular index
/// should be removed.
///
void remove_facets(function_ref<bool(Index)> should_remove_func);
///
/// Clear buffer for mesh vertices and other vertex attributes. Since this function also removes
/// any invalid facet, the entire mesh will be cleared.
///
void clear_vertices();
///
/// Clear buffer for mesh facets and other facet/corner attributes. The resulting mesh will be a
/// point cloud made of isolated vertices.
///
void clear_facets();
///
/// Shrink buffer capacities to fit current mesh attributes, deallocating any extra capacity.
///
void shrink_to_fit();
///
/// Compress mesh storage if the mesh is regular. This iterates over all facets to check if the
/// mesh is regular. If so the following attributes are removed:
///
/// - "$facet_to_first_corner"
/// - "$corner_to_facet"
///
void compress_if_regular();
public:
/// @}
/// @name Mesh accessors
/// @{
///
/// Whether the mesh *must* only contain triangular facets. A mesh with no facet is considered a
/// triangle mesh.
///
/// @note A mesh with hybrid storage *may* still be a triangle mesh, which this method
/// does not check.
///
/// @return True if triangle mesh, False otherwise.
///
[[nodiscard]] bool is_triangle_mesh() const;
///
/// Whether the mesh *must* only contains quadrilateral facets. A mesh with no facet is
/// considered a quad mesh.
///
/// @note A mesh with hybrid storage *may* still be a quad mesh, which this method
/// does not check.
///
/// @return True if quad mesh, False otherwise.
///
[[nodiscard]] bool is_quad_mesh() const;
///
/// Whether the mesh *must* only contains facets of equal sizes. A mesh with no facet is
/// considered regular.
///
/// @note A mesh with hybrid storage *may* still be have regular facet sizes, which this
/// method does not check.
///
/// @return True if regular, False otherwise.
///
[[nodiscard]] bool is_regular() const;
///
/// Whether the mesh *may* contain facets of different sizes. This is the opposite of is_regular
/// (an empty mesh is _not_ considered hybrid).
///
/// @note A mesh with hybrid storage *may* still have all its facet be the same size,
/// which this method does not check.
///
/// @return True if hybrid, False otherwise.
///
[[nodiscard]] bool is_hybrid() const { return !is_regular(); }
///
/// Retrieves the dimension of the mesh vertices.
///
/// @return The mesh dimension.
///
[[nodiscard]] Index get_dimension() const { return m_dimension; }
///
/// Retrieves the number of vertex per facet in a regular mesh. If the mesh is a hybrid mesh, an
/// exception is thrown.s
///
/// @return The number of vertices per facet.
///
[[nodiscard]] Index get_vertex_per_facet() const;
///
/// Retrieves the number of vertices.
///
/// @return The number of vertices.
///
[[nodiscard]] Index get_num_vertices() const { return m_num_vertices; }
///
/// Retrieves the number of facets.
///
/// @return The number of facets.
///
[[nodiscard]] Index get_num_facets() const { return m_num_facets; }
///
/// Retrieves the number of corners.
///
/// @return The number of corners.
///
[[nodiscard]] Index get_num_corners() const { return m_num_corners; }
///
/// Retrieves the number of edges.
///
/// @return The number of edges.
///
[[nodiscard]] Index get_num_edges() const { return m_num_edges; }
///
/// Retrieves a read-only pointer to a vertex coordinates.
///
/// @param[in] v Vertex index.
///
/// @return Point coordinates of the queried vertex.
///
[[nodiscard]] span<const Scalar> get_position(Index v) const;
///
/// Retrieves a writeable pointer to a vertex coordinates.
///
/// @param[in] v Vertex index.
///
/// @return Point coordinates of the queried vertex.
///
[[nodiscard]] span<Scalar> ref_position(Index v);
///
/// Number of vertices in the facet.
///
/// @param[in] f Facet index.
///
/// @return Number of vertices in the queried facet.
///
[[nodiscard]] Index get_facet_size(Index f) const
{
return get_facet_corner_end(f) - get_facet_corner_begin(f);
}
///
/// Index of a vertex given from a facet + local index.
///
/// @param[in] f Facet index.
/// @param[in] lv Local vertex index inside the facet.
///
/// @return Index of the vertex in the mesh.
///
[[nodiscard]] Index get_facet_vertex(Index f, Index lv) const
{
return get_corner_vertex(get_facet_corner_begin(f) + lv);
}
///
/// First corner around the facet.
///
/// @param[in] f Facet index.
///
/// @return First corner index for the queried facet.
///
[[nodiscard]] Index get_facet_corner_begin(Index f) const;
///
/// Index past the last corner around the facet.
///
/// @param[in] f Facet index.
///
/// @return Index past the last corner index for the queried facet.
///
[[nodiscard]] Index get_facet_corner_end(Index f) const;
///
/// Retrieves the index of a vertex given its corner index. E.g. this can be used in conjunction
/// with get_facet_corner_begin and get_facet_corner_end to iterate over the vertices of a
/// facet.
///
/// @param[in] c Corner index.
///
/// @return Index of the vertex in the mesh.
///
[[nodiscard]] Index get_corner_vertex(Index c) const;
///
/// Retrieves the index of a facet given its corner index. If the mesh is regular, this is
/// simply the corner index / num of vertex per facet. If the mesh is a hybrid polygonal mesh,
/// this mapping is stored in a reserved attribute.s
///
/// @param[in] c Corner index.
///
/// @return Index of the facet containing this corner.
///
[[nodiscard]] Index get_corner_facet(Index c) const;
///
/// Retrieves a read-only pointer to a facet indices.
///
/// @param[in] f Facet index.
///
/// @return A pointer to the facet vertices.
///
[[nodiscard]] span<const Index> get_facet_vertices(Index f) const;
///
/// Retrieves a writable pointer to a facet indices.
///
/// @warning If the mesh contains edge/connectivity attributes, this function will throw an
/// exception. This is because it is impossible to update edge/connectivity
/// information if the facet buffer is directly modified by the user. Instead, the
/// correct facet indices must be provided when the facet is constructed.
///
/// @param[in] f Facet index.
///
/// @return A pointer to the facet vertices.
///
[[nodiscard]] span<Index> ref_facet_vertices(Index f);
public:
/// @}
/// @name Attribute construction
/// @{
///
/// Retrieve an attribute id given its name. If the attribute doesn't exist, invalid_attribute_id()
/// is returned instead.
///
/// @param[in] name %Attribute name.
///
/// @return The attribute identifier.
///
[[nodiscard]] AttributeId get_attribute_id(std::string_view name) const;
///
/// Retrieve attribute name from its id.
///
/// @param[in] id %Attribute id.
///
/// @return %Attribute's name.
///
/// @throws Exception if id is invalid.
///
[[nodiscard]] std::string_view get_attribute_name(AttributeId id) const;
///
/// Create a new attribute and return the newly created attribute id. A mesh attribute is stored
/// as a row-major R x C matrix. The number of rows (R) is determined by the number of elements
/// in the mesh that the attribute is attached to. The number of columns (C) is determined by
/// the user when the attribute is created (num_channels), and cannot be modified afterwards.
/// Note that the attribute tag determines how many channels are acceptable for certain types of
/// attributes.
///
/// @param[in] name %Attribute name to create.
/// @param[in] element %Mesh element to which the attribute is attached to (Vertex,
/// Facet, etc.).
/// @param[in] num_channels The number of channels for the attribute. Cannot be modified
/// once the attribute has been created.
/// @param[in] usage Tag to indicate how the values are modified under rigid
/// transformation.
/// @param[in] initial_values A span of initial values to populate the attribute values with.
/// The data is copied into the attribute. If the span is provided,
/// it must have the right dimension (number of elements x number
/// of channels).
/// @param[in] initial_indices A span of initial values to populate the attribute indices
/// with. If the attribute element type is not Indexed, providing a
/// non-empty value for this argument will result in a runtime
/// error. The data is copied into the attribute. If the span is
/// provided, it must have the right dimension (number of corners).
/// @param[in] policy %Attribute creation policy. By default using a reserved
/// attribute name (starting with a "$") will throw an exception.
///
/// @tparam ValueType Value type for the attribute.
///
/// @return The attribute identifier.
///
/// @see AttributeUsage
///
template <typename ValueType>
AttributeId create_attribute(
std::string_view name,
AttributeElement element,
size_t num_channels = 1,
AttributeUsage usage = AttributeUsage::Vector,
span<const ValueType> initial_values = {},
span<const Index> initial_indices = {},
AttributeCreatePolicy policy = AttributeCreatePolicy::ErrorIfReserved);
///
/// @overload
///
/// Create a new attribute and return the newly created attribute id. A mesh attribute is stored
/// as a row-major R x C matrix. The number of rows (R) is determined by the number of elements
/// in the mesh that the attribute is attached to. The number of columns (C) is determined by
/// the user when the attribute is created (num_channels), and cannot be modified afterwards.
/// Note that the attribute tag determines how many channels are acceptable for certain types of
/// attributes.
///
/// @param[in] name %Attribute name to create.
/// @param[in] element %Mesh element to which the attribute is attached to (Vertex,
/// Facet, etc.).
/// @param[in] usage Tag to indicate how the values are modified under rigid
/// transformation.
/// @param[in] num_channels The number of channels for the attribute. Cannot be modified
/// once the attribute has been created.
/// @param[in] initial_values A span of initial values to populate the attribute values with.
/// The data is copied into the attribute. If the span is provided,
/// it must have the right dimension (number of elements x number
/// of channels).
/// @param[in] initial_indices A span of initial values to populate the attribute indices
/// with. If the attribute element type is not Indexed, providing a
/// non-empty value for this argument will result in a runtime
/// error. The data is copied into the attribute. If the span is
/// provided, it must have the right dimension (number of corners).
/// @param[in] policy %Attribute creation policy. By default using a reserved
/// attribute name (starting with a "$") will throw an exception.
///
/// @tparam ValueType Value type for the attribute.
///
/// @return The attribute identifier.
///
/// @see AttributeUsage
///
template <typename ValueType>
AttributeId create_attribute(
std::string_view name,
AttributeElement element,
AttributeUsage usage,
size_t num_channels = 1,
span<const ValueType> initial_values = {},
span<const Index> initial_indices = {},
AttributeCreatePolicy policy = AttributeCreatePolicy::ErrorIfReserved);
///
/// Creates an new attribute by creating a shallow copy of another mesh's attribute. The mesh
/// can reference itself. This only performs a shallow copy, sharing the underlying data buffer.
/// If either meshes performs a write operation, a deep copy will be performed (the
/// modifications are not shared). Note that if the number of elements differs between the
/// source and target meshes, an exception is raised.
///
/// @param[in] name %Attribute name to create.
/// @param[in] source_mesh Source mesh from which to copy the attribute from. The mesh can
/// reference itself.
/// @param[in] source_name Attribute name to copy from. If left empty, the target attribute
/// name will be used.
///
/// @tparam OtherScalar Source mesh scalar type.
/// @tparam OtherIndex Source mesh index type.
///
/// @return The attribute identifier.
///
template <typename OtherScalar, typename OtherIndex>
AttributeId create_attribute_from(
std::string_view name,
const SurfaceMesh<OtherScalar, OtherIndex>& source_mesh,
std::string_view source_name = {});
///
/// Wraps a writable external buffer as a mesh attribute. The buffer must remain valid during
/// the lifetime of the mesh object (and any derived meshes that might have been copied from
/// it).
///
/// @param[in] name %Attribute name to create.
/// @param[in] element %Mesh element to which the attribute is attached to (Vertex,
/// Facet, etc.). The element type must not be Indexed. Please use
/// wrap_as_indexed_attribute for that.
/// @param[in] usage Tag to indicate how the values are modified under rigid
/// transformation.
/// @param[in] num_channels The number of channels for the attribute. Cannot be modified once
/// the attribute has been created.
/// @param[in] values_view A span of the external buffer to use as storage for the attribute
/// values. The provided span must have enough capacity to hold
/// (number of elements x number of channels) items. It is ok to
/// provide a span with a larger capacity than needed, which will
/// allow for the mesh to grow.
///
/// @tparam ValueType Value type for the attribute.
///
/// @return The attribute identifier.
///
template <typename ValueType>
AttributeId wrap_as_attribute(
std::string_view name,
AttributeElement element,
AttributeUsage usage,
size_t num_channels,
span<ValueType> values_view);
///
/// @overload
///
/// @note
/// This function differs from the prevous version by participating in
/// shared ownership management of the buffer referred by `shared_values`.
///
template <typename ValueType>
AttributeId wrap_as_attribute(
std::string_view name,
AttributeElement element,
AttributeUsage usage,
size_t num_channels,
SharedSpan<ValueType> shared_values);
///
/// Wraps a read-only external buffer as a mesh attribute. The buffer must remain valid during
/// the lifetime of the mesh object (and any derived meshes that might have been copied from
/// it). Any operation that attempts to write data to the attribute will throw a runtime
/// exception.
///
/// @param[in] name %Attribute name to create.
/// @param[in] element %Mesh element to which the attribute is attached to (Vertex,
/// Facet, etc.). The element type must not be Indexed. Please use
/// wrap_as_indexed_attribute for that.
/// @param[in] usage Tag to indicate how the values are modified under rigid
/// transformation.
/// @param[in] num_channels The number of channels for the attribute. Cannot be modified once
/// the attribute has been created.
/// @param[in] values_view A span of the external buffer to use as storage for the attribute
/// values. The provided span must have a size >= (number of elements
/// x number of channels) items. It is ok to provide a span with a
/// larger capacity than needed.
///
/// @tparam ValueType Value type for the attribute.
///
/// @return The attribute identifier.
///
template <typename ValueType>
AttributeId wrap_as_const_attribute(
std::string_view name,
AttributeElement element,
AttributeUsage usage,
size_t num_channels,
span<const ValueType> values_view);
///
/// @overload
///
/// @note
/// This function differs from the prevous version by participating in
/// shared ownership management of the buffer referred by `shared_values`.
///
template <typename ValueType>