A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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
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 
127 std::ostream &
128 operator << (std::ostream &os, const Rectangle &rectangle)
129 {
130  os << rectangle.xMin << "|" << rectangle.xMax << "|" << rectangle.yMin << "|" << rectangle.yMax;
131  return os;
132 }
133 std::istream &
134 operator >> (std::istream &is, Rectangle &rectangle)
135 {
136  char c1, c2, c3;
137  is >> rectangle.xMin >> c1 >> rectangle.xMax >> c2 >> rectangle.yMin >> c3 >> rectangle.yMax;
138  if (c1 != '|' ||
139  c2 != '|' ||
140  c3 != '|')
141  {
142  is.setstate (std::ios_base::failbit);
143  }
144  return is;
145 }
146 
147 
148 } // namespace ns3
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:49
double x
x coordinate of vector
Definition: vector.h:49
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: rectangle.cc:89
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
double yMax
Definition: rectangle.h:91
a 3d vector
Definition: vector.h:31
double xMin
Definition: rectangle.h:85
Rectangle()
Create a zero-sized rectangle located at coordinates (0.0,0.0)
Definition: rectangle.cc:39
bool IsInside(const Vector &position) const
Definition: rectangle.cc:48
Vector3D Vector
Definition: vector.h:118
double xMax
Definition: rectangle.h:87
double yMin
Definition: rectangle.h:89
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:43
double y
y coordinate of vector
Definition: vector.h:53
Time current
Side GetClosestSide(const Vector &position) const
Definition: rectangle.cc:56
ATTRIBUTE_HELPER_CPP(ObjectFactory)
a 2d rectangle
Definition: rectangle.h:33