MeshLib C++ Docs
Loading...
Searching...
No Matches
MRWatershedGraph.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRGraph.h"
4#include <cassert>
5#include <cfloat>
6
7namespace MR
8{
11
12
15{
16public:
18 struct BasinInfo
19 {
20 VertId lowestVert;
21 float lowestLevel = FLT_MAX;
22 float area = 0;
23 float lowestBdLevel = FLT_MAX;
24 float maxVolume = 0;
25 float accVolume = 0;
26 float lastUpdateAmount = 0;
27 float lastMergeLevel = FLT_MAX;
28 float lastMergeVolume = 0;
30
32
35 float amountTillOverflow() const
36 {
37 assert( !overflowVia );
38 assert( maxVolume >= accVolume );
39 return ( maxVolume - accVolume ) / area;
40 }
41
43 float approxLevel() const
44 {
45 assert( lastMergeLevel <= lowestBdLevel );
46 assert( lastMergeVolume <= maxVolume );
48 return lowestBdLevel;
49 const auto p = ( maxVolume - accVolume ) / ( maxVolume - lastMergeVolume );
50 assert( p >= 0 && p <= 1 );
51 return p * lastMergeLevel + ( 1 - p ) * lowestBdLevel;
52 }
53
55 void updateAccVolume( float amount )
56 {
57 assert( !overflowVia );
58 assert( amount >= lastUpdateAmount );
59 accVolume += ( amount - lastUpdateAmount ) * area;
60 if ( accVolume > maxVolume )
62 lastUpdateAmount = amount;
63 }
64 };
65
67 struct BdInfo
68 {
69 VertId lowestVert;
70 };
71
72public:
74 MRMESH_API WatershedGraph( const Mesh & mesh, const Vector<int, FaceId> & face2basin, int numBasins );
75
77 [[nodiscard]] MRMESH_API float getHeightAt( VertId v ) const;
78
80 [[nodiscard]] const Graph & graph() const { return graph_; }
81
83 [[nodiscard]] float totalArea() const { return totalArea_; }
84
86 [[nodiscard]] int numBasins() const { return (int)graph_.validVerts().count() - 1; }
87
89 [[nodiscard]] const BasinInfo & basinInfo( Graph::VertId v ) const { return basins_[v]; }
90 [[nodiscard]] BasinInfo & basinInfo( Graph::VertId v ) { return basins_[v]; }
91
93 [[nodiscard]] const BdInfo & bdInfo( Graph::EdgeId e ) const { return bds_[e]; }
94 [[nodiscard]] BdInfo & bdInfo( Graph::EdgeId e ) { return bds_[e]; }
95
97 [[nodiscard]] Graph::VertId outsideId() const { return outsideId_; }
98
100 [[nodiscard]] MRMESH_API Graph::VertId getRootBasin( Graph::VertId v ) const;
101
103 [[nodiscard]] MRMESH_API Graph::VertId flowsTo( Graph::VertId v ) const;
104
107 [[nodiscard]] MRMESH_API Graph::VertId flowsFinallyTo( Graph::VertId v, bool exceptOutside = false ) const;
108
111 MRMESH_API void setParentsToRoots();
112
115 [[nodiscard]] MRMESH_API std::pair<Graph::EdgeId, float> findLowestBd() const;
116
119
122
124 [[nodiscard]] MRMESH_API FaceBitSet getBasinFaces( Graph::VertId basin ) const;
125
128 [[nodiscard]] MRMESH_API Vector<FaceBitSet, Graph::VertId> getAllBasinFaces( bool joinOverflowBasins = false ) const;
129
131 [[nodiscard]] MRMESH_API FaceBitSet getBasinFacesBelowLevel( Graph::VertId basin, float waterLevel ) const;
132
135 [[nodiscard]] MRMESH_API double computeBasinVolume( Graph::VertId basin, float waterLevel ) const;
136
139 [[nodiscard]] MRMESH_API UndirectedEdgeBitSet getInterBasinEdges( bool joinOverflowBasins = false ) const;
140
148
150 [[nodiscard]] MRMESH_API std::vector<OverflowPoint> getOverflowPoints() const;
151
154 [[nodiscard]] MRMESH_API Vector<Graph::VertId, Graph::VertId> iniBasin2Tgt( bool joinOverflowBasins = false ) const;
155
156private:
157 const Mesh & mesh_;
158 const Vector<int, FaceId> & face2iniBasin_;
159
160 Graph graph_;
163 float totalArea_ = 0;
164
166 Graph::VertId outsideId_;
167
170};
171
172}
mathematical graph consisting from vertices and undirected edges
Definition MRGraph.h:17
std::vector<T>-like container that requires specific indexing type,
Definition MRVector.h:23
graphs representing rain basins on the mesh
Definition MRWatershedGraph.h:15
float lowestBdLevel
lowest position on the boundary of the basin
Definition MRWatershedGraph.h:23
void updateAccVolume(float amount)
updates accumulated volume in the basin to the moment of given precipitation amount
Definition MRWatershedGraph.h:55
MRMESH_API UndirectedEdgeBitSet getInterBasinEdges(bool joinOverflowBasins=false) const
MRMESH_API double computeBasinVolume(Graph::VertId basin, float waterLevel) const
VertId lowestVert
on this boundary
Definition MRWatershedGraph.h:69
MRMESH_API Vector< Graph::VertId, Graph::VertId > iniBasin2Tgt(bool joinOverflowBasins=false) const
MRMESH_API FaceBitSet getBasinFaces(Graph::VertId basin) const
returns the mesh faces of given basin
MRMESH_API std::vector< OverflowPoint > getOverflowPoints() const
returns all overflow points in the graph
VertId lowestVert
in the whole basin
Definition MRWatershedGraph.h:20
MRMESH_API std::pair< Graph::EdgeId, float > findLowestBd() const
Graph::VertId overflowTo
Definition MRWatershedGraph.h:146
Graph::VertId outsideId() const
returns special "basin" representing outside areas of the mesh
Definition MRWatershedGraph.h:97
float maxVolume
full water volume to be accumulated in the basin till water reaches the lowest height on the boundary
Definition MRWatershedGraph.h:24
float lastMergeVolume
water volume in the basin when it was formed (by merge or creation)
Definition MRWatershedGraph.h:28
const Graph & graph() const
returns underlying graph where each basin is a vertex
Definition MRWatershedGraph.h:80
float accVolume
accumulated water volume in the basin so far
Definition MRWatershedGraph.h:25
MRMESH_API Graph::VertId flowsFinallyTo(Graph::VertId v, bool exceptOutside=false) const
const VertBitSet & validVerts() const
returns all valid vertices in the graph
Definition MRGraph.h:58
BasinInfo()
Definition MRWatershedGraph.h:31
int numBasins() const
returns the current number of basins (excluding special "outside" basin)
Definition MRWatershedGraph.h:86
float area
precipitation area that flows in this basin (and if it is full, continue flowing next)
Definition MRWatershedGraph.h:22
MRMESH_API FaceBitSet getBasinFacesBelowLevel(Graph::VertId basin, float waterLevel) const
returns the mesh faces of given basin with at least one vertex below given level
float amountTillOverflow() const
Apparently I need this for MR::Vector to register default-constructibility inside the enclosing class...
Definition MRWatershedGraph.h:35
BasinInfo & basinInfo(Graph::VertId v)
Definition MRWatershedGraph.h:90
Graph::VertId fullBasin
mesh vertex on the boundary of full basin and the other where it overflows
Definition MRWatershedGraph.h:145
float totalArea() const
returns total precipitation area
Definition MRWatershedGraph.h:83
MRMESH_API void setParentsToRoots()
const BasinInfo & basinInfo(Graph::VertId v) const
returns data associated with given basin
Definition MRWatershedGraph.h:89
float lowestLevel
lowest level (z-coordinate of lowestVert) in the basin
Definition MRWatershedGraph.h:21
VertId v
Definition MRWatershedGraph.h:144
MRMESH_API Graph::VertId mergeViaBd(Graph::EdgeId bd)
merges two basins sharing given boundary, returns remaining basin
GraphEdgeId EdgeId
Definition MRGraph.h:20
MRMESH_API Graph::VertId merge(Graph::VertId v0, Graph::VertId v1)
merges basin v1 into basin v0, v1 is deleted after that, returns v0
MRMESH_API Graph::VertId flowsTo(Graph::VertId v) const
returns the basin where the flow from this basin goes next (it can be self id if the basin is not ful...
float lastMergeLevel
water level in the basin when it was formed (by merge or creation)
Definition MRWatershedGraph.h:27
const BdInfo & bdInfo(Graph::EdgeId e) const
returns data associated with given boundary between basins
Definition MRWatershedGraph.h:93
MRMESH_API WatershedGraph(const Mesh &mesh, const Vector< int, FaceId > &face2basin, int numBasins)
constructs the graph from given mesh, heights in z-coordinate, and initial subdivision on basins
Graph::EdgeId overflowVia
when level=lowestBdLevel, volume=0, all water from this basin overflows via this boundary
Definition MRWatershedGraph.h:29
float approxLevel() const
approximate current level of water (z-coordinate) in the basin
Definition MRWatershedGraph.h:43
BdInfo & bdInfo(Graph::EdgeId e)
Definition MRWatershedGraph.h:94
MRMESH_API Vector< FaceBitSet, Graph::VertId > getAllBasinFaces(bool joinOverflowBasins=false) const
MRMESH_API Graph::VertId getRootBasin(Graph::VertId v) const
for valid basin returns self id; for invalid basin returns the id of basin it was merged in
GraphVertId VertId
Definition MRGraph.h:19
MRMESH_API float getHeightAt(VertId v) const
returns height at given vertex or FLT_MAX if the vertex is invalid
float lastUpdateAmount
the amount when accVolume was last updated
Definition MRWatershedGraph.h:26
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRMesh.h:23
associated with each vertex in graph
Definition MRWatershedGraph.h:19
associated with each edge in graph
Definition MRWatershedGraph.h:68
describes a point where a flow from one basin overflows into another basin
Definition MRWatershedGraph.h:143