org.ibex.arenaj.Gladiator
[org.ibex.core.git] / src / org / ibex / graphics / Mesh.java
index 4693b64..0680a4c 100644 (file)
@@ -29,8 +29,8 @@ public final class Mesh {
     private static final float epsilon2 = (float)0.001;
     private static final boolean debug = false;
 
-    private  Vector    triangles   = new Vector();
-    private  Hash      edges       = new Hash();
+    private  Vector    triangles   = new Vector();     /* we no longer need this */
+    private  Hash      edges       = new Hash();       /* we no longer need this either */
     private  int       numvertices = 0;
     private  Triangle  triangle0   = null;
     private  Vertex    vertex0     = null;
@@ -82,13 +82,17 @@ public final class Mesh {
     public void add(Mesh m, Affine a) { iterateTriangles(ITERATE_ADD, m, a); }
     public static long seekTime = 0;
 
-    public static final int ITERATE_SUBTRACT  = 1;
-    public static final int ITERATE_INTERSECT = 2;
-    public static final int ITERATE_ADD       = 3;
+    public static final int ITERATE_SUBTRACT     = 1;
+    public static final int ITERATE_INTERSECT    = 2;
+    public static final int ITERATE_ADD          = 3;
+    public static final int ITERATE_CLEAR_WASSET = 4;
+    public static final int ITERATE_CLEAR        = 5;
+    public static final int ITERATE_STROKE       = 6;
     int tick = 0;
     Triangle[] iter  = new Triangle[100];
 
-    private void iterateTriangles(int mode, Mesh m, Affine a) {
+    private void iterateTriangles(int mode, Mesh m, Affine a) { iterateTriangles(mode, m, a, null, 0); }
+    private void iterateTriangles(int mode, Mesh m, Affine a, PixelBuffer buf, int color) {
         tick++;
         int numiter = 0;
         if (iter.length < triangles.size()) iter = new Triangle[triangles.size()];
@@ -97,6 +101,9 @@ public final class Mesh {
             Triangle t = iter[--numiter];
             if (t.tick >= this.tick) continue;
             switch(mode) {
+                case ITERATE_STROKE:          t.stroke(buf, a, color); break;
+                case ITERATE_CLEAR:           t.clear(); break;
+                case ITERATE_CLEAR_WASSET:    t.inWasSet = false; break;
                 case ITERATE_INTERSECT:
                 case ITERATE_SUBTRACT: {
                     if (!t.in) break;
@@ -222,6 +229,7 @@ public final class Mesh {
     }
 
     private Point point(float x, float y) { return new Point(x,y); }
+    private Point point(Point a, Point b) { return new Point((a.x+b.x)/2,(a.y+b.y)/2); }
     private Point point(Point p, Affine a) { return point(p.x(a), p.y(a)); }
     private class Point {
         public float x;
@@ -250,27 +258,43 @@ public final class Mesh {
         }
     }
 
-    private final class Vertex extends Point implements org.ibex.arenaj.Gladiator {
+    private final class Vertex extends Point implements org.ibex.classgen.opt.Arena.Gladiator {
         public Vertex(Point p) { super(p); numvertices++; }
     }
 
     // Edge //////////////////////////////////////////////////////////////////////////////
 
     public Edge newEdge(Vertex v1, Vertex v2) {
+        return getEdge(v1,v2);
+        /*
         if (v1==v2) throw new Error();
         Edge ret = (Edge)edges.get(v1,v2);
+        //if (ret != null) throw new Error("tried to get an edge that already exists!");
         if (ret == null) ret = new Edge(v1,v2);
         return ret;
+        */
     }
 
     public Edge getEdge(Vertex v1, Vertex v2) {
         if (v1==v2) throw new Error();
-        Edge ret = (Edge)edges.get(v1,v2);
+        //Edge ret = (Edge)edges.get(v1,v2);
+        Edge ret = null;
+        Triangle t = null;
+        if (triangle0 != null) {
+            t = triangle0.seek(point(v1,v2));
+            if (t != null)
+                for(int i=1; i<=3; i++)
+                    if (t.e(i).hasVertex(v1) && t.e(i).hasVertex(v2)) ret = t.e(i);
+        }
+        if (ret == null) {
+            ret = (Edge)edges.get(v1,v2);
+            if (ret != null && (ret.t1 != null || ret.t2 != null)) throw new Error("bah! " + ret);
+        }
         if (ret == null) ret = new Edge(v1,v2);
         return ret;
     }
 
-    private final class Edge implements org.ibex.arenaj.Gladiator {
+    private final class Edge implements org.ibex.classgen.opt.Arena.Gladiator {
         private final Vertex v1;
         private final Vertex v2;
         Triangle t1 = null;
@@ -315,7 +339,12 @@ public final class Mesh {
         public boolean hasVertex(Vertex v) { return v1==v || v2==v; }
         public boolean hasTriangle(Triangle t) { return t==t1 || t==t2; }
         public String toString() { return v(1) + "--" + v(2); }
-        public void rmTriangle(Triangle t) { if (t1==t) t1 = null; else if (t2==t) t2 = null; else throw new Error(); }
+        public void rmTriangle(Triangle t) {
+            if (t1==t) t1 = null;
+            else if (t2==t) t2 = null;
+            else throw new Error();
+            if (t1==null && t2==null) delete();
+        }
         public boolean convex() { return this.intersects(t1.opposingVertex(t2), t2.opposingVertex(t1)); }
 
         public boolean colinear(Point v) { return area(v,v1,v2)<=epsilon; }
@@ -551,7 +580,7 @@ public final class Mesh {
     }
 
         public static boolean fixing = false;
-    private final class Triangle implements org.ibex.arenaj.Gladiator {
+    private final class Triangle implements org.ibex.classgen.opt.Arena.Gladiator {
 
         final float r2;
         final Point cc;
@@ -812,20 +841,19 @@ public final class Mesh {
     // Drawing //////////////////////////////////////////////////////////////////////////////
 
     public void setIn(boolean evenOdd) {
-        for (int i=0; i<triangles.size(); i++) ((Triangle)triangles.elementAt(i)).inWasSet = false;
+        iterateTriangles(ITERATE_CLEAR_WASSET, null, null);
         triangle0.setIn(evenOdd, 1);
     }
 
     public void fill(PixelBuffer buf, Affine a, Mesh.Chain clip, int color, boolean strokeOnly) {
         if (triangle0==null) return;
-        System.out.println("I have " + triangles.size() + " triangles");
-        for (int i=0; i<triangles.size(); i++) ((Triangle)triangles.elementAt(i)).clear();
+        iterateTriangles(ITERATE_CLEAR, null, null);
         triangle0.fill(buf, a, clip, color, strokeOnly);
     }
 
     public void stroke(PixelBuffer buf, Affine a, int color) {
         if (triangle0==null) return;
-        for (int i=0; i<triangles.size(); i++) ((Triangle)triangles.elementAt(i)).stroke(buf, a, color);
+        iterateTriangles(ITERATE_STROKE, null, a, buf, color);
     }
 
     public void newcontour() {