introduce Vector2D and Vector3D

This commit is contained in:
Mathieu Lacage
2009-08-14 11:53:23 +02:00
parent 9bfce1c7b2
commit 7e5b776cda
12 changed files with 179 additions and 98 deletions

View File

@@ -24,23 +24,39 @@
namespace ns3 {
ATTRIBUTE_HELPER_CPP (Vector);
ATTRIBUTE_HELPER_CPP (Vector3D);
ATTRIBUTE_HELPER_CPP (Vector2D);
// compatibility for mobility code
Ptr<const AttributeChecker> MakeVectorChecker (void)
{
return MakeVector3DChecker ();
}
Vector::Vector (double _x, double _y, double _z)
Vector3D::Vector3D (double _x, double _y, double _z)
: x (_x),
y (_y),
z (_z)
{}
Vector::Vector ()
Vector3D::Vector3D ()
: x (0.0),
y (0.0),
z (0.0)
{}
Vector2D::Vector2D (double _x, double _y)
: x (_x),
y (_y)
{}
Vector2D::Vector2D ()
: x (0.0),
y (0.0)
{}
double
CalculateDistance (const Vector &a, const Vector &b)
CalculateDistance (const Vector3D &a, const Vector3D &b)
{
double dx = b.x - a.x;
double dy = b.y - a.y;
@@ -48,13 +64,21 @@ CalculateDistance (const Vector &a, const Vector &b)
double distance = std::sqrt (dx * dx + dy * dy + dz * dz);
return distance;
}
double
CalculateDistance (const Vector2D &a, const Vector2D &b)
{
double dx = b.x - a.x;
double dy = b.y - a.y;
double distance = std::sqrt (dx * dx + dy * dy);
return distance;
}
std::ostream &operator << (std::ostream &os, const Vector &vector)
std::ostream &operator << (std::ostream &os, const Vector3D &vector)
{
os << vector.x << ":" << vector.y << ":" << vector.z;
return os;
}
std::istream &operator >> (std::istream &is, Vector &vector)
std::istream &operator >> (std::istream &is, Vector3D &vector)
{
char c1, c2;
is >> vector.x >> c1 >> vector.y >> c2 >> vector.z;
@@ -65,5 +89,20 @@ std::istream &operator >> (std::istream &is, Vector &vector)
}
return is;
}
std::ostream &operator << (std::ostream &os, const Vector2D &vector)
{
os << vector.x << ":" << vector.y;
return os;
}
std::istream &operator >> (std::istream &is, Vector2D &vector)
{
char c1;
is >> vector.x >> c1 >> vector.y;
if (c1 != ':')
{
is.setstate (std::ios_base::failbit);
}
return is;
}
} // namespace ns3

126
src/core/vector.h Normal file
View File

@@ -0,0 +1,126 @@
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2007 INRIA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
#ifndef NS3_VECTOR_H
#define NS3_VECTOR_H
#include "ns3/attribute.h"
#include "ns3/attribute-helper.h"
namespace ns3 {
/**
* \brief a 3d vector
*/
class Vector3D
{
public:
/**
* \param _x x coordinate of vector
* \param _y y coordinate of vector
* \param _z z coordinate of vector
*
* Create vector (_x, _y, _z)
*/
Vector3D (double _x, double _y, double _z);
/**
* Create vector (0.0, 0.0, 0.0)
*/
Vector3D ();
/**
* x coordinate of vector
*/
double x;
/**
* y coordinate of vector
*/
double y;
/**
* z coordinate of vector
*/
double z;
};
/**
* \brief a 3d vector
*/
class Vector2D
{
public:
/**
* \param _x x coordinate of vector
* \param _y y coordinate of vector
*
* Create vector (_x, _y)
*/
Vector2D (double _x, double _y);
/**
* Create vector vector (0.0, 0.0)
*/
Vector2D ();
/**
* x coordinate of vector
*/
double x;
/**
* y coordinate of vector
*/
double y;
};
/**
* \param a one point
* \param b another point
* \returns the cartesian distance between a and b.
*/
double CalculateDistance (const Vector3D &a, const Vector3D &b);
/**
* \param a one point
* \param b another point
* \returns the cartesian distance between a and b.
*/
double CalculateDistance (const Vector2D &a, const Vector2D &b);
/**
* \class ns3::Vector3DValue
* \brief hold objects of type ns3::Vector3D
*/
/**
* \class ns3::Vector2DValue
* \brief hold objects of type ns3::Vector2D
*/
ATTRIBUTE_HELPER_HEADER (Vector3D);
ATTRIBUTE_HELPER_HEADER (Vector2D);
std::ostream &operator << (std::ostream &os, const Vector3D &vector);
std::istream &operator >> (std::istream &is, Vector3D &vector);
std::ostream &operator << (std::ostream &os, const Vector2D &vector);
std::istream &operator >> (std::istream &is, Vector2D &vector);
// for compatibility with mobility models
typedef Vector3D Vector;
typedef Vector3DValue VectorValue;
typedef Vector3DChecker VectorChecker;
ATTRIBUTE_ACCESSOR_DEFINE(Vector);
Ptr<const AttributeChecker> MakeVectorChecker (void);
} // namespace ns3
#endif /* NS3_VECTOR_H */

View File

@@ -78,6 +78,7 @@ def build(bld):
'config.cc',
'callback.cc',
'names.cc',
'vector.cc',
]
headers = bld.new_task_gen('ns3header')
@@ -123,6 +124,7 @@ def build(bld):
'deprecated.h',
'abort.h',
'names.h',
'vector.h',
]
if sys.platform == 'win32':

View File

@@ -21,7 +21,6 @@
#define CONSTANT_POSITION_MOBILITY_MODEL_H
#include "mobility-model.h"
#include "vector.h"
namespace ns3 {

View File

@@ -21,7 +21,7 @@
#define CONSTANT_VELOCITY_HELPER_H
#include "ns3/nstime.h"
#include "vector.h"
#include "ns3/vector.h"
namespace ns3 {

View File

@@ -21,8 +21,8 @@
#define CONSTANT_VELOCITY_MOBILITY_MODEL_H
#include <stdint.h>
#include "mobility-model.h"
#include "ns3/nstime.h"
#include "mobility-model.h"
#include "constant-velocity-helper.h"
namespace ns3 {

View File

@@ -20,8 +20,7 @@
#ifndef MOBILITY_MODEL_H
#define MOBILITY_MODEL_H
#include "vector.h"
#include "ns3/vector.h"
#include "ns3/object.h"
#include "ns3/traced-callback.h"

View File

@@ -22,7 +22,7 @@
#include "ns3/object.h"
#include "ns3/random-variable.h"
#include "vector.h"
#include "ns3/vector.h"
namespace ns3 {

View File

@@ -18,7 +18,7 @@
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
#include "rectangle.h"
#include "vector.h"
#include "ns3/vector.h"
#include "ns3/assert.h"
#include "ns3/fatal-error.h"
#include <cmath>

View File

@@ -22,11 +22,10 @@
#include "ns3/attribute.h"
#include "ns3/attribute-helper.h"
#include "ns3/vector.h"
namespace ns3 {
class Vector;
/**
* \brief a 2d rectangle
*/

View File

@@ -1,81 +0,0 @@
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2007 INRIA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
#ifndef VECTOR_H
#define VECTOR_H
#include "ns3/attribute.h"
#include "ns3/attribute-helper.h"
namespace ns3 {
/**
* \brief a 3d cartesian position vector
*
* Unit is meters.
*/
class Vector
{
public:
/**
* \param _x x coordinate of vector vector
* \param _y y coordinate of vector vector
* \param _z z coordinate of vector vector
*
* Create vector vector (_x, _y, _z)
*/
Vector (double _x, double _y, double _z);
/**
* Create vector vector (0.0, 0.0, 0.0)
*/
Vector ();
/**
* x coordinate of vector vector
*/
double x;
/**
* y coordinate of vector vector
*/
double y;
/**
* z coordinate of vector vector
*/
double z;
};
/**
* \param a one point
* \param b another point
* \returns the cartesian distance between a and b.
*/
double CalculateDistance (const Vector &a, const Vector &b);
/**
* \class ns3::VectorValue
* \brief hold objects of type ns3::Vector
*/
ATTRIBUTE_HELPER_HEADER (Vector);
std::ostream &operator << (std::ostream &os, const Vector &vector);
std::istream &operator >> (std::istream &is, Vector &vector);
} // namespace ns3
#endif /* VECTOR_H */

View File

@@ -3,7 +3,6 @@
def build(bld):
mobility = bld.create_ns3_module('mobility', ['core', 'simulator'])
mobility.source = [
'vector.cc',
'hierarchical-mobility-model.cc',
'mobility-model.cc',
'position-allocator.cc',
@@ -20,7 +19,6 @@ def build(bld):
headers = bld.new_task_gen('ns3header')
headers.module = 'mobility'
headers.source = [
'vector.h',
'hierarchical-mobility-model.h',
'mobility-model.h',
'position-allocator.h',