SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeomHelper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Some static methods performing geometrical operations
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2014 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 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <cmath>
35 #include <limits>
36 #include <algorithm>
37 #include <iostream>
38 #include <utils/common/StdDefs.h>
39 #include <utils/common/ToString.h>
40 #include <utils/geom/Line.h>
41 #include "Boundary.h"
42 #include "GeomHelper.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 bool
54  const SUMOReal x2, const SUMOReal y2,
55  const SUMOReal x3, const SUMOReal y3,
56  const SUMOReal x4, const SUMOReal y4,
57  SUMOReal* x, SUMOReal* y, SUMOReal* mu) {
58  const SUMOReal eps = std::numeric_limits<SUMOReal>::epsilon();
59  const double denominator = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
60  const double numera = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
61  const double numerb = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3);
62  /* Are the lines coincident? */
63  if (fabs(numera) < eps && fabs(numerb) < eps && fabs(denominator) < eps) {
64  SUMOReal a1;
65  SUMOReal a2;
66  SUMOReal a3;
67  SUMOReal a4;
68  SUMOReal a = -1e12;
69  if (x1 != x2) {
70  a1 = x1 < x2 ? x1 : x2;
71  a2 = x1 < x2 ? x2 : x1;
72  a3 = x3 < x4 ? x3 : x4;
73  a4 = x3 < x4 ? x4 : x3;
74  } else {
75  a1 = y1 < y2 ? y1 : y2;
76  a2 = y1 < y2 ? y2 : y1;
77  a3 = y3 < y4 ? y3 : y4;
78  a4 = y3 < y4 ? y4 : y3;
79  }
80  if (a1 <= a3 && a3 <= a2) {
81  if (a4 < a2) {
82  a = (a3 + a4) / 2;
83  } else {
84  a = (a2 + a3) / 2;
85  }
86  }
87  if (a3 <= a1 && a1 <= a4) {
88  if (a2 < a4) {
89  a = (a1 + a2) / 2;
90  } else {
91  a = (a1 + a4) / 2;
92  }
93  }
94  if (a != -1e12) {
95  if (x != 0) {
96  if (x1 != x2) {
97  *mu = (a - x1) / (x2 - x1);
98  *x = a;
99  *y = y1 + (*mu) * (y2 - y1);
100  } else {
101  *x = x1;
102  *y = a;
103  if (y2 == y1) {
104  *mu = 0;
105  } else {
106  *mu = (a - y1) / (y2 - y1);
107  }
108  }
109  }
110  return true;
111  }
112  return false;
113  }
114  /* Are the lines parallel */
115  if (fabs(denominator) < eps) {
116  return false;
117  }
118  /* Is the intersection along the segments */
119  double mua = numera / denominator;
120  /* reduce rounding errors for lines ending in the same point */
121  if (fabs(x2 - x4) < eps && fabs(y2 - y4) < eps) {
122  mua = 1.;
123  } else {
124  const double mub = numerb / denominator;
125  if (mua < 0 || mua > 1 || mub < 0 || mub > 1) {
126  return false;
127  }
128  }
129  if (x != 0) {
130  *x = x1 + mua * (x2 - x1);
131  *y = y1 + mua * (y2 - y1);
132  *mu = mua;
133  }
134  return true;
135 }
136 
137 
138 
139 bool
140 GeomHelper::intersects(const Position& p11, const Position& p12,
141  const Position& p21, const Position& p22) {
142  return intersects(p11.x(), p11.y(), p12.x(), p12.y(),
143  p21.x(), p21.y(), p22.x(), p22.y(), 0, 0, 0);
144 }
145 
146 
147 bool
148 GeomHelper::pointOnLine(const Position& p, const Position& from, const Position& to) {
149  if (p.x() >= MIN2(from.x(), to.x()) && p.x() <= MAX2(from.x(), to.x()) &&
150  p.y() >= MIN2(from.y(), to.y()) && p.y() <= MAX2(from.y(), to.y())) {
151  return true;
152  }
153  return false;
154 }
155 
156 
157 void
159  std::vector<SUMOReal>& into) {
160  SUMOReal dx = p2.x() - p1.x();
161  SUMOReal dy = p2.y() - p1.y();
162 
163  SUMOReal A = dx * dx + dy * dy;
164  SUMOReal B = 2 * (dx * (p1.x() - c.x()) + dy * (p1.y() - c.y()));
165  SUMOReal C = (p1.x() - c.x()) * (p1.x() - c.x()) + (p1.y() - c.y()) * (p1.y() - c.y()) - radius * radius;
166 
167  SUMOReal det = B * B - 4 * A * C;
168  if ((A <= 0.0000001) || (det < 0)) {
169  // No real solutions.
170  return;
171  } else if (det == 0) {
172  // One solution.
173  SUMOReal t = -B / (2 * A);
174  Position intersection(p1.x() + t * dx, p1.y() + t * dy);
175  if (GeomHelper::pointOnLine(intersection, p1, p2)) {
176  into.push_back(t);
177  }
178  return;
179  } else {
180  // Two solutions.
181  SUMOReal t = (float)((-B + sqrt(det)) / (2 * A));
182  Position intersection(p1.x() + t * dx, p1.y() + t * dy);
183  if (GeomHelper::pointOnLine(intersection, p1, p2)) {
184  into.push_back(t);
185  }
186  t = (float)((-B - sqrt(det)) / (2 * A));
187  intersection.set(p1.x() + t * dx, p1.y() + t * dy);
188  if (GeomHelper::pointOnLine(intersection, p1, p2)) {
189  into.push_back(t);
190  }
191  return;
192  }
193 }
194 
195 
196 
197 Position
199  const Position& p12,
200  const Position& p21,
201  const Position& p22) {
202  SUMOReal x, y, m;
203  if (intersects(p11.x(), p11.y(), p12.x(), p12.y(),
204  p21.x(), p21.y(), p22.x(), p22.y(), &x, &y, &m)) {
205  // @todo calculate better "average" z value
206  return Position(x, y, p11.z() + m * (p12.z() - p11.z()));
207  }
208  return Position::INVALID;
209 }
210 
211 
212 
213 /*
214  Return the angle between two vectors on a plane
215  The angle is from vector 1 to vector 2, positive anticlockwise
216  The result is between -pi -> pi
217 */
218 SUMOReal
220  SUMOReal dtheta = atan2(y2, x2) - atan2(y1, x1);
221  while (dtheta > (SUMOReal) M_PI) {
222  dtheta -= (SUMOReal)(2.0 * M_PI);
223  }
224  while (dtheta < (SUMOReal) - M_PI) {
225  dtheta += (SUMOReal)(2.0 * M_PI);
226  }
227  return dtheta;
228 }
229 
230 
231 Position
233  const Position& p2, SUMOReal length) {
234  const SUMOReal factor = length / p1.distanceTo(p2);
235  return p1 + (p2 - p1) * factor;
236 }
237 
238 
239 Position
241  const Position& p2, SUMOReal length) {
242  const SUMOReal factor = length / p1.distanceTo(p2);
243  return p1 - (p2 - p1) * factor;
244 }
245 
246 
247 Position
249  const Position& p2, SUMOReal length) {
250  const SUMOReal factor = length / p1.distanceTo(p2);
251  return p2 - (p1 - p2) * factor;
252 }
253 
254 
255 SUMOReal
257  const Position& LineEnd,
258  const Position& Point, bool perpendicular) {
259  const SUMOReal lineLength2D = LineStart.distanceTo2D(LineEnd);
260  if (lineLength2D == 0.0f) {
261  return 0.0f;
262  } else {
263  // scalar product equals length of orthogonal projection times length of vector being projected onto
264  // dividing the scalar product by the square of the distance gives the relative position
265  const SUMOReal u = (((Point.x() - LineStart.x()) * (LineEnd.x() - LineStart.x())) +
266  ((Point.y() - LineStart.y()) * (LineEnd.y() - LineStart.y()))
267  ) / (lineLength2D * lineLength2D);
268  if (u < 0.0f || u > 1.0f) { // closest point does not fall within the line segment
269  if (perpendicular) {
270  return -1;
271  }
272  if (u < 0.0f) {
273  return 0.0f;
274  }
275  return lineLength2D;
276  }
277  return u * lineLength2D;
278  }
279 }
280 
281 
282 SUMOReal
284  const Position& lineStart,
285  const Position& lineEnd) {
286  const SUMOReal lineLengthSquared = lineStart.distanceSquaredTo(lineEnd);
287  if (lineLengthSquared == 0.0f) {
288  return point.distanceTo(lineStart);
289  } else {
290  // scalar product equals length of orthogonal projection times length of vector being projected onto
291  // dividing the scalar product by the square of the distance gives the relative position
292  SUMOReal u = (((point.x() - lineStart.x()) * (lineEnd.x() - lineStart.x())) +
293  ((point.y() - lineStart.y()) * (lineEnd.y() - lineStart.y()))
294  ) / lineLengthSquared;
295  if (u < 0.0f || u > 1.0f) {
296  return -1; // closest point does not fall within the line segment
297  }
298  Position intersection(
299  lineStart.x() + u * (lineEnd.x() - lineStart.x()),
300  lineStart.y() + u * (lineEnd.y() - lineStart.y()));
301  return point.distanceTo(intersection);
302  }
303 }
304 
305 
306 SUMOReal
308  const Position& lineStart,
309  const Position& lineEnd,
310  Position& outIntersection) {
311  const SUMOReal length = nearest_offset_on_line_to_point2D(lineStart, lineEnd, point, false);
312  outIntersection.set(Line(lineStart, lineEnd).getPositionAtDistance(length));
313  return point.distanceTo2D(outIntersection);
314 }
315 
316 
317 
318 Position
320  const Position& lineBeg,
321  const Position& lineEnd,
322  SUMOReal amount) {
323  const SUMOReal dx = lineBeg.x() - lineEnd.x();
324  const SUMOReal dy = lineBeg.y() - lineEnd.y();
325  const SUMOReal length = sqrt(dx * dx + dy * dy);
326  if (length > 0) {
327  p.add(dy * amount / length, -dx * amount / length);
328  }
329  return p;
330 }
331 
332 
333 
334 Position
336  if (v.intersects(Position(b.xmin(), b.ymin()), Position(b.xmin(), b.ymax()))) {
337  return v.intersectsAtPoint(
338  Position(b.xmin(), b.ymin()),
339  Position(b.xmin(), b.ymax()));
340  }
341  if (v.intersects(Position(b.xmax(), b.ymin()), Position(b.xmax(), b.ymax()))) {
342  return v.intersectsAtPoint(
343  Position(b.xmax(), b.ymin()),
344  Position(b.xmax(), b.ymax()));
345  }
346  if (v.intersects(Position(b.xmin(), b.ymin()), Position(b.xmax(), b.ymin()))) {
347  return v.intersectsAtPoint(
348  Position(b.xmin(), b.ymin()),
349  Position(b.xmax(), b.ymin()));
350  }
351  if (v.intersects(Position(b.xmin(), b.ymax()), Position(b.xmax(), b.ymax()))) {
352  return v.intersectsAtPoint(
353  Position(b.xmin(), b.ymax()),
354  Position(b.xmax(), b.ymax()));
355  }
356  throw 1;
357 }
358 
359 std::pair<SUMOReal, SUMOReal>
361  const Position& end,
362  SUMOReal wanted_offset) {
363  return getNormal90D_CW(beg, end, beg.distanceTo2D(end), wanted_offset);
364 }
365 
366 
367 std::pair<SUMOReal, SUMOReal>
369  const Position& end,
370  SUMOReal length, SUMOReal wanted_offset) {
371  if (beg == end) {
372  throw InvalidArgument("same points at " + toString(beg));
373  }
374  return std::pair<SUMOReal, SUMOReal>
375  ((beg.y() - end.y()) * wanted_offset / length, (end.x() - beg.x()) * wanted_offset / length);
376 }
377 
378 
379 SUMOReal
381  SUMOReal v = angle2 - angle1;
382  if (v < 0) {
383  v = 360 + v;
384  }
385  return v;
386 }
387 
388 
389 SUMOReal
391  SUMOReal v = angle1 - angle2;
392  if (v < 0) {
393  v = 360 + v;
394  }
395  return v;
396 }
397 
398 
399 SUMOReal
401  return MIN2(getCWAngleDiff(angle1, angle2), getCCWAngleDiff(angle1, angle2));
402 }
403 
404 
405 SUMOReal
407  return MAX2(getCWAngleDiff(angle1, angle2), getCCWAngleDiff(angle1, angle2));
408 }
409 
410 
411 
412 /****************************************************************************/
413 
static std::pair< SUMOReal, SUMOReal > getNormal90D_CW(const Position &beg, const Position &end, SUMOReal length, SUMOReal wanted_offset)
Definition: GeomHelper.cpp:368
static SUMOReal Angle2D(SUMOReal x1, SUMOReal y1, SUMOReal x2, SUMOReal y2)
Definition: GeomHelper.cpp:219
static SUMOReal getCWAngleDiff(SUMOReal angle1, SUMOReal angle2)
Returns the distance of second angle from first angle clockwise.
Definition: GeomHelper.cpp:390
static SUMOReal getCCWAngleDiff(SUMOReal angle1, SUMOReal angle2)
Returns the distance of second angle from first angle counter-clockwise.
Definition: GeomHelper.cpp:380
static Position intersection_position2D(const Position &p11, const Position &p12, const Position &p21, const Position &p22)
returns the intersection point of the (infinite) lines p11,p12 and p21,p22. If the given lines are pa...
Definition: GeomHelper.cpp:198
static Position interpolate(const Position &p1, const Position &p2, SUMOReal length)
Definition: GeomHelper.cpp:232
void add(const Position &pos)
Adds the given position to this one.
Definition: Position.h:119
SUMOReal distanceSquaredTo(const Position &p2) const
Definition: Position.h:234
#define M_PI
Definition: angles.h:37
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:124
SUMOReal xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:112
bool intersects(const Position &p1, const Position &p2) const
T MAX2(T a, T b)
Definition: StdDefs.h:74
static Position extrapolate_second(const Position &p1, const Position &p2, SUMOReal length)
Definition: GeomHelper.cpp:248
SUMOReal distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:229
static Position extrapolate_first(const Position &p1, const Position &p2, SUMOReal length)
Definition: GeomHelper.cpp:240
static void FindLineCircleIntersections(const Position &c, SUMOReal radius, const Position &p1, const Position &p2, std::vector< SUMOReal > &into)
Returns the positions the given circle is crossed by the given line.
Definition: GeomHelper.cpp:158
static Position crossPoint(const Boundary &b, const PositionVector &v)
Definition: GeomHelper.cpp:335
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:118
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
static SUMOReal nearest_offset_on_line_to_point2D(const Position &l1, const Position &l2, const Position &p, bool perpendicular=true)
Definition: GeomHelper.cpp:256
static bool intersects(const Position &p11, const Position &p12, const Position &p21, const Position &p22)
return whether given lines intersect
Definition: GeomHelper.cpp:140
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
static Position transfer_to_side(Position &p, const Position &lineBeg, const Position &lineEnd, SUMOReal amount)
Definition: GeomHelper.cpp:319
A list of positions.
static SUMOReal distancePointLine(const Position &point, const Position &lineStart, const Position &lineEnd)
Definition: GeomHelper.cpp:283
SUMOReal z() const
Returns the z-position.
Definition: Position.h:73
Definition: Line.h:51
T MIN2(T a, T b)
Definition: StdDefs.h:68
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
Position intersectsAtPoint(const Position &p1, const Position &p2) const
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
static SUMOReal getMinAngleDiff(SUMOReal angle1, SUMOReal angle2)
Returns the minimum distance (clockwise/counter-clockwise) between both angles.
Definition: GeomHelper.cpp:400
void set(SUMOReal x, SUMOReal y)
Definition: Position.h:78
static SUMOReal getMaxAngleDiff(SUMOReal angle1, SUMOReal angle2)
Returns the maximum distance (clockwise/counter-clockwise) between both angles.
Definition: GeomHelper.cpp:406
SUMOReal distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition: Position.h:240
#define SUMOReal
Definition: config.h:218
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:130
static bool pointOnLine(const Position &p, const Position &from, const Position &to)
Returns whether the given point lies on the given line.
Definition: GeomHelper.cpp:148
static SUMOReal closestDistancePointLine(const Position &point, const Position &lineStart, const Position &lineEnd, Position &outIntersection)
Definition: GeomHelper.cpp:307
static const Position INVALID
Definition: Position.h:262