forked from kscience/visionforge
Transparency rules for gdml
This commit is contained in:
parent
8f1dcac6cd
commit
39b70a6064
@ -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<GDMLMaterial, Meta>()
|
||||
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 {
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user