Transparency rules for gdml

This commit is contained in:
Alexander Nozik 2019-08-20 15:46:46 +03:00
parent 8f1dcac6cd
commit 39b70a6064
3 changed files with 26 additions and 22 deletions

View File

@ -4,10 +4,9 @@ import hep.dataforge.meta.Meta
import hep.dataforge.meta.buildMeta import hep.dataforge.meta.buildMeta
import hep.dataforge.meta.builder import hep.dataforge.meta.builder
import hep.dataforge.vis.spatial.VisualGroup3D import hep.dataforge.vis.spatial.VisualGroup3D
import scientifik.gdml.GDML import hep.dataforge.vis.spatial.VisualObject3D
import scientifik.gdml.GDMLGroup import hep.dataforge.vis.spatial.material
import scientifik.gdml.GDMLMaterial import scientifik.gdml.*
import scientifik.gdml.GDMLSolid
import kotlin.random.Random import kotlin.random.Random
class GDMLTransformer(val root: GDML) { class GDMLTransformer(val root: GDML) {
@ -26,23 +25,29 @@ class GDMLTransformer(val root: GDML) {
val templates by lazy { VisualGroup3D() } val templates by lazy { VisualGroup3D() }
var lUnit: LUnit = LUnit.MM 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) { val materialColor = materialCache.getOrPut(material) {
buildMeta { buildMeta {
"color" to random.nextInt(0, Int.MAX_VALUE) "color" to random.nextInt(0, Int.MAX_VALUE)
} }
} }
if (this?.physVolumes?.isEmpty() != false) { obj.material = if (parent.transparent(solid)) {
materialColor
} else {
materialColor.builder().apply { "opacity" to 0.5 } materialColor.builder().apply { "opacity" to 0.5 }
} else {
materialColor
} }
} }
var solidAction: (GDMLSolid) -> Action = { Action.CACHE }
var volumeAction: (GDMLGroup) -> Action = { Action.ACCEPT }
fun printStatistics() { fun printStatistics() {
println("Solids:") println("Solids:")
solidCounter.entries.sortedByDescending { it.value }.forEach { solidCounter.entries.sortedByDescending { it.value }.forEach {

View File

@ -1,6 +1,5 @@
package hep.dataforge.vis.spatial.gdml package hep.dataforge.vis.spatial.gdml
import hep.dataforge.meta.Meta
import hep.dataforge.names.EmptyName import hep.dataforge.names.EmptyName
import hep.dataforge.names.plus import hep.dataforge.names.plus
import hep.dataforge.vis.common.asName import hep.dataforge.vis.common.asName
@ -38,6 +37,7 @@ private fun VisualObject3D.withPosition(
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
private inline operator fun Number.times(d: Double) = toDouble() * d private inline operator fun Number.times(d: Double) = toDouble() * d
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
private inline operator fun Number.times(f: Float) = toFloat() * f private inline operator fun Number.times(f: Float) = toFloat() * f
@ -211,18 +211,18 @@ private fun volume(
if (group is GDMLVolume) { if (group is GDMLVolume) {
val solid = group.solidref.resolve(context.root) val solid = group.solidref.resolve(context.root)
?: error("Solid with tag ${group.solidref.ref} for volume ${group.name} not defined") ?: 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)) { when (context.solidAction(solid)) {
GDMLTransformer.Action.ACCEPT -> { GDMLTransformer.Action.ACCEPT -> {
addSolid(context, solid, solid.name) { addSolid(context, solid, solid.name) {
this.material = context.resolveColor(group, material, solid) context.configureSolid(this, group, solid)
} }
} }
GDMLTransformer.Action.CACHE -> { GDMLTransformer.Action.CACHE -> {
if (context.templates.get(solid.name) == null) { if (context.templates[solid.name] == null) {
context.templates.addSolid(context, solid, solid.name) { 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) 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 { fun GDML.toVisual(block: GDMLTransformer.() -> Unit = {}): VisualGroup3D {
val context = GDMLTransformer(this).apply(block) val context = GDMLTransformer(this).apply(block)

View File

@ -100,6 +100,8 @@ private class GDMLDemoApp : ApplicationBase() {
} }
} }
transparent = { !physVolumes.isEmpty() || (it?.name?.startsWith("Coil") ?: false) }
} }
launch { message("Rendering") } launch { message("Rendering") }
val output = three.output(canvas) { val output = three.output(canvas) {