Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
Position.h
Go to the documentation of this file.
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/****************************************************************************/
21// A position in the 2D- or 3D-world
22/****************************************************************************/
23#pragma once
24#include <config.h>
25#include <iostream>
26#include <cmath>
27
28#include <config.h>
29
30// ===========================================================================
31// class definitions
32// ===========================================================================
37class Position {
38public:
41 myX(0.0), myY(0.0), myZ(0.0) { }
42
44 Position(double x, double y) :
45 myX(x), myY(y), myZ(0) { }
46
48 Position(double x, double y, double z) :
49 myX(x), myY(y), myZ(z) { }
50
52 inline double x() const {
53 return myX;
54 }
55
57 inline double y() const {
58 return myY;
59 }
60
62 inline double z() const {
63 return myZ;
64 }
65
67 void setx(double x) {
68 myX = x;
69 }
70
72 void sety(double y) {
73 myY = y;
74 }
75
77 void setz(double z) {
78 myZ = z;
79 }
80
82 void set(double x, double y) {
83 myX = x;
84 myY = y;
85 }
86
88 void set(double x, double y, double z) {
89 myX = x;
90 myY = y;
91 myZ = z;
92 }
93
95 void set(const Position& pos) {
96 myX = pos.myX;
97 myY = pos.myY;
98 myZ = pos.myZ;
99 }
100
102 void mul(double val) {
103 myX *= val;
104 myY *= val;
105 myZ *= val;
106 }
107
109 void div(double val) {
110 myX /= val;
111 myY /= val;
112 myZ /= val;
113 }
114
116 void mul(double mx, double my) {
117 myX *= mx;
118 myY *= my;
119 }
120
122 void mul(double mx, double my, double mz) {
123 myX *= mx;
124 myY *= my;
125 myZ *= mz;
126 }
127
129 void add(const Position& pos) {
130 myX += pos.myX;
131 myY += pos.myY;
132 myZ += pos.myZ;
133 }
134
136 void add(double dx, double dy) {
137 myX += dx;
138 myY += dy;
139 }
140
142 void add(double dx, double dy, double dz) {
143 myX += dx;
144 myY += dy;
145 myZ += dz;
146 }
147
149 void sub(double dx, double dy) {
150 myX -= dx;
151 myY -= dy;
152 }
153
155 void sub(double dx, double dy, double dz) {
156 myX -= dx;
157 myY -= dy;
158 myZ -= dz;
159 }
160
162 void sub(const Position& pos) {
163 myX -= pos.myX;
164 myY -= pos.myY;
165 myZ -= pos.myZ;
166 }
167
169 inline double length() const {
170 return sqrt(myX * myX + myY * myY + myZ * myZ);
171 }
172
174 inline double length2D() const {
175 return sqrt(myX * myX + myY * myY);
176 }
177
179 inline void norm2D() {
180 const double val = length2D();
181 if (val != 0.) {
182 myX /= val;
183 myY /= val;
184 }
185 }
186
188 friend std::ostream& operator<<(std::ostream& os, const Position& p) {
189 os << p.x() << "," << p.y();
190 if (p.z() != double(0.0)) {
191 os << "," << p.z();
192 }
193 return os;
194 }
195
197 Position operator+(const Position& p2) const {
198 return Position(myX + p2.myX, myY + p2.myY, myZ + p2.myZ);
199 }
200
202 Position operator-(const Position& p2) const {
203 return Position(myX - p2.myX, myY - p2.myY, myZ - p2.myZ);
204 }
205
207 Position operator*(double scalar) const {
208 return Position(myX * scalar, myY * scalar, myZ * scalar);
209 }
210
212 Position operator/(double scalar) const {
213 return Position(myX / scalar, myY / scalar, myZ / scalar);
214 }
215
217 Position operator+(double offset) const {
218 const double length = distanceTo(Position(0, 0, 0));
219 if (length == 0) {
220 return *this;
221 }
222 const double scalar = (length + offset) / length;
223 return Position(myX * scalar, myY * scalar, myZ * scalar);
224 }
225
227 Position operator-(double offset) const {
228 const double length = distanceTo(Position(0, 0, 0));
229 if (length == 0) {
230 return *this;
231 }
232 const double scalar = (length - offset) / length;
233 return Position(myX * scalar, myY * scalar, myZ * scalar);
234 }
235
237 bool operator==(const Position& p2) const {
238 return myX == p2.myX && myY == p2.myY && myZ == p2.myZ;
239 }
240
242 bool operator!=(const Position& p2) const {
243 return myX != p2.myX || myY != p2.myY || myZ != p2.myZ;
244 }
245
247 bool operator<(const Position& p2) const {
248 if (myX != p2.myX) {
249 return myX < p2.myX;
250 }
251 if (myY != p2.myY) {
252 return myY < p2.myY;
253 }
254 return myZ < p2.myZ;
255 }
256
258 bool almostSame(const Position& p2, double maxDiv = POSITION_EPS) const {
259 return distanceTo(p2) < maxDiv;
260 }
261
263 inline double distanceTo(const Position& p2) const {
264 return sqrt(distanceSquaredTo(p2));
265 }
266
268 inline double distanceSquaredTo(const Position& p2) const {
269 return (myX - p2.myX) * (myX - p2.myX) + (myY - p2.myY) * (myY - p2.myY) + (myZ - p2.myZ) * (myZ - p2.myZ);
270 }
271
273 inline double distanceTo2D(const Position& p2) const {
274 return sqrt(distanceSquaredTo2D(p2));
275 }
276
278 inline double distanceSquaredTo2D(const Position& p2) const {
279 return (myX - p2.myX) * (myX - p2.myX) + (myY - p2.myY) * (myY - p2.myY);
280 }
281
283 inline double angleTo2D(const Position& other) const {
284 return atan2(other.myY - myY, other.myX - myX);
285 }
286
288 inline double slopeTo2D(const Position& other) const {
289 return atan2(other.myZ - myZ, distanceTo2D(other));
290 }
291
294 return Position(
295 myY * pos.myZ - myZ * pos.myY,
296 myZ * pos.myX - myX * pos.myZ,
297 myX * pos.myY - myY * pos.myX);
298 }
299
301 inline double dotProduct(const Position& pos) const {
302 return myX * pos.myX + myY * pos.myY + myZ * pos.myZ;
303 }
304
306 Position rotateAround2D(double rad, const Position& origin);
307
309 void swapXY() {
310 std::swap(myX, myY);
311 }
312
314 bool isNAN() const {
315 return (std::isnan(myX) || std::isnan(myY) || std::isnan(myZ));
316 }
317
319 void round(int precision);
320
321
323 static const Position INVALID;
324
325private:
327 double myX;
328
330 double myY;
331
333 double myZ;
334};
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
Position(double x, double y)
Parametrised constructor (only for x-y)
Definition Position.h:44
Position(double x, double y, double z)
Parametrised constructor.
Definition Position.h:48
double length() const
Computes the length of the given vector.
Definition Position.h:169
void sub(const Position &pos)
Subtracts the given position from this one.
Definition Position.h:162
bool isNAN() const
check if position is NAN
Definition Position.h:314
double distanceSquaredTo2D(const Position &p2) const
returns the square of the distance to another position (Only using x and y positions)
Definition Position.h:278
void add(double dx, double dy)
Adds the given position to this one.
Definition Position.h:136
void add(double dx, double dy, double dz)
Adds the given position to this one.
Definition Position.h:142
Position()
default constructor
Definition Position.h:40
double slopeTo2D(const Position &other) const
returns the slope of the vector pointing from here to the other position (in radians between -M_PI an...
Definition Position.h:288
void setx(double x)
set position x
Definition Position.h:67
bool operator!=(const Position &p2) const
difference operator
Definition Position.h:242
Position operator-(const Position &p2) const
sub operator
Definition Position.h:202
double dotProduct(const Position &pos) const
returns the dot product (scalar product) between this point and the second one
Definition Position.h:301
void set(const Position &pos)
set position with another position
Definition Position.h:95
void set(double x, double y)
set positions x and y
Definition Position.h:82
static const Position INVALID
used to indicate that a position is valid
Definition Position.h:323
Position operator/(double scalar) const
keep the direction but modify the length of the (location) vector to length / scalar
Definition Position.h:212
double distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition Position.h:273
void norm2D()
Normalizes the given vector.
Definition Position.h:179
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimensions
Definition Position.h:263
void sub(double dx, double dy)
Subtracts the given position from this one.
Definition Position.h:149
double distanceSquaredTo(const Position &p2) const
returns the square of the distance to another position
Definition Position.h:268
void sub(double dx, double dy, double dz)
Subtracts the given position from this one.
Definition Position.h:155
Position operator+(double offset) const
keep the direction but modify the length of the (location) vector to length + scalar
Definition Position.h:217
void mul(double mx, double my)
Multiplies position with the given values.
Definition Position.h:116
friend std::ostream & operator<<(std::ostream &os, const Position &p)
output operator
Definition Position.h:188
double x() const
Returns the x-position.
Definition Position.h:52
double myZ
The z-position.
Definition Position.h:333
Position operator*(double scalar) const
keep the direction but modify the length of the (location) vector to length * scalar
Definition Position.h:207
void div(double val)
Divides position with the given value.
Definition Position.h:109
Position crossProduct(const Position &pos)
returns the cross product between this point and the second one
Definition Position.h:293
void swapXY()
swap position X and Y
Definition Position.h:309
void round(int precision)
round all coordinates to the given precision
Definition Position.cpp:53
Position operator-(double offset) const
keep the direction but modify the length of the (location) vector to length - scalar
Definition Position.h:227
void add(const Position &pos)
Adds the given position to this one.
Definition Position.h:129
bool operator==(const Position &p2) const
comparation operator
Definition Position.h:237
void set(double x, double y, double z)
set positions x, y and z
Definition Position.h:88
Position operator+(const Position &p2) const
add operator
Definition Position.h:197
void setz(double z)
set position z
Definition Position.h:77
double myY
The y-position.
Definition Position.h:330
bool operator<(const Position &p2) const
lexicographical sorting for use in maps and sets
Definition Position.h:247
double length2D() const
Computes the length of the given vector neglecting the z coordinate.
Definition Position.h:174
void mul(double val)
Multiplies position with the given value.
Definition Position.h:102
Position rotateAround2D(double rad, const Position &origin)
rotate this position by rad around origin and return the result
Definition Position.cpp:42
double z() const
Returns the z-position.
Definition Position.h:62
double angleTo2D(const Position &other) const
returns the angle in the plane of the vector pointing from here to the other position (in radians bet...
Definition Position.h:283
void sety(double y)
set position y
Definition Position.h:72
double myX
The x-position.
Definition Position.h:327
bool almostSame(const Position &p2, double maxDiv=POSITION_EPS) const
check whether the other position has a euclidean distance of less than maxDiv
Definition Position.h:258
void mul(double mx, double my, double mz)
Multiplies position with the given values.
Definition Position.h:122
double y() const
Returns the y-position.
Definition Position.h:57
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.hpp:21884