MeshLib C++ Docs
Loading...
Searching...
No Matches
MRId.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include "MRMacros.h"
5#include <cassert>
6#include <cstddef>
7#include <type_traits>
8#include <utility>
9
10namespace MR
11{
14
15
17template <typename T>
18class Id
19{
20public:
21 using ValueType = int;
22
23 constexpr Id() noexcept : id_( -1 ) { }
24 explicit Id( NoInit ) noexcept { }
25
33 template <typename U = int
36 #if MR_HAS_REQUIRES
37 > requires std::is_integral_v<U>
38 #else
39 , std::enable_if_t<std::is_integral_v<U>, std::nullptr_t> = nullptr>
40 #endif
41 explicit constexpr Id( U i ) noexcept : id_( ValueType( i ) ) { }
42
43 constexpr operator ValueType() const { return id_; }
44 constexpr bool valid() const { return id_ >= 0; }
45 explicit constexpr operator bool() const { return id_ >= 0; }
46 constexpr ValueType & get() noexcept { return id_; }
47
48 constexpr bool operator == (Id b) const { return id_ == b.id_; }
49 constexpr bool operator != (Id b) const { return id_ != b.id_; }
50 constexpr bool operator < (Id b) const { return id_ < b.id_; }
51
52 template <typename U>
53 bool operator == (Id<U> b) const = delete;
54 template <typename U>
55 bool operator != (Id<U> b) const = delete;
56 template <typename U>
57 bool operator < (Id<U> b) const = delete;
58
59 constexpr Id & operator --() { --id_; return * this; }
60 constexpr Id & operator ++() { ++id_; return * this; }
61
62 constexpr Id operator --( int ) { auto res = *this; --id_; return res; }
63 constexpr Id operator ++( int ) { auto res = *this; ++id_; return res; }
64
65 constexpr Id & operator -=( ValueType a ) { id_ -= a; return * this; }
66 constexpr Id & operator +=( ValueType a ) { id_ += a; return * this; }
67
70#if !MR_PARSING_FOR_C_BINDINGS && !MR_COMPILING_C_BINDINGS
71private:
72#endif
73
74 ValueType id_;
75};
76
78template <typename T>
79class NoInitId : public Id<T>
80{
81public:
82 NoInitId() : Id<T>( noInit ) {}
83 NoInitId( Id<T> id ) : Id<T>( id ) {}
84};
85
86template <>
87class Id<EdgeTag>
88{
89public:
90 using ValueType = int;
91
92 constexpr Id() noexcept : id_( -1 ) { }
93 explicit Id( NoInit ) noexcept { }
94 constexpr Id( UndirectedEdgeId u ) noexcept : id_( (ValueType)u << 1 ) { assert( u.valid() ); }
95 explicit constexpr Id( ValueType i ) noexcept : id_( i ) { }
96 explicit constexpr Id( unsigned int i ) noexcept : id_( i ) { }
97 explicit constexpr Id( size_t i ) noexcept : id_( ValueType( i ) ) { }
98 constexpr operator ValueType() const { return id_; }
99 constexpr bool valid() const { return id_ >= 0; }
100 explicit constexpr operator bool() const { return id_ >= 0; }
101 constexpr ValueType & get() noexcept { return id_; }
102
104 constexpr Id sym() const { assert( valid() ); return Id(id_ ^ 1); }
106 constexpr bool even() const { assert( valid() ); return (id_ & 1) == 0; }
107 constexpr bool odd() const { assert( valid() ); return (id_ & 1) == 1; }
109 constexpr UndirectedEdgeId undirected() const { assert( valid() ); return UndirectedEdgeId( id_ >> 1 ); }
110 constexpr operator UndirectedEdgeId() const { return undirected(); }
111
112 constexpr bool operator == (Id b) const { return id_ == b.id_; }
113 constexpr bool operator != (Id b) const { return id_ != b.id_; }
114 constexpr bool operator < (Id b) const { return id_ < b.id_; }
115
116 template <typename U>
117 bool operator == (Id<U> b) const = delete;
118 template <typename U>
119 bool operator != (Id<U> b) const = delete;
120 template <typename U>
121 bool operator < (Id<U> b) const = delete;
122
123 constexpr Id & operator --() { --id_; return * this; }
124 constexpr Id & operator ++() { ++id_; return * this; }
125
126 constexpr Id operator --( int ) { auto res = *this; --id_; return res; }
127 constexpr Id operator ++( int ) { auto res = *this; ++id_; return res; }
128
129 constexpr Id & operator -=( ValueType a ) { id_ -= a; return * this; }
130 constexpr Id & operator +=( ValueType a ) { id_ += a; return * this; }
131
133#if !MR_PARSING_FOR_C_BINDINGS && !MR_COMPILING_C_BINDINGS
134private:
135#endif
136
137 ValueType id_;
138};
139
140template <>
141class Id<VoxelTag>
142{
143public:
144 using ValueType = size_t;
145
146 constexpr Id() noexcept : id_( ~ValueType( 0 ) ) { }
147 explicit Id( NoInit ) noexcept { }
148 explicit constexpr Id( ValueType i ) noexcept : id_( i ) { }
149 explicit constexpr Id( int ) noexcept = delete;
150 constexpr operator ValueType() const { return id_; }
151 constexpr bool valid() const { return id_ != ~ValueType( 0 ); }
152 explicit constexpr operator bool() const { return id_ != ~ValueType( 0 ); }
153 constexpr ValueType& get() noexcept { return id_; }
154
155 constexpr bool operator == (Id b) const { return id_ == b.id_; }
156 constexpr bool operator != (Id b) const { return id_ != b.id_; }
157 constexpr bool operator < (Id b) const { return id_ < b.id_; }
158
159 template <typename U>
160 bool operator == (Id<U> b) const = delete;
161 template <typename U>
162 bool operator != (Id<U> b) const = delete;
163 template <typename U>
164 bool operator < (Id<U> b) const = delete;
165
166 constexpr Id & operator --() { --id_; return * this; }
167 constexpr Id & operator ++() { ++id_; return * this; }
168
169 constexpr Id operator --( int ) { auto res = *this; --id_; return res; }
170 constexpr Id operator ++( int ) { auto res = *this; ++id_; return res; }
171
172 constexpr Id & operator -=( ValueType a ) { id_ -= a; return * this; }
173 constexpr Id & operator +=( ValueType a ) { id_ += a; return * this; }
174
176#if !MR_PARSING_FOR_C_BINDINGS && !MR_COMPILING_C_BINDINGS
177private:
178#endif
179
180 ValueType id_;
181};
182
183template <typename T>
184inline constexpr Id<T> operator + ( Id<T> id, int a ) { return Id<T>{ id.get() + a }; }
185template <typename T>
186inline constexpr Id<T> operator + ( Id<T> id, unsigned int a ) { return Id<T>{ id.get() + a }; }
187template <typename T>
188inline constexpr Id<T> operator + ( Id<T> id, size_t a ) { return Id<T>{ id.get() + a }; }
189
190template <typename T>
191inline constexpr Id<T> operator - ( Id<T> id, int a ) { return Id<T>{ id.get() - a }; }
192template <typename T>
193inline constexpr Id<T> operator - ( Id<T> id, unsigned int a ) { return Id<T>{ id.get() - a }; }
194template <typename T>
195inline constexpr Id<T> operator - ( Id<T> id, size_t a ) { return Id<T>{ id.get() - a }; }
196
197inline constexpr FaceId operator ""_f( unsigned long long i ) noexcept { return FaceId{ (int)i }; }
198inline constexpr VertId operator ""_v( unsigned long long i ) noexcept { return VertId{ (int)i }; }
199inline constexpr EdgeId operator ""_e( unsigned long long i ) noexcept { return EdgeId{ (int)i }; }
200inline constexpr UndirectedEdgeId operator ""_ue( unsigned long long i ) noexcept { return UndirectedEdgeId{ (int)i }; }
201inline constexpr VoxelId operator ""_vox( unsigned long long i ) noexcept { return VoxelId{ size_t( i ) }; }
202
203}
204
205template <typename T>
206struct std::hash<MR::Id<T>>
207{
208 size_t operator() ( MR::Id<T> const& p ) const noexcept
209 {
210 return (int)p;
211 }
212};
stores index of some element, it is made as template class to avoid mixing faces, edges and vertices
Definition MRId.h:19
Variant of Id<T> with omitted initialization by default. Useful for containers.
Definition MRId.h:80
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:457
constexpr bool odd() const
Definition MRId.h:107
constexpr bool valid() const
Definition MRId.h:151
size_t ValueType
Definition MRId.h:144
constexpr ValueType & get() noexcept
Definition MRId.h:153
constexpr Id() noexcept
the type used for internal representation of Id
Definition MRId.h:146
constexpr bool valid() const
Definition MRId.h:99
constexpr bool operator!=(Id b) const
Definition MRId.h:49
constexpr Id & operator+=(ValueType a)
Definition MRId.h:66
constexpr Id & operator++()
Definition MRId.h:60
int ValueType
Definition MRId.h:90
NoInitId()
Definition MRId.h:82
constexpr Id sym() const
returns identifier of the edge with same ends but opposite orientation
Definition MRId.h:104
Id(NoInit) noexcept
Definition MRId.h:24
constexpr bool operator==(Id b) const
Definition MRId.h:48
Id(NoInit) noexcept
Definition MRId.h:93
constexpr bool valid() const
Definition MRId.h:44
constexpr Id(UndirectedEdgeId u) noexcept
Definition MRId.h:94
constexpr Id(ValueType i) noexcept
Definition MRId.h:95
constexpr Id & operator-=(ValueType a)
Definition MRId.h:65
constexpr Id & operator--()
Definition MRId.h:59
Id(NoInit) noexcept
Definition MRId.h:147
constexpr Id(int) noexcept=delete
constexpr Id() noexcept
the type used for internal representation of Id
Definition MRId.h:23
NoInitId(Id< T > id)
Definition MRId.h:83
constexpr Id(ValueType i) noexcept
Definition MRId.h:148
constexpr bool operator<(Id b) const
Definition MRId.h:50
constexpr ValueType & get() noexcept
Definition MRId.h:46
constexpr Id(size_t i) noexcept
Definition MRId.h:97
constexpr Id() noexcept
the type used for internal representation of Id
Definition MRId.h:92
int ValueType
Definition MRId.h:21
constexpr Id(unsigned int i) noexcept
Definition MRId.h:96
constexpr Id(U i) noexcept
Definition MRId.h:41
constexpr ValueType & get() noexcept
Definition MRId.h:101
constexpr bool even() const
among each pair of sym-edges: one is always even and the other is odd
Definition MRId.h:106
constexpr UndirectedEdgeId undirected() const
returns unique identifier of the edge ignoring its direction
Definition MRId.h:109
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:109
only for bindings generation
Definition MRCameraOrientationPlugin.h:8