A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("BuildingPositionAllocator");
37 
38 namespace ns3 {
39 
40 NS_OBJECT_ENSURE_REGISTERED (RandomBuildingPositionAllocator)
41  ;
42 
43 
45 {
46  m_rand = CreateObject<UniformRandomVariable> ();
47 }
48 
49 TypeId
51 {
52  static TypeId tid = TypeId ("ns3::RandomBuildingPositionAllocator")
54  .SetGroupName ("Mobility")
55  .AddConstructor<RandomBuildingPositionAllocator> ()
56  .AddAttribute ("WithReplacement",
57  "If true, the building will be randomly selected with replacement. "
58  "If false, no replacement will occur, until the list of buildings "
59  "to select becomes empty, at which point it will be filled again "
60  "with the list of all buildings.",
61  BooleanValue (false),
63  MakeBooleanChecker ());
64  return tid;
65 }
66 
67 Vector
69 {
70  NS_ASSERT_MSG (BuildingList::GetNBuildings () > 0, "no building found");
71  Ptr<Building> b;
73  {
74  uint32_t n = m_rand->GetInteger (0, BuildingList::GetNBuildings () - 1);
76  }
77  else
78  {
80  {
81  for (BuildingList::Iterator bit = BuildingList::Begin (); bit != BuildingList::End (); ++bit)
82  {
83  m_buildingListWithoutReplacement.push_back (*bit);
84  }
85  }
86  uint32_t n = m_rand->GetInteger (0, m_buildingListWithoutReplacement.size () - 1);
89  }
90 
91  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
92  BoxValue bv;
93  b->GetAttribute ("Boundaries", bv);
94  double x = m_rand->GetValue (bv.Get ().xMin, bv.Get ().xMax);
95  double y = m_rand->GetValue (bv.Get ().yMin, bv.Get ().yMax);
96  double z = m_rand->GetValue (bv.Get ().zMin, bv.Get ().zMax);
97  return Vector (x, y, z);
98 }
99 
100 int64_t
102 {
103  m_rand->SetStream (stream);
104  return 1;
105 }
106 
107 
109  ;
110 
111 
113 {
114  m_rand = CreateObject<UniformRandomVariable> ();
115 }
116 
117 TypeId
119 {
120  static TypeId tid = TypeId ("ns3::RandomRoomPositionAllocator")
122  .SetGroupName ("Mobility")
123  .AddConstructor<RandomRoomPositionAllocator> ();
124  return tid;
125 }
126 
127 Vector
129 {
130  NS_LOG_FUNCTION (this);
131  NS_ASSERT_MSG (BuildingList::GetNBuildings () > 0, "no building found");
132 
133  if (m_roomListWithoutReplacement.empty ())
134  {
135  for (BuildingList::Iterator bit = BuildingList::Begin (); bit != BuildingList::End (); ++bit)
136  {
137  NS_LOG_LOGIC ("building " << (*bit)->GetId ());
138  for (uint32_t rx = 1; rx <= (*bit)->GetNRoomsX (); ++rx)
139  {
140  for (uint32_t ry = 1; ry <= (*bit)->GetNRoomsY (); ++ry)
141  {
142  for (uint32_t f = 1; f <= (*bit)->GetNFloors (); ++f)
143  {
144  RoomInfo i;
145  i.roomx = rx;
146  i.roomy = ry;
147  i.floor = f;
148  i.b = *bit;
149  NS_LOG_LOGIC ("adding room (" << rx << ", " << ry << ", " << f << ")");
150  m_roomListWithoutReplacement.push_back (i);
151  }
152  }
153  }
154  }
155  }
156  uint32_t n = m_rand->GetInteger (0,m_roomListWithoutReplacement.size () - 1);
159  NS_LOG_LOGIC ("considering building " << r.b->GetId () << " room (" << r.roomx << ", " << r.roomy << ", " << r.floor << ")");
160 
161  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
162  BoxValue bv;
163  r.b->GetAttribute ("Boundaries", bv);
164  Box box = bv.Get ();
165  double rdx = (box.xMax - box.xMin) / r.b->GetNRoomsX ();
166  double rdy = (box.yMax - box.yMin) / r.b->GetNRoomsY ();
167  double rdz = (box.zMax - box.zMin) / r.b->GetNFloors ();
168  double x1 = box.xMin + rdx * (r.roomx - 1);
169  double x2 = box.xMin + rdx * r.roomx;
170  double y1 = box.yMin + rdy * (r.roomy -1);
171  double y2 = box.yMin + rdy * r.roomy;
172  double z1 = box.zMin + rdz * (r.floor - 1);
173  double z2 = box.zMin + rdz * r.floor;
174  NS_LOG_LOGIC ("randomly allocating position in "
175  << " (" << x1 << "," << x2 << ") "
176  << "x (" << y1 << "," << y2 << ") "
177  << "x (" << z1 << "," << z2 << ") ");
178 
179  double x = m_rand->GetValue (x1, x2);
180  double y = m_rand->GetValue (y1, y2);
181  double z = m_rand->GetValue (z1, z2);
182 
183  return Vector (x, y, z);
184 }
185 
186 int64_t
188 {
189  m_rand->SetStream (stream);
190  return 1;
191 }
192 
193 
194 
195 
196 
198  ;
199 
201 {
202  NS_FATAL_ERROR (" Constructor \"SameRoomPositionAllocator ()\" should not be used");
203 }
204 
205 
207  : m_nodes (c)
208 {
209  m_rand = CreateObject<UniformRandomVariable> ();
210  m_nodeIt = m_nodes.Begin ();
211  // this is needed to make sure the building models associated with c have been initialized
212  for (NodeContainer::Iterator it = m_nodes.Begin (); it != m_nodes.End (); ++it)
213  {
215  NS_ASSERT_MSG (mm, "no mobility model aggregated to this node");
217  NS_ASSERT_MSG (bmm, "MobilityBuildingInfo has not been aggregated to this node mobility model");
219  }
220 }
221 
222 TypeId
224 {
225  static TypeId tid = TypeId ("ns3::SameRoomPositionAllocator")
227  .SetGroupName ("Mobility")
228  .AddConstructor<SameRoomPositionAllocator> ();
229  return tid;
230 }
231 
232 Vector
234 {
235  NS_LOG_FUNCTION (this);
236  if (m_nodeIt == m_nodes.End ())
237  {
238  m_nodeIt = m_nodes.Begin ();
239  }
240 
241  NS_ASSERT_MSG (m_nodeIt != m_nodes.End (), "no node in container");
242 
243  NS_LOG_LOGIC ("considering node " << (*m_nodeIt)->GetId ());
244  Ptr<MobilityModel> mm = (*m_nodeIt)->GetObject<MobilityModel> ();
245  NS_ASSERT_MSG (mm, "no mobility model aggregated to this node");
246  Ptr<MobilityBuildingInfo> bmm = mm->GetObject<MobilityBuildingInfo> ();
247  NS_ASSERT_MSG (bmm, "MobilityBuildingInfo has not been aggregated to this node mobility model");
248 
249  ++m_nodeIt;
250  uint32_t roomx = bmm->GetRoomNumberX ();
251  uint32_t roomy = bmm->GetRoomNumberY ();
252  uint32_t floor = bmm->GetFloorNumber ();
253  NS_LOG_LOGIC ("considering building " << bmm->GetBuilding ()->GetId () << " room (" << roomx << ", " << roomy << ", " << floor << ")");
254 
255  Ptr<Building> b = bmm->GetBuilding ();
256  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
257  BoxValue bv;
258  b->GetAttribute ("Boundaries", bv);
259  Box box = bv.Get ();
260  double rdx = (box.xMax - box.xMin) / b->GetNRoomsX ();
261  double rdy = (box.yMax - box.yMin) / b->GetNRoomsY ();
262  double rdz = (box.zMax - box.zMin) / b->GetNFloors ();
263  double x1 = box.xMin + rdx * (roomx - 1);
264  double x2 = box.xMin + rdx * roomx;
265  double y1 = box.yMin + rdy * (roomy -1);
266  double y2 = box.yMin + rdy * roomy;
267  double z1 = box.zMin + rdz * (floor - 1);
268  double z2 = box.zMin + rdz * floor;
269  NS_LOG_LOGIC ("randomly allocating position in "
270  << " (" << x1 << "," << x2 << ") "
271  << "x (" << y1 << "," << y2 << ") "
272  << "x (" << z1 << "," << z2 << ") ");
273 
274  double x = m_rand->GetValue (x1, x2);
275  double y = m_rand->GetValue (y1, y2);
276  double z = m_rand->GetValue (z1, z2);
277 
278  return Vector (x, y, z);
279 }
280 
281 int64_t
283 {
284  m_rand->SetStream (stream);
285  return 1;
286 }
287 
289  ;
290 
291 
293  uint32_t x,
294  uint32_t y,
295  uint32_t z,
296  Ptr<Building> pbtr)
297 {
298  m_rand = CreateObject<UniformRandomVariable> ();
299  roomx = x;
300  roomy = y;
301  floor = z;
302  bptr = pbtr;
303 }
304 
305 TypeId
307 {
308  static TypeId tid = TypeId ("ns3::FixedRoomPositionAllocator")
310  .SetGroupName ("Mobility")
311  .AddConstructor<SameRoomPositionAllocator> ();
312  return tid;
313 }
314 
315 Vector
317 {
318 
319  NS_LOG_LOGIC ("considering building " << bptr->GetId () << " room (" << roomx << ", " << roomy << ", " << floor << ")");
320 
321  Ptr<RandomBoxPositionAllocator> pa = CreateObject<RandomBoxPositionAllocator> ();
322 
323  Box box = bptr->GetBoundaries ();
324  double rdx = (box.xMax - box.xMin) / bptr->GetNRoomsX ();
325  double rdy = (box.yMax - box.yMin) / bptr->GetNRoomsY ();
326  double rdz = (box.zMax - box.zMin) / bptr->GetNFloors ();
327  double x1 = box.xMin + rdx * (roomx - 1);
328  double x2 = box.xMin + rdx * roomx;
329  double y1 = box.yMin + rdy * (roomy -1);
330  double y2 = box.yMin + rdy * roomy;
331  double z1 = box.zMin + rdz * (floor - 1);
332  double z2 = box.zMin + rdz * floor;
333  NS_LOG_LOGIC ("randomly allocating position in "
334  << " (" << x1 << "," << x2 << ") "
335  << "x (" << y1 << "," << y2 << ") "
336  << "x (" << z1 << "," << z2 << ") ");
337 
338  double x = m_rand->GetValue (x1, x2);
339  double y = m_rand->GetValue (y1, y2);
340  double z = m_rand->GetValue (z1, z2);
341  return Vector (x, y, z);
342 }
343 
344 
345 int64_t
347 {
348  m_rand->SetStream (stream);
349  return 1;
350 }
351 
352 
353 } // 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:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
double zMin
Definition: box.h:97
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
Hold a bool native type.
Definition: boolean.h:38
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.
std::vector< Ptr< Node > >::const_iterator Iterator
static uint32_t GetNBuildings(void)
static Ptr< Building > GetBuilding(uint32_t n)
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
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.
static Iterator End(void)
double xMax
Definition: box.h:91
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
double yMax
Definition: box.h:95
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.
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...
Vector3D Vector
Definition: vector.h:118
std::vector< Ptr< Building > >::const_iterator Iterator
Definition: building-list.h:36
hold objects of type ns3::Box
double yMin
Definition: box.h:93
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
Ptr< UniformRandomVariable > m_rand
Provides uniform random variables.
keep track of a set of node pointers.
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.
double zMax
Definition: box.h:99
Allocate each position by randomly chosing a room from the list of all buildings, and then randomly c...
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
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
NS_LOG_COMPONENT_DEFINE("BuildingPositionAllocator")
mobility buildings information (to be used by mobility models)
Ptr< T > GetObject(void) const
Definition: object.h:361
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
int64_t AssignStreams(int64_t)
Assign a fixed random variable stream number to the random variables used by this model...
double xMin
Definition: box.h:89
Allocate a set of positions.
std::vector< RoomInfo > m_roomListWithoutReplacement