import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;

 public class Dtracker extends Frame  {
 static float len=0,wid=0,thi=0,hbot=0,htop=0,hapo=0,hthi=0;
 static float posx=0,posy=0,posz=0,xa=0,ya=0,za=0,angle=0;
 static float vposx[]=new float[2000];
 static float vposy[]=new float[2000];
 static float vposz[]=new float[2000];
 static float vhbot[]=new float[2000];
 static float vhtop[]=new float[2000];
 static float vhapo[]=new float[2000];
  static int nmodane[]={24,24,40,56,40,56,80};       
  static int nmodaneb[]={18,30,42,28,38,44,56,42,48,54,60,66,74};       
 static int vane[]=new int[2000];
 static int vmod[]=new int[2000];
 static Polygon pol[]=new Polygon[2000];
 static int nmoduli = 0;
 static int nlay = 0;
 static int var = 0;
 public static void main(String[] args) throws IOException
{
 Dtracker h = new Dtracker();
 Disegnatr appl = new Disegnatr(h);
 nlay=1;
 appl.init();
 h.add("Center",appl);
 h.pack();
 h.show();
}
static void leggi(int nlay) throws IOException{
 nmoduli=0;
 File inputFile = new File("bf.dat");
 StringTokenizer st;
 BufferedReader br = new BufferedReader(
    new FileReader (inputFile));
 String s ;
 int layer1;
 while(br.ready()){
  s = br.readLine();
 st = new StringTokenizer(s);
 String px = new String(st.nextToken());
 String br_fw = new String(st.nextToken());
 int layer = new Integer(st.nextToken()).intValue();
 int anello = new Integer(st.nextToken()).intValue();
 int nmod = new Integer(st.nextToken()).intValue();
 posx = new Float(st.nextToken()).floatValue();
 posy = new Float(st.nextToken()).floatValue();
 posz = new Float(st.nextToken()).floatValue();
 xa = new Float(st.nextToken()).floatValue();
 ya = new Float(st.nextToken()).floatValue(); 
 za = new Float(st.nextToken()).floatValue();
 angle = new Float(st.nextToken()).floatValue();
  var = new Integer(st.nextToken()).intValue();
 if(var == 3) {
  len = new Float(st.nextToken()).floatValue();
  wid = new Float(st.nextToken()).floatValue();
  thi = new Float(st.nextToken()).floatValue();
 
}else{
  hbot = new Float(st.nextToken()).floatValue();
  htop = new Float(st.nextToken()).floatValue();
  hapo = new Float(st.nextToken()).floatValue();
  hthi = new Float(st.nextToken()).floatValue();
}
layer1=layer;if(br_fw.equals("barrel"))layer1=layer1+28;
if(layer1==nlay){
vane[nmoduli]=anello;vmod[nmoduli]=nmod;
vposx[nmoduli]=posx;
vposy[nmoduli]=posy;
vposz[nmoduli]=posz;
if(var==4){vhtop[nmoduli]=htop;        
vhbot[nmoduli]=hbot;
vhapo[nmoduli]=hapo;
} else {
vhtop[nmoduli]=len;
vhapo[nmoduli]=wid;
}
nmoduli++;
}
}
br.close();  
System.out.println(nlay+" "+nmoduli);
}
Dtracker(){ setTitle("Tracker layer");}
}
class Disegnatr extends Applet{
Dtracker fr;
  Image o;  Graphics og,bg;
  BufferedImage buf;
 double xmin=-2.0, xmax=2.0, ymin=-2.0, ymax=2.0;
 Polygon c;
 int xsize = 400,ysize=200;
public Dimension getPreferredSize(){ 
                        return (new Dimension(1200,1400)); 
                      } 
Disegnatr(Dtracker f){ fr=f;
}
public void init() {


}
synchronized public void paint( Graphics g) 
{
int ix=0,iy=0;
 g.setColor(Color.white);
 g.fillRect(0,0,getSize().width,getSize().height);
  if(o==null||buf==null){
   o = createImage(xsize,ysize);
  buf = new
 BufferedImage(3*xsize,14*ysize,BufferedImage.TYPE_INT_BGR);
    } else {
     og = o.getGraphics();
  bg = buf.createGraphics();
     
 for(fr.nlay=14;fr.nlay>0;fr.nlay--){
 xmin=-2.;ymin=-2.;xmax=2.;ymax=2.;
 if(fr.nlay >12 && fr.nlay <17){
 xmin=-.70;xmax=.70;ymin=-.70;ymax=.70;
 }
 if(fr.nlay >28 ){
 xmin=-2.5;xmax=2.5;ymin=-1.;ymax=28.;
 if(fr.nlay <32){xmin=-1.;xmax=1.;}
  }
      try {
  fr.leggi(fr.nlay);
        } catch (IOException e) {
           System.out.println("errore IO");
        }
 fillimage();
 if(fr.nlay==14){ix =0;iy=-ysize;}
 if(fr.nlay==15){ix=2*xsize;iy=-ysize;}
 if(fr.nlay==29){ix=xsize;iy=-ysize;}
 iy = iy+ysize;
 System.out.println(ix+" "+iy);
 g.drawImage(o,ix,iy,this);
 bg.drawImage(o,ix,iy,null);
  }
 for(fr.nlay=15;fr.nlay<42;fr.nlay++){
 xmin=-2.;ymin=-2.;xmax=2.;ymax=2.;
 if(fr.nlay >12 && fr.nlay <17){
 xmin=-.70;xmax=.70;ymin=-.70;ymax=.70;
 }
 if(fr.nlay >28 ){
 xmin=-2.5;xmax=2.5;ymin=-1.;ymax=28.;
 if(fr.nlay <32){xmin=-1.;xmax=1.;}
  }
      try {
  fr.leggi(fr.nlay);
        } catch (IOException e) {
           System.out.println("errore IO");
        }
 fillimage();
 if(fr.nlay==1){ix =0;iy=-ysize;}
 if(fr.nlay==15){ix=2*xsize;iy=-ysize;}
 if(fr.nlay==29){ix=xsize;iy=-ysize;}
 iy = iy+ysize;
 System.out.println(ix+" "+iy);
 g.drawImage(o,ix,iy,this);
 bg.drawImage(o,ix,iy,null);

}
  }
try{
 bg.setColor(Color.white);
 bg.fillRect(xsize,13*ysize,xsize,ysize);
    File theFile = new File("mappa.jpeg");
    JPEGCodec.createJPEGEncoder(new FileOutputStream(theFile)).encode(buf);
    System.out.println(bg+" "+buf);
 }catch(java.io.IOException ioe){System.out.println(ioe.getMessage());

  }

}
synchronized void fillimage(){
 og.setColor(Color.white);
 og.fillRect(0,0,xsize,ysize);
 og.setColor(Color.red);
  for(int i=0;i<fr.nmoduli;i++){
  c = conv(i,og);
  fr.pol[i]=c;
  }
}
    int xpixel(double x){
        return(int)((x-xmin)/(xmax-xmin)*xsize);
    }
    int ypixel(double y){
        double y1;
        y1 = (y-ymin)/(ymax-ymin);
        return ysize-(int)(y1*ysize);
    }         
synchronized Polygon conv(int i,Graphics g){
   int x,y;
   int nmodulo=fr.vmod[i];
   Polygon c = new Polygon();
 int px[]= new int[4];
 int py[]= new int[4];
 double r,phi;
 double xp[]=new double[4],yp[]=new double[4];
 double xp1,yp1;
  phi=Math.atan(fr.vposy[i]/fr.vposx[i]);
  if(fr.vposy[i] < 0.)phi=phi+Math.PI;
  if(Math.abs(fr.vposy[i])<0.000001&&fr.vposx[i]>0)phi=0;
  if(Math.abs(fr.vposy[i])<0.000001&&fr.vposx[i]<0)phi=Math.PI;
  if(Math.abs(fr.vposx[i])<0.000001&&fr.vposy[i]>0)phi=Math.PI/2.;
  if(Math.abs(fr.vposx[i])<0.000001&&fr.vposy[i]<0)phi=-Math.PI/2.;
  if(phi < 0.)phi = 2.*Math.PI +phi;
  if((fr.nlay < 13 || fr.nlay >16) && fr.nlay<29)nmodulo=(int)((phi/(2.*Math.PI))*fr.nmodane[fr.vane[i]-1]+.1)+1;
  if(fr.nlay > 12 && fr.nlay<17)nmodulo=(int)((phi/(2.*Math.PI))*24+.1)+1;
  if( fr.nlay>28)nmodulo=(int)((phi/(2.*Math.PI))*fr.nmodaneb[fr.nlay-29]+.1)+1;
  if(fr.nlay < 29){
  r = Math.sqrt(fr.vposx[i]*fr.vposx[i]+fr.vposy[i]*fr.vposy[i]);
  //System.out.println(i+" "+r+" "+phi+" " + fr.vposx[i]+" "+fr.vposy[i]+" "+fr.vane[i]+" "+fr.vmod[i]);
if(fr.nlay >12 && fr.nlay <17){
  r = r*4;
  xp[0]=r-fr.vhapo[i];yp[0]=-fr.vhtop[i];
  xp[1]=r+fr.vhapo[i];yp[1]=-fr.vhtop[i];
  xp[2]=r+fr.vhapo[i];yp[2]=fr.vhtop[i];
  xp[3]=r-fr.vhapo[i];yp[3]=fr.vhtop[i];
  }else{
  r = r + r/3.;
  xp[0]=r-fr.vhapo[i];yp[0]=-fr.vhbot[i];
  xp[1]=r+fr.vhapo[i];yp[1]=-fr.vhtop[i];
  xp[2]=r+fr.vhapo[i];yp[2]=fr.vhtop[i];
  xp[3]=r-fr.vhapo[i];yp[3]=fr.vhbot[i];
   }
for(int j=0;j<4;j++){
    xp1 = xp[j]*Math.cos(phi)-yp[j]*Math.sin(phi); 
    yp1 = xp[j]*Math.sin(phi)+yp[j]*Math.cos(phi); 
    xp[j] = xp1;yp[j]=yp1;
  x=xpixel(xp[j]);y=ypixel(yp[j]);
   c.addPoint(x,y);
   //System.out.println(x+" "+y);
    }
  } else {
 // Barrel
  xp[0]=fr.vposz[i]+fr.vposz[i]/1.-fr.vhapo[i];yp[0]=4.*phi-fr.vhtop[i];
  xp[1]=fr.vposz[i]+fr.vposz[i]/1.+fr.vhapo[i];yp[1]=4.*phi-fr.vhtop[i];
  xp[2]=fr.vposz[i]+fr.vposz[i]/1. +fr.vhapo[i];yp[2]=4.*phi+fr.vhtop[i];
  xp[3]=fr.vposz[i]+fr.vposz[i]/1.-fr.vhapo[i];yp[3]=4.*phi+fr.vhtop[i];
for(int j=0;j<4;j++){
  x=xpixel(xp[j]);y=ypixel(yp[j]);
   c.addPoint(x,y);
   //System.out.println(x+" "+y);
    }
    }
// g.drawString(new Integer(fr.nlay).toString(),10,20);
  g.setColor(Color.red);
 for(int k=0;k<i;k++){
   if(c.xpoints[0]==fr.pol[k].xpoints[0] && c.ypoints[0]==fr.pol[k].ypoints[0] )   {g.setColor(Color.blue);break;}
   }
  g.drawPolygon(c);
   return c;

}

}

