// light_normal_stl.java projection may not be best // needs to size and center 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_normal_stl extends Frame { // .dat format file input int num_points; int num_polys; double x[] = new double[20000]; double y[] = new double[20000]; double z[] = new double[20000]; int data_polys[] = new int[256000]; int maxpolys; 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 = true; 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; int vert = 1; int colorz = 0; 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; String stlfile; light_normal_stl(String filename) { System.out.println("light_normal_stl.java reading "+filename); stlfile = filename; read_data(filename); System.out.println("num_points="+num_points+", num_polys="+num_polys); System.out.println("size="+size+", maxpolys="+maxpolys); fname = filename; setTitle("light_normal_stl"); setSize(win_w,win_h); setBackground(Color.white); setForeground(Color.black); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); this.addMouseListener (new mousePressHandler()); } // end light_normal_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 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); if(wire==1) draw_wire_dat(g); if(vert==1) draw_vert_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 = 's'; if(xm>370 && xm<400) key = 'S'; 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;} 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;} if(key == 'v' || key == 'V') {vert = 1-vert;} if(key == 'c' || key == 'C') {colorz = 1-colorz;} if(key == 's') {gsize = gsize*1.15;} if(key == 'S') {gsize = gsize*0.85;} System.out.println("roll="+roll+", pitch="+pitch+", head="+heading+ ", size="+gsize); // debug print requestFocus(); repaint(); } } // end mousePressHandler void draw_wire_dat(Graphics g) { int i, j, k, pts, pt, pt1; int skipz=0; double winxyz[] = new double[3]; double nxyz[] = new double[3]; double avgx, avgy, avgz, zmin=0.0, zmax=0.0, zavg=0.0; double lenn = size/15.0; // for normal vector double x1, x2, y1, y2, z1, z2; build_mmat(); // from pitch, heading, roll, pos //glGetDoublev(GL_MODELVIEW_MATRIX, mmatrix); // save transformation //glGetDoublev(GL_PROJECTION_MATRIX, pmatrix); System.out.println("in draw_wire_dat lenn="+lenn); if(colorz==1) { zavg = 0.0; for(i=0; i zmax) zmax = winxyz[2]; } zavg = zavg/ (double)num_points; } setForeground(Color.black); // use num_polys to do wire frame k = 0; for(i=0; i