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 
90 uint32_t
92 {
93  return m_positions.size ();
94 }
95 
97 
98 TypeId
100 {
101  static TypeId tid = TypeId ("ns3::GridPositionAllocator")
103  .SetGroupName ("Mobility")
104  .AddConstructor<GridPositionAllocator> ()
105  .AddAttribute ("GridWidth", "The number of objects laid out on a line.",
106  UintegerValue (10),
108  MakeUintegerChecker<uint32_t> ())
109  .AddAttribute ("MinX", "The x coordinate where the grid starts.",
110  DoubleValue (1.0),
112  MakeDoubleChecker<double> ())
113  .AddAttribute ("MinY", "The y coordinate where the grid starts.",
114  DoubleValue (0.0),
116  MakeDoubleChecker<double> ())
117  .AddAttribute ("Z",
118  "The z coordinate of all the positions allocated.",
119  DoubleValue (0.0),
121  MakeDoubleChecker<double> ())
122  .AddAttribute ("DeltaX", "The x space between objects.",
123  DoubleValue (1.0),
125  MakeDoubleChecker<double> ())
126  .AddAttribute ("DeltaY", "The y space between objects.",
127  DoubleValue (1.0),
129  MakeDoubleChecker<double> ())
130  .AddAttribute ("LayoutType", "The type of layout.",
133  MakeEnumChecker (ROW_FIRST, "RowFirst",
134  COLUMN_FIRST, "ColumnFirst"))
135  ;
136  return tid;
137 }
139  : m_current (0)
140 {
141 }
142 
143 void
145 {
146  m_xMin = xMin;
147 }
148 void
150 {
151  m_yMin = yMin;
152 }
153 void
155 {
156  m_z = z;
157 }
158 void
160 {
161  m_deltaX = deltaX;
162 }
163 void
165 {
166  m_deltaY = deltaY;
167 }
168 void
170 {
171  m_n = n;
172 }
173 void
175 {
176  m_layoutType = layoutType;
177 }
178 
179 double
181 {
182  return m_xMin;
183 }
184 double
186 {
187  return m_yMin;
188 }
189 double
191 {
192  return m_deltaX;
193 }
194 double
196 {
197  return m_deltaY;
198 }
199 uint32_t
201 {
202  return m_n;
203 }
206 {
207  return m_layoutType;
208 }
209 
210 Vector
212 {
213  double x = 0.0, y = 0.0;
214  switch (m_layoutType) {
215  case ROW_FIRST:
216  x = m_xMin + m_deltaX * (m_current % m_n);
217  y = m_yMin + m_deltaY * (m_current / m_n);
218  break;
219  case COLUMN_FIRST:
220  x = m_xMin + m_deltaX * (m_current / m_n);
221  y = m_yMin + m_deltaY * (m_current % m_n);
222  break;
223  }
224  m_current++;
225  return Vector (x, y, m_z);
226 }
227 
228 int64_t
230 {
231  return 0;
232 }
233 
235 
236 TypeId
238 {
239  static TypeId tid = TypeId ("ns3::RandomRectanglePositionAllocator")
241  .SetGroupName ("Mobility")
242  .AddConstructor<RandomRectanglePositionAllocator> ()
243  .AddAttribute ("X",
244  "A random variable which represents the x coordinate of a position in a random rectangle.",
245  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
247  MakePointerChecker<RandomVariableStream> ())
248  .AddAttribute ("Y",
249  "A random variable which represents the y coordinate of a position in a random rectangle.",
250  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
252  MakePointerChecker<RandomVariableStream> ())
253  .AddAttribute ("Z",
254  "The z coordinate of all the positions allocated.",
255  DoubleValue (0.0),
257  MakeDoubleChecker<double> ())
258  ;
259  return tid;
260 }
261 
263 {
264 }
266 {
267 }
268 
269 void
271 {
272  m_x = x;
273 }
274 void
276 {
277  m_y = y;
278 }
279 void
281 {
282  m_z = z;
283 }
284 
285 Vector
287 {
288  double x = m_x->GetValue ();
289  double y = m_y->GetValue ();
290  return Vector (x, y, m_z);
291 }
292 
293 int64_t
295 {
296  m_x->SetStream (stream);
297  m_y->SetStream (stream + 1);
298  return 2;
299 }
300 
302 
303 TypeId
305 {
306  static TypeId tid = TypeId ("ns3::RandomBoxPositionAllocator")
308  .SetGroupName ("Mobility")
309  .AddConstructor<RandomBoxPositionAllocator> ()
310  .AddAttribute ("X",
311  "A random variable which represents the x coordinate of a position in a random box.",
312  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
314  MakePointerChecker<RandomVariableStream> ())
315  .AddAttribute ("Y",
316  "A random variable which represents the y coordinate of a position in a random box.",
317  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
319  MakePointerChecker<RandomVariableStream> ())
320  .AddAttribute ("Z",
321  "A random variable which represents the z coordinate of a position in a random box.",
322  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
324  MakePointerChecker<RandomVariableStream> ());
325  return tid;
326 }
327 
329 {
330 }
332 {
333 }
334 
335 void
337 {
338  m_x = x;
339 }
340 void
342 {
343  m_y = y;
344 }
345 void
347 {
348  m_z = z;
349 }
350 
351 Vector
353 {
354  double x = m_x->GetValue ();
355  double y = m_y->GetValue ();
356  double z = m_z->GetValue ();
357  return Vector (x, y, z);
358 }
359 
360 int64_t
362 {
363  m_x->SetStream (stream);
364  m_y->SetStream (stream + 1);
365  m_z->SetStream (stream + 2);
366  return 3;
367 }
368 
370 
371 TypeId
373 {
374  static TypeId tid = TypeId ("ns3::RandomDiscPositionAllocator")
376  .SetGroupName ("Mobility")
377  .AddConstructor<RandomDiscPositionAllocator> ()
378  .AddAttribute ("Theta",
379  "A random variable which represents the angle (gradients) of a position in a random disc.",
380  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=6.2830]"),
382  MakePointerChecker<RandomVariableStream> ())
383  .AddAttribute ("Rho",
384  "A random variable which represents the radius of a position in a random disc.",
385  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=200.0]"),
387  MakePointerChecker<RandomVariableStream> ())
388  .AddAttribute ("X",
389  "The x coordinate of the center of the random position disc.",
390  DoubleValue (0.0),
392  MakeDoubleChecker<double> ())
393  .AddAttribute ("Y",
394  "The y coordinate of the center of the random position disc.",
395  DoubleValue (0.0),
397  MakeDoubleChecker<double> ())
398  .AddAttribute ("Z",
399  "The z coordinate of all the positions in the disc.",
400  DoubleValue (0.0),
402  MakeDoubleChecker<double> ())
403  ;
404  return tid;
405 }
406 
408 {
409 }
411 {
412 }
413 
414 void
416 {
417  m_theta = theta;
418 }
419 void
421 {
422  m_rho = rho;
423 }
424 void
426 {
427  m_x = x;
428 }
429 void
431 {
432  m_y = y;
433 }
434 void
436 {
437  m_z = z;
438 }
439 Vector
441 {
442  double theta = m_theta->GetValue ();
443  double rho = m_rho->GetValue ();
444  double x = m_x + std::cos (theta) * rho;
445  double y = m_y + std::sin (theta) * rho;
446  NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y);
447  return Vector (x, y, m_z);
448 }
449 
450 int64_t
452 {
453  m_theta->SetStream (stream);
454  m_rho->SetStream (stream + 1);
455  return 2;
456 }
457 
458 
459 
461 
462 TypeId
464 {
465  static TypeId tid = TypeId ("ns3::UniformDiscPositionAllocator")
467  .SetGroupName ("Mobility")
468  .AddConstructor<UniformDiscPositionAllocator> ()
469  .AddAttribute ("rho",
470  "The radius of the disc",
471  DoubleValue (0.0),
473  MakeDoubleChecker<double> ())
474  .AddAttribute ("X",
475  "The x coordinate of the center of the disc.",
476  DoubleValue (0.0),
478  MakeDoubleChecker<double> ())
479  .AddAttribute ("Y",
480  "The y coordinate of the center of the disc.",
481  DoubleValue (0.0),
483  MakeDoubleChecker<double> ())
484  .AddAttribute ("Z",
485  "The z coordinate of all the positions in the disc.",
486  DoubleValue (0.0),
488  MakeDoubleChecker<double> ())
489  ;
490  return tid;
491 }
492 
494 {
495  m_rv = CreateObject<UniformRandomVariable> ();
496 }
498 {
499 }
500 
501 void
503 {
504  m_rho = rho;
505 }
506 void
508 {
509  m_x = x;
510 }
511 void
513 {
514  m_y = y;
515 }
516 void
518 {
519  m_z = z;
520 }
521 Vector
523 {
524  double x,y;
525  do
526  {
527  x = m_rv->GetValue (-m_rho, m_rho);
528  y = m_rv->GetValue (-m_rho, m_rho);
529  }
530  while (std::sqrt (x*x + y*y) > m_rho);
531 
532  x += m_x;
533  y += m_y;
534  NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y);
535  return Vector (x, y, m_z);
536 }
537 
538 int64_t
540 {
541  m_rv->SetStream (stream);
542  return 1;
543 }
544 
545 
546 } // namespace ns3
uint32_t m_current
currently position
In column-first mode, positions are allocated on the first column until N positions have been allocat...
enum LayoutType GetLayoutType(void) const
Ptr< RandomVariableStream > m_y
pointer to y&#39;s random variable stream
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
Ptr< RandomVariableStream > m_y
pointer to y&#39;s random variable stream
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
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&#39;s random variable stream
void SetZ(Ptr< RandomVariableStream > z)
Set the random variable stream object that generates z-positions.
double m_y
y coordinate of center of disc
double m_z
z coordinate of the disc
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:205
Ptr< RandomVariableStream > m_rho
pointer to rho&#39;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 radius, in meters.
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
double m_deltaY
y interval between two consecutive y positions
double m_xMin
minimum boundary on x positions
virtual Vector GetNext(void) const
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:227
Hold variables of type enum.
Definition: enum.h:54
uint32_t GetSize(void) const
Return the number of positions stored.
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&#39;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
virtual Vector GetNext(void) const
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:203
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 angle, in radians.
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.
double m_z
z coordinate of all the positions generated
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 .
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 uniformly (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.
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...
In row-first mode, positions are allocated on the first row until N positions have been allocated...
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
virtual Vector GetNext(void) const
void Add(Vector v)
Add a position to the list of positions.
virtual Vector GetNext(void) const
void SetLayoutType(enum LayoutType layoutType)
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:161
double m_z
z coordinate of the disc
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
double m_x
x coordinate of center of disc
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
double m_z
z coordinate of all the positions generated
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
virtual Vector GetNext(void) const
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:923
virtual int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
LayoutType
Determine whether positions are allocated row first or column first.
Ptr< RandomVariableStream > m_z
pointer to z&#39;s random variable stream
static TypeId GetTypeId(void)
Register this type with the TypeId system.
Ptr< RandomVariableStream > m_x
pointer to x&#39;s random variable stream
Allocate a set of positions.