SWMMVis  6.0.0-alpha.4
Qt6/C++ GIS-based graphical user interface for the SWMMVis engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
planmodel.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
4#include <sstream>
5#include <iomanip>
6#include <cmath>
7namespace meshkit::plan
8{
9using detail::Vector;
10using detail::Map;
11using detail::Set;
12inline constexpr double pi = 3.141592653589793238462643383279502884;
13class Text : public std::string
14{
15public:
16 using std::string::string;
17 Text() = default;
18 Text(const std::string &v) : std::string(v) {};
19 bool isEmpty() const { return empty(); }
20 template <class T> Text arg(const T &value) const
21 {
22 std::ostringstream out;
23 out << value;
24 Text result = *this;
25 std::size_t at = std::string::npos;
26 int lowest = 100;
27 for (std::size_t i = 0; i + 1 < size(); ++i)
28 if ((*this)[i] == '%' && (*this)[i + 1] >= '1' && (*this)[i + 1] <= '9') {
29 int n = (*this)[i + 1] - '0';
30 if (n < lowest) {
31 lowest = n;
32 at = i;
33 }
34 }
35 if (at != std::string::npos) {
36 const auto key = "%" + std::to_string(lowest);
37 std::size_t p = 0;
38 while ((p = result.find(key, p)) != std::string::npos) {
39 result.replace(p, key.size(), out.str());
40 p += out.str().size();
41 }
42 }
43 return result;
44 }
45 template <class T, class U> Text arg(const T &a, const U &b) const { return arg(a).arg(b); }
46};
47using Bytes = std::string;
48class Polygon : public Vector<Point2>
49{
50public:
52 Polygon() = default;
53 Polygon(const Vector<Point2> &v) : Vector<Point2>(v) {};
54 Polygon(const std::vector<Point2> &v) : Vector<Point2>(v) {}
56 {
57 if (empty())
58 return {};
59 double x0 = front().x(), x1 = x0, y0 = front().y(), y1 = y0;
60 for (auto p : *this) {
61 x0 = std::min(x0, p.x());
62 x1 = std::max(x1, p.x());
63 y0 = std::min(y0, p.y());
64 y1 = std::max(y1, p.y());
65 }
66 return {{x0, y0}, {x1, y1}};
67 }
68};
69struct Line2 {
71 Line2(Point2 x, Point2 y) : a(x), b(y) {}
72 double length() const { return std::hypot(b.x() - a.x(), b.y() - a.y()); }
73};
74struct MeshVertex {
76 double z = 0;
77};
79 int v0 = 0, v1 = 0, v2 = 0, v3 = -1;
81 bool isQuad() const { return v3 >= 0; }
82 int vertexCount() const { return isQuad() ? 4 : 3; }
83 int vertex(int k) const { return k == 0 ? v0 : k == 1 ? v1 : k == 2 ? v2 : k == 3 ? v3 : -1; }
84 void setVertex(int k, int v)
85 {
86 if (k == 0)
87 v0 = v;
88 else if (k == 1)
89 v1 = v;
90 else if (k == 2)
91 v2 = v;
92 else if (k == 3)
93 v3 = v;
94 }
95};
96struct MeshEdge {
97 int v0 = 0, v1 = 0, marker = 0;
99};
100template <class T> class ReadArray : public Vector<T>
101{
102 const void *owner_ = nullptr;
103 T (*read_)(const void *, std::size_t) = nullptr;
104 std::ptrdiff_t count_ = 0;
105
106public:
107 using Vector<T>::Vector;
108 void borrow(const void *owner, std::ptrdiff_t count, T (*read)(const void *, std::size_t))
109 {
110 owner_ = owner;
111 count_ = count;
112 read_ = read;
113 }
114 std::ptrdiff_t size() const { return owner_ ? count_ : Vector<T>::size(); }
115 bool isEmpty() const { return size() == 0; }
116 T operator[](std::size_t i) const
117 {
118 return owner_ ? read_(owner_, i) : Vector<T>::operator[](i);
119 }
120 struct Iterator {
122 std::size_t i;
123 T operator*() const { return (*array)[i]; }
125 {
126 ++i;
127 return *this;
128 }
129 bool operator!=(const Iterator &b) const { return i != b.i; }
130 };
131 Iterator begin() const { return {this, 0}; }
132 Iterator end() const { return {this, std::size_t(size())}; }
133};
149 int marker = 0;
151 double z = 0;
152 bool hasZ = false;
153};
154inline bool cellIsConvex(const Vector<MeshVertex> &v, const MeshTriangle &c)
155{
156 if (!c.isQuad())
157 return true;
158 double before = 0;
159 for (int i = 0; i < 4; ++i) {
160 auto a = v[c.vertex(i)].xy, b = v[c.vertex((i + 1) % 4)].xy,
161 d = v[c.vertex((i + 2) % 4)].xy;
162 auto ab = b - a, bd = d - b;
163 double turn = ab.x() * bd.y() - ab.y() * bd.x();
164 if (!std::isfinite(turn) || turn == 0 || (before && (turn > 0) != (before > 0)))
165 return false;
166 before = turn;
167 }
168 return true;
169}
170}
Definition geometry.hpp:59
Definition geometry.hpp:11
constexpr double y() const noexcept
Definition geometry.hpp:18
constexpr double x() const noexcept
Definition geometry.hpp:17
Definition storage.hpp:18
std::ptrdiff_t size() const noexcept
Definition storage.hpp:49
Definition planmodel.hpp:49
Polygon(const Vector< Point2 > &v)
Definition planmodel.hpp:53
Polygon(const std::vector< Point2 > &v)
Definition planmodel.hpp:54
Box2 boundingRect() const
Definition planmodel.hpp:55
Definition planmodel.hpp:101
T operator[](std::size_t i) const
Definition planmodel.hpp:116
void borrow(const void *owner, std::ptrdiff_t count, T(*read)(const void *, std::size_t))
Definition planmodel.hpp:108
Iterator end() const
Definition planmodel.hpp:132
bool isEmpty() const
Definition planmodel.hpp:115
std::ptrdiff_t size() const
Definition planmodel.hpp:114
Iterator begin() const
Definition planmodel.hpp:131
Definition planmodel.hpp:14
Text(const std::string &v)
Definition planmodel.hpp:18
Text arg(const T &a, const U &b) const
Definition planmodel.hpp:45
Text arg(const T &value) const
Definition planmodel.hpp:20
bool isEmpty() const
Definition planmodel.hpp:19
size_t i
Definition contourjob.cpp:30
ArrowPlacement p
Definition linesymbollayer.cpp:29
std::vector< int > owner
containing cell per pixel (-1 = outside)
Definition mesh2dresultsexport.cpp:766
std::size_t n
Definition mesh2dresultsexport.cpp:538
int k
Definition mesh2dresultsexport.cpp:694
Definition blocks.hpp:25
constexpr double pi
Definition planmodel.hpp:12
bool cellIsConvex(const Vector< MeshVertex > &v, const MeshTriangle &c)
Definition planmodel.hpp:154
std::string Bytes
Definition planmodel.hpp:47
int at
Definition constraintaudit.cpp:156
double value
Definition rasterfluxintegration.cpp:27
Definition planmodel.hpp:141
Vector< Point2 > path
Definition planmodel.hpp:142
double stripWidth
Definition planmodel.hpp:145
int marker
Definition planmodel.hpp:143
Text tag
Definition planmodel.hpp:144
Definition planmodel.hpp:69
double length() const
Definition planmodel.hpp:72
Point2 a
Definition planmodel.hpp:70
Point2 b
Definition planmodel.hpp:70
Line2(Point2 x, Point2 y)
Definition planmodel.hpp:71
Definition planmodel.hpp:96
int v0
Definition planmodel.hpp:97
int v1
Definition planmodel.hpp:97
int marker
Definition planmodel.hpp:97
Text tag
Definition planmodel.hpp:98
Definition planmodel.hpp:134
bool ok
Definition planmodel.hpp:138
ReadArray< MeshEdge > boundaryEdges
Definition planmodel.hpp:137
ReadArray< MeshTriangle > triangles
Definition planmodel.hpp:136
ReadArray< MeshVertex > vertices
Definition planmodel.hpp:135
Text errorMsg
Definition planmodel.hpp:139
Definition planmodel.hpp:78
int vertex(int k) const
Definition planmodel.hpp:83
Text tag
Definition planmodel.hpp:80
int v1
Definition planmodel.hpp:79
bool isQuad() const
Definition planmodel.hpp:81
int v2
Definition planmodel.hpp:79
int v3
Definition planmodel.hpp:79
int vertexCount() const
Definition planmodel.hpp:82
int v0
Definition planmodel.hpp:79
void setVertex(int k, int v)
Definition planmodel.hpp:84
Definition planmodel.hpp:74
Point2 xy
Definition planmodel.hpp:75
double z
Definition planmodel.hpp:76
Definition planmodel.hpp:120
Iterator & operator++()
Definition planmodel.hpp:124
T operator*() const
Definition planmodel.hpp:123
bool operator!=(const Iterator &b) const
Definition planmodel.hpp:129
const ReadArray * array
Definition planmodel.hpp:121
std::size_t i
Definition planmodel.hpp:122
Definition planmodel.hpp:147
Point2 xy
Definition planmodel.hpp:148
bool hasZ
Definition planmodel.hpp:152
double z
Definition planmodel.hpp:151
int marker
Definition planmodel.hpp:149
Text tag
Definition planmodel.hpp:150
int y
Definition terrainerrorfield.cpp:60
double a
Definition terrainerrorfield.cpp:42
double b
Definition terrainerrorfield.cpp:42
quint64 count
Definition terrainerrorfield.cpp:43
double c
Definition terrainerrorfield.cpp:42
int x
Definition terrainerrorfield.cpp:60
double y0
Definition terrainerrorfield.cpp:41
double y1
Definition terrainerrorfield.cpp:41
double x1
Definition terrainerrorfield.cpp:41
double x0
Definition terrainerrorfield.cpp:41