From d1607459a120667f00f575949d7c92704072d475 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 21 Nov 2020 10:04:15 +0300 Subject: [PATCH] Move Vector3D to common instead of expect/actual --- .../vision/gdml/demo/GDMLAppComponent.kt | 5 +- .../kotlin/ru/mipt/npm/muon/monitor/Event.kt | 3 - .../vision/solid/demo/VariableBox.kt | 3 - .../vision/bootstrap/tabComponent.kt | 9 -- .../vision/bootstrap/threeControls.kt | 2 +- .../vision/editor/ColorValueChooser.kt | 2 +- .../vision/editor/ComboBoxValueChooser.kt | 2 +- .../dataforge/vision/gdml/GDMLTransformer.kt | 6 +- .../hep/dataforge/vision/solid/BasicSolid.kt | 3 - .../kotlin/hep/dataforge/vision/solid/Box.kt | 3 - .../hep/dataforge/vision/solid/Composite.kt | 2 - .../hep/dataforge/vision/solid/ConeSegment.kt | 3 - .../hep/dataforge/vision/solid/Convex.kt | 3 - .../hep/dataforge/vision/solid/Extruded.kt | 2 - .../hep/dataforge/vision/solid/PolyLine.kt | 3 - .../hep/dataforge/vision/solid/SolidGroup.kt | 32 +++++- .../dataforge/vision/solid/SolidManager.kt | 8 +- .../hep/dataforge/vision/solid/geometry.kt | 46 ++++---- .../dataforge/vision/solid/serialization.kt | 100 ------------------ .../solid/transform/RemoveSingleChild.kt | 4 +- .../hep/dataforge/vision/solid/geometryJs.kt | 14 --- .../solid/three/ThreeGeometryBuilder.kt | 8 +- .../vision/solid/three/ThreeLineFactory.kt | 4 +- .../hep/dataforge/vision/solid/three/three.kt | 4 +- .../dataforge/vision/solid/fx/FX3DPlugin.kt | 3 +- .../hep/dataforge/vision/solid/geometryJVM.kt | 50 --------- 26 files changed, 78 insertions(+), 246 deletions(-) delete mode 100644 visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/serialization.kt delete mode 100644 visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/geometryJs.kt delete mode 100644 visionforge-solid/src/jvmMain/kotlin/hep/dataforge/vision/solid/geometryJVM.kt diff --git a/demo/gdml/src/jsMain/kotlin/hep/dataforge/vision/gdml/demo/GDMLAppComponent.kt b/demo/gdml/src/jsMain/kotlin/hep/dataforge/vision/gdml/demo/GDMLAppComponent.kt index fd9beb97..09ae9a66 100644 --- a/demo/gdml/src/jsMain/kotlin/hep/dataforge/vision/gdml/demo/GDMLAppComponent.kt +++ b/demo/gdml/src/jsMain/kotlin/hep/dataforge/vision/gdml/demo/GDMLAppComponent.kt @@ -78,7 +78,7 @@ val GDMLApp = functionalComponent("GDMLApp") { props -> } flexColumn { css { - flex(10.0, 10.0, FlexBasis.zero) + flex(1.0, 1.0, FlexBasis.auto) } styledDiv { css { @@ -106,10 +106,11 @@ val GDMLApp = functionalComponent("GDMLApp") { props -> } flexColumn { css { - minWidth = 500.px + minWidth = 400.px height = 100.pct margin(left = 4.px, right = 4.px, top = 4.px) border(1.px, BorderStyle.solid, Color.lightGray) + flex(0.0,1.0, FlexBasis.zero) } fileDrop("(drag file here)") { files -> val file = files?.get(0) diff --git a/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Event.kt b/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Event.kt index b0f8ca04..3d64e67a 100644 --- a/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Event.kt +++ b/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Event.kt @@ -1,10 +1,7 @@ -@file:UseSerializers(Point3DSerializer::class) package ru.mipt.npm.muon.monitor import hep.dataforge.vision.solid.Point3D -import hep.dataforge.vision.solid.Point3DSerializer import kotlinx.serialization.Serializable -import kotlinx.serialization.UseSerializers typealias Track = List diff --git a/demo/spatial-showcase/src/jsMain/kotlin/hep/dataforge/vision/solid/demo/VariableBox.kt b/demo/spatial-showcase/src/jsMain/kotlin/hep/dataforge/vision/solid/demo/VariableBox.kt index 8df28ddc..983b92b7 100644 --- a/demo/spatial-showcase/src/jsMain/kotlin/hep/dataforge/vision/solid/demo/VariableBox.kt +++ b/demo/spatial-showcase/src/jsMain/kotlin/hep/dataforge/vision/solid/demo/VariableBox.kt @@ -1,5 +1,3 @@ -@file:UseSerializers(Point3DSerializer::class) - package hep.dataforge.vision.solid.demo import hep.dataforge.meta.int @@ -20,7 +18,6 @@ import info.laht.threekt.core.BufferGeometry import info.laht.threekt.core.Object3D import info.laht.threekt.geometries.BoxBufferGeometry import info.laht.threekt.objects.Mesh -import kotlinx.serialization.UseSerializers import kotlin.math.max internal fun SolidGroup.varBox( diff --git a/ui/bootstrap/src/main/kotlin/hep/dataforge/vision/bootstrap/tabComponent.kt b/ui/bootstrap/src/main/kotlin/hep/dataforge/vision/bootstrap/tabComponent.kt index 1b37644d..5f22a353 100644 --- a/ui/bootstrap/src/main/kotlin/hep/dataforge/vision/bootstrap/tabComponent.kt +++ b/ui/bootstrap/src/main/kotlin/hep/dataforge/vision/bootstrap/tabComponent.kt @@ -1,7 +1,6 @@ package hep.dataforge.vision.bootstrap import hep.dataforge.vision.react.flexColumn -import kotlinx.css.* import kotlinx.html.DIV import kotlinx.html.classes import kotlinx.html.js.onClickFunction @@ -10,7 +9,6 @@ import react.dom.button import react.dom.li import react.dom.ul import styled.StyledDOMBuilder -import styled.css import styled.styledDiv public external class TabProps : RProps { @@ -40,9 +38,6 @@ public val TabPane: FunctionalComponent = functionalComponent("Tab } flexColumn { - css { - flexGrow = 1.0 - } ul("nav nav-tabs") { childrenProps.forEach { cp -> li("nav-item") { @@ -74,10 +69,6 @@ public class TabBuilder(internal val parentBuilder: RBuilder) { this.title = title } styledDiv { - css { - height = 100.pct - overflowY = Overflow.auto - } builder() } } diff --git a/ui/bootstrap/src/main/kotlin/hep/dataforge/vision/bootstrap/threeControls.kt b/ui/bootstrap/src/main/kotlin/hep/dataforge/vision/bootstrap/threeControls.kt index a7d67d63..68d90705 100644 --- a/ui/bootstrap/src/main/kotlin/hep/dataforge/vision/bootstrap/threeControls.kt +++ b/ui/bootstrap/src/main/kotlin/hep/dataforge/vision/bootstrap/threeControls.kt @@ -36,7 +36,7 @@ public val ThreeControls: FunctionalComponent = functionalCo h2 { +"Object tree" } styledDiv { css { - flex(1.0, 0.0, FlexBasis.auto) + flex(1.0, 1.0, FlexBasis.inherit) } props.canvas.content?.let { objectTree(it, props.selected, props.onSelect) diff --git a/visionforge-core/src/jvmMain/kotlin/hep/dataforge/vision/editor/ColorValueChooser.kt b/visionforge-core/src/jvmMain/kotlin/hep/dataforge/vision/editor/ColorValueChooser.kt index fe2101ec..117210c1 100644 --- a/visionforge-core/src/jvmMain/kotlin/hep/dataforge/vision/editor/ColorValueChooser.kt +++ b/visionforge-core/src/jvmMain/kotlin/hep/dataforge/vision/editor/ColorValueChooser.kt @@ -14,7 +14,7 @@ import tornadofx.* /** * Created by darksnake on 01-May-17. */ -class ColorValueChooser : ValueChooserBase() { +public class ColorValueChooser : ValueChooserBase() { private fun ColorPicker.setColor(value: Value?) { if (value != null && value != Null) { try { diff --git a/visionforge-core/src/jvmMain/kotlin/hep/dataforge/vision/editor/ComboBoxValueChooser.kt b/visionforge-core/src/jvmMain/kotlin/hep/dataforge/vision/editor/ComboBoxValueChooser.kt index 72798444..051469c7 100644 --- a/visionforge-core/src/jvmMain/kotlin/hep/dataforge/vision/editor/ComboBoxValueChooser.kt +++ b/visionforge-core/src/jvmMain/kotlin/hep/dataforge/vision/editor/ComboBoxValueChooser.kt @@ -17,7 +17,7 @@ import javafx.scene.control.ComboBox import javafx.util.StringConverter import java.util.* -class ComboBoxValueChooser(val values: Collection? = null) : ValueChooserBase>() { +public class ComboBoxValueChooser(public val values: Collection? = null) : ValueChooserBase>() { // @Override // protected void displayError(String error) { diff --git a/visionforge-gdml/src/commonMain/kotlin/hep/dataforge/vision/gdml/GDMLTransformer.kt b/visionforge-gdml/src/commonMain/kotlin/hep/dataforge/vision/gdml/GDMLTransformer.kt index 75c93cc4..a0607435 100644 --- a/visionforge-gdml/src/commonMain/kotlin/hep/dataforge/vision/gdml/GDMLTransformer.kt +++ b/visionforge-gdml/src/commonMain/kotlin/hep/dataforge/vision/gdml/GDMLTransformer.kt @@ -114,20 +114,20 @@ private class GDMLTransformer(val settings: GDMLTransformerSettings) { ): T = apply { newPos?.let { val point = Point3D(it.x(settings.lUnit), it.y(settings.lUnit), it.z(settings.lUnit)) - if (position != null || point != World.ZERO) { + if (position != null || point != Point3D.ZERO) { position = point } } newRotation?.let { val point = Point3D(it.x(settings.aUnit), it.y(settings.aUnit), it.z(settings.aUnit)) - if (rotation != null || point != World.ZERO) { + if (rotation != null || point != Point3D.ZERO) { rotation = point } //this@withPosition.rotationOrder = RotationOrder.ZXY } newScale?.let { val point = Point3D(it.x, it.y, it.z) - if (scale != null || point != World.ONE) { + if (scale != null || point != Point3D.ONE) { scale = point } } diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/BasicSolid.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/BasicSolid.kt index 40f2543e..93fef60f 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/BasicSolid.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/BasicSolid.kt @@ -15,13 +15,10 @@ import kotlinx.serialization.Serializable public open class BasicSolid : VisionBase(), Solid { override val descriptor: NodeDescriptor get() = Solid.descriptor - @Serializable(Point3DSerializer::class) override var position: Point3D? = null - @Serializable(Point3DSerializer::class) override var rotation: Point3D? = null - @Serializable(Point3DSerializer::class) override var scale: Point3D? = null override fun update(change: Vision) { diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Box.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Box.kt index 31eca267..5bbbcc7d 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Box.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Box.kt @@ -1,5 +1,3 @@ -@file:UseSerializers(Point3DSerializer::class) - package hep.dataforge.vision.solid import hep.dataforge.vision.VisionContainerBuilder @@ -7,7 +5,6 @@ import hep.dataforge.vision.set import hep.dataforge.vision.solid.Solid.Companion.solidEquals import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import kotlinx.serialization.UseSerializers @Serializable @SerialName("solid.box") diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Composite.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Composite.kt index a293dc09..ec036419 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Composite.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Composite.kt @@ -1,4 +1,3 @@ -@file:UseSerializers(Point3DSerializer::class) package hep.dataforge.vision.solid @@ -7,7 +6,6 @@ import hep.dataforge.names.NameToken import hep.dataforge.vision.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import kotlinx.serialization.UseSerializers public enum class CompositeType { UNION, diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/ConeSegment.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/ConeSegment.kt index 31ca2786..15116d93 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/ConeSegment.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/ConeSegment.kt @@ -1,12 +1,9 @@ -@file:UseSerializers(Point3DSerializer::class) - package hep.dataforge.vision.solid import hep.dataforge.vision.VisionContainerBuilder import hep.dataforge.vision.set import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import kotlinx.serialization.UseSerializers import kotlin.math.cos import kotlin.math.sin diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Convex.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Convex.kt index 6a0b9de9..c6425d5c 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Convex.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Convex.kt @@ -1,12 +1,9 @@ -@file:UseSerializers(Point3DSerializer::class) - package hep.dataforge.vision.solid import hep.dataforge.vision.VisionContainerBuilder import hep.dataforge.vision.set import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import kotlinx.serialization.UseSerializers @Serializable @SerialName("solid.convex") diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Extruded.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Extruded.kt index f0f22814..dc539387 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Extruded.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Extruded.kt @@ -1,11 +1,9 @@ -@file:UseSerializers(Point2DSerializer::class, Point3DSerializer::class) package hep.dataforge.vision.solid import hep.dataforge.vision.VisionContainerBuilder import hep.dataforge.vision.set import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import kotlinx.serialization.UseSerializers import kotlin.math.PI import kotlin.math.cos import kotlin.math.sin diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/PolyLine.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/PolyLine.kt index 766eab08..f55cc89a 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/PolyLine.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/PolyLine.kt @@ -1,5 +1,3 @@ -@file:UseSerializers(Point3DSerializer::class) - package hep.dataforge.vision.solid import hep.dataforge.meta.number @@ -11,7 +9,6 @@ import hep.dataforge.vision.props import hep.dataforge.vision.set import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import kotlinx.serialization.UseSerializers @Serializable @SerialName("solid.line") diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidGroup.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidGroup.kt index d441ab6c..991fc09b 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidGroup.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidGroup.kt @@ -5,8 +5,13 @@ import hep.dataforge.meta.descriptors.NodeDescriptor import hep.dataforge.names.Name import hep.dataforge.names.NameToken import hep.dataforge.vision.* +import kotlinx.serialization.KSerializer import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.builtins.MapSerializer +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder public interface PrototypeHolder { public val parent: VisionGroup? @@ -25,7 +30,7 @@ public class SolidGroup : VisionGroupBase(), Solid, PrototypeHolder { /** * A container for templates visible inside this group */ - @Serializable(PrototypesSerializer::class) + @Serializable(Prototypes.Companion::class) override var prototypes: MutableVisionGroup? = null private set @@ -39,13 +44,10 @@ public class SolidGroup : VisionGroupBase(), Solid, PrototypeHolder { }).run(builder) } - @Serializable(Point3DSerializer::class) override var position: Point3D? = null - @Serializable(Point3DSerializer::class) override var rotation: Point3D? = null - @Serializable(Point3DSerializer::class) override var scale: Point3D? = null override fun attachChildren() { @@ -91,7 +93,7 @@ public fun MutableVisionGroup.group(name: String, action: SolidGroup.() -> Unit /** * A special class which works as a holder for prototypes */ -@Serializable(PrototypesSerializer::class) +@Serializable(Prototypes.Companion::class) internal class Prototypes( children: Map = emptyMap(), ) : VisionGroupBase(), PrototypeHolder { @@ -118,4 +120,24 @@ internal class Prototypes( (it as? VisionGroup)?.attachChildren() } } + + companion object : KSerializer { + + private val mapSerializer: KSerializer> = + MapSerializer( + NameToken.serializer(), + Vision.serializer() + ) + + override val descriptor: SerialDescriptor get() = mapSerializer.descriptor + + override fun deserialize(decoder: Decoder): MutableVisionGroup { + val map = mapSerializer.deserialize(decoder) + return Prototypes(map) + } + + override fun serialize(encoder: Encoder, value: MutableVisionGroup) { + mapSerializer.serialize(encoder, value.children) + } + } } diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidManager.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidManager.kt index 64b032d4..1800865d 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidManager.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidManager.kt @@ -13,7 +13,10 @@ import hep.dataforge.vision.VisionGroupBase import hep.dataforge.vision.VisionManager import hep.dataforge.vision.VisionManager.Companion.VISION_SERIALIZER_MODULE_TARGET import kotlinx.serialization.json.Json -import kotlinx.serialization.modules.* +import kotlinx.serialization.modules.PolymorphicModuleBuilder +import kotlinx.serialization.modules.SerializersModule +import kotlinx.serialization.modules.polymorphic +import kotlinx.serialization.modules.subclass import kotlin.reflect.KClass @@ -47,9 +50,6 @@ public class SolidManager(meta: Meta) : AbstractPlugin(meta) { } public val serializersModuleForSolids: SerializersModule = SerializersModule { - contextual(Point3DSerializer) - contextual(Point2DSerializer) - polymorphic(Vision::class) { subclass(VisionGroupBase.serializer()) solids() diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/geometry.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/geometry.kt index 1eb501b0..94c61da5 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/geometry.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/geometry.kt @@ -2,45 +2,47 @@ package hep.dataforge.vision.solid import hep.dataforge.meta.Meta import hep.dataforge.meta.MetaBuilder +import hep.dataforge.meta.double import hep.dataforge.meta.get -import hep.dataforge.meta.number +import kotlinx.serialization.Serializable import kotlin.math.PI -public object World { - public val ZERO: Point3D = Point3D(0.0, 0.0, 0.0) - public val ONE: Point3D = Point3D(1.0, 1.0, 1.0) -} - public const val PI2: Float = 2 * PI.toFloat() -public expect class Point2D(x: Number, y: Number) { - public var x: Double - public var y: Double +@Serializable +public data class Point2D(public var x: Double, public var y: Double){ + public constructor(x: Number, y: Number) : this(x.toDouble(), y.toDouble()) } -public operator fun Point2D.component1(): Double = x -public operator fun Point2D.component2(): Double = y public fun Point2D.toMeta(): Meta = Meta { Solid.X_KEY put x Solid.Y_KEY put y } -internal fun Meta.point2D(): Point2D = Point2D(this["x"].number ?: 0, this["y"].number ?: 0) +internal fun Meta.point2D(): Point2D = Point2D(this["x"].double ?: 0.0, this["y"].double ?: 0.0) -public expect class Point3D(x: Number, y: Number, z: Number) { - public var x: Double - public var y: Double - public var z: Double +@Serializable +public data class Point3D( + public var x: Double, + public var y: Double, + public var z: Double, +) { + public constructor(x: Number, y: Number, z: Number) : this(x.toDouble(), y.toDouble(), z.toDouble()) + + public companion object{ + public val ZERO: Point3D = Point3D(0.0, 0.0, 0.0) + public val ONE: Point3D = Point3D(1.0, 1.0, 1.0) + } } -public expect operator fun Point3D.plus(other: Point3D): Point3D +public operator fun Point3D.plus(other: Point3D): Point3D = Point3D( + this.x + other.x, + this.y + other.y, + this.z + other.z +) -public operator fun Point3D.component1(): Double = x -public operator fun Point3D.component2(): Double = y -public operator fun Point3D.component3(): Double = z - -internal fun Meta.point3D() = Point3D(this["x"].number ?: 0, this["y"].number ?: 0, this["y"].number ?: 0) +internal fun Meta.point3D() = Point3D(this["x"].double ?: 0.0, this["y"].double ?: 0.0, this["y"].double ?: 0.0) public fun Point3D.toMeta(): MetaBuilder = Meta { Solid.X_KEY put x diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/serialization.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/serialization.kt deleted file mode 100644 index fafc0c5c..00000000 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/serialization.kt +++ /dev/null @@ -1,100 +0,0 @@ -package hep.dataforge.vision.solid - -import hep.dataforge.names.NameToken -import hep.dataforge.vision.MutableVisionGroup -import hep.dataforge.vision.Vision -import kotlinx.serialization.* -import kotlinx.serialization.builtins.MapSerializer -import kotlinx.serialization.builtins.nullable -import kotlinx.serialization.builtins.serializer -import kotlinx.serialization.descriptors.SerialDescriptor -import kotlinx.serialization.descriptors.buildClassSerialDescriptor -import kotlinx.serialization.descriptors.element -import kotlinx.serialization.encoding.* - - -@OptIn(ExperimentalSerializationApi::class) -public object Point3DSerializer : KSerializer { - override val descriptor: SerialDescriptor = buildClassSerialDescriptor("hep.dataforge.vis.spatial.Point3D") { - element("x") - element("y") - element("z") - } - - override fun deserialize(decoder: Decoder): Point3D { - var x: Double? = null - var y: Double? = null - var z: Double? = null - decoder.decodeStructure(descriptor) { - loop@ while (true) { - when (val i = decodeElementIndex(descriptor)) { - CompositeDecoder.DECODE_DONE -> break@loop - 0 -> x = decodeNullableSerializableElement(descriptor, 0, Double.serializer().nullable) ?: 0.0 - 1 -> y = decodeNullableSerializableElement(descriptor, 1, Double.serializer().nullable) ?: 0.0 - 2 -> z = decodeNullableSerializableElement(descriptor, 2, Double.serializer().nullable) ?: 0.0 - else -> throw SerializationException("Unknown index $i") - } - } - } - return Point3D(x ?: 0.0, y ?: 0.0, z ?: 0.0) - } - - override fun serialize(encoder: Encoder, value: Point3D) { - encoder.encodeStructure(descriptor) { - if (value.x != 0.0) encodeDoubleElement(descriptor, 0, value.x) - if (value.y != 0.0) encodeDoubleElement(descriptor, 1, value.y) - if (value.z != 0.0) encodeDoubleElement(descriptor, 2, value.z) - } - } -} - -@OptIn(ExperimentalSerializationApi::class) -public object Point2DSerializer : KSerializer { - override val descriptor: SerialDescriptor = buildClassSerialDescriptor("hep.dataforge.vis.spatial.Point2D") { - element("x") - element("y") - } - - override fun deserialize(decoder: Decoder): Point2D { - var x: Double? = null - var y: Double? = null - decoder.decodeStructure(descriptor) { - loop@ while (true) { - when (val i = decodeElementIndex(descriptor)) { - CompositeDecoder.DECODE_DONE -> break@loop - 0 -> x = decodeNullableSerializableElement(descriptor, 0, Double.serializer().nullable) ?: 0.0 - 1 -> y = decodeNullableSerializableElement(descriptor, 1, Double.serializer().nullable) ?: 0.0 - else -> throw SerializationException("Unknown index $i") - } - } - } - return Point2D(x ?: 0.0, y ?: 0.0) - } - - override fun serialize(encoder: Encoder, value: Point2D) { - encoder.encodeStructure(descriptor) { - if (value.x != 0.0) encodeDoubleElement(descriptor, 0, value.x) - if (value.y != 0.0) encodeDoubleElement(descriptor, 1, value.y) - } - } -} - -internal object PrototypesSerializer : KSerializer { - - private val mapSerializer: KSerializer> = - MapSerializer( - NameToken.serializer(), - Vision.serializer() - ) - - override val descriptor: SerialDescriptor get() = mapSerializer.descriptor - - override fun deserialize(decoder: Decoder): MutableVisionGroup { - val map = mapSerializer.deserialize(decoder) - return Prototypes(map) - } - - override fun serialize(encoder: Encoder, value: MutableVisionGroup) { - mapSerializer.serialize(encoder, value.children) - } -} \ No newline at end of file diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/transform/RemoveSingleChild.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/transform/RemoveSingleChild.kt index 7bedebe4..ff879d7e 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/transform/RemoveSingleChild.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/transform/RemoveSingleChild.kt @@ -17,8 +17,8 @@ internal fun mergeChild(parent: VisionGroup, child: Vision): Vision { //parent.properties?.let { config.update(it) } if (this is Solid && parent is Solid) { - position = (position ?: World.ZERO) + (parent.position ?: World.ZERO) - rotation = (parent.rotation ?: World.ZERO) + (parent.rotation ?: World.ZERO) + position = (position ?: Point3D.ZERO) + (parent.position ?: Point3D.ZERO) + rotation = (parent.rotation ?: Point3D.ZERO) + (parent.rotation ?: Point3D.ZERO) scale = when { scale == null && parent.scale == null -> null scale == null -> parent.scale diff --git a/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/geometryJs.kt b/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/geometryJs.kt deleted file mode 100644 index 170e8249..00000000 --- a/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/geometryJs.kt +++ /dev/null @@ -1,14 +0,0 @@ -package hep.dataforge.vision.solid - - -import info.laht.threekt.math.Vector2 -import info.laht.threekt.math.Vector3 -import info.laht.threekt.math.plus - -public actual typealias Point2D = Vector2 - -public actual typealias Point3D = Vector3 - -public actual operator fun Point3D.plus(other: Point3D): Point3D { - return this.plus(other) -} \ No newline at end of file diff --git a/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/three/ThreeGeometryBuilder.kt b/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/three/ThreeGeometryBuilder.kt index c06a0ff6..e0861f98 100644 --- a/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/three/ThreeGeometryBuilder.kt +++ b/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/three/ThreeGeometryBuilder.kt @@ -10,12 +10,14 @@ import info.laht.threekt.core.Face3 import info.laht.threekt.core.Geometry import info.laht.threekt.math.Vector3 +internal fun Point3D.toVector() = Vector3(x, y, z) + /** * An implementation of geometry builder for Three.js [BufferGeometry] */ public class ThreeGeometryBuilder : GeometryBuilder { - private val vertices = ArrayList() + private val vertices = ArrayList() private val faces = ArrayList() private val vertexCache = HashMap() @@ -25,14 +27,14 @@ public class ThreeGeometryBuilder : GeometryBuilder { return if (index > 0) { index } else { - vertices.add(vertex) + vertices.add(vertex.toVector()) vertexCache[vertex] = vertices.size - 1 vertices.size - 1 } } override fun face(vertex1: Point3D, vertex2: Point3D, vertex3: Point3D, normal: Point3D?, meta: Meta) { - val face = Face3(append(vertex1), append(vertex2), append(vertex3), normal ?: Vector3(0, 0, 0)) + val face = Face3(append(vertex1), append(vertex2), append(vertex3), normal?.toVector() ?: Vector3(0, 0, 0)) meta["materialIndex"].int?.let { face.materialIndex = it } meta["color"]?.getColor()?.let { face.color = it } faces.add(face) diff --git a/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/three/ThreeLineFactory.kt b/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/three/ThreeLineFactory.kt index 987f19b1..974b0727 100644 --- a/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/three/ThreeLineFactory.kt +++ b/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/three/ThreeLineFactory.kt @@ -15,7 +15,7 @@ public object ThreeLineFactory : ThreeFactory { override fun invoke(obj: PolyLine): Object3D { val geometry = Geometry().apply { - vertices = obj.points.toTypedArray() + vertices = Array(obj.points.size) { obj.points[it].toVector() } } val material = ThreeMaterials.getLineMaterial(obj.getProperty(MeshThreeFactory.EDGES_MATERIAL_KEY).node, true) @@ -27,7 +27,7 @@ public object ThreeLineFactory : ThreeFactory { updatePosition(obj) //layers.enable(obj.layer) //add listener to object properties - obj.onPropertyChange(this) { propertyName-> + obj.onPropertyChange(this) { propertyName -> updateProperty(obj, propertyName) } } diff --git a/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/three/three.kt b/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/three/three.kt index b18cd2eb..c0a739e9 100644 --- a/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/three/three.kt +++ b/visionforge-solid/src/jsMain/kotlin/hep/dataforge/vision/solid/three/three.kt @@ -22,6 +22,8 @@ public fun Geometry.toBufferGeometry(): BufferGeometry = BufferGeometry().apply internal fun Double.toRadians() = this * PI / 180 +internal val ZERO = Vector3(0,0,0) + public fun CSG.toGeometry(): Geometry { val geom = Geometry() @@ -37,7 +39,7 @@ public fun CSG.toGeometry(): Geometry { } for (j in 3..polygon.vertices.size) { - val fc = Face3(v0, v0 + j - 2, v0 + j - 1, World.ZERO) + val fc = Face3(v0, v0 + j - 2, v0 + j - 1, ZERO) fc.vertexNormals = arrayOf( Vector3().copy(pvs[0].normal), Vector3().copy(pvs[j - 2].normal), diff --git a/visionforge-solid/src/jvmMain/kotlin/hep/dataforge/vision/solid/fx/FX3DPlugin.kt b/visionforge-solid/src/jvmMain/kotlin/hep/dataforge/vision/solid/fx/FX3DPlugin.kt index be2f5788..30b30a81 100644 --- a/visionforge-solid/src/jvmMain/kotlin/hep/dataforge/vision/solid/fx/FX3DPlugin.kt +++ b/visionforge-solid/src/jvmMain/kotlin/hep/dataforge/vision/solid/fx/FX3DPlugin.kt @@ -16,6 +16,7 @@ import javafx.scene.shape.Shape3D import javafx.scene.text.Font import javafx.scene.text.Text import javafx.scene.transform.Rotate +import org.fxyz3d.geometry.Point3D import org.fxyz3d.shapes.composites.PolyLine3D import org.fxyz3d.shapes.primitives.CuboidMesh import org.fxyz3d.shapes.primitives.SpheroidMesh @@ -69,7 +70,7 @@ class FX3DPlugin : AbstractPlugin() { y = layoutBounds.height / 2 } is PolyLine -> PolyLine3D( - obj.points.map { it.point }, + obj.points.map { Point3D(it.x, it.y, it.z) }, obj.thickness.toFloat(), obj.getProperty(SolidMaterial.MATERIAL_COLOR_KEY)?.color() ).apply { diff --git a/visionforge-solid/src/jvmMain/kotlin/hep/dataforge/vision/solid/geometryJVM.kt b/visionforge-solid/src/jvmMain/kotlin/hep/dataforge/vision/solid/geometryJVM.kt deleted file mode 100644 index 153aace7..00000000 --- a/visionforge-solid/src/jvmMain/kotlin/hep/dataforge/vision/solid/geometryJVM.kt +++ /dev/null @@ -1,50 +0,0 @@ -package hep.dataforge.vision.solid -import org.fxyz3d.geometry.Point3D as FXPoint3D - -public actual data class Point2D(public actual var x: Double, public actual var y: Double) { - actual constructor(x: Number, y: Number) : this(x.toDouble(), y.toDouble()) -} - -public actual class Point3D(val point: FXPoint3D) { - actual constructor(x: Number, y: Number, z: Number) : this( - FXPoint3D( - x.toFloat(), - y.toFloat(), - z.toFloat() - ) - ) - - public actual var x: Double - inline get() = point.x.toDouble() - inline set(value) { - point.x = value.toFloat() - } - - public actual var y: Double - inline get() = point.y.toDouble() - inline set(value) { - point.y = value.toFloat() - } - - public actual var z: Double - inline get() = point.z.toDouble() - inline set(value) { - point.z = value.toFloat() - } - - override fun equals(other: Any?): Boolean { - return this.point == (other as? Point3D)?.point - } - - override fun hashCode(): Int { - return point.hashCode() - } - - override fun toString(): String { - return point.toString() - } -} - -public actual operator fun Point3D.plus(other: Point3D): Point3D { - return Point3D(point.add(other.point)) -} \ No newline at end of file