OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
PolygonRings.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Caleb Buahin
3#pragma once
4
5#include <cstddef>
6#include <vector>
7
9struct Ring {
10 std::size_t first, count; // excludes a repeated closing vertex
11};
12
13// A flat spatial polygon can contain successive explicitly closed rings.
14// A final open ring remains compatible with ordinary SWMM [POLYGONS].
15inline std::vector<Ring> rings(const std::vector<double>& x, const std::vector<double>& y) {
16 std::vector<Ring> out;
17 if (x.size() != y.size())
18 return out;
19 std::size_t first = 0;
20 for (std::size_t i = 1; i < x.size(); ++i) {
21 if (i - first >= 3 && x[i] == x[first] && y[i] == y[first]) {
22 out.push_back({first, i - first});
23 first = i + 1;
24 }
25 }
26 if (x.size() - first >= 3)
27 out.push_back({first, x.size() - first});
28 return out;
29}
30
31inline bool contains(const std::vector<double>& x, const std::vector<double>& y,
32 Ring ring, double px, double py) {
33 bool inside = false;
34 for (std::size_t i = 0, j = ring.count - 1; i < ring.count; j = i++) {
35 const auto a = ring.first + i, b = ring.first + j;
36 if ((y[a] > py) != (y[b] > py) &&
37 px < (x[b] - x[a]) * (py - y[a]) / (y[b] - y[a]) + x[a])
38 inside = !inside;
39 }
40 return inside;
41}
42
43inline bool contains(const std::vector<double>& x, const std::vector<double>& y,
44 double px, double py) {
45 bool inside = false;
46 for (auto ring : rings(x, y))
47 inside ^= contains(x, y, ring, px, py);
48 return inside;
49}
50} // namespace openswmm::polygon
Definition PolygonRings.hpp:8
bool contains(const std::vector< double > &x, const std::vector< double > &y, Ring ring, double px, double py)
Definition PolygonRings.hpp:31
std::vector< Ring > rings(const std::vector< double > &x, const std::vector< double > &y)
Definition PolygonRings.hpp:15
double * y
Definition odesolve.c:37
Definition PolygonRings.hpp:9
std::size_t first
Definition PolygonRings.hpp:10
std::size_t count
Definition PolygonRings.hpp:10