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 } // while FetchNextRow
107 NS_LOG_INFO("read " << csv.RowNumber() << " rows");
108}
109
110Vector
112{
113 Vector v = *m_current;
114 m_current++;
115 if (m_current == m_positions.end())
116 {
117 m_current = m_positions.begin();
118 }
119 return v;
120}
121
122int64_t
124{
125 return 0;
126}
127
130{
131 return m_positions.size();
132}
133
135
136TypeId
138{
139 static TypeId tid =
140 TypeId("ns3::GridPositionAllocator")
142 .SetGroupName("Mobility")
143 .AddConstructor<GridPositionAllocator>()
144 .AddAttribute("GridWidth",
145 "The number of objects laid out on a line.",
146 UintegerValue(10),
149 .AddAttribute("MinX",
150 "The x coordinate where the grid starts.",
151 DoubleValue(1.0),
154 .AddAttribute("MinY",
155 "The y coordinate where the grid starts.",
156 DoubleValue(0.0),
159 .AddAttribute("Z",
160 "The z coordinate of all the positions allocated.",
161 DoubleValue(0.0),
164 .AddAttribute("DeltaX",
165 "The x space between objects.",
166 DoubleValue(1.0),
169 .AddAttribute("DeltaY",
170 "The y space between objects.",
171 DoubleValue(1.0),
174 .AddAttribute("LayoutType",
175 "The type of layout.",
178 MakeEnumChecker(ROW_FIRST, "RowFirst", COLUMN_FIRST, "ColumnFirst"));
179 return tid;
180}
181
183 : m_current(0)
184{
185}
186
187void
189{
190 m_xMin = xMin;
191}
192
193void
195{
196 m_yMin = yMin;
197}
198
199void
201{
202 m_z = z;
203}
204
205void
207{
208 m_deltaX = deltaX;
209}
210
211void
213{
214 m_deltaY = deltaY;
215}
216
217void
222
223void
225{
226 m_layoutType = layoutType;
227}
228
229double
231{
232 return m_xMin;
233}
234
235double
237{
238 return m_yMin;
239}
240
241double
243{
244 return m_deltaX;
245}
246
247double
249{
250 return m_deltaY;
251}
252
255{
256 return m_n;
257}
258
264
265Vector
267{
268 double x = 0.0;
269 double y = 0.0;
270 switch (m_layoutType)
271 {
272 case ROW_FIRST:
273 x = m_xMin + m_deltaX * (m_current % m_n);
274 y = m_yMin + m_deltaY * (m_current / m_n);
275 break;
276 case COLUMN_FIRST:
277 x = m_xMin + m_deltaX * (m_current / m_n);
278 y = m_yMin + m_deltaY * (m_current % m_n);
279 break;
280 }
281 m_current++;
282 return Vector(x, y, m_z);
283}
284
285int64_t
287{
288 return 0;
289}
290
292
293TypeId
295{
296 static TypeId tid =
297 TypeId("ns3::RandomRectanglePositionAllocator")
299 .SetGroupName("Mobility")
300 .AddConstructor<RandomRectanglePositionAllocator>()
301 .AddAttribute("X",
302 "A random variable which represents the x coordinate of a position in a "
303 "random rectangle.",
304 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
307 .AddAttribute("Y",
308 "A random variable which represents the y coordinate of a position in a "
309 "random rectangle.",
310 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
313 .AddAttribute("Z",
314 "The z coordinate of all the positions allocated.",
315 DoubleValue(0.0),
318 return tid;
319}
320
324
328
329void
334
335void
340
341void
343{
344 m_z = z;
345}
346
347Vector
349{
350 double x = m_x->GetValue();
351 double y = m_y->GetValue();
352 return Vector(x, y, m_z);
353}
354
355int64_t
357{
358 m_x->SetStream(stream);
359 m_y->SetStream(stream + 1);
360 return 2;
361}
362
364
365TypeId
367{
368 static TypeId tid =
369 TypeId("ns3::RandomBoxPositionAllocator")
371 .SetGroupName("Mobility")
372 .AddConstructor<RandomBoxPositionAllocator>()
373 .AddAttribute("X",
374 "A random variable which represents the x coordinate of a position in a "
375 "random box.",
376 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
379 .AddAttribute("Y",
380 "A random variable which represents the y coordinate of a position in a "
381 "random box.",
382 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
385 .AddAttribute("Z",
386 "A random variable which represents the z coordinate of a position in a "
387 "random box.",
388 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
391 return tid;
392}
393
397
401
402void
407
408void
413
414void
419
420Vector
422{
423 double x = m_x->GetValue();
424 double y = m_y->GetValue();
425 double z = m_z->GetValue();
426 return Vector(x, y, z);
427}
428
429int64_t
431{
432 m_x->SetStream(stream);
433 m_y->SetStream(stream + 1);
434 m_z->SetStream(stream + 2);
435 return 3;
436}
437
439
440TypeId
442{
443 static TypeId tid =
444 TypeId("ns3::RandomDiscPositionAllocator")
446 .SetGroupName("Mobility")
447 .AddConstructor<RandomDiscPositionAllocator>()
448 .AddAttribute("Theta",
449 "A random variable which represents the angle (gradients) of a position "
450 "in a random disc.",
451 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=6.2830]"),
454 .AddAttribute(
455 "Rho",
456 "A random variable which represents the radius of a position in a random disc.",
457 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=200.0]"),
460 .AddAttribute("X",
461 "The x coordinate of the center of the random position disc.",
462 DoubleValue(0.0),
465 .AddAttribute("Y",
466 "The y coordinate of the center of the random position disc.",
467 DoubleValue(0.0),
470 .AddAttribute("Z",
471 "The z coordinate of all the positions in the disc.",
472 DoubleValue(0.0),
475 return tid;
476}
477
481
485
486void
491
492void
497
498void
500{
501 m_x = x;
502}
503
504void
506{
507 m_y = y;
508}
509
510void
512{
513 m_z = z;
514}
515
516Vector
518{
519 double theta = m_theta->GetValue();
520 double rho = m_rho->GetValue();
521 double x = m_x + std::cos(theta) * rho;
522 double y = m_y + std::sin(theta) * rho;
523 NS_LOG_DEBUG("Disc position x=" << x << ", y=" << y);
524 return Vector(x, y, m_z);
525}
526
527int64_t
529{
530 m_theta->SetStream(stream);
531 m_rho->SetStream(stream + 1);
532 return 2;
533}
534
536
537TypeId
539{
540 static TypeId tid = TypeId("ns3::UniformDiscPositionAllocator")
542 .SetGroupName("Mobility")
543 .AddConstructor<UniformDiscPositionAllocator>()
544 .AddAttribute("rho",
545 "The radius of the disc",
546 DoubleValue(0.0),
549 .AddAttribute("X",
550 "The x coordinate of the center of the disc.",
551 DoubleValue(0.0),
554 .AddAttribute("Y",
555 "The y coordinate of the center of the disc.",
556 DoubleValue(0.0),
559 .AddAttribute("Z",
560 "The z coordinate of all the positions in the disc.",
561 DoubleValue(0.0),
564 return tid;
565}
566
571
575
576void
578{
579 m_rho = rho;
580}
581
582void
584{
585 m_x = x;
586}
587
588void
590{
591 m_y = y;
592}
593
594void
596{
597 m_z = z;
598}
599
600Vector
602{
603 double x;
604 double y;
605 do
606 {
607 x = m_rv->GetValue(-m_rho, m_rho);
608 y = m_rv->GetValue(-m_rho, m_rho);
609 } while (std::sqrt(x * x + y * y) > m_rho);
610
611 x += m_x;
612 y += m_y;
613 NS_LOG_DEBUG("Disc position x=" << x << ", y=" << y);
614 return Vector(x, y, m_z);
615}
616
617int64_t
619{
620 m_rv->SetStream(stream);
621 return 1;
622}
623
624} // 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:400
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:48
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 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_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 > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
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
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition double.h:32
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Definition enum.h:221