From 8fecd7647b277fd686e3fbaaf3893004e617582e Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 10 Jul 2019 20:14:37 +0300 Subject: [PATCH] Polyhedra shape --- .../dataforge/vis/spatial/gdml/GDMLPlugin.kt | 31 ------- .../dataforge/vis/spatial/gdml/Polyhedra.kt | 16 ++++ .../vis/spatial/gdml/ThreeGDMLFactory.kt | 92 ------------------- .../dataforge/vis/spatial/gdml/visualGDML.kt | 33 ++++++- .../hep/dataforge/vis/spatial/Extruded.kt | 4 +- 5 files changed, 47 insertions(+), 129 deletions(-) delete mode 100644 dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/GDMLPlugin.kt create mode 100644 dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/Polyhedra.kt delete mode 100644 dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/ThreeGDMLFactory.kt diff --git a/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/GDMLPlugin.kt b/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/GDMLPlugin.kt deleted file mode 100644 index e484039c..00000000 --- a/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/GDMLPlugin.kt +++ /dev/null @@ -1,31 +0,0 @@ -//package hep.dataforge.vis.spatial.gdml -// -//import hep.dataforge.context.AbstractPlugin -//import hep.dataforge.context.Context -//import hep.dataforge.context.PluginFactory -//import hep.dataforge.context.PluginTag -//import hep.dataforge.meta.Meta -//import hep.dataforge.names.Name -//import hep.dataforge.names.toName -//import hep.dataforge.vis.spatial.three.ThreeFactory -//import hep.dataforge.vis.spatial.three.ThreePlugin -//import kotlin.reflect.KClass -// -//class GDMLPlugin : AbstractPlugin() { -// override val tag: PluginTag get() = GDMLPlugin.tag -// -// override fun dependsOn() = listOf(ThreePlugin) -// -// override fun provideTop(target: String): Map { -// return when(target){ -// ThreeFactory.TYPE-> mapOf("gdml".toName() to ThreeGDMLFactory) -// else -> emptyMap() -// } -// } -// -// companion object : PluginFactory { -// override val tag = PluginTag("vis.gdml", "hep.dataforge") -// override val type: KClass = GDMLPlugin::class -// override fun invoke(meta: Meta) = GDMLPlugin() -// } -//} \ No newline at end of file diff --git a/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/Polyhedra.kt b/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/Polyhedra.kt new file mode 100644 index 00000000..0bb9d661 --- /dev/null +++ b/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/Polyhedra.kt @@ -0,0 +1,16 @@ +package hep.dataforge.vis.spatial.gdml + +import hep.dataforge.meta.Meta +import hep.dataforge.vis.common.DisplayLeaf +import hep.dataforge.vis.common.VisualObject +import hep.dataforge.vis.spatial.GeometryBuilder +import hep.dataforge.vis.spatial.Shape + +class Polyhedra(parent: VisualObject?, meta: Meta) : DisplayLeaf(parent, meta), Shape { + + + override fun toGeometry(geometryBuilder: GeometryBuilder) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + +} \ No newline at end of file diff --git a/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/ThreeGDMLFactory.kt b/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/ThreeGDMLFactory.kt deleted file mode 100644 index dbde0130..00000000 --- a/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/ThreeGDMLFactory.kt +++ /dev/null @@ -1,92 +0,0 @@ -//package hep.dataforge.vis.spatial.gdml -// -//import hep.dataforge.meta.Meta -//import hep.dataforge.meta.buildMeta -//import hep.dataforge.meta.toDynamic -//import hep.dataforge.meta.values -//import hep.dataforge.vis.common.DisplayLeaf -//import hep.dataforge.vis.common.VisualObject -//import hep.dataforge.vis.common.int -//import hep.dataforge.vis.spatial.three.MeshThreeFactory -////import hep.dataforge.vis.spatial.jsroot.createCubeBuffer -////import hep.dataforge.vis.spatial.jsroot.createGeometry -////import hep.dataforge.vis.spatial.jsroot.createTubeBuffer -////import hep.dataforge.vis.spatial.jsroot.createXtruBuffer -//import info.laht.threekt.core.BufferGeometry -// -// -//class GDMLShape(parent: VisualObject?, meta: Meta, val shape: GDMLSolid) : -// DisplayLeaf(parent, meta) { -// -// var facesLimit by int(0) -// -// companion object { -// const val TYPE = "geometry.3d.gdml" -// } -//} -// -// -//object ThreeGDMLFactory : MeshThreeFactory(GDMLShape::class) { -// //TODO fix ineffective conversion -// private fun Meta?.toJsRoot() = this?.let { -// buildMeta { -// values().forEach { (name, value) -> -// name.toString().replaceFirst("p", "f") to value -// } -// } -// } -// -// override fun buildGeometry(obj: GDMLShape): BufferGeometry { -// return when (obj.shape) { -// is GDMLBox -> createCubeBuffer( -// obj.shape.config.toJsRoot()?.toDynamic(), -// obj.facesLimit -// ) -// is GDMLTube -> createTubeBuffer( -// obj.shape.config.toJsRoot()?.toDynamic(), -// obj.facesLimit -// ) -// is GDMLXtru -> { -// val meta = buildMeta { -// val vertices = obj.shape.vertices -// val zs = obj.shape.sections.sortedBy { it.zOrder!! } -// "fNz" to zs.size -// "fNvert" to vertices.size -// "fX" to vertices.map { it.x } -// "fY" to vertices.map { it.y } -// "fX0" to zs.map { it.xOffsset } -// "fY0" to zs.map { it.yOffset } -// "fZ" to zs.map { it.zPosition!! } -// "fScale" to zs.map { it.scalingFactor } -// } -// createXtruBuffer(meta.toDynamic(), obj.facesLimit) -// } -// is GDMLUnion -> { -// val meta = buildMeta { -// "fNode.fLeft" to obj.shape.first()?.config.toJsRoot() -// "fNode.fRight" to obj.shape.second()?.config.toJsRoot() -// "fNode._typename" to "TGeoUnion" -// } -// createGeometry(meta.toDynamic(), obj.facesLimit) -// } -// is GDMLSubtraction -> { -// val meta = buildMeta { -// "fNode.fLeft" to obj.shape.first()?.config.toJsRoot() -// "fNode.fRight" to obj.shape.second()?.config.toJsRoot() -// "fNode._typename" to "TGeoSubtraction" -// } -// createGeometry(meta.toDynamic(), obj.facesLimit) -// } -// is GDMLIntersection -> { -// val meta = buildMeta { -// "fNode.fLeft" to obj.shape.first()?.config.toJsRoot() -// "fNode.fRight" to obj.shape.second()?.config.toJsRoot() -// "fNode._typename" to "TGeoIntersection" -// } -// createGeometry(meta.toDynamic(), obj.facesLimit) -// } -// -// -// } -// } -//} \ No newline at end of file diff --git a/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/visualGDML.kt b/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/visualGDML.kt index 3f5f725d..8f48d915 100644 --- a/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/visualGDML.kt +++ b/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/visualGDML.kt @@ -7,6 +7,8 @@ import hep.dataforge.vis.common.VisualObject import hep.dataforge.vis.common.color import hep.dataforge.vis.spatial.* import scientifik.gdml.* +import kotlin.math.cos +import kotlin.math.sin private fun VisualObject.applyPosition(pos: GDMLPosition): VisualObject = apply { @@ -28,7 +30,14 @@ private fun VisualGroup.addSolid(root: GDML, solid: GDMLSolid, block: VisualObje is GDMLBox -> box(solid.x, solid.y, solid.z) is GDMLTube -> TODO() is GDMLXtru -> extrude { - TODO() + shape { + solid.vertices.forEach { + point(it.x, it.y) + } + } + solid.sections.sortedBy { it.zOrder }.forEach { section -> + layer(section.zPosition ?: 0f, section.xOffset, section.yOffset, section.scalingFactor) + } } is GDMLScaledSolid -> { //Add solid with modified scale @@ -46,7 +55,21 @@ private fun VisualGroup.addSolid(root: GDML, solid: GDMLSolid, block: VisualObje thetaStart = solid.starttheta.toDouble() } is GDMLOrb -> sphere(solid.r) - is GDMLPolyhedra -> TODO() + is GDMLPolyhedra -> extrude { + //getting the radius of first + require(solid.planes.size > 1) { "The polyhedron geometry requires at least two planes" } + val baseRadius = solid.planes.first().rmax.toDouble() + shape { + (0..solid.numsides).forEach { + val phi = solid.deltaphi.toDouble() / solid.numsides * it + solid.startphi.toDouble() + baseRadius * cos(phi) to baseRadius * sin(phi) + } + } + solid.planes.forEach { plane -> + //scaling all radii relative to first layer radius + layer(plane.z, scale = plane.rmax.toDouble() / baseRadius) + } + } is GDMLBoolSolid -> { val first = solid.first.resolve(root) ?: error("") val second = solid.second.resolve(root) ?: error("") @@ -85,8 +108,8 @@ private fun VisualGroup.addVolume( } gdmlVolume.physVolumes.forEach { - val volume = it.volumeref.resolve(root)?: error("Volume with ref ${it.volumeref.ref} could not be resolved") - addVolume(root,volume,resolveColor).apply { + val volume = it.volumeref.resolve(root) ?: error("Volume with ref ${it.volumeref.ref} could not be resolved") + addVolume(root, volume, resolveColor).apply { it.resolvePosition(root)?.let { pos -> applyPosition(pos) } it.resolveRotation(root)?.let { rot -> applyRotation(rot) } } @@ -99,5 +122,5 @@ private fun VisualGroup.addVolume( fun GDML.toVisual(): VisualGroup { //TODO add materials cache fun GDMLMaterialBase.color(): Meta = EmptyMeta - return VisualGroup().also { it.addVolume(this, world){color()} } + return VisualGroup().also { it.addVolume(this, world) { color() } } } \ No newline at end of file diff --git a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Extruded.kt b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Extruded.kt index 23473de0..0f1763bb 100644 --- a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Extruded.kt +++ b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Extruded.kt @@ -1,8 +1,8 @@ package hep.dataforge.vis.spatial import hep.dataforge.meta.* -import hep.dataforge.vis.common.VisualGroup import hep.dataforge.vis.common.DisplayLeaf +import hep.dataforge.vis.common.VisualGroup import hep.dataforge.vis.common.VisualObject import kotlin.math.PI import kotlin.math.cos @@ -18,6 +18,8 @@ class Shape2DBuilder { list.add(Point2D(x, y)) } + infix fun Number.to(y:Number) = point(this, y) + fun build(): Shape2D = list }