Published
Edited
Dec 8, 2018
1 fork
9 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function HalfEdge(tail,head){
this.head = head; //Integer index in vertex list
this.tail = tail; //Integer index in vertex list
this.next = null;
this.twin = null;
this.face = null;
//Here, the next and twin values will be populated as the mesh is created.
}
Insert cell
function Vertex(point){
this.point = point; // Coordinate position
this.halfedge = null;
}
Insert cell
HalfEdgeMesh = {
function HalfEdgeMesh(){
this.verts = [];
this.edges = {};
this.geom = null; // Store the original geometry too
}

HalfEdgeMesh.prototype.addVert = function(point){
this.verts.push(new Vertex(point));
}
HalfEdgeMesh.prototype.addHalfEdge = function(head,tail){
if(!([head,tail] in this.edges)){
// To store halfedges, we use the head and tail as a key to reference the halfedge object.
var E = new HalfEdge(this.verts[head],this.verts[tail]);
this.edges[[head,tail]] = E;
return E;
}
}
HalfEdgeMesh.prototype.populateFromThreeGeom = function(geom){
this.geom = geom;
for(var V in geom.vertices){
//Populate the vertices. Three.js uses index-based vert references, so we will too.
this.addVert(geom.vertices[V]);
}
for(var faceIndex in geom.faces){
var F = geom.faces[faceIndex];
// Each face is a tri, so we know what the vert pairs are for each face.
var vertPairs = [[F.a,F.b],[F.b,F.c],[F.c,F.a]];
for(var i = 0; i<3; i++){
var E=vertPairs[i];
// Create our half edges
var newEdge = this.addHalfEdge(E[0],E[1]);
newEdge.face = F;
// If the source vert doesn't have a half-edge yet, populate it.
if(this.verts[E[0]].halfedge == null){
this.verts[E[0]].halfedge = newEdge;
}
}
for(var i=0; i<3; i++){
var E = vertPairs[i]
// We know the next from the order around our tri, so populate that
this.edges[E].next = this.edges[vertPairs[(i+1) % 3]];
// Look for the halfedge going the opposite direction to populate the twin.
if([E[1],E[0]] in this.edges){
this.edges[E].twin = this.edges[[E[1],E[0]]];
this.edges[[E[1],E[0]]].twin = this.edges[E];
}
}
}
}
return HalfEdgeMesh
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more