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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "vector.h"
9
10#include "log.h"
11
12#include <cmath>
13#include <tuple>
14
15/**
16 * @file
17 * @ingroup geometry
18 * ns3::Vector, ns3::Vector2D and ns3::Vector3D attribute value implementations.
19 */
20
21namespace ns3
22{
23
25
28
29// compatibility for mobility code
36
37Vector3D::Vector3D(double _x, double _y, double _z)
38 : x(_x),
39 y(_y),
40 z(_z)
41{
42 NS_LOG_FUNCTION(this << _x << _y << _z);
43}
44
46 : x(0.0),
47 y(0.0),
48 z(0.0)
49{
50 NS_LOG_FUNCTION(this);
51}
52
53Vector2D::Vector2D(double _x, double _y)
54 : x(_x),
55 y(_y)
56{
57 NS_LOG_FUNCTION(this << _x << _y);
58}
59
61 : x(0.0),
62 y(0.0)
63{
64 NS_LOG_FUNCTION(this);
65}
66
67double
69{
70 NS_LOG_FUNCTION(this);
71 return std::sqrt(x * x + y * y + z * z);
72}
73
74double
76{
77 NS_LOG_FUNCTION(this);
78 return std::sqrt(x * x + y * y);
79}
80
81double
83{
84 NS_LOG_FUNCTION(this);
85 return x * x + y * y + z * z;
86}
87
88double
90{
91 NS_LOG_FUNCTION(this);
92 return x * x + y * y;
93}
94
95double
97{
98 NS_LOG_FUNCTION(a << b);
99 return (b - a).GetLength();
100}
101
102double
104{
105 NS_LOG_FUNCTION(a << b);
106 return (b - a).GetLength();
107}
108
109double
111{
112 NS_LOG_FUNCTION(a << b);
113 return (b - a).GetLengthSquared();
114}
115
116double
118{
119 NS_LOG_FUNCTION(a << b);
120 return (b - a).GetLengthSquared();
121}
122
123std::ostream&
124operator<<(std::ostream& os, const Vector3D& vector)
125{
126 os << vector.x << ":" << vector.y << ":" << vector.z;
127 return os;
128}
129
130std::istream&
131operator>>(std::istream& is, Vector3D& vector)
132{
133 char c1;
134 char c2;
135 is >> vector.x >> c1 >> vector.y >> c2 >> vector.z;
136 if (c1 != ':' || c2 != ':')
137 {
138 is.setstate(std::ios_base::failbit);
139 }
140 return is;
141}
142
143bool
144operator<(const Vector3D& a, const Vector3D& b)
145{
146 return std::tie(a.x, a.y, a.z) < std::tie(b.x, b.y, b.z);
147}
148
149bool
150operator<=(const Vector3D& a, const Vector3D& b)
151{
152 return std::tie(a.x, a.y, a.z) <= std::tie(b.x, b.y, b.z);
153}
154
155bool
156operator>(const Vector3D& a, const Vector3D& b)
157{
158 return std::tie(a.x, a.y, a.z) > std::tie(b.x, b.y, b.z);
159}
160
161bool
162operator>=(const Vector3D& a, const Vector3D& b)
163{
164 return std::tie(a.x, a.y, a.z) >= std::tie(b.x, b.y, b.z);
165}
166
167bool
168operator==(const Vector3D& a, const Vector3D& b)
169{
170 return std::tie(a.x, a.y, a.z) == std::tie(b.x, b.y, b.z);
171}
172
173bool
174operator!=(const Vector3D& a, const Vector3D& b)
175{
176 return !(a == b);
177}
178
180operator+(const Vector3D& a, const Vector3D& b)
181{
182 return Vector3D(a.x + b.x, a.y + b.y, a.z + b.z);
183}
184
186operator-(const Vector3D& a, const Vector3D& b)
187{
188 return Vector3D(a.x - b.x, a.y - b.y, a.z - b.z);
189}
190
192operator*(const Vector3D& a, double b)
193{
194 return Vector3D(a.x * b, a.y * b, a.z * b);
195}
196
198operator*(double a, const Vector3D& b)
199{
200 return Vector3D(b.x * a, b.y * a, b.z * a);
201}
202
203double
204operator*(const Vector3D& a, const Vector3D& b)
205{
206 return a.x * b.x + a.y * b.y + a.z * b.z;
207}
208
209std::ostream&
210operator<<(std::ostream& os, const Vector2D& vector)
211{
212 os << vector.x << ":" << vector.y;
213 return os;
214}
215
216std::istream&
217operator>>(std::istream& is, Vector2D& vector)
218{
219 char c1;
220 is >> vector.x >> c1 >> vector.y;
221 if (c1 != ':')
222 {
223 is.setstate(std::ios_base::failbit);
224 }
225 return is;
226}
227
228bool
229operator<(const Vector2D& a, const Vector2D& b)
230{
231 return std::tie(a.x, a.y) < std::tie(b.x, b.y);
232}
233
234bool
235operator<=(const Vector2D& a, const Vector2D& b)
236{
237 return std::tie(a.x, a.y) <= std::tie(b.x, b.y);
238}
239
240bool
241operator>(const Vector2D& a, const Vector2D& b)
242{
243 return std::tie(a.x, a.y) > std::tie(b.x, b.y);
244}
245
246bool
247operator>=(const Vector2D& a, const Vector2D& b)
248{
249 return std::tie(a.x, a.y) >= std::tie(b.x, b.y);
250}
251
252bool
253operator==(const Vector2D& a, const Vector2D& b)
254{
255 return std::tie(a.x, a.y) == std::tie(b.x, b.y);
256}
257
258bool
259operator!=(const Vector2D& a, const Vector2D& b)
260{
261 return !(a == b);
262}
263
265operator+(const Vector2D& a, const Vector2D& b)
266{
267 return Vector2D(a.x + b.x, a.y + b.y);
268}
269
271operator-(const Vector2D& a, const Vector2D& b)
272{
273 return Vector2D(a.x - b.x, a.y - b.y);
274}
275
277operator*(const Vector2D& a, double b)
278{
279 return Vector2D(a.x * b, a.y * b);
280}
281
283operator*(double a, const Vector2D& b)
284{
285 return Vector2D(b.x * a, b.y * a);
286}
287
288double
289operator*(const Vector2D& a, const Vector2D& b)
290{
291 return a.x * b.x + a.y * b.y;
292}
293
294} // namespace ns3
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
a 2d vector
Definition vector.h:196
Vector2D(double _x, double _y)
Definition vector.cc:53
double y
y coordinate of vector
Definition vector.h:208
Vector2D()
Constructor: (0.0, 0.0).
Definition vector.cc:60
double x
x coordinate of vector
Definition vector.h:207
double GetLength() const
Compute the length (magnitude) of the vector.
Definition vector.cc:75
double GetLengthSquared() const
Compute the squared length of the vector.
Definition vector.cc:89
a 3d vector
Definition vector.h:35
double GetLength() const
Compute the length (magnitude) of the vector.
Definition vector.cc:68
Vector3D(double _x, double _y, double _z)
Definition vector.cc:37
double x
x coordinate of vector
Definition vector.h:48
Vector3D()
Create vector (0.0, 0.0, 0.0).
Definition vector.cc:45
double z
z coordinate of vector
Definition vector.h:50
double y
y coordinate of vector
Definition vector.h:49
double GetLengthSquared() const
Compute the squared length of the vector.
Definition vector.cc:82
STL class.
Ptr< const AttributeChecker > MakeVectorChecker()
Definition vector.cc:31
Ptr< const AttributeChecker > MakeVector3DChecker()
Definition vector.cc:26
#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:162
bool operator<=(const int64x64_t &lhs, const int64x64_t &rhs)
Less or equal operator.
Definition int64x64.h:149
int64x64_t operator-(const int64x64_t &lhs, const int64x64_t &rhs)
Subtraction operator.
Definition int64x64.h:91
int64x64_t operator+(const int64x64_t &lhs, const int64x64_t &rhs)
Addition operator.
Definition int64x64.h:76
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition int64x64.h:106
bool operator>(const Length &left, const Length &right)
Check if left has a value greater than right.
Definition length.cc:406
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#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:664
bool operator==(const EventId &a, const EventId &b)
Definition event-id.h:146
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
std::istream & operator>>(std::istream &is, Angles &a)
Definition angles.cc:172
bool operator<(const EventId &a, const EventId &b)
Definition event-id.h:159
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition vector.cc:96
double CalculateDistanceSquared(const Vector3D &a, const Vector3D &b)
Definition vector.cc:110
ns3::Vector, ns3::Vector2D and ns3::Vector3D declarations.