A Discrete-Event Network Simulator
API
building-position-allocator.cc
Go to the documentation of this file.
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  */
21 #include <ns3/mobility-building-info.h>
22 #include "ns3/mobility-model.h"
23 #include "ns3/buildings-helper.h"
24 #include "ns3/random-variable-stream.h"
25 #include "ns3/double.h"
26 #include "ns3/uinteger.h"
27 #include "ns3/enum.h"
28 #include "ns3/boolean.h"
29 #include "ns3/log.h"
30 #include "ns3/box.h"
31 #include "ns3/building.h"
32 #include <cmath>
33 
34 #include "ns3/building-list.h"
35 
36 namespace ns3 {
37 
38 NS_LOG_COMPONENT_DEFINE ("BuildingPositionAllocator");
39 
40 NS_OBJECT_ENSURE_REGISTERED (RandomBuildingPositionAllocator);
41 
42 
44 {
45  m_rand = CreateObject<UniformRandomVariable> ();
46 }
47 
48 TypeId
50 {
51  static TypeId tid = TypeId ("ns3::RandomBuildingPositionAllocator")
53  .SetGroupName ("Buildings")
54  .AddConstructor<RandomBuildingPositionAllocator> ()
55  .AddAttribute ("WithReplacement",
56  "If true, the building will be randomly selected with replacement. "
57  "If false, no replacement will occur, until the list of buildings "
58  "to select becomes empty, at which point it will be filled again "
59  "with the list of all buildings.",
60  BooleanValue (false),
63  return tid;
64 }
65 
66 Vector
68 {
69  NS_ASSERT_MSG (BuildingList::GetNBuildings () > 0, "no building found");
70  Ptr<Building> b;
72  {
73  uint32_t n = m_rand->GetInteger (0, BuildingList::GetNBuildings () - 1);
75  }
76  else
77  {
79  {
80  for (BuildingList::Iterator bit = BuildingList::Begin (); bit != BuildingList::End (); ++bit)
81  {
82  m_buildingListWithoutReplacement.push_back (*bit);
83  }
84  }
85  uint32_t n = m_rand->GetInteger (0, m_buildingListWithoutReplacement.size () - 1);
88  }
89 
90  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
91  BoxValue bv;
92  b->GetAttribute ("Boundaries", bv);
93  double x = m_rand->GetValue (bv.Get ().xMin, bv.Get ().xMax);
94  double y = m_rand->GetValue (bv.Get ().yMin, bv.Get ().yMax);
95  double z = m_rand->GetValue (bv.Get ().zMin, bv.Get ().zMax);
96  return Vector (x, y, z);
97 }
98 
99 int64_t
101 {
102  m_rand->SetStream (stream);
103  return 1;
104 }
105 
106 
108 
109 
111 {
112  m_rand = CreateObject<UniformRandomVariable> ();
113 }
114 
115 TypeId
117 {
118  static TypeId tid = TypeId ("ns3::RandomRoomPositionAllocator")
120  .SetGroupName ("Buildings")
121  .AddConstructor<RandomRoomPositionAllocator> ();
122  return tid;
123 }
124 
125 Vector
127 {
128  NS_LOG_FUNCTION (this);
129  NS_ASSERT_MSG (BuildingList::GetNBuildings () > 0, "no building found");
130 
131  if (m_roomListWithoutReplacement.empty ())
132  {
133  for (BuildingList::Iterator bit = BuildingList::Begin (); bit != BuildingList::End (); ++bit)
134  {
135  NS_LOG_LOGIC ("building " << (*bit)->GetId ());
136  for (uint32_t rx = 1; rx <= (*bit)->GetNRoomsX (); ++rx)
137  {
138  for (uint32_t ry = 1; ry <= (*bit)->GetNRoomsY (); ++ry)
139  {
140  for (uint32_t f = 1; f <= (*bit)->GetNFloors (); ++f)
141  {
142  RoomInfo i;
143  i.roomx = rx;
144  i.roomy = ry;
145  i.floor = f;
146  i.b = *bit;
147  NS_LOG_LOGIC ("adding room (" << rx << ", " << ry << ", " << f << ")");
148  m_roomListWithoutReplacement.push_back (i);
149  }
150  }
151  }
152  }
153  }
154  uint32_t n = m_rand->GetInteger (0,m_roomListWithoutReplacement.size () - 1);
157  NS_LOG_LOGIC ("considering building " << r.b->GetId () << " room (" << r.roomx << ", " << r.roomy << ", " << r.floor << ")");
158 
159  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
160  BoxValue bv;
161  r.b->GetAttribute ("Boundaries", bv);
162  Box box = bv.Get ();
163  double rdx = (box.xMax - box.xMin) / r.b->GetNRoomsX ();
164  double rdy = (box.yMax - box.yMin) / r.b->GetNRoomsY ();
165  double rdz = (box.zMax - box.zMin) / r.b->GetNFloors ();
166  double x1 = box.xMin + rdx * (r.roomx - 1);
167  double x2 = box.xMin + rdx * r.roomx;
168  double y1 = box.yMin + rdy * (r.roomy -1);
169  double y2 = box.yMin + rdy * r.roomy;
170  double z1 = box.zMin + rdz * (r.floor - 1);
171  double z2 = box.zMin + rdz * r.floor;
172  NS_LOG_LOGIC ("randomly allocating position in "
173  << " (" << x1 << "," << x2 << ") "
174  << "x (" << y1 << "," << y2 << ") "
175  << "x (" << z1 << "," << z2 << ") ");
176 
177  double x = m_rand->GetValue (x1, x2);
178  double y = m_rand->GetValue (y1, y2);
179  double z = m_rand->GetValue (z1, z2);
180 
181  return Vector (x, y, z);
182 }
183 
184 int64_t
186 {
187  m_rand->SetStream (stream);
188  return 1;
189 }
190 
191 
192 
193 
194 
196 
198 {
199  NS_FATAL_ERROR (" Constructor \"SameRoomPositionAllocator ()\" should not be used");
200 }
201 
202 
204  : m_nodes (c)
205 {
206  m_rand = CreateObject<UniformRandomVariable> ();
207  m_nodeIt = m_nodes.Begin ();
208  // this is needed to make sure the building models associated with c have been initialized
209  for (NodeContainer::Iterator it = m_nodes.Begin (); it != m_nodes.End (); ++it)
210  {
212  NS_ASSERT_MSG (mm, "no mobility model aggregated to this node");
214  NS_ASSERT_MSG (bmm, "MobilityBuildingInfo has not been aggregated to this node mobility model");
216  }
217 }
218 
219 TypeId
221 {
222  static TypeId tid = TypeId ("ns3::SameRoomPositionAllocator")
224  .SetGroupName ("Buildings")
225  .AddConstructor<SameRoomPositionAllocator> ();
226  return tid;
227 }
228 
229 Vector
231 {
232  NS_LOG_FUNCTION (this);
233  if (m_nodeIt == m_nodes.End ())
234  {
235  m_nodeIt = m_nodes.Begin ();
236  }
237 
238  NS_ASSERT_MSG (m_nodeIt != m_nodes.End (), "no node in container");
239 
240  NS_LOG_LOGIC ("considering node " << (*m_nodeIt)->GetId ());
241  Ptr<MobilityModel> mm = (*m_nodeIt)->GetObject<MobilityModel> ();
242  NS_ASSERT_MSG (mm, "no mobility model aggregated to this node");
243  Ptr<MobilityBuildingInfo> bmm = mm->GetObject<MobilityBuildingInfo> ();
244  NS_ASSERT_MSG (bmm, "MobilityBuildingInfo has not been aggregated to this node mobility model");
245 
246  ++m_nodeIt;
247  uint32_t roomx = bmm->GetRoomNumberX ();
248  uint32_t roomy = bmm->GetRoomNumberY ();
249  uint32_t floor = bmm->GetFloorNumber ();
250  NS_LOG_LOGIC ("considering building " << bmm->GetBuilding ()->GetId () << " room (" << roomx << ", " << roomy << ", " << floor << ")");
251 
252  Ptr<Building> b = bmm->GetBuilding ();
253  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
254  BoxValue bv;
255  b->GetAttribute ("Boundaries", bv);
256  Box box = bv.Get ();
257  double rdx = (box.xMax - box.xMin) / b->GetNRoomsX ();
258  double rdy = (box.yMax - box.yMin) / b->GetNRoomsY ();
259  double rdz = (box.zMax - box.zMin) / b->GetNFloors ();
260  double x1 = box.xMin + rdx * (roomx - 1);
261  double x2 = box.xMin + rdx * roomx;
262  double y1 = box.yMin + rdy * (roomy -1);
263  double y2 = box.yMin + rdy * roomy;
264  double z1 = box.zMin + rdz * (floor - 1);
265  double z2 = box.zMin + rdz * floor;
266  NS_LOG_LOGIC ("randomly allocating position in "
267  << " (" << x1 << "," << x2 << ") "
268  << "x (" << y1 << "," << y2 << ") "
269  << "x (" << z1 << "," << z2 << ") ");
270 
271  double x = m_rand->GetValue (x1, x2);
272  double y = m_rand->GetValue (y1, y2);
273  double z = m_rand->GetValue (z1, z2);
274 
275  return Vector (x, y, z);
276 }
277 
278 int64_t
280 {
281  m_rand->SetStream (stream);
282  return 1;
283 }
284 
286 
287 
289  uint32_t x,
290  uint32_t y,
291  uint32_t z,
292  Ptr<Building> pbtr)
293 {
294  m_rand = CreateObject<UniformRandomVariable> ();
295  roomx = x;
296  roomy = y;
297  floor = z;
298  bptr = pbtr;
299 }
300 
301 TypeId
303 {
304  static TypeId tid = TypeId ("ns3::FixedRoomPositionAllocator")
306  .SetGroupName ("Buildings")
307  .AddConstructor<SameRoomPositionAllocator> ();
308  return tid;
309 }
310 
311 Vector
313 {
314 
315  NS_LOG_LOGIC ("considering building " << bptr->GetId () << " room (" << roomx << ", " << roomy << ", " << floor << ")");
316 
317  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
318 
319  Box box = bptr->GetBoundaries ();
320  double rdx = (box.xMax - box.xMin) / bptr->GetNRoomsX ();
321  double rdy = (box.yMax - box.yMin) / bptr->GetNRoomsY ();
322  double rdz = (box.zMax - box.zMin) / bptr->GetNFloors ();
323  double x1 = box.xMin + rdx * (roomx - 1);
324  double x2 = box.xMin + rdx * roomx;
325  double y1 = box.yMin + rdy * (roomy -1);
326  double y2 = box.yMin + rdy * roomy;
327  double z1 = box.zMin + rdz * (floor - 1);
328  double z2 = box.zMin + rdz * floor;
329  NS_LOG_LOGIC ("randomly allocating position in "
330  << " (" << x1 << "," << x2 << ") "
331  << "x (" << y1 << "," << y2 << ") "
332  << "x (" << z1 << "," << z2 << ") ");
333 
334  double x = m_rand->GetValue (x1, x2);
335  double y = m_rand->GetValue (y1, y2);
336  double z = m_rand->GetValue (z1, z2);
337  return Vector (x, y, z);
338 }
339 
340 
341 int64_t
343 {
344  m_rand->SetStream (stream);
345  return 1;
346 }
347 
348 } // namespace ns3
Ptr< UniformRandomVariable > m_rand
Provides uniform random variables.
FixedRoomPositionAllocator(uint32_t x, uint32_t y, uint32_t z, Ptr< Building > b)
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
double zMin
The z coordinate of the down bound of the box.
Definition: box.h:101
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
AttributeValue implementation for Boolean.
Definition: boolean.h:34
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
static uint32_t GetNBuildings(void)
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
static Ptr< Building > GetBuilding(uint32_t n)
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: boolean.h:81
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Walks a given NodeContainer sequentially, and for each node allocate a new position randomly in the s...
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
static Iterator End(void)
double xMax
The x coordinate of the right bound of the box.
Definition: box.h:95
static Iterator Begin(void)
a 3d box
Definition: box.h:34
double yMax
The y coordinate of the top bound of the box.
Definition: box.h:99
static void MakeConsistent(Ptr< MobilityModel > bmm)
Make the given mobility model consistent, by determining whether its position falls inside any of the...
Keep track of the current position and velocity of an object.
virtual uint32_t GetInteger(void)=0
Get the next random value as an integer drawn from the distribution.
Generate a random position uniformly distributed in the volume of a chosen room inside a chosen build...
Allocate each position by randomly chosing a building from the list of all buildings, and then randomly chosing a position inside the building.
int64_t AssignStreams(int64_t)
Assign a fixed random variable stream number to the random variables used by this model...
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
std::vector< Ptr< Building > >::const_iterator Iterator
Definition: building-list.h:36
AttributeValue implementation for Box.
Definition: box.h:109
double yMin
The y coordinate of the bottom bound of the box.
Definition: box.h:97
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
Ptr< UniformRandomVariable > m_rand
Provides uniform random variables.
double f(double x, void *params)
Definition: 80211b.c:60
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Ptr< UniformRandomVariable > m_rand
Provides uniform random variables.
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
double zMax
The z coordinate of the up bound of the box.
Definition: box.h:103
Allocate each position by randomly chosing a room from the list of all buildings, and then randomly c...
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Ptr< UniformRandomVariable > m_rand
Provides uniform random variables.
std::vector< Ptr< Building > > m_buildingListWithoutReplacement
mobility buildings information (to be used by mobility models)
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
int64_t AssignStreams(int64_t)
Assign a fixed random variable stream number to the random variables used by this model...
double xMin
The x coordinate of the left bound of the box.
Definition: box.h:93
Allocate a set of positions.
std::vector< RoomInfo > m_roomListWithoutReplacement