A Discrete-Event Network Simulator
API
vector.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2007 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19 */
20#include "vector.h"
21#include "fatal-error.h"
22#include "log.h"
23#include <cmath>
24#include <sstream>
25#include <tuple>
26
33namespace ns3 {
34
36
39
40// compatibility for mobility code
42{
44 return MakeVector3DChecker ();
45}
46
47
48Vector3D::Vector3D (double _x, double _y, double _z)
49 : x (_x),
50 y (_y),
51 z (_z)
52{
53 NS_LOG_FUNCTION (this << _x << _y << _z);
54}
55
57 : x (0.0),
58 y (0.0),
59 z (0.0)
60{
61 NS_LOG_FUNCTION (this);
62}
63
64Vector2D::Vector2D (double _x, double _y)
65 : x (_x),
66 y (_y)
67{
68 NS_LOG_FUNCTION (this << _x << _y);
69}
70
72 : x (0.0),
73 y (0.0)
74{
75 NS_LOG_FUNCTION (this);
76}
77
78double
80{
81 NS_LOG_FUNCTION (this);
82 return std::sqrt (x * x + y * y + z * z);
83}
84double
86{
87 NS_LOG_FUNCTION (this);
88 return std::sqrt (x * x + y * y);
89}
90
91double
93{
94 NS_LOG_FUNCTION (this);
95 return x * x + y * y + z * z;
96}
97double
99{
100 NS_LOG_FUNCTION (this);
101 return x * x + y * y;
102}
103
104double
106{
107 NS_LOG_FUNCTION (a << b);
108 return (b - a).GetLength ();
109}
110double
112{
113 NS_LOG_FUNCTION (a << b);
114 return (b - a).GetLength ();
115}
116
117double
119{
120 NS_LOG_FUNCTION (a << b);
121 return (b - a).GetLengthSquared ();
122}
123double
125{
126 NS_LOG_FUNCTION (a << b);
127 return (b - a).GetLengthSquared ();
128}
129
130std::ostream &operator << (std::ostream &os, const Vector3D &vector)
131{
132 os << vector.x << ":" << vector.y << ":" << vector.z;
133 return os;
134}
135std::istream &operator >> (std::istream &is, Vector3D &vector)
136{
137 char c1, c2;
138 is >> vector.x >> c1 >> vector.y >> c2 >> vector.z;
139 if (c1 != ':'
140 || c2 != ':')
141 {
142 is.setstate (std::ios_base::failbit);
143 }
144 return is;
145}
146bool operator < (const Vector3D &a, const Vector3D &b)
147{
148 return std::tie (a.x, a.y, a.z) <
149 std::tie (b.x, b.y, b.z);
150}
151bool operator <= (const Vector3D &a, const Vector3D &b)
152{
153 return std::tie (a.x, a.y, a.z) <=
154 std::tie (b.x, b.y, b.z);
155}
156bool operator > (const Vector3D &a, const Vector3D &b)
157{
158 return std::tie (a.x, a.y, a.z) >
159 std::tie (b.x, b.y, b.z);
160}
161bool operator >= (const Vector3D &a, const Vector3D &b)
162{
163 return std::tie (a.x, a.y, a.z) >=
164 std::tie (b.x, b.y, b.z);
165}
166bool operator == (const Vector3D &a, const Vector3D &b)
167{
168 return std::tie (a.x, a.y, a.z) ==
169 std::tie (b.x, b.y, b.z);
170}
171bool operator != (const Vector3D &a, const Vector3D &b)
172{
173 return !(a == b);
174}
176operator + (const Vector3D &a, const Vector3D &b)
177{
178 return Vector3D (a.x + b.x, a.y + b.y, a.z + b.z);
179}
181operator - (const Vector3D &a, const Vector3D &b)
182{
183 return Vector3D (a.x - b.x, a.y - b.y, a.z - b.z);
184}
185std::ostream &operator << (std::ostream &os, const Vector2D &vector)
186{
187 os << vector.x << ":" << vector.y;
188 return os;
189}
190std::istream &operator >> (std::istream &is, Vector2D &vector)
191{
192 char c1;
193 is >> vector.x >> c1 >> vector.y;
194 if (c1 != ':')
195 {
196 is.setstate (std::ios_base::failbit);
197 }
198 return is;
199}
200bool operator < (const Vector2D &a, const Vector2D &b)
201{
202 return std::tie (a.x, a.y) <
203 std::tie (b.x, b.y);
204}
205bool operator <= (const Vector2D &a, const Vector2D &b)
206{
207 return std::tie (a.x, a.y) <=
208 std::tie (b.x, b.y);
209}
210bool operator > (const Vector2D &a, const Vector2D &b)
211{
212 return std::tie (a.x, a.y) >
213 std::tie (b.x, b.y);
214}
215bool operator >= (const Vector2D &a, const Vector2D &b)
216{
217 return std::tie (a.x, a.y) >=
218 std::tie (b.x, b.y);
219}
220bool operator == (const Vector2D &a, const Vector2D &b)
221{
222 return std::tie (a.x, a.y) ==
223 std::tie (b.x, b.y);
224}
225bool operator != (const Vector2D &a, const Vector2D &b)
226{
227 return !(a == b);
228}
230operator + (const Vector2D &a, const Vector2D &b)
231{
232 return Vector2D (a.x + b.x, a.y + b.y);
233}
235operator - (const Vector2D &a, const Vector2D &b)
236{
237 return Vector2D (a.x - b.x, a.y - b.y);
238}
239
240} // namespace ns3
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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:71
double x
x coordinate of vector
Definition: vector.h:194
double GetLength() const
Compute the length (magnitude) of the vector.
Definition: vector.cc:85
double GetLengthSquared() const
Compute the squared length of the vector.
Definition: vector.cc:98
a 3d vector
Definition: vector.h:46
double GetLength() const
Compute the length (magnitude) of the vector.
Definition: vector.cc:79
double x
x coordinate of vector
Definition: vector.h:59
Vector3D()
Create vector (0.0, 0.0, 0.0)
Definition: vector.cc:56
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:92
NS_FATAL_x macro definitions.
Ptr< const AttributeChecker > MakeVector3DChecker(void)
Definition: vector.cc:37
Ptr< const AttributeChecker > MakeVectorChecker(void)
Definition: vector.cc:41
bool operator>=(const int64x64_t &lhs, const int64x64_t &rhs)
Greater or equal operator.
Definition: int64x64.h:166
bool operator<=(const int64x64_t &lhs, const int64x64_t &rhs)
Less or equal operator.
Definition: int64x64.h:155
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:89
bool operator>(const Length &left, const Length &right)
Check if left has a value greater than right.
Definition: length.cc:413
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:206
#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:636
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:158
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
ATTRIBUTE_HELPER_CPP(Length)
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:162
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:176
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:105
double CalculateDistanceSquared(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:118
ns3::Vector, ns3::Vector2D and ns3::Vector3D declarations.