Skip to content

Commit

Permalink
Half Edge magic
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyBoivie authored and JimmyBoivie committed Dec 5, 2015
1 parent 86670ff commit 130e197
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 16 deletions.
6 changes: 4 additions & 2 deletions main/CompGeo.pde
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
public static class CompGeo {

public int CCW(Point a, Point b, Point c) {
public static int CCW(Point a, Point b, Point c) {
// Return 1 if turn abc is ccw -1 if turn abc is cw 0 if no turn
float f = (b.getX() - a.getX())*(c.getY() - a.getY()) - (c.getX() - a.getX())*(b.getY()-a.getY());
if (f < 0) { return -1; }
if (f > 0) { return 1; }
return 0;
}

public int immediateCCW(Point parent, Point child, Point v1, Point v2) {
public static int immediateCCW(Point parent, Point child, Point v1, Point v2) {
// returns 1 if v1 is more immediate CCW of parent, child than v2
// returns -1 if v2 is more immediate CCW of parent, child than v1
if (CCW(parent, child, v1) > CCW(parent, child, v2)) { return 1; }
if (CCW(parent, child, v1) < CCW(parent, child, v2)) { return -1; }
return CCW(parent, v1, v2);
}



}
109 changes: 96 additions & 13 deletions main/HalfEdge.pde
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,76 @@ public class HalfEdge {

public HalfEdge(){}

public HalfEdge(Point p, Point q) {
// SETS THE TWIN
settwin(new HalfEdge());
twin.settwin(this);
// SETS THE ORIGIN
setOrigin(p);
twin.setOrigin(q);
// SETS THE COUNTED
setCounted(false);
twin.setCounted(false);

// MAGIC METHOD
connect();
twin.connect();




}

public int connect() {
ArrayList<HalfEdge> others = origin.getEntering();
if (others == null) {
this.setprevious(gettwin());
gettwin().setnext(this);
// update vertex
return 0;
}
if (others.size() == 1) {
HalfEdge other = others.get(0);
this.setprevious(other.gettwin());
gettwin().setnext(other);
// no update vertex
return 1;
}
return complex(this, others);
}


public int complex (HalfEdge self, ArrayList<HalfEdge> entering) {
Point parent = self.getOrigin();
Point child = self.gettwin().getOrigin();
Point p1;
Point p2;
int num = entering.size();
int[] chart = new int[num];
for (int i = 0; i < num; i++) {
p1 = entering.get(i).getOrigin();
p2 = entering.get((i+1)%num).getOrigin();
if (CompGeo.immediateCCW(parent, child, p1, p2) == 1) { chart[i]++; }
else { chart[(i+1)%num]++; }
}
int Loser = 0;
int Winner = 0;
for (int i = 0; i < num; i++) {
if (chart[i] == 0) { Loser = i; }
if (chart[i] == 2) { Winner = i; }
}
self.setprevious(entering.get(Winner));
entering.get(Winner).setnext(self);
self.gettwin().setnext(entering.get(Loser).gettwin());
entering.get(Loser).gettwin().setprevious(self.gettwin());
return 2;
}






public int count() {
if (counted == false) {
counted = true;
Expand All @@ -29,29 +99,42 @@ public class HalfEdge {
}
}

public ArrayList<HalfEdge> getHalfEdgesAlsoLeaving() {
ArrayList<HalfEdge> result = new ArrayList<HalfEdge>();
result.add(this);
public ArrayList<Point> getAdjacentPoints() {
ArrayList<Point> points = new ArrayList<Point>();
HalfEdge temp = previous.gettwin();
while (temp != this) {
result.add(temp);
temp = (temp.getprevious()).gettwin();
points.add(temp.gettwin().getOrigin());
temp = temp.getprevious().gettwin();
}
return result;
points.add(this.getOrigin());
return points;
}

public ArrayList<HalfEdge> getFace() {
public ArrayList<HalfEdge> getEntering() {
ArrayList<HalfEdge> result = new ArrayList<HalfEdge>();
result.add(this);
HalfEdge temp = next;
while (temp != this) {
HalfEdge temp = twin.getnext().gettwin();
while (temp != twin) {
result.add(temp);
temp = temp.getnext();
temp = temp.getnext().gettwin();
}
return result;
result.add(twin);
return result;
}


public ArrayList<HalfEdge> getOtherLeaving() {
ArrayList<HalfEdge> result = new ArrayList<HalfEdge>();
HalfEdge temp = previous.gettwin();
while (temp != this) {
result.add(temp);
temp = (temp.getprevious()).gettwin();
}
return result;
}
public ArrayList<HalfEdge> getAllLeaving() {
ArrayList<HalfEdge> others = getOtherLeaving();
others.add(this);
return others;
}

// may not want getters, but for now they are here.
public void setprevious(HalfEdge p) {
Expand Down
15 changes: 14 additions & 1 deletion main/Point.pde
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ public class Point {
return ((a == x) && (b == y));
}

public ArrayList<HalfEdge> getLeaving() {
if (ref == null) { return null; }
return ref.getAllLeaving();
}

public ArrayList<Point> getAdjacentPoints() {
if (ref == null) { return null; }
return ref.getAdjacentPoints();
}

public ArrayList<HalfEdge> getEntering() {
if (ref == null) { return null; }
return ref.getEntering();
}

// NEED METHOD GETTING ALL EDGES LEAVING POINT
}

0 comments on commit 130e197

Please sign in to comment.