SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Line.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // }
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2001-2013 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
71 }
72 
73 
74 void
77 }
78 
79 const Position&
80 Line::p1() const {
81  return myP1;
82 }
83 
84 
85 const Position&
86 Line::p2() const {
87  return myP2;
88 }
89 
90 
94  if (length == 0) {
95  if (offset != 0) {
96  throw InvalidArgument("Invalid offset " + toString(offset) + " for Line with length " + toString(length));;
97  }
98  return myP1;
99  }
100  return myP1 + (myP2 - myP1) * (offset / length);
101 }
102 
103 
104 Position
107  if (length == 0) {
108  if (offset != 0) {
109  throw InvalidArgument("Invalid offset " + toString(offset) + " for Line with length " + toString(length));;
110  }
111  return myP1;
112  }
113  return myP1 + (myP2 - myP1) * (offset / length);
114 }
115 
116 
117 void
119  std::pair<SUMOReal, SUMOReal> p = GeomHelper::getNormal90D_CW(myP1, myP2, amount);
120  myP1.add(p.first, p.second);
121  myP2.add(p.first, p.second);
122 }
123 
124 
125 std::vector<SUMOReal>
128  std::vector<SUMOReal> ret;
129  for (size_t i = 0; i < p.size(); i++) {
130  ret.push_back(myP1.distanceTo2D(p[int(i)]));
131  }
132  return ret;
133 }
134 
135 
136 SUMOReal
138  return atan2(myP1.x() - myP2.x(), myP1.y() - myP2.y());
139 }
140 
141 
142 SUMOReal
144  return RAD2DEG(atan2(myP1.x() - myP2.x(), myP1.y() - myP2.y()));
145 }
146 
147 
148 SUMOReal
150  SUMOReal angle = atan2Angle();
151  if (angle < 0) {
152  angle = (SUMOReal) M_PI * (SUMOReal) 2.0 + angle;
153  }
154  return angle;
155 }
156 
157 
158 SUMOReal
160  return RAD2DEG(atan2(myP2.z() - myP1.z(), myP1.distanceTo2D(myP2)));
161 }
162 
163 
164 Position
165 Line::intersectsAt(const Line& l) const {
167 }
168 
169 
170 bool
171 Line::intersects(const Line& l) const {
172  return GeomHelper::intersects(myP1, myP2, l.myP1, l.myP2);
173 }
174 
175 
176 SUMOReal
177 Line::length2D() const {
178  return myP1.distanceTo2D(myP2);
179 }
180 
181 
182 SUMOReal
183 Line::length() const {
184  return myP1.distanceTo(myP2);
185 }
186 
187 
188 void
190  myP1.add(x, y);
191  myP2.add(x, y);
192 }
193 
194 
195 void
196 Line::add(const Position& p) {
197  myP1.add(p.x(), p.y(), p.z());
198  myP2.add(p.x(), p.y(), p.z());
199 }
200 
201 
202 void
204  myP1.sub(x, y);
205  myP2.sub(x, y);
206 }
207 
208 
209 
210 Line&
212  Position tmp(myP1);
213  myP1 = myP2;
214  myP2 = tmp;
215  return *this;
216 }
217 
218 
219 SUMOReal
221  Position pos =
224 }
225 
226 
227 void
229  Position p = myP2;
230  p.sub(myP1);
231  p.reshiftRotate(0, 0, rot);
232  p.add(myP1);
233  myP2 = p;
234 }
235 
236 
237 /****************************************************************************/