Skip to content

Gabriel, Urquhart, and minimal spanning tree #941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ export {Arrow, arrow} from "./marks/arrow.js";
export {BarX, BarY, barX, barY} from "./marks/bar.js";
export {boxX, boxY} from "./marks/box.js";
export {Cell, cell, cellX, cellY} from "./marks/cell.js";
export {delaunayLink, delaunayMesh, hull, voronoi, voronoiMesh} from "./marks/delaunay.js";
export {
delaunayLink,
delaunayMesh,
hull,
voronoi,
voronoiMesh,
gabrielMesh,
urquhartMesh,
mstMesh
} from "./marks/delaunay.js";
export {Density, density} from "./marks/density.js";
export {Dot, dot, dotX, dotY, circle, hexagon} from "./marks/dot.js";
export {Frame, frame} from "./marks/frame.js";
Expand Down
129 changes: 128 additions & 1 deletion src/marks/delaunay.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {group, path, select, Delaunay} from "d3";
import {bisector, extent, group, path, select, Delaunay} from "d3";
import {create} from "../context.js";
import {Curve} from "../curve.js";
import {constant, maybeTuple, maybeZ} from "../options.js";
Expand Down Expand Up @@ -298,3 +298,130 @@ export function voronoi(data, options) {
export function voronoiMesh(data, options) {
return delaunayMark(VoronoiMesh, data, options);
}

class GabrielMesh extends AbstractDelaunayMark {
constructor(data, options) {
super(data, options, voronoiMeshDefaults);
this.fill = "none";
}
_accept(delaunay) {
const {points, triangles} = delaunay;
return (i) => {
const a = triangles[i];
const b = triangles[i % 3 === 2 ? i - 2 : i + 1];
return [a, b].includes(
delaunay.find((points[2 * a] + points[2 * b]) / 2, (points[2 * a + 1] + points[2 * b + 1]) / 2, a)
);
};
}
_render(delaunay) {
const p = new path();
const {points, halfedges, triangles} = delaunay;
const accept = this._accept(delaunay);
for (let i = 0, n = triangles.length; i < n; ++i) {
const j = halfedges[i];
if (i < j) continue;
if (accept(i)) {
const a = triangles[i];
const b = triangles[i % 3 === 2 ? i - 2 : i + 1];
p.moveTo(points[2 * a], points[2 * a + 1]);
p.lineTo(points[2 * b], points[2 * b + 1]);
}
}
return "" + p;
}
}

class UrquhartMesh extends GabrielMesh {
constructor(data, options) {
super(data, options, voronoiMeshDefaults);
this.fill = "none";
}
_accept(delaunay, score = euclidean2) {
const {halfedges, points, triangles} = delaunay;
const n = triangles.length;
const removed = new Uint8Array(n);
for (let e = 0; e < n; e += 3) {
const p0 = triangles[e],
p1 = triangles[e + 1],
p2 = triangles[e + 2];
const p01 = score(points, p0, p1),
p12 = score(points, p1, p2),
p20 = score(points, p2, p0);
removed[
p20 > p01 && p20 > p12
? Math.max(e + 2, halfedges[e + 2])
: p12 > p01 && p12 > p20
? Math.max(e + 1, halfedges[e + 1])
: Math.max(e, halfedges[e])
] = 1;
}
return (i) => !removed[i];
}
}

function euclidean2(points, i, j) {
return (points[i * 2] - points[j * 2]) ** 2 + (points[i * 2 + 1] - points[j * 2 + 1]) ** 2;
}

class MSTMesh extends GabrielMesh {
constructor(data, options) {
super(data, options, voronoiMeshDefaults);
this.fill = "none";
}
_accept(delaunay, score = euclidean2) {
const {points, triangles} = delaunay;
const set = new Uint8Array(points.length / 2);
const tree = new Set();
const heap = [];

const bisect = bisector(([v]) => -v).left;
function heap_insert(x, v) {
heap.splice(bisect(heap, -v), 0, [v, x]);
}
function heap_pop() {
return heap.length && heap.pop()[1];
}

// Initialize the heap with the outgoing edges of vertex zero.
set[0] = 1;
for (const i of delaunay.neighbors(0)) {
heap_insert([0, i], score(points, 0, i));
}

// For each remaining minimum edge in the heap…
let edge;
while ((edge = heap_pop())) {
const [i, j] = edge;

// If j is already connected, skip; otherwise add the new edge to point j.
if (set[j]) continue;
set[j] = 1;
tree.add(`${extent([i, j])}`);

// Add each unconnected neighbor k of point j to the heap.
for (const k of delaunay.neighbors(j)) {
if (set[k]) continue;
heap_insert([j, k], score(points, j, k));
}
}

return (i) => {
const a = triangles[i];
const b = triangles[i % 3 === 2 ? i - 2 : i + 1];
return tree.has(`${extent([a, b])}`);
};
}
}

export function gabrielMesh(data, options) {
return delaunayMark(GabrielMesh, data, options);
}

export function urquhartMesh(data, options) {
return delaunayMark(UrquhartMesh, data, options);
}

export function mstMesh(data, options) {
return delaunayMark(MSTMesh, data, options);
}
449 changes: 449 additions & 0 deletions test/output/penguinCulmenGabriel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
449 changes: 449 additions & 0 deletions test/output/penguinCulmenMST.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
449 changes: 449 additions & 0 deletions test/output/penguinCulmenUrquhart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/plots/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ export {default as penguinCulmenArray} from "./penguin-culmen-array.js";
export {default as penguinCulmenDelaunay} from "./penguin-culmen-delaunay.js";
export {default as penguinCulmenDelaunayMesh} from "./penguin-culmen-delaunay-mesh.js";
export {default as penguinCulmenDelaunaySpecies} from "./penguin-culmen-delaunay-species.js";
export {default as penguinCulmenGabriel} from "./penguin-culmen-gabriel.js";
export {default as penguinCulmenMST} from "./penguin-culmen-mst.js";
export {default as penguinCulmenUrquhart} from "./penguin-culmen-urquhart.js";
export {default as penguinCulmenVoronoi} from "./penguin-culmen-voronoi.js";
export {default as penguinVoronoi1D} from "./penguin-voronoi-1d.js";
export {default as penguinDensity} from "./penguin-density.js";
Expand Down
19 changes: 19 additions & 0 deletions test/plots/penguin-culmen-gabriel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";

export default async function () {
const data = await await d3.csv("data/penguins.csv", d3.autoType);
return Plot.plot({
marks: [
Plot.delaunayMesh(data, {x: "culmen_depth_mm", y: "culmen_length_mm", stroke: "species", z: "species"}),
Plot.gabrielMesh(data, {
x: "culmen_depth_mm",
y: "culmen_length_mm",
stroke: "species",
z: "species",
strokeOpacity: 1
}),
Plot.dot(data, {x: "culmen_depth_mm", y: "culmen_length_mm", fill: "species", z: "species"})
]
});
}
19 changes: 19 additions & 0 deletions test/plots/penguin-culmen-mst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";

export default async function () {
const data = await await d3.csv("data/penguins.csv", d3.autoType);
return Plot.plot({
marks: [
Plot.delaunayMesh(data, {x: "culmen_depth_mm", y: "culmen_length_mm", stroke: "species", z: "species"}),
Plot.mstMesh(data, {
x: "culmen_depth_mm",
y: "culmen_length_mm",
stroke: "species",
z: "species",
strokeOpacity: 1
}),
Plot.dot(data, {x: "culmen_depth_mm", y: "culmen_length_mm", fill: "species", z: "species"})
]
});
}
19 changes: 19 additions & 0 deletions test/plots/penguin-culmen-urquhart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";

export default async function () {
const data = await await d3.csv("data/penguins.csv", d3.autoType);
return Plot.plot({
marks: [
Plot.delaunayMesh(data, {x: "culmen_depth_mm", y: "culmen_length_mm", stroke: "species", z: "species"}),
Plot.urquhartMesh(data, {
x: "culmen_depth_mm",
y: "culmen_length_mm",
stroke: "species",
z: "species",
strokeOpacity: 1
}),
Plot.dot(data, {x: "culmen_depth_mm", y: "culmen_length_mm", fill: "species", z: "species"})
]
});
}