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