optimize Section flood filling

the position is now not wrapped inside a Number object by the compiler anymore
This commit is contained in:
Bixilon 2023-05-31 18:27:21 +02:00
parent 33fb02ac50
commit 2fb7c61007
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4

View File

@ -46,12 +46,7 @@ class SectionOcclusion(
update(calculateOcclusion(floodFill()), notify)
}
private fun floodFill(): ShortArray {
// mark regions and check direct neighbours
val regions = ShortArray(ProtocolDefinition.BLOCKS_PER_SECTION)
fun trace(x: Int, y: Int, z: Int, nextId: Short) {
private fun trace(regions: ShortArray, x: Int, y: Int, z: Int, nextId: Short) {
val index = y shl 8 or (z shl 4) or x
if (regions[index] > 0) {
return
@ -61,19 +56,24 @@ class SectionOcclusion(
return
}
regions[index] = nextId
if (x > 0) trace(x - 1, y, z, nextId)
if (x < ProtocolDefinition.SECTION_MAX_X) trace(x + 1, y, z, nextId)
if (y > 0) trace(x, y - 1, z, nextId)
if (y < ProtocolDefinition.SECTION_MAX_Y) trace(x, y + 1, z, nextId)
if (z > 0) trace(x, y, z - 1, nextId)
if (z < ProtocolDefinition.SECTION_MAX_Z) trace(x, y, z + 1, nextId)
if (x > 0) trace(regions, x - 1, y, z, nextId)
if (x < ProtocolDefinition.SECTION_MAX_X) trace(regions, x + 1, y, z, nextId)
if (y > 0) trace(regions, x, y - 1, z, nextId)
if (y < ProtocolDefinition.SECTION_MAX_Y) trace(regions, x, y + 1, z, nextId)
if (z > 0) trace(regions, x, y, z - 1, nextId)
if (z < ProtocolDefinition.SECTION_MAX_Z) trace(regions, x, y, z + 1, nextId)
}
var next: Short = 1
private fun floodFill(): ShortArray {
// mark regions and check direct neighbours
val regions = ShortArray(ProtocolDefinition.BLOCKS_PER_SECTION)
var next: Short = 0
for (y in 0 until ProtocolDefinition.SECTION_HEIGHT_Y) {
for (z in 0 until ProtocolDefinition.SECTION_WIDTH_Z) {
for (x in 0 until ProtocolDefinition.SECTION_WIDTH_X) {
trace(x, y, z, next++)
trace(regions, x, y, z, ++next)
}
}
}