A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
hex-grid-position-allocator.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 *
15 * Author: Davide Magrin <magrinda@dei.unipd.it>
16 */
17
19
20#include "ns3/double.h"
21#include "ns3/log.h"
22
23namespace ns3
24{
25
26NS_LOG_COMPONENT_DEFINE("HexGridPositionAllocator");
27
28NS_OBJECT_ENSURE_REGISTERED(HexGridPositionAllocator);
29
30TypeId
32{
33 static TypeId tid = TypeId("ns3::HexGridPositionAllocator")
35 .AddConstructor<HexGridPositionAllocator>()
36 .SetGroupName("Lora")
37 .AddAttribute("Radius",
38 "The radius of a single hexagon",
39 DoubleValue(6000),
41 MakeDoubleChecker<double>());
42
43 return tid;
44}
45
47 : m_radius(6000)
48{
50
51 // Create the first position
52 m_positions.emplace_back(0.0, 0.0, 0.0);
53
54 // Add rings
55 for (int i = 0; i < 20; i++)
56 {
58 }
59
60 // Set the iterator
61 m_next = m_positions.begin();
62}
63
65 : m_radius(radius)
66{
68
69 // Create the first position
70 m_positions.emplace_back(0.0, 0.0, 0.0);
71
72 // Add a couple rings
73 // Add rings
74 for (int i = 0; i < 20; i++)
75 {
77 }
78
79 // Set the iterator
80 m_next = m_positions.begin();
81}
82
84{
86}
87
88const double HexGridPositionAllocator::pi = std::acos(-1);
89
90double
92{
93 return m_radius;
94}
95
96void
98{
99 m_radius = radius;
100}
101
102Vector
104{
105 // TODO: Check that there is a next element
106 Vector position = *m_next;
107 m_next++;
108 return position;
109}
110
111int64_t
113{
114 return 0;
115}
116
117std::vector<Vector>
118HexGridPositionAllocator::AddRing(std::vector<Vector> positions)
119{
120 NS_LOG_FUNCTION(this);
121
122 // Make a copy of the vector
123 std::vector<Vector> copy = positions;
124
125 // Iterate on the given vector
126 for (auto it = positions.begin(); it != positions.end(); it++)
127 {
128 // Get the current position
129 Vector currentPosition = *it;
130 NS_LOG_DEBUG("Current position " << currentPosition);
131
132 // Iterate to create the 6 surrounding positions
133 // The angle is with respect to a vertical line
134 Vector newPosition;
135 for (double angle = 0; angle < 2 * pi; angle += pi / 3)
136 {
137 newPosition = Vector(currentPosition.x + 2 * m_radius * std::sin(angle),
138 currentPosition.y + 2 * m_radius * std::cos(angle),
139 currentPosition.z);
140 NS_LOG_DEBUG("New position: " << newPosition);
141
142 // If the newly created position is not already in the copy, add it
143 bool found = false;
144 for (auto it = copy.begin(); it != copy.end(); it++)
145 {
146 // If the vector is already in the vector
147 // 1 is an EPSILON used to determine whether two floats are equal
148 if (CalculateDistance(newPosition, *it) < 10)
149 {
150 found = true;
151 break;
152 }
153 }
154 if (!found)
155 {
156 NS_LOG_DEBUG("Adding position " << newPosition);
157 copy.push_back(newPosition);
158 }
159 }
160 }
161 return copy;
162}
163} // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this model.
void SetRadius(double radius)
Set the radius of the circle inscribed in the hexagonal tiles.
static TypeId GetTypeId()
Register this type.
std::vector< Vector > AddRing(std::vector< Vector > positions)
This method adds to the given list of positions an outer ring of positions.
double GetRadius() const
Get the radius of the circle inscribed in the hexagonal tiles.
double m_radius
The radius of a cell (defined as the half the distance between two adjacent nodes,...
std::vector< Vector > m_positions
The current list of positions.
std::vector< Vector >::const_iterator m_next
The iterator pointing to the next position to return.
Allocate a set of positions.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:109
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43