From 25fc143363f9cc37779d6df5fae1be14b00b47a4 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 23 Aug 2022 12:06:27 +0300 Subject: [PATCH] Performance optimization --- build.gradle.kts | 3 +- .../kotlin/ru/mipt/npm/muon/monitor/Model.kt | 1 - .../ru/mipt/npm/muon/monitor/Monitor.kt | 85 +++++++++++-------- .../kscience/visionforge/VisionProperties.kt | 4 +- .../solid/three/ThreeCanvasLabelFactory.kt | 22 +++-- .../visionforge/solid/three/ThreeMaterials.kt | 17 ++-- .../solid/three/ThreeReferenceFactory.kt | 23 ++--- 7 files changed, 85 insertions(+), 70 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 4c76b98f..939d197b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,7 +3,7 @@ plugins { // id("org.jetbrains.kotlinx.kover") version "0.5.0" } -val dataforgeVersion by extra("0.6.0-dev-13") +val dataforgeVersion by extra("0.6.0-dev-15") val fxVersion by extra("11") allprojects{ @@ -15,6 +15,7 @@ subprojects { if (name.startsWith("visionforge")) apply() repositories { + mavenLocal() maven("https://repo.kotlin.link") mavenCentral() maven("https://maven.jzy3d.org/releases") diff --git a/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Model.kt b/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Model.kt index cf8caf80..1b8eb566 100644 --- a/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Model.kt +++ b/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Model.kt @@ -82,7 +82,6 @@ class Model(val manager: VisionManager) { } event.track?.let { tracks.polyline(*it.toTypedArray(), name = "track[${event.id}]") { - thickness = 4 color.set("red") } } diff --git a/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Monitor.kt b/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Monitor.kt index 3e1db5bc..1a8c8aa9 100644 --- a/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Monitor.kt +++ b/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Monitor.kt @@ -11,12 +11,12 @@ import space.kscience.visionforge.solid.plus class SC1( val name: String, val center: Point3D, - val xSize: Float = PIXEL_XY_SIZE, val ySize: Float = PIXEL_XY_SIZE, val zSize: Float = PIXEL_Z_SIZE + val xSize: Float = PIXEL_XY_SIZE, val ySize: Float = PIXEL_XY_SIZE, val zSize: Float = PIXEL_Z_SIZE, ) class SC16( val name: String, - val center: Point3D + val center: Point3D, ) { /** @@ -28,73 +28,86 @@ class SC16( val y: Double when (index) { 7 -> { - x = 1.5 * Monitor.PIXEL_XY_SPACING; - y = 1.5 * Monitor.PIXEL_XY_SPACING; + x = 1.5 * Monitor.PIXEL_XY_SPACING + y = 1.5 * Monitor.PIXEL_XY_SPACING } + 4 -> { - x = 0.5 * Monitor.PIXEL_XY_SPACING; - y = 1.5 * Monitor.PIXEL_XY_SPACING; + x = 0.5 * Monitor.PIXEL_XY_SPACING + y = 1.5 * Monitor.PIXEL_XY_SPACING } + 6 -> { - x = 1.5 * Monitor.PIXEL_XY_SPACING; - y = 0.5 * Monitor.PIXEL_XY_SPACING; + x = 1.5 * Monitor.PIXEL_XY_SPACING + y = 0.5 * Monitor.PIXEL_XY_SPACING } + 5 -> { - x = 0.5 * Monitor.PIXEL_XY_SPACING; - y = 0.5 * Monitor.PIXEL_XY_SPACING; + x = 0.5 * Monitor.PIXEL_XY_SPACING + y = 0.5 * Monitor.PIXEL_XY_SPACING } 3 -> { - x = -1.5 * Monitor.PIXEL_XY_SPACING; - y = 1.5 * Monitor.PIXEL_XY_SPACING; + x = -1.5 * Monitor.PIXEL_XY_SPACING + y = 1.5 * Monitor.PIXEL_XY_SPACING } + 0 -> { - x = -0.5 * Monitor.PIXEL_XY_SPACING; - y = 1.5 * Monitor.PIXEL_XY_SPACING; + x = -0.5 * Monitor.PIXEL_XY_SPACING + y = 1.5 * Monitor.PIXEL_XY_SPACING } + 2 -> { - x = -1.5 * Monitor.PIXEL_XY_SPACING; - y = 0.5 * Monitor.PIXEL_XY_SPACING; + x = -1.5 * Monitor.PIXEL_XY_SPACING + y = 0.5 * Monitor.PIXEL_XY_SPACING } + 1 -> { - x = -0.5 * Monitor.PIXEL_XY_SPACING; - y = 0.5 * Monitor.PIXEL_XY_SPACING; + x = -0.5 * Monitor.PIXEL_XY_SPACING + y = 0.5 * Monitor.PIXEL_XY_SPACING } 11 -> { - x = -1.5 * Monitor.PIXEL_XY_SPACING; - y = -1.5 * Monitor.PIXEL_XY_SPACING; + x = -1.5 * Monitor.PIXEL_XY_SPACING + y = -1.5 * Monitor.PIXEL_XY_SPACING } + 8 -> { - x = -0.5 * Monitor.PIXEL_XY_SPACING; - y = -1.5 * Monitor.PIXEL_XY_SPACING; + x = -0.5 * Monitor.PIXEL_XY_SPACING + y = -1.5 * Monitor.PIXEL_XY_SPACING } + 10 -> { - x = -1.5 * Monitor.PIXEL_XY_SPACING; - y = -0.5 * Monitor.PIXEL_XY_SPACING; + x = -1.5 * Monitor.PIXEL_XY_SPACING + y = -0.5 * Monitor.PIXEL_XY_SPACING } + 9 -> { - x = -0.5 * Monitor.PIXEL_XY_SPACING; - y = -0.5 * Monitor.PIXEL_XY_SPACING; + x = -0.5 * Monitor.PIXEL_XY_SPACING + y = -0.5 * Monitor.PIXEL_XY_SPACING } 15 -> { - x = 1.5 * Monitor.PIXEL_XY_SPACING; - y = -1.5 * Monitor.PIXEL_XY_SPACING; + x = 1.5 * Monitor.PIXEL_XY_SPACING + y = -1.5 * Monitor.PIXEL_XY_SPACING } + 12 -> { - x = 0.5 * Monitor.PIXEL_XY_SPACING; - y = -1.5 * Monitor.PIXEL_XY_SPACING; + x = 0.5 * Monitor.PIXEL_XY_SPACING + y = -1.5 * Monitor.PIXEL_XY_SPACING } + 14 -> { - x = 1.5 * Monitor.PIXEL_XY_SPACING; - y = -0.5 * Monitor.PIXEL_XY_SPACING; + x = 1.5 * Monitor.PIXEL_XY_SPACING + y = -0.5 * Monitor.PIXEL_XY_SPACING } + 13 -> { - x = 0.5 * Monitor.PIXEL_XY_SPACING; - y = -0.5 * Monitor.PIXEL_XY_SPACING; + x = 0.5 * Monitor.PIXEL_XY_SPACING + y = -0.5 * Monitor.PIXEL_XY_SPACING } - else -> throw Error(); + + else -> throw Error() } val offset = Point3D(-y, x, 0)//rotateDetector(Point3D(x, y, 0.0)); val pixelName = "${name}_${index}" @@ -137,7 +150,7 @@ object Monitor { .mapNotNull { line -> if (line.startsWith(" ")) { val split = line.trim().split("\\s+".toRegex()) - val detectorName = split[1]; + val detectorName = split[1] val x = split[4].toDouble() - 500 val y = split[5].toDouble() - 500 val z = 180 - split[6].toDouble() diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionProperties.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionProperties.kt index 3a80dcf2..cddb6585 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionProperties.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionProperties.kt @@ -165,6 +165,8 @@ public abstract class AbstractVisionProperties( return properties!! } + private val descriptorCache = HashMap() + override fun getValue( name: Name, inherit: Boolean?, @@ -172,7 +174,7 @@ public abstract class AbstractVisionProperties( ): Value? { own?.get(name)?.value?.let { return it } - val descriptor = descriptor?.get(name) + val descriptor = descriptor?.let { descriptor -> descriptorCache.getOrPut(name) { descriptor[name] } } val stylesFlag = includeStyles ?: descriptor?.usesStyles ?: true if (stylesFlag) { diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvasLabelFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvasLabelFactory.kt index 0b0019df..7a0f1c7b 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvasLabelFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvasLabelFactory.kt @@ -24,15 +24,19 @@ public object ThreeCanvasLabelFactory : ThreeFactory { override fun build(three: ThreePlugin, vision: SolidLabel, observe: Boolean): Object3D { val canvas = document.createElement("canvas") as HTMLCanvasElement - val context = canvas.getContext("2d") as CanvasRenderingContext2D - context.font = "Bold ${vision.fontSize}pt ${vision.fontFamily}" - context.fillStyle = vision.properties.getValue(SolidMaterial.MATERIAL_COLOR_KEY, false, true)?.value ?: "black" - context.textBaseline = CanvasTextBaseline.MIDDLE - val metrics = context.measureText(vision.text) - //canvas.width = metrics.width.toInt() - - - context.fillText(vision.text, (canvas.width - metrics.width) / 2, 0.5 * canvas.height) + canvas.getContext("2d").apply { + this as CanvasRenderingContext2D + font = "Bold ${vision.fontSize}pt ${vision.fontFamily}" + fillStyle = vision.properties.getValue( + SolidMaterial.MATERIAL_COLOR_KEY, + inherit = false, + includeStyles = true + )?.value ?: "black" + textBaseline = CanvasTextBaseline.MIDDLE + val metrics = measureText(vision.text) + //canvas.width = metrics.width.toInt() + fillText(vision.text, (canvas.width - metrics.width) / 2, 0.5 * canvas.height) + } // canvas contents will be used for a texture diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeMaterials.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeMaterials.kt index 5966ee30..f5ac5c2b 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeMaterials.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeMaterials.kt @@ -93,22 +93,17 @@ public object ThreeMaterials { * Compute color */ public fun Meta.threeColor(): Color? { - if (isEmpty()) return null - val value = value - return if (isLeaf) { - when { - value == null -> null + value?.let { value -> + return when { value === Null -> null value.type == ValueType.NUMBER -> Color(value.int) else -> Color(value.string) } - } else { - Color( - getValue(Colors.RED_KEY.asName())?.int ?: 0, - getValue(Colors.GREEN_KEY.asName())?.int ?: 0, - getValue(Colors.BLUE_KEY.asName())?.int ?: 0 - ) } + val red = getValue(Colors.RED_KEY.asName())?.int + val green = getValue(Colors.GREEN_KEY.asName())?.int + val blue = getValue(Colors.BLUE_KEY.asName())?.int + return if (red == null && green == null && blue == null) null else Color(red ?: 0, green ?: 0, blue ?: 0) } public fun ColorAccessor.threeColor(): Color? { diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeReferenceFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeReferenceFactory.kt index bde8c456..405956f8 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeReferenceFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeReferenceFactory.kt @@ -16,17 +16,18 @@ public object ThreeReferenceFactory : ThreeFactory { override val type: KClass = SolidReference::class - private fun Object3D.replicate(): Object3D { - return when { - isMesh(this) -> Mesh(geometry, material).also { - it.applyMatrix4(matrix) - } - else -> clone(false) - }.also { obj: Object3D -> - obj.name = this.name - children.forEach { child: Object3D -> - obj.add(child.replicate()) - } + private fun Object3D.replicate(): Object3D = when { + isMesh(this) -> Mesh(geometry, material).also { + //clone geometry + it.material.cached = true + it.applyMatrix4(matrix) + } + + else -> clone(false) + }.also { obj: Object3D -> + obj.name = this.name + children.forEach { child: Object3D -> + obj.add(child.replicate()) } }