# HG changeset patch # User Tommaso Pecorella # Date 1470614209 -7200 # Parent 98eda94a45d061ffa923e0d2c13ff86d5ddf0569 DiscGridPostionAllocator diff --git a/src/mobility/model/disc-grid-position-allocator.cc b/src/mobility/model/disc-grid-position-allocator.cc new file mode 100644 --- /dev/null +++ b/src/mobility/model/disc-grid-position-allocator.cc @@ -0,0 +1,173 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Andrea Sacco + */ +#include "disc-grid-position-allocator.h" +#include "ns3/log.h" +#include + +namespace ns3 { + +NS_OBJECT_ENSURE_REGISTERED (DiskGridPositionAllocator); + +TypeId +DiskGridPositionAllocator::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::DiskGridPositionAllocator") + .SetParent () + .SetGroupName ("Mobility") + .AddConstructor () + ; + return tid; +} +DiskGridPositionAllocator::DiskGridPositionAllocator () + : m_x (0), + m_y (0), + m_z (0), + m_rho (0), + m_intervals (0), + m_count (0) +{ +} +DiskGridPositionAllocator::~DiskGridPositionAllocator () +{ +} +void +DiskGridPositionAllocator::SetRho (double rho) +{ + m_rho = rho; + CalculatePositions (); +} +void +DiskGridPositionAllocator::SetX (double x) +{ + m_x = x; + CalculatePositions (); +} +void +DiskGridPositionAllocator::SetY (double y) +{ + m_y = y; + CalculatePositions (); +} +void +DiskGridPositionAllocator::SetZ (double z) +{ + m_z = z; + CalculatePositions (); +} +void +DiskGridPositionAllocator::SetIntervals (uint32_t intervals) +{ + m_intervals = intervals; + CalculatePositions (); +} +double +DiskGridPositionAllocator::GetRho () const +{ + return m_rho; +} +double +DiskGridPositionAllocator::GetX () const +{ + return m_x; +} +double +DiskGridPositionAllocator::GetY () const +{ + return m_y; +} +double +DiskGridPositionAllocator::GetZ () const +{ + return m_z; +} +uint32_t +DiskGridPositionAllocator::GetIntervals () const +{ + return m_intervals; +} +int64_t +DiskGridPositionAllocator::AssignStreams (int64_t stream) +{ + return 0; +} +Vector +DiskGridPositionAllocator::GetNext (void) const +{ + static uint32_t count = m_count; + NS_ASSERT (count > 0); + Vector v = *m_current; + m_current++; + count--; + return v; +} +uint32_t +DiskGridPositionAllocator::GetN () const +{ + return m_count; +} +void +DiskGridPositionAllocator::CalculatePositions () +{ + uint32_t i; + double x; + double y; + + if (m_intervals == 0 || m_rho == 0) + { + return; + } + m_positions.clear (); + m_count = 0; + for ( uint32_t j = 0; j <= m_intervals; j++ ) + { + i = 0; + x = m_x; + y = m_y + m_rho * ( double ) ( 2 * j ) / ( double ) ( 2 * m_intervals + 1 ); + m_positions.push_back (Vector (x, y, m_z)); + m_count++; + + if ( 0 < j ) + { + m_positions.push_back (Vector (x, (2.0 * m_y - y), m_z)); + m_count++; + } + while (true) + { + i = i + 1; + x = m_x + m_rho * ( double ) ( 2 * i ) / ( double ) ( 2 * m_intervals + 1 ); + + if ( m_rho * m_rho < pow ( x - m_x, 2 ) + pow ( y - m_y, 2 ) ) + { + break; + } + m_positions.push_back (Vector (x, y, m_z)); + m_positions.push_back (Vector ((2.0 * m_x - x), y, m_z)); + m_count += 2; + if ( 0 < j ) + { + m_positions.push_back (Vector (x, (2.0 * m_y - y), m_z)); + m_positions.push_back (Vector ((2.0 * m_x - x), (2.0 * m_y - y), m_z)); + m_count += 2; + } + } + } + m_current = m_positions.begin (); +} + + +} // namespace ns3 diff --git a/src/mobility/model/disc-grid-position-allocator.h b/src/mobility/model/disc-grid-position-allocator.h new file mode 100644 --- /dev/null +++ b/src/mobility/model/disc-grid-position-allocator.h @@ -0,0 +1,124 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Andrea Sacco + */ +#ifndef AUV_POSITION_ALLOCATOR_H +#define AUV_POSITION_ALLOCATOR_H + +#include "ns3/position-allocator.h" + +namespace ns3 { + +/** + * \ingroup mobility + * \brief Allocate positions on a rectangular 2d grid + * delimited by a circle. + */ +class DiskGridPositionAllocator : public PositionAllocator +{ +public: + /** + * Register this type with the TypeId system. + * \return the object TypeId + */ + static TypeId GetTypeId (void); + DiskGridPositionAllocator (); + virtual ~DiskGridPositionAllocator (); + + /** + * \brief Set the radius of the disc delimiting the grid. + * \param rho The radius of the disc. + */ + void SetRho (double rho); + /** + * \brief Set the X coordinate of the center of the disc. + * \param x The X coordinate of the center of the disc. + */ + void SetX (double x); + /** + * \brief Set the Y coordinate of the center of the disc. + * \param y The Y coordinate of the center of the disc. + */ + void SetY (double y); + /** + * \brief Set the Z coordinate of the center of the disc. + * \param z The Z coordinate of the center of the disc. + */ + void SetZ (double z); + /** + * \brief Set the number of subintervals into which the horizontal + * radius should be divided. + * \param intervals The number of subintervals into which the horizontal + * radius should be divided. + */ + void SetIntervals (uint32_t intervals); + /** + * \brief Get the radius of the disc delimiting the grid. + * \return The radius of the disc. + */ + double GetRho () const; + /** + * \brief Get the X coordinate of the center of the disc. + * \return the X coordinate of the center of the disc. + */ + double GetX () const; + /** + * \brief Get the Y coordinate of the center of the disc. + * \return The Y coordinate of the center of the disc. + */ + double GetY () const; + /** + * \brief Get the Z coordinate of the center of the disc. + * \return The Z coordinate of the center of the disc. + */ + double GetZ () const; + /** + * \brief Get the number of subintervals into which the horizontal + * radius should be divided. + * \return The number of subintervals into which the horizontal + * radius should be divided. + */ + uint32_t GetIntervals () const; + + /** + * \brief Get the number of points in the grid. + * \return The number of points in the grid. + */ + uint32_t GetN () const; + + virtual Vector GetNext (void) const; + virtual int64_t AssignStreams (int64_t stream); + +private: + /** + * \brief calculates the positions + */ + void CalculatePositions (); + +private: + std::vector m_positions; //!< vector of positions + mutable std::vector::const_iterator m_current; //!< vector iterator + double m_x; //!< x coordinate of center of disc + double m_y; //!< y coordinate of center of disc + double m_z; //!< z coordinate of center of disc + double m_rho; //!< value of the radius of the disc + uint32_t m_intervals; //!< number of subintervals + uint32_t m_count; //!< number of points +}; + +} // namespace ns3 + +#endif /* AUV_POSITION_ALLOCATOR_H */ diff --git a/src/mobility/wscript b/src/mobility/wscript --- a/src/mobility/wscript +++ b/src/mobility/wscript @@ -13,6 +13,7 @@ 'model/hierarchical-mobility-model.cc', 'model/mobility-model.cc', 'model/position-allocator.cc', + 'model/disc-grid-position-allocator.cc', 'model/random-direction-2d-mobility-model.cc', 'model/random-walk-2d-mobility-model.cc', 'model/random-waypoint-mobility-model.cc', @@ -48,6 +49,7 @@ 'model/hierarchical-mobility-model.h', 'model/mobility-model.h', 'model/position-allocator.h', + 'model/disc-grid-position-allocator.h', 'model/rectangle.h', 'model/random-direction-2d-mobility-model.h', 'model/random-walk-2d-mobility-model.h',