MeshLib C++ Docs
Loading...
Searching...
No Matches
MRGraph.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRVector.h"
4#include "MRId.h"
5#include "MRBitSet.h"
6#include <cassert>
7#include <functional>
8
9namespace MR
10{
13
14
16class Graph
17{
18public:
19 using VertId = GraphVertId;
20 using EdgeId = GraphEdgeId;
21
22 using VertBitSet = GraphVertBitSet;
23 using EdgeBitSet = GraphEdgeBitSet;
24
25 using Neighbours = std::vector<EdgeId>;
27
29 {
31 [[nodiscard]] VertId otherEnd( VertId a ) const
32 {
33 assert( a == v0 || a == v1 );
34 return a == v0 ? v1 : v0;
35 }
36 void replaceEnd( VertId what, VertId with )
37 {
38 assert( what != with );
39 assert( ( v0 == what && v1 != with ) || ( v1 == what && v0 != with ) );
40 if ( v0 == what )
41 v0 = with;
42 else
43 v1 = with;
44 if ( v0 > v1 )
45 std::swap( v0, v1 );
46 }
47 auto operator <=>( const EndVertices& ) const = default;
48 };
50
52 MRMESH_API void construct( NeighboursPerVertex neighboursPerVertex, EndsPerEdge endsPerEdge );
53
55 [[nodiscard]] size_t vertSize() const { return neighboursPerVertex_.size(); }
56
58 [[nodiscard]] const VertBitSet & validVerts() const { return validVerts_; }
59
61 [[nodiscard]] bool valid( VertId v ) const { return validVerts_.test( v ); }
62
64 [[nodiscard]] size_t edgeSize() const { return endsPerEdge_.size(); }
65
67 [[nodiscard]] const EdgeBitSet & validEdges() const { return validEdges_; }
68
70 [[nodiscard]] bool valid( EdgeId e ) const { return validEdges_.test( e ); }
71
73 [[nodiscard]] const Neighbours & neighbours( VertId v ) const { return neighboursPerVertex_[v]; }
74
76 [[nodiscard]] const EndVertices & ends( EdgeId e ) const { return endsPerEdge_[e]; }
77
79 [[nodiscard]] MRMESH_API EdgeId findEdge( VertId a, VertId b ) const;
80
82 [[nodiscard]] bool areNeighbors( VertId a, VertId b ) const { return findEdge( a, b ).valid(); }
83
86 MRMESH_API void merge( VertId remnant, VertId dead, std::function<void( EdgeId remnant, EdgeId dead )> onMergeEdges );
87
89 MRMESH_API bool checkValidity() const;
90
91private:
92 VertBitSet validVerts_;
93 EdgeBitSet validEdges_;
94
95 NeighboursPerVertex neighboursPerVertex_;
96 EndsPerEdge endsPerEdge_;
97};
98
99}
mathematical graph consisting from vertices and undirected edges
Definition MRGraph.h:17
MRMESH_API void construct(NeighboursPerVertex neighboursPerVertex, EndsPerEdge endsPerEdge)
constructs the graph from all valid vertices and edges
const EndVertices & ends(EdgeId e) const
returns the ends of given edge
Definition MRGraph.h:76
size_t edgeSize() const
returns the number of edge records, including invalid ones
Definition MRGraph.h:64
VertId v1
Definition MRGraph.h:30
std::size_t size() const
Definition MRVector.h:55
MRMESH_API void merge(VertId remnant, VertId dead, std::function< void(EdgeId remnant, EdgeId dead)> onMergeEdges)
const Neighbours & neighbours(VertId v) const
returns all edges adjacent to given vertex
Definition MRGraph.h:73
bool valid(VertId v) const
returns true if given vertex is valid
Definition MRGraph.h:61
bool valid(EdgeId e) const
returns true if given edge is valid
Definition MRGraph.h:70
VertId otherEnd(VertId a) const
v0 < v1
Definition MRGraph.h:31
MRMESH_API EdgeId findEdge(VertId a, VertId b) const
finds and returns edge between vertices a and b; returns invalid edge otherwise
const VertBitSet & validVerts() const
returns all valid vertices in the graph
Definition MRGraph.h:58
MRMESH_API bool checkValidity() const
verifies that all internal data structures are valid
const EdgeBitSet & validEdges() const
returns all valid edges in the graph
Definition MRGraph.h:67
std::vector< EdgeId > Neighbours
Definition MRGraph.h:25
VertId v0
Definition MRGraph.h:30
GraphEdgeBitSet EdgeBitSet
Definition MRGraph.h:23
GraphEdgeId EdgeId
Definition MRGraph.h:20
GraphVertBitSet VertBitSet
Definition MRGraph.h:22
bool areNeighbors(VertId a, VertId b) const
returns true if the vertices a and b are neighbors
Definition MRGraph.h:82
size_t vertSize() const
returns the number of vertex records, including invalid ones
Definition MRGraph.h:55
void replaceEnd(VertId what, VertId with)
Definition MRGraph.h:36
GraphVertId VertId
Definition MRGraph.h:19
auto operator<=>(const EndVertices &) const =default
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRGraph.h:29