forked from kscience/visionforge
WIP Root to Solid conversion
This commit is contained in:
parent
df30f8ecc3
commit
193665b99a
@ -25,10 +25,10 @@ private fun <T> jsonRootDeserializer(tSerializer: KSerializer<T>, builder: (Json
|
||||
/**
|
||||
* Load Json encoded TObject
|
||||
*/
|
||||
public fun TObject.Companion.decodeFromJson(serializer: KSerializer<out TObject>, jsonElement: JsonElement): TObject =
|
||||
public fun <T: TObject> TObject.Companion.decodeFromJson(serializer: KSerializer<T>, jsonElement: JsonElement): T =
|
||||
RootDecoder.decode(serializer, jsonElement)
|
||||
|
||||
public fun TObject.Companion.decodeFromString(serializer: KSerializer<out TObject>, string: String): TObject {
|
||||
public fun <T: TObject> TObject.Companion.decodeFromString(serializer: KSerializer<T>, string: String): T {
|
||||
val json = RootDecoder.json.parseToJsonElement(string)
|
||||
return RootDecoder.decode(serializer, json)
|
||||
}
|
||||
@ -49,12 +49,14 @@ private object RootDecoder {
|
||||
//Forward ref for shapes
|
||||
when (tSerializer.descriptor.serialName) {
|
||||
"TGeoShape" -> return TGeoShapeRef {
|
||||
//Should be not null at the moment of actualization
|
||||
refCache[refId].value as TGeoShape
|
||||
refCache[refId].getOrPutValue {
|
||||
input.json.decodeFromJsonElement(tSerializer, it) as TGeoShape
|
||||
}
|
||||
} as T
|
||||
"TGeoVolumeAssembly" -> return TGeoVolumeAssemblyRef {
|
||||
//Should be not null at the moment of actualization
|
||||
refCache[refId].value as TGeoVolumeAssembly
|
||||
refCache[refId].getOrPutValue {
|
||||
input.json.decodeFromJsonElement(tSerializer, it) as TGeoVolumeAssembly
|
||||
}
|
||||
} as T
|
||||
//Do unref
|
||||
else -> refCache[refId]
|
||||
@ -138,7 +140,7 @@ private object RootDecoder {
|
||||
}
|
||||
|
||||
|
||||
fun decode(sourceDeserializer: KSerializer<out TObject>, source: JsonElement): TObject {
|
||||
fun <T: TObject> decode(sourceDeserializer: KSerializer<T>, source: JsonElement): T {
|
||||
val refCache = ArrayList<RefEntry>()
|
||||
|
||||
fun fillCache(element: JsonElement) {
|
||||
|
@ -2,7 +2,7 @@ package ru.mipt.npm.root
|
||||
|
||||
import space.kscience.dataforge.names.Name
|
||||
import space.kscience.dataforge.names.asName
|
||||
import space.kscience.visionforge.setPropertyNode
|
||||
import space.kscience.dataforge.names.plus
|
||||
import space.kscience.visionforge.solid.*
|
||||
import kotlin.math.PI
|
||||
import kotlin.math.atan2
|
||||
@ -19,8 +19,6 @@ private operator fun Number.times(f: Float) = toFloat() * f
|
||||
|
||||
private fun degToRad(d: Double) = d * PI / 180.0
|
||||
|
||||
private class GdmlLoader {
|
||||
|
||||
// converting to XYZ to Tait–Bryan angles according to https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
|
||||
private fun Solid.rotate(rot: DoubleArray) {
|
||||
val xAngle = atan2(-rot[5], rot[8])
|
||||
@ -58,7 +56,7 @@ private class GdmlLoader {
|
||||
}
|
||||
}
|
||||
|
||||
fun SolidGroup.addShape(shape: TGeoShape) {
|
||||
private fun SolidGroup.addShape(shape: TGeoShape) {
|
||||
when (shape) {
|
||||
is TGeoShapeRef -> addShape(shape.value)
|
||||
is TGeoCompositeShape -> {
|
||||
@ -115,7 +113,7 @@ private class GdmlLoader {
|
||||
}
|
||||
}
|
||||
|
||||
fun SolidGroup.node(obj: TGeoNode) {
|
||||
private fun SolidGroup.node(obj: TGeoNode) {
|
||||
if (obj.fVolume != null) {
|
||||
volume(obj.fVolume).apply {
|
||||
when (obj) {
|
||||
@ -130,7 +128,7 @@ private class GdmlLoader {
|
||||
}
|
||||
}
|
||||
|
||||
fun buildGroup(volume: TGeoVolume): SolidGroup {
|
||||
private fun buildGroup(volume: TGeoVolume): SolidGroup {
|
||||
return if (volume is TGeoVolumeAssemblyRef) {
|
||||
buildGroup(volume.value)
|
||||
} else {
|
||||
@ -147,30 +145,36 @@ private class GdmlLoader {
|
||||
|
||||
private val SolidGroup.rootPrototypes: SolidGroup get() = (parent as? SolidGroup)?.rootPrototypes ?: this
|
||||
|
||||
fun SolidGroup.volume(volume: TGeoVolume): SolidGroup {
|
||||
private fun SolidGroup.volume(volume: TGeoVolume, cache: Boolean = true): Solid {
|
||||
val group = buildGroup(volume)
|
||||
val ref = rootPrototypes.prototypes {
|
||||
|
||||
}
|
||||
set(volume.fName.ifEmpty { null }?.asName(), group)
|
||||
return group
|
||||
return if (!cache) {
|
||||
group
|
||||
} else newRef(
|
||||
name = volume.fName.ifEmpty { null },
|
||||
obj = group,
|
||||
prototypeHolder = rootPrototypes,
|
||||
templateName = volumesName + Name.parse(volume.fName.ifEmpty { group.toString() })
|
||||
)
|
||||
}
|
||||
|
||||
fun load(geo: TGeoManager): SolidGroup {
|
||||
/**
|
||||
* A special group for local templates
|
||||
*/
|
||||
val proto = SolidGroup()
|
||||
// private fun load(geo: TGeoManager): SolidGroup {
|
||||
//// /**
|
||||
//// * A special group for local templates
|
||||
//// */
|
||||
//// val proto = SolidGroup()
|
||||
////
|
||||
//// val solids = proto.group(solidsName) {
|
||||
//// setPropertyNode("edges.enabled", false)
|
||||
//// }
|
||||
////
|
||||
//// val volumes = proto.group(volumesName)
|
||||
////
|
||||
//// val referenceStore = HashMap<Name, MutableList<SolidReferenceGroup>>()
|
||||
// }
|
||||
|
||||
val solids = proto.group(solidsName) {
|
||||
setPropertyNode("edges.enabled", false)
|
||||
|
||||
public fun TGeoManager.toSolid(): SolidGroup = SolidGroup {
|
||||
fNodes.arr.forEach {
|
||||
node(it)
|
||||
}
|
||||
|
||||
val volumes = proto.group(volumesName)
|
||||
|
||||
val referenceStore = HashMap<Name, MutableList<SolidReferenceGroup>>()
|
||||
|
||||
TODO()
|
||||
}
|
||||
|
||||
}
|
@ -31,7 +31,9 @@ fun main() {
|
||||
|
||||
val time = measureTimeMillis {
|
||||
val geo = TObject.decodeFromString(TGeoManager.serializer(), string)
|
||||
println(geo)
|
||||
val solid = geo.toSolid()
|
||||
|
||||
println(solid)
|
||||
}
|
||||
|
||||
println(Duration.ofMillis(time))
|
||||
|
@ -158,21 +158,28 @@ public fun SolidGroup.ref(
|
||||
name: String? = null,
|
||||
): SolidReferenceGroup = SolidReferenceGroup(templateName).also { set(name, it) }
|
||||
|
||||
/**
|
||||
* Add new [SolidReferenceGroup] wrapping given object and automatically adding it to the prototypes
|
||||
*/
|
||||
public fun SolidGroup.ref(
|
||||
name: String,
|
||||
templateName: String,
|
||||
name: String? = null,
|
||||
): SolidReferenceGroup = ref(Name.parse(templateName), name)
|
||||
|
||||
/**
|
||||
* Add new [SolidReferenceGroup] wrapping given object and automatically adding it to the prototypes.
|
||||
* One must ensure that [prototypeHolder] is the owner of this group.
|
||||
*/
|
||||
public fun SolidGroup.newRef(
|
||||
name: String?,
|
||||
obj: Solid,
|
||||
templateName: Name = Name.parse(name),
|
||||
prototypeHolder: PrototypeHolder = this,
|
||||
templateName: Name = Name.parse(name ?: obj.toString()),
|
||||
): SolidReferenceGroup {
|
||||
val existing = getPrototype(templateName)
|
||||
if (existing == null) {
|
||||
prototypes {
|
||||
this[templateName] = obj
|
||||
prototypeHolder.prototypes {
|
||||
set(templateName, obj)
|
||||
}
|
||||
} else if (existing != obj) {
|
||||
error("Can't add different prototype on top of existing one")
|
||||
}
|
||||
return this@ref.ref(templateName, name)
|
||||
return ref(templateName, name)
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ fun SolidGroup.refGroup(
|
||||
block: MutableVisionGroup.() -> Unit
|
||||
): SolidReferenceGroup {
|
||||
val group = SolidGroup().apply(block)
|
||||
return ref(name, group, templateName)
|
||||
return newRef(name, group, templateName = templateName)
|
||||
}
|
||||
|
||||
|
||||
@ -42,7 +42,7 @@ class SerializationTest {
|
||||
z = -100
|
||||
}
|
||||
val group = SolidGroup {
|
||||
ref("cube", cube)
|
||||
newRef("cube", cube)
|
||||
refGroup("pg", Name.parse("pg.content")) {
|
||||
sphere(50) {
|
||||
x = -100
|
||||
|
@ -14,7 +14,7 @@ class SolidReferenceTest {
|
||||
val theStyle by style {
|
||||
SolidMaterial.MATERIAL_COLOR_KEY put "red"
|
||||
}
|
||||
ref("test", Box(100f,100f,100f).apply {
|
||||
newRef("test", Box(100f,100f,100f).apply {
|
||||
color("blue")
|
||||
useStyle(theStyle)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user