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 ("DeltaX", "The x space between objects.",
118  DoubleValue (1.0),
120  MakeDoubleChecker<double> ())
121  .AddAttribute ("DeltaY", "The y space between objects.",
122  DoubleValue (1.0),
124  MakeDoubleChecker<double> ())
125  .AddAttribute ("LayoutType", "The type of layout.",
128  MakeEnumChecker (ROW_FIRST, "RowFirst",
129  COLUMN_FIRST, "ColumnFirst"))
130  ;
131  return tid;
132 }
134  : m_current (0)
135 {
136 }
137 
138 void
140 {
141  m_xMin = xMin;
142 }
143 void
145 {
146  m_yMin = yMin;
147 }
148 void
150 {
151  m_deltaX = deltaX;
152 }
153 void
155 {
156  m_deltaY = deltaY;
157 }
158 void
160 {
161  m_n = n;
162 }
163 void
165 {
166  m_layoutType = layoutType;
167 }
168 
169 double
171 {
172  return m_xMin;
173 }
174 double
176 {
177  return m_yMin;
178 }
179 double
181 {
182  return m_deltaX;
183 }
184 double
186 {
187  return m_deltaY;
188 }
189 uint32_t
191 {
192  return m_n;
193 }
196 {
197  return m_layoutType;
198 }
199 
200 Vector
202 {
203  double x = 0.0, y = 0.0;
204  switch (m_layoutType) {
205  case ROW_FIRST:
206  x = m_xMin + m_deltaX * (m_current % m_n);
207  y = m_yMin + m_deltaY * (m_current / m_n);
208  break;
209  case COLUMN_FIRST:
210  x = m_xMin + m_deltaX * (m_current / m_n);
211  y = m_yMin + m_deltaY * (m_current % m_n);
212  break;
213  }
214  m_current++;
215  return Vector (x, y, 0.0);
216 }
217 
218 int64_t
220 {
221  return 0;
222 }
223 
225 
226 TypeId
228 {
229  static TypeId tid = TypeId ("ns3::RandomRectanglePositionAllocator")
231  .SetGroupName ("Mobility")
232  .AddConstructor<RandomRectanglePositionAllocator> ()
233  .AddAttribute ("X",
234  "A random variable which represents the x coordinate of a position in a random rectangle.",
235  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
237  MakePointerChecker<RandomVariableStream> ())
238  .AddAttribute ("Y",
239  "A random variable which represents the y coordinate of a position in a random rectangle.",
240  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
242  MakePointerChecker<RandomVariableStream> ());
243  return tid;
244 }
245 
247 {
248 }
250 {
251 }
252 
253 void
255 {
256  m_x = x;
257 }
258 void
260 {
261  m_y = y;
262 }
263 
264 Vector
266 {
267  double x = m_x->GetValue ();
268  double y = m_y->GetValue ();
269  return Vector (x, y, 0.0);
270 }
271 
272 int64_t
274 {
275  m_x->SetStream (stream);
276  m_y->SetStream (stream + 1);
277  return 2;
278 }
279 
281 
282 TypeId
284 {
285  static TypeId tid = TypeId ("ns3::RandomBoxPositionAllocator")
287  .SetGroupName ("Mobility")
288  .AddConstructor<RandomBoxPositionAllocator> ()
289  .AddAttribute ("X",
290  "A random variable which represents the x coordinate of a position in a random box.",
291  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
293  MakePointerChecker<RandomVariableStream> ())
294  .AddAttribute ("Y",
295  "A random variable which represents the y coordinate of a position in a random box.",
296  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
298  MakePointerChecker<RandomVariableStream> ())
299  .AddAttribute ("Z",
300  "A random variable which represents the z coordinate of a position in a random box.",
301  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
303  MakePointerChecker<RandomVariableStream> ());
304  return tid;
305 }
306 
308 {
309 }
311 {
312 }
313 
314 void
316 {
317  m_x = x;
318 }
319 void
321 {
322  m_y = y;
323 }
324 void
326 {
327  m_z = z;
328 }
329 
330 Vector
332 {
333  double x = m_x->GetValue ();
334  double y = m_y->GetValue ();
335  double z = m_z->GetValue ();
336  return Vector (x, y, z);
337 }
338 
339 int64_t
341 {
342  m_x->SetStream (stream);
343  m_y->SetStream (stream + 1);
344  m_z->SetStream (stream + 2);
345  return 3;
346 }
347 
349 
350 TypeId
352 {
353  static TypeId tid = TypeId ("ns3::RandomDiscPositionAllocator")
355  .SetGroupName ("Mobility")
356  .AddConstructor<RandomDiscPositionAllocator> ()
357  .AddAttribute ("Theta",
358  "A random variable which represents the angle (gradients) of a position in a random disc.",
359  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=6.2830]"),
361  MakePointerChecker<RandomVariableStream> ())
362  .AddAttribute ("Rho",
363  "A random variable which represents the radius of a position in a random disc.",
364  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=200.0]"),
366  MakePointerChecker<RandomVariableStream> ())
367  .AddAttribute ("X",
368  "The x coordinate of the center of the random position disc.",
369  DoubleValue (0.0),
371  MakeDoubleChecker<double> ())
372  .AddAttribute ("Y",
373  "The y coordinate of the center of the random position disc.",
374  DoubleValue (0.0),
376  MakeDoubleChecker<double> ())
377  ;
378  return tid;
379 }
380 
382 {
383 }
385 {
386 }
387 
388 void
390 {
391  m_theta = theta;
392 }
393 void
395 {
396  m_rho = rho;
397 }
398 void
400 {
401  m_x = x;
402 }
403 void
405 {
406  m_y = y;
407 }
408 Vector
410 {
411  double theta = m_theta->GetValue ();
412  double rho = m_rho->GetValue ();
413  double x = m_x + std::cos (theta) * rho;
414  double y = m_y + std::sin (theta) * rho;
415  NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y);
416  return Vector (x, y, 0.0);
417 }
418 
419 int64_t
421 {
422  m_theta->SetStream (stream);
423  m_rho->SetStream (stream + 1);
424  return 2;
425 }
426 
427 
428 
430 
431 TypeId
433 {
434  static TypeId tid = TypeId ("ns3::UniformDiscPositionAllocator")
436  .SetGroupName ("Mobility")
437  .AddConstructor<UniformDiscPositionAllocator> ()
438  .AddAttribute ("rho",
439  "The radius of the disc",
440  DoubleValue (0.0),
442  MakeDoubleChecker<double> ())
443  .AddAttribute ("X",
444  "The x coordinate of the center of the disc.",
445  DoubleValue (0.0),
447  MakeDoubleChecker<double> ())
448  .AddAttribute ("Y",
449  "The y coordinate of the center of the disc.",
450  DoubleValue (0.0),
452  MakeDoubleChecker<double> ())
453  ;
454  return tid;
455 }
456 
458 {
459  m_rv = CreateObject<UniformRandomVariable> ();
460 }
462 {
463 }
464 
465 void
467 {
468  m_rho = rho;
469 }
470 void
472 {
473  m_x = x;
474 }
475 void
477 {
478  m_y = y;
479 }
480 Vector
482 {
483  double x,y;
484  do
485  {
486  x = m_rv->GetValue (-m_rho, m_rho);
487  y = m_rv->GetValue (-m_rho, m_rho);
488  }
489  while (std::sqrt (x*x + y*y) > m_rho);
490 
491  x += m_x;
492  y += m_y;
493  NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y);
494  return Vector (x, y, 0.0);
495 }
496 
497 int64_t
499 {
500  m_rv->SetStream (stream);
501  return 1;
502 }
503 
504 
505 } // 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
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
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:202
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 angle.
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:220
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
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 .
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...
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:270
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)
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
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:915
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.