A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
position-allocator.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
9
10#include "ns3/csv-reader.h"
11#include "ns3/double.h"
12#include "ns3/enum.h"
13#include "ns3/log.h"
14#include "ns3/pointer.h"
15#include "ns3/string.h"
16#include "ns3/uinteger.h"
17
18#include <cmath>
19
20namespace ns3
21{
22
23NS_LOG_COMPONENT_DEFINE("PositionAllocator");
24
25NS_OBJECT_ENSURE_REGISTERED(PositionAllocator);
26
27TypeId
29{
30 static TypeId tid =
31 TypeId("ns3::PositionAllocator").SetParent<Object>().SetGroupName("Mobility");
32 return tid;
33}
34
38
42
44
47{
48 static TypeId tid = TypeId("ns3::ListPositionAllocator")
50 .SetGroupName("Mobility")
51 .AddConstructor<ListPositionAllocator>();
52 return tid;
53}
54
58
59void
61{
62 m_positions.push_back(v);
63 m_current = m_positions.begin();
64}
65
66void
67ListPositionAllocator::Add(const std::string filePath,
68 double defaultZ /* = 0 */,
69 char delimiter /* = ',' */)
70{
71 NS_LOG_FUNCTION(this << filePath << std::string("'") + delimiter + "'");
72
73 CsvReader csv(filePath, delimiter);
74 while (csv.FetchNextRow())
75 {
76 if (csv.ColumnCount() == 1)
77 {
78 // comment line
79 continue;
80 }
81
82 double x;
83 double y;
84 double z;
85 bool ok = csv.GetValue(0, x);
86 NS_LOG_INFO("read x: " << x << (ok ? " ok" : " FAIL"));
87 NS_ASSERT_MSG(ok, "failed reading x");
88 ok = csv.GetValue(1, y);
89 NS_LOG_INFO("read y = " << y << (ok ? " ok" : " FAIL"));
90 NS_ASSERT_MSG(ok, "failed reading y");
91 if (csv.ColumnCount() > 2)
92 {
93 ok = csv.GetValue(2, z);
94 NS_LOG_INFO("read z = " << z << (ok ? " ok" : " FAIL"));
95 NS_ASSERT_MSG(ok, "failed reading z");
96 }
97 else
98 {
99 z = defaultZ;
100 NS_LOG_LOGIC("using default Z " << defaultZ);
101 }
102
103 Vector pos(x, y, z);
104 Add(pos);
105 }
106 NS_LOG_INFO("read " << csv.RowNumber() << " rows");
107}
108
109Vector
111{
112 Vector v = *m_current;
113 m_current++;
114 if (m_current == m_positions.end())
115 {
116 m_current = m_positions.begin();
117 }
118 return v;
119}
120
121int64_t
123{
124 return 0;
125}
126
129{
130 return m_positions.size();
131}
132
134
135TypeId
137{
138 static TypeId tid =
139 TypeId("ns3::GridPositionAllocator")
141 .SetGroupName("Mobility")
142 .AddConstructor<GridPositionAllocator>()
143 .AddAttribute("GridWidth",
144 "The number of objects laid out on a line.",
145 UintegerValue(10),
148 .AddAttribute("MinX",
149 "The x coordinate where the grid starts.",
150 DoubleValue(1.0),
153 .AddAttribute("MinY",
154 "The y coordinate where the grid starts.",
155 DoubleValue(0.0),
158 .AddAttribute("Z",
159 "The z coordinate of all the positions allocated.",
160 DoubleValue(0.0),
163 .AddAttribute("DeltaX",
164 "The x space between objects.",
165 DoubleValue(1.0),
168 .AddAttribute("DeltaY",
169 "The y space between objects.",
170 DoubleValue(1.0),
173 .AddAttribute("LayoutType",
174 "The type of layout.",
177 MakeEnumChecker(ROW_FIRST, "RowFirst", COLUMN_FIRST, "ColumnFirst"));
178 return tid;
179}
180
182 : m_current(0)
183{
184}
185
186void
188{
189 m_xMin = xMin;
190}
191
192void
194{
195 m_yMin = yMin;
196}
197
198void
200{
201 m_z = z;
202}
203
204void
206{
207 m_deltaX = deltaX;
208}
209
210void
212{
213 m_deltaY = deltaY;
214}
215
216void
221
222void
224{
225 m_layoutType = layoutType;
226}
227
228double
230{
231 return m_xMin;
232}
233
234double
236{
237 return m_yMin;
238}
239
240double
242{
243 return m_deltaX;
244}
245
246double
248{
249 return m_deltaY;
250}
251
254{
255 return m_n;
256}
257
263
264Vector
266{
267 double x = 0.0;
268 double y = 0.0;
269 switch (m_layoutType)
270 {
271 case ROW_FIRST:
272 x = m_xMin + m_deltaX * (m_current % m_n);
273 y = m_yMin + m_deltaY * (m_current / m_n);
274 break;
275 case COLUMN_FIRST:
276 x = m_xMin + m_deltaX * (m_current / m_n);
277 y = m_yMin + m_deltaY * (m_current % m_n);
278 break;
279 }
280 m_current++;
281 return Vector(x, y, m_z);
282}
283
284int64_t
286{
287 return 0;
288}
289
291
292TypeId
294{
295 static TypeId tid =
296 TypeId("ns3::RandomRectanglePositionAllocator")
298 .SetGroupName("Mobility")
299 .AddConstructor<RandomRectanglePositionAllocator>()
300 .AddAttribute("X",
301 "A random variable which represents the x coordinate of a position in a "
302 "random rectangle.",
303 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
306 .AddAttribute("Y",
307 "A random variable which represents the y coordinate of a position in a "
308 "random rectangle.",
309 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
312 .AddAttribute("Z",
313 "The z coordinate of all the positions allocated.",
314 DoubleValue(0.0),
317 return tid;
318}
319
323
327
328void
333
334void
339
340void
342{
343 m_z = z;
344}
345
346Vector
348{
349 double x = m_x->GetValue();
350 double y = m_y->GetValue();
351 return Vector(x, y, m_z);
352}
353
354int64_t
356{
357 m_x->SetStream(stream);
358 m_y->SetStream(stream + 1);
359 return 2;
360}
361
363
364TypeId
366{
367 static TypeId tid =
368 TypeId("ns3::RandomBoxPositionAllocator")
370 .SetGroupName("Mobility")
371 .AddConstructor<RandomBoxPositionAllocator>()
372 .AddAttribute("X",
373 "A random variable which represents the x coordinate of a position in a "
374 "random box.",
375 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
378 .AddAttribute("Y",
379 "A random variable which represents the y coordinate of a position in a "
380 "random box.",
381 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
384 .AddAttribute("Z",
385 "A random variable which represents the z coordinate of a position in a "
386 "random box.",
387 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
390 return tid;
391}
392
396
400
401void
406
407void
412
413void
418
419Vector
421{
422 double x = m_x->GetValue();
423 double y = m_y->GetValue();
424 double z = m_z->GetValue();
425 return Vector(x, y, z);
426}
427
428int64_t
430{
431 m_x->SetStream(stream);
432 m_y->SetStream(stream + 1);
433 m_z->SetStream(stream + 2);
434 return 3;
435}
436
438
439TypeId
441{
442 static TypeId tid =
443 TypeId("ns3::RandomDiscPositionAllocator")
445 .SetGroupName("Mobility")
446 .AddConstructor<RandomDiscPositionAllocator>()
447 .AddAttribute("Theta",
448 "A random variable which represents the angle (gradients) of a position "
449 "in a random disc.",
450 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=6.2830]"),
453 .AddAttribute(
454 "Rho",
455 "A random variable which represents the radius of a position in a random disc.",
456 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=200.0]"),
459 .AddAttribute("X",
460 "The x coordinate of the center of the random position disc.",
461 DoubleValue(0.0),
464 .AddAttribute("Y",
465 "The y coordinate of the center of the random position disc.",
466 DoubleValue(0.0),
469 .AddAttribute("Z",
470 "The z coordinate of all the positions in the disc.",
471 DoubleValue(0.0),
474 return tid;
475}
476
480
484
485void
490
491void
496
497void
499{
500 m_x = x;
501}
502
503void
505{
506 m_y = y;
507}
508
509void
511{
512 m_z = z;
513}
514
515Vector
517{
518 double theta = m_theta->GetValue();
519 double rho = m_rho->GetValue();
520 double x = m_x + std::cos(theta) * rho;
521 double y = m_y + std::sin(theta) * rho;
522 NS_LOG_DEBUG("Disc position x=" << x << ", y=" << y);
523 return Vector(x, y, m_z);
524}
525
526int64_t
528{
529 m_theta->SetStream(stream);
530 m_rho->SetStream(stream + 1);
531 return 2;
532}
533
535
536TypeId
538{
539 static TypeId tid = TypeId("ns3::UniformDiscPositionAllocator")
541 .SetGroupName("Mobility")
542 .AddConstructor<UniformDiscPositionAllocator>()
543 .AddAttribute("rho",
544 "The radius of the disc",
545 DoubleValue(0.0),
548 .AddAttribute("X",
549 "The x coordinate of the center of the disc.",
550 DoubleValue(0.0),
553 .AddAttribute("Y",
554 "The y coordinate of the center of the disc.",
555 DoubleValue(0.0),
558 .AddAttribute("Z",
559 "The z coordinate of all the positions in the disc.",
560 DoubleValue(0.0),
563 return tid;
564}
565
570
574
575void
577{
578 m_rho = rho;
579}
580
581void
583{
584 m_x = x;
585}
586
587void
589{
590 m_y = y;
591}
592
593void
595{
596 m_z = z;
597}
598
599Vector
601{
602 double x;
603 double y;
604 do
605 {
606 x = m_rv->GetValue(-m_rho, m_rho);
607 y = m_rv->GetValue(-m_rho, m_rho);
608 } while (std::sqrt(x * x + y * y) > m_rho);
609
610 x += m_x;
611 y += m_y;
612 NS_LOG_DEBUG("Disc position x=" << x << ", y=" << y);
613 return Vector(x, y, m_z);
614}
615
616int64_t
618{
619 m_rv->SetStream(stream);
620 return 1;
621}
622
623} // namespace ns3
Provides functions for parsing and extracting data from Comma Separated Value (CSV) formatted text fi...
Definition csv-reader.h:221
bool GetValue(std::size_t columnIndex, T &value) const
Attempt to convert from the string data in the specified column to the specified data type.
Definition csv-reader.h:399
std::size_t RowNumber() const
The number of lines that have been read.
Definition csv-reader.cc:94
std::size_t ColumnCount() const
Returns the number of columns in the csv data.
Definition csv-reader.cc:86
bool FetchNextRow()
Reads one line from the input until a new line is encountered.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
Hold variables of type enum.
Definition enum.h:52
Allocate positions on a rectangular 2d grid.
double m_deltaX
x interval between two consecutive x positions
double m_deltaY
y interval between two consecutive y positions
LayoutType
Determine whether positions are allocated row first or column first.
@ COLUMN_FIRST
In column-first mode, positions are allocated on the first column until N positions have been allocat...
@ ROW_FIRST
In row-first mode, positions are allocated on the first row until N positions have been allocated.
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this model.
uint32_t m_current
currently position
double m_yMin
minimum boundary on y positions
Vector GetNext() const override
double m_z
z coordinate of all the positions generated
LayoutType m_layoutType
currently selected layout type
double m_xMin
minimum boundary on x positions
uint32_t m_n
number of positions to allocate on each row or column
static TypeId GetTypeId()
Register this type with the TypeId system.
void SetLayoutType(LayoutType layoutType)
Allocate positions from a deterministic list specified by the user.
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this model.
void Add(Vector v)
Add a position to the list of positions.
uint32_t GetSize() const
Return the number of positions stored.
static TypeId GetTypeId()
Register this type with the TypeId system.
std::vector< Vector >::const_iterator m_current
vector iterator
Vector GetNext() const override
std::vector< Vector > m_positions
vector of positions
A base class which provides memory management and object aggregation.
Definition object.h:78
Allocate a set of positions.
static TypeId GetTypeId()
Register this type with the TypeId system.
Smart pointer class similar to boost::intrusive_ptr.
Allocate random positions within a 3D box according to a set of three random variables.
void SetZ(Ptr< RandomVariableStream > z)
Set the random variable stream object that generates z-positions.
Ptr< RandomVariableStream > m_x
pointer to x's random variable stream
Ptr< RandomVariableStream > m_y
pointer to y's random variable stream
void SetX(Ptr< RandomVariableStream > x)
Set the random variable stream object that generates x-positions.
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this model.
void SetY(Ptr< RandomVariableStream > y)
Set the random variable stream object that generates y-positions.
static TypeId GetTypeId()
Register this type with the TypeId system.
Ptr< RandomVariableStream > m_z
pointer to z's random variable stream
Allocate random positions within a disc according to a given distribution for the polar coordinates o...
double m_y
y coordinate of center of disc
Ptr< RandomVariableStream > m_rho
pointer to rho's random variable stream
static TypeId GetTypeId()
Register this type with the TypeId system.
void SetRho(Ptr< RandomVariableStream > rho)
Set the random variable that generates position radius, in meters.
double m_x
x coordinate of center of disc
void SetTheta(Ptr< RandomVariableStream > theta)
Set the random variable that generates position angle, in radians.
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this model.
Ptr< RandomVariableStream > m_theta
pointer to theta's random variable stream
double m_z
z coordinate of the disc
Allocate random positions within a rectangle according to a pair of random variables.
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this model.
Ptr< RandomVariableStream > m_y
pointer to y's random variable stream
Ptr< RandomVariableStream > m_x
pointer to x's random variable stream
void SetY(Ptr< RandomVariableStream > y)
Set the random variable stream object that generates y-positions.
static TypeId GetTypeId()
Register this type with the TypeId system.
void SetX(Ptr< RandomVariableStream > x)
Set the random variable stream object that generates x-positions.
double m_z
z coordinate of all the positions generated
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.
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
Allocate the positions uniformly (with constant density) randomly within a disc.
static TypeId GetTypeId()
Register this type with the TypeId system.
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this model.
double m_rho
value of the radius of the disc
double m_x
x coordinate of center of disc
double m_y
y coordinate of center of disc
Ptr< UniformRandomVariable > m_rv
pointer to uniform random variable
double m_z
z coordinate of the disc
#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 > MakeDoubleChecker()
Definition double.h:82
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition double.h:32
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition enum.h:221
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_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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 > MakeEnumChecker(T v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition enum.h:179