From f5fba4747eb6e107b761f0c8cd6513cbb7236dc6 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 25 Jul 2023 13:35:55 +0300 Subject: [PATCH] Some refactoring and new server demo --- CHANGELOG.md | 2 + build.gradle.kts | 2 +- .../kotlin/ru/mipt/npm/root/dRootToSolid.kt | 2 +- .../visionforge/gdml/demo/GDMLAppComponent.kt | 4 +- .../visionforge/gdml/demo/GdmlJsDemoApp.kt | 4 +- .../src/main/kotlin/JsPlaygroundApp.kt | 4 +- .../src/main/kotlin/gravityDemo.kt | 4 +- .../kotlin/ru/mipt/npm/muon/monitor/Model.kt | 6 +- .../mipt/npm/muon/monitor/MMAppComponent.kt | 4 +- demo/playground/src/jvmMain/kotlin/antenna.kt | 91 +++++++++++++++---- .../src/jvmMain/kotlin/gdmlCurve.kt | 4 +- .../src/jvmMain/kotlin/randomSpheres.kt | 4 +- .../src/jvmMain/kotlin/rootParser.kt | 4 +- .../src/jvmMain/kotlin/serverExtensions.kt | 52 +++++++++++ demo/playground/src/jvmMain/kotlin/shapes.kt | 8 +- .../src/jvmMain/kotlin/simpleCube.kt | 4 +- .../main/kotlin/ru/mipt/npm/sat/geometry.kt | 2 +- .../main/kotlin/ru/mipt/npm/sat/satServer.kt | 4 +- .../src/main/kotlin/ru/mipt/npm/sat/static.kt | 4 +- .../kscience/visionforge/solid/demo/demo.kt | 30 +++--- .../ThreeWithControlsPlugin.kt | 18 +++- .../visionforge/gdml/GdmlLoaderOptions.kt | 2 +- .../visionforge/server/VisionServer.kt | 6 +- .../visionforge/solid/ColorAccessor.kt | 8 +- .../kscience/visionforge/solid/LightSource.kt | 2 +- .../kscience/visionforge/solid/SolidGroup.kt | 5 + .../kscience/visionforge/solid/Solids.kt | 4 + .../visionforge/solid/CompositeTest.kt | 2 +- .../kscience/visionforge/solid/GroupTest.kt | 6 +- .../visionforge/solid/SerializationTest.kt | 6 +- .../visionforge/solid/SolidPropertyTest.kt | 4 +- .../visionforge/solid/SolidReferenceTest.kt | 2 +- .../visionforge/solid/VisionUpdateTest.kt | 4 +- 33 files changed, 215 insertions(+), 93 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afc740a3..dbb44a80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - MeshLine for thick lines ### Changed +- Color accessor property is now `colorProperty`. Color uses `invoke` instead of `set` - API update for server and pages - Edges moved to solids module for easier construction - Visions **must** be rooted in order to subscribe to updates. @@ -20,6 +21,7 @@ ### Removed ### Fixed +- Jupyter integration for IDEA and Jupyter lab. ### Security diff --git a/build.gradle.kts b/build.gradle.kts index 35502755..68c3569b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -13,7 +13,7 @@ val fxVersion by extra("11") allprojects { group = "space.kscience" - version = "0.3.0-dev-12" + version = "0.3.0-dev-13" } subprojects { diff --git a/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/dRootToSolid.kt b/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/dRootToSolid.kt index a2e1f70a..be9372b5 100644 --- a/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/dRootToSolid.kt +++ b/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/dRootToSolid.kt @@ -328,7 +328,7 @@ private fun buildVolume(volume: DGeoVolume, context: RootToSolidContext): Solid? group }.apply { volume.fMedium?.let { medium -> - color.set(context.colorCache.getOrPut(medium.meta) { RootColors[11 + context.colorCache.size] }) + color(context.colorCache.getOrPut(medium.meta) { RootColors[11 + context.colorCache.size] }) } if (!context.ignoreRootColors) { diff --git a/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt b/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt index 4301966a..37c178f4 100644 --- a/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt +++ b/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt @@ -23,7 +23,7 @@ import space.kscience.visionforge.setAsRoot import space.kscience.visionforge.solid.Solid import space.kscience.visionforge.solid.Solids import space.kscience.visionforge.solid.ambientLight -import space.kscience.visionforge.solid.set +import space.kscience.visionforge.solid.invoke import styled.css import styled.styledDiv @@ -53,7 +53,7 @@ val GDMLApp = fc("GDMLApp") { props -> console.info("Marking layers for file $name") markLayers() ambientLight { - color.set(Colors.white) + color(Colors.white) } } } diff --git a/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GdmlJsDemoApp.kt b/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GdmlJsDemoApp.kt index cdca1957..7c6514bf 100644 --- a/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GdmlJsDemoApp.kt +++ b/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GdmlJsDemoApp.kt @@ -12,7 +12,7 @@ import space.kscience.visionforge.react.createRoot import space.kscience.visionforge.react.render import space.kscience.visionforge.solid.Solids import space.kscience.visionforge.solid.ambientLight -import space.kscience.visionforge.solid.set +import space.kscience.visionforge.solid.invoke import space.kscience.visionforge.solid.three.ThreePlugin import space.kscience.visionforge.startApplication import styled.injectGlobal @@ -49,7 +49,7 @@ private class GDMLDemoApp : Application { child(GDMLApp) { val vision = GdmlShowCase.cubes().toVision().apply { ambientLight { - color.set(Colors.white) + color(Colors.white) } } //println(context.plugins.fetch(VisionManager).encodeToString(vision)) diff --git a/demo/js-playground/src/main/kotlin/JsPlaygroundApp.kt b/demo/js-playground/src/main/kotlin/JsPlaygroundApp.kt index cb6eb3e6..d77d8f52 100644 --- a/demo/js-playground/src/main/kotlin/JsPlaygroundApp.kt +++ b/demo/js-playground/src/main/kotlin/JsPlaygroundApp.kt @@ -76,7 +76,7 @@ private class JsPlaygroundApp : Application { solids = playgroundContext.request(Solids) solid { ambientLight { - color.set(Colors.white) + color(Colors.white) } repeat(100) { sphere(5, name = "sphere[$it]") { @@ -84,7 +84,7 @@ private class JsPlaygroundApp : Application { y = random.nextDouble(-300.0, 300.0) z = random.nextDouble(-300.0, 300.0) material { - color.set(random.nextInt()) + color(random.nextInt()) } detail = 16 } diff --git a/demo/js-playground/src/main/kotlin/gravityDemo.kt b/demo/js-playground/src/main/kotlin/gravityDemo.kt index c04baf98..a4bc9057 100644 --- a/demo/js-playground/src/main/kotlin/gravityDemo.kt +++ b/demo/js-playground/src/main/kotlin/gravityDemo.kt @@ -42,13 +42,13 @@ val GravityDemo = fc { props -> solids = props.solids solid { pointLight(200, 200, 200, name = "light"){ - color.set(Colors.white) + color(Colors.white) } ambientLight() sphere(5.0, "ball") { detail = 16 - color.set("red") + color("red") val h = 100.0 y = h solids.context.launch { diff --git a/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Model.kt b/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Model.kt index 82c20def..f11c9e01 100644 --- a/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Model.kt +++ b/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Model.kt @@ -39,7 +39,7 @@ class Model(val manager: VisionManager) { val root: SolidGroup = SolidGroup().apply { setAsRoot(this@Model.manager) material { - color.set("darkgreen") + color("darkgreen") } rotationX = PI / 2 solidGroup("bottom") { @@ -64,7 +64,7 @@ class Model(val manager: VisionManager) { private fun highlight(pixel: String) { println("highlight $pixel") - map[pixel]?.color.set("blue") + map[pixel]?.color("blue") } fun reset() { @@ -82,7 +82,7 @@ class Model(val manager: VisionManager) { } event.track?.let { tracks.polyline(*it.toTypedArray(), name = "track[${event.id}]") { - color.set("red") + color("red") } } } diff --git a/demo/muon-monitor/src/jsMain/kotlin/ru/mipt/npm/muon/monitor/MMAppComponent.kt b/demo/muon-monitor/src/jsMain/kotlin/ru/mipt/npm/muon/monitor/MMAppComponent.kt index 4c9649c6..07dc7c7c 100644 --- a/demo/muon-monitor/src/jsMain/kotlin/ru/mipt/npm/muon/monitor/MMAppComponent.kt +++ b/demo/muon-monitor/src/jsMain/kotlin/ru/mipt/npm/muon/monitor/MMAppComponent.kt @@ -26,7 +26,7 @@ import space.kscience.visionforge.ring.tab import space.kscience.visionforge.solid.Solids import space.kscience.visionforge.solid.ambientLight import space.kscience.visionforge.solid.edges -import space.kscience.visionforge.solid.set +import space.kscience.visionforge.solid.invoke import space.kscience.visionforge.solid.specifications.Canvas3DOptions import styled.css import styled.styledDiv @@ -58,7 +58,7 @@ val MMApp = fc("Muon monitor") { props -> props.model.root.apply { edges() ambientLight{ - color.set(Colors.white) + color(Colors.white) } } } diff --git a/demo/playground/src/jvmMain/kotlin/antenna.kt b/demo/playground/src/jvmMain/kotlin/antenna.kt index cd711d11..6e9b759b 100644 --- a/demo/playground/src/jvmMain/kotlin/antenna.kt +++ b/demo/playground/src/jvmMain/kotlin/antenna.kt @@ -1,16 +1,19 @@ package space.kscience.visionforge.examples +import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch +import space.kscience.dataforge.meta.configure import space.kscience.kmath.complex.Quaternion import space.kscience.kmath.complex.QuaternionField -import space.kscience.kmath.geometry.Angle -import space.kscience.kmath.geometry.Euclidean3DSpace -import space.kscience.kmath.geometry.degrees -import space.kscience.kmath.geometry.fromRotation -import space.kscience.visionforge.html.ResourceLocation +import space.kscience.kmath.complex.conjugate +import space.kscience.kmath.geometry.* import space.kscience.visionforge.solid.* import kotlin.math.PI +import kotlin.math.cos +import kotlin.math.sin -fun main() = makeVisionFile(resourceLocation = ResourceLocation.SYSTEM) { +fun main() = serve { val azimuth = 60.degrees val inclination = 15.degrees @@ -22,28 +25,76 @@ fun main() = makeVisionFile(resourceLocation = ResourceLocation.SYSTEM) { //val direction2 = Quaternion.fromEuler(Angle.zero, Angle.piDiv2 - inclination, -azimuth, RotationOrder.ZYX) + val target = Quaternion.fromEuler((-45).degrees, 45.degrees, Angle.zero, RotationOrder.XYZ) + vision("canvas") { requirePlugin(Solids) - solid { + + solid(options = { + configure { "controls.enabled" put false } + }) { rotationX = -PI / 2 rotationZ = PI - axes(200) + //axes(200) ambientLight() - cylinder(50, 5, name = "base") - solidGroup("frame") { - z = 60 - axes(200) - solidGroup("antenna") { - tube(40, 10, 30) - sphereLayer(100, 95, theta = PI / 6) { - z = 100 - rotationX = -PI / 2 + val platform = solidGroup("platform") { + cylinder(50, 5, name = "base") + solidGroup("frame") { + z = 60 + + val antenna = solidGroup("antenna") { + axes(200) + tube(40, 10, 30) + sphereLayer(100, 95, theta = PI / 6) { + z = 100 + rotationX = -PI / 2 + } + cylinder(5, 30) { + z = 15 + } + + sphereLayer(101, 94, phi = PI / 32, theta = PI / 6) { + z = 100 + rotationX = -PI / 2 + color("red") + } + + quaternion = target } - cylinder(5, 30) { - z = 15 + } + } + + val frame = platform["frame"] as SolidGroup + + val antenna = frame["antenna"] as SolidGroup + + val xPeriod = 5000 //ms + val yPeriod = 7000 //ms + + val incRot = Quaternion.fromRotation(30.degrees, Euclidean3DSpace.zAxis) + + + val rotationJob = context.launch { + var time: Long = 0L + while (isActive) { + with(QuaternionField) { + delay(200) + platform.quaternion = Quaternion.fromRotation( + 15.degrees * sin(time.toDouble() * 2 * PI / xPeriod), + Euclidean3DSpace.xAxis + ) * Quaternion.fromRotation( + 15.degrees * cos(time * 2 * PI / yPeriod), + Euclidean3DSpace.yAxis + ) + + val qi = platform.quaternion * incRot + + antenna.quaternion = qi.conjugate * incRot.conjugate * target + + time += 200 + //antenna.quaternion = Quaternion.fromRotation(5.degrees, Euclidean3DSpace.zAxis) * antenna.quaternion } - quaternion = direction } } } diff --git a/demo/playground/src/jvmMain/kotlin/gdmlCurve.kt b/demo/playground/src/jvmMain/kotlin/gdmlCurve.kt index c2af2a8f..4cac02b1 100644 --- a/demo/playground/src/jvmMain/kotlin/gdmlCurve.kt +++ b/demo/playground/src/jvmMain/kotlin/gdmlCurve.kt @@ -7,7 +7,7 @@ import space.kscience.visionforge.gdml.toVision import space.kscience.visionforge.html.ResourceLocation import space.kscience.visionforge.solid.Solids import space.kscience.visionforge.solid.color -import space.kscience.visionforge.solid.set +import space.kscience.visionforge.solid.invoke import space.kscience.visionforge.visible import java.nio.file.Path @@ -229,7 +229,7 @@ fun main() = makeVisionFile(Path.of("curves.html"), resourceLocation = ResourceL visible = false } if(solid.name.startsWith("gas")){ - color.set("green") + color("green") } else { //make all solids semi-transparent transparent() diff --git a/demo/playground/src/jvmMain/kotlin/randomSpheres.kt b/demo/playground/src/jvmMain/kotlin/randomSpheres.kt index 47201ff8..fd1b9865 100644 --- a/demo/playground/src/jvmMain/kotlin/randomSpheres.kt +++ b/demo/playground/src/jvmMain/kotlin/randomSpheres.kt @@ -19,7 +19,7 @@ fun main() = makeVisionFile( vision { solid { ambientLight { - color.set(Colors.white) + color(Colors.white) } repeat(100) { sphere(5, name = "sphere[$it]") { @@ -27,7 +27,7 @@ fun main() = makeVisionFile( y = random.nextDouble(-300.0, 300.0) z = random.nextDouble(-300.0, 300.0) material { - color.set(random.nextInt()) + color(random.nextInt()) } detail = 16 } diff --git a/demo/playground/src/jvmMain/kotlin/rootParser.kt b/demo/playground/src/jvmMain/kotlin/rootParser.kt index be70faf8..d5fea32e 100644 --- a/demo/playground/src/jvmMain/kotlin/rootParser.kt +++ b/demo/playground/src/jvmMain/kotlin/rootParser.kt @@ -11,7 +11,7 @@ import space.kscience.visionforge.Colors import space.kscience.visionforge.html.ResourceLocation import space.kscience.visionforge.solid.Solids import space.kscience.visionforge.solid.ambientLight -import space.kscience.visionforge.solid.set +import space.kscience.visionforge.solid.invoke import space.kscience.visionforge.solid.solid import java.util.zip.ZipInputStream import kotlin.io.path.Path @@ -44,7 +44,7 @@ fun main() { requirePlugin(Solids) solid { ambientLight { - color.set(Colors.white) + color(Colors.white) } rootGeo(geo,"BM@N", maxLayer = 3, ignoreRootColors = true).also { Path("data/BM@N.vf.json").writeText(Solids.encodeToString(it)) diff --git a/demo/playground/src/jvmMain/kotlin/serverExtensions.kt b/demo/playground/src/jvmMain/kotlin/serverExtensions.kt index 9eb87730..c20f27c7 100644 --- a/demo/playground/src/jvmMain/kotlin/serverExtensions.kt +++ b/demo/playground/src/jvmMain/kotlin/serverExtensions.kt @@ -1,11 +1,24 @@ package space.kscience.visionforge.examples +import io.ktor.server.cio.CIO +import io.ktor.server.engine.embeddedServer +import io.ktor.server.http.content.staticResources +import io.ktor.server.routing.routing +import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.Global import space.kscience.visionforge.html.* +import space.kscience.visionforge.markup.MarkupPlugin +import space.kscience.visionforge.plotly.PlotlyPlugin +import space.kscience.visionforge.server.close +import space.kscience.visionforge.server.openInBrowser +import space.kscience.visionforge.server.visionPage +import space.kscience.visionforge.solid.Solids +import space.kscience.visionforge.tables.TableVisionPlugin import space.kscience.visionforge.visionManager import java.awt.Desktop import java.nio.file.Path + public fun makeVisionFile( path: Path? = null, title: String = "VisionForge page", @@ -26,6 +39,45 @@ public fun makeVisionFile( if (show) Desktop.getDesktop().browse(actualPath.toFile().toURI()) } +public fun serve( + title: String = "VisionForge page", + show: Boolean = true, + content: HtmlVisionFragment, +) { + val context = Context("playground") { + plugin(Solids) + plugin(PlotlyPlugin) + plugin(MarkupPlugin) + plugin(TableVisionPlugin) + } + + val server = embeddedServer(CIO, port = 7779) { + routing { + staticResources("", null, null) + } + + visionPage( + context.visionManager, + VisionPage.scriptHeader("js/visionforge-playground.js") { + defer = true + }, + VisionPage.title(title), + visionFragment = content + ) + }.start(false) + + if (show) { + server.openInBrowser() + } + + println("Enter 'exit' to close server") + while (readlnOrNull() != "exit") { + // + } + + server.close() +} + //@DFExperimental //public fun Context.makeVisionFile( // vision: Vision, diff --git a/demo/playground/src/jvmMain/kotlin/shapes.kt b/demo/playground/src/jvmMain/kotlin/shapes.kt index a338d123..55ce28d5 100644 --- a/demo/playground/src/jvmMain/kotlin/shapes.kt +++ b/demo/playground/src/jvmMain/kotlin/shapes.kt @@ -10,23 +10,23 @@ fun main() = makeVisionFile { ambientLight() box(100.0, 100.0, 100.0) { z = -110.0 - color.set("teal") + color("teal") } sphere(50.0) { x = 110 detail = 16 - color.set("red") + color("red") } tube(50, height = 10, innerRadius = 25, angle = PI) { y = 110 detail = 16 rotationX = PI / 4 - color.set("blue") + color("blue") } sphereLayer(50, 40, theta = PI / 2) { rotationX = -PI * 3 / 4 z = 110 - color.set(Colors.pink) + color(Colors.pink) } diff --git a/demo/playground/src/jvmMain/kotlin/simpleCube.kt b/demo/playground/src/jvmMain/kotlin/simpleCube.kt index 5dae515c..e1fc91eb 100644 --- a/demo/playground/src/jvmMain/kotlin/simpleCube.kt +++ b/demo/playground/src/jvmMain/kotlin/simpleCube.kt @@ -2,8 +2,8 @@ package space.kscience.visionforge.examples import space.kscience.visionforge.html.ResourceLocation import space.kscience.visionforge.solid.box +import space.kscience.visionforge.solid.invoke import space.kscience.visionforge.solid.material -import space.kscience.visionforge.solid.set import space.kscience.visionforge.solid.solid fun main() = makeVisionFile(resourceLocation = ResourceLocation.SYSTEM) { @@ -11,7 +11,7 @@ fun main() = makeVisionFile(resourceLocation = ResourceLocation.SYSTEM) { solid { box(100, 100, 100) material { - emissiveColor.set("red") + emissiveColor("red") } } } diff --git a/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/geometry.kt b/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/geometry.kt index 052fb6e0..d2c30422 100644 --- a/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/geometry.kt +++ b/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/geometry.kt @@ -15,7 +15,7 @@ internal fun Solids.visionOfSatellite( ySegmentSize: Number = xSegmentSize, fiberDiameter: Number = 1.0, ): SolidGroup = solidGroup { - color.set("darkgreen") + color("darkgreen") val transparent by style { this[SolidMaterial.MATERIAL_OPACITY_KEY] = 0.3 } diff --git a/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/satServer.kt b/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/satServer.kt index 9e0c8282..9f2f7e59 100644 --- a/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/satServer.kt +++ b/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/satServer.kt @@ -33,7 +33,7 @@ fun main() { //Create a geometry val sat = solids.visionOfSatellite(ySegments = 3).apply { ambientLight { - color.set(Colors.white) + color(Colors.white) } } val server = embeddedServer(CIO, port = 7777) { @@ -63,7 +63,7 @@ fun main() { val randomJ = Random.nextInt(1, 4) val target = Name.parse("layer[$randomLayer].segment[$randomI,$randomJ]") val targetVision = sat[target] as Solid - targetVision.color.set("red") + targetVision.color("red") delay(1000) //use to ensure that color is cleared targetVision.color.value = Null diff --git a/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/static.kt b/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/static.kt index 2a1fd240..89dc3a09 100644 --- a/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/static.kt +++ b/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/static.kt @@ -3,8 +3,8 @@ package ru.mipt.npm.sat import space.kscience.dataforge.misc.DFExperimental import space.kscience.visionforge.html.ResourceLocation import space.kscience.visionforge.solid.box +import space.kscience.visionforge.solid.invoke import space.kscience.visionforge.solid.material -import space.kscience.visionforge.solid.set import space.kscience.visionforge.solid.solid import space.kscience.visionforge.three.makeThreeJsFile @@ -14,7 +14,7 @@ fun main() = makeThreeJsFile(resourceLocation = ResourceLocation.SYSTEM) { solid { box(100, 100, 100) material { - emissiveColor.set("red") + emissiveColor("red") } } } diff --git a/demo/solid-showcase/src/commonMain/kotlin/space/kscience/visionforge/solid/demo/demo.kt b/demo/solid-showcase/src/commonMain/kotlin/space/kscience/visionforge/solid/demo/demo.kt index baee4c71..9e2ecedb 100644 --- a/demo/solid-showcase/src/commonMain/kotlin/space/kscience/visionforge/solid/demo/demo.kt +++ b/demo/solid-showcase/src/commonMain/kotlin/space/kscience/visionforge/solid/demo/demo.kt @@ -23,7 +23,7 @@ fun VisionLayout.demo(name: String, title: String = name, block: SolidGro val vision = solids.solidGroup { block() ambientLight { - color.set(Colors.white) + color(Colors.white) } } render(Name.parse(name), vision, meta) @@ -49,23 +49,23 @@ fun VisionLayout.showcase() { ambientLight() box(100.0, 100.0, 100.0) { z = -110.0 - color.set("teal") + color("teal") } sphere(50.0) { x = 110 detail = 16 - color.set("red") + color("red") } tube(50, height = 10, innerRadius = 25, angle = PI) { y = 110 detail = 16 rotationX = PI / 4 - color.set("blue") + color("blue") } sphereLayer(50, 40, theta = PI / 2) { rotationX = -PI * 3 / 4 z = 110 - color.set(Colors.pink) + color(Colors.pink) } } @@ -80,7 +80,7 @@ fun VisionLayout.showcase() { visible = false x = 110.0 //override color for this cube - color.set(1530) + color(1530) GlobalScope.launch(Dispatchers.Main) { while (isActive) { @@ -95,7 +95,7 @@ fun VisionLayout.showcase() { val random = Random(111) while (isActive) { delay(1000) - group.color.set(random.nextInt(0, Int.MAX_VALUE)) + group.color(random.nextInt(0, Int.MAX_VALUE)) } } } @@ -114,7 +114,7 @@ fun VisionLayout.showcase() { rotate((PI/20).radians,Euclidean3DSpace.yAxis) } } - color.set(Colors.red) + color(Colors.red) } } } @@ -127,7 +127,7 @@ fun VisionLayout.showcase() { for (i in 0..100) { layer(i * 5, 20 * sin(2 * PI / 100 * i), 20 * cos(2 * PI / 100 * i)) } - color.set(Colors.teal) + color(Colors.teal) rotationX = -PI / 2 } } @@ -136,7 +136,7 @@ fun VisionLayout.showcase() { sphere(100) { detail = 32 opacity = 0.4 - color.set(Colors.blue) + color(Colors.blue) } repeat(20) { polyline( @@ -145,7 +145,7 @@ fun VisionLayout.showcase() { ) { thickness = 3.0 rotationX = it * PI2 / 20 - color.set(Colors.green) + color(Colors.green) //rotationY = it * PI2 / 20 } } @@ -176,7 +176,7 @@ fun VisionLayout.showcaseCSG() { detail = 32 } material { - color.set(Colors.pink) + color(Colors.pink) } } composite(CompositeType.UNION) { @@ -186,7 +186,7 @@ fun VisionLayout.showcaseCSG() { sphere(50) { detail = 32 } - color.set("lightgreen") + color("lightgreen") opacity = 0.7 } composite(CompositeType.SUBTRACT) { @@ -197,7 +197,7 @@ fun VisionLayout.showcaseCSG() { sphere(50) { detail = 32 } - color.set("teal") + color("teal") opacity = 0.7 } } @@ -208,7 +208,7 @@ fun VisionLayout.showcaseCSG() { detail = 32 } box(100, 100, 100) - color.set("red") + color("red") opacity = 0.5 } } diff --git a/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ThreeWithControlsPlugin.kt b/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ThreeWithControlsPlugin.kt index dd7f8e0d..94259b2f 100644 --- a/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ThreeWithControlsPlugin.kt +++ b/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ThreeWithControlsPlugin.kt @@ -7,12 +7,15 @@ import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.PluginFactory import space.kscience.dataforge.context.PluginTag import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.meta.boolean +import space.kscience.dataforge.meta.get import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName import space.kscience.visionforge.ElementVisionRenderer import space.kscience.visionforge.Vision import space.kscience.visionforge.react.render import space.kscience.visionforge.solid.Solid +import space.kscience.visionforge.solid.specifications.Canvas3DOptions import space.kscience.visionforge.solid.three.ThreePlugin public class ThreeWithControlsPlugin : AbstractPlugin(), ElementVisionRenderer { @@ -24,11 +27,16 @@ public class ThreeWithControlsPlugin : AbstractPlugin(), ElementVisionRenderer { if (vision is Solid) ElementVisionRenderer.DEFAULT_RATING * 2 else ElementVisionRenderer.ZERO_RATING override fun render(element: Element, name: Name, vision: Vision, meta: Meta) { - space.kscience.visionforge.react.createRoot(element).render { - child(ThreeCanvasWithControls) { - attrs { - this.solids = three.solids - this.builderOfSolid = context.async { vision as Solid} + if(meta["controls.enabled"].boolean == false){ + three.render(element, name, vision, meta) + } else { + space.kscience.visionforge.react.createRoot(element).render { + child(ThreeCanvasWithControls) { + attrs { + this.solids = three.solids + this.options = Canvas3DOptions.read(meta) + this.builderOfSolid = context.async { vision as Solid } + } } } } diff --git a/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlLoaderOptions.kt b/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlLoaderOptions.kt index 56158c55..2d3c18c9 100644 --- a/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlLoaderOptions.kt +++ b/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlLoaderOptions.kt @@ -42,7 +42,7 @@ public class GdmlLoaderOptions { * Configure paint for given solid with given [GdmlMaterial] */ public var configurePaint: SolidMaterial.(material: GdmlMaterial, solid: GdmlSolid) -> Unit = - { material, _ -> color.set(randomColor(material)) } + { material, _ -> color(randomColor(material)) } private set public fun paint(block: SolidMaterial.(material: GdmlMaterial, solid: GdmlSolid) -> Unit) { diff --git a/visionforge-server/src/main/kotlin/space/kscience/visionforge/server/VisionServer.kt b/visionforge-server/src/main/kotlin/space/kscience/visionforge/server/VisionServer.kt index 47c5db84..61952497 100644 --- a/visionforge-server/src/main/kotlin/space/kscience/visionforge/server/VisionServer.kt +++ b/visionforge-server/src/main/kotlin/space/kscience/visionforge/server/VisionServer.kt @@ -57,7 +57,7 @@ public class VisionRoute( override val context: Context get() = visionManager.context /** - * Update minimal interval between updates in milliseconds (if there are no updates, push will not happen + * Update the minimal interval between updates in milliseconds (if there are no updates, push will not happen */ public var updateInterval: Long by meta.long(300, key = UPDATE_INTERVAL_KEY) @@ -170,8 +170,8 @@ public fun Application.visionPage( meta { charset = "utf-8" } - headers.forEach { header -> - consumer.header() + headers.forEach { headerContent -> + headerContent.appendTo(consumer) } } body { diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/ColorAccessor.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/ColorAccessor.kt index 01abf310..60789027 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/ColorAccessor.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/ColorAccessor.kt @@ -28,7 +28,7 @@ public class ColorAccessor( } } -public fun Vision.color( +public fun Vision.colorProperty( propertyName: Name? = null, ): ReadOnlyProperty = ReadOnlyProperty { _, property -> ColorAccessor(properties.root(true), propertyName ?: property.name.asName()) @@ -43,21 +43,21 @@ public var ColorAccessor?.string: String? /** * Set [webcolor](https://en.wikipedia.org/wiki/Web_colors) as string */ -public fun ColorAccessor?.set(webColor: String) { +public operator fun ColorAccessor?.invoke(webColor: String) { this?.value = webColor.asValue() } /** * Set color as RGB integer */ -public fun ColorAccessor?.set(rgb: Int) { +public operator fun ColorAccessor?.invoke(rgb: Int) { this?.value = Colors.rgbToString(rgb).asValue() } /** * Set color as RGB */ -public fun ColorAccessor?.set(r: UByte, g: UByte, b: UByte) { +public operator fun ColorAccessor?.invoke(r: UByte, g: UByte, b: UByte) { this?.value = Colors.rgbToString(r, g, b).asValue() } diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/LightSource.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/LightSource.kt index beeb4eb3..6064e6ed 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/LightSource.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/LightSource.kt @@ -15,7 +15,7 @@ import space.kscience.visionforge.* public abstract class LightSource : SolidBase() { override val descriptor: MetaDescriptor get() = LightSource.descriptor - public val color: ColorAccessor by color(SolidMaterial.COLOR_KEY) + public val color: ColorAccessor by colorProperty(SolidMaterial.COLOR_KEY) public var intensity: Number by properties.root(includeStyles = false).number(INTENSITY_KEY) { 1.0 } public companion object { diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidGroup.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidGroup.kt index 791bb0c9..8e04b638 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidGroup.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidGroup.kt @@ -43,6 +43,9 @@ public class SolidGroup : AbstractVisionGroup(), Solid, PrototypeHolder, Mutable it to value }.toMap() + /** + * Get a child solid with given relative [name] if it exists + */ public operator fun get(name: Name): Solid? = children.getChild(name) as? Solid private var prototypes: SolidGroup? @@ -84,6 +87,8 @@ public class SolidGroup : AbstractVisionGroup(), Solid, PrototypeHolder, Mutable } } +public operator fun SolidGroup.get(name:String): Solid? = get(name.parseAsName()) + @VisionBuilder public inline fun MutableVisionContainer.solidGroup( name: Name? = null, 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 e9301f67..1477c2a0 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 @@ -94,3 +94,7 @@ public inline fun VisionOutput.solid(options: Canvas3DOptions? = null, block: So } } } + +@VisionBuilder +public inline fun VisionOutput.solid(options: Canvas3DOptions.() -> Unit, block: SolidGroup.() -> Unit): SolidGroup = + solid(Canvas3DOptions(options), block) diff --git a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/CompositeTest.kt b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/CompositeTest.kt index e426aee2..a7c1c688 100644 --- a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/CompositeTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/CompositeTest.kt @@ -18,7 +18,7 @@ class CompositeTest { detail = 32 } material { - color.set("pink") + color("pink") } } } diff --git a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/GroupTest.kt b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/GroupTest.kt index 7f5138b1..78e2f03c 100644 --- a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/GroupTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/GroupTest.kt @@ -18,7 +18,7 @@ class GroupTest { } box(100, 100, 100) material { - color.set(Colors.lightgreen) + color(Colors.lightgreen) opacity = 0.3f } } @@ -30,7 +30,7 @@ class GroupTest { } box(100, 100, 100) y = 300 - color.set(Colors.red) + color(Colors.red) } subtract("subtract") { box(100, 100, 100) { @@ -40,7 +40,7 @@ class GroupTest { } box(100, 100, 100) y = -300 - color.set(Colors.blue) + color(Colors.blue) } } diff --git a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SerializationTest.kt b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SerializationTest.kt index 2497b5b3..e3069647 100644 --- a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SerializationTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SerializationTest.kt @@ -24,7 +24,7 @@ class SerializationTest { @Test fun testCubeSerialization() { val cube = Box(100f, 100f, 100f).apply { - color.set(222) + color(222) x = 100 z = -100 } @@ -37,7 +37,7 @@ class SerializationTest { @Test fun testProxySerialization() { val cube = Box(100f, 100f, 100f).apply { - color.set(222) + color(222) x = 100 z = -100 } @@ -59,7 +59,7 @@ class SerializationTest { fun lightSerialization(){ val group = testSolids.solidGroup { ambientLight { - color.set(Colors.white) + color(Colors.white) intensity = 100.0 } } diff --git a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SolidPropertyTest.kt b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SolidPropertyTest.kt index 5fa22b86..87ba368c 100644 --- a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SolidPropertyTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SolidPropertyTest.kt @@ -20,7 +20,7 @@ class SolidPropertyTest { val box = Box(10.0f, 10.0f, 10.0f) box.material { //meta["color"] = "pink" - color.set("pink") + color("pink") } assertEquals("pink", box.properties.getValue("material.color")?.string) assertEquals("pink", box.color.string) @@ -41,7 +41,7 @@ class SolidPropertyTest { delay(5) box.material { - color.set("pink") + color("pink") } assertEquals("pink", c.await()) diff --git a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SolidReferenceTest.kt b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SolidReferenceTest.kt index 8e851cb5..d8d971bb 100644 --- a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SolidReferenceTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SolidReferenceTest.kt @@ -16,7 +16,7 @@ class SolidReferenceTest { SolidMaterial.MATERIAL_COLOR_KEY put "red" } newRef("test", Box(100f,100f,100f).apply { - color.set("blue") + color("blue") useStyle(theStyle) }) } diff --git a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/VisionUpdateTest.kt b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/VisionUpdateTest.kt index 50e9362d..0e495aaa 100644 --- a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/VisionUpdateTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/VisionUpdateTest.kt @@ -22,7 +22,7 @@ internal class VisionUpdateTest { } val dif = visionManager.VisionChange { solidGroup("top") { - color.set(123) + color(123) box(100, 100, 100) } propertyChanged("top".asName(), SolidMaterial.MATERIAL_COLOR_KEY, Meta("red".asValue())) @@ -41,7 +41,7 @@ internal class VisionUpdateTest { fun testVisionChangeSerialization() { val change = visionManager.VisionChange { solidGroup("top") { - color.set(123) + color(123) box(100, 100, 100) } propertyChanged("top".asName(), SolidMaterial.MATERIAL_COLOR_KEY, Meta("red".asValue()))