-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeightMap.cpp
More file actions
192 lines (153 loc) · 4.57 KB
/
Copy pathHeightMap.cpp
File metadata and controls
192 lines (153 loc) · 4.57 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "HeightMap.h"
////////////////////////////////////////////////////////////
HeightMap::HeightMap(unsigned t_w, unsigned t_h, const std::vector<double>& t_values) :
m_height{ t_h }, m_width{ t_w }, m_heights{ t_values }{
if (m_heights.size() > t_w* t_h) {
while (m_heights.size() > t_w* t_h)
m_heights.pop_back();
}
else if (m_heights.size() < t_w * t_h) {
unsigned delta{ t_w * t_h - (unsigned)m_heights.size() };
m_heights.reserve(t_w * t_h);
for (unsigned i{ 0 }; i < delta; i++)
m_heights.emplace_back(0.0);
}
}
////////////////////////////////////////////////////////////
HeightMap::HeightMap(unsigned t_w, unsigned t_h) :
m_height{ t_h }, m_width{ t_w }, m_heights((size_t)(t_w* t_h), 0.0){}
////////////////////////////////////////////////////////////
bool HeightMap::compareDimensions(const HeightMap& t_hm) const {
return m_width == t_hm.m_width && m_height == t_hm.m_height;
}
////////////////////////////////////////////////////////////
double HeightMap::mean() const {
double sum{ 0 };
for (const auto& val : m_heights) {
sum += val;
}
return sum / (m_width * m_height);
}
////////////////////////////////////////////////////////////
double HeightMap::standtDev() const {
double ave{ mean() };
double sum{ 0 };
for (const auto& val : m_heights) {
sum += (val - ave) * (val - ave);
}
return sqrt(sum / (m_width * m_height));
}
////////////////////////////////////////////////////////////
double HeightMap::getValue(unsigned t_x, unsigned t_y) const {
return m_heights[t_y * m_width + t_x];
}
////////////////////////////////////////////////////////////
void HeightMap::setValue(unsigned t_x, unsigned t_y, double t_n) {
m_heights[t_y * m_width + t_x] = t_n;
}
////////////////////////////////////////////////////////////
HeightMap& HeightMap::sediment(const HeightMap& t_hm) {
if (compareDimensions(t_hm)) {
unsigned size{ (unsigned)m_heights.size() };
for (unsigned i{ 0 }; i < size; i++) {
m_heights[i] += t_hm.m_heights[i];
}
}
return *this;
}
////////////////////////////////////////////////////////////
HeightMap& HeightMap::merge(const HeightMap t_hm) {
if (compareDimensions(t_hm)) {
unsigned size{ (unsigned)m_heights.size() };
for (unsigned i{ 0 }; i < size; i++) {
m_heights[i] = std::max(m_heights[i], t_hm.m_heights[i]);
}
}
return *this;
}
////////////////////////////////////////////////////////////
HeightMap& HeightMap::increment(double t_n) {
for (auto& val : m_heights) {
val += t_n;
}
return *this;
}
////////////////////////////////////////////////////////////
double HeightMap::getMax()const {
double max{ -INFINITY };
for (const auto& val : m_heights) {
if (val > max) {
max = val;
}
}
return max;
}
////////////////////////////////////////////////////////////
double HeightMap::getMin()const {
double min{ INFINITY };
for (const auto& val : m_heights) {
if (val < min) {
min = val;
}
}
return min;
}
////////////////////////////////////////////////////////////
std::pair<double, double> HeightMap::getRange() const {
double min{ INFINITY };
double max{ -INFINITY };
for (const auto& val : m_heights) {
if (val < min) {
min = val;
}
else if (val > max) {
max = val;
}
}
return std::move(std::make_pair(min, max));
}
////////////////////////////////////////////////////////////
HeightMap& HeightMap::multiply(double t_n) {
for (auto& val : m_heights) {
val *= t_n;
}
return *this;
}
////////////////////////////////////////////////////////////
HeightMap& HeightMap::clamp(double t_min, double t_max) {
double min{ std::min(t_min, t_max) };
double max{ std::max(t_min, t_max) };
for (auto& val : m_heights) {
if (val < min) {
val = min;
}
else if (val > max) {
val = max;
}
}
return *this;
}
////////////////////////////////////////////////////////////
HeightMap& HeightMap::mapValuesToRange(double t_min, double t_max) {
auto range{ getRange() };
for (auto& val : m_heights) {
val = t_min + (val - range.first) * (t_max - t_min) / (range.second - range.first);
}
return *this;
}
////////////////////////////////////////////////////////////
std::vector<double>::const_iterator HeightMap::cbegin() const {
return m_heights.cbegin();
}
////////////////////////////////////////////////////////////
std::vector<double>::const_iterator HeightMap::cend() const {
return m_heights.cend();
}
////////////////////////////////////////////////////////////
std::vector<double>::iterator HeightMap::begin() {
return m_heights.begin();
}
////////////////////////////////////////////////////////////
std::vector<double>::iterator HeightMap::end() {
return m_heights.end();
}