image.vision\fieldimg\field_ops.mbt
95.5% · 126 / 132 points · 359 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 场运算 fieldimg/field_ops - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 场通用算子:梯度、散度、旋度、拉普拉斯、积分、阈值、掩码、代数运算、等值提取。 - 7
// Field operators: gradient, divergence, curl, laplacian, integrate, clamp, mask, algebra, isosurface. - 8
// - 9
// 关键映射(GeoVerse 思想): - 10
// gradient = 边缘检测(Sobel/Canny 的数学本质) - 11
// laplacian = 模糊/锐化(LoG) - 12
// integrate = 直方图/总量积分 - 13
// field_clamp = 阈值分割 - 14
// isosurface = 等值线提取 → 分割 - 15
- 16
///| - 17
/// 梯度:中心差分计算标量场梯度,返回矢量场(dim=2,[gx, gy])。 - 18
/// 边界使用单侧差分。 - 19
/// 梯度:标量场中心差分,返回 VectorField Gradient: central difference of a scalar field, returns VectorField (dim=2, [gx, gy]). - 20
/// 边界使用单侧差分 Boundary uses one-sided difference. - 21
pub fn field_gradient( - 22
field : ScalarField, - 23
) -> VectorField raise @types.LoadError { - 24●
let w = field.width - 25
let h = field.height - 26
let n = w * h - 27●
let data = Array::make(n * 2, 0.0F) - 28
for y in 0..<h { - 29●
for x in 0..<w { - 30●
let idx = y * w + x - 31
let gx = if x == 0 { - 32●
field.data[idx + 1] - field.data[idx] - 33●
} else if x == w - 1 { - 34●
field.data[idx] - field.data[idx - 1] - 35
} else { - 36●
(field.data[idx + 1] - field.data[idx - 1]) * 0.5F - 37
} - 38
let gy = if y == 0 { - 39●
field.data[idx + w] - field.data[idx] - 40●
} else if y == h - 1 { - 41●
field.data[idx] - field.data[idx - w] - 42
} else { - 43●
(field.data[idx + w] - field.data[idx - w]) * 0.5F - 44
} - 45
data[idx * 2] = gx - 46
data[idx * 2 + 1] = gy - 47
} - 48
} - 49●
VectorField::make(w, h, 2, data) - 50
} - 51
- 52
///| - 53
/// 散度:计算 2D 矢量场(dim=2)的散度,返回标量场。 - 54
/// div = du/dx + dv/dy。边界使用单侧差分。 - 55
/// 二维向量场散度,返回 ScalarField Divergence of a 2D vector field (dim=2), returns ScalarField. - 56
/// 散度 div = du/dx + dv/dy. Boundary uses one-sided difference. - 57
pub fn field_divergence( - 58
field : VectorField, - 59
) -> ScalarField raise @types.LoadError { - 60●
if field.dim != 2 { - 61●
raise @types.LoadError::DecodeFailed( - 62
"field_divergence: 矢量场维度必须为2", - 63
) - 64
} - 65
let w = field.width - 66
let h = field.height - 67
let n = w * h - 68●
let data = Array::make(n, 0.0F) - 69
for y in 0..<h { - 70●
for x in 0..<w { - 71●
let idx = y * w + x - 72
let u_here = field.data[idx * 2] - 73
let v_here = field.data[idx * 2 + 1] - 74
let du_dx = if x == 0 { - 75●
field.data[(idx + 1) * 2] - u_here - 76●
} else if x == w - 1 { - 77●
u_here - field.data[(idx - 1) * 2] - 78
} else { - 79●
(field.data[(idx + 1) * 2] - field.data[(idx - 1) * 2]) * 0.5F - 80
} - 81
let dv_dy = if y == 0 { - 82●
field.data[(idx + w) * 2 + 1] - v_here - 83●
} else if y == h - 1 { - 84●
v_here - field.data[(idx - w) * 2 + 1] - 85
} else { - 86●
(field.data[(idx + w) * 2 + 1] - field.data[(idx - w) * 2 + 1]) * 0.5F - 87
} - 88
data[idx] = du_dx + dv_dy - 89
} - 90
} - 91●
ScalarField::make(w, h, data) - 92
} - 93
- 94
///| - 95
/// 旋度:计算 2D 矢量场(dim=2)的标量旋度,返回标量场。 - 96
/// curl = dv/dx - du/dy(2D 中的标量旋度)。边界使用单侧差分。 - 97
/// 二维向量场标量旋度,返回 ScalarField Scalar curl of a 2D vector field (dim=2), returns ScalarField. - 98
/// 旋度 curl = dv/dx - du/dy. Boundary uses one-sided difference. - 99
pub fn field_curl(field : VectorField) -> ScalarField raise @types.LoadError { - 100●
if field.dim != 2 { - 101○
raise @types.LoadError::DecodeFailed( - 102
"field_curl: 矢量场维度必须为2", - 103
) - 104
} - 105
let w = field.width - 106
let h = field.height - 107
let n = w * h - 108●
let data = Array::make(n, 0.0F) - 109
for y in 0..<h { - 110●
for x in 0..<w { - 111●
let idx = y * w + x - 112
let u_here = field.data[idx * 2] - 113
let v_here = field.data[idx * 2 + 1] - 114
let dv_dx = if x == 0 { - 115●
field.data[(idx + 1) * 2 + 1] - v_here - 116●
} else if x == w - 1 { - 117●
v_here - field.data[(idx - 1) * 2 + 1] - 118
} else { - 119●
(field.data[(idx + 1) * 2 + 1] - field.data[(idx - 1) * 2 + 1]) * 0.5F - 120
} - 121
let du_dy = if y == 0 { - 122●
field.data[(idx + w) * 2] - u_here - 123●
} else if y == h - 1 { - 124●
u_here - field.data[(idx - w) * 2] - 125
} else { - 126●
(field.data[(idx + w) * 2] - field.data[(idx - w) * 2]) * 0.5F - 127
} - 128
data[idx] = dv_dx - du_dy - 129
} - 130
} - 131●
ScalarField::make(w, h, data) - 132
} - 133
- 134
///| - 135
/// 拉普拉斯:计算标量场拉普拉斯,返回标量场。 - 136
/// 拉普拉斯 lap = f[x+1,y] + f[x-1,y] + f[x,y+1] + f[x,y-1] - 4*f[x,y]。 - 137
/// 边界使用钳制(复制边界值)。 - 138
/// 标量场拉普拉斯,返回 ScalarField Laplacian of a scalar field, returns ScalarField. - 139
/// 拉普拉斯 lap = f[x+1,y] + f[x-1,y] + f[x,y+1] + f[x,y-1] - 4*f[x,y]. - 140
/// 边界使用钳位(复制边缘)值 Boundary uses clamped (replicate edge) values. - 141
pub fn field_laplacian( - 142
field : ScalarField, - 143
) -> ScalarField raise @types.LoadError { - 144●
let w = field.width - 145
let h = field.height - 146
let n = w * h - 147●
let data = Array::make(n, 0.0F) - 148
for y in 0..<h { - 149●
for x in 0..<w { - 150●
let idx = y * w + x - 151
let center = field.data[idx] - 152●
let left = field.evaluate_clamped(x - 1, y) - 153●
let right = field.evaluate_clamped(x + 1, y) - 154●
let up = field.evaluate_clamped(x, y - 1) - 155●
let down = field.evaluate_clamped(x, y + 1) - 156
data[idx] = left + right + up + down - 4.0F * center - 157
} - 158
} - 159●
ScalarField::make(w, h, data) - 160
} - 161
- 162
///| - 163
/// 积分:标量场全场求和(离散积分,相当于总量/面积积分)。 - 164
/// 积分:求和所有值(离散积分 = 总和 / 面积积分) Integrate: sum all values (discrete integral = total / area integral). - 165
pub fn field_integrate(field : ScalarField) -> Float { - 166●
let mut sum = 0.0F - 167●
for i in 0..<field.data.length() { - 168●
sum = sum + field.data[i] - 169
} - 170
sum - 171
} - 172
- 173
///| - 174
/// 区域积分:对标量场在 [x0,x1)×[y0,y1) 区域求和。 - 175
/// 区域积分 Regional integrate: sum values in the region [x0,x1)×[y0,y1). - 176
pub fn field_integrate_region( - 177
field : ScalarField, - 178
x0 : Int32, - 179
y0 : Int32, - 180
x1 : Int32, - 181
y1 : Int32, - 182
) -> Float { - 183●
let mut sum = 0.0F - 184●
let x_lo = x0.clamp(min=0, max=field.width) - 185●
let x_hi = x1.clamp(min=0, max=field.width) - 186●
let y_lo = y0.clamp(min=0, max=field.height) - 187●
let y_hi = y1.clamp(min=0, max=field.height) - 188
for y in y_lo..<y_hi { - 189●
for x in x_lo..<x_hi { - 190●
sum = sum + field.data[y * field.width + x] - 191
} - 192
} - 193
sum - 194
} - 195
- 196
///| - 197
- 198
///| - 199
/// 阈值裁剪:将场值限制在 [lo, hi] 范围内。 - 200
/// 将场值钳位到 [lo, hi] Clamp field values to [lo, hi]. - 201
pub fn field_clamp( - 202
field : ScalarField, - 203
lo : Float, - 204
hi : Float, - 205
) -> ScalarField raise @types.LoadError { - 206●
let data = Array::make(field.data.length(), 0.0F) - 207●
for i in 0..<field.data.length() { - 208●
let v = field.data[i] - 209●
data[i] = if v < lo { lo } else if v > hi { hi } else { v } - 210
} - 211●
ScalarField::make(field.width, field.height, data) - 212
} - 213
- 214
///| - 215
/// 掩码:按阈值生成二值掩码场(value >= threshold → 1.0,否则 0.0)。 - 216
/// 掩膜:二值掩膜场 Mask: binary mask field (value >= threshold → 1.0, else 0.0). - 217
pub fn field_mask( - 218
field : ScalarField, - 219
threshold : Float, - 220
) -> ScalarField raise @types.LoadError { - 221●
let data = Array::make(field.data.length(), 0.0F) - 222●
for i in 0..<field.data.length() { - 223●
data[i] = if field.data[i] >= threshold { 1.0F } else { 0.0F } - 224
} - 225●
ScalarField::make(field.width, field.height, data) - 226
} - 227
- 228
///| - 229
/// 场加法:a + b(逐元素)。两场尺寸必须一致。 - 230
/// 场加法:a + b(逐元素)。两场须同尺寸 Field addition: a + b (element-wise). Both fields must have the same size. - 231
pub fn field_add( - 232
a : ScalarField, - 233
b : ScalarField, - 234
) -> ScalarField raise @types.LoadError { - 235●
if a.width != b.width || a.height != b.height { - 236●
raise @types.LoadError::DecodeFailed("field_add: 两场尺寸必须一致") - 237
} - 238●
let data = Array::make(a.data.length(), 0.0F) - 239●
for i in 0..<a.data.length() { - 240●
data[i] = a.data[i] + b.data[i] - 241
} - 242●
ScalarField::make(a.width, a.height, data) - 243
} - 244
- 245
///| - 246
/// 场减法:a - b(逐元素)。两场尺寸必须一致。 - 247
/// 场减法:a - b(逐元素)。两场须同尺寸 Field subtraction: a - b (element-wise). Both fields must have the same size. - 248
pub fn field_sub( - 249
a : ScalarField, - 250
b : ScalarField, - 251
) -> ScalarField raise @types.LoadError { - 252●
if a.width != b.width || a.height != b.height { - 253○
raise @types.LoadError::DecodeFailed("field_sub: 两场尺寸必须一致") - 254
} - 255●
let data = Array::make(a.data.length(), 0.0F) - 256●
for i in 0..<a.data.length() { - 257●
data[i] = a.data[i] - b.data[i] - 258
} - 259●
ScalarField::make(a.width, a.height, data) - 260
} - 261
- 262
///| - 263
/// 场标量乘法:field * scalar(逐元素)。 - 264
/// 场标量乘法:field * scalar(逐元素) Field scalar multiplication: field * scalar (element-wise). - 265
pub fn field_mul_scalar( - 266
field : ScalarField, - 267
scalar : Float, - 268
) -> ScalarField raise @types.LoadError { - 269●
let data = Array::make(field.data.length(), 0.0F) - 270●
for i in 0..<field.data.length() { - 271●
data[i] = field.data[i] * scalar - 272
} - 273●
ScalarField::make(field.width, field.height, data) - 274
} - 275
- 276
///| - 277
/// 场逐元素乘法:a * b(逐元素)。两场尺寸必须一致。 - 278
/// 场逐元素乘法:a * b。两场须同尺寸 Field element-wise multiplication: a * b. Both fields must have the same size. - 279
pub fn field_mul( - 280
a : ScalarField, - 281
b : ScalarField, - 282
) -> ScalarField raise @types.LoadError { - 283●
if a.width != b.width || a.height != b.height { - 284○
raise @types.LoadError::DecodeFailed("field_mul: 两场尺寸必须一致") - 285
} - 286●
let data = Array::make(a.data.length(), 0.0F) - 287●
for i in 0..<a.data.length() { - 288●
data[i] = a.data[i] * b.data[i] - 289
} - 290●
ScalarField::make(a.width, a.height, data) - 291
} - 292
- 293
///| - 294
/// 场混合:(1 - alpha) * a + alpha * b。两场尺寸必须一致,alpha 通常在 [0, 1]。 - 295
/// 场混合:(1 - alpha) * a + alpha * b。须同尺寸 Field blend: (1 - alpha) * a + alpha * b. Same size required; alpha typically in [0, 1]. - 296
pub fn field_blend( - 297
a : ScalarField, - 298
b : ScalarField, - 299
alpha : Float, - 300
) -> ScalarField raise @types.LoadError { - 301●
if a.width != b.width || a.height != b.height { - 302○
raise @types.LoadError::DecodeFailed( - 303
"field_blend: 两场尺寸必须一致", - 304
) - 305
} - 306●
let data = Array::make(a.data.length(), 0.0F) - 307
let one_minus = 1.0F - alpha - 308●
for i in 0..<a.data.length() { - 309●
data[i] = one_minus * a.data[i] + alpha * b.data[i] - 310
} - 311●
ScalarField::make(a.width, a.height, data) - 312
} - 313
- 314
///| - 315
/// 等值面提取:标记 |value - level| < epsilon 的点为 1.0,否则 0.0。 - 316
/// 在 2D 中即等值线提取,可用于分割。 - 317
/// 等值面 Isosurface: mark points where |value - level| < epsilon as 1.0, else 0.0. - 318
/// 2D 下为 In 2D this is - 319
pub fn field_isosurface( - 320
field : ScalarField, - 321
level : Float, - 322
epsilon : Float, - 323
) -> ScalarField raise @types.LoadError { - 324●
let data = Array::make(field.data.length(), 0.0F) - 325●
for i in 0..<field.data.length() { - 326●
let diff = field.data[i] - level - 327●
let abs_diff = if diff < 0.0F { -diff } else { diff } - 328●
data[i] = if abs_diff < epsilon { 1.0F } else { 0.0F } - 329
} - 330●
ScalarField::make(field.width, field.height, data) - 331
} - 332
- 333
///| - 334
/// 直方图:统计标量场值落在 [min_val, max_val) 分成 bins 个桶的计数。 - 335
/// 直方图:统计落入 bins 桶的值 Histogram: count values falling into bins buckets over [min_val, max_val). - 336
pub fn field_histogram( - 337
field : ScalarField, - 338
min_val : Float, - 339
max_val : Float, - 340
bins : Int32, - 341
) -> Array[Int32] raise @types.LoadError { - 342●
if bins <= 0 { - 343○
raise @types.LoadError::DecodeFailed("field_histogram: bins 必须大于0") - 344
} - 345●
let hist = Array::make(bins, 0) - 346
let range = max_val - min_val - 347
if range <= 0.0F { - 348○
return hist - 349
} - 350●
for i in 0..<field.data.length() { - 351●
let v = field.data[i] - 352
if v >= min_val && v < max_val { - 353●
let idx = ((v - min_val) / range * Float::from_int(bins)).to_int() - 354●
let clamped_idx = idx.clamp(min=0, max=bins - 1) - 355
hist[clamped_idx] = hist[clamped_idx] + 1 - 356
} - 357
} - 358
hist - 359
}