/src/geos/src/operation/grid/Traversal.cpp
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * GEOS - Geometry Engine Open Source |
4 | | * http://geos.osgeo.org |
5 | | * |
6 | | * Copyright (C) 2018-2025 ISciences, LLC |
7 | | * |
8 | | * This is free software; you can redistribute and/or modify it under |
9 | | * the terms of the GNU Lesser General Public Licence as published |
10 | | * by the Free Software Foundation. |
11 | | * See the COPYING file for more information. |
12 | | * |
13 | | **********************************************************************/ |
14 | | |
15 | | #include <cstddef> |
16 | | #include <stdexcept> |
17 | | |
18 | | #include <geos/algorithm/Area.h> |
19 | | #include <geos/operation/grid/Traversal.h> |
20 | | |
21 | | using geos::geom::CoordinateXY; |
22 | | |
23 | | namespace geos::operation::grid { |
24 | | |
25 | | void |
26 | | Traversal::add(const CoordinateXY& c) |
27 | 0 | { |
28 | 0 | if (m_coords.empty() || (m_coords.back() != c)) { |
29 | 0 | m_coords.push_back(c); |
30 | 0 | } |
31 | 0 | } |
32 | | |
33 | | bool |
34 | | Traversal::isEmpty() const |
35 | 0 | { |
36 | 0 | return m_coords.empty(); |
37 | 0 | } |
38 | | |
39 | | void |
40 | | Traversal::enter(const CoordinateXY& c, Side s, const void* parentage) |
41 | 0 | { |
42 | 0 | if (!m_coords.empty()) { |
43 | 0 | throw std::runtime_error("Traversal already started"); |
44 | 0 | } |
45 | | |
46 | 0 | add(c); |
47 | 0 | m_entry = s; |
48 | 0 | m_parentage = parentage; |
49 | 0 | } |
50 | | |
51 | | void |
52 | | Traversal::exit(const CoordinateXY& c, Side s) |
53 | 0 | { |
54 | 0 | add(c); |
55 | 0 | m_exit = s; |
56 | 0 | } |
57 | | |
58 | | bool |
59 | | Traversal::isClosedRing() const |
60 | 0 | { |
61 | 0 | return m_coords.size() >= 3 && m_coords[0] == m_coords[m_coords.size() - 1]; |
62 | 0 | } |
63 | | |
64 | | bool |
65 | | Traversal::isClosedRingWithArea() const |
66 | 0 | { |
67 | | // TODO: Don't actually need to compute the area here, just need to make sure there are >= 3 unique points. |
68 | 0 | return isClosedRing() && algorithm::Area::ofRing(m_coords) > 0; |
69 | 0 | } |
70 | | |
71 | | bool |
72 | | Traversal::isEntered() const |
73 | 0 | { |
74 | 0 | return m_entry != Side::NONE; |
75 | 0 | } |
76 | | |
77 | | bool |
78 | | Traversal::isExited() const |
79 | 0 | { |
80 | 0 | return m_exit != Side::NONE; |
81 | 0 | } |
82 | | |
83 | | bool |
84 | | Traversal::hasMultipleUniqueCoordinates() const |
85 | 0 | { |
86 | 0 | for (size_t i = 1; i < m_coords.size(); i++) { |
87 | 0 | if (m_coords[0] != m_coords[i]) { |
88 | 0 | return true; |
89 | 0 | } |
90 | 0 | } |
91 | | |
92 | 0 | return false; |
93 | 0 | } |
94 | | |
95 | | bool |
96 | | Traversal::isTraversed() const |
97 | 0 | { |
98 | 0 | return isEntered() && isExited(); |
99 | 0 | } |
100 | | |
101 | | const CoordinateXY& |
102 | | Traversal::getFirstCoordinate() const |
103 | 0 | { |
104 | 0 | return m_coords.front(); |
105 | 0 | } |
106 | | |
107 | | const CoordinateXY& |
108 | | Traversal::getLastCoordinate() const |
109 | 0 | { |
110 | 0 | return m_coords.back(); |
111 | 0 | } |
112 | | |
113 | | const CoordinateXY& |
114 | | Traversal::getExitCoordinate() const |
115 | 0 | { |
116 | 0 | if (!isExited()) { |
117 | 0 | throw std::runtime_error("Can't get exit coordinate from incomplete traversal."); |
118 | 0 | } |
119 | | |
120 | 0 | return getLastCoordinate(); |
121 | 0 | } |
122 | | |
123 | | } |