1817bea06261cd94c6cef4019832b9ac3c4a04b0
[anneal.git] / src / Geom.java
1 import java.awt.*;
2 import java.util.*;
3 import java.awt.event.*;
4 import javax.swing.*;
5 import javax.media.opengl.*;
6 import javax.media.opengl.glu.*;
7
8 public class Geom implements Iterable<Geom.T> {
9
10     public static float EPSILON = (float)0.000001;
11
12     private HashMap<P,P> ps = new HashMap<P,P>();
13     private HashMap<E,E> es = new HashMap<E,E>();
14     private HashSet<T>   ts = new HashSet<T>();
15
16     public Iterator<T> iterator() { return ts.iterator(); }
17
18     public P newP(float x, float y, float z) {
19         P p = new P(x, y, z);
20         P p2 = ps.get(p);
21         if (p2 != null) return p2;
22         ps.put(p,p);
23         return p;
24     }
25     
26     public E newE(P p1, P p2) {
27         E e = new E(p1, p2);
28         E e2 = es.get(e);
29         if (e2 != null) return e2;
30         es.put(e,e);
31         return e;
32     }
33
34     public T newT(E e1, E e2, E e3, V norm) {
35         P p1 = e1.shared(e2);
36         P p2 = e2.shared(e3);
37         P p3 = e3.shared(e1);
38         // FEATURE: colinearity check?
39         V norm2 = p2.minus(p1).cross(p3.minus(p1));
40         float dot = norm.dot(norm2);
41         if (Math.abs(dot) < EPSILON) throw new Error("dot products within epsilon of each other: "+norm+" "+norm2);
42         if (dot < 0) { E t = e1; e1 = e3; e2 = e2; e3 = t; }
43         if (e1.t1 != null && e1.t1.hasE(e1) && e1.t1.hasE(e2) && e1.t1.hasE(e3)) return e1.t1;
44         if (e1.t2 != null && e1.t2.hasE(e1) && e1.t2.hasE(e2) && e1.t2.hasE(e3)) return e1.t2;
45         if (e2.t1 != null && e2.t1.hasE(e1) && e2.t1.hasE(e2) && e2.t1.hasE(e3)) return e2.t1;
46         if (e2.t2 != null && e2.t2.hasE(e1) && e2.t2.hasE(e2) && e2.t2.hasE(e3)) return e2.t2;
47         if (e3.t1 != null && e3.t1.hasE(e1) && e3.t1.hasE(e2) && e3.t1.hasE(e3)) return e3.t1;
48         if (e3.t2 != null && e3.t2.hasE(e1) && e3.t2.hasE(e2) && e3.t2.hasE(e3)) return e3.t2;
49         T ret = new T(e1, e2, e3);
50         ts.add(ret);
51         return ret;
52     }
53
54     /** [UNIQUE] point in 3-space */
55     public final class P {
56         public final float x, y, z;
57         public P(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
58         public V minus(P p) { return new V(x-p.x, y-p.y, z-p.z); }
59         public P plus(V v) { return newP(x+v.x, y+v.y, z+v.z); }
60         public boolean equals(Object o) {
61             if (o==null || !(o instanceof P)) return false;
62             P p = (P)o;
63             return p.x==x && p.y==y && p.z==z;
64         }
65         public int hashCode() {
66             return
67                 Float.floatToIntBits(x) ^
68                 Float.floatToIntBits(y) ^
69                 Float.floatToIntBits(z);
70         }
71         public void glVertex(GL gl) { gl.glVertex3f(x, y, z); }
72         public String toString() { return "("+x+","+y+","+z+")"; }
73     }
74
75     /** vector in 3-space */
76     public final class V {
77         private final float x, y, z;
78         public V(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
79         public V cross(V v) { return new V(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
80         public V norm() { float m = mag(); return new V(x/m, y/m, z/m); }
81         public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
82         public float dot(V v) { return x*v.x + y*v.y + z*v.z; }
83         public V times(float mag) { return new V(x*mag, y*mag, z*mag); }
84         public String toString() { return "<"+x+","+y+","+z+">"; }
85     }
86
87     /** [UNIQUE] an edge */
88     public final class E {
89         public final P p1, p2;
90         T t1, t2;
91         public E(P p1, P p2) { this.p1 = p1; this.p2 = p2; }
92         public int hashCode() { return p1.hashCode() ^ p2.hashCode(); }
93         public float length() { return p1.minus(p2).mag(); }
94         public boolean equals(Object o) {
95             if (o==null || !(o instanceof E)) return false;
96             E e = (E)o;
97             if (this.p1 == e.p1 && this.p2 == e.p2) return true;
98             if (this.p2 == e.p1 && this.p1 == e.p2) return true;
99             return false;
100         }
101         public P shared(E e) {
102             if (p1==e.p1) return p1;
103             if (p1==e.p2) return p1;
104             if (p2==e.p1) return p2;
105             if (p2==e.p2) return p2;
106             throw new Error("no shared vertex in shared()");
107         }
108         public P unshared(E e) {
109             if (p1==e.p1) return p2;
110             if (p1==e.p2) return p2;
111             if (p2==e.p1) return p1;
112             if (p2==e.p2) return p1;
113             throw new Error("no shared vertex in unshared()");
114         }
115     }
116
117     /** [UNIQUE] a triangle (face) */
118     public final class T {
119         public final E e1, e2, e3;
120         public V norm() {
121             P p1 = e1.shared(e2);
122             P p2 = e2.shared(e3);
123             P p3 = e3.shared(e1);
124             return p2.minus(p1).cross(p3.minus(p1)).norm();
125         }
126         T(E e1, E e2, E e3) {
127             this.e1 = e1;
128             this.e2 = e2;
129             this.e3 = e3;
130             if (e1.t1 == null) e1.t1 = this; else if (e1.t2 == null) e1.t2 = this; else throw new Error("non-manifold surface");
131             if (e2.t1 == null) e2.t1 = this; else if (e2.t2 == null) e2.t2 = this; else throw new Error("non-manifold surface");
132             if (e3.t1 == null) e3.t1 = this; else if (e3.t2 == null) e3.t2 = this; else throw new Error("non-manifold surface");
133         }
134         public boolean hasE(E e) { return e1==e || e2==e || e3==e; }
135         public void glVertices(GL gl) {
136             p1().glVertex(gl);
137             p2().glVertex(gl);
138             p3().glVertex(gl);
139         }
140         public P p1() { return e1.shared(e2); }
141         public P p2() { return e1.shared(e3); }
142         public P p3() { return e3.shared(e2); }
143         public P centroid() { return newP((p1().x+p2().x+p3().x)/3,
144                                           (p1().y+p2().y+p3().y)/3, 
145                                           (p1().z+p2().z+p3().z)/3); }
146         public float diameter() {
147             // FIXME: what is this supposed to be?
148             return Math.max(Math.max(e1.length(), e2.length()), e3.length()) / 2;
149         }
150     }
151
152     /** matrix */
153     public class M {
154     }
155
156 }