SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Boundary.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A class that stores the 2D geometrical boundary
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 #include <utility>
33 
34 #include "GeomHelper.h"
35 #include "Boundary.h"
36 #include "PositionVector.h"
37 #include "Position.h"
38 
39 #ifdef CHECK_MEMORY_LEAKS
40 #include <foreign/nvwa/debug_new.h>
41 #endif // CHECK_MEMORY_LEAKS
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
48  : myXmin(10000000000.0), myXmax(-10000000000.0),
49  myYmin(10000000000.0), myYmax(-10000000000.0),
50  myWasInitialised(false) {}
51 
52 
54  : myXmin(10000000000.0), myXmax(-10000000000.0),
55  myYmin(10000000000.0), myYmax(-10000000000.0),
56  myWasInitialised(false) {
57  add(x1, y1);
58  add(x2, y2);
59 }
60 
61 
63 
64 
65 void
67  myXmin = 10000000000.0;
68  myXmax = -10000000000.0;
69  myYmin = 10000000000.0;
70  myYmax = -10000000000.0;
71  myWasInitialised = false;
72 }
73 
74 
75 void
77  if (!myWasInitialised) {
78  myYmin = y;
79  myYmax = y;
80  myXmin = x;
81  myXmax = x;
82  } else {
83  myXmin = myXmin < x ? myXmin : x;
84  myXmax = myXmax > x ? myXmax : x;
85  myYmin = myYmin < y ? myYmin : y;
86  myYmax = myYmax > y ? myYmax : y;
87  }
88  myWasInitialised = true;
89 }
90 
91 
92 void
94  add(p.x(), p.y());
95 }
96 
97 
98 void
100  add(p.xmin(), p.ymin());
101  add(p.xmax(), p.ymax());
102 }
103 
104 
105 Position
107  return Position((myXmin + myXmax) / (SUMOReal) 2.0, (myYmin + myYmax) / (SUMOReal) 2.0);
108 }
109 
110 
111 SUMOReal
112 Boundary::xmin() const {
113  return myXmin;
114 }
115 
116 
117 SUMOReal
118 Boundary::xmax() const {
119  return myXmax;
120 }
121 
122 
123 SUMOReal
124 Boundary::ymin() const {
125  return myYmin;
126 }
127 
128 
129 SUMOReal
130 Boundary::ymax() const {
131  return myYmax;
132 }
133 
134 
135 SUMOReal
137  return myXmax - myXmin;
138 }
139 
140 
141 SUMOReal
143  return myYmax - myYmin;
144 }
145 
146 
147 bool
148 Boundary::around(const Position& p, SUMOReal offset) const {
149  return
150  (p.x() <= myXmax + offset && p.x() >= myXmin - offset) &&
151  (p.y() <= myYmax + offset && p.y() >= myYmin - offset);
152 }
153 
154 
155 bool
157  if (
158  // check whether one of my points lies within the given poly
159  partialWithin(p, offset) ||
160  // check whether the polygon lies within me
161  p.partialWithin(*this, offset)) {
162  return true;
163  }
164  // check whether the bounderies cross
165  return
166  p.crosses(Position(myXmax + offset, myYmax + offset), Position(myXmin - offset, myYmax + offset))
167  ||
168  p.crosses(Position(myXmin - offset, myYmax + offset), Position(myXmin - offset, myYmin - offset))
169  ||
170  p.crosses(Position(myXmin - offset, myYmin - offset), Position(myXmax + offset, myYmin - offset))
171  ||
172  p.crosses(Position(myXmax + offset, myYmin - offset), Position(myXmax + offset, myYmax + offset));
173 }
174 
175 
176 bool
177 Boundary::crosses(const Position& p1, const Position& p2) const {
178  return
180  ||
182  ||
184  ||
186 }
187 
188 
189 bool
190 Boundary::partialWithin(const AbstractPoly& poly, SUMOReal offset) const {
191  return
192  poly.around(Position(myXmax, myYmax), offset) ||
193  poly.around(Position(myXmin, myYmax), offset) ||
194  poly.around(Position(myXmax, myYmin), offset) ||
195  poly.around(Position(myXmin, myYmin), offset);
196 }
197 
198 
199 Boundary&
201  myXmax += by;
202  myYmax += by;
203  myXmin -= by;
204  myYmin -= by;
205  return *this;
206 }
207 
208 void
210  myXmin -= by;
211  myXmax += by;
212 }
213 
214 
215 void
217  myYmin -= by;
218  myYmax += by;
219 }
220 
221 void
223  myYmin *= -1.0;
224  myYmax *= -1.0;
225  SUMOReal tmp = myYmin;
226  myYmin = myYmax;
227  myYmax = tmp;
228 }
229 
230 
231 
232 std::ostream&
233 operator<<(std::ostream& os, const Boundary& b) {
234  os << b.myXmin << "," << b.myYmin << "," << b.myXmax << "," << b.myYmax;
235  return os;
236 }
237 
238 
239 void
241  myXmin = xmin;
242  myYmin = ymin;
243  myXmax = xmax;
244  myYmax = ymax;
245 }
246 
247 
248 void
250  myXmin += x;
251  myYmin += y;
252  myXmax += x;
253  myYmax += y;
254 }
255 
256 
257 
258 /****************************************************************************/
259