A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
building-position-allocator.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Nicola Baldo <nbaldo@cttc.es>
7 * Michele Polese <michele.polese@gmail.com> for the OutdoorPositionAllocator class
8 */
10
11#include "buildings-helper.h"
12
13#include "ns3/boolean.h"
14#include "ns3/box.h"
15#include "ns3/building-list.h"
16#include "ns3/building.h"
17#include "ns3/double.h"
18#include "ns3/enum.h"
19#include "ns3/log.h"
20#include "ns3/mobility-building-info.h"
21#include "ns3/mobility-model.h"
22#include "ns3/pointer.h"
23#include "ns3/random-variable-stream.h"
24#include "ns3/string.h"
25#include "ns3/uinteger.h"
26
27#include <cmath>
28
29namespace ns3
30{
31
32NS_LOG_COMPONENT_DEFINE("BuildingPositionAllocator");
33
34NS_OBJECT_ENSURE_REGISTERED(RandomBuildingPositionAllocator);
35
40
43{
44 static TypeId tid =
45 TypeId("ns3::RandomBuildingPositionAllocator")
47 .SetGroupName("Buildings")
48 .AddConstructor<RandomBuildingPositionAllocator>()
49 .AddAttribute("WithReplacement",
50 "If true, the building will be randomly selected with replacement. "
51 "If false, no replacement will occur, until the list of buildings "
52 "to select becomes empty, at which point it will be filled again "
53 "with the list of all buildings.",
54 BooleanValue(false),
57 return tid;
58}
59
60Vector
62{
63 NS_ASSERT_MSG(BuildingList::GetNBuildings() > 0, "no building found");
66 {
67 uint32_t n = m_rand->GetInteger(0, BuildingList::GetNBuildings() - 1);
69 }
70 else
71 {
73 {
74 for (auto bit = BuildingList::Begin(); bit != BuildingList::End(); ++bit)
75 {
77 }
78 }
79 uint32_t n = m_rand->GetInteger(0, m_buildingListWithoutReplacement.size() - 1);
82 }
83
85 BoxValue bv;
86 b->GetAttribute("Boundaries", bv);
87 double x = m_rand->GetValue(bv.Get().xMin, bv.Get().xMax);
88 double y = m_rand->GetValue(bv.Get().yMin, bv.Get().yMax);
89 double z = m_rand->GetValue(bv.Get().zMin, bv.Get().zMax);
90 return Vector(x, y, z);
91}
92
93int64_t
95{
96 m_rand->SetStream(stream);
97 return 1;
98}
99
101
105
106TypeId
108{
109 static TypeId tid =
110 TypeId("ns3::OutdoorPositionAllocator")
112 .SetGroupName("Buildings")
113 .AddConstructor<OutdoorPositionAllocator>()
114 .AddAttribute("X",
115 "A random variable which represents the x coordinate of a position in a "
116 "random box.",
117 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
120 .AddAttribute("Y",
121 "A random variable which represents the y coordinate of a position in a "
122 "random box.",
123 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
126 .AddAttribute("Z",
127 "A random variable which represents the z coordinate of a position in a "
128 "random box.",
129 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
132 .AddAttribute("MaxAttempts",
133 "Maximum number of attempts for the rejection sampling before giving up.",
134 UintegerValue(1000),
137
138 return tid;
139}
140
141void
146
147void
152
153void
158
159Vector
161{
162 NS_ABORT_MSG_IF(BuildingList::GetNBuildings() == 0, "no building found");
163
164 bool outdoor = false;
165 uint32_t attempts = 0;
166 Vector position = Vector(0, 0, 0);
167
168 while (!outdoor && attempts < m_maxAttempts)
169 {
170 // get a random position
171 double x = m_x->GetValue();
172 double y = m_y->GetValue();
173 double z = m_z->GetValue();
174
175 position = Vector(x, y, z);
176
177 NS_LOG_INFO("Position " << position);
178
179 bool inside = false;
180 for (auto bit = BuildingList::Begin(); bit != BuildingList::End(); ++bit)
181 {
182 if ((*bit)->IsInside(position))
183 {
184 NS_LOG_INFO("Position "
185 << position << " is inside the building with boundaries "
186 << (*bit)->GetBoundaries().xMin << " " << (*bit)->GetBoundaries().xMax
187 << " " << (*bit)->GetBoundaries().yMin << " "
188 << (*bit)->GetBoundaries().yMax << " " << (*bit)->GetBoundaries().zMin
189 << " " << (*bit)->GetBoundaries().zMax);
190 inside = true;
191 break;
192 }
193 }
194
195 if (inside)
196 {
197 NS_LOG_INFO("Inside a building, attempt " << attempts << " out of " << m_maxAttempts);
198 attempts++;
199 }
200 else
201 {
202 NS_LOG_INFO("Outdoor position found " << position);
203 outdoor = true;
204 }
205 }
206
207 NS_ABORT_MSG_IF(attempts >= m_maxAttempts, "Too many attempts, give up");
208 NS_ABORT_MSG_IF(!outdoor, "Still indoor, give up");
209 return position;
210}
211
212int64_t
214{
215 m_x->SetStream(stream);
216 m_y->SetStream(stream + 1);
217 m_z->SetStream(stream + 2);
218 return 3;
219}
220
222
227
228TypeId
230{
231 static TypeId tid = TypeId("ns3::RandomRoomPositionAllocator")
233 .SetGroupName("Buildings")
234 .AddConstructor<RandomRoomPositionAllocator>();
235 return tid;
236}
237
238Vector
240{
241 NS_LOG_FUNCTION(this);
242 NS_ASSERT_MSG(BuildingList::GetNBuildings() > 0, "no building found");
243
245 {
246 for (auto bit = BuildingList::Begin(); bit != BuildingList::End(); ++bit)
247 {
248 NS_LOG_LOGIC("building " << (*bit)->GetId());
249 for (uint32_t rx = 1; rx <= (*bit)->GetNRoomsX(); ++rx)
250 {
251 for (uint32_t ry = 1; ry <= (*bit)->GetNRoomsY(); ++ry)
252 {
253 for (uint32_t f = 1; f <= (*bit)->GetNFloors(); ++f)
254 {
255 RoomInfo i;
256 i.roomx = rx;
257 i.roomy = ry;
258 i.floor = f;
259 i.b = *bit;
260 NS_LOG_LOGIC("adding room (" << rx << ", " << ry << ", " << f << ")");
261 m_roomListWithoutReplacement.push_back(i);
262 }
263 }
264 }
265 }
266 }
267 uint32_t n = m_rand->GetInteger(0, m_roomListWithoutReplacement.size() - 1);
270 NS_LOG_LOGIC("considering building " << r.b->GetId() << " room (" << r.roomx << ", " << r.roomy
271 << ", " << r.floor << ")");
272
274 BoxValue bv;
275 r.b->GetAttribute("Boundaries", bv);
276 Box box = bv.Get();
277 double rdx = (box.xMax - box.xMin) / r.b->GetNRoomsX();
278 double rdy = (box.yMax - box.yMin) / r.b->GetNRoomsY();
279 double rdz = (box.zMax - box.zMin) / r.b->GetNFloors();
280 double x1 = box.xMin + rdx * (r.roomx - 1);
281 double x2 = box.xMin + rdx * r.roomx;
282 double y1 = box.yMin + rdy * (r.roomy - 1);
283 double y2 = box.yMin + rdy * r.roomy;
284 double z1 = box.zMin + rdz * (r.floor - 1);
285 double z2 = box.zMin + rdz * r.floor;
286 NS_LOG_LOGIC("randomly allocating position in (" << x1 << "," << x2 << ") "
287 << "x (" << y1 << "," << y2 << ") "
288 << "x (" << z1 << "," << z2 << ") ");
289
290 double x = m_rand->GetValue(x1, x2);
291 double y = m_rand->GetValue(y1, y2);
292 double z = m_rand->GetValue(z1, z2);
293
294 return Vector(x, y, z);
295}
296
297int64_t
299{
300 m_rand->SetStream(stream);
301 return 1;
302}
303
305
307{
308 NS_FATAL_ERROR(" Constructor \"SameRoomPositionAllocator ()\" should not be used");
309}
310
312 : m_nodes(c)
313{
316 // this is needed to make sure the building models associated with c have been initialized
317 for (auto it = m_nodes.Begin(); it != m_nodes.End(); ++it)
318 {
319 Ptr<MobilityModel> mm = (*it)->GetObject<MobilityModel>();
320 NS_ASSERT_MSG(mm, "no mobility model aggregated to this node");
321 Ptr<MobilityBuildingInfo> bmm = mm->GetObject<MobilityBuildingInfo>();
322 NS_ASSERT_MSG(bmm,
323 "MobilityBuildingInfo has not been aggregated to this node mobility model");
324 bmm->MakeConsistent(mm);
325 }
326}
327
328TypeId
330{
331 static TypeId tid = TypeId("ns3::SameRoomPositionAllocator")
333 .SetGroupName("Buildings")
334 .AddConstructor<SameRoomPositionAllocator>();
335 return tid;
336}
337
338Vector
340{
341 NS_LOG_FUNCTION(this);
342 if (m_nodeIt == m_nodes.End())
343 {
345 }
346
347 NS_ASSERT_MSG(m_nodeIt != m_nodes.End(), "no node in container");
348
349 NS_LOG_LOGIC("considering node " << (*m_nodeIt)->GetId());
350 Ptr<MobilityModel> mm = (*m_nodeIt)->GetObject<MobilityModel>();
351 NS_ASSERT_MSG(mm, "no mobility model aggregated to this node");
352 Ptr<MobilityBuildingInfo> bmm = mm->GetObject<MobilityBuildingInfo>();
353 NS_ASSERT_MSG(bmm, "MobilityBuildingInfo has not been aggregated to this node mobility model");
354
355 ++m_nodeIt;
356 uint32_t roomx = bmm->GetRoomNumberX();
357 uint32_t roomy = bmm->GetRoomNumberY();
358 uint32_t floor = bmm->GetFloorNumber();
359 NS_LOG_LOGIC("considering building " << bmm->GetBuilding()->GetId() << " room (" << roomx
360 << ", " << roomy << ", " << floor << ")");
361
362 Ptr<Building> b = bmm->GetBuilding();
364 BoxValue bv;
365 b->GetAttribute("Boundaries", bv);
366 Box box = bv.Get();
367 double rdx = (box.xMax - box.xMin) / b->GetNRoomsX();
368 double rdy = (box.yMax - box.yMin) / b->GetNRoomsY();
369 double rdz = (box.zMax - box.zMin) / b->GetNFloors();
370 double x1 = box.xMin + rdx * (roomx - 1);
371 double x2 = box.xMin + rdx * roomx;
372 double y1 = box.yMin + rdy * (roomy - 1);
373 double y2 = box.yMin + rdy * roomy;
374 double z1 = box.zMin + rdz * (floor - 1);
375 double z2 = box.zMin + rdz * floor;
376 NS_LOG_LOGIC("randomly allocating position in (" << x1 << "," << x2 << ") "
377 << "x (" << y1 << "," << y2 << ") "
378 << "x (" << z1 << "," << z2 << ") ");
379
380 double x = m_rand->GetValue(x1, x2);
381 double y = m_rand->GetValue(y1, y2);
382 double z = m_rand->GetValue(z1, z2);
383
384 return Vector(x, y, z);
385}
386
387int64_t
389{
390 m_rand->SetStream(stream);
391 return 1;
392}
393
395
407
408TypeId
410{
411 static TypeId tid = TypeId("ns3::FixedRoomPositionAllocator")
413 .SetGroupName("Buildings")
414 .AddConstructor<SameRoomPositionAllocator>();
415 return tid;
416}
417
418Vector
420{
421 NS_LOG_LOGIC("considering building " << bptr->GetId() << " room (" << roomx << ", " << roomy
422 << ", " << floor << ")");
423
425
426 Box box = bptr->GetBoundaries();
427 double rdx = (box.xMax - box.xMin) / bptr->GetNRoomsX();
428 double rdy = (box.yMax - box.yMin) / bptr->GetNRoomsY();
429 double rdz = (box.zMax - box.zMin) / bptr->GetNFloors();
430 double x1 = box.xMin + rdx * (roomx - 1);
431 double x2 = box.xMin + rdx * roomx;
432 double y1 = box.yMin + rdy * (roomy - 1);
433 double y2 = box.yMin + rdy * roomy;
434 double z1 = box.zMin + rdz * (floor - 1);
435 double z2 = box.zMin + rdz * floor;
436 NS_LOG_LOGIC("randomly allocating position in (" << x1 << "," << x2 << ") "
437 << "x (" << y1 << "," << y2 << ") "
438 << "x (" << z1 << "," << z2 << ") ");
439
440 double x = m_rand->GetValue(x1, x2);
441 double y = m_rand->GetValue(y1, y2);
442 double z = m_rand->GetValue(z1, z2);
443 return Vector(x, y, z);
444}
445
446int64_t
448{
449 m_rand->SetStream(stream);
450 return 1;
451}
452
453} // namespace ns3
AttributeValue implementation for Boolean.
Definition boolean.h:26
a 3d box
Definition box.h:24
double yMax
The y coordinate of the top bound of the box.
Definition box.h:105
double xMin
The x coordinate of the left bound of the box.
Definition box.h:99
double yMin
The y coordinate of the bottom bound of the box.
Definition box.h:103
double xMax
The x coordinate of the right bound of the box.
Definition box.h:101
double zMin
The z coordinate of the down bound of the box.
Definition box.h:107
double zMax
The z coordinate of the up bound of the box.
Definition box.h:109
AttributeValue implementation for Box.
Definition box.h:115
Box Get() const
Definition box.cc:191
static Ptr< Building > GetBuilding(uint32_t n)
static uint32_t GetNBuildings()
static Iterator End()
static Iterator Begin()
Generate a random position uniformly distributed in the volume of a chosen room inside a chosen build...
uint32_t floor
Index of the room on the z-axis (i.e., floor number)
uint32_t roomx
Index of the room on the x-axis.
uint32_t roomy
Index of the room on the y-axis.
Ptr< UniformRandomVariable > m_rand
Provides uniform random variables.
Ptr< Building > bptr
Pointer to the chosen building.
int64_t AssignStreams(int64_t) override
Assign a fixed random variable stream number to the random variables used by this model.
static TypeId GetTypeId()
Get the type ID.
FixedRoomPositionAllocator(uint32_t x, uint32_t y, uint32_t z, Ptr< Building > b)
mobility buildings information (to be used by mobility models)
Keep track of the current position and velocity of an object.
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
void SetY(Ptr< RandomVariableStream > y)
Set the random variable stream object that generates y-positions.
uint32_t m_maxAttempts
maximum number of attempts before giving up
static TypeId GetTypeId()
Get the type ID.
void SetX(Ptr< RandomVariableStream > x)
Set the random variable stream object that generates x-positions.
Ptr< RandomVariableStream > m_x
pointer to x's random variable stream
Ptr< RandomVariableStream > m_z
pointer to z's random variable stream
Ptr< RandomVariableStream > m_y
pointer to y's random variable stream
void SetZ(Ptr< RandomVariableStream > z)
Set the random variable stream object that generates z-positions.
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this model.
Allocate a set of positions.
Smart pointer class similar to boost::intrusive_ptr.
Allocate each position by randomly choosing a building from the list of all buildings,...
std::vector< Ptr< Building > > m_buildingListWithoutReplacement
List of building without replacement.
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this model.
bool m_withReplacement
If true, the building will be randomly selected with replacement.
Ptr< UniformRandomVariable > m_rand
Provides uniform random variables.
Allocate each position by randomly choosing a room from the list of all buildings,...
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this model.
std::vector< RoomInfo > m_roomListWithoutReplacement
Container of rooms.
static TypeId GetTypeId()
Get the type ID.
Ptr< UniformRandomVariable > m_rand
Provides uniform random variables.
virtual double GetValue()=0
Get the next random value drawn from the distribution.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
Walks a given NodeContainer sequentially, and for each node allocate a new position randomly in the s...
NodeContainer m_nodes
Nodes container.
Ptr< UniformRandomVariable > m_rand
Provides uniform random variables.
int64_t AssignStreams(int64_t) override
Assign a fixed random variable stream number to the random variables used by this model.
NodeContainer::Iterator m_nodeIt
Nodes iterator.
static TypeId GetTypeId()
Get the type ID.
Hold variables of type string.
Definition string.h:45
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Hold an unsigned integer type.
Definition uinteger.h:34
#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:75
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:113
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:70
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:248
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:269
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
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:35
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:97
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.