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