// light_stl.java 3D projection of a .stl file // needs center // needs heapsort.java compiled import java.io.*; import java.util.*; import java.text.*; import java.awt.*; import java.awt.event.*; // solid rect // facet normal 0.0 0.0 -1.0 // outer loop // vertex 1.0 1.0 1.0 // vertex 5.0 4.0 1.0 // vertex 1.0 4.0 1.0 // endloop // endfacet // facet normal 0.0 0.0 -1.0 // outer loop // vertex 1.0 1.0 1.0 // vertex 5.0 1.0 1.0 // vertex 5.0 4.0 1.0 // endloop // endfacet //... // endsolid public class light_stl extends Frame { // input .slt convert to .dat format internally int num_points; int num_polys; double x[] = new double[60000]; double y[] = new double[60000]; double z[] = new double[60000]; int data_polys[] = new int[1000000]; int pripoly[] = new int[40000]; int priindex[] = new int[40000]; double priz[] = new double[40000]; double winxyz[] = new double[3]; double xmin, xmax, ymin, ymax, zmin, zmax; double dxmin, dxmax, dymin, dymax, dzmin, dzmax; double dxinc, dyinc, dzinc; String fname; double size = 1.0; double gsize = 1.0; double nsize = -1.0; double scale = 200.0; boolean debug = false; int xm, ym, bm; // mouse double pos[] = {0.0, 0.0, 0.0}; double roll = 0.0; // rotate object double pitch = 0.0; double heading = 0.0; int wire = 1; // ??? set to zero when debugged int vert = 0; int colorz = 0; int noden = 1; double mmatrix[][] = new double[4][4];// 4 by 4 model transformation matrix double pmatrix[][] = new double[4][4];// 4 by 4 persp transformation matrix int viewport[] = new int[4]; double pxyzw[] = new double[4]; int win_w = 600; int win_h = 600; double M_PI = Math.PI; heapsort H = new heapsort(); light_stl(String filename) { System.out.println("light_stl.java reading "+filename); read_data(filename); System.out.println("num_points="+num_points+", num_polys="+num_polys); fname = filename; setTitle("light_stl"); setSize(win_w,win_h); setBackground(Color.white); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); this.addMouseListener (new mousePressHandler()); } // end light_stl void read_data(String filein) { int npt = 0; int pts[] = new int[3]; int ipt; // unique StringTokenizer T; String token; String solid = "solid"; String facet = "facet"; String normal = "normal"; String outer = "outer"; String loop = "loop"; String vertex = "vertex"; String endloop = "endloop"; String endfacet = "endfacet"; String endsolid = "endsolid"; if(debug) System.out.println("light_stl.java reading "+filein); try { FileReader fin = new FileReader(filein); BufferedReader in = new BufferedReader(fin); String input_line; int len; while(true) // to endsolid { input_line = in.readLine(); // len = input_line.length(); if(debug) System.out.println("input:"+input_line+" len="+len); T = new StringTokenizer(input_line," "); token = T.nextToken(); // ? if(debug) System.out.println("token:"+token); if(token.equals(endsolid)) break; if(token.equals(endloop)) { data_polys[num_polys] = 3; data_polys[num_polys+1] = pts[0]; data_polys[num_polys+2] = pts[1]; data_polys[num_polys+3] = pts[2]; num_polys += 4; npt = 0; continue; } if(token.equals(endfacet)) continue; if(token.equals(vertex)) { token = T.nextToken(); // x if(debug) System.out.println("token:"+token); x[num_points] = Double.parseDouble(token); token = T.nextToken(); // y if(debug) System.out.println("token:"+token); y[num_points] = Double.parseDouble(token); token = T.nextToken(); // z if(debug) System.out.println("token:"+token); z[num_points] = Double.parseDouble(token); num_points++; ipt = unique(); pts[npt] = ipt; // one big per .dat npt++; } } // end while } // end try catch(IOException exception) { System.out.println("datread.java "+exception); } System.out.println("unique data points"); xmin = x[0]; xmax = x[0]; ymin = y[0]; ymax = y[0]; zmin = z[0]; zmax = z[0]; for(int i=0; i v Y ^ < Z > < R > ^ P v v H ^ W V C N sz SZ", 20, win_h-25); g.drawString("x="+pos[0]+", y="+pos[1]+", z-"+pos[2]+", R="+roll+", P="+ pitch+", H="+heading+", S="+size, 20, win_h-10); order_polys(); if(colorz==1) draw_face_dat(g); if(wire==1) draw_wire_dat(g); if(noden==1) draw_node_dat(g); else if(vert==1) draw_vert_dat(g); else draw_face_dat(g); } // end paint class mousePressHandler extends MouseAdapter { public void mousePressed (MouseEvent e) { char key=' '; xm = e.getX(); ym = e.getY(); bm = e.getButton(); System.out.println("mouse xm="+xm+", ym="+ym+", bm="+bm); if(ym>win_h-21 || ym18 && xm<28) key = 'x'; if(xm>45 && xm<55) key = 'X'; if(xm>62 && xm<72) key = 'y'; if(xm>85 && xm<95) key = 'Y'; if(xm>102 && xm<112) key = 'z'; if(xm>125 && xm<135) key = 'Z'; if(xm>148 && xm<158) key = 'r'; if(xm>172 && xm<182) key = 'R'; if(xm>191 && xm<201) key = 'p'; if(xm>214 && xm<224) key = 'P'; if(xm>230 && xm<240) key = 'h'; if(xm>255 && xm<265) key = 'H'; if(xm>275 && xm<295) key = 'W'; if(xm>300 && xm<320) key = 'V'; if(xm>325 && xm<345) key = 'C'; if(xm>345 && xm<370) key = 'N'; if(xm>370 && xm<400) key = 's'; if(xm>400 && xm<430) key = 'S'; if(debug) { System.out.println("at xm="+xm+", ym="+ym+", bm="+bm); // debug print System.out.println("key="+key); } if(key == 'x') {pos[0] -= dxinc;} // position object if(key == 'X') {pos[0] += dxinc;} if(key == 'y') {pos[1] -= dyinc;} // fix Y down if(key == 'Y') {pos[1] += dyinc;} if(key == 'z') {pos[2] -= dzinc;} if(key == 'Z') {pos[2] += dzinc;} if(key == 'r') {roll -= 15.0; // rotate object if(roll<=-360) roll += 360.0;} if(key == 'R') {roll += 15.0; if(roll>=360) roll -= 360.0;} if(key == 'p') {pitch -= 15.0; if(pitch<=-360) pitch += 360.0;} if(key == 'P') {pitch += 15.0; if(pitch>=360) pitch -= 360.0;} if(key == 'h') {heading -= 15.0; if(heading<=-360) heading += 360.0;} if(key == 'H') {heading += 15.0; if(heading>=360) heading -= 360.0;} if(key == 'w' || key == 'W') {wire = 1-wire;} // 0,0,0 is colorz face if(key == 'v' || key == 'V') {vert = 1-vert; wire=0; colorz=0;} if(key == 'c' || key == 'C') {colorz = 1-colorz; wire=0; vert=0;} if(key == 'n' || key == 'N') {noden = 1-noden;} if(key == 's') {gsize = gsize*2.0;} // size smaller if(key == 'S') {gsize = gsize*0.5;} // size larger System.out.println("roll="+roll+", pitch="+pitch+", head="+heading+ ", size="+gsize); // debug print requestFocus(); repaint(); } } // end mousePressHandler void draw_face_dat(Graphics g) { int i, j, k, pts, pt, pt1; int skipz=0; double nxyz[] = new double[3]; int xpts[] = new int[10]; int ypts[] = new int[10]; double xsav[] = new double[3]; double ysav[] = new double[3]; double zsav[] = new double[3]; double zmin=0.0, zmax=0.0, zavg=0.0; double x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4 ; build_mmat(); // from pitch, heading, roll, pos //glGetDoublev(GL_MODELVIEW_MATRIX, mmatrix); // save transformation //glGetDoublev(GL_PROJECTION_MATRIX, pmatrix); if(debug) System.out.println("in draw_face_dat"); // use num_polys to do that many faces k = 0; for(i=0; i zmax) zmax = winxyz[2]; } zavg = zavg/ (double)num_points; } // use num_polys to do wire frame k = 0; for(i=0; i