00001 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2007 INRIA 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> 00019 */ 00020 #ifndef POSITION_ALLOCATOR_H 00021 #define POSITION_ALLOCATOR_H 00022 00023 #include "ns3/object.h" 00024 #include "ns3/random-variable.h" 00025 #include "vector.h" 00026 00027 namespace ns3 { 00028 00029 /** 00030 * \brief allocate a set of positions. The allocation strategy is implemented 00031 * in subclasses. 00032 * 00033 * This is a pure abstract base class. 00034 */ 00035 class PositionAllocator : public Object 00036 { 00037 public: 00038 static TypeId GetTypeId (void); 00039 PositionAllocator (); 00040 virtual ~PositionAllocator (); 00041 /** 00042 * \returns the next chosen position. 00043 * 00044 * This method _must_ be implement in subclasses. 00045 */ 00046 virtual Vector GetNext (void) const = 0; 00047 }; 00048 00049 /** 00050 * \brief Allocate positions from a deterministic list specified by the user. 00051 * 00052 * The first call to ListPositionAllocator::GetNext will return the 00053 * first element of the list, the second call, the second element, and so on. 00054 */ 00055 class ListPositionAllocator : public PositionAllocator 00056 { 00057 public: 00058 static TypeId GetTypeId (void); 00059 ListPositionAllocator (); 00060 00061 /** 00062 * \param v the position to append at the end of the list of 00063 * positions to return from GetNext. 00064 */ 00065 void Add (Vector v); 00066 virtual Vector GetNext (void) const; 00067 private: 00068 std::vector<Vector> m_positions; 00069 mutable std::vector<Vector>::const_iterator m_current; 00070 }; 00071 00072 /** 00073 * \brief Allocate positions on a rectangular 2d grid. 00074 */ 00075 class GridPositionAllocator : public PositionAllocator 00076 { 00077 public: 00078 static TypeId GetTypeId (void); 00079 00080 /** 00081 * Determine whether positions are allocated row first or column first. 00082 */ 00083 enum LayoutType { 00084 /** 00085 * In row-first mode, positions are allocated on the first row until 00086 * N positions have been allocated. Then, the second row located a yMin + yDelta 00087 * is used to allocate positions. 00088 */ 00089 ROW_FIRST, 00090 /** 00091 * In column-first mode, positions are allocated on the first column until 00092 * N positions have been allocated. Then, the second column located a xMin + xDelta 00093 * is used to allocate positions. 00094 */ 00095 COLUMN_FIRST 00096 }; 00097 00098 GridPositionAllocator (); 00099 00100 /** 00101 * \param xMin the x coordinate where layout will start. 00102 */ 00103 void SetMinX (double xMin); 00104 /** 00105 * \param yMin the y coordinate where layout will start 00106 */ 00107 void SetMinY (double yMin); 00108 /** 00109 * \param deltaX the x interval between two x-consecutive positions. 00110 */ 00111 void SetDeltaX (double deltaX); 00112 /** 00113 * \param deltaY the y interval between two y-consecutive positions. 00114 */ 00115 void SetDeltaY (double deltaY); 00116 /** 00117 * \param n the number of positions allocated on each row (or each column) 00118 * before switching to the next column (or row). 00119 */ 00120 void SetN (uint32_t n); 00121 /** 00122 * \param layoutType the type of layout to use (row first or column first). 00123 */ 00124 void SetLayoutType (enum LayoutType layoutType); 00125 00126 /** 00127 * \returns the x coordinate of the first allocated position. 00128 */ 00129 double GetMinX (void) const; 00130 /** 00131 * \returns the y coordinate of the first allocated position. 00132 */ 00133 double GetMinY (void) const; 00134 /** 00135 * \returns the x interval between two x-consecutive positions. 00136 */ 00137 double GetDeltaX (void) const; 00138 /** 00139 * \returns the y interval between two y-consecutive positions. 00140 */ 00141 double GetDeltaY (void) const; 00142 /** 00143 * \returns the number of positions to allocate on each row or each column. 00144 */ 00145 uint32_t GetN (void) const; 00146 /** 00147 * \returns the currently-selected layout type. 00148 */ 00149 enum LayoutType GetLayoutType (void) const; 00150 00151 00152 virtual Vector GetNext (void) const; 00153 private: 00154 mutable uint32_t m_current; 00155 enum LayoutType m_layoutType; 00156 double m_xMin; 00157 double m_yMin; 00158 uint32_t m_n; 00159 double m_deltaX; 00160 double m_deltaY; 00161 }; 00162 00163 /** 00164 * \brief allocate random positions within a rectangle 00165 * according to a pair of random variables. 00166 */ 00167 class RandomRectanglePositionAllocator : public PositionAllocator 00168 { 00169 public: 00170 static TypeId GetTypeId (void); 00171 RandomRectanglePositionAllocator (); 00172 virtual ~RandomRectanglePositionAllocator (); 00173 00174 void SetX (RandomVariable x); 00175 void SetY (RandomVariable y); 00176 00177 virtual Vector GetNext (void) const; 00178 private: 00179 RandomVariable m_x; 00180 RandomVariable m_y; 00181 }; 00182 00183 /** 00184 * \brief allocate random positions within a disc 00185 * according to a pair of random variables. 00186 */ 00187 class RandomDiscPositionAllocator : public PositionAllocator 00188 { 00189 public: 00190 static TypeId GetTypeId (void); 00191 RandomDiscPositionAllocator (); 00192 virtual ~RandomDiscPositionAllocator (); 00193 00194 void SetTheta (RandomVariable theta); 00195 void SetRho (RandomVariable rho); 00196 void SetX (double x); 00197 void SetY (double y); 00198 00199 virtual Vector GetNext (void) const; 00200 private: 00201 RandomVariable m_theta; 00202 RandomVariable m_rho; 00203 double m_x; 00204 double m_y; 00205 }; 00206 00207 } // namespace ns3 00208 00209 #endif /* RANDOM_POSITION_H */