From 691dbad44e8c284164919e088955650be41dcacd Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 24 Feb 2020 20:39:24 +0300 Subject: [PATCH] Minor refactoring --- .../hep/dataforge/vis/common/StyleSheet.kt | 5 +- .../vis/common/VisualObjectDelegate.kt | 73 +++++++++---------- .../hep/dataforge/vis/js/editor/jsTree.kt | 11 ++- .../vis/spatial/gdml/visualGDMLJvm.kt | 2 +- .../hep/dataforge/vis/spatial/Material3D.kt | 36 ++++++--- .../hep/dataforge/vis/spatial/PolyLine.kt | 10 ++- .../dataforge/vis/spatial/VisualObject3D.kt | 14 +--- .../vis/spatial/three/ThreeLineFactory.kt | 5 +- .../vis/spatial/three/ThreeMaterials.kt | 4 +- .../dataforge/vis/spatial/fx/FXMaterials.kt | 2 +- .../resources/cubes.gdml | 0 .../vis/spatial/gdml/demo/saveToJson.kt | 19 +++++ 12 files changed, 104 insertions(+), 77 deletions(-) rename demo/gdml/src/{jsMain => commonMain}/resources/cubes.gdml (100%) create mode 100644 demo/gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/saveToJson.kt diff --git a/dataforge-vis-common/src/commonMain/kotlin/hep/dataforge/vis/common/StyleSheet.kt b/dataforge-vis-common/src/commonMain/kotlin/hep/dataforge/vis/common/StyleSheet.kt index fce0b422..f831bf68 100644 --- a/dataforge-vis-common/src/commonMain/kotlin/hep/dataforge/vis/common/StyleSheet.kt +++ b/dataforge-vis-common/src/commonMain/kotlin/hep/dataforge/vis/common/StyleSheet.kt @@ -10,6 +10,9 @@ import kotlinx.serialization.Serializable import kotlinx.serialization.Transient import kotlinx.serialization.UseSerializers +/** + * A container for styles + */ @Serializable class StyleSheet() { @Transient @@ -28,7 +31,7 @@ class StyleSheet() { } /** - * Define a style without notifying + * Define a style without notifying owner */ fun define(key: String, style: Meta?) { if (style == null) { diff --git a/dataforge-vis-common/src/commonMain/kotlin/hep/dataforge/vis/common/VisualObjectDelegate.kt b/dataforge-vis-common/src/commonMain/kotlin/hep/dataforge/vis/common/VisualObjectDelegate.kt index 1dba2c7f..ec16f977 100644 --- a/dataforge-vis-common/src/commonMain/kotlin/hep/dataforge/vis/common/VisualObjectDelegate.kt +++ b/dataforge-vis-common/src/commonMain/kotlin/hep/dataforge/vis/common/VisualObjectDelegate.kt @@ -2,9 +2,7 @@ package hep.dataforge.vis.common import hep.dataforge.meta.* import hep.dataforge.names.Name -import hep.dataforge.names.NameToken import hep.dataforge.names.asName -import hep.dataforge.names.toName import hep.dataforge.values.Value import kotlin.jvm.JvmName import kotlin.properties.ReadOnlyProperty @@ -48,7 +46,7 @@ class VisualObjectDelegateWrapper( override fun getValue(thisRef: Any?, property: KProperty<*>): T { 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) { @@ -58,75 +56,72 @@ class VisualObjectDelegateWrapper( } -fun VisualObject.value(default: Value? = null, key: String? = null, inherited: Boolean = false) = - VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.value } +fun VisualObject.value(default: Value? = null, name: Name? = null, inherited: Boolean = false) = + VisualObjectDelegateWrapper(this, name, default, inherited) { it.value } -fun VisualObject.string(default: String? = null, key: String? = null, inherited: Boolean = false) = - VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.string } +fun VisualObject.string(default: String? = null, name: Name? = null, inherited: Boolean = false) = + VisualObjectDelegateWrapper(this, name, default, inherited) { it.string } -fun VisualObject.boolean(default: Boolean? = null, key: String? = null, inherited: Boolean = false) = - VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.boolean } +fun VisualObject.boolean(default: Boolean? = null, name: Name? = null, inherited: Boolean = false) = + VisualObjectDelegateWrapper(this, name, default, inherited) { it.boolean } -fun VisualObject.number(default: Number? = null, key: String? = null, inherited: Boolean = false) = - VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.number } +fun VisualObject.number(default: Number? = null, name: Name? = null, inherited: Boolean = false) = + VisualObjectDelegateWrapper(this, name, default, inherited) { it.number } -fun VisualObject.double(default: Double? = null, key: String? = null, inherited: Boolean = false) = - VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.double } +fun VisualObject.double(default: Double? = null, name: Name? = null, inherited: Boolean = false) = + VisualObjectDelegateWrapper(this, name, default, inherited) { it.double } -fun VisualObject.int(default: Int? = null, key: String? = null, inherited: Boolean = false) = - VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.int } +fun VisualObject.int(default: Int? = null, name: Name? = null, inherited: Boolean = false) = + VisualObjectDelegateWrapper(this, name, default, inherited) { it.int } -fun VisualObject.node(key: String? = null, inherited: Boolean = true) = - VisualObjectDelegateWrapper(this, key?.toName(), null, inherited) { it.node } +fun VisualObject.node(name: Name? = null, inherited: Boolean = true) = + VisualObjectDelegateWrapper(this, name, null, inherited) { it.node } -fun VisualObject.item(key: String? = null, inherited: Boolean = true) = - VisualObjectDelegateWrapper(this, key?.toName(), null, inherited) { it } +fun VisualObject.item(name: Name? = null, inherited: Boolean = true) = + VisualObjectDelegateWrapper(this, name, null, inherited) { it } //fun Configurable.spec(spec: Specification, key: String? = null) = ChildConfigDelegate(key) { spec.wrap(this) } @JvmName("safeString") -fun VisualObject.string(default: String, key: String? = null, inherited: Boolean = false) = - VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.string } +fun VisualObject.string(default: String, name: Name? = null, inherited: Boolean = false) = + VisualObjectDelegateWrapper(this, name, default, inherited) { it.string } @JvmName("safeBoolean") -fun VisualObject.boolean(default: Boolean, key: String? = null, inherited: Boolean = false) = - VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.boolean } +fun VisualObject.boolean(default: Boolean, name: Name? = null, inherited: Boolean = false) = + VisualObjectDelegateWrapper(this, name, default, inherited) { it.boolean } @JvmName("safeNumber") -fun VisualObject.number(default: Number, key: String? = null, inherited: Boolean = false) = - VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.number } +fun VisualObject.number(default: Number, name: Name? = null, inherited: Boolean = false) = + VisualObjectDelegateWrapper(this, name, default, inherited) { it.number } @JvmName("safeDouble") -fun VisualObject.double(default: Double, key: String? = null, inherited: Boolean = false) = - VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.double } +fun VisualObject.double(default: Double, name: Name? = null, inherited: Boolean = false) = + VisualObjectDelegateWrapper(this, name, default, inherited) { it.double } @JvmName("safeInt") -fun VisualObject.int(default: Int, key: String? = null, inherited: Boolean = false) = - VisualObjectDelegateWrapper(this, key?.toName(), default, inherited) { it.int } +fun VisualObject.int(default: Int, name: Name? = null, inherited: Boolean = false) = + VisualObjectDelegateWrapper(this, name, default, inherited) { it.int } -inline fun > VisualObject.enum(default: E, key: String? = null, inherited: Boolean = false) = - VisualObjectDelegateWrapper( - this, - key?.let { NameToken(it).asName() }, - default, - inherited - ) { item -> item.string?.let { enumValueOf(it) } } +inline fun > VisualObject.enum(default: E, name: Name? = null, inherited: Boolean = false) = + VisualObjectDelegateWrapper(this, name, default, inherited) { + item -> item.string?.let { enumValueOf(it) } + } //merge properties fun VisualObject.merge( - key: String? = null, + name: Name? = null, transformer: (Sequence>) -> T ): ReadOnlyProperty { return object : ReadOnlyProperty { override fun getValue(thisRef: Any?, property: KProperty<*>): T { - val name = key?.toName() ?: property.name.asName() + val actualName = name ?: property.name.asName() val sequence = sequence> { var thisObj: VisualObject? = this@merge while (thisObj != null) { - thisObj.config[name]?.let { yield(it) } + thisObj.config[actualName]?.let { yield(it) } thisObj = thisObj.parent } } diff --git a/dataforge-vis-common/src/jsMain/kotlin/hep/dataforge/vis/js/editor/jsTree.kt b/dataforge-vis-common/src/jsMain/kotlin/hep/dataforge/vis/js/editor/jsTree.kt index 3cf4a985..dde67544 100644 --- a/dataforge-vis-common/src/jsMain/kotlin/hep/dataforge/vis/js/editor/jsTree.kt +++ b/dataforge-vis-common/src/jsMain/kotlin/hep/dataforge/vis/js/editor/jsTree.kt @@ -26,12 +26,11 @@ fun Element.displayObjectTree( } private fun TagConsumer.subTree( - fullName: Name, + name: Name, obj: VisualObject, clickCallback: (Name) -> Unit ) { -// val fullName = parentName + token - val token = fullName.last()?.toString()?:"World" + val token = name.last()?.toString()?:"World" //display as node if any child is visible if (obj is VisualGroup && obj.children.keys.any { !it.body.startsWith("@") }) { @@ -40,7 +39,7 @@ private fun TagConsumer.subTree( toggle = span("objTree-caret") label("objTree-label") { +token - onClickFunction = { clickCallback(fullName) } + onClickFunction = { clickCallback(name) } } } val subtree = ul("objTree-subtree") @@ -55,7 +54,7 @@ private fun TagConsumer.subTree( .forEach { (childToken, child) -> append { li().apply { - subTree(fullName + childToken, child, clickCallback) + subTree(name + childToken, child, clickCallback) } } } @@ -70,7 +69,7 @@ private fun TagConsumer.subTree( span("objTree-leaf") label("objTree-label") { +token - onClickFunction = { clickCallback(fullName) } + onClickFunction = { clickCallback(name) } } } } diff --git a/dataforge-vis-spatial-gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/visualGDMLJvm.kt b/dataforge-vis-spatial-gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/visualGDMLJvm.kt index ae1904f5..1127cead 100644 --- a/dataforge-vis-spatial-gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/visualGDMLJvm.kt +++ b/dataforge-vis-spatial-gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/visualGDMLJvm.kt @@ -8,7 +8,7 @@ import java.nio.file.Path fun GDML.Companion.readFile(file: Path): GDML { 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 = {}) { diff --git a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Material3D.kt b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Material3D.kt index 342016cb..f489c62a 100644 --- a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Material3D.kt +++ b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Material3D.kt @@ -7,16 +7,29 @@ import hep.dataforge.names.plus import hep.dataforge.values.ValueType import hep.dataforge.vis.common.Colors 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 class Material3D(override val config: Config) : Specific { + /** + * Primary web-color for the material + */ 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) + /** + * Replace material by wire frame + */ var wireframe by boolean(false, WIREFRAME_KEY) companion object : Specification { @@ -25,7 +38,7 @@ class Material3D(override val config: Config) : Specific { val MATERIAL_KEY = "material".asName() internal val COLOR_KEY = "color".asName() 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() val MATERIAL_OPACITY_KEY = MATERIAL_KEY + OPACITY_KEY 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) { setProperty(MATERIAL_COLOR_KEY, rgb) } @@ -76,16 +95,15 @@ var VisualObject3D.color: String? setProperty(MATERIAL_COLOR_KEY, value) } -//var VisualObject3D.material: Material3D? -// get() = getProperty(MATERIAL_KEY).node?.let { Material3D.wrap(it) } -// set(value) = setProperty(MATERIAL_KEY, value?.config) +val VisualObject3D.material: Material3D? + get() = getProperty(MATERIAL_KEY).node?.let { Material3D.wrap(it) } fun VisualObject3D.material(builder: Material3D.() -> Unit) { - val node = config[Material3D.MATERIAL_KEY].node + val node = config[MATERIAL_KEY].node if (node != null) { Material3D.update(node, builder) } else { - config[Material3D.MATERIAL_KEY] = Material3D(builder) + config[MATERIAL_KEY] = Material3D(builder) } } diff --git a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/PolyLine.kt b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/PolyLine.kt index 75d00ab4..eb850165 100644 --- a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/PolyLine.kt +++ b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/PolyLine.kt @@ -4,8 +4,10 @@ package hep.dataforge.vis.spatial import hep.dataforge.io.serialization.ConfigSerializer 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.number import kotlinx.serialization.Serializable import kotlinx.serialization.UseSerializers @@ -19,7 +21,11 @@ class PolyLine(var points: List) : AbstractVisualObject(), VisualObject override var scale: Point3D? = null //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() + } } diff --git a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualObject3D.kt b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualObject3D.kt index db95754a..8e94ed65 100644 --- a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualObject3D.kt +++ b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualObject3D.kt @@ -22,22 +22,10 @@ interface VisualObject3D : VisualObject { var rotation: 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 { val VISIBLE_KEY = "visible".asName() - val SELECTED_KEY = "selected".asName() +// val SELECTED_KEY = "selected".asName() val DETAIL_KEY = "detail".asName() val LAYER_KEY = "layer".asName() val IGNORE_KEY = "ignore".asName() diff --git a/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeLineFactory.kt b/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeLineFactory.kt index d2e8d52e..945bfc44 100644 --- a/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeLineFactory.kt +++ b/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeLineFactory.kt @@ -18,11 +18,10 @@ object ThreeLineFactory : ThreeFactory { vertices = obj.points.toTypedArray() } - val material = - ThreeMaterials.getLineMaterial(obj.getProperty(MeshThreeFactory.EDGES_MATERIAL_KEY).node) + val material = ThreeMaterials.getLineMaterial(obj.getProperty(MeshThreeFactory.EDGES_MATERIAL_KEY).node) 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 { updatePosition(obj) diff --git a/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeMaterials.kt b/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeMaterials.kt index ff3ddcf9..d0d7d390 100644 --- a/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeMaterials.kt +++ b/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeMaterials.kt @@ -39,10 +39,10 @@ object ThreeMaterials { fun getMaterial(visualObject3D: VisualObject3D): Material { 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 { 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 transparent = opacity < 1.0 wireframe = meta[Material3D.WIREFRAME_KEY].boolean ?: false diff --git a/dataforge-vis-spatial/src/jvmMain/kotlin/hep/dataforge/vis/spatial/fx/FXMaterials.kt b/dataforge-vis-spatial/src/jvmMain/kotlin/hep/dataforge/vis/spatial/fx/FXMaterials.kt index af8cb2ee..bdaf52dc 100644 --- a/dataforge-vis-spatial/src/jvmMain/kotlin/hep/dataforge/vis/spatial/fx/FXMaterials.kt +++ b/dataforge-vis-spatial/src/jvmMain/kotlin/hep/dataforge/vis/spatial/fx/FXMaterials.kt @@ -66,7 +66,7 @@ fun MetaItem<*>?.material(): Material { is MetaItem.NodeItem -> PhongMaterial().apply { val opacity = node[Material3D.OPACITY_KEY].double ?: 1.0 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 } } } diff --git a/demo/gdml/src/jsMain/resources/cubes.gdml b/demo/gdml/src/commonMain/resources/cubes.gdml similarity index 100% rename from demo/gdml/src/jsMain/resources/cubes.gdml rename to demo/gdml/src/commonMain/resources/cubes.gdml diff --git a/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/saveToJson.kt b/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/saveToJson.kt new file mode 100644 index 00000000..09ab2053 --- /dev/null +++ b/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/saveToJson.kt @@ -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) +} \ No newline at end of file