SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Boundary.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A class that stores the 2D geometrical boundary
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 #include <utility>
33 
34 #include "GeomHelper.h"
35 #include "Boundary.h"
36 #include "PositionVector.h"
37 #include "Position.h"
38 
39 #ifdef CHECK_MEMORY_LEAKS
40 #include <foreign/nvwa/debug_new.h>
41 #endif // CHECK_MEMORY_LEAKS
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
48  : myXmin(10000000000.0), myXmax(-10000000000.0),
49  myYmin(10000000000.0), myYmax(-10000000000.0),
50  myWasInitialised(false) {}
51 
52 
54  : myXmin(10000000000.0), myXmax(-10000000000.0),
55  myYmin(10000000000.0), myYmax(-10000000000.0),
56  myWasInitialised(false) {
57  add(x1, y1);
58  add(x2, y2);
59 }
60 
61 
63 
64 
65 void
67  myXmin = 10000000000.0;
68  myXmax = -10000000000.0;
69  myYmin = 10000000000.0;
70  myYmax = -10000000000.0;
71  myWasInitialised = false;
72 }
73 
74 
75 void
77  if (!myWasInitialised) {
78  myYmin = y;
79  myYmax = y;
80  myXmin = x;
81  myXmax = x;
82  } else {
83  myXmin = myXmin < x ? myXmin : x;
84  myXmax = myXmax > x ? myXmax : x;
85  myYmin = myYmin < y ? myYmin : y;
86  myYmax = myYmax > y ? myYmax : y;
87  }
88  myWasInitialised = true;
89 }
90 
91 
92 void
94  add(p.x(), p.y());
95 }
96 
97 
98 void
100  add(p.xmin(), p.ymin());
101  add(p.xmax(), p.ymax());
102 }
103 
104 
105 Position
107  return Position((myXmin + myXmax) / (SUMOReal) 2.0, (myYmin + myYmax) / (SUMOReal) 2.0);
108 }
109 
110 
111 SUMOReal
112 Boundary::xmin() const {
113  return myXmin;
114 }
115 
116 
117 SUMOReal
118 Boundary::xmax() const {
119  return myXmax;
120 }
121 
122 
123 SUMOReal
124 Boundary::ymin() const {
125  return myYmin;
126 }
127 
128 
129 SUMOReal
130 Boundary::ymax() const {
131  return myYmax;
132 }
133 
134 
135 SUMOReal
137  return myXmax - myXmin;
138 }
139 
140 
141 SUMOReal
143  return myYmax - myYmin;
144 }
145 
146 
147 bool
148 Boundary::around(const Position& p, SUMOReal offset) const {
149  return
150  (p.x() <= myXmax + offset && p.x() >= myXmin - offset) &&
151  (p.y() <= myYmax + offset && p.y() >= myYmin - offset);
152 }
153 
154 
155 bool
157  if (
158  // check whether one of my points lies within the given poly
159  partialWithin(p, offset) ||
160  // check whether the polygon lies within me
161  p.partialWithin(*this, offset)) {
162  return true;
163  }
164  // check whether the bounderies cross
165  return
166  p.crosses(Position(myXmax + offset, myYmax + offset), Position(myXmin - offset, myYmax + offset))
167  ||
168  p.crosses(Position(myXmin - offset, myYmax + offset), Position(myXmin - offset, myYmin - offset))
169  ||
170  p.crosses(Position(myXmin - offset, myYmin - offset), Position(myXmax + offset, myYmin - offset))
171  ||
172  p.crosses(Position(myXmax + offset, myYmin - offset), Position(myXmax + offset, myYmax + offset));
173 }
174 
175 
176 bool
177 Boundary::crosses(const Position& p1, const Position& p2) const {
178  return
180  ||
182  ||
184  ||
186 }
187 
188 
189 bool
190 Boundary::partialWithin(const AbstractPoly& poly, SUMOReal offset) const {
191  return
192  poly.around(Position(myXmax, myYmax), offset) ||
193  poly.around(Position(myXmin, myYmax), offset) ||
194  poly.around(Position(myXmax, myYmin), offset) ||
195  poly.around(Position(myXmin, myYmin), offset);
196 }
197 
198 
199 Boundary&
201  myXmax += by;
202  myYmax += by;
203  myXmin -= by;
204  myYmin -= by;
205  return *this;
206 }
207 
208 void
210  myXmin -= by;
211  myXmax += by;
212 }
213 
214 
215 void
217  myYmin -= by;
218  myYmax += by;
219 }
220 
221 void
223  myYmin *= -1.0;
224  myYmax *= -1.0;
225  SUMOReal tmp = myYmin;
226  myYmin = myYmax;
227  myYmax = tmp;
228 }
229 
230 
231 
232 std::ostream&
233 operator<<(std::ostream& os, const Boundary& b) {
234  os << b.myXmin << "," << b.myYmin << "," << b.myXmax << "," << b.myYmax;
235  return os;
236 }
237 
238 
239 void
241  myXmin = xmin;
242  myYmin = ymin;
243  myXmax = xmax;
244  myYmax = ymax;
245 }
246 
247 
248 void
250  myXmin += x;
251  myYmin += y;
252  myXmax += x;
253  myYmax += y;
254 }
255 
256 
257 
258 /****************************************************************************/
259 
SUMOReal myXmin
The boundaries.
Definition: Boundary.h:129
SUMOReal getHeight() const
Returns the height of the boundary.
Definition: Boundary.cpp:142
void growWidth(SUMOReal by)
Definition: Boundary.cpp:209
SUMOReal myXmax
Definition: Boundary.h:129
SUMOReal getWidth() const
Returns the width of the boudary.
Definition: Boundary.cpp:136
Position getCenter() const
Returns the center of the boundary.
Definition: Boundary.cpp:106
bool around(const Position &p, SUMOReal offset=0) const
Returns whether the boundary contains the given coordinate.
Definition: Boundary.cpp:148
virtual bool partialWithin(const AbstractPoly &poly, SUMOReal offset=0) const =0
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:124
bool partialWithin(const AbstractPoly &poly, SUMOReal offset=0) const
Returns whether the boundary is partially within the given polygon.
Definition: Boundary.cpp:190
SUMOReal xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:112
~Boundary()
Destructor.
Definition: Boundary.cpp:62
virtual bool crosses(const Position &p1, const Position &p2) const =0
SUMOReal myYmin
Definition: Boundary.h:129
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:118
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
SUMOReal myYmax
Definition: Boundary.h:129
void set(SUMOReal xmin, SUMOReal ymin, SUMOReal xmax, SUMOReal ymax)
Sets the boundary to the given values.
Definition: Boundary.cpp:240
bool overlapsWith(const AbstractPoly &poly, SUMOReal offset=0) const
Returns whether the boundary overlaps with the given polygon.
Definition: Boundary.cpp:156
static bool intersects(const Position &p11, const Position &p12, const Position &p21, const Position &p22)
return whether the line segments defined by Line p11,p12 and Line p21,p22 intersect ...
Definition: GeomHelper.cpp:132
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
bool myWasInitialised
Information whether the boundary was initialised.
Definition: Boundary.h:132
virtual bool around(const Position &p, SUMOReal offset=0) const =0
void add(SUMOReal x, SUMOReal y)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:76
void reset()
Resets the boundary.
Definition: Boundary.cpp:66
Boundary & grow(SUMOReal by)
extends the boundary by the given amount
Definition: Boundary.cpp:200
void flipY()
flips ymin and ymax
Definition: Boundary.cpp:222
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
#define SUMOReal
Definition: config.h:221
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:130
void growHeight(SUMOReal by)
Definition: Boundary.cpp:216
void moveby(SUMOReal x, SUMOReal y)
Moves the boundary by the given amount.
Definition: Boundary.cpp:249
std::ostream & operator<<(std::ostream &os, const MTRand &mtrand)
bool crosses(const Position &p1, const Position &p2) const
Returns whether the boundary crosses the given line.
Definition: Boundary.cpp:177
Boundary()
Constructor - the boundary is unset.
Definition: Boundary.cpp:47