From 39b70a6064cdd4ad8f4feaddc4cb184198161537 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 20 Aug 2019 15:46:46 +0300 Subject: [PATCH] Transparency rules for gdml --- .../vis/spatial/gdml/GDMLTransformer.kt | 29 +++++++++++-------- .../dataforge/vis/spatial/gdml/visualGDML.kt | 15 ++++------ .../vis/spatial/gdml/demo/GDMLDemoApp.kt | 4 ++- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/GDMLTransformer.kt b/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/GDMLTransformer.kt index 12fc2aee..b4b4e3a4 100644 --- a/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/GDMLTransformer.kt +++ b/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/GDMLTransformer.kt @@ -4,17 +4,16 @@ import hep.dataforge.meta.Meta import hep.dataforge.meta.buildMeta import hep.dataforge.meta.builder import hep.dataforge.vis.spatial.VisualGroup3D -import scientifik.gdml.GDML -import scientifik.gdml.GDMLGroup -import scientifik.gdml.GDMLMaterial -import scientifik.gdml.GDMLSolid +import hep.dataforge.vis.spatial.VisualObject3D +import hep.dataforge.vis.spatial.material +import scientifik.gdml.* import kotlin.random.Random class GDMLTransformer(val root: GDML) { private val materialCache = HashMap() private val random = Random(111) - enum class Action{ + enum class Action { ACCEPT, REJECT, CACHE @@ -26,23 +25,29 @@ class GDMLTransformer(val root: GDML) { val templates by lazy { VisualGroup3D() } var lUnit: LUnit = LUnit.MM - var resolveColor: ColorResolver = { material, _ -> + + var solidAction: (GDMLSolid) -> Action = { Action.CACHE } + var volumeAction: (GDMLGroup) -> Action = { Action.ACCEPT } + + + var transparent: GDMLVolume.(GDMLSolid?) -> Boolean = { !physVolumes.isEmpty() } + + internal fun configureSolid(obj: VisualObject3D, parent: GDMLVolume, solid: GDMLSolid?) { + val material = parent.materialref.resolve(root) ?: GDMLElement(parent.materialref.ref) + val materialColor = materialCache.getOrPut(material) { buildMeta { "color" to random.nextInt(0, Int.MAX_VALUE) } } - if (this?.physVolumes?.isEmpty() != false) { - materialColor - } else { + obj.material = if (parent.transparent(solid)) { materialColor.builder().apply { "opacity" to 0.5 } + } else { + materialColor } } - var solidAction: (GDMLSolid) -> Action = { Action.CACHE } - var volumeAction: (GDMLGroup) -> Action = { Action.ACCEPT } - fun printStatistics() { println("Solids:") solidCounter.entries.sortedByDescending { it.value }.forEach { diff --git a/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/visualGDML.kt b/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/visualGDML.kt index f71b4948..a59b1a58 100644 --- a/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/visualGDML.kt +++ b/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/visualGDML.kt @@ -1,6 +1,5 @@ package hep.dataforge.vis.spatial.gdml -import hep.dataforge.meta.Meta import hep.dataforge.names.EmptyName import hep.dataforge.names.plus import hep.dataforge.vis.common.asName @@ -26,7 +25,7 @@ private fun VisualObject3D.withPosition( this@withPosition.rotationX = rotation.x() this@withPosition.rotationY = rotation.y() this@withPosition.rotationZ = rotation.z() - this@withPosition.rotationOrder=RotationOrder.ZXY + this@withPosition.rotationOrder = RotationOrder.ZXY } scale?.let { this@withPosition.scaleX = scale.x.toFloat() @@ -38,6 +37,7 @@ private fun VisualObject3D.withPosition( @Suppress("NOTHING_TO_INLINE") private inline operator fun Number.times(d: Double) = toDouble() * d + @Suppress("NOTHING_TO_INLINE") private inline operator fun Number.times(f: Float) = toFloat() * f @@ -211,18 +211,18 @@ private fun volume( if (group is GDMLVolume) { val solid = group.solidref.resolve(context.root) ?: error("Solid with tag ${group.solidref.ref} for volume ${group.name} not defined") - val material = group.materialref.resolve(context.root) ?: GDMLElement(group.materialref.ref) + //val material = group.materialref.resolve(context.root) ?: GDMLElement(group.materialref.ref) when (context.solidAction(solid)) { GDMLTransformer.Action.ACCEPT -> { addSolid(context, solid, solid.name) { - this.material = context.resolveColor(group, material, solid) + context.configureSolid(this, group, solid) } } GDMLTransformer.Action.CACHE -> { - if (context.templates.get(solid.name) == null) { + if (context.templates[solid.name] == null) { context.templates.addSolid(context, solid, solid.name) { - this.material = context.resolveColor(group, material, solid) + context.configureSolid(this, group, solid) } } ref(solid.name.asName(), solid.name) @@ -244,9 +244,6 @@ private fun volume( } } -typealias ColorResolver = GDMLGroup?.(GDMLMaterial, GDMLSolid?) -> Meta - - fun GDML.toVisual(block: GDMLTransformer.() -> Unit = {}): VisualGroup3D { val context = GDMLTransformer(this).apply(block) diff --git a/dataforge-vis-spatial-gdml/src/jsMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/GDMLDemoApp.kt b/dataforge-vis-spatial-gdml/src/jsMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/GDMLDemoApp.kt index 7de8ea84..444961fc 100644 --- a/dataforge-vis-spatial-gdml/src/jsMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/GDMLDemoApp.kt +++ b/dataforge-vis-spatial-gdml/src/jsMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/GDMLDemoApp.kt @@ -100,9 +100,11 @@ private class GDMLDemoApp : ApplicationBase() { } } + transparent = { !physVolumes.isEmpty() || (it?.name?.startsWith("Coil") ?: false) } + } launch { message("Rendering") } - val output = three.output(canvas){ + val output = three.output(canvas) { "axis" to { "size" to 100 }