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
contourchain.h
Go to the documentation of this file.
1
13#ifndef OPENSWMMVIS_CONTOUR_CONTOURCHAIN_H
14#define OPENSWMMVIS_CONTOUR_CONTOURCHAIN_H
15
16#include <QLineF>
17#include <QMultiHash>
18#include <QPolygonF>
19#include <QVector>
20
21#include <cmath>
22
24
34inline QVector<QPolygonF>
35chainIsoSegments(const QVector<QLineF> &segs, double quantum)
36{
37 QVector<QPolygonF> chains;
38 if (segs.isEmpty() || quantum <= 0.0) return chains;
39
40 auto key = [quantum](const QPointF &pt) -> quint64 {
41 const auto qx = qint32(std::lround(pt.x() / quantum));
42 const auto qy = qint32(std::lround(pt.y() / quantum));
43 return (quint64(quint32(qx)) << 32) | quint64(quint32(qy));
44 };
45
46 QMultiHash<quint64, int> byEndpoint;
47 byEndpoint.reserve(segs.size() * 2);
48 for (int i = 0; i < segs.size(); ++i) {
49 byEndpoint.insert(key(segs[i].p1()), i);
50 byEndpoint.insert(key(segs[i].p2()), i);
51 }
52
53 QVector<bool> used(segs.size(), false);
54
55 auto takeNext = [&](const QPointF &tip, QPointF &nextPt) -> bool {
56 const auto range = byEndpoint.equal_range(key(tip));
57 for (auto it = range.first; it != range.second; ++it) {
58 const int j = it.value();
59 if (used[j]) continue;
60 used[j] = true;
61 // Continue from whichever endpoint matched the tip.
62 const QLineF &s = segs[j];
63 nextPt = (key(s.p1()) == key(tip)) ? s.p2() : s.p1();
64 return true;
65 }
66 return false;
67 };
68
69 for (int i = 0; i < segs.size(); ++i) {
70 if (used[i]) continue;
71 used[i] = true;
72 QPolygonF poly;
73 poly << segs[i].p1() << segs[i].p2();
74
75 QPointF next;
76 while (takeNext(poly.last(), next)) poly.append(next);
77 while (takeNext(poly.first(), next)) poly.prepend(next);
78 chains.append(std::move(poly));
79 }
80 return chains;
81}
82
83} // namespace OpenSWMM::Contour
84
85#endif // OPENSWMMVIS_CONTOUR_CONTOURCHAIN_H
const char * tip
Definition aquifereditordialog.cpp:55
size_t i
Definition contourjob.cpp:27
double s
Scene px per model length unit.
Definition inletdrawingview.cpp:82
const char * key
Definition meshcellparams.cpp:24
Definition contourchain.h:23
QVector< QPolygonF > chainIsoSegments(const QVector< QLineF > &segs, double quantum)
Chain unordered iso-line segments into polylines.
Definition contourchain.h:35