Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/tools/sumolib/geomhelper/runner.py
428384 views
1
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
2
# Copyright (C) 2013-2026 German Aerospace Center (DLR) and others.
3
# This program and the accompanying materials are made available under the
4
# terms of the Eclipse Public License 2.0 which is available at
5
# https://www.eclipse.org/legal/epl-2.0/
6
# This Source Code may also be made available under the following Secondary
7
# Licenses when the conditions for such availability set forth in the Eclipse
8
# Public License 2.0 are satisfied: GNU General Public License, version 2
9
# or later which is available at
10
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
11
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
12
13
# @file runner.py
14
# @author Daniel Krajzewicz
15
# @author Jakob Erdmann
16
# @author Michael Behrisch
17
# @author Matthias Schwamborn
18
# @date 2013-02-25
19
20
import os
21
import sys
22
import unittest
23
# Do not use SUMO_HOME here to ensure you are always testing the
24
# functions from the same tree the test is in
25
sys.path.append(
26
os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', 'tools'))
27
import sumolib # noqa
28
29
30
class TestGeomhelper(unittest.TestCase):
31
32
def testPolygonOffsetWithMinimumDistanceToPoint(self):
33
"""Return the offset from the polygon start where the distance to point is minimal"""
34
offsetMinDist = sumolib.geomhelper.polygonOffsetWithMinimumDistanceToPoint
35
shape = ((0, 1), (0, 0), (1, 0))
36
self.assertEqual(1, offsetMinDist((-1, -1), shape, False))
37
self.assertEqual(1, offsetMinDist((-1, -1), shape, True))
38
self.assertEqual(2, offsetMinDist((2, 1), shape, False))
39
self.assertEqual(0, offsetMinDist((2, 1), shape, True))
40
self.assertEqual(2, offsetMinDist((3, 2), shape, False))
41
self.assertEqual(
42
sumolib.geomhelper.INVALID_DISTANCE, offsetMinDist((3, 2), shape, True))
43
44
def testDistancePointToPolygon(self):
45
point = (81365.994719034992, 9326.8304398041219)
46
polygon = [
47
(81639.699999999997, 9196.8400000000001),
48
(81554.910000000003, 9246.7600000000002),
49
(81488.800000000003, 9288.2999999999993),
50
(81376.100000000006, 9358.5799999999999),
51
(81305.089999999997, 9404.4400000000005),
52
(81230.610000000001, 9452.4200000000001),
53
(81154.699999999997, 9502.6000000000004),
54
(81063.419999999998, 9564.5799999999999),
55
(80969.389999999999, 9627.6100000000006),
56
(80882.990000000005, 9686.3899999999994),
57
(80772.160000000003, 9763.4200000000001),
58
(80682.259999999995, 9825.4500000000007),
59
(80617.509999999995, 9868.1499999999996),
60
(80552.660000000003, 9914.1900000000005)]
61
dist = sumolib.geomhelper.distancePointToPolygon(point, polygon, True)
62
self.assertTrue(abs(dist - 32.288) < 0.01)
63
64
def testPointInPoly(self):
65
convex = [(0, 0), (2, 0), (2, 2), (0, 2)]
66
self.assertEqual(sumolib.geomhelper.isWithin((1, 1), convex), True)
67
self.assertEqual(sumolib.geomhelper.isWithin((-1, -1), convex), False)
68
self.assertEqual(sumolib.geomhelper.isWithin((3, 3), convex), False)
69
self.assertEqual(sumolib.geomhelper.isWithin((1.5, 1.5), convex), True)
70
concave = [(0, 0), (2, 0), (2, 1), (1, 1), (1, 2), (0, 2)]
71
self.assertEqual(
72
sumolib.geomhelper.isWithin((0.5, 0.5), concave), True)
73
self.assertEqual(
74
sumolib.geomhelper.isWithin((1.5, 1.5), concave), False)
75
76
def testSplitPolygonAtLengths2D(self):
77
singlePointPolygon = [(0, 0)]
78
polygon1 = [(0, 0), (3, 0)]
79
lengths1 = [1]
80
polygon1Slices = [[(0, 0), (1, 0)], [(1, 0), (3, 0)]]
81
self.assertEqual(sumolib.geomhelper.splitPolygonAtLengths2D(polygon1, []), [polygon1])
82
self.assertEqual(sumolib.geomhelper.splitPolygonAtLengths2D(singlePointPolygon, lengths1), [singlePointPolygon])
83
self.assertEqual(sumolib.geomhelper.splitPolygonAtLengths2D(polygon1, lengths1), polygon1Slices)
84
85
def testIntersectsAtLengths2D(self):
86
polygon1 = [(0, 0), (3, 0)]
87
polygon2 = [(1, 0), (2, 0)]
88
self.assertEqual(sumolib.geomhelper.intersectsPolygon(polygon1, polygon2), True)
89
self.assertEqual(sumolib.geomhelper.intersectsAtLengths2D(polygon1, polygon2), [1.0, 2.0])
90
polygon3 = [(0, 0), (2, 0)]
91
self.assertEqual(sumolib.geomhelper.intersectsAtLengths2D(polygon1, polygon3), [0.0, 2.0])
92
polygon4 = [(1, 0), (3, 0)]
93
self.assertEqual(sumolib.geomhelper.intersectsAtLengths2D(polygon1, polygon4), [1.0, 3.0])
94
polygon5 = [(-1, 0), (2, 0)]
95
self.assertEqual(sumolib.geomhelper.intersectsAtLengths2D(polygon1, polygon5), [0.0, 2.0])
96
polygon6 = [(1, 0), (4, 0)]
97
self.assertEqual(sumolib.geomhelper.intersectsAtLengths2D(polygon1, polygon6), [1.0, 3.0])
98
polygon7 = [(-1, 0), (4, 0)]
99
self.assertEqual(sumolib.geomhelper.intersectsAtLengths2D(polygon1, polygon7), [0.0, 3.0])
100
101
def testIsClosedPolygon(self):
102
polygon1 = [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]
103
polygon2 = [(0, 0), (1, 0), (1, 1), (0, 1)]
104
self.assertEqual(sumolib.geomhelper.isClosedPolygon(polygon1), True)
105
self.assertEqual(sumolib.geomhelper.isClosedPolygon(polygon2), False)
106
107
108
if __name__ == '__main__':
109
unittest.main()
110
111