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 <cmath>
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("PositionAllocator");
32 
33 NS_OBJECT_ENSURE_REGISTERED (PositionAllocator);
34 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::PositionAllocator")
39  .SetParent<Object> ();
40  return tid;
41 }
42 
44 {
45 }
46 
48 {
49 }
50 
52 
53 TypeId
55 {
56  static TypeId tid = TypeId ("ns3::ListPositionAllocator")
58  .AddConstructor<ListPositionAllocator> ()
59  ;
60  return tid;
61 }
63 {
64 }
65 void
67 {
68  m_positions.push_back (v);
69  m_current = m_positions.begin ();
70 }
71 Vector
73 {
74  Vector v = *m_current;
75  m_current++;
76  if (m_current == m_positions.end ())
77  {
78  m_current = m_positions.begin ();
79  }
80  return v;
81 }
82 int64_t
84 {
85  return 0;
86 }
87 
89 
90 TypeId
92 {
93  static TypeId tid = TypeId ("ns3::GridPositionAllocator")
95  .SetGroupName ("Mobility")
96  .AddConstructor<GridPositionAllocator> ()
97  .AddAttribute ("GridWidth", "The number of objects layed out on a line.",
98  UintegerValue (10),
100  MakeUintegerChecker<uint32_t> ())
101  .AddAttribute ("MinX", "The x coordinate where the grid starts.",
102  DoubleValue (1.0),
104  MakeDoubleChecker<double> ())
105  .AddAttribute ("MinY", "The y coordinate where the grid starts.",
106  DoubleValue (0.0),
108  MakeDoubleChecker<double> ())
109  .AddAttribute ("DeltaX", "The x space between objects.",
110  DoubleValue (1.0),
112  MakeDoubleChecker<double> ())
113  .AddAttribute ("DeltaY", "The y space between objects.",
114  DoubleValue (1.0),
116  MakeDoubleChecker<double> ())
117  .AddAttribute ("LayoutType", "The type of layout.",
120  MakeEnumChecker (ROW_FIRST, "RowFirst",
121  COLUMN_FIRST, "ColumnFirst"))
122  ;
123  return tid;
124 }
126  : m_current (0)
127 {
128 }
129 
130 void
132 {
133  m_xMin = xMin;
134 }
135 void
137 {
138  m_yMin = yMin;
139 }
140 void
142 {
143  m_deltaX = deltaX;
144 }
145 void
147 {
148  m_deltaY = deltaY;
149 }
150 void
152 {
153  m_n = n;
154 }
155 void
157 {
158  m_layoutType = layoutType;
159 }
160 
161 double
163 {
164  return m_xMin;
165 }
166 double
168 {
169  return m_yMin;
170 }
171 double
173 {
174  return m_deltaX;
175 }
176 double
178 {
179  return m_deltaY;
180 }
181 uint32_t
183 {
184  return m_n;
185 }
188 {
189  return m_layoutType;
190 }
191 
192 Vector
194 {
195  double x = 0.0, y = 0.0;
196  switch (m_layoutType) {
197  case ROW_FIRST:
198  x = m_xMin + m_deltaX * (m_current % m_n);
199  y = m_yMin + m_deltaY * (m_current / m_n);
200  break;
201  case COLUMN_FIRST:
202  x = m_xMin + m_deltaX * (m_current / m_n);
203  y = m_yMin + m_deltaY * (m_current % m_n);
204  break;
205  }
206  m_current++;
207  return Vector (x, y, 0.0);
208 }
209 
210 int64_t
212 {
213  return 0;
214 }
215 
217 
218 TypeId
220 {
221  static TypeId tid = TypeId ("ns3::RandomRectanglePositionAllocator")
223  .SetGroupName ("Mobility")
224  .AddConstructor<RandomRectanglePositionAllocator> ()
225  .AddAttribute ("X",
226  "A random variable which represents the x coordinate of a position in a random rectangle.",
227  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
229  MakePointerChecker<RandomVariableStream> ())
230  .AddAttribute ("Y",
231  "A random variable which represents the y coordinate of a position in a random rectangle.",
232  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
234  MakePointerChecker<RandomVariableStream> ());
235  return tid;
236 }
237 
239 {
240 }
242 {
243 }
244 
245 void
247 {
248  m_x = x;
249 }
250 void
252 {
253  m_y = y;
254 }
255 
256 Vector
258 {
259  double x = m_x->GetValue ();
260  double y = m_y->GetValue ();
261  return Vector (x, y, 0.0);
262 }
263 
264 int64_t
266 {
267  m_x->SetStream (stream);
268  m_y->SetStream (stream + 1);
269  return 2;
270 }
271 
273 
274 TypeId
276 {
277  static TypeId tid = TypeId ("ns3::RandomBoxPositionAllocator")
279  .SetGroupName ("Mobility")
280  .AddConstructor<RandomBoxPositionAllocator> ()
281  .AddAttribute ("X",
282  "A random variable which represents the x coordinate of a position in a random box.",
283  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
285  MakePointerChecker<RandomVariableStream> ())
286  .AddAttribute ("Y",
287  "A random variable which represents the y coordinate of a position in a random box.",
288  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
290  MakePointerChecker<RandomVariableStream> ())
291  .AddAttribute ("Z",
292  "A random variable which represents the z coordinate of a position in a random box.",
293  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
295  MakePointerChecker<RandomVariableStream> ());
296  return tid;
297 }
298 
300 {
301 }
303 {
304 }
305 
306 void
308 {
309  m_x = x;
310 }
311 void
313 {
314  m_y = y;
315 }
316 void
318 {
319  m_z = z;
320 }
321 
322 Vector
324 {
325  double x = m_x->GetValue ();
326  double y = m_y->GetValue ();
327  double z = m_z->GetValue ();
328  return Vector (x, y, z);
329 }
330 
331 int64_t
333 {
334  m_x->SetStream (stream);
335  m_y->SetStream (stream + 1);
336  m_z->SetStream (stream + 2);
337  return 3;
338 }
339 
341 
342 TypeId
344 {
345  static TypeId tid = TypeId ("ns3::RandomDiscPositionAllocator")
347  .SetGroupName ("Mobility")
348  .AddConstructor<RandomDiscPositionAllocator> ()
349  .AddAttribute ("Theta",
350  "A random variable which represents the angle (gradients) of a position in a random disc.",
351  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=6.2830]"),
353  MakePointerChecker<RandomVariableStream> ())
354  .AddAttribute ("Rho",
355  "A random variable which represents the radius of a position in a random disc.",
356  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=200.0]"),
358  MakePointerChecker<RandomVariableStream> ())
359  .AddAttribute ("X",
360  "The x coordinate of the center of the random position disc.",
361  DoubleValue (0.0),
363  MakeDoubleChecker<double> ())
364  .AddAttribute ("Y",
365  "The y coordinate of the center of the random position disc.",
366  DoubleValue (0.0),
368  MakeDoubleChecker<double> ())
369  ;
370  return tid;
371 }
372 
374 {
375 }
377 {
378 }
379 
380 void
382 {
383  m_theta = theta;
384 }
385 void
387 {
388  m_rho = rho;
389 }
390 void
392 {
393  m_x = x;
394 }
395 void
397 {
398  m_y = y;
399 }
400 Vector
402 {
403  double theta = m_theta->GetValue ();
404  double rho = m_rho->GetValue ();
405  double x = m_x + std::cos (theta) * rho;
406  double y = m_y + std::sin (theta) * rho;
407  NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y);
408  return Vector (x, y, 0.0);
409 }
410 
411 int64_t
413 {
414  m_theta->SetStream (stream);
415  m_rho->SetStream (stream + 1);
416  return 2;
417 }
418 
419 
420 
422 
423 TypeId
425 {
426  static TypeId tid = TypeId ("ns3::UniformDiscPositionAllocator")
428  .SetGroupName ("Mobility")
429  .AddConstructor<UniformDiscPositionAllocator> ()
430  .AddAttribute ("rho",
431  "The radius of the disc",
432  DoubleValue (0.0),
434  MakeDoubleChecker<double> ())
435  .AddAttribute ("X",
436  "The x coordinate of the center of the disc.",
437  DoubleValue (0.0),
439  MakeDoubleChecker<double> ())
440  .AddAttribute ("Y",
441  "The y coordinate of the center of the disc.",
442  DoubleValue (0.0),
444  MakeDoubleChecker<double> ())
445  ;
446  return tid;
447 }
448 
450 {
451  m_rv = CreateObject<UniformRandomVariable> ();
452 }
454 {
455 }
456 
457 void
459 {
460  m_rho = rho;
461 }
462 void
464 {
465  m_x = x;
466 }
467 void
469 {
470  m_y = y;
471 }
472 Vector
474 {
475  double x,y;
476  do
477  {
478  x = m_rv->GetValue (-m_rho, m_rho);
479  y = m_rv->GetValue (-m_rho, m_rho);
480  }
481  while (std::sqrt (x*x + y*y) > m_rho);
482 
483  x += m_x;
484  y += m_y;
485  NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y);
486  return Vector (x, y, 0.0);
487 }
488 
489 int64_t
491 {
492  m_rv->SetStream (stream);
493  return 1;
494 }
495 
496 
497 } // namespace ns3
uint32_t m_current
currently position
LayoutType
Determine whether positions are allocated row first or column first.
Ptr< RandomVariableStream > m_y
pointer to y's random variable stream
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
Ptr< RandomVariableStream > m_y
pointer to y's random variable stream
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
static TypeId GetTypeId(void)
Register this type with the TypeId system.
Hold variables of type string.
Definition: string.h:41
Ptr< RandomVariableStream > m_theta
pointer to theta's random variable stream
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:209
void SetZ(Ptr< RandomVariableStream > z)
Set the random variable stream object that generates z-positions.
double m_y
y coordinate of center of disc
enum LayoutType GetLayoutType(void) const
virtual Vector GetNext(void) const
std::vector< Vector > m_positions
vector of positions
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Ptr< RandomVariableStream > m_rho
pointer to rho's random variable stream
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...
double m_deltaX
x interval between two consecutive x positions
static TypeId GetTypeId(void)
Register this type with the TypeId system.
double m_rho
value of the radius of the disc
std::vector< Vector >::const_iterator m_current
vector iterator
Ptr< UniformRandomVariable > m_rv
pointer to uniform random variable
void SetRho(Ptr< RandomVariableStream > rho)
Set the random variable that generates position angle.
virtual Vector GetNext(void) const
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
In row-first mode, positions are allocated on the first row until N positions have been allocated...
double m_deltaY
y interval between two consecutive y positions
virtual Vector GetNext(void) const
double m_xMin
minimum boundary on x positions
static TypeId GetTypeId(void)
Register this type with the TypeId system.
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:220
Hold variables of type enum.
Definition: enum.h:54
double m_x
x coordinate of center of disc
uint32_t m_n
number of positions to allocate on each row or column
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.
Hold an unsigned integer type.
Definition: uinteger.h:44
Allocate positions on a rectangular 2d grid.
double m_yMin
minimum boundary on y positions
void SetTheta(Ptr< RandomVariableStream > theta)
Set the random variable that generates position radius.
Allocate positions from a deterministic list specified by the user.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetY(Ptr< RandomVariableStream > y)
Set the random variable stream object that generates y-positions.
virtual int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
virtual Vector GetNext(void) const
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...
Allocate the positions uniformely (with constant density) randomly within a disc. ...
enum LayoutType m_layoutType
currently selected layout type
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:42
static TypeId GetTypeId(void)
Register this type with the TypeId system.
Ptr< const AttributeChecker > MakeEnumChecker(int v1, std::string n1, int v2, std::string n2, int v3, std::string n3, int v4, std::string n4, int v5, std::string n5, int v6, std::string n6, int v7, std::string n7, int v8, std::string n8, int v9, std::string n9, int v10, std::string n10, int v11, std::string n11, int v12, std::string n12, int v13, std::string n13, int v14, std::string n14, int v15, std::string n15, int v16, std::string n16, int v17, std::string n17, int v18, std::string n18, int v19, std::string n19, int v20, std::string n20, int v21, std::string n21, int v22, std::string n22)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.cc:184
double m_y
y coordinate of center of disc
Allocate random positions within a rectangle according to a pair of random variables.
virtual int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
void Add(Vector v)
Add a position to the list of positions.
void SetLayoutType(enum LayoutType layoutType)
virtual Vector GetNext(void) const
uint32_t GetN(void) const
void SetX(Ptr< RandomVariableStream > x)
Set the random variable stream object that generates x-positions.
void SetX(Ptr< RandomVariableStream > x)
Set the random variable stream object that generates x-positions.
Allocate random positions within a 3D box according to a set of three random variables.
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:87
In column-first mode, positions are allocated on the first column until N positions have been allocat...
double m_x
x coordinate of center of disc
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
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:45
a unique identifier for an interface.
Definition: type-id.h:58
Allocate random positions within a disc according to a given distribution for the polar coordinates o...
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:826
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_z
pointer to z's random variable stream
static TypeId GetTypeId(void)
Register this type with the TypeId system.
Ptr< RandomVariableStream > m_x
pointer to x's random variable stream
Allocate a set of positions.