From dba001eff345c0dafe0c38a80278fa81ec369291 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 20 Feb 2024 20:39:57 +0300 Subject: [PATCH] Fix types in geometry algebras --- .../kotlin/space/kscience/kmath/geometry/Line.kt | 11 +++++------ .../kmath/geometry/{euclidean2d => }/Polygon.kt | 4 +--- .../kscience/kmath/geometry/euclidean2d/Circle2D.kt | 12 ++++++++---- .../kmath/geometry/euclidean3d/Float64Space3D.kt | 3 ++- .../kmath/geometry/euclidean3d/rotations3D.kt | 5 +++-- .../space/kscience/kmath/geometry/vectorPrecision.kt | 5 +++-- 6 files changed, 22 insertions(+), 18 deletions(-) rename kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/{euclidean2d => }/Polygon.kt (73%) diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Line.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Line.kt index a226ab04d..c65ccd43d 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Line.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Line.kt @@ -7,8 +7,7 @@ package space.kscience.kmath.geometry import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import space.kscience.kmath.geometry.euclidean2d.Float64Vector2D -import space.kscience.kmath.geometry.euclidean3d.Float64Vector3D +import space.kscience.kmath.structures.Float64 /** * A line formed by [start] vector of start and a [direction] vector. Direction vector is not necessarily normalized, @@ -25,8 +24,8 @@ private data class LineImpl(override val start: V, override val dir public fun Line(base: V, direction: V): Line = LineImpl(base, direction) -public typealias Line2D = Line -public typealias Line3D = Line +public typealias Line2D = Line> +public typealias Line3D = Line> /** * A directed line segment between [begin] and [end] @@ -49,5 +48,5 @@ public fun LineSegment.line(algebra: GeometrySpace): Line Line(begin, end - begin) } -public typealias LineSegment2D = LineSegment -public typealias LineSegment3D = LineSegment +public typealias LineSegment2D = LineSegment> +public typealias LineSegment3D = LineSegment> diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Polygon.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Polygon.kt similarity index 73% rename from kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Polygon.kt rename to kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Polygon.kt index 21ae86dbb..d7b450ab0 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Polygon.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Polygon.kt @@ -3,9 +3,7 @@ * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ -package space.kscience.kmath.geometry.euclidean2d - -import space.kscience.kmath.geometry.Vector2D +package space.kscience.kmath.geometry /** diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Circle2D.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Circle2D.kt index e17a5573a..fcffc7714 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Circle2D.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Circle2D.kt @@ -7,10 +7,11 @@ package space.kscience.kmath.geometry.euclidean2d import kotlinx.serialization.Serializable import space.kscience.kmath.geometry.Vector2D +import space.kscience.kmath.structures.Float64 import kotlin.math.PI -public interface Circle2D{ +public interface Circle2D { public val center: Vector2D public val radius: Double } @@ -23,9 +24,12 @@ public val Circle2D<*>.circumference: Double get() = radius * 2 * PI @Serializable public data class Float64Circle2D( @Serializable(Float64Space2D.VectorSerializer::class) override val center: Float64Vector2D, - override val radius: Double -): Circle2D + override val radius: Float64, +) : Circle2D -public fun Circle2D(center: Float64Vector2D, radius: Double): Circle2D = Float64Circle2D(center, radius) +public fun Circle2D(center: Vector2D, radius: Double): Circle2D = Float64Circle2D( + center as? Float64Vector2D ?: Float64Vector2D(center.x, center.y), + radius +) diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/Float64Space3D.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/Float64Space3D.kt index a2c23926a..86ddf554b 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/Float64Space3D.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/Float64Space3D.kt @@ -128,7 +128,8 @@ public object Float64Space3D : GeometrySpace, Double> { override val bufferFactory: MutableBufferFactory> = MutableBufferFactory() } +@Suppress("UnusedReceiverParameter") public val Float64Field.euclidean3D: Float64Space3D get() = Float64Space3D -public val Float64Vector3D.r: Double get() = Float64Space3D.norm(this) +public val Vector3D.r: Double get() = Float64Space3D.norm(this) diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/rotations3D.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/rotations3D.kt index 212cc251e..e65bd8e22 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/rotations3D.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/rotations3D.kt @@ -37,7 +37,8 @@ public infix fun Quaternion.dot(other: Quaternion): Double = w * other.w + x * o /** * Represent a vector as quaternion with zero a rotation angle. */ -internal fun Float64Vector3D.asQuaternion(): Quaternion = Quaternion(0.0, x, y, z) +internal fun Vector3D.asQuaternion(): Quaternion = Quaternion(0.0, x, y, z) + /** * Angle in radians denoted by this quaternion rotation @@ -71,7 +72,7 @@ public val Quaternion.vector: Float64Vector3D /** * Rotate a vector in a [Float64Space3D] with [quaternion] */ -public fun Float64Space3D.rotate(vector: Float64Vector3D, quaternion: Quaternion): Float64Vector3D = +public fun Float64Space3D.rotate(vector: Vector3D, quaternion: Quaternion): Float64Vector3D = with(QuaternionAlgebra) { val p = vector.asQuaternion() (quaternion * p * quaternion.reciprocal).vector diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/vectorPrecision.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/vectorPrecision.kt index 9eb1c069c..e8a14d036 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/vectorPrecision.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/vectorPrecision.kt @@ -9,6 +9,7 @@ import space.kscience.kmath.geometry.euclidean2d.Float64Space2D import space.kscience.kmath.geometry.euclidean2d.Float64Vector2D import space.kscience.kmath.geometry.euclidean3d.Float64Space3D import space.kscience.kmath.geometry.euclidean3d.Float64Vector3D +import space.kscience.kmath.structures.Float64 /** @@ -25,7 +26,7 @@ public fun > V.equalsVector( /** * Vector equality using Euclidian L2 norm and given [precision] */ -public fun Float64Vector2D.equalsVector( +public fun Vector2D.equalsVector( other: Float64Vector2D, precision: Double = Float64Space2D.defaultPrecision, ): Boolean = equalsVector(Float64Space2D, other, precision) @@ -33,7 +34,7 @@ public fun Float64Vector2D.equalsVector( /** * Vector equality using Euclidean L2 norm and given [precision] */ -public fun Float64Vector3D.equalsVector( +public fun Vector3D.equalsVector( other: Float64Vector3D, precision: Double = Float64Space3D.defaultPrecision, ): Boolean = equalsVector(Float64Space3D, other, precision)