From 9a89cf1978342759e49d2ce2bdaf46f848160580 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 10:04:16 +0200 Subject: [PATCH] change GridTopology API and add RandomRectangle topology with similar API. --- SConstruct | 2 + samples/main-grid-topology.cc | 3 +- src/node/grid-topology.cc | 47 ++++-------------- src/node/grid-topology.h | 36 +++++++++++++- src/node/random-rectangle-topology.cc | 60 +++++++++++++++++++++++ src/node/random-rectangle-topology.h | 69 +++++++++++++++++++++++++++ 6 files changed, 175 insertions(+), 42 deletions(-) create mode 100644 src/node/random-rectangle-topology.cc create mode 100644 src/node/random-rectangle-topology.h diff --git a/SConstruct b/SConstruct index 7342503d2..9a18bbf93 100644 --- a/SConstruct +++ b/SConstruct @@ -249,6 +249,7 @@ node.add_sources ([ 'static-position.cc', 'static-speed-position.cc', 'grid-topology.cc', + 'random-rectangle-topology.cc', 'random-walk-position.cc', ]) node.add_inst_headers ([ @@ -272,6 +273,7 @@ node.add_inst_headers ([ 'static-position.h', 'static-speed-position.h', 'grid-topology.h', + 'random-rectangle-topology.h', 'random-walk-position.h', ]) diff --git a/samples/main-grid-topology.cc b/samples/main-grid-topology.cc index 0da0c0bd0..20ce064d9 100644 --- a/samples/main-grid-topology.cc +++ b/samples/main-grid-topology.cc @@ -31,8 +31,7 @@ int main (int argc, char *argv[]) // finalize the setup by attaching to each object // in the input array a position and initializing // this position with the calculated coordinates. - grid.ArrangeHorizontally (nodes); - + grid.LayoutRowFirst (nodes.begin (), nodes.end ()); // iterate our nodes and print their position. for (std::vector >::const_iterator j = nodes.begin (); diff --git a/src/node/grid-topology.cc b/src/node/grid-topology.cc index d4e637f95..a621c7105 100644 --- a/src/node/grid-topology.cc +++ b/src/node/grid-topology.cc @@ -39,51 +39,22 @@ GridTopology::SetPositionModel (ClassId classId) } void -GridTopology::ArrangeHorizontally (std::vector > objects) +GridTopology::LayoutOneRowFirst (Ptr object, uint32_t i) { double x, y; - uint32_t col; - x = m_xMin; - y = m_yMin; - col = 0; - for (std::vector >::const_iterator i = objects.begin (); - i != objects.end (); i++) - { - Ptr object = *i; - object->AddInterface (ComponentManager::Create (m_positionClassId, x, y)); - x += m_deltaX; - col++; - if (col == m_n) - { - col = 0; - x = m_xMin; - y += m_deltaY; - } - } + x = m_xMin + m_deltaX * (i % m_n); + y = m_yMin + m_deltaY * (i / m_n); + object->AddInterface (ComponentManager::Create (m_positionClassId, x, y)); } void -GridTopology::ArrangeVertically (std::vector > objects) +GridTopology::LayoutOneColumnFirst (Ptr object, uint32_t i) { double x, y; - uint32_t row; - x = m_xMin; - y = m_yMin; - row = 0; - for (std::vector >::const_iterator i = objects.begin (); - i != objects.end (); i++) - { - Ptr object = *i; - object->AddInterface (ComponentManager::Create (m_positionClassId, x, y)); - y += m_deltaY; - row++; - if (row == m_n) - { - row = 0; - y = m_yMin; - x += m_deltaX; - } - } + x = m_xMin + m_deltaX * (i / m_n); + y = m_yMin + m_deltaY * (i % m_n); + object->AddInterface (ComponentManager::Create (m_positionClassId, x, y)); } + } // namespace ns3 diff --git a/src/node/grid-topology.h b/src/node/grid-topology.h index 822024038..f2cc724a1 100644 --- a/src/node/grid-topology.h +++ b/src/node/grid-topology.h @@ -59,7 +59,8 @@ class GridTopology * of coordinates arranged according to a regular rectangular grid, * one row after the other. */ - void ArrangeHorizontally (std::vector > objects); + template + void LayoutRowFirst (const T &begin, const T &end); /** * \param objects a vector of objects @@ -70,9 +71,12 @@ class GridTopology * of coordinates arranged according to a regular rectangular grid, * one column after the other. */ - void ArrangeVertically (std::vector > objects); + 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; @@ -83,4 +87,32 @@ class GridTopology } // 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/node/random-rectangle-topology.cc b/src/node/random-rectangle-topology.cc new file mode 100644 index 000000000..b35122b1f --- /dev/null +++ b/src/node/random-rectangle-topology.cc @@ -0,0 +1,60 @@ +/* -*- 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 "random-rectangle-topology.h" +#include "static-position.h" + +namespace ns3 { + +RandomRectangleTopology::RandomRectangleTopology (double xMin, double xMax, double yMin, double yMax) + : m_xVariable (new UniformVariable (xMin, xMax)), + m_yVariable (new UniformVariable (yMin, yMax)), + m_positionModel (StaticPosition::cid) +{} +RandomRectangleTopology::RandomRectangleTopology (const RandomVariable &xVariable, + const RandomVariable &yVariable) + : m_xVariable (xVariable.Copy ()), + m_yVariable (yVariable.Copy ()), + m_positionModel (StaticPosition::cid) +{} +RandomRectangleTopology::~RandomRectangleTopology () +{ + delete m_xVariable; + delete m_yVariable; + m_xVariable = 0; + m_yVariable = 0; +} + +void +RandomRectangleTopology::SetPositionModel (ClassId classId) +{ + m_positionModel = classId; +} + +void +RandomRectangleTopology::LayoutOne (Ptr object) +{ + double x = m_xVariable->GetValue (); + double y = m_yVariable->GetValue (); + object->AddInterface (ComponentManager::Create (m_positionModel, x, y)); +} + + +} // namespace ns3 diff --git a/src/node/random-rectangle-topology.h b/src/node/random-rectangle-topology.h new file mode 100644 index 000000000..6568a993e --- /dev/null +++ b/src/node/random-rectangle-topology.h @@ -0,0 +1,69 @@ +/* -*- 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_RECTANGLE_TOPOLOGY_H +#define RANDOM_RECTANGLE_TOPOLOGY_H + +#include "ns3/random-variable.h" +#include "ns3/ptr.h" +#include "ns3/object.h" +#include "ns3/component-manager.h" + +namespace ns3 { + +class RandomRectangleTopology +{ + public: + + RandomRectangleTopology (double xMin, double xMax, double yMin, double yMax); + RandomRectangleTopology (const RandomVariable &xVariable, const RandomVariable &yVariable); + + ~RandomRectangleTopology (); + + void SetPositionModel (ClassId classId); + + void LayoutOne (Ptr object); + + template + void Layout (const T &begin, const T &end); + private: + RandomVariable *m_xVariable; + RandomVariable *m_yVariable; + ClassId m_positionModel; +}; + +} // namespace ns3 + +namespace ns3 { + +template +void +RandomRectangleTopology::Layout (const T &begin, const T &end) +{ + for (T i = begin; i != end; i++) + { + LayoutOne (*i); + } +} + + +} // namespace ns3 + +#endif /* RANDOM_RECTANGLE_TOPOLOGY_H */