change GridTopology API and add RandomRectangle topology with similar API.
--- a/SConstruct Tue Jul 03 09:15:20 2007 +0200
+++ b/SConstruct Tue Jul 03 10:04:16 2007 +0200
@@ -249,6 +249,7 @@
'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 @@
'static-position.h',
'static-speed-position.h',
'grid-topology.h',
+ 'random-rectangle-topology.h',
'random-walk-position.h',
])
--- a/samples/main-grid-topology.cc Tue Jul 03 09:15:20 2007 +0200
+++ b/samples/main-grid-topology.cc Tue Jul 03 10:04:16 2007 +0200
@@ -31,8 +31,7 @@
// 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<Ptr<Object> >::const_iterator j = nodes.begin ();
--- a/src/node/grid-topology.cc Tue Jul 03 09:15:20 2007 +0200
+++ b/src/node/grid-topology.cc Tue Jul 03 10:04:16 2007 +0200
@@ -39,51 +39,22 @@
}
void
-GridTopology::ArrangeHorizontally (std::vector<Ptr<Object> > objects)
+GridTopology::LayoutOneRowFirst (Ptr<Object> object, uint32_t i)
{
double x, y;
- uint32_t col;
- x = m_xMin;
- y = m_yMin;
- col = 0;
- for (std::vector<Ptr<Object> >::const_iterator i = objects.begin ();
- i != objects.end (); i++)
- {
- Ptr<Object> 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<Ptr<Object> > objects)
+GridTopology::LayoutOneColumnFirst (Ptr<Object> object, uint32_t i)
{
double x, y;
- uint32_t row;
- x = m_xMin;
- y = m_yMin;
- row = 0;
- for (std::vector<Ptr<Object> >::const_iterator i = objects.begin ();
- i != objects.end (); i++)
- {
- Ptr<Object> 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
--- a/src/node/grid-topology.h Tue Jul 03 09:15:20 2007 +0200
+++ b/src/node/grid-topology.h Tue Jul 03 10:04:16 2007 +0200
@@ -59,7 +59,8 @@
* of coordinates arranged according to a regular rectangular grid,
* one row after the other.
*/
- void ArrangeHorizontally (std::vector<Ptr<Object> > objects);
+ template <typename T>
+ void LayoutRowFirst (const T &begin, const T &end);
/**
* \param objects a vector of objects
@@ -70,9 +71,12 @@
* of coordinates arranged according to a regular rectangular grid,
* one column after the other.
*/
- void ArrangeVertically (std::vector<Ptr<Object> > objects);
+ template <typename T>
+ void LayoutColumnFirst (const T &begin, const T &end);
private:
GridTopology ();
+ void LayoutOneRowFirst (Ptr<Object> object, uint32_t i);
+ void LayoutOneColumnFirst (Ptr<Object> object, uint32_t i);
double m_xMin;
double m_yMin;
uint32_t m_n;
@@ -83,4 +87,32 @@
} // namespace ns3
+namespace ns3 {
+
+template <typename T>
+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 <typename T>
+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 */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/node/random-rectangle-topology.cc Tue Jul 03 10:04:16 2007 +0200
@@ -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 <mathieu.lacage@sophia.inria.fr>
+ */
+#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> object)
+{
+ double x = m_xVariable->GetValue ();
+ double y = m_yVariable->GetValue ();
+ object->AddInterface (ComponentManager::Create (m_positionModel, x, y));
+}
+
+
+} // namespace ns3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/node/random-rectangle-topology.h Tue Jul 03 10:04:16 2007 +0200
@@ -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 <mathieu.lacage@sophia.inria.fr>
+ */
+#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> object);
+
+ template <typename T>
+ void Layout (const T &begin, const T &end);
+ private:
+ RandomVariable *m_xVariable;
+ RandomVariable *m_yVariable;
+ ClassId m_positionModel;
+};
+
+} // namespace ns3
+
+namespace ns3 {
+
+template <typename T>
+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 */