xxxxxxxxxx
403
let he_mesh;
function setup() {
createCanvas(400, 400);
he_mesh = new HE_Mesh();
}
function draw() {
background(220);
let n1 = he_mesh.nodes[0];
let n2 = he_mesh.nodes[1];
if(frameCount<2){
he_mesh.subdivideEdge(n1,n2);
}
he_mesh.draw();
//he_mesh.listEdges();
//he_mesh.listNodes();
he_mesh.listTable();
noLoop();
}
class HE_Mesh{
constructor(){
let p1 = createVector(50,50);
let p2 = createVector(300,50);
let p3 = createVector(50,300);
let points = [p1,p2,p3]
let edges = [[0,1],[1,2],[2,0]];
let triangles = [[0,1,2]];
this.build(points, edges, triangles);
}
build(points, edges, triangles){
this.buildNodes(points);
this.buildEdges(edges);
this.buildTriangles(triangles);
}
buildNodes(points){
this.nodes = [];
for(let i = 0; i < points.length ; i++){
this.nodes.push(new HE_Node(points[i]))
}
}
buildEdges(edges){
this.edges = [];
for(let i = 0; i < edges.length; i++){
let edge = new HE_Edge(str(i) + " norm");
let flip = new HE_Edge(str(i) + " flip");
edge.setFlip(flip);
edge.setNode(this.nodes[edges[i][0]]);
this.nodes[edges[i][0]].setEdge(edge); // make this cleaner but dont put it in a recursion
// because last time it fucked the next and previous thingie
//flip.setFlip(edge); // no need
flip.setNode(this.nodes[edges[i][1]]);
this.edges.push(edge);
this.edges.push(flip);
}
}
buildTriangles(triangles){ // Builds references to previous and nexts
this.triangles = [];
for(let i = 0; i < triangles.length; i++){
//let triangle = new HE_Triangle()
//triangle.setEdge(this.edges[triangles[i][0]])
//this.triangles.push(triangle);
// set nexts and previous halfedges
// get all 3 node objects
let n1 = this.nodes[triangles[i][0]];
let n2 = this.nodes[triangles[i][1]];
let n3 = this.nodes[triangles[i][2]];
let e1 = n1.he;
let e2 = n2.he;
let e3 = n3.he;
e1.setNext(e2);
e2.setNext(e3);
e3.setNext(e1);
e1.flip.setNext(e3.flip);
e2.flip.setNext(e1.flip);
e3.flip.setNext(e2.flip);
}
}
iterateVertex(n){ // Vertex Umbrella : loops around a vertex
let originalEdge = n.he;
let current = originalEdge;
let result = [];
let maxIter = 100;
for(let i = 0; i < maxIter ; i++){
current = current.flip;
result.push(current);
current = current.next; // was prev before
result.push(current);
if(current == originalEdge){break;}
}
return result;
}
iterateVertexNext(n){ // vertex umbrella gets the next of every edge around a vertex
// basically gets the secondary edge
let originalEdge = n.he;
let current = originalEdge;
let result = [];
let maxIter = 100;
for(let i = 0; i < maxIter ; i++){
current = current.flip;
current = current.next;
result.push(current.next)
if(current == originalEdge){break;}
}
return result;
}
iterateEdge(){
}
getEdge(n1, n2){ // THIS THING GETS ANY EDGE WETHER IT IS FLIP OR NOT THIS IS A FUTURE PROBLEM
// find which edge we're talking about
let umbrella1 = this.iterateVertex(n1);
let umbrella2 = this.iterateVertex(n2);
return returnMatch(umbrella1, umbrella2);
}
getEdgesInBetween(n1, n2){ // gets the two edges in between two nodes (on the right side, the next side)
let uA2 = [];
let uA = this.iterateVertex(n1);
for(let i = 0; i < uA.length ; i++){ // iterate second degree umbrellaz
uA2.push(this.iterateVertex(uA[i].node));
}
let uB = this.iterateVertex(n2);
//console.log(uA);
//console.log(uA2);
//console.log(uB);
for(let i = 0; i < uA2.length ; i++){ // iterate over second degree edges
let match = returnMatch(uA2[i], uB);
if(match){
let result = [uA[i],match]; // return the two edges
//console.log(result);
return result;
}
}
console.log("there is no 2 edges in between the two points")
return false;
}
//given two points, finds the third point in a triangle
getPointInBetween(n1, n2, dir=0){ // dir is for left or right // THERE IS A PROBLEM CONSEQUENCE OF THE GETEDGE DIR PROBLEM
let e = this.getEdge(n1,n2);
//return e.flip.next.next.node; // is there the right amount of nexts here ?
return e.next.next.node;
}
addNode(edge){
// create new mid point
let midpos = midpoint(edge.node.pos, edge.flip.node.pos)
let m = new HE_Node(midpos);
this.nodes.push(m);
// create new halfedges
let r = str(floor(random(0,100)));
let newedge = new HE_Edge("3 norm");
let newflip = new HE_Edge("3 flip");
this.edges.push(newedge);
this.edges.push(newflip);
// set nodes
newedge.setNode(m);
newflip.setNode(edge.flip.node);
//edge.setNode(); // no change
edge.flip.setNode(m);
m.setEdge(newedge); // set an edge for the new node
// set flips
newedge.setFlip(newflip);
//newflip.setFlip() // no change
// new edge prev/nexts
newedge.setNext(edge.next);
//newflip.setNext(edge.flip)
//newflip.setPrev(edge.next.flip)
//newedge.setPrev(edge);
// old edge prevnexts
//edge.setNext(newedge);
// edge.flip.setNext(); // no change
//edge.setPrev(); // no change
//edge.flip.setPrev(newflip);
return m;
}
addEdge(n1, n2){ // first is the middle node and second is the opposite node
let r = str(floor(random(0,100)));
let newedge = new HE_Edge("4 norm");
let newflip = new HE_Edge("4 flip");
this.edges.push(newedge);
this.edges.push(newflip);
let edge = this.getEdgesInBetween(n1, n2)
// so this should be the nonflipedge arriving to the middle node
//console.log(edge);
// set nodes
newedge.setNode(n1);
newflip.setNode(n2);
// set flips
newedge.setFlip(newflip);
// set next
newedge.setNext(edge[0].next);
newflip.setPrev(edge[0].next.next);
newedge.setPrev(edge[0]);
newflip.setNext(edge[1]);
}
subdivideEdge(n1, n2){
// # Get opposing points
// find opposing node 1 to connect with
let o1 = this.getPointInBetween(n1, n2);
// find opposing node 2 to connect with
//let o2 = edge.flip.next.next.node;
// 1. add node
// 2. check how many edges to add
// 3. add edge(s)
let edge = this.getEdge(n1, n2); // THIS IS WHAT NEEDS TO CHANGE, AS THERE ARE TWO EDGES AND I NEED TO GET THE EDGE FROM N1 to N2 not the other
let newnode = this.addNode(edge);
//if only o1 or o2 exists then it is an outside edge
//if both o2 and o2 exist then it is an inside edge
if(o1){ // make link to o1
//this.addEdge(o1, newnode);
}
//if(o2){ // make link to o2
//this.addEdge(o2, newnode, newedge2, newflip2);
//}
}
draw(){
this.drawEdges();
this.drawNodes();
this.drawTriangles();
this.drawHover();
}
drawEdges(){
// draw actual edges
stroke(100);
for(let i = 0; i < this.edges.length; i++){
let x1 = this.edges[i].node.pos.x;
let y1 = this.edges[i].node.pos.y;
let x2 = this.edges[i].flip.node.pos.x;
let y2 = this.edges[i].flip.node.pos.y;
line(x1, y1, x2, y2);
}
// draw arrows
stroke(50,150,120)
// draw half edge vectors
for(let i = 0; i < this.edges.length; i++){
let x1 = this.edges[i].node.pos.x;
let y1 = this.edges[i].node.pos.y;
let x2 = this.edges[i].flip.node.pos.x;
let y2 = this.edges[i].flip.node.pos.y;
let v1 = createVector(x1, y1);
let v2 = createVector(x2, y2);
let l1 = p5.Vector.lerp(v1,v2, 0.25); // make them fuckers a little shorter
let l2 = p5.Vector.lerp(v1,v2, 0.75);
let v = createVector(x2 - x1, y2 - y1);
let perp = PerpendicularClockWise(v);
perp.setMag(5);
line(l1.x+perp.x, l1.y+perp.y, l2.x+perp.x, l2.y+perp.y);
let t1 = createVector(l2.x+perp.x, l2.y+perp.y); // triangle summit
let l3 = p5.Vector.lerp(l1,l2,0.95);
let t2 = createVector(l3.x + 2 * perp.x, l3.y + 2 * perp.y);
let t3 = createVector(l3.x, l3.y);
triangle(t1.x, t1.y,t2.x, t2.y,t3.x, t3.y);
}
}
drawNodes(){
stroke(50)
for(let i = 0; i < this.nodes.length; i++){
circle(this.nodes[i].pos.x, this.nodes[i].pos.y,10);
}
}
drawTriangles(){
}
drawHover(){
for(let i = 0; i < this.edges.length; i++){
let x1 = this.edges[i].node.pos.x;
let y1 = this.edges[i].node.pos.y;
let x2 = this.edges[i].flip.node.pos.x;
let y2 = this.edges[i].flip.node.pos.y;
if(isMouseOverLine(x1, y1, x2, y2)){
//console.log(i)
//console.log(this.edges[i]);
let nx1 = this.edges[i].next.node.pos.x;
let ny1 = this.edges[i].next.node.pos.y;
let nx2 = this.edges[i].next.flip.node.pos.x;
let ny2 = this.edges[i].next.flip.node.pos.y;
let px1 = this.edges[i].prev.node.pos.x;
let py1 = this.edges[i].prev.node.pos.y;
let px2 = this.edges[i].prev.flip.node.pos.x;
let py2 = this.edges[i].prev.flip.node.pos.y;
strokeWeight(random(0,10));
stroke(255,0,0);
line(nx1, ny1, nx2, ny2);
stroke(0,255,0);
line(px1, py1, px2, py2);
strokeWeight(1);
}
}
}
listEdges(){
for(let i = 0; i < this.edges.length; i++){
console.log(this.edges[i]);
}
}
listNodes(){
for(let i = 0; i < this.nodes.length; i++){
console.log(this.nodes[i]);
}
}
listTable(){
console.log("Name | Next | Prev ")
for(let i = 0; i < this.edges.length; i++){
let e = this.edges[i];
console.log(e.name + " | " + e.next.name + " | " + e.prev.name );
}
}
}