A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
building-position-allocator.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (C) 2012 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Nicola Baldo <nbaldo@cttc.es>
19  */
20 #include "building-position-allocator.h"
21 #include "ns3/buildings-mobility-model.h"
22 #include "ns3/buildings-helper.h"
23 #include "ns3/random-variable-stream.h"
24 #include "ns3/double.h"
25 #include "ns3/uinteger.h"
26 #include "ns3/enum.h"
27 #include "ns3/boolean.h"
28 #include "ns3/log.h"
29 #include "ns3/box.h"
30 #include "ns3/building.h"
31 #include <cmath>
32 
33 #include "ns3/building-list.h"
34 
35 NS_LOG_COMPONENT_DEFINE ("BuildingPositionAllocator");
36 
37 namespace ns3 {
38 
39 NS_OBJECT_ENSURE_REGISTERED (RandomBuildingPositionAllocator);
40 
41 
42 RandomBuildingPositionAllocator::RandomBuildingPositionAllocator ()
43 {
44  m_rand = CreateObject<UniformRandomVariable> ();
45 }
46 
47 TypeId
48 RandomBuildingPositionAllocator::GetTypeId (void)
49 {
50  static TypeId tid = TypeId ("ns3::RandomBuildingPositionAllocator")
51  .SetParent<PositionAllocator> ()
52  .SetGroupName ("Mobility")
53  .AddConstructor<RandomBuildingPositionAllocator> ()
54  .AddAttribute ("WithReplacement",
55  "If true, the building will be randomly selected with replacement. "
56  "If false, no replacement will occur, until the list of buildings "
57  "to select becomes empty, at which point it will be filled again "
58  "with the list of all buildings.",
59  BooleanValue (false),
60  MakeBooleanAccessor (&RandomBuildingPositionAllocator::m_withReplacement),
61  MakeBooleanChecker ());
62  return tid;
63 }
64 
65 Vector
67 {
68  NS_ASSERT_MSG (BuildingList::GetNBuildings () > 0, "no building found");
69  Ptr<Building> b;
70  if (m_withReplacement)
71  {
72  uint32_t n = m_rand->GetInteger (0, BuildingList::GetNBuildings () - 1);
74  }
75  else
76  {
77  if (m_buildingListWithoutReplacement.empty ())
78  {
79  for (BuildingList::Iterator bit = BuildingList::Begin (); bit != BuildingList::End (); ++bit)
80  {
81  m_buildingListWithoutReplacement.push_back (*bit);
82  }
83  }
84  uint32_t n = m_rand->GetInteger (0, m_buildingListWithoutReplacement.size () - 1);
85  b = m_buildingListWithoutReplacement.at (n);
86  m_buildingListWithoutReplacement.erase (m_buildingListWithoutReplacement.begin () + n);
87  }
88 
89  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
90  BoxValue bv;
91  b->GetAttribute ("Boundaries", bv);
92  double x = m_rand->GetValue (bv.Get ().xMin, bv.Get ().xMax);
93  double y = m_rand->GetValue (bv.Get ().yMin, bv.Get ().yMax);
94  double z = m_rand->GetValue (bv.Get ().zMin, bv.Get ().zMax);
95  return Vector (x, y, z);
96 }
97 
98 int64_t
100 {
101  m_rand->SetStream (stream);
102  return 1;
103 }
104 
105 
106 NS_OBJECT_ENSURE_REGISTERED (RandomRoomPositionAllocator);
107 
108 
109 RandomRoomPositionAllocator::RandomRoomPositionAllocator ()
110 {
111  m_rand = CreateObject<UniformRandomVariable> ();
112 }
113 
114 TypeId
115 RandomRoomPositionAllocator::GetTypeId (void)
116 {
117  static TypeId tid = TypeId ("ns3::RandomRoomPositionAllocator")
118  .SetParent<PositionAllocator> ()
119  .SetGroupName ("Mobility")
120  .AddConstructor<RandomRoomPositionAllocator> ();
121  return tid;
122 }
123 
124 Vector
126 {
127  NS_LOG_FUNCTION (this);
128  NS_ASSERT_MSG (BuildingList::GetNBuildings () > 0, "no building found");
129 
130  if (m_roomListWithoutReplacement.empty ())
131  {
132  for (BuildingList::Iterator bit = BuildingList::Begin (); bit != BuildingList::End (); ++bit)
133  {
134  NS_LOG_LOGIC ("building " << (*bit)->GetId ());
135  for (uint32_t rx = 1; rx <= (*bit)->GetNRoomsX (); ++rx)
136  {
137  for (uint32_t ry = 1; ry <= (*bit)->GetNRoomsY (); ++ry)
138  {
139  for (uint32_t f = 1; f <= (*bit)->GetNFloors (); ++f)
140  {
141  RoomInfo i;
142  i.roomx = rx;
143  i.roomy = ry;
144  i.floor = f;
145  i.b = *bit;
146  NS_LOG_LOGIC ("adding room (" << rx << ", " << ry << ", " << f << ")");
147  m_roomListWithoutReplacement.push_back (i);
148  }
149  }
150  }
151  }
152  }
153  uint32_t n = m_rand->GetInteger (0,m_roomListWithoutReplacement.size () - 1);
154  RoomInfo r = m_roomListWithoutReplacement.at (n);
155  m_roomListWithoutReplacement.erase (m_roomListWithoutReplacement.begin () + n);
156  NS_LOG_LOGIC ("considering building " << r.b->GetId () << " room (" << r.roomx << ", " << r.roomy << ", " << r.floor << ")");
157 
158  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
159  BoxValue bv;
160  r.b->GetAttribute ("Boundaries", bv);
161  Box box = bv.Get ();
162  double rdx = (box.xMax - box.xMin) / r.b->GetNRoomsX ();
163  double rdy = (box.yMax - box.yMin) / r.b->GetNRoomsY ();
164  double rdz = (box.zMax - box.zMin) / r.b->GetNFloors ();
165  double x1 = box.xMin + rdx * (r.roomx - 1);
166  double x2 = box.xMin + rdx * r.roomx;
167  double y1 = box.yMin + rdy * (r.roomy -1);
168  double y2 = box.yMin + rdy * r.roomy;
169  double z1 = box.zMin + rdz * (r.floor - 1);
170  double z2 = box.zMin + rdz * r.floor;
171  NS_LOG_LOGIC ("randomly allocating position in "
172  << " (" << x1 << "," << x2 << ") "
173  << "x (" << y1 << "," << y2 << ") "
174  << "x (" << z1 << "," << z2 << ") ");
175 
176  double x = m_rand->GetValue (x1, x2);
177  double y = m_rand->GetValue (y1, y2);
178  double z = m_rand->GetValue (z1, z2);
179 
180  return Vector (x, y, z);
181 }
182 
183 int64_t
185 {
186  m_rand->SetStream (stream);
187  return 1;
188 }
189 
190 
191 
192 
193 
194 NS_OBJECT_ENSURE_REGISTERED (SameRoomPositionAllocator);
195 
196 SameRoomPositionAllocator::SameRoomPositionAllocator ()
197 {
198  NS_FATAL_ERROR (" Constructor \"SameRoomPositionAllocator ()\" should not be used");
199 }
200 
201 
202 SameRoomPositionAllocator::SameRoomPositionAllocator (NodeContainer c)
203  : m_nodes (c)
204 {
205  m_rand = CreateObject<UniformRandomVariable> ();
206  m_nodeIt = m_nodes.Begin ();
207  // this is needed to make sure the building models associated with c have been initialized
208  for (NodeContainer::Iterator it = m_nodes.Begin (); it != m_nodes.End (); ++it)
209  {
210  Ptr<MobilityModel> mm = (*it)->GetObject<MobilityModel> ();
211  NS_ASSERT_MSG (mm, "no mobility model aggregated to this node");
212  Ptr<BuildingsMobilityModel> bmm = DynamicCast<BuildingsMobilityModel> (mm);
213  NS_ASSERT_MSG (bmm, "mobility model aggregated to this node is not a BuildingsMobilityModel");
214  BuildingsHelper::MakeConsistent (bmm);
215  }
216 }
217 
218 TypeId
219 SameRoomPositionAllocator::GetTypeId (void)
220 {
221  static TypeId tid = TypeId ("ns3::SameRoomPositionAllocator")
222  .SetParent<PositionAllocator> ()
223  .SetGroupName ("Mobility")
224  .AddConstructor<SameRoomPositionAllocator> ();
225  return tid;
226 }
227 
228 Vector
230 {
231  NS_LOG_FUNCTION (this);
232  if (m_nodeIt == m_nodes.End ())
233  {
234  m_nodeIt = m_nodes.Begin ();
235  }
236 
237  NS_ASSERT_MSG (m_nodeIt != m_nodes.End (), "no node in container");
238 
239  NS_LOG_LOGIC ("considering node " << (*m_nodeIt)->GetId ());
240  Ptr<MobilityModel> mm = (*m_nodeIt)->GetObject<MobilityModel> ();
241  NS_ASSERT_MSG (mm, "no mobility model aggregated to this node");
242  Ptr<BuildingsMobilityModel> bmm = DynamicCast<BuildingsMobilityModel> (mm);
243  NS_ASSERT_MSG (bmm, "mobility model aggregated to this node is not a BuildingsMobilityModel");
244 
245  ++m_nodeIt;
246  uint32_t roomx = bmm->GetRoomNumberX ();
247  uint32_t roomy = bmm->GetRoomNumberY ();
248  uint32_t floor = bmm->GetFloorNumber ();
249  NS_LOG_LOGIC ("considering building " << bmm->GetBuilding ()->GetId () << " room (" << roomx << ", " << roomy << ", " << floor << ")");
250 
251  Ptr<Building> b = bmm->GetBuilding ();
252  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
253  BoxValue bv;
254  b->GetAttribute ("Boundaries", bv);
255  Box box = bv.Get ();
256  double rdx = (box.xMax - box.xMin) / b->GetNRoomsX ();
257  double rdy = (box.yMax - box.yMin) / b->GetNRoomsY ();
258  double rdz = (box.zMax - box.zMin) / b->GetNFloors ();
259  double x1 = box.xMin + rdx * (roomx - 1);
260  double x2 = box.xMin + rdx * roomx;
261  double y1 = box.yMin + rdy * (roomy -1);
262  double y2 = box.yMin + rdy * roomy;
263  double z1 = box.zMin + rdz * (floor - 1);
264  double z2 = box.zMin + rdz * floor;
265  NS_LOG_LOGIC ("randomly allocating position in "
266  << " (" << x1 << "," << x2 << ") "
267  << "x (" << y1 << "," << y2 << ") "
268  << "x (" << z1 << "," << z2 << ") ");
269 
270  double x = m_rand->GetValue (x1, x2);
271  double y = m_rand->GetValue (y1, y2);
272  double z = m_rand->GetValue (z1, z2);
273 
274  return Vector (x, y, z);
275 }
276 
277 int64_t
279 {
280  m_rand->SetStream (stream);
281  return 1;
282 }
283 
284 
285 
286 } // namespace ns3
Ptr< UniformRandomVariable > m_rand
Provides uniform random variables.
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
uint32_t GetInteger(uint32_t min, uint32_t max)
Returns a random unsigned integer from a uniform distribution over the interval [min,max] including both ends.
static uint32_t GetNBuildings(void)
static Ptr< Building > GetBuilding(uint32_t n)
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
static Iterator End(void)
static Iterator Begin(void)
a 3d vector
Definition: vector.h:31
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
a 3d box
Definition: box.h:33
Keep track of the current position and velocity of an object.
hold objects of type ns3::Box
#define NS_LOG_LOGIC(msg)
Definition: log.h:334
Ptr< UniformRandomVariable > m_rand
Provides uniform random variables.
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
double GetValue(double min, double max)
Returns a random double from the uniform distribution with the specified range.
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Ptr< UniformRandomVariable > m_rand
Provides uniform random variables.
Ptr< T > GetObject(void) const
Definition: object.h:332