+
+ public boolean intersects(T t) {
+ double A0=t.v1().p.x, A1=t.v1().p.y, A2=t.v1().p.z;
+ double B0=t.v2().p.x, B1=t.v2().p.y, B2=t.v2().p.z;
+ double C0=t.v3().p.x, C1=t.v3().p.y, C2=t.v3().p.z;
+ double j0=p1.p.x, j1=p1.p.y, j2=p1.p.z;
+ double k0=p2.p.x, k1=p2.p.y, k2=p2.p.z;
+ double J0, J1, J2;
+ double K0, K1, K2;
+ double i0, i1, i2;
+ double a0, a1, a2;
+ double b0, b1, b2;
+ double c0, c1, c2;
+ double in_det;
+ double R00, R01, R02, R03,
+ R10, R11, R12, R13,
+ R20, R21, R22, R23,
+ R30, R31, R32, R33;
+
+
+ /* a = B - A */
+ a0 = B0 - A0;
+ a1 = B1 - A1;
+ a2 = B2 - A2;
+ /* b = C - B */
+ b0 = C0 - A0;
+ b1 = C1 - A1;
+ b2 = C2 - A2;
+ /* c = a × b */
+ c0 = a1 * b2 - a2 * b1;
+ c1 = a2 * b0 - a0 * b2;
+ c2 = a0 * b1 - a1 * b0;
+
+ /* M^(-1) = (1/det(M)) * adj(M) */
+ in_det = 1 / (c0 * c0 + c1 * c1 + c2 * c2);
+ R00 = (b1 * c2 - b2 * c1) * in_det;
+ R01 = (b2 * c0 - b0 * c2) * in_det;
+ R02 = (b0 * c1 - b1 * c0) * in_det;
+ R10 = (c1 * a2 - c2 * a1) * in_det;
+ R11 = (c2 * a0 - c0 * a2) * in_det;
+ R12 = (c0 * a1 - c1 * a0) * in_det;
+ R20 = (c0) * in_det;
+ R21 = (c1) * in_det;
+ R22 = (c2) * in_det;
+
+ /* O = M^(-1) * A */
+ R03 = -(R00 * A0 + R01 * A1 + R02 * A2);
+ R13 = -(R10 * A0 + R11 * A1 + R12 * A2);
+ R23 = -(R20 * A0 + R21 * A1 + R22 * A2);
+
+ /* fill in last row of 4x4 matrix */
+ R30 = R31 = R32 = 0;
+ R33 = 1;
+
+ J2 = R20 * j0 + R21 * j1 + R22 * j2 + R23;
+ K2 = R20 * k0 + R21 * k1 + R22 * k2 + R23;
+ if (J2 * K2 >= 0) return false;
+
+ J0 = R00 * j0 + R01 * j1 + R02 * j2 + R03;
+ K0 = R00 * k0 + R01 * k1 + R02 * k2 + R03;
+ i0 = J0 + J2 * ((K0 - J0) / (J2 - K2));
+ if (i0 < 0 || i0 > 1) return false;
+
+ J1 = R10 * j0 + R11 * j1 + R12 * j2 + R13;
+ K1 = R10 * k0 + R11 * k1 + R12 * k2 + R13;
+ i1 = J1 + J2 * ((K1 - J1) / (J2 - K2));
+ if (i1 < 0 || i1 > 1 || i0 + i1 > 1) return false;
+
+ return true;
+ }
+ }
+
+ public E makeE(Point p1, Point p2) {
+ Vert v1 = pointset.get(p1);
+ Vert v2 = pointset.get(p2);
+ if (v1 != null && v2 != null) {
+ E e = v1.getE(v2);
+ if (e != null) return e;
+ e = v2.getE(v1);
+ if (e != null) return e;
+ }
+ if (v1 != null) return new E(v1.getFreeIncident(), p2);
+ if (v2 != null) return new E(v2.getFreeIncident(), p1).pair;
+ return new E(p1, p2);
+ }
+ public T newT(Point p1, Point p2, Point p3, Vec norm, int colorclass) {
+ if (norm != null) {
+ Vec norm2 = p3.minus(p1).cross(p2.minus(p1));
+ float dot = norm.dot(norm2);
+ //if (Math.abs(dot) < EPointSILON) throw new Error("dot products within evertsilon of each other: "+norm+" "+norm2);
+ if (dot < 0) { Point p = p1; p1=p2; p2 = p; }
+ }
+ E e12 = makeE(p1, p2);
+ E e23 = makeE(p2, p3);
+ E e31 = makeE(p3, p1);
+ while(e12.next != e23 || e23.next != e31 || e31.next != e12) {
+ e12.makeAdjacent(e23);
+ e23.makeAdjacent(e31);
+ e31.makeAdjacent(e12);
+ }
+ T ret = e12.makeT(colorclass);
+ if (e12.t == null) throw new Error();
+ if (e23.t == null) throw new Error();
+ if (e31.t == null) throw new Error();
+ return ret;
+ }
+
+
+ public class FaceIterator implements Iterator<T> {
+ private HashSet<T> visited = new HashSet<T>();
+ private LinkedList<T> next = new LinkedList<T>();
+ public FaceIterator() { }
+ public FaceIterator(Vert v) { next.addFirst(v.e.t); }
+ public boolean hasNext() { return next.peek()!=null; }
+ public void remove() { throw new Error(); }
+ public T next() {
+ T ret = next.removeFirst();
+ if (ret == null) return null;
+ visited.add(ret);
+ T t1 = ret.e1().pair.t;
+ T t2 = ret.e2().pair.t;
+ T t3 = ret.e3().pair.t;
+ if (t1 != null && !visited.contains(t1)) next.addFirst(t1);
+ if (t2 != null && !visited.contains(t2)) next.addFirst(t2);
+ if (t3 != null && !visited.contains(t3)) next.addFirst(t3);
+ return ret;
+ }