Moved everything out of loop

This commit is contained in:
Alexey 2025-09-24 17:16:14 +03:00
commit 32cfa000c7
2 changed files with 109 additions and 101 deletions

View file

@ -23,6 +23,9 @@ class KhmParser {
private var header: HeightInfo? = null private var header: HeightInfo? = null
const val HEIGHT_FILE: String = "height.khm" const val HEIGHT_FILE: String = "height.khm"
@OptIn(ExperimentalUnsignedTypes::class)
var cachedHeights: Pair<UShortArray, Array<Pair<Float, Float>>>? = null
fun load(filePath: String, ctx: Context) { fun load(filePath: String, ctx: Context) {
if (ctx.getFileStreamPath(HEIGHT_FILE).exists()) { if (ctx.getFileStreamPath(HEIGHT_FILE).exists()) {
return return
@ -84,6 +87,7 @@ class KhmParser {
@OptIn(ExperimentalUnsignedTypes::class) @OptIn(ExperimentalUnsignedTypes::class)
fun getHeightsMul(ctx: Context, coords: Array<Pair<Float, Float>>): Pair<UShortArray, Array<Pair<Float, Float>>> { fun getHeightsMul(ctx: Context, coords: Array<Pair<Float, Float>>): Pair<UShortArray, Array<Pair<Float, Float>>> {
if (coords.isEmpty()) return Pair(ushortArrayOf(), coords) if (coords.isEmpty()) return Pair(ushortArrayOf(), coords)
if (cachedHeights != null) return cachedHeights!!
val dis = DataInputStream(ctx.openFileInput(HEIGHT_FILE)) val dis = DataInputStream(ctx.openFileInput(HEIGHT_FILE))
dis.use { dis.use {
val header = readHeader(dis) val header = readHeader(dis)
@ -112,12 +116,13 @@ class KhmParser {
} else break } else break
} }
return Pair(UShortArray(coords.size) { i -> cachedHeights = Pair(UShortArray(coords.size) { i ->
if (cutOffsets[i] > 0) { if (cutOffsets[i] > 0) {
dis.skipBytes(cutOffsets[i]) dis.skipBytes(cutOffsets[i])
dis.readUnsignedShort().toUShort() dis.readUnsignedShort().toUShort()
} else 0u } else 0u
}, coords) }, coords)
return cachedHeights!!
} }
} }

View file

@ -1,5 +1,6 @@
package com.mirenkov.ktheightmap package com.mirenkov.ktheightmap
import android.util.Log
import androidx.compose.foundation.Canvas import androidx.compose.foundation.Canvas
import androidx.compose.foundation.gestures.detectDragGestures import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
@ -54,9 +55,11 @@ fun MapCanvas(
.pointerInput(Unit) { .pointerInput(Unit) {
detectDragGestures ( detectDragGestures (
onDragEnd = { onDragEnd = {
KhmParser.cachedHeights = null
invokeHeightCalc = true invokeHeightCalc = true
}, },
onDragCancel = { onDragCancel = {
KhmParser.cachedHeights = null
invokeHeightCalc = true invokeHeightCalc = true
} }
) { _, distance -> ) { _, distance ->
@ -137,6 +140,7 @@ fun MapCanvas(
size = size size = size
) )
// Tiles
for (cellX in 0 .. gridWidth + 2) { for (cellX in 0 .. gridWidth + 2) {
val tileX = tileOffsetX + cellX val tileX = tileOffsetX + cellX
val localOffsetX = TILE_SIZE * (cellX - 1) val localOffsetX = TILE_SIZE * (cellX - 1)
@ -181,7 +185,8 @@ fun MapCanvas(
size = Size(TILE_SIZE, TILE_SIZE) size = Size(TILE_SIZE, TILE_SIZE)
) )
} }
}
}
// Placed point and line to center // Placed point and line to center
if (pointLat != 0F) { if (pointLat != 0F) {
val pointOffsetX = SphereMercator.mercateLon(pointLon.toDouble(), level, -TILE_SIZE.toDouble()).toFloat() - offsetX val pointOffsetX = SphereMercator.mercateLon(pointLon.toDouble(), level, -TILE_SIZE.toDouble()).toFloat() - offsetX
@ -286,5 +291,3 @@ fun MapCanvas(
) )
} }
} }
}
}