extract GroupAttributesCalculator

This commit is contained in:
InsanusMokrassar 2024-07-08 14:03:59 +06:00
parent 3a4c9133c6
commit bf128a3eb9
3 changed files with 25 additions and 24 deletions

View File

@ -23,6 +23,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.sample import kotlinx.coroutines.flow.sample
import space.kscience.attributes.Attributes import space.kscience.attributes.Attributes
import space.kscience.attributes.plus import space.kscience.attributes.plus
import space.kscience.maps.utils.GroupAttributesCalculator
import kotlin.time.Duration import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.milliseconds
@ -103,24 +104,13 @@ public fun <T : Any> FeatureCanvas(
} }
clipRect { clipRect {
ComposeFeatureDrawScope(this, state, painterCache, textMeasurer).apply(draw).apply { ComposeFeatureDrawScope(this, state, painterCache, textMeasurer).apply(draw).apply {
val attributesCalculator = GroupAttributesCalculator(features)
val attributesCache = mutableMapOf<List<String>, Attributes>()
fun computeGroupAttributes(path: List<String>): Attributes = attributesCache.getOrPut(path) {
if (path.isEmpty()) return Attributes.EMPTY
else if (path.size == 1) {
features[path.first()]?.attributes ?: Attributes.EMPTY
} else {
computeGroupAttributes(path.dropLast(1)) + (features[path.first()]?.attributes
?: Attributes.EMPTY)
}
}
features.entries.sortedBy { it.value.z } features.entries.sortedBy { it.value.z }
.filter { state.viewPoint.zoom in it.value.zoomRange } .filter { state.viewPoint.zoom in it.value.zoomRange }
.forEach { (id, feature) -> .forEach { (id, feature) ->
val path = id.split("/") val path = id.split("/")
drawFeature(feature, computeGroupAttributes(path.dropLast(1))) drawFeature(feature, attributesCalculator.computeGroupAttributes(path.dropLast(1)))
} }
} }
} }

View File

@ -0,0 +1,19 @@
package space.kscience.maps.utils
import space.kscience.attributes.Attributes
import space.kscience.attributes.plus
import space.kscience.maps.features.Feature
public class GroupAttributesCalculator<T : Any>(
private val features: Map<String, Feature<T>>,
private val attributesCache: MutableMap<List<String>, Attributes> = mutableMapOf()
) {
public fun computeGroupAttributes(path: List<String>): Attributes = attributesCache.getOrPut(path){
if (path.isEmpty()) return Attributes.EMPTY
else if (path.size == 1) {
features[path.first()]?.attributes ?: Attributes.EMPTY
} else {
computeGroupAttributes(path.dropLast(1)) + (features[path.first()]?.attributes ?: Attributes.EMPTY)
}
}
}

View File

@ -11,6 +11,7 @@ import space.kscience.attributes.plus
import space.kscience.maps.features.* import space.kscience.maps.features.*
import space.kscience.maps.scheme.XY import space.kscience.maps.scheme.XY
import space.kscience.maps.scheme.XYCanvasState import space.kscience.maps.scheme.XYCanvasState
import space.kscience.maps.utils.GroupAttributesCalculator
public class FeatureStateSnapshot<T : Any>( public class FeatureStateSnapshot<T : Any>(
@ -165,19 +166,10 @@ public fun FeatureStateSnapshot<XY>.generateSvg(
features.entries.sortedBy { it.value.z } features.entries.sortedBy { it.value.z }
.filter { state.viewPoint.zoom in it.value.zoomRange } .filter { state.viewPoint.zoom in it.value.zoomRange }
.forEach { (id, feature) -> .forEach { (id, feature) ->
val attributesCache = mutableMapOf<List<String>, Attributes>() val attributesCalculator = GroupAttributesCalculator(features)
fun computeGroupAttributes(path: List<String>): Attributes = attributesCache.getOrPut(path){
if (path.isEmpty()) return Attributes.EMPTY
else if (path.size == 1) {
features[path.first()]?.attributes ?: Attributes.EMPTY
} else {
computeGroupAttributes(path.dropLast(1)) + (features[path.first()]?.attributes ?: Attributes.EMPTY)
}
}
val path = id.split("/") val path = id.split("/")
drawFeature(feature, computeGroupAttributes(path.dropLast(1))) drawFeature(feature, attributesCalculator.computeGroupAttributes(path.dropLast(1)))
} }
} }
return svgGraphics2D.getSVGElement(id) return svgGraphics2D.getSVGElement(id)