# HG changeset patch # User Nicola Baldo # Date 1335779597 -7200 # Node ID a7407f3773a146b0cb555213d09b173768c5d9ac # Parent 0ff7e5c7fb18a0a8206319707d40fcce2bac9384 GridBuildingAllocator now reusing GridPositionAllocator diff -r 0ff7e5c7fb18 -r a7407f3773a1 src/buildings/helper/building-allocator.cc --- a/src/buildings/helper/building-allocator.cc Mon Apr 30 11:47:34 2012 +0200 +++ b/src/buildings/helper/building-allocator.cc Mon Apr 30 11:53:17 2012 +0200 @@ -1,6 +1,7 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* * Copyright (c) 2007 INRIA + * Copyright (C) 2012 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC) * * 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 @@ -16,6 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author: Mathieu Lacage + * Author: Nicola Baldo (took position-allocator and turned it into building-allocator) */ #include "building-allocator.h" #include "ns3/building.h" @@ -36,6 +38,8 @@ : m_current (0) { m_buildingFactory.SetTypeId ("ns3::Building"); + m_lowerLeftPositionAllocator = CreateObject (); + m_upperRightPositionAllocator = CreateObject (); } GridBuildingAllocator::~GridBuildingAllocator () @@ -68,11 +72,11 @@ DoubleValue (1.0), MakeDoubleAccessor (&GridBuildingAllocator::m_lengthY), MakeDoubleChecker ()) - .AddAttribute ("DeltaX", "The x space between objects.", + .AddAttribute ("DeltaX", "The x space between buildings.", DoubleValue (1.0), MakeDoubleAccessor (&GridBuildingAllocator::m_deltaX), MakeDoubleChecker ()) - .AddAttribute ("DeltaY", "The y space between objects.", + .AddAttribute ("DeltaY", "The y space between buildings.", DoubleValue (1.0), MakeDoubleAccessor (&GridBuildingAllocator::m_deltaY), MakeDoubleChecker ()) @@ -81,88 +85,15 @@ MakeDoubleAccessor (&GridBuildingAllocator::m_height), MakeDoubleChecker ()) .AddAttribute ("LayoutType", "The type of layout.", - EnumValue (ROW_FIRST), + EnumValue (GridPositionAllocator::ROW_FIRST), MakeEnumAccessor (&GridBuildingAllocator::m_layoutType), - MakeEnumChecker (ROW_FIRST, "RowFirst", - COLUMN_FIRST, "ColumnFirst")) + MakeEnumChecker (GridPositionAllocator::ROW_FIRST, "RowFirst", + GridPositionAllocator::COLUMN_FIRST, "ColumnFirst")) ; return tid; } void -GridBuildingAllocator::SetMinX (double xMin) -{ - m_xMin = xMin; -} -void -GridBuildingAllocator::SetMinY (double yMin) -{ - m_yMin = yMin; -} -void -GridBuildingAllocator::SetLengthX (double lengthX) -{ - m_lengthX = lengthX; -} -void -GridBuildingAllocator::SetLengthY (double lengthY) -{ - m_lengthY = lengthY; -} - -void -GridBuildingAllocator::SetDeltaX (double deltaX) -{ - m_deltaX = deltaX; -} -void -GridBuildingAllocator::SetDeltaY (double deltaY) -{ - m_deltaY = deltaY; -} -void -GridBuildingAllocator::SetN (uint32_t n) -{ - m_n = n; -} -void -GridBuildingAllocator::SetLayoutType (enum LayoutType layoutType) -{ - m_layoutType = layoutType; -} - -double -GridBuildingAllocator::GetMinX (void) const -{ - return m_xMin; -} -double -GridBuildingAllocator::GetMinY (void) const -{ - return m_yMin; -} -double -GridBuildingAllocator::GetDeltaX (void) const -{ - return m_deltaX; -} -double -GridBuildingAllocator::GetDeltaY (void) const -{ - return m_deltaY; -} -uint32_t -GridBuildingAllocator::GetN (void) const -{ - return m_n; -} -enum GridBuildingAllocator::LayoutType -GridBuildingAllocator::GetLayoutType (void) const -{ - return m_layoutType; -} - -void GridBuildingAllocator::SetBuildingAttribute (std::string n, const AttributeValue &v) { NS_LOG_FUNCTION (this); @@ -172,34 +103,44 @@ BuildingContainer GridBuildingAllocator::Create (uint32_t n) const { + NS_LOG_FUNCTION (this); + PushAttributes (); BuildingContainer bc; uint32_t limit = n + m_current; for (; m_current < limit; ++m_current) { - double bxmin = 0.0; - double bymin = 0.0; - switch (m_layoutType) { - case ROW_FIRST: - bxmin = m_xMin + (m_deltaX + m_lengthX) * (m_current % m_n); - bymin = m_yMin + (m_deltaY + m_lengthY) * (m_current / m_n); - break; - case COLUMN_FIRST: - bxmin = m_xMin + (m_deltaX + m_lengthX) * (m_current / m_n); - bymin = m_yMin + (m_deltaY + m_lengthY) * (m_current % m_n); - break; - } - double bxmax = bxmin + m_lengthX; - double bymax = bymin + m_lengthY; - Box box (bxmin, bxmax, bymin, bymax, 0, m_height); + Vector lowerLeft = m_lowerLeftPositionAllocator->GetNext (); + Vector upperRight = m_upperRightPositionAllocator->GetNext (); + Box box (lowerLeft.x, upperRight.x, lowerLeft.y, upperRight.y, 0, m_height); NS_LOG_LOGIC ("new building : " << box); BoxValue boxValue (box); m_buildingFactory.Set ("Boundaries", boxValue); Ptr b = m_buildingFactory.Create (); - //b->SetAttribute ("Boundaries", boxValue); bc.Add (b); } return bc; } +void +GridBuildingAllocator::PushAttributes () const +{ + NS_LOG_FUNCTION (this); + m_lowerLeftPositionAllocator->SetMinX (m_xMin); + m_upperRightPositionAllocator->SetMinX (m_xMin + m_lengthX); + m_lowerLeftPositionAllocator->SetDeltaX (m_lengthX + m_deltaX); + m_upperRightPositionAllocator->SetDeltaX (m_lengthX + m_deltaX); + + m_lowerLeftPositionAllocator->SetMinY (m_yMin); + m_upperRightPositionAllocator->SetMinY (m_yMin + m_lengthY); + m_lowerLeftPositionAllocator->SetDeltaY (m_lengthY + m_deltaY); + m_upperRightPositionAllocator->SetDeltaY (m_lengthY + m_deltaY); + + m_lowerLeftPositionAllocator->SetLayoutType (m_layoutType); + m_upperRightPositionAllocator->SetLayoutType (m_layoutType); + + m_lowerLeftPositionAllocator->SetN (m_n); + m_upperRightPositionAllocator->SetN (m_n); +} + } // namespace ns3 diff -r 0ff7e5c7fb18 -r a7407f3773a1 src/buildings/helper/building-allocator.h --- a/src/buildings/helper/building-allocator.h Mon Apr 30 11:47:34 2012 +0200 +++ b/src/buildings/helper/building-allocator.h Mon Apr 30 11:53:17 2012 +0200 @@ -27,6 +27,7 @@ #include "ns3/random-variable.h" #include "ns3/vector.h" #include "ns3/building-container.h" +#include "ns3/position-allocator.h" namespace ns3 { @@ -36,6 +37,10 @@ /** * \ingroup buildings * \brief Allocate buildings on a rectangular 2d grid. + * + * This class allows to create a set of buildings positioned on a + * rectangular 2D grid. Under the hood, this class uses two instances + * of GridPositionAllocator. */ class GridBuildingAllocator : public Object { @@ -46,91 +51,6 @@ // inherited from Object static TypeId GetTypeId (void); - /** - * Determine whether buildings are allocated row first or column first. - */ - enum LayoutType { - /** - * In row-first mode, buildings are allocated on the first row until - * N buildings have been allocated. Then, the second row located a yMin + yDelta - * is used to allocate buildings. - */ - ROW_FIRST, - /** - * In column-first mode, buildings are allocated on the first column until - * N buildings have been allocated. Then, the second column located a xMin + xDelta - * is used to allocate buildings. - */ - COLUMN_FIRST - }; - - /** - * \param xMin the x coordinate where layout will start. - */ - void SetMinX (double xMin); - /** - * \param yMin the y coordinate where layout will start - */ - void SetMinY (double yMin); - /** - * \param lengthX the length of the wall of each building along the X axis. - */ - void SetLengthX (double lengthX); - /** - * \param lengthY the length of the wall of each building along the X axis. - */ - void SetLengthY (double lengthY); - /** - * \param deltaX the x interval between two x-consecutive buildings. - */ - void SetDeltaX (double deltaX); - /** - * \param deltaY the y interval between two y-consecutive buildings. - */ - void SetDeltaY (double deltaY); - /** - * \param n the number of buildings allocated on each row (or each column) - * before switching to the next column (or row). - */ - void SetN (uint32_t n); - /** - * \param layoutType the type of layout to use (row first or column first). - */ - void SetLayoutType (enum LayoutType layoutType); - - /** - * \return the x coordinate of the first allocated building. - */ - double GetMinX (void) const; - /** - * \return the y coordinate of the first allocated building. - */ - double GetMinY (void) const; - /** - * \return the length of the wall along the X axis - */ - double GetLengthX (void) const; - /** - * \return the length of the wall along the Y axis - */ - double GetLengthY (void) const; - /** - * \return the x interval between two x-consecutive buildings. - */ - double GetDeltaX (void) const; - /** - * \return the y interval between two y-consecutive buildings. - */ - double GetDeltaY (void) const; - /** - * \return the number of buildings to allocate on each row or each column. - */ - uint32_t GetN (void) const; - /** - * \return the currently-selected layout type. - */ - enum LayoutType GetLayoutType (void) const; - /** * Set an attribute to be used for each new building to be created * @@ -149,8 +69,10 @@ BuildingContainer Create (uint32_t n) const; private: + + void PushAttributes () const; mutable uint32_t m_current; - enum LayoutType m_layoutType; + enum GridPositionAllocator::LayoutType m_layoutType; double m_xMin; double m_yMin; uint32_t m_n; @@ -161,6 +83,9 @@ double m_height; mutable ObjectFactory m_buildingFactory; + Ptr m_lowerLeftPositionAllocator; + Ptr m_upperRightPositionAllocator; + }; } // namespace ns3