SUMO - Simulation of Urban MObility
Line.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // }
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2015 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 
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include "Position.h"
36 #include "Line.h"
37 #include "GeomHelper.h"
38 #include <utils/common/ToString.h>
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // member method definitions
47 // ===========================================================================
48 
50 
51 
52 Line::Line(const Position& p1, const Position& p2)
53  : myP1(p1), myP2(p2) {}
54 
55 
57 
58 
59 void
61  SUMOReal factor = length / myP1.distanceTo(myP2);
62  Position offset = (myP2 - myP1) * factor;
63  myP1.sub(offset);
64  myP2.add(offset);
65 }
66 
67 
68 void
70  SUMOReal factor = length / myP1.distanceTo2D(myP2);
71  Position offset = (myP2 - myP1) * factor;
72  myP1.sub(offset);
73  myP2.add(offset);
74 }
75 
76 
77 void
80 }
81 
82 
83 void
86 }
87 
88 const Position&
89 Line::p1() const {
90  return myP1;
91 }
92 
93 
94 const Position&
95 Line::p2() const {
96  return myP2;
97 }
98 
99 
100 Position
103  if (length == 0) {
104  if (offset != 0) {
105  throw InvalidArgument("Invalid offset " + toString(offset) + " for Line with length " + toString(length));;
106  }
107  return myP1;
108  }
109  return myP1 + (myP2 - myP1) * (offset / length);
110 }
111 
112 
113 Position
116  if (length == 0) {
117  if (offset != 0) {
118  throw InvalidArgument("Invalid offset " + toString(offset) + " for Line with length " + toString(length));;
119  }
120  return myP1;
121  }
122  return myP1 + (myP2 - myP1) * (offset / length);
123 }
124 
125 
126 void
128  std::pair<SUMOReal, SUMOReal> p = GeomHelper::getNormal90D_CW(myP1, myP2, amount);
129  myP1.add(p.first, p.second);
130  myP2.add(p.first, p.second);
131 }
132 
133 
134 std::vector<SUMOReal>
137  std::vector<SUMOReal> ret;
138  for (size_t i = 0; i < p.size(); i++) {
139  ret.push_back(myP1.distanceTo2D(p[int(i)]));
140  }
141  return ret;
142 }
143 
144 
145 SUMOReal
147  return atan2(myP1.x() - myP2.x(), myP1.y() - myP2.y());
148 }
149 
150 
151 SUMOReal
153  return RAD2DEG(atan2(myP1.x() - myP2.x(), myP1.y() - myP2.y()));
154 }
155 
156 
157 SUMOReal
159  SUMOReal angle = atan2Angle();
160  if (angle < 0) {
161  angle = (SUMOReal) M_PI * (SUMOReal) 2.0 + angle;
162  }
163  return angle;
164 }
165 
166 
167 SUMOReal
169  return RAD2DEG(atan2(myP2.z() - myP1.z(), myP1.distanceTo2D(myP2)));
170 }
171 
172 
173 Position
174 Line::intersectsAt(const Line& l) const {
176 }
177 
178 
179 bool
180 Line::intersects(const Line& l) const {
181  return GeomHelper::intersects(myP1, myP2, l.myP1, l.myP2);
182 }
183 
184 
185 SUMOReal
186 Line::length2D() const {
187  return myP1.distanceTo2D(myP2);
188 }
189 
190 
191 SUMOReal
192 Line::length() const {
193  return myP1.distanceTo(myP2);
194 }
195 
196 
197 void
199  myP1.add(x, y);
200  myP2.add(x, y);
201 }
202 
203 
204 void
205 Line::add(const Position& p) {
206  myP1.add(p.x(), p.y(), p.z());
207  myP2.add(p.x(), p.y(), p.z());
208 }
209 
210 
211 void
213  myP1.sub(x, y);
214  myP2.sub(x, y);
215 }
216 
217 
218 
219 Line&
221  Position tmp(myP1);
222  myP1 = myP2;
223  myP2 = tmp;
224  return *this;
225 }
226 
227 
228 SUMOReal
230  Position pos =
233 }
234 
235 
236 void
238  Position p = myP2;
239  p.sub(myP1);
240  p.reshiftRotate(0, 0, rot);
241  p.add(myP1);
242  myP2 = p;
243 }
244 
245 
246 std::ostream&
247 operator<<(std::ostream& os, const Line& line) {
248  os << line.myP1 << " " << line.myP2;
249  return os;
250 }
251 
252 
253 
254 /****************************************************************************/
SUMOReal length2D() const
Definition: Line.cpp:186
SUMOReal atan2PositiveAngle() const
Definition: Line.cpp:158
void sub(SUMOReal dx, SUMOReal dy)
Substracts the given position from this one.
Definition: Position.h:139
SUMOReal atan2DegreeAngle() const
Definition: Line.cpp:152
static std::pair< SUMOReal, SUMOReal > getNormal90D_CW(const Position &beg, const Position &end, SUMOReal length, SUMOReal wanted_offset)
Definition: GeomHelper.cpp:373
const Position & p2() const
Definition: Line.cpp:95
void reshiftRotate(SUMOReal xoff, SUMOReal yoff, SUMOReal rot)
Definition: Position.h:164
Position myP2
Definition: Line.h:88
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:203
void add(const Position &pos)
Adds the given position to this one.
Definition: Position.h:119
SUMOReal intersectsAtLength2D(const Line &v)
returns distance between myP1 and intersection or -1 if line segments do not intersect ...
Definition: Line.cpp:229
#define M_PI
Definition: angles.h:37
SUMOReal atan2DegreeSlope() const
Definition: Line.cpp:168
Position getPositionAtDistance2D(SUMOReal offset) const
Definition: Line.cpp:114
void rotateAtP1(SUMOReal rot)
Definition: Line.cpp:237
void extrapolateFirstBy(SUMOReal length)
Definition: Line.cpp:78
static Position extrapolate_second(const Position &p1, const Position &p2, SUMOReal length)
Definition: GeomHelper.cpp:253
#define RAD2DEG(x)
Definition: GeomHelper.h:46
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:245
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
Position getPositionAtDistance(SUMOReal offset) const
Definition: Line.cpp:101
static SUMOReal nearest_offset_on_line_to_point2D(const Position &l1, const Position &l2, const Position &p, bool perpendicular=true)
Definition: GeomHelper.cpp:261
static bool intersects(const Position &p11, const Position &p12, const Position &p21, const Position &p22)
return whether given lines intersect
Definition: GeomHelper.cpp:145
const Position & p1() const
Definition: Line.cpp:89
~Line()
Definition: Line.cpp:56
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
void move2side(SUMOReal amount)
Definition: Line.cpp:127
SUMOReal z() const
Returns the z-position.
Definition: Position.h:73
Definition: Line.h:51
Line & reverse()
Definition: Line.cpp:220
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
SUMOReal atan2Angle() const
Definition: Line.cpp:146
bool intersects(const Line &l) const
Definition: Line.cpp:180
std::ostream & operator<<(std::ostream &os, const Line &line)
Definition: Line.cpp:247
void extrapolateBy(SUMOReal length)
Definition: Line.cpp:60
void push_back(const PositionVector &p)
Appends all positions from the given vector.
void extrapolateSecondBy(SUMOReal length)
Definition: Line.cpp:84
SUMOReal length() const
Definition: Line.cpp:192
PositionVector intersectionPoints2D(const Line &line) const
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
void sub(SUMOReal x, SUMOReal y)
Definition: Line.cpp:212
void extrapolateBy2D(SUMOReal length)
Definition: Line.cpp:69
Position myP1
Definition: Line.h:88
SUMOReal distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition: Position.h:240
Line()
Definition: Line.cpp:49
#define SUMOReal
Definition: config.h:218
std::vector< SUMOReal > intersectsAtLengths2D(const PositionVector &v)
Definition: Line.cpp:135
Position intersectsAt(const Line &l) const
Definition: Line.cpp:174
void add(SUMOReal x, SUMOReal y)
Definition: Line.cpp:198