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-2020 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 "GeomHelper.h"
26 #include "Boundary.h"
27 #include "PositionVector.h"
28 #include "Position.h"
29 
30 
31 // ===========================================================================
32 // method definitions
33 // ===========================================================================
35  : myXmin(10000000000.0), myXmax(-10000000000.0),
36  myYmin(10000000000.0), myYmax(-10000000000.0),
37  myZmin(10000000000.0), myZmax(-10000000000.0),
38  myWasInitialised(false) {}
39 
40 
41 Boundary::Boundary(double x1, double y1, double x2, double y2)
42  : myXmin(10000000000.0), myXmax(-10000000000.0),
43  myYmin(10000000000.0), myYmax(-10000000000.0),
44  myZmin(10000000000.0), myZmax(-10000000000.0),
45  myWasInitialised(false) {
46  add(x1, y1);
47  add(x2, y2);
48 }
49 
50 
51 Boundary::Boundary(double x1, double y1, double z1, double x2, double y2, double z2)
52  : myXmin(10000000000.0), myXmax(-10000000000.0),
53  myYmin(10000000000.0), myYmax(-10000000000.0),
54  myZmin(10000000000.0), myZmax(-10000000000.0),
55  myWasInitialised(false) {
56  add(x1, y1, z1);
57  add(x2, y2, z2);
58 }
59 
60 
62 
63 
64 void
66  myXmin = 10000000000.0;
67  myXmax = -10000000000.0;
68  myYmin = 10000000000.0;
69  myYmax = -10000000000.0;
70  myZmin = 10000000000.0;
71  myZmax = -10000000000.0;
72  myWasInitialised = false;
73 }
74 
75 
76 void
77 Boundary::add(double x, double y, double z) {
78  if (!myWasInitialised) {
79  myYmin = y;
80  myYmax = y;
81  myXmin = x;
82  myXmax = x;
83  myZmin = z;
84  myZmax = z;
85  } else {
86  myXmin = myXmin < x ? myXmin : x;
87  myXmax = myXmax > x ? myXmax : x;
88  myYmin = myYmin < y ? myYmin : y;
89  myYmax = myYmax > y ? myYmax : y;
90  myZmin = myZmin < z ? myZmin : z;
91  myZmax = myZmax > z ? myZmax : z;
92  }
93  myWasInitialised = true;
94 }
95 
96 
97 void
99  add(p.x(), p.y(), p.z());
100 }
101 
102 
103 void
105  add(p.xmin(), p.ymin(), p.zmin());
106  add(p.xmax(), p.ymax(), p.zmax());
107 }
108 
109 
110 Position
112  return Position((myXmin + myXmax) / (double) 2.0, (myYmin + myYmax) / (double) 2.0, (myZmin + myZmax) / (double) 2.0);
113 }
114 
115 
116 double
117 Boundary::xmin() const {
118  return myXmin;
119 }
120 
121 
122 double
123 Boundary::xmax() const {
124  return myXmax;
125 }
126 
127 
128 double
129 Boundary::ymin() const {
130  return myYmin;
131 }
132 
133 
134 double
135 Boundary::ymax() const {
136  return myYmax;
137 }
138 
139 
140 double
141 Boundary::zmin() const {
142  return myZmin;
143 }
144 
145 
146 double
147 Boundary::zmax() const {
148  return myZmax;
149 }
150 
151 
152 double
154  return myXmax - myXmin;
155 }
156 
157 
158 double
160  return myYmax - myYmin;
161 }
162 
163 
164 double
166  return myZmax - myZmin;
167 }
168 
169 
170 bool
171 Boundary::around(const Position& p, double offset) const {
172  return
173  (p.x() <= myXmax + offset && p.x() >= myXmin - offset) &&
174  (p.y() <= myYmax + offset && p.y() >= myYmin - offset) &&
175  (p.z() <= myZmax + offset && p.z() >= myZmin - offset);
176 }
177 
178 
179 bool
180 Boundary::overlapsWith(const AbstractPoly& p, double offset) const {
181  if (
182  // check whether one of my points lies within the given poly
183  partialWithin(p, offset) ||
184  // check whether the polygon lies within me
185  p.partialWithin(*this, offset)) {
186  return true;
187  }
188  // check whether the bounderies cross
189  return
190  p.crosses(Position(myXmax + offset, myYmax + offset), Position(myXmin - offset, myYmax + offset))
191  ||
192  p.crosses(Position(myXmin - offset, myYmax + offset), Position(myXmin - offset, myYmin - offset))
193  ||
194  p.crosses(Position(myXmin - offset, myYmin - offset), Position(myXmax + offset, myYmin - offset))
195  ||
196  p.crosses(Position(myXmax + offset, myYmin - offset), Position(myXmax + offset, myYmax + offset));
197 }
198 
199 
200 bool
201 Boundary::crosses(const Position& p1, const Position& p2) const {
202  const PositionVector line(p1, p2);
203  return
205  ||
207  ||
209  ||
211 }
212 
213 
214 bool
216  return myWasInitialised;
217 }
218 
219 
220 double
222  const double leftDist = myXmin - p.x();
223  const double rightDist = p.x() - myXmax;
224  const double bottomDist = myYmin - p.y();
225  const double topDist = p.y() - myYmax;
226  if (leftDist > 0.) {
227  if (bottomDist > 0.) {
228  return sqrt(leftDist * leftDist + bottomDist * bottomDist);
229  }
230  if (topDist > 0.) {
231  return sqrt(leftDist * leftDist + topDist * topDist);
232  }
233  return leftDist;
234  }
235  if (rightDist > 0.) {
236  if (bottomDist > 0.) {
237  return sqrt(rightDist * rightDist + bottomDist * bottomDist);
238  }
239  if (topDist > 0.) {
240  return sqrt(rightDist * rightDist + topDist * topDist);
241  }
242  return rightDist;
243  }
244  if (bottomDist > 0) {
245  return bottomDist;
246  }
247  if (topDist > 0) {
248  return topDist;
249  }
250  return 0.;
251 }
252 
253 
254 double
256  const double leftDist = myXmin - b.myXmax;
257  const double rightDist = b.myXmin - myXmax;
258  const double bottomDist = myYmin - b.myYmax;
259  const double topDist = b.myYmin - myYmax;
260  if (leftDist > 0.) {
261  if (bottomDist > 0.) {
262  return sqrt(leftDist * leftDist + bottomDist * bottomDist);
263  }
264  if (topDist > 0.) {
265  return sqrt(leftDist * leftDist + topDist * topDist);
266  }
267  return leftDist;
268  }
269  if (rightDist > 0.) {
270  if (bottomDist > 0.) {
271  return sqrt(rightDist * rightDist + bottomDist * bottomDist);
272  }
273  if (topDist > 0.) {
274  return sqrt(rightDist * rightDist + topDist * topDist);
275  }
276  return rightDist;
277  }
278  if (bottomDist > 0) {
279  return bottomDist;
280  }
281  if (topDist > 0) {
282  return topDist;
283  }
284  return 0.;
285 }
286 
287 
288 bool
289 Boundary::partialWithin(const AbstractPoly& poly, double offset) const {
290  return
291  poly.around(Position(myXmax, myYmax), offset) ||
292  poly.around(Position(myXmin, myYmax), offset) ||
293  poly.around(Position(myXmax, myYmin), offset) ||
294  poly.around(Position(myXmin, myYmin), offset);
295 }
296 
297 
298 Boundary&
299 Boundary::grow(double by) {
300  myXmax += by;
301  myYmax += by;
302  myXmin -= by;
303  myYmin -= by;
304  return *this;
305 }
306 
307 void
309  myXmin -= by;
310  myXmax += by;
311 }
312 
313 
314 void
316  myYmin -= by;
317  myYmax += by;
318 }
319 
320 void
322  myYmin *= -1.0;
323  myYmax *= -1.0;
324  double tmp = myYmin;
325  myYmin = myYmax;
326  myYmax = tmp;
327 }
328 
329 
330 
331 std::ostream&
332 operator<<(std::ostream& os, const Boundary& b) {
333  os << b.myXmin << "," << b.myYmin << "," << b.myXmax << "," << b.myYmax;
334  return os;
335 }
336 
337 
338 bool
340  return (
341  myXmin == b.myXmin &&
342  myXmax == b.myXmax &&
343  myYmin == b.myYmin &&
344  myYmax == b.myYmax &&
345  myZmin == b.myZmin &&
346  myZmax == b.myZmax &&
348 }
349 
350 
351 bool
353  return !(*this == b);
354 }
355 
356 
357 void
358 Boundary::set(double xmin, double ymin, double xmax, double ymax) {
359  myXmin = xmin;
360  myYmin = ymin;
361  myXmax = xmax;
362  myYmax = ymax;
363 }
364 
365 
366 void
367 Boundary::moveby(double x, double y, double z) {
368  myXmin += x;
369  myYmin += y;
370  myZmin += z;
371  myXmax += x;
372  myYmax += y;
373  myZmax += z;
374 }
375 
376 
377 /****************************************************************************/
std::ostream & operator<<(std::ostream &os, const Boundary &b)
Definition: Boundary.cpp:332
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:111
bool partialWithin(const AbstractPoly &poly, double offset=0) const
Returns whether the boundary is partially within the given polygon.
Definition: Boundary.cpp:289
double myZmin
Definition: Boundary.h:150
void add(double x, double y, double z=0)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:77
void moveby(double x, double y, double z=0)
Moves the boundary by the given amount.
Definition: Boundary.cpp:367
void growHeight(double by)
Increases the height of the boundary (y-axis)
Definition: Boundary.cpp:315
bool isInitialised() const
check if Boundary is Initialised
Definition: Boundary.cpp:215
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:129
double myZmax
Definition: Boundary.h:150
void reset()
Resets the boundary.
Definition: Boundary.cpp:65
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:117
Boundary & grow(double by)
extends the boundary by the given amount
Definition: Boundary.cpp:299
void flipY()
flips ymin and ymax
Definition: Boundary.cpp:321
double distanceTo2D(const Position &p) const
returns the euclidean distance in the x-y-plane
Definition: Boundary.cpp:221
double getHeight() const
Returns the height of the boundary (y-axis)
Definition: Boundary.cpp:159
bool myWasInitialised
Information whether the boundary was initialised.
Definition: Boundary.h:153
bool overlapsWith(const AbstractPoly &poly, double offset=0) const
Returns whether the boundary overlaps with the given polygon.
Definition: Boundary.cpp:180
Boundary()
Constructor - the boundary is unset.
Definition: Boundary.cpp:34
~Boundary()
Destructor.
Definition: Boundary.cpp:61
double getWidth() const
Returns the width of the boudary (x-axis)
Definition: Boundary.cpp:153
bool operator!=(const Boundary &b) const
Comparison operator not equal.
Definition: Boundary.cpp:352
double myYmin
Definition: Boundary.h:150
void set(double xmin, double ymin, double xmax, double ymax)
Sets the boundary to the given values.
Definition: Boundary.cpp:358
double zmin() const
Returns minimum z-coordinate.
Definition: Boundary.cpp:141
void growWidth(double by)
Increases the width of the boundary (x-axis)
Definition: Boundary.cpp:308
double myYmax
Definition: Boundary.h:150
bool crosses(const Position &p1, const Position &p2) const
Returns whether the boundary crosses the given line.
Definition: Boundary.cpp:201
bool around(const Position &p, double offset=0) const
Returns whether the AbstractPoly the given coordinate.
Definition: Boundary.cpp:171
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:135
double myXmin
The boundaries.
Definition: Boundary.h:150
double myXmax
Definition: Boundary.h:150
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:123
double zmax() const
Returns maximum z-coordinate.
Definition: Boundary.cpp:147
double getZRange() const
Returns the elevation range of the boundary (z-axis)
Definition: Boundary.cpp:165
bool operator==(const Boundary &b) const
Comparison operator equal.
Definition: Boundary.cpp:339
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
double x() const
Returns the x-position.
Definition: Position.h:54
double z() const
Returns the z-position.
Definition: Position.h:64
double y() const
Returns the y-position.
Definition: Position.h:59
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.