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
11namespace ns3
12{
13
14NS_LOG_COMPONENT_DEFINE("HexGridPositionAllocator");
15
17
20{
21 static TypeId tid = TypeId("ns3::HexGridPositionAllocator")
23 .AddConstructor<HexGridPositionAllocator>()
24 .SetGroupName("Lora")
25 .AddAttribute("Radius",
26 "The radius of a single hexagon",
27 DoubleValue(6000),
30
31 return tid;
32}
33
35 : m_radius(6000)
36{
38
39 // Create the first position
40 m_positions.emplace_back(0.0, 0.0, 0.0);
41
42 // Add rings
43 for (int i = 0; i < 20; i++)
44 {
46 }
47
48 // Set the iterator
49 m_next = m_positions.begin();
50}
51
53 : m_radius(radius)
54{
56
57 // Create the first position
58 m_positions.emplace_back(0.0, 0.0, 0.0);
59
60 // Add a couple rings
61 // Add rings
62 for (int i = 0; i < 20; i++)
63 {
65 }
66
67 // Set the iterator
68 m_next = m_positions.begin();
69}
70
75
76const double HexGridPositionAllocator::pi = std::acos(-1);
77
78double
83
84void
86{
87 m_radius = radius;
88}
89
90Vector
92{
93 // TODO: Check that there is a next element
94 Vector position = *m_next;
95 m_next++;
96 return position;
97}
98
99int64_t
101{
102 return 0;
103}
104
105std::vector<Vector>
106HexGridPositionAllocator::AddRing(std::vector<Vector> positions)
107{
108 NS_LOG_FUNCTION(this);
109
110 // Make a copy of the vector
111 std::vector<Vector> copy = positions;
112
113 // Iterate on the given vector
114 for (auto it = positions.begin(); it != positions.end(); it++)
115 {
116 // Get the current position
117 Vector currentPosition = *it;
118 NS_LOG_DEBUG("Current position " << currentPosition);
119
120 // Iterate to create the 6 surrounding positions
121 // The angle is with respect to a vertical line
122 Vector newPosition;
123 for (double angle = 0; angle < 2 * pi; angle += pi / 3)
124 {
125 newPosition = Vector(currentPosition.x + 2 * m_radius * std::sin(angle),
126 currentPosition.y + 2 * m_radius * std::cos(angle),
127 currentPosition.z);
128 NS_LOG_DEBUG("New position: " << newPosition);
129
130 // If the newly created position is not already in the copy, add it
131 bool found = false;
132 for (auto it = copy.begin(); it != copy.end(); it++)
133 {
134 // If the vector is already in the vector
135 // 1 is an EPSILON used to determine whether two floats are equal
136 if (CalculateDistance(newPosition, *it) < 10)
137 {
138 found = true;
139 break;
140 }
141 }
142 if (!found)
143 {
144 NS_LOG_DEBUG("Adding position " << newPosition);
145 copy.push_back(newPosition);
146 }
147 }
148 }
149 return copy;
150}
151} // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
Position allocator for hexagonal tiling.
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.
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:260
#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:96
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition double.h:32