@allmaps/triangulate

中文文档
1.0.0-beta.34发布于 2026/09/22MITES Module近 30 天下载 8731

Allmaps Triangulation Library

原始使用文档
文档目录8 节
  1. How it works
  2. Installation
  3. Usage
  4. License
  5. API
  6. Notes
  7. Stability
  8. Benchmark

@allmaps/triangulate

This package provides methods to triangulate a polygon: i.e. return a set of triangles that partition the polygon.

It does this in a constrained way, where the triangles meet certain conditions: if a distance parameter is provided, the triangles are not larger than distance, and in general don't contain very sharp angles or very blunt angles.

This package is used internally in @allmaps/render to triangulate the full mask of a georeferenced map (using the GCPs as Steiner points and the appliable mask as Steiner polygons) into a set of triangles that can be rendered with WebGL.

How it works

It uses a modern constrained Delaunay triangulation algorithm for polygons, built using Delaunator and Constrainautor.

To learn more on how it works, check out this Observable notebook.

In short, triangles are made firstly using a grid of points within the bounding box of the polygon, spaced distance apart, and with points resulting from the polygons edges (interpolated using distance), provided Steiner points and points from the Steiner Polygons. From all these points, a Delaunay triangulation is performed. This results in most grid cell resulting in two well-conditioned triangles. Then the triangulation is constrained with the edges of the interpolated polygon and the edges of the interpolated Steiner polygons.

Installation

This is an ESM-only module that works in browsers and Node.js.

Install with pnpm:

pnpm install @allmaps/triangulate

Usage

This package exposes two functions: triangulate() simply triangulates a polygon and returns the triangles, and triangulateToUnique() returns a set of objects that describe the triangulation using an array of unique points and edges between them.

In the simplest case, it can be used as follows:

import { triangulate } from '@allmaps/triangulate'

// Note that polygons are in double brackets (an array of an outer ring, and possibly inner rings if there are holes) and their rings are not round-trip (the first coordinate is not repeated at the and)
const polygon = [
  [
    [0.592, 0.953],
    [0.304, 2.394],
    [2.904, 2.201],
    [2.394, 0.232]
  ]
]

const distance = 1

// Compute constrained triangulation of `polygon` using a grid of size `distance`
const triangles = triangulate(polygon, distance)

// triangles = [
//   [
//     [ 1.304, 1.232 ],
//     [ 1.4655588463411926, 0.6034795070965593 ],
//     [ 0.592, 0.953 ]
//   ],
//   ...
// ]

To both functions, the following options can be passed:

  • steinerPoints: Steiner points. These become a third group of points taken into account when building the triangles.
  • steinerPolygons: Steiner polygons. These are polygons whose points need to be added as Steiner points, and whose edges (interpolated using distance) also need to be constrained.
  • minimumTriangleAngle: The minimum angle (in radians) of the resulting triangles. Using this options, sliver polygons that are possibly produced by internal functions can be removed. Default: 0.01.
  • computeInsideSteinerPolygons: Whether or not to compute, for each triangle, whether they are inside the Steiner polygons or not.

License

MIT

API

Notes

Stability

  • Constrainautor doesn't allow self-intersection polygons and will raise an error for such inputs.

Benchmark

For a 10 point polygon (with diameter ~ 200), here are some benchmarks for computing the triangulation with given distances:

  • triangulate(polygon, 1000) (no grid points): 74544 ops/s to compute 8 triangles
  • triangulate(polygon, 100): 56849 ops/s to compute 11 triangles
  • triangulate(polygon, 10): 2163 ops/s to compute 435 triangles
  • triangulate(polygon, 1): 3 ops/s to compute 38352 triangles

See ./bench/index.js.

To run the benchmark, run npm run bench.

版本与发布记录

@allmaps/triangulate 共发布 29 个版本,首次发布于 2023/08/22;过去 12 个月发布了 5 个版本。

版本发布日期距今
1.0.0-beta.34latest2026/09/221 个月前
1.0.0-beta.332026/05/295 个月前
1.0.0-beta.322026/02/108 个月前
1.0.0-beta.312025/11/2711 个月前
1.0.0-beta.302025/11/1111 个月前
1.0.0-beta.292025/07/201.3 年前

其他发布通道:beta → 1.0.0-beta.26。

关于 @allmaps/triangulate 的常见问题

@allmaps/triangulate 是什么?

Allmaps Triangulation Library

如何安装 @allmaps/triangulate?

在项目目录运行 npm install @allmaps/triangulate,或使用 pnpm add / yarn add / bun add。

@allmaps/triangulate 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/allmaps__triangulate 是否存在。

@allmaps/triangulate 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@allmaps/triangulate 可以商用吗?

@allmaps/triangulate 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@allmaps/triangulate 的下载量有多少?

根据 npm 官方下载统计,@allmaps/triangulate 在 2026/10/06 – 2026/11/04 这 30 天内被下载 8,731 次,日均约 291 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@allmaps/triangulate 还在维护吗?

最新版本 1.0.0-beta.34 发布于 2026/09/22(1 个月前);至今共发布 29 个版本,其中过去 12 个月发布了 5 个;首次发布于 2023/08/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。