impl<T: Copy, Src, Dst> Transform2D<T, Src, Dst> {/// Returns an array containing this transform's terms.////// The terms are laid out in the same order as they are/// specified in [`Transform2D::new`], that is following the/// column-major-column-vector matrix notation.////// For example the translation terms are found in the/// last two slots of the array.#[inline]#[rustfmt::skip]pub fn to_array(&self) -> [T; 6] {[self.m11, self.m12,self.m21, self.m22,self.m31, self.m32]}/// Returns an array containing this transform's terms transposed.////// The terms are laid out in transposed order from the same order of/// `Transform3D::new` and `Transform3D::to_array`, that is following/// the row-major-column-vector matrix notation.////// For example the translation terms are found at indices 2 and 5/// in the array.#[inline]#[rustfmt::skip]pub fn to_array_transposed(&self) -> [T; 6] {[self.m11, self.m21, self.m31,self.m12, self.m22, self.m32]}/// Equivalent to `to_array` with elements packed two at a time/// in an array of arrays.#[inline]pub fn to_arrays(&self) -> [[T; 2]; 3] {[[self.m11, self.m12],[self.m21, self.m22],[self.m31, self.m32],]}/// Create a transform providing its components via an array/// of 6 elements instead of as individual parameters.////// The order of the components corresponds to the/// column-major-column-vector matrix notation (the same order/// as `Transform2D::new`).#[inline]#[rustfmt::skip]pub fn from_array(array: [T; 6]) -> Self {Self::new(array[0], array[1],array[2], array[3],array[4], array[5],)}/// Equivalent to `from_array` with elements packed two at a time/// in an array of arrays.////// The order of the components corresponds to the/// column-major-column-vector matrix notation (the same order/// as `Transform3D::new`).#[inline]#[rustfmt::skip]pub fn from_arrays(array: [[T; 2]; 3]) -> Self {Self::new(array[0][0], array[0][1],array[1][0], array[1][1],array[2][0], array[2][1],)}/// Drop the units, preserving only the numeric value.#[inline]#[rustfmt::skip]pub fn to_untyped(&self) -> Transform2D<T, UnknownUnit, UnknownUnit> {Transform2D::new(self.m11, self.m12,self.m21, self.m22,self.m31, self.m32)}/// Tag a unitless value with units.#[inline]#[rustfmt::skip]pub fn from_untyped(p: &Transform2D<T, UnknownUnit, UnknownUnit>) -> Self {Transform2D::new(p.m11, p.m12,p.m21, p.m22,p.m31, p.m32)}/// Returns the same transform with a different source unit.#[inline]#[rustfmt::skip]pub fn with_source<NewSrc>(&self) -> Transform2D<T, NewSrc, Dst> {Transform2D::new(self.m11, self.m12,self.m21, self.m22,self.m31, self.m32,)}/// Returns the same transform with a different destination unit.#[inline]#[rustfmt::skip]pub fn with_destination<NewDst>(&self) -> Transform2D<T, Src, NewDst> {Transform2D::new(self.m11, self.m12,self.m21, self.m22,self.m31, self.m32,)}/// Create a 3D transform from the current transformpub fn to_3d(&self) -> Transform3D<T, Src, Dst>whereT: Zero + One,{Transform3D::new_2d(self.m11, self.m12, self.m21, self.m22, self.m31, self.m32)}
}
这段代码是 Rust 语言中为 Transform2D 类型实现的一系列方法,主要用于处理二维变换矩阵。下面我会逐步解释这些方法的作用:
结构体和方法概述
Transform2D<T, Src, Dst> 是一个表示二维变换的泛型结构体,其中:
-
T 是矩阵元素的类型,需要实现 Copy trait
-
Src 和 Dst 是表示源和目标坐标系的类型参数
主要方法解释
- to_array
-
将变换矩阵的元素按列主序(column-major)排列成一个包含6个元素的数组
-
顺序是:m11, m12, m21, m22, m31, m32
-
最后两个元素(m31, m32)是平移分量
- to_array_transposed
-
将变换矩阵的元素按转置后的顺序排列成数组
-
顺序是:m11, m21, m31, m12, m22, m32
-
平移分量在索引2和5的位置
- to_arrays
-
将矩阵元素打包成3个数组,每个数组包含2个元素
-
格式:[[m11, m12], [m21, m22], [m31, m32]]
- from_array
-
从包含6个元素的数组创建变换矩阵
-
顺序与to_array相同
- from_arrays
-
从包含3个数组的数组创建变换矩阵
-
每个子数组包含2个元素
-
顺序与to_arrays相同
- to_untyped
-
移除矩阵的单位类型信息,转换为UnknownUnit类型
-
保留数值不变
- from_untyped
-
给无单位的矩阵添加单位类型信息
-
是to_untyped的逆操作
- with_source 和 with_destination
-
分别改变变换矩阵的源坐标系和目标坐标系类型
-
矩阵元素保持不变
- to_3d
-
将2D变换扩展为3D变换
-
需要元素类型T实现Zero和One trait
-
新的3D变换会保持原有的2D变换效果,z轴方向不变
矩阵布局说明
-
代码中提到的"column-major-column-vector"表示矩阵按列主序存储,且向量是列向量
-
这意味着变换矩阵的乘法是矩阵右乘向量
这些方法提供了在不同表示形式之间转换变换矩阵的便捷方式,同时也支持单位系统的转换和维度扩展。