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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // A class that stores the 2D geometrical boundary
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 #include <utility>
32 
33 #include "GeomHelper.h"
34 #include "Boundary.h"
35 #include "PositionVector.h"
36 #include "Position.h"
37 
38 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
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 
48 
49 Boundary::Boundary(double x1, double y1, double x2, double y2)
50  : myXmin(10000000000.0), myXmax(-10000000000.0),
51  myYmin(10000000000.0), myYmax(-10000000000.0),
52  myZmin(10000000000.0), myZmax(-10000000000.0),
53  myWasInitialised(false) {
54  add(x1, y1);
55  add(x2, y2);
56 }
57 
58 
59 Boundary::Boundary(double x1, double y1, double z1, double x2, double y2, double z2)
60  : myXmin(10000000000.0), myXmax(-10000000000.0),
61  myYmin(10000000000.0), myYmax(-10000000000.0),
62  myZmin(10000000000.0), myZmax(-10000000000.0),
63  myWasInitialised(false) {
64  add(x1, y1, z1);
65  add(x2, y2, z2);
66 }
67 
68 
70 
71 
72 void
74  myXmin = 10000000000.0;
75  myXmax = -10000000000.0;
76  myYmin = 10000000000.0;
77  myYmax = -10000000000.0;
78  myZmin = 10000000000.0;
79  myZmax = -10000000000.0;
80  myWasInitialised = false;
81 }
82 
83 
84 void
85 Boundary::add(double x, double y, double z) {
86  if (!myWasInitialised) {
87  myYmin = y;
88  myYmax = y;
89  myXmin = x;
90  myXmax = x;
91  myZmin = z;
92  myZmax = z;
93  } else {
94  myXmin = myXmin < x ? myXmin : x;
95  myXmax = myXmax > x ? myXmax : x;
96  myYmin = myYmin < y ? myYmin : y;
97  myYmax = myYmax > y ? myYmax : y;
98  myZmin = myZmin < z ? myZmin : z;
99  myZmax = myZmax > z ? myZmax : z;
100  }
101  myWasInitialised = true;
102 }
103 
104 
105 void
107  add(p.x(), p.y(), p.z());
108 }
109 
110 
111 void
113  add(p.xmin(), p.ymin(), p.zmin());
114  add(p.xmax(), p.ymax(), p.zmax());
115 }
116 
117 
118 Position
120  return Position((myXmin + myXmax) / (double) 2.0, (myYmin + myYmax) / (double) 2.0, (myZmin + myZmax) / (double) 2.0);
121 }
122 
123 
124 double
125 Boundary::xmin() const {
126  return myXmin;
127 }
128 
129 
130 double
131 Boundary::xmax() const {
132  return myXmax;
133 }
134 
135 
136 double
137 Boundary::ymin() const {
138  return myYmin;
139 }
140 
141 
142 double
143 Boundary::ymax() const {
144  return myYmax;
145 }
146 
147 
148 double
149 Boundary::zmin() const {
150  return myZmin;
151 }
152 
153 
154 double
155 Boundary::zmax() const {
156  return myZmax;
157 }
158 
159 
160 double
162  return myXmax - myXmin;
163 }
164 
165 
166 double
168  return myYmax - myYmin;
169 }
170 
171 
172 double
174  return myZmax - myZmin;
175 }
176 
177 
178 bool
179 Boundary::around(const Position& p, double offset) const {
180  return
181  (p.x() <= myXmax + offset && p.x() >= myXmin - offset) &&
182  (p.y() <= myYmax + offset && p.y() >= myYmin - offset) &&
183  (p.z() <= myZmax + offset && p.z() >= myZmin - offset);
184 }
185 
186 
187 bool
188 Boundary::overlapsWith(const AbstractPoly& p, double offset) const {
189  if (
190  // check whether one of my points lies within the given poly
191  partialWithin(p, offset) ||
192  // check whether the polygon lies within me
193  p.partialWithin(*this, offset)) {
194  return true;
195  }
196  // check whether the bounderies cross
197  return
198  p.crosses(Position(myXmax + offset, myYmax + offset), Position(myXmin - offset, myYmax + offset))
199  ||
200  p.crosses(Position(myXmin - offset, myYmax + offset), Position(myXmin - offset, myYmin - offset))
201  ||
202  p.crosses(Position(myXmin - offset, myYmin - offset), Position(myXmax + offset, myYmin - offset))
203  ||
204  p.crosses(Position(myXmax + offset, myYmin - offset), Position(myXmax + offset, myYmax + offset));
205 }
206 
207 
208 bool
209 Boundary::crosses(const Position& p1, const Position& p2) const {
210  const PositionVector line(p1, p2);
211  return
213  ||
215  ||
217  ||
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 void
341 Boundary::set(double xmin, double ymin, double xmax, double ymax) {
342  myXmin = xmin;
343  myYmin = ymin;
344  myXmax = xmax;
345  myYmax = ymax;
346 }
347 
348 
349 void
350 Boundary::moveby(double x, double y, double z) {
351  myXmin += x;
352  myYmin += y;
353  myZmin += z;
354  myXmax += x;
355  myYmax += y;
356  myZmax += z;
357 }
358 
359 
360 
361 /****************************************************************************/
362 
double distanceTo2D(const Position &p) const
returns the euclidean distance in the x-y-plane
Definition: Boundary.cpp:223
virtual bool partialWithin(const AbstractPoly &poly, double offset=0) const =0
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:137
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:131
double z() const
Returns the z-position.
Definition: Position.h:72
double y() const
Returns the y-position.
Definition: Position.h:67
bool crosses(const Position &p1, const Position &p2) const
Returns whether the boundary crosses the given line.
Definition: Boundary.cpp:209
void moveby(double x, double y, double z=0)
Moves the boundary by the given amount.
Definition: Boundary.cpp:350
double myXmax
Definition: Boundary.h:147
double x() const
Returns the x-position.
Definition: Position.h:62
~Boundary()
Destructor.
Definition: Boundary.cpp:69
virtual bool crosses(const Position &p1, const Position &p2) const =0
void set(double xmin, double ymin, double xmax, double ymax)
Sets the boundary to the given values.
Definition: Boundary.cpp:341
double zmax() const
Returns maximum z-coordinate.
Definition: Boundary.cpp:155
double getWidth() const
Returns the width of the boudary (x-axis)
Definition: Boundary.cpp:161
double myZmax
Definition: Boundary.h:147
double myYmin
Definition: Boundary.h:147
double myYmax
Definition: Boundary.h:147
bool overlapsWith(const AbstractPoly &poly, double offset=0) const
Returns whether the boundary overlaps with the given polygon.
Definition: Boundary.cpp:188
friend std::ostream & operator<<(std::ostream &os, const Boundary &b)
Output operator.
Definition: Boundary.cpp:334
double zmin() const
Returns minimum z-coordinate.
Definition: Boundary.cpp:149
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:47
double myXmin
The boundaries.
Definition: Boundary.h:147
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
A list of positions.
virtual bool around(const Position &p, double offset=0) const =0
double myZmin
Definition: Boundary.h:147
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:125
bool myWasInitialised
Information whether the boundary was initialised.
Definition: Boundary.h:150
Boundary & grow(double by)
extends the boundary by the given amount
Definition: Boundary.cpp:301
void growHeight(double by)
Increases the height of the boundary (y-axis)
Definition: Boundary.cpp:317
void reset()
Resets the boundary.
Definition: Boundary.cpp:73
bool partialWithin(const AbstractPoly &poly, double offset=0) const
Returns whether the boundary is partially within the given polygon.
Definition: Boundary.cpp:291
double getHeight() const
Returns the height of the boundary (y-axis)
Definition: Boundary.cpp:167
void flipY()
flips ymin and ymax
Definition: Boundary.cpp:323
bool around(const Position &p, double offset=0) const
Returns whether the boundary contains the given coordinate.
Definition: Boundary.cpp:179
void growWidth(double by)
Increases the width of the boundary (x-axis)
Definition: Boundary.cpp:310
Position getCenter() const
Returns the center of the boundary.
Definition: Boundary.cpp:119
void add(double x, double y, double z=0)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:85
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:143
double getZRange() const
Returns the elevation range of the boundary (z-axis)
Definition: Boundary.cpp:173
Boundary()
Constructor - the boundary is unset.
Definition: Boundary.cpp:42
bool intersects(const Position &p1, const Position &p2) const
Returns the information whether this list of points interesects the given line.