Skip to content

Commit cd36af1

Browse files
committed
启用进度条,修复并完善路径预览
1 parent 4d93b1a commit cd36af1

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

composeApp/src/commonMain/kotlin/ftc19656/azconductor/route/RouteCore.kt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ class RouteCore() {
1111
// 获取最后一个点
1212
val lastPoint: ControlNode? get() = waypoints.lastOrNull()
1313
val totalLength: Double get() = trajectoryList.sumOf { it.length }
14-
val totalTime: Double get() = trajectoryList.sumOf { it.duration }
14+
val totalTime: Double
15+
get() = trajectoryList.indices.sumOf { index ->
16+
trajectoryList[index].duration.coerceAtLeast(0.0) + arrivalDelayForTrajectory(index)
17+
}
18+
19+
private fun arrivalDelayForTrajectory(index: Int): Double {
20+
return _waypoints.getOrNull(index + 1)?.delayAfterArrive?.coerceAtLeast(0.0) ?: 0.0
21+
}
1522

1623
// 根据 waypoints 重新构建整条轨迹
1724
private fun rebuildTrajectories() {
@@ -119,13 +126,20 @@ class RouteCore() {
119126
val coercedTime = time.coerceIn(0.0, totalTime)
120127

121128
var accumulatedTime = 0.0
122-
for (traj in trajectoryList) {
123-
val nextAccumulatedTime = accumulatedTime + traj.duration
124-
if (coercedTime <= nextAccumulatedTime) {
129+
for ((index, traj) in trajectoryList.withIndex()) {
130+
val trajectoryDuration = traj.duration.coerceAtLeast(0.0)
131+
val trajectoryEndTime = accumulatedTime + trajectoryDuration
132+
if (coercedTime <= trajectoryEndTime) {
125133
val localTime = coercedTime - accumulatedTime
126134
return traj.getPointAtTime(localTime)
127135
}
128-
accumulatedTime = nextAccumulatedTime
136+
137+
val delayEndTime = trajectoryEndTime + arrivalDelayForTrajectory(index)
138+
if (coercedTime <= delayEndTime) {
139+
return traj.getPointAtTime(traj.duration)
140+
}
141+
142+
accumulatedTime = delayEndTime
129143
}
130144
// 理论上循环一定能命中,但为了防止极限情况下的浮点精度微小误差导致跳出循环
131145
// 直接返回最后一段轨迹的终点状态

composeApp/src/commonTest/kotlin/ftc19656/azconductor/ComposeAppCommonTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ftc19656.azconductor
22

33
import ftc19656.azconductor.route.ControlNode
4+
import ftc19656.azconductor.route.RouteCore
45
import kotlinx.serialization.encodeToString
56
import kotlinx.serialization.json.Json
67
import kotlin.test.Test
@@ -30,4 +31,17 @@ class ComposeAppCommonTest {
3031
assertTrue(p1 isCloseTo p2)
3132
assertTrue(!(p1 isCloseTo p3))
3233
}
34+
35+
@Test
36+
fun testRoutePreviewIncludesDelayAfterArrive() {
37+
val route = RouteCore()
38+
route.addPoint(ControlNode(x = 0.0, dx = 0.0, y = 0.0, dy = 0.0))
39+
route.addPoint(ControlNode(x = 10.0, dx = 0.0, y = 0.0, dy = 0.0, duration = 2.0, delayAfterArrive = 3.0))
40+
route.addPoint(ControlNode(x = 20.0, dx = 0.0, y = 0.0, dy = 0.0, duration = 2.0))
41+
42+
assertEquals(7.0, route.totalTime)
43+
assertEquals(10.0, route.getPointAtTime(2.5)!!.x)
44+
assertEquals(10.0, route.getPointAtTime(5.0)!!.x)
45+
assertTrue(route.getPointAtTime(6.0)!!.x > 10.0)
46+
}
3347
}

0 commit comments

Comments
 (0)