Eclipse SUMO - Simulation of Urban MObility
PositionVector.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 /****************************************************************************/
20 // A list of positions
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
25 #include <vector>
26 #include <limits>
27 #include "AbstractPoly.h"
28 
29 
30 // ===========================================================================
31 // class declarations
32 // ===========================================================================
33 
34 class Boundary;
35 
36 // ===========================================================================
37 // class definitions
38 // ===========================================================================
43 class PositionVector : public AbstractPoly, private std::vector<Position> {
44 
45 private:
47  typedef std::vector<Position> vp;
48 
49 public:
52 
55  PositionVector(const std::vector<Position>& v);
56 
60  PositionVector(const std::vector<Position>::const_iterator beg, const std::vector<Position>::const_iterator end);
61 
65  PositionVector(const Position& p1, const Position& p2);
66 
69 
71  static const PositionVector EMPTY;
72 
76  using vp::iterator;
77 
79  using vp::const_iterator;
80 
82  using vp::const_reference;
83 
85  using vp::value_type;
86 
88  using vp::begin;
89 
91  using vp::end;
92 
94  using vp::push_back;
95 
97  using vp::pop_back;
98 
100  using vp::clear;
102  using vp::size;
103 
105  using vp::front;
106 
108  using vp::back;
109 
111  using vp::reference;
112 
114  using vp::erase;
115 
117  using vp::insert;
119 
122  bool around(const Position& p, double offset = 0) const;
123 
126  bool overlapsWith(const AbstractPoly& poly, double offset = 0) const;
127 
129  double getOverlapWith(const PositionVector& poly, double zThreshold) const;
130 
132  bool intersects(const Position& p1, const Position& p2) const;
133 
135  bool intersects(const PositionVector& v1) const;
136 
138  Position intersectionPosition2D(const Position& p1, const Position& p2, const double withinDist = 0.) const;
139 
141  std::vector<double> intersectsAtLengths2D(const PositionVector& other) const;
142 
144  std::vector<double> intersectsAtLengths2D(const Position& lp1, const Position& lp2) const;
145 
148 
150  void closePolygon();
151 
154  const Position& operator[](int index) const;
155 
157  Position& operator[](int index);
158 
160  Position positionAtOffset(double pos, double lateralOffset = 0) const;
161 
163  Position positionAtOffset2D(double pos, double lateralOffset = 0) const;
164 
166  double rotationAtOffset(double pos) const;
167 
169  double rotationDegreeAtOffset(double pos) const;
170 
172  double slopeDegreeAtOffset(double pos) const;
173 
175  static Position positionAtOffset(const Position& p1, const Position& p2, double pos, double lateralOffset = 0.);
176 
178  static Position positionAtOffset2D(const Position& p1, const Position& p2, double pos, double lateralOffset = 0.);
179 
181  Boundary getBoxBoundary() const;
182 
185  Position getPolygonCenter() const;
186 
188  Position getCentroid() const;
189 
191  void scaleRelative(double factor);
192 
194  void scaleAbsolute(double offset);
195 
197  Position getLineCenter() const;
198 
200  double length() const;
201 
203  double length2D() const;
204 
206  double area() const;
207 
209  bool partialWithin(const AbstractPoly& poly, double offset = 0) const;
210 
212  std::pair<PositionVector, PositionVector> splitAt(double where, bool use2D = false) const;
213 
215  friend std::ostream& operator<<(std::ostream& os, const PositionVector& geom);
216 
218  bool crosses(const Position& p1, const Position& p2) const;
219 
221  void add(double xoff, double yoff, double zoff);
222 
224  void add(const Position& offset);
225 
227  void sub(const Position& offset);
228 
230  PositionVector added(const Position& offset) const;
231 
233  void mirrorX();
234 
236  void rotate2D(double angle);
237 
239  void append(const PositionVector& v, double sameThreshold = 2.0);
240 
242  void prepend(const PositionVector& v, double sameThreshold = 2.0);
243 
245  PositionVector getSubpart(double beginOffset, double endOffset) const;
246 
248  PositionVector getSubpart2D(double beginOffset, double endOffset) const;
249 
251  PositionVector getSubpartByIndex(int beginIndex, int count) const;
252 
254  void sortAsPolyCWByAngle();
255 
257  void sortByIncreasingXY();
258 
260  void extrapolate(const double val, const bool onlyFirst = false, const bool onlyLast = false);
261 
263  void extrapolate2D(const double val, const bool onlyFirst = false);
264 
266  PositionVector reverse() const;
267 
269  static Position sideOffset(const Position& beg, const Position& end, const double amount);
270 
272  void move2side(double amount, double maxExtension = 100);
273 
275  void move2sideCustom(std::vector<double> amount, double maxExtension = 100);
276 
278  double angleAt2D(int pos) const;
279 
285  int insertAtClosest(const Position& p, bool interpolateZ);
286 
288  int removeClosest(const Position& p);
289 
291  bool operator==(const PositionVector& v2) const;
292 
294  bool operator!=(const PositionVector& v2) const;
295 
297  PositionVector operator-(const PositionVector& v2) const;
298 
300  PositionVector operator+(const PositionVector& v2) const;
301 
304  public:
307 
309  int operator()(const Position& p1, const Position& p2) const;
310  };
311 
314  public:
316  explicit increasing_x_y_sorter();
317 
319  int operator()(const Position& p1, const Position& p2) const;
320  };
321 
324  double isLeft(const Position& P0, const Position& P1, const Position& P2) const;
325 
327  double beginEndAngle() const;
328 
330  double nearest_offset_to_point2D(const Position& p, bool perpendicular = true) const;
331 
333  double nearest_offset_to_point25D(const Position& p, bool perpendicular = true) const;
334 
342  Position transformToVectorCoordinates(const Position& p, bool extend = false) const;
343 
344  /* @brief index of the closest position to p
345  * @in twoD whether all positions should be projected onto the plan
346  @note: may only be called for a non-empty vector
347  */
348  int indexOfClosest(const Position& p, bool twoD = false) const;
349 
352  std::vector<double> distances(const PositionVector& s, bool perpendicular = false) const;
353 
355  double distance2D(const Position& p, bool perpendicular = false) const;
356 
358  void push_front(const Position& p);
359 
361  void pop_front();
362 
364  void push_back_noDoublePos(const Position& p);
365 
367  void push_front_noDoublePos(const Position& p);
368 
370  void insert_noDoublePos(const std::vector<Position>::iterator& at, const Position& p);
371 
373  bool isClosed() const;
374 
376  bool isNAN() const;
377 
382  void removeDoublePoints(double minDist = POSITION_EPS, bool assertLength = false, int beginOffset = 0, int endOffset = 0, bool resample = false);
383 
385  bool hasElevation() const;
386 
388  PositionVector simplified() const;
389 
397  PositionVector getOrthogonal(const Position& p, double extend, bool before, double length = 1.0, double deg = 90) const;
398 
400  PositionVector smoothedZFront(double dist = std::numeric_limits<double>::max()) const;
401 
403  PositionVector interpolateZ(double zStart, double zEnd) const;
404 
409  PositionVector resample(double maxLength, const bool adjustEnd) const;
410 
412  double offsetAtIndex2D(int index) const;
413 
414  /* @brief return the maximum grade of all segments as a fraction of zRange/length2D
415  * @param[out] maxJump The maximum vertical jump (with grade infinity)
416  */
417  double getMaxGrade(double& maxJump) const;
418 
420  PositionVector bezier(int numPoints);
421 
422  static double localAngle(const Position& from, const Position& pos, const Position& to);
423 
424 private:
426  static bool intersects(const Position& p11, const Position& p12, const Position& p21, const Position& p22, const double withinDist = 0., double* x = 0, double* y = 0, double* mu = 0);
427 };
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
int operator()(const Position &p1, const Position &p2) const
comparing operation for sort
clase for increasing Sorter
int operator()(const Position &p1, const Position &p2) const
comparing operation
A list of positions.
PositionVector operator-(const PositionVector &v2) const
substracts two vectors (requires vectors of the same length)
void scaleAbsolute(double offset)
enlarges/shrinks the polygon by an absolute offset based at the centroid
double length2D() const
Returns the length.
void append(const PositionVector &v, double sameThreshold=2.0)
bool overlapsWith(const AbstractPoly &poly, double offset=0) const
Returns the information whether the given polygon overlaps with this.
PositionVector added(const Position &offset) const
double isLeft(const Position &P0, const Position &P1, const Position &P2) const
get left
double beginEndAngle() const
returns the angle in radians of the line connecting the first and the last position
double length() const
Returns the length.
void move2sideCustom(std::vector< double > amount, double maxExtension=100)
move position vector to side using a custom offset for each geometry point
friend std::ostream & operator<<(std::ostream &os, const PositionVector &geom)
void sortAsPolyCWByAngle()
sort as polygon CW by angle
PositionVector simplified() const
return the same shape with intermediate colinear points removed
void rotate2D(double angle)
PositionVector()
Constructor. Creates an empty position vector.
Position getPolygonCenter() const
Returns the arithmetic of all corner points.
Position intersectionPosition2D(const Position &p1, const Position &p2, const double withinDist=0.) const
Returns the position of the intersection.
const Position & operator[](int index) const
returns the constat position at the given index @ToDo !!! exceptions?
double rotationAtOffset(double pos) const
Returns the rotation at the given length.
std::vector< Position > vp
vector of position
void push_front_noDoublePos(const Position &p)
insert in front a non double position
bool operator!=(const PositionVector &v2) const
comparing operation
PositionVector resample(double maxLength, const bool adjustEnd) const
resample shape (i.e. transform to segments, equal spacing)
void sortByIncreasingXY()
sort by increasing X-Y Positions
double rotationDegreeAtOffset(double pos) const
Returns the rotation at the given length.
bool isNAN() const
check if PositionVector is NAN
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
void add(double xoff, double yoff, double zoff)
void closePolygon()
ensures that the last position equals the first
static Position sideOffset(const Position &beg, const Position &end, const double amount)
get a side position of position vector using a offset
std::vector< double > intersectsAtLengths2D(const PositionVector &other) const
For all intersections between this vector and other, return the 2D-length of the subvector from this ...
double distance2D(const Position &p, bool perpendicular=false) const
closest 2D-distance to point p (or -1 if perpendicular is true and the point is beyond this vector)
void prepend(const PositionVector &v, double sameThreshold=2.0)
double nearest_offset_to_point2D(const Position &p, bool perpendicular=true) const
return the nearest offest to point 2D
std::vector< double > distances(const PositionVector &s, bool perpendicular=false) const
distances of all my points to s and all of s points to myself
PositionVector getOrthogonal(const Position &p, double extend, bool before, double length=1.0, double deg=90) const
return orthogonal through p (extending this vector if necessary)
int indexOfClosest(const Position &p, bool twoD=false) const
std::pair< PositionVector, PositionVector > splitAt(double where, bool use2D=false) const
Returns the two lists made when this list vector is splitted at the given point.
void move2side(double amount, double maxExtension=100)
move position vector to side using certain ammount
bool crosses(const Position &p1, const Position &p2) const
Returns whether the AbstractPoly crosses the given line.
PositionVector getSubpart2D(double beginOffset, double endOffset) const
get subpart of a position vector in two dimensions (Z is ignored)
PositionVector interpolateZ(double zStart, double zEnd) const
returned vector that varies z smoothly over its length
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
double offsetAtIndex2D(int index) const
return the offset at the given index
PositionVector smoothedZFront(double dist=std::numeric_limits< double >::max()) const
returned vector that is smoothed at the front (within dist)
double angleAt2D(int pos) const
get angle in certain position of position vector
void insert_noDoublePos(const std::vector< Position >::iterator &at, const Position &p)
insert in front a non double position
double slopeDegreeAtOffset(double pos) const
Returns the slope at the given length.
bool hasElevation() const
return whether two positions differ in z-coordinate
static const PositionVector EMPTY
empty Vector
void extrapolate(const double val, const bool onlyFirst=false, const bool onlyLast=false)
extrapolate position vector
PositionVector bezier(int numPoints)
return a bezier interpolation
Position getLineCenter() const
get line center
Position getCentroid() const
Returns the centroid (closes the polygon if unclosed)
double getOverlapWith(const PositionVector &poly, double zThreshold) const
Returns the maximum overlaps between this and the given polygon (when not separated by at least zThre...
PositionVector operator+(const PositionVector &v2) const
adds two vectors (requires vectors of the same length)
void extrapolate2D(const double val, const bool onlyFirst=false)
extrapolate position vector in two dimensions (Z is ignored)
int insertAtClosest(const Position &p, bool interpolateZ)
inserts p between the two closest positions
void push_front(const Position &p)
insert in front a Position
void scaleRelative(double factor)
enlarges/shrinks the polygon by a factor based at the centroid
void push_back_noDoublePos(const Position &p)
insert in back a non double position
void removeDoublePoints(double minDist=POSITION_EPS, bool assertLength=false, int beginOffset=0, int endOffset=0, bool resample=false)
Removes positions if too near.
bool partialWithin(const AbstractPoly &poly, double offset=0) const
Returns the information whether this polygon lies partially within the given polygon.
double getMaxGrade(double &maxJump) const
double area() const
Returns the area (0 for non-closed)
bool isClosed() const
check if PositionVector is closed
void pop_front()
pop first Position
double nearest_offset_to_point25D(const Position &p, bool perpendicular=true) const
return the nearest offest to point 2D projected onto the 3D geometry
int removeClosest(const Position &p)
removes the point closest to p and return the removal index
static double localAngle(const Position &from, const Position &pos, const Position &to)
bool intersects(const Position &p1, const Position &p2) const
Returns the information whether this list of points interesects the given line.
PositionVector reverse() const
reverse position vector
PositionVector getSubpartByIndex(int beginIndex, int count) const
get subpart of a position vector using index and a cout
Position positionAtOffset2D(double pos, double lateralOffset=0) const
Returns the position at the given length.
bool operator==(const PositionVector &v2) const
comparing operation
void sub(const Position &offset)
PositionVector getSubpart(double beginOffset, double endOffset) const
get subpart of a position vector
~PositionVector()
Destructor.
bool around(const Position &p, double offset=0) const
Returns the information whether the position vector describes a polygon lying around the given point.
Position transformToVectorCoordinates(const Position &p, bool extend=false) const
return position p within the length-wise coordinate system defined by this position vector....