Working on new shapes

This commit is contained in:
Alexander Nozik 2019-06-30 20:23:38 +03:00
parent 598030f970
commit aa867af3c3
5 changed files with 30 additions and 13 deletions

View File

@ -25,6 +25,7 @@ configure<KotlinFrontendExtension> {
configure<NpmExtension> { configure<NpmExtension> {
dependency("three-full") dependency("three-full")
dependency("style-loader") dependency("style-loader")
dependency("element-resize-event")
devDependency("karma") devDependency("karma")
} }

View File

@ -6,6 +6,7 @@ import hep.dataforge.meta.*
import hep.dataforge.output.Output import hep.dataforge.output.Output
import hep.dataforge.vis.common.DisplayGroup import hep.dataforge.vis.common.DisplayGroup
import hep.dataforge.vis.common.DisplayObject import hep.dataforge.vis.common.DisplayObject
import hep.dataforge.vis.spatial.demo.require
import hep.dataforge.vis.spatial.three.Group import hep.dataforge.vis.spatial.three.Group
import info.laht.threekt.WebGLRenderer import info.laht.threekt.WebGLRenderer
import info.laht.threekt.cameras.PerspectiveCamera import info.laht.threekt.cameras.PerspectiveCamera
@ -18,6 +19,8 @@ import info.laht.threekt.scenes.Scene
import org.w3c.dom.Element import org.w3c.dom.Element
import kotlin.browser.window import kotlin.browser.window
private val elementResizeEvent = require("element-resize-event")
class ThreeOutput(override val context: Context, val meta: Meta = EmptyMeta) : Output<DisplayObject> { class ThreeOutput(override val context: Context, val meta: Meta = EmptyMeta) : Output<DisplayObject> {
private val aspectRatio by meta.number(1.0).double private val aspectRatio by meta.number(1.0).double
@ -41,7 +44,6 @@ class ThreeOutput(override val context: Context, val meta: Meta = EmptyMeta) : O
} }
fun attach(element: Element, computeWidth: Element.() -> Int = { element.clientWidth }) { fun attach(element: Element, computeWidth: Element.() -> Int = { element.clientWidth }) {
val width by meta.number(computeWidth(element)).int val width by meta.number(computeWidth(element)).int
val height: Int = (width / aspectRatio).toInt() val height: Int = (width / aspectRatio).toInt()
@ -60,13 +62,11 @@ class ThreeOutput(override val context: Context, val meta: Meta = EmptyMeta) : O
renderer.render(scene, camera) renderer.render(scene, camera)
} }
window.addEventListener("resize", { elementResizeEvent(element) {
camera.updateProjectionMatrix() camera.updateProjectionMatrix()
val newWidth = computeWidth(element)
val width by meta.number(computeWidth(element)).int renderer.setSize(newWidth, (newWidth / aspectRatio).toInt())
}
renderer.setSize(width, (width / aspectRatio).toInt())
}, false)
element.replaceWith(renderer.domElement) element.replaceWith(renderer.domElement)
animate() animate()

View File

@ -52,8 +52,7 @@ class ThreeDemoGrid(meta: Meta) : AbstractPlugin(meta), OutputManager {
div("col-4") { div("col-4") {
h2 { +(meta["title"].string ?: name.toString()) } h2 { +(meta["title"].string ?: name.toString()) }
hr() hr()
val id = "output-$name" output.attach(div { id = "output-$name" })
output.attach(div { this.id = id }) { 300 }
} }
} }
} }

View File

@ -6,18 +6,37 @@ import hep.dataforge.vis.common.DisplayLeaf
import hep.dataforge.vis.common.DisplayObject import hep.dataforge.vis.common.DisplayObject
import hep.dataforge.vis.common.DisplayObjectList import hep.dataforge.vis.common.DisplayObjectList
typealias Shape2D = List<Point2D>
data class Layer(val z: Number, val x: Number = 0.0, val y: Number = 0.0, val scale: Number = 1.0)
class Extruded(parent: DisplayObject?, meta: Meta) : DisplayLeaf(parent, meta) { class Extruded(parent: DisplayObject?, meta: Meta) : DisplayLeaf(parent, meta) {
val shape get() = shape(properties["shape"] ?: error("Shape not defined")) val shape
get() = shape(properties["shape"] ?: error("Shape not defined"))
val layers
get() = properties.getAll("layer").values.map {
layer(it.node ?: error("layer item is not a node"))
}
companion object { companion object {
const val TYPE = "geometry.3d.extruded" const val TYPE = "geometry.3d.extruded"
fun shape(item: MetaItem<*>): Shape2D { private fun shape(item: MetaItem<*>): Shape2D {
return item.node?.getAll("xyPoint".toName())?.map { (_, value) -> return item.node?.getAll("xyPoint".toName())?.map { (_, value) ->
Point2D(value.node["x"].number ?: 0, value.node["y"].number ?: 0) Point2D(value.node["x"].number ?: 0, value.node["y"].number ?: 0)
} ?: emptyList() } ?: emptyList()
} }
private fun layer(meta: Meta): Layer {
val x by meta.number(0.0)
val y by meta.number(0.0)
val z by meta.number { error("z is undefined in layer") }
val scale by meta.number(1.0)
return Layer(z, x, y, scale)
}
} }
} }

View File

@ -8,8 +8,6 @@ import hep.dataforge.vis.common.DisplayObject
data class Point2D(val x: Number, val y: Number) data class Point2D(val x: Number, val y: Number)
typealias Shape2D = List<Point2D>
data class Point3D(val x: Number, val y: Number, val z: Number) : MetaRepr { data class Point3D(val x: Number, val y: Number, val z: Number) : MetaRepr {
override fun toMeta(): Meta = buildMeta { override fun toMeta(): Meta = buildMeta {
"x" to x "x" to x