Minor refactoring of tangents

This commit is contained in:
Alexander Nozik 2023-02-16 10:57:24 +03:00
parent 67316c4a70
commit ed4aa47913
6 changed files with 14 additions and 15 deletions

View File

@ -100,9 +100,7 @@ public data class CircleTrajectory2D(
val pose1 = calculatePose(start, s1.bearing, direction) val pose1 = calculatePose(start, s1.bearing, direction)
val pose2 = calculatePose(end, s2.bearing, direction) val pose2 = calculatePose(end, s2.bearing, direction)
val trajectory = CircleTrajectory2D(Circle2D(center, s1.length), pose1, pose2) val trajectory = CircleTrajectory2D(Circle2D(center, s1.length), pose1, pose2)
if (trajectory.direction != direction) { if (trajectory.direction != direction) error("Trajectory direction mismatch")
error("Trajectory direction mismatch")
}
return trajectory return trajectory
} }
} }

View File

@ -16,7 +16,7 @@ import kotlin.test.assertTrue
class TangentTest { class TangentTest {
@Test @Test
fun tangent() { fun tangents() {
val c1 = Circle2D(vector(0.0, 0.0), 1.0) val c1 = Circle2D(vector(0.0, 0.0), 1.0)
val c2 = Circle2D(vector(4.0, 0.0), 1.0) val c2 = Circle2D(vector(4.0, 0.0), 1.0)
val routes = listOf( val routes = listOf(

View File

@ -6,6 +6,7 @@
package space.kscience.kmath.trajectory.dubins package space.kscience.kmath.trajectory.dubins
import space.kscience.kmath.geometry.Euclidean2DSpace import space.kscience.kmath.geometry.Euclidean2DSpace
import space.kscience.kmath.geometry.equalsFloat
import space.kscience.kmath.trajectory.* import space.kscience.kmath.trajectory.*
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertNotNull import kotlin.test.assertNotNull
@ -39,7 +40,7 @@ class DubinsTests {
val path = dubins.find { p -> DubinsPath.trajectoryTypeOf(p) === it.key } val path = dubins.find { p -> DubinsPath.trajectoryTypeOf(p) === it.key }
assertNotNull(path, "Path ${it.key} not found") assertNotNull(path, "Path ${it.key} not found")
println("${it.key}: ${path.length}") println("${it.key}: ${path.length}")
assertTrue(it.value.equalFloat(path.length)) assertTrue(it.value.equalsFloat(path.length))
val a = path.segments[0] as CircleTrajectory2D val a = path.segments[0] as CircleTrajectory2D
val b = path.segments[1] val b = path.segments[1]

View File

@ -6,20 +6,16 @@
package space.kscience.kmath.trajectory package space.kscience.kmath.trajectory
import space.kscience.kmath.geometry.Euclidean2DSpace import space.kscience.kmath.geometry.Euclidean2DSpace
import space.kscience.kmath.geometry.equalsFloat
import space.kscience.kmath.geometry.radians import space.kscience.kmath.geometry.radians
import space.kscience.kmath.geometry.sin import space.kscience.kmath.geometry.sin
import kotlin.math.PI
import kotlin.math.abs
const val maxFloatDelta = 0.000001
fun Double.radiansToDegrees() = this * 180 / PI
fun Double.equalFloat(other: Double) = abs(this - other) < maxFloatDelta
fun DubinsPose2D.equalsFloat(other: DubinsPose2D) = fun DubinsPose2D.equalsFloat(other: DubinsPose2D) =
x.equalFloat(other.x) && y.equalFloat(other.y) && bearing.radians.equalFloat(other.bearing.radians) x.equalsFloat(other.x) && y.equalsFloat(other.y) && bearing.radians.equalsFloat(other.bearing.radians)
fun StraightTrajectory2D.inverse() = StraightTrajectory2D(end, start) fun StraightTrajectory2D.inverse() = StraightTrajectory2D(end, start)
fun StraightTrajectory2D.shift(shift: Int, width: Double): StraightTrajectory2D = with(Euclidean2DSpace) { fun StraightTrajectory2D.shift(shift: Int, width: Double): StraightTrajectory2D = with(Euclidean2DSpace) {
val dX = width * sin(inverse().bearing) val dX = width * sin(inverse().bearing)
val dY = width * sin(bearing) val dY = width * sin(bearing)

View File

@ -18,7 +18,12 @@ class ArcTests {
@Test @Test
fun arcTest() = with(Euclidean2DSpace){ fun arcTest() = with(Euclidean2DSpace){
val circle = Circle2D(vector(0.0, 0.0), 2.0) val circle = Circle2D(vector(0.0, 0.0), 2.0)
val arc = CircleTrajectory2D.of(circle.center, vector(-2.0, 0.0), vector(0.0, 2.0), CircleTrajectory2D.Direction.RIGHT) val arc = CircleTrajectory2D.of(
circle.center,
vector(-2.0, 0.0),
vector(0.0, 2.0),
CircleTrajectory2D.Direction.RIGHT
)
assertEquals(circle.circumference / 4, arc.length, 1.0) assertEquals(circle.circumference / 4, arc.length, 1.0)
assertEquals(0.0, arc.start.bearing.degrees) assertEquals(0.0, arc.start.bearing.degrees)
assertEquals(90.0, arc.end.bearing.degrees) assertEquals(90.0, arc.end.bearing.degrees)

View File

@ -8,7 +8,6 @@ package space.kscience.kmath.trajectory.segments
import space.kscience.kmath.geometry.Circle2D import space.kscience.kmath.geometry.Circle2D
import space.kscience.kmath.geometry.Euclidean2DSpace import space.kscience.kmath.geometry.Euclidean2DSpace
import space.kscience.kmath.geometry.circumference import space.kscience.kmath.geometry.circumference
import space.kscience.kmath.trajectory.maxFloatDelta
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -20,6 +19,6 @@ class CircleTests {
val radius = 2.0 val radius = 2.0
val expectedCircumference = 12.56637 val expectedCircumference = 12.56637
val circle = Circle2D(center, radius) val circle = Circle2D(center, radius)
assertEquals(expectedCircumference, circle.circumference, maxFloatDelta) assertEquals(expectedCircumference, circle.circumference)
} }
} }