From de3791a4efbad3a0f42ef077c8bb73841a23fb7d Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 26 Feb 2020 10:02:26 +0300 Subject: [PATCH] Add universal file reader to gdml-jvm --- .../vis/spatial/gdml/demo/GDMLDemoApp.kt | 35 ++++--------- .../vis/spatial/gdml/demo/readFile.kt | 50 +++++++++++++++++++ 2 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 demo/gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/readFile.kt diff --git a/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/GDMLDemoApp.kt b/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/GDMLDemoApp.kt index b4a75c37..23ac0290 100644 --- a/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/GDMLDemoApp.kt +++ b/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/GDMLDemoApp.kt @@ -4,15 +4,13 @@ import hep.dataforge.context.Global import hep.dataforge.vis.fx.editor.VisualObjectEditorFragment import hep.dataforge.vis.fx.editor.VisualObjectTreeFragment import hep.dataforge.vis.spatial.Material3D +import hep.dataforge.vis.spatial.Visual3DPlugin +import hep.dataforge.vis.spatial.VisualGroup3D import hep.dataforge.vis.spatial.fx.FX3DPlugin import hep.dataforge.vis.spatial.fx.FXCanvas3D -import hep.dataforge.vis.spatial.gdml.LUnit -import hep.dataforge.vis.spatial.gdml.readFile -import hep.dataforge.vis.spatial.gdml.toVisual import javafx.geometry.Orientation import javafx.scene.Parent import javafx.stage.FileChooser -import scientifik.gdml.GDML import tornadofx.* class GDMLDemoApp : App(GDMLView::class) @@ -38,24 +36,10 @@ class GDMLView : View() { buttonbar { button("Load GDML") { action { - val file = chooseFile("Select a GDML file", filters = gdmlFilter).firstOrNull() - if (file != null) { - val obj = GDML.readFile(file.toPath()).toVisual { - lUnit = LUnit.CM - - solidConfiguration = { parent, solid -> - if (solid.name == "cave") { - setProperty(Material3D.MATERIAL_WIREFRAME_KEY, true) - } - if (parent.physVolumes.isNotEmpty()) { - useStyle("opaque") { - Material3D.MATERIAL_OPACITY_KEY put 0.3 - } - } - } - } - canvas.render(obj) - } + val file = chooseFile("Select a GDML/json file", filters = fileNameFilter).firstOrNull() + ?: return@action + val visual: VisualGroup3D = Visual3DPlugin.readFile(file) + canvas.render(visual) } } } @@ -68,8 +52,11 @@ class GDMLView : View() { } companion object { - private val gdmlFilter = arrayOf( - FileChooser.ExtensionFilter("GDML", "*.gdml", "*.xml") + private val fileNameFilter = arrayOf( + FileChooser.ExtensionFilter("GDML", "*.gdml", "*.xml"), + FileChooser.ExtensionFilter("JSON", "*.json"), + FileChooser.ExtensionFilter("JSON.ZIP", "*.json.zip"), + FileChooser.ExtensionFilter("JSON.GZ", "*.json.gz") ) } } diff --git a/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/readFile.kt b/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/readFile.kt new file mode 100644 index 00000000..11c4e162 --- /dev/null +++ b/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/readFile.kt @@ -0,0 +1,50 @@ +package hep.dataforge.vis.spatial.gdml.demo + +import hep.dataforge.vis.spatial.Material3D +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.util.zip.GZIPInputStream +import java.util.zip.ZipInputStream + +fun Visual3DPlugin.Companion.readFile(file: File): VisualGroup3D = when { + file.extension == "gdml" || file.extension == "xml" -> { + GDML.readFile(file.toPath()).toVisual { + lUnit = LUnit.CM + + solidConfiguration = { parent, solid -> + if (solid.name == "cave") { + setProperty(Material3D.MATERIAL_WIREFRAME_KEY, true) + } + if (parent.physVolumes.isNotEmpty()) { + useStyle("opaque") { + Material3D.MATERIAL_OPACITY_KEY put 0.3 + } + } + } + } + } + file.extension == "json" -> Visual3DPlugin.json + .parse(VisualGroup3D.serializer(), file.readText()) + file.name.endsWith("json.zip") -> { + file.inputStream().use { + val unzip = ZipInputStream(it, Charsets.UTF_8) + val text = unzip.readAllBytes().decodeToString() + json.parse(VisualGroup3D.serializer(), text) + } + } + file.name.endsWith("json.gz") -> { + file.inputStream().use { + val unzip = GZIPInputStream(it) + val text = unzip.readAllBytes().decodeToString() + json.parse(VisualGroup3D.serializer(), text) + } + } + else -> error("Unknown extension ${file.extension}") +} + +fun Visual3DPlugin.Companion.readFile(fileName: String): VisualGroup3D = readFile(File(fileName)) \ No newline at end of file