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-2024 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
53
55 inline double x() const {
56 return myX;
57 }
58
60 inline double y() const {
61 return myY;
62 }
63
65 inline double z() const {
66 return myZ;
67 }
68
70 void setx(double x) {
71 myX = x;
72 }
73
75 void sety(double y) {
76 myY = y;
77 }
78
80 void setz(double z) {
81 myZ = z;
82 }
83
85 void set(double x, double y) {
86 myX = x;
87 myY = y;
88 }
89
91 void set(double x, double y, double z) {
92 myX = x;
93 myY = y;
94 myZ = z;
95 }
96
98 void set(const Position& pos) {
99 myX = pos.myX;
100 myY = pos.myY;
101 myZ = pos.myZ;
102 }
103
105 void mul(double val) {
106 myX *= val;
107 myY *= val;
108 myZ *= val;
109 }
110
112 void div(double val) {
113 myX /= val;
114 myY /= val;
115 myZ /= val;
116 }
117
119 void mul(double mx, double my) {
120 myX *= mx;
121 myY *= my;
122 }
123
125 void mul(double mx, double my, double mz) {
126 myX *= mx;
127 myY *= my;
128 myZ *= mz;
129 }
130
132 void add(const Position& pos) {
133 myX += pos.myX;
134 myY += pos.myY;
135 myZ += pos.myZ;
136 }
137
139 void add(double dx, double dy) {
140 myX += dx;
141 myY += dy;
142 }
143
145 void add(double dx, double dy, double dz) {
146 myX += dx;
147 myY += dy;
148 myZ += dz;
149 }
150
152 void sub(double dx, double dy) {
153 myX -= dx;
154 myY -= dy;
155 }
156
158 void sub(double dx, double dy, double dz) {
159 myX -= dx;
160 myY -= dy;
161 myZ -= dz;
162 }
163
165 void sub(const Position& pos) {
166 myX -= pos.myX;
167 myY -= pos.myY;
168 myZ -= pos.myZ;
169 }
170
172 inline double length() const {
173 return sqrt(myX * myX + myY * myY + myZ * myZ);
174 }
175
177 inline double length2D() const {
178 return sqrt(myX * myX + myY * myY);
179 }
180
182 inline void norm2D() {
183 const double val = length2D();
184 if (val != 0.) {
185 myX /= val;
186 myY /= val;
187 }
188 }
189
191 friend std::ostream& operator<<(std::ostream& os, const Position& p) {
192 os << p.x() << "," << p.y();
193 if (p.z() != double(0.0)) {
194 os << "," << p.z();
195 }
196 return os;
197 }
198
200 Position operator+(const Position& p2) const {
201 return Position(myX + p2.myX, myY + p2.myY, myZ + p2.myZ);
202 }
203
205 Position operator-(const Position& p2) const {
206 return Position(myX - p2.myX, myY - p2.myY, myZ - p2.myZ);
207 }
208
210 Position operator*(double scalar) const {
211 return Position(myX * scalar, myY * scalar, myZ * scalar);
212 }
213
215 Position operator/(double scalar) const {
216 return Position(myX / scalar, myY / scalar, myZ / scalar);
217 }
218
220 Position operator+(double offset) const {
221 const double length = distanceTo(Position(0, 0, 0));
222 if (length == 0) {
223 return *this;
224 }
225 const double scalar = (length + offset) / length;
226 return Position(myX * scalar, myY * scalar, myZ * scalar);
227 }
228
230 Position operator-(double offset) const {
231 const double length = distanceTo(Position(0, 0, 0));
232 if (length == 0) {
233 return *this;
234 }
235 const double scalar = (length - offset) / length;
236 return Position(myX * scalar, myY * scalar, myZ * scalar);
237 }
238
240 bool operator==(const Position& p2) const {
241 return myX == p2.myX && myY == p2.myY && myZ == p2.myZ;
242 }
243
245 bool operator!=(const Position& p2) const {
246 return myX != p2.myX || myY != p2.myY || myZ != p2.myZ;
247 }
248
250 bool operator<(const Position& p2) const {
251 if (myX != p2.myX) {
252 return myX < p2.myX;
253 }
254 if (myY != p2.myY) {
255 return myY < p2.myY;
256 }
257 return myZ < p2.myZ;
258 }
259
261 bool almostSame(const Position& p2, double maxDiv = POSITION_EPS) const {
262 return distanceTo(p2) < maxDiv;
263 }
264
266 inline double distanceTo(const Position& p2) const {
267 return sqrt(distanceSquaredTo(p2));
268 }
269
271 inline double distanceSquaredTo(const Position& p2) const {
272 return (myX - p2.myX) * (myX - p2.myX) + (myY - p2.myY) * (myY - p2.myY) + (myZ - p2.myZ) * (myZ - p2.myZ);
273 }
274
276 inline double distanceTo2D(const Position& p2) const {
277 return sqrt(distanceSquaredTo2D(p2));
278 }
279
281 inline double distanceSquaredTo2D(const Position& p2) const {
282 return (myX - p2.myX) * (myX - p2.myX) + (myY - p2.myY) * (myY - p2.myY);
283 }
284
286 inline double angleTo2D(const Position& other) const {
287 return atan2(other.myY - myY, other.myX - myX);
288 }
289
291 inline double slopeTo2D(const Position& other) const {
292 return atan2(other.myZ - myZ, distanceTo2D(other));
293 }
294
297 return Position(
298 myY * pos.myZ - myZ * pos.myY,
299 myZ * pos.myX - myX * pos.myZ,
300 myX * pos.myY - myY * pos.myX);
301 }
302
304 inline double dotProduct(const Position& pos) const {
305 return myX * pos.myX + myY * pos.myY + myZ * pos.myZ;
306 }
307
309 Position rotateAround2D(double rad, const Position& origin);
310
312 void swapXY() {
313 std::swap(myX, myY);
314 }
315
317 bool isNAN() const {
318 return (std::isnan(myX) || std::isnan(myY) || std::isnan(myZ));
319 }
320
322 static const Position INVALID;
323
324private:
326 double myX;
327
329 double myY;
330
332 double myZ;
333};
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:172
void sub(const Position &pos)
Subtracts the given position from this one.
Definition Position.h:165
bool isNAN() const
check if position is NAN
Definition Position.h:317
double distanceSquaredTo2D(const Position &p2) const
returns the square of the distance to another position (Only using x and y positions)
Definition Position.h:281
void add(double dx, double dy)
Adds the given position to this one.
Definition Position.h:139
void add(double dx, double dy, double dz)
Adds the given position to this one.
Definition Position.h:145
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:291
void setx(double x)
set position x
Definition Position.h:70
bool operator!=(const Position &p2) const
difference operator
Definition Position.h:245
Position operator-(const Position &p2) const
sub operator
Definition Position.h:205
double dotProduct(const Position &pos) const
returns the dot product (scalar product) between this point and the second one
Definition Position.h:304
void set(const Position &pos)
set position with another position
Definition Position.h:98
void set(double x, double y)
set positions x and y
Definition Position.h:85
static const Position INVALID
used to indicate that a position is valid
Definition Position.h:322
Position operator/(double scalar) const
keep the direction but modify the length of the (location) vector to length / scalar
Definition Position.h:215
double distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition Position.h:276
void norm2D()
Normalizes the given vector.
Definition Position.h:182
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimensions
Definition Position.h:266
void sub(double dx, double dy)
Subtracts the given position from this one.
Definition Position.h:152
double distanceSquaredTo(const Position &p2) const
returns the square of the distance to another position
Definition Position.h:271
void sub(double dx, double dy, double dz)
Subtracts the given position from this one.
Definition Position.h:158
Position operator+(double offset) const
keep the direction but modify the length of the (location) vector to length + scalar
Definition Position.h:220
void mul(double mx, double my)
Multiplies position with the given values.
Definition Position.h:119
friend std::ostream & operator<<(std::ostream &os, const Position &p)
output operator
Definition Position.h:191
double x() const
Returns the x-position.
Definition Position.h:55
double myZ
The z-position.
Definition Position.h:332
Position operator*(double scalar) const
keep the direction but modify the length of the (location) vector to length * scalar
Definition Position.h:210
void div(double val)
Divides position with the given value.
Definition Position.h:112
Position crossProduct(const Position &pos)
returns the cross product between this point and the second one
Definition Position.h:296
void swapXY()
swap position X and Y
Definition Position.h:312
Position operator-(double offset) const
keep the direction but modify the length of the (location) vector to length - scalar
Definition Position.h:230
void add(const Position &pos)
Adds the given position to this one.
Definition Position.h:132
bool operator==(const Position &p2) const
comparation operator
Definition Position.h:240
void set(double x, double y, double z)
set positions x, y and z
Definition Position.h:91
Position operator+(const Position &p2) const
add operator
Definition Position.h:200
void setz(double z)
set position z
Definition Position.h:80
double myY
The y-position.
Definition Position.h:329
~Position()
Destructor.
Definition Position.h:52
bool operator<(const Position &p2) const
lexicographical sorting for use in maps and sets
Definition Position.h:250
double length2D() const
Computes the length of the given vector neglecting the z coordinate.
Definition Position.h:177
void mul(double val)
Multiplies position with the given value.
Definition Position.h:105
Position rotateAround2D(double rad, const Position &origin)
rotate this position by rad around origin and return the result
Definition Position.cpp:41
double z() const
Returns the z-position.
Definition Position.h:65
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:286
void sety(double y)
set position y
Definition Position.h:75
double myX
The x-position.
Definition Position.h:326
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:261
void mul(double mx, double my, double mz)
Multiplies position with the given values.
Definition Position.h:125
double y() const
Returns the y-position.
Definition Position.h:60
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