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 e593150f1..a7f6ae35d 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 @@ -5,15 +5,23 @@ package space.kscience.kmath.geometry +import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import space.kscience.kmath.operations.DoubleField.pow /** - * A line formed by [base] vector of start and a [direction] vector. Direction vector is not necessarily normalized, + * A line formed by [start] vector of start and a [direction] vector. Direction vector is not necessarily normalized, * but its length does not affect line properties */ +public interface Line { + public val start: V + public val direction: V +} + @Serializable -public data class Line(val base: V, val direction: V) +@SerialName("Line") +private data class LineImpl(override val start: V, override val direction: V): Line + +public fun Line(base: V, direction: V): Line = LineImpl(base, direction) public typealias Line2D = Line public typealias Line3D = Line @@ -21,8 +29,19 @@ public typealias Line3D = Line /** * A directed line segment between [begin] and [end] */ +public interface LineSegment { + public val begin: V + public val end: V +} + +/** + * Basic implementation for [LineSegment] + */ @Serializable -public data class LineSegment(val begin: V, val end: V) +@SerialName("LineSegment") +private data class LineSegmentImpl(override val begin: V, override val end: V) : LineSegment + +public fun LineSegment(begin: V, end: V): LineSegment = LineSegmentImpl(begin, end) public fun LineSegment.line(algebra: GeometrySpace): Line = with(algebra) { Line(begin, end - begin) diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/projections.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/projections.kt index 6950abbdc..c5c3487a1 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/projections.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/projections.kt @@ -13,7 +13,7 @@ package space.kscience.kmath.geometry * @param line line to which vector should be projected */ public fun GeometrySpace.projectToLine(vector: V, line: Line): V = with(line) { - base + (direction dot (vector - base)) / (direction dot direction) * direction + start + (direction dot (vector - start)) / (direction dot direction) * direction } /** diff --git a/kmath-trajectory/src/commonMain/kotlin/space/kscience/kmath/trajectory/Obstacle.kt b/kmath-trajectory/src/commonMain/kotlin/space/kscience/kmath/trajectory/Obstacle.kt index f2768bbca..3819afb54 100644 --- a/kmath-trajectory/src/commonMain/kotlin/space/kscience/kmath/trajectory/Obstacle.kt +++ b/kmath-trajectory/src/commonMain/kotlin/space/kscience/kmath/trajectory/Obstacle.kt @@ -23,7 +23,7 @@ internal data class Tangent( val lineSegment: LineSegment2D, val startDirection: Trajectory2D.Direction, val endDirection: Trajectory2D.Direction = startDirection, -) +): LineSegment2D by lineSegment private class TangentPath(val tangents: List) { fun last() = tangents.last() @@ -128,7 +128,7 @@ private fun dubinsTangentsToCircles( endCircle = secondCircle, startObstacle = firstObstacle, endObstacle = secondObstacle, - lineSegment = LineSegment2D( + lineSegment = LineSegment( firstCircle.center + w * r1, secondCircle.center + w * r2 ), @@ -192,7 +192,7 @@ public class Obstacle( other, this@Obstacle, this@Obstacle, - LineSegment2D( + LineSegment( center + w * r1, other.center + w * r2 ), @@ -245,7 +245,7 @@ public class Obstacle( circles[i - 1], this, this, - LineSegment2D( + LineSegment( tangents[i - 1].lineSegment.end, tangents[i - 1].lineSegment.begin ), @@ -257,7 +257,7 @@ public class Obstacle( circles.last(), this, this, - LineSegment2D( + LineSegment( tangents.last().lineSegment.end, tangents.last().lineSegment.begin ), @@ -493,7 +493,7 @@ internal fun findAllPaths( initialCircles[i]!!, Obstacle(listOf(initialCircles[i]!!)), Obstacle(listOf(initialCircles[i]!!)), - LineSegment2D(start, start), + LineSegment(start, start), i ) ) @@ -582,7 +582,7 @@ internal fun findAllPaths( end, Obstacle(end), Obstacle(end), - LineSegment2D(finish, finish), + LineSegment(finish, finish), startDirection = lastDirection, endDirection = j ) diff --git a/kmath-trajectory/src/commonMain/kotlin/space/kscience/kmath/trajectory/Trajectory2D.kt b/kmath-trajectory/src/commonMain/kotlin/space/kscience/kmath/trajectory/Trajectory2D.kt index d6974b105..8df0de237 100644 --- a/kmath-trajectory/src/commonMain/kotlin/space/kscience/kmath/trajectory/Trajectory2D.kt +++ b/kmath-trajectory/src/commonMain/kotlin/space/kscience/kmath/trajectory/Trajectory2D.kt @@ -50,7 +50,7 @@ public data class StraightTrajectory2D( public val bearing: Angle get() = (atan2(end.x - start.x, end.y - start.y).radians).normalized() } -public fun StraightTrajectory2D.toSegment(): LineSegment> = LineSegment2D(start, end) +public fun StraightTrajectory2D.toSegment(): LineSegment> = LineSegment(start, end) /** * An arc segment