#gis #low-cost-code #earcut

无 std earclip

设计为快速、高效且能处理球体的三角形网格

2 个稳定版本

1.1.0 2024年7月19日
1.0.0 2024年5月26日

#633算法

Download history 142/week @ 2024-05-24 18/week @ 2024-05-31 3/week @ 2024-06-07 114/week @ 2024-07-19 7/week @ 2024-07-26 1/week @ 2024-08-02

每月下载量 122

MIT 许可证 MIT

58KB
1.5K SLoC

earclip

GitHub Actions Workflow Status npm crate downloads bundle docs-ts docs-rust doc-coverage code-coverage Discord

关于

这是最快的、体积最小的 JavaScript 多边形三角剖分库,内置剖分。压缩后仅3.18 kB。

安装

# NPM
npm install earcut
# PNPM
pnpm add earcut
# Yarn
yarn add earcut
# Bun
bun add earcut

算法

该库实现了一种改进的耳切算法,通过 z-order 曲线哈希进行优化,并扩展以处理孔洞、扭曲的多边形、退化性和自相交,但不保证三角剖分的正确性,但尝试为实际数据始终产生可接受的结果。

它基于 Martin Held 的 FIST:快速工业强度多边形三角剖分 和 David Eberly 的 耳切三角剖分 的想法。

用法

import { earclip } from 'earclip'

const poly = [[[3506,-2048],[7464,402],[-2048,2685],[-2048,-2048],[3506,-2048]],[[-2048,-37],[1235,747],[338,-1464],[-116,-1188],[-2048,-381],[-2048,-37]],[[-1491,-1981],[-1300,-1800],[-1155,-1981],[-1491,-1981]]]
const modulo = 8192 / 2

const res = earclip(poly, modulo)
console.log(res)

const polyAsPoints = [
    [{ x: 3506, y: -2048 },{ x: 7464, y: 402 },{ x: -2048, y: 2685 },{ x: -2048, y: -2048 },{ 3506, y: -2048 }],
    [{ x: -2048, y: -37 },{ x: 1235, y: 747 },{ x: 338, y: -1464 },{ x: -116, y: -1188 },{ x: -2048, y: -381 },{ x: -2048, y: -37 }],
    [{ x: -1491, y: -1981 },{ x: -1300, y: -1800 },{ x: -1155, y: -1981 },{ x: -1491, y: -1981 }],
]
const res2 = earclip(polyAsPoints, modulo)

assert(res === res2)

开发

需求

您需要 tarpaulin 工具来生成覆盖率报告。使用以下命令安装它:

cargo install cargo-tarpaulin

使用 bacon coverage 工具生成覆盖率报告。要使用 pycobertura 包生成更美观的覆盖率报告,请使用以下命令安装它:

pip install pycobertura

运行测试

要运行测试,请使用以下命令:

# TYPESCRIPT
## basic test
bun run test
## live testing
bun run test:dev

# RUST
## basic test
cargo test
# live testing
bacon test

生成覆盖率报告

要生成覆盖率报告,请使用以下命令:

cargo tarpaulin
# bacon
bacon coverage # or type `l` inside the tool

基准测试

Rust

使用以下命令运行 Rust 基准测试:

cargo +nightly bench

依赖项

~155KB