Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/unittest/src/utils/geom/GeomHelperTest.cpp
169684 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
14
/// @file GeomHelperTest.cpp
15
/// @author Jakob Erdmann
16
/// @author Laura Bieker
17
/// @author Michael Behrisch
18
/// @date 2011-12-12
19
///
20
// Tests the class GeomHelper
21
/****************************************************************************/
22
#include <config.h>
23
24
#include <gtest/gtest.h>
25
#include <utils/geom/Position.h>
26
#include <utils/geom/GeomHelper.h>
27
28
using namespace std;
29
30
/*
31
TEST(GeomHelper, test_method_intersects) {
32
// on the same line but non-overlapping segments
33
EXPECT_FALSE(GeomHelper::intersects(
34
Position(0,0), Position(1,0),
35
Position(2,0), Position(3,0)));
36
37
// overlapping line segments
38
EXPECT_TRUE(GeomHelper::intersects(
39
Position(0,0), Position(2,0),
40
Position(1,0), Position(3,0)));
41
42
// identical line segments
43
EXPECT_TRUE(GeomHelper::intersects(
44
Position(1,0), Position(3,0),
45
Position(1,0), Position(3,0)));
46
47
// parallel line segments
48
EXPECT_FALSE(GeomHelper::intersects(
49
Position(0,0), Position(1,0),
50
Position(0,1), Position(1,1)));
51
52
// intersection outside of segments
53
EXPECT_FALSE(GeomHelper::intersects(
54
Position(0,0), Position(2,0),
55
Position(1,3), Position(1,1)));
56
57
// intersection at (1,0)
58
EXPECT_TRUE(GeomHelper::intersects(
59
Position(0,0), Position(2,0),
60
Position(1,3), Position(1,-1)));
61
}
62
63
64
TEST(GeomHelper, test_method_intersection_position2D) {
65
Position expected(1,0);
66
Position pos = GeomHelper::intersection_position2D(
67
Position(0,0), Position(2,0),
68
Position(1,3), Position(1,-1));
69
EXPECT_FLOAT_EQ(expected.x(), pos.x());
70
EXPECT_FLOAT_EQ(expected.y(), pos.y());
71
// overlapping line segments
72
Position expected2(1.5,0);
73
Position pos2 = GeomHelper::intersection_position2D(
74
Position(0,0), Position(2,0),
75
Position(1,0), Position(3,0));
76
EXPECT_FLOAT_EQ(expected2.x(), pos2.x());
77
EXPECT_FLOAT_EQ(expected2.y(), pos2.y());
78
// identical line segments
79
Position expected3(1,0);
80
Position pos3 = GeomHelper::intersection_position2D(
81
Position(0,0), Position(2,0),
82
Position(0,0), Position(2,0));
83
EXPECT_FLOAT_EQ(expected3.x(), pos3.x());
84
EXPECT_FLOAT_EQ(expected3.y(), pos3.y());
85
}
86
87
88
TEST(GeomHelper, test_method_closestDistancePointLine_basic) {
89
Position expected(1,0);
90
double expectedDistance = 1;
91
Position point(1,1);
92
Position start(0,0);
93
Position end(2,0);
94
Position closestPoint;
95
double result = GeomHelper::closestDistancePointLine2D(point, start, end, closestPoint);
96
EXPECT_FLOAT_EQ(expected.x(), closestPoint.x());
97
EXPECT_FLOAT_EQ(expected.y(), closestPoint.y());
98
EXPECT_FLOAT_EQ(expected.z(), closestPoint.z());
99
EXPECT_FLOAT_EQ(expectedDistance, result);
100
}
101
TEST(GeomHelper, test_method_closestDistancePointLine_onLine) {
102
Position expected(1,0);
103
double expectedDistance = 0;
104
Position point(1,0);
105
Position start(0,0);
106
Position end(2,0);
107
Position closestPoint;
108
double result = GeomHelper::closestDistancePointLine2D(point, start, end, closestPoint);
109
EXPECT_FLOAT_EQ(expected.x(), closestPoint.x());
110
EXPECT_FLOAT_EQ(expected.y(), closestPoint.y());
111
EXPECT_FLOAT_EQ(expected.z(), closestPoint.z());
112
EXPECT_FLOAT_EQ(expectedDistance, result);
113
}
114
TEST(GeomHelper, test_method_closestDistancePointLine_outside_after) {
115
Position expected(2,0);
116
double expectedDistance = 5;
117
Position point(5,4);
118
Position start(0,0);
119
Position end(2,0);
120
Position closestPoint;
121
double result = GeomHelper::closestDistancePointLine2D(point, start, end, closestPoint);
122
EXPECT_FLOAT_EQ(expected.x(), closestPoint.x());
123
EXPECT_FLOAT_EQ(expected.y(), closestPoint.y());
124
EXPECT_FLOAT_EQ(expected.z(), closestPoint.z());
125
EXPECT_FLOAT_EQ(expectedDistance, result);
126
}
127
TEST(GeomHelper, test_method_closestDistancePointLine_outside_before) {
128
Position expected(0,0);
129
double expectedDistance = 5;
130
Position point(-3,4);
131
Position start(0,0);
132
Position end(2,0);
133
Position closestPoint;
134
double result = GeomHelper::closestDistancePointLine2D(point, start, end, closestPoint);
135
EXPECT_FLOAT_EQ(expected.x(), closestPoint.x());
136
EXPECT_FLOAT_EQ(expected.y(), closestPoint.y());
137
EXPECT_FLOAT_EQ(expected.z(), closestPoint.z());
138
EXPECT_FLOAT_EQ(expectedDistance, result);
139
}*/
140
141