Minor refactoring

This commit is contained in:
Alexander Nozik 2020-02-24 20:39:24 +03:00
parent a065bee04d
commit 691dbad44e
12 changed files with 104 additions and 77 deletions

View File

@ -10,6 +10,9 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient import kotlinx.serialization.Transient
import kotlinx.serialization.UseSerializers import kotlinx.serialization.UseSerializers
/**
* A container for styles
*/
@Serializable @Serializable
class StyleSheet() { class StyleSheet() {
@Transient @Transient
@ -28,7 +31,7 @@ class StyleSheet() {
} }
/** /**
* Define a style without notifying * Define a style without notifying owner
*/ */
fun define(key: String, style: Meta?) { fun define(key: String, style: Meta?) {
if (style == null) { if (style == null) {

View File

@ -2,9 +2,7 @@ package hep.dataforge.vis.common
import hep.dataforge.meta.* import hep.dataforge.meta.*
import hep.dataforge.names.Name import hep.dataforge.names.Name
import hep.dataforge.names.NameToken
import hep.dataforge.names.asName import hep.dataforge.names.asName
import hep.dataforge.names.toName
import hep.dataforge.values.Value import hep.dataforge.values.Value
import kotlin.jvm.JvmName import kotlin.jvm.JvmName
import kotlin.properties.ReadOnlyProperty import kotlin.properties.ReadOnlyProperty
@ -48,7 +46,7 @@ class VisualObjectDelegateWrapper<T>(
override fun getValue(thisRef: Any?, property: KProperty<*>): T { override fun getValue(thisRef: Any?, property: KProperty<*>): T {
val name = key ?: property.name.asName() val name = key ?: property.name.asName()
return read(obj.getProperty(name,inherited))?:default return read(obj.getProperty(name, inherited)) ?: default
} }
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
@ -58,75 +56,72 @@ class VisualObjectDelegateWrapper<T>(
} }
fun VisualObject.value(default: Value? = null, key: String? = null, inherited: Boolean = false) = fun VisualObject.value(default: Value? = null, name: Name? = null, inherited: Boolean = false) =
VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.value } VisualObjectDelegateWrapper(this, name, default, inherited) { it.value }
fun VisualObject.string(default: String? = null, key: String? = null, inherited: Boolean = false) = fun VisualObject.string(default: String? = null, name: Name? = null, inherited: Boolean = false) =
VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.string } VisualObjectDelegateWrapper(this, name, default, inherited) { it.string }
fun VisualObject.boolean(default: Boolean? = null, key: String? = null, inherited: Boolean = false) = fun VisualObject.boolean(default: Boolean? = null, name: Name? = null, inherited: Boolean = false) =
VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.boolean } VisualObjectDelegateWrapper(this, name, default, inherited) { it.boolean }
fun VisualObject.number(default: Number? = null, key: String? = null, inherited: Boolean = false) = fun VisualObject.number(default: Number? = null, name: Name? = null, inherited: Boolean = false) =
VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.number } VisualObjectDelegateWrapper(this, name, default, inherited) { it.number }
fun VisualObject.double(default: Double? = null, key: String? = null, inherited: Boolean = false) = fun VisualObject.double(default: Double? = null, name: Name? = null, inherited: Boolean = false) =
VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.double } VisualObjectDelegateWrapper(this, name, default, inherited) { it.double }
fun VisualObject.int(default: Int? = null, key: String? = null, inherited: Boolean = false) = fun VisualObject.int(default: Int? = null, name: Name? = null, inherited: Boolean = false) =
VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.int } VisualObjectDelegateWrapper(this, name, default, inherited) { it.int }
fun VisualObject.node(key: String? = null, inherited: Boolean = true) = fun VisualObject.node(name: Name? = null, inherited: Boolean = true) =
VisualObjectDelegateWrapper(this, key?.toName(), null, inherited) { it.node } VisualObjectDelegateWrapper(this, name, null, inherited) { it.node }
fun VisualObject.item(key: String? = null, inherited: Boolean = true) = fun VisualObject.item(name: Name? = null, inherited: Boolean = true) =
VisualObjectDelegateWrapper(this, key?.toName(), null, inherited) { it } VisualObjectDelegateWrapper(this, name, null, inherited) { it }
//fun <T : Configurable> Configurable.spec(spec: Specification<T>, key: String? = null) = ChildConfigDelegate<T>(key) { spec.wrap(this) } //fun <T : Configurable> Configurable.spec(spec: Specification<T>, key: String? = null) = ChildConfigDelegate<T>(key) { spec.wrap(this) }
@JvmName("safeString") @JvmName("safeString")
fun VisualObject.string(default: String, key: String? = null, inherited: Boolean = false) = fun VisualObject.string(default: String, name: Name? = null, inherited: Boolean = false) =
VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.string } VisualObjectDelegateWrapper(this, name, default, inherited) { it.string }
@JvmName("safeBoolean") @JvmName("safeBoolean")
fun VisualObject.boolean(default: Boolean, key: String? = null, inherited: Boolean = false) = fun VisualObject.boolean(default: Boolean, name: Name? = null, inherited: Boolean = false) =
VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.boolean } VisualObjectDelegateWrapper(this, name, default, inherited) { it.boolean }
@JvmName("safeNumber") @JvmName("safeNumber")
fun VisualObject.number(default: Number, key: String? = null, inherited: Boolean = false) = fun VisualObject.number(default: Number, name: Name? = null, inherited: Boolean = false) =
VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.number } VisualObjectDelegateWrapper(this, name, default, inherited) { it.number }
@JvmName("safeDouble") @JvmName("safeDouble")
fun VisualObject.double(default: Double, key: String? = null, inherited: Boolean = false) = fun VisualObject.double(default: Double, name: Name? = null, inherited: Boolean = false) =
VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.double } VisualObjectDelegateWrapper(this, name, default, inherited) { it.double }
@JvmName("safeInt") @JvmName("safeInt")
fun VisualObject.int(default: Int, key: String? = null, inherited: Boolean = false) = fun VisualObject.int(default: Int, name: Name? = null, inherited: Boolean = false) =
VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.int } VisualObjectDelegateWrapper(this, name, default, inherited) { it.int }
inline fun <reified E : Enum<E>> VisualObject.enum(default: E, key: String? = null, inherited: Boolean = false) = inline fun <reified E : Enum<E>> VisualObject.enum(default: E, name: Name? = null, inherited: Boolean = false) =
VisualObjectDelegateWrapper( VisualObjectDelegateWrapper(this, name, default, inherited) {
this, item -> item.string?.let { enumValueOf<E>(it) }
key?.let { NameToken(it).asName() }, }
default,
inherited
) { item -> item.string?.let { enumValueOf<E>(it) } }
//merge properties //merge properties
fun <T> VisualObject.merge( fun <T> VisualObject.merge(
key: String? = null, name: Name? = null,
transformer: (Sequence<MetaItem<*>>) -> T transformer: (Sequence<MetaItem<*>>) -> T
): ReadOnlyProperty<VisualObject, T> { ): ReadOnlyProperty<VisualObject, T> {
return object : ReadOnlyProperty<Any?, T> { return object : ReadOnlyProperty<Any?, T> {
override fun getValue(thisRef: Any?, property: KProperty<*>): T { override fun getValue(thisRef: Any?, property: KProperty<*>): T {
val name = key?.toName() ?: property.name.asName() val actualName = name ?: property.name.asName()
val sequence = sequence<MetaItem<*>> { val sequence = sequence<MetaItem<*>> {
var thisObj: VisualObject? = this@merge var thisObj: VisualObject? = this@merge
while (thisObj != null) { while (thisObj != null) {
thisObj.config[name]?.let { yield(it) } thisObj.config[actualName]?.let { yield(it) }
thisObj = thisObj.parent thisObj = thisObj.parent
} }
} }

View File

@ -26,12 +26,11 @@ fun Element.displayObjectTree(
} }
private fun TagConsumer<HTMLElement>.subTree( private fun TagConsumer<HTMLElement>.subTree(
fullName: Name, name: Name,
obj: VisualObject, obj: VisualObject,
clickCallback: (Name) -> Unit clickCallback: (Name) -> Unit
) { ) {
// val fullName = parentName + token val token = name.last()?.toString()?:"World"
val token = fullName.last()?.toString()?:"World"
//display as node if any child is visible //display as node if any child is visible
if (obj is VisualGroup && obj.children.keys.any { !it.body.startsWith("@") }) { if (obj is VisualGroup && obj.children.keys.any { !it.body.startsWith("@") }) {
@ -40,7 +39,7 @@ private fun TagConsumer<HTMLElement>.subTree(
toggle = span("objTree-caret") toggle = span("objTree-caret")
label("objTree-label") { label("objTree-label") {
+token +token
onClickFunction = { clickCallback(fullName) } onClickFunction = { clickCallback(name) }
} }
} }
val subtree = ul("objTree-subtree") val subtree = ul("objTree-subtree")
@ -55,7 +54,7 @@ private fun TagConsumer<HTMLElement>.subTree(
.forEach { (childToken, child) -> .forEach { (childToken, child) ->
append { append {
li().apply { li().apply {
subTree(fullName + childToken, child, clickCallback) subTree(name + childToken, child, clickCallback)
} }
} }
} }
@ -70,7 +69,7 @@ private fun TagConsumer<HTMLElement>.subTree(
span("objTree-leaf") span("objTree-leaf")
label("objTree-label") { label("objTree-label") {
+token +token
onClickFunction = { clickCallback(fullName) } onClickFunction = { clickCallback(name) }
} }
} }
} }

View File

@ -8,7 +8,7 @@ import java.nio.file.Path
fun GDML.Companion.readFile(file: Path): GDML { fun GDML.Companion.readFile(file: Path): GDML {
val xmlReader = StAXReader(Files.newInputStream(file), "UTF-8") val xmlReader = StAXReader(Files.newInputStream(file), "UTF-8")
return GDML.format.parse(GDML.serializer(), xmlReader) return format.parse(GDML.serializer(), xmlReader)
} }
fun VisualGroup3D.gdml(file: Path, key: String = "", transformer: GDMLTransformer.() -> Unit = {}) { fun VisualGroup3D.gdml(file: Path, key: String = "", transformer: GDMLTransformer.() -> Unit = {}) {

View File

@ -7,16 +7,29 @@ import hep.dataforge.names.plus
import hep.dataforge.values.ValueType import hep.dataforge.values.ValueType
import hep.dataforge.vis.common.Colors import hep.dataforge.vis.common.Colors
import hep.dataforge.vis.spatial.Material3D.Companion.MATERIAL_COLOR_KEY import hep.dataforge.vis.spatial.Material3D.Companion.MATERIAL_COLOR_KEY
import hep.dataforge.vis.spatial.Material3D.Companion.MATERIAL_KEY
import hep.dataforge.vis.spatial.Material3D.Companion.MATERIAL_OPACITY_KEY import hep.dataforge.vis.spatial.Material3D.Companion.MATERIAL_OPACITY_KEY
class Material3D(override val config: Config) : Specific { class Material3D(override val config: Config) : Specific {
/**
* Primary web-color for the material
*/
var color by string(key = COLOR_KEY) var color by string(key = COLOR_KEY)
var specularColor by string() /**
* Specular color for phong material
*/
var specularColor by string(key = SPECULAR_COLOR_KEY)
/**
* Opacity
*/
var opacity by float(1f, key = OPACITY_KEY) var opacity by float(1f, key = OPACITY_KEY)
/**
* Replace material by wire frame
*/
var wireframe by boolean(false, WIREFRAME_KEY) var wireframe by boolean(false, WIREFRAME_KEY)
companion object : Specification<Material3D> { companion object : Specification<Material3D> {
@ -25,7 +38,7 @@ class Material3D(override val config: Config) : Specific {
val MATERIAL_KEY = "material".asName() val MATERIAL_KEY = "material".asName()
internal val COLOR_KEY = "color".asName() internal val COLOR_KEY = "color".asName()
val MATERIAL_COLOR_KEY = MATERIAL_KEY + COLOR_KEY val MATERIAL_COLOR_KEY = MATERIAL_KEY + COLOR_KEY
val SPECULAR_COLOR = "specularColor".asName() val SPECULAR_COLOR_KEY = "specularColor".asName()
internal val OPACITY_KEY = "opacity".asName() internal val OPACITY_KEY = "opacity".asName()
val MATERIAL_OPACITY_KEY = MATERIAL_KEY + OPACITY_KEY val MATERIAL_OPACITY_KEY = MATERIAL_KEY + OPACITY_KEY
internal val WIREFRAME_KEY = "wireframe".asName() internal val WIREFRAME_KEY = "wireframe".asName()
@ -54,10 +67,16 @@ class Material3D(override val config: Config) : Specific {
} }
} }
fun VisualObject3D.color(rgb: String) { /**
setProperty(MATERIAL_COLOR_KEY, rgb) * Set color as web-color
*/
fun VisualObject3D.color(webColor: String) {
setProperty(MATERIAL_COLOR_KEY, webColor)
} }
/**
* Set color as integer
*/
fun VisualObject3D.color(rgb: Int) { fun VisualObject3D.color(rgb: Int) {
setProperty(MATERIAL_COLOR_KEY, rgb) setProperty(MATERIAL_COLOR_KEY, rgb)
} }
@ -76,16 +95,15 @@ var VisualObject3D.color: String?
setProperty(MATERIAL_COLOR_KEY, value) setProperty(MATERIAL_COLOR_KEY, value)
} }
//var VisualObject3D.material: Material3D? val VisualObject3D.material: Material3D?
// get() = getProperty(MATERIAL_KEY).node?.let { Material3D.wrap(it) } get() = getProperty(MATERIAL_KEY).node?.let { Material3D.wrap(it) }
// set(value) = setProperty(MATERIAL_KEY, value?.config)
fun VisualObject3D.material(builder: Material3D.() -> Unit) { fun VisualObject3D.material(builder: Material3D.() -> Unit) {
val node = config[Material3D.MATERIAL_KEY].node val node = config[MATERIAL_KEY].node
if (node != null) { if (node != null) {
Material3D.update(node, builder) Material3D.update(node, builder)
} else { } else {
config[Material3D.MATERIAL_KEY] = Material3D(builder) config[MATERIAL_KEY] = Material3D(builder)
} }
} }

View File

@ -4,8 +4,10 @@ package hep.dataforge.vis.spatial
import hep.dataforge.io.serialization.ConfigSerializer import hep.dataforge.io.serialization.ConfigSerializer
import hep.dataforge.meta.Config import hep.dataforge.meta.Config
import hep.dataforge.meta.number
import hep.dataforge.names.asName
import hep.dataforge.names.plus
import hep.dataforge.vis.common.AbstractVisualObject import hep.dataforge.vis.common.AbstractVisualObject
import hep.dataforge.vis.common.number
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers import kotlinx.serialization.UseSerializers
@ -19,7 +21,11 @@ class PolyLine(var points: List<Point3D>) : AbstractVisualObject(), VisualObject
override var scale: Point3D? = null override var scale: Point3D? = null
//var lineType by string() //var lineType by string()
var thickness by number(1.0, key = "material.thickness") var thickness by number(1.0, key = Material3D.MATERIAL_KEY + THICKNESS_KEY)
companion object {
val THICKNESS_KEY = "thickness".asName()
}
} }

View File

@ -22,22 +22,10 @@ interface VisualObject3D : VisualObject {
var rotation: Point3D? var rotation: Point3D?
var scale: Point3D? var scale: Point3D?
fun MetaBuilder.updatePosition() {
xPos to position?.x
yPos to position?.y
zPos to position?.z
xRotation to rotation?.x
yRotation to rotation?.y
zRotation to rotation?.z
xScale to scale?.x
yScale to scale?.y
zScale to scale?.z
}
companion object { companion object {
val VISIBLE_KEY = "visible".asName() val VISIBLE_KEY = "visible".asName()
val SELECTED_KEY = "selected".asName() // val SELECTED_KEY = "selected".asName()
val DETAIL_KEY = "detail".asName() val DETAIL_KEY = "detail".asName()
val LAYER_KEY = "layer".asName() val LAYER_KEY = "layer".asName()
val IGNORE_KEY = "ignore".asName() val IGNORE_KEY = "ignore".asName()

View File

@ -18,11 +18,10 @@ object ThreeLineFactory : ThreeFactory<PolyLine> {
vertices = obj.points.toTypedArray() vertices = obj.points.toTypedArray()
} }
val material = val material = ThreeMaterials.getLineMaterial(obj.getProperty(MeshThreeFactory.EDGES_MATERIAL_KEY).node)
ThreeMaterials.getLineMaterial(obj.getProperty(MeshThreeFactory.EDGES_MATERIAL_KEY).node)
material.linewidth = obj.thickness.toDouble() material.linewidth = obj.thickness.toDouble()
material.color = obj.color?.let { Color(it) }?: DEFAULT_LINE_COLOR material.color = obj.color?.let { Color(it) } ?: DEFAULT_LINE_COLOR
return LineSegments(geometry, material).apply { return LineSegments(geometry, material).apply {
updatePosition(obj) updatePosition(obj)

View File

@ -39,10 +39,10 @@ object ThreeMaterials {
fun getMaterial(visualObject3D: VisualObject3D): Material { fun getMaterial(visualObject3D: VisualObject3D): Material {
val meta = visualObject3D.getProperty(Material3D.MATERIAL_KEY).node ?: return ThreeMaterials.DEFAULT val meta = visualObject3D.getProperty(Material3D.MATERIAL_KEY).node ?: return ThreeMaterials.DEFAULT
return if (meta[Material3D.SPECULAR_COLOR] != null) { return if (meta[Material3D.SPECULAR_COLOR_KEY] != null) {
MeshPhongMaterial().apply { MeshPhongMaterial().apply {
color = meta[Material3D.COLOR_KEY]?.getColor() ?: DEFAULT_COLOR color = meta[Material3D.COLOR_KEY]?.getColor() ?: DEFAULT_COLOR
specular = meta[Material3D.SPECULAR_COLOR]!!.getColor() specular = meta[Material3D.SPECULAR_COLOR_KEY]!!.getColor()
opacity = meta[Material3D.OPACITY_KEY]?.double ?: 1.0 opacity = meta[Material3D.OPACITY_KEY]?.double ?: 1.0
transparent = opacity < 1.0 transparent = opacity < 1.0
wireframe = meta[Material3D.WIREFRAME_KEY].boolean ?: false wireframe = meta[Material3D.WIREFRAME_KEY].boolean ?: false

View File

@ -66,7 +66,7 @@ fun MetaItem<*>?.material(): Material {
is MetaItem.NodeItem -> PhongMaterial().apply { is MetaItem.NodeItem -> PhongMaterial().apply {
val opacity = node[Material3D.OPACITY_KEY].double ?: 1.0 val opacity = node[Material3D.OPACITY_KEY].double ?: 1.0
diffuseColor = node[Material3D.COLOR_KEY]?.color(opacity) ?: Color.DARKGREY diffuseColor = node[Material3D.COLOR_KEY]?.color(opacity) ?: Color.DARKGREY
specularColor = node[Material3D.SPECULAR_COLOR]?.color(opacity) ?: Color.WHITE specularColor = node[Material3D.SPECULAR_COLOR_KEY]?.color(opacity) ?: Color.WHITE
} }
} }
} }

View File

@ -0,0 +1,19 @@
package hep.dataforge.vis.spatial.gdml.demo
import hep.dataforge.vis.spatial.Visual3DPlugin
import hep.dataforge.vis.spatial.VisualGroup3D
import hep.dataforge.vis.spatial.gdml.LUnit
import hep.dataforge.vis.spatial.gdml.readFile
import hep.dataforge.vis.spatial.gdml.toVisual
import scientifik.gdml.GDML
import java.io.File
import java.nio.file.Paths
fun main() {
val gdml = GDML.readFile(Paths.get("D:\\Work\\Projects\\gdml.kt\\gdml-source\\BM@N.gdml"))
val visual = gdml.toVisual {
lUnit = LUnit.CM
}
val json = Visual3DPlugin.json.stringify(VisualGroup3D.serializer(), visual)
File("D:\\Work\\Projects\\gdml.kt\\gdml-source\\BM@N.json").writeText(json)
}