From e644b1b20a066f243c4f7f003a914d04e8d4ef7e Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 7 May 2021 18:24:36 +0300 Subject: [PATCH] Add Hexagon. Second part of #39 fix --- CHANGELOG.md | 2 + .../playground/src/jvmMain/kotlin/gdmlIaxo.kt | 1 + .../src/jvmMain/kotlin/serverExtensions.kt | 1 - .../kscience/visionforge/solid/demo/demo.kt | 2 +- .../visionforge/gdml/GdmlTransformer.kt | 17 +++- .../space/kscience/visionforge/solid/Box.kt | 50 ----------- .../kscience/visionforge/solid/ConeSegment.kt | 4 +- .../kscience/visionforge/solid/Hexagon.kt | 88 +++++++++++++++++++ .../kscience/visionforge/solid/Solids.kt | 1 + 9 files changed, 112 insertions(+), 54 deletions(-) delete mode 100644 visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Box.kt create mode 100644 visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Hexagon.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 5dc2ab31..1685272b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Change collector - Customizable accessors for colors - SphereLayer solid +- Hexagon interface and GenericHexagon implementation (Box inherits Hexagon) ### Changed - Vision does not implement ItemProvider anymore. Property changes are done via `getProperty`/`setProperty` and `property` delegate. @@ -15,6 +16,7 @@ - \[Format breaking change!\] Stylesheets are moved into properties under `@stylesheet` key - VisionGroup builder accepts `null` as name for statics instead of `""` - gdml sphere is rendered as a SphereLayer instead of Sphere (#35) +- Tube is replaced by more general ConeSurface ### Deprecated diff --git a/demo/playground/src/jvmMain/kotlin/gdmlIaxo.kt b/demo/playground/src/jvmMain/kotlin/gdmlIaxo.kt index a45345cc..cd496761 100644 --- a/demo/playground/src/jvmMain/kotlin/gdmlIaxo.kt +++ b/demo/playground/src/jvmMain/kotlin/gdmlIaxo.kt @@ -10,6 +10,7 @@ fun main() { val context = Context { plugin(Solids) } + context.makeVisionFile(resourceLocation = ResourceLocation.EMBED) { vision("canvas") { GdmlShowCase.babyIaxo().toVision() diff --git a/demo/playground/src/jvmMain/kotlin/serverExtensions.kt b/demo/playground/src/jvmMain/kotlin/serverExtensions.kt index 4dfd35f1..b71b1873 100644 --- a/demo/playground/src/jvmMain/kotlin/serverExtensions.kt +++ b/demo/playground/src/jvmMain/kotlin/serverExtensions.kt @@ -25,7 +25,6 @@ public fun Context.makeVisionFile( show: Boolean = true, content: VisionTagConsumer<*>.() -> Unit ): Unit { - val actualPath = page(title, content = content).makeFile(path) { actualPath -> mapOf("threeJs" to scriptHeader("js/visionforge-playground.js", resourceLocation, actualPath)) } 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 c5cf2a56..18c56704 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 @@ -181,7 +181,7 @@ fun VisionLayout.showcaseCSG() { demo("CSG.custom", "CSG with manually created object") { intersect { - tube(60, 10) { + cylinder(60, 10) { detail = 32 } box(100, 100, 100) diff --git a/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlTransformer.kt b/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlTransformer.kt index ec53b430..08aa7351 100644 --- a/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlTransformer.kt +++ b/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlTransformer.kt @@ -282,13 +282,28 @@ private class GdmlTransformer(val settings: GdmlTransformerSettings) { } } + is GdmlTrapezoid -> { + val dxBottom = solid.x1.toDouble() + val dxTop = solid.x2.toDouble() + val dyBottom = solid.y1.toDouble() + val dyTop = solid.y2.toDouble() + val dz = solid.z.toDouble() + val node1 = Point3D(-dxBottom, -dyBottom, -dz) + val node2 = Point3D(dxBottom, -dyBottom, -dz) + val node3 = Point3D(dxBottom, dyBottom, -dz) + val node4 = Point3D(-dxBottom, dyBottom, -dz) + val node5 = Point3D(-dxTop, -dyTop, dz) + val node6 = Point3D(dxTop, -dyTop, dz) + val node7 = Point3D(dxTop, dyTop, dz) + val node8 = Point3D(-dxTop, dyTop, dz) + hexagon(node1, node2, node3, node4, node5, node6, node7, node8, name) + } is GdmlEllipsoid -> TODO("Renderer for $solid not supported yet") is GdmlElTube -> TODO("Renderer for $solid not supported yet") is GdmlElCone -> TODO("Renderer for $solid not supported yet") is GdmlParaboloid -> TODO("Renderer for $solid not supported yet") is GdmlParallelepiped -> TODO("Renderer for $solid not supported yet") is GdmlTorus -> TODO("Renderer for $solid not supported yet") - is GdmlTrapezoid -> TODO("Renderer for $solid not supported yet") is GdmlPolycone -> TODO("Renderer for $solid not supported yet") } } diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Box.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Box.kt deleted file mode 100644 index c84d5c9b..00000000 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Box.kt +++ /dev/null @@ -1,50 +0,0 @@ -package space.kscience.visionforge.solid - -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable -import space.kscience.visionforge.VisionBuilder -import space.kscience.visionforge.VisionContainerBuilder -import space.kscience.visionforge.set - -@Serializable -@SerialName("solid.box") -public class Box( - public val xSize: Float, - public val ySize: Float, - public val zSize: Float -) : SolidBase(), GeometrySolid { - - //TODO add helper for color configuration - override fun toGeometry(geometryBuilder: GeometryBuilder) { - val dx = xSize / 2 - val dy = ySize / 2 - val dz = zSize / 2 - val node1 = Point3D(-dx, -dy, -dz) - val node2 = Point3D(dx, -dy, -dz) - val node3 = Point3D(dx, dy, -dz) - val node4 = Point3D(-dx, dy, -dz) - val node5 = Point3D(-dx, -dy, dz) - val node6 = Point3D(dx, -dy, dz) - val node7 = Point3D(dx, dy, dz) - val node8 = Point3D(-dx, dy, dz) - geometryBuilder.face4(node1, node4, node3, node2) - geometryBuilder.face4(node1, node2, node6, node5) - geometryBuilder.face4(node2, node3, node7, node6) - geometryBuilder.face4(node4, node8, node7, node3) - geometryBuilder.face4(node1, node5, node8, node4) - geometryBuilder.face4(node8, node5, node6, node7) - } - - public companion object { - - } -} - -@VisionBuilder -public inline fun VisionContainerBuilder.box( - xSize: Number, - ySize: Number, - zSize: Number, - name: String? = null, - action: Box.() -> Unit = {} -): Box = Box(xSize.toFloat(), ySize.toFloat(), zSize.toFloat()).apply(action).also { set(name, it) } \ No newline at end of file diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/ConeSegment.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/ConeSegment.kt index a0cc0342..94d99cc3 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/ConeSegment.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/ConeSegment.kt @@ -2,6 +2,7 @@ package space.kscience.visionforge.solid import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import space.kscience.visionforge.VisionBuilder import space.kscience.visionforge.VisionContainerBuilder import space.kscience.visionforge.set import kotlin.math.cos @@ -64,6 +65,7 @@ public class ConeSegment( } +@VisionBuilder public inline fun VisionContainerBuilder.cylinder( r: Number, height: Number, @@ -75,7 +77,7 @@ public inline fun VisionContainerBuilder.cylinder( r.toFloat() ).apply(block).also { set(name, it) } - +@VisionBuilder public inline fun VisionContainerBuilder.cone( bottomRadius: Number, height: Number, diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Hexagon.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Hexagon.kt new file mode 100644 index 00000000..baced452 --- /dev/null +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Hexagon.kt @@ -0,0 +1,88 @@ +package space.kscience.visionforge.solid + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import space.kscience.visionforge.VisionBuilder +import space.kscience.visionforge.VisionContainerBuilder +import space.kscience.visionforge.set + +public interface Hexagon : GeometrySolid { + public val node1: Point3D + public val node2: Point3D + public val node3: Point3D + public val node4: Point3D + public val node5: Point3D + public val node6: Point3D + public val node7: Point3D + public val node8: Point3D + + override fun toGeometry(geometryBuilder: GeometryBuilder) { + geometryBuilder.face4(node1, node4, node3, node2) + geometryBuilder.face4(node1, node2, node6, node5) + geometryBuilder.face4(node2, node3, node7, node6) + geometryBuilder.face4(node4, node8, node7, node3) + geometryBuilder.face4(node1, node5, node8, node4) + geometryBuilder.face4(node8, node5, node6, node7) + } +} + +/** + * A separate class is created to optimize native rendering + */ +@Serializable +@SerialName("solid.box") +public class Box( + public val xSize: Float, + public val ySize: Float, + public val zSize: Float, +) : SolidBase(), Hexagon { + + private inline val dx get() = xSize / 2 + private inline val dy get() = ySize / 2 + private inline val dz get() = zSize / 2 + + override val node1: Point3D get() = Point3D(-dx, -dy, -dz) + override val node2: Point3D get() = Point3D(dx, -dy, -dz) + override val node3: Point3D get() = Point3D(dx, dy, -dz) + override val node4: Point3D get() = Point3D(-dx, dy, -dz) + override val node5: Point3D get() = Point3D(-dx, -dy, dz) + override val node6: Point3D get() = Point3D(dx, -dy, dz) + override val node7: Point3D get() = Point3D(dx, dy, dz) + override val node8: Point3D get() = Point3D(-dx, dy, dz) +} + +@VisionBuilder +public inline fun VisionContainerBuilder.box( + xSize: Number, + ySize: Number, + zSize: Number, + name: String? = null, + action: Box.() -> Unit = {}, +): Box = Box(xSize.toFloat(), ySize.toFloat(), zSize.toFloat()).apply(action).also { set(name, it) } + +@Serializable +@SerialName("solid.hexagon") +public class GenericHexagon( + override val node1: Point3D, + override val node2: Point3D, + override val node3: Point3D, + override val node4: Point3D, + override val node5: Point3D, + override val node6: Point3D, + override val node7: Point3D, + override val node8: Point3D, +) : SolidBase(), Hexagon + +@VisionBuilder +public inline fun VisionContainerBuilder.hexagon( + node1: Point3D, + node2: Point3D, + node3: Point3D, + node4: Point3D, + node5: Point3D, + node6: Point3D, + node7: Point3D, + node8: Point3D, + name: String? = null, + action: Hexagon.() -> Unit = {}, +): Hexagon = GenericHexagon(node1, node2, node3, node4, node5, node6, node7, node8).apply(action).also { set(name, it) } \ 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 536770ec..f65bd7d2 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 @@ -30,6 +30,7 @@ public class Solids(meta: Meta) : VisionPlugin(meta) { subclass(SolidReferenceGroup.serializer()) subclass(Composite.serializer()) subclass(Box.serializer()) + subclass(GenericHexagon.serializer()) subclass(ConeSegment.serializer()) subclass(ConeSurface.serializer()) subclass(Convex.serializer())