From f2e7e16d624479fdb41545beb312ff31c6a7e3d2 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 12 Jul 2021 22:50:00 +0300 Subject: [PATCH] [WIP] layer marking --- CHANGELOG.md | 1 + .../visionforge/gdml/demo/GDMLAppComponent.kt | 5 +- .../kscience/visionforge/gdml/layerMarker.kt | 23 ------- .../kscience/visionforge/gdml/markLayers.kt | 62 +++++++++++++++++++ .../visionforge/solid/SolidReference.kt | 2 +- .../kscience/visionforge/solid/Sphere.kt | 2 +- visionforge-threejs/build.gradle.kts | 2 +- .../solid/three/ThreeGeometryBuilder.kt | 3 - 8 files changed, 70 insertions(+), 30 deletions(-) delete mode 100644 visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/layerMarker.kt create mode 100644 visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/markLayers.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index aff57288..8c84f7aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Tube is replaced by more general ConeSurface - position, rotation and size moved to properties - prototypes moved to children +- Immutable Solid instances ### Deprecated diff --git a/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt b/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt index 1d619dfb..fe2d377c 100644 --- a/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt +++ b/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt @@ -32,7 +32,10 @@ val GDMLApp = functionalComponent("GDMLApp") { props -> val parsedVision = when { name.endsWith(".gdml") || name.endsWith(".xml") -> { val gdml = Gdml.decodeFromString(data) - gdml.toVision() + gdml.toVision().apply { +// console.info("Marking layers for file $name") +// markLayers() + } } name.endsWith(".json") -> visionManager.decodeFromString(data) else -> { diff --git a/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/layerMarker.kt b/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/layerMarker.kt deleted file mode 100644 index 17b75e79..00000000 --- a/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/layerMarker.kt +++ /dev/null @@ -1,23 +0,0 @@ -package space.kscience.visionforge.gdml - -import space.kscience.dataforge.names.Name -import space.kscience.dataforge.names.plus -import space.kscience.visionforge.VisionGroup - -private fun VisionGroup.countChildren(namePrefix: Name, cache: MutableMap = hashMapOf()): Int { - var counter = 0 - children.forEach { (token, child) -> - if (child is VisionGroup) { - counter += child.countChildren(namePrefix + token, cache) - } else { - counter++ - } - } - cache[namePrefix] = counter - return counter -} - - -public fun VisionGroup.processLayers() { - -} \ No newline at end of file diff --git a/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/markLayers.kt b/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/markLayers.kt new file mode 100644 index 00000000..2689d1d6 --- /dev/null +++ b/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/markLayers.kt @@ -0,0 +1,62 @@ +package space.kscience.visionforge.gdml + +import space.kscience.dataforge.names.Name +import space.kscience.dataforge.names.NameToken +import space.kscience.dataforge.names.length +import space.kscience.dataforge.names.plus +import space.kscience.visionforge.VisionGroup +import space.kscience.visionforge.solid.Solid +import space.kscience.visionforge.solid.SolidGroup +import space.kscience.visionforge.solid.layer + +private class VisionCounterTree( + val name: Name, + val vision: Solid, +) { + val children: Map = + (vision as? VisionGroup)?.children?.mapValues { + VisionCounterTree(name + it.key, it.value as Solid) + } ?: emptyMap() + +// +// val directChildrenCount: Int by lazy { +// children.size +// } + + val childrenCount: Int = + children.values.sumOf { it.childrenCount + 1 } + +} + + +private fun VisionCounterTree.topToBottom(): Sequence = sequence { + yield(this@topToBottom) + children.values.forEach { + yieldAll(it.topToBottom()) + } +} + +public fun SolidGroup.markLayers(thresholds: List = listOf(1000, 20000, 100000)) { + val counterTree = VisionCounterTree(Name.EMPTY, this) + val totalCount = counterTree.childrenCount + if (totalCount > thresholds.firstOrNull() ?: 0) { + val allNodes = counterTree.topToBottom().toMutableList() + //println("tree construction finished") + allNodes.sortWith(compareBy({ it.name.length }, { it.childrenCount }).reversed()) + + //mark layers + var removed = 0 + var thresholdIndex = thresholds.indexOfLast { it < totalCount } + + for (node in allNodes) { + node.vision.layer = thresholdIndex + 1 + removed++ + if (totalCount - removed < thresholds[thresholdIndex]) { + thresholdIndex-- + } + if (thresholdIndex < 0) break + } + + + } +} \ No newline at end of file diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt index 2b7a8358..1ba659b9 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt @@ -100,7 +100,7 @@ public class SolidReferenceGroup( private class ReferenceChild( val owner: SolidReferenceGroup, private val childName: Name - ) : SolidReference, VisionGroup { + ) : SolidReference, VisionGroup, Solid { override val prototype: Solid by lazy { if (childName.isEmpty()) owner.prototype else { diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Sphere.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Sphere.kt index a6af3cd8..bae8d83d 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Sphere.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Sphere.kt @@ -17,7 +17,7 @@ public class Sphere( public val phiStart: Float = 0f, public val phi: Float = PI2, public val thetaStart: Float = 0f, - public val theta: Float = PI.toFloat(), + public val theta: Float = PI .toFloat(), ) : SolidBase(), GeometrySolid, VisionPropertyContainer { override fun toGeometry(geometryBuilder: GeometryBuilder) { diff --git a/visionforge-threejs/build.gradle.kts b/visionforge-threejs/build.gradle.kts index 32803a61..a862f776 100644 --- a/visionforge-threejs/build.gradle.kts +++ b/visionforge-threejs/build.gradle.kts @@ -5,5 +5,5 @@ plugins { dependencies { api(project(":visionforge-solid")) implementation(npm("three", "0.130.1")) - implementation(npm("three-csg-ts", "3.1.4")) + implementation(npm("three-csg-ts", "3.1.5")) } diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeGeometryBuilder.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeGeometryBuilder.kt index c1c5aa72..4f3e0b58 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeGeometryBuilder.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeGeometryBuilder.kt @@ -54,9 +54,6 @@ public class ThreeGeometryBuilder : GeometryBuilder { setAttribute("position", Float32BufferAttribute(positions.toTypedArray(), 3)) setAttribute("normal", Float32BufferAttribute(normals.toTypedArray(), 3)) //setAttribute("color", Float32BufferAttribute(colors.toFloatArray(), 3)) - //a temporary fix for CSG problem - val uvsArray = Array((counter+1)*2){0f} - setAttribute("uv", Float32BufferAttribute(uvsArray, 2)) computeBoundingSphere() }