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 
28 namespace ns3 {
29 
30 Rectangle::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 
47 bool
48 Rectangle::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 
56 Rectangle::GetClosestSide (const Vector &position) const
57 {
58  double xMinDist = std::abs (position.x - this->xMin);
59  double xMaxDist = std::abs (this->xMax - position.x);
60  double yMinDist = std::abs (position.y - this->yMin);
61  double yMaxDist = std::abs (this->yMax - position.y);
62  double minX = std::min (xMinDist, xMaxDist);
63  double minY = std::min (yMinDist, yMaxDist);
64  if (minX < minY)
65  {
66  if (xMinDist < xMaxDist)
67  {
68  return LEFT;
69  }
70  else
71  {
72  return RIGHT;
73  }
74  }
75  else
76  {
77  if (yMinDist < yMaxDist)
78  {
79  return BOTTOM;
80  }
81  else
82  {
83  return TOP;
84  }
85  }
86 }
87 
88 Vector
89 Rectangle::CalculateIntersection (const Vector &current, const Vector &speed) const
90 {
91  NS_ASSERT (IsInside (current));
92  double xMaxY = current.y + (this->xMax - current.x) / speed.x * speed.y;
93  double xMinY = current.y + (this->xMin - current.x) / speed.x * speed.y;
94  double yMaxX = current.x + (this->yMax - current.y) / speed.y * speed.x;
95  double yMinX = current.x + (this->yMin - current.y) / speed.y * speed.x;
96  bool xMaxYOk = (xMaxY <= this->yMax && xMaxY >= this->yMin);
97  bool xMinYOk = (xMinY <= this->yMax && xMinY >= this->yMin);
98  bool yMaxXOk = (yMaxX <= this->xMax && yMaxX >= this->xMin);
99  bool yMinXOk = (yMinX <= this->xMax && yMinX >= this->xMin);
100  if (xMaxYOk && speed.x >= 0)
101  {
102  return Vector (this->xMax, xMaxY, 0.0);
103  }
104  else if (xMinYOk && speed.x <= 0)
105  {
106  return Vector (this->xMin, xMinY, 0.0);
107  }
108  else if (yMaxXOk && speed.y >= 0)
109  {
110  return Vector (yMaxX, this->yMax, 0.0);
111  }
112  else if (yMinXOk && speed.y <= 0)
113  {
114  return Vector (yMinX, this->yMin, 0.0);
115  }
116  else
117  {
118  NS_ASSERT (false);
119  // quiet compiler
120  return Vector (0.0, 0.0, 0.0);
121  }
122 
123 }
124 
126 
134 std::ostream &
135 operator << (std::ostream &os, const Rectangle &rectangle)
136 {
137  os << rectangle.xMin << "|" << rectangle.xMax << "|" << rectangle.yMin << "|" << rectangle.yMax;
138  return os;
139 }
147 std::istream &
148 operator >> (std::istream &is, Rectangle &rectangle)
149 {
150  char c1, c2, c3;
151  is >> rectangle.xMin >> c1 >> rectangle.xMax >> c2 >> rectangle.yMin >> c3 >> rectangle.yMax;
152  if (c1 != '|' ||
153  c2 != '|' ||
154  c3 != '|')
155  {
156  is.setstate (std::ios_base::failbit);
157  }
158  return is;
159 }
160 
161 
162 } // namespace ns3
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:48
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: rectangle.cc:89
#define min(a, b)
Definition: 80211b.c:44
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type.
#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
double yMax
The y coordinate of the top bound of the rectangle.
Definition: rectangle.h:91
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
Side
enum for naming sides
Definition: rectangle.h:40
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
double yMin
The y coordinate of the bottom bound of the rectangle.
Definition: rectangle.h:90
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Side GetClosestSide(const Vector &position) const
Definition: rectangle.cc:56
a 2d rectangle
Definition: rectangle.h:34