import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import Point3D;
import Matrix3D;

public class MyTest1 extends Applet {
  int current_choice;
  private Choice trans_choice;
  float xinit, yinit;

  final static int CHUNKSIZE = 100;
  Raster raster;
  Image screen;
  Point3D vertList[];
  Point3D worldList[];
  Point3D tranList[];
    int vertices;
  Matrix3D view;
  Matrix3D model;
  
  float eyex, eyey, eyez;
  float lookatx, lookaty, lookatz;
  float upx, upy, upz;
  float fov;
  
  public void init( )
  {

    trans_choice = new Choice();
    trans_choice.addItem("Translation");
    trans_choice.addItem("Scale");
    //trans_choice.addItem("Skew");
    trans_choice.addItem("Shear");
    trans_choice.addItem("Rotate");
    this.add(trans_choice);
    current_choice = 1;
    
    raster = new Raster(size().width, size().height);
    raster.fill(getBackground());
    screen = raster.toImage();
    
    eyex = 0;        eyey = 0;        eyez = -10;
    lookatx = 0;     lookaty = 0;     lookatz = 0;
    upx = 0;         upy = 1;         upz = 0;
    fov = 30;
    
    vertList = new Point3D[CHUNKSIZE];
    vertices = 0;
    
    String filename = getParameter("datafile");
    showStatus("Reading "+filename);
    InputStream is = null;
    try {
      is = new URL(getDocumentBase(), filename).openStream();
      ReadInput(is);
      is.close();
    } catch (IOException e) {
      showStatus("Error reading "+filename);
    }
    
    worldList = new Point3D[vertList.length];
    tranList = new Point3D[vertList.length];
    for (int i = 0; i < vertices; i++) {
      worldList[i] = new Point3D();
      tranList[i] = new Point3D();
        }
    
    view = new Matrix3D(raster);
    float t = (float) Math.sin(Math.PI*(fov/2)/180);
    float s = (t*size().height)/size().width;
    
    view.perspective(-t, t, -s, s, -1, -200);
    view.lookAt(eyex, eyey, eyez, lookatx, lookaty, lookatz, upx, upy, upz);
    
    model = new Matrix3D();
    long time = System.currentTimeMillis();
    model.transform(vertList, worldList, 0, vertices);
    view.transform(worldList, tranList, 0, vertices);
    
    DrawObject();
    time = System.currentTimeMillis() - time;
    showStatus("Time = "+time+" ms");
    screen = raster.toImage();
  }

  public boolean action(Event e, Object arg) {
    if (e.target == trans_choice) {
      if (arg.equals("Translation")) current_choice = 1;
      if (arg.equals("Scale")) current_choice = 2;
      //      if (arg.equals("Skew")) current_choice = 3;
      if (arg.equals("Shear")) current_choice = 4;
      if (arg.equals("Rotate")) current_choice = 5;
    }
    return true;
  }
  

    private double getNumber(StreamTokenizer st) throws IOException
  {
    if (st.nextToken() != StreamTokenizer.TT_NUMBER) {
            System.err.println("ERROR: line "+st.lineno()+": expected number");
            throw new IOException(st.toString());
    }
        return st.nval;
  }

    public void ReadInput(InputStream is) throws IOException
  {
            StreamTokenizer st = new StreamTokenizer(is);
        st.commentChar('#');
  scan: while (true) {
    switch (st.nextToken()) {
    default:
                        break scan;
    case StreamTokenizer.TT_WORD:
      if (st.sval.equals("v")) {
                            float x = (float) getNumber(st);
                            float y = (float) getNumber(st);
                            float z = (float) getNumber(st);
                            if (vertices == vertList.length) {
                                Point3D newList[] = new Point3D[vertList.length+CHUNKSIZE];
                        System.arraycopy(vertList, 0, newList, 0, vertList.length);
                                vertList = newList;
                            }
                            vertList[vertices++] = new Point3D(x, y, z);
      } else
	if (st.sval.equals("eye")) {
                    eyex = (float) getNumber(st);
                    eyey = (float) getNumber(st);
                            eyez = (float) getNumber(st);
	} else
	  if (st.sval.equals("look")) {
                    lookatx = (float) getNumber(st);
                    lookaty = (float) getNumber(st);
                            lookatz = (float) getNumber(st);
	  } else
	    if (st.sval.equals("up")) {
                    upx = (float) getNumber(st);
                    upy = (float) getNumber(st);
                            upz = (float) getNumber(st);
	    } else
	      if (st.sval.equals("fov")) {
                    fov = (float) getNumber(st);
	      } else {
                    System.err.println("ERROR: line "+st.lineno()+": unexpected token :"+st.sval);
                    break scan;
	      }
                            break;
    }
                if (vertices % 100 == 0) showStatus("vertices = " + vertices);
  }
        is.close();
	if (st.ttype != StreamTokenizer.TT_EOF) {
                throw new IOException(st.toString());
	}
  }

    public void paint(Graphics g)
  {
        g.drawImage(screen, 0, 0, this);
  }

    public void update(Graphics g)
  {
        paint(g);
  }

    float v0x, v0y, v0z;

    public boolean mouseDown(Event e, int x, int y)
  {
    if (e.metaDown()) {
      showStatus("Resetting model matrix");
      model.loadIdentity();
      showStatus("Redrawing initial picture");
      DrawObject();
    }

        v0x = (float) (x - (size().width / 2));
        v0y = (float) ((size().height / 2) - y);
        v0z = (float) size().width;
        float l0 = (float) (1 / Math.sqrt(v0x*v0x + v0y*v0y + v0z*v0z));
        v0x *= l0;
        v0y *= l0;
        v0z *= l0;

	xinit = x;
	yinit = y;
	return true;
  }

    public boolean mouseDrag(Event e, int x, int y)
  {
    if (e.metaDown()) {
            showStatus("Resetting model matrix");
    } else {
            float v1x = (float) (x - (size().width / 2));
            float v1y = (float) ((size().height / 2) - y);
            float v1z = (float) size().width;
            float l = (float) (1 / Math.sqrt(v1x*v1x + v1y*v1y + v1z*v1z));
            v1x *= l;
            v1y *= l;
            v1z *= l;

            float ax = v0y*v1z - v0z*v1y;
            float ay = v0z*v1x - v0x*v1z;
            float az = v0x*v1y - v0y*v1x;
            l = (float) Math.sqrt(ax*ax + ay*ay + az*az);
            float theta = (float) Math.asin(l);
            if (v0x*v1x + v0y*v1y + v0z*v1z < 0)
                theta += (float) Math.PI / 2;

	    
	    //	    System.out.println("ax=" + ax + ", ay=" + ay + ", az=" + az + "\n");
	    //	    System.out.println("x=" + x + ", y=" + y + "\n");
	    //	    System.out.println("xinit=" + xinit + ", yinit=" + yinit + "\n");
	    

	    if (current_choice==1) {
	      showStatus("Translating");
	      model.translate((x-xinit)/size().width, (yinit-y)/size().height, 0);
	    }
	    else if (current_choice==2) {
	      showStatus("Scaling");
	      model.scale(1+(x-xinit)/size().width, 1+(yinit-y)/size().height, 0);
	    }
	    /*
	      else if (current_choice==3) {
	      showStatus("Skewing");
	      model.skew((x-xinit)/size().width, (yinit-y)/size().height, 0);
	      }
	      */
	    else if (current_choice==4) {
	      showStatus("Shearing");
	      model.shear(ax, ay, az);
	    }
	    else if (current_choice==5) {
	      showStatus("Rotating");
	      model.rotate(ax, ay, az, theta);
	    }
	    
            v0x = v1x;
            v0y = v1y;
            v0z = v1z;
            model.transform(vertList, worldList, 0, vertices);
            view.transform(worldList, tranList, 0, vertices);
            DrawObject();
    }
    screen = raster.toImage();
    repaint();
    return true;
  }
  
  void DrawObject()
  {
    int ix, iy;
    int w, h;
    w = raster.width;
    h = raster.height;
    raster.fill(getBackground());
    for (int i = 0; i < vertices; i++) {
      ix = (int) tranList[i].x;
      iy = (int) tranList[i].y;
      if (ix >= 0 && iy >= 0 && ix < w && iy < h)
	raster.setPixel(0xffa000a0, ix, iy);
    }
  }
  
}
