image.vision\spatiotemporal\temporal_ctx.mbt

100% · 14 / 14 points · 80 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 时间上下文 spatiotemporal/temporal_ctx
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 时态控制器:管理时变图像的播放状态。
  7. 7// Temporal context: manages playback state of time-varying images.
  8. 8
  9. 9///|
  10. 10/// 时态控制器:当前时间、帧率、循环、暂停、播放速度。
  11. 11/// 时间上下文 Temporal context: current time, fps, is_loop, paused, playback speed.
  12. 12#derive(Eq, @debug.Debug)
  13. 13pub(all) struct TContext {
  14. 14 current_time : Float
  15. 15 fps : Float
  16. 16 is_loop : Bool
  17. 17 paused : Bool
  18. 18 speed : Float
  19. 19}
  20. 20
  21. 21///|
  22. 22pub extend TContext with @debug.Debug::{to_repr}
  23. 23
  24. 24///|
  25. 25pub extend TContext with Eq::{not_equal, equal}
  26. 26
  27. 27///|
  28. 28/// 创建时态控制器,默认播放速度 1.0,非暂停,非循环。
  29. 29/// 创建时间上下文 Create a temporal context
  30. 30pub fn TContext::make(fps : Float) -> TContext raise @types.LoadError {
  31. 31● if fps <= 0.0F {
  32. 32● raise @types.LoadError::DecodeFailed("TContext::make: fps 必须大于0")
  33. 33 }
  34. 34 { current_time: 0.0F, fps, is_loop: false, paused: false, speed: 1.0F, }
  35. 35}
  36. 36
  37. 37///|
  38. 38/// 推进时间:按 fps 和 speed 推进 current_time,支持循环。
  39. 39/// 推进一帧时间(考虑速度和循环) Advance time by one frame step, considering speed and is_loop.
  40. 40pub fn TContext::step(self : TContext, total_duration : Float) -> TContext {
  41. 41● if self.paused {
  42. 42● return self
  43. 43 }
  44. 44 let dt = 1.0F / self.fps * self.speed
  45. 45 let mut new_time = self.current_time + dt
  46. 46 if self.is_loop && total_duration > 0.0F {
  47. 47● while new_time >= total_duration {
  48. 48● new_time = new_time - total_duration
  49. 49 }
  50. 50 }
  51. 51 { ..self, current_time: new_time, }
  52. 52}
  53. 53
  54. 54///|
  55. 55/// 跳转到指定时间。
  56. 56/// 跳转到特定时间 Seek to a specific time.
  57. 57pub fn TContext::seek(self : TContext, time : Float) -> TContext {
  58. 58● { ..self, current_time: if time < 0.0F { 0.0F } else { time }, }
  59. 59}
  60. 60
  61. 61///|
  62. 62/// 暂停/恢复。
  63. 63/// 暂停或恢复 Pause or resume.
  64. 64pub fn TContext::set_paused(self : TContext, paused : Bool) -> TContext {
  65. 65● { ..self, paused, }
  66. 66}
  67. 67
  68. 68///|
  69. 69/// 设置播放速度。
  70. 70/// 设置播放速度 Set playback speed.
  71. 71pub fn TContext::set_speed(self : TContext, speed : Float) -> TContext {
  72. 72● { ..self, speed: if speed < 0.0F { 0.0F } else { speed }, }
  73. 73}
  74. 74
  75. 75///|
  76. 76/// 设置循环。
  77. 77/// 设置循环模式 Set is_loop mode.
  78. 78pub fn TContext::set_loop(self : TContext, is_loop : Bool) -> TContext {
  79. 79● { ..self, is_loop, }
  80. 80}