Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2024 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
63
64
65void
67 myXmin = 10000000000.0;
68 myXmax = -10000000000.0;
69 myYmin = 10000000000.0;
70 myYmax = -10000000000.0;
71 myZmin = 10000000000.0;
72 myZmax = -10000000000.0;
73 myWasInitialised = false;
74}
75
76
77void
78Boundary::add(double x, double y, double z) {
79 if (!myWasInitialised) {
80 myYmin = y;
81 myYmax = y;
82 myXmin = x;
83 myXmax = x;
84 myZmin = z;
85 myZmax = z;
86 } else {
87 myXmin = myXmin < x ? myXmin : x;
88 myXmax = myXmax > x ? myXmax : x;
89 myYmin = myYmin < y ? myYmin : y;
90 myYmax = myYmax > y ? myYmax : y;
91 myZmin = myZmin < z ? myZmin : z;
92 myZmax = myZmax > z ? myZmax : z;
93 }
94 myWasInitialised = true;
95}
96
97
98void
100 add(p.x(), p.y(), p.z());
101}
102
103
104void
106 add(p.xmin(), p.ymin(), p.zmin());
107 add(p.xmax(), p.ymax(), p.zmax());
108}
109
110
113 return Position((myXmin + myXmax) / (double) 2.0, (myYmin + myYmax) / (double) 2.0, (myZmin + myZmax) / (double) 2.0);
114}
115
116
117double
119 return myXmin;
120}
121
122
123double
125 return myXmax;
126}
127
128
129double
131 return myYmin;
132}
133
134
135double
137 return myYmax;
138}
139
140
141double
143 return myZmin;
144}
145
146
147double
149 return myZmax;
150}
151
152
153double
155 return myXmax - myXmin;
156}
157
158
159double
161 return myYmax - myYmin;
162}
163
164
165double
167 return myZmax - myZmin;
168}
169
170
171bool
172Boundary::around(const Position& p, double offset) const {
173 return
174 ((p.x() <= myXmax + offset) && (p.x() >= myXmin - offset)) &&
175 ((p.y() <= myYmax + offset) && (p.y() >= myYmin - offset)) &&
176 ((p.z() <= myZmax + offset) && (p.z() >= myZmin - offset));
177}
178
179
180bool
181Boundary::around2D(const Position& p, double offset) const {
182 return
183 ((p.x() <= myXmax + offset) && (p.x() >= myXmin - offset)) &&
184 ((p.y() <= myYmax + offset) && (p.y() >= myYmin - offset));
185}
186
187
188bool
189Boundary::around2D(const double x, const double y) const {
190 return
191 ((x <= myXmax) && (x >= myXmin)) &&
192 ((y <= myYmax) && (y >= myYmin));
193}
194
195
196bool
197Boundary::overlapsWith(const AbstractPoly& p, double offset) const {
198 if (
199 // check whether one of my points lies within the given poly
200 partialWithin(p, offset) ||
201 // check whether the polygon lies within me
202 p.partialWithin(*this, offset)) {
203 return true;
204 }
205 // check whether the bounderies cross
206 return
207 p.crosses(Position(myXmax + offset, myYmax + offset), Position(myXmin - offset, myYmax + offset))
208 ||
209 p.crosses(Position(myXmin - offset, myYmax + offset), Position(myXmin - offset, myYmin - offset))
210 ||
211 p.crosses(Position(myXmin - offset, myYmin - offset), Position(myXmax + offset, myYmin - offset))
212 ||
213 p.crosses(Position(myXmax + offset, myYmin - offset), Position(myXmax + offset, myYmax + offset));
214}
215
216
217bool
218Boundary::crosses(const Position& p1, const Position& p2) const {
219 const PositionVector line(p1, p2);
220 return
222 ||
224 ||
226 ||
228}
229
230
231bool
233 if ((myXmin <= b.xmin()) && (myYmin <= b.ymin()) &&
234 (myXmax >= b.xmax()) && (myYmax >= b.ymax())) {
235 return true;
236 } else {
237 return false;
238 }
239}
240
241
242bool
244 if (around2D(b.myXmin, b.myYmin)) {
245 return true;
246 } else if (around2D(b.myXmin, b.myYmax)) {
247 return true;
248 } else if (around2D(b.myXmax, b.myYmin)) {
249 return true;
250 } else if (around2D(b.myXmax, b.myYmax)) {
251 return true;
252 } else {
253 return false;
254 }
255}
256
257
258bool
260 return myWasInitialised;
261}
262
263
264double
266 const double leftDist = myXmin - p.x();
267 const double rightDist = p.x() - myXmax;
268 const double bottomDist = myYmin - p.y();
269 const double topDist = p.y() - myYmax;
270 if (leftDist > 0.) {
271 if (bottomDist > 0.) {
272 return sqrt(leftDist * leftDist + bottomDist * bottomDist);
273 }
274 if (topDist > 0.) {
275 return sqrt(leftDist * leftDist + topDist * topDist);
276 }
277 return leftDist;
278 }
279 if (rightDist > 0.) {
280 if (bottomDist > 0.) {
281 return sqrt(rightDist * rightDist + bottomDist * bottomDist);
282 }
283 if (topDist > 0.) {
284 return sqrt(rightDist * rightDist + topDist * topDist);
285 }
286 return rightDist;
287 }
288 if (bottomDist > 0) {
289 return bottomDist;
290 }
291 if (topDist > 0) {
292 return topDist;
293 }
294 return 0.;
295}
296
297
298double
300 const double leftDist = myXmin - b.myXmax;
301 const double rightDist = b.myXmin - myXmax;
302 const double bottomDist = myYmin - b.myYmax;
303 const double topDist = b.myYmin - myYmax;
304 if (leftDist > 0.) {
305 if (bottomDist > 0.) {
306 return sqrt(leftDist * leftDist + bottomDist * bottomDist);
307 }
308 if (topDist > 0.) {
309 return sqrt(leftDist * leftDist + topDist * topDist);
310 }
311 return leftDist;
312 }
313 if (rightDist > 0.) {
314 if (bottomDist > 0.) {
315 return sqrt(rightDist * rightDist + bottomDist * bottomDist);
316 }
317 if (topDist > 0.) {
318 return sqrt(rightDist * rightDist + topDist * topDist);
319 }
320 return rightDist;
321 }
322 if (bottomDist > 0) {
323 return bottomDist;
324 }
325 if (topDist > 0) {
326 return topDist;
327 }
328 return 0.;
329}
330
331
332bool
333Boundary::partialWithin(const AbstractPoly& poly, double offset) const {
334 return
335 poly.around(Position(myXmax, myYmax), offset) ||
336 poly.around(Position(myXmin, myYmax), offset) ||
337 poly.around(Position(myXmax, myYmin), offset) ||
338 poly.around(Position(myXmin, myYmin), offset);
339}
340
341
343Boundary::grow(double by) {
344
345 myXmax += by;
346 myYmax += by;
347 myXmin -= by;
348 myYmin -= by;
349 return *this;
350}
351
352
354Boundary::scale(double by) {
355 growWidth(by * (myXmax - myXmin));
356 growHeight(by * (myYmax - myYmin));
357 return *this;
358}
359
360
361void
363 myXmin -= by;
364 myXmax += by;
365}
366
367
368void
370 myYmin -= by;
371 myYmax += by;
372}
373
374void
376 myYmin *= -1.0;
377 myYmax *= -1.0;
378 double tmp = myYmin;
379 myYmin = myYmax;
380 myYmax = tmp;
381}
382
383
384
385std::ostream&
386operator<<(std::ostream& os, const Boundary& b) {
387 os << b.myXmin << "," << b.myYmin << "," << b.myXmax << "," << b.myYmax;
388 return os;
389}
390
391
392bool
394 return (
395 myXmin == b.myXmin &&
396 myXmax == b.myXmax &&
397 myYmin == b.myYmin &&
398 myYmax == b.myYmax &&
399 myZmin == b.myZmin &&
400 myZmax == b.myZmax &&
402}
403
404
405bool
407 return !(*this == b);
408}
409
410
411void
412Boundary::set(double xmin, double ymin, double xmax, double ymax) {
413 /*
414 Takes care of the following extraneous cases w.r.t the input parameters:
415 - xmin > xmax
416 - ymin > ymax
417 */
418
419 myXmin = MIN2(xmin, xmax);
420 myYmin = MIN2(ymin, ymax);
421 myXmax = MAX2(xmin, xmax);
422 myYmax = MAX2(ymin, ymax);
423}
424
425
426void
427Boundary::setOffsets(double xmin, double ymin, double xmax, double ymax) {
428 myXmin = xmin;
429 myYmin = ymin;
430 myXmax = xmax;
431 myYmax = ymax;
432}
433
434
435void
436Boundary::moveby(double x, double y, double z) {
437 myXmin += x;
438 myYmin += y;
439 myZmin += z;
440 myXmax += x;
441 myYmax += y;
442 myZmax += z;
443}
444
445
447Boundary::getShape(const bool closeShape) const {
448 PositionVector shape;
449 shape.push_back(Position(myXmin, myYmin));
450 shape.push_back(Position(myXmin, myYmax));
451 shape.push_back(Position(myXmax, myYmax));
452 shape.push_back(Position(myXmax, myYmin));
453 if (closeShape) {
454 shape.push_back(Position(myXmin, myYmin));
455 }
456 return shape;
457}
458
459/****************************************************************************/
std::ostream & operator<<(std::ostream &os, const Boundary &b)
Definition Boundary.cpp:386
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:112
bool partialWithin(const AbstractPoly &poly, double offset=0) const
Returns whether the boundary is partially within the given polygon.
Definition Boundary.cpp:333
double myZmin
Definition Boundary.h:175
void add(double x, double y, double z=0)
Makes the boundary include the given coordinate.
Definition Boundary.cpp:78
void moveby(double x, double y, double z=0)
Moves the boundary by the given amount.
Definition Boundary.cpp:436
void growHeight(double by)
Increases the height of the boundary (y-axis)
Definition Boundary.cpp:369
bool isInitialised() const
check if Boundary is Initialised
Definition Boundary.cpp:259
double ymin() const
Returns minimum y-coordinate.
Definition Boundary.cpp:130
double myZmax
Definition Boundary.h:175
void reset()
Resets the boundary.
Definition Boundary.cpp:66
double xmin() const
Returns minimum x-coordinate.
Definition Boundary.cpp:118
Boundary & grow(double by)
extends the boundary by the given amount
Definition Boundary.cpp:343
void flipY()
flips ymin and ymax
Definition Boundary.cpp:375
double distanceTo2D(const Position &p) const
returns the euclidean distance in the x-y-plane
Definition Boundary.cpp:265
double getHeight() const
Returns the height of the boundary (y-axis)
Definition Boundary.cpp:160
bool myWasInitialised
Information whether the boundary was initialised.
Definition Boundary.h:178
bool overlapsWith(const AbstractPoly &poly, double offset=0) const
Returns whether the boundary overlaps with the given polygon.
Definition Boundary.cpp:197
Boundary()
Constructor - the boundary is unset.
Definition Boundary.cpp:35
~Boundary()
Destructor.
Definition Boundary.cpp:62
bool contains2D(const Boundary &b) const
return true if this boundary contains the given boundary (only X-Y)
Definition Boundary.cpp:232
double getWidth() const
Returns the width of the boudary (x-axis)
Definition Boundary.cpp:154
bool operator!=(const Boundary &b) const
Comparison operator not equal.
Definition Boundary.cpp:406
PositionVector getShape(const bool closeShape) const
get position vector (shape) based on this boundary
Definition Boundary.cpp:447
double myYmin
Definition Boundary.h:175
void set(double xmin, double ymin, double xmax, double ymax)
Sets the boundary to the given values.
Definition Boundary.cpp:412
Boundary & scale(double by)
scale the boundary by the given amount
Definition Boundary.cpp:354
double zmin() const
Returns minimum z-coordinate.
Definition Boundary.cpp:142
bool around2D(const Position &p, double offset=0) const
Returns whether the boundary contains the given 2D coordinate (position)
Definition Boundary.cpp:181
void growWidth(double by)
Increases the width of the boundary (x-axis)
Definition Boundary.cpp:362
double myYmax
Definition Boundary.h:175
bool crosses(const Position &p1, const Position &p2) const
Returns whether the boundary crosses the given line.
Definition Boundary.cpp:218
bool around(const Position &p, double offset=0) const
Returns whether the boundary contains the given coordinate.
Definition Boundary.cpp:172
void setOffsets(double xmin, double ymin, double xmax, double ymax)
Sets the boundary to the given values, ignoring min < max constraints.
Definition Boundary.cpp:427
double ymax() const
Returns maximum y-coordinate.
Definition Boundary.cpp:136
double myXmin
The boundaries.
Definition Boundary.h:175
double myXmax
Definition Boundary.h:175
double xmax() const
Returns maximum x-coordinate.
Definition Boundary.cpp:124
double zmax() const
Returns maximum z-coordinate.
Definition Boundary.cpp:148
double getZRange() const
Returns the elevation range of the boundary (z-axis)
Definition Boundary.cpp:166
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:243
bool operator==(const Boundary &b) const
Comparison operator equal.
Definition Boundary.cpp:393
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:55
double z() const
Returns the z-position.
Definition Position.h:65
double y() const
Returns the y-position.
Definition Position.h:60
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.