Add universal file reader to gdml-jvm

This commit is contained in:
Alexander Nozik 2020-02-26 10:02:26 +03:00
parent 691dbad44e
commit de3791a4ef
2 changed files with 61 additions and 24 deletions

View File

@ -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")
)
}
}

View File

@ -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))