Eclipse SUMO - Simulation of Urban MObility
Boundary.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2022 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 
25 #include <utils/common/StdDefs.h>
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 
42 Boundary::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 
52 Boundary::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 
65 void
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 
77 void
78 Boundary::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 
98 void
100  add(p.x(), p.y(), p.z());
101 }
102 
103 
104 void
106  add(p.xmin(), p.ymin(), p.zmin());
107  add(p.xmax(), p.ymax(), p.zmax());
108 }
109 
110 
111 Position
113  return Position((myXmin + myXmax) / (double) 2.0, (myYmin + myYmax) / (double) 2.0, (myZmin + myZmax) / (double) 2.0);
114 }
115 
116 
117 double
118 Boundary::xmin() const {
119  return myXmin;
120 }
121 
122 
123 double
124 Boundary::xmax() const {
125  return myXmax;
126 }
127 
128 
129 double
130 Boundary::ymin() const {
131  return myYmin;
132 }
133 
134 
135 double
136 Boundary::ymax() const {
137  return myYmax;
138 }
139 
140 
141 double
142 Boundary::zmin() const {
143  return myZmin;
144 }
145 
146 
147 double
148 Boundary::zmax() const {
149  return myZmax;
150 }
151 
152 
153 double
155  return myXmax - myXmin;
156 }
157 
158 
159 double
161  return myYmax - myYmin;
162 }
163 
164 
165 double
167  return myZmax - myZmin;
168 }
169 
170 
171 bool
172 Boundary::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 
180 bool
181 Boundary::overlapsWith(const AbstractPoly& p, double offset) const {
182  if (
183  // check whether one of my points lies within the given poly
184  partialWithin(p, offset) ||
185  // check whether the polygon lies within me
186  p.partialWithin(*this, offset)) {
187  return true;
188  }
189  // check whether the bounderies cross
190  return
191  p.crosses(Position(myXmax + offset, myYmax + offset), Position(myXmin - offset, myYmax + offset))
192  ||
193  p.crosses(Position(myXmin - offset, myYmax + offset), Position(myXmin - offset, myYmin - offset))
194  ||
195  p.crosses(Position(myXmin - offset, myYmin - offset), Position(myXmax + offset, myYmin - offset))
196  ||
197  p.crosses(Position(myXmax + offset, myYmin - offset), Position(myXmax + offset, myYmax + offset));
198 }
199 
200 
201 bool
202 Boundary::crosses(const Position& p1, const Position& p2) const {
203  const PositionVector line(p1, p2);
204  return
206  ||
208  ||
210  ||
212 }
213 
214 
215 bool
217  return myWasInitialised;
218 }
219 
220 
221 double
223  const double leftDist = myXmin - p.x();
224  const double rightDist = p.x() - myXmax;
225  const double bottomDist = myYmin - p.y();
226  const double topDist = p.y() - myYmax;
227  if (leftDist > 0.) {
228  if (bottomDist > 0.) {
229  return sqrt(leftDist * leftDist + bottomDist * bottomDist);
230  }
231  if (topDist > 0.) {
232  return sqrt(leftDist * leftDist + topDist * topDist);
233  }
234  return leftDist;
235  }
236  if (rightDist > 0.) {
237  if (bottomDist > 0.) {
238  return sqrt(rightDist * rightDist + bottomDist * bottomDist);
239  }
240  if (topDist > 0.) {
241  return sqrt(rightDist * rightDist + topDist * topDist);
242  }
243  return rightDist;
244  }
245  if (bottomDist > 0) {
246  return bottomDist;
247  }
248  if (topDist > 0) {
249  return topDist;
250  }
251  return 0.;
252 }
253 
254 
255 double
257  const double leftDist = myXmin - b.myXmax;
258  const double rightDist = b.myXmin - myXmax;
259  const double bottomDist = myYmin - b.myYmax;
260  const double topDist = b.myYmin - myYmax;
261  if (leftDist > 0.) {
262  if (bottomDist > 0.) {
263  return sqrt(leftDist * leftDist + bottomDist * bottomDist);
264  }
265  if (topDist > 0.) {
266  return sqrt(leftDist * leftDist + topDist * topDist);
267  }
268  return leftDist;
269  }
270  if (rightDist > 0.) {
271  if (bottomDist > 0.) {
272  return sqrt(rightDist * rightDist + bottomDist * bottomDist);
273  }
274  if (topDist > 0.) {
275  return sqrt(rightDist * rightDist + topDist * topDist);
276  }
277  return rightDist;
278  }
279  if (bottomDist > 0) {
280  return bottomDist;
281  }
282  if (topDist > 0) {
283  return topDist;
284  }
285  return 0.;
286 }
287 
288 
289 bool
290 Boundary::partialWithin(const AbstractPoly& poly, double offset) const {
291  return
292  poly.around(Position(myXmax, myYmax), offset) ||
293  poly.around(Position(myXmin, myYmax), offset) ||
294  poly.around(Position(myXmax, myYmin), offset) ||
295  poly.around(Position(myXmin, myYmin), offset);
296 }
297 
298 
299 Boundary&
300 Boundary::grow(double by) {
301 
302  myXmax += by;
303  myYmax += by;
304  myXmin -= by;
305  myYmin -= by;
306  return *this;
307 }
308 
309 
310 Boundary&
311 Boundary::scale(double by) {
312  growWidth(by * (myXmax - myXmin));
313  growHeight(by * (myYmax - myYmin));
314  return *this;
315 }
316 
317 
318 void
320  myXmin -= by;
321  myXmax += by;
322 }
323 
324 
325 void
327  myYmin -= by;
328  myYmax += by;
329 }
330 
331 void
333  myYmin *= -1.0;
334  myYmax *= -1.0;
335  double tmp = myYmin;
336  myYmin = myYmax;
337  myYmax = tmp;
338 }
339 
340 
341 
342 std::ostream&
343 operator<<(std::ostream& os, const Boundary& b) {
344  os << b.myXmin << "," << b.myYmin << "," << b.myXmax << "," << b.myYmax;
345  return os;
346 }
347 
348 
349 bool
351  return (
352  myXmin == b.myXmin &&
353  myXmax == b.myXmax &&
354  myYmin == b.myYmin &&
355  myYmax == b.myYmax &&
356  myZmin == b.myZmin &&
357  myZmax == b.myZmax &&
359 }
360 
361 
362 bool
364  return !(*this == b);
365 }
366 
367 
368 void
369 Boundary::set(double xmin, double ymin, double xmax, double ymax) {
370  /*
371  Takes care of the following extraneous cases w.r.t the input parameters:
372  - xmin > xmax
373  - ymin > ymax
374  */
375 
376  myXmin = MIN2(xmin, xmax);
377  myYmin = MIN2(ymin, ymax);
378  myXmax = MAX2(xmin, xmax);
379  myYmax = MAX2(ymin, ymax);
380 }
381 
382 
383 void
384 Boundary::setOffsets(double xmin, double ymin, double xmax, double ymax) {
385  myXmin = xmin;
386  myYmin = ymin;
387  myXmax = xmax;
388  myYmax = ymax;
389 }
390 
391 
392 void
393 Boundary::moveby(double x, double y, double z) {
394  myXmin += x;
395  myYmin += y;
396  myZmin += z;
397  myXmax += x;
398  myYmax += y;
399  myZmax += z;
400 }
401 
402 
404 Boundary::getShape(const bool closeShape) const {
405  PositionVector shape;
406  shape.push_back(Position(myXmin, myYmin));
407  shape.push_back(Position(myXmin, myYmax));
408  shape.push_back(Position(myXmax, myYmax));
409  shape.push_back(Position(myXmax, myYmin));
410  if (closeShape) {
411  shape.push_back(Position(myXmin, myYmin));
412  }
413  return shape;
414 }
415 
416 /****************************************************************************/
std::ostream & operator<<(std::ostream &os, const Boundary &b)
Definition: Boundary.cpp:343
T MIN2(T a, T b)
Definition: StdDefs.h:71
T MAX2(T a, T b)
Definition: StdDefs.h:77
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:290
double myZmin
Definition: Boundary.h:161
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:393
void growHeight(double by)
Increases the height of the boundary (y-axis)
Definition: Boundary.cpp:326
bool isInitialised() const
check if Boundary is Initialised
Definition: Boundary.cpp:216
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:130
double myZmax
Definition: Boundary.h:161
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:300
void flipY()
flips ymin and ymax
Definition: Boundary.cpp:332
double distanceTo2D(const Position &p) const
returns the euclidean distance in the x-y-plane
Definition: Boundary.cpp:222
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:164
bool overlapsWith(const AbstractPoly &poly, double offset=0) const
Returns whether the boundary overlaps with the given polygon.
Definition: Boundary.cpp:181
Boundary()
Constructor - the boundary is unset.
Definition: Boundary.cpp:35
~Boundary()
Destructor.
Definition: Boundary.cpp:62
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:363
PositionVector getShape(const bool closeShape) const
get position vector (shape) based on this boundary
Definition: Boundary.cpp:404
double myYmin
Definition: Boundary.h:161
void set(double xmin, double ymin, double xmax, double ymax)
Sets the boundary to the given values.
Definition: Boundary.cpp:369
Boundary & scale(double by)
scale the boundary by the given amount
Definition: Boundary.cpp:311
double zmin() const
Returns minimum z-coordinate.
Definition: Boundary.cpp:142
void growWidth(double by)
Increases the width of the boundary (x-axis)
Definition: Boundary.cpp:319
double myYmax
Definition: Boundary.h:161
bool crosses(const Position &p1, const Position &p2) const
Returns whether the boundary crosses the given line.
Definition: Boundary.cpp:202
bool around(const Position &p, double offset=0) const
Returns whether the AbstractPoly 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:384
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:136
double myXmin
The boundaries.
Definition: Boundary.h:161
double myXmax
Definition: Boundary.h:161
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 operator==(const Boundary &b) const
Comparison operator equal.
Definition: Boundary.cpp:350
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.