A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
building-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) 2012 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
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: Nicola Baldo <nbaldo@cttc.es>
19
*/
20
#include "
building-position-allocator.h
"
21
#include <ns3/mobility-building-info.h>
22
#include "ns3/mobility-model.h"
23
#include "ns3/buildings-helper.h"
24
#include "ns3/random-variable-stream.h"
25
#include "ns3/double.h"
26
#include "ns3/uinteger.h"
27
#include "ns3/enum.h"
28
#include "ns3/boolean.h"
29
#include "ns3/log.h"
30
#include "ns3/box.h"
31
#include "ns3/building.h"
32
#include <cmath>
33
34
#include "ns3/building-list.h"
35
36
NS_LOG_COMPONENT_DEFINE
(
"BuildingPositionAllocator"
);
37
38
namespace
ns3 {
39
40
NS_OBJECT_ENSURE_REGISTERED
(RandomBuildingPositionAllocator);
41
42
43
RandomBuildingPositionAllocator::RandomBuildingPositionAllocator
()
44
{
45
m_rand
= CreateObject<UniformRandomVariable> ();
46
}
47
48
TypeId
49
RandomBuildingPositionAllocator::GetTypeId
(
void
)
50
{
51
static
TypeId
tid =
TypeId
(
"ns3::RandomBuildingPositionAllocator"
)
52
.
SetParent
<
PositionAllocator
> ()
53
.SetGroupName (
"Mobility"
)
54
.AddConstructor<
RandomBuildingPositionAllocator
> ()
55
.AddAttribute (
"WithReplacement"
,
56
"If true, the building will be randomly selected with replacement. "
57
"If false, no replacement will occur, until the list of buildings "
58
"to select becomes empty, at which point it will be filled again "
59
"with the list of all buildings."
,
60
BooleanValue
(
false
),
61
MakeBooleanAccessor (&
RandomBuildingPositionAllocator::m_withReplacement
),
62
MakeBooleanChecker ());
63
return
tid;
64
}
65
66
Vector
67
RandomBuildingPositionAllocator::GetNext
()
const
68
{
69
NS_ASSERT_MSG
(
BuildingList::GetNBuildings
() > 0,
"no building found"
);
70
Ptr<Building>
b;
71
if
(
m_withReplacement
)
72
{
73
uint32_t n =
m_rand
->
GetInteger
(0,
BuildingList::GetNBuildings
() - 1);
74
b =
BuildingList::GetBuilding
(n);
75
}
76
else
77
{
78
if
(
m_buildingListWithoutReplacement
.empty ())
79
{
80
for
(
BuildingList::Iterator
bit =
BuildingList::Begin
(); bit !=
BuildingList::End
(); ++bit)
81
{
82
m_buildingListWithoutReplacement
.push_back (*bit);
83
}
84
}
85
uint32_t n =
m_rand
->
GetInteger
(0,
m_buildingListWithoutReplacement
.size () - 1);
86
b =
m_buildingListWithoutReplacement
.at (n);
87
m_buildingListWithoutReplacement
.erase (
m_buildingListWithoutReplacement
.begin () + n);
88
}
89
90
Ptr<RandomBoxPositionAllocator>
pa = CreateObject<RandomBoxPositionAllocator> ();
91
BoxValue
bv;
92
b->
GetAttribute
(
"Boundaries"
, bv);
93
double
x
=
m_rand
->
GetValue
(bv.Get ().xMin, bv.Get ().xMax);
94
double
y =
m_rand
->
GetValue
(bv.Get ().yMin, bv.Get ().yMax);
95
double
z =
m_rand
->
GetValue
(bv.Get ().zMin, bv.Get ().zMax);
96
return
Vector
(x, y, z);
97
}
98
99
int64_t
100
RandomBuildingPositionAllocator::AssignStreams
(int64_t stream)
101
{
102
m_rand
->
SetStream
(stream);
103
return
1;
104
}
105
106
107
NS_OBJECT_ENSURE_REGISTERED
(
RandomRoomPositionAllocator
);
108
109
110
RandomRoomPositionAllocator::RandomRoomPositionAllocator
()
111
{
112
m_rand
= CreateObject<UniformRandomVariable> ();
113
}
114
115
TypeId
116
RandomRoomPositionAllocator::GetTypeId
(
void
)
117
{
118
static
TypeId
tid =
TypeId
(
"ns3::RandomRoomPositionAllocator"
)
119
.
SetParent
<
PositionAllocator
> ()
120
.SetGroupName (
"Mobility"
)
121
.AddConstructor<
RandomRoomPositionAllocator
> ();
122
return
tid;
123
}
124
125
Vector
126
RandomRoomPositionAllocator::GetNext
()
const
127
{
128
NS_LOG_FUNCTION
(
this
);
129
NS_ASSERT_MSG
(
BuildingList::GetNBuildings
() > 0,
"no building found"
);
130
131
if
(
m_roomListWithoutReplacement
.empty ())
132
{
133
for
(
BuildingList::Iterator
bit =
BuildingList::Begin
(); bit !=
BuildingList::End
(); ++bit)
134
{
135
NS_LOG_LOGIC
(
"building "
<< (*bit)->GetId ());
136
for
(uint32_t rx = 1; rx <= (*bit)->GetNRoomsX (); ++rx)
137
{
138
for
(uint32_t ry = 1; ry <= (*bit)->GetNRoomsY (); ++ry)
139
{
140
for
(uint32_t f = 1; f <= (*bit)->GetNFloors (); ++f)
141
{
142
RoomInfo
i;
143
i.
roomx
= rx;
144
i.
roomy
= ry;
145
i.
floor
= f;
146
i.
b
= *bit;
147
NS_LOG_LOGIC
(
"adding room ("
<< rx <<
", "
<< ry <<
", "
<< f <<
")"
);
148
m_roomListWithoutReplacement
.push_back (i);
149
}
150
}
151
}
152
}
153
}
154
uint32_t n =
m_rand
->
GetInteger
(0,
m_roomListWithoutReplacement
.size () - 1);
155
RoomInfo
r =
m_roomListWithoutReplacement
.at (n);
156
m_roomListWithoutReplacement
.erase (
m_roomListWithoutReplacement
.begin () + n);
157
NS_LOG_LOGIC
(
"considering building "
<< r.
b
->
GetId
() <<
" room ("
<< r.
roomx
<<
", "
<< r.
roomy
<<
", "
<< r.
floor
<<
")"
);
158
159
Ptr<RandomBoxPositionAllocator>
pa = CreateObject<RandomBoxPositionAllocator> ();
160
BoxValue
bv;
161
r.
b
->
GetAttribute
(
"Boundaries"
, bv);
162
Box
box = bv.Get ();
163
double
rdx = (box.
xMax
- box.
xMin
) / r.
b
->
GetNRoomsX
();
164
double
rdy = (box.
yMax
- box.
yMin
) / r.
b
->
GetNRoomsY
();
165
double
rdz = (box.
zMax
- box.
zMin
) / r.
b
->
GetNFloors
();
166
double
x1 = box.
xMin
+ rdx * (r.
roomx
- 1);
167
double
x2 = box.
xMin
+ rdx * r.
roomx
;
168
double
y1 = box.
yMin
+ rdy * (r.
roomy
-1);
169
double
y2 = box.
yMin
+ rdy * r.
roomy
;
170
double
z1 = box.
zMin
+ rdz * (r.
floor
- 1);
171
double
z2 = box.
zMin
+ rdz * r.
floor
;
172
NS_LOG_LOGIC
(
"randomly allocating position in "
173
<<
" ("
<< x1 <<
","
<< x2 <<
") "
174
<<
"x ("
<< y1 <<
","
<< y2 <<
") "
175
<<
"x ("
<< z1 <<
","
<< z2 <<
") "
);
176
177
double
x
=
m_rand
->
GetValue
(x1, x2);
178
double
y =
m_rand
->
GetValue
(y1, y2);
179
double
z =
m_rand
->
GetValue
(z1, z2);
180
181
return
Vector
(x, y, z);
182
}
183
184
int64_t
185
RandomRoomPositionAllocator::AssignStreams
(int64_t stream)
186
{
187
m_rand
->
SetStream
(stream);
188
return
1;
189
}
190
191
192
193
194
195
NS_OBJECT_ENSURE_REGISTERED
(
SameRoomPositionAllocator
);
196
197
SameRoomPositionAllocator::SameRoomPositionAllocator
()
198
{
199
NS_FATAL_ERROR
(
" Constructor \"SameRoomPositionAllocator ()\" should not be used"
);
200
}
201
202
203
SameRoomPositionAllocator::SameRoomPositionAllocator
(
NodeContainer
c)
204
: m_nodes (c)
205
{
206
m_rand
= CreateObject<UniformRandomVariable> ();
207
m_nodeIt
=
m_nodes
.
Begin
();
208
// this is needed to make sure the building models associated with c have been initialized
209
for
(
NodeContainer::Iterator
it =
m_nodes
.
Begin
(); it !=
m_nodes
.
End
(); ++it)
210
{
211
Ptr<MobilityModel>
mm = (*it)->
GetObject
<
MobilityModel
> ();
212
NS_ASSERT_MSG
(mm,
"no mobility model aggregated to this node"
);
213
Ptr<MobilityBuildingInfo>
bmm = mm->
GetObject
<
MobilityBuildingInfo
> ();
214
NS_ASSERT_MSG
(bmm,
"MobilityBuildingInfo has not been aggregated to this node mobility model"
);
215
BuildingsHelper::MakeConsistent
(mm);
216
}
217
}
218
219
TypeId
220
SameRoomPositionAllocator::GetTypeId
(
void
)
221
{
222
static
TypeId
tid =
TypeId
(
"ns3::SameRoomPositionAllocator"
)
223
.
SetParent
<
PositionAllocator
> ()
224
.SetGroupName (
"Mobility"
)
225
.AddConstructor<
SameRoomPositionAllocator
> ();
226
return
tid;
227
}
228
229
Vector
230
SameRoomPositionAllocator::GetNext
()
const
231
{
232
NS_LOG_FUNCTION
(
this
);
233
if
(
m_nodeIt
==
m_nodes
.
End
())
234
{
235
m_nodeIt
=
m_nodes
.
Begin
();
236
}
237
238
NS_ASSERT_MSG
(
m_nodeIt
!=
m_nodes
.
End
(),
"no node in container"
);
239
240
NS_LOG_LOGIC
(
"considering node "
<< (*m_nodeIt)->GetId ());
241
Ptr<MobilityModel>
mm = (*m_nodeIt)->
GetObject
<
MobilityModel
> ();
242
NS_ASSERT_MSG
(mm,
"no mobility model aggregated to this node"
);
243
Ptr<MobilityBuildingInfo>
bmm = mm->GetObject<
MobilityBuildingInfo
> ();
244
NS_ASSERT_MSG
(bmm,
"MobilityBuildingInfo has not been aggregated to this node mobility model"
);
245
246
++
m_nodeIt
;
247
uint32_t roomx = bmm->GetRoomNumberX ();
248
uint32_t roomy = bmm->GetRoomNumberY ();
249
uint32_t floor = bmm->GetFloorNumber ();
250
NS_LOG_LOGIC
(
"considering building "
<< bmm->GetBuilding ()->GetId () <<
" room ("
<< roomx <<
", "
<< roomy <<
", "
<< floor <<
")"
);
251
252
Ptr<Building>
b = bmm->GetBuilding ();
253
Ptr<RandomBoxPositionAllocator>
pa = CreateObject<RandomBoxPositionAllocator> ();
254
BoxValue
bv;
255
b->GetAttribute (
"Boundaries"
, bv);
256
Box
box = bv.Get ();
257
double
rdx = (box.
xMax
- box.
xMin
) / b->GetNRoomsX ();
258
double
rdy = (box.
yMax
- box.
yMin
) / b->GetNRoomsY ();
259
double
rdz = (box.
zMax
- box.
zMin
) / b->GetNFloors ();
260
double
x1 = box.
xMin
+ rdx * (roomx - 1);
261
double
x2 = box.
xMin
+ rdx * roomx;
262
double
y1 = box.
yMin
+ rdy * (roomy -1);
263
double
y2 = box.
yMin
+ rdy * roomy;
264
double
z1 = box.
zMin
+ rdz * (floor - 1);
265
double
z2 = box.
zMin
+ rdz * floor;
266
NS_LOG_LOGIC
(
"randomly allocating position in "
267
<<
" ("
<< x1 <<
","
<< x2 <<
") "
268
<<
"x ("
<< y1 <<
","
<< y2 <<
") "
269
<<
"x ("
<< z1 <<
","
<< z2 <<
") "
);
270
271
double
x
=
m_rand
->
GetValue
(x1, x2);
272
double
y =
m_rand
->
GetValue
(y1, y2);
273
double
z =
m_rand
->
GetValue
(z1, z2);
274
275
return
Vector
(x, y, z);
276
}
277
278
int64_t
279
SameRoomPositionAllocator::AssignStreams
(int64_t stream)
280
{
281
m_rand
->
SetStream
(stream);
282
return
1;
283
}
284
285
286
287
}
// namespace ns3
src
buildings
helper
building-position-allocator.cc
Generated on Fri Aug 30 2013 01:42:45 for ns-3 by
1.8.1.2