From 968d544d3bed5bda24a92f586915b047acab36aa Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Thu, 9 May 2024 16:10:51 +0200 Subject: [PATCH] [orx-fcurve] Add FCurve.start, Fcurve.end, MultiFCurve.start, MultiFCurve.end --- orx-fcurve/src/commonMain/kotlin/FCurve.kt | 26 +++++++++++++++++++ .../src/commonMain/kotlin/MultiFCurve.kt | 12 +++++++++ .../src/commonTest/kotlin/TestFCurve.kt | 6 +++++ 3 files changed, 44 insertions(+) diff --git a/orx-fcurve/src/commonMain/kotlin/FCurve.kt b/orx-fcurve/src/commonMain/kotlin/FCurve.kt index dd7eaef0..37fd834e 100644 --- a/orx-fcurve/src/commonMain/kotlin/FCurve.kt +++ b/orx-fcurve/src/commonMain/kotlin/FCurve.kt @@ -129,6 +129,31 @@ data class FCurve(val segments: List) { * The unitless duration of the Fcurve */ val duration: Double + get() { + return if (segments.isEmpty()) { + 0.0 + } else { + end - start + } + } + + /** + * The unitless start position of the Fcurve + */ + val start: Double + get() { + return if (segments.isEmpty()) { + 0.0 + } else { + segments.first().start.x + + } + } + + /** + * The unitless end position of the Fcurve + */ + val end: Double get() { return if (segments.isEmpty()) { 0.0 @@ -137,6 +162,7 @@ data class FCurve(val segments: List) { } } + /** * Evaluate the Fcurve at [t] * @param segment an optional segment that can be used to speed up scanning for the relevant segment diff --git a/orx-fcurve/src/commonMain/kotlin/MultiFCurve.kt b/orx-fcurve/src/commonMain/kotlin/MultiFCurve.kt index f801a39b..82174473 100644 --- a/orx-fcurve/src/commonMain/kotlin/MultiFCurve.kt +++ b/orx-fcurve/src/commonMain/kotlin/MultiFCurve.kt @@ -13,6 +13,18 @@ open class MultiFCurve(val compounds: Map) { * Duration of the [MultiFCurve] */ val duration by lazy { compounds.values.maxOfOrNull { it?.duration ?: 0.0 } ?: 0.0 } + + + /** + * Start position of the [MultiFCurve] + */ + val start by lazy { compounds.values.minOfOrNull { it?.start ?: 0.0 } ?: 0.0 } + + /** + * End position of the [MultiFCurve] + */ + val end by lazy { compounds.values.maxOfOrNull { it?.end ?: 0.0 } ?: 0.0 } + operator fun get(name: String): FCurve? { return compounds[name] } diff --git a/orx-fcurve/src/commonTest/kotlin/TestFCurve.kt b/orx-fcurve/src/commonTest/kotlin/TestFCurve.kt index c62d1b6b..cf20660a 100644 --- a/orx-fcurve/src/commonTest/kotlin/TestFCurve.kt +++ b/orx-fcurve/src/commonTest/kotlin/TestFCurve.kt @@ -24,12 +24,18 @@ class TestFCurve { val fc = fcurve(text) assertEquals(0.0, fc.value(-1.0)) assertEquals(5.0, fc.value(4.0)) + assertEquals(-1.0, fc.start) + assertEquals(4.0, fc.end) + assertEquals(5.0, fc.duration) } run { val text = "H1 L 5 5" val fc = fcurve(text) assertEquals(0.0, fc.value(1.0)) assertEquals(5.0, fc.value(6.0)) + assertEquals(1.0, fc.start) + assertEquals(6.0, fc.end) + assertEquals(5.0, fc.duration) } } } \ No newline at end of file