// plot4d.java plot up to 4d data, mouse select choice // data 4 nx ny nz nt import java.io.*; import java.awt.*; import java.awt.event.*; public class plot4d extends Frame { // mouse and display variables int xm, ym, bm; // mouse position int height = 750; int width = 750; double xmin = 0.0; // scaled for plotting double xmax = 1.0; double ymin = 0.0; double ymax = 1.0; double zmin = 0.0; double zmax = 1.0; double tmin = 0.0; double tmax = 1.0; double Umin = 0.0; double Umax = 1.0; double dxmin, dxmax, dymin, dymax, dzmin, dzmax, dtmin, dtmax, dUmin, dUmax; double v1min = 0.0; // computed based on mouse click double v1max = 1.0; double v2min = 0.0; double v2max = 1.0; double v3min = 0.0; double v3max = 1.0; double v4min = 0.0; double v4max = 1.0; double xw, yh, xoff, yoff; int dim = 4; // should be read in int nn[] = new int[dim]; // nx, ny, nz, nt in var order int var1 = 0; // x=0, y=1, z=2, t=3 front axis mouse change int var2 = 1; // x=0, y=1, z=2, t=3 side axis int var3 = 2; // x=0, y=1, z=2, t=3 scan-run int var4 = 3; // x=0, y=1, z=2, t=3 other-run double v1, v2, v3, v4; int v3step = 1; int v4step = 1; int nx; // computed based on data int ny; int nz; int nt; int nv1; // nx, ny, nz, nt based on var1, var2, var3, var4 int nv2; int nv3; int nv4; int nxyzt; double pt[][]; // = new double[nxyzt][dim+1]; double ya = 0.5; // orthographic typ 0.2 to 0.7, must fix text double xs = (0.60*(width-100)); double ys = (0.60*(height-100)); int xp, yp; // for passing back ortho point boolean debug = false; int n_input; boolean run34 = false; boolean stop = false; int speed = 100; // millisecond delay // set up double buffering for smooth "run" Image dbImage; Graphics dbg; // double buffered graphics public plot4d() { System.out.println("plot4d.java running."); setTitle("plot4d x,y,z,t value u"); setSize(width,height); setBackground(Color.white); setForeground(Color.black); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); this.addMouseListener (new mousePressHandler()); try { read_data(); nn[0] = nx; nn[1] = ny; nn[2] = nz; nn[3] = nt; nv1 = nn[0]; nv2 = nn[1]; nv3 = nn[2]; nv4 = nn[3]; v3step = nn[var3]-1; v4step = nn[var4]-1; } // end try catch(IOException exception) { System.out.println(exception); } // repaint(); } // plot4d constructor // this is the key to double buffering, repaint() calls update, not paint public void update(Graphics g) { // initialize Double Buffers if(dbImage==null) { dbImage = createImage(this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics(); } // only first time // clear Image in RAM, dbg is off screen buffer dbg.setColor(Color.white); dbg.fillRect(0, 0, this.getSize().width, this.getSize().height); paint(dbg); // now call paint to render in dbg g.drawImage(dbImage, 0, 0, this); // display rendered image, very fast } // end update public class do_scan extends Thread // var1, var2, var3, var4, v3step, v4step can be changed { do_scan() { if(debug) System.out.println("do_scan on var3="+var3+" with nv3="+nv3); if(debug) System.out.println("do_scan on var4="+var4+" with nv4="+nv4); } public void run() { for(int iii=0; iii= "+nxyzt); System.out.println("i="+i+" ii="+ii+" iii="+iii+" iiii="+iiii); System.out.println("var1="+var1+" var2="+var2+" var3="+var3+" var4="+var4); System.out.println("offest[0]="+offset[0]+" offset[1]="+offset[1]+" offset[2]="+offset[2]+ " offset[3]="+offset[3]); lin = nxyzt-1; } return lin; } // end i4x // orthographic projection, sets xp,yp void ortho(double x, double y, double u) { double x1, y1, u1, x2, y2, x3, y3; x1 = (x-xmin)/(xmax-xmin); // zero base, normalized y1 = (y-ymin)/(ymax-ymin); u1 = (u-Umin)/(Umax-Umin); // u-Umin; x2 = x1+y1*ya; // ortho z is up, y is back y2 = u1+y1*ya; x3 = x2*xs; // scale y3 = y2*ys; x3 = x3+25; // away from boarder y3 = height-y3-25; // away from boarder xp = (int)x3; yp = (int)y3; //System.out.println("ortho xp="+xp+", yp="+yp); //System.out.println("x1="+x1+", y1="+y1+", u1="+u1); //System.out.println(" "); } // end ortho void set_color(int i, Graphics g) { if(i<0) {g.setColor(Color.black); return;} if(i>9) {g.setColor(Color.gray); return; } switch(i) { case 0: g.setColor(Color.black); break; case 1: g.setColor(new Color(188,143,143)); break; // Color.pink); brown case 2: g.setColor(Color.red); break; case 3: g.setColor(Color.orange); break; case 4: g.setColor(Color.yellow); break; case 5: g.setColor(Color.green); break; case 6: g.setColor(Color.blue); break; case 7: g.setColor(Color.magenta); break; case 8: g.setColor(Color.cyan); break; case 9: g.setColor(Color.gray); break; } } // end set_color void read_data() throws IOException // big ugly input method { try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String input_line; double amount; int len; int index; int last; System.out.println("read_data running"); input_line = in.readLine(); // dim nx ny nz nt System.out.println("input: "+input_line); len = input_line.length(); index = 0; if(debug) System.out.println("index="+index); last = input_line.indexOf(' ',index); if(debug) System.out.println("last="+last); if(debug) System.out.println(input_line.substring(index,last)+ "| len="+(input_line.substring(index,last).length())); dim = Integer.parseInt(input_line.substring(index,last)); System.out.println("dim="+dim); index = input_line.indexOf(' ',index); // nx if(debug) System.out.println("index="+index); last = input_line.indexOf(' ',index+1); if(debug) System.out.println("last="+last); if(debug) System.out.println(input_line.substring(index+1,last)); nx = Integer.parseInt(input_line.substring(index+1,last)); System.out.println("nx="+nx); index = input_line.indexOf(' ',last); // ny if(debug) System.out.println("index="+index); last = input_line.indexOf(' ',index+1); if(debug) System.out.println("last="+last); if(debug) System.out.println(input_line.substring(index+1,last)); ny = Integer.parseInt(input_line.substring(index+1,last)); System.out.println("ny="+ny); index = input_line.indexOf(' ',last); // nz if(debug) System.out.println("index="+index); last = input_line.indexOf(' ',index+1); if(debug) System.out.println("last="+last); if(debug) System.out.println(input_line.substring(index+1,last)); nz = Integer.parseInt(input_line.substring(index+1,last)); System.out.println("nz="+nz); index = input_line.indexOf(' ',last); // nt if(debug) System.out.println("index="+index); last = input_line.indexOf(' ',index+1); if(last==-1) last = len; if(debug) System.out.println("last="+last); nt = Integer.parseInt(input_line.substring(index+1,last)); System.out.println("nt="+nt); nxyzt = nx*ny*nz*nt; System.out.println("about to allocate "+(nxyzt*(dim+1))+" doubles"); pt = new double[nxyzt][dim+1]; n_input = 0; // read data points, last index varies by 1 input_line = in.readLine(); // tested for short in 'while' while(input_line.length()>6) // exit on blank or short line { if(debug && n_input==0) System.out.println("input: "+input_line); len = input_line.length(); index = 0; // first x if(debug && n_input==0) System.out.println("index="+index); last = input_line.indexOf(' ',index+1); if(debug && n_input==0) System.out.println("last="+last); amount = Double.parseDouble(input_line.substring(index+1,last)); if(debug && n_input==0) System.out.println("amount="+amount); pt[n_input][0] = amount; index = input_line.indexOf(' ',last); // second y if(debug && n_input==0) System.out.println("index="+index); last = input_line.indexOf(' ',index+1); if(debug && n_input==0) System.out.println("last="+last); amount = Double.parseDouble(input_line.substring(index+1,last)); if(debug && n_input==0) System.out.println("amount="+amount); pt[n_input][1] = amount; index = input_line.indexOf(' ',last); // third z if(debug && n_input==0) System.out.println("index="+index); last = input_line.indexOf(' ',index+1); if(debug && n_input==0) System.out.println("last="+last); amount = Double.parseDouble(input_line.substring(index+1,last)); if(debug && n_input==0) System.out.println("amount="+amount); pt[n_input][2] = amount; index = input_line.indexOf(' ',last); // fourth t if(debug && n_input==0) System.out.println("index="+index); last = input_line.indexOf(' ',index+1); if(debug && n_input==0) System.out.println("last="+last); amount = Double.parseDouble(input_line.substring(index+1,last)); if(debug && n_input==0) System.out.println("amount="+amount); pt[n_input][3] = amount; index = input_line.indexOf(' ',last); // U if(debug && n_input==0) System.out.println("index="+index); last = input_line.indexOf(' ',index+1); if(last==-1) last = len; if(debug && n_input==0) System.out.println("last="+last); amount = Double.parseDouble(input_line.substring(index+1,last)); if(debug && n_input==0) System.out.println("amount="+amount); pt[n_input][4] = amount; if(n_input==0) { dxmin = pt[n_input][0]; dxmax = pt[n_input][0]; dymin = pt[n_input][1]; dymax = pt[n_input][1]; dzmin = pt[n_input][2]; dzmax = pt[n_input][2]; dtmin = pt[n_input][3]; dtmax = pt[n_input][3]; dUmin = pt[n_input][4]; dUmax = pt[n_input][4]; System.out.println("first x="+dxmin+", y="+dymin+", z="+dzmin); System.out.println("first t="+dtmin+", u="+dUmin); } else { dxmin = Math.min(dxmin,pt[n_input][0]); dxmax = Math.max(dxmax,pt[n_input][0]); dymin = Math.min(dymin,pt[n_input][1]); dymax = Math.max(dymax,pt[n_input][1]); dzmin = Math.min(dzmin,pt[n_input][2]); dzmax = Math.max(dzmax,pt[n_input][2]); dtmin = Math.min(dtmin,pt[n_input][3]); dtmax = Math.max(dtmax,pt[n_input][3]); dUmin = Math.min(dUmin,pt[n_input][4]); dUmax = Math.max(dUmax,pt[n_input][4]); } n_input++; if(n_input>=nxyzt) break; input_line = in.readLine(); // done with this line, read next } // end while } // end try catch(IOException exception) { System.out.println(exception); } System.out.println("read_data finished, n_input="+n_input); System.out.println("xmin="+dxmin+", xmax="+dxmax+", last="+pt[nxyzt-1][0]); System.out.println("ymin="+dymin+", ymax="+dymax+", last="+pt[nxyzt-1][1]); System.out.println("zmin="+dzmin+", zmax="+dzmax+", last="+pt[nxyzt-1][2]); System.out.println("tmin="+dtmin+", tmax="+dtmax+", last="+pt[nxyzt-1][3]); System.out.println("Umin="+dUmin+", Umax="+dUmax+", last="+pt[nxyzt-1][4]); for(int i=0; i