
Rerun Vector2D 组件深度解析2D 向量数据模型、Arrow 编码与 Arrows2D 可视化【免费下载链接】rerunVisualize, query, and stream to train on multimodal robotics data.项目地址: https://gitcode.com/GitHub_Trending/re/rerunVector2D是 Rerun 数据模型中用于表示二维空间向量的核心组件Component也是Arrows2D图元Archetype唯一必需的组成部分。本文以仓库中 组件参考文档 为主体结合 Rust / Python 两套 SDK 的源码实现完整讲解Vector2D的语义定义、底层Vec2D编码、Arrow 内存布局、跨语言 API 用法及其在Arrows2D可视化链路中的角色帮助你在日志机器人运动学、光流、速度场等二维矢量数据时正确选型与使用。Vector2D 是什么一个稳定的二维向量组件在 Rerun 的类型体系中组件Component是附着在实体Entity上的最小语义单元而Vector2D的定位非常单纯——一个处于 2D 空间中的向量A vector in 2D space.。它与同样表示二维数据的Position2D位置点的关键区别在于语义位置描述在哪向量描述朝哪个方向、多长。从仓库中的类型定义源文件可以看到该组件的官方契约// crates/build/re_type_definitions/rerun/components/vector2d.def.rs /// A vector in 2D space. #[rerun::rerun_type] #[rerun(state stable)] #[rust(derive(Default, Copy, PartialEq, bytemuck::Pod, bytemuck::Zeroable))] #[rust(repr transparent)] pub struct Vector2D { pub vector: rerun::encodings::Vec2D, }这段代码本身不是可执行的 Rust 代码而是 Rerun 的类型定义 DSL位于crates/build/re_type_definitions/下由re_types_builder解析后统一生成 Rust、Python 与 C 三套 SDK 的绑定代码。其中值得注意的两个标记state stable表明该组件处于稳定状态其名称与内存布局受版本兼容性保障可以放心用于长期存储的数据repr transparent生成的结构体是底层编码类型Vec2D的透明包装transparent wrapper零额外内存开销。Rerun 编码Vec2D 与透明包装结构文档中明确列出Vector2D的 Rerun 编码encoding为Vec2D。编码Encoding是比组件更低一层的原始数据类型描述它直接对应 Arrow 的内存表示并且被多个组件复用例如Position2D同样基于二维浮点编码。Rust 端生成的组件结构体如下// crates/store/re_sdk_types/src/components/vector2d.rs #[repr(transparent)] pub struct Vector2D(pub crate::encodings::Vec2D); impl ::re_types_core::WrapperComponent for Vector2D { type Encoding crate::encodings::Vec2D; #[inline] fn name() - ComponentType { rerun.components.Vector2D.into() } }而编码Vec2D的实现crates/store/re_sdk_types/src/encodings/vec2d.rs本质就是一个[f32; 2]的强类型包装#[repr(C)] pub struct Vec2D(pub [f32; 2usize]);同时提供了便捷的类型互转impl From[f32; 2usize] for Vec2D { fn from(xy: [f32; 2usize]) - Self { Self(xy) } } impl FromVec2D for [f32; 2usize] { fn from(value: Vec2D) - Self { value.0 } }Python 端的结构完全对应Vector2D类直接继承encodings.Vec2D并混入ComponentMixin本身不新增字段见 rerun_py/rerun_sdk/rerun/components/vector2d.pyclass Vector2D(encodings.Vec2D, ComponentMixin): **Component**: A vector in 2D space. class Vector2DBatch(encodings.Vec2DBatch, ComponentBatchMixin): _COMPONENT_TYPE: str rerun.components.Vector2D实用的扩展实现在 crates/store/re_sdk_types/src/components/vector2d_ext.rs 中Rust 端还提供了两个常用常量与外部数学库的互操作impl Vector2D { /// The zero vector, i.e. the additive identity. pub const ZERO: Self Self(crate::encodings::Vec2D::ZERO); /// [1, 1], i.e. the multiplicative identity. pub const ONE: Self Self(crate::encodings::Vec2D::ONE); } #[cfg(feature glam)] impl FromVector2D for glam::Vec2 { /* ... */ } #[cfg(feature mint)] impl FromVector2D for mint::Vector2f32 { /* ... */ }这意味着当你的应用已经基于glamVec2或mintVector2f32做向量运算时可以直接通过From转换轻松接入 Rerun 的日志管线无需手写逐元素拷贝。Arrow 数据类型FixedSizeList(2 × Float32)Vector2D在 Arrow 层面的数据布局是文档中给出的核心事实FixedSizeList(2 x non-null Float32)即外层是长度为 2 的定长列表FixedSizeList元素不可为 null内层是两个非空Float32。这一布局在三个层面都有源码印证Rust 端crates/store/re_sdk_types/src/encodings/vec2d.rsimpl ::re_types_core::ArrowDataType for Vec2D { fn arrow_data_type() - arrow::datatypes::DataType { DataType::FixedSizeList( std::sync::Arc::new(Field::new(item, DataType::Float32, false)), 2, ) } }序列化ToArrow时先展平为[f32; 2]的连续数组再构造FixedSizeListArray反序列化FromArrow时校验value_length() 2并通过bytemuck::try_cast_slice把底层Float32Array的连续缓冲区直接重解释为[[f32; 2]]切片——这正是选择定长列表而非可变长度列表的原因定长 无 null 使得每个向量恰好 8 字节可以按元素零拷贝访问。Python 端rerun_py/rerun_sdk/rerun/encodings/vec2d.pyclass Vec2DBatch(BaseBatch[Vec2DArrayLike]): _ARROW_DATATYPE pa.list_(pa.field(item, pa.float32(), nullableFalse, metadata{}), 2)而输入数据在 vec2d_ext.py 中经由flat_np_float32_array_from_array_like(data, 2)见 rerun_py/rerun_sdk/rerun/_validators.py统一转成维度为 2 的扁平 float32 numpy 数组再包成pa.FixedSizeListArraystaticmethod def native_to_pa_array_override(data: Vec2DArrayLike, data_type: pa.DataType) - pa.Array: points flat_np_float32_array_from_array_like(data, 2) return pa.FixedSizeListArray.from_arrays(points, typedata_type)因此 Python 侧你可以传入[[1.0, 2.0], [3.0, 4.0]]、np.array(...)等任意可转为(N, 2)float32 数组的对象SDK 会统一校验并转换。使用场景作为 Arrows2D 的必选向量Vector2D组件当前唯一的直接消费者是Arrows2D图元——用于绘制一批带可选颜色、半径、标签的二维箭头。从类型定义看crates/build/re_type_definitions/rerun/archetypes/arrows2d.def.rsvectors是Arrows2D唯一的**必需required**字段pub struct Arrows2D { /// All the vectors for each arrow in the batch. #[rerun(required)] pub vectors: Vecrerun::components::Vector2D, /// All the origin (base) positions for each arrow in the batch. /// If no origins are set, (0, 0) is used as the origin for each arrow. #[rerun(recommended)] pub origins: OptionVecrerun::components::Position2D, /// Optional radii for the arrows. #[rerun(optional)] pub radii: OptionVecrerun::components::Radius, // ... colors / labels / show_labels / draw_order / class_ids ... }理解必需的含义很重要Arrows2D图元本身还带有origins、radii、colors、labels、draw_order、class_ids等字段但它们全部可选——去掉任何一个剩下的箭头依然能渲染唯独删掉vectors后Arrows2D就不再有意义。因此Vector2D是二维箭头可视化的数据基石其余字段只是修饰。可视化侧的印证在渲染端Arrows2DVisualizercrates/views/re_view_spatial/src/visualizers/arrows2d.rs通过查询信息明确把Vector2D声明为单必需组件impl VisualizerSystem for Arrows2DVisualizer { fn visualizer_query_info(self, _app_options: re_viewer_context::AppOptions) - VisualizerQueryInfo { VisualizerQueryInfo::single_required_component::Vector2D( Arrows2D::descriptor_vectors(), Arrows2D::all_components(), ) // ... } }即只要某个实体上存在rerun.components.Vector2D组件批次空间视图Spatial 2D / 3D3D 时需在投影下就会自动启用Arrows2DVisualizer来渲染这批向量。从源码结构看vectors的语义与文档定义All the vectors for each arrow in the batch完全一致——每个Vector2D对应批量中的一根箭头。各语言实战用法Pythonimport rerun as rr rr.init(rerun_example_arrow2d) rr.spawn() rr.log( arrows, rr.Arrows2D( vectors[[1.0, 0.0], [0.0, -1.0], [-0.7, 0.7]], origins[[0.25, 0.0], [0.25, 0.0], [-0.1, -0.1]], radii0.025, colors[[255, 0, 0], [0, 255, 0], [127, 0, 255]], labels[right, up, left-down], ), )Rustuse rerun::{Arrows2D, RecordingStreamBuilder}; fn main() - Result(), Boxdyn std::error::Error { let rec rerun::RecordingStreamBuilder::new(rerun_example_arrow2d).spawn()?; rec.log( arrows, Arrows2D::from_vectors([[1.0, 0.0], [0.0, -1.0], [-0.7, 0.7]]) .with_radii([0.025]) .with_origins([[0.25, 0.0], [0.25, 0.0], [-0.1, -0.1]]) .with_colors([[255, 0, 0], [0, 255, 0], [127, 0, 255]]) .with_labels([right, up, left-down]), )?; Ok(()) }以上 Rust 示例直接取自 crates/store/re_sdk_types/src/archetypes/arrows2d.rs 中生成的文档注释。可以看出from_vectors(...)接受数组字面量内部逐个IntoVector2D转换Vector2D实现了FromT: IntoVec2D而Vec2D又实现了From[f32; 2]未指定origins时默认以(0, 0)为起点radii的渲染语义为箭杆按radius 0.5 * radius绘制成线箭头按height 2.0 * radius、radius 1.0 * radius绘制labels只有一个时会显示在实体中心多个时每个实例各显示一个。类型生成链路与兼容性保证Vector2D的代码并非手写而是由构建期工具链驱动类型契约定义在 crates/build/re_type_definitions/rerun/components/vector2d.def.rsDSL 源crates/build/re_types_builder/src/codegen/rust/api.rs依据 DSL 生成 Rust 绑定即 crates/store/re_sdk_types/src/components/vector2d.rs文件头注明 DO NOT EDIT! This file was auto-generated生成代码在re_sdk_types的 components/mod.rs 与 reflection/mod.rs 中统一注册组件名rerun.components.Vector2D供日志与查询反射系统使用。state stable意味着该组件名与 Arrow 布局属于稳定接口。对于需要长期落盘.rrd/ Parquet或跨语言交换的 2D 向量数据可以放心依赖这一布局FixedSizeList(2 x non-null Float32)既能被 Arrow 生态广泛支持又因为定长无 null 而保持紧凑每向量 8 字节。小结Vector2D是 Rerun 中表示 2D 向量的稳定组件编码为Vec2DArrow 布局为FixedSizeList(2 × non-null Float32)它在 Rust 端是#[repr(transparent)]的零开销包装在 Python 端是encodings.Vec2D的直接子类语义与内存表示完全统一目前唯一使用方是Arrows2D图元的必选字段vectors用于批量绘制二维箭头可配合origins、radii、colors、labels等可选字段相关定义与实现可在仓库中直接追溯组件参考文档、编码文档、类型 DSL 源、Rust 生成代码、Python 组件绑定、渲染可视化器。【免费下载链接】rerunVisualize, query, and stream to train on multimodal robotics data.项目地址: https://gitcode.com/GitHub_Trending/re/rerun创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考