Add proper serialization for trajectory

This commit is contained in:
Alexander Nozik 2024-08-13 20:17:01 +03:00
parent 3ddced5c4a
commit e3b5ad0df4
5 changed files with 30 additions and 13 deletions

View File

@ -71,7 +71,7 @@ public fun StraightTrajectory2D(segment: LineSegment2D): StraightTrajectory2D =
@Serializable
@SerialName("arc")
public data class CircleTrajectory2D(
public val circle: Circle2D<Float64>,
@Serializable(Circle2DSerializer::class) public val circle: Circle2D<Float64>,
public val arcStart: Angle,
public val arcAngle: Angle,
) : Trajectory2D {

View File

@ -1,7 +1,12 @@
package space.kscience.trajectory
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import space.kscience.kmath.geometry.*
import space.kscience.kmath.geometry.euclidean2d.Circle2D
import space.kscience.kmath.geometry.euclidean2d.Float64Circle2D
import space.kscience.kmath.geometry.euclidean2d.Float64Space2D
import space.kscience.kmath.structures.Float64
import kotlin.math.abs
@ -95,3 +100,21 @@ public fun CircleTrajectory2D.containsPoint(point: Vector2D<Float64>): Boolean =
else -> arcAngle < offset - Angle.piTimes2
}
}
internal object Circle2DSerializer : KSerializer<Circle2D<Float64>> {
private val serializer = Float64Circle2D.serializer()
override val descriptor: SerialDescriptor
get() = serializer.descriptor
override fun deserialize(decoder: Decoder): Circle2D<Float64> {
return decoder.decodeSerializableValue(serializer)
}
override fun serialize(encoder: Encoder, value: Circle2D<Float64>) {
encoder.encodeSerializableValue(
serializer,
value as? Float64Circle2D ?: Float64Circle2D(value.center, value.radius)
)
}
}

View File

@ -3,15 +3,13 @@
* 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
package space.kscience.trajectory
import space.kscience.kmath.geometry.Angle
import space.kscience.kmath.geometry.euclidean2d.Circle2D
import space.kscience.kmath.geometry.euclidean2d.Float64Space2D
import space.kscience.kmath.geometry.euclidean2d.circumference
import space.kscience.trajectory.CircleTrajectory2D
import space.kscience.trajectory.Trajectory2D
import space.kscience.trajectory.circle
import space.kscience.trajectory.containsPoint
import space.kscience.kmath.geometry.radians
import kotlin.math.PI
import kotlin.test.Test
import kotlin.test.assertEquals

View File

@ -3,16 +3,13 @@
* 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
package space.kscience.trajectory
import space.kscience.kmath.geometry.LineSegment2D
import space.kscience.kmath.geometry.euclidean2d.Circle2D
import space.kscience.kmath.geometry.euclidean2d.Float64Space2D
import space.kscience.kmath.geometry.euclidean2d.circumference
import space.kscience.kmath.structures.Float64
import space.kscience.trajectory.circle
import space.kscience.trajectory.intersects
import space.kscience.trajectory.intersectsOrInside
import space.kscience.trajectory.segment
import kotlin.math.pow
import kotlin.math.sqrt
import kotlin.test.Test

View File

@ -3,10 +3,9 @@
* 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
package space.kscience.trajectory
import space.kscience.kmath.geometry.euclidean2d.Float64Space2D
import space.kscience.trajectory.StraightTrajectory2D
import kotlin.math.pow
import kotlin.math.sqrt
import kotlin.test.Test