1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 INRIA
4  * Copyright (C) 2012 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20  * Author: Nicola Baldo <nbaldo@cttc.es> (took position-allocator and turned it into building-allocator)
21  */
22 #include "building-allocator.h"
23 #include "ns3/building.h"
24 #include "ns3/double.h"
25 #include "ns3/uinteger.h"
26 #include "ns3/enum.h"
27 #include "ns3/log.h"
28 #include <cmath>
29 
30 namespace ns3 {
31 
32 NS_LOG_COMPONENT_DEFINE ("BuildingAllocator");
33 
34 NS_OBJECT_ENSURE_REGISTERED (GridBuildingAllocator);
35 
GridBuildingAllocator()36 GridBuildingAllocator::GridBuildingAllocator ()
37   : m_current (0)
38 {
39   m_buildingFactory.SetTypeId ("ns3::Building");
40   m_lowerLeftPositionAllocator = CreateObject<GridPositionAllocator> ();
41   m_upperRightPositionAllocator = CreateObject<GridPositionAllocator> ();
42 }
43 
~GridBuildingAllocator()44 GridBuildingAllocator::~GridBuildingAllocator ()
45 {
46 }
47 
48 TypeId
GetTypeId(void)49 GridBuildingAllocator::GetTypeId (void)
50 {
51   static TypeId tid = TypeId ("ns3::GridBuildingAllocator")
52     .SetParent<Object> ()
53     .AddConstructor<GridBuildingAllocator> ()
54     .SetGroupName ("Buildings")
55     .AddAttribute ("GridWidth", "The number of objects laid out on a line.",
56                    UintegerValue (10),
57                    MakeUintegerAccessor (&GridBuildingAllocator::m_n),
58                    MakeUintegerChecker<uint32_t> ())
59     .AddAttribute ("MinX", "The x coordinate where the grid starts.",
60                    DoubleValue (1.0),
61                    MakeDoubleAccessor (&GridBuildingAllocator::m_xMin),
62                    MakeDoubleChecker<double> ())
63     .AddAttribute ("MinY", "The y coordinate where the grid starts.",
64                    DoubleValue (0.0),
65                    MakeDoubleAccessor (&GridBuildingAllocator::m_yMin),
66                    MakeDoubleChecker<double> ())
67     .AddAttribute ("LengthX", " the length of the wall of each building along the X axis.",
68                    DoubleValue (1.0),
69                    MakeDoubleAccessor (&GridBuildingAllocator::m_lengthX),
70                    MakeDoubleChecker<double> ())
71     .AddAttribute ("LengthY", " the length of the wall of each building along the X axis.",
72                    DoubleValue (1.0),
73                    MakeDoubleAccessor (&GridBuildingAllocator::m_lengthY),
74                    MakeDoubleChecker<double> ())
75     .AddAttribute ("DeltaX", "The x space between buildings.",
76                    DoubleValue (1.0),
77                    MakeDoubleAccessor (&GridBuildingAllocator::m_deltaX),
78                    MakeDoubleChecker<double> ())
79     .AddAttribute ("DeltaY", "The y space between buildings.",
80                    DoubleValue (1.0),
81                    MakeDoubleAccessor (&GridBuildingAllocator::m_deltaY),
82                    MakeDoubleChecker<double> ())
83     .AddAttribute ("Height", "The height of the building (roof level)",
84                    DoubleValue (10),
85                    MakeDoubleAccessor (&GridBuildingAllocator::m_height),
86                    MakeDoubleChecker<double> ())
87     .AddAttribute ("LayoutType", "The type of layout.",
88                    EnumValue (GridPositionAllocator::ROW_FIRST),
89                    MakeEnumAccessor (&GridBuildingAllocator::m_layoutType),
90                    MakeEnumChecker (GridPositionAllocator::ROW_FIRST, "RowFirst",
91                                     GridPositionAllocator::COLUMN_FIRST, "ColumnFirst"))
92   ;
93   return tid;
94 }
95 
96 void
SetBuildingAttribute(std::string n,const AttributeValue & v)97 GridBuildingAllocator::SetBuildingAttribute (std::string n, const AttributeValue &v)
98 {
99   NS_LOG_FUNCTION (this);
100   m_buildingFactory.Set (n, v);
101 }
102 
103 BuildingContainer
Create(uint32_t n) const104 GridBuildingAllocator::Create (uint32_t n) const
105 {
106   NS_LOG_FUNCTION (this);
107   PushAttributes ();
108   BuildingContainer bc;
109   uint32_t limit = n + m_current;
110   for (; m_current < limit; ++m_current)
111     {
112       Vector lowerLeft = m_lowerLeftPositionAllocator->GetNext ();
113       Vector upperRight = m_upperRightPositionAllocator->GetNext ();
114       Box box (lowerLeft.x, upperRight.x, lowerLeft.y, upperRight.y, 0, m_height);
115       NS_LOG_LOGIC ("new building : " <<  box);
116       BoxValue boxValue (box);
117       m_buildingFactory.Set ("Boundaries", boxValue);
118       Ptr<Building> b  = m_buildingFactory.Create<Building> ();
119       bc.Add (b);
120     }
121   return bc;
122 }
123 
124 void
PushAttributes() const125 GridBuildingAllocator::PushAttributes () const
126 {
127   NS_LOG_FUNCTION (this);
128   m_lowerLeftPositionAllocator->SetMinX (m_xMin);
129   m_upperRightPositionAllocator->SetMinX (m_xMin + m_lengthX);
130   m_lowerLeftPositionAllocator->SetDeltaX (m_lengthX + m_deltaX);
131   m_upperRightPositionAllocator->SetDeltaX (m_lengthX + m_deltaX);
132 
133   m_lowerLeftPositionAllocator->SetMinY (m_yMin);
134   m_upperRightPositionAllocator->SetMinY (m_yMin + m_lengthY);
135   m_lowerLeftPositionAllocator->SetDeltaY (m_lengthY + m_deltaY);
136   m_upperRightPositionAllocator->SetDeltaY (m_lengthY + m_deltaY);
137 
138   m_lowerLeftPositionAllocator->SetLayoutType (m_layoutType);
139   m_upperRightPositionAllocator->SetLayoutType (m_layoutType);
140 
141   m_lowerLeftPositionAllocator->SetN (m_n);
142   m_upperRightPositionAllocator->SetN (m_n);
143 }
144 
145 
146 } // namespace ns3
147