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-sim.org/
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  *mu = (a - y1) / (y2 - y1);
104  }
105  }
106  return true;
107  }
108  return false;
109  }
110  /* Are the lines parallel */
111  if (fabs(denominator) < eps) {
112  return false;
113  }
114  /* Is the intersection along the segments */
115  const double mua = numera / denominator;
116  const double mub = numerb / denominator;
117  if (mua < 0 || mua > 1 || mub < 0 || mub > 1) {
118  return false;
119  }
120  if (x != 0) {
121  *x = x1 + mua * (x2 - x1);
122  *y = y1 + mua * (y2 - y1);
123  *mu = mua;
124  }
125  return true;
126 }
127 
128 
129 
130 bool
131 GeomHelper::intersects(const Position& p11, const Position& p12,
132  const Position& p21, const Position& p22) {
133  return intersects(p11.x(), p11.y(), p12.x(), p12.y(),
134  p21.x(), p21.y(), p22.x(), p22.y(), 0, 0, 0);
135 }
136 
137 
138 bool
139 GeomHelper::pointOnLine(const Position& p, const Position& from, const Position& to) {
140  if (p.x() >= MIN2(from.x(), to.x()) && p.x() <= MAX2(from.x(), to.x()) &&
141  p.y() >= MIN2(from.y(), to.y()) && p.y() <= MAX2(from.y(), to.y())) {
142  return true;
143  }
144  return false;
145 }
146 
147 
148 void
150  std::vector<SUMOReal>& into) {
151  SUMOReal dx = p2.x() - p1.x();
152  SUMOReal dy = p2.y() - p1.y();
153 
154  SUMOReal A = dx * dx + dy * dy;
155  SUMOReal B = 2 * (dx * (p1.x() - c.x()) + dy * (p1.y() - c.y()));
156  SUMOReal C = (p1.x() - c.x()) * (p1.x() - c.x()) + (p1.y() - c.y()) * (p1.y() - c.y()) - radius * radius;
157 
158  SUMOReal det = B * B - 4 * A * C;
159  if ((A <= 0.0000001) || (det < 0)) {
160  // No real solutions.
161  return;
162  } else if (det == 0) {
163  // One solution.
164  SUMOReal t = -B / (2 * A);
165  Position intersection(p1.x() + t * dx, p1.y() + t * dy);
166  if (GeomHelper::pointOnLine(intersection, p1, p2)) {
167  into.push_back(t);
168  }
169  return;
170  } else {
171  // Two solutions.
172  SUMOReal t = (float)((-B + sqrt(det)) / (2 * A));
173  Position intersection(p1.x() + t * dx, p1.y() + t * dy);
174  if (GeomHelper::pointOnLine(intersection, p1, p2)) {
175  into.push_back(t);
176  }
177  t = (float)((-B - sqrt(det)) / (2 * A));
178  intersection.set(p1.x() + t * dx, p1.y() + t * dy);
179  if (GeomHelper::pointOnLine(intersection, p1, p2)) {
180  into.push_back(t);
181  }
182  return;
183  }
184 }
185 
186 
187 
188 Position
190  const Position& p12,
191  const Position& p21,
192  const Position& p22) {
193  SUMOReal x, y, m;
194  if (intersects(p11.x(), p11.y(), p12.x(), p12.y(),
195  p21.x(), p21.y(), p22.x(), p22.y(), &x, &y, &m)) {
196  // @todo calculate better "average" z value
197  return Position(x, y, p11.z() + m * (p12.z() - p11.z()));
198  }
199  return Position(-1, -1);
200 }
201 
202 
203 
204 /*
205  Return the angle between two vectors on a plane
206  The angle is from vector 1 to vector 2, positive anticlockwise
207  The result is between -pi -> pi
208 */
209 SUMOReal
211  SUMOReal dtheta = atan2(y2, x2) - atan2(y1, x1);
212  while (dtheta > (SUMOReal) M_PI) {
213  dtheta -= (SUMOReal)(2.0 * M_PI);
214  }
215  while (dtheta < (SUMOReal) - M_PI) {
216  dtheta += (SUMOReal)(2.0 * M_PI);
217  }
218  return dtheta;
219 }
220 
221 
222 Position
224  const Position& p2, SUMOReal length) {
225  const SUMOReal factor = length / p1.distanceTo(p2);
226  return p1 + (p2 - p1) * factor;
227 }
228 
229 
230 Position
232  const Position& p2, SUMOReal length) {
233  const SUMOReal factor = length / p1.distanceTo(p2);
234  return p1 - (p2 - p1) * factor;
235 }
236 
237 
238 Position
240  const Position& p2, SUMOReal length) {
241  const SUMOReal factor = length / p1.distanceTo(p2);
242  return p2 - (p1 - p2) * factor;
243 }
244 
245 
246 SUMOReal
248  const Position& LineEnd,
249  const Position& Point, bool perpendicular) {
250  const SUMOReal lineLength2D = LineStart.distanceTo2D(LineEnd);
251  if (lineLength2D == 0.0f) {
252  return 0.0f;
253  } else {
254  // scalar product equals length of orthogonal projection times length of vector being projected onto
255  // dividing the scalar product by the square of the distance gives the relative position
256  const SUMOReal u = (((Point.x() - LineStart.x()) * (LineEnd.x() - LineStart.x())) +
257  ((Point.y() - LineStart.y()) * (LineEnd.y() - LineStart.y()))
258  ) / (lineLength2D * lineLength2D);
259  if (u < 0.0f || u > 1.0f) { // closest point does not fall within the line segment
260  if (perpendicular) {
261  return -1;
262  }
263  if (u < 0.0f) {
264  return 0.0f;
265  }
266  return lineLength2D;
267  }
268  return u * lineLength2D;
269  }
270 }
271 
272 
273 SUMOReal
275  const Position& lineStart,
276  const Position& lineEnd) {
277  const SUMOReal lineLengthSquared = lineStart.distanceSquaredTo(lineEnd);
278  if (lineLengthSquared == 0.0f) {
279  return point.distanceTo(lineStart);
280  } else {
281  // scalar product equals length of orthogonal projection times length of vector being projected onto
282  // dividing the scalar product by the square of the distance gives the relative position
283  SUMOReal u = (((point.x() - lineStart.x()) * (lineEnd.x() - lineStart.x())) +
284  ((point.y() - lineStart.y()) * (lineEnd.y() - lineStart.y()))
285  ) / lineLengthSquared;
286  if (u < 0.0f || u > 1.0f) {
287  return -1; // closest point does not fall within the line segment
288  }
289  Position intersection(
290  lineStart.x() + u * (lineEnd.x() - lineStart.x()),
291  lineStart.y() + u * (lineEnd.y() - lineStart.y()));
292  return point.distanceTo(intersection);
293  }
294 }
295 
296 
297 SUMOReal
299  const Position& lineStart,
300  const Position& lineEnd,
301  Position& outIntersection) {
302  const SUMOReal length = nearest_offset_on_line_to_point2D(lineStart, lineEnd, point, false);
303  outIntersection.set(Line(lineStart, lineEnd).getPositionAtDistance(length));
304  return point.distanceTo2D(outIntersection);
305 }
306 
307 
308 
309 Position
311  const Position& lineBeg,
312  const Position& lineEnd,
313  SUMOReal amount) {
314  const SUMOReal dx = lineBeg.x() - lineEnd.x();
315  const SUMOReal dy = lineBeg.y() - lineEnd.y();
316  const SUMOReal length = sqrt(dx * dx + dy * dy);
317  if (length > 0) {
318  p.add(dy * amount / length, -dx * amount / length);
319  }
320  return p;
321 }
322 
323 
324 
325 Position
327  if (v.intersects(Position(b.xmin(), b.ymin()), Position(b.xmin(), b.ymax()))) {
328  return v.intersectsAtPoint(
329  Position(b.xmin(), b.ymin()),
330  Position(b.xmin(), b.ymax()));
331  }
332  if (v.intersects(Position(b.xmax(), b.ymin()), Position(b.xmax(), b.ymax()))) {
333  return v.intersectsAtPoint(
334  Position(b.xmax(), b.ymin()),
335  Position(b.xmax(), b.ymax()));
336  }
337  if (v.intersects(Position(b.xmin(), b.ymin()), Position(b.xmax(), b.ymin()))) {
338  return v.intersectsAtPoint(
339  Position(b.xmin(), b.ymin()),
340  Position(b.xmax(), b.ymin()));
341  }
342  if (v.intersects(Position(b.xmin(), b.ymax()), Position(b.xmax(), b.ymax()))) {
343  return v.intersectsAtPoint(
344  Position(b.xmin(), b.ymax()),
345  Position(b.xmax(), b.ymax()));
346  }
347  throw 1;
348 }
349 
350 std::pair<SUMOReal, SUMOReal>
352  const Position& end,
353  SUMOReal wanted_offset) {
354  return getNormal90D_CW(beg, end, beg.distanceTo2D(end), wanted_offset);
355 }
356 
357 
358 std::pair<SUMOReal, SUMOReal>
360  const Position& end,
361  SUMOReal length, SUMOReal wanted_offset) {
362  if (beg == end) {
363  throw InvalidArgument("same points at " + toString(beg));
364  }
365  return std::pair<SUMOReal, SUMOReal>
366  ((beg.y() - end.y()) * wanted_offset / length, (end.x() - beg.x()) * wanted_offset / length);
367 }
368 
369 
370 SUMOReal
372  SUMOReal v = angle2 - angle1;
373  if (v < 0) {
374  v = 360 + v;
375  }
376  return v;
377 }
378 
379 
380 SUMOReal
382  SUMOReal v = angle1 - angle2;
383  if (v < 0) {
384  v = 360 + v;
385  }
386  return v;
387 }
388 
389 
390 SUMOReal
392  return MIN2(getCWAngleDiff(angle1, angle2), getCCWAngleDiff(angle1, angle2));
393 }
394 
395 
396 SUMOReal
398  return MAX2(getCWAngleDiff(angle1, angle2), getCCWAngleDiff(angle1, angle2));
399 }
400 
401 
402 
403 /****************************************************************************/
404 
static std::pair< SUMOReal, SUMOReal > getNormal90D_CW(const Position &beg, const Position &end, SUMOReal length, SUMOReal wanted_offset)
Definition: GeomHelper.cpp:359
static SUMOReal Angle2D(SUMOReal x1, SUMOReal y1, SUMOReal x2, SUMOReal y2)
Definition: GeomHelper.cpp:210
static SUMOReal getCWAngleDiff(SUMOReal angle1, SUMOReal angle2)
Returns the distance of second angle from first angle clockwise.
Definition: GeomHelper.cpp:381
static SUMOReal getCCWAngleDiff(SUMOReal angle1, SUMOReal angle2)
Returns the distance of second angle from first angle counter-clockwise.
Definition: GeomHelper.cpp:371
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:189
static Position interpolate(const Position &p1, const Position &p2, SUMOReal length)
Definition: GeomHelper.cpp:223
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:224
#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:71
static Position extrapolate_second(const Position &p1, const Position &p2, SUMOReal length)
Definition: GeomHelper.cpp:239
SUMOReal distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:219
static Position extrapolate_first(const Position &p1, const Position &p2, SUMOReal length)
Definition: GeomHelper.cpp:231
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:149
static Position crossPoint(const Boundary &b, const PositionVector &v)
Definition: GeomHelper.cpp:326
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:247
static bool intersects(const Position &p11, const Position &p12, const Position &p21, const Position &p22)
return whether given lines intersect
Definition: GeomHelper.cpp:131
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:310
A list of positions.
static SUMOReal distancePointLine(const Position &point, const Position &lineStart, const Position &lineEnd)
Definition: GeomHelper.cpp:274
SUMOReal z() const
Returns the z-position.
Definition: Position.h:73
Definition: Line.h:51
T MIN2(T a, T b)
Definition: StdDefs.h:65
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:52
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:391
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:397
SUMOReal distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition: Position.h:230
#define SUMOReal
Definition: config.h:215
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:139
static SUMOReal closestDistancePointLine(const Position &point, const Position &lineStart, const Position &lineEnd, Position &outIntersection)
Definition: GeomHelper.cpp:298