From 20851baaf521a9de883ae89deefad27c62854d5a Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 6 Jun 2023 18:43:18 +0300 Subject: [PATCH] Make Axes a separate object --- demo/playground/src/jvmMain/kotlin/axes.kt | 22 ++++++++++++++++ .../kscience/visionforge/solid/MiscSolid.kt | 26 +++++++++++++++++++ .../space/kscience/visionforge/solid/Solid.kt | 21 +++++++-------- .../kscience/visionforge/solid/Solids.kt | 2 ++ .../solid/{StlVision.kt => StlSolid.kt} | 10 +++---- .../solid/specifications/AxesScheme.kt | 2 ++ .../solid/specifications/Canvas3DOptions.kt | 2 ++ .../solid/three/ThreeAxesFactory.kt | 22 ++++++++++++++++ .../visionforge/solid/three/ThreeCanvas.kt | 17 +++--------- .../visionforge/solid/three/ThreeFactory.kt | 2 +- .../visionforge/solid/three/ThreePlugin.kt | 3 ++- .../solid/three/ThreeSmartLineFactory.kt | 14 +++++----- .../solid/three/ThreeStlFactory.kt | 14 +++++----- 13 files changed, 112 insertions(+), 45 deletions(-) create mode 100644 demo/playground/src/jvmMain/kotlin/axes.kt create mode 100644 visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/MiscSolid.kt rename visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/{StlVision.kt => StlSolid.kt} (61%) create mode 100644 visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeAxesFactory.kt diff --git a/demo/playground/src/jvmMain/kotlin/axes.kt b/demo/playground/src/jvmMain/kotlin/axes.kt new file mode 100644 index 00000000..b8178887 --- /dev/null +++ b/demo/playground/src/jvmMain/kotlin/axes.kt @@ -0,0 +1,22 @@ +package space.kscience.visionforge.examples + +import space.kscience.kmath.geometry.Euclidean3DSpace +import space.kscience.kmath.geometry.radians +import space.kscience.visionforge.html.ResourceLocation +import space.kscience.visionforge.solid.* +import kotlin.math.PI + +fun main() = makeVisionFile(resourceLocation = ResourceLocation.SYSTEM) { + vision("canvas") { + requirePlugin(Solids) + solid { + axes(100, "root-axes") + solidGroup("group") { + z = 100 + rotate((PI / 4).radians, Euclidean3DSpace.vector(1, 1, 1)) + axes(100, "local-axes") + box(50, 50, 50, "box") + } + } + } +} \ No newline at end of file diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/MiscSolid.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/MiscSolid.kt new file mode 100644 index 00000000..f83f6cb9 --- /dev/null +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/MiscSolid.kt @@ -0,0 +1,26 @@ +package space.kscience.visionforge.solid + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import space.kscience.visionforge.MutableVisionContainer +import space.kscience.visionforge.VisionBuilder +import space.kscience.visionforge.setChild + +public abstract class MiscSolid: SolidBase() + +@Serializable +@SerialName("solid.axes") +public class AxesSolid(public val size: Double): MiscSolid(){ + public companion object{ + public const val AXES_NAME: String = "@xes" + } +} + +@VisionBuilder +public fun MutableVisionContainer.axes( + size: Number, + name: String = "@axes", + block: AxesSolid.() -> Unit = {}, +): AxesSolid = AxesSolid(size.toDouble()).apply(block).also { + setChild(name, it) +} \ No newline at end of file diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solid.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solid.kt index e0140fe4..cb0c90f0 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solid.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solid.kt @@ -9,9 +9,7 @@ import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName import space.kscience.dataforge.names.plus import space.kscience.kmath.complex.Quaternion -import space.kscience.kmath.geometry.RotationOrder -import space.kscience.kmath.geometry.fromEuler -import space.kscience.kmath.geometry.radians +import space.kscience.kmath.geometry.* import space.kscience.visionforge.* import space.kscience.visionforge.Vision.Companion.VISIBLE_KEY import space.kscience.visionforge.solid.Solid.Companion.DETAIL_KEY @@ -242,14 +240,13 @@ public var Solid.quaternion: Quaternion quaternionValue = value } - -//public var Solid.quaternion: Quaternion? -// get() = meta[Solid::quaternion.name]?.value?.doubleArray?.let { Quaternion(it) } -// set(value) { -// meta[Solid::quaternion.name] = value?.values?.asValue() -// } - - public var Solid.scaleX: Number by float(X_SCALE_KEY, 1f) public var Solid.scaleY: Number by float(Y_SCALE_KEY, 1f) -public var Solid.scaleZ: Number by float(Z_SCALE_KEY, 1f) \ No newline at end of file +public var Solid.scaleZ: Number by float(Z_SCALE_KEY, 1f) + +/** + * Add rotation with given [angle] relative to given [axis] + */ +public fun Solid.rotate(angle: Angle, axis: DoubleVector3D) { + quaternion = Quaternion.fromRotation(angle, axis) +} \ No newline at end of file diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solids.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solids.kt index ab9fde1e..e9301f67 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solids.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solids.kt @@ -48,6 +48,8 @@ public class Solids(meta: Meta) : VisionPlugin(meta), MutableVisionContainer() +public sealed class StlSolid: SolidBase() @Serializable @SerialName("solid.stl.url") -public class StlUrlVision(public val url: String) : StlVision() +public class StlUrlSolid(public val url: String) : StlSolid() @Serializable @SerialName("solid.stl.binary") -public class StlBinaryVision(public val data: ByteArray) : StlVision() +public class StlBinarySolid(public val data: ByteArray) : StlSolid() @VisionBuilder public inline fun MutableVisionContainer.stl( url: String, name: String? = null, - action: StlVision.() -> Unit = {}, -): StlVision = StlUrlVision(url).apply(action).also { setChild(name, it) } \ No newline at end of file + action: StlSolid.() -> Unit = {}, +): StlSolid = StlUrlSolid(url).apply(action).also { setChild(name, it) } \ No newline at end of file diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/AxesScheme.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/AxesScheme.kt index d4eb8e2b..0ed9126e 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/AxesScheme.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/AxesScheme.kt @@ -7,11 +7,13 @@ import space.kscience.dataforge.meta.descriptors.MetaDescriptor import space.kscience.dataforge.meta.descriptors.value import space.kscience.dataforge.meta.double +@Deprecated("Use separate axes object instead") public class AxesScheme : Scheme() { public var visible: Boolean by boolean(false) public var size: Double by double(AXIS_SIZE) public var width: Double by double(AXIS_WIDTH) + @Suppress("DEPRECATION") public companion object : SchemeSpec(::AxesScheme) { public const val AXIS_SIZE: Double = 1000.0 public const val AXIS_WIDTH: Double = 3.0 diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Canvas3DOptions.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Canvas3DOptions.kt index 78e35dfa..b24c9b75 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Canvas3DOptions.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Canvas3DOptions.kt @@ -59,6 +59,7 @@ public class CanvasSize : Scheme() { } public class Canvas3DOptions : Scheme() { + @Suppress("DEPRECATION") public var axes: AxesScheme by spec(AxesScheme) public var camera: CameraScheme by spec(CameraScheme) public var controls: ControlsScheme by spec(ControlsScheme) @@ -75,6 +76,7 @@ public class Canvas3DOptions : Scheme() { public companion object : SchemeSpec(::Canvas3DOptions) { override val descriptor: MetaDescriptor by lazy { MetaDescriptor { + @Suppress("DEPRECATION") scheme(Canvas3DOptions::axes, AxesScheme) value(Canvas3DOptions::layers) { diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeAxesFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeAxesFactory.kt new file mode 100644 index 00000000..8af445b9 --- /dev/null +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeAxesFactory.kt @@ -0,0 +1,22 @@ +package space.kscience.visionforge.solid.three + +import space.kscience.visionforge.onPropertyChange +import space.kscience.visionforge.solid.AxesSolid +import three.helpers.AxesHelper +import kotlin.reflect.KClass + +public object ThreeAxesFactory : ThreeFactory { + override val type: KClass get() = AxesSolid::class + + override suspend fun build(three: ThreePlugin, vision: AxesSolid, observe: Boolean): AxesHelper { + val res = AxesHelper(vision.size.toInt()) + + if (observe) { + vision.onPropertyChange(three.context) { propertyName -> + res.updateProperty(vision, propertyName) + } + } + + return res + } +} \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvas.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvas.kt index 6ecdad78..c692b88d 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvas.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvas.kt @@ -57,16 +57,6 @@ public class ThreeCanvas( axesObject.name = AXES_NAME add(axesObject) } - -// //Set up light -// options.useProperty(Canvas3DOptions::light, this) { lightConfig -> -// //remove old light if present -// getObjectByName(LIGHT_NAME)?.let { remove(it) } -// //add new light -// val lightObject = buildLight(lightConfig) -// lightObject.name = LIGHT_NAME -// add(lightObject) -// } } @@ -110,7 +100,7 @@ public class ThreeCanvas( } /** - * Force camera aspect ration and renderer size recalculation + * Force camera aspect ratio and renderer size recalculation */ private fun updateSize() { val width = element.clientWidth @@ -202,7 +192,7 @@ public class ThreeCanvas( } /** - * Resolve full name of the object relative to the global root + * Resolve the full name of the object relative to the global root */ private fun Object3D.fullName(): Name { if (root == null) error("Can't resolve element name without the root") @@ -213,7 +203,7 @@ public class ThreeCanvas( } } - //find first non-static parent in this object ancestry + //find the first non-static parent in this object ancestry private tailrec fun Object3D.upTrace(): Object3D? = if (!name.startsWith("@")) this else parent?.upTrace() private fun pick(): Object3D? { @@ -267,6 +257,7 @@ public class ThreeCanvas( } three.context.launch { val object3D = three.buildObject3D(vision) + object3D.name = "@root" scene.add(object3D) root = object3D diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeFactory.kt index dcf94717..46e3d41b 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeFactory.kt @@ -37,7 +37,7 @@ public interface ThreeFactory { * Update position, rotation and visibility */ public fun Object3D.updatePosition(vision: Vision) { - visible = vision.visible ?: true +// visible = vision.visible ?: true if (vision is Solid) { position.set(vision.x, vision.y, vision.z) diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreePlugin.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreePlugin.kt index e2af8c30..c1c2cf4d 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreePlugin.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreePlugin.kt @@ -37,7 +37,8 @@ public class ThreePlugin : AbstractPlugin(), ElementVisionRenderer { objectFactories[SolidLabel::class] = ThreeCanvasLabelFactory objectFactories[AmbientLightSource::class] = ThreeAmbientLightFactory objectFactories[PointLightSource::class] = ThreePointLightFactory - objectFactories[StlVision::class] = ThreeStlFactory + objectFactories[StlSolid::class] = ThreeStlFactory + objectFactories[AxesSolid::class] = ThreeAxesFactory } @Suppress("UNCHECKED_CAST") diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeSmartLineFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeSmartLineFactory.kt index 6f2ee1a5..b3609b66 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeSmartLineFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeSmartLineFactory.kt @@ -7,11 +7,13 @@ import kotlin.reflect.KClass public object ThreeSmartLineFactory : ThreeFactory { override val type: KClass get() = PolyLine::class - override suspend fun build(three: ThreePlugin, vision: PolyLine, observe: Boolean): Object3D { - return if (vision.thickness == 1.0) { - ThreeLineFactory.build(three, vision, observe) - } else { - ThreeMeshLineFactory.build(three, vision, observe) - } + override suspend fun build( + three: ThreePlugin, + vision: PolyLine, + observe: Boolean, + ): Object3D = if (vision.thickness == 1.0) { + ThreeLineFactory.build(three, vision, observe) + } else { + ThreeMeshLineFactory.build(three, vision, observe) } } \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeStlFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeStlFactory.kt index d21389aa..e3fb676f 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeStlFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeStlFactory.kt @@ -2,9 +2,9 @@ package space.kscience.visionforge.solid.three import org.khronos.webgl.ArrayBuffer import org.khronos.webgl.Int8Array -import space.kscience.visionforge.solid.StlBinaryVision -import space.kscience.visionforge.solid.StlUrlVision -import space.kscience.visionforge.solid.StlVision +import space.kscience.visionforge.solid.StlBinarySolid +import space.kscience.visionforge.solid.StlSolid +import space.kscience.visionforge.solid.StlUrlSolid import three.core.BufferGeometry import three.external.loaders.STLLoader import kotlin.coroutines.resume @@ -13,15 +13,15 @@ import kotlin.coroutines.suspendCoroutine fun ArrayBuffer.toByteArray(): ByteArray = Int8Array(this).unsafeCast() -public object ThreeStlFactory : ThreeMeshFactory(StlVision::class) { +public object ThreeStlFactory : ThreeMeshFactory(StlSolid::class) { private val loader = STLLoader().apply { requestHeader = listOf("Access-Control-Allow-Origin: *") } - override suspend fun buildGeometry(obj: StlVision): BufferGeometry = when (obj) { - is StlBinaryVision -> loader.parse(obj.data) - is StlUrlVision -> suspendCoroutine { continuation -> + override suspend fun buildGeometry(obj: StlSolid): BufferGeometry = when (obj) { + is StlBinarySolid -> loader.parse(obj.data) + is StlUrlSolid -> suspendCoroutine { continuation -> loader.load( url = obj.url, onLoad = {