A Discrete-Event Network Simulator
API
position-allocator.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2007 INRIA
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19 */
20#include "position-allocator.h"
21#include "ns3/double.h"
22#include "ns3/string.h"
23#include "ns3/pointer.h"
24#include "ns3/uinteger.h"
25#include "ns3/enum.h"
26#include "ns3/log.h"
27#include "ns3/csv-reader.h"
28
29#include <cmath>
30
31namespace ns3 {
32
33NS_LOG_COMPONENT_DEFINE ("PositionAllocator");
34
35NS_OBJECT_ENSURE_REGISTERED (PositionAllocator);
36
37TypeId
39{
40 static TypeId tid = TypeId ("ns3::PositionAllocator")
41 .SetParent<Object> ()
42 .SetGroupName ("Mobility");
43 return tid;
44}
45
47{}
48
50{}
51
52
54
57{
58 static TypeId tid = TypeId ("ns3::ListPositionAllocator")
60 .SetGroupName ("Mobility")
61 .AddConstructor<ListPositionAllocator> ()
62 ;
63 return tid;
64}
65
67{}
68
69void
71{
72 m_positions.push_back (v);
73 m_current = m_positions.begin ();
74}
75
76void
77ListPositionAllocator::Add (const std::string filePath,
78 double defaultZ /* = 0 */,
79 char delimiter /* = ',' */)
80{
81 NS_LOG_FUNCTION (this << filePath << std::string ("'") + delimiter + "'");
82
83 CsvReader csv (filePath, delimiter);
84 while (csv.FetchNextRow ())
85 {
86 if (csv.ColumnCount () == 1)
87 {
88 // comment line
89 continue;
90 }
91
92 double x, y, z;
93 bool ok = csv.GetValue (0, x);
94 NS_LOG_INFO ("read x: " << x << (ok ? " ok" : " FAIL"));
95 NS_ASSERT_MSG (ok, "failed reading x");
96 ok = csv.GetValue (1, y);
97 NS_LOG_INFO ("read y = " << y << (ok ? " ok" : " FAIL"));
98 NS_ASSERT_MSG (ok, "failed reading y");
99 if (csv.ColumnCount () > 2)
100 {
101 ok = csv.GetValue (2, z);
102 NS_LOG_INFO ("read z = " << z << (ok ? " ok" : " FAIL"));
103 NS_ASSERT_MSG (ok, "failed reading z");
104 }
105 else
106 {
107 z = defaultZ;
108 NS_LOG_LOGIC ("using default Z " << defaultZ);
109 }
110
111 Vector pos (x, y, z);
112 Add (pos);
113
114 } // while FetchNextRow
115 NS_LOG_INFO ("read " << csv.RowNumber () << " rows");
116}
117
118Vector
120{
121 Vector v = *m_current;
122 m_current++;
123 if (m_current == m_positions.end ())
124 {
125 m_current = m_positions.begin ();
126 }
127 return v;
128}
129
130int64_t
132{
133 return 0;
134}
135
138{
139 return m_positions.size ();
140}
141
142
144
145TypeId
147{
148 static TypeId tid = TypeId ("ns3::GridPositionAllocator")
150 .SetGroupName ("Mobility")
151 .AddConstructor<GridPositionAllocator> ()
152 .AddAttribute ("GridWidth", "The number of objects laid out on a line.",
153 UintegerValue (10),
155 MakeUintegerChecker<uint32_t> ())
156 .AddAttribute ("MinX", "The x coordinate where the grid starts.",
157 DoubleValue (1.0),
159 MakeDoubleChecker<double> ())
160 .AddAttribute ("MinY", "The y coordinate where the grid starts.",
161 DoubleValue (0.0),
163 MakeDoubleChecker<double> ())
164 .AddAttribute ("Z",
165 "The z coordinate of all the positions allocated.",
166 DoubleValue (0.0),
168 MakeDoubleChecker<double> ())
169 .AddAttribute ("DeltaX", "The x space between objects.",
170 DoubleValue (1.0),
172 MakeDoubleChecker<double> ())
173 .AddAttribute ("DeltaY", "The y space between objects.",
174 DoubleValue (1.0),
176 MakeDoubleChecker<double> ())
177 .AddAttribute ("LayoutType", "The type of layout.",
180 MakeEnumChecker (ROW_FIRST, "RowFirst",
181 COLUMN_FIRST, "ColumnFirst"))
182 ;
183 return tid;
184}
185
187 : m_current (0)
188{}
189
190void
192{
193 m_xMin = xMin;
194}
195
196void
198{
199 m_yMin = yMin;
200}
201
202void
204{
205 m_z = z;
206}
207
208void
210{
211 m_deltaX = deltaX;
212}
213
214void
216{
217 m_deltaY = deltaY;
218}
219
220void
222{
223 m_n = n;
224}
225
226void
228{
229 m_layoutType = layoutType;
230}
231
232double
234{
235 return m_xMin;
236}
237
238double
240{
241 return m_yMin;
242}
243
244double
246{
247 return m_deltaX;
248}
249
250double
252{
253 return m_deltaY;
254}
255
258{
259 return m_n;
260}
261
264{
265 return m_layoutType;
266}
267
268Vector
270{
271 double x = 0.0, y = 0.0;
272 switch (m_layoutType)
273 {
274 case ROW_FIRST:
275 x = m_xMin + m_deltaX * (m_current % m_n);
276 y = m_yMin + m_deltaY * (m_current / m_n);
277 break;
278 case COLUMN_FIRST:
279 x = m_xMin + m_deltaX * (m_current / m_n);
280 y = m_yMin + m_deltaY * (m_current % m_n);
281 break;
282 }
283 m_current++;
284 return Vector (x, y, m_z);
285}
286
287int64_t
289{
290 return 0;
291}
292
293
295
296TypeId
298{
299 static TypeId tid = TypeId ("ns3::RandomRectanglePositionAllocator")
301 .SetGroupName ("Mobility")
302 .AddConstructor<RandomRectanglePositionAllocator> ()
303 .AddAttribute ("X",
304 "A random variable which represents the x coordinate of a position in a random rectangle.",
305 StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
307 MakePointerChecker<RandomVariableStream> ())
308 .AddAttribute ("Y",
309 "A random variable which represents the y coordinate of a position in a random rectangle.",
310 StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
312 MakePointerChecker<RandomVariableStream> ())
313 .AddAttribute ("Z",
314 "The z coordinate of all the positions allocated.",
315 DoubleValue (0.0),
317 MakeDoubleChecker<double> ())
318 ;
319 return tid;
320}
321
323{}
324
326{}
327
328void
330{
331 m_x = x;
332}
333
334void
336{
337 m_y = y;
338}
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
362
364
365TypeId
367{
368 static TypeId tid = 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 random box.",
374 StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
376 MakePointerChecker<RandomVariableStream> ())
377 .AddAttribute ("Y",
378 "A random variable which represents the y coordinate of a position in a random box.",
379 StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
381 MakePointerChecker<RandomVariableStream> ())
382 .AddAttribute ("Z",
383 "A random variable which represents the z coordinate of a position in a random box.",
384 StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
386 MakePointerChecker<RandomVariableStream> ())
387 ;
388 return tid;
389}
390
392{}
393
395{}
396
397void
399{
400 m_x = x;
401}
402
403void
405{
406 m_y = y;
407}
408
409void
411{
412 m_z = z;
413}
414
415Vector
417{
418 double x = m_x->GetValue ();
419 double y = m_y->GetValue ();
420 double z = m_z->GetValue ();
421 return Vector (x, y, z);
422}
423
424int64_t
426{
427 m_x->SetStream (stream);
428 m_y->SetStream (stream + 1);
429 m_z->SetStream (stream + 2);
430 return 3;
431}
432
433
435
436TypeId
438{
439 static TypeId tid = TypeId ("ns3::RandomDiscPositionAllocator")
441 .SetGroupName ("Mobility")
442 .AddConstructor<RandomDiscPositionAllocator> ()
443 .AddAttribute ("Theta",
444 "A random variable which represents the angle (gradients) of a position in a random disc.",
445 StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=6.2830]"),
447 MakePointerChecker<RandomVariableStream> ())
448 .AddAttribute ("Rho",
449 "A random variable which represents the radius of a position in a random disc.",
450 StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=200.0]"),
452 MakePointerChecker<RandomVariableStream> ())
453 .AddAttribute ("X",
454 "The x coordinate of the center of the random position disc.",
455 DoubleValue (0.0),
457 MakeDoubleChecker<double> ())
458 .AddAttribute ("Y",
459 "The y coordinate of the center of the random position disc.",
460 DoubleValue (0.0),
462 MakeDoubleChecker<double> ())
463 .AddAttribute ("Z",
464 "The z coordinate of all the positions in the disc.",
465 DoubleValue (0.0),
467 MakeDoubleChecker<double> ())
468 ;
469 return tid;
470}
471
473{}
474
476{}
477
478void
480{
481 m_theta = theta;
482}
483
484void
486{
487 m_rho = rho;
488}
489
490void
492{
493 m_x = x;
494}
495
496void
498{
499 m_y = y;
500}
501
502void
504{
505 m_z = z;
506}
507
508Vector
510{
511 double theta = m_theta->GetValue ();
512 double rho = m_rho->GetValue ();
513 double x = m_x + std::cos (theta) * rho;
514 double y = m_y + std::sin (theta) * rho;
515 NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y);
516 return Vector (x, y, m_z);
517}
518
519int64_t
521{
522 m_theta->SetStream (stream);
523 m_rho->SetStream (stream + 1);
524 return 2;
525}
526
527
529
530TypeId
532{
533 static TypeId tid = TypeId ("ns3::UniformDiscPositionAllocator")
535 .SetGroupName ("Mobility")
536 .AddConstructor<UniformDiscPositionAllocator> ()
537 .AddAttribute ("rho",
538 "The radius of the disc",
539 DoubleValue (0.0),
541 MakeDoubleChecker<double> ())
542 .AddAttribute ("X",
543 "The x coordinate of the center of the disc.",
544 DoubleValue (0.0),
546 MakeDoubleChecker<double> ())
547 .AddAttribute ("Y",
548 "The y coordinate of the center of the disc.",
549 DoubleValue (0.0),
551 MakeDoubleChecker<double> ())
552 .AddAttribute ("Z",
553 "The z coordinate of all the positions in the disc.",
554 DoubleValue (0.0),
556 MakeDoubleChecker<double> ())
557 ;
558 return tid;
559}
560
562{
563 m_rv = CreateObject<UniformRandomVariable> ();
564}
565
567{}
568
569void
571{
572 m_rho = rho;
573}
574
575void
577{
578 m_x = x;
579}
580
581void
583{
584 m_y = y;
585}
586
587void
589{
590 m_z = z;
591}
592
593Vector
595{
596 double x,y;
597 do
598 {
599 x = m_rv->GetValue (-m_rho, m_rho);
600 y = m_rv->GetValue (-m_rho, m_rho);
601 }
602 while (std::sqrt (x * x + y * y) > m_rho);
603
604 x += m_x;
605 y += m_y;
606 NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y);
607 return Vector (x, y, m_z);
608}
609
610int64_t
612{
613 m_rv->SetStream (stream);
614 return 1;
615}
616
617
618} // namespace ns3
Provides functions for parsing and extracting data from Comma Separated Value (CSV) formatted text fi...
Definition: csv-reader.h:236
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:415
std::size_t RowNumber() const
The number of lines that have been read.
Definition: csv-reader.cc:103
std::size_t ColumnCount() const
Returns the number of columns in the csv data.
Definition: csv-reader.cc:95
bool FetchNextRow()
Reads one line from the input until a new line is encountered.
Definition: csv-reader.cc:119
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
Hold variables of type enum.
Definition: enum.h:55
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.
virtual Vector GetNext(void) const
virtual int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
uint32_t m_current
currently position
enum LayoutType m_layoutType
currently selected layout type
double m_yMin
minimum boundary on y positions
double m_z
z coordinate of all the positions generated
void SetLayoutType(enum LayoutType layoutType)
static TypeId GetTypeId(void)
Register this type with the TypeId system.
double m_xMin
minimum boundary on x positions
uint32_t m_n
number of positions to allocate on each row or column
enum LayoutType GetLayoutType(void) const
Allocate positions from a deterministic list specified by the user.
void Add(Vector v)
Add a position to the list of positions.
std::vector< Vector >::const_iterator m_current
vector iterator
std::vector< Vector > m_positions
vector of positions
static TypeId GetTypeId(void)
Register this type with the TypeId system.
uint32_t GetSize(void) const
Return the number of positions stored.
virtual Vector GetNext(void) const
virtual int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
A base class which provides memory management and object aggregation.
Definition: object.h:88
Allocate a set of positions.
static TypeId GetTypeId(void)
Register this type with the TypeId system.
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.
virtual int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Ptr< RandomVariableStream > m_x
pointer to x's random variable stream
Ptr< RandomVariableStream > m_y
pointer to y's random variable stream
virtual Vector GetNext(void) const
void SetX(Ptr< RandomVariableStream > x)
Set the random variable stream object that generates x-positions.
static TypeId GetTypeId(void)
Register this type with the TypeId system.
void SetY(Ptr< RandomVariableStream > y)
Set the random variable stream object that generates y-positions.
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
void SetRho(Ptr< RandomVariableStream > rho)
Set the random variable that generates position radius, in meters.
static TypeId GetTypeId(void)
Register this type with the TypeId system.
double m_x
x coordinate of center of disc
void SetTheta(Ptr< RandomVariableStream > theta)
Set the random variable that generates position angle, in radians.
virtual Vector GetNext(void) const
Ptr< RandomVariableStream > m_theta
pointer to theta's random variable stream
double m_z
z coordinate of the disc
virtual int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Allocate random positions within a rectangle according to a pair of random variables.
static TypeId GetTypeId(void)
Register this type with the TypeId system.
virtual int64_t AssignStreams(int64_t stream)
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.
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(void)=0
Get the next random value as a double drawn from the distribution.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
Hold variables of type string.
Definition: string.h:41
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Hold an unsigned integer type.
Definition: uinteger.h:44
Allocate the positions uniformly (with constant density) randomly within a disc.
virtual Vector GetNext(void) const
virtual int64_t AssignStreams(int64_t stream)
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
static TypeId GetTypeId(void)
Register this type with the TypeId system.
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
#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:88
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:42
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Definition: enum.h:205
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:227
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#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:281
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeEnumChecker(int v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.h:162
list x
Random number samples.