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-model.h"
21#include "ns3/pointer.h"
22#include "ns3/random-variable-stream.h"
23#include "ns3/string.h"
24#include "ns3/uinteger.h"
25#include <ns3/mobility-building-info.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 "
287 << " (" << x1 << "," << x2 << ") "
288 << "x (" << y1 << "," << y2 << ") "
289 << "x (" << z1 << "," << z2 << ") ");
290
291 double x = m_rand->GetValue(x1, x2);
292 double y = m_rand->GetValue(y1, y2);
293 double z = m_rand->GetValue(z1, z2);
294
295 return Vector(x, y, z);
296}
297
298int64_t
300{
301 m_rand->SetStream(stream);
302 return 1;
303}
304
306
308{
309 NS_FATAL_ERROR(" Constructor \"SameRoomPositionAllocator ()\" should not be used");
310}
311
313 : m_nodes(c)
314{
317 // this is needed to make sure the building models associated with c have been initialized
318 for (auto it = m_nodes.Begin(); it != m_nodes.End(); ++it)
319 {
320 Ptr<MobilityModel> mm = (*it)->GetObject<MobilityModel>();
321 NS_ASSERT_MSG(mm, "no mobility model aggregated to this node");
322 Ptr<MobilityBuildingInfo> bmm = mm->GetObject<MobilityBuildingInfo>();
323 NS_ASSERT_MSG(bmm,
324 "MobilityBuildingInfo has not been aggregated to this node mobility model");
325 bmm->MakeConsistent(mm);
326 }
327}
328
329TypeId
331{
332 static TypeId tid = TypeId("ns3::SameRoomPositionAllocator")
334 .SetGroupName("Buildings")
335 .AddConstructor<SameRoomPositionAllocator>();
336 return tid;
337}
338
339Vector
341{
342 NS_LOG_FUNCTION(this);
343 if (m_nodeIt == m_nodes.End())
344 {
346 }
347
348 NS_ASSERT_MSG(m_nodeIt != m_nodes.End(), "no node in container");
349
350 NS_LOG_LOGIC("considering node " << (*m_nodeIt)->GetId());
351 Ptr<MobilityModel> mm = (*m_nodeIt)->GetObject<MobilityModel>();
352 NS_ASSERT_MSG(mm, "no mobility model aggregated to this node");
353 Ptr<MobilityBuildingInfo> bmm = mm->GetObject<MobilityBuildingInfo>();
354 NS_ASSERT_MSG(bmm, "MobilityBuildingInfo has not been aggregated to this node mobility model");
355
356 ++m_nodeIt;
357 uint32_t roomx = bmm->GetRoomNumberX();
358 uint32_t roomy = bmm->GetRoomNumberY();
359 uint32_t floor = bmm->GetFloorNumber();
360 NS_LOG_LOGIC("considering building " << bmm->GetBuilding()->GetId() << " room (" << roomx
361 << ", " << roomy << ", " << floor << ")");
362
363 Ptr<Building> b = bmm->GetBuilding();
365 BoxValue bv;
366 b->GetAttribute("Boundaries", bv);
367 Box box = bv.Get();
368 double rdx = (box.xMax - box.xMin) / b->GetNRoomsX();
369 double rdy = (box.yMax - box.yMin) / b->GetNRoomsY();
370 double rdz = (box.zMax - box.zMin) / b->GetNFloors();
371 double x1 = box.xMin + rdx * (roomx - 1);
372 double x2 = box.xMin + rdx * roomx;
373 double y1 = box.yMin + rdy * (roomy - 1);
374 double y2 = box.yMin + rdy * roomy;
375 double z1 = box.zMin + rdz * (floor - 1);
376 double z2 = box.zMin + rdz * floor;
377 NS_LOG_LOGIC("randomly allocating position in "
378 << " (" << x1 << "," << x2 << ") "
379 << "x (" << y1 << "," << y2 << ") "
380 << "x (" << z1 << "," << z2 << ") ");
381
382 double x = m_rand->GetValue(x1, x2);
383 double y = m_rand->GetValue(y1, y2);
384 double z = m_rand->GetValue(z1, z2);
385
386 return Vector(x, y, z);
387}
388
389int64_t
391{
392 m_rand->SetStream(stream);
393 return 1;
394}
395
397
409
410TypeId
412{
413 static TypeId tid = TypeId("ns3::FixedRoomPositionAllocator")
415 .SetGroupName("Buildings")
416 .AddConstructor<SameRoomPositionAllocator>();
417 return tid;
418}
419
420Vector
422{
423 NS_LOG_LOGIC("considering building " << bptr->GetId() << " room (" << roomx << ", " << roomy
424 << ", " << floor << ")");
425
427
428 Box box = bptr->GetBoundaries();
429 double rdx = (box.xMax - box.xMin) / bptr->GetNRoomsX();
430 double rdy = (box.yMax - box.yMin) / bptr->GetNRoomsY();
431 double rdz = (box.zMax - box.zMin) / bptr->GetNFloors();
432 double x1 = box.xMin + rdx * (roomx - 1);
433 double x2 = box.xMin + rdx * roomx;
434 double y1 = box.yMin + rdy * (roomy - 1);
435 double y2 = box.yMin + rdy * roomy;
436 double z1 = box.zMin + rdz * (floor - 1);
437 double z2 = box.zMin + rdz * floor;
438 NS_LOG_LOGIC("randomly allocating position in "
439 << " (" << x1 << "," << x2 << ") "
440 << "x (" << y1 << "," << y2 << ") "
441 << "x (" << z1 << "," << z2 << ") ");
442
443 double x = m_rand->GetValue(x1, x2);
444 double y = m_rand->GetValue(y1, y2);
445 double z = m_rand->GetValue(z1, z2);
446 return Vector(x, y, z);
447}
448
449int64_t
451{
452 m_rand->SetStream(stream);
453 return 1;
454}
455
456} // namespace ns3
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
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:48
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 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
#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.
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:113
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition boolean.h:70