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