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

(-)a/src/buildings/helper/building-position-allocator.cc (+117 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 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
(-)a/src/buildings/helper/building-position-allocator.h (+58 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
#ifndef BUILDING_POSITION_ALLOCATOR_H
21
#ifndef BUILDING_POSITION_ALLOCATOR_H
21
#define BUILDING_POSITION_ALLOCATOR_H
22
#define BUILDING_POSITION_ALLOCATOR_H
 Lines 66-71   private: Link Here 
66
  Ptr<UniformRandomVariable> m_rand;
67
  Ptr<UniformRandomVariable> m_rand;
67
};
68
};
68
69
70
/**
71
 * \ingroup buildings
72
 * \brief allocate outdoor positions
73
 *
74
 * Allocate positions outside of existing buildings using rejection sampling.
75
 * This class extracts a random position in a box defined by the three RandomVariableStreams
76
 * for the X, Y and Z dimensions (similarly to RandomBoxPositionAllocator), until
77
 * a position which is outdoor with respect to all the buildings in the scenario is found,
78
 * or a maximum number of attempts is reached.
79
 * The RandomVariableStream and the maximum number of attempts can be set using attributes.
80
 * If the maximum number of attempts is reached, then the simulation aborts.
81
 */
82
class OutdoorPositionAllocator : public PositionAllocator
83
{
84
public:
85
  OutdoorPositionAllocator ();
86
87
  // inherited from Object
88
  static TypeId GetTypeId (void);
89
90
  // inherited from PositionAllocator
91
  virtual Vector GetNext (void) const;
92
93
  /**
94
   * \brief Set the random variable stream object that generates x-positions
95
   * \param x pointer to a RandomVariableStream object
96
   */
97
  void SetX (Ptr<RandomVariableStream> x);
98
  /**
99
   * \brief Set the random variable stream object that generates y-positions
100
   * \param y pointer to a RandomVariableStream object
101
   */
102
  void SetY (Ptr<RandomVariableStream> y);
103
  /**
104
   * \brief Set the random variable stream object that generates z-positions
105
   * \param z pointer to a RandomVariableStream object
106
   */
107
  void SetZ (Ptr<RandomVariableStream> z);
108
109
  /**
110
   * Assign a fixed random variable stream number to the random variables
111
   * used by this model.  Return the number of streams (possibly zero) that
112
   * have been assigned.
113
   *
114
   * \param stream first stream index to use
115
   * \return the number of stream indices assigned by this model
116
   */
117
  int64_t AssignStreams (int64_t stream);
118
119
private:
120
  Ptr<RandomVariableStream> m_x; //!< pointer to x's random variable stream
121
  Ptr<RandomVariableStream> m_y; //!< pointer to y's random variable stream
122
  Ptr<RandomVariableStream> m_z; //!< pointer to z's random variable stream
123
124
  uint32_t m_maxAttempts; //!< maximum number of attempts before giving up
125
};
126
69
127
70
/**
128
/**
71
 * Allocate each position by randomly chosing a room from the list
129
 * Allocate each position by randomly chosing a room from the list

Return to bug 2907