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  .SetGroupName ("Mobility");
41  return tid;
42 }
43 
45 {
46 }
47 
49 {
50 }
51 
53 
54 TypeId
56 {
57  static TypeId tid = TypeId ("ns3::ListPositionAllocator")
59  .SetGroupName ("Mobility")
60  .AddConstructor<ListPositionAllocator> ()
61  ;
62  return tid;
63 }
65 {
66 }
67 void
69 {
70  m_positions.push_back (v);
71  m_current = m_positions.begin ();
72 }
73 Vector
75 {
76  Vector v = *m_current;
77  m_current++;
78  if (m_current == m_positions.end ())
79  {
80  m_current = m_positions.begin ();
81  }
82  return v;
83 }
84 int64_t
86 {
87  return 0;
88 }
89 
91 
92 TypeId
94 {
95  static TypeId tid = TypeId ("ns3::GridPositionAllocator")
97  .SetGroupName ("Mobility")
98  .AddConstructor<GridPositionAllocator> ()
99  .AddAttribute ("GridWidth", "The number of objects layed out on a line.",
100  UintegerValue (10),
102  MakeUintegerChecker<uint32_t> ())
103  .AddAttribute ("MinX", "The x coordinate where the grid starts.",
104  DoubleValue (1.0),
106  MakeDoubleChecker<double> ())
107  .AddAttribute ("MinY", "The y coordinate where the grid starts.",
108  DoubleValue (0.0),
110  MakeDoubleChecker<double> ())
111  .AddAttribute ("DeltaX", "The x space between objects.",
112  DoubleValue (1.0),
114  MakeDoubleChecker<double> ())
115  .AddAttribute ("DeltaY", "The y space between objects.",
116  DoubleValue (1.0),
118  MakeDoubleChecker<double> ())
119  .AddAttribute ("LayoutType", "The type of layout.",
122  MakeEnumChecker (ROW_FIRST, "RowFirst",
123  COLUMN_FIRST, "ColumnFirst"))
124  ;
125  return tid;
126 }
128  : m_current (0)
129 {
130 }
131 
132 void
134 {
135  m_xMin = xMin;
136 }
137 void
139 {
140  m_yMin = yMin;
141 }
142 void
144 {
145  m_deltaX = deltaX;
146 }
147 void
149 {
150  m_deltaY = deltaY;
151 }
152 void
154 {
155  m_n = n;
156 }
157 void
159 {
160  m_layoutType = layoutType;
161 }
162 
163 double
165 {
166  return m_xMin;
167 }
168 double
170 {
171  return m_yMin;
172 }
173 double
175 {
176  return m_deltaX;
177 }
178 double
180 {
181  return m_deltaY;
182 }
183 uint32_t
185 {
186  return m_n;
187 }
190 {
191  return m_layoutType;
192 }
193 
194 Vector
196 {
197  double x = 0.0, y = 0.0;
198  switch (m_layoutType) {
199  case ROW_FIRST:
200  x = m_xMin + m_deltaX * (m_current % m_n);
201  y = m_yMin + m_deltaY * (m_current / m_n);
202  break;
203  case COLUMN_FIRST:
204  x = m_xMin + m_deltaX * (m_current / m_n);
205  y = m_yMin + m_deltaY * (m_current % m_n);
206  break;
207  }
208  m_current++;
209  return Vector (x, y, 0.0);
210 }
211 
212 int64_t
214 {
215  return 0;
216 }
217 
219 
220 TypeId
222 {
223  static TypeId tid = TypeId ("ns3::RandomRectanglePositionAllocator")
225  .SetGroupName ("Mobility")
226  .AddConstructor<RandomRectanglePositionAllocator> ()
227  .AddAttribute ("X",
228  "A random variable which represents the x coordinate of a position in a random rectangle.",
229  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
231  MakePointerChecker<RandomVariableStream> ())
232  .AddAttribute ("Y",
233  "A random variable which represents the y coordinate of a position in a random rectangle.",
234  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
236  MakePointerChecker<RandomVariableStream> ());
237  return tid;
238 }
239 
241 {
242 }
244 {
245 }
246 
247 void
249 {
250  m_x = x;
251 }
252 void
254 {
255  m_y = y;
256 }
257 
258 Vector
260 {
261  double x = m_x->GetValue ();
262  double y = m_y->GetValue ();
263  return Vector (x, y, 0.0);
264 }
265 
266 int64_t
268 {
269  m_x->SetStream (stream);
270  m_y->SetStream (stream + 1);
271  return 2;
272 }
273 
275 
276 TypeId
278 {
279  static TypeId tid = TypeId ("ns3::RandomBoxPositionAllocator")
281  .SetGroupName ("Mobility")
282  .AddConstructor<RandomBoxPositionAllocator> ()
283  .AddAttribute ("X",
284  "A random variable which represents the x coordinate of a position in a random box.",
285  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
287  MakePointerChecker<RandomVariableStream> ())
288  .AddAttribute ("Y",
289  "A random variable which represents the y coordinate of a position in a random box.",
290  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
292  MakePointerChecker<RandomVariableStream> ())
293  .AddAttribute ("Z",
294  "A random variable which represents the z coordinate of a position in a random box.",
295  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
297  MakePointerChecker<RandomVariableStream> ());
298  return tid;
299 }
300 
302 {
303 }
305 {
306 }
307 
308 void
310 {
311  m_x = x;
312 }
313 void
315 {
316  m_y = y;
317 }
318 void
320 {
321  m_z = z;
322 }
323 
324 Vector
326 {
327  double x = m_x->GetValue ();
328  double y = m_y->GetValue ();
329  double z = m_z->GetValue ();
330  return Vector (x, y, z);
331 }
332 
333 int64_t
335 {
336  m_x->SetStream (stream);
337  m_y->SetStream (stream + 1);
338  m_z->SetStream (stream + 2);
339  return 3;
340 }
341 
343 
344 TypeId
346 {
347  static TypeId tid = TypeId ("ns3::RandomDiscPositionAllocator")
349  .SetGroupName ("Mobility")
350  .AddConstructor<RandomDiscPositionAllocator> ()
351  .AddAttribute ("Theta",
352  "A random variable which represents the angle (gradients) of a position in a random disc.",
353  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=6.2830]"),
355  MakePointerChecker<RandomVariableStream> ())
356  .AddAttribute ("Rho",
357  "A random variable which represents the radius of a position in a random disc.",
358  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=200.0]"),
360  MakePointerChecker<RandomVariableStream> ())
361  .AddAttribute ("X",
362  "The x coordinate of the center of the random position disc.",
363  DoubleValue (0.0),
365  MakeDoubleChecker<double> ())
366  .AddAttribute ("Y",
367  "The y coordinate of the center of the random position disc.",
368  DoubleValue (0.0),
370  MakeDoubleChecker<double> ())
371  ;
372  return tid;
373 }
374 
376 {
377 }
379 {
380 }
381 
382 void
384 {
385  m_theta = theta;
386 }
387 void
389 {
390  m_rho = rho;
391 }
392 void
394 {
395  m_x = x;
396 }
397 void
399 {
400  m_y = y;
401 }
402 Vector
404 {
405  double theta = m_theta->GetValue ();
406  double rho = m_rho->GetValue ();
407  double x = m_x + std::cos (theta) * rho;
408  double y = m_y + std::sin (theta) * rho;
409  NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y);
410  return Vector (x, y, 0.0);
411 }
412 
413 int64_t
415 {
416  m_theta->SetStream (stream);
417  m_rho->SetStream (stream + 1);
418  return 2;
419 }
420 
421 
422 
424 
425 TypeId
427 {
428  static TypeId tid = TypeId ("ns3::UniformDiscPositionAllocator")
430  .SetGroupName ("Mobility")
431  .AddConstructor<UniformDiscPositionAllocator> ()
432  .AddAttribute ("rho",
433  "The radius of the disc",
434  DoubleValue (0.0),
436  MakeDoubleChecker<double> ())
437  .AddAttribute ("X",
438  "The x coordinate of the center of the disc.",
439  DoubleValue (0.0),
441  MakeDoubleChecker<double> ())
442  .AddAttribute ("Y",
443  "The y coordinate of the center of the disc.",
444  DoubleValue (0.0),
446  MakeDoubleChecker<double> ())
447  ;
448  return tid;
449 }
450 
452 {
453  m_rv = CreateObject<UniformRandomVariable> ();
454 }
456 {
457 }
458 
459 void
461 {
462  m_rho = rho;
463 }
464 void
466 {
467  m_x = x;
468 }
469 void
471 {
472  m_y = y;
473 }
474 Vector
476 {
477  double x,y;
478  do
479  {
480  x = m_rv->GetValue (-m_rho, m_rho);
481  y = m_rv->GetValue (-m_rho, m_rho);
482  }
483  while (std::sqrt (x*x + y*y) > m_rho);
484 
485  x += m_x;
486  y += m_y;
487  NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y);
488  return Vector (x, y, 0.0);
489 }
490 
491 int64_t
493 {
494  m_rv->SetStream (stream);
495  return 1;
496 }
497 
498 
499 } // 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:904
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.