View | Details | Raw Unified | Return to bug 2907
Collapse All | Expand All

(-)a/src/buildings/helper/building-position-allocator.cc (-8 / +127 lines)
 Lines 16-21    Link Here 
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 26-31    Link Here 
26
#include "ns3/uinteger.h"
27
#include "ns3/uinteger.h"
27
#include "ns3/enum.h"
28
#include "ns3/enum.h"
28
#include "ns3/boolean.h"
29
#include "ns3/boolean.h"
30
#include "ns3/string.h"
31
#include "ns3/pointer.h"
29
#include "ns3/log.h"
32
#include "ns3/log.h"
30
#include "ns3/box.h"
33
#include "ns3/box.h"
31
#include "ns3/building.h"
34
#include "ns3/building.h"
 Lines 63-69   RandomBuildingPositionAllocator::GetTypeId (void) Link Here 
63
  return tid;
66
  return tid;
64
}
67
}
65
68
66
Vector 
69
Vector
67
RandomBuildingPositionAllocator::GetNext () const
70
RandomBuildingPositionAllocator::GetNext () const
68
{
71
{
69
  NS_ASSERT_MSG (BuildingList::GetNBuildings () > 0, "no building found");
72
  NS_ASSERT_MSG (BuildingList::GetNBuildings () > 0, "no building found");
 Lines 103-108   RandomBuildingPositionAllocator::AssignStreams (int64_t stream) Link Here 
103
  return 1;
106
  return 1;
104
}
107
}
105
108
109
NS_OBJECT_ENSURE_REGISTERED (OutdoorPositionAllocator);
110
111
OutdoorPositionAllocator::OutdoorPositionAllocator ()
112
{
113
  m_maxAttempts = 1000;
114
}
115
116
TypeId
117
OutdoorPositionAllocator::GetTypeId (void)
118
{
119
  static TypeId tid = TypeId ("ns3::OutdoorPositionAllocator")
120
    .SetParent<PositionAllocator> ()
121
    .SetGroupName ("Buildings")
122
    .AddConstructor<OutdoorPositionAllocator> ()
123
    .AddAttribute ("X",
124
                   "A random variable which represents the x coordinate of a position in a random box.",
125
                   StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
126
                   MakePointerAccessor (&OutdoorPositionAllocator::m_x),
127
                   MakePointerChecker<RandomVariableStream> ())
128
    .AddAttribute ("Y",
129
                   "A random variable which represents the y coordinate of a position in a random box.",
130
                   StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
131
                   MakePointerAccessor (&OutdoorPositionAllocator::m_y),
132
                   MakePointerChecker<RandomVariableStream> ())
133
    .AddAttribute ("Z",
134
                   "A random variable which represents the z coordinate of a position in a random box.",
135
                   StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
136
                   MakePointerAccessor (&OutdoorPositionAllocator::m_z),
137
                   MakePointerChecker<RandomVariableStream> ())
138
    .AddAttribute ("MaxAttempts",
139
                   "Maximum number of attempts for the rejection sampling before giving up.",
140
                   UintegerValue (1000),
141
                   MakeUintegerAccessor (&OutdoorPositionAllocator::m_maxAttempts),
142
                   MakeUintegerChecker<uint32_t> ())
143
  ;
144
145
  return tid;
146
}
147
148
void
149
OutdoorPositionAllocator::SetX (Ptr<RandomVariableStream> x)
150
{
151
  m_x = x;
152
}
153
void
154
OutdoorPositionAllocator::SetY (Ptr<RandomVariableStream> y)
155
{
156
  m_y = y;
157
}
158
void
159
OutdoorPositionAllocator::SetZ (Ptr<RandomVariableStream> z)
160
{
161
  m_z = z;
162
}
163
164
Vector
165
OutdoorPositionAllocator::GetNext () const
166
{
167
  NS_ABORT_MSG_IF (BuildingList::GetNBuildings () == 0, "no building found");
168
169
  bool outdoor = false;
170
  uint32_t attempts = 0;
171
  Vector position = Vector (0,0,0);
172
173
  while (!outdoor && attempts < m_maxAttempts)
174
    {
175
      // get a random position
176
      double x = m_x->GetValue ();
177
      double y = m_y->GetValue ();
178
      double z = m_z->GetValue ();
179
180
      position = Vector (x, y, z);
181
182
      NS_LOG_INFO ("Position " << position);
183
184
      bool inside = false;
185
      for (BuildingList::Iterator bit = BuildingList::Begin (); bit != BuildingList::End (); ++bit)
186
        {
187
          if ((*bit)->IsInside (position))
188
            {
189
              NS_LOG_INFO ("Position " << position << " is inside the building with boundaries "
190
                                       << (*bit)->GetBoundaries ().xMin << " " << (*bit)->GetBoundaries ().xMax << " "
191
                                       << (*bit)->GetBoundaries ().yMin << " " << (*bit)->GetBoundaries ().yMax << " "
192
                                       << (*bit)->GetBoundaries ().zMin << " " << (*bit)->GetBoundaries ().zMax);
193
              inside = true;
194
              break;
195
            }
196
        }
197
198
      if (inside)
199
        {
200
          NS_LOG_INFO ("Inside a building, attemp " << attempts << " out of " << m_maxAttempts);
201
          attempts++;
202
        }
203
      else
204
        {
205
          NS_LOG_INFO ("Outdoor position found " << position);
206
          outdoor = true;
207
        }
208
    }
209
210
  NS_ABORT_MSG_IF (attempts >= m_maxAttempts, "Too many attempts, give up");
211
  NS_ABORT_MSG_IF (!outdoor, "Still indoor, give up");
212
  return position;
213
}
214
215
int64_t
216
OutdoorPositionAllocator::AssignStreams (int64_t stream)
217
{
218
  m_x->SetStream (stream);
219
  m_y->SetStream (stream + 1);
220
  m_z->SetStream (stream + 2);
221
  return 3;
222
}
223
224
106
225
107
NS_OBJECT_ENSURE_REGISTERED (RandomRoomPositionAllocator);
226
NS_OBJECT_ENSURE_REGISTERED (RandomRoomPositionAllocator);
108
227
 Lines 122-133   RandomRoomPositionAllocator::GetTypeId (void) Link Here 
122
  return tid;
241
  return tid;
123
}
242
}
124
243
125
Vector 
244
Vector
126
RandomRoomPositionAllocator::GetNext () const
245
RandomRoomPositionAllocator::GetNext () const
127
{
246
{
128
  NS_LOG_FUNCTION (this);
247
  NS_LOG_FUNCTION (this);
129
  NS_ASSERT_MSG (BuildingList::GetNBuildings () > 0, "no building found");
248
  NS_ASSERT_MSG (BuildingList::GetNBuildings () > 0, "no building found");
130
 
249
131
  if (m_roomListWithoutReplacement.empty ())
250
  if (m_roomListWithoutReplacement.empty ())
132
    {
251
    {
133
      for (BuildingList::Iterator bit = BuildingList::Begin (); bit != BuildingList::End (); ++bit)
252
      for (BuildingList::Iterator bit = BuildingList::Begin (); bit != BuildingList::End (); ++bit)
 Lines 142-148   RandomRoomPositionAllocator::GetNext () const Link Here 
142
                      RoomInfo i;
261
                      RoomInfo i;
143
                      i.roomx = rx;
262
                      i.roomx = rx;
144
                      i.roomy = ry;
263
                      i.roomy = ry;
145
                      i.floor = f; 
264
                      i.floor = f;
146
                      i.b = *bit;
265
                      i.b = *bit;
147
                      NS_LOG_LOGIC ("adding room (" << rx << ", " << ry << ", " << f << ")");
266
                      NS_LOG_LOGIC ("adding room (" << rx << ", " << ry << ", " << f << ")");
148
                      m_roomListWithoutReplacement.push_back (i);
267
                      m_roomListWithoutReplacement.push_back (i);
 Lines 165-171   RandomRoomPositionAllocator::GetNext () const Link Here 
165
  double rdz =  (box.zMax - box.zMin) / r.b->GetNFloors ();
284
  double rdz =  (box.zMax - box.zMin) / r.b->GetNFloors ();
166
  double x1 = box.xMin + rdx * (r.roomx - 1);
285
  double x1 = box.xMin + rdx * (r.roomx - 1);
167
  double x2 = box.xMin + rdx * r.roomx;
286
  double x2 = box.xMin + rdx * r.roomx;
168
  double y1 = box.yMin + rdy * (r.roomy -1);
287
  double y1 = box.yMin + rdy * (r.roomy - 1);
169
  double y2 = box.yMin + rdy * r.roomy;
288
  double y2 = box.yMin + rdy * r.roomy;
170
  double z1 = box.zMin + rdz * (r.floor - 1);
289
  double z1 = box.zMin + rdz * (r.floor - 1);
171
  double z2 = box.zMin + rdz * r.floor;
290
  double z2 = box.zMin + rdz * r.floor;
 Lines 226-232   SameRoomPositionAllocator::GetTypeId (void) Link Here 
226
  return tid;
345
  return tid;
227
}
346
}
228
347
229
Vector 
348
Vector
230
SameRoomPositionAllocator::GetNext () const
349
SameRoomPositionAllocator::GetNext () const
231
{
350
{
232
  NS_LOG_FUNCTION (this);
351
  NS_LOG_FUNCTION (this);
 Lines 259-265   SameRoomPositionAllocator::GetNext () const Link Here 
259
  double rdz =  (box.zMax - box.zMin) / b->GetNFloors ();
378
  double rdz =  (box.zMax - box.zMin) / b->GetNFloors ();
260
  double x1 = box.xMin + rdx * (roomx - 1);
379
  double x1 = box.xMin + rdx * (roomx - 1);
261
  double x2 = box.xMin + rdx * roomx;
380
  double x2 = box.xMin + rdx * roomx;
262
  double y1 = box.yMin + rdy * (roomy -1);
381
  double y1 = box.yMin + rdy * (roomy - 1);
263
  double y2 = box.yMin + rdy * roomy;
382
  double y2 = box.yMin + rdy * roomy;
264
  double z1 = box.zMin + rdz * (floor - 1);
383
  double z1 = box.zMin + rdz * (floor - 1);
265
  double z2 = box.zMin + rdz * floor;
384
  double z2 = box.zMin + rdz * floor;
 Lines 322-328   FixedRoomPositionAllocator::GetNext () const Link Here 
322
  double rdz =  (box.zMax - box.zMin) / bptr->GetNFloors ();
441
  double rdz =  (box.zMax - box.zMin) / bptr->GetNFloors ();
323
  double x1 = box.xMin + rdx * (roomx - 1);
442
  double x1 = box.xMin + rdx * (roomx - 1);
324
  double x2 = box.xMin + rdx * roomx;
443
  double x2 = box.xMin + rdx * roomx;
325
  double y1 = box.yMin + rdy * (roomy -1);
444
  double y1 = box.yMin + rdy * (roomy - 1);
326
  double y2 = box.yMin + rdy * roomy;
445
  double y2 = box.yMin + rdy * roomy;
327
  double z1 = box.zMin + rdz * (floor - 1);
446
  double z1 = box.zMin + rdz * (floor - 1);
328
  double z2 = box.zMin + rdz * floor;
447
  double z2 = box.zMin + rdz * floor;
(-)a/src/buildings/helper/building-position-allocator.h (-23 / +76 lines)
 Lines 16-22    Link Here 
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
 */
21
20
#ifndef BUILDING_POSITION_ALLOCATOR_H
22
#ifndef BUILDING_POSITION_ALLOCATOR_H
21
#define BUILDING_POSITION_ALLOCATOR_H
23
#define BUILDING_POSITION_ALLOCATOR_H
22
24
 Lines 34-40   class UniformRandomVariable; Link Here 
34
/**
36
/**
35
 * Allocate each position by randomly chosing a building from the list
37
 * Allocate each position by randomly chosing a building from the list
36
 * of all buildings, and then randomly chosing a position inside the building.
38
 * of all buildings, and then randomly chosing a position inside the building.
37
 * 
39
 *
38
 */
40
 */
39
class RandomBuildingPositionAllocator : public PositionAllocator
41
class RandomBuildingPositionAllocator : public PositionAllocator
40
{
42
{
 Lines 58-64   public: Link Here 
58
  int64_t AssignStreams (int64_t stream);
60
  int64_t AssignStreams (int64_t stream);
59
61
60
private:
62
private:
61
62
  bool m_withReplacement;
63
  bool m_withReplacement;
63
  mutable std::vector< Ptr<Building> > m_buildingListWithoutReplacement;
64
  mutable std::vector< Ptr<Building> > m_buildingListWithoutReplacement;
64
65
 Lines 68-77   private: Link Here 
68
69
69
70
70
/**
71
/**
72
 * Allocate positions outside of existing buildings using rejection sampling.
73
 * This class extracts a random position in a box defined by the three RandomVariableStreams
74
 * for the X, Y and Z dimensions (similarly to RandomBoxPositionAllocator), until
75
 * a position which is outdoor with respect to all the buildings in the scenario is found,
76
 * or a maximum number of attempts is reached.
77
 * The RandomVariableStream and the maximum number of attempts can be set using attributes.
78
 * If the maximum number of attempts is reached, the simulation aborts.
79
 */
80
class OutdoorPositionAllocator : public PositionAllocator
81
{
82
public:
83
  OutdoorPositionAllocator ();
84
85
  // inherited from Object
86
  static TypeId GetTypeId (void);
87
88
  // inherited from PositionAllocator
89
  virtual Vector GetNext (void) const;
90
91
  /**
92
   * \brief Set the random variable stream object that generates x-positions
93
   * \param x pointer to a RandomVariableStream object
94
   */
95
  void SetX (Ptr<RandomVariableStream> x);
96
  /**
97
   * \brief Set the random variable stream object that generates y-positions
98
   * \param y pointer to a RandomVariableStream object
99
   */
100
  void SetY (Ptr<RandomVariableStream> y);
101
  /**
102
   * \brief Set the random variable stream object that generates z-positions
103
   * \param z pointer to a RandomVariableStream object
104
   */
105
  void SetZ (Ptr<RandomVariableStream> z);
106
107
  /**
108
   * Assign a fixed random variable stream number to the random variables
109
   * used by this model.  Return the number of streams (possibly zero) that
110
   * have been assigned.
111
   *
112
   * \param stream first stream index to use
113
   * \return the number of stream indices assigned by this model
114
   */
115
  int64_t AssignStreams (int64_t stream);
116
117
private:
118
  Ptr<RandomVariableStream> m_x; //!< pointer to x's random variable stream
119
  Ptr<RandomVariableStream> m_y; //!< pointer to y's random variable stream
120
  Ptr<RandomVariableStream> m_z; //!< pointer to z's random variable stream
121
122
  uint32_t m_maxAttempts; //!< maximum number of attempts before giving up
123
};
124
125
126
127
/**
71
 * Allocate each position by randomly chosing a room from the list
128
 * Allocate each position by randomly chosing a room from the list
72
 * of all buildings, and then randomly chosing a position inside the room.
129
 * of all buildings, and then randomly chosing a position inside the room.
73
 * The selection of the room is always done without replacement.
130
 * The selection of the room is always done without replacement.
74
 * 
131
 *
75
 */
132
 */
76
class RandomRoomPositionAllocator : public PositionAllocator
133
class RandomRoomPositionAllocator : public PositionAllocator
77
{
134
{
 Lines 84-102   public: Link Here 
84
  // inherited from PositionAllocator
141
  // inherited from PositionAllocator
85
  virtual Vector GetNext (void) const;
142
  virtual Vector GetNext (void) const;
86
143
87
 /**
144
  /**
88
  * Assign a fixed random variable stream number to the random variables
145
   * Assign a fixed random variable stream number to the random variables
89
  * used by this model.  Return the number of streams (possibly zero) that
146
   * used by this model.  Return the number of streams (possibly zero) that
90
  * have been assigned.
147
   * have been assigned.
91
  *
148
   *
92
  * \param stream first stream index to use
149
   * \param stream first stream index to use
93
  * \return the number of stream indices assigned by this model
150
   * \return the number of stream indices assigned by this model
94
  */
151
   */
95
  int64_t AssignStreams (int64_t stream);
152
  int64_t AssignStreams (int64_t stream);
96
153
97
private:
154
private:
98
155
  struct RoomInfo
99
  struct RoomInfo 
100
  {
156
  {
101
    Ptr<Building> b;
157
    Ptr<Building> b;
102
    uint32_t roomx;
158
    uint32_t roomx;
 Lines 139-145   public: Link Here 
139
  int64_t AssignStreams (int64_t);
195
  int64_t AssignStreams (int64_t);
140
196
141
private:
197
private:
142
143
  NodeContainer m_nodes;
198
  NodeContainer m_nodes;
144
  mutable NodeContainer::Iterator m_nodeIt;
199
  mutable NodeContainer::Iterator m_nodeIt;
145
200
 Lines 149-168   private: Link Here 
149
204
150
/**
205
/**
151
 * Generate a random position uniformly distributed in the volume of a
206
 * Generate a random position uniformly distributed in the volume of a
152
 * chosen room inside a chosen building.  
207
 * chosen room inside a chosen building.
153
 */
208
 */
154
class FixedRoomPositionAllocator : public PositionAllocator
209
class FixedRoomPositionAllocator : public PositionAllocator
155
{
210
{
156
public:
211
public:
157
212
  /**
158
  /** 
213
   *
159
   * 
214
   *
160
   * 
215
   * \param x index of the room on the x-axis
161
   * \param x index of the room on the x-axis 
216
   * \param y index of the room on the y-axis
162
   * \param y index of the room on the y-axis 
163
   * \param z index of the room on the z-axis (i.e., floor number)
217
   * \param z index of the room on the z-axis (i.e., floor number)
164
   * \param b pointer to the chosen building
218
   * \param b pointer to the chosen building
165
   * 
219
   *
166
   */
220
   */
167
  FixedRoomPositionAllocator (uint32_t x,
221
  FixedRoomPositionAllocator (uint32_t x,
168
                              uint32_t y,
222
                              uint32_t y,
 Lines 184-190   public: Link Here 
184
  int64_t AssignStreams (int64_t);
238
  int64_t AssignStreams (int64_t);
185
239
186
private:
240
private:
187
188
  uint32_t roomx;
241
  uint32_t roomx;
189
  uint32_t roomy;
242
  uint32_t roomy;
190
  uint32_t floor;
243
  uint32_t floor;

Return to bug 2907