A Discrete-Event Network Simulator
API
vector.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 #include "vector.h"
20 
21 #include "fatal-error.h"
22 #include "log.h"
23 
24 #include <cmath>
25 #include <sstream>
26 #include <tuple>
27 
34 namespace ns3
35 {
36 
37 NS_LOG_COMPONENT_DEFINE("Vector");
38 
41 
42 // compatibility for mobility code
45 {
47  return MakeVector3DChecker();
48 }
49 
50 Vector3D::Vector3D(double _x, double _y, double _z)
51  : x(_x),
52  y(_y),
53  z(_z)
54 {
55  NS_LOG_FUNCTION(this << _x << _y << _z);
56 }
57 
59  : x(0.0),
60  y(0.0),
61  z(0.0)
62 {
63  NS_LOG_FUNCTION(this);
64 }
65 
66 Vector2D::Vector2D(double _x, double _y)
67  : x(_x),
68  y(_y)
69 {
70  NS_LOG_FUNCTION(this << _x << _y);
71 }
72 
74  : x(0.0),
75  y(0.0)
76 {
77  NS_LOG_FUNCTION(this);
78 }
79 
80 double
82 {
83  NS_LOG_FUNCTION(this);
84  return std::sqrt(x * x + y * y + z * z);
85 }
86 
87 double
89 {
90  NS_LOG_FUNCTION(this);
91  return std::sqrt(x * x + y * y);
92 }
93 
94 double
96 {
97  NS_LOG_FUNCTION(this);
98  return x * x + y * y + z * z;
99 }
100 
101 double
103 {
104  NS_LOG_FUNCTION(this);
105  return x * x + y * y;
106 }
107 
108 double
110 {
111  NS_LOG_FUNCTION(a << b);
112  return (b - a).GetLength();
113 }
114 
115 double
117 {
118  NS_LOG_FUNCTION(a << b);
119  return (b - a).GetLength();
120 }
121 
122 double
124 {
125  NS_LOG_FUNCTION(a << b);
126  return (b - a).GetLengthSquared();
127 }
128 
129 double
131 {
132  NS_LOG_FUNCTION(a << b);
133  return (b - a).GetLengthSquared();
134 }
135 
136 std::ostream&
137 operator<<(std::ostream& os, const Vector3D& vector)
138 {
139  os << vector.x << ":" << vector.y << ":" << vector.z;
140  return os;
141 }
142 
143 std::istream&
144 operator>>(std::istream& is, Vector3D& vector)
145 {
146  char c1;
147  char c2;
148  is >> vector.x >> c1 >> vector.y >> c2 >> vector.z;
149  if (c1 != ':' || c2 != ':')
150  {
151  is.setstate(std::ios_base::failbit);
152  }
153  return is;
154 }
155 
156 bool
157 operator<(const Vector3D& a, const Vector3D& b)
158 {
159  return std::tie(a.x, a.y, a.z) < std::tie(b.x, b.y, b.z);
160 }
161 
162 bool
163 operator<=(const Vector3D& a, const Vector3D& b)
164 {
165  return std::tie(a.x, a.y, a.z) <= std::tie(b.x, b.y, b.z);
166 }
167 
168 bool
169 operator>(const Vector3D& a, const Vector3D& b)
170 {
171  return std::tie(a.x, a.y, a.z) > std::tie(b.x, b.y, b.z);
172 }
173 
174 bool
175 operator>=(const Vector3D& a, const Vector3D& b)
176 {
177  return std::tie(a.x, a.y, a.z) >= std::tie(b.x, b.y, b.z);
178 }
179 
180 bool
181 operator==(const Vector3D& a, const Vector3D& b)
182 {
183  return std::tie(a.x, a.y, a.z) == std::tie(b.x, b.y, b.z);
184 }
185 
186 bool
187 operator!=(const Vector3D& a, const Vector3D& b)
188 {
189  return !(a == b);
190 }
191 
192 Vector3D
193 operator+(const Vector3D& a, const Vector3D& b)
194 {
195  return Vector3D(a.x + b.x, a.y + b.y, a.z + b.z);
196 }
197 
198 Vector3D
199 operator-(const Vector3D& a, const Vector3D& b)
200 {
201  return Vector3D(a.x - b.x, a.y - b.y, a.z - b.z);
202 }
203 
204 std::ostream&
205 operator<<(std::ostream& os, const Vector2D& vector)
206 {
207  os << vector.x << ":" << vector.y;
208  return os;
209 }
210 
211 std::istream&
212 operator>>(std::istream& is, Vector2D& vector)
213 {
214  char c1;
215  is >> vector.x >> c1 >> vector.y;
216  if (c1 != ':')
217  {
218  is.setstate(std::ios_base::failbit);
219  }
220  return is;
221 }
222 
223 bool
224 operator<(const Vector2D& a, const Vector2D& b)
225 {
226  return std::tie(a.x, a.y) < std::tie(b.x, b.y);
227 }
228 
229 bool
230 operator<=(const Vector2D& a, const Vector2D& b)
231 {
232  return std::tie(a.x, a.y) <= std::tie(b.x, b.y);
233 }
234 
235 bool
236 operator>(const Vector2D& a, const Vector2D& b)
237 {
238  return std::tie(a.x, a.y) > std::tie(b.x, b.y);
239 }
240 
241 bool
242 operator>=(const Vector2D& a, const Vector2D& b)
243 {
244  return std::tie(a.x, a.y) >= std::tie(b.x, b.y);
245 }
246 
247 bool
248 operator==(const Vector2D& a, const Vector2D& b)
249 {
250  return std::tie(a.x, a.y) == std::tie(b.x, b.y);
251 }
252 
253 bool
254 operator!=(const Vector2D& a, const Vector2D& b)
255 {
256  return !(a == b);
257 }
258 
259 Vector2D
260 operator+(const Vector2D& a, const Vector2D& b)
261 {
262  return Vector2D(a.x + b.x, a.y + b.y);
263 }
264 
265 Vector2D
266 operator-(const Vector2D& a, const Vector2D& b)
267 {
268  return Vector2D(a.x - b.x, a.y - b.y);
269 }
270 
271 } // namespace ns3
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
a 2d vector
Definition: vector.h:183
double y
y coordinate of vector
Definition: vector.h:195
Vector2D()
Constructor: (0.0, 0.0)
Definition: vector.cc:73
double x
x coordinate of vector
Definition: vector.h:194
double GetLength() const
Compute the length (magnitude) of the vector.
Definition: vector.cc:88
double GetLengthSquared() const
Compute the squared length of the vector.
Definition: vector.cc:102
a 3d vector
Definition: vector.h:46
double GetLength() const
Compute the length (magnitude) of the vector.
Definition: vector.cc:81
double x
x coordinate of vector
Definition: vector.h:59
Vector3D()
Create vector (0.0, 0.0, 0.0)
Definition: vector.cc:58
double z
z coordinate of vector
Definition: vector.h:61
double y
y coordinate of vector
Definition: vector.h:60
double GetLengthSquared() const
Compute the squared length of the vector.
Definition: vector.cc:95
NS_FATAL_x macro definitions.
bool operator>=(const int64x64_t &lhs, const int64x64_t &rhs)
Greater or equal operator.
Definition: int64x64.h:174
bool operator<=(const int64x64_t &lhs, const int64x64_t &rhs)
Less or equal operator.
Definition: int64x64.h:161
int64x64_t operator-(const int64x64_t &lhs, const int64x64_t &rhs)
Subtraction operator.
Definition: int64x64.h:103
int64x64_t operator+(const int64x64_t &lhs, const int64x64_t &rhs)
Addition operator.
Definition: int64x64.h:88
bool operator>(const Length &left, const Length &right)
Check if left has a value greater than right.
Definition: length.cc:421
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
Definition: callback.h:678
Ptr< const AttributeChecker > MakeVectorChecker()
Definition: vector.cc:44
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:157
ATTRIBUTE_HELPER_CPP(ValueClassTest)
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:170
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:109
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183
double CalculateDistanceSquared(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:123
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
Ptr< const AttributeChecker > MakeVector3DChecker()
Definition: vector.cc:39
ns3::Vector, ns3::Vector2D and ns3::Vector3D declarations.