Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
A robust 2D constrained Delaunay triangulation library written in JavaScript.
WORK IN PROGRESS
To test out this module, you can open up a demo in your browser with the following link:
Here is a simple example showing how to invoke cdt2d
:
//First we need to reqire the module
var cdt2d = require('cdt2d')
//Then we define a list of points, represented as pairs of x,y coordinates
var points = [
[-2,-2],
[-2, 2],
[ 2, 2],
[ 2,-2],
[ 1, 0],
[ 0, 1],
[-1, 0],
[ 0,-1]
]
//Next we can optionally define some edge constraints
// This set of edges determines a pair of loops
var edges = [
//Outer loop
[0, 1],
[1, 2],
[2, 3],
[3, 0],
//Inner loop
[4, 5],
[5, 6],
[6, 7],
[7, 4]
]
//Finally we call cdt2d with the points and edges
// The flag {exterior: false} tells it to remove exterior faces
console.log(cdt2d(points, edges, {exterior: false}))
The above program will output the following triangles:
[ [ 0, 3, 7 ],
[ 0, 6, 1 ],
[ 0, 7, 6 ],
[ 1, 5, 2 ],
[ 1, 6, 5 ],
[ 2, 4, 3 ],
[ 2, 5, 4 ],
[ 3, 4, 7 ] ]
Each triangle is represented as an array of 3 indices of points. We can visualize this data in the following figure:
If your input doesn't satisfy the validity invariants (ie no self intersections, duplicate vertices or t-junctions), then you will need to preprocess it to clean it up. One way to do this is with the clean-pslg
module. Here is an example showing how to do this:
var cleanPSLG = require('clean-pslg')
var cdt2d = require('cdt2d')
var points = [
[-1, 0],
[ 1, 0],
[ 0,-1],
[ 0, 1]
]
var edges = [
[0, 1],
[1, 2]
]
//This updates points/edges so that they now form a valid PSLG
cleanPSLG(points, edges)
//Generate the triangulation
console.log({
points: points,
edges: edges,
triangles: cdt2d(points, edges)
})
TODO
It is also pretty easy to use this module with polygons, as one would get from a GeoJSON file. To do this, it is first necessary to convert them into a planar straight line graph. This can be done using the poly-to-pslg
module:
var toPSLG = require('poly-to-pslg')
var cdt2d = require('cdt2d')
TODO
The above procedure even works if the polygons have holes:
var toPSLG = require('poly-to-pslg')
var cdt2d = require('cdt2d')
TODO
You can also use cdt2d
to generate Delaunay triangulations of arbitrary point sets in the plane:
TODO
This module works in any modern CommonJS environment. You can install it using npm with the following command:
npm i cdt2d
You should be able to then use it in node or on the web with browserify.
var cells = require('cdt2d')(points[, edges, options])
Constructs a constrained Delaunay triangulation of a planar straight-line graph.
points
are the vertices of the triangulation, represented by pairs of numbers.edges
is an optional list of edge constraints which must occur within the triangulation. These constraints are given by pairs of indices of points. If not specified, then no constraints are used.options
is an object which takes some optional parameters.
delaunay
if this flag is set to true, then the resulting triangulation is converted to a Delaunay triangulation by edge flipping. Otherwise if it is false, then an arbitrary triangulation is returned. (Default true
)interior
if set, only return interior faces. See note. (Default true
)exterior
if set, only return exterior faces. See note. (Default true
)infinity
if set, then the triangulation is augmented with a point at infinity represented by the index -1
. (Default false
)Returns A list of all triangles represented as triples of indices of vertices
Note on interior/exterior classification Interior/exterior faces are classified by treating the constraint edges as the boundary and traversing the triangulation. The point at infinity is in the exterior of the set, and other faces are classified by the parity of the path with fewest crossings from the face to the point at infinity.
Assumptions This module makes the following assumptions about the points and edge constraints:
If your input does not satisfy these conditions, you will need to preprocess it first (using clean-pslg
for example) otherwise cdt2d
may return incorrect results.
Limitations Currently there is no way to specify that only some edge constraints are to be included in the boundary. It is also not possible to add a constraint from a vertex to the point at infinity. If there is enough demand I may add these features or perhaps create a separate module.
Assertion: cdt2d
is the only non-broken triangulation library in JavaScript.
Libraries to compare against:
earcut
poly2tri
pnltri
libtess.js
(c) 2015 Mikola Lysenko. MIT License
FAQs
Constrained Delaunay Triangulation in 2D
The npm package cdt2d receives a total of 18,374 weekly downloads. As such, cdt2d popularity was classified as popular.
We found that cdt2d demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.