ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

Rerun Spatial2DView 蓝图视图详解:用 Blueprint 定制二维空间数据可视化

Rerun Spatial2DView 蓝图视图详解:用 Blueprint 定制二维空间数据可视化 Rerun Spatial2DView 蓝图视图详解用 Blueprint 定制二维空间数据可视化【免费下载链接】rerunVisualize, query, and stream to train on multimodal robotics data.项目地址: https://gitcode.com/GitHub_Trending/re/rerunSpatial2DView是 Rerun 蓝图Blueprint体系中用于展示二维空间数据的视图类型负责渲染Points2D、图像、深度图、2D 线段与包围盒等全部二维数据。本文以 Rerun 官方类型定义文档为主线结合仓库内的类型定义源码与 Python SDK 实现完整讲解其四大属性background、visual_bounds、spatial_information、time_ranges、可可视化原型清单并给出可直接运行的完整示例代码。⚠️ 需要特别注意根据官方文档与源码注释Spatial2DView当前标记为unstable不稳定状态后续可能发生不向后兼容的显著变更。在生产或长期维护的工程中引用该类型时请锁定 SDK 版本。Spatial2DView 是什么类型定位与视图标识Spatial2DView是 Rerun 的蓝图视图类型之一官方定义一句话概括其用途“用于查看空间二维数据For viewing spatial 2D data”。在仓库的类型定义文件中该视图的声明如下spatial2d.def.rs#[rerun::rerun_type] #[rerun(view_identifier 2D)] #[rerun(state unstable)] pub struct Spatial2DView { /// Configuration for the background of the view. pub background: rerun::blueprint::archetypes::Background, /// The visible parts of the scene, in the coordinate space of the scene. /// /// Everything within these bounds are guaranteed to be visible. /// Somethings outside of these bounds may also be visible due to letterboxing. pub visual_bounds: rerun::blueprint::archetypes::VisualBounds2D, /// Configuration of spatial information shown in the view. pub spatial_information: rerun::blueprint::archetypes::SpatialInformation, /// Configures which range on each timeline is shown by this view (unless specified differently per entity). /// /// If not specified, the default is to show the latest state of each component. /// If a timeline is specified more than once, the first entry will be used. pub time_ranges: rerun::blueprint::archetypes::VisibleTimeRanges, }其中两个关键属性值得注意视图类标识view_identifier为2D这是该视图类在 Rerun 内部的唯一标识。在生成的 Rust 代码中spatial2d_view.rsSpatial2DView实现了re_types_core::Viewtraitidentifier()方法返回字符串常量2DPython 端构造该视图时同样传入class_identifier2D见 spatial2d_view.py。state unstableRerun 通过类型定义文件中的state属性管理 API 稳定性unstable意味着该类型的序列化格式与 API 都可能在后续版本中调整。类型定义文件本身是 Rerun 的“元数据”层它由re_types_builder解析自动生成 Rust、Python、C 三种语言的绑定代码。因此无论是官方文档由 docs.rs 生成、Rust SDK 类型还是 Python SDK 类其行为都严格以这份.def.rs为准。四大属性逐一解析Spatial2DView的构造函数接受四组属性分别对应四个 blueprint archetype。下面结合源码逐一展开。1.background视图背景配置background控制视图的背景呈现方式对应 blueprint archetypeBackgroundbackground.def.rspub struct Background { /// The type of the background. #[rerun(required)] pub kind: rerun::blueprint::components::BackgroundKind, /// Color used for the solid background type. #[rerun(optional)] pub color: Optionrerun::components::Color, }它包含两个字段kind背景类型必填取值为枚举BackgroundKindbackground_kind.def.rsGradientDark默认值 1暗色渐变背景。在 3D 视图中会随观察方向变化2D 视图中表现为固定渐变GradientBright 2亮色渐变背景SolidColor 3纯色背景此时配合color字段使用。color仅对SolidColor生效的颜色值可选类型为 Rerun 通用颜色组件Color。Python 端为Background提供了更友好的构造方式background_ext.py传入单个位置参数color_or_kind要么给一个 RGBA 颜色此时自动推导kind SolidColor要么给一个BackgroundKind或者通过关键字参数color/kind分别指定若什么都不传kind默认GradientDark若只传color不传kind则自动设为SolidColor若只传kind不传color颜色默认白色White。在Spatial2DView构造函数中background参数接受blueprint_archetypes.Background | encodings.Rgba32Like | blueprint_components.BackgroundKindLike因此可以直接传一个[r, g, b]列表来快速设置纯色背景见示例章节。2.visual_bounds场景可视范围视口裁剪visual_bounds决定“场景中哪些区域是可见的”对应 blueprint archetypeVisualBounds2Dvisual_bounds2d.def.rspub struct VisualBounds2D { /// Controls the visible range of a 2D view. /// /// Use this to control pan zoom of the view. #[rerun(required)] pub range: rerun::blueprint::components::VisualBounds2D, }官方文档对它的语义描述如下视图可见部分是场景坐标系中的范围。这些边界内的所有内容保证可见由于 letterboxing信箱式留边边界外的一些内容也可能可见。也就是说visual_bounds相当于给 2D 视图设置“取景框”同时承担平移pan与缩放zoom控制的作用——官方组件注释明确指出“Use this to control pan zoom of the view”。如果未设置visual_bounds视图会根据当前数据或其他相机信息的包围盒自动确定可视范围。Python 端构造方式为关键字参数x_range与y_rangevisual_bounds2d_ext.pyrrb.VisualBounds2D(x_range[-5, 5], y_range[-5, 5])x_rangeX 轴方向的最小可见范围通常即左右边界y_rangeY 轴方向的最小可见范围。由于它表达的是“保证可见的最小范围”实际窗口宽高比与 bounds 宽高比不一致时某一条轴方向会通过 letterboxing 显示更多内容——这正是官方文档“边界外的内容也可能可见”的原因。3.spatial_information空间信息显示配置spatial_information配置视图中空间参考信息的展示方式对应 blueprint archetypeSpatialInformationspatial_information.def.rs共四个可选字段字段类型说明target_frameTransformFrameId所有变换的目标参考坐标系。默认使用视图空间原点实体space origin entity所使用的坐标系show_bounding_boxEnabledbool 包装是否显示包围盒show_axesEnabledbool 包装是否在原点处显示坐标轴axesViewCoordinates控制坐标轴朝向该字段只对 3D 视图生效在 2D 视图中没有效果关于axes字段源码注释补充了几个重要细节它决定 3D 观察者朝向eye orientation、导航方式与默认网格平面三个方向按[x, y, z]顺序排列例如[Right, Down, Forward]表示 X 向右、Y 向下、Z 向前当该属性未设置时3D 视图会依次回退到空间原点实体或最近祖先上记录的ViewCoordinates→ 最近祖先Pinhole的相机朝向 → 默认的 RFU右-前-上该属性在 2D 视图的选择面板selection panel中被隐藏因为对 2D 视图无意义源码 TODO 注明“This property has no effect in 2D views and is hidden from its selection panel”。4.time_ranges时间轴范围配置time_ranges配置视图在每条时间线timeline上展示的时间范围对应 blueprint archetypeVisibleTimeRangesvisible_time_ranges.def.rspub struct VisibleTimeRanges { /// The time ranges to show for each timeline unless specified otherwise on a per-entity basis. /// /// If a timeline is specified more than once, the first entry will be used. #[rerun(required)] pub ranges: Vecrerun::blueprint::components::VisibleTimeRange, }其核心语义与官方文档一致未指定时默认行为展示每个组件的“最新状态”latest-at 语义。更精确地说源码注释指出当某条时间线没有适用的可视化时间范围时查询采用 “latest-at” 语义——视图从时间游标位置出发为每个组件类型查询可用的最新数据重复指定时取首条如果同一条时间线被指定多次仅使用第一条记录默认可视时间范围取决于视图类型时间序列time series与状态时间线state timeline视图默认显示整条时间线其余视图包括Spatial2DView默认采用 latest-at 语义视图级配置优先级低于实体级配置——实体可以在视图配置之外单独指定自己的时间范围。Python 端该参数可接受单个VisibleTimeRangeLike或Sequence[VisibleTimeRangeLike]例如time_rangesrrb.VisibleTimeRange( timelinelog_time, range[rr.TimeInt(0), rr.TimeInt(100)], )完整示例用 Blueprint 定制一个 Spatial2DView官方文档为Spatial2DView提供了一个完整示例原始代码位于 docs/snippets/all/views/spatial2d.py。下面给出带注释的完整版本Use a blueprint to customize a Spatial2DView. import numpy as np import rerun as rr import rerun.blueprint as rrb rr.init(rerun_example_spatial_2d, spawnTrue) # Create a spiral of points: n 150 angle np.linspace(0, 10 * np.pi, n) spiral_radius np.linspace(0.0, 3.0, n) ** 2 positions np.column_stack(( np.cos(angle) * spiral_radius, np.sin(angle) * spiral_radius, )) colors np.dstack(( np.linspace(255, 255, n), # R恒定 255 np.linspace(255, 0, n), # G从 255 渐变到 0 np.linspace(0, 255, n), # B从 0 渐变到 255 ))[0].astype(int) radii np.linspace(0.01, 0.7, n) rr.log(points, rr.Points2D(positions, colorscolors, radiiradii)) # Create a Spatial2D view to display the points. blueprint rrb.Blueprint( rrb.Spatial2DView( origin/, name2D Scene, # Set the background color纯色背景RGBA 风格自动推导 kindSolidColor background[105, 20, 105], # 注意此范围小于数据点的实际范围 # 所以部分点不会显示在视图中超出 visual_bounds 的部分被裁剪。 visual_boundsrrb.VisualBounds2D(x_range[-5, 5], y_range[-5, 5]), ), collapse_panelsTrue, ) rr.send_blueprint(blueprint)运行这个脚本需要本机安装 Rerun Python SDKpip install rerun-sdk执行后会自动打开一个 Rerun 查看器窗口。示例中有几个值得深入理解的细节origin/视图原点设为根路径即所有实体都直接以全局坐标系显示。在 Python 构造器中origin的默认值就是/contents默认值为$origin/**表示该视图包含原点下的全部实体详见 spatial2d_view.py。background[105, 20, 105]传入裸 RGB 列表。根据 background_ext.py 的逻辑非BackgroundKind的值会被当作颜色处理kind自动设为SolidColor——因此效果是一个紫红色的纯色背景。visual_bounds裁剪行为数据点的最大半径约为 3.0spiral_radius np.linspace(0.0, 3.0, n) ** 2最大值3.0² 9.0而visual_bounds仅覆盖[-5, 5]因此螺旋外围的点会超出可视范围这与属性定义“边界内内容保证可见”的描述完全吻合。collapse_panelsTrue让查看器默认收起两侧面板突出视图内容。rr.send_blueprint(blueprint)将蓝图发送到当前连接的查看器会话它发生在rr.log数据之后用于覆盖默认视图布局。此外Spatial2DView的 Python 构造函数还支持一组通用的蓝图参数同样定义在 spatial2d_view.pycontents视图内容查询表达式默认$origin/**name视图显示名称visible视图是否可见默认truedefaults视图级组件默认值——当视图中的某个 archetype 缺少某组件时用这里的值替代视觉化器的常规回退值overrides按实体路径指定的视觉化器覆盖映射注意覆盖路径必须是根起始的完整实体路径暂不支持$origin相对路径或 glob 表达式。可视化原型清单Spatial2DView 能展示什么官方文档列出了Spatial2DView可可视化的完整 archetype 清单本文完整继承如下。二维数据直接在 2D 视图中渲染AnnotationContext— 注解上下文类别标注、颜色映射等Arrows2D— 二维箭头AssetVideo— 视频资源Boxes2D— 二维包围盒Clear— 清空/重置组件CoordinateFrame— 坐标参考系DepthImage— 深度图像Ellipses2D— 二维椭圆EncodedDepthImage— 编码深度图像EncodedImage— 编码图像GridMap— 栅格地图Image— 图像LineStrips2D— 二维折线Pinhole— 针孔相机模型用于投影关系Points2D— 二维点SegmentationImage— 分割图像VideoFrameReference— 视频帧引用VideoStream— 视频流三维数据“记录在活动投影之上”时显示Arrows3D、Asset3D、Boxes3D、Capsules3D、Cylinders3D、Ellipsoids3D、InstancePoses3D、LineStrips3D、Mesh3D、Points3D、Transform3D官方文档对后者给出的限定条件是 “if logged above active projection”——即当三维实体被记录在活动投影如针孔相机投影平面之上时这些 3D 数据也能在Spatial2DView中以投影形式呈现这是 2D 视图与相机/3D 场景联动的关键机制。上述每个 archetype 的完整字段说明可查阅 types/archetypes 目录下的对应文档例如 points2d.md、image.md。源码级实现从类型定义到 SDK 绑定的完整链路理解Spatial2DView的实现链路有助于掌握 Rerun 蓝图体系的运作方式类型定义层spatial2d.def.rs 是唯一的手写“事实来源”source of truth以#[rerun::rerun_type]宏声明结构体并通过#[rerun(view_identifier 2D)]、#[rerun(state unstable)]声明元信息代码生成层re_types_builder解析.def.rs分别生成 Rust / Python / C 绑定。Rust 端产物位于 re_sdk_types/src/blueprint/views/spatial2d_view.rsPython 端产物位于 rerun_sdk/rerun/blueprint/views/spatial2d_view.py两文件头部均标注 “DO NOT EDIT”并注明基于哪个.def.rs生成文档生成层本指南所依据的官方文档页面同样由代码生成器产出见 website.rs这保证了文档与 SDK 行为永远同步。在 Python 端Spatial2DView.__init__会将四个属性分别序列化为properties字典中的键值对键名与 blueprint archetype 同名Background、VisualBounds2D、SpatialInformation、VisibleTimeRanges再连同class_identifier2D、origin、contents等一起交给基类View构造spatial2d_view.py。因此Spatial2DView本质上是一个携带四组 blueprint 组件的视图容器其数据以蓝图blueprint身份存储并随rr.send_blueprint下发到查看器。实用建议与注意事项基于文档与源码使用Spatial2DView时有以下几点值得留意API 不稳定Spatial2DView及其底层属性 archetype 均标注为 unstablebackground、visual_bounds2d、visible_time_ranges等均带#[rerun(state unstable)]。其中仅BackgroundKind枚举当前为 stable。请锁定 SDK 版本并关注升级日志。visual_bounds不等于严格裁剪它是“保证可见的最小范围”由于 letterboxing范围外内容仍可能部分显示若需要精确控制相机取景应理解该字段的语义边界。2D 视图下axes无效spatial_information.axes只影响 3D 视图在 2D 视图的选择面板中该属性被隐藏无需也无法通过 2D 视图配置。时间范围优先级time_ranges仅在实体未单独指定时生效同一时间线重复指定时取第一条Spatial2DView默认采用 latest-at 语义即展示最新状态。示例裁剪现象是预期行为官方示例刻意将visual_bounds设得小于数据范围用于演示视口裁剪效果——当你需要看到全部数据时应让 bounds 覆盖数据包围盒或干脆不设置visual_bounds以启用自动计算。延伸阅读类型定义源文件spatial2d.def.rsPython SDK 实现spatial2d_view.pyRust SDK 实现spatial2d_view.rs可运行示例views/spatial2d.py相关属性 archetype 文档Background、VisualBounds2D、SpatialInformation、VisibleTimeRanges【免费下载链接】rerunVisualize, query, and stream to train on multimodal robotics data.项目地址: https://gitcode.com/GitHub_Trending/re/rerun创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表