Skip to content

Commit

Permalink
DwHalfEdge and VerletPhysics update
Browse files Browse the repository at this point in the history
  • Loading branch information
diwi committed Nov 28, 2017
1 parent b4427bf commit 66f9298
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 48 deletions.
98 changes: 60 additions & 38 deletions src/com/thomasdiewald/pixelflow/java/geometry/DwHalfEdge.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,24 @@
public class DwHalfEdge {

static public class Edge{
public Edge pair;
public Edge next;
public Vert vert;
public int FLAG; // can be used for anything. e.g. bitmask, pointer, boolean etc...
public Edge pair;
public Edge next;
public Vert vert;
public int FLAG; // can be used for anything. e.g. bitmask, pointer, boolean etc...
public Object user; // user pointer

public Edge(Vert vert){
this.vert = vert;
this.vert.edge = this;
}

public void dispose(){
pair = null;
next = null;
vert = null;
user = null;
FLAG = 0;
}
}

static public class Face{
Expand All @@ -41,11 +51,19 @@ public Face(Edge edge){
}

static public class Vert{
public Edge edge;
public int vert;
public int FLAG;
public Edge edge;
public int FLAG;
public int idx; // vertex array index
public Object user; // user pointer
public Vert(int vert){
this.vert = vert;
this.idx = vert;
}

public void dispose(){
edge = null;
user = null;
idx = 0;
FLAG = 0;
}
}

Expand Down Expand Up @@ -102,7 +120,8 @@ private void create(DwIndexedFaceSetAble ifs){
// edgemap, for finding edge-pairs
HashMap<DwPair<DwHalfEdge.Vert>, DwHalfEdge.Edge> edgemap = new HashMap<DwPair<DwHalfEdge.Vert>, DwHalfEdge.Edge>();

// verts initialized

// init vertices
for(int i = 0; i < verts_count; i++){
verts[i] = new Vert(i);
}
Expand Down Expand Up @@ -138,14 +157,7 @@ private void create(DwIndexedFaceSetAble ifs){


public int getNumberOfVertexEdges(int vertex_id){
DwHalfEdge.Vert vert = verts[vertex_id];
DwHalfEdge.Edge edge = vert.edge;
DwHalfEdge.Edge iter = edge;
int count = 0;
do {
count++;
} while((iter = iter.pair.next) != edge);
return count;
return DwHalfEdge.getNumberOfVertexEdges(verts[vertex_id]);
}

/**
Expand All @@ -157,20 +169,8 @@ public int getNumberOfVertexEdges(int vertex_id){
* @return int, number of edges attached to this vertex
*/
public int getVertexEdges(int vertex_id, DwHalfEdge.Edge[] edges){
DwHalfEdge.Vert vert = verts[vertex_id];
DwHalfEdge.Edge edge = vert.edge;
DwHalfEdge.Edge iter = edge;
int count = 0;
do {
if(count < edges.length){
edges[count] = iter;
}
count++;
} while(iter.pair != null && (iter = iter.pair.next) != edge);
return count;
return DwHalfEdge.getVertexEdges(verts[vertex_id], edges);
}



// display stuff
private int DISPLAY_BIT = 0;
Expand All @@ -196,7 +196,7 @@ private void displayPolygons(PGraphics3D pg, DwHalfEdge.Edge edge){
DwHalfEdge.Edge iter = edge;
pg.beginShape();
do {
v = verts[iter.vert.vert]; pg.vertex(v[0], v[1], v[2]);
v = verts[iter.vert.idx]; pg.vertex(v[0], v[1], v[2]);
} while((iter = iter.next) != edge);
pg.endShape(PConstants.CLOSE);

Expand All @@ -219,10 +219,10 @@ private void displayQuads(PGraphics3D pg, DwHalfEdge.Edge edge){
edge = stack.pop();
if(edge != null && getFLAG_display(edge)){
// draw quad
v = verts[edge.vert.vert]; edge = edge.next; pg.vertex(v[0], v[1], v[2]);
v = verts[edge.vert.vert]; edge = edge.next; pg.vertex(v[0], v[1], v[2]);
v = verts[edge.vert.vert]; edge = edge.next; pg.vertex(v[0], v[1], v[2]);
v = verts[edge.vert.vert]; edge = edge.next; pg.vertex(v[0], v[1], v[2]);
v = verts[edge.vert.idx]; edge = edge.next; pg.vertex(v[0], v[1], v[2]);
v = verts[edge.vert.idx]; edge = edge.next; pg.vertex(v[0], v[1], v[2]);
v = verts[edge.vert.idx]; edge = edge.next; pg.vertex(v[0], v[1], v[2]);
v = verts[edge.vert.idx]; edge = edge.next; pg.vertex(v[0], v[1], v[2]);
// recursively draw neighbors
stack.push((edge = edge.next).pair); setFLAG_display(edge);
stack.push((edge = edge.next).pair); setFLAG_display(edge);
Expand All @@ -243,9 +243,9 @@ private void displayTriangles(PGraphics3D pg, DwHalfEdge.Edge edge){
edge = stack.pop();
if(edge != null && getFLAG_display(edge)){
// draw triangle
v = verts[edge.vert.vert]; edge = edge.next; pg.vertex(v[0], v[1], v[2]);
v = verts[edge.vert.vert]; edge = edge.next; pg.vertex(v[0], v[1], v[2]);
v = verts[edge.vert.vert]; edge = edge.next; pg.vertex(v[0], v[1], v[2]);
v = verts[edge.vert.idx]; edge = edge.next; pg.vertex(v[0], v[1], v[2]);
v = verts[edge.vert.idx]; edge = edge.next; pg.vertex(v[0], v[1], v[2]);
v = verts[edge.vert.idx]; edge = edge.next; pg.vertex(v[0], v[1], v[2]);
// recursively draw neighbors
stack.push((edge = edge.next).pair); setFLAG_display(edge);
stack.push((edge = edge.next).pair); setFLAG_display(edge);
Expand Down Expand Up @@ -274,6 +274,28 @@ private void setFLAG_display(DwHalfEdge.Edge edge){



static public int getNumberOfVertexEdges(DwHalfEdge.Vert vert){
DwHalfEdge.Edge edge = vert.edge;
DwHalfEdge.Edge iter = edge;
int count = 0;
do {
count++;
} while((iter = iter.pair.next) != edge);
return count;
}

static public int getVertexEdges(DwHalfEdge.Vert vert, DwHalfEdge.Edge[] edges){
DwHalfEdge.Edge edge = vert.edge;
DwHalfEdge.Edge iter = edge;
int count = 0;
do {
if(count < edges.length){
edges[count] = iter;
}
count++;
} while(iter.pair != null && (iter = iter.pair.next) != edge);
return count;
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void addParticles(T[] particles_add, int particles_add_count){
}
}


public void setParticles(T[] particles_set, int particles_set_count){
this.particles = particles_set;
this.particles_count = particles_set_count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ public void update(){
}


static public DwSpringConstraint addSpring(DwPhysics<DwParticle2D> physics, DwParticle2D pa, DwParticle2D pb, float rest_length, Param param){
static public DwSpringConstraint addSpring(DwPhysics<? extends DwParticle> physics, DwParticle2D pa, DwParticle2D pb, float rest_length, Param param){
DwSpringConstraint spring = addSpring(physics, pa, pb, param, TYPE.STRUCT);
spring.dd_rest = rest_length;
spring.dd_rest_sq = rest_length*rest_length;
return spring;
}

static public DwSpringConstraint addSpring(DwPhysics<DwParticle2D> physics, DwParticle2D pa, DwParticle2D pb, Param param){
static public DwSpringConstraint addSpring(DwPhysics<? extends DwParticle> physics, DwParticle2D pa, DwParticle2D pb, Param param){
return addSpring(physics, pa, pb, param, TYPE.STRUCT);
}
static public DwSpringConstraint addSpring(DwPhysics<? extends DwParticle> physics, DwParticle2D pa, DwParticle2D pb, Param param, TYPE type){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ public void update(){
}



static public DwSpringConstraint addSpring(DwPhysics<DwParticle3D> physics, DwParticle3D pa, DwParticle3D pb, float rest_length, Param param){
static public DwSpringConstraint addSpring(DwPhysics<? extends DwParticle> physics, DwParticle3D pa, DwParticle3D pb, float rest_length, Param param){
DwSpringConstraint spring = addSpring(physics, pa, pb, param, TYPE.STRUCT);
spring.dd_rest = rest_length;
spring.dd_rest_sq = rest_length*rest_length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void create(DwPhysics<DwParticle3D> physics, int subdivisions, float radi
int edge_count = mesh.getVertexEdges(ia, edges);

for(int j = 0; j < edge_count; j++){
int ib = edges[j].pair.vert.vert;
int ib = edges[j].pair.vert.idx;
DwSpringConstraint spring = addSpring(ia, ib, DwSpringConstraint.TYPE.STRUCT);
if(spring != null && spring.dd_rest < nodes_r_tmp){
nodes_r_tmp = spring.dd_rest;
Expand Down Expand Up @@ -129,7 +129,7 @@ public void create(DwPhysics<DwParticle3D> physics, int subdivisions, float radi
edge = edge.pair.next;
}

ib = edge.vert.vert;
ib = edge.vert.idx;
addSpring(ia, ib, DwSpringConstraint.TYPE.BEND);
}
}
Expand Down Expand Up @@ -191,12 +191,12 @@ public void computeNormals(){
float[]n = normals[ia]; n[0] = n[1] = n[2] = 0;
int edge_count = mesh.getVertexEdges(ia, edges);
for(int j = 0; j < edge_count-1; j++){
int ib0 = edges[j+0].next.vert.vert;
int ib1 = edges[j+1].next.vert.vert;
int ib0 = edges[j+0].next.vert.idx;
int ib1 = edges[j+1].next.vert.idx;
DwParticle3D.crossAccum(pC, particles[ib0], particles[ib1], n);
}
int ib0 = edges[0].next.vert.vert;
int ib1 = edges[edge_count-1].next.vert.vert;
int ib0 = edges[0].next.vert.idx;
int ib1 = edges[edge_count-1].next.vert.idx;
DwParticle3D.crossAccum(pC, particles[ib0], particles[ib1], n);

float dd_sq = n[0]*n[0] + n[1]*n[1] + n[2]*n[2];
Expand Down

0 comments on commit 66f9298

Please sign in to comment.