cdt2d icon indicating copy to clipboard operation
cdt2d copied to clipboard

is halfedge data present (connections between generated triangles) ?

Open mreinstein opened this issue 5 years ago • 1 comments

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?

mreinstein avatar Feb 01 '21 20:02 mreinstein

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)
        }
    }
}

mreinstein avatar Feb 03 '21 08:02 mreinstein