MeshLib C++ Docs
Loading...
Searching...
No Matches
MRMesh/MRId.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include <cassert>
5#include <cstddef>
6#include <type_traits>
7
8namespace MR
9{
10
11// stores index of some element, it is made as template class to avoid mixing faces, edges and vertices
12template <typename T>
13class Id
14{
15public:
16 using ValueType = int; //the type used for internal representation of Id
17
18 constexpr Id() noexcept : id_( -1 ) { }
19 explicit Id( NoInit ) noexcept { }
20
21 // Allow constructing from `int` and other integral types.
22 // This constructor is written like this instead of a plain `Id(int)`, because we also wish to disable construction
23 // from other unrelated `Id<U>` specializations, which themselves have implicit conversions to `int`.
24 // We could also achieve that using `template <typename U> Id(Id<U>) = delete;`, but it turns out that that causes issues
25 // for the `EdgeId::operator UndirectedEdgeId` below. There, while `UndirectedEdgeId x = EdgeId{};` compiles with this approach,
26 // but `UndirectedEdgeId x(EdgeId{});` doesn't. So to allow both forms, this constructor must be written this way, as a template.
27 template <typename U, std::enable_if_t<std::is_integral_v<U>, std::nullptr_t> = nullptr>
28 explicit constexpr Id( U i ) noexcept : id_( ValueType( i ) ) { }
29
30 constexpr operator ValueType() const { return id_; }
31 constexpr bool valid() const { return id_ >= 0; }
32 explicit constexpr operator bool() const { return id_ >= 0; }
33 constexpr ValueType & get() noexcept { return id_; }
34
35 constexpr bool operator == (Id b) const { return id_ == b.id_; }
36 constexpr bool operator != (Id b) const { return id_ != b.id_; }
37 constexpr bool operator < (Id b) const { return id_ < b.id_; }
38
39 template <typename U>
40 bool operator == (Id<U> b) const = delete;
41 template <typename U>
42 bool operator != (Id<U> b) const = delete;
43 template <typename U>
44 bool operator < (Id<U> b) const = delete;
45
46 constexpr Id & operator --() { --id_; return * this; }
47 constexpr Id & operator ++() { ++id_; return * this; }
48
49 constexpr Id operator --( int ) { auto res = *this; --id_; return res; }
50 constexpr Id operator ++( int ) { auto res = *this; ++id_; return res; }
51
52 constexpr Id & operator -=( ValueType a ) { id_ -= a; return * this; }
53 constexpr Id & operator +=( ValueType a ) { id_ += a; return * this; }
54
55private:
56 ValueType id_;
57};
58
59// Variant of Id<T> with omitted initialization by default. Useful for containers.
60template <typename T>
61class NoInitId : public Id<T>
62{
63public:
64 NoInitId() : Id<T>( noInit ) {}
65 NoInitId( Id<T> id ) : Id<T>( id ) {}
66};
67
68template <>
69class Id<MR::EdgeTag> // Need `MR::` here to simplify binding generation. See libclang bug: https://github.com/llvm/llvm-project/issues/92371
70{
71public:
72 using ValueType = int; //the type used for internal representation of Id
73
74 constexpr Id() noexcept : id_( -1 ) { }
75 explicit Id( NoInit ) noexcept { }
76 constexpr Id( UndirectedEdgeId u ) noexcept : id_( (ValueType)u << 1 ) { assert( u.valid() ); }
77 explicit constexpr Id( ValueType i ) noexcept : id_( i ) { }
78 explicit constexpr Id( unsigned int i ) noexcept : id_( i ) { }
79 explicit constexpr Id( size_t i ) noexcept : id_( ValueType( i ) ) { }
80 constexpr operator ValueType() const { return id_; }
81 constexpr bool valid() const { return id_ >= 0; }
82 explicit constexpr operator bool() const { return id_ >= 0; }
83 constexpr ValueType & get() noexcept { return id_; }
84
85 // returns identifier of the edge with same ends but opposite orientation
86 constexpr Id sym() const { assert( valid() ); return Id(id_ ^ 1); }
87 // among each pair of sym-edges: one is always even and the other is odd
88 constexpr bool even() const { assert( valid() ); return (id_ & 1) == 0; }
89 constexpr bool odd() const { assert( valid() ); return (id_ & 1) == 1; }
90 // returns unique identifier of the edge ignoring its direction
91 constexpr UndirectedEdgeId undirected() const { assert( valid() ); return UndirectedEdgeId( id_ >> 1 ); }
92 constexpr operator UndirectedEdgeId() const { return undirected(); }
93
94 constexpr bool operator == (Id b) const { return id_ == b.id_; }
95 constexpr bool operator != (Id b) const { return id_ != b.id_; }
96 constexpr bool operator < (Id b) const { return id_ < b.id_; }
97
98 template <typename U>
99 bool operator == (Id<U> b) const = delete;
100 template <typename U>
101 bool operator != (Id<U> b) const = delete;
102 template <typename U>
103 bool operator < (Id<U> b) const = delete;
104
105 constexpr Id & operator --() { --id_; return * this; }
106 constexpr Id & operator ++() { ++id_; return * this; }
107
108 constexpr Id operator --( int ) { auto res = *this; --id_; return res; }
109 constexpr Id operator ++( int ) { auto res = *this; ++id_; return res; }
110
111 constexpr Id & operator -=( ValueType a ) { id_ -= a; return * this; }
112 constexpr Id & operator +=( ValueType a ) { id_ += a; return * this; }
113
114private:
115 ValueType id_;
116};
117
118template <>
120{
121public:
122 using ValueType = size_t; //the type used for internal representation of Id
123
124 constexpr Id() noexcept : id_( ~ValueType( 0 ) ) { }
125 explicit Id( NoInit ) noexcept { }
126 explicit constexpr Id( ValueType i ) noexcept : id_( i ) { }
127 explicit constexpr Id( int ) noexcept = delete;
128 constexpr operator ValueType() const { return id_; }
129 constexpr bool valid() const { return id_ != ~ValueType( 0 ); }
130 explicit constexpr operator bool() const { return id_ != ~ValueType( 0 ); }
131 constexpr ValueType& get() noexcept { return id_; }
132
133 constexpr bool operator == (Id b) const { return id_ == b.id_; }
134 constexpr bool operator != (Id b) const { return id_ != b.id_; }
135 constexpr bool operator < (Id b) const { return id_ < b.id_; }
136
137 template <typename U>
138 bool operator == (Id<U> b) const = delete;
139 template <typename U>
140 bool operator != (Id<U> b) const = delete;
141 template <typename U>
142 bool operator < (Id<U> b) const = delete;
143
144 constexpr Id & operator --() { --id_; return * this; }
145 constexpr Id & operator ++() { ++id_; return * this; }
146
147 constexpr Id operator --( int ) { auto res = *this; --id_; return res; }
148 constexpr Id operator ++( int ) { auto res = *this; ++id_; return res; }
149
150 constexpr Id & operator -=( ValueType a ) { id_ -= a; return * this; }
151 constexpr Id & operator +=( ValueType a ) { id_ += a; return * this; }
152
153private:
154 ValueType id_;
155};
156
157template <typename T>
158inline constexpr Id<T> operator + ( Id<T> id, int a ) { return Id<T>{ id.get() + a }; }
159template <typename T>
160inline constexpr Id<T> operator + ( Id<T> id, unsigned int a ) { return Id<T>{ id.get() + a }; }
161template <typename T>
162inline constexpr Id<T> operator + ( Id<T> id, size_t a ) { return Id<T>{ id.get() + a }; }
163
164template <typename T>
165inline constexpr Id<T> operator - ( Id<T> id, int a ) { return Id<T>{ id.get() - a }; }
166template <typename T>
167inline constexpr Id<T> operator - ( Id<T> id, unsigned int a ) { return Id<T>{ id.get() - a }; }
168template <typename T>
169inline constexpr Id<T> operator - ( Id<T> id, size_t a ) { return Id<T>{ id.get() - a }; }
170
171inline constexpr FaceId operator ""_f( unsigned long long i ) noexcept { return FaceId{ (int)i }; }
172inline constexpr VertId operator ""_v( unsigned long long i ) noexcept { return VertId{ (int)i }; }
173inline constexpr EdgeId operator ""_e( unsigned long long i ) noexcept { return EdgeId{ (int)i }; }
174inline constexpr UndirectedEdgeId operator ""_ue( unsigned long long i ) noexcept { return UndirectedEdgeId{ (int)i }; }
175inline constexpr VoxelId operator ""_vox( unsigned long long i ) noexcept { return VoxelId{ size_t( i ) }; }
176
177} //namespace MR
constexpr ValueType & get() noexcept
Definition MRMesh/MRId.h:83
constexpr bool valid() const
Definition MRMesh/MRId.h:81
constexpr bool odd() const
Definition MRMesh/MRId.h:89
constexpr UndirectedEdgeId undirected() const
Definition MRMesh/MRId.h:91
constexpr Id sym() const
Definition MRMesh/MRId.h:86
Id(NoInit) noexcept
Definition MRMesh/MRId.h:75
int ValueType
Definition MRMesh/MRId.h:72
constexpr Id(size_t i) noexcept
Definition MRMesh/MRId.h:79
constexpr Id(unsigned int i) noexcept
Definition MRMesh/MRId.h:78
constexpr Id(UndirectedEdgeId u) noexcept
Definition MRMesh/MRId.h:76
constexpr Id() noexcept
Definition MRMesh/MRId.h:74
constexpr bool even() const
Definition MRMesh/MRId.h:88
constexpr Id(ValueType i) noexcept
Definition MRMesh/MRId.h:77
constexpr bool valid() const
Definition MRMesh/MRId.h:129
size_t ValueType
Definition MRMesh/MRId.h:122
constexpr ValueType & get() noexcept
Definition MRMesh/MRId.h:131
constexpr Id() noexcept
Definition MRMesh/MRId.h:124
Id(NoInit) noexcept
Definition MRMesh/MRId.h:125
constexpr Id(int) noexcept=delete
constexpr Id(ValueType i) noexcept
Definition MRMesh/MRId.h:126
Definition MRMesh/MRId.h:14
constexpr bool operator!=(Id b) const
Definition MRMesh/MRId.h:36
constexpr Id & operator+=(ValueType a)
Definition MRMesh/MRId.h:53
constexpr Id & operator++()
Definition MRMesh/MRId.h:47
Id(NoInit) noexcept
Definition MRMesh/MRId.h:19
constexpr bool operator==(Id b) const
Definition MRMesh/MRId.h:35
constexpr bool valid() const
Definition MRMesh/MRId.h:31
constexpr Id & operator-=(ValueType a)
Definition MRMesh/MRId.h:52
constexpr Id & operator--()
Definition MRMesh/MRId.h:46
constexpr Id() noexcept
Definition MRMesh/MRId.h:18
constexpr bool operator<(Id b) const
Definition MRMesh/MRId.h:37
constexpr ValueType & get() noexcept
Definition MRMesh/MRId.h:33
int ValueType
Definition MRMesh/MRId.h:16
constexpr Id(U i) noexcept
Definition MRMesh/MRId.h:28
Definition MRMesh/MRId.h:62
NoInitId()
Definition MRMesh/MRId.h:64
NoInitId(Id< T > id)
Definition MRMesh/MRId.h:65
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:369
Definition MRCameraOrientationPlugin.h:8
class MRMESH_CLASS VoxelTag
Definition MRMesh/MRMeshFwd.h:98
constexpr NoInit noInit
Definition MRMesh/MRMeshFwd.h:90
class MRMESH_CLASS EdgeTag
Definition MRMesh/MRMeshFwd.h:93
Color operator+(const Color &a, const Color &b)
Definition MRMesh/MRColor.h:108
Definition MRMesh/MRMeshFwd.h:89