MeshLib
 
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{
11
13class Graph
14{
15public:
16 using VertId = GraphVertId;
17 using EdgeId = GraphEdgeId;
18
19 using VertBitSet = GraphVertBitSet;
20 using EdgeBitSet = GraphEdgeBitSet;
21
22 using Neighbours = std::vector<EdgeId>; // sorted by edgeID
24
26 {
27 VertId v0, v1; // v0 < v1
28 [[nodiscard]] VertId otherEnd( VertId a ) const
29 {
30 assert( a == v0 || a == v1 );
31 return a == v0 ? v1 : v0;
32 }
33 void replaceEnd( VertId what, VertId with )
34 {
35 assert( what != with );
36 assert( ( v0 == what && v1 != with ) || ( v1 == what && v0 != with ) );
37 if ( v0 == what )
38 v0 = with;
39 else
40 v1 = with;
41 if ( v0 > v1 )
42 std::swap( v0, v1 );
43 }
44 auto operator <=>( const EndVertices& ) const = default;
45 };
47
49 MRMESH_API void construct( NeighboursPerVertex neighboursPerVertex, EndsPerEdge endsPerEdge );
50
52 [[nodiscard]] size_t vertSize() const { return neighboursPerVertex_.size(); }
53
55 [[nodiscard]] const VertBitSet & validVerts() const { return validVerts_; }
56
58 [[nodiscard]] bool valid( VertId v ) const { return validVerts_.test( v ); }
59
61 [[nodiscard]] size_t edgeSize() const { return endsPerEdge_.size(); }
62
64 [[nodiscard]] const EdgeBitSet & validEdges() const { return validEdges_; }
65
67 [[nodiscard]] bool valid( EdgeId e ) const { return validEdges_.test( e ); }
68
70 [[nodiscard]] const Neighbours & neighbours( VertId v ) const { return neighboursPerVertex_[v]; }
71
73 [[nodiscard]] const EndVertices & ends( EdgeId e ) const { return endsPerEdge_[e]; }
74
76 [[nodiscard]] MRMESH_API EdgeId findEdge( VertId a, VertId b ) const;
77
79 [[nodiscard]] bool areNeighbors( VertId a, VertId b ) const { return findEdge( a, b ).valid(); }
80
83 MRMESH_API void merge( VertId remnant, VertId dead, std::function<void( EdgeId remnant, EdgeId dead )> onMergeEdges );
84
87
88private:
89 VertBitSet validVerts_;
90 EdgeBitSet validEdges_;
91
92 NeighboursPerVertex neighboursPerVertex_;
93 EndsPerEdge endsPerEdge_;
94};
95
96} //namespace MR
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
mathematical graph consisting from vertices and undirected edges
Definition MRGraph.h:14
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:73
size_t edgeSize() const
returns the number of edge records, including invalid ones
Definition MRGraph.h:61
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:70
bool valid(VertId v) const
returns true if given vertex is valid
Definition MRGraph.h:58
bool valid(EdgeId e) const
returns true if given edge is valid
Definition MRGraph.h:67
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:55
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:64
std::vector< EdgeId > Neighbours
Definition MRGraph.h:22
GraphEdgeBitSet EdgeBitSet
Definition MRGraph.h:20
GraphEdgeId EdgeId
Definition MRGraph.h:17
GraphVertBitSet VertBitSet
Definition MRGraph.h:19
bool areNeighbors(VertId a, VertId b) const
returns true if the vertices a and b are neighbors
Definition MRGraph.h:79
size_t vertSize() const
returns the number of vertex records, including invalid ones
Definition MRGraph.h:52
GraphVertId VertId
Definition MRGraph.h:16
std::size_t size() const
Definition MRMesh/MRVector.h:40
Definition MRCameraOrientationPlugin.h:7
Definition MRGraph.h:26
VertId v1
Definition MRGraph.h:27
VertId otherEnd(VertId a) const
Definition MRGraph.h:28
VertId v0
Definition MRGraph.h:27
void replaceEnd(VertId what, VertId with)
Definition MRGraph.h:33
auto operator<=>(const EndVertices &) const =default