SUMO - Simulation of Urban MObility
Position.h
Go to the documentation of this file.
1 /****************************************************************************/
10 // A position in the 2D- or 3D-world
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 #ifndef Position_h
24 #define Position_h
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #include <iostream>
31 #include <cmath>
32 
33 #ifdef _MSC_VER
34 #include <windows_config.h>
35 #else
36 #include <config.h>
37 #endif
38 
39 // ===========================================================================
40 // class definitions
41 // ===========================================================================
46 class Position {
47 public:
49  Position() : myX(0.0), myY(0.0), myZ(0.0) { }
50 
53  : myX(x), myY(y), myZ(0) { }
54 
57  : myX(x), myY(y), myZ(z) { }
58 
60  ~Position() { }
61 
63  SUMOReal x() const {
64  return myX;
65  }
66 
68  SUMOReal y() const {
69  return myY;
70  }
71 
73  SUMOReal z() const {
74  return myZ;
75  }
76 
78  void set(SUMOReal x, SUMOReal y) {
79  myX = x;
80  myY = y;
81  }
82 
84  void set(SUMOReal x, SUMOReal y, SUMOReal z) {
85  myX = x;
86  myY = y;
87  myZ = z;
88  }
89 
91  void set(const Position& pos) {
92  myX = pos.myX;
93  myY = pos.myY;
94  myZ = pos.myZ;
95  }
96 
97 
99  void mul(SUMOReal val) {
100  myX *= val;
101  myY *= val;
102  myZ *= val;
103  }
104 
106  void mul(SUMOReal mx, SUMOReal my) {
107  myX *= mx;
108  myY *= my;
109  }
110 
112  void mul(SUMOReal mx, SUMOReal my, SUMOReal mz) {
113  myX *= mx;
114  myY *= my;
115  myZ *= mz;
116  }
117 
119  void add(const Position& pos) {
120  myX += pos.myX;
121  myY += pos.myY;
122  myZ += pos.myZ;
123  }
124 
126  void add(SUMOReal dx, SUMOReal dy) {
127  myX += dx;
128  myY += dy;
129  }
130 
132  void add(SUMOReal dx, SUMOReal dy, SUMOReal dz) {
133  myX += dx;
134  myY += dy;
135  myZ += dz;
136  }
137 
139  void sub(SUMOReal dx, SUMOReal dy) {
140  myX -= dx;
141  myY -= dy;
142  }
143 
145  void sub(SUMOReal dx, SUMOReal dy, SUMOReal dz) {
146  myX -= dx;
147  myY -= dy;
148  myZ -= dz;
149  }
150 
152  void sub(const Position& pos) {
153  myX -= pos.myX;
154  myY -= pos.myY;
155  myZ -= pos.myZ;
156  }
157 
158  void norm2d() {
159  SUMOReal val = sqrt(myX * myX + myY * myY);
160  myX = myX / val;
161  myY = myY / val;
162  }
163 
165  friend std::ostream& operator<<(std::ostream& os, const Position& p) {
166  os << p.x() << "," << p.y();
167  if (p.z() != SUMOReal(0.0)) {
168  os << "," << p.z();
169  }
170  return os;
171  }
172 
173  Position operator+(const Position& p2) const {
174  return Position(myX + p2.myX, myY + p2.myY, myZ + p2.myZ);
175  }
176 
177  Position operator-(const Position& p2) const {
178  return Position(myX - p2.myX, myY - p2.myY, myZ - p2.myZ);
179  }
180 
182  Position operator*(SUMOReal scalar) const {
183  return Position(myX * scalar, myY * scalar, myZ * scalar);
184  }
185 
187  Position operator+(SUMOReal offset) const {
188  const SUMOReal length = distanceTo(Position(0, 0, 0));
189  if (length == 0) {
190  return *this;
191  }
192  const SUMOReal scalar = (length + offset) / length;
193  return Position(myX * scalar, myY * scalar, myZ * scalar);
194  }
195 
196  bool operator==(const Position& p2) const {
197  return myX == p2.myX && myY == p2.myY && myZ == p2.myZ;
198  }
199 
200  bool operator!=(const Position& p2) const {
201  return myX != p2.myX || myY != p2.myY || myZ != p2.myZ;
202  }
203 
205  bool operator<(const Position& p2) const {
206  if (myX < p2.myX) {
207  return true;
208  } else if (myY < p2.myY) {
209  return true;
210  } else {
211  return myZ < p2.myZ;
212  }
213  }
214 
215  bool almostSame(const Position& p2, SUMOReal maxDiv = POSITION_EPS) const {
216  return distanceTo(p2) < maxDiv;
217  }
218 
219 
221  inline SUMOReal distanceTo(const Position& p2) const {
222  return sqrt(distanceSquaredTo(p2));
223  }
224 
225 
226  inline SUMOReal distanceSquaredTo(const Position& p2) const {
227  return (myX - p2.myX) * (myX - p2.myX) + (myY - p2.myY) * (myY - p2.myY) + (myZ - p2.myZ) * (myZ - p2.myZ);
228  }
229 
230 
232  inline SUMOReal distanceTo2D(const Position& p2) const {
233  return sqrt(distanceSquaredTo2D(p2));
234  }
235 
236 
237  inline SUMOReal distanceSquaredTo2D(const Position& p2) const {
238  return (myX - p2.myX) * (myX - p2.myX) + (myY - p2.myY) * (myY - p2.myY);
239  }
240 
241 
243  inline SUMOReal angleTo2D(const Position& other) const {
244  return atan2(other.myY - myY, other.myX - myX);
245  }
246 
247 
250  return Position(
251  myY * pos.myZ - myZ * pos.myY,
252  myZ * pos.myX - myX * pos.myZ,
253  myX * pos.myY - myY * pos.myX);
254  }
255 
257  inline SUMOReal dotProduct(const Position& pos) {
258  return myX * pos.myX + myY * pos.myY + myZ * pos.myZ;
259  }
260 
261  static const Position INVALID;
262 
263 private:
266 
269 
272 
273 };
274 
275 
276 #endif
277 
278 /****************************************************************************/
279 
void sub(SUMOReal dx, SUMOReal dy)
Substracts the given position from this one.
Definition: Position.h:139
void add(SUMOReal dx, SUMOReal dy)
Adds the given position to this one.
Definition: Position.h:126
Position(SUMOReal x, SUMOReal y, SUMOReal z)
parametrised constructor
Definition: Position.h:56
bool operator==(const Position &p2) const
Definition: Position.h:196
void add(SUMOReal dx, SUMOReal dy, SUMOReal dz)
Adds the given position to this one.
Definition: Position.h:132
SUMOReal myZ
The z-position.
Definition: Position.h:271
Position operator-(const Position &p2) const
Definition: Position.h:177
void add(const Position &pos)
Adds the given position to this one.
Definition: Position.h:119
void norm2d()
Definition: Position.h:158
bool operator<(const Position &p2) const
lexicographical sorting for use in maps and sets
Definition: Position.h:205
SUMOReal distanceSquaredTo(const Position &p2) const
Definition: Position.h:226
bool operator!=(const Position &p2) const
Definition: Position.h:200
~Position()
Destructor.
Definition: Position.h:60
void mul(SUMOReal mx, SUMOReal my, SUMOReal mz)
Multiplies position with the given values.
Definition: Position.h:112
SUMOReal distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:221
void sub(SUMOReal dx, SUMOReal dy, SUMOReal dz)
Substracts the given position from this one.
Definition: Position.h:145
bool almostSame(const Position &p2, SUMOReal maxDiv=POSITION_EPS) const
Definition: Position.h:215
SUMOReal distanceSquaredTo2D(const Position &p2) const
Definition: Position.h:237
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
Position crossProduct(const Position &pos)
returns the cross product between this point and the second one
Definition: Position.h:249
void sub(const Position &pos)
Substracts the given position from this one.
Definition: Position.h:152
void mul(SUMOReal mx, SUMOReal my)
Multiplies position with the given values.
Definition: Position.h:106
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
SUMOReal z() const
Returns the z-position.
Definition: Position.h:73
SUMOReal myX
The x-position.
Definition: Position.h:265
#define POSITION_EPS
Definition: config.h:187
Position operator+(const Position &p2) const
Definition: Position.h:173
friend std::ostream & operator<<(std::ostream &os, const Position &p)
Prints to the output.
Definition: Position.h:165
SUMOReal dotProduct(const Position &pos)
returns the dot product (scalar product) between this point and the second one
Definition: Position.h:257
Position operator*(SUMOReal scalar) const
keep the direction but modify the length of the (location) vector to length * scalar ...
Definition: Position.h:182
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
void mul(SUMOReal val)
Multiplies both positions with the given value.
Definition: Position.h:99
SUMOReal myY
The y-position.
Definition: Position.h:268
SUMOReal distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition: Position.h:232
#define SUMOReal
Definition: config.h:213
Position(SUMOReal x, SUMOReal y)
parametrised constructor
Definition: Position.h:52
Position operator+(SUMOReal offset) const
keep the direction but modify the length of the (location) vector to length + scalar ...
Definition: Position.h:187
Position()
default constructor
Definition: Position.h:49
SUMOReal angleTo2D(const Position &other) const
returns the angle in the plane of the vector pointing from here to the other position ...
Definition: Position.h:243
static const Position INVALID
Definition: Position.h:261