A Discrete-Event Network Simulator
API
rectangle.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2007 INRIA
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19 */
20#include "rectangle.h"
21#include "ns3/vector.h"
22#include "ns3/assert.h"
23#include "ns3/fatal-error.h"
24#include <cmath>
25#include <algorithm>
26#include <sstream>
27
28namespace ns3 {
29
30Rectangle::Rectangle (double _xMin, double _xMax,
31 double _yMin, double _yMax)
32 : xMin (_xMin),
33 xMax (_xMax),
34 yMin (_yMin),
35 yMax (_yMax)
36{
37}
38
40 : xMin (0.0),
41 xMax (0.0),
42 yMin (0.0),
43 yMax (0.0)
44{
45}
46
47bool
48Rectangle::IsInside (const Vector &position) const
49{
50 return
51 position.x <= this->xMax && position.x >= this->xMin &&
52 position.y <= this->yMax && position.y >= this->yMin;
53}
54
56Rectangle::GetClosestSide (const Vector &position) const
57{
58 if (IsInside (position))
59 {
60 double xMinDist = std::abs (position.x - this->xMin);
61 double xMaxDist = std::abs (this->xMax - position.x);
62 double yMinDist = std::abs (position.y - this->yMin);
63 double yMaxDist = std::abs (this->yMax - position.y);
64 double minX = std::min (xMinDist, xMaxDist);
65 double minY = std::min (yMinDist, yMaxDist);
66 if (minX < minY)
67 {
68 if (xMinDist < xMaxDist)
69 {
70 return LEFT;
71 }
72 else
73 {
74 return RIGHT;
75 }
76 }
77 else
78 {
79 if (yMinDist < yMaxDist)
80 {
81 return BOTTOM;
82 }
83 else
84 {
85 return TOP;
86 }
87 }
88 }
89 else
90 {
91 if (position.x < this->xMin)
92 {
93 if (position.y < this->yMin)
94 {
95 double yDiff = this->yMin - position.y;
96 double xDiff = this->xMin - position.x;
97 if (yDiff > xDiff)
98 {
99 return BOTTOM;
100 }
101 else
102 {
103 return LEFT;
104 }
105 }
106 else if (position.y < this->yMax)
107 {
108 return LEFT;
109 }
110 else
111 {
112 double yDiff = position.y - this->yMax;
113 double xDiff = this->xMin - position.x;
114 if (yDiff > xDiff)
115 {
116 return TOP;
117 }
118 else
119 {
120 return LEFT;
121 }
122 }
123 }
124 else if (position.x < this->xMax)
125 {
126 if (position.y < this->yMin)
127 {
128 return BOTTOM;
129 }
130 else if (position.y < this->yMax)
131 {
132 NS_FATAL_ERROR ("This region should have been reached if the IsInside check was true");
133 return TOP; // silence compiler warning
134 }
135 else
136 {
137 return TOP;
138 }
139 }
140 else
141 {
142 if (position.y < this->yMin)
143 {
144 double yDiff = this->yMin - position.y;
145 double xDiff = position.x - this->xMin;
146 if (yDiff > xDiff)
147 {
148 return BOTTOM;
149 }
150 else
151 {
152 return RIGHT;
153 }
154 }
155 else if (position.y < this->yMax)
156 {
157 return RIGHT;
158 }
159 else
160 {
161 double yDiff = position.y - this->yMax;
162 double xDiff = position.x - this->xMin;
163 if (yDiff > xDiff)
164 {
165 return TOP;
166 }
167 else
168 {
169 return RIGHT;
170 }
171 }
172 }
173 }
174}
175
176Vector
177Rectangle::CalculateIntersection (const Vector &current, const Vector &speed) const
178{
179 NS_ASSERT (IsInside (current));
180 double xMaxY = current.y + (this->xMax - current.x) / speed.x * speed.y;
181 double xMinY = current.y + (this->xMin - current.x) / speed.x * speed.y;
182 double yMaxX = current.x + (this->yMax - current.y) / speed.y * speed.x;
183 double yMinX = current.x + (this->yMin - current.y) / speed.y * speed.x;
184 bool xMaxYOk = (xMaxY <= this->yMax && xMaxY >= this->yMin);
185 bool xMinYOk = (xMinY <= this->yMax && xMinY >= this->yMin);
186 bool yMaxXOk = (yMaxX <= this->xMax && yMaxX >= this->xMin);
187 bool yMinXOk = (yMinX <= this->xMax && yMinX >= this->xMin);
188 if (xMaxYOk && speed.x >= 0)
189 {
190 return Vector (this->xMax, xMaxY, 0.0);
191 }
192 else if (xMinYOk && speed.x <= 0)
193 {
194 return Vector (this->xMin, xMinY, 0.0);
195 }
196 else if (yMaxXOk && speed.y >= 0)
197 {
198 return Vector (yMaxX, this->yMax, 0.0);
199 }
200 else if (yMinXOk && speed.y <= 0)
201 {
202 return Vector (yMinX, this->yMin, 0.0);
203 }
204 else
205 {
206 NS_ASSERT (false);
207 // quiet compiler
208 return Vector (0.0, 0.0, 0.0);
209 }
210
211}
212
214
222std::ostream &
223operator << (std::ostream &os, const Rectangle &rectangle)
224{
225 os << rectangle.xMin << "|" << rectangle.xMax << "|" << rectangle.yMin << "|" << rectangle.yMax;
226 return os;
227}
235std::istream &
236operator >> (std::istream &is, Rectangle &rectangle)
237{
238 char c1, c2, c3;
239 is >> rectangle.xMin >> c1 >> rectangle.xMax >> c2 >> rectangle.yMin >> c3 >> rectangle.yMax;
240 if (c1 != '|' ||
241 c2 != '|' ||
242 c3 != '|')
243 {
244 is.setstate (std::ios_base::failbit);
245 }
246 return is;
247}
248
249
250} // namespace ns3
#define min(a, b)
Definition: 80211b.c:42
a 2d rectangle
Definition: rectangle.h:35
Side GetClosestSide(const Vector &position) const
Definition: rectangle.cc:56
double yMax
The y coordinate of the top bound of the rectangle.
Definition: rectangle.h:91
bool IsInside(const Vector &position) const
Definition: rectangle.cc:48
double xMax
The x coordinate of the right bound of the rectangle.
Definition: rectangle.h:89
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: rectangle.cc:177
Side
enum for naming sides
Definition: rectangle.h:40
double xMin
The x coordinate of the left bound of the rectangle.
Definition: rectangle.h:88
Rectangle()
Create a zero-sized rectangle located at coordinates (0.0,0.0)
Definition: rectangle.cc:39
double yMin
The y coordinate of the bottom bound of the rectangle.
Definition: rectangle.h:90
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
ATTRIBUTE_HELPER_CPP(Length)
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:162