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 
33 namespace ns3 {
34 
35 NS_LOG_COMPONENT_DEFINE ("Vector");
36 
39 
40 // compatibility for mobility code
42 {
44  return MakeVector3DChecker ();
45 }
46 
47 
48 Vector3D::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 
64 Vector2D::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 
78 double
80 {
81  NS_LOG_FUNCTION (this);
82  return std::sqrt (x * x + y * y + z * z);
83 }
84 double
86 {
87  NS_LOG_FUNCTION (this);
88  return std::sqrt (x * x + y * y);
89 }
90 
91 double
92 CalculateDistance (const Vector3D &a, const Vector3D &b)
93 {
94  NS_LOG_FUNCTION (a << b);
95  return (b - a).GetLength ();
96 }
97 double
98 CalculateDistance (const Vector2D &a, const Vector2D &b)
99 {
100  NS_LOG_FUNCTION (a << b);
101  return (b - a).GetLength ();
102 }
103 
104 std::ostream &operator << (std::ostream &os, const Vector3D &vector)
105 {
106  os << vector.x << ":" << vector.y << ":" << vector.z;
107  return os;
108 }
109 std::istream &operator >> (std::istream &is, Vector3D &vector)
110 {
111  char c1, c2;
112  is >> vector.x >> c1 >> vector.y >> c2 >> vector.z;
113  if (c1 != ':' ||
114  c2 != ':')
115  {
116  is.setstate (std::ios_base::failbit);
117  }
118  return is;
119 }
120 bool operator < (const Vector3D &a, const Vector3D &b)
121 {
122  return std::tie (a.x, a.y, a.z) <
123  std::tie (b.x, b.y, b.z);
124 }
125 Vector3D
126 operator + (const Vector3D &a, const Vector3D &b)
127 {
128  return Vector3D (a.x + b.x, a.y + b.y, a.z + b.z);
129 }
130 Vector3D
131 operator - (const Vector3D &a, const Vector3D &b)
132 {
133  return Vector3D (a.x - b.x, a.y - b.y, a.z - b.z);
134 }
135 std::ostream &operator << (std::ostream &os, const Vector2D &vector)
136 {
137  os << vector.x << ":" << vector.y;
138  return os;
139 }
140 std::istream &operator >> (std::istream &is, Vector2D &vector)
141 {
142  char c1;
143  is >> vector.x >> c1 >> vector.y;
144  if (c1 != ':')
145  {
146  is.setstate (std::ios_base::failbit);
147  }
148  return is;
149 }
150 bool operator < (const Vector2D &a, const Vector2D &b)
151 {
152  return std::tie (a.x, a.y) <
153  std::tie (b.x, b.y);
154 }
155 Vector2D
156 operator + (const Vector2D &a, const Vector2D &b)
157 {
158  return Vector2D (a.x + b.x, a.y + b.y);
159 }
160 Vector2D
161 operator - (const Vector2D &a, const Vector2D &b)
162 {
163  return Vector2D (a.x - b.x, a.y - b.y);
164 }
165 
166 } // namespace ns3
NS_FATAL_x macro definitions.
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:48
double x
x coordinate of vector
Definition: vector.h:59
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
int64x64_t operator+(const int64x64_t &lhs)
Unary plus operator.
Definition: int64x64-128.h:410
Ptr< const AttributeChecker > MakeVectorChecker(void)
Definition: vector.cc:41
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type.
int64x64_t operator-(const int64x64_t &lhs)
Unary negation operator (change sign operator).
Definition: int64x64-128.h:418
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Ptr< const AttributeChecker > MakeVector3DChecker(void)
Definition: vector.cc:37
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
a 3d vector
Definition: vector.h:45
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:153
double GetLength() const
Compute the length (magnitude) of the vector.
Definition: vector.cc:79
double x
x coordinate of vector
Definition: vector.h:140
Vector2D()
Constructor: (0.0, 0.0)
Definition: vector.cc:71
double y
y coordinate of vector
Definition: vector.h:141
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:92
Vector3D()
Create vector (0.0, 0.0, 0.0)
Definition: vector.cc:56
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
a 2d vector
Definition: vector.h:128
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double y
y coordinate of vector
Definition: vector.h:60
ns3::Vector, ns3::Vector2D and ns3::Vector3D declarations.
Debug message logging.
double GetLength() const
Compute the length (magnitude) of the vector.
Definition: vector.cc:85
double z
z coordinate of vector
Definition: vector.h:61