src/mobility/position-allocator.cc
changeset 2503 e667dc0f350e
parent 2336 28ce210b91bb
parent 2499 806f6efe1c33
child 2518 54022e0d243b
equal deleted inserted replaced
2357:d64b1561b1c2 2503:e667dc0f350e
       
     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/random-variable.h"
       
    22 #include "ns3/double.h"
       
    23 #include "ns3/integer.h"
       
    24 #include "ns3/enum.h"
       
    25 #include "ns3/log.h"
       
    26 #include <cmath>
       
    27 
       
    28 NS_LOG_COMPONENT_DEFINE ("PositionAllocator");
       
    29 
       
    30 namespace ns3 {
       
    31 
       
    32 NS_OBJECT_ENSURE_REGISTERED (PositionAllocator);
       
    33 
       
    34 TypeId 
       
    35 PositionAllocator::GetTypeId (void)
       
    36 {
       
    37   static TypeId tid = TypeId ("PositionAllocator")
       
    38     .SetParent<Object> ();
       
    39   return tid;
       
    40 }
       
    41 
       
    42 PositionAllocator::PositionAllocator ()
       
    43 {
       
    44 }
       
    45 
       
    46 PositionAllocator::~PositionAllocator ()
       
    47 {}
       
    48 
       
    49 TypeId 
       
    50 GridPositionAllocator::GetTypeId (void)
       
    51 {
       
    52   static TypeId tid = TypeId ("GridPositionAllocator")
       
    53     .SetParent<PositionAllocator> ()
       
    54     .SetGroupName ("Mobility")
       
    55     .AddConstructor<GridPositionAllocator> ()
       
    56     .AddAttribute ("GridWidth", "The number of objects layed out on a line.",
       
    57                    Integer (10),
       
    58                    MakeIntegerAccessor (&GridPositionAllocator::m_n),
       
    59                    MakeIntegerChecker<uint32_t> ())
       
    60     .AddAttribute ("MinX", "The x coordinate where the grid starts.",
       
    61                    Double (1.0),
       
    62                    MakeDoubleAccessor (&GridPositionAllocator::m_xMin),
       
    63                    MakeDoubleChecker<double> ())
       
    64     .AddAttribute ("MinY", "The y coordinate where the grid starts.",
       
    65                    Double (0.0),
       
    66                    MakeDoubleAccessor (&GridPositionAllocator::m_yMin),
       
    67                    MakeDoubleChecker<double> ())
       
    68     .AddAttribute ("DeltaX", "The x space between objects.",
       
    69                    Double (1.0),
       
    70                    MakeDoubleAccessor (&GridPositionAllocator::m_deltaX),
       
    71                    MakeDoubleChecker<double> ())
       
    72     .AddAttribute ("DeltaY", "The y space between objects.",
       
    73                    Double (1.0),
       
    74                    MakeDoubleAccessor (&GridPositionAllocator::m_deltaY),
       
    75                    MakeDoubleChecker<double> ())
       
    76     .AddAttribute ("LayoutType", "The type of layout.",
       
    77                    Enum (ROW_FIRST),
       
    78                    MakeEnumAccessor (&GridPositionAllocator::m_layoutType),
       
    79                    MakeEnumChecker (ROW_FIRST, "RowFirst",
       
    80                                     COLUMN_FIRST, "ColumnFirst"))
       
    81     ;
       
    82   return tid;
       
    83 }
       
    84 GridPositionAllocator::GridPositionAllocator ()
       
    85   : m_current (0)
       
    86 {}
       
    87 
       
    88 Vector 
       
    89 GridPositionAllocator::GetNext (void) const
       
    90 {
       
    91   double x = 0.0, y = 0.0;
       
    92   switch (m_layoutType) {
       
    93   case ROW_FIRST:
       
    94     x = m_xMin + m_deltaX * (m_current % m_n);
       
    95     y = m_yMin + m_deltaY * (m_current / m_n);
       
    96     break;
       
    97   case COLUMN_FIRST:
       
    98     x = m_xMin + m_deltaX * (m_current / m_n);
       
    99     y = m_yMin + m_deltaY * (m_current % m_n);
       
   100     break;
       
   101   }
       
   102   m_current++;
       
   103   return Vector (x, y, 0.0);
       
   104 }
       
   105 
       
   106 
       
   107 NS_OBJECT_ENSURE_REGISTERED (RandomRectanglePositionAllocator);
       
   108 
       
   109 TypeId
       
   110 RandomRectanglePositionAllocator::GetTypeId (void)
       
   111 {
       
   112   static TypeId tid = TypeId ("RandomRectanglePositionAllocator")
       
   113     .SetParent<PositionAllocator> ()
       
   114     .SetGroupName ("Mobility")
       
   115     .AddConstructor<RandomRectanglePositionAllocator> ()
       
   116     .AddAttribute ("X",
       
   117                    "A random variable which represents the x coordinate of a position in a random rectangle.",
       
   118                    UniformVariable (0.0, 1.0),
       
   119                    MakeRandomVariableAccessor (&RandomRectanglePositionAllocator::m_x),
       
   120                    MakeRandomVariableChecker ())
       
   121     .AddAttribute ("Y",
       
   122                    "A random variable which represents the y coordinate of a position in a random rectangle.",
       
   123                    UniformVariable (0.0, 1.0),
       
   124                    MakeRandomVariableAccessor (&RandomRectanglePositionAllocator::m_y),
       
   125                    MakeRandomVariableChecker ());
       
   126   return tid;
       
   127 }
       
   128 
       
   129 RandomRectanglePositionAllocator::RandomRectanglePositionAllocator ()
       
   130 {}
       
   131 RandomRectanglePositionAllocator::~RandomRectanglePositionAllocator ()
       
   132 {}
       
   133 Vector
       
   134 RandomRectanglePositionAllocator::GetNext (void) const
       
   135 {
       
   136   double x = m_x.GetValue ();
       
   137   double y = m_y.GetValue ();
       
   138   return Vector (x, y, 0.0);
       
   139 }
       
   140 
       
   141 NS_OBJECT_ENSURE_REGISTERED (RandomDiscPositionAllocator);
       
   142 
       
   143 TypeId
       
   144 RandomDiscPositionAllocator::GetTypeId (void)
       
   145 {
       
   146   static TypeId tid = TypeId ("RandomDiscPositionAllocator")
       
   147     .SetParent<PositionAllocator> ()
       
   148     .SetGroupName ("Mobility")
       
   149     .AddConstructor<RandomDiscPositionAllocator> ()
       
   150     .AddAttribute ("Theta",
       
   151                    "A random variable which represents the angle (gradients) of a position in a random disc.",
       
   152                    UniformVariable (0.0, 6.2830),
       
   153                    MakeRandomVariableAccessor (&RandomDiscPositionAllocator::m_theta),
       
   154                    MakeRandomVariableChecker ())
       
   155     .AddAttribute ("Rho",
       
   156                    "A random variable which represents the radius of a position in a random disc.",
       
   157                    UniformVariable (0.0, 200.0),
       
   158                    MakeRandomVariableAccessor (&RandomDiscPositionAllocator::m_rho),
       
   159                    MakeRandomVariableChecker ())
       
   160     .AddAttribute ("X",
       
   161                    "The x coordinate of the center of the random position disc.",
       
   162                    Double (0.0),
       
   163                    MakeDoubleAccessor (&RandomDiscPositionAllocator::m_x),
       
   164                    MakeDoubleChecker<double> ())
       
   165     .AddAttribute ("Y",
       
   166                    "The y coordinate of the center of the random position disc.",
       
   167                    Double (0.0),
       
   168                    MakeDoubleAccessor (&RandomDiscPositionAllocator::m_y),
       
   169                    MakeDoubleChecker<double> ())
       
   170     ;
       
   171   return tid;
       
   172 }   
       
   173 
       
   174 RandomDiscPositionAllocator::RandomDiscPositionAllocator ()
       
   175 {}
       
   176 RandomDiscPositionAllocator::~RandomDiscPositionAllocator ()
       
   177 {}
       
   178 Vector
       
   179 RandomDiscPositionAllocator::GetNext (void) const
       
   180 {
       
   181   double theta = m_theta.GetValue ();
       
   182   double rho = m_rho.GetValue ();
       
   183   double x = m_x + std::cos (theta) * rho;
       
   184   double y = m_y + std::sin (theta) * rho;
       
   185   NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y);
       
   186   return Vector (x, y, 0.0);
       
   187 }
       
   188 
       
   189 
       
   190 } // namespace ns3