MeshLib
 
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
7namespace MR
8{
9
10// stores index of some element, it is made as template class to avoid mixing faces, edges and vertices
11template <typename T>
12class Id
13{
14public:
15 constexpr Id() noexcept : id_( -1 ) { }
16 explicit Id( NoInit ) noexcept { }
17 explicit constexpr Id( int i ) noexcept : id_( i ) { }
18 explicit constexpr Id( unsigned int i ) noexcept : id_( i ) { }
19 explicit constexpr Id( size_t i ) noexcept : id_( int( i ) ) { }
20 template <typename U> Id( Id<U> ) = delete;
21
22 constexpr operator int() const { return id_; }
23 constexpr bool valid() const { return id_ >= 0; }
24 explicit constexpr operator bool() const { return id_ >= 0; }
25 constexpr int & get() noexcept { return id_; }
26
27 constexpr bool operator == (Id b) const { return id_ == b.id_; }
28 constexpr bool operator != (Id b) const { return id_ != b.id_; }
29 constexpr bool operator < (Id b) const { return id_ < b.id_; }
30
31 template <typename U>
32 bool operator == (Id<U> b) const = delete;
33 template <typename U>
34 bool operator != (Id<U> b) const = delete;
35 template <typename U>
36 bool operator < (Id<U> b) const = delete;
37
38 constexpr Id & operator --() { --id_; return * this; }
39 constexpr Id & operator ++() { ++id_; return * this; }
40
41 constexpr Id operator --( int ) { auto res = *this; --id_; return res; }
42 constexpr Id operator ++( int ) { auto res = *this; ++id_; return res; }
43
44 constexpr Id & operator -=( int a ) { id_ -= a; return * this; }
45 constexpr Id & operator +=( int a ) { id_ += a; return * this; }
46
47private:
48 int id_;
49};
50
51template <>
52class Id<MR::EdgeTag> // Need `MR::` here to simplify binding generation. See libclang bug: https://github.com/llvm/llvm-project/issues/92371
53{
54public:
55 constexpr Id() noexcept : id_( -1 ) { }
56 explicit Id( NoInit ) noexcept { }
57 constexpr Id( UndirectedEdgeId u ) noexcept : id_( (int)u << 1 ) { assert( u.valid() ); }
58 explicit constexpr Id( int i ) noexcept : id_( i ) { }
59 explicit constexpr Id( unsigned int i ) noexcept : id_( i ) { }
60 explicit constexpr Id( size_t i ) noexcept : id_( int( i ) ) { }
61 constexpr operator int() const { return id_; }
62 constexpr bool valid() const { return id_ >= 0; }
63 explicit constexpr operator bool() const { return id_ >= 0; }
64 constexpr int & get() noexcept { return id_; }
65
66 // returns identifier of the edge with same ends but opposite orientation
67 constexpr Id sym() const { assert( valid() ); return Id(id_ ^ 1); }
68 // among each pair of sym-edges: one is always even and the other is odd
69 constexpr bool even() const { assert( valid() ); return (id_ & 1) == 0; }
70 constexpr bool odd() const { assert( valid() ); return (id_ & 1) == 1; }
71 // returns unique identifier of the edge ignoring its direction
72 constexpr UndirectedEdgeId undirected() const { assert( valid() ); return UndirectedEdgeId( id_ >> 1 ); }
73 constexpr operator UndirectedEdgeId() const { return undirected(); }
74
75 constexpr bool operator == (Id b) const { return id_ == b.id_; }
76 constexpr bool operator != (Id b) const { return id_ != b.id_; }
77 constexpr bool operator < (Id b) const { return id_ < b.id_; }
78
79 template <typename U>
80 bool operator == (Id<U> b) const = delete;
81 template <typename U>
82 bool operator != (Id<U> b) const = delete;
83 template <typename U>
84 bool operator < (Id<U> b) const = delete;
85
86 constexpr Id & operator --() { --id_; return * this; }
87 constexpr Id & operator ++() { ++id_; return * this; }
88
89 constexpr Id operator --( int ) { auto res = *this; --id_; return res; }
90 constexpr Id operator ++( int ) { auto res = *this; ++id_; return res; }
91
92 constexpr Id & operator -=( int a ) { id_ -= a; return * this; }
93 constexpr Id & operator +=( int a ) { id_ += a; return * this; }
94
95private:
96 int id_;
97};
98
99template <>
101{
102public:
103 constexpr Id() noexcept : id_( ~size_t( 0 ) ) { }
104 explicit Id( NoInit ) noexcept { }
105 explicit constexpr Id( size_t i ) noexcept : id_( i ) { }
106 explicit constexpr Id( int ) noexcept = delete;
107 constexpr operator size_t() const { return id_; }
108 constexpr bool valid() const { return id_ != ~size_t( 0 ); }
109 explicit constexpr operator bool() const { return id_ != ~size_t( 0 ); }
110 constexpr size_t& get() noexcept { return id_; }
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 -=( size_t a ) { id_ -= a; return * this; }
130 constexpr Id & operator +=( size_t a ) { id_ += a; return * this; }
131
132private:
133 size_t id_;
134};
135
136template <typename T>
137inline constexpr Id<T> operator + ( Id<T> id, int a ) { return Id<T>{ id.get() + a }; }
138template <typename T>
139inline constexpr Id<T> operator + ( Id<T> id, unsigned int a ) { return Id<T>{ id.get() + a }; }
140template <typename T>
141inline constexpr Id<T> operator + ( Id<T> id, size_t a ) { return Id<T>{ id.get() + a }; }
142
143template <typename T>
144inline constexpr Id<T> operator - ( Id<T> id, int a ) { return Id<T>{ id.get() - a }; }
145template <typename T>
146inline constexpr Id<T> operator - ( Id<T> id, unsigned int a ) { return Id<T>{ id.get() - a }; }
147template <typename T>
148inline constexpr Id<T> operator - ( Id<T> id, size_t a ) { return Id<T>{ id.get() - a }; }
149
150inline constexpr FaceId operator "" _f( unsigned long long i ) noexcept { return FaceId{ (int)i }; }
151inline constexpr VertId operator "" _v( unsigned long long i ) noexcept { return VertId{ (int)i }; }
152inline constexpr EdgeId operator "" _e( unsigned long long i ) noexcept { return EdgeId{ (int)i }; }
153inline constexpr UndirectedEdgeId operator "" _ue( unsigned long long i ) noexcept { return UndirectedEdgeId{ (int)i }; }
154inline constexpr VoxelId operator "" _vox( unsigned long long i ) noexcept { return VoxelId{ size_t( i ) }; }
155
156} //namespace MR
int VertId
Definition MRDotNet/MRMeshFwd.h:51
int FaceId
Definition MRDotNet/MRMeshFwd.h:53
int EdgeId
Definition MRDotNet/MRMeshFwd.h:52
constexpr bool valid() const
Definition MRMesh/MRId.h:62
constexpr bool odd() const
Definition MRMesh/MRId.h:70
constexpr UndirectedEdgeId undirected() const
Definition MRMesh/MRId.h:72
constexpr Id sym() const
Definition MRMesh/MRId.h:67
Id(NoInit) noexcept
Definition MRMesh/MRId.h:56
constexpr Id(size_t i) noexcept
Definition MRMesh/MRId.h:60
constexpr Id(unsigned int i) noexcept
Definition MRMesh/MRId.h:59
constexpr Id(UndirectedEdgeId u) noexcept
Definition MRMesh/MRId.h:57
constexpr Id() noexcept
Definition MRMesh/MRId.h:55
constexpr bool even() const
Definition MRMesh/MRId.h:69
constexpr Id(int i) noexcept
Definition MRMesh/MRId.h:58
constexpr int & get() noexcept
Definition MRMesh/MRId.h:64
constexpr bool valid() const
Definition MRMesh/MRId.h:108
constexpr Id(size_t i) noexcept
Definition MRMesh/MRId.h:105
constexpr Id() noexcept
Definition MRMesh/MRId.h:103
constexpr size_t & get() noexcept
Definition MRMesh/MRId.h:110
Id(NoInit) noexcept
Definition MRMesh/MRId.h:104
constexpr Id(int) noexcept=delete
Definition MRMesh/MRId.h:13
constexpr Id(int i) noexcept
Definition MRMesh/MRId.h:17
constexpr bool operator!=(Id b) const
Definition MRMesh/MRId.h:28
constexpr Id & operator++()
Definition MRMesh/MRId.h:39
Id(Id< U >)=delete
Id(NoInit) noexcept
Definition MRMesh/MRId.h:16
constexpr bool operator==(Id b) const
Definition MRMesh/MRId.h:27
constexpr Id(unsigned int i) noexcept
Definition MRMesh/MRId.h:18
constexpr bool valid() const
Definition MRMesh/MRId.h:23
constexpr Id(size_t i) noexcept
Definition MRMesh/MRId.h:19
constexpr Id & operator--()
Definition MRMesh/MRId.h:38
constexpr Id() noexcept
Definition MRMesh/MRId.h:15
constexpr bool operator<(Id b) const
Definition MRMesh/MRId.h:29
constexpr int & get() noexcept
Definition MRMesh/MRId.h:25
constexpr Id & operator-=(int a)
Definition MRMesh/MRId.h:44
constexpr Id & operator+=(int a)
Definition MRMesh/MRId.h:45
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:325
Definition MRCameraOrientationPlugin.h:7
class MRMESH_CLASS VoxelTag
Definition MRMesh/MRMeshFwd.h:65
class MRMESH_CLASS EdgeTag
Definition MRMesh/MRMeshFwd.h:60
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:108
Definition MRMesh/MRMeshFwd.h:56