27#ifndef OPENSWMMVIS_MESH_MESHCELLGEOM_H
28#define OPENSWMMVIS_MESH_MESHCELLGEOM_H
53constexpr int edgeSlot(
int cell,
int e)
noexcept
106 const int nv = t.vertexCount();
107 a = t.vertex((k + 1) % nv);
108 b = t.vertex((k + 2) % nv);
142 const float *nx,
const float *ny,
double &qx,
double &qy)
noexcept
144 constexpr double kQMax = 10.0;
145 double a00 = 0.0, a01 = 0.0, a11 = 0.0;
146 double b0 = 0.0, b1 = 0.0;
147 for (
int e = 0; e < nv; ++e) {
149 const double len = length[idx];
152 double q = double(flux[idx]) / len;
153 if (!std::isfinite(q))
159 const double enx = nx[idx], eny = ny[idx];
166 const double det = a00 * a11 - a01 * a01;
169 if (!(std::abs(det) >= 1e-12))
171 const double inv_det = 1.0 / det;
172 qx = (a11 * b0 - a01 * b1) * inv_det;
173 qy = (-a01 * b0 + a00 * b1) * inv_det;
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,
205 if (!std::isfinite(depth) || depth <= 0. || depth < dryDepth)
207 double qx = 0., qy = 0.;
212 return std::isfinite(vx) && std::isfinite(vy);
221 std::array<std::array<int, 3>, 2>
sub{};
250 auto triArea = [&](
int a,
int b,
int c) {
251 const QPointF &A = vertices[a].xy, &B = vertices[b].xy, &C = vertices[c].xy;
253 std::abs((B.x() - A.x()) * (C.y() - A.y()) - (C.x() - A.x()) * (B.y() - A.y()));
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);
264 g.
zMean = (vertices[t.
v0].z + vertices[t.
v1].z + vertices[t.
v2].z) / 3.0;
267 const int v[4] = {t.
v0, t.
v1, t.
v2, t.
v3};
269 int p[4] = {0, 1, 2, 3};
270 for (
int i = 1; i < 4; ++i) {
271 const int key = p[i];
273 while (j >= 0 && vertices[v[p[j]]].z > vertices[v[key]].z) {
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;
285 if (!adjacent(n1, n4)) {
293 }
else if (adjacent(n2, n1)) {
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]);
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]);
320 ? QPointF((a1 * c1.x() + a2 * c2.x()) / g.
area, (a1 * c1.y() + a2 * c2.y()) / g.
area)
322 (vertices[t.
v0].xy + vertices[t.
v1].xy + vertices[t.
v2].xy + vertices[t.
v3].xy) /
324 g.
zMean = (vertices[t.
v0].z + vertices[t.
v1].z + vertices[t.
v2].z + vertices[t.
v3].z) / 4.0;
341 const int nv = t.vertexCount();
342 const QPointF origin = vertices[t.v0].xy;
344 for (
int k = 0; k < nv; ++k) {
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();
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))
402 const QPointF &p,
double eps = 0.0) noexcept
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)
412 ((b.y() - c.y()) * (p.x() - c.x()) + (c.x() - b.x()) * (p.y() - c.y())) / d;
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)
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