diff --git a/README.md b/README.md index ff2820f1..afdbe49e 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ This repository contains [DataForge](http://npm.mipt.ru/dataforge/) (also [here](https://github.com/mipt-npm/dataforge-core)) components useful for visualization in -various scientific applications. Currently, the main application is 3D visualization for particle -physics experiments. +various scientific applications. The main application for now is 3D visualization for particle +physics experiments. Other applications including 2D plots are planned for future. The project is developed as a Kotlin multiplatform application, currently targeting browser JavaScript and JVM. @@ -70,4 +70,6 @@ To run full-stack app (both server and browser front-end), run ##### gdml -Visualization example for geometry defined as GDML file. +Visualization example for geometry defined as GDML file. Once you open Web application, +drag-and-drop GDML file to the window to see visualization. For example file, use +`demo\gdml\src\jsMain\resources\cubes.gdml`. diff --git a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Sphere.kt b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Sphere.kt index 26a9e820..58e2c8b6 100644 --- a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Sphere.kt +++ b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Sphere.kt @@ -8,6 +8,8 @@ import hep.dataforge.vis.common.AbstractVisualObject import kotlinx.serialization.Serializable import kotlinx.serialization.UseSerializers import kotlin.math.PI +import kotlin.math.cos +import kotlin.math.sin @Serializable class Sphere( @@ -26,16 +28,33 @@ class Sphere( override var scale: Point3D? = null override fun toGeometry(geometryBuilder: GeometryBuilder) { - TODO("not implemented") -// val segments = this.detail ?: 8 -// require(segments >= 4) { "The detail for sphere must be >= 4" } -// val phiStep = phi / segments -// val thetaStep = theta / segments -// for (i in 1 until segments - 1) { -// for (j in 0 until segments - 1) { -// val point1 = Point3D() -// } -// } + fun point3DfromSphCoord(r: Float, theta: Float, phi: Float): Point3D { + // This transformation matches three.js sphere implementation + val y = r * cos(theta) + val z = r * sin(theta) * sin(phi) + val x = - r * sin(theta) * cos(phi) + return Point3D(x, y, z) + } + val segments = this.detail ?: 8 + require(segments >= 4) { "The detail for sphere must be >= 4" } + val phiStep = phi / segments + val thetaStep = theta / segments + for (i in 0 until segments) { // theta iteration + val theta1 = thetaStart + i * thetaStep + val theta2 = theta1 + thetaStep + for (j in 0 until segments) { // phi iteration + val phi1 = phiStart + j * phiStep + val phi2 = phi1 + phiStep + val point1 = point3DfromSphCoord(radius, theta1, phi1) + val point2 = point3DfromSphCoord(radius, theta1, phi2) + val point3 = point3DfromSphCoord(radius, theta2, phi2) + val point4 = point3DfromSphCoord(radius, theta2, phi1) + geometryBuilder.apply { + // 1-2-3-4 gives the same face but with opposite orientation + face4(point1, point4, point3, point2) + } + } + } } } diff --git a/demo/gdml/src/jsMain/resources/cubes.gdml b/demo/gdml/src/jsMain/resources/cubes.gdml new file mode 100644 index 00000000..4d66c389 --- /dev/null +++ b/demo/gdml/src/jsMain/resources/cubes.gdml @@ -0,0 +1,254 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/demo/gdml/src/jsMain/web/index.html b/demo/gdml/src/jsMain/web/index.html index 3b2a99b2..bad59418 100644 --- a/demo/gdml/src/jsMain/web/index.html +++ b/demo/gdml/src/jsMain/web/index.html @@ -12,10 +12,10 @@
- Загрузить данные + title="To load data in text format, drag-and-drop file here"> + Load data
- (перетащить файл сюда) + (drag file here)

GDML demo