MeshLib C++ Docs
Loading...
Searching...
No Matches
MRWatershedGraph.h
1#pragma once
2
3#include "MRGraph.h"
4#include <cassert>
5#include <cfloat>
6
7namespace MR
8{
9
11class WatershedGraph
12{
13public:
15 struct BasinInfo
16 {
18 float lowestLevel = FLT_MAX;
19 float area = 0;
20 float lowestBdLevel = FLT_MAX;
21 float maxVolume = 0;
22 float accVolume = 0;
23 float lastUpdateAmount = 0;
24 float lastMergeLevel = FLT_MAX;
25 float lastMergeVolume = 0;
27
28 BasinInfo() {} // Apparently I need this for `MR::Vector` to register default-constructibility inside the enclosing class.
29
32 float amountTillOverflow() const
33 {
34 assert( !overflowVia );
35 assert( maxVolume >= accVolume );
36 return ( maxVolume - accVolume ) / area;
37 }
38
40 float approxLevel() const
41 {
42 assert( lastMergeLevel <= lowestBdLevel );
43 assert( lastMergeVolume <= maxVolume );
45 return lowestBdLevel;
46 const auto p = ( maxVolume - accVolume ) / ( maxVolume - lastMergeVolume );
47 assert( p >= 0 && p <= 1 );
48 return p * lastMergeLevel + ( 1 - p ) * lowestBdLevel;
49 }
50
52 void updateAccVolume( float amount )
53 {
54 assert( !overflowVia );
55 assert( amount >= lastUpdateAmount );
56 accVolume += ( amount - lastUpdateAmount ) * area;
57 if ( accVolume > maxVolume ) // due to rounding errors
59 lastUpdateAmount = amount;
60 }
61 };
62
64 struct BdInfo
65 {
67 };
68
69public:
71 MRMESH_API WatershedGraph( const Mesh & mesh, const Vector<int, FaceId> & face2basin, int numBasins );
72
74 [[nodiscard]] MRMESH_API float getHeightAt( VertId v ) const;
75
77 [[nodiscard]] const Graph & graph() const { return graph_; }
78
80 [[nodiscard]] float totalArea() const { return totalArea_; }
81
83 [[nodiscard]] int numBasins() const { return (int)graph_.validVerts().count() - 1; }
84
86 [[nodiscard]] const BasinInfo & basinInfo( Graph::VertId v ) const { return basins_[v]; }
87 [[nodiscard]] BasinInfo & basinInfo( Graph::VertId v ) { return basins_[v]; }
88
90 [[nodiscard]] const BdInfo & bdInfo( Graph::EdgeId e ) const { return bds_[e]; }
91 [[nodiscard]] BdInfo & bdInfo( Graph::EdgeId e ) { return bds_[e]; }
92
94 [[nodiscard]] Graph::VertId outsideId() const { return outsideId_; }
95
97 [[nodiscard]] MRMESH_API Graph::VertId getRootBasin( Graph::VertId v ) const;
98
100 [[nodiscard]] MRMESH_API Graph::VertId flowsTo( Graph::VertId v ) const;
101
104 [[nodiscard]] MRMESH_API Graph::VertId flowsFinallyTo( Graph::VertId v, bool exceptOutside = false ) const;
105
108 MRMESH_API void setParentsToRoots();
109
112 [[nodiscard]] MRMESH_API std::pair<Graph::EdgeId, float> findLowestBd() const;
113
115 MRMESH_API Graph::VertId merge( Graph::VertId v0, Graph::VertId v1 );
116
118 MRMESH_API Graph::VertId mergeViaBd( Graph::EdgeId bd );
119
121 [[nodiscard]] MRMESH_API FaceBitSet getBasinFaces( Graph::VertId basin ) const;
122
125 [[nodiscard]] MRMESH_API Vector<FaceBitSet, Graph::VertId> getAllBasinFaces( bool joinOverflowBasins = false ) const;
126
128 [[nodiscard]] MRMESH_API FaceBitSet getBasinFacesBelowLevel( Graph::VertId basin, float waterLevel ) const;
129
132 [[nodiscard]] MRMESH_API double computeBasinVolume( Graph::VertId basin, float waterLevel ) const;
133
136 [[nodiscard]] MRMESH_API UndirectedEdgeBitSet getInterBasinEdges( bool joinOverflowBasins = false ) const;
137
140 {
141 VertId v; // mesh vertex on the boundary of full basin and the other where it overflows
142 Graph::VertId fullBasin;
143 Graph::VertId overflowTo; // basin where the flow from v goes
144 };
145
147 [[nodiscard]] MRMESH_API std::vector<OverflowPoint> getOverflowPoints() const;
148
151 [[nodiscard]] MRMESH_API Vector<Graph::VertId, Graph::VertId> iniBasin2Tgt( bool joinOverflowBasins = false ) const;
152
153private:
154 const Mesh & mesh_;
155 const Vector<int, FaceId> & face2iniBasin_;
156
157 Graph graph_;
160 float totalArea_ = 0;
161
163 Graph::VertId outsideId_;
164
167};
168
169} //namespace MR
Definition MRGraph.h:14
Definition MRMesh/MRMesh.h:23
std::vector<T>-like container that requires specific indexing type,
Definition MRVector.h:19
unsafe WatershedGraph(MR._ByValue_WatershedGraph _other)
unsafe new MR.WatershedGraph.BasinInfo basinInfo(MR.GraphVertId v)
unsafe MR.GraphVertId mergeViaBd(MR.GraphEdgeId bd)
unsafe void setParentsToRoots()
unsafe new MR.WatershedGraph.BdInfo bdInfo(MR.GraphEdgeId e)
unsafe MR.GraphVertId merge(MR.GraphVertId v0, MR.GraphVertId v1)
associated with each vertex in graph
Definition MRWatershedGraph.h:16
float lowestBdLevel
lowest position on the boundary of the basin
Definition MRWatershedGraph.h:20
void updateAccVolume(float amount)
updates accumulated volume in the basin to the moment of given precipitation amount
Definition MRWatershedGraph.h:52
VertId lowestVert
in the whole basin
Definition MRWatershedGraph.h:17
float maxVolume
full water volume to be accumulated in the basin till water reaches the lowest height on the boundary
Definition MRWatershedGraph.h:21
float lastMergeVolume
water volume in the basin when it was formed (by merge or creation)
Definition MRWatershedGraph.h:25
float accVolume
accumulated water volume in the basin so far
Definition MRWatershedGraph.h:22
float area
precipitation area that flows in this basin (and if it is full, continue flowing next)
Definition MRWatershedGraph.h:19
float amountTillOverflow() const
Definition MRWatershedGraph.h:32
float lowestLevel
lowest level (z-coordinate of lowestVert) in the basin
Definition MRWatershedGraph.h:18
float lastMergeLevel
water level in the basin when it was formed (by merge or creation)
Definition MRWatershedGraph.h:24
Graph::EdgeId overflowVia
when level=lowestBdLevel, volume=0, all water from this basin overflows via this boundary
Definition MRWatershedGraph.h:26
float approxLevel() const
approximate current level of water (z-coordinate) in the basin
Definition MRWatershedGraph.h:40
float lastUpdateAmount
the amount when accVolume was last updated
Definition MRWatershedGraph.h:23
associated with each edge in graph
Definition MRWatershedGraph.h:65
VertId lowestVert
on this boundary
Definition MRWatershedGraph.h:66
describes a point where a flow from one basin overflows into another basin
Definition MRWatershedGraph.h:140