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  * Michele Polese <michele.polese@gmail.com> for the OutdoorPositionAllocator class
20  */
22 #include <ns3/mobility-building-info.h>
23 #include "ns3/mobility-model.h"
24 #include "ns3/buildings-helper.h"
25 #include "ns3/random-variable-stream.h"
26 #include "ns3/double.h"
27 #include "ns3/uinteger.h"
28 #include "ns3/enum.h"
29 #include "ns3/boolean.h"
30 #include "ns3/log.h"
31 #include "ns3/box.h"
32 #include "ns3/building.h"
33 #include "ns3/string.h"
34 #include "ns3/pointer.h"
35 #include <cmath>
36 
37 #include "ns3/building-list.h"
38 
39 namespace ns3 {
40 
41 NS_LOG_COMPONENT_DEFINE ("BuildingPositionAllocator");
42 
43 NS_OBJECT_ENSURE_REGISTERED (RandomBuildingPositionAllocator);
44 
45 
47 {
48  m_rand = CreateObject<UniformRandomVariable> ();
49 }
50 
51 TypeId
53 {
54  static TypeId tid = TypeId ("ns3::RandomBuildingPositionAllocator")
56  .SetGroupName ("Buildings")
57  .AddConstructor<RandomBuildingPositionAllocator> ()
58  .AddAttribute ("WithReplacement",
59  "If true, the building will be randomly selected with replacement. "
60  "If false, no replacement will occur, until the list of buildings "
61  "to select becomes empty, at which point it will be filled again "
62  "with the list of all buildings.",
63  BooleanValue (false),
66  return tid;
67 }
68 
69 Vector
71 {
72  NS_ASSERT_MSG (BuildingList::GetNBuildings () > 0, "no building found");
73  Ptr<Building> b;
75  {
76  uint32_t n = m_rand->GetInteger (0, BuildingList::GetNBuildings () - 1);
78  }
79  else
80  {
82  {
83  for (BuildingList::Iterator bit = BuildingList::Begin (); bit != BuildingList::End (); ++bit)
84  {
85  m_buildingListWithoutReplacement.push_back (*bit);
86  }
87  }
88  uint32_t n = m_rand->GetInteger (0, m_buildingListWithoutReplacement.size () - 1);
91  }
92 
93  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
94  BoxValue bv;
95  b->GetAttribute ("Boundaries", bv);
96  double x = m_rand->GetValue (bv.Get ().xMin, bv.Get ().xMax);
97  double y = m_rand->GetValue (bv.Get ().yMin, bv.Get ().yMax);
98  double z = m_rand->GetValue (bv.Get ().zMin, bv.Get ().zMax);
99  return Vector (x, y, z);
100 }
101 
102 int64_t
104 {
105  m_rand->SetStream (stream);
106  return 1;
107 }
108 
109 
111 
113 {
114 }
115 
116 TypeId
118 {
119  static TypeId tid = TypeId ("ns3::OutdoorPositionAllocator")
121  .SetGroupName ("Buildings")
122  .AddConstructor<OutdoorPositionAllocator> ()
123  .AddAttribute ("X",
124  "A random variable which represents the x coordinate of a position in a random box.",
125  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
127  MakePointerChecker<RandomVariableStream> ())
128  .AddAttribute ("Y",
129  "A random variable which represents the y coordinate of a position in a random box.",
130  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
132  MakePointerChecker<RandomVariableStream> ())
133  .AddAttribute ("Z",
134  "A random variable which represents the z coordinate of a position in a random box.",
135  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
137  MakePointerChecker<RandomVariableStream> ())
138  .AddAttribute ("MaxAttempts",
139  "Maximum number of attempts for the rejection sampling before giving up.",
140  UintegerValue (1000),
142  MakeUintegerChecker<uint32_t> ())
143  ;
144 
145  return tid;
146 }
147 
148 void
150 {
151  m_x = x;
152 }
153 void
155 {
156  m_y = y;
157 }
158 void
160 {
161  m_z = z;
162 }
163 
164 Vector
166 {
167  NS_ABORT_MSG_IF (BuildingList::GetNBuildings () == 0, "no building found");
168 
169  bool outdoor = false;
170  uint32_t attempts = 0;
171  Vector position = Vector (0,0,0);
172 
173  while (!outdoor && attempts < m_maxAttempts)
174  {
175  // get a random position
176  double x = m_x->GetValue ();
177  double y = m_y->GetValue ();
178  double z = m_z->GetValue ();
179 
180  position = Vector (x, y, z);
181 
182  NS_LOG_INFO ("Position " << position);
183 
184  bool inside = false;
185  for (BuildingList::Iterator bit = BuildingList::Begin (); bit != BuildingList::End (); ++bit)
186  {
187  if ((*bit)->IsInside (position))
188  {
189  NS_LOG_INFO ("Position " << position << " is inside the building with boundaries "
190  << (*bit)->GetBoundaries ().xMin << " " << (*bit)->GetBoundaries ().xMax << " "
191  << (*bit)->GetBoundaries ().yMin << " " << (*bit)->GetBoundaries ().yMax << " "
192  << (*bit)->GetBoundaries ().zMin << " " << (*bit)->GetBoundaries ().zMax);
193  inside = true;
194  break;
195  }
196  }
197 
198  if (inside)
199  {
200  NS_LOG_INFO ("Inside a building, attempt " << attempts << " out of " << m_maxAttempts);
201  attempts++;
202  }
203  else
204  {
205  NS_LOG_INFO ("Outdoor position found " << position);
206  outdoor = true;
207  }
208  }
209 
210  NS_ABORT_MSG_IF (attempts >= m_maxAttempts, "Too many attempts, give up");
211  NS_ABORT_MSG_IF (!outdoor, "Still indoor, give up");
212  return position;
213 }
214 
215 int64_t
217 {
218  m_x->SetStream (stream);
219  m_y->SetStream (stream + 1);
220  m_z->SetStream (stream + 2);
221  return 3;
222 }
223 
224 
226 
227 
229 {
230  m_rand = CreateObject<UniformRandomVariable> ();
231 }
232 
233 TypeId
235 {
236  static TypeId tid = TypeId ("ns3::RandomRoomPositionAllocator")
238  .SetGroupName ("Buildings")
239  .AddConstructor<RandomRoomPositionAllocator> ();
240  return tid;
241 }
242 
243 Vector
245 {
246  NS_LOG_FUNCTION (this);
247  NS_ASSERT_MSG (BuildingList::GetNBuildings () > 0, "no building found");
248 
249  if (m_roomListWithoutReplacement.empty ())
250  {
251  for (BuildingList::Iterator bit = BuildingList::Begin (); bit != BuildingList::End (); ++bit)
252  {
253  NS_LOG_LOGIC ("building " << (*bit)->GetId ());
254  for (uint32_t rx = 1; rx <= (*bit)->GetNRoomsX (); ++rx)
255  {
256  for (uint32_t ry = 1; ry <= (*bit)->GetNRoomsY (); ++ry)
257  {
258  for (uint32_t f = 1; f <= (*bit)->GetNFloors (); ++f)
259  {
260  RoomInfo i;
261  i.roomx = rx;
262  i.roomy = ry;
263  i.floor = f;
264  i.b = *bit;
265  NS_LOG_LOGIC ("adding room (" << rx << ", " << ry << ", " << f << ")");
266  m_roomListWithoutReplacement.push_back (i);
267  }
268  }
269  }
270  }
271  }
272  uint32_t n = m_rand->GetInteger (0,m_roomListWithoutReplacement.size () - 1);
275  NS_LOG_LOGIC ("considering building " << r.b->GetId () << " room (" << r.roomx << ", " << r.roomy << ", " << r.floor << ")");
276 
277  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
278  BoxValue bv;
279  r.b->GetAttribute ("Boundaries", bv);
280  Box box = bv.Get ();
281  double rdx = (box.xMax - box.xMin) / r.b->GetNRoomsX ();
282  double rdy = (box.yMax - box.yMin) / r.b->GetNRoomsY ();
283  double rdz = (box.zMax - box.zMin) / r.b->GetNFloors ();
284  double x1 = box.xMin + rdx * (r.roomx - 1);
285  double x2 = box.xMin + rdx * r.roomx;
286  double y1 = box.yMin + rdy * (r.roomy -1);
287  double y2 = box.yMin + rdy * r.roomy;
288  double z1 = box.zMin + rdz * (r.floor - 1);
289  double z2 = box.zMin + rdz * r.floor;
290  NS_LOG_LOGIC ("randomly allocating position in "
291  << " (" << x1 << "," << x2 << ") "
292  << "x (" << y1 << "," << y2 << ") "
293  << "x (" << z1 << "," << z2 << ") ");
294 
295  double x = m_rand->GetValue (x1, x2);
296  double y = m_rand->GetValue (y1, y2);
297  double z = m_rand->GetValue (z1, z2);
298 
299  return Vector (x, y, z);
300 }
301 
302 int64_t
304 {
305  m_rand->SetStream (stream);
306  return 1;
307 }
308 
309 
310 
311 
312 
314 
316 {
317  NS_FATAL_ERROR (" Constructor \"SameRoomPositionAllocator ()\" should not be used");
318 }
319 
320 
322  : m_nodes (c)
323 {
324  m_rand = CreateObject<UniformRandomVariable> ();
325  m_nodeIt = m_nodes.Begin ();
326  // this is needed to make sure the building models associated with c have been initialized
327  for (NodeContainer::Iterator it = m_nodes.Begin (); it != m_nodes.End (); ++it)
328  {
330  NS_ASSERT_MSG (mm, "no mobility model aggregated to this node");
332  NS_ASSERT_MSG (bmm, "MobilityBuildingInfo has not been aggregated to this node mobility model");
334  }
335 }
336 
337 TypeId
339 {
340  static TypeId tid = TypeId ("ns3::SameRoomPositionAllocator")
342  .SetGroupName ("Buildings")
343  .AddConstructor<SameRoomPositionAllocator> ();
344  return tid;
345 }
346 
347 Vector
349 {
350  NS_LOG_FUNCTION (this);
351  if (m_nodeIt == m_nodes.End ())
352  {
353  m_nodeIt = m_nodes.Begin ();
354  }
355 
356  NS_ASSERT_MSG (m_nodeIt != m_nodes.End (), "no node in container");
357 
358  NS_LOG_LOGIC ("considering node " << (*m_nodeIt)->GetId ());
359  Ptr<MobilityModel> mm = (*m_nodeIt)->GetObject<MobilityModel> ();
360  NS_ASSERT_MSG (mm, "no mobility model aggregated to this node");
361  Ptr<MobilityBuildingInfo> bmm = mm->GetObject<MobilityBuildingInfo> ();
362  NS_ASSERT_MSG (bmm, "MobilityBuildingInfo has not been aggregated to this node mobility model");
363 
364  ++m_nodeIt;
365  uint32_t roomx = bmm->GetRoomNumberX ();
366  uint32_t roomy = bmm->GetRoomNumberY ();
367  uint32_t floor = bmm->GetFloorNumber ();
368  NS_LOG_LOGIC ("considering building " << bmm->GetBuilding ()->GetId () << " room (" << roomx << ", " << roomy << ", " << floor << ")");
369 
370  Ptr<Building> b = bmm->GetBuilding ();
371  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
372  BoxValue bv;
373  b->GetAttribute ("Boundaries", bv);
374  Box box = bv.Get ();
375  double rdx = (box.xMax - box.xMin) / b->GetNRoomsX ();
376  double rdy = (box.yMax - box.yMin) / b->GetNRoomsY ();
377  double rdz = (box.zMax - box.zMin) / b->GetNFloors ();
378  double x1 = box.xMin + rdx * (roomx - 1);
379  double x2 = box.xMin + rdx * roomx;
380  double y1 = box.yMin + rdy * (roomy -1);
381  double y2 = box.yMin + rdy * roomy;
382  double z1 = box.zMin + rdz * (floor - 1);
383  double z2 = box.zMin + rdz * floor;
384  NS_LOG_LOGIC ("randomly allocating position in "
385  << " (" << x1 << "," << x2 << ") "
386  << "x (" << y1 << "," << y2 << ") "
387  << "x (" << z1 << "," << z2 << ") ");
388 
389  double x = m_rand->GetValue (x1, x2);
390  double y = m_rand->GetValue (y1, y2);
391  double z = m_rand->GetValue (z1, z2);
392 
393  return Vector (x, y, z);
394 }
395 
396 int64_t
398 {
399  m_rand->SetStream (stream);
400  return 1;
401 }
402 
404 
405 
407  uint32_t x,
408  uint32_t y,
409  uint32_t z,
410  Ptr<Building> pbtr)
411 {
412  m_rand = CreateObject<UniformRandomVariable> ();
413  roomx = x;
414  roomy = y;
415  floor = z;
416  bptr = pbtr;
417 }
418 
419 TypeId
421 {
422  static TypeId tid = TypeId ("ns3::FixedRoomPositionAllocator")
424  .SetGroupName ("Buildings")
425  .AddConstructor<SameRoomPositionAllocator> ();
426  return tid;
427 }
428 
429 Vector
431 {
432 
433  NS_LOG_LOGIC ("considering building " << bptr->GetId () << " room (" << roomx << ", " << roomy << ", " << floor << ")");
434 
435  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
436 
437  Box box = bptr->GetBoundaries ();
438  double rdx = (box.xMax - box.xMin) / bptr->GetNRoomsX ();
439  double rdy = (box.yMax - box.yMin) / bptr->GetNRoomsY ();
440  double rdz = (box.zMax - box.zMin) / bptr->GetNFloors ();
441  double x1 = box.xMin + rdx * (roomx - 1);
442  double x2 = box.xMin + rdx * roomx;
443  double y1 = box.yMin + rdy * (roomy -1);
444  double y2 = box.yMin + rdy * roomy;
445  double z1 = box.zMin + rdz * (floor - 1);
446  double z2 = box.zMin + rdz * floor;
447  NS_LOG_LOGIC ("randomly allocating position in "
448  << " (" << x1 << "," << x2 << ") "
449  << "x (" << y1 << "," << y2 << ") "
450  << "x (" << z1 << "," << z2 << ") ");
451 
452  double x = m_rand->GetValue (x1, x2);
453  double y = m_rand->GetValue (y1, y2);
454  double z = m_rand->GetValue (z1, z2);
455  return Vector (x, y, z);
456 }
457 
458 
459 int64_t
461 {
462  m_rand->SetStream (stream);
463  return 1;
464 }
465 
466 } // 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 the RngStream.
AttributeValue implementation for Boolean.
Definition: boolean.h:36
Ptr< RandomVariableStream > m_z
pointer to z&#39;s random variable stream
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
static uint32_t GetNBuildings(void)
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
Hold variables of type string.
Definition: string.h:41
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:84
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Walks a given NodeContainer sequentially, and for each node allocate a new position randomly in the s...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
static Iterator End(void)
Ptr< RandomVariableStream > m_y
pointer to y&#39;s random variable stream
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
double xMax
The x coordinate of the right bound of the box.
Definition: box.h:95
static Iterator Begin(void)
void SetZ(Ptr< RandomVariableStream > z)
Set the random variable stream object that generates z-positions.
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
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.
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: pointer.h:220
Generate a random position uniformly distributed in the volume of a chosen room inside a chosen build...
Allocate each position by randomly choosing a building from the list of all buildings, and then randomly choosing a position inside the building.
Hold an unsigned integer type.
Definition: uinteger.h:44
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
void SetX(Ptr< RandomVariableStream > x)
Set the random variable stream object that generates x-positions.
Ptr< UniformRandomVariable > m_rand
Provides uniform random variables.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
double f(double x, void *params)
Definition: 80211b.c:70
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
Ptr< RandomVariableStream > m_x
pointer to x&#39;s random variable stream
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
void SetY(Ptr< RandomVariableStream > y)
Set the random variable stream object that generates y-positions.
Allocate each position by randomly choosing a room from the list of all buildings, and then randomly choosing a position inside the room.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
uint32_t m_maxAttempts
maximum number of attempts before giving up
int64_t AssignStreams(int64_t stream)
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...
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
Ptr< UniformRandomVariable > m_rand
Provides uniform random variables.
std::vector< Ptr< Building > > m_buildingListWithoutReplacement
mobility buildings information (to be used by mobility models)
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
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