A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
28/**
29 * \file
30 * \ingroup geometry
31 * ns3::Vector, ns3::Vector2D and ns3::Vector3D attribute value implementations.
32 */
33
34namespace ns3
35{
36
38
41
42// compatibility for mobility code
45{
47 return MakeVector3DChecker();
48}
49
50Vector3D::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
66Vector2D::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
80double
82{
83 NS_LOG_FUNCTION(this);
84 return std::sqrt(x * x + y * y + z * z);
85}
86
87double
89{
90 NS_LOG_FUNCTION(this);
91 return std::sqrt(x * x + y * y);
92}
93
94double
96{
97 NS_LOG_FUNCTION(this);
98 return x * x + y * y + z * z;
99}
100
101double
103{
104 NS_LOG_FUNCTION(this);
105 return x * x + y * y;
106}
107
108double
110{
111 NS_LOG_FUNCTION(a << b);
112 return (b - a).GetLength();
113}
114
115double
117{
118 NS_LOG_FUNCTION(a << b);
119 return (b - a).GetLength();
120}
121
122double
124{
125 NS_LOG_FUNCTION(a << b);
126 return (b - a).GetLengthSquared();
127}
128
129double
131{
132 NS_LOG_FUNCTION(a << b);
133 return (b - a).GetLengthSquared();
134}
135
136std::ostream&
137operator<<(std::ostream& os, const Vector3D& vector)
138{
139 os << vector.x << ":" << vector.y << ":" << vector.z;
140 return os;
141}
142
143std::istream&
144operator>>(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
156bool
157operator<(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
162bool
163operator<=(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
168bool
169operator>(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
174bool
175operator>=(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
180bool
181operator==(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
186bool
187operator!=(const Vector3D& a, const Vector3D& b)
188{
189 return !(a == b);
190}
191
193operator+(const Vector3D& a, const Vector3D& b)
194{
195 return Vector3D(a.x + b.x, a.y + b.y, a.z + b.z);
196}
197
199operator-(const Vector3D& a, const Vector3D& b)
200{
201 return Vector3D(a.x - b.x, a.y - b.y, a.z - b.z);
202}
203
205operator*(const Vector3D& a, double b)
206{
207 return Vector3D(a.x * b, a.y * b, a.z * b);
208}
209
211operator*(double a, const Vector3D& b)
212{
213 return Vector3D(b.x * a, b.y * a, b.z * a);
214}
215
216double
217operator*(const Vector3D& a, const Vector3D& b)
218{
219 return a.x * b.x + a.y * b.y + a.z * b.z;
220}
221
222std::ostream&
223operator<<(std::ostream& os, const Vector2D& vector)
224{
225 os << vector.x << ":" << vector.y;
226 return os;
227}
228
229std::istream&
230operator>>(std::istream& is, Vector2D& vector)
231{
232 char c1;
233 is >> vector.x >> c1 >> vector.y;
234 if (c1 != ':')
235 {
236 is.setstate(std::ios_base::failbit);
237 }
238 return is;
239}
240
241bool
242operator<(const Vector2D& a, const Vector2D& b)
243{
244 return std::tie(a.x, a.y) < std::tie(b.x, b.y);
245}
246
247bool
248operator<=(const Vector2D& a, const Vector2D& b)
249{
250 return std::tie(a.x, a.y) <= std::tie(b.x, b.y);
251}
252
253bool
254operator>(const Vector2D& a, const Vector2D& b)
255{
256 return std::tie(a.x, a.y) > std::tie(b.x, b.y);
257}
258
259bool
260operator>=(const Vector2D& a, const Vector2D& b)
261{
262 return std::tie(a.x, a.y) >= std::tie(b.x, b.y);
263}
264
265bool
266operator==(const Vector2D& a, const Vector2D& b)
267{
268 return std::tie(a.x, a.y) == std::tie(b.x, b.y);
269}
270
271bool
272operator!=(const Vector2D& a, const Vector2D& b)
273{
274 return !(a == b);
275}
276
278operator+(const Vector2D& a, const Vector2D& b)
279{
280 return Vector2D(a.x + b.x, a.y + b.y);
281}
282
284operator-(const Vector2D& a, const Vector2D& b)
285{
286 return Vector2D(a.x - b.x, a.y - b.y);
287}
288
290operator*(const Vector2D& a, double b)
291{
292 return Vector2D(a.x * b, a.y * b);
293}
294
296operator*(double a, const Vector2D& b)
297{
298 return Vector2D(b.x * a, b.y * a);
299}
300
301double
302operator*(const Vector2D& a, const Vector2D& b)
303{
304 return a.x * b.x + a.y * b.y;
305}
306
307} // namespace ns3
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
a 2d vector
Definition: vector.h:207
double y
y coordinate of vector
Definition: vector.h:219
Vector2D()
Constructor: (0.0, 0.0)
Definition: vector.cc:73
double x
x coordinate of vector
Definition: vector.h:218
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.
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type
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
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition: int64x64.h:118
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:674
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:166
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:179
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:109
double CalculateDistanceSquared(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:123
Ptr< const AttributeChecker > MakeVectorChecker()
Definition: vector.cc:44
Ptr< const AttributeChecker > MakeVector3DChecker()
Definition: vector.cc:39
ns3::Vector, ns3::Vector2D and ns3::Vector3D declarations.