SUMO - Simulation of Urban MObility
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.dlr.de/
12 // Copyright (C) 2001-2016 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  myZmin(10000000000.0), myZmax(-10000000000.0),
51  myWasInitialised(false) {}
52 
53 
55  : myXmin(10000000000.0), myXmax(-10000000000.0),
56  myYmin(10000000000.0), myYmax(-10000000000.0),
57  myZmin(10000000000.0), myZmax(-10000000000.0),
58  myWasInitialised(false) {
59  add(x1, y1);
60  add(x2, y2);
61 }
62 
63 
65  : myXmin(10000000000.0), myXmax(-10000000000.0),
66  myYmin(10000000000.0), myYmax(-10000000000.0),
67  myZmin(10000000000.0), myZmax(-10000000000.0),
68  myWasInitialised(false) {
69  add(x1, y1, z1);
70  add(x2, y2, z2);
71 }
72 
73 
75 
76 
77 void
79  myXmin = 10000000000.0;
80  myXmax = -10000000000.0;
81  myYmin = 10000000000.0;
82  myYmax = -10000000000.0;
83  myZmin = 10000000000.0;
84  myZmax = -10000000000.0;
85  myWasInitialised = false;
86 }
87 
88 
89 void
91  if (!myWasInitialised) {
92  myYmin = y;
93  myYmax = y;
94  myXmin = x;
95  myXmax = x;
96  myZmin = z;
97  myZmax = z;
98  } else {
99  myXmin = myXmin < x ? myXmin : x;
100  myXmax = myXmax > x ? myXmax : x;
101  myYmin = myYmin < y ? myYmin : y;
102  myYmax = myYmax > y ? myYmax : y;
103  myZmin = myZmin < z ? myZmin : z;
104  myZmax = myZmax > z ? myZmax : z;
105  }
106  myWasInitialised = true;
107 }
108 
109 
110 void
112  add(p.x(), p.y(), p.z());
113 }
114 
115 
116 void
118  add(p.xmin(), p.ymin(), p.zmin());
119  add(p.xmax(), p.ymax(), p.zmax());
120 }
121 
122 
123 Position
125  return Position((myXmin + myXmax) / (SUMOReal) 2.0, (myYmin + myYmax) / (SUMOReal) 2.0, (myZmin + myZmax) / (SUMOReal) 2.0);
126 }
127 
128 
129 SUMOReal
130 Boundary::xmin() const {
131  return myXmin;
132 }
133 
134 
135 SUMOReal
136 Boundary::xmax() const {
137  return myXmax;
138 }
139 
140 
141 SUMOReal
142 Boundary::ymin() const {
143  return myYmin;
144 }
145 
146 
147 SUMOReal
148 Boundary::ymax() const {
149  return myYmax;
150 }
151 
152 
153 SUMOReal
154 Boundary::zmin() const {
155  return myZmin;
156 }
157 
158 
159 SUMOReal
160 Boundary::zmax() const {
161  return myZmax;
162 }
163 
164 
165 SUMOReal
167  return myXmax - myXmin;
168 }
169 
170 
171 SUMOReal
173  return myYmax - myYmin;
174 }
175 
176 
177 bool
178 Boundary::around(const Position& p, SUMOReal offset) const {
179  return
180  (p.x() <= myXmax + offset && p.x() >= myXmin - offset) &&
181  (p.y() <= myYmax + offset && p.y() >= myYmin - offset) &&
182  (p.z() <= myZmax + offset && p.z() >= myZmin - offset);
183 }
184 
185 
186 bool
188  if (
189  // check whether one of my points lies within the given poly
190  partialWithin(p, offset) ||
191  // check whether the polygon lies within me
192  p.partialWithin(*this, offset)) {
193  return true;
194  }
195  // check whether the bounderies cross
196  return
197  p.crosses(Position(myXmax + offset, myYmax + offset), Position(myXmin - offset, myYmax + offset))
198  ||
199  p.crosses(Position(myXmin - offset, myYmax + offset), Position(myXmin - offset, myYmin - offset))
200  ||
201  p.crosses(Position(myXmin - offset, myYmin - offset), Position(myXmax + offset, myYmin - offset))
202  ||
203  p.crosses(Position(myXmax + offset, myYmin - offset), Position(myXmax + offset, myYmax + offset));
204 }
205 
206 
207 bool
208 Boundary::crosses(const Position& p1, const Position& p2) const {
209  const PositionVector line(p1, p2);
210  return
212  ||
214  ||
216  ||
218 }
219 
220 
221 bool
222 Boundary::partialWithin(const AbstractPoly& poly, SUMOReal offset) const {
223  return
224  poly.around(Position(myXmax, myYmax), offset) ||
225  poly.around(Position(myXmin, myYmax), offset) ||
226  poly.around(Position(myXmax, myYmin), offset) ||
227  poly.around(Position(myXmin, myYmin), offset);
228 }
229 
230 
231 Boundary&
233  myXmax += by;
234  myYmax += by;
235  myXmin -= by;
236  myYmin -= by;
237  return *this;
238 }
239 
240 void
242  myXmin -= by;
243  myXmax += by;
244 }
245 
246 
247 void
249  myYmin -= by;
250  myYmax += by;
251 }
252 
253 void
255  myYmin *= -1.0;
256  myYmax *= -1.0;
257  SUMOReal tmp = myYmin;
258  myYmin = myYmax;
259  myYmax = tmp;
260 }
261 
262 
263 
264 std::ostream&
265 operator<<(std::ostream& os, const Boundary& b) {
266  os << b.myXmin << "," << b.myYmin << "," << b.myXmax << "," << b.myYmax;
267  return os;
268 }
269 
270 
271 void
273  myXmin = xmin;
274  myYmin = ymin;
275  myXmax = xmax;
276  myYmax = ymax;
277 }
278 
279 
280 void
282  myXmin += x;
283  myYmin += y;
284  myZmin += z;
285  myXmax += x;
286  myYmax += y;
287  myZmax += z;
288 }
289 
290 
291 
292 /****************************************************************************/
293 
SUMOReal myXmin
The boundaries.
Definition: Boundary.h:139
void growWidth(SUMOReal by)
Increases the width of the boundary (x-axis)
Definition: Boundary.cpp:241
SUMOReal myXmax
Definition: Boundary.h:139
SUMOReal getHeight() const
Returns the height of the boundary (y-axis)
Definition: Boundary.cpp:172
bool overlapsWith(const AbstractPoly &poly, SUMOReal offset=0) const
Returns whether the boundary overlaps with the given polygon.
Definition: Boundary.cpp:187
virtual bool partialWithin(const AbstractPoly &poly, SUMOReal offset=0) const =0
bool crosses(const Position &p1, const Position &p2) const
Returns whether the boundary crosses the given line.
Definition: Boundary.cpp:208
~Boundary()
Destructor.
Definition: Boundary.cpp:74
virtual bool crosses(const Position &p1, const Position &p2) const =0
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:142
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:148
SUMOReal getWidth() const
Returns the width of the boudary (x-axis)
Definition: Boundary.cpp:166
SUMOReal myYmin
Definition: Boundary.h:139
friend std::ostream & operator<<(std::ostream &os, const Boundary &b)
Output operator.
Definition: Boundary.cpp:265
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
SUMOReal xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:130
SUMOReal myYmax
Definition: Boundary.h:139
void set(SUMOReal xmin, SUMOReal ymin, SUMOReal xmax, SUMOReal ymax)
Sets the boundary to the given values.
Definition: Boundary.cpp:272
SUMOReal zmax() const
Returns maximum z-coordinate.
Definition: Boundary.cpp:160
SUMOReal z() const
Returns the z-position.
Definition: Position.h:73
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
SUMOReal myZmin
Definition: Boundary.h:139
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
bool myWasInitialised
Information whether the boundary was initialised.
Definition: Boundary.h:142
virtual bool around(const Position &p, SUMOReal offset=0) const =0
void reset()
Resets the boundary.
Definition: Boundary.cpp:78
Boundary & grow(SUMOReal by)
extends the boundary by the given amount
Definition: Boundary.cpp:232
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:136
void add(SUMOReal x, SUMOReal y, SUMOReal z=0)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:90
void flipY()
flips ymin and ymax
Definition: Boundary.cpp:254
bool partialWithin(const AbstractPoly &poly, SUMOReal offset=0) const
Returns whether the boundary is partially within the given polygon.
Definition: Boundary.cpp:222
SUMOReal myZmax
Definition: Boundary.h:139
bool around(const Position &p, SUMOReal offset=0) const
Returns whether the boundary contains the given coordinate.
Definition: Boundary.cpp:178
SUMOReal zmin() const
Returns minimum z-coordinate.
Definition: Boundary.cpp:154
Position getCenter() const
Returns the center of the boundary.
Definition: Boundary.cpp:124
#define SUMOReal
Definition: config.h:213
void growHeight(SUMOReal by)
Increases the height of the boundary (y-axis)
Definition: Boundary.cpp:248
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
void moveby(SUMOReal x, SUMOReal y, SUMOReal z=0)
Moves the boundary by the given amount.
Definition: Boundary.cpp:281
Boundary()
Constructor - the boundary is unset.
Definition: Boundary.cpp:47
bool intersects(const Position &p1, const Position &p2) const
Returns the information whether this list of points interesects the given line.