Skip to content
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

Add support for multiple hulls #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ These methods are used in d3-geo-voronoi’s [geoContour](https://github.com/Fil

Sets the *triangulate* function. Defaults to d3.Delaunay.from. See [Reusing a tricontour triangulation](https://observablehq.com/@fil/reusing-a-tricontour-triangulation) and [UK tricontour](https://observablehq.com/@fil/tricontours-with-a-personalized-triangulation) for detailed examples.

The *triangulate* function should create an object which is compatible with d3.Delaunay, i.e. it must have `points`, `halfedges`, `hull` and `triangles`.
However, in order to support hulls made up of multiple connected components, `hull` does not have to be an `Int32Array` but can be an `Array` of `Int32Array` (one per connected component of the hull).
This allows to handle the case where the domain is made up of multiple pieces and/or has holes.

[<img src="https://raw.githubusercontent.com/Fil/d3-tricontour/main/img/tricontour-triangulation.jpg" alt="UK tricontour" width="320">](https://observablehq.com/@fil/tricontours-with-a-personalized-triangulation)


Expand All @@ -108,4 +112,4 @@ Sets the *pointInterpolate* function. Arguments: *i*, *j*, *0≤a<1*. Defaults t

<a href="#ringsort" name="ringsort">#</a> _tricontour_.<b>ringsort</b>(_[ringsort]_)

Sets the *ringsort* function.
Sets the *ringsort* function.
30 changes: 21 additions & 9 deletions src/tricontour.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ export default function() {
// sanity check
for (const d of values) if (!isFinite(d)) throw ["Invalid value", d];

const { halfedges, hull, inedges, triangles } = triangulation,
const { halfedges, hull: hull_or_hulls, inedges, triangles } = triangulation,
n = values.length;

const hulls = Array.isArray(hull_or_hulls[0]) ? hull_or_hulls : [hull_or_hulls];

function edgealpha(i) {
return alpha(triangles[i], triangles[next(i)]);
}
Expand Down Expand Up @@ -163,7 +165,15 @@ export default function() {

// or follow the hull
else {
let h = (hull.indexOf(triangles[i]) + 1) % hull.length;
let idx, hull;
for (hull of hulls) {
idx = hull.indexOf(triangles[i]);
if (idx >= 0) {
// `triangles[i]` is on the hull
break;
}
}
let h = (idx + 1) % hull.length;

while (values[hull[h]] < v0) {
// debugger;
Expand Down Expand Up @@ -195,14 +205,16 @@ export default function() {
}

// special case all values on the hull are >=v0, add the hull
if (hull.every(d => values[d] >= v0)) {
rings.unshift(
Array.from(hull)
.concat([hull[0]])
.map(i => pointInterpolate(i, i, 0))
);
for (const hull of hulls) {
if (hull.every(d => values[d] >= v0)) {
rings.unshift(
Array.from(hull)
.concat([hull[0]])
.map(i => pointInterpolate(i, i, 0))
);
}
}

return ringsort(rings); // return [rings] if we don't need to sort
}
}
}
Loading