ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Rerun `TransformMat3x3` 组件详解:3×3 仿射变换矩阵的列主序编码与多语言实战

Rerun `TransformMat3x3` 组件详解:3×3 仿射变换矩阵的列主序编码与多语言实战 RerunTransformMat3x3组件详解3×3 仿射变换矩阵的列主序编码与多语言实战【免费下载链接】rerunVisualize, query, and stream to train on multimodal robotics data.项目地址: https://gitcode.com/GitHub_Trending/re/rerun本篇技术指南聚焦 Rerun 数据类型系统中的TransformMat3x3组件——一个用于表示 3D 空间中任意仿射变换旋转、缩放、剪切、反射的 3×3 矩阵。你将掌握它在 Rerun 中的列主序column-major扁平存储约定、底层 Arrow 序列化格式以及 Python / Rust 中构造矩阵的正确姿势行序输入与列序输入的差异极易踩坑并了解它如何作为Transform3D与InstancePoses3D两个 archetype 的可选组件参与实体层级变换的构建。TransformMat3x3 是什么3×3 变换矩阵的能力边界TransformMat3x3是 Rerun 中一类用于描述 3D 空间仿射变换的组件其官方定义为A 3x3 transformation matrix Matrix. 3x3 matrixes are able to represent any affine transformation in 3D space, i.e. rotation, scaling, shearing, reflection etc.也就是说一个 3×3 矩阵足以表达 3D 空间中旋转rotation、缩放scaling、剪切shearing、反射reflection等线性部分的任意组合。注意它并不包含平移分量——在 Rerun 的变换体系中平移由Transform3Darchetype 中的translation组件单独承载当矩阵与平移同时出现时矩阵线性部分先作用于坐标随后再施加平移详见下文组件组合顺序小节。该组件的类型定义位于 crates/build/re_type_definitions/rerun/components/transform_mat3x3.def.rs标注为#[rerun(state stable)]即属于稳定的公开数据类型Rust、Python、C 三套 SDK 的绑定均由re_types_builder基于这份定义自动生成生成器入口见crates/build/re_types_builder/src/codegen/。核心约定列主序Column-Major扁平存储Rerun 中所有矩阵都以列主序的扁平系数列表存储这一点是理解TransformMat3x3内存布局与序列化格式的基石。官方文档给出的映射关系如下column 0 column 1 column 2 ------------------------------------------------- row 0 | flat_columns[0] flat_columns[3] flat_columns[6] row 1 | flat_columns[1] flat_columns[4] flat_columns[7] row 2 | flat_columns[2] flat_columns[5] flat_columns[8]即对于一个 9 元素列表flat_columns索引0..2依次是第一列的三个行分量3..5是第二列6..8是第三列。用公式表达矩阵第 i 行第 j 列的元素0-based位于flat_columns[j * 3 i]。这一约定与底层 Rust 实现完全一致crates/store/re_sdk_types/src/encodings/mat3x3.rs中Mat3x3是一个#[repr(transparent)]的[f32; 9]元组结构体其字段注释明确写着 Flat list of matrix coefficients in column-major order并且From[f32; 9]与FromMat3x3 for [f32; 9]直接透传这份扁平数组。Rerun 编码与 Arrow 数据类型TransformMat3x3不自己定义内存表示而是委托给编码类型Mat3x3Mat3x3 编码文档。其 Arrow 数据类型为FixedSizeList(9 x non-null Float32)含义解读FixedSizeList定长列表每个元素固定 9 个分量9正好对应 3×3 矩阵的 9 个系数non-null Float32内部每个元素为非空单精度浮点数32 位没有有效性位图validity mask序列化开销极小。在 Rust 侧crates/store/re_sdk_types/src/encodings/mat3x3.rs的arrow_data_type()与to_arrow/from_arrow实现确认了这一格式序列化时将[f32; 9]扁平展开进PrimitiveArrayFloat32Type再包装为FixedSizeListArray::new(Field::new(item, Float32, false), 9, ...)反序列化时通过bytemuck::try_cast_slice::_, [f32; 9]零拷贝地把连续 Float32 缓冲区切回[f32; 9]切片并校验value_length() 9不匹配即返回datatype_mismatch错误。Python 侧同理rerun_py/rerun_sdk/rerun/encodings/mat3x3_ext.py的native_to_pa_array_override先将输入统一压平成 float32 连续数组再调用pa.FixedSizeListArray.from_arrays(float_arrays, typedata_type)生成 Arrow 数组。源码视角从类型定义到多语言绑定TransformMat3x3的 Rust 实现位于 crates/store/re_sdk_types/src/components/transform_mat3x3.rs#[repr(transparent)] pub struct TransformMat3x3(pub crate::encodings::Mat3x3);关键设计点#[repr(transparent)]Deref/DerefMut/Borrow全部转发到内部的Mat3x3因此它在内存布局上与 9 个连续f32完全等价并可借助bytemuck::Pod/Zeroable做零拷贝转换WrapperComponenttrait 指定其组件类型名为rerun.components.TransformMat3x3底层编码为Mat3x3派生Copy、PartialEq、SizeBytes等便于批量数据的高效复制与统计。配套的手写扩展非生成代码位于 crates/store/re_sdk_types/src/components/transform_mat3x3_ext.rs在启用glamfeature 时提供impl FromTransformMat3x3 for glam::Affine3A { fn from(v: TransformMat3x3) - Self { Self { matrix3: glam::Mat3A::from_cols_slice(v.0.0), translation: glam::Vec3A::ZERO, } } }即可以直接把TransformMat3x3转换为图形数学库 glam 的Affine3A平移分量为零方便与现有渲染/物理管线互操作。源码注释特别说明这个语义转换有意不实现在裸Mat3x3上——变换语义由TransformMat3x3表达而Mat3x3只负责提供到glam::Mat3A的纯线性转换职责划分清晰。Python 构造细节rows 与 columns 的关键差异Python 中TransformMat3x3直接继承自encodings.Mat3x3rerun_py/rerun_sdk/rerun/components/transform_mat3x3.py构造逻辑集中在 rerun_py/rerun_sdk/rerun/encodings/mat3x3_ext.py 的Mat3x3Ext.__init__。理解这一点能帮你避开最常见的坑1. 按行输入默认遵循 NumPy 约定位置参数按行构造遵循 NumPynp.array的行优先row-major直觉import numpy as np import rerun as rr # 单层 9 元素列表按行填充[[1,2,3],[4,5,6],[7,8,9]] rr.components.TransformMat3x3([1, 2, 3, 4, 5, 6, 7, 8, 9]).flat_columns # np.array([1, 4, 7, 2, 5, 8, 3, 6, 9], dtypenp.float32) # 嵌套 3x3 数组按行填充 rr.components.TransformMat3x3([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).flat_columns # np.array([1, 4, 7, 2, 5, 8, 3, 6, 9], dtypenp.float32)这两条断言直接来自类型定义文档与 Python 生成的 docstring底层实现先np.asarray(rows, dtypenp.float32).reshape(3, 3)再arr.ravel(F)Fortran 序 列主序得到内部存储。2. 按列输入关键字参数columns如果手头的数据天然是按列组织的用命名参数columns# 直接按列给出 9 个系数存储原样保留 rr.components.TransformMat3x3(columns[1, 2, 3, 4, 5, 6, 7, 8, 9]).flat_columns # np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtypenp.float32) rr.components.TransformMat3x3(columns[[1, 2, 3], [4, 5, 6], [7, 8, 9]]).flat_columns # np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtypenp.float32)columns分支走arr.ravel(C)C 序 行主序的扁平化恰好等价于列主序存储因此输入顺序即存储顺序。3. 边界与约束rows与columns不能同时指定否则会触发警告Cant specify both columns and rows of matrix.两者都未指定时同样发出警告Need to specify either columns or columns of matrix.并回退为单位矩阵np.identity(3, dtypenp.float32).ravel()避免程序崩溃构造结果统一通过flat_columns属性暴露且Mat3x3Batch的_COMPONENT_TYPE为rerun.components.TransformMat3x3保证批量化日志log/send_columns时组件类型一致。在 archetype 中的角色Transform3D 与 InstancePoses3DTransformMat3x3被两个 archetype 使用Archetype说明Transform3D描述两个 3D 空间之间的变换pose沿实体树层级传播InstancePoses3D仅作用于单个实体、不沿实体树传播的逐实例位姿详见 Transform3D 类型文档 与 InstancePoses3D 类型文档。组件组合顺序在Transform3D中crates/store/re_sdk_types/src/archetypes/transform3d.rsmat3x3与translation、rotation_axis_angle、quaternion、scale、relation、child_frame、parent_frame一起构成8 个全部可选的组件。archetype 文档明确说明From the point of view of the entitys coordinate system, all components are applied in the inverse order they are listed here. E.g. if both a translation and a mat3x3 transform are present, the 3x3 matrix is applied first, followed by the translation.即从实体坐标系视角看各组件按列出顺序的逆序作用矩阵的线性变换先发生平移后施加——这与经典的先旋转缩放、后平移的仿射复合直觉一致。同时需要注意 archetype 的整组替换语义一旦日志了新的Transform3D整个变换关系会被重置为新值而不是按组件做 latest-at 合并这与通常 archetype 的行为不同。Rust 便捷构造手写扩展crates/store/re_sdk_types/src/archetypes/transform3d_ext.rs围绕矩阵提供了几个实用构造器// 仅矩阵 Transform3D::from_mat3x3(mat3x3) // 矩阵 平移矩阵先作用平移后作用 Transform3D::from_translation_mat3x3(translation, mat3x3) // 在既有实例上设置/批量设置矩阵 transform.with_mat3x3(mat3x3) transform.with_many_mat3x3(matrices) // 配合 send_columns 使用with_mat3x3接受任意impl IntoTransformMat3x3的值得益于transform_mat3x3.rs中的泛型FromT实现with_many_mat3x3则用于把多条矩阵打包进单一组件批次与Transform3D::columns/columns_of_unit_batches配合即可走列式send_columns通道实现时间序列上的批量变换日志。典型应用场景机器人学 / 多模态数据处理将各关节、传感器外参手眼标定矩阵、相机内参相关的线性部分以 3×3 矩阵形式随Transform3D记录在 Rerun 中直观校验坐标系关系非刚性/仿射形变可视化剪切、非均匀缩放等无法仅用旋转轴角/四元数均匀缩放表达的仿射变形直接用 3×3 矩阵承载与下游数学库对接Rust 侧借助glam::Affine3A转换无缝接入既有计算管线Python 侧按 NumPy 惯例构造后即可用于flat_columns直出或其他数值计算。进一步阅读TransformMat3x3 官方类型文档本文所依据的原始文档Mat3x3 编码文档共享存储格式与其他使用该编码的组件如PinholeProjectionTransform3D archetype 文档 与 InstancePoses3D archetype 文档组件的实际消费方源码类型定义 transform_mat3x3.def.rs、Rust 实现 transform_mat3x3.rs 及扩展 transform_mat3x3_ext.rs、Python 实现 transform_mat3x3.py 与构造逻辑 mat3x3_ext.py【免费下载链接】rerunVisualize, query, and stream to train on multimodal robotics data.项目地址: https://gitcode.com/GitHub_Trending/re/rerun创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表