A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
hex-grid-position-allocator.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Author: Davide Magrin <magrinda@dei.unipd.it>
5 */
6
8
9#include "ns3/double.h"
10#include "ns3/log.h"
11
12namespace ns3
13{
14
15NS_LOG_COMPONENT_DEFINE("HexGridPositionAllocator");
16
17NS_OBJECT_ENSURE_REGISTERED(HexGridPositionAllocator);
18
19TypeId
21{
22 static TypeId tid = TypeId("ns3::HexGridPositionAllocator")
24 .AddConstructor<HexGridPositionAllocator>()
25 .SetGroupName("Lora")
26 .AddAttribute("Radius",
27 "The radius of a single hexagon",
28 DoubleValue(6000),
31
32 return tid;
33}
34
36 : m_radius(6000)
37{
39
40 // Create the first position
41 m_positions.emplace_back(0.0, 0.0, 0.0);
42
43 // Add rings
44 for (int i = 0; i < 20; i++)
45 {
47 }
48
49 // Set the iterator
50 m_next = m_positions.begin();
51}
52
54 : m_radius(radius)
55{
57
58 // Create the first position
59 m_positions.emplace_back(0.0, 0.0, 0.0);
60
61 // Add a couple rings
62 // Add rings
63 for (int i = 0; i < 20; i++)
64 {
66 }
67
68 // Set the iterator
69 m_next = m_positions.begin();
70}
71
76
77const double HexGridPositionAllocator::pi = std::acos(-1);
78
79double
84
85void
87{
88 m_radius = radius;
89}
90
91Vector
93{
94 // TODO: Check that there is a next element
95 Vector position = *m_next;
96 m_next++;
97 return position;
98}
99
100int64_t
102{
103 return 0;
104}
105
106std::vector<Vector>
107HexGridPositionAllocator::AddRing(std::vector<Vector> positions)
108{
109 NS_LOG_FUNCTION(this);
110
111 // Make a copy of the vector
112 std::vector<Vector> copy = positions;
113
114 // Iterate on the given vector
115 for (auto it = positions.begin(); it != positions.end(); it++)
116 {
117 // Get the current position
118 Vector currentPosition = *it;
119 NS_LOG_DEBUG("Current position " << currentPosition);
120
121 // Iterate to create the 6 surrounding positions
122 // The angle is with respect to a vertical line
123 Vector newPosition;
124 for (double angle = 0; angle < 2 * pi; angle += pi / 3)
125 {
126 newPosition = Vector(currentPosition.x + 2 * m_radius * std::sin(angle),
127 currentPosition.y + 2 * m_radius * std::cos(angle),
128 currentPosition.z);
129 NS_LOG_DEBUG("New position: " << newPosition);
130
131 // If the newly created position is not already in the copy, add it
132 bool found = false;
133 for (auto it = copy.begin(); it != copy.end(); it++)
134 {
135 // If the vector is already in the vector
136 // 1 is an EPSILON used to determine whether two floats are equal
137 if (CalculateDistance(newPosition, *it) < 10)
138 {
139 found = true;
140 break;
141 }
142 }
143 if (!found)
144 {
145 NS_LOG_DEBUG("Adding position " << newPosition);
146 copy.push_back(newPosition);
147 }
148 }
149 }
150 return copy;
151}
152} // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
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:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition vector.cc:98
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition double.h:32