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

(-)a/src/buildings/helper/building-position-allocator.cc (+118 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 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 ("MaxNumAttempts",
139
                   "Maximum number of attempts 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_ASSERT_MSG (BuildingList::GetNBuildings () > 0, "no building found");
168
169
  bool outdoor = false;
170
  uint32_t attempts = 0;
171
  Vector pos = 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
    pos = Vector (x, y, z);
181
182
    NS_LOG_INFO("pos " << pos);
183
184
    bool inside = false;
185
    for (BuildingList::Iterator bit = BuildingList::Begin (); bit != BuildingList::End (); ++bit)
186
    {
187
      if((*bit)->IsInside (pos))
188
      {
189
        NS_LOG_INFO("Pos " << pos << " inside building " << (*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 " << pos);
205
      outdoor = true;
206
    }
207
  }
208
209
  NS_ASSERT_MSG(attempts < m_maxAttempts, "Too many attempts, give up");
210
  NS_ASSERT_MSG(outdoor, "Still indoor, give up");
211
  return pos;
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
106
224
107
NS_OBJECT_ENSURE_REGISTERED (RandomRoomPositionAllocator);
225
NS_OBJECT_ENSURE_REGISTERED (RandomRoomPositionAllocator);
108
226
(-)a/src/buildings/helper/building-position-allocator.h (+53 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 68-73   private: Link Here 
68
70
69
71
70
/**
72
/**
73
 * Allocate each position outside of existing buildings
74
 * 
75
 */
76
class OutdoorPositionAllocator : public PositionAllocator
77
{
78
public:
79
  OutdoorPositionAllocator ();
80
81
  // inherited from Object
82
  static TypeId GetTypeId (void);
83
84
  // inherited from PositionAllocator
85
  virtual Vector GetNext (void) const;
86
87
  /**
88
   * \brief Set the random variable stream object that generates x-positions
89
   * \param x pointer to a RandomVariableStream object
90
   */
91
  void SetX (Ptr<RandomVariableStream> x);
92
  /**
93
   * \brief Set the random variable stream object that generates y-positions
94
   * \param y pointer to a RandomVariableStream object
95
   */
96
  void SetY (Ptr<RandomVariableStream> y);
97
  /**
98
   * \brief Set the random variable stream object that generates z-positions
99
   * \param z pointer to a RandomVariableStream object
100
   */
101
  void SetZ (Ptr<RandomVariableStream> z);
102
103
  /**
104
   * Assign a fixed random variable stream number to the random variables
105
   * used by this model.  Return the number of streams (possibly zero) that
106
   * have been assigned.
107
   *
108
   * \param stream first stream index to use
109
   * \return the number of stream indices assigned by this model
110
   */
111
  int64_t AssignStreams (int64_t stream);
112
113
private:
114
  Ptr<RandomVariableStream> m_x; //!< pointer to x's random variable stream
115
  Ptr<RandomVariableStream> m_y; //!< pointer to y's random variable stream
116
  Ptr<RandomVariableStream> m_z; //!< pointer to z's random variable stream
117
118
  uint32_t m_maxAttempts; //!< maximum number of attempts before giving up
119
};
120
121
122
123
/**
71
 * Allocate each position by randomly chosing a room from the list
124
 * Allocate each position by randomly chosing a room from the list
72
 * of all buildings, and then randomly chosing a position inside the room.
125
 * of all buildings, and then randomly chosing a position inside the room.
73
 * The selection of the room is always done without replacement.
126
 * The selection of the room is always done without replacement.

Return to bug 2907