|
|
| 16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
* |
17 |
* |
| 18 |
* Author: Nicola Baldo <nbaldo@cttc.es> |
18 |
* Author: Nicola Baldo <nbaldo@cttc.es> |
|
|
19 |
* Michele Polese <michele.polese@gmail.com> for the OutdoorPositionAllocator class |
| 19 |
*/ |
20 |
*/ |
| 20 |
#include "building-position-allocator.h" |
21 |
#include "building-position-allocator.h" |
| 21 |
#include <ns3/mobility-building-info.h> |
22 |
#include <ns3/mobility-building-info.h> |
|
Lines 104-109
RandomBuildingPositionAllocator::AssignStreams (int64_t stream)
|
Link Here
|
|---|
|
| 104 |
} |
105 |
} |
| 105 |
|
106 |
|
| 106 |
|
107 |
|
|
|
108 |
NS_OBJECT_ENSURE_REGISTERED (OutdoorPositionAllocator); |
| 109 |
|
| 110 |
OutdoorPositionAllocator::OutdoorPositionAllocator () |
| 111 |
{ |
| 112 |
m_maxAttempts = 1000; |
| 113 |
} |
| 114 |
|
| 115 |
TypeId |
| 116 |
OutdoorPositionAllocator::GetTypeId (void) |
| 117 |
{ |
| 118 |
static TypeId tid = TypeId ("ns3::OutdoorPositionAllocator") |
| 119 |
.SetParent<PositionAllocator> () |
| 120 |
.SetGroupName ("Buildings") |
| 121 |
.AddConstructor<OutdoorPositionAllocator> () |
| 122 |
.AddAttribute ("X", |
| 123 |
"A random variable which represents the x coordinate of a position in a random box.", |
| 124 |
StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"), |
| 125 |
MakePointerAccessor (&OutdoorPositionAllocator::m_x), |
| 126 |
MakePointerChecker<RandomVariableStream> ()) |
| 127 |
.AddAttribute ("Y", |
| 128 |
"A random variable which represents the y coordinate of a position in a random box.", |
| 129 |
StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"), |
| 130 |
MakePointerAccessor (&OutdoorPositionAllocator::m_y), |
| 131 |
MakePointerChecker<RandomVariableStream> ()) |
| 132 |
.AddAttribute ("Z", |
| 133 |
"A random variable which represents the z coordinate of a position in a random box.", |
| 134 |
StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"), |
| 135 |
MakePointerAccessor (&OutdoorPositionAllocator::m_z), |
| 136 |
MakePointerChecker<RandomVariableStream> ()) |
| 137 |
.AddAttribute ("MaxAttempts", |
| 138 |
"Maximum number of attempts for the rejection sampling before giving up.", |
| 139 |
UintegerValue (1000), |
| 140 |
MakeUintegerAccessor (&OutdoorPositionAllocator::m_maxAttempts), |
| 141 |
MakeUintegerChecker<uint32_t> ()) |
| 142 |
; |
| 143 |
|
| 144 |
return tid; |
| 145 |
} |
| 146 |
|
| 147 |
void |
| 148 |
OutdoorPositionAllocator::SetX (Ptr<RandomVariableStream> x) |
| 149 |
{ |
| 150 |
m_x = x; |
| 151 |
} |
| 152 |
void |
| 153 |
OutdoorPositionAllocator::SetY (Ptr<RandomVariableStream> y) |
| 154 |
{ |
| 155 |
m_y = y; |
| 156 |
} |
| 157 |
void |
| 158 |
OutdoorPositionAllocator::SetZ (Ptr<RandomVariableStream> z) |
| 159 |
{ |
| 160 |
m_z = z; |
| 161 |
} |
| 162 |
|
| 163 |
Vector |
| 164 |
OutdoorPositionAllocator::GetNext () const |
| 165 |
{ |
| 166 |
NS_ABORT_MSG_IF (BuildingList::GetNBuildings () == 0, "no building found"); |
| 167 |
|
| 168 |
bool outdoor = false; |
| 169 |
uint32_t attempts = 0; |
| 170 |
Vector position = Vector (0,0,0); |
| 171 |
|
| 172 |
while (!outdoor && attempts < m_maxAttempts) |
| 173 |
{ |
| 174 |
// get a random position |
| 175 |
double x = m_x->GetValue (); |
| 176 |
double y = m_y->GetValue (); |
| 177 |
double z = m_z->GetValue (); |
| 178 |
|
| 179 |
position = Vector (x, y, z); |
| 180 |
|
| 181 |
NS_LOG_INFO ("Position " << position); |
| 182 |
|
| 183 |
bool inside = false; |
| 184 |
for (BuildingList::Iterator bit = BuildingList::Begin (); bit != BuildingList::End (); ++bit) |
| 185 |
{ |
| 186 |
if ((*bit)->IsInside (position)) |
| 187 |
{ |
| 188 |
NS_LOG_INFO ("Position " << position << " is inside the building with boundaries " |
| 189 |
<< (*bit)->GetBoundaries ().xMin << " " << (*bit)->GetBoundaries ().xMax << " " |
| 190 |
<< (*bit)->GetBoundaries ().yMin << " " << (*bit)->GetBoundaries ().yMax << " " |
| 191 |
<< (*bit)->GetBoundaries ().zMin << " " << (*bit)->GetBoundaries ().zMax); |
| 192 |
inside = true; |
| 193 |
break; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if (inside) |
| 198 |
{ |
| 199 |
NS_LOG_INFO ("Inside a building, attemp " << attempts << " out of " << m_maxAttempts); |
| 200 |
attempts++; |
| 201 |
} |
| 202 |
else |
| 203 |
{ |
| 204 |
NS_LOG_INFO ("Outdoor position found " << position); |
| 205 |
outdoor = true; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
NS_ABORT_MSG_IF (attempts >= m_maxAttempts, "Too many attempts, give up"); |
| 210 |
NS_ABORT_MSG_IF (!outdoor, "Still indoor, give up"); |
| 211 |
return position; |
| 212 |
} |
| 213 |
|
| 214 |
int64_t |
| 215 |
OutdoorPositionAllocator::AssignStreams (int64_t stream) |
| 216 |
{ |
| 217 |
m_x->SetStream (stream); |
| 218 |
m_y->SetStream (stream + 1); |
| 219 |
m_z->SetStream (stream + 2); |
| 220 |
return 3; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 107 |
NS_OBJECT_ENSURE_REGISTERED (RandomRoomPositionAllocator); |
224 |
NS_OBJECT_ENSURE_REGISTERED (RandomRoomPositionAllocator); |
| 108 |
|
225 |
|
| 109 |
|
226 |
|