Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/geom/GeomHelper.h
169678 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 GeomHelper.h
15
/// @author Daniel Krajzewicz
16
/// @author Friedemann Wesner
17
/// @author Jakob Erdmann
18
/// @author Michael Behrisch
19
/// @date Sept 2002
20
///
21
// Some static methods performing geometrical operations
22
/****************************************************************************/
23
#pragma once
24
#include <config.h>
25
26
#include <cmath>
27
#include "Position.h"
28
#include "PositionVector.h"
29
#include <utils/common/UtilExceptions.h>
30
31
#ifndef M_PI
32
#define M_PI 3.1415926535897932384626433832795
33
#endif
34
35
#define DEG2RAD(x) static_cast<double>((x) * M_PI / 180.)
36
#define RAD2DEG(x) static_cast<double>((x) * 180. / M_PI)
37
#define GRAVITY 9.80665
38
39
40
// ===========================================================================
41
// class definitions
42
// ===========================================================================
43
/** @class GeomHelper
44
* @brief Some static methods performing geometrical operations
45
*/
46
class GeomHelper {
47
48
public:
49
/// @brief a value to signify offsets outside the range of [0, Line.length()]
50
static const double INVALID_OFFSET;
51
52
/** @brief Returns the positions the given circle is crossed by the given line
53
* @param[in] c The center position of the circle
54
* @param[in] radius The radius of the circle
55
* @param[in] p1 The begin of the line
56
* @param[in] p2 The end of the line
57
* @param[filled] into The list of crossing positions (0-1 along the line's length)
58
* @see http://blog.csharphelper.com/2010/03/28/determine-where-a-line-intersects-a-circle-in-c.aspx
59
* @see http://gamedev.stackexchange.com/questions/18333/circle-line-collision-detection-problem (jazzdawg)
60
*/
61
static void findLineCircleIntersections(const Position& c, double radius, const Position& p1, const Position& p2,
62
std::vector<double>& into);
63
64
65
/** @brief Returns the angle between two vectors on a plane
66
The angle is from vector 1 to vector 2, positive anticlockwise
67
The result is between -pi and pi
68
*/
69
static double angle2D(const Position& p1, const Position& p2);
70
71
static double nearest_offset_on_line_to_point2D(
72
const Position& lineStart, const Position& lineEnd,
73
const Position& p, bool perpendicular = true);
74
75
static double nearest_offset_on_line_to_point25D(
76
const Position& lineStart, const Position& lineEnd,
77
const Position& p, bool perpendicular = true);
78
79
static Position crossPoint(const Boundary& b,
80
const PositionVector& v);
81
82
/** @brief Returns the distance of second angle from first angle counter-clockwise
83
* @param[in] angle1 The first angle
84
* @param[in] angle2 The second angle
85
* @return Angle (counter-clockwise) starting from first to second angle
86
*/
87
static double getCCWAngleDiff(double angle1, double angle2);
88
89
90
/** @brief Returns the distance of second angle from first angle clockwise
91
* @param[in] angle1 The first angle
92
* @param[in] angle2 The second angle
93
* @return Angle (clockwise) starting from first to second angle
94
*/
95
static double getCWAngleDiff(double angle1, double angle2);
96
97
98
/** @brief Returns the minimum distance (clockwise/counter-clockwise) between both angles
99
* @param[in] angle1 The first angle
100
* @param[in] angle2 The second angle
101
* @return The minimum distance between both angles
102
*/
103
static double getMinAngleDiff(double angle1, double angle2);
104
105
106
/** @brief Returns the difference of the second angle to the first angle in radiants
107
*
108
* The results are always between -pi and pi.
109
* Positive values denote that the second angle is counter clockwise closer, negative values mean
110
* it is clockwise closer.
111
* @param[in] angle1 The first angle
112
* @param[in] angle2 The second angle
113
* @return angle starting from first to second angle
114
*/
115
static double angleDiff(const double angle1, const double angle2);
116
117
118
/** Converts an angle from mathematical radians where 0 is to the right and positive angles
119
* are counterclockwise to navigational degrees where 0 is up and positive means clockwise.
120
* The result is always in the range [0, 360).
121
* @param[in] angle The angle in radians to convert
122
* @return the angle in degrees
123
*/
124
static double naviDegree(const double angle);
125
126
/** Converts an angle from navigational degrees to mathematical radians.
127
* @see naviDegree
128
* @param[in] angle The angle in degree to convert
129
* @return the angle in radians
130
*/
131
static double fromNaviDegree(const double angle);
132
133
/** Converts an angle from mathematical radians where 0 is to the right and positive angles
134
* are counterclockwise to the legacy degrees used in sumo where 0 is down and positive means clockwise.
135
* If positive is true the result is in the range [0, 360), otherwise in the range [-180, 180).
136
* @param[in] angle The angle in radians to convert
137
* @return the angle in degrees
138
*/
139
static double legacyDegree(const double angle, const bool positive = false);
140
141
/** Creates a circular polygon
142
* @param[in] radius Radius of the circle
143
* @param[in] center Position of the circle's center
144
* @param[in] nPoints Number of points of the circle (Polygon's shape will have noPoints+1 points), must be >=3
145
* @return the polygon approximating the circle
146
*/
147
static PositionVector makeCircle(const double radius, const Position& center, unsigned int nPoints);
148
149
/** Creates a circular polygon
150
* @param[in] radius1 Inner radius of the ring
151
* @param[in] radius2 Outer radius of the ring
152
* @param[in] center Position of the circle's center
153
* @param[in] nPoints Number of points of the circle (Polygon's shape will have noPoints+1 points), must be >=3
154
* @return the polygon approximating the circle
155
*/
156
static PositionVector makeRing(const double radius1, const double radius2, const Position& center, unsigned int nPoints);
157
158
/// @brief calculate lotSpace position
159
static const Position calculateLotSpacePosition(const PositionVector& shape, const int index,
160
const double spaceDim, const double angle, const double width, const double length);
161
162
/// @brief calculate lotSpace angle
163
static double calculateLotSpaceAngle(const PositionVector& shape, const int index, const double spaceDim, const double angle);
164
165
/// @brief calculate lotSpace slope
166
static double calculateLotSpaceSlope(const PositionVector& shape, const int index, const double spaceDim);
167
};
168
169