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
meshspatialgrid.h
Go to the documentation of this file.
1
37#ifndef OPENSWMMVIS_LAYERS_MESHSPATIALGRID_H
38#define OPENSWMMVIS_LAYERS_MESHSPATIALGRID_H
39
40#include <QRectF>
41#include <QVector>
42#include <QtGlobal>
43
44#include <algorithm>
45#include <cmath>
46#include <vector>
47
63{
64 // ── Grid geometry (read-only after rebuild) ───────────────────────
65 QRectF extent;
66 double cellW = 0.0;
67 double cellH = 0.0;
68 int cols = 0;
69 int rows = 0;
70
71 // ── CSR cell-occupancy storage ────────────────────────────────────
72 // cellOffsets has size (cols * rows) + 1 when populated; the trailing
73 // sentinel equals cellIndices.size(), so range [offsets[k],
74 // offsets[k+1]) inside cellIndices is always a valid (possibly empty)
75 // slice for cell k.
76 QVector<int> cellOffsets;
77 QVector<int> cellIndices;
78
79 // ── query() scratch state ─────────────────────────────────────────
80 // Sized once in rebuild() and reused for every query. Bumping `epoch`
81 // is an O(1) reset of the visited set. On wrap (every ~4B queries)
82 // a single fill restores invariants. See query() for details.
83 int avgPerCell = 0;
84 mutable std::vector<quint32> seen;
85 mutable quint32 epoch = 0;
86
87 void clear()
88 {
89 extent = {};
90 cellW = cellH = 0.0;
91 cols = rows = 0;
92 cellOffsets.clear();
93 cellIndices.clear();
94 seen.clear();
95 epoch = 0;
96 avgPerCell = 0;
97 }
98
99 [[nodiscard]] bool isEmpty() const { return cellOffsets.isEmpty(); }
100
109 void rebuild(const QVector<QRectF> &bboxes);
110
117 [[nodiscard]] QVector<int> query(const QRectF &rect) const;
118
137 inline void candidatesAtPoint(double x, double y,
138 const int *&begin, const int *&end) const
139 {
140 begin = end = nullptr;
141 if (cellOffsets.isEmpty() || !std::isfinite(x) || !std::isfinite(y))
142 return;
143 const int cx = std::clamp(int(std::floor((x - extent.left()) / cellW)), 0, cols - 1);
144 const int cy = std::clamp(int(std::floor((y - extent.top()) / cellH)), 0, rows - 1);
145 const int k = cy * cols + cx;
146 const int *d = cellIndices.constData();
147 begin = d + cellOffsets[k];
148 end = d + cellOffsets[k + 1];
149 }
150};
151
152#endif // OPENSWMMVIS_LAYERS_MESHSPATIALGRID_H
std::vector< double > cx
Definition mesh2dresultsexport.cpp:103
std::vector< double > cy
area centroid per cell
Definition mesh2dresultsexport.cpp:103
int k
Definition mesh2dresultsexport.cpp:549
Uniform spatial grid index over a set of axis-aligned bboxes.
Definition meshspatialgrid.h:63
double cellW
Definition meshspatialgrid.h:66
void candidatesAtPoint(double x, double y, const int *&begin, const int *&end) const
Candidate bbox indices for the single grid cell containing the point (x,y), returned as the CSR slice...
Definition meshspatialgrid.h:137
QVector< int > cellOffsets
Definition meshspatialgrid.h:76
QVector< int > query(const QRectF &rect) const
Return the bbox indices whose bboxes intersect rect.
Definition meshspatialgrid.cpp:170
std::vector< quint32 > seen
Definition meshspatialgrid.h:84
int rows
Definition meshspatialgrid.h:69
QVector< int > cellIndices
Definition meshspatialgrid.h:77
bool isEmpty() const
Definition meshspatialgrid.h:99
QRectF extent
Definition meshspatialgrid.h:65
void rebuild(const QVector< QRectF > &bboxes)
(Re)build the grid from a set of bboxes.
Definition meshspatialgrid.cpp:39
double cellH
Definition meshspatialgrid.h:67
void clear()
Definition meshspatialgrid.h:87
int cols
Definition meshspatialgrid.h:68
quint32 epoch
Definition meshspatialgrid.h:85
int avgPerCell
Definition meshspatialgrid.h:83