Loading...
Searching...
No Matches
meshcellgeom.h
Go to the documentation of this file.
1
26
27#ifndef OPENSWMMVIS_MESH_MESHCELLGEOM_H
28#define OPENSWMMVIS_MESH_MESHCELLGEOM_H
29
30#include "mesh/meshresult.h"
31
32#include <QPointF>
33#include <QVector>
34
35#include <array>
36#include <cmath>
37
38namespace mesh
39{
40
42constexpr int kEdgeStride = 4;
43
53constexpr int edgeSlot(int cell, int e) noexcept
54{
55 return cell * kEdgeStride + e;
56}
57
65constexpr int slotCell(int slot) noexcept
66{
67 return slot / kEdgeStride;
68}
69
75constexpr int slotLocal(int slot) noexcept
76{
77 return slot % kEdgeStride;
78}
79
87constexpr int edgeSlotCount(int nCells) noexcept
88{
89 return nCells * kEdgeStride;
90}
91
104inline void edgeEndpoints(const MeshTriangle &t, int k, int &a, int &b) noexcept
105{
106 const int nv = t.vertexCount();
107 a = t.vertex((k + 1) % nv);
108 b = t.vertex((k + 2) % nv);
109}
110
141inline bool rt0CellDischarge(int cell, int nv, const float *flux, const float *length,
142 const float *nx, const float *ny, double &qx, double &qy) noexcept
143{
144 constexpr double kQMax = 10.0;
145 double a00 = 0.0, a01 = 0.0, a11 = 0.0; // NᵀN entries
146 double b0 = 0.0, b1 = 0.0; // Nᵀ b entries
147 for (int e = 0; e < nv; ++e) {
148 const int idx = edgeSlot(cell, e);
149 const double len = length[idx];
150 if (len <= 1e-12)
151 continue;
152 double q = double(flux[idx]) / len;
153 if (!std::isfinite(q))
154 continue;
155 if (q > kQMax)
156 q = kQMax;
157 if (q < -kQMax)
158 q = -kQMax;
159 const double enx = nx[idx], eny = ny[idx];
160 a00 += enx * enx;
161 a01 += enx * eny;
162 a11 += eny * eny;
163 b0 += enx * q;
164 b1 += eny * q;
165 }
166 const double det = a00 * a11 - a01 * a01;
167 // NaN-robust degeneracy gate: `abs(NaN) < eps` is false, so the inverted
168 // form is required to reject the cell instead of emitting NaN.
169 if (!(std::abs(det) >= 1e-12))
170 return false;
171 const double inv_det = 1.0 / det;
172 qx = (a11 * b0 - a01 * b1) * inv_det;
173 qy = (-a01 * b0 + a00 * b1) * inv_det;
174 return true;
175}
176
200inline bool cellVelocity(int cell, int nv, const float *flux, const float *length, const float *nx,
201 const float *ny, double depth, double dryDepth, double &vx,
202 double &vy) noexcept
203{
204 vx = vy = 0.;
205 if (!std::isfinite(depth) || depth <= 0. || depth < dryDepth)
206 return false;
207 double qx = 0., qy = 0.;
208 if (!rt0CellDischarge(cell, nv, flux, length, nx, ny, qx, qy))
209 return false;
210 vx = qx / depth;
211 vy = qy / depth;
212 return std::isfinite(vx) && std::isfinite(vy);
213}
214
216struct CellGeom {
217 double area = 0.0;
218 QPointF centroid;
219 double zMean = 0.0;
220 int nSub = 1;
221 std::array<std::array<int, 3>, 2> sub{};
222 int diagCase = 0;
223};
224
247inline CellGeom cellGeom(const QVector<MeshVertex> &vertices, const MeshTriangle &t)
248{
249 CellGeom g;
250 auto triArea = [&](int a, int b, int c) {
251 const QPointF &A = vertices[a].xy, &B = vertices[b].xy, &C = vertices[c].xy;
252 return 0.5 *
253 std::abs((B.x() - A.x()) * (C.y() - A.y()) - (C.x() - A.x()) * (B.y() - A.y()));
254 };
255 auto triCentroid = [&](int a, int b, int c) {
256 const QPointF &A = vertices[a].xy, &B = vertices[b].xy, &C = vertices[c].xy;
257 return QPointF((A.x() + B.x() + C.x()) / 3.0, (A.y() + B.y() + C.y()) / 3.0);
258 };
259 if (!t.isQuad()) {
260 g.nSub = 1;
261 g.sub[0] = {t.v0, t.v1, t.v2};
262 g.area = triArea(t.v0, t.v1, t.v2);
263 g.centroid = triCentroid(t.v0, t.v1, t.v2);
264 g.zMean = (vertices[t.v0].z + vertices[t.v1].z + vertices[t.v2].z) / 3.0;
265 return g;
266 }
267 const int v[4] = {t.v0, t.v1, t.v2, t.v3};
268 // Elevation ordering of the cyclic positions (stable insertion sort).
269 int p[4] = {0, 1, 2, 3};
270 for (int i = 1; i < 4; ++i) {
271 const int key = p[i];
272 int j = i - 1;
273 while (j >= 0 && vertices[v[p[j]]].z > vertices[v[key]].z) {
274 p[j + 1] = p[j];
275 --j;
276 }
277 p[j + 1] = key;
278 }
279 const int n1 = p[0], n2 = p[1], n3 = p[2], n4 = p[3];
280 auto adjacent = [](int a, int b) {
281 const int d = (a - b + 4) % 4;
282 return d == 1 || d == 3;
283 };
284 int t1[3], t2[3];
285 if (!adjacent(n1, n4)) {
286 g.diagCase = 1;
287 t1[0] = n1;
288 t1[1] = n2;
289 t1[2] = n4;
290 t2[0] = n1;
291 t2[1] = n3;
292 t2[2] = n4;
293 } else if (adjacent(n2, n1)) {
294 g.diagCase = 2;
295 t1[0] = n1;
296 t1[1] = n2;
297 t1[2] = n4;
298 t2[0] = n2;
299 t2[1] = n3;
300 t2[2] = n4;
301 } else {
302 g.diagCase = 3;
303 t1[0] = n1;
304 t1[1] = n3;
305 t1[2] = n4;
306 t2[0] = n2;
307 t2[1] = n3;
308 t2[2] = n4;
309 }
310 g.nSub = 2;
311 g.sub[0] = {v[t1[0]], v[t1[1]], v[t1[2]]};
312 g.sub[1] = {v[t2[0]], v[t2[1]], v[t2[2]]};
313 const double a1 = triArea(g.sub[0][0], g.sub[0][1], g.sub[0][2]);
314 const double a2 = triArea(g.sub[1][0], g.sub[1][1], g.sub[1][2]);
315 g.area = a1 + a2;
316 const QPointF c1 = triCentroid(g.sub[0][0], g.sub[0][1], g.sub[0][2]);
317 const QPointF c2 = triCentroid(g.sub[1][0], g.sub[1][1], g.sub[1][2]);
318 g.centroid =
319 (g.area > 0.0)
320 ? QPointF((a1 * c1.x() + a2 * c2.x()) / g.area, (a1 * c1.y() + a2 * c2.y()) / g.area)
321 : QPointF(
322 (vertices[t.v0].xy + vertices[t.v1].xy + vertices[t.v2].xy + vertices[t.v3].xy) /
323 4.0);
324 g.zMean = (vertices[t.v0].z + vertices[t.v1].z + vertices[t.v2].z + vertices[t.v3].z) / 4.0;
325 return g;
326}
327
339inline double cellSignedArea(const QVector<MeshVertex> &vertices, const MeshTriangle &t) noexcept
340{
341 const int nv = t.vertexCount();
342 const QPointF origin = vertices[t.v0].xy;
343 double s = 0.0;
344 for (int k = 0; k < nv; ++k) {
345 // Translate before multiplication: projected map coordinates can
346 // otherwise cancel the small area and even reverse its sign.
347 const QPointF a = vertices[t.vertex(k)].xy - origin;
348 const QPointF b = vertices[t.vertex((k + 1) % nv)].xy - origin;
349 s += a.x() * b.y() - b.x() * a.y();
350 }
351 return 0.5 * s;
352}
353
367inline bool cellIsConvex(const QVector<MeshVertex> &vertices, const MeshTriangle &t) noexcept
368{
369 if (!t.isQuad())
370 return true;
371 int sign = 0;
372 for (int k = 0; k < 4; ++k) {
373 const QPointF &p = vertices[t.vertex(k)].xy;
374 const QPointF &q = vertices[t.vertex((k + 1) % 4)].xy;
375 const QPointF &r = vertices[t.vertex((k + 2) % 4)].xy;
376 const double cr = (q.x() - p.x()) * (r.y() - q.y()) - (q.y() - p.y()) * (r.x() - q.x());
377 const int s = (cr > 0.0) ? 1 : (cr < 0.0) ? -1 : 0;
378 if (s == 0 || (sign != 0 && s != sign))
379 return false;
380 sign = s;
381 }
382 return true;
383}
384
401inline bool cellContains(const QVector<MeshVertex> &vertices, const MeshTriangle &t,
402 const QPointF &p, double eps = 0.0) noexcept
403{
404 const CellGeom g = cellGeom(vertices, t);
405 for (int s = 0; s < g.nSub; ++s) {
406 const QPointF &a = vertices[g.sub[s][0]].xy, &b = vertices[g.sub[s][1]].xy,
407 &c = vertices[g.sub[s][2]].xy;
408 const double d = (b.y() - c.y()) * (a.x() - c.x()) + (c.x() - b.x()) * (a.y() - c.y());
409 if (std::abs(d) < 1e-300)
410 continue;
411 const double l1 =
412 ((b.y() - c.y()) * (p.x() - c.x()) + (c.x() - b.x()) * (p.y() - c.y())) / d;
413 const double l2 =
414 ((c.y() - a.y()) * (p.x() - c.x()) + (a.x() - c.x()) * (p.y() - c.y())) / d;
415 const double l3 = 1.0 - l1 - l2;
416 if (l1 >= -eps && l2 >= -eps && l3 >= -eps)
417 return true;
418 }
419 return false;
420}
421
422} // namespace mesh
423
424#endif // OPENSWMMVIS_MESH_MESHCELLGEOM_H
value types for the 2D mesh generator.
The Qt-facing MeshKit API (meshkit::core_qt, meshkit::model_qt, meshkit::io_qt): mesh value types and...
bool cellContains(const QVector< MeshVertex > &vertices, const MeshTriangle &t, const QPointF &p, double eps=0.0) noexcept
Test a point against the elevation-selected cell subtriangle fan using barycentric slack.
Definition meshcellgeom.h:401
void edgeEndpoints(const MeshTriangle &t, int k, int &a, int &b) noexcept
Resolve the ordered vertex IDs of one cell-local opposite-edge convention.
Definition meshcellgeom.h:104
bool rt0CellDischarge(int cell, int nv, const float *flux, const float *length, const float *nx, const float *ny, double &qx, double &qy) noexcept
Reconstruct planar specific discharge by least-squares fitting outward edge-normal discharge.
Definition meshcellgeom.h:141
CellGeom cellGeom(const QVector< MeshVertex > &vertices, const MeshTriangle &t)
Compute triangle/quad subtriangles, positive fan area, area centroid and arithmetic mean elevation.
Definition meshcellgeom.h:247
constexpr int edgeSlot(int cell, int e) noexcept
Compute the flat stride-four slot for one cell-local edge.
Definition meshcellgeom.h:53
constexpr int kEdgeStride
Edge slots per cell in every flat per-edge array (== engine kMaxCellVerts).
Definition meshcellgeom.h:42
double cellSignedArea(const QVector< MeshVertex > &vertices, const MeshTriangle &t) noexcept
Compute oriented polygon area in local coordinates to reduce projected-origin cancellation.
Definition meshcellgeom.h:339
bool cellVelocity(int cell, int nv, const float *flux, const float *length, const float *nx, const float *ny, double depth, double dryDepth, double &vx, double &vy) noexcept
Reconstruct cell velocity from edge discharge only when a usable wet depth is available.
Definition meshcellgeom.h:200
bool cellIsConvex(const QVector< MeshVertex > &vertices, const MeshTriangle &t) noexcept
Check strict consistent quad turns, treating every triangle as convex without inspection.
Definition meshcellgeom.h:367
constexpr int slotLocal(int slot) noexcept
Decode local edge position from a stride-four flat slot.
Definition meshcellgeom.h:75
constexpr int slotCell(int slot) noexcept
Decode the owning cell index from a stride-four flat edge slot.
Definition meshcellgeom.h:65
constexpr int edgeSlotCount(int nCells) noexcept
Calculate padded per-edge storage length for a cell count.
Definition meshcellgeom.h:87
Per-cell derived geometry shared by every consumer.
Definition meshcellgeom.h:216
double zMean
mean of the vertex elevations
Definition meshcellgeom.h:219
std::array< std::array< int, 3 >, 2 > sub
sub-triangle vertex indices
Definition meshcellgeom.h:221
QPointF centroid
AREA centroid (not the vertex mean for a quad)
Definition meshcellgeom.h:218
int diagCase
B&S 2007 case (1..3) for a quad; 0 for a triangle.
Definition meshcellgeom.h:222
int nSub
sub-triangles: 1 (triangle) or 2 (quad)
Definition meshcellgeom.h:220
double area
planimetric (map units²)
Definition meshcellgeom.h:217
A cell in the mesh — a triangle or (since the engine's mixed tri-quad meshes, workplans/TRI_QUAD_MESH...
Definition meshresult.h:64
int v2
Third corner, cyclically after v1.
Definition meshresult.h:71
int v1
Second corner, cyclically after v0.
Definition meshresult.h:71
int v3
Fourth vertex of a quadrilateral; -1 = triangle.
Definition meshresult.h:72
int v0
First corner: index into MeshResult::vertices (not range-checked here).
Definition meshresult.h:71
bool isQuad() const noexcept
Classify the cell solely by whether a fourth vertex identifier is present.
Definition meshresult.h:99