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.sourceforge.net/
13 // Copyright (C) 2001-2012 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 #include <cassert>
40 
41 #ifdef CHECK_MEMORY_LEAKS
42 #include <foreign/nvwa/debug_new.h>
43 #endif // CHECK_MEMORY_LEAKS
44 
45 
46 // ===========================================================================
47 // member method definitions
48 // ===========================================================================
49 
51 
52 
53 Line::Line(const Position& p1, const Position& p2)
54  : myP1(p1), myP2(p2) {}
55 
56 
58 
59 
60 void
62  SUMOReal factor = length / myP1.distanceTo(myP2);
63  Position offset = (myP2 - myP1) * factor;
64  myP1.sub(offset);
65  myP2.add(offset);
66 }
67 
68 
69 void
72 }
73 
74 
75 void
78 }
79 
80 const Position&
81 Line::p1() const {
82  return myP1;
83 }
84 
85 
86 const Position&
87 Line::p2() const {
88  return myP2;
89 }
90 
91 
95  if (length == 0) {
96  if (offset != 0) {
97  throw InvalidArgument("Invalid offset " + toString(offset) + " for Line with length " + toString(length));;
98  }
99  return myP1;
100  }
101  return myP1 + (myP2 - myP1) * (offset / length);
102 }
103 
104 
105 Position
108  if (length == 0) {
109  if (offset != 0) {
110  throw InvalidArgument("Invalid offset " + toString(offset) + " for Line with length " + toString(length));;
111  }
112  return myP1;
113  }
114  return myP1 + (myP2 - myP1) * (offset / length);
115 }
116 
117 
118 void
120  std::pair<SUMOReal, SUMOReal> p = GeomHelper::getNormal90D_CW(myP1, myP2, amount);
121  myP1.add(p.first, p.second);
122  myP2.add(p.first, p.second);
123 }
124 
125 
126 std::vector<SUMOReal>
129  std::vector<SUMOReal> ret;
130  for (size_t i = 0; i < p.size(); i++) {
131  ret.push_back(myP1.distanceTo2D(p[int(i)]));
132  }
133  return ret;
134 }
135 
136 
137 SUMOReal
139  return atan2(myP1.x() - myP2.x(), myP1.y() - myP2.y());
140 }
141 
142 
143 SUMOReal
145  return (SUMOReal) atan2(myP1.x() - myP2.x(), myP1.y() - myP2.y()) * (SUMOReal) 180.0 / (SUMOReal) PI;
146 }
147 
148 
149 SUMOReal
151  SUMOReal angle = atan2Angle();
152  if (angle < 0) {
153  angle = (SUMOReal) PI * (SUMOReal) 2.0 + angle;
154  }
155  return angle;
156 }
157 
158 Position
159 Line::intersectsAt(const Line& l) const {
161 }
162 
163 
164 bool
165 Line::intersects(const Line& l) const {
166  return GeomHelper::intersects(myP1, myP2, l.myP1, l.myP2);
167 }
168 
169 
170 SUMOReal
171 Line::length2D() const {
172  return myP1.distanceTo2D(myP2);
173 }
174 
175 
176 SUMOReal
177 Line::length() const {
178  return myP1.distanceTo(myP2);
179 }
180 
181 
182 void
184  myP1.add(x, y);
185  myP2.add(x, y);
186 }
187 
188 
189 void
190 Line::add(const Position& p) {
191  myP1.add(p.x(), p.y(), p.z());
192  myP2.add(p.x(), p.y(), p.z());
193 }
194 
195 
196 void
198  myP1.sub(x, y);
199  myP2.sub(x, y);
200 }
201 
202 
203 
204 Line&
206  Position tmp(myP1);
207  myP1 = myP2;
208  myP2 = tmp;
209  return *this;
210 }
211 
212 
213 SUMOReal
215  Position pos =
218 }
219 
220 
221 void
223  Position p = myP2;
224  p.sub(myP1);
225  p.reshiftRotate(0, 0, rot);
226  p.add(myP1);
227  myP2 = p;
228 }
229 
230 
231 /****************************************************************************/