image.vision\fieldimg\vector_field.mbt

53.8% · 14 / 26 points · 158 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/vector_field
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 矢量场:2D 矢量场,每个点 (x, y) 映射到 dim 维 Float 矢量。
  7. 7// Vector field: 2D vector field, each point (x, y) maps to a dim-dimensional Float vector.
  8. 8// RGB 图可视为 2D 矢量场(dim=3);梯度场 dim=2。
  9. 9
  10. 10///|
  11. 11/// 矢量场:2D 矢量场,data 为行主序 Float 数组,长度 = width * height * dim。
  12. 12/// 向量场:2D 向量场,行主序 Vector field: 2D vector field, data is row-major, length = width * height * dim.
  13. 13#derive(Eq, @debug.Debug)
  14. 14pub(all) struct VectorField {
  15. 15 width : Int32
  16. 16 height : Int32
  17. 17 dim : Int32
  18. 18 data : Array[Float]
  19. 19}
  20. 20
  21. 21///|
  22. 22pub extend VectorField with @debug.Debug::{to_repr}
  23. 23
  24. 24///|
  25. 25pub extend VectorField with Eq::{not_equal, equal}
  26. 26
  27. 27///|
  28. 28/// 创建矢量场,数据长度必须等于 width * height * dim。
  29. 29/// 创建向量场;数据 Create a vector field; data
  30. 30pub fn VectorField::make(
  31. 31 width : Int32,
  32. 32 height : Int32,
  33. 33 dim : Int32,
  34. 34 data : Array[Float],
  35. 35) -> VectorField raise @types.LoadError {
  36. 36● if width <= 0 || height <= 0 || dim <= 0 {
  37. 37○ raise @types.LoadError::DecodeFailed(
  38. 38 "VectorField::make: width, height, dim 必须大于0",
  39. 39 )
  40. 40 }
  41. 41● if data.length() != width * height * dim {
  42. 42● raise @types.LoadError::DecodeFailed(
  43. 43 "VectorField::make: 数据长度必须等于 width * height * dim",
  44. 44 )
  45. 45 }
  46. 46 { width, height, dim, data, }
  47. 47}
  48. 48
  49. 49///|
  50. 50/// 创建全零矢量场。
  51. 51/// 创建全零向量场 Create an all-zero vector field.
  52. 52pub fn VectorField::zeros(
  53. 53 width : Int32,
  54. 54 height : Int32,
  55. 55 dim : Int32,
  56. 56) -> VectorField raise @types.LoadError {
  57. 57● if width <= 0 || height <= 0 || dim <= 0 {
  58. 58○ raise @types.LoadError::DecodeFailed(
  59. 59 "VectorField::zeros: width, height, dim 必须大于0",
  60. 60 )
  61. 61 }
  62. 62● { width, height, dim, data: Array::make(width * height * dim, 0.0F), }
  63. 63}
  64. 64
  65. 65///|
  66. 66/// 求值:获取点 (x, y) 处的矢量(返回 dim 维数组副本),边界外返回零矢量。
  67. 67/// 求值向量于 (x, y);越界返回零向量 Evaluate the vector at (x, y); returns zero vector for out-of-bounds.
  68. 68pub fn VectorField::evaluate(
  69. 69 self : VectorField,
  70. 70 x : Int32,
  71. 71 y : Int32,
  72. 72) -> Array[Float] {
  73. 73○ if x < 0 || x >= self.width || y < 0 || y >= self.height {
  74. 74○ return Array::make(self.dim, 0.0F)
  75. 75 }
  76. 76 let base = (y * self.width + x) * self.dim
  77. 77○ let result = Array::make(self.dim, 0.0F)
  78. 78 for k in 0..<self.dim {
  79. 79○ result[k] = self.data[base + k]
  80. 80 }
  81. 81 result
  82. 82}
  83. 83
  84. 84///|
  85. 85/// 求值单分量:获取点 (x, y) 处矢量第 k 分量,边界外返回 0。
  86. 86/// 求值分量 k 于 (x, y);越界返回 0 Evaluate a single component k at (x, y); returns 0 for out-of-bounds.
  87. 87pub fn VectorField::evaluate_component(
  88. 88 self : VectorField,
  89. 89 x : Int32,
  90. 90 y : Int32,
  91. 91 k : Int32,
  92. 92) -> Float {
  93. 93● if x < 0 ||
  94. 94 x >= self.width ||
  95. 95 y < 0 ||
  96. 96 y >= self.height ||
  97. 97 k < 0 ||
  98. 98 k >= self.dim {
  99. 99● return 0.0F
  100. 100 }
  101. 101 self.data[(y * self.width + x) * self.dim + k]
  102. 102}
  103. 103
  104. 104///|
  105. 105/// 求值单分量(边界钳制)。
  106. 106/// 钳位边界求值单个分量 Evaluate a single component with clamped boundary.
  107. 107pub fn VectorField::evaluate_component_clamped(
  108. 108 self : VectorField,
  109. 109 x : Int32,
  110. 110 y : Int32,
  111. 111 k : Int32,
  112. 112) -> Float {
  113. 113○ let cx = x.clamp(min=0, max=self.width - 1)
  114. 114○ let cy = y.clamp(min=0, max=self.height - 1)
  115. 115○ let ck = k.clamp(min=0, max=self.dim - 1)
  116. 116 self.data[(cy * self.width + cx) * self.dim + ck]
  117. 117}
  118. 118
  119. 119///|
  120. 120/// 设值单分量:设置点 (x, y) 处矢量第 k 分量,越界则忽略。
  121. 121/// 设置分量 k 于 (x, y);越界忽略 Set component k at (x, y); out-of-bounds is ignored.
  122. 122pub fn VectorField::set_component(
  123. 123 self : VectorField,
  124. 124 x : Int32,
  125. 125 y : Int32,
  126. 126 k : Int32,
  127. 127 value : Float,
  128. 128) -> Unit {
  129. 129● if x < 0 ||
  130. 130 x >= self.width ||
  131. 131 y < 0 ||
  132. 132 y >= self.height ||
  133. 133 k < 0 ||
  134. 134 k >= self.dim {
  135. 135○ return
  136. 136 }
  137. 137 self.data[(y * self.width + x) * self.dim + k] = value
  138. 138}
  139. 139
  140. 140///|
  141. 141/// 矢量场幅值:返回每个点的矢量模长,形成标量场。
  142. 142/// 向量幅值:返回各点向量范数的标量场 Vector magnitude: returns a scalar field of vector norms at each point.
  143. 143pub fn VectorField::magnitude(
  144. 144 self : VectorField,
  145. 145) -> ScalarField raise @types.LoadError {
  146. 146● let n = self.width * self.height
  147. 147● let data = Array::make(n, 0.0F)
  148. 148 for i in 0..<n {
  149. 149● let base = i * self.dim
  150. 150 let mut sum = 0.0F
  151. 151 for k in 0..<self.dim {
  152. 152● let v = self.data[base + k]
  153. 153 sum = sum + v * v
  154. 154 }
  155. 155● data[i] = sum.sqrt()
  156. 156 }
  157. 157● ScalarField::make(self.width, self.height, data)
  158. 158}