1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

SAPI/Noise: Add PerlinNoiseMap:getMapSlice() function

This adds the ability to grab 'slices' of noise calculated by PerlinNoiseMap.
Retrieving smaller slices of noise from the computation result as needed
optimizes memory usage while maintaining a reasonable amount of CPU overhead.
This commit is contained in:
kwolekr 2015-05-17 03:38:39 -04:00
parent c0edb8e313
commit 4c9a8a91c4
6 changed files with 178 additions and 4 deletions

View file

@ -510,3 +510,95 @@ void setboolfield(lua_State *L, int table,
}
////
//// Array table slices
////
size_t write_array_slice_float(
lua_State *L,
int table_index,
float *data,
v3u16 data_size,
v3u16 slice_offset,
v3u16 slice_size)
{
v3u16 pmin, pmax(data_size);
if (slice_offset.X > 0) {
slice_offset.X--;
pmin.X = slice_offset.X;
pmax.X = MYMIN(slice_offset.X + slice_size.X, data_size.X);
}
if (slice_offset.Y > 0) {
slice_offset.Y--;
pmin.Y = slice_offset.Y;
pmax.Y = MYMIN(slice_offset.Y + slice_size.Y, data_size.Y);
}
if (slice_offset.Z > 0) {
slice_offset.Z--;
pmin.Z = slice_offset.Z;
pmax.Z = MYMIN(slice_offset.Z + slice_size.Z, data_size.Z);
}
const u32 ystride = data_size.X;
const u32 zstride = data_size.X * data_size.Y;
u32 elem_index = 1;
for (u32 z = pmin.Z; z != pmax.Z; z++)
for (u32 y = pmin.Y; y != pmax.Y; y++)
for (u32 x = pmin.X; x != pmax.X; x++) {
u32 i = z * zstride + y * ystride + x;
lua_pushnumber(L, data[i]);
lua_rawseti(L, table_index, elem_index);
elem_index++;
}
return elem_index - 1;
}
size_t write_array_slice_u16(
lua_State *L,
int table_index,
u16 *data,
v3u16 data_size,
v3u16 slice_offset,
v3u16 slice_size)
{
v3u16 pmin, pmax(data_size);
if (slice_offset.X > 0) {
slice_offset.X--;
pmin.X = slice_offset.X;
pmax.X = MYMIN(slice_offset.X + slice_size.X, data_size.X);
}
if (slice_offset.Y > 0) {
slice_offset.Y--;
pmin.Y = slice_offset.Y;
pmax.Y = MYMIN(slice_offset.Y + slice_size.Y, data_size.Y);
}
if (slice_offset.Z > 0) {
slice_offset.Z--;
pmin.Z = slice_offset.Z;
pmax.Z = MYMIN(slice_offset.Z + slice_size.Z, data_size.Z);
}
const u32 ystride = data_size.X;
const u32 zstride = data_size.X * data_size.Y;
u32 elem_index = 1;
for (u32 z = pmin.Z; z != pmax.Z; z++)
for (u32 y = pmin.Y; y != pmax.Y; y++)
for (u32 x = pmin.X; x != pmax.X; x++) {
u32 i = z * zstride + y * ystride + x;
lua_pushinteger(L, data[i]);
lua_rawseti(L, table_index, elem_index);
elem_index++;
}
return elem_index - 1;
}