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