Skip to content

Commit 83cd71a

Browse files
committed
Moved height map operations from MapGenerator into NodeMap.
1 parent cf6d813 commit 83cd71a

File tree

3 files changed

+99
-80
lines changed

3 files changed

+99
-80
lines changed

src/mapgenerator.cpp

Lines changed: 21 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -60,105 +60,35 @@ void gen::MapGenerator::normalize() {
6060
if (!_isInitialized) {
6161
throw std::runtime_error("MapGenerator must be initialized.");
6262
}
63-
64-
/*
65-
Normalize height map values to range [0, 1]
66-
*/
67-
double min = std::numeric_limits<double>::infinity();
68-
double max = -std::numeric_limits<double>::infinity();
69-
for (unsigned int i = 0; i < _heightMap.size(); i++) {
70-
min = fmin(min, _heightMap(i));
71-
max = fmax(max, _heightMap(i));
72-
}
73-
74-
for (unsigned int i = 0; i < _heightMap.size(); i++) {
75-
double val = _heightMap(i);
76-
double normalized = (val - min) / (max - min);
77-
_heightMap.set(i, normalized);
78-
}
63+
_heightMap.normalize();
7964
}
8065

8166
void gen::MapGenerator::round() {
8267
if (!_isInitialized) {
8368
throw std::runtime_error("MapGenerator must be initialized.");
8469
}
85-
86-
/*
87-
Normalize height map and square root the values
88-
*/
89-
normalize();
90-
for (unsigned int i = 0; i < _heightMap.size(); i++) {
91-
double rounded = sqrt(_heightMap(i));
92-
_heightMap.set(i, rounded);
93-
}
70+
_heightMap.round();
9471
}
9572

9673
void gen::MapGenerator::relax() {
9774
if (!_isInitialized) {
9875
throw std::runtime_error("MapGenerator must be initialized.");
9976
}
100-
101-
/*
102-
Replace height with average of its neighbours
103-
*/
104-
std::vector<double> averages;
105-
averages.reserve(_heightMap.size());
106-
107-
std::vector<double> nbs;
108-
for (unsigned int i = 0; i < _heightMap.size(); i++) {
109-
nbs.clear();
110-
_heightMap.getNeighbours(i, nbs);
111-
if (nbs.size() == 0) {
112-
continue;
113-
}
114-
115-
double sum = 0.0;
116-
for (unsigned int nidx = 0; nidx < nbs.size(); nidx++) {
117-
sum += nbs[nidx];
118-
}
119-
averages.push_back(sum / nbs.size());
120-
}
121-
122-
for (unsigned int i = 0; i < _heightMap.size(); i++) {
123-
_heightMap.set(i, averages[i]);
124-
}
77+
_heightMap.relax();
12578
}
12679

12780
void gen::MapGenerator::setSeaLevel(double level) {
12881
if (!_isInitialized) {
12982
throw std::runtime_error("MapGenerator must be initialized.");
13083
}
131-
132-
/*
133-
Translate height map so that level is at 0.5
134-
*/
135-
for (unsigned int i = 0; i < _heightMap.size(); i++) {
136-
double newval = _heightMap(i) - (level - 0.5);
137-
_heightMap.set(i, newval);
138-
}
84+
_heightMap.setLevel(level);
13985
}
14086

14187
void gen::MapGenerator::setSeaLevelToMedian() {
14288
if (!_isInitialized) {
14389
throw std::runtime_error("MapGenerator must be initialized.");
14490
}
145-
146-
std::vector<double> values;
147-
values.reserve(_heightMap.size());
148-
for (unsigned int i = 0; i < _heightMap.size(); i++) {
149-
values.push_back(_heightMap(i));
150-
}
151-
152-
std::sort(values.begin(), values.end());
153-
int mididx = values.size() / 2;
154-
double median;
155-
if (values.size() % 2 == 0) {
156-
median = 0.5 * (values[mididx - 1] + values[mididx]);
157-
} else {
158-
median = values[mididx];
159-
}
160-
161-
setSeaLevel(median);
91+
_heightMap.setLevelToMedian();
16292
}
16393

16494
void gen::MapGenerator::addHill(double px, double py, double r, double height) {
@@ -268,7 +198,9 @@ void gen::MapGenerator::erode(double amount) {
268198
_calculateErosionMap(erosionMap);
269199

270200
for (unsigned int i = 0; i < _heightMap.size(); i++) {
271-
_heightMap.set(i, _heightMap(i) - amount * erosionMap(i));
201+
double currlevel = _heightMap(i);
202+
double newlevel = currlevel - amount * erosionMap(i);
203+
_heightMap.set(i, newlevel);
272204
}
273205
}
274206

@@ -341,6 +273,16 @@ void gen::MapGenerator::outputHeightMap(std::string filename) {
341273
}
342274

343275
std::vector<double> facecolors = _computeFaceHeights(_heightMap);
276+
double min = facecolors[0];
277+
double max = facecolors[0];
278+
for (unsigned int i = 0; i < facecolors.size(); i++) {
279+
min = fmin(min, facecolors[i]);
280+
max = fmax(max, facecolors[i]);
281+
}
282+
283+
for (unsigned int i = 0; i < facecolors.size(); i++) {
284+
facecolors[i] = (facecolors[i] - min) / (max - min);
285+
}
344286

345287
jsoncons::json output;
346288
output["colors"] = facecolors;
@@ -485,10 +427,7 @@ void gen::MapGenerator::_calculateErosionMap(NodeMap<double> &erosionMap) {
485427
erosionMap.set(i, erosion);
486428
}
487429

488-
double max = erosionMap.max();
489-
for (unsigned int i = 0; i < erosionMap.size(); i++) {
490-
erosionMap.set(i, erosionMap(i) / max);
491-
}
430+
erosionMap.normalize();
492431
}
493432

494433
void gen::MapGenerator::_fillDepressions() {
@@ -578,6 +517,8 @@ void gen::MapGenerator::_calculateFluxMap(NodeMap<double> &fluxMap) {
578517
f /= maxFlux;
579518
fluxMap.set(i, f);
580519
}
520+
521+
_fluxMap = fluxMap;
581522
}
582523

583524
double gen::MapGenerator::_calculateFluxCap(NodeMap<double> &fluxMap) {

src/mapgenerator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class MapGenerator {
6868
dcel::DCEL _voronoi;
6969
VertexMap _vertexMap;
7070
NodeMap<double> _heightMap;
71+
NodeMap<double> _fluxMap;
7172
bool _isInitialized = false;
7273

7374
double _samplePadFactor = 2.5;

src/nodemap.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,83 @@ class NodeMap {
141141
bool isInterior(dcel::Vertex &v) {
142142
return _vertexMap->isInterior(v);
143143
}
144+
145+
// Normalize height map values to range [0, 1]
146+
void normalize() {
147+
double min = std::numeric_limits<double>::infinity();
148+
double max = -std::numeric_limits<double>::infinity();
149+
for (unsigned int i = 0; i < size(); i++) {
150+
min = fmin(min, (*this)(i));
151+
max = fmax(max, (*this)(i));
152+
}
153+
154+
for (unsigned int i = 0; i < size(); i++) {
155+
double val = (*this)(i);
156+
double normalized = (val - min) / (max - min);
157+
set(i, normalized);
158+
}
159+
}
160+
161+
// Normalize height map and square root the values
162+
void round() {
163+
normalize();
164+
for (unsigned int i = 0; i < size(); i++) {
165+
double rounded = sqrt((*this)(i));
166+
set(i, rounded);
167+
}
168+
}
169+
170+
// Replace height with average of its neighbours
171+
void relax() {
172+
std::vector<double> averages;
173+
averages.reserve(size());
174+
175+
std::vector<double> nbs;
176+
for (unsigned int i = 0; i < size(); i++) {
177+
nbs.clear();
178+
getNeighbours(i, nbs);
179+
if (nbs.size() == 0) {
180+
continue;
181+
}
182+
183+
double sum = 0.0;
184+
for (unsigned int nidx = 0; nidx < nbs.size(); nidx++) {
185+
sum += nbs[nidx];
186+
}
187+
averages.push_back(sum / nbs.size());
188+
}
189+
190+
for (unsigned int i = 0; i < size(); i++) {
191+
set(i, averages[i]);
192+
}
193+
}
194+
195+
// Translate height map so that level is at zero
196+
void setLevel(double level) {
197+
for (unsigned int i = 0; i < size(); i++) {
198+
double newval = (*this)(i) - level;
199+
set(i, newval);
200+
}
201+
}
202+
203+
void setLevelToMedian() {
204+
std::vector<double> values;
205+
values.reserve(size());
206+
for (unsigned int i = 0; i < size(); i++) {
207+
values.push_back((*this)(i));
208+
}
209+
210+
std::sort(values.begin(), values.end());
211+
int mididx = values.size() / 2;
212+
double median;
213+
if (values.size() % 2 == 0) {
214+
median = 0.5 * (values[mididx - 1] + values[mididx]);
215+
} else {
216+
median = values[mididx];
217+
}
218+
219+
setLevel(median);
220+
}
144221

145222
private:
146223
void _initializeNodes() {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy