A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("PositionAllocator");
30 
31 namespace ns3 {
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),
99  MakeUintegerAccessor (&GridPositionAllocator::m_n),
100  MakeUintegerChecker<uint32_t> ())
101  .AddAttribute ("MinX", "The x coordinate where the grid starts.",
102  DoubleValue (1.0),
103  MakeDoubleAccessor (&GridPositionAllocator::m_xMin),
104  MakeDoubleChecker<double> ())
105  .AddAttribute ("MinY", "The y coordinate where the grid starts.",
106  DoubleValue (0.0),
107  MakeDoubleAccessor (&GridPositionAllocator::m_yMin),
108  MakeDoubleChecker<double> ())
109  .AddAttribute ("DeltaX", "The x space between objects.",
110  DoubleValue (1.0),
111  MakeDoubleAccessor (&GridPositionAllocator::m_deltaX),
112  MakeDoubleChecker<double> ())
113  .AddAttribute ("DeltaY", "The y space between objects.",
114  DoubleValue (1.0),
115  MakeDoubleAccessor (&GridPositionAllocator::m_deltaY),
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]"),
228  MakePointerAccessor (&RandomRectanglePositionAllocator::m_x),
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]"),
233  MakePointerAccessor (&RandomRectanglePositionAllocator::m_y),
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]"),
284  MakePointerAccessor (&RandomBoxPositionAllocator::m_x),
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]"),
289  MakePointerAccessor (&RandomBoxPositionAllocator::m_y),
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]"),
294  MakePointerAccessor (&RandomBoxPositionAllocator::m_z),
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]"),
352  MakePointerAccessor (&RandomDiscPositionAllocator::m_theta),
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]"),
357  MakePointerAccessor (&RandomDiscPositionAllocator::m_rho),
358  MakePointerChecker<RandomVariableStream> ())
359  .AddAttribute ("X",
360  "The x coordinate of the center of the random position disc.",
361  DoubleValue (0.0),
362  MakeDoubleAccessor (&RandomDiscPositionAllocator::m_x),
363  MakeDoubleChecker<double> ())
364  .AddAttribute ("Y",
365  "The y coordinate of the center of the random position disc.",
366  DoubleValue (0.0),
367  MakeDoubleAccessor (&RandomDiscPositionAllocator::m_y),
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),
433  MakeDoubleAccessor (&UniformDiscPositionAllocator::m_rho),
434  MakeDoubleChecker<double> ())
435  .AddAttribute ("X",
436  "The x coordinate of the center of the disc.",
437  DoubleValue (0.0),
438  MakeDoubleAccessor (&UniformDiscPositionAllocator::m_x),
439  MakeDoubleChecker<double> ())
440  .AddAttribute ("Y",
441  "The y coordinate of the center of the disc.",
442  DoubleValue (0.0),
443  MakeDoubleAccessor (&UniformDiscPositionAllocator::m_y),
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
In column-first mode, positions are allocated on the first column until N positions have been allocat...
Ptr< RandomVariableStream > m_y
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
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)
Definition: enum.cc:178
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
hold variables of type string
Definition: string.h:18
Ptr< RandomVariableStream > m_theta
void SetZ(Ptr< RandomVariableStream > z)
enum LayoutType GetLayoutType(void) const
virtual Vector GetNext(void) const
std::vector< Vector > m_positions
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
Ptr< RandomVariableStream > m_rho
virtual int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
static TypeId GetTypeId(void)
std::vector< Vector >::const_iterator m_current
Ptr< UniformRandomVariable > m_rv
void SetRho(Ptr< RandomVariableStream > rho)
virtual Vector GetNext(void) const
a 3d vector
Definition: vector.h:31
virtual double GetValue(void)=0
Returns a random double from the underlying distribution.
virtual Vector GetNext(void) const
static TypeId GetTypeId(void)
hold variables of type 'enum'
Definition: enum.h:37
void SetY(Ptr< RandomVariableStream > y)
Hold an unsigned integer type.
Definition: uinteger.h:46
Vector3D Vector
Definition: vector.h:118
Allocate positions on a rectangular 2d grid.
void SetTheta(Ptr< RandomVariableStream > theta)
Allocate positions from a deterministic list specified by the user.
void SetY(Ptr< RandomVariableStream > y)
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)
Returns a random double from the uniform distribution with the specified range.
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...
Allocate the positions uniformely (with constant density) randomly within a disc. ...
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Definition: enum.h:118
static TypeId GetTypeId(void)
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:213
void SetLayoutType(enum LayoutType layoutType)
virtual Vector GetNext(void) const
uint32_t GetN(void) const
void SetX(Ptr< RandomVariableStream > x)
void SetX(Ptr< RandomVariableStream > x)
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:64
Hold a floating point type.
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:49
Allocate random positions within a disc according to a given distribution for the polar coordinates o...
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
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
Ptr< RandomVariableStream > m_x
Allocate a set of positions.