imported initial version of Mesh.java
authoradam <adam@megacz.com>
Mon, 14 Feb 2005 04:18:24 +0000 (04:18 +0000)
committeradam <adam@megacz.com>
Mon, 14 Feb 2005 04:18:24 +0000 (04:18 +0000)
darcs-hash:20050214041824-5007d-e8adfd9700b3ea9ad2c17a62e36a52050b0906f2.gz

src/org/ibex/graphics/Mesh.java [new file with mode: 0644]

diff --git a/src/org/ibex/graphics/Mesh.java b/src/org/ibex/graphics/Mesh.java
new file mode 100644 (file)
index 0000000..43875eb
--- /dev/null
@@ -0,0 +1,868 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the GNU General Public License version 2 ("the License").
+// You may not use this file except in compliance with the License.
+
+package org.ibex.graphics;
+import java.util.*;
+import java.util.collections.*;
+import org.ibex.util.*;
+
+/**
+ *  An incremental, adaptive, constrained Delaunay Triangulation.
+ *  @see Kallmann, Bieri, and Thalmann: Fully Dynamic Constrained Delaunay Triangulations
+ */
+public final class Mesh {
+
+    private static final double epsilon = (double)0.00000001;
+    private static final boolean debug = true;
+
+    private              Vector vertices = new Vector();
+    private              Vector triangles = new Vector();
+
+    public static double B = 5;
+    public static double Q = 0.4;
+
+    // Utilities //////////////////////////////////////////////////////////////////////////////
+
+    public static double distance(double x1, double y1, double x2, double y2) {
+        return (double)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
+    }
+    public static int side(Vertex v1, Vertex v2, Vertex v3) { return side(v1, v2, v3.x, v3.y); }
+    public static int side(Vertex v1, Vertex v2, double x, double y) { return side(v1, v2.x, v2.y, x, y); }
+    public static int side(Vertex v1, double x, double y, Vertex v3) { return side(v1, x, y, v3.x, v3.y); }
+    public static int side(Vertex v1, double x, double y, double x2, double y2) {
+        double a = y-v1.y, b = v1.x-x, c = -1 * (a*v1.x+b*v1.y);
+        return (- (a*x2+c)/b > y2) ? -1 : (- (a*x2+c)/b < y2) ? 1 : 0;
+    }
+
+    public boolean isect(Vertex v1, Vertex v2, Vertex v3, Vertex v4) {
+        if (v1==v3 || v1==v4 || v2==v3 || v2==v4) return false;
+        double a = side(v1,v2,v3);
+        double b = side(v1,v2,v4);
+        double c = side(v3,v4,v1);
+        double d = side(v3,v4,v2);
+        return a!=b && c!=d && a!=0 && b!=0 && c!=0 && d!=0;
+    }
+
+    private void drawLine(PixelBuffer buf, double x1, double y1, double x2, double y2, Affine a, int color) {
+        buf.drawLine((int)a.multiply_px((float)x1, (float)y1),
+                     (int)a.multiply_py((float)x1, (float)y1),
+                     (int)a.multiply_px((float)x2, (float)y2),
+                     (int)a.multiply_py((float)x2, (float)y2),
+                     color);
+    }
+    public void fill(PixelBuffer buf, Affine a, int color, boolean evenOdd, boolean strokeOnly) {
+        for (int i=0; i<triangles.size(); i++) ((Triangle)triangles.elementAt(i)).clear();
+        ((Triangle)triangles.elementAt(0)).fill(buf, a, color, evenOdd, 1, strokeOnly);
+    }
+    public void stroke(PixelBuffer buf, Affine a, int color) {
+        if (debug)
+            for (int i=0; i<vertices.size(); i++) {
+                Vertex v = (Vertex)vertices.elementAt(i);
+                for(Iterator it = v.next.iterator(); it.hasNext();) {
+                    Vertex v2 = (Vertex)it.next();
+                    drawLine(buf, v.x, v.y, v2.x, v2.y, a, 0xffff0000);
+                }
+            }
+        for (int i=0; i<triangles.size(); i++) {
+            Triangle t = ((Triangle)triangles.elementAt(i));
+            Vertex v1 = t.v1;
+            Vertex v2 = t.v2;
+            Vertex v3 = t.v3;
+            drawLine(buf, v1.x, v1.y, v2.x, v2.y, a, (debug && t.wind(v1,v2)!=0) ? 0xffffffff : color);
+            drawLine(buf, v3.x, v3.y, v2.x, v2.y, a, (debug && t.wind(v3,v2)!=0) ? 0xffffffff : color);
+            drawLine(buf, v1.x, v1.y, v3.x, v3.y, a, (debug && t.wind(v1,v3)!=0) ? 0xffffffff : color);
+        }
+    }
+
+    // Static //////////////////////////////////////////////////////////////////////////////
+
+    public Vertex newVertex(double x, double y) {
+        Vertex ret = null;
+        for(int k=0; k<vertices.size(); k++) {
+            Vertex v = (Vertex)vertices.elementAt(k);
+            if (v.distance(x,y)<epsilon) if (ret==null || v.distance(x,y) < ret.distance(x,y)) ret = v;
+        }
+        return ret==null ? new Vertex(x,y) : ret;
+    }
+
+    public Triangle newTriangle(Vertex v1, Vertex v2, Vertex v3, String source) {
+        if (v1==v2 || v2==v3 || v3==v1) throw new Error("identical vertices");
+        for(int i=0; i<triangles.size(); i++) {
+            Triangle t = (Triangle)triangles.elementAt(i);
+            if (t.v1==v1&&t.v2==v2&&t.v3==v3) return t;
+            if (t.v1==v1&&t.v2==v3&&t.v3==v2) return t;
+            if (t.v1==v2&&t.v2==v3&&t.v3==v1) return t;
+            if (t.v1==v2&&t.v2==v1&&t.v3==v3) return t;
+            if (t.v1==v3&&t.v2==v1&&t.v3==v2) return t;
+            if (t.v1==v3&&t.v2==v2&&t.v3==v1) return t;
+        }
+        Triangle t = new Triangle(source, v1, v2, v3, false, false);
+        if (debug) checktri();
+        return t;
+    }
+
+    public void checktri() {
+        for(int i=0; i<triangles.size(); i++) {
+            Triangle t = (Triangle)triangles.elementAt(i);
+                /*
+            for(int j=0; j<vertices.size(); j++) {
+                Vertex v = (Vertex)vertices.elementAt(j);
+                if (t.contains(v)) {
+                    t.special = true;
+                    throw new Error("triangle "+t+" contains vertex " + v);
+                }
+            }
+                */
+            if (t.t12 != null && (t.t12.t12 != t && t.t12.t23 != t && t.t12.t31 != t)) throw new Error("t12");
+            if (t.t23 != null && (t.t23.t12 != t && t.t23.t23 != t && t.t23.t31 != t)) throw new Error("t23");
+            if (t.t31 != null && (t.t31.t12 != t && t.t31.t23 != t && t.t31.t31 != t)) throw new Error("t31");
+            for(int j=0; j<triangles.size(); j++) {
+                Triangle t2 = (Triangle)triangles.elementAt(j);
+                if (t2==t) continue;
+                if (t.v1==t2.v1&&t.v2==t2.v2&&t.v3==t2.v3) throw new Error("poo");
+                if (t.v1==t2.v1&&t.v2==t2.v3&&t.v3==t2.v2) throw new Error("poo");
+                if (t.v1==t2.v2&&t.v2==t2.v3&&t.v3==t2.v1) throw new Error("poo");
+                if (t.v1==t2.v2&&t.v2==t2.v1&&t.v3==t2.v3) throw new Error("poo");
+                if (t.v1==t2.v3&&t.v2==t2.v1&&t.v3==t2.v2) throw new Error("poo");
+                if (t.v1==t2.v3&&t.v2==t2.v2&&t.v3==t2.v1) throw new Error("poo");
+            }
+        }
+    }
+
+    // Vertex //////////////////////////////////////////////////////////////////////////////
+
+    private final class Vertex {
+        final double x, y;
+        LinkedList next = new LinkedList();
+        boolean forced = false;
+        public String toString() { return "("+x+","+y+")"; }
+        public double distance(Vertex v) { return distance(v.x, v.y); }
+        public double distance(double x, double y) { return Mesh.distance(this.x,this.y,x,y); }
+        public Vertex(double x, double y) {
+            this.x = x;
+            this.y = y;
+            vertices.addElement(this);
+            Vector vec = new Vector();
+            Vector vec2 = new Vector();
+            boolean interior = false;
+            int edges = 0;
+            Vertex v = this;
+            boolean good = false;
+            for(int i=0; i<triangles.size(); i++) {
+                Triangle t = (Triangle)triangles.elementAt(i);
+                if (good) throw new Error("impossible: Vertex() -> good twice");
+                if (t.contains(this)) {
+                    System.out.println(t + " contains me");
+                    Triangle t12 = t.t12;
+                    Triangle t23 = t.t23;
+                    Triangle t31 = t.t31;
+                    t.delete(vec);
+                    vec2.addElement(newTriangle(this, t.v1, t.v2, "vertexinsertion " + this));
+                    vec2.addElement(newTriangle(this, t.v3, t.v2, "vertexinsertion " + this));
+                    vec2.addElement(newTriangle(this, t.v1, t.v3, "vertexinsertion " + this));
+                    fixup(vec2);
+                    good = true;
+                    break;
+                } else if (t.t23 != null && v.intersectsSegment(t.v2, t.v3)) {
+                    System.out.println("on an edge");
+                    good = true;
+                    Triangle tt = t.t23;
+                    tt.delete(null);
+                    t.delete(null);
+                    Vertex va =
+                        ((tt.v1==t.v3&&tt.v2==t.v2)||(tt.v1==t.v2&&tt.v2==t.v3)) ? tt.v3 :
+                        ((tt.v2==t.v3&&tt.v3==t.v2)||(tt.v2==t.v2&&tt.v3==t.v3)) ? tt.v1 :
+                        ((tt.v1==t.v3&&tt.v3==t.v2)||(tt.v1==t.v2&&tt.v3==t.v3)) ? tt.v2 : null;
+                    vec2.addElement(newTriangle(this, t.v1, t.v3, "vertexinsertion " + this));
+                    vec2.addElement(newTriangle(this, t.v1, t.v2, "vertexinsertion " + this));
+                    vec2.addElement(newTriangle(this, t.v3, va  , "vertexinsertion " + this));
+                    vec2.addElement(newTriangle(this, t.v2, va  , "vertexinsertion " + this));
+                    fixup(vec2);
+                    break;
+                } else if (t.t31 != null && v.intersectsSegment(t.v1, t.v3)) {
+                    good = true;
+                    System.out.println("on an edge");
+                    Triangle tt = t.t31;
+                    tt.delete(null);
+                    t.delete(null);
+                    Vertex va =
+                        ((tt.v1==t.v1&&tt.v2==t.v3)||(tt.v1==t.v3&&tt.v2==t.v1)) ? tt.v3 :
+                        ((tt.v2==t.v1&&tt.v3==t.v3)||(tt.v2==t.v3&&tt.v3==t.v1)) ? tt.v1 :
+                        ((tt.v1==t.v1&&tt.v3==t.v3)||(tt.v1==t.v3&&tt.v3==t.v1)) ? tt.v2 : null;
+                    vec2.addElement(newTriangle(this, t.v2, t.v1, "vertexinsertion " + this));
+                    vec2.addElement(newTriangle(this, t.v2, t.v3, "vertexinsertion " + this));
+                    vec2.addElement(newTriangle(this, t.v1, va  , "vertexinsertion " + this));
+                    vec2.addElement(newTriangle(this, t.v3, va  , "vertexinsertion " + this));
+                    fixup(vec2);
+                    break;
+                } else if (t.t12 != null && v.intersectsSegment(t.v1, t.v2)) {
+                    System.out.println("on an edge");
+                    good = true;
+                    Triangle tt = t.t12;
+                    tt.delete(null);
+                    t.delete(null);
+                    Vertex va =
+                        ((tt.v1==t.v1&&tt.v2==t.v2)||(tt.v1==t.v2&&tt.v2==t.v1)) ? tt.v3 :
+                        ((tt.v2==t.v1&&tt.v3==t.v2)||(tt.v2==t.v2&&tt.v3==t.v1)) ? tt.v1 :
+                        ((tt.v1==t.v1&&tt.v3==t.v2)||(tt.v1==t.v2&&tt.v3==t.v1)) ? tt.v2 : null;
+                    vec2.addElement(newTriangle(this, t.v3, t.v1, "vertexinsertion " + this));
+                    vec2.addElement(newTriangle(this, t.v3, t.v2, "vertexinsertion " + this));
+                    vec2.addElement(newTriangle(this, t.v1, va  , "vertexinsertion " + this));
+                    vec2.addElement(newTriangle(this, t.v2, va  , "vertexinsertion " + this));
+                    fixup(vec2);
+                    break;
+                }
+            }
+            if (triangles.size() > 2 && !good)
+                throw new Error("vertex "+this+" did not fall within a triangle or on the edge of at least two");
+        }
+        public void fixup(Vector vec) {
+            while(vec.size() > 0) {
+                Triangle t = (Triangle)vec.lastElement();
+                vec.setSize(vec.size() - 1);
+                if (t.deleted) continue;
+                Triangle to = t.v1==this ? t.t23 : t.v2==this ? t.t31 : t.v3==this ? t.t12 : null;
+                if (to==null)  continue;
+                    //throw new Error("fixup invoked with a triangle which does not have me as a vertex");
+                if (to.deleted) throw new Error("this should not happen");
+                if (to.r < this.distance(to.ccx, to.ccy)) continue;
+                if (t.v1==this) {
+                    //if (t.v2.next.contains(t.v3)) continue;
+                    //if (t.v3.next.contains(t.v2)) continue;
+                    Vertex v =
+                        (to.v1!=t.v2 && to.v1!=t.v3) ? to.v1 :
+                        (to.v2!=t.v2 && to.v2!=t.v3) ? to.v2 :
+                        (to.v3!=t.v2 && to.v3!=t.v3) ? to.v3 : null;
+                    t.delete(null);
+                    to.delete(null);
+                    vec.addElement(newTriangle(this, t.v2, v, "fixup"));
+                    vec.addElement(newTriangle(this, t.v3, v, "fixup"));
+                } else if (t.v2==this) {
+                    //if (t.v3.next.contains(t.v1)) continue;
+                    //if (t.v1.next.contains(t.v3)) continue;
+                    Vertex v =
+                        (to.v1!=t.v1 && to.v1!=t.v3) ? to.v1 :
+                        (to.v2!=t.v1 && to.v2!=t.v3) ? to.v2 :
+                        (to.v3!=t.v1 && to.v3!=t.v3) ? to.v3 : null;
+                    t.delete(null);
+                    to.delete(null);
+                    vec.addElement(newTriangle(this, t.v1, v, "fixup"));
+                    vec.addElement(newTriangle(this, t.v3, v, "fixup"));
+                    
+                } else if (t.v3==this) {
+                    //if (t.v1.next.contains(t.v2)) continue;
+                    //if (t.v2.next.contains(t.v1)) continue;
+                    Vertex v =
+                        (to.v1!=t.v2 && to.v1!=t.v1) ? to.v1 :
+                        (to.v2!=t.v2 && to.v2!=t.v1) ? to.v2 :
+                        (to.v3!=t.v2 && to.v3!=t.v1) ? to.v3 : null;
+                    t.delete(null);
+                    to.delete(null);
+                    vec.addElement(newTriangle(this, t.v2, v, "fixup"));
+                    vec.addElement(newTriangle(this, t.v1, v, "fixup"));
+                } else {
+                    throw new Error("this should not happen");
+                }
+                System.out.println("> made a swap");
+            }
+        }
+
+        public boolean makeNonDegenerate(Vector vec) {
+            Vertex v1 = this;
+            for(int i2 = 0; i2<next.size(); i2++) {
+                Vertex v2 = (Vertex)next.get(i2);
+                for(int i3 = 0; i3<vertices.size(); i3++) {
+                    Vertex v3 = (Vertex)vertices.get(i3);
+                    for(int i4=0; i4<v3.next.size(); i4++) {
+                        Vertex v4 = (Vertex)v3.next.get(i4);
+                        if (v1==v3||v1==v4||v2==v3||v2==v4) continue;
+                        if (isect(v1,v2,v3,v4)) {
+                            double a1 = v2.y-v1.y;
+                            double a2 = v4.y-v3.y;
+                            double b1 = v1.x-v2.x;
+                            double b2 = v3.x-v4.x;
+                            double c1 = -1 * (a1*v1.x+b1*v1.y);
+                            double c2 = -1 * (a2*v3.x+b2*v3.y);
+                            // a1x+b1y+c1=0
+                            // y=-(a1x+c1)/b1
+                            // a2x-(a1x+c1)(b2/b1)+c2=0
+                            // a2b1x-b2(a1x+c1)+b1c2=0
+                            // a2b1x-a1b2x-b2c1+b1c2=0
+                            // x(a2b1-a1b2)-b2c1+b1c2=0
+                            // x(a2b1-a1b2)=b2c1-b1c2
+                            // x=(b2c1-b1c2)/(a2b1-a1b2)
+                            double xq = (b2*c1-c2*b1)/(b1*a2-b2*a1);
+                            Vertex v0 = newVertex(xq, -1 * (a1*xq+c1) / b1);
+                            //if (v0==v1||v0==v2||v0==v3||v0==v4) continue;
+                            System.out.println("inserting new vertex " + v0+" between " + v1+"-"+v2 +" and "+ v3+"-"+v4);
+                            v1.next.remove(v2);
+                            v1.next.add(v0);
+                            v0.next.add(v2);
+                            v3.next.remove(v4);
+                            v3.next.add(v0);
+                            v0.next.add(v4);
+                            return true;
+                        }
+                    }
+                }
+            }
+            return false;
+        }
+
+        public boolean force() {
+            if (forced) return false;
+            forced = true;
+            boolean ret = false;
+            OUTER: for(Iterator it=next.iterator(); it.hasNext();) {
+                Vertex v = (Vertex)it.next();
+
+                for(int k=0; k<vertices.size(); k++) {
+                    Vertex v2 = (Vertex)vertices.elementAt(k);
+                    if (v2==this||v2==v) continue;
+                    //if (next.contains(v2) || v2.next.contains(this)) continue;
+                    if (v2.intersectsSegment(v,this)) {
+                        System.out.println("breaking line " + this+"-"+v + " at vertex " + v2);
+                        next.remove(v);
+                        next.add(v2);
+                        v2.next.add(v);
+                        ret = true;
+                        break OUTER;
+                    }
+                }
+                boolean good = false;
+                for (int i=0; i<triangles.size(); i++) {
+                    Triangle t = ((Triangle)triangles.elementAt(i));
+                    if (t.v1==this && t.v2==v) { good = true; break; }
+                    if (t.v3==this && t.v1==v) { good = true; break; }
+                    if (t.v3==this && t.v2==v) { good = true; break; }
+                    if (t.v1==v && t.v2==this) { good = true; break; }
+                    if (t.v3==v && t.v1==this) { good = true; break; }
+                    if (t.v3==v && t.v2==this) { good = true; break; }
+                }
+                if (good) continue;
+                HashSet orphans = new HashSet();
+                for (int i=0; i<triangles.size(); i++) {
+                    Triangle t = ((Triangle)triangles.elementAt(i));
+                    if (t.deleted) continue;
+                    if (t.intersectsSegment(this, v)) {
+                        System.out.println("removing triangle " + t + " because it intersects segment " + this+"-"+v);
+                        Vector vec = new Vector();
+                        orphans.add(t.v1);
+                        orphans.add(t.v2);
+                        orphans.add(t.v3);
+                        orphans.remove(v);
+                        orphans.remove(this);
+                        t.delete(vec);
+                        i--;
+                        ret = true;
+                    }
+                }
+                orphans.remove(this);
+                orphans.remove(v);
+                triangulate(orphans, v);
+                break;
+            }
+            return ret;
+        }
+        public void triangulate(HashSet orphans, Vertex v) {
+            HashSet left = new HashSet();
+            HashSet right = new HashSet();
+            for(Iterator it=orphans.iterator(); it.hasNext();) {
+                Vertex o = (Vertex)it.next();
+                if (o==v||o==this) continue;
+                if (side(this, v, o) == -1) left.add(o);
+                else if (side(this, v, o) == 1) right.add(o);
+                else throw new Error("impossible "+this+" "+v + " " + o);
+            }
+            triangulate2(left, v, false);
+            v.triangulate2(right, this, false);
+        }
+        public boolean triangulate2(HashSet orphans, Vertex v, boolean skip) {
+            Vertex farthest = null;
+            double dist = 0;
+            System.out.println("orphans: " + orphans.size());
+            if (orphans.size()==0) return true;
+            Vertex o = (Vertex)orphans.iterator().next();
+            if (orphans.size()==1) {
+                Triangle t = newTriangle(this, v, o, "triangulate2 " + v + " " + this);
+                if (((t.v1==this && t.v2==v)||(t.v1==v && t.v2==this)) && t.t12==null) return true;
+                if (((t.v3==this && t.v2==v)||(t.v3==v && t.v2==this)) && t.t23==null) return true;
+                if (((t.v1==this && t.v3==v)||(t.v1==v && t.v3==this)) && t.t31==null) return true;
+                return false;
+            }
+
+            Vertex best = null;
+            OUTER: for(Iterator it=orphans.iterator(); it.hasNext();) {
+                o = (Vertex)it.next();
+                if (o==v) continue;
+                Triangle t = new Triangle("temp", this, v, o, true);
+                for(Iterator it2=orphans.iterator(); it2.hasNext();) {
+                    Vertex z = (Vertex)it2.next();
+                    if (z==o) continue;
+                    if (z==v) continue;
+                    if (z==this) continue;
+                    if (z.distance(t.ccx, t.ccy) < t.r || t.contains(z)) continue OUTER;
+                }
+                if (best==null || new Triangle("temp",this,v,best,true).area() > new Triangle("temp",this,v,o,true).area())
+                    best = o;
+            }
+            if (best==null) throw new Error("notgood, #orphans = " + orphans.size());
+            o = best;
+            HashSet left = new HashSet();
+            HashSet right = new HashSet();
+            for(Iterator it2=orphans.iterator(); it2.hasNext();) {
+                Vertex x = (Vertex)it2.next();
+                if (o==x||this==x||v==x) continue;
+                int side_of_x    = side(o,(this.x+v.x)/2,(this.y+v.y)/2,x);
+                int side_of_v    = side(o,(this.x+v.x)/2,(this.y+v.y)/2,v);
+                int side_of_this = side(o,(this.x+v.x)/2,(this.y+v.y)/2,this);
+                if (side_of_x==side_of_this && side_of_this!=0) right.add(x);
+                else if (side_of_x==side_of_v && side_of_v!=0) left.add(x);
+                else throw new Error("impossible "+this+" "+v + " " + o + " ==> " + x);
+            }
+            boolean doit = true;
+            doit &= o.triangulate2(left, v, false);
+            doit &= o.triangulate2(right, this, false);
+            if (doit) {
+                Triangle t = newTriangle(v, this, o,"triangulate 0/0");
+                if (((t.v1==this && t.v2==v)||(t.v1==v && t.v2==this)) && t.t12==null) return true;
+                if (((t.v3==this && t.v2==v)||(t.v3==v && t.v2==this)) && t.t23==null) return true;
+                if (((t.v1==this && t.v3==v)||(t.v1==v && t.v3==this)) && t.t31==null) return true;
+                return false;
+            }
+            return true;
+        }
+
+        public int wind(Vertex v) {
+            if (v.next.contains(this) && next.contains(v)) return 0;
+            if (v.y < y || (v.y==y && v.x<x)) return v.wind(this);
+            if (next.contains(v)) return 1;
+            if (v.next.contains(this)) return -1;
+            return 0;
+        }
+        public boolean intersectsSegment(Vertex v1, Vertex v2) {
+            return
+                side(v1,v2,this)==0 &&
+                x - Math.max(v1.x,v2.x) < 0 &&
+                x - Math.min(v1.x,v2.x) > 0 &&
+                y - Math.max(v1.y,v2.y) < 0 &&
+                y - Math.min(v1.y,v2.y) > 0;
+        }
+    }
+
+    // Triangle //////////////////////////////////////////////////////////////////////////////
+
+    private final class Triangle {
+        Triangle t12=null, t23=null, t31=null;
+        final Vertex v1, v2, v3;
+        boolean in = false;
+        boolean deleted = false;
+        boolean painted = false;
+        boolean special = false;
+        final String source;
+        final double ccx, ccy, r;
+
+        public String toString() { return "<<"+v1+""+v2+""+v3+">>"; }
+        public boolean intersectsSegment(Vertex a, Vertex b) { return isect(v1,v2,a,b) || isect(v3,v2,a,b) || isect(v1,v3,a,b); }
+        public double area() {
+            double a = v1.distance(v2);
+            double b = v3.distance(v2);
+            double c = v1.distance(v3);
+            double s = (a+b+c)/2;
+            return Math.sqrt(s*(s-a)*(s-b)*(s-c));
+        }
+
+        public boolean checkSkinny() {
+            if (!(r / Math.min(Math.min(v1.distance(v2), v2.distance(v3)), v3.distance(v1)) > B)) return false;
+            System.out.println("skinny triangle " + this);
+            for (Iterator it = vertices.iterator(); it.hasNext();) {
+                Vertex v = (Vertex)it.next();
+                for (Iterator nit = v.next.iterator(); nit.hasNext();) {
+                    Vertex v2 = (Vertex)nit.next();
+                    double midx = (v.x+v2.x)/2;
+                    double midy = (v.y+v2.y)/2;
+                    if (distance(midx,midy,ccx,ccy) <= v.distance(midx,midy)) {
+                        System.out.println("  but splitting it would encroach on a segment");
+                        return false;
+                    }
+                }
+            }
+            newVertex(ccx, ccy);
+            return true;
+        }
+
+        public void fill(PixelBuffer buf, Affine a, int color, boolean evenOdd, int count, boolean strokeOnly) {
+            if (painted) return;
+            if (deleted) throw new Error("gargh!");
+            if (!strokeOnly) {
+                if (special) {
+                    buf.fillTriangle((int)a.multiply_px((float)v1.x, (float)v1.y),
+                                     (int)a.multiply_py((float)v1.x, (float)v1.y),
+                                     (int)a.multiply_px((float)v2.x, (float)v2.y),
+                                     (int)a.multiply_py((float)v2.x, (float)v2.y),
+                                     (int)a.multiply_px((float)v3.x, (float)v3.y),
+                                     (int)a.multiply_py((float)v3.x, (float)v3.y),
+                                     0xff0000ff);
+                } else if ((evenOdd && count%2!=0) || (!evenOdd && count!=0)) {
+                    buf.fillTriangle((int)a.multiply_px((float)v1.x, (float)v1.y),
+                                     (int)a.multiply_py((float)v1.x, (float)v1.y),
+                                     (int)a.multiply_px((float)v2.x, (float)v2.y),
+                                     (int)a.multiply_py((float)v2.x, (float)v2.y),
+                                     (int)a.multiply_px((float)v3.x, (float)v3.y),
+                                     (int)a.multiply_py((float)v3.x, (float)v3.y),
+                                     color);
+                } else {
+                    buf.fillTriangle((int)a.multiply_px((float)v1.x, (float)v1.y),
+                                     (int)a.multiply_py((float)v1.x, (float)v1.y),
+                                     (int)a.multiply_px((float)v2.x, (float)v2.y),
+                                     (int)a.multiply_py((float)v2.x, (float)v2.y),
+                                     (int)a.multiply_px((float)v3.x, (float)v3.y),
+                                     (int)a.multiply_py((float)v3.x, (float)v3.y),
+                                     0xff000000 | ((color & 0x00ffffff) >> 8));
+                }
+            }
+            painted = true;
+            if (t12 != null && !t12.painted) {
+                t12.fill(buf, a, color, evenOdd, count + wind(v1, v2), strokeOnly);
+                drawLine(buf, (v1.x*2+v2.x)/3, (v1.y*2+v2.y)/3, (v1.x+2*v2.x)/3, (v1.y+2*v2.y)/3, a,
+                         wind(v2,v1)==0?0xff000000:0xff0000ff);
+            }
+            if (t23 != null && !t23.painted) {
+                t23.fill(buf, a, color, evenOdd, count + wind(v2, v3), strokeOnly);
+                drawLine(buf, (v2.x*2+v3.x)/3, (v2.y*2+v3.y)/3, (v2.x+2*v3.x)/3, (v2.y+2*v3.y)/3, a,
+                         wind(v3,v2)==0?0xff000000:0xff0000ff);
+            }
+            if (t31 != null && !t31.painted) {
+                t31.fill(buf, a, color, evenOdd, count + wind(v3, v1), strokeOnly);
+                drawLine(buf, (v1.x*2+v3.x)/3, (v1.y*2+v3.y)/3, (v1.x+2*v3.x)/3, (v1.y+2*v3.y)/3, a,
+                         wind(v3,v1)==0?0xff000000:0xff0000ff);
+            }
+        }
+        public int wind(Vertex a, Vertex b) { return a.wind(b); }
+        public void clear() {
+            if (!painted) return;
+            painted = false;
+            if (t12 != null) t12.clear();
+            if (t23 != null) t23.clear();
+            if (t31 != null) t31.clear();
+        }
+        public Triangle(String source, Vertex v1, Vertex v2, Vertex v3) { this(source, v1, v2, v3, false, false); }
+        public Triangle(String source, Vertex v1, Vertex v2, Vertex v3, boolean fake) { this(source, v1,v2,v3,fake,false); }
+        public Triangle(String source, Vertex v1, Vertex v2, Vertex v3, boolean fake, boolean ignoreProblems) {
+            this.source = source;
+            double a=(v2.y-v3.y)*(v2.x-v1.x)-(v2.y-v1.y)*(v2.x-v3.x);
+            if (a==0) {
+                Vertex t = v2; v2=v3; v3=v1; v1 = t; 
+                a=(v2.y-v3.y)*(v2.x-v1.x)-(v2.y-v1.y)*(v2.x-v3.x);
+                if (a==0) {
+                    t = v2; v2=v3; v3=v1; v1 = t; 
+                    a=(v2.y-v3.y)*(v2.x-v1.x)-(v2.y-v1.y)*(v2.x-v3.x);
+                }
+                if (a==0) {
+                    t = v2; v2=v3; v3=v1; v1 = t; 
+                    a=(v2.y-v3.y)*(v2.x-v1.x)-(v2.y-v1.y)*(v2.x-v3.x);
+                }
+                if (a==0) {
+                    t = v1; v1=v3; v3=t;
+                    a=(v2.y-v3.y)*(v2.x-v1.x)-(v2.y-v1.y)*(v2.x-v3.x);
+                }
+                if (a==0) {
+                    t = v2; v2=v3; v3=v1; v1 = t; 
+                    a=(v2.y-v3.y)*(v2.x-v1.x)-(v2.y-v1.y)*(v2.x-v3.x);
+                }
+                if (a==0) {
+                    t = v2; v2=v3; v3=v1; v1 = t; 
+                    a=(v2.y-v3.y)*(v2.x-v1.x)-(v2.y-v1.y)*(v2.x-v3.x);
+                }
+                if (a==0) {
+                    t = v2; v2=v3; v3=v1; v1 = t; 
+                    a=(v2.y-v3.y)*(v2.x-v1.x)-(v2.y-v1.y)*(v2.x-v3.x);
+                }
+            }
+            if (a==0) throw new Error("a==0 for " + v1 + " " + v2 + " " + v3);
+            this.v1 = v1; this.v2 = v2; this.v3 = v3;
+            double a1=(v1.x+v2.x)*(v2.x-v1.x)+(v2.y-v1.y)*(v1.y+v2.y);
+            double a2=(v2.x+v3.x)*(v2.x-v3.x)+(v2.y-v3.y)*(v2.y+v3.y);
+            ccx=(a1*(v2.y-v3.y)-a2*(v2.y-v1.y))/a/2;
+            ccy=(a2*(v2.x-v1.x)-a1*(v2.x-v3.x))/a/2;
+            r = v1.distance(ccx, ccy);
+            if (fake) return;
+            for(int i=0; i<triangles.size(); i++) {
+                Triangle t = (Triangle)triangles.elementAt(i);
+                if ((t.v1==v1&&t.v2==v2)||(t.v1==v2&&t.v2==v1)) {
+                    if (t.t12!=null && !ignoreProblems) {
+                        System.out.println("v1=="+v1);
+                        System.out.println("v2=="+v2);
+                        System.out.println("t.v3=="+t.v3);
+                        System.out.println("v3=="+v3);
+                        special = true;
+                        t.special = true;
+                        triangles.add(this);
+                        System.out.println(t.t12.source);
+                        throw new Error();
+                        //t.t12.delete(null);
+                    }
+                    t.t12=this;
+                    t12=t;
+                    //continue;
+                }
+                if ((t.v1==v2&&t.v2==v3)||(t.v1==v3&&t.v2==v2)) {
+                    if (t.t12!=null && !ignoreProblems) {
+                        special = true;
+                        t.special = true;
+                        System.out.println("v2=="+v2);
+                        System.out.println("v3=="+v3);
+                        System.out.println("t.v3=="+t.v3);
+                        System.out.println("v1=="+v1);
+                        System.out.println("t.t12.v1=="+t.t12.v1);
+                        System.out.println("t.t12.v2=="+t.t12.v2);
+                        System.out.println("t.t12.v3=="+t.t12.v3);
+                        special = true;
+                        t.special = true;
+                        triangles.add(this);
+                        throw new Error();
+                        //t.t12.delete(null);
+                    }
+                    t.t12=this;
+                    t23=t;
+                    //continue;
+                }
+                if ((t.v1==v3&&t.v2==v1)||(t.v1==v1&&t.v2==v3)) {
+                    if (t.t12!=null && !ignoreProblems) {
+                        special = true;
+                        t.special = true;
+                        System.out.println("v1=="+v1);
+                        System.out.println("v3=="+v3);
+                        System.out.println("t.v3=="+t.v3);
+                        System.out.println("v2=="+v2);
+                        special = true;
+                        t.special = true;
+                        triangles.add(this);
+                        throw new Error();
+                        //t.t12.delete(null);
+                    }
+                    t.t12=this;
+                    t31=t;
+                    //continue;
+                }
+                if ((t.v2==v1&&t.v3==v2)||(t.v2==v2&&t.v3==v1)) {
+                    if (t.t23!=null && !ignoreProblems) {
+                        t = t.t23;
+                        System.out.println("v1=="+v1);
+                        System.out.println("v2=="+v2);
+                        System.out.println("t.v1=="+t.v1);
+                        System.out.println("v3=="+v3);
+                        special = true;
+                        t.special = true;
+                        triangles.add(this);
+                        System.out.println(t.source);
+                        throw new Error();
+                        //t.t23.delete(null);
+                    }
+                    t.t23=this;
+                    t12=t;
+                    //continue;
+                }
+                if ((t.v2==v2&&t.v3==v3)||(t.v2==v3&&t.v3==v2)) {
+                    if (t.t23!=null && !ignoreProblems) {
+                        special = true;
+                        t=t.t23;
+                        t.special = true;
+                        triangles.add(this);
+                        System.out.println(t.source);
+                        throw new Error();
+                        //t.t23.delete(null);
+                    }
+                    t.t23=this;
+                    t23=t;
+                    //continue;
+                }
+                if ((t.v2==v3&&t.v3==v1)||(t.v2==v1&&t.v3==v3)) {
+                    if (t.t23!=null && !ignoreProblems) {
+                        special = true;
+                        t = t.t23;
+                        t.special = true;
+                        System.out.println("v1=="+v1);
+                        System.out.println("v3=="+v3);
+                        System.out.println("t.v3=="+t.v3);
+                        System.out.println("v2=="+v2);
+                        special = true;
+                        t.special = true;
+                        System.out.println(t.source);
+                        triangles.add(this);
+                        throw new Error();
+                        //t.t23.delete(null);
+                    }
+                    t.t23=this;
+                    t31=t;
+                    //continue;
+                }
+                if ((t.v3==v1&&t.v1==v2)||(t.v3==v2&&t.v1==v1)) {
+                    if (t.t31!=null && !ignoreProblems) {
+                        t=t.t31;
+                        special = true;
+                        t.special = true;
+                        System.out.println("v1=="+v1);
+                        System.out.println("v2=="+v2);
+                        System.out.println("t.v2=="+t.v2);
+                        System.out.println("v3=="+v3);
+                        triangles.add(this);
+                        System.out.println(t.source);
+                        throw new Error();
+                        //t.t31.delete(null);
+                    }
+                    t.t31=this;
+                    t12=t;
+                    //continue;
+                }
+                if ((t.v3==v2&&t.v1==v3)||(t.v3==v3&&t.v1==v2)) {
+                    if (t.t31!=null && !ignoreProblems) {
+                        special = true;
+                        t=t.t31;
+                        t.special = true;
+                        triangles.add(this);
+                        System.out.println(t.source);
+                        throw new Error();
+                        //t.t31.delete(null);
+                    }
+                    t.t31=this;
+                    t23=t;
+                    //continue; 
+                }
+                if ((t.v3==v3&&t.v1==v1)||(t.v3==v1&&t.v1==v3)) {
+                    if (t.t31!=null && !ignoreProblems) {
+                        t=t.t31;
+                        System.out.println("t.v1="+t.v1);
+                        System.out.println("t.v2="+t.v2);
+                        System.out.println("t.v3="+t.v3);
+                        System.out.println("v1="+v1);
+                        System.out.println("v2="+v2);
+                        System.out.println("v3="+v3);
+                        special = true;
+                        t.special = true;
+                        triangles.add(this);
+                        System.out.println("source=="+source);
+                        System.out.println("t.source=="+t.source);
+                        throw new Error();
+                        //t.t31.delete(null);
+                    }
+                    t.t31=this;
+                    t31=t;
+                    //continue;
+                }
+            }
+            triangles.add(this);
+        }
+        public void delete(Triangle t, Vertex v, Vector vec) {
+            if (deleted) return;
+            if (t12==t) { t12 = null; if (vec != null) { vec.add(v1); vec.add(v2); } }
+            if (t23==t) { t23 = null; if (vec != null) { vec.add(v2); vec.add(v3); } }
+            if (t31==t) { t31 = null; if (vec != null) { vec.add(v3); vec.add(v1); } }
+        }
+        public void delete(Vector vec) {
+            if (deleted) return;
+            deleted = true;
+            triangles.remove(this);
+            if (t12 != null) t12.delete(this,null,vec); else if (vec != null) { vec.add(v1); vec.add(v2); }
+            if (t23 != null) t23.delete(this,null,vec); else if (vec != null) { vec.add(v3); vec.add(v2); }
+            if (t31 != null) t31.delete(this,null,vec); else if (vec != null) { vec.add(v1); vec.add(v3); }
+        }
+        public boolean contains(Vertex v) {
+            double x = v.x;
+            double y = v.y;
+            if (v==v1) return false;
+            if (v==v2) return false;
+            if (v==v3) return false;
+            if (v.intersectsSegment(v1,v2)) return false;
+            if (v.intersectsSegment(v2,v3)) return false;
+            if (v.intersectsSegment(v3,v1)) return false;
+            int a = side(v1,v2,x,y);
+            int b = side(v3,v2,x,y);
+            int c = side(v1,v3,x,y);
+            return (a==side(v1,v2,v3)) && (b==side(v3,v2,v1)) && (c==side(v1,v3,v2));
+        }
+    }
+
+    // Constructor //////////////////////////////////////////////////////////////////////////////
+
+    public Mesh(Polygon p) {
+        p.sort();
+        triangles.setSize(0);
+        vertices.setSize(0);
+
+        newTriangle(newVertex(p.minx-10, p.miny-10),
+                    newVertex(p.maxx+10, p.miny-10),
+                    newVertex(p.maxx+10, p.maxy+10), "original0");
+
+        newTriangle(newVertex(p.minx-10, p.miny-10),
+                    newVertex(p.minx-10, p.maxy+10),
+                    newVertex(p.maxx+10, p.maxy+10), "original");
+
+        for(int i=0; i<p.numcontours-1; i++) {
+            Vertex last = null, first = null;
+            boolean good = false;
+            for(int j=p.contours[i]; j<p.contours[i+1]; j++) {
+                Vertex v = newVertex(p.x[j], p.y[j]);
+                if (v==last) continue;
+                if (v==first) continue;
+                if (last == null) { last = v; continue; }
+                if (first ==null) { first = v; continue; }
+                good = true;
+                break;
+            }
+            System.out.println("contour " + i + " is " + (good?"good":"bad"));
+            if (!good) continue;
+            last = null; first = null;
+            for(int j=p.contours[i]; j<p.contours[i+1]; j++) {
+                System.out.println("****************************************");
+                Vertex v = newVertex(p.x[j], p.y[j]);
+                if (v==last) continue;
+                if (first==null) first=v;
+                if (last != null) last.next.add(v);
+                last = v;
+                System.out.print(((int)v.x)+","+((int)v.y)+" ");
+            }
+            if (last!=first) last.next.add(first);
+            System.out.println();
+        }
+
+        while(true) {
+            boolean redo = false;
+            for(int k=0; k<vertices.size(); k++) {
+                Vector vec = new Vector();
+                Vertex vx = (Vertex)vertices.elementAt(k);
+                redo |= vx.makeNonDegenerate(vec);
+            }
+            if (!redo) break;
+        }
+
+        /*
+        redo = true;
+        for(; redo;) {
+            redo = false;
+            for(int k=0; k<triangles.size(); k++) {
+                Triangle t = (Triangle)triangles.elementAt(k);
+                redo |= t.checkSkinny();
+            }
+            break;
+        }
+        System.out.println("I now have " + vertices.size() + " vertices");
+        */
+
+        while(true) {
+            boolean redo = false;
+            for(int k=0; k<vertices.size(); k++) {
+                Vertex v = (Vertex)vertices.elementAt(k);
+                v.forced = false;
+            }
+            for(int k=0; k<vertices.size(); k++) {
+                Vertex v = (Vertex)vertices.elementAt(k);
+                redo |= v.force();
+            }
+            if (!redo) break;
+        }
+    }
+}
+
+