npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@allmaps/triangulate

v1.0.0-beta.23

Published

Allmaps Triangulation Library

Downloads

309

Readme

@allmaps/triangulate

This module triangulates a polygon: it returns a set of small triangule that partition the polygon.

It is used in @allmaps/render to triangulate the mask of a georeferenced map into a set of triangles that can be rendered with WebGL.

How it works

It uses a simple constrained Delaunay triangulation algorithm for polygons, built using poly2tri.js.

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

Installation

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

Install using npm:

npm install @allmaps/triangulate

Usage

import { triangulate } from '@allmaps/triangulate'

// polygons are not round-trip
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 mesh of size `distance`
const triangles = triangulate(polygon, distance)

// triangles = [
//   [
//     [1.3012562303117026, 2.3199729029037854],
//     [0.304, 2.394],
//     [1.304, 2.232]
//   ],
//   ...
// ]

API

Table of Contents

Triangulation

triangulate

Triangulates a polygon

Parameters
  • polygon Ring Polygon
  • distance number Distance between the Steiner points placed in a grid inside the polygon

Returns Array<Triangle> Array of triangles partitioning the polygon

triangulatePoly2tri

Triangulates a polygon (and returns the full Poly2tri output)

Parameters
  • polygon Ring Polygon
  • distance number Distance between the Steiner points placed in a grid inside the polygon

Returns Array<poly2tri.Triangle> Array of triangles partitioning the polygon

Types

The types used in this module are described below.

poly2tri.Triangle

Triangle object from poly2tri package

Type: Object

triangulateToUnique

Triangulates a polygon and return unique points. Grid points typically occure in 6 triangles This function reutrns the list of unique points, and returns the triangles as uniquePointsIndexTriangles with indices refering to the unique points

Parameters

  • polygon Ring Polygon
  • distance number Distance between the Steiner points placed in a grid inside the polygon

Returns {uniquePointsIndexTriangles: Array<UniquePointsIndexTriangle>, uniquePoints: Array<Point>} Object with uniquePointsIndexTriangles and uniquePoints

Notes

Stability

  • poly2tri doesn't allow self-intersection polygons and will raise an error for such inputs.
  • For certain polygon vertex configurations an especially for round numbers or small distance sizes, poly2tri is known to throw errors such as 'point collinearity' or 'pointerror'.

Benchmark

For a 10 point polygon, here are some benchmarks for computing the triangulation with the distance as a fraction of the polygon's bbox diameter:

  • triangulate(polygon, 1): 66719 ops/s to compute 8 triangles
  • triangulate(polygon, bboxDiameter / 10): 10924 ops/s to compute 86 triangles
  • triangulate(polygon, bboxDiameter / 40): 1115 ops/s to compute 1048 triangles
  • triangulate(polygon, bboxDiameter / 100): 167 ops/s to compute 6216 triangles

See ./bench/index.js.

To run the benchmark, run npm run bench.