#bounding-box #computational-geometry #spatial #2d #geometry #computational #algorithm

static_aabb2d_index

为2D轴对齐边界框提供快速静态空间索引数据结构

12个版本 (3个稳定版)

2.0.0 2023年9月4日
1.1.0 2023年9月1日
1.0.0 2023年3月2日
0.7.1 2023年2月23日
0.3.0 2020年12月29日

#587 in 算法

Download history 59/week @ 2024-03-13 12/week @ 2024-03-20 164/week @ 2024-03-27 45/week @ 2024-04-03 2/week @ 2024-04-10 5/week @ 2024-04-17 15/week @ 2024-04-24 30/week @ 2024-05-01 6/week @ 2024-05-08 16/week @ 2024-05-15 24/week @ 2024-05-22 33/week @ 2024-05-29 59/week @ 2024-06-05 38/week @ 2024-06-12 53/week @ 2024-06-19 80/week @ 2024-06-26

每月231次下载
用于 3 个crate(2个直接使用)

MIT/Apache

60KB
1K SLoC

StaticAABB2DIndex

摘要

使用Hilbert曲线空间排序的2D轴对齐边界框的快速静态空间索引数据结构。这是优秀的flatbush JavaScript库的Rust端口。

默认情况下不使用不安全代码(应用了#![forbid(unsafe_code)])。可以通过启用unsafe_optimizations标志来启用一些不安全的优化。请注意,当此标志启用时,API仍然安全,所有优化都在库内部进行。目前,不安全代码用于消除切片边界检查并利用未初始化的内存来避免在分配时清零数组。

crate

文档

示例

快速代码示例

use static_aabb2d_index::*;
// create builder for index containing 4 axis aligned bounding boxes
// index also supports integers and custom types that implement the IndexableNum trait
let mut builder: StaticAABB2DIndexBuilder<f64> = StaticAABB2DIndexBuilder::new(4);
// add bounding boxes to the index
// add takes in (min_x, min_y, max_x, max_y) of the bounding box
builder.add(0.0, 0.0, 2.0, 2.0);
builder.add(-1.0, -1.0, 3.0, 3.0);
builder.add(0.0, 0.0, 1.0, 3.0);
builder.add(4.0, 2.0, 16.0, 8.0);
// note build may return an error if the number of added boxes does not equal the static size
// given at the time the builder was created or the type used fails to cast to/from a u16
let index: StaticAABB2DIndex<f64> = builder.build().unwrap();
// query the created index (min_x, min_y, max_x, max_y)
let query_results = index.query(-1.0, -1.0, -0.5, -0.5);
// query_results holds the index positions of the boxes that overlap with the box given
// (positions are according to the order boxes were added the index builder)
assert_eq!(query_results, vec![1]);
// the query may also be done with a visiting function that can stop the query early
let mut visited_results: Vec<usize> = Vec::new();
let mut visitor = |box_added_pos: usize| -> Control<()> {
    visited_results.push(box_added_pos);
    // return continue to continue visiting results, break to stop early
    Control::Continue
};

index.visit_query(-1.0, -1.0, -0.5, -0.5, &mut visitor);
assert_eq!(visited_results, vec![1]);

依赖项

~155KB