image.vision\fieldimg\scalar_field.mbt

92.3% · 24 / 26 points · 147 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/// # 标量场 fieldimg/scalar_field
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 标量场:2D 标量场,将每个点 (x, y) 映射到 Float 值。
  7. 7// Scalar field: a 2D field mapping each point (x, y) to a Float value.
  8. 8// 灰度图可视为 2D 标量场;场是一等几何对象。
  9. 9
  10. 10///|
  11. 11/// 标量场:2D 标量场,data 为行主序 Float 数组,长度 = width * height。
  12. 12/// 标量场:2D Scalar field: 2D
  13. 13#derive(Eq, @debug.Debug)
  14. 14pub(all) struct ScalarField {
  15. 15 width : Int32
  16. 16 height : Int32
  17. 17 data : Array[Float]
  18. 18}
  19. 19
  20. 20///|
  21. 21pub extend ScalarField with @debug.Debug::{to_repr}
  22. 22
  23. 23///|
  24. 24pub extend ScalarField with Eq::{not_equal, equal}
  25. 25
  26. 26///|
  27. 27/// 创建标量场,数据长度必须等于 width * height。
  28. 28/// 创建标量场;数据长度须等于 width * height Create a scalar field; data length must equal width * height.
  29. 29pub fn ScalarField::make(
  30. 30 width : Int32,
  31. 31 height : Int32,
  32. 32 data : Array[Float],
  33. 33) -> ScalarField raise @types.LoadError {
  34. 34● if width <= 0 || height <= 0 {
  35. 35● raise @types.LoadError::DecodeFailed(
  36. 36 "ScalarField::make: width 和 height 必须大于0",
  37. 37 )
  38. 38 }
  39. 39● if data.length() != width * height {
  40. 40● raise @types.LoadError::DecodeFailed(
  41. 41 "ScalarField::make: 数据长度必须等于 width * height",
  42. 42 )
  43. 43 }
  44. 44 { width, height, data, }
  45. 45}
  46. 46
  47. 47///|
  48. 48/// 创建全零标量场。
  49. 49/// 创建全零标量场 Create an all-zero scalar field.
  50. 50pub fn ScalarField::zeros(
  51. 51 width : Int32,
  52. 52 height : Int32,
  53. 53) -> ScalarField raise @types.LoadError {
  54. 54● if width <= 0 || height <= 0 {
  55. 55○ raise @types.LoadError::DecodeFailed(
  56. 56 "ScalarField::zeros: width 和 height 必须大于0",
  57. 57 )
  58. 58 }
  59. 59● { width, height, data: Array::make(width * height, 0.0F), }
  60. 60}
  61. 61
  62. 62///|
  63. 63/// 创建常量标量场(所有点值相同)。
  64. 64/// 创建常量标量场(所有点共享同一值) Create a constant scalar field (all points share the same value).
  65. 65pub fn ScalarField::constant(
  66. 66 width : Int32,
  67. 67 height : Int32,
  68. 68 value : Float,
  69. 69) -> ScalarField raise @types.LoadError {
  70. 70● if width <= 0 || height <= 0 {
  71. 71○ raise @types.LoadError::DecodeFailed(
  72. 72 "ScalarField::constant: width 和 height 必须大于0",
  73. 73 )
  74. 74 }
  75. 75● { width, height, data: Array::make(width * height, value), }
  76. 76}
  77. 77
  78. 78///|
  79. 79/// 求值:获取点 (x, y) 处的场值,边界外返回 0。
  80. 80/// 求值场于 (x, y);越界返回 0 Evaluate the field at (x, y); returns 0 for out-of-bounds.
  81. 81pub fn ScalarField::evaluate(self : ScalarField, x : Int32, y : Int32) -> Float {
  82. 82● if x < 0 || x >= self.width || y < 0 || y >= self.height {
  83. 83● return 0.0F
  84. 84 }
  85. 85 self.data[y * self.width + x]
  86. 86}
  87. 87
  88. 88///|
  89. 89/// 求值(边界钳制):边界外取最近边界点的值。
  90. 90/// 钳位边界求值:越界取最近边缘值 Evaluate with clamped boundary: out-of-bounds takes nearest edge value.
  91. 91pub fn ScalarField::evaluate_clamped(
  92. 92 self : ScalarField,
  93. 93 x : Int32,
  94. 94 y : Int32,
  95. 95) -> Float {
  96. 96● let cx = x.clamp(min=0, max=self.width - 1)
  97. 97● let cy = y.clamp(min=0, max=self.height - 1)
  98. 98 self.data[cy * self.width + cx]
  99. 99}
  100. 100
  101. 101///|
  102. 102/// 设值:设置点 (x, y) 处的场值,越界则忽略。
  103. 103/// 设置值于 (x, y);越界忽略 Set the value at (x, y); out-of-bounds is ignored.
  104. 104pub fn ScalarField::set(
  105. 105 self : ScalarField,
  106. 106 x : Int32,
  107. 107 y : Int32,
  108. 108 value : Float,
  109. 109) -> Unit {
  110. 110● if x < 0 || x >= self.width || y < 0 || y >= self.height {
  111. 111● return
  112. 112 }
  113. 113 self.data[y * self.width + x] = value
  114. 114}
  115. 115
  116. 116///|
  117. 117/// 场的像素总数。
  118. 118/// 场中总点数 Total number of points in the field.
  119. 119pub fn ScalarField::size(self : ScalarField) -> Int32 {
  120. 120● self.width * self.height
  121. 121}
  122. 122
  123. 123///|
  124. 124/// 场的最小值。
  125. 125/// 场中最小值 Minimum value in the field.
  126. 126pub fn ScalarField::min_value(self : ScalarField) -> Float {
  127. 127● let mut m = self.data[0]
  128. 128● for i in 1..<self.data.length() {
  129. 129● if self.data[i] < m {
  130. 130● m = self.data[i]
  131. 131 }
  132. 132 }
  133. 133 m
  134. 134}
  135. 135
  136. 136///|
  137. 137/// 场的最大值。
  138. 138/// 场中最大值 Maximum value in the field.
  139. 139pub fn ScalarField::max_value(self : ScalarField) -> Float {
  140. 140● let mut m = self.data[0]
  141. 141● for i in 1..<self.data.length() {
  142. 142● if self.data[i] > m {
  143. 143● m = self.data[i]
  144. 144 }
  145. 145 }
  146. 146 m
  147. 147}