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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include <vector>
#include <cassert>
#include <limits>
#include "SmoothHeightMesh.h"
#include "Map/Ground.h"
#include "Map/ReadMap.h"
#include "System/float3.h"
#include "System/SpringMath.h"
#include "System/TimeProfiler.h"
#include "System/Threading/ThreadPool.h"
SmoothHeightMesh smoothGround;
static float Interpolate(float x, float y, const int maxx, const int maxy, const float res, const float* heightmap)
{
x = Clamp(x / res, 0.0f, maxx - 1.0f);
y = Clamp(y / res, 0.0f, maxy - 1.0f);
const int sx = x;
const int sy = y;
const float dx = (x - sx);
const float dy = (y - sy);
const int sxp1 = std::min(sx + 1, maxx - 1);
const int syp1 = std::min(sy + 1, maxy - 1);
const float& h1 = heightmap[sx + sy * maxx];
const float& h2 = heightmap[sxp1 + sy * maxx];
const float& h3 = heightmap[sx + syp1 * maxx];
const float& h4 = heightmap[sxp1 + syp1 * maxx];
const float hi1 = mix(h1, h2, dx);
const float hi2 = mix(h3, h4, dx);
return mix(hi1, hi2, dy);
}
void SmoothHeightMesh::Init(float mx, float my, float res, float smoothRad)
{
maxx = ((fmaxx = mx) / res) + 1;
maxy = ((fmaxy = my) / res) + 1;
resolution = res;
smoothRadius = std::max(1.0f, smoothRad);
MakeSmoothMesh();
}
void SmoothHeightMesh::Kill() {
mesh.clear();
origMesh.clear();
}
float SmoothHeightMesh::GetHeight(float x, float y)
{
assert(!mesh.empty());
return Interpolate(x, y, maxx, maxy, resolution, &mesh[0]);
}
float SmoothHeightMesh::GetHeightAboveWater(float x, float y)
{
assert(!mesh.empty());
return std::max(0.0f, Interpolate(x, y, maxx, maxy, resolution, &mesh[0]));
}
float SmoothHeightMesh::SetHeight(int index, float h)
{
return (mesh[index] = h);
}
float SmoothHeightMesh::AddHeight(int index, float h)
{
return (mesh[index] += h);
}
float SmoothHeightMesh::SetMaxHeight(int index, float h)
{
return (mesh[index] = std::max(h, mesh[index]));
}
inline static void FindMaximumColumnHeights(
const int maxx,
const int maxy,
const int winSize,
const float resolution,
std::vector<float>& colsMaxima,
std::vector<int>& maximaRows
) {
// initialize the algorithm: find the maximum
// height per column and the corresponding row
for (int y = 0; y <= std::min(maxy, winSize); ++y) {
for (int x = 0; x <= maxx; ++x) {
const float curx = x * resolution;
const float cury = y * resolution;
const float curh = CGround::GetHeightAboveWater(curx, cury);
if (curh > colsMaxima[x]) {
colsMaxima[x] = curh;
maximaRows[x] = y;
}
}
}
}
inline static void AdvanceMaximaRows(
const int y,
const int maxx,
const float resolution,
const std::vector<float>& colsMaxima,
std::vector<int>& maximaRows
) {
const float cury = y * resolution;
// try to advance rows if they're equal to current maximum but are further away
for (int x = 0; x <= maxx; ++x) {
if (maximaRows[x] == (y - 1)) {
const float curx = x * resolution;
const float curh = CGround::GetHeightAboveWater(curx, cury);
if (curh == colsMaxima[x]) {
maximaRows[x] = y;
}
assert(curh <= colsMaxima[x]);
}
}
}
inline static void FindRadialMaximum(
int y,
int maxx,
int winSize,
float resolution,
const std::vector<float>& colsMaxima,
std::vector<float>& mesh
) {
const float cury = y * resolution;
for (int x = 0; x <= maxx; ++x) {
float maxRowHeight = -std::numeric_limits<float>::max();
// find current maximum within radius smoothRadius
// (in every column stack) along the current row
const int startx = std::max(x - winSize, 0);
const int endx = std::min(maxx, x + winSize);
for (int i = startx; i <= endx; ++i) {
assert(i >= 0);
assert(i <= maxx);
assert(CGround::GetHeightReal(i * resolution, cury) <= colsMaxima[i]);
maxRowHeight = std::max(colsMaxima[i], maxRowHeight);
}
#ifndef NDEBUG
const float curx = x * resolution;
assert(maxRowHeight <= std::max(readMap->GetCurrMaxHeight(), 0.0f));
assert(maxRowHeight >= CGround::GetHeightAboveWater(curx, cury));
#ifdef SMOOTHMESH_CORRECTNESS_CHECK
// naive algorithm
float maxRowHeightAlt = -std::numeric_limits<float>::max();
for (float y1 = cury - smoothRadius; y1 <= cury + smoothRadius; y1 += resolution) {
for (float x1 = curx - smoothRadius; x1 <= curx + smoothRadius; x1 += resolution) {
maxRowHeightAlt = std::max(maxRowHeightAlt, CGround::GetHeightAboveWater(x1, y1));
}
}
assert(maxRowHeightAlt == maxRowHeight);
#endif
#endif
mesh[x + y * maxx] = maxRowHeight;
}
}
inline static void FixRemainingMaxima(
const int y,
const int maxx,
const int maxy,
const int winSize,
const float resolution,
std::vector<float>& colsMaxima,
std::vector<int>& maximaRows
) {
// fix remaining maximums after a pass
const int nextrow = y + winSize + 1;
const float nextrowy = nextrow * resolution;
for (int x = 0; x <= maxx; ++x) {
#ifdef _DEBUG
for (int y1 = std::max(0, y - winSize); y1 <= std::min(maxy, y + winSize); ++y1) {
assert(CGround::GetHeightReal(x * resolution, y1 * resolution) <= colsMaxima[x]);
}
#endif
const float curx = x * resolution;
if (maximaRows[x] <= (y - winSize)) {
// find a new maximum if the old one left the window
colsMaxima[x] = -std::numeric_limits<float>::max();
for (int y1 = std::max(0, y - winSize + 1); y1 <= std::min(maxy, nextrow); ++y1) {
const float h = CGround::GetHeightAboveWater(curx, y1 * resolution);
if (h > colsMaxima[x]) {
colsMaxima[x] = h;
maximaRows[x] = y1;
} else if (colsMaxima[x] == h) {
// if equal, move as far down as possible
maximaRows[x] = y1;
}
}
} else if (nextrow <= maxy) {
// else, just check if a new maximum has entered the window
const float h = CGround::GetHeightAboveWater(curx, nextrowy);
if (h > colsMaxima[x]) {
colsMaxima[x] = h;
maximaRows[x] = nextrow;
}
}
assert(maximaRows[x] <= nextrow);
assert(maximaRows[x] >= y - winSize + 1);
#ifdef _DEBUG
for (int y1 = std::max(0, y - winSize + 1); y1 <= std::min(maxy, y + winSize + 1); ++y1) {
assert(colsMaxima[x] >= CGround::GetHeightReal(curx, y1 * resolution));
}
#endif
}
}
inline static void BlurHorizontal(
const int maxx,
const int maxy,
const int blurSize,
const float resolution,
const std::vector<float>& mesh,
std::vector<float>& smoothed
) {
const float n = 2.0f * blurSize + 1.0f;
const float recipn = 1.0f / n;
const int lineSize = maxx + 1;
for_mt(0, maxy+1, [&](const int y) {
float avg = 0.0f;
for (int x = 0; x <= 2 * blurSize; ++x) {
avg += mesh[x + y * lineSize];
}
for (int x = 0; x <= maxx; ++x) {
const int idx = x + y * lineSize;
if (x <= blurSize || x > (maxx - blurSize)) {
// map-border case
smoothed[idx] = 0.0f;
const int xstart = std::max(x - blurSize, 0);
const int xend = std::min(x + blurSize, maxx);
for (int x1 = xstart; x1 <= xend; ++x1) {
smoothed[idx] += mesh[x1 + y * lineSize];
}
const float gh = CGround::GetHeightAboveWater(x * resolution, y * resolution);
const float sh = smoothed[idx] / (xend - xstart + 1);
smoothed[idx] = std::min(readMap->GetCurrMaxHeight(), std::max(gh, sh));
} else {
// non-border case
avg += mesh[idx + blurSize] - mesh[idx - blurSize - 1];
const float gh = CGround::GetHeightAboveWater(x * resolution, y * resolution);
const float sh = recipn * avg;
smoothed[idx] = std::min(readMap->GetCurrMaxHeight(), std::max(gh, sh));
}
assert(smoothed[idx] <= std::max(readMap->GetCurrMaxHeight(), 0.0f));
assert(smoothed[idx] >= readMap->GetCurrMinHeight() );
}
});
}
inline static void BlurVertical(
const int maxx,
const int maxy,
const int blurSize,
const float resolution,
const std::vector<float>& mesh,
std::vector<float>& smoothed
) {
const float n = 2.0f * blurSize + 1.0f;
const float recipn = 1.0f / n;
const int lineSize = maxx + 1;
for_mt(0, maxx+1, [&](const int x) {
float avg = 0.0f;
for (int y = 0; y <= 2 * blurSize; ++y) {
avg += mesh[x + y * lineSize];
}
for (int y = 0; y <= maxy; ++y) {
const int idx = x + y * lineSize;
if (y <= blurSize || y > (maxy - blurSize)) {
// map-border case
smoothed[idx] = 0.0f;
const int ystart = std::max(y - blurSize, 0);
const int yend = std::min(y + blurSize, maxy);
for (int y1 = ystart; y1 <= yend; ++y1) {
smoothed[idx] += mesh[x + y1 * lineSize];
}
const float gh = CGround::GetHeightAboveWater(x * resolution, y * resolution);
const float sh = smoothed[idx] / (yend - ystart + 1);
smoothed[idx] = std::min(readMap->GetCurrMaxHeight(), std::max(gh, sh));
} else {
// non-border case
avg += mesh[x + (y + blurSize) * lineSize] - mesh[x + (y - blurSize - 1) * lineSize];
const float gh = CGround::GetHeightAboveWater(x * resolution, y * resolution);
const float sh = recipn * avg;
smoothed[idx] = std::min(readMap->GetCurrMaxHeight(), std::max(gh, sh));
}
assert(smoothed[idx] <= std::max(readMap->GetCurrMaxHeight(), 0.0f));
assert(smoothed[idx] >= readMap->GetCurrMinHeight() );
}
});
}
inline static void CheckInvariants(
int y,
int maxx,
int maxy,
int winSize,
float resolution,
const std::vector<float>& colsMaxima,
const std::vector<int>& maximaRows
) {
// check invariants
if (y < maxy) {
for (int x = 0; x <= maxx; ++x) {
assert(maximaRows[x] > y - winSize);
assert(maximaRows[x] <= maxy);
assert(colsMaxima[x] <= std::max(readMap->GetCurrMaxHeight(), 0.0f));
assert(colsMaxima[x] >= readMap->GetCurrMinHeight() );
}
}
for (int y1 = std::max(0, y - winSize + 1); y1 <= std::min(maxy, y + winSize + 1); ++y1) {
for (int x1 = 0; x1 <= maxx; ++x1) {
assert(CGround::GetHeightReal(x1 * resolution, y1 * resolution) <= colsMaxima[x1]);
}
}
}
void SmoothHeightMesh::MakeSmoothMesh()
{
ScopedOnceTimer timer("SmoothHeightMesh::MakeSmoothMesh");
// info:
// height-value array has size <maxx + 1> * <maxy + 1>
// and represents a grid of <maxx> cols by <maxy> rows
// maximum legal index is ((maxx + 1) * (maxy + 1)) - 1
//
// row-width (number of height-value corners per row) is (maxx + 1)
// col-height (number of height-value corners per col) is (maxy + 1)
//
// 1st row has indices [maxx*( 0) + ( 0), maxx*(1) + ( 0)] inclusive
// 2nd row has indices [maxx*( 1) + ( 1), maxx*(2) + ( 1)] inclusive
// 3rd row has indices [maxx*( 2) + ( 2), maxx*(3) + ( 2)] inclusive
// ...
// Nth row has indices [maxx*(N-1) + (N-1), maxx*(N) + (N-1)] inclusive
//
// use sliding window of maximums to reduce computational complexity
const int winSize = smoothRadius / resolution;
constexpr int blurSize = 3;
assert(mesh.empty());
mesh.resize((maxx + 1) * (maxy + 1), 0.0f);
origMesh.resize((maxx + 1) * (maxy + 1), 0.0f);
colsMaxima.clear();
colsMaxima.resize(maxx + 1, -std::numeric_limits<float>::max());
maximaRows.clear();
maximaRows.resize(maxx + 1, -1);
FindMaximumColumnHeights(maxx, maxy, winSize, resolution, colsMaxima, maximaRows);
for (int y = 0; y <= maxy; ++y) {
AdvanceMaximaRows(y, maxx, resolution, colsMaxima, maximaRows);
FindRadialMaximum(y, maxx, winSize, resolution, colsMaxima, mesh);
FixRemainingMaxima(y, maxx, maxy, winSize, resolution, colsMaxima, maximaRows);
#ifdef _DEBUG
CheckInvariants(y, maxx, maxy, winSize, resolution, colsMaxima, maximaRows);
#endif
}
// actually smooth with approximate Gaussian blur passes
for (int numBlurs = 3; numBlurs > 0; --numBlurs) {
BlurHorizontal(maxx, maxy, blurSize, resolution, mesh, origMesh); mesh.swap(origMesh);
BlurVertical(maxx, maxy, blurSize, resolution, mesh, origMesh); mesh.swap(origMesh);
}
// <mesh> now contains the final smoothed heightmap, save it in origMesh
std::copy(mesh.begin(), mesh.end(), origMesh.begin());
}
|