diff --git a/samples/main-grid-topology.cc b/samples/main-grid-topology.cc new file mode 100644 index 000000000..e2fb1c3e6 --- /dev/null +++ b/samples/main-grid-topology.cc @@ -0,0 +1,49 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ + +#include "ns3/ptr.h" +#include "ns3/grid-topology.h" +#include "ns3/static-mobility-model.h" +#include "ns3/internet-node.h" +#include "ns3/command-line.h" + +using namespace ns3; + +int main (int argc, char *argv[]) +{ + CommandLine::Parse (argc, argv); + + std::vector > nodes; + + // create an array of empty nodes for testing purposes + for (uint32_t i = 0; i < 120; i++) + { + nodes.push_back (Create ()); + } + + // setup the grid itself: objects are layed out + // started from (-100,-100) with 20 objects per row, + // the x interval between each object is 5 meters + // and the y interval between each object is 20 meters + GridTopology grid (-100, -100, 20, 5, 20); + + // each object will be attached a static position. + grid.SetMobilityModel (StaticMobilityModel::cid); + + // finalize the setup by attaching to each object + // in the input array a position and initializing + // this position with the calculated coordinates. + grid.LayoutRowFirst (nodes.begin (), nodes.end ()); + + // iterate our nodes and print their position. + for (std::vector >::const_iterator j = nodes.begin (); + j != nodes.end (); j++) + { + Ptr object = *j; + Ptr position = object->QueryInterface (MobilityModel::iid); + NS_ASSERT (position != 0); + Position pos = position->Get (); + std::cout << "x=" << pos.x << ", y=" << pos.y << ", z=" << pos.z << std::endl; + } + + return 0; +} diff --git a/samples/main-random-topology.cc b/samples/main-random-topology.cc new file mode 100644 index 000000000..05f7f26c1 --- /dev/null +++ b/samples/main-random-topology.cc @@ -0,0 +1,53 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ + +#include + +#include "ns3/ptr.h" +#include "ns3/mobility-model.h" +#include "ns3/mobility-model-notifier.h" +#include "ns3/static-mobility-model.h" +#include "ns3/random-topology.h" +#include "ns3/default-value.h" +#include "ns3/command-line.h" +#include "ns3/simulator.h" +#include "ns3/nstime.h" + +using namespace ns3; + +static void +CourseChange (Ptr position) +{ + Position pos = position->Get (); + std::cout << Simulator::Now () << ", pos=" << position << ", x=" << pos.x << ", y=" << pos.y + << ", z=" << pos.z << std::endl; +} + +int main (int argc, char *argv[]) +{ + Bind ("RandomDiscPositionX", "100"); + Bind ("RandomDiscPositionY", "50"); + Bind ("RandomDiscPositionRho", "Uniform:0:30"); + + Bind ("RandomTopologyPositionType", "RandomDiscPosition"); + Bind ("RandomTopologyMobilityType", "StaticMobilityModel"); + + CommandLine::Parse (argc, argv); + + RandomTopology topology; + + std::vector > objects; + for (uint32_t i = 0; i < 10000; i++) + { + Ptr notifier = Create (); + notifier->RegisterListener (MakeCallback (&CourseChange)); + objects.push_back (notifier); + } + + topology.Layout (objects.begin (), objects.end ()); + + Simulator::StopAt (Seconds (100.0)); + + Simulator::Run (); + + return 0; +} diff --git a/samples/wscript b/samples/wscript index 0c00491f0..9883cd24d 100644 --- a/samples/wscript +++ b/samples/wscript @@ -21,4 +21,8 @@ def build(bld): #obj = create_ns_prog('main-simple-p2p', 'main-simple-p2p.cc', deps=['node', 'p2p']) obj = create_ns_prog('main-default-value', 'main-default-value.cc', deps=['core', 'simulator', 'node', 'p2p']) + obj = create_ns_prog('main-grid-topology', 'main-grid-topology.cc', + deps=['core', 'simulator', 'mobility', 'internet-node']) + obj = create_ns_prog('main-random-topology', 'main-random-topology.cc', + deps=['core', 'simulator', 'mobility']) diff --git a/src/mobility/grid-topology.cc b/src/mobility/grid-topology.cc new file mode 100644 index 000000000..fbf7d2511 --- /dev/null +++ b/src/mobility/grid-topology.cc @@ -0,0 +1,66 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + */ +#include "grid-topology.h" +#include "static-mobility-model.h" + +namespace ns3 { + +GridTopology::GridTopology (double xMin, double yMin, uint32_t n, double deltaX, double deltaY) + : m_xMin (xMin), + m_yMin (yMin), + m_n (n), + m_deltaX (deltaX), + m_deltaY (deltaY), + m_positionClassId (StaticMobilityModel::cid) +{} + +void +GridTopology::SetMobilityModel (ClassId classId) +{ + m_positionClassId = classId; +} + +void +GridTopology::LayoutOneRowFirst (Ptr object, uint32_t i) +{ + double x, y; + x = m_xMin + m_deltaX * (i % m_n); + y = m_yMin + m_deltaY * (i / m_n); + Ptr mobility = ComponentManager::Create (m_positionClassId, + MobilityModel::iid); + object->AddInterface (mobility); + mobility->Set (Position (x, y, 0.0)); +} + +void +GridTopology::LayoutOneColumnFirst (Ptr object, uint32_t i) +{ + double x, y; + x = m_xMin + m_deltaX * (i / m_n); + y = m_yMin + m_deltaY * (i % m_n); + Ptr mobility = ComponentManager::Create (m_positionClassId, + MobilityModel::iid); + object->AddInterface (mobility); + mobility->Set (Position (x, y, 0.0)); +} + + +} // namespace ns3 diff --git a/src/mobility/grid-topology.h b/src/mobility/grid-topology.h new file mode 100644 index 000000000..93457503d --- /dev/null +++ b/src/mobility/grid-topology.h @@ -0,0 +1,120 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + */ +#ifndef GRID_TOPOLOGY_H +#define GRID_TOPOLOGY_H + +#include +#include "ns3/component-manager.h" +#include "ns3/ptr.h" + +namespace ns3 { + +/** + * \brief a 2D grid of objects + */ +class GridTopology +{ + public: + /** + * \param xMin the left boundary where the objects will start being arranged. + * \param yMin the lower boundary where the objects will start being arranged. + * \param n number of objects for each row or column + * \param deltaX distance separating two adjacent objects along the x axis. + * \param deltaY distance separating two adjacent objects along the y axis. + * + * The first object is positioned at (xMin,yMin). + */ + GridTopology (double xMin, double yMin, uint32_t n, double deltaX, double deltaY); + + /** + * \param classId the classId of the position object to attach to each + * input object. + */ + void SetMobilityModel (ClassId classId); + + /** + * \param begin an iterator to the first object to layout. + * \param end an iterator to the last object to layout. + * + * Attach a position (the type of position is specified through + * the ClassId given to SetMobilityModelModel) to each input object + * and configure its initial location with a set + * of coordinates arranged according to a regular rectangular grid, + * one row after the other. + */ + template + void LayoutRowFirst (const T &begin, const T &end); + + /** + * \param begin an iterator to the first object to layout. + * \param end an iterator to the last object to layout. + * + * Attach a position (the type of position is specified through + * the ClassId given to SetMobilityModelModel) to each input object + * and configure its initial location with a set + * of coordinates arranged according to a regular rectangular grid, + * one column after the other. + */ + template + void LayoutColumnFirst (const T &begin, const T &end); + private: + GridTopology (); + void LayoutOneRowFirst (Ptr object, uint32_t i); + void LayoutOneColumnFirst (Ptr object, uint32_t i); + double m_xMin; + double m_yMin; + uint32_t m_n; + double m_deltaX; + double m_deltaY; + ClassId m_positionClassId; +}; + +} // namespace ns3 + +namespace ns3 { + +template +void +GridTopology::LayoutRowFirst (const T &begin, const T &end) +{ + uint32_t j = 0; + for (T i = begin; i != end; i++) + { + j++; + LayoutOneRowFirst (*i, j); + } +} + +template +void +GridTopology::LayoutColumnFirst (const T &begin, const T &end) +{ + uint32_t j = 0; + for (T i = begin; i != end; i++) + { + j++; + LayoutOneColumnFirst (*i, j); + } +} + +} // namespace ns3 + +#endif /* GRID_TOPOLOGY_H */ diff --git a/src/mobility/hierarchical-mobility-model.cc b/src/mobility/hierarchical-mobility-model.cc new file mode 100644 index 000000000..7e4aa3fe3 --- /dev/null +++ b/src/mobility/hierarchical-mobility-model.cc @@ -0,0 +1,105 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + */ +#include "hierarchical-mobility-model.h" +#include "mobility-model-notifier.h" + +namespace ns3 { + +HierarchicalMobilityModel::HierarchicalMobilityModel (Ptr child, Ptr parent) + : m_child (child), + m_parent (parent) +{ + Ptr childNotifier = + m_child->QueryInterface (MobilityModelNotifier::iid); + Ptr parentNotifier = + m_parent->QueryInterface (MobilityModelNotifier::iid); + if (childNotifier == 0) + { + childNotifier = Create (); + child->AddInterface (childNotifier); + } + if (parentNotifier == 0) + { + parentNotifier = Create (); + parent->AddInterface (parentNotifier); + } + childNotifier->RegisterListener (MakeCallback (&HierarchicalMobilityModel::ChildChanged, this)); + parentNotifier->RegisterListener (MakeCallback (&HierarchicalMobilityModel::ParentChanged, this)); +} + +Ptr +HierarchicalMobilityModel::GetChild (void) const +{ + return m_child; +} + +Ptr +HierarchicalMobilityModel::GetParent (void) const +{ + return m_parent; +} + +Position +HierarchicalMobilityModel::DoGet (void) const +{ + Position parentPosition = m_parent->Get (); + Position childPosition = m_child->Get (); + return Position (parentPosition.x + childPosition.x, + parentPosition.y + childPosition.y, + parentPosition.z + childPosition.z); +} +void +HierarchicalMobilityModel::DoSet (const Position &position) +{ + // This implementation of DoSet is really an arbitraty choice. + // anything else would have been ok. + Position parentPosition = m_parent->Get (); + Position childPosition (position.x - parentPosition.x, + position.y - parentPosition.y, + position.z - parentPosition.z); + m_child->Set (childPosition); +} +Speed +HierarchicalMobilityModel::DoGetSpeed (void) const +{ + Speed parentSpeed = m_parent->GetSpeed (); + Speed childSpeed = m_child->GetSpeed (); + Speed speed (parentSpeed.dx + childSpeed.dx, + parentSpeed.dy + childSpeed.dy, + parentSpeed.dz + childSpeed.dz); + return speed; +} + +void +HierarchicalMobilityModel::ParentChanged (Ptr model) +{ + MobilityModel::NotifyCourseChange (); +} + +void +HierarchicalMobilityModel::ChildChanged (Ptr model) +{ + MobilityModel::NotifyCourseChange (); +} + + + +} // namespace ns3 diff --git a/src/mobility/hierarchical-mobility-model.h b/src/mobility/hierarchical-mobility-model.h new file mode 100644 index 000000000..1502c516a --- /dev/null +++ b/src/mobility/hierarchical-mobility-model.h @@ -0,0 +1,76 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + */ +#ifndef HIERARCHICAL_MOBILITY_MODEL_H +#define HIERARCHICAL_MOBILITY_MODEL_H + +#include "mobility-model.h" + +namespace ns3 { + +/** + * \brief a hierachical mobility model. + * + * This model allows you to specify the position of a + * child object relative to a parent object. + */ +class HierarchicalMobilityModel : public MobilityModel +{ +public: + static const InterfaceId iid; + + /** + * \param child the "relative" mobility model + * \param parent the "reference" mobility model + */ + HierarchicalMobilityModel (Ptr child, Ptr parent); + + /** + * \returns the child mobility model. + * + * This allows you to get access to the position of the child + * relative to its parent. + */ + Ptr GetChild (void) const; + /** + * \returns the parent mobility model. + * + * This allows you to get access to the position of the + * parent mobility model which is used as the reference + * position by the child mobility model. + */ + Ptr GetParent (void) const; + +private: + virtual Position DoGet (void) const; + virtual void DoSet (const Position &position); + virtual Speed DoGetSpeed (void) const; + + void ParentChanged (Ptr model); + void ChildChanged (Ptr model); + + Ptr m_child; + Ptr m_parent; +}; + + +} // namespace ns3 + +#endif /* HIERARCHICAL_MOBILITY_MODEL_H */ diff --git a/src/mobility/mobility-model-notifier.cc b/src/mobility/mobility-model-notifier.cc new file mode 100644 index 000000000..abb476c20 --- /dev/null +++ b/src/mobility/mobility-model-notifier.cc @@ -0,0 +1,68 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + */ +#include "mobility-model-notifier.h" + +namespace ns3 { + +const InterfaceId MobilityModelNotifier::iid = MakeInterfaceId ("MobilityModelNotifier", Object::iid); +const ClassId MobilityModelNotifier::cid = + MakeClassId ("MobilityModelNotifier", + MobilityModelNotifier::iid); + +MobilityModelNotifier::MobilityModelNotifier () +{ + SetInterfaceId (MobilityModelNotifier::iid); +} + +void +MobilityModelNotifier::RegisterListener (Listener listener) +{ + m_listeners.push_back (listener); +} +void +MobilityModelNotifier::UnregisterListener (Listener callback) +{ + for (std::list::iterator i = m_listeners.begin (); + i != m_listeners.end ();) + { + Listener listener = *i; + if (listener.IsEqual (callback)) + { + i = m_listeners.erase (i); + } + else + { + i++; + } + } +} +void +MobilityModelNotifier::Notify (Ptr position) const +{ + for (std::list::const_iterator i = m_listeners.begin (); + i != m_listeners.end (); i++) + { + Listener listener = *i; + listener (position); + } +} + +} // namespace ns3 diff --git a/src/mobility/mobility-model-notifier.h b/src/mobility/mobility-model-notifier.h new file mode 100644 index 000000000..8aec327de --- /dev/null +++ b/src/mobility/mobility-model-notifier.h @@ -0,0 +1,72 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + */ +#ifndef MOBILITY_MODEL_NOTIFIER_H +#define MOBILITY_MODEL_NOTIFIER_H + +#include "ns3/object.h" +#include "ns3/component-manager.h" +#include "ns3/callback.h" +#include "mobility-model.h" + +namespace ns3 { + +/** + * \brief notify listeners of position changes. + */ +class MobilityModelNotifier : public Object +{ +public: + static const InterfaceId iid; + static const ClassId cid; + + typedef Callback > Listener; + + /** + * Create a new position notifier + */ + MobilityModelNotifier (); + + /** + * \param position the position which just changed. + */ + void Notify (Ptr position) const; + + /** + * \param listener listener to add + * + * The listener will be notified upon every position change. + */ + void RegisterListener (Listener listener); + /** + * \param listener listener to remove + * + * The listener will not be notified anymore upon every + * position change. It is not an error to try to unregister + * a non-registered liste + */ + void UnregisterListener (Listener listener); +private: + std::list m_listeners; +}; + +} // namespace ns3 + +#endif /* MOBILITY_MODEL_NOTIFIER_H */ diff --git a/src/mobility/mobility-model.cc b/src/mobility/mobility-model.cc new file mode 100644 index 000000000..959e29a1e --- /dev/null +++ b/src/mobility/mobility-model.cc @@ -0,0 +1,73 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006,2007 INRIA + * All rights reserved. + * + * 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 + */ +#include "mobility-model.h" +#include "mobility-model-notifier.h" +#include + +namespace ns3 { + +const InterfaceId MobilityModel::iid = MakeInterfaceId ("MobilityModel", Object::iid); + +MobilityModel::MobilityModel () +{ + SetInterfaceId (MobilityModel::iid); +} + +MobilityModel::~MobilityModel () +{} + +Position +MobilityModel::Get (void) const +{ + return DoGet (); +} +Speed +MobilityModel::GetSpeed (void) const +{ + return DoGetSpeed (); +} + +void +MobilityModel::Set (const Position &position) +{ + DoSet (position); +} + +double +MobilityModel::GetDistanceFrom (const MobilityModel &other) const +{ + Position oPosition = other.DoGet (); + Position position = DoGet (); + return CalculateDistance (position, oPosition); +} + +void +MobilityModel::NotifyCourseChange (void) const +{ + Ptr notifier = + QueryInterface (MobilityModelNotifier::iid); + if (notifier != 0) + { + notifier->Notify (this); + } +} + +} // namespace ns3 diff --git a/src/mobility/mobility-model.h b/src/mobility/mobility-model.h new file mode 100644 index 000000000..4c4cf9000 --- /dev/null +++ b/src/mobility/mobility-model.h @@ -0,0 +1,93 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006,2007 INRIA + * All rights reserved. + * + * 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 + */ +#ifndef MOBILITY_MODEL_H +#define MOBILITY_MODEL_H + +#include "ns3/object.h" +#include "position.h" +#include "speed.h" + +namespace ns3 { + +/** + * \brief keep track of the current position of an object + * + * All space coordinates in this class and its subclasses are + * understood to be meters or meters/s. i.e., they are all + * metric international units. + */ +class MobilityModel : public Object +{ +public: + static const InterfaceId iid; + MobilityModel (); + virtual ~MobilityModel () = 0; + + /** + * \returns the current position + */ + Position Get (void) const; + /** + * \param position the position to set. + */ + void Set (const Position &position); + /** + * \returns the current position. + */ + Speed GetSpeed (void) const; + /** + * \param position a reference to another mobility model + * \returns the distance between the two objects. Unit is meters. + */ + double GetDistanceFrom (const MobilityModel &position) const; +protected: + /** + * Must be invoked by subclasses when the course of the + * position changes to notify course change listeners. + */ + void NotifyCourseChange (void) const; +private: + /** + * \returns the current position. + * + * Concrete subclasses of this base class must + * implement this method. + */ + virtual Position DoGet (void) const = 0; + /** + * \param position the position to set. + * + * Concrete subclasses of this base class must + * implement this method. + */ + virtual void DoSet (const Position &position) = 0; + /** + * \returns the current speed. + * + * Concrete subclasses of this base class must + * implement this method. + */ + virtual Speed DoGetSpeed (void) const = 0; +}; + +}; // namespace ns3 + +#endif /* MOBILITY_MODEL_H */ diff --git a/src/mobility/mobility.h b/src/mobility/mobility.h new file mode 100644 index 000000000..6d7a39f46 --- /dev/null +++ b/src/mobility/mobility.h @@ -0,0 +1,42 @@ +/** + * \defgroup mobility Mobility + * + * The mobility support includes: + * - a set of mobility models which are used to track and maintain + * the "current" cartesian position and speed of an object. + * + * - a "course change notifier" which can be used to register listeners + * to the course changes of a mobility model: ns3::MobilityModelNotifier. + * + * - a set of topology constructors which are used to set the initial + * position and associate a specific mobility model to a set of objects. + * + * The mobility models themselves are: + * - ns3::StaticMobilityModel: a model which maintains a constant position + * until it is changed by the user. + * + * - ns3::StaticSpeedMobilityModel: a model which maintains a constant speed + * until it is changed by the user. + * + * - ns3::HierarchicalMobilityModel: a model which calculates the current + * absolute position from a "reference" (parent) mobility model + * and a "relative" (child) mobility model. This allows users to + * compose mobility models. + * + * - ns3::RandomWalk2dMobilityModel: a 2d "brownian" motion mobility model + * where the bounds of the mobility area are a rectangle. + * + * - ns3::RandomWaypointMobilityModel: a 3d random waypoint mobility model. + * + * - ns3::RandomDirection2dMobilityModel: a 2d random direction mobility + * model where the bounds of the mobility are are a rectangle. + * + * The topology constructors: + * - ns3::GridTopology: layout objects in a 2d grid. + * + * - ns3::RandomTopology: layout objects in a 3d space, according to a + * RandomPosition model. + * + * - ns3::Ns2MobilityFileTopology: layout objects in a 3d space according + * to an ns2 CMU mobility file (as generated by the setdest tool). + */ diff --git a/src/mobility/position.cc b/src/mobility/position.cc new file mode 100644 index 000000000..802fe3bb7 --- /dev/null +++ b/src/mobility/position.cc @@ -0,0 +1,29 @@ +#include "position.h" +#include + +namespace ns3 { + + +Position::Position (double _x, double _y, double _z) + : x (_x), + y (_y), + z (_z) +{} + +Position::Position () + : x (0.0), + y (0.0), + z (0.0) +{} + +double +CalculateDistance (const Position &a, const Position &b) +{ + double dx = b.x - a.x; + double dy = b.y - a.y; + double dz = b.z - a.z; + double distance = std::sqrt (dx * dx + dy * dy + dz * dz); + return distance; +} + +} // namespace ns3 diff --git a/src/mobility/position.h b/src/mobility/position.h new file mode 100644 index 000000000..884ba50b0 --- /dev/null +++ b/src/mobility/position.h @@ -0,0 +1,44 @@ +#ifndef POSITION_H +#define POSITION_H + +namespace ns3 { + +/** + * \brief a 3d cartesian position vector + * + * Unit is meters. + */ +class Position +{ +public: + /** + * \param _x x coordinate of position vector + * \param _y y coordinate of position vector + * \param _z z coordinate of position vector + * + * Create position vector (_x, _y, _z) + */ + Position (double _x, double _y, double _z); + /** + * Create position vector (0.0, 0.0, 0.0) + */ + Position (); + /** + * x coordinate of position vector + */ + double x; + /** + * y coordinate of position vector + */ + double y; + /** + * z coordinate of position vector + */ + double z; +}; + +double CalculateDistance (const Position &a, const Position &b); + +} // namespace ns3 + +#endif /* POSITION_H */ diff --git a/src/mobility/random-position.cc b/src/mobility/random-position.cc new file mode 100644 index 000000000..93a3d825e --- /dev/null +++ b/src/mobility/random-position.cc @@ -0,0 +1,116 @@ +#include "random-position.h" +#include "ns3/random-variable.h" +#include "ns3/default-value.h" +#include "ns3/random-variable-default-value.h" +#include "ns3/debug.h" +#include + +NS_DEBUG_COMPONENT_DEFINE ("RandomPosition"); + +namespace ns3 { + +static RandomVariableDefaultValue +g_rectangleX ("RandomRectanglePositionX", + "A random variable which represents the x position of a position in a random rectangle.", + "Uniform:0:200"); + +static RandomVariableDefaultValue +g_rectangleY ("RandomRectanglePositionY", + "A random variable which represents the y position of a position in a random rectangle.", + "Uniform:0:200"); + +static RandomVariableDefaultValue +g_discTheta ("RandomDiscPositionTheta", + "A random variable which represents the angle (gradients) of a position in a random disc.", + "Uniform:0:6.2830"); + +static RandomVariableDefaultValue +g_discRho ("RandomDiscPositionRho", + "A random variable which represents the radius of a position in a random disc.", + "Uniform:0:200"); + +static NumericDefaultValue +g_discX ("RandomDiscPositionX", + "The x coordinate of the center of the random position disc.", + 0.0); + +static NumericDefaultValue +g_discY ("RandomDiscPositionY", + "The y coordinate of the center of the random position disc.", + 0.0); + +const InterfaceId RandomPosition::iid = MakeInterfaceId ("RandomPosition", Object::iid); + +const ClassId RandomRectanglePosition::cid = + MakeClassId ("RandomRectanglePosition", + RandomPosition::iid); +const ClassId RandomDiscPosition::cid = + MakeClassId ("RandomDiscPosition", + RandomPosition::iid); + +RandomPosition::RandomPosition () +{ + Object::SetInterfaceId (RandomPosition::iid); +} + +RandomPosition::~RandomPosition () +{} + +RandomRectanglePosition::RandomRectanglePosition () + : m_x (g_rectangleX.GetCopy ()), + m_y (g_rectangleY.GetCopy ()) +{} +RandomRectanglePosition::RandomRectanglePosition (const RandomVariable &x, + const RandomVariable &y) + : m_x (x.Copy ()), + m_y (y.Copy ()) +{} +RandomRectanglePosition::~RandomRectanglePosition () +{ + delete m_x; + delete m_y; + m_x = 0; + m_y = 0; +} +Position +RandomRectanglePosition::Get (void) const +{ + double x = m_x->GetValue (); + double y = m_y->GetValue (); + return Position (x, y, 0.0); +} + +RandomDiscPosition::RandomDiscPosition () + : m_theta (g_discTheta.GetCopy ()), + m_rho (g_discRho.GetCopy ()), + m_x (g_discX.GetValue ()), + m_y (g_discY.GetValue ()) +{} +RandomDiscPosition::RandomDiscPosition (const RandomVariable &theta, + const RandomVariable &rho, + double x, double y) + : m_theta (theta.Copy ()), + m_rho (rho.Copy ()), + m_x (0.0), + m_y (0.0) +{} +RandomDiscPosition::~RandomDiscPosition () +{ + delete m_theta; + delete m_rho; + m_theta = 0; + m_rho = 0; +} +Position +RandomDiscPosition::Get (void) const +{ + double theta = m_theta->GetValue (); + double rho = m_rho->GetValue (); + double x = m_x + std::cos (theta) * rho; + double y = m_y + std::sin (theta) * rho; + NS_DEBUG ("Disc position x=" << x << ", y=" << y); + return Position (x, y, 0.0); +} + + +} // namespace ns3 diff --git a/src/mobility/random-position.h b/src/mobility/random-position.h new file mode 100644 index 000000000..ee97a2286 --- /dev/null +++ b/src/mobility/random-position.h @@ -0,0 +1,96 @@ +#ifndef RANDOM_POSITION_H +#define RANDOM_POSITION_H + +#include "ns3/object.h" +#include "ns3/component-manager.h" +#include "position.h" + +namespace ns3 { + +class RandomVariable; + +/** + * \brief choose a position at random. + * + * This is a pure abstract base class. + */ +class RandomPosition : public Object +{ +public: + static const InterfaceId iid; + RandomPosition (); + virtual ~RandomPosition (); + /** + * \returns the next randomly-choosen position. + */ + virtual Position Get (void) const = 0; +}; + +/** + * \brief allocate random positions within a rectangle + * according to a pair of random variables. + */ +class RandomRectanglePosition : public RandomPosition +{ +public: + static const ClassId cid; + /** + * Create a random position model based on the + * Bind default values. + */ + RandomRectanglePosition (); + /** + * \param x the random variable which is used to choose + * the x coordinates. + * \param y the random variable which is used to choose + * the y coordinates. + */ + RandomRectanglePosition (const RandomVariable &x, + const RandomVariable &y); + virtual ~RandomRectanglePosition (); + virtual Position Get (void) const; +private: + RandomVariable *m_x; + RandomVariable *m_y; +}; + +/** + * \brief allocate random positions within a disc + * according to a pair of random variables. + */ +class RandomDiscPosition : public RandomPosition +{ +public: + static const ClassId cid; + /** + * Create a random position model based on the + * Bind default values. + */ + RandomDiscPosition (); + /** + * \param theta the random variable used to pick + * the angle of the random position in polar + * coordinates. + * \param rho the random variable used to pick the + * radius of the random position in polar + * coordinates. + * \param x the x coordinate of the center of the + * polar coodinate system. + * \param y the y coordinate of the center of the + * polar coodinate system. + */ + RandomDiscPosition (const RandomVariable &theta, + const RandomVariable &rho, + double x, double y); + virtual ~RandomDiscPosition (); + virtual Position Get (void) const; +private: + RandomVariable *m_theta; + RandomVariable *m_rho; + double m_x; + double m_y; +}; + +} // namespace ns3 + +#endif /* RANDOM_POSITION_H */ diff --git a/src/mobility/random-topology.cc b/src/mobility/random-topology.cc new file mode 100644 index 000000000..f04ce2b56 --- /dev/null +++ b/src/mobility/random-topology.cc @@ -0,0 +1,78 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + */ +#include "ns3/random-variable-default-value.h" +#include "random-topology.h" +#include "random-position.h" +#include "mobility-model.h" + +namespace ns3 { + +static ClassIdDefaultValue +g_position ("RandomTopologyPositionType", + "The type of initial random position in a 3d topology.", + RandomPosition::iid, + "RandomRectanglePosition"); + +static ClassIdDefaultValue +g_mobility ("RandomTopologyMobilityType", + "The type of mobility model attached to an object in a 3d topology.", + MobilityModel::iid, + "StaticMobilityModel"); + +RandomTopology::RandomTopology () + : m_mobilityModel (g_mobility.GetValue ()) +{ + m_positionModel = ComponentManager::Create (g_position.GetValue (), + RandomPosition::iid); +} +RandomTopology::RandomTopology (Ptr positionModel, ClassId mobilityModel) + : m_positionModel (positionModel), + m_mobilityModel (mobilityModel) +{} +RandomTopology::~RandomTopology () +{ + m_positionModel = 0; +} + +void +RandomTopology::SetMobilityModel (ClassId classId) +{ + m_mobilityModel = classId; +} + +void +RandomTopology::SetPositionModel (Ptr positionModel) +{ + m_positionModel = positionModel; +} + +void +RandomTopology::LayoutOne (Ptr object) +{ + Ptr mobility = ComponentManager::Create (m_mobilityModel, + MobilityModel::iid); + object->AddInterface (mobility); + Position position = m_positionModel->Get (); + mobility->Set (position); +} + + +} // namespace ns3 diff --git a/src/mobility/random-topology.h b/src/mobility/random-topology.h new file mode 100644 index 000000000..a955839e0 --- /dev/null +++ b/src/mobility/random-topology.h @@ -0,0 +1,113 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + */ +#ifndef RANDOM_TOPOLOGY_H +#define RANDOM_TOPOLOGY_H + +#include "ns3/ptr.h" +#include "ns3/object.h" +#include "ns3/component-manager.h" + +namespace ns3 { + +class RandomPosition; + +/** + * \brief layout objects randomly in 3d space. + * + * This assigns an initial position to each object + * according to its position model and assigns + * an instance of a mobility model to each object + * according to its mobility model class id. + */ +class RandomTopology +{ + public: + /** + * Create a default random topology based + * on Bind configuration. + */ + RandomTopology (); + /** + * \param positionModel model to set the initial position + * of each object. + * \param mobilityModel type of mobility model to attach to each object. + * + * Create a random topology based on the + * specified position and mobility models. + */ + RandomTopology (Ptr positionModel, + ClassId mobilityModel); + + ~RandomTopology (); + + /** + * \param classId the type of mobility model attached to each + * input object if it does not have one already. + */ + void SetMobilityModel (ClassId classId); + /** + * \param positionModel the position model used to initialize + * the position of each object. + */ + void SetPositionModel (Ptr positionModel); + + /** + * \param object the object to layout + * + * Assign an initial position and a mobility model + * to the object. + */ + void LayoutOne (Ptr object); + + /** + * \param begin iterator which identifies the first + * object to configure. + * \param end iterator which identifies the last + * object to configure. + * + * Assign an initial position and a mobility model + * to the objects. + */ + template + void Layout (const T &begin, const T &end); + private: + Ptr m_positionModel; + ClassId m_mobilityModel; +}; + +} // namespace ns3 + +namespace ns3 { + +template +void +RandomTopology::Layout (const T &begin, const T &end) +{ + for (T i = begin; i != end; i++) + { + LayoutOne (*i); + } +} + + +} // namespace ns3 + +#endif /* RANDOM_TOPOLOGY_H */ diff --git a/src/mobility/rectangle-default-value.cc b/src/mobility/rectangle-default-value.cc new file mode 100644 index 000000000..1526f5a8e --- /dev/null +++ b/src/mobility/rectangle-default-value.cc @@ -0,0 +1,93 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + */ + +#include "rectangle-default-value.h" + +namespace ns3 { + +RectangleDefaultValue::RectangleDefaultValue (std::string name, + std::string help, + double xMin, double xMax, + double yMin, double yMax) + : DefaultValueBase (name, help), + m_default (xMin, xMax, yMin, yMax), + m_rectangle (xMin, xMax, yMin, yMax) +{ + DefaultValueList::Add (this); +} + +Rectangle +RectangleDefaultValue::GetValue (void) const +{ + return m_rectangle; +} +double +RectangleDefaultValue::ReadDouble (std::string str, bool &ok) +{ + double value; + std::istringstream iss; + iss.str (str); + iss >> value; + if (iss.bad () || iss.fail ()) + { + ok = false; + } + return value; +} +bool +RectangleDefaultValue::DoParseValue (const std::string &value) +{ + std::string::size_type xMinStart = 0; + std::string::size_type xMinEnd = value.find_first_of(":", xMinStart); + std::string::size_type xMaxStart = xMinEnd + 1; + std::string::size_type xMaxEnd = value.find_first_of(":", xMaxStart); + std::string::size_type yMinStart = xMaxEnd + 1; + std::string::size_type yMinEnd = value.find_first_of(":", yMinStart); + std::string::size_type yMaxStart = yMinEnd + 1; + std::string::size_type yMaxEnd = std::string::npos; + + std::string xMinString = value.substr (xMinStart, xMinEnd); + std::string xMaxString = value.substr (xMaxStart, xMaxEnd); + std::string yMinString = value.substr (yMinStart, yMinEnd); + std::string yMaxString = value.substr (yMaxStart, yMaxEnd); + + bool ok = true; + m_rectangle.xMin = ReadDouble (xMinString, ok); + m_rectangle.yMin = ReadDouble (yMinString, ok); + m_rectangle.xMax = ReadDouble (xMaxString, ok); + m_rectangle.yMax = ReadDouble (yMaxString, ok); + return ok; +} +std::string +RectangleDefaultValue::DoGetType (void) const +{ + return "(xMin:xMax:yMin:yMax)"; +} +std::string +RectangleDefaultValue::DoGetDefaultValue (void) const +{ + std::ostringstream oss; + oss << m_default.xMin << ":" << m_default.xMax << ":" << m_default.yMin << ":" << m_default.yMax; + return oss.str (); +} + + +} // namespace ns3 diff --git a/src/mobility/rectangle-default-value.h b/src/mobility/rectangle-default-value.h new file mode 100644 index 000000000..047e7bbb6 --- /dev/null +++ b/src/mobility/rectangle-default-value.h @@ -0,0 +1,51 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + */ +#ifndef RECTANGLE_DEFAULT_VALUE_H +#define RECTANGLE_DEFAULT_VALUE_H + +#include +#include "ns3/default-value.h" +#include "rectangle.h" + +namespace ns3 { + +class RectangleDefaultValue : public DefaultValueBase +{ + public: + RectangleDefaultValue (std::string name, + std::string help, + double xMin, double xMax, + double yMin, double yMax); + + Rectangle GetValue (void) const; + private: + double ReadDouble (std::string str, bool &ok); + virtual bool DoParseValue (const std::string &value); + virtual std::string DoGetType (void) const; + virtual std::string DoGetDefaultValue (void) const; + + Rectangle m_default; + Rectangle m_rectangle; +}; + +} // namespace ns3 + +#endif /* RECTANGLE_DEFAULT_VALUE_H */ diff --git a/src/mobility/rectangle.cc b/src/mobility/rectangle.cc new file mode 100644 index 000000000..34e23e58b --- /dev/null +++ b/src/mobility/rectangle.cc @@ -0,0 +1,123 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + */ +#include "rectangle.h" +#include "position.h" +#include "speed.h" +#include "ns3/assert.h" +#include +#include + +namespace ns3 { + +Rectangle::Rectangle (double _xMin, double _xMax, + double _yMin, double _yMax) + : xMin (_xMin), + xMax (_xMax), + yMin (_yMin), + yMax (_yMax) +{} + +Rectangle::Rectangle () + : xMin (0.0), + xMax (0.0), + yMin (0.0), + yMax (0.0) +{} + +bool +Rectangle::IsInside (const Position &position) const +{ + return + position.x <= xMax && position.x >= xMin && + position.y <= yMax && position.y >= yMin; +} + +Rectangle::Side +Rectangle::GetClosestSide (const Position &position) const +{ + double xMinDist = std::abs (position.x - xMin); + double xMaxDist = std::abs (xMax - position.x); + double yMinDist = std::abs (position.y - yMin); + double yMaxDist = std::abs (yMax - position.y); + double minX = std::min (xMinDist, xMaxDist); + double minY = std::min (yMinDist, yMaxDist); + if (minX < minY) + { + if (xMinDist < xMaxDist) + { + return LEFT; + } + else + { + return RIGHT; + } + } + else + { + if (yMinDist < yMaxDist) + { + return BOTTOM; + } + else + { + return TOP; + } + } +} + +Position +Rectangle::CalculateIntersection (const Position ¤t, const Speed &speed) const +{ + double xMaxY = current.y + (xMax - current.x) / speed.dx * speed.dy; + double xMinY = current.y + (xMin - current.x) / speed.dx * speed.dy; + double yMaxX = current.x + (yMax - current.y) / speed.dy * speed.dx; + double yMinX = current.x + (yMin - current.y) / speed.dy * speed.dx; + bool xMaxOk = xMaxY <= yMax && xMaxY >= yMin; + bool xMinOk = xMinY <= yMax && xMinY >= yMin; + bool yMaxOk = yMaxX <= xMax && yMaxX >= xMin; + bool yMinOk = yMinX <= xMax && yMinX >= xMin; + if (xMaxOk && speed.dx >= 0) + { + return Position (xMax, xMaxY, 0.0); + } + else if (xMinOk && speed.dx <= 0) + { + return Position (xMin, xMinY, 0.0); + } + else if (yMaxOk && speed.dy >= 0) + { + return Position (yMaxX, yMax, 0.0); + } + else if (yMinOk && speed.dy <= 0) + { + return Position (yMinX, yMin, 0.0); + } + else + { + NS_ASSERT (false); + // quiet compiler + return Position (0.0, 0.0, 0.0); + } + +} + + +} // namespace ns3 diff --git a/src/mobility/rectangle.h b/src/mobility/rectangle.h new file mode 100644 index 000000000..a53f641d6 --- /dev/null +++ b/src/mobility/rectangle.h @@ -0,0 +1,67 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * 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 + */ +#ifndef RECTANGLE_H +#define RECTANGLE_H + +namespace ns3 { + +class Position; +class Speed; + +/** + * \brief a 2d rectangle + */ +class Rectangle +{ +public: + enum Side { + RIGHT, + LEFT, + TOP, + BOTTOM + }; + /** + * \param _xMin x coordinates of left boundary. + * \param _xMax x coordinates of right boundary. + * \param _yMin y coordinates of bottom boundary. + * \param _yMax y coordinates of top boundary. + * + * Create a rectangle. + */ + Rectangle (double _xMin, double _xMax, + double _yMin, double _yMax); + /** + * Create a zero-sized rectangle located at coordinates (0.0,0.0) + */ + Rectangle (); + bool IsInside (const Position &position) const; + Side GetClosestSide (const Position &position) const; + Position CalculateIntersection (const Position ¤t, const Speed &speed) const; + + double xMin; + double xMax; + double yMin; + double yMax; +}; + +} // namespace ns3 + +#endif /* RECTANGLE_H */ diff --git a/src/mobility/speed.cc b/src/mobility/speed.cc new file mode 100644 index 000000000..3f97ded7c --- /dev/null +++ b/src/mobility/speed.cc @@ -0,0 +1,17 @@ +#include "speed.h" + +namespace ns3 { + +Speed::Speed (double _dx, double _dy, double _dz) + : dx (_dx), + dy (_dy), + dz (_dz) +{} + +Speed::Speed () + : dx (0.0), + dy (0.0), + dz (0.0) +{} + +} // namespace ns3 diff --git a/src/mobility/speed.h b/src/mobility/speed.h new file mode 100644 index 000000000..5e2ad3c45 --- /dev/null +++ b/src/mobility/speed.h @@ -0,0 +1,42 @@ +#ifndef SPEED_H +#define SPEED_H + +namespace ns3 { + +/** + * \brief keep track of 3d cartesian speed vectors + * + * Unit is meters/s. + */ +class Speed +{ +public: + /** + * \param _dx x coordinate of speed vector + * \param _dy y coordinate of speed vector + * \param _dz z coordinate of speed vector + * + * Create speed vector (_dx, _dy, _dz) + */ + Speed (double _dx, double _dy, double _dz); + /** + * Create speed vector (0.0, 0.0, 0.0) + */ + Speed (); + /** + * x coordinate of speed vector + */ + double dx; + /** + * y coordinate of speed vector + */ + double dy; + /** + * z coordinate of speed vector + */ + double dz; +}; + +} // namespace ns3 + +#endif /* SPEED_H */ diff --git a/src/mobility/static-mobility-model.cc b/src/mobility/static-mobility-model.cc new file mode 100644 index 000000000..e6d68f5c3 --- /dev/null +++ b/src/mobility/static-mobility-model.cc @@ -0,0 +1,57 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006 INRIA + * All rights reserved. + * + * 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 + */ +#include "static-mobility-model.h" + +namespace ns3 { + +const ClassId StaticMobilityModel::cid = MakeClassId ("StaticMobilityModel", + MobilityModel::iid); + +StaticMobilityModel::StaticMobilityModel () +{ + SetInterfaceId (StaticMobilityModel::iid); +} +StaticMobilityModel::StaticMobilityModel (const Position &position) + : m_position (position) +{ + SetInterfaceId (StaticMobilityModel::iid); +} +StaticMobilityModel::~StaticMobilityModel () +{} + +Position +StaticMobilityModel::DoGet (void) const +{ + return m_position; +} +void +StaticMobilityModel::DoSet (const Position &position) +{ + m_position = position; + NotifyCourseChange (); +} +Speed +StaticMobilityModel::DoGetSpeed (void) const +{ + return Speed (); +} + +}; // namespace ns3 diff --git a/src/mobility/static-mobility-model.h b/src/mobility/static-mobility-model.h new file mode 100644 index 000000000..d70890cc0 --- /dev/null +++ b/src/mobility/static-mobility-model.h @@ -0,0 +1,61 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006,2007 INRIA + * All rights reserved. + * + * 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 + */ +#ifndef STATIC_MOBILITY_MODEL_H +#define STATIC_MOBILITY_MODEL_H + +#include "ns3/component-manager.h" +#include "mobility-model.h" + +namespace ns3 { + +/** + * \brief a position model for which the current position does not + * change once it has been set and until it is set again + * explicitely to a new value. + */ +class StaticMobilityModel : public MobilityModel +{ +public: + static const ClassId cid; + /** + * Create a position located at coordinates (0,0,0) + */ + StaticMobilityModel (); + /** + * \param position the initial position. + * + * Create a position located at coordinates (x,y,z). + * Unit is meters + */ + StaticMobilityModel (const Position &position); + virtual ~StaticMobilityModel (); + +private: + virtual Position DoGet (void) const; + virtual void DoSet (const Position &position); + virtual Speed DoGetSpeed (void) const; + + Position m_position; +}; + +}; // namespace ns3 + +#endif /* STATIC_MOBILITY_MODEL_H */ diff --git a/src/mobility/static-speed-helper.cc b/src/mobility/static-speed-helper.cc new file mode 100644 index 000000000..b46f88721 --- /dev/null +++ b/src/mobility/static-speed-helper.cc @@ -0,0 +1,100 @@ +#include "ns3/simulator.h" +#include "ns3/rectangle.h" +#include "static-speed-helper.h" + +namespace ns3 { + +StaticSpeedHelper::StaticSpeedHelper () +{} +StaticSpeedHelper::StaticSpeedHelper (const Position &position) + : m_position (position) +{} +StaticSpeedHelper::StaticSpeedHelper (const Position &position, + const Speed &speed) + : m_position (position), + m_speed (speed) +{} +void +StaticSpeedHelper::InitializePosition (const Position &position) +{ + m_position = position; + m_speed.dx = 0.0; + m_speed.dy = 0.0; + m_speed.dz = 0.0; + m_lastUpdate = Simulator::Now (); + m_pauseEnd = Simulator::Now (); +} + +void +StaticSpeedHelper::Reset (const Speed &speed, const Time &pauseDelay) +{ + Reset (speed); + m_pauseEnd = Simulator::Now () + pauseDelay; +} + +Position +StaticSpeedHelper::GetCurrentPosition (void) const +{ + Update (); + return m_position; +} + +Speed +StaticSpeedHelper::GetSpeed (void) const +{ + return m_speed; +} +void +StaticSpeedHelper::SetSpeed (const Speed &speed) +{ + Update (); + m_speed = speed; +} + +void +StaticSpeedHelper::Update (void) const +{ + Time now = Simulator::Now (); + if (m_pauseEnd > now) + { + return; + } + Time last = std::max (now, m_pauseEnd); + if (m_lastUpdate >= last) + { + return; + } + Time deltaTime = now - last; + m_lastUpdate = now; + double deltaS = deltaTime.GetSeconds (); + m_position.x += m_speed.dx * deltaS; + m_position.y += m_speed.dy * deltaS; + m_position.z += m_speed.dz * deltaS; +} + +void +StaticSpeedHelper::Reset (const Speed &speed) +{ + Update (); + m_speed = speed; + m_pauseEnd = Simulator::Now (); +} +void +StaticSpeedHelper::UpdateFull (const Rectangle &bounds) const +{ + Update (); + m_position.x = std::min (bounds.xMax, m_position.x); + m_position.x = std::max (bounds.xMin, m_position.x); + m_position.y = std::min (bounds.yMax, m_position.y); + m_position.y = std::max (bounds.yMin, m_position.y); +} + +Position +StaticSpeedHelper::GetCurrentPosition (const Rectangle &bounds) const +{ + UpdateFull (bounds); + return m_position; +} + + +} // namespace ns3 diff --git a/src/mobility/static-speed-helper.h b/src/mobility/static-speed-helper.h new file mode 100644 index 000000000..0ba541b75 --- /dev/null +++ b/src/mobility/static-speed-helper.h @@ -0,0 +1,39 @@ +#ifndef STATIC_SPEED_HELPER_H +#define STATIC_SPEED_HELPER_H + +#include "ns3/nstime.h" +#include "position.h" +#include "speed.h" + +namespace ns3 { + +class Rectangle; + +class StaticSpeedHelper +{ + public: + StaticSpeedHelper (); + StaticSpeedHelper (const Position &position); + StaticSpeedHelper (const Position &position, + const Speed &speed); + void InitializePosition (const Position &position); + + void Reset (const Speed &speed, const Time &pauseDelay); + void Reset (const Speed &speed); + Position GetCurrentPosition (const Rectangle &bounds) const; + Position GetCurrentPosition (void) const; + Speed GetSpeed (void) const; + void SetSpeed (const Speed &speed); + + private: + void Update (void) const; + void UpdateFull (const Rectangle &rectangle) const; + mutable Time m_lastUpdate; + mutable Position m_position; + Speed m_speed; + Time m_pauseEnd; +}; + +} // namespace ns3 + +#endif /* STATIC_SPEED_HELPER_H */ diff --git a/src/mobility/static-speed-mobility-model.cc b/src/mobility/static-speed-mobility-model.cc new file mode 100644 index 000000000..d4cfc9873 --- /dev/null +++ b/src/mobility/static-speed-mobility-model.cc @@ -0,0 +1,77 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006, 2007 INRIA + * All rights reserved. + * + * 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 + */ +#include "static-speed-mobility-model.h" +#include "ns3/simulator.h" + +namespace ns3 { + +const InterfaceId StaticSpeedMobilityModel::iid = + MakeInterfaceId ("StaticSpeedMobilityModel", MobilityModel::iid); +const ClassId StaticSpeedMobilityModel::cid = + MakeClassId ("StaticSpeedMobilityModel", + StaticSpeedMobilityModel::iid); + + +StaticSpeedMobilityModel::StaticSpeedMobilityModel () +{ + SetInterfaceId (StaticSpeedMobilityModel::iid); +} +StaticSpeedMobilityModel::StaticSpeedMobilityModel (const Position &position) + : m_helper (position) +{ + SetInterfaceId (StaticSpeedMobilityModel::iid); +} +StaticSpeedMobilityModel::StaticSpeedMobilityModel (const Position &position, + const Speed &speed) + : m_helper (position, speed) +{ + SetInterfaceId (StaticSpeedMobilityModel::iid); +} + +StaticSpeedMobilityModel::~StaticSpeedMobilityModel () +{} + +void +StaticSpeedMobilityModel::SetSpeed (const Speed speed) +{ + m_helper.SetSpeed (speed); + NotifyCourseChange (); +} + + +Position +StaticSpeedMobilityModel::DoGet (void) const +{ + return m_helper.GetCurrentPosition (); +} +void +StaticSpeedMobilityModel::DoSet (const Position &position) +{ + m_helper.InitializePosition (position); + NotifyCourseChange (); +} +Speed +StaticSpeedMobilityModel::DoGetSpeed (void) const +{ + return m_helper.GetSpeed (); +} + +}; // namespace ns3 diff --git a/src/mobility/static-speed-mobility-model.h b/src/mobility/static-speed-mobility-model.h new file mode 100644 index 000000000..74f5b51f9 --- /dev/null +++ b/src/mobility/static-speed-mobility-model.h @@ -0,0 +1,80 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006, 2007 INRIA + * All rights reserved. + * + * 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 + */ +#ifndef STATIC_SPEED_MOBILITY_MODEL_H +#define STATIC_SPEED_MOBILITY_MODEL_H + +#include +#include "mobility-model.h" +#include "ns3/nstime.h" +#include "ns3/component-manager.h" +#include "static-speed-helper.h" +#include "speed.h" + +namespace ns3 { + +/** + * \brief a position model for which the current speed does not + * change once it has been set and until it is set again + * explicitely to a new value. + */ +class StaticSpeedMobilityModel : public MobilityModel +{ +public: + static const InterfaceId iid; + static const ClassId cid; + /** + * Create position located at coordinates (0,0,0) with + * speed (0,0,0). + */ + StaticSpeedMobilityModel (); + /** + * Create a position located at coordinates (x,y,z) with + * speed (0,0,0). + */ + StaticSpeedMobilityModel (const Position &position); + /** + * + * Create a position located at coordinates (x,y,z) with + * speed (dx,dy,dz). + * Unit is meters and meters/s + */ + StaticSpeedMobilityModel (const Position &position, + const Speed &speed); + virtual ~StaticSpeedMobilityModel (); + + /** + * \param speed the new speed to set. + * + * Set the current speed now to (dx,dy,dz) + * Unit is meters/s + */ + void SetSpeed (const Speed speed); +private: + virtual Position DoGet (void) const; + virtual void DoSet (const Position &position); + virtual Speed DoGetSpeed (void) const; + void Update (void) const; + StaticSpeedHelper m_helper; +}; + +}; // namespace ns3 + +#endif /* STATIC_SPEED_POSITION */ diff --git a/src/mobility/wscript b/src/mobility/wscript new file mode 100644 index 000000000..99f732fc2 --- /dev/null +++ b/src/mobility/wscript @@ -0,0 +1,40 @@ +## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- + +def build(bld): + mobility = bld.create_obj('cpp', 'shlib') + mobility.name = 'ns3-mobility' + mobility.target = mobility.name + mobility.uselib_local = ['ns3-core', 'ns3-simulator'] + mobility.source = [ + 'grid-topology.cc', + 'hierarchical-mobility-model.cc', + 'mobility-model.cc', + 'mobility-model-notifier.cc', + 'position.cc', + 'random-position.cc', + 'random-topology.cc', + 'rectangle.cc', + 'rectangle-default-value.cc', + 'speed.cc', + 'static-mobility-model.cc', + 'static-speed-helper.cc', + 'static-speed-mobility-model.cc', + ] + mobility.includes = '.' + + headers = bld.create_obj('ns3header') + headers.source = [ + 'grid-topology.h', + 'hierarchical-mobility-model.h', + 'mobility-model.h', + 'mobility-model-notifier.h', + 'position.h', + 'random-position.h', + 'random-topology.h', + 'rectangle.h', + 'rectangle-default-value.h', + 'speed.h', + 'static-mobility-model.h', + 'static-speed-helper.h', + 'static-speed-mobility-model.h', + ] diff --git a/src/wscript b/src/wscript index 72344847e..1547429d7 100644 --- a/src/wscript +++ b/src/wscript @@ -17,6 +17,7 @@ all_modules = [ 'internet-node', 'devices/p2p', 'applications', + 'mobility', ]