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.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 // ===========================================================================
37 class Position {
38 public:
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  ~Position() { }
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 length2D() const {
173  return sqrt(myX * myX + myY * myY);
174  }
175 
177  inline void norm2D() {
178  const double val = length2D();
179  if (val != 0.) {
180  myX /= val;
181  myY /= val;
182  }
183  }
184 
186  friend std::ostream& operator<<(std::ostream& os, const Position& p) {
187  os << p.x() << "," << p.y();
188  if (p.z() != double(0.0)) {
189  os << "," << p.z();
190  }
191  return os;
192  }
193 
195  Position operator+(const Position& p2) const {
196  return Position(myX + p2.myX, myY + p2.myY, myZ + p2.myZ);
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*(double scalar) const {
206  return Position(myX * scalar, myY * scalar, myZ * scalar);
207  }
208 
210  Position operator/(double scalar) const {
211  return Position(myX / scalar, myY / scalar, myZ / scalar);
212  }
213 
215  Position operator+(double offset) const {
216  const double length = distanceTo(Position(0, 0, 0));
217  if (length == 0) {
218  return *this;
219  }
220  const double scalar = (length + offset) / length;
221  return Position(myX * scalar, myY * scalar, myZ * scalar);
222  }
223 
225  Position operator-(double offset) const {
226  const double length = distanceTo(Position(0, 0, 0));
227  if (length == 0) {
228  return *this;
229  }
230  const double scalar = (length - offset) / length;
231  return Position(myX * scalar, myY * scalar, myZ * scalar);
232  }
233 
235  bool operator==(const Position& p2) const {
236  return myX == p2.myX && myY == p2.myY && myZ == p2.myZ;
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  if (myX < p2.myX) {
247  return true;
248  } else if (myY < p2.myY) {
249  return true;
250  } else {
251  return myZ < p2.myZ;
252  }
253  }
254 
256  bool almostSame(const Position& p2, double maxDiv = POSITION_EPS) const {
257  return distanceTo(p2) < maxDiv;
258  }
259 
261  inline double distanceTo(const Position& p2) const {
262  return sqrt(distanceSquaredTo(p2));
263  }
264 
266  inline double distanceSquaredTo(const Position& p2) const {
267  return (myX - p2.myX) * (myX - p2.myX) + (myY - p2.myY) * (myY - p2.myY) + (myZ - p2.myZ) * (myZ - p2.myZ);
268  }
269 
271  inline double distanceTo2D(const Position& p2) const {
272  return sqrt(distanceSquaredTo2D(p2));
273  }
274 
276  inline double distanceSquaredTo2D(const Position& p2) const {
277  return (myX - p2.myX) * (myX - p2.myX) + (myY - p2.myY) * (myY - p2.myY);
278  }
279 
281  inline double angleTo2D(const Position& other) const {
282  return atan2(other.myY - myY, other.myX - myX);
283  }
284 
286  inline double slopeTo2D(const Position& other) const {
287  return atan2(other.myZ - myZ, distanceTo2D(other));
288  }
289 
292  return Position(
293  myY * pos.myZ - myZ * pos.myY,
294  myZ * pos.myX - myX * pos.myZ,
295  myX * pos.myY - myY * pos.myX);
296  }
297 
299  inline double dotProduct(const Position& pos) const {
300  return myX * pos.myX + myY * pos.myY + myZ * pos.myZ;
301  }
302 
304  Position rotateAround2D(double rad, const Position& origin);
305 
307  void swapXY() {
308  std::swap(myX, myY);
309  }
310 
312  bool isNAN() const {
313  return (std::isnan(myX) || std::isnan(myY) || std::isnan(myZ));
314  }
315 
317  static const Position INVALID;
318 
319 private:
321  double myX;
322 
324  double myY;
325 
327  double myZ;
328 };
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)
Subtracts the given position from this one.
Definition: Position.h:165
bool isNAN() const
check if position is NAN
Definition: Position.h:312
double distanceSquaredTo2D(const Position &p2) const
returns the square of the distance to another position (Only using x and y positions)
Definition: Position.h:276
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:286
void setx(double x)
set position x
Definition: Position.h:70
bool operator!=(const Position &p2) const
difference operator
Definition: Position.h:240
Position operator-(const Position &p2) const
sub operator
Definition: Position.h:200
double dotProduct(const Position &pos) const
returns the dot product (scalar product) between this point and the second one
Definition: Position.h:299
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:317
Position operator/(double scalar) const
keep the direction but modify the length of the (location) vector to length / scalar
Definition: Position.h:210
double distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition: Position.h:271
void norm2D()
Normalizes the given vector.
Definition: Position.h:177
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:261
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:266
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:215
void mul(double mx, double my)
Multiplies position with the given values.
Definition: Position.h:119
double x() const
Returns the x-position.
Definition: Position.h:55
double myZ
The z-position.
Definition: Position.h:327
Position operator*(double scalar) const
keep the direction but modify the length of the (location) vector to length * scalar
Definition: Position.h:205
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:291
void swapXY()
swap position X and Y
Definition: Position.h:307
friend std::ostream & operator<<(std::ostream &os, const Position &p)
output operator
Definition: Position.h:186
Position operator-(double offset) const
keep the direction but modify the length of the (location) vector to length - scalar
Definition: Position.h:225
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:235
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:195
void setz(double z)
set position z
Definition: Position.h:80
double myY
The y-position.
Definition: Position.h:324
~Position()
Destructor.
Definition: Position.h:52
bool operator<(const Position &p2) const
lexicographical sorting for use in maps and sets
Definition: Position.h:245
double length2D() const
Computes the length of the given vector.
Definition: Position.h:172
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:281
void sety(double y)
set position y
Definition: Position.h:75
double myX
The x-position.
Definition: Position.h:321
bool almostSame(const Position &p2, double maxDiv=POSITION_EPS) const
check if two position is almost the sme as other
Definition: Position.h:256
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