0.3.0 #23
@ -23,6 +23,7 @@ import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.sample
|
||||
import space.kscience.attributes.Attributes
|
||||
import space.kscience.attributes.plus
|
||||
import space.kscience.maps.utils.GroupAttributesCalculator
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.milliseconds
|
||||
|
||||
@ -103,24 +104,13 @@ public fun <T : Any> FeatureCanvas(
|
||||
}
|
||||
clipRect {
|
||||
ComposeFeatureDrawScope(this, state, painterCache, textMeasurer).apply(draw).apply {
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
val attributesCalculator = GroupAttributesCalculator(features)
|
||||
|
||||
features.entries.sortedBy { it.value.z }
|
||||
.filter { state.viewPoint.zoom in it.value.zoomRange }
|
||||
.forEach { (id, feature) ->
|
||||
val path = id.split("/")
|
||||
drawFeature(feature, computeGroupAttributes(path.dropLast(1)))
|
||||
drawFeature(feature, attributesCalculator.computeGroupAttributes(path.dropLast(1)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@ -11,6 +11,7 @@ import space.kscience.attributes.plus
|
||||
import space.kscience.maps.features.*
|
||||
import space.kscience.maps.scheme.XY
|
||||
import space.kscience.maps.scheme.XYCanvasState
|
||||
import space.kscience.maps.utils.GroupAttributesCalculator
|
||||
|
||||
|
||||
public class FeatureStateSnapshot<T : Any>(
|
||||
@ -165,19 +166,10 @@ public fun FeatureStateSnapshot<XY>.generateSvg(
|
||||
features.entries.sortedBy { it.value.z }
|
||||
.filter { state.viewPoint.zoom in it.value.zoomRange }
|
||||
.forEach { (id, feature) ->
|
||||
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)
|
||||
}
|
||||
}
|
||||
val attributesCalculator = GroupAttributesCalculator(features)
|
||||
|
||||
val path = id.split("/")
|
||||
drawFeature(feature, computeGroupAttributes(path.dropLast(1)))
|
||||
drawFeature(feature, attributesCalculator.computeGroupAttributes(path.dropLast(1)))
|
||||
}
|
||||
}
|
||||
return svgGraphics2D.getSVGElement(id)
|
||||
|
Loading…
Reference in New Issue
Block a user