From 68704086e9564689433d2db4112a1d4514bcf3f1 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 26 Aug 2021 09:52:35 +0300 Subject: [PATCH] WIP Root to Solid conversion --- .../kotlin/ru/mipt/npm/root/TGeoManager.kt | 8 +++---- .../kotlin/ru/mipt/npm/root/jsonToRoot.kt | 23 ++++++++++--------- .../kotlin/ru/mipt/npm/root/rootToSolid.kt | 15 ++++++++---- .../kotlin/ru/mipt/npm/root/loadBMN.kt | 6 ++--- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/TGeoManager.kt b/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/TGeoManager.kt index 41607fbd..8b2c3c26 100644 --- a/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/TGeoManager.kt +++ b/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/TGeoManager.kt @@ -9,14 +9,14 @@ import kotlinx.serialization.Serializable public class TGeoManager : TNamed() { @Contextual - public val fMatrices: TObjArray = TObjArray.getEmpty() + public val fMatrices: TObjArray<@Contextual TGeoMatrix> = TObjArray.getEmpty() @Contextual - public val fShapes: TObjArray = TObjArray.getEmpty() + public val fShapes: TObjArray<@Contextual TGeoShape> = TObjArray.getEmpty() @Contextual - public val fVolumes: TObjArray = TObjArray.getEmpty() + public val fVolumes: TObjArray<@Contextual TGeoVolume> = TObjArray.getEmpty() @Contextual - public val fNodes: TObjArray = TObjArray.getEmpty() + public val fNodes: TObjArray<@Contextual TGeoNode> = TObjArray.getEmpty() } diff --git a/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/jsonToRoot.kt b/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/jsonToRoot.kt index 585accf0..1b7fe798 100644 --- a/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/jsonToRoot.kt +++ b/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/jsonToRoot.kt @@ -81,19 +81,19 @@ private object RootDecoder { ): SerializersModule = SerializersModule { include(serializersModule) - contextual(TGeoManager.serializer().unref(refCache)) + //contextual(TGeoManager.serializer().unref(refCache)) contextual(TObjArray::class) { TObjArray.serializer(it[0]).unref(refCache) } - contextual(TGeoVolumeAssembly.serializer().unref(refCache)) - contextual(TGeoShapeAssembly.serializer().unref(refCache)) + //contextual(TGeoVolumeAssembly.serializer().unref(refCache)) + //contextual(TGeoShapeAssembly.serializer().unref(refCache)) contextual(TGeoRotation.serializer().unref(refCache)) contextual(TGeoMedium.serializer().unref(refCache)) - contextual(TGeoVolume.serializer().unref(refCache)) - contextual(TGeoMatrix.serializer().unref(refCache)) - contextual(TGeoNode.serializer().unref(refCache)) - contextual(TGeoNodeOffset.serializer().unref(refCache)) - contextual(TGeoNodeMatrix.serializer().unref(refCache)) - contextual(TGeoShape.serializer().unref(refCache)) - contextual(TObject.serializer().unref(refCache)) + //contextual(TGeoVolume.serializer().unref(refCache)) + //contextual(TGeoMatrix.serializer().unref(refCache)) + //contextual(TGeoNode.serializer().unref(refCache)) + //contextual(TGeoNodeOffset.serializer().unref(refCache)) + //contextual(TGeoNodeMatrix.serializer().unref(refCache)) + //contextual(TGeoShape.serializer().unref(refCache)) + //contextual(TObject.serializer().unref(refCache)) polymorphicDefault(TGeoShape::class) { @@ -245,7 +245,8 @@ private object RootDecoder { subclass(TGeoNodeOffset.serializer()) } - polymorphic(TGeoVolume::class, TGeoVolume.serializer()) { + polymorphic(TGeoVolume::class) { + subclass(TGeoVolume.serializer()) subclass(TGeoVolumeAssembly.serializer()) } } diff --git a/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/rootToSolid.kt b/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/rootToSolid.kt index 9cbbb64b..ee14324d 100644 --- a/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/rootToSolid.kt +++ b/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/rootToSolid.kt @@ -115,7 +115,7 @@ private fun SolidGroup.addShape(shape: TGeoShape) { private fun SolidGroup.node(obj: TGeoNode) { if (obj.fVolume != null) { - volume(obj.fVolume).apply { + volume(obj.fVolume, obj.fName).apply { when (obj) { is TGeoNodeMatrix -> { useMatrix(obj.fMatrix) @@ -145,15 +145,22 @@ private fun buildGroup(volume: TGeoVolume): SolidGroup { private val SolidGroup.rootPrototypes: SolidGroup get() = (parent as? SolidGroup)?.rootPrototypes ?: this -private fun SolidGroup.volume(volume: TGeoVolume, cache: Boolean = true): Solid { +private fun SolidGroup.volume(volume: TGeoVolume, name: String? = null, cache: Boolean = true): Solid { val group = buildGroup(volume) + val combinedName = if (volume.fName.isEmpty()) { + name + } else if (name == null) { + volume.fName + } else { + "${name}_${volume.fName}" + } return if (!cache) { group } else newRef( - name = volume.fName.ifEmpty { null }, + name = combinedName, obj = group, prototypeHolder = rootPrototypes, - templateName = volumesName + Name.parse(volume.fName.ifEmpty { group.toString() }) + templateName = volumesName + Name.parse(combinedName ?: "volume[${group.hashCode()}]") ) } diff --git a/cern-root-loader/src/jvmTest/kotlin/ru/mipt/npm/root/loadBMN.kt b/cern-root-loader/src/jvmTest/kotlin/ru/mipt/npm/root/loadBMN.kt index 7d5f4b24..7fcd1e22 100644 --- a/cern-root-loader/src/jvmTest/kotlin/ru/mipt/npm/root/loadBMN.kt +++ b/cern-root-loader/src/jvmTest/kotlin/ru/mipt/npm/root/loadBMN.kt @@ -1,12 +1,12 @@ package ru.mipt.npm.root import kotlinx.serialization.json.* +import space.kscience.visionforge.solid.Solids import java.time.Duration import kotlin.system.measureTimeMillis private fun JsonElement.countTypes(): Sequence = sequence { - val json = this@countTypes - when (json){ + when (val json = this@countTypes){ is JsonObject -> { json["_typename"]?.let { yield(it.jsonPrimitive.content) } json.values.forEach { yieldAll(it.countTypes()) } @@ -33,7 +33,7 @@ fun main() { val geo = TObject.decodeFromString(TGeoManager.serializer(), string) val solid = geo.toSolid() - println(solid) + println(Solids.encodeToString(solid)) } println(Duration.ofMillis(time))