checkpoint
[anneal.git] / src / edu / berkeley / qfat / InteractiveMeshViewer.java
1 package edu.berkeley.qfat;
2 import java.io.*;
3 import java.nio.*;
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import javax.media.opengl.*;
8 import javax.media.opengl.glu.*;
9 import com.sun.opengl.util.*;
10 import java.util.*;
11 import edu.berkeley.qfat.geom.*;
12 import edu.berkeley.qfat.geom.Point;
13
14 public class InteractiveMeshViewer extends JPanel implements KeyListener {
15
16     public Mesh tile;
17     public Mesh goal;
18
19     private HashSet<MeshViewer> mvs = new HashSet<MeshViewer>();
20
21     public InteractiveMeshViewer(JFrame f) {
22         MeshViewer mv1 = new MeshViewer();
23         MeshViewer mv2 = new MeshViewer();
24         MeshViewer mv3 = new MeshViewer();
25         MeshViewer mv4 = new MeshViewer();
26         mv1.main = (Main)this;
27         mv2.main = (Main)this;
28         mv3.main = (Main)this;
29         mv4.main = (Main)this;
30         mvs.add(mv1);
31         mvs.add(mv2);
32         mvs.add(mv3);
33         mvs.add(mv4);
34         f.addKeyListener(this);
35         addKeyListener(this);
36         mv1.addKeyListener(this);
37         mv2.addKeyListener(this);
38         mv3.addKeyListener(this);
39         mv4.addKeyListener(this);
40         setLayout(new GridLayout(2,2));
41         this.add(mv1);
42         this.add(mv2);
43         this.add(mv3);
44         this.add(mv4);
45         setTile(new Mesh(false));
46         setGoal(new Mesh(false));
47     }
48     public void repaint() {
49         if (mvs != null)
50             for(MeshViewer mv : mvs)
51                 mv.repaint();
52     }
53
54
55     public void setTile(Mesh tile) {
56         for(MeshViewer mv : mvs) mv.removeMesh(this.tile);
57         this.tile = tile;
58         for(MeshViewer mv : mvs) mv.addMesh(this.tile);
59     }
60     public void setGoal(Mesh goal) {
61         for(MeshViewer mv : mvs) mv.removeMesh(this.goal);
62         this.goal = goal;
63         goal.option_selectable = false;
64         goal.option_wireframe = true;
65         for(MeshViewer mv : mvs) mv.addMesh(this.goal);
66     }
67
68     public synchronized void dump() {
69         try {
70             PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("dump.stl")));
71             pw.println("solid dump");
72             for(Mesh.T t : tile) {
73                 Vec normal = t.norm();
74                 pw.println("facet normal " + normal.x + " " + normal.y + " " + normal.z);
75                 pw.println("  outer loop");
76                 for(Mesh.Vertex v : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
77                     pw.println("    vertex " + v.p.x + " " + v.p.y + " " + v.p.z);
78                 }
79                 pw.println("  endloop");
80                 pw.println("endfacet");
81             }
82             pw.println("endsolid dump");
83             pw.flush();
84             pw.close();
85         } catch (Exception e) { throw new RuntimeException(e); }
86     }
87
88     public int whichNeighbor = 1;
89     public double temp;
90     public boolean tileon = true;
91     public boolean tilemeshon = false;
92     public boolean goalon = true;
93     public boolean anneal = false;
94     public boolean hillclimb = false;
95     public boolean neighbors = false;
96     public boolean neighborsWire = false;
97     public boolean neighborsWireOne = false;
98     public boolean errorNormals = false;
99
100     public boolean force = false;
101     public Mesh.Vertex[] points;
102     public int breaks = 0;
103
104     public int temps;
105     public int accepts;
106     public    int vertss;
107
108     public void keyTyped(KeyEvent e)  { }
109     public void keyReleased(KeyEvent e) { }
110     public void keyPressed(KeyEvent e)  {
111         //super.keyPressed(e);
112         switch(e.getKeyCode()) {
113             case KeyEvent.VK_SPACE:
114                 System.err.println("hak");
115                 synchronized(this) {
116                     tile.subdivide();
117                     tile.rebindPoints();
118                     break;
119                 }
120                 //case KeyEvent.VK_SPACE: breaks++; force = true; break;
121             case KeyEvent.VK_UP: temp = temp * 2; break;
122             case KeyEvent.VK_ENTER: temp = 10; break;
123             case KeyEvent.VK_N: whichNeighbor++; break;
124             case KeyEvent.VK_RIGHT: whichNeighbor++; break;
125             case KeyEvent.VK_D: dump(); break;
126             case KeyEvent.VK_E: errorNormals = !errorNormals; break;
127             case KeyEvent.VK_A: hillclimb = false; anneal = !anneal; break;
128             case KeyEvent.VK_H: anneal = true; hillclimb = !hillclimb; break;
129                 //case KeyEvent.VK_N: neighbors = !neighbors; break;
130             case KeyEvent.VK_T: tileon = !tileon; break;
131             case KeyEvent.VK_G: goalon = !goalon; break;
132             case KeyEvent.VK_M: tilemeshon = !tilemeshon; break;
133         }
134         if (transforms!=null)
135             whichNeighbor = (whichNeighbor % (transforms.length+1));
136     }
137     public Matrix[] transforms;
138 }