-
Notifications
You must be signed in to change notification settings - Fork 3
/
simulation-area.cc
119 lines (102 loc) · 3.91 KB
/
simulation-area.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/// \file grid-divider.cc
/// \author Keefer Rourke <krourke@uoguelph.ca>
///
/// Copyright (c) 2020 by Keefer Rourke <krourke@uoguelph.ca>
/// Permission to use, copy, modify, and/or distribute this software for any
/// purpose with or without fee is hereby granted, provided that the above
/// copyright notice and this permission notice appear in all copies.
///
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
/// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
/// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
/// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
/// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
/// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
/// PERFORMANCE OF THIS SOFTWARE.
///
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include "ns3/core-module.h"
#include "ns3/mobility-model.h"
#include "ns3/rectangle.h"
#include "simulation-area.h"
namespace rhpman {
ns3::Rectangle SimulationArea::asRectangle() const {
return ns3::Rectangle(this->minX(), this->maxX(), this->minY(), this->maxY());
}
std::vector<SimulationArea> SimulationArea::divideHorizontally(int32_t parts) const {
const double maxX = m_max.first;
const double minX = m_min.first;
const double delta = maxX - minX;
const double size = delta / parts;
std::vector<SimulationArea> dividers(parts);
double currentMin = minX;
double currentMax = minX + size;
for (int i = 0; i < parts; i++) {
dividers[i] = SimulationArea(
std::pair<double, double>(currentMin, m_min.second),
std::pair<double, double>(currentMax, m_max.second));
currentMin = currentMax;
currentMax += size;
}
return dividers;
}
std::vector<SimulationArea> SimulationArea::divideVertically(int32_t parts) const {
const double maxY = m_max.second;
const double minY = m_min.second;
const double delta = maxY - minY;
const double size = delta / parts;
std::vector<SimulationArea> dividers(parts);
double currentMin = minY;
double currentMax = minY + size;
for (int i = 0; i < parts; i++) {
dividers[i] = SimulationArea(
std::pair<double, double>(m_min.first, currentMin),
std::pair<double, double>(m_max.first, currentMax));
currentMin = currentMax;
currentMax += size;
}
return dividers;
}
std::vector<SimulationArea> SimulationArea::splitIntoGrid(int32_t x, int32_t y) const {
auto strata = this->divideHorizontally(x);
std::vector<SimulationArea> grid;
for (auto stratum : strata) {
auto boxes = stratum.divideVertically(y);
for (auto box : boxes) {
grid.push_back(box);
}
}
return grid;
}
ns3::Ptr<ns3::GridPositionAllocator> SimulationArea::getGridPositionAllocator() const {
auto alloc = ns3::CreateObject<ns3::GridPositionAllocator>();
alloc->SetMinX(this->minX());
alloc->SetMinY(this->minY());
alloc->SetDeltaX(this->deltaX());
alloc->SetDeltaY(this->deltaY());
return alloc;
}
ns3::Ptr<ns3::RandomRectanglePositionAllocator> SimulationArea::
getRandomRectanglePositionAllocator() const {
ns3::Ptr<ns3::UniformRandomVariable> x = ns3::CreateObject<ns3::UniformRandomVariable>();
x->SetAttribute("Min", ns3::DoubleValue(this->minX()));
x->SetAttribute("Max", ns3::DoubleValue(this->maxX()));
ns3::Ptr<ns3::UniformRandomVariable> y = ns3::CreateObject<ns3::UniformRandomVariable>();
y->SetAttribute("Min", ns3::DoubleValue(this->minY()));
y->SetAttribute("Max", ns3::DoubleValue(this->maxY()));
auto alloc = ns3::CreateObject<ns3::RandomRectanglePositionAllocator>();
alloc->SetX(x);
alloc->SetY(y);
return alloc;
}
std::string SimulationArea::toString() const {
std::stringstream ss;
ss << "{(" << m_min.first << "," << m_min.second << "),(" << m_max.first << "," << m_max.second
<< ")}";
return ss.str();
}
} // namespace rhpman