add basic heightmaps generator optimization

This commit is contained in:
MihailRis
2024-08-18 00:08:36 +03:00
parent 8c0a3f4260
commit 8fb0f6a1bb
5 changed files with 74 additions and 12 deletions
+30
View File
@@ -1,6 +1,7 @@
#include "Heightmap.hpp"
#include <cmath>
#include <cstring>
#include <stdexcept>
#include <glm/glm.hpp>
@@ -59,6 +60,9 @@ static inline float sample_at(
void Heightmap::resize(
uint dstwidth, uint dstheight, InterpolationType interp
) {
if (width == dstwidth && height == dstheight) {
return;
}
std::vector<float> dst;
dst.resize(dstwidth*dstheight);
@@ -75,3 +79,29 @@ void Heightmap::resize(
height = dstheight;
buffer = std::move(dst);
}
void Heightmap::crop(
uint srcx, uint srcy, uint dstwidth, uint dstheight
) {
if (srcx + dstwidth > width || srcy + dstheight > height) {
throw std::runtime_error(
"crop zone is not fully inside of the source image");
}
if (dstwidth == width && dstheight == height) {
return;
}
std::vector<float> dst;
dst.resize(dstwidth*dstheight);
for (uint y = 0; y < dstheight; y++) {
std::memcpy(
dst.data()+y*dstwidth,
buffer.data()+(y+srcy)*width+srcx,
dstwidth*sizeof(float));
}
width = dstwidth;
height = dstheight;
buffer = std::move(dst);
}