Fix tests

This commit is contained in:
Alexander Nozik 2023-02-18 18:53:03 +03:00
parent cc0fb2a718
commit 04127fc3f2
4 changed files with 43 additions and 49 deletions

View File

@ -9,7 +9,7 @@ kotlin.native.ignoreDisabledTargets=true
org.gradle.configureondemand=true org.gradle.configureondemand=true
org.gradle.jvmargs=-Xmx4096m org.gradle.jvmargs=-Xmx4096m
toolsVersion=0.14.0-kotlin-1.8.10 toolsVersion=0.14.1-kotlin-1.8.10
org.gradle.parallel=true org.gradle.parallel=true

View File

@ -5,10 +5,7 @@
package space.kscience.kmath.trajectory package space.kscience.kmath.trajectory
import space.kscience.kmath.geometry.Circle2D import space.kscience.kmath.geometry.*
import space.kscience.kmath.geometry.DoubleVector2D
import space.kscience.kmath.geometry.Euclidean2DSpace
import space.kscience.kmath.geometry.LineSegment
import kotlin.math.* import kotlin.math.*
/** /**
@ -18,10 +15,14 @@ import kotlin.math.*
public fun Circle2D.tangentsToCircle( public fun Circle2D.tangentsToCircle(
other: Circle2D, other: Circle2D,
): Map<DubinsPath.Type, LineSegment<DoubleVector2D>> = with(Euclidean2DSpace) { ): Map<DubinsPath.Type, LineSegment<DoubleVector2D>> = with(Euclidean2DSpace) {
//return empty map for concentric circles
if(center.equalsVector(other.center)) return@tangentsToCircle emptyMap()
// A line connecting centers
val line = LineSegment(center, other.center) val line = LineSegment(center, other.center)
val d = line.begin.distanceTo(line.end) // Distance between centers
val distance = line.begin.distanceTo(line.end)
val angle1 = atan2(other.center.x - center.x, other.center.y - center.y) val angle1 = atan2(other.center.x - center.x, other.center.y - center.y)
var r: Double
var angle2: Double var angle2: Double
val routes = mapOf( val routes = mapOf(
DubinsPath.Type.RSR to Pair(radius, other.radius), DubinsPath.Type.RSR to Pair(radius, other.radius),
@ -33,30 +34,27 @@ public fun Circle2D.tangentsToCircle(
for ((route, r1r2) in routes) { for ((route, r1r2) in routes) {
val r1 = r1r2.first val r1 = r1r2.first
val r2 = r1r2.second val r2 = r1r2.second
r = if (r1.sign == r2.sign) { val r = if (r1.sign == r2.sign) {
r1.absoluteValue - r2.absoluteValue r1.absoluteValue - r2.absoluteValue
} else { } else {
r1.absoluteValue + r2.absoluteValue r1.absoluteValue + r2.absoluteValue
} }
if (d * d > r * r) { if (distance <= r) TODO("Intersecting circles are not supported yet")
val l = (d * d - r * r).pow(0.5) val l = sqrt(distance * distance - r * r)
angle2 = if (r1.absoluteValue > r2.absoluteValue) { angle2 = if (r1.absoluteValue > r2.absoluteValue) {
angle1 + r1.sign * atan2(r.absoluteValue, l) angle1 + r1.sign * atan2(r.absoluteValue, l)
} else { } else {
angle1 - r2.sign * atan2(r.absoluteValue, l) angle1 - r2.sign * atan2(r.absoluteValue, l)
} }
val w = vector(-cos(angle2), sin(angle2)) val w = vector(-cos(angle2), sin(angle2))
put( put(
route, route,
LineSegment( LineSegment(
center + w * r1, center + w * r1,
other.center + w * r2 other.center + w * r2
)
) )
} )
else {
throw Exception("Circles should not be")
}
} }
} }
} }

View File

@ -13,7 +13,6 @@ import space.kscience.kmath.geometry.equalsLine
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertTrue import kotlin.test.assertTrue
import kotlin.test.assertFailsWith
class TangentTest { class TangentTest {
@Test @Test
@ -56,26 +55,23 @@ class TangentTest {
} }
@Test @Test
fun nonExistingTangents() { fun concentric(){
assertFailsWith<Exception> { val c1 = Circle2D(vector(0.0, 0.0), 10.0)
val c1 = Circle2D(vector(0.0, 0.0), 10.0) val c2 = Circle2D(vector(0.0, 0.0), 1.0)
val c2 = Circle2D(vector(0.0, 0.0), 1.0) assertEquals(emptyMap(), c1.tangentsToCircle(c2))
val segments = c1.tangentsToCircle(c2)
}
assertFailsWith<Exception> {
val c1 = Circle2D(vector(0.0, 0.0), 1.0)
val c2 = Circle2D(vector(0.0, 0.0), 10.0)
val segments = c1.tangentsToCircle(c2)
}
assertFailsWith<Exception> {
val c1 = Circle2D(vector(0.0, 0.0), 1.0)
val c2 = Circle2D(vector(2.0, 0.0), 1.0)
val segments = c1.tangentsToCircle(c2)
}
assertFailsWith<Exception> {
val c1 = Circle2D(vector(0.0, 0.0), 1.0)
val c2 = Circle2D(vector(0.5, 0.0), 1.0)
val segments = c1.tangentsToCircle(c2)
}
} }
//
// @Test
// fun nonExistingTangents() {
// assertFailsWith<NotImplementedError> {
// val c1 = Circle2D(vector(0.0, 0.0), 1.0)
// val c2 = Circle2D(vector(2.0, 0.0), 1.0)
// c1.tangentsToCircle(c2)
// }
// assertFailsWith<NotImplementedError> {
// val c1 = Circle2D(vector(0.0, 0.0), 1.0)
// val c2 = Circle2D(vector(0.5, 0.0), 1.0)
// c1.tangentsToCircle(c2)
// }
// }
} }

View File

@ -19,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) assertEquals(expectedCircumference, circle.circumference, 1e-4)
} }
} }