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 /****************************************************************************/
16 // A class that stores the 2D geometrical boundary
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 #include <utility>
25 
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  myXmax += by;
302  myYmax += by;
303  myXmin -= by;
304  myYmin -= by;
305  return *this;
306 }
307 
308 void
310  myXmin -= by;
311  myXmax += by;
312 }
313 
314 
315 void
317  myYmin -= by;
318  myYmax += by;
319 }
320 
321 void
323  myYmin *= -1.0;
324  myYmax *= -1.0;
325  double tmp = myYmin;
326  myYmin = myYmax;
327  myYmax = tmp;
328 }
329 
330 
331 
332 std::ostream&
333 operator<<(std::ostream& os, const Boundary& b) {
334  os << b.myXmin << "," << b.myYmin << "," << b.myXmax << "," << b.myYmax;
335  return os;
336 }
337 
338 
339 bool
341  return (
342  myXmin == b.myXmin &&
343  myXmax == b.myXmax &&
344  myYmin == b.myYmin &&
345  myYmax == b.myYmax &&
346  myZmin == b.myZmin &&
347  myZmax == b.myZmax &&
349 }
350 
351 
352 bool
354  return !(*this == b);
355 }
356 
357 
358 void
359 Boundary::set(double xmin, double ymin, double xmax, double ymax) {
360  myXmin = xmin;
361  myYmin = ymin;
362  myXmax = xmax;
363  myYmax = ymax;
364 }
365 
366 
367 void
368 Boundary::moveby(double x, double y, double z) {
369  myXmin += x;
370  myYmin += y;
371  myZmin += z;
372  myXmax += x;
373  myYmax += y;
374  myZmax += z;
375 }
376 
377 
378 
379 /****************************************************************************/
380 
AbstractPoly
Definition: AbstractPoly.h:35
Boundary.h
Boundary::moveby
void moveby(double x, double y, double z=0)
Moves the boundary by the given amount.
Definition: Boundary.cpp:368
Boundary::getZRange
double getZRange() const
Returns the elevation range of the boundary (z-axis)
Definition: Boundary.cpp:166
Boundary::myXmax
double myXmax
Definition: Boundary.h:152
Boundary::zmax
double zmax() const
Returns maximum z-coordinate.
Definition: Boundary.cpp:148
Boundary::ymin
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:130
Boundary::distanceTo2D
double distanceTo2D(const Position &p) const
returns the euclidean distance in the x-y-plane
Definition: Boundary.cpp:222
Boundary::myYmin
double myYmin
Definition: Boundary.h:152
Position::z
double z() const
Returns the z-position.
Definition: Position.h:66
Boundary::myYmax
double myYmax
Definition: Boundary.h:152
Boundary::myZmax
double myZmax
Definition: Boundary.h:152
Boundary::xmax
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:124
Boundary::partialWithin
bool partialWithin(const AbstractPoly &poly, double offset=0) const
Returns whether the boundary is partially within the given polygon.
Definition: Boundary.cpp:290
Boundary::getHeight
double getHeight() const
Returns the height of the boundary (y-axis)
Definition: Boundary.cpp:160
Boundary::around
bool around(const Position &p, double offset=0) const
Returns whether the AbstractPoly the given coordinate.
Definition: Boundary.cpp:172
PositionVector
A list of positions.
Definition: PositionVector.h:45
operator<<
std::ostream & operator<<(std::ostream &os, const Boundary &b)
Definition: Boundary.cpp:333
Boundary::myZmin
double myZmin
Definition: Boundary.h:152
Boundary::reset
void reset()
Resets the boundary.
Definition: Boundary.cpp:66
Boundary::set
void set(double xmin, double ymin, double xmax, double ymax)
Sets the boundary to the given values.
Definition: Boundary.cpp:359
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:159
Boundary::xmin
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:118
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:41
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:154
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
Position::x
double x() const
Returns the x-position.
Definition: Position.h:56
Boundary::add
void add(double x, double y, double z=0)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:78
Boundary::crosses
bool crosses(const Position &p1, const Position &p2) const
Returns whether the boundary crosses the given line.
Definition: Boundary.cpp:202
Boundary::growWidth
void growWidth(double by)
Increases the width of the boundary (x-axis)
Definition: Boundary.cpp:309
Boundary::myXmin
double myXmin
The boundaries.
Definition: Boundary.h:152
Position.h
Boundary::myWasInitialised
bool myWasInitialised
Information whether the boundary was initialised.
Definition: Boundary.h:155
Position::y
double y() const
Returns the y-position.
Definition: Position.h:61
Boundary::operator!=
bool operator!=(const Boundary &b) const
Comparison operator not equal.
Definition: Boundary.cpp:353
Boundary::getCenter
Position getCenter() const
Returns the center of the boundary.
Definition: Boundary.cpp:112
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:340
config.h
Boundary::isInitialised
bool isInitialised() const
check if Boundary is Initialised
Definition: Boundary.cpp:216
GeomHelper.h
Boundary::grow
Boundary & grow(double by)
extends the boundary by the given amount
Definition: Boundary.cpp:300
Boundary::Boundary
Boundary()
Constructor - the boundary is unset.
Definition: Boundary.cpp:35
Boundary::~Boundary
~Boundary()
Destructor.
Definition: Boundary.cpp:62
Boundary::flipY
void flipY()
flips ymin and ymax
Definition: Boundary.cpp:322
PositionVector.h
Boundary::zmin
double zmin() const
Returns minimum z-coordinate.
Definition: Boundary.cpp:142
Boundary::growHeight
void growHeight(double by)
Increases the height of the boundary (y-axis)
Definition: Boundary.cpp:316
Boundary::ymax
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:136
Boundary::overlapsWith
bool overlapsWith(const AbstractPoly &poly, double offset=0) const
Returns whether the boundary overlaps with the given polygon.
Definition: Boundary.cpp:181