import java.awt.*;
+import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
-public class Geom implements GLEventListener {
+public class Geom implements Iterable<Geom.T> {
- public static StlFile stlf = null;
+ private HashMap<P,P> ps = new HashMap<P,P>();
+ private HashMap<E,E> es = new HashMap<E,E>();
+ private HashSet<T> ts = new HashSet<T>();
- /**
- * Take care of initialization here.
- */
- public void init(GLAutoDrawable gld) {
- GL gl = gld.getGL();
- GLU glu = new GLU();
- gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- gl.glViewport(0, 0, 500, 300);
- gl.glMatrixMode(GL.GL_PROJECTION);
- gl.glLoadIdentity();
- //glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0);
- display(gld);
- }
-
-
- /**
- * Take care of drawing here.
- */
- public void display(GLAutoDrawable drawable) {
- float red = 0.0f;
- float green = 0.0f;
- float blue = 0.0f;
- GL gl = drawable.getGL();
- gl.glClear(GL.GL_COLOR_BUFFER_BIT);
- gl.glPointSize(5.0f);
+ public Iterator<T> iterator() { return ts.iterator(); }
- for(int i=0; i<stlf.coordArray.length; i+=3) {
- red -= .09f;
- green -= .12f;
- blue -= .15f;
- if (red < 0.15) red = 1.0f;
- if (green < 0.15) green = 1.0f;
- if (blue < 0.15) blue = 1.0f;
- gl.glColor3f(red, green, blue);
-
- gl.glBegin(GL.GL_TRIANGLES);
- for(int j=0; j<3; j++)
- gl.glVertex3f(stlf.coordArray[i+j].x,
- stlf.coordArray[i+j].y,
- stlf.coordArray[i+j].z);
- gl.glEnd();
- }
+ public P newP(float x, float y, float z) {
+ P p = new P(x, y, z);
+ P p2 = ps.get(p);
+ if (p2 != null) return p2;
+ ps.put(p,p);
+ return p;
+ }
+
+ public E newE(P p1, P p2) {
+ E e = new E(p1, p2);
+ E e2 = es.get(e);
+ if (e2 != null) return e2;
+ es.put(e,e);
+ return e;
}
+ public T newT(E e1, E e2, E e3) {
+ if (e1.t1 != null && e1.t1.hasE(e1) && e1.t1.hasE(e2) && e1.t1.hasE(e3)) return e1.t1;
+ if (e1.t2 != null && e1.t2.hasE(e1) && e1.t2.hasE(e2) && e1.t2.hasE(e3)) return e1.t2;
+ if (e2.t1 != null && e2.t1.hasE(e1) && e2.t1.hasE(e2) && e2.t1.hasE(e3)) return e2.t1;
+ if (e2.t2 != null && e2.t2.hasE(e1) && e2.t2.hasE(e2) && e2.t2.hasE(e3)) return e2.t2;
+ if (e3.t1 != null && e3.t1.hasE(e1) && e3.t1.hasE(e2) && e3.t1.hasE(e3)) return e3.t1;
+ if (e3.t2 != null && e3.t2.hasE(e1) && e3.t2.hasE(e2) && e3.t2.hasE(e3)) return e3.t2;
+ return new T(e1, e2, e3);
+ }
- public void reshape(
- GLAutoDrawable drawable,
- int x,
- int y,
- int width,
- int height
- ) {}
- public void displayChanged(
- GLAutoDrawable drawable,
- boolean modeChanged,
- boolean deviceChanged
- ) {}
-
- /** point in 3-space */
- public static class P {
+ /** [UNIQUE] point in 3-space */
+ public final class P {
public final float x, y, z;
public P(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
- public V minus(P p) { return null; }
+ public V minus(P p) { return new V(x-p.x, y-p.y, z-p.z); }
+ public boolean equals(Object o) {
+ if (o==null || !(o instanceof P)) return false;
+ P p = (P)o;
+ return p.x==x && p.y==y && p.z==z;
+ }
+ public int hashCode() {
+ return
+ Float.floatToIntBits(x) ^
+ Float.floatToIntBits(y) ^
+ Float.floatToIntBits(z);
+ }
+ public void glVertex(GL gl) { gl.glVertex3f(x, y, z); }
}
/** vector in 3-space */
- public static class V {
+ public final class V {
private final float x, y, z;
public V(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
- public V norm() { return null; /* FIXME */ }
public boolean sameDirection(V v) { throw new Error(); }
- public V cross(V v) { return null; }
+ 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); }
+ public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
+ public V norm() { float m = mag(); return new V(x/m, y/m, z/m); }
}
- /** an edge */
- public static class E {
+ /** [UNIQUE] an edge */
+ public final class E {
public final P p1, p2;
- private T t1, t2;
+ T t1, t2;
public E(P p1, P p2) { this.p1 = p1; this.p2 = p2; }
+ public int hashCode() { return p1.hashCode() ^ p2.hashCode(); }
public boolean equals(Object o) {
if (o==null || !(o instanceof E)) return false;
E e = (E)o;
if (this.p2 == e.p1 && this.p1 == e.p2) return true;
return false;
}
- public P shared(E e) { return null; }
+ public P shared(E e) {
+ if (p1==e.p1) return p1;
+ if (p1==e.p2) return p1;
+ if (p2==e.p1) return p2;
+ if (p2==e.p2) return p2;
+ throw new Error("no shared vertex in shared()");
+ }
+ public P unshared(E e) {
+ if (p1==e.p1) return p2;
+ if (p1==e.p2) return p2;
+ if (p2==e.p1) return p1;
+ if (p2==e.p2) return p1;
+ throw new Error("no shared vertex in unshared()");
+ }
}
- /** a triangle (face) */
- public static class T {
+ /** [UNIQUE] a triangle (face) */
+ public final class T {
public final E e1, e2, e3;
public T(E e1, E e2, E e3, V normal) {
P p1 = e1.shared(e2);
this.e2 = e2;
this.e3 = e1;
}
+ if (e1.t1 == null) e1.t1 = this; else if (e1.t2 == null) e1.t2 = this; else throw new Error("non-manifold surface");
+ if (e2.t1 == null) e2.t1 = this; else if (e2.t2 == null) e2.t2 = this; else throw new Error("non-manifold surface");
+ if (e3.t1 == null) e3.t1 = this; else if (e3.t2 == null) e3.t2 = this; else throw new Error("non-manifold surface");
+ ts.add(this);
}
public V norm() {
P p1 = e1.shared(e2);
P p3 = e3.shared(e1);
return p2.minus(p1).cross(p3.minus(p1)).norm();
}
- public T(E e1, E e2, E e3) { this.e1 = e1; this.e2 = e2; this.e3 = e3; }
+ public T(E e1, E e2, E e3) {
+ this.e1 = e1;
+ this.e2 = e2;
+ this.e3 = e3;
+ if (e1.t1 == null) e1.t1 = this; else if (e1.t2 == null) e1.t2 = this; else throw new Error("non-manifold surface");
+ if (e2.t1 == null) e2.t1 = this; else if (e2.t2 == null) e2.t2 = this; else throw new Error("non-manifold surface");
+ if (e3.t1 == null) e3.t1 = this; else if (e3.t2 == null) e3.t2 = this; else throw new Error("non-manifold surface");
+ ts.add(this);
+ }
+ public boolean hasE(E e) { return e1==e || e2==e || e3==e; }
+ public void glVertices(GL gl) {
+ e1.unshared(e2).glVertex(gl);
+ e1.shared(e2).glVertex(gl);
+ e2.shared(e3).glVertex(gl);
+ }
}
/** matrix */
- public static class M {
- }
-
- public static void main(String[] s) throws Exception {
- stlf = new StlFile();
- stlf.load("teapot.stl");
- /*
- for(int i=0; i<stlf.coordArray.length; i+=3) {
- System.out.println("triangle: (" +
- stlf.coordArray[i+0] + ", " +
- stlf.coordArray[i+1] + ", " +
- stlf.coordArray[i+2] + ")");
- }
-
- gi.setCoordinates();
- gi.setNormals(normArray);
- gi.setStripCounts(stripCounts);
-
- GeometryInfo gi = ;
- ga.convertToIndexedTriangles();
- GeometryArray ga = ;
- ga.convertToIndexedTriangles();
- */
-
- Geom geom = new Geom();
-
-
- Frame f = new Frame();
- GLCapabilities glcaps = new GLCapabilities();
- GLCanvas glcanvas = new GLCanvas();
- glcanvas.addGLEventListener(geom);
- f.add(glcanvas, BorderLayout.CENTER);
- f.show();
- f.setSize(500, 300);
+ public class M {
}
}