@@ -60,105 +60,35 @@ void gen::MapGenerator::normalize() {
60
60
if (!_isInitialized) {
61
61
throw std::runtime_error (" MapGenerator must be initialized." );
62
62
}
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 ();
79
64
}
80
65
81
66
void gen::MapGenerator::round () {
82
67
if (!_isInitialized) {
83
68
throw std::runtime_error (" MapGenerator must be initialized." );
84
69
}
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 ();
94
71
}
95
72
96
73
void gen::MapGenerator::relax () {
97
74
if (!_isInitialized) {
98
75
throw std::runtime_error (" MapGenerator must be initialized." );
99
76
}
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 ();
125
78
}
126
79
127
80
void gen::MapGenerator::setSeaLevel (double level) {
128
81
if (!_isInitialized) {
129
82
throw std::runtime_error (" MapGenerator must be initialized." );
130
83
}
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);
139
85
}
140
86
141
87
void gen::MapGenerator::setSeaLevelToMedian () {
142
88
if (!_isInitialized) {
143
89
throw std::runtime_error (" MapGenerator must be initialized." );
144
90
}
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 ();
162
92
}
163
93
164
94
void gen::MapGenerator::addHill (double px, double py, double r, double height) {
@@ -268,7 +198,9 @@ void gen::MapGenerator::erode(double amount) {
268
198
_calculateErosionMap (erosionMap);
269
199
270
200
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);
272
204
}
273
205
}
274
206
@@ -341,6 +273,16 @@ void gen::MapGenerator::outputHeightMap(std::string filename) {
341
273
}
342
274
343
275
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
+ }
344
286
345
287
jsoncons::json output;
346
288
output[" colors" ] = facecolors;
@@ -485,10 +427,7 @@ void gen::MapGenerator::_calculateErosionMap(NodeMap<double> &erosionMap) {
485
427
erosionMap.set (i, erosion);
486
428
}
487
429
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 ();
492
431
}
493
432
494
433
void gen::MapGenerator::_fillDepressions () {
@@ -578,6 +517,8 @@ void gen::MapGenerator::_calculateFluxMap(NodeMap<double> &fluxMap) {
578
517
f /= maxFlux;
579
518
fluxMap.set (i, f);
580
519
}
520
+
521
+ _fluxMap = fluxMap;
581
522
}
582
523
583
524
double gen::MapGenerator::_calculateFluxCap (NodeMap<double > &fluxMap) {
0 commit comments