A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
box.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 Dan Broyles
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: Dan Broyles <dbroyl01@ku.edu>
19  */
20 
21 #include "box.h"
22 #include "ns3/vector.h"
23 #include "ns3/assert.h"
24 #include "ns3/fatal-error.h"
25 #include <cmath>
26 #include <algorithm>
27 #include <sstream>
28 
29 namespace ns3 {
30 
31 Box::Box (double _xMin, double _xMax,
32  double _yMin, double _yMax,
33  double _zMin, double _zMax)
34  : xMin (_xMin),
35  xMax (_xMax),
36  yMin (_yMin),
37  yMax (_yMax),
38  zMin (_zMin),
39  zMax (_zMax)
40 {
41 }
42 
44  : xMin (0.0),
45  xMax (0.0),
46  yMin (0.0),
47  yMax (0.0),
48  zMin (0.0),
49  zMax (0.0)
50 {
51 }
52 
53 bool
54 Box::IsInside (const Vector &position) const
55 {
56  return
57  position.x <= this->xMax && position.x >= this->xMin &&
58  position.y <= this->yMax && position.y >= this->yMin &&
59  position.z <= this->zMax && position.z >= this->zMin;
60 }
61 
62 Box::Side
63 Box::GetClosestSide (const Vector &position) const
64 {
65  double xMinDist = std::abs (position.x - this->xMin);
66  double xMaxDist = std::abs (this->xMax - position.x);
67  double yMinDist = std::abs (position.y - this->yMin);
68  double yMaxDist = std::abs (this->yMax - position.y);
69  double zMinDist = std::abs (position.z - this->zMin);
70  double zMaxDist = std::abs (this->zMax - position.z);
71  double minX = std::min (xMinDist, xMaxDist);
72  double minY = std::min (yMinDist, yMaxDist);
73  double minZ = std::min (zMinDist, zMaxDist);
74  if (minX < minY && minX < minZ)
75  {
76  if (xMinDist < xMaxDist)
77  {
78  return LEFT;
79  }
80  else
81  {
82  return RIGHT;
83  }
84  }
85  else if (minY < minZ)
86  {
87  if (yMinDist < yMaxDist)
88  {
89  return BOTTOM;
90  }
91  else
92  {
93  return TOP;
94  }
95  }
96  else
97  {
98  if (zMinDist < zMaxDist)
99  {
100  return DOWN;
101  }
102  else
103  {
104  return UP;
105  }
106  }
107 }
108 
109 Vector
110 Box::CalculateIntersection (const Vector &current, const Vector &speed) const
111 {
112  NS_ASSERT (IsInside (current));
113  double xMaxY = current.y + (this->xMax - current.x) / speed.x * speed.y;
114  double xMinY = current.y + (this->xMin - current.x) / speed.x * speed.y;
115  double yMaxX = current.x + (this->yMax - current.y) / speed.y * speed.x;
116  double yMinX = current.x + (this->yMin - current.y) / speed.y * speed.x;
117  bool xMaxYOk = (xMaxY <= this->yMax && xMaxY >= this->yMin);
118  bool xMinYOk = (xMinY <= this->yMax && xMinY >= this->yMin);
119  bool yMaxXOk = (yMaxX <= this->xMax && yMaxX >= this->xMin);
120  bool yMinXOk = (yMinX <= this->xMax && yMinX >= this->xMin);
121  if (xMaxYOk && speed.x >= 0)
122  {
123  return Vector (this->xMax, xMaxY, 0.0);
124  }
125  else if (xMinYOk && speed.x <= 0)
126  {
127  return Vector (this->xMin, xMinY, 0.0);
128  }
129  else if (yMaxXOk && speed.y >= 0)
130  {
131  return Vector (yMaxX, this->yMax, 0.0);
132  }
133  else if (yMinXOk && speed.y <= 0)
134  {
135  return Vector (yMinX, this->yMin, 0.0);
136  }
137  else
138  {
139  NS_ASSERT (false);
140  // quiet compiler
141  return Vector (0.0, 0.0, 0.0);
142  }
143 
144 }
145 
147 
148 std::ostream &
149 operator << (std::ostream &os, const Box &box)
150 {
151  os << box.xMin << "|" << box.xMax << "|" << box.yMin << "|" << box.yMax << "|" << box.zMin << "|" << box.zMax;
152  return os;
153 }
154 std::istream &
155 operator >> (std::istream &is, Box &box)
156 {
157  char c1, c2, c3, c4, c5;
158  is >> box.xMin >> c1 >> box.xMax >> c2 >> box.yMin >> c3 >> box.yMax >> c4 >> box.zMin >> c5 >> box.zMax;
159  if (c1 != '|' ||
160  c2 != '|' ||
161  c3 != '|' ||
162  c4 != '|' ||
163  c5 != '|')
164  {
165  is.setstate (std::ios_base::failbit);
166  }
167  return is;
168 }
169 
170 
171 } // namespace ns3
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:49
double x
Definition: vector.h:49
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: box.cc:110
Side GetClosestSide(const Vector &position) const
Definition: box.cc:63
#define NS_ASSERT(condition)
Definition: assert.h:64
a 3d vector
Definition: vector.h:31
a 3d box
Definition: box.h:33
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:43
#define ATTRIBUTE_HELPER_CPP(type)
double y
Definition: vector.h:53
Box()
Definition: box.cc:43
bool IsInside(const Vector &position) const
Definition: box.cc:54
double z
Definition: vector.h:57