Path: blob/main/tests/tools/sumolib/geomhelper/runner.py
428384 views
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo1# Copyright (C) 2013-2026 German Aerospace Center (DLR) and others.2# This program and the accompanying materials are made available under the3# terms of the Eclipse Public License 2.0 which is available at4# https://www.eclipse.org/legal/epl-2.0/5# This Source Code may also be made available under the following Secondary6# Licenses when the conditions for such availability set forth in the Eclipse7# Public License 2.0 are satisfied: GNU General Public License, version 28# or later which is available at9# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html10# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1112# @file runner.py13# @author Daniel Krajzewicz14# @author Jakob Erdmann15# @author Michael Behrisch16# @author Matthias Schwamborn17# @date 2013-02-251819import os20import sys21import unittest22# Do not use SUMO_HOME here to ensure you are always testing the23# functions from the same tree the test is in24sys.path.append(25os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', 'tools'))26import sumolib # noqa272829class TestGeomhelper(unittest.TestCase):3031def testPolygonOffsetWithMinimumDistanceToPoint(self):32"""Return the offset from the polygon start where the distance to point is minimal"""33offsetMinDist = sumolib.geomhelper.polygonOffsetWithMinimumDistanceToPoint34shape = ((0, 1), (0, 0), (1, 0))35self.assertEqual(1, offsetMinDist((-1, -1), shape, False))36self.assertEqual(1, offsetMinDist((-1, -1), shape, True))37self.assertEqual(2, offsetMinDist((2, 1), shape, False))38self.assertEqual(0, offsetMinDist((2, 1), shape, True))39self.assertEqual(2, offsetMinDist((3, 2), shape, False))40self.assertEqual(41sumolib.geomhelper.INVALID_DISTANCE, offsetMinDist((3, 2), shape, True))4243def testDistancePointToPolygon(self):44point = (81365.994719034992, 9326.8304398041219)45polygon = [46(81639.699999999997, 9196.8400000000001),47(81554.910000000003, 9246.7600000000002),48(81488.800000000003, 9288.2999999999993),49(81376.100000000006, 9358.5799999999999),50(81305.089999999997, 9404.4400000000005),51(81230.610000000001, 9452.4200000000001),52(81154.699999999997, 9502.6000000000004),53(81063.419999999998, 9564.5799999999999),54(80969.389999999999, 9627.6100000000006),55(80882.990000000005, 9686.3899999999994),56(80772.160000000003, 9763.4200000000001),57(80682.259999999995, 9825.4500000000007),58(80617.509999999995, 9868.1499999999996),59(80552.660000000003, 9914.1900000000005)]60dist = sumolib.geomhelper.distancePointToPolygon(point, polygon, True)61self.assertTrue(abs(dist - 32.288) < 0.01)6263def testPointInPoly(self):64convex = [(0, 0), (2, 0), (2, 2), (0, 2)]65self.assertEqual(sumolib.geomhelper.isWithin((1, 1), convex), True)66self.assertEqual(sumolib.geomhelper.isWithin((-1, -1), convex), False)67self.assertEqual(sumolib.geomhelper.isWithin((3, 3), convex), False)68self.assertEqual(sumolib.geomhelper.isWithin((1.5, 1.5), convex), True)69concave = [(0, 0), (2, 0), (2, 1), (1, 1), (1, 2), (0, 2)]70self.assertEqual(71sumolib.geomhelper.isWithin((0.5, 0.5), concave), True)72self.assertEqual(73sumolib.geomhelper.isWithin((1.5, 1.5), concave), False)7475def testSplitPolygonAtLengths2D(self):76singlePointPolygon = [(0, 0)]77polygon1 = [(0, 0), (3, 0)]78lengths1 = [1]79polygon1Slices = [[(0, 0), (1, 0)], [(1, 0), (3, 0)]]80self.assertEqual(sumolib.geomhelper.splitPolygonAtLengths2D(polygon1, []), [polygon1])81self.assertEqual(sumolib.geomhelper.splitPolygonAtLengths2D(singlePointPolygon, lengths1), [singlePointPolygon])82self.assertEqual(sumolib.geomhelper.splitPolygonAtLengths2D(polygon1, lengths1), polygon1Slices)8384def testIntersectsAtLengths2D(self):85polygon1 = [(0, 0), (3, 0)]86polygon2 = [(1, 0), (2, 0)]87self.assertEqual(sumolib.geomhelper.intersectsPolygon(polygon1, polygon2), True)88self.assertEqual(sumolib.geomhelper.intersectsAtLengths2D(polygon1, polygon2), [1.0, 2.0])89polygon3 = [(0, 0), (2, 0)]90self.assertEqual(sumolib.geomhelper.intersectsAtLengths2D(polygon1, polygon3), [0.0, 2.0])91polygon4 = [(1, 0), (3, 0)]92self.assertEqual(sumolib.geomhelper.intersectsAtLengths2D(polygon1, polygon4), [1.0, 3.0])93polygon5 = [(-1, 0), (2, 0)]94self.assertEqual(sumolib.geomhelper.intersectsAtLengths2D(polygon1, polygon5), [0.0, 2.0])95polygon6 = [(1, 0), (4, 0)]96self.assertEqual(sumolib.geomhelper.intersectsAtLengths2D(polygon1, polygon6), [1.0, 3.0])97polygon7 = [(-1, 0), (4, 0)]98self.assertEqual(sumolib.geomhelper.intersectsAtLengths2D(polygon1, polygon7), [0.0, 3.0])99100def testIsClosedPolygon(self):101polygon1 = [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]102polygon2 = [(0, 0), (1, 0), (1, 1), (0, 1)]103self.assertEqual(sumolib.geomhelper.isClosedPolygon(polygon1), True)104self.assertEqual(sumolib.geomhelper.isClosedPolygon(polygon2), False)105106107if __name__ == '__main__':108unittest.main()109110111