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 
36 TypeId
38 {
39  static TypeId tid = TypeId ("ns3::PositionAllocator")
40  .SetParent<Object> ();
41  return tid;
42 }
43 
45 {
46 }
47 
49 {
50 }
51 
53  ;
54 
55 TypeId
57 {
58  static TypeId tid = TypeId ("ns3::ListPositionAllocator")
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 
93 TypeId
95 {
96  static TypeId tid = TypeId ("ns3::GridPositionAllocator")
98  .SetGroupName ("Mobility")
99  .AddConstructor<GridPositionAllocator> ()
100  .AddAttribute ("GridWidth", "The number of objects layed out on a line.",
101  UintegerValue (10),
102  MakeUintegerAccessor (&GridPositionAllocator::m_n),
103  MakeUintegerChecker<uint32_t> ())
104  .AddAttribute ("MinX", "The x coordinate where the grid starts.",
105  DoubleValue (1.0),
106  MakeDoubleAccessor (&GridPositionAllocator::m_xMin),
107  MakeDoubleChecker<double> ())
108  .AddAttribute ("MinY", "The y coordinate where the grid starts.",
109  DoubleValue (0.0),
110  MakeDoubleAccessor (&GridPositionAllocator::m_yMin),
111  MakeDoubleChecker<double> ())
112  .AddAttribute ("DeltaX", "The x space between objects.",
113  DoubleValue (1.0),
114  MakeDoubleAccessor (&GridPositionAllocator::m_deltaX),
115  MakeDoubleChecker<double> ())
116  .AddAttribute ("DeltaY", "The y space between objects.",
117  DoubleValue (1.0),
118  MakeDoubleAccessor (&GridPositionAllocator::m_deltaY),
119  MakeDoubleChecker<double> ())
120  .AddAttribute ("LayoutType", "The type of layout.",
123  MakeEnumChecker (ROW_FIRST, "RowFirst",
124  COLUMN_FIRST, "ColumnFirst"))
125  ;
126  return tid;
127 }
129  : m_current (0)
130 {
131 }
132 
133 void
135 {
136  m_xMin = xMin;
137 }
138 void
140 {
141  m_yMin = yMin;
142 }
143 void
145 {
146  m_deltaX = deltaX;
147 }
148 void
150 {
151  m_deltaY = deltaY;
152 }
153 void
155 {
156  m_n = n;
157 }
158 void
160 {
161  m_layoutType = layoutType;
162 }
163 
164 double
166 {
167  return m_xMin;
168 }
169 double
171 {
172  return m_yMin;
173 }
174 double
176 {
177  return m_deltaX;
178 }
179 double
181 {
182  return m_deltaY;
183 }
184 uint32_t
186 {
187  return m_n;
188 }
191 {
192  return m_layoutType;
193 }
194 
195 Vector
197 {
198  double x = 0.0, y = 0.0;
199  switch (m_layoutType) {
200  case ROW_FIRST:
201  x = m_xMin + m_deltaX * (m_current % m_n);
202  y = m_yMin + m_deltaY * (m_current / m_n);
203  break;
204  case COLUMN_FIRST:
205  x = m_xMin + m_deltaX * (m_current / m_n);
206  y = m_yMin + m_deltaY * (m_current % m_n);
207  break;
208  }
209  m_current++;
210  return Vector (x, y, 0.0);
211 }
212 
213 int64_t
215 {
216  return 0;
217 }
218 
220  ;
221 
222 TypeId
224 {
225  static TypeId tid = TypeId ("ns3::RandomRectanglePositionAllocator")
227  .SetGroupName ("Mobility")
228  .AddConstructor<RandomRectanglePositionAllocator> ()
229  .AddAttribute ("X",
230  "A random variable which represents the x coordinate of a position in a random rectangle.",
231  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
232  MakePointerAccessor (&RandomRectanglePositionAllocator::m_x),
233  MakePointerChecker<RandomVariableStream> ())
234  .AddAttribute ("Y",
235  "A random variable which represents the y coordinate of a position in a random rectangle.",
236  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
237  MakePointerAccessor (&RandomRectanglePositionAllocator::m_y),
238  MakePointerChecker<RandomVariableStream> ());
239  return tid;
240 }
241 
243 {
244 }
246 {
247 }
248 
249 void
251 {
252  m_x = x;
253 }
254 void
256 {
257  m_y = y;
258 }
259 
260 Vector
262 {
263  double x = m_x->GetValue ();
264  double y = m_y->GetValue ();
265  return Vector (x, y, 0.0);
266 }
267 
268 int64_t
270 {
271  m_x->SetStream (stream);
272  m_y->SetStream (stream + 1);
273  return 2;
274 }
275 
277  ;
278 
279 TypeId
281 {
282  static TypeId tid = TypeId ("ns3::RandomBoxPositionAllocator")
284  .SetGroupName ("Mobility")
285  .AddConstructor<RandomBoxPositionAllocator> ()
286  .AddAttribute ("X",
287  "A random variable which represents the x coordinate of a position in a random box.",
288  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
289  MakePointerAccessor (&RandomBoxPositionAllocator::m_x),
290  MakePointerChecker<RandomVariableStream> ())
291  .AddAttribute ("Y",
292  "A random variable which represents the y coordinate of a position in a random box.",
293  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
294  MakePointerAccessor (&RandomBoxPositionAllocator::m_y),
295  MakePointerChecker<RandomVariableStream> ())
296  .AddAttribute ("Z",
297  "A random variable which represents the z coordinate of a position in a random box.",
298  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
299  MakePointerAccessor (&RandomBoxPositionAllocator::m_z),
300  MakePointerChecker<RandomVariableStream> ());
301  return tid;
302 }
303 
305 {
306 }
308 {
309 }
310 
311 void
313 {
314  m_x = x;
315 }
316 void
318 {
319  m_y = y;
320 }
321 void
323 {
324  m_z = z;
325 }
326 
327 Vector
329 {
330  double x = m_x->GetValue ();
331  double y = m_y->GetValue ();
332  double z = m_z->GetValue ();
333  return Vector (x, y, z);
334 }
335 
336 int64_t
338 {
339  m_x->SetStream (stream);
340  m_y->SetStream (stream + 1);
341  m_z->SetStream (stream + 2);
342  return 3;
343 }
344 
346  ;
347 
348 TypeId
350 {
351  static TypeId tid = TypeId ("ns3::RandomDiscPositionAllocator")
353  .SetGroupName ("Mobility")
354  .AddConstructor<RandomDiscPositionAllocator> ()
355  .AddAttribute ("Theta",
356  "A random variable which represents the angle (gradients) of a position in a random disc.",
357  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=6.2830]"),
358  MakePointerAccessor (&RandomDiscPositionAllocator::m_theta),
359  MakePointerChecker<RandomVariableStream> ())
360  .AddAttribute ("Rho",
361  "A random variable which represents the radius of a position in a random disc.",
362  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=200.0]"),
363  MakePointerAccessor (&RandomDiscPositionAllocator::m_rho),
364  MakePointerChecker<RandomVariableStream> ())
365  .AddAttribute ("X",
366  "The x coordinate of the center of the random position disc.",
367  DoubleValue (0.0),
368  MakeDoubleAccessor (&RandomDiscPositionAllocator::m_x),
369  MakeDoubleChecker<double> ())
370  .AddAttribute ("Y",
371  "The y coordinate of the center of the random position disc.",
372  DoubleValue (0.0),
373  MakeDoubleAccessor (&RandomDiscPositionAllocator::m_y),
374  MakeDoubleChecker<double> ())
375  ;
376  return tid;
377 }
378 
380 {
381 }
383 {
384 }
385 
386 void
388 {
389  m_theta = theta;
390 }
391 void
393 {
394  m_rho = rho;
395 }
396 void
398 {
399  m_x = x;
400 }
401 void
403 {
404  m_y = y;
405 }
406 Vector
408 {
409  double theta = m_theta->GetValue ();
410  double rho = m_rho->GetValue ();
411  double x = m_x + std::cos (theta) * rho;
412  double y = m_y + std::sin (theta) * rho;
413  NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y);
414  return Vector (x, y, 0.0);
415 }
416 
417 int64_t
419 {
420  m_theta->SetStream (stream);
421  m_rho->SetStream (stream + 1);
422  return 2;
423 }
424 
425 
426 
428  ;
429 
430 TypeId
432 {
433  static TypeId tid = TypeId ("ns3::UniformDiscPositionAllocator")
435  .SetGroupName ("Mobility")
436  .AddConstructor<UniformDiscPositionAllocator> ()
437  .AddAttribute ("rho",
438  "The radius of the disc",
439  DoubleValue (0.0),
440  MakeDoubleAccessor (&UniformDiscPositionAllocator::m_rho),
441  MakeDoubleChecker<double> ())
442  .AddAttribute ("X",
443  "The x coordinate of the center of the disc.",
444  DoubleValue (0.0),
445  MakeDoubleAccessor (&UniformDiscPositionAllocator::m_x),
446  MakeDoubleChecker<double> ())
447  .AddAttribute ("Y",
448  "The y coordinate of the center of the disc.",
449  DoubleValue (0.0),
450  MakeDoubleAccessor (&UniformDiscPositionAllocator::m_y),
451  MakeDoubleChecker<double> ())
452  ;
453  return tid;
454 }
455 
457 {
458  m_rv = CreateObject<UniformRandomVariable> ();
459 }
461 {
462 }
463 
464 void
466 {
467  m_rho = rho;
468 }
469 void
471 {
472  m_x = x;
473 }
474 void
476 {
477  m_y = y;
478 }
479 Vector
481 {
482  double x,y;
483  do
484  {
485  x = m_rv->GetValue (-m_rho, m_rho);
486  y = m_rv->GetValue (-m_rho, m_rho);
487  }
488  while (std::sqrt (x*x + y*y) > m_rho);
489 
490  x += m_x;
491  y += m_y;
492  NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y);
493  return Vector (x, y, 0.0);
494 }
495 
496 int64_t
498 {
499  m_rv->SetStream (stream);
500  return 1;
501 }
502 
503 
504 } // 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
hold variables of type string
Definition: string.h:19
Ptr< RandomVariableStream > m_theta
void SetZ(Ptr< RandomVariableStream > z)
enum LayoutType GetLayoutType(void) const
virtual Vector GetNext(void) const
std::vector< Vector > m_positions
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
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
NS_LOG_COMPONENT_DEFINE("PositionAllocator")
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)
Definition: log.h:289
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:63
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:611
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.