Eclipse SUMO - Simulation of Urban MObility
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Boundary.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
20// A class that stores the 2D geometrical boundary
21/****************************************************************************/
22#include <config.h>
23#include <utility>
24
26#include "GeomHelper.h"
27#include "Boundary.h"
28#include "PositionVector.h"
29#include "Position.h"
30
31
32// ===========================================================================
33// method definitions
34// ===========================================================================
36 : myXmin(10000000000.0), myXmax(-10000000000.0),
37 myYmin(10000000000.0), myYmax(-10000000000.0),
38 myZmin(10000000000.0), myZmax(-10000000000.0),
39 myWasInitialised(false) {}
40
41
42Boundary::Boundary(double x1, double y1, double x2, double y2)
43 : myXmin(10000000000.0), myXmax(-10000000000.0),
44 myYmin(10000000000.0), myYmax(-10000000000.0),
45 myZmin(10000000000.0), myZmax(-10000000000.0),
46 myWasInitialised(false) {
47 add(x1, y1);
48 add(x2, y2);
49}
50
51
52Boundary::Boundary(double x1, double y1, double z1, double x2, double y2, double z2)
53 : myXmin(10000000000.0), myXmax(-10000000000.0),
54 myYmin(10000000000.0), myYmax(-10000000000.0),
55 myZmin(10000000000.0), myZmax(-10000000000.0),
56 myWasInitialised(false) {
57 add(x1, y1, z1);
58 add(x2, y2, z2);
59}
60
61
62void
64 myXmin = 10000000000.0;
65 myXmax = -10000000000.0;
66 myYmin = 10000000000.0;
67 myYmax = -10000000000.0;
68 myZmin = 10000000000.0;
69 myZmax = -10000000000.0;
70 myWasInitialised = false;
71}
72
73
74void
75Boundary::add(double x, double y, double z) {
76 if (!myWasInitialised) {
77 myYmin = y;
78 myYmax = y;
79 myXmin = x;
80 myXmax = x;
81 myZmin = z;
82 myZmax = z;
83 } else {
84 myXmin = myXmin < x ? myXmin : x;
85 myXmax = myXmax > x ? myXmax : x;
86 myYmin = myYmin < y ? myYmin : y;
87 myYmax = myYmax > y ? myYmax : y;
88 myZmin = myZmin < z ? myZmin : z;
89 myZmax = myZmax > z ? myZmax : z;
90 }
91 myWasInitialised = true;
92}
93
94
95void
97 add(p.x(), p.y(), p.z());
98}
99
100
101void
103 add(p.xmin(), p.ymin(), p.zmin());
104 add(p.xmax(), p.ymax(), p.zmax());
105}
106
107
110 return Position((myXmin + myXmax) / (double) 2.0, (myYmin + myYmax) / (double) 2.0, (myZmin + myZmax) / (double) 2.0);
111}
112
113
114double
116 return myXmin;
117}
118
119
120double
122 return myXmax;
123}
124
125
126double
128 return myYmin;
129}
130
131
132double
134 return myYmax;
135}
136
137
138double
140 return myZmin;
141}
142
143
144double
146 return myZmax;
147}
148
149
150double
152 return myXmax - myXmin;
153}
154
155
156double
158 return myYmax - myYmin;
159}
160
161
162double
164 return myZmax - myZmin;
165}
166
167
168bool
169Boundary::around(const Position& p, double offset) const {
170 return
171 ((p.x() <= myXmax + offset) && (p.x() >= myXmin - offset)) &&
172 ((p.y() <= myYmax + offset) && (p.y() >= myYmin - offset)) &&
173 ((p.z() <= myZmax + offset) && (p.z() >= myZmin - offset));
174}
175
176
177bool
178Boundary::around2D(const Position& p, double offset) const {
179 return
180 ((p.x() <= myXmax + offset) && (p.x() >= myXmin - offset)) &&
181 ((p.y() <= myYmax + offset) && (p.y() >= myYmin - offset));
182}
183
184
185bool
186Boundary::around2D(const double x, const double y) const {
187 return
188 ((x <= myXmax) && (x >= myXmin)) &&
189 ((y <= myYmax) && (y >= myYmin));
190}
191
192
193bool
194Boundary::overlapsWith(const AbstractPoly& p, double offset) const {
195 if (
196 // check whether one of my points lies within the given poly
197 partialWithin(p, offset) ||
198 // check whether the polygon lies within me
199 p.partialWithin(*this, offset)) {
200 return true;
201 }
202 // check whether the bounderies cross
203 return
204 p.crosses(Position(myXmax + offset, myYmax + offset), Position(myXmin - offset, myYmax + offset))
205 ||
206 p.crosses(Position(myXmin - offset, myYmax + offset), Position(myXmin - offset, myYmin - offset))
207 ||
208 p.crosses(Position(myXmin - offset, myYmin - offset), Position(myXmax + offset, myYmin - offset))
209 ||
210 p.crosses(Position(myXmax + offset, myYmin - offset), Position(myXmax + offset, myYmax + offset));
211}
212
213
214bool
215Boundary::crosses(const Position& p1, const Position& p2) const {
216 const PositionVector line(p1, p2);
217 return
219 ||
221 ||
223 ||
225}
226
227
228bool
230 if ((myXmin <= b.xmin()) && (myYmin <= b.ymin()) &&
231 (myXmax >= b.xmax()) && (myYmax >= b.ymax())) {
232 return true;
233 } else {
234 return false;
235 }
236}
237
238
239bool
241 if (around2D(b.myXmin, b.myYmin)) {
242 return true;
243 } else if (around2D(b.myXmin, b.myYmax)) {
244 return true;
245 } else if (around2D(b.myXmax, b.myYmin)) {
246 return true;
247 } else if (around2D(b.myXmax, b.myYmax)) {
248 return true;
249 } else {
250 return false;
251 }
252}
253
254
255bool
257 return myWasInitialised;
258}
259
260
261double
263 const double leftDist = myXmin - p.x();
264 const double rightDist = p.x() - myXmax;
265 const double bottomDist = myYmin - p.y();
266 const double topDist = p.y() - myYmax;
267 if (leftDist > 0.) {
268 if (bottomDist > 0.) {
269 return sqrt(leftDist * leftDist + bottomDist * bottomDist);
270 }
271 if (topDist > 0.) {
272 return sqrt(leftDist * leftDist + topDist * topDist);
273 }
274 return leftDist;
275 }
276 if (rightDist > 0.) {
277 if (bottomDist > 0.) {
278 return sqrt(rightDist * rightDist + bottomDist * bottomDist);
279 }
280 if (topDist > 0.) {
281 return sqrt(rightDist * rightDist + topDist * topDist);
282 }
283 return rightDist;
284 }
285 if (bottomDist > 0) {
286 return bottomDist;
287 }
288 if (topDist > 0) {
289 return topDist;
290 }
291 return 0.;
292}
293
294
295double
297 const double leftDist = myXmin - b.myXmax;
298 const double rightDist = b.myXmin - myXmax;
299 const double bottomDist = myYmin - b.myYmax;
300 const double topDist = b.myYmin - myYmax;
301 if (leftDist > 0.) {
302 if (bottomDist > 0.) {
303 return sqrt(leftDist * leftDist + bottomDist * bottomDist);
304 }
305 if (topDist > 0.) {
306 return sqrt(leftDist * leftDist + topDist * topDist);
307 }
308 return leftDist;
309 }
310 if (rightDist > 0.) {
311 if (bottomDist > 0.) {
312 return sqrt(rightDist * rightDist + bottomDist * bottomDist);
313 }
314 if (topDist > 0.) {
315 return sqrt(rightDist * rightDist + topDist * topDist);
316 }
317 return rightDist;
318 }
319 if (bottomDist > 0) {
320 return bottomDist;
321 }
322 if (topDist > 0) {
323 return topDist;
324 }
325 return 0.;
326}
327
328
329bool
330Boundary::partialWithin(const AbstractPoly& poly, double offset) const {
331 return
332 poly.around(Position(myXmax, myYmax), offset) ||
333 poly.around(Position(myXmin, myYmax), offset) ||
334 poly.around(Position(myXmax, myYmin), offset) ||
335 poly.around(Position(myXmin, myYmin), offset);
336}
337
338
340Boundary::grow(double by) {
341
342 myXmax += by;
343 myYmax += by;
344 myXmin -= by;
345 myYmin -= by;
346 return *this;
347}
348
349
351Boundary::scale(double by) {
352 growWidth(by * (myXmax - myXmin));
353 growHeight(by * (myYmax - myYmin));
354 return *this;
355}
356
357
358void
360 myXmin -= by;
361 myXmax += by;
362}
363
364
365void
367 myYmin -= by;
368 myYmax += by;
369}
370
371void
373 myYmin *= -1.0;
374 myYmax *= -1.0;
375 double tmp = myYmin;
376 myYmin = myYmax;
377 myYmax = tmp;
378}
379
380
381
382std::ostream&
383operator<<(std::ostream& os, const Boundary& b) {
384 os << b.myXmin << "," << b.myYmin << "," << b.myXmax << "," << b.myYmax;
385 return os;
386}
387
388
389bool
391 return (
392 myXmin == b.myXmin &&
393 myXmax == b.myXmax &&
394 myYmin == b.myYmin &&
395 myYmax == b.myYmax &&
396 myZmin == b.myZmin &&
397 myZmax == b.myZmax &&
399}
400
401
402bool
404 return !(*this == b);
405}
406
407
408void
409Boundary::set(double xmin, double ymin, double xmax, double ymax) {
410 /*
411 Takes care of the following extraneous cases w.r.t the input parameters:
412 - xmin > xmax
413 - ymin > ymax
414 */
415
416 myXmin = MIN2(xmin, xmax);
417 myYmin = MIN2(ymin, ymax);
418 myXmax = MAX2(xmin, xmax);
419 myYmax = MAX2(ymin, ymax);
420}
421
422
423void
424Boundary::setOffsets(double xmin, double ymin, double xmax, double ymax) {
425 myXmin = xmin;
426 myYmin = ymin;
427 myXmax = xmax;
428 myYmax = ymax;
429}
430
431
432void
433Boundary::moveby(double x, double y, double z) {
434 myXmin += x;
435 myYmin += y;
436 myZmin += z;
437 myXmax += x;
438 myYmax += y;
439 myZmax += z;
440}
441
442
444Boundary::getShape(const bool closeShape) const {
445 PositionVector shape;
446 shape.push_back(Position(myXmin, myYmin));
447 shape.push_back(Position(myXmin, myYmax));
448 shape.push_back(Position(myXmax, myYmax));
449 shape.push_back(Position(myXmax, myYmin));
450 if (closeShape) {
451 shape.push_back(Position(myXmin, myYmin));
452 }
453 return shape;
454}
455
456/****************************************************************************/
std::ostream & operator<<(std::ostream &os, const Boundary &b)
Definition Boundary.cpp:383
T MIN2(T a, T b)
Definition StdDefs.h:76
T MAX2(T a, T b)
Definition StdDefs.h:82
virtual bool partialWithin(const AbstractPoly &poly, double offset=0) const =0
Returns whether the AbstractPoly is partially within the given polygon.
virtual bool crosses(const Position &p1, const Position &p2) const =0
Returns whether the AbstractPoly crosses the given line.
virtual bool around(const Position &p, double offset=0) const =0
Returns whether the AbstractPoly the given coordinate.
A class that stores a 2D geometrical boundary.
Definition Boundary.h:39
Position getCenter() const
Returns the center of the boundary.
Definition Boundary.cpp:109
bool partialWithin(const AbstractPoly &poly, double offset=0) const
Returns whether the boundary is partially within the given polygon.
Definition Boundary.cpp:330
double myZmin
Definition Boundary.h:172
void add(double x, double y, double z=0)
Makes the boundary include the given coordinate.
Definition Boundary.cpp:75
void moveby(double x, double y, double z=0)
Moves the boundary by the given amount.
Definition Boundary.cpp:433
void growHeight(double by)
Increases the height of the boundary (y-axis)
Definition Boundary.cpp:366
bool isInitialised() const
check if Boundary is Initialised
Definition Boundary.cpp:256
double ymin() const
Returns minimum y-coordinate.
Definition Boundary.cpp:127
double myZmax
Definition Boundary.h:172
void reset()
Resets the boundary.
Definition Boundary.cpp:63
double xmin() const
Returns minimum x-coordinate.
Definition Boundary.cpp:115
Boundary & grow(double by)
extends the boundary by the given amount
Definition Boundary.cpp:340
void flipY()
flips ymin and ymax
Definition Boundary.cpp:372
double distanceTo2D(const Position &p) const
returns the euclidean distance in the x-y-plane
Definition Boundary.cpp:262
double getHeight() const
Returns the height of the boundary (y-axis)
Definition Boundary.cpp:157
bool myWasInitialised
Information whether the boundary was initialised.
Definition Boundary.h:175
bool overlapsWith(const AbstractPoly &poly, double offset=0) const
Returns whether the boundary overlaps with the given polygon.
Definition Boundary.cpp:194
Boundary()
Constructor - the boundary is unset.
Definition Boundary.cpp:35
bool contains2D(const Boundary &b) const
return true if this boundary contains the given boundary (only X-Y)
Definition Boundary.cpp:229
double getWidth() const
Returns the width of the boudary (x-axis)
Definition Boundary.cpp:151
bool operator!=(const Boundary &b) const
Comparison operator not equal.
Definition Boundary.cpp:403
PositionVector getShape(const bool closeShape) const
get position vector (shape) based on this boundary
Definition Boundary.cpp:444
double myYmin
Definition Boundary.h:172
void set(double xmin, double ymin, double xmax, double ymax)
Sets the boundary to the given values.
Definition Boundary.cpp:409
Boundary & scale(double by)
scale the boundary by the given amount
Definition Boundary.cpp:351
double zmin() const
Returns minimum z-coordinate.
Definition Boundary.cpp:139
bool around2D(const Position &p, double offset=0) const
Returns whether the boundary contains the given 2D coordinate (position)
Definition Boundary.cpp:178
void growWidth(double by)
Increases the width of the boundary (x-axis)
Definition Boundary.cpp:359
double myYmax
Definition Boundary.h:172
bool crosses(const Position &p1, const Position &p2) const
Returns whether the boundary crosses the given line.
Definition Boundary.cpp:215
bool around(const Position &p, double offset=0) const
Returns whether the boundary contains the given coordinate.
Definition Boundary.cpp:169
void setOffsets(double xmin, double ymin, double xmax, double ymax)
Sets the boundary to the given values, ignoring min < max constraints.
Definition Boundary.cpp:424
double ymax() const
Returns maximum y-coordinate.
Definition Boundary.cpp:133
double myXmin
The boundaries.
Definition Boundary.h:172
double myXmax
Definition Boundary.h:172
double xmax() const
Returns maximum x-coordinate.
Definition Boundary.cpp:121
double zmax() const
Returns maximum z-coordinate.
Definition Boundary.cpp:145
double getZRange() const
Returns the elevation range of the boundary (z-axis)
Definition Boundary.cpp:163
bool overlaps2D(const Boundary &b) const
return true if at least one point of the given boundary is in boundary(only X-Y)
Definition Boundary.cpp:240
bool operator==(const Boundary &b) const
Comparison operator equal.
Definition Boundary.cpp:390
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
double x() const
Returns the x-position.
Definition Position.h:52
double z() const
Returns the z-position.
Definition Position.h:62
double y() const
Returns the y-position.
Definition Position.h:57
A list of positions.
bool intersects(const Position &p1, const Position &p2) const
Returns the information whether this list of points interesects the given line.