X-Git-Url: http://git.megacz.com/?p=anneal.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fqfat%2FMain.java;h=83bf28173d3b8b3f3516e070820f03ef0d5daa5b;hp=52782bea8f6e1a2221726b476c5981044b8eab95;hb=73ed19a2635750c10ed5ff71677f04ccfd3d0ef3;hpb=3a566fdd09e751b7901edc4d22060032f96fe438 diff --git a/src/edu/berkeley/qfat/Main.java b/src/edu/berkeley/qfat/Main.java index 52782be..83bf281 100644 --- a/src/edu/berkeley/qfat/Main.java +++ b/src/edu/berkeley/qfat/Main.java @@ -8,139 +8,150 @@ import java.util.*; import edu.berkeley.qfat.geom.*; import edu.berkeley.qfat.geom.Point; -// FIXME: recenter goal to have centroid coincident with tile -// FIXME: re-orient goal (how?) -// fish case: ensure that spinal axis of fish is the x-axis of the tile +// TO DO: +// - real anneal +// - solve self-intersection problem +// - get a better test model? +// - symmetry constraints withing the tile +// - rotation matrices +// - overbinding results in forced equational constraints on the leader +// - shatter in invertd-triforce pattern brian mentioned +// - aspect ratio? non-uniform deformation? +// - rotational alignment + +// - movie-style user interface like +// http://www.coleran.com/markcoleranreell.html ? + +// - consider recasting the Shewchuk predicates in Java? +// http://www.cs.cmu.edu/afs/cs/project/quake/public/code/predicates.c + +/* + blender keys + - middle mouse = option+click + - right mouse = command+click + + 3,7,1 = view along axes (control for opp direction) + 4, 8, 7, 2 = rotate in discrete increments (+control to translate) + middle trag: rotate space + shift+middle drag: translate space + wheel: zoom + home: home view: take current angle, zoom to whole scnee + 5 = ortho vs non-ortho + +*/ -public class Main implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener { - - boolean alt = false; - boolean shift = false; - boolean control = false; - public void mouseWheelMoved(MouseWheelEvent e) { - tz -= e.getWheelRotation(); - } +// FIXME: re-orient goal (how?) - public void keyTyped(KeyEvent e) { } - public void keyPressed(KeyEvent e) { - switch(e.getKeyCode()) { - case KeyEvent.VK_CONTROL: control = true; break; - case KeyEvent.VK_ALT: alt = true; break; - case KeyEvent.VK_SHIFT: shift = true; break; - } - } - public void keyReleased(KeyEvent e) { - switch(e.getKeyCode()) { - case KeyEvent.VK_CONTROL: control = false; break; - case KeyEvent.VK_ALT: alt = false; break; - case KeyEvent.VK_SHIFT: shift = false; break; - } - } +public class Main extends MeshViewer { - public void mouseClicked(MouseEvent e) { } - public void mouseEntered(MouseEvent e) { } - public void mouseExited(MouseEvent e) { } - public void mousePressed(MouseEvent e) { } - public void mouseReleased(MouseEvent e) { } - - int mousex; - int mousey; - public void mouseMoved(MouseEvent e) { - mousex = e.getX(); - mousey = e.getY(); - } - - float tx = 0; - float ty = 0; - float tz = 0; - float anglex = 0; - float angley = 0; - public void mouseDragged(MouseEvent e) { - if (shift) { - tx += (mousex - e.getX())/(float)20; - ty += (mousey - e.getY())/(float)20; - } else { - anglex -= mousex - e.getX(); - angley += mousey - e.getY(); - } - mousex = e.getX(); - mousey = e.getY(); - } - - private Mesh tile = new Mesh(); - private Mesh goal = new Mesh(); + public static int verts = 1; + public static final Random random = new Random(); + /** magnification factor */ private static final float MAG = 1; - Matrix[] translations; - Mesh.Vert[] points; - - public Main(StlFile stlf) { + public Main(StlFile stlf, Frame f) { + super(f); for(int i=0; i 300) return; - //double min = (tile.avgedge/tile.numedges)*(1+(4/(double)verts)); - //if (verts>0 && tile.es.peek().length() < min) return; + public void breakit() { + int oldverts = verts; + System.out.println("doubling vertices."); PriorityQueue es = new PriorityQueue(); - for(Mesh.E e : tile.edges()) es.add(e); - for(int i=0; i<10; i++) { + for(Mesh.T t : tile) { + es.add(t.e1()); + es.add(t.e2()); + es.add(t.e3()); + Thread.yield(); + repaint(); + } + for(int i=0; i 0) { + while (breaks>0) { + breaks--; + breakit(); + } + seek_upward = true; + } else if (acceptance > 0.96) gamma = 0.4f; + else if (acceptance > 0.9) gamma = 0.5f; + else if (acceptance > 0.8) gamma = 0.65f; + else if (acceptance > 0.6) gamma = 0.7f; + else if (acceptance > 0.3) gamma = 0.8f; + else if (acceptance > 0.15) gamma = 0.9f; + else if (acceptance > 0.05) gamma = 0.95f; + else if (acceptance > 0.01) gamma = 0.98f; + else { /*breaks++;*/ } + + if (seek_upward) { + if (acceptance > 0.2) seek_upward = false; + else gamma = 2-gamma; + } - public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { } - public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { } - public synchronized void display(GLAutoDrawable drawable) { - GL gl = drawable.getGL(); - GLU glu = new GLU(); - gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); - gl.glPointSize(5.0f); - gl.glLoadIdentity(); - glu.gluPerspective(50-tz, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10); - glu.gluLookAt(0, 0, -1, 0, 0, 0, 0, 1, 0); - gl.glTranslatef(tx/(float)20, ty/(float)20, 0); - gl.glRotatef(anglex/3, 0, 1, 0); - gl.glRotatef(angley/3, 1, 0, 0); - - gl.glBegin(GL.GL_TRIANGLES); - draw(gl, true, tile); - gl.glEnd(); - - gl.glBegin(GL.GL_TRIANGLES); - gl.glColor4f((float)0.5, (float)0.5, (float)0.5, (float)0.8); - //draw(gl, false, goal); - gl.glEnd(); - - - int i = 0; - //gl.glDisable(GL.GL_DEPTH_TEST); - gl.glColor4f(1,1,1,1); - for(Matrix m : translations) { - //if (v1.z==0 && v1.y==0) continue; - i++; - if (i != 1 /*&& i!=4*/) continue; - Point p = new Point(0, 0, 0).times(m); - Vec v = new Vec(p.x, p.y, p.z); - v = v.times((float)1.04); - gl.glTranslatef(v.x, v.y, v.z); - draw(gl, false, tile); - gl.glTranslatef(-v.x, -v.y, -v.z); - } - //gl.glEnable(GL.GL_DEPTH_TEST); - } + temp = temp * gamma; - private synchronized void draw(GL gl, boolean triangles, Mesh mesh) { - float red = 0.0f; - float green = 0.0f; - float blue = 0.0f; - for(Mesh.T t : mesh) { - if (red < 0.15) red = 1.0f; - if (green < 0.15) green = 1.0f; - if (blue < 0.15) blue = 1.0f; - red -= .09f; - green -= .12f; - blue -= .15f; - - if (triangles) switch(t.color) { - case 0: gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3); break; - case 1: gl.glColor4f((float)0.25, (float)0.75, (float)0.25, (float)0.3); break; - case 2: gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3); break; - case 3: gl.glColor4f((float)0.50, (float)0.50, (float)0.50, (float)0.3); break; - } - //gl.glBegin(GL.GL_LINES); - - if (triangles) { - gl.glBegin(GL.GL_TRIANGLES); - t.glVertices(gl); - gl.glEnd(); - } else { - gl.glBegin(GL.GL_LINES); - t.e1().p1.p.glVertex(gl); - t.e1().p2.p.glVertex(gl); - t.e2().p1.p.glVertex(gl); - t.e2().p2.p.glVertex(gl); - t.e3().p1.p.glVertex(gl); - t.e3().p2.p.glVertex(gl); - gl.glEnd(); - } - Point centroid = t.centroid(); - gl.glBegin(GL.GL_LINES); - gl.glColor3f(1, 1, 1); - /* - centroid.glVertex(gl); - centroid.plus(t.norm().times(t.diameter())).glVertex(gl); - */ + HashSet hs = new HashSet(); + for(Mesh.Vertex p : tile.vertices()) hs.add(p); + Mesh.Vertex[] pts = (Mesh.Vertex[])hs.toArray(new Mesh.Vertex[0]); + + int count = 0; + long then = System.currentTimeMillis(); + for(int i=0; i<40; i++) { + if (anneal) { + count++; + Mesh.Vertex v = pts[Math.abs(random.nextInt()) % pts.length]; + rand(temp,v); + v.recomputeFundamentalQuadricIfStale(); + v.recomputeFundamentalQuadricIfNeighborChanged(); + } + Thread.yield(); + repaint(); + } + PriorityQueue es = new PriorityQueue(); + for(Mesh.T t : tile) { + float max = 5; + for(Mesh.E e : new Mesh.E[] { t.e1(), t.e2(), t.e3() }) { + if (e==null) continue; + if (e.stretchRatio() > max) es.add(e); + if (t.aspect() < 0.1 && e.length()>e.next.length() && e.length()>e.prev.length()) es.add(e); + } + } - if (mesh==goal) - for(Mesh.Vert p : new Mesh.Vert[] { t.v1(), t.v2(), t.v3() }) { - p.p.glVertex(gl); - //p.plus(p.norm().times(p.score()*10)).glVertex(gl); - p.partner().p.glVertex(gl); - //tile.nearest(p).centroid().glVertex(gl); + for(int i=0; i<5; i++) { + Mesh.E e = es.poll(); + if (e==null) break; + e.shatter(); } + tile.rebindPoints(); + + System.out.println("temp="+temp + " ratio="+(Math.ceil(acceptance*100)) + " " + + "points_per_second=" + + (count*1000)/((double)(System.currentTimeMillis()-then))); - gl.glEnd(); + for(Mesh.Vertex p : goal.vertices()) p.recomputeFundamentalQuadricIfNeighborChanged(); + synchronized(safeTriangles) { + safeTriangles.clear(); + for(Mesh.T t : tile) + if (t.shouldBeDrawn()) + safeTriangles.add(t); + } + } } } + public static void main(String[] s) throws Exception { StlFile stlf = new StlFile(); - stlf.load("simplefish.stl"); - Main main = new Main(stlf); + stlf.load("fish.stl"); + //stlf.load("monkey.stl"); Frame f = new Frame(); - GLCapabilities glcaps = new GLCapabilities(); - GLCanvas glcanvas = new GLCanvas(); - glcanvas.addGLEventListener(main); - f.add(glcanvas, BorderLayout.CENTER); + Main main = new Main(stlf, f); f.pack(); f.show(); f.setSize(900, 900); f.doLayout(); - - glcanvas.addMouseListener(main); - glcanvas.addMouseMotionListener(main); - glcanvas.addMouseWheelListener(main); - glcanvas.addKeyListener(main); - - main.anneal(glcanvas); - } - public static int verts = 0; - public void anneal(GLCanvas glcanvas) throws Exception { - int verts = 0; - while(true) { - //Thread.sleep(10); - for(int i=0; i<1; i++) { - glcanvas.repaint(); - //tile.ts.get(Math.abs(random.nextInt()) % tile.ts.size()).e1().p1 - for(Mesh.T t : tile) - for(Mesh.Vert p : new Mesh.Vert[] { t.v1(), t.v2(), t.v3() }) { - rand(10,p); - } - goal.rescore(); - tile.rescore(); - } - breakit(); - } - + main.anneal(); } } \ No newline at end of file