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 "MRPch/MRBindingMacros.h"
6#include <cassert>
7#include <cstddef>
8#include <type_traits>
9#include <utility>
10
11namespace MR
12{
15
16
18template <typename T>
19class Id
20{
21public:
22 using ValueType = int;
23
24 constexpr Id() noexcept : id_( -1 ) { }
25 explicit Id( NoInit ) noexcept { }
26
34 template <typename U = int, std::enable_if_t<std::is_integral_v<U>, std::nullptr_t> = nullptr>
35 MR_BIND_IGNORE explicit constexpr Id( U i ) noexcept : id_( ValueType( i ) ) { }
36 #ifdef MR_PARSING_FOR_ANY_BINDINGS
38 explicit constexpr Id( int i ) noexcept : id_( ValueType( i ) ) {}
39 #endif
40
41 constexpr operator ValueType() const { return id_; }
42 constexpr bool valid() const { return id_ >= 0; }
43 explicit constexpr operator bool() const { return id_ >= 0; }
44 constexpr ValueType & get() noexcept { return id_; }
45
46 constexpr bool operator == (Id b) const { return id_ == b.id_; }
47 constexpr bool operator != (Id b) const { return id_ != b.id_; }
48 constexpr bool operator < (Id b) const { return id_ < b.id_; }
49
50 template <typename U>
51 bool operator == (Id<U> b) const = delete;
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
57 constexpr Id & operator --() { --id_; return * this; }
58 constexpr Id & operator ++() { ++id_; return * this; }
59
60 constexpr Id operator --( int ) { auto res = *this; --id_; return res; }
61 constexpr Id operator ++( int ) { auto res = *this; ++id_; return res; }
62
63 constexpr Id & operator -=( ValueType a ) { id_ -= a; return * this; }
64 constexpr Id & operator +=( ValueType a ) { id_ += a; return * this; }
65
68#if !MR_PARSING_FOR_C_BINDINGS && !MR_COMPILING_C_BINDINGS
69private:
70#endif
71
72 ValueType id_;
73};
74
76template <typename T>
77class NoInitId : public Id<T>
78{
79public:
80 NoInitId() : Id<T>( noInit ) {}
81 NoInitId( Id<T> id ) : Id<T>( id ) {}
82};
83
84template <>
85class Id<EdgeTag>
86{
87public:
88 using ValueType = int;
89
90 constexpr Id() noexcept : id_( -1 ) { }
91 explicit Id( NoInit ) noexcept { }
92 constexpr Id( UndirectedEdgeId u ) noexcept : id_( (ValueType)u << 1 ) { assert( u.valid() ); }
93 explicit constexpr Id( ValueType i ) noexcept : id_( i ) { }
94 explicit constexpr Id( unsigned int i ) noexcept : id_( i ) { }
95 explicit constexpr Id( size_t i ) noexcept : id_( ValueType( i ) ) { }
96 constexpr operator ValueType() const { return id_; }
97 constexpr bool valid() const { return id_ >= 0; }
98 explicit constexpr operator bool() const { return id_ >= 0; }
99 constexpr ValueType & get() noexcept { return id_; }
100
102 constexpr Id sym() const { assert( valid() ); return Id(id_ ^ 1); }
104 constexpr bool even() const { assert( valid() ); return (id_ & 1) == 0; }
105 constexpr bool odd() const { assert( valid() ); return (id_ & 1) == 1; }
107 constexpr UndirectedEdgeId undirected() const { assert( valid() ); return UndirectedEdgeId( id_ >> 1 ); }
108 constexpr operator UndirectedEdgeId() const { return undirected(); }
109
110 constexpr bool operator == (Id b) const { return id_ == b.id_; }
111 constexpr bool operator != (Id b) const { return id_ != b.id_; }
112 constexpr bool operator < (Id b) const { return id_ < b.id_; }
113
114 template <typename U>
115 bool operator == (Id<U> b) const = delete;
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
121 constexpr Id & operator --() { --id_; return * this; }
122 constexpr Id & operator ++() { ++id_; return * this; }
123
124 constexpr Id operator --( int ) { auto res = *this; --id_; return res; }
125 constexpr Id operator ++( int ) { auto res = *this; ++id_; return res; }
126
127 constexpr Id & operator -=( ValueType a ) { id_ -= a; return * this; }
128 constexpr Id & operator +=( ValueType a ) { id_ += a; return * this; }
129
131#if !MR_PARSING_FOR_C_BINDINGS && !MR_COMPILING_C_BINDINGS
132private:
133#endif
134
135 ValueType id_;
136};
137
138template <>
140{
141public:
142 using ValueType = size_t;
143
144 constexpr Id() noexcept : id_( ~ValueType( 0 ) ) { }
145 explicit Id( NoInit ) noexcept { }
146 explicit constexpr Id( ValueType i ) noexcept : id_( i ) { }
147 explicit constexpr Id( int ) noexcept = delete;
148 constexpr operator ValueType() const { return id_; }
149 constexpr bool valid() const { return id_ != ~ValueType( 0 ); }
150 explicit constexpr operator bool() const { return id_ != ~ValueType( 0 ); }
151 constexpr ValueType& get() noexcept { return id_; }
152
153 constexpr bool operator == (Id b) const { return id_ == b.id_; }
154 constexpr bool operator != (Id b) const { return id_ != b.id_; }
155 constexpr bool operator < (Id b) const { return id_ < b.id_; }
156
157 template <typename U>
158 bool operator == (Id<U> b) const = delete;
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
164 constexpr Id & operator --() { --id_; return * this; }
165 constexpr Id & operator ++() { ++id_; return * this; }
166
167 constexpr Id operator --( int ) { auto res = *this; --id_; return res; }
168 constexpr Id operator ++( int ) { auto res = *this; ++id_; return res; }
169
170 constexpr Id & operator -=( ValueType a ) { id_ -= a; return * this; }
171 constexpr Id & operator +=( ValueType a ) { id_ += a; return * this; }
172
174#if !MR_PARSING_FOR_C_BINDINGS && !MR_COMPILING_C_BINDINGS
175private:
176#endif
177
178 ValueType id_;
179};
180
181template <typename T>
182inline constexpr Id<T> operator + ( Id<T> id, int a ) { return Id<T>{ id.get() + a }; }
183template <typename T>
184inline constexpr Id<T> operator + ( Id<T> id, unsigned int a ) { return Id<T>{ id.get() + a }; }
185template <typename T>
186inline constexpr Id<T> operator + ( Id<T> id, size_t a ) { return Id<T>{ id.get() + a }; }
187
188template <typename T>
189inline constexpr Id<T> operator - ( Id<T> id, int a ) { return Id<T>{ id.get() - a }; }
190template <typename T>
191inline constexpr Id<T> operator - ( Id<T> id, unsigned int a ) { return Id<T>{ id.get() - a }; }
192template <typename T>
193inline constexpr Id<T> operator - ( Id<T> id, size_t a ) { return Id<T>{ id.get() - a }; }
194
195inline constexpr FaceId operator ""_f( unsigned long long i ) noexcept { return FaceId{ (int)i }; }
196inline constexpr VertId operator ""_v( unsigned long long i ) noexcept { return VertId{ (int)i }; }
197inline constexpr EdgeId operator ""_e( unsigned long long i ) noexcept { return EdgeId{ (int)i }; }
198inline constexpr UndirectedEdgeId operator ""_ue( unsigned long long i ) noexcept { return UndirectedEdgeId{ (int)i }; }
199inline constexpr VoxelId operator ""_vox( unsigned long long i ) noexcept { return VoxelId{ size_t( i ) }; }
200
201}
202
203template <typename T>
204struct std::hash<MR::Id<T>>
205{
206 size_t operator() ( MR::Id<T> const& p ) const noexcept
207 {
208 return (int)p;
209 }
210};
stores index of some element, it is made as template class to avoid mixing faces, edges and vertices
Definition MRId.h:20
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:477
constexpr bool odd() const
Definition MRId.h:105
constexpr bool valid() const
Definition MRId.h:149
size_t ValueType
Definition MRId.h:142
MR_BIND_IGNORE constexpr Id(U i) noexcept
Definition MRId.h:35
constexpr ValueType & get() noexcept
Definition MRId.h:151
constexpr Id() noexcept
the type used for internal representation of Id
Definition MRId.h:144
constexpr bool valid() const
Definition MRId.h:97
constexpr bool operator!=(Id b) const
Definition MRId.h:47
constexpr Id & operator+=(ValueType a)
Definition MRId.h:64
constexpr Id & operator++()
Definition MRId.h:58
int ValueType
Definition MRId.h:88
NoInitId()
Definition MRId.h:80
constexpr Id sym() const
returns identifier of the edge with same ends but opposite orientation
Definition MRId.h:102
Id(NoInit) noexcept
Definition MRId.h:25
class MRMESH_CLASS VoxelTag
Definition MRMeshFwd.h:108
constexpr bool operator==(Id b) const
Definition MRId.h:46
Id(NoInit) noexcept
Definition MRId.h:91
constexpr bool valid() const
Definition MRId.h:42
constexpr Id(UndirectedEdgeId u) noexcept
Definition MRId.h:92
constexpr Id(ValueType i) noexcept
Definition MRId.h:93
constexpr Id & operator-=(ValueType a)
Definition MRId.h:63
constexpr Id & operator--()
Definition MRId.h:57
Id(NoInit) noexcept
Definition MRId.h:145
constexpr Id(int) noexcept=delete
constexpr NoInit noInit
Definition MRMeshFwd.h:100
constexpr Id() noexcept
the type used for internal representation of Id
Definition MRId.h:24
NoInitId(Id< T > id)
Definition MRId.h:81
constexpr Id(ValueType i) noexcept
Definition MRId.h:146
constexpr bool operator<(Id b) const
Definition MRId.h:48
constexpr ValueType & get() noexcept
Definition MRId.h:44
constexpr Id(size_t i) noexcept
Definition MRId.h:95
constexpr Id() noexcept
the type used for internal representation of Id
Definition MRId.h:90
int ValueType
Definition MRId.h:22
constexpr Id(unsigned int i) noexcept
Definition MRId.h:94
class MRMESH_CLASS Id(EdgeId, Id< EdgeTag >)(UndirectedEdgeId
class MRMESH_CLASS EdgeTag
Definition MRMeshFwd.h:103
std::array< Vector3f, 3 > MR_BIND_IGNORE
Definition MRMeshBuilderTypes.h:13
constexpr ValueType & get() noexcept
Definition MRId.h:99
constexpr bool even() const
among each pair of sym-edges: one is always even and the other is odd
Definition MRId.h:104
constexpr UndirectedEdgeId undirected() const
returns unique identifier of the edge ignoring its direction
Definition MRId.h:107
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:109
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRMeshFwd.h:99