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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#include "position-allocator.h"
20
21#include "ns3/csv-reader.h"
22#include "ns3/double.h"
23#include "ns3/enum.h"
24#include "ns3/log.h"
25#include "ns3/pointer.h"
26#include "ns3/string.h"
27#include "ns3/uinteger.h"
28
29#include <cmath>
30
31namespace ns3
32{
33
34NS_LOG_COMPONENT_DEFINE("PositionAllocator");
35
36NS_OBJECT_ENSURE_REGISTERED(PositionAllocator);
37
38TypeId
40{
41 static TypeId tid =
42 TypeId("ns3::PositionAllocator").SetParent<Object>().SetGroupName("Mobility");
43 return tid;
44}
45
47{
48}
49
51{
52}
53
55
58{
59 static TypeId tid = TypeId("ns3::ListPositionAllocator")
61 .SetGroupName("Mobility")
62 .AddConstructor<ListPositionAllocator>();
63 return tid;
64}
65
67{
68}
69
70void
72{
73 m_positions.push_back(v);
74 m_current = m_positions.begin();
75}
76
77void
78ListPositionAllocator::Add(const std::string filePath,
79 double defaultZ /* = 0 */,
80 char delimiter /* = ',' */)
81{
82 NS_LOG_FUNCTION(this << filePath << std::string("'") + delimiter + "'");
83
84 CsvReader csv(filePath, delimiter);
85 while (csv.FetchNextRow())
86 {
87 if (csv.ColumnCount() == 1)
88 {
89 // comment line
90 continue;
91 }
92
93 double x;
94 double y;
95 double z;
96 bool ok = csv.GetValue(0, x);
97 NS_LOG_INFO("read x: " << x << (ok ? " ok" : " FAIL"));
98 NS_ASSERT_MSG(ok, "failed reading x");
99 ok = csv.GetValue(1, y);
100 NS_LOG_INFO("read y = " << y << (ok ? " ok" : " FAIL"));
101 NS_ASSERT_MSG(ok, "failed reading y");
102 if (csv.ColumnCount() > 2)
103 {
104 ok = csv.GetValue(2, z);
105 NS_LOG_INFO("read z = " << z << (ok ? " ok" : " FAIL"));
106 NS_ASSERT_MSG(ok, "failed reading z");
107 }
108 else
109 {
110 z = defaultZ;
111 NS_LOG_LOGIC("using default Z " << defaultZ);
112 }
113
114 Vector pos(x, y, z);
115 Add(pos);
116
117 } // while FetchNextRow
118 NS_LOG_INFO("read " << csv.RowNumber() << " rows");
119}
120
121Vector
123{
124 Vector v = *m_current;
125 m_current++;
126 if (m_current == m_positions.end())
127 {
128 m_current = m_positions.begin();
129 }
130 return v;
131}
132
133int64_t
135{
136 return 0;
137}
138
141{
142 return m_positions.size();
143}
144
146
147TypeId
149{
150 static TypeId tid =
151 TypeId("ns3::GridPositionAllocator")
153 .SetGroupName("Mobility")
154 .AddConstructor<GridPositionAllocator>()
155 .AddAttribute("GridWidth",
156 "The number of objects laid out on a line.",
157 UintegerValue(10),
159 MakeUintegerChecker<uint32_t>())
160 .AddAttribute("MinX",
161 "The x coordinate where the grid starts.",
162 DoubleValue(1.0),
164 MakeDoubleChecker<double>())
165 .AddAttribute("MinY",
166 "The y coordinate where the grid starts.",
167 DoubleValue(0.0),
169 MakeDoubleChecker<double>())
170 .AddAttribute("Z",
171 "The z coordinate of all the positions allocated.",
172 DoubleValue(0.0),
174 MakeDoubleChecker<double>())
175 .AddAttribute("DeltaX",
176 "The x space between objects.",
177 DoubleValue(1.0),
179 MakeDoubleChecker<double>())
180 .AddAttribute("DeltaY",
181 "The y space between objects.",
182 DoubleValue(1.0),
184 MakeDoubleChecker<double>())
185 .AddAttribute("LayoutType",
186 "The type of layout.",
188 MakeEnumAccessor<LayoutType>(&GridPositionAllocator::m_layoutType),
189 MakeEnumChecker(ROW_FIRST, "RowFirst", COLUMN_FIRST, "ColumnFirst"));
190 return tid;
191}
192
194 : m_current(0)
195{
196}
197
198void
200{
201 m_xMin = xMin;
202}
203
204void
206{
207 m_yMin = yMin;
208}
209
210void
212{
213 m_z = z;
214}
215
216void
218{
219 m_deltaX = deltaX;
220}
221
222void
224{
225 m_deltaY = deltaY;
226}
227
228void
230{
231 m_n = n;
232}
233
234void
236{
237 m_layoutType = layoutType;
238}
239
240double
242{
243 return m_xMin;
244}
245
246double
248{
249 return m_yMin;
250}
251
252double
254{
255 return m_deltaX;
256}
257
258double
260{
261 return m_deltaY;
262}
263
266{
267 return m_n;
268}
269
272{
273 return m_layoutType;
274}
275
276Vector
278{
279 double x = 0.0;
280 double y = 0.0;
281 switch (m_layoutType)
282 {
283 case ROW_FIRST:
284 x = m_xMin + m_deltaX * (m_current % m_n);
285 y = m_yMin + m_deltaY * (m_current / m_n);
286 break;
287 case COLUMN_FIRST:
288 x = m_xMin + m_deltaX * (m_current / m_n);
289 y = m_yMin + m_deltaY * (m_current % m_n);
290 break;
291 }
292 m_current++;
293 return Vector(x, y, m_z);
294}
295
296int64_t
298{
299 return 0;
300}
301
303
304TypeId
306{
307 static TypeId tid =
308 TypeId("ns3::RandomRectanglePositionAllocator")
310 .SetGroupName("Mobility")
311 .AddConstructor<RandomRectanglePositionAllocator>()
312 .AddAttribute("X",
313 "A random variable which represents the x coordinate of a position in a "
314 "random rectangle.",
315 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
317 MakePointerChecker<RandomVariableStream>())
318 .AddAttribute("Y",
319 "A random variable which represents the y coordinate of a position in a "
320 "random rectangle.",
321 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
323 MakePointerChecker<RandomVariableStream>())
324 .AddAttribute("Z",
325 "The z coordinate of all the positions allocated.",
326 DoubleValue(0.0),
328 MakeDoubleChecker<double>());
329 return tid;
330}
331
333{
334}
335
337{
338}
339
340void
342{
343 m_x = x;
344}
345
346void
348{
349 m_y = y;
350}
351
352void
354{
355 m_z = z;
356}
357
358Vector
360{
361 double x = m_x->GetValue();
362 double y = m_y->GetValue();
363 return Vector(x, y, m_z);
364}
365
366int64_t
368{
369 m_x->SetStream(stream);
370 m_y->SetStream(stream + 1);
371 return 2;
372}
373
375
376TypeId
378{
379 static TypeId tid =
380 TypeId("ns3::RandomBoxPositionAllocator")
382 .SetGroupName("Mobility")
383 .AddConstructor<RandomBoxPositionAllocator>()
384 .AddAttribute("X",
385 "A random variable which represents the x coordinate of a position in a "
386 "random box.",
387 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
389 MakePointerChecker<RandomVariableStream>())
390 .AddAttribute("Y",
391 "A random variable which represents the y coordinate of a position in a "
392 "random box.",
393 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
395 MakePointerChecker<RandomVariableStream>())
396 .AddAttribute("Z",
397 "A random variable which represents the z coordinate of a position in a "
398 "random box.",
399 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
401 MakePointerChecker<RandomVariableStream>());
402 return tid;
403}
404
406{
407}
408
410{
411}
412
413void
415{
416 m_x = x;
417}
418
419void
421{
422 m_y = y;
423}
424
425void
427{
428 m_z = z;
429}
430
431Vector
433{
434 double x = m_x->GetValue();
435 double y = m_y->GetValue();
436 double z = m_z->GetValue();
437 return Vector(x, y, z);
438}
439
440int64_t
442{
443 m_x->SetStream(stream);
444 m_y->SetStream(stream + 1);
445 m_z->SetStream(stream + 2);
446 return 3;
447}
448
450
451TypeId
453{
454 static TypeId tid =
455 TypeId("ns3::RandomDiscPositionAllocator")
457 .SetGroupName("Mobility")
458 .AddConstructor<RandomDiscPositionAllocator>()
459 .AddAttribute("Theta",
460 "A random variable which represents the angle (gradients) of a position "
461 "in a random disc.",
462 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=6.2830]"),
464 MakePointerChecker<RandomVariableStream>())
465 .AddAttribute(
466 "Rho",
467 "A random variable which represents the radius of a position in a random disc.",
468 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=200.0]"),
470 MakePointerChecker<RandomVariableStream>())
471 .AddAttribute("X",
472 "The x coordinate of the center of the random position disc.",
473 DoubleValue(0.0),
475 MakeDoubleChecker<double>())
476 .AddAttribute("Y",
477 "The y coordinate of the center of the random position disc.",
478 DoubleValue(0.0),
480 MakeDoubleChecker<double>())
481 .AddAttribute("Z",
482 "The z coordinate of all the positions in the disc.",
483 DoubleValue(0.0),
485 MakeDoubleChecker<double>());
486 return tid;
487}
488
490{
491}
492
494{
495}
496
497void
499{
500 m_theta = theta;
501}
502
503void
505{
506 m_rho = rho;
507}
508
509void
511{
512 m_x = x;
513}
514
515void
517{
518 m_y = y;
519}
520
521void
523{
524 m_z = z;
525}
526
527Vector
529{
530 double theta = m_theta->GetValue();
531 double rho = m_rho->GetValue();
532 double x = m_x + std::cos(theta) * rho;
533 double y = m_y + std::sin(theta) * rho;
534 NS_LOG_DEBUG("Disc position x=" << x << ", y=" << y);
535 return Vector(x, y, m_z);
536}
537
538int64_t
540{
541 m_theta->SetStream(stream);
542 m_rho->SetStream(stream + 1);
543 return 2;
544}
545
547
548TypeId
550{
551 static TypeId tid = TypeId("ns3::UniformDiscPositionAllocator")
553 .SetGroupName("Mobility")
554 .AddConstructor<UniformDiscPositionAllocator>()
555 .AddAttribute("rho",
556 "The radius of the disc",
557 DoubleValue(0.0),
559 MakeDoubleChecker<double>())
560 .AddAttribute("X",
561 "The x coordinate of the center of the disc.",
562 DoubleValue(0.0),
564 MakeDoubleChecker<double>())
565 .AddAttribute("Y",
566 "The y coordinate of the center of the disc.",
567 DoubleValue(0.0),
569 MakeDoubleChecker<double>())
570 .AddAttribute("Z",
571 "The z coordinate of all the positions in the disc.",
572 DoubleValue(0.0),
574 MakeDoubleChecker<double>());
575 return tid;
576}
577
579{
580 m_rv = CreateObject<UniformRandomVariable>();
581}
582
584{
585}
586
587void
589{
590 m_rho = rho;
591}
592
593void
595{
596 m_x = x;
597}
598
599void
601{
602 m_y = y;
603}
604
605void
607{
608 m_z = z;
609}
610
611Vector
613{
614 double x;
615 double y;
616 do
617 {
618 x = m_rv->GetValue(-m_rho, m_rho);
619 y = m_rv->GetValue(-m_rho, m_rho);
620 } while (std::sqrt(x * x + y * y) > m_rho);
621
622 x += m_x;
623 y += m_y;
624 NS_LOG_DEBUG("Disc position x=" << x << ", y=" << y);
625 return Vector(x, y, m_z);
626}
627
628int64_t
630{
631 m_rv->SetStream(stream);
632 return 1;
633}
634
635} // namespace ns3
Provides functions for parsing and extracting data from Comma Separated Value (CSV) formatted text fi...
Definition: csv-reader.h:232
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:411
std::size_t RowNumber() const
The number of lines that have been read.
Definition: csv-reader.cc:105
std::size_t ColumnCount() const
Returns the number of columns in the csv data.
Definition: csv-reader.cc:97
bool FetchNextRow()
Reads one line from the input until a new line is encountered.
Definition: csv-reader.cc:121
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Hold variables of type enum.
Definition: enum.h:62
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 GetLayoutType() const
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:89
Allocate a set of positions.
static TypeId GetTypeId()
Register this type with the TypeId system.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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:56
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
Hold an unsigned integer type.
Definition: uinteger.h:45
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
double GetValue(double min, double max)
Get the next random value drawn from the distribution.
#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:86
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:259
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#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:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
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:189