cdt2d
cdt2d copied to clipboard
is halfedge data present (connections between generated triangles) ?
I'm interested in knowing what the neighbors of all triangles are, so that I can graph traverse them. Is this information present, or would this need to be calculated from the resulting triangle set?
This is the logic I'm currently using to get triangle neighbors, maybe it'll be useful to someone:
const triangles = cdt2d(points, edges)
// connections to other triangles
// e.g., neighbors[0] might look like [ 4, 5, 96 ] which means triangle 0 is connected to triangles 4,5,96
const neighbors = [ ]
function hasEdge (tri, p0, p1) {
return (tri.indexOf(p0) >= 0) && (tri.indexOf(p1) >= 0)
}
// build the neighbors list
for (let i=0; i < triangles.length; i++) {
const T = triangles[i]
neighbors.push([ ])
// find all triangles that share an edge with T
for (let j=0; j < triangles.length; j++) {
if (j !== i) {
const T2 = triangles[j]
if (hasEdge(T2, T[0], T[1]))
neighbors[i].push(j)
else if (hasEdge(T2, T[1], T[2]))
neighbors[i].push(j)
else if (hasEdge(T2, T[2], T[0]))
neighbors[i].push(j)
}
}
}