Fixed reversing

This commit is contained in:
Alexey 2025-09-24 17:49:30 +03:00
commit 86a2dc3786
2 changed files with 8 additions and 11 deletions

View file

@ -20,9 +20,6 @@ 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,11 +81,12 @@ 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)
val reversed = coords[0].first < coords[coords.size - 1].first
val glist: MutableMap<Pair<Float, Float>, Int> = mutableMapOf() val glist: MutableMap<Pair<Float, Float>, Int> = mutableMapOf()
for (coord in coords) { for (coord in coords) {
@ -113,13 +111,13 @@ class KhmParser {
} else break } else break
} }
cachedHeights = Pair(UShortArray(coords.size) { i -> return 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) if (reversed) coords else coords.reversed().toTypedArray())
return cachedHeights!!
} }
} }

View file

@ -54,11 +54,9 @@ 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 ->
@ -223,7 +221,8 @@ fun MapCanvas(
} }
val heightPair = KhmParser.getHeightsMul(ctx, array) val heightPair = KhmParser.getHeightsMul(ctx, array)
val heights = heightPair.first val heights = heightPair.first
val coords = heightPair.second.reversed() val coords = heightPair.second
for (step in 0 until coords.size) { for (step in 0 until coords.size) {
val stepLat = coords[step].second val stepLat = coords[step].second
val stepLon = coords[step].first val stepLon = coords[step].first