From 8206a636b5466db9b0f7066d5bfd75e50e65bdb9 Mon Sep 17 00:00:00 2001 From: Peter Klimai Date: Fri, 31 Jan 2020 13:42:58 +0300 Subject: [PATCH] Implement Sphere.toGeometry #11 --- README.md | 4 +- .../hep/dataforge/vis/spatial/Sphere.kt | 37 ++++++++++++++----- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index aab72d1b..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. 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..a016747c 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,31 @@ 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 { + val z = r * cos(theta) + val x = r * sin(theta) * cos(phi) + val y = r * sin(theta) * sin(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 { + face4(point1, point2, point3, point4) + } + } + } } }