Eclipse SUMO - Simulation of Urban MObility
Position.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2001-2022 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 mul(double mx, double my) {
113 myX *= mx;
114 myY *= my;
115 }
116
118 void mul(double mx, double my, double mz) {
119 myX *= mx;
120 myY *= my;
121 myZ *= mz;
122 }
123
125 void add(const Position& pos) {
126 myX += pos.myX;
127 myY += pos.myY;
128 myZ += pos.myZ;
129 }
130
132 void add(double dx, double dy) {
133 myX += dx;
134 myY += dy;
135 }
136
138 void add(double dx, double dy, double dz) {
139 myX += dx;
140 myY += dy;
141 myZ += dz;
142 }
143
145 void sub(double dx, double dy) {
146 myX -= dx;
147 myY -= dy;
148 }
149
151 void sub(double dx, double dy, double dz) {
152 myX -= dx;
153 myY -= dy;
154 myZ -= dz;
155 }
156
158 void sub(const Position& pos) {
159 myX -= pos.myX;
160 myY -= pos.myY;
161 myZ -= pos.myZ;
162 }
163
165 void norm2d() {
166 const double val = sqrt(myX * myX + myY * myY);
167 myX /= val;
168 myY /= val;
169 }
170
172 friend std::ostream& operator<<(std::ostream& os, const Position& p) {
173 os << p.x() << "," << p.y();
174 if (p.z() != double(0.0)) {
175 os << "," << p.z();
176 }
177 return os;
178 }
179
181 Position operator+(const Position& p2) const {
182 return Position(myX + p2.myX, myY + p2.myY, myZ + p2.myZ);
183 }
184
186 Position operator-(const Position& p2) const {
187 return Position(myX - p2.myX, myY - p2.myY, myZ - p2.myZ);
188 }
189
191 Position operator*(double scalar) const {
192 return Position(myX * scalar, myY * scalar, myZ * scalar);
193 }
194
196 Position operator+(double offset) const {
197 const double length = distanceTo(Position(0, 0, 0));
198 if (length == 0) {
199 return *this;
200 }
201 const double scalar = (length + offset) / length;
202 return Position(myX * scalar, myY * scalar, myZ * scalar);
203 }
204
206 Position operator-(double offset) const {
207 const double length = distanceTo(Position(0, 0, 0));
208 if (length == 0) {
209 return *this;
210 }
211 const double scalar = (length - offset) / length;
212 return Position(myX * scalar, myY * scalar, myZ * scalar);
213 }
214
216 bool operator==(const Position& p2) const {
217 return myX == p2.myX && myY == p2.myY && myZ == p2.myZ;
218 }
219
221 bool operator!=(const Position& p2) const {
222 return myX != p2.myX || myY != p2.myY || myZ != p2.myZ;
223 }
224
226 bool operator<(const Position& p2) const {
227 if (myX < p2.myX) {
228 return true;
229 } else if (myY < p2.myY) {
230 return true;
231 } else {
232 return myZ < p2.myZ;
233 }
234 }
235
237 bool almostSame(const Position& p2, double maxDiv = POSITION_EPS) const {
238 return distanceTo(p2) < maxDiv;
239 }
240
242 inline double distanceTo(const Position& p2) const {
243 return sqrt(distanceSquaredTo(p2));
244 }
245
247 inline double distanceSquaredTo(const Position& p2) const {
248 return (myX - p2.myX) * (myX - p2.myX) + (myY - p2.myY) * (myY - p2.myY) + (myZ - p2.myZ) * (myZ - p2.myZ);
249 }
250
252 inline double distanceTo2D(const Position& p2) const {
253 return sqrt(distanceSquaredTo2D(p2));
254 }
255
257 inline double distanceSquaredTo2D(const Position& p2) const {
258 return (myX - p2.myX) * (myX - p2.myX) + (myY - p2.myY) * (myY - p2.myY);
259 }
260
262 inline double angleTo2D(const Position& other) const {
263 return atan2(other.myY - myY, other.myX - myX);
264 }
265
267 inline double slopeTo2D(const Position& other) const {
268 return atan2(other.myZ - myZ, distanceTo2D(other));
269 }
270
273 return Position(
274 myY * pos.myZ - myZ * pos.myY,
275 myZ * pos.myX - myX * pos.myZ,
276 myX * pos.myY - myY * pos.myX);
277 }
278
280 inline double dotProduct(const Position& pos) {
281 return myX * pos.myX + myY * pos.myY + myZ * pos.myZ;
282 }
283
285 Position rotateAround2D(double rad, const Position& origin);
286
288 void swapXY() {
289 std::swap(myX, myY);
290 }
291
293 bool isNAN() const {
294 return (std::isnan(myX) || std::isnan(myY) || std::isnan(myZ));
295 }
296
298 static const Position INVALID;
299
300private:
302 double myX;
303
305 double myY;
306
308 double myZ;
309};
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
void sub(const Position &pos)
Substracts the given position from this one.
Definition: Position.h:158
bool isNAN() const
check if position is NAN
Definition: Position.h:293
double distanceSquaredTo2D(const Position &p2) const
returns the square of the distance to another position (Only using x and y positions)
Definition: Position.h:257
void add(double dx, double dy)
Adds the given position to this one.
Definition: Position.h:132
void add(double dx, double dy, double dz)
Adds the given position to this one.
Definition: Position.h:138
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
Definition: Position.h:267
void setx(double x)
set position x
Definition: Position.h:70
bool operator!=(const Position &p2) const
difference operator
Definition: Position.h:221
void norm2d()
Definition: Position.h:165
Position operator-(const Position &p2) const
sub operator
Definition: Position.h:186
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
double dotProduct(const Position &pos)
returns the dot product (scalar product) between this point and the second one
Definition: Position.h:280
static const Position INVALID
used to indicate that a position is valid
Definition: Position.h:298
double distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition: Position.h:252
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:242
void sub(double dx, double dy)
Substracts the given position from this one.
Definition: Position.h:145
double distanceSquaredTo(const Position &p2) const
returns the square of the distance to another position
Definition: Position.h:247
void sub(double dx, double dy, double dz)
Substracts the given position from this one.
Definition: Position.h:151
Position operator+(double offset) const
keep the direction but modify the length of the (location) vector to length + scalar
Definition: Position.h:196
void mul(double mx, double my)
Multiplies position with the given values.
Definition: Position.h:112
friend std::ostream & operator<<(std::ostream &os, const Position &p)
output operator
Definition: Position.h:172
double x() const
Returns the x-position.
Definition: Position.h:55
double myZ
The z-position.
Definition: Position.h:308
Position operator*(double scalar) const
keep the direction but modify the length of the (location) vector to length * scalar
Definition: Position.h:191
Position crossProduct(const Position &pos)
returns the cross product between this point and the second one
Definition: Position.h:272
void swapXY()
swap position X and Y
Definition: Position.h:288
Position operator-(double offset) const
keep the direction but modify the length of the (location) vector to length - scalar
Definition: Position.h:206
void add(const Position &pos)
Adds the given position to this one.
Definition: Position.h:125
bool operator==(const Position &p2) const
comparation operator
Definition: Position.h:216
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:181
void setz(double z)
set position z
Definition: Position.h:80
double myY
The y-position.
Definition: Position.h:305
~Position()
Destructor.
Definition: Position.h:52
bool operator<(const Position &p2) const
lexicographical sorting for use in maps and sets
Definition: Position.h:226
void mul(double val)
Multiplies both positions 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
Definition: Position.h:262
void sety(double y)
set position y
Definition: Position.h:75
double myX
The x-position.
Definition: Position.h:302
bool almostSame(const Position &p2, double maxDiv=POSITION_EPS) const
check if two position is almost the sme as other
Definition: Position.h:237
void mul(double mx, double my, double mz)
Multiplies position with the given values.
Definition: Position.h:118
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