/* Author:  WeiChung Hsu
 */

import java.io.*;

public class Ply_3D 
{    
	String filename;
    float[] myX;
    float[] myY;
	float[] myZ;
	int[][] myPly;
	
	double maxX=0;
	double maxY=0;
	double maxZ=0;
	double minX=0;
	double minY=0;
	double minZ=0;
	double dif_L=0;
	double dif_W=0;
	double dif_H=0;
	double scale = 100;
	
	double translate_x = 121.32856;
	double translate_y = 25.01259;
	double translate_z = 100.0;
		
	int total_v;
	int total_ply;
	int state = 0;
	int reading_v=0;
	int reading_ply=0;
	int list_num=0;
	
	
	//final String outputFilename = "test.kml"; // OUPTUT FILENAME	
	//PrintWriter outp;
	
    
 // constructor   
    public Ply_3D(){
    	}
	public void translateTo(double x, double y, double z){
		translate_x = x;
		translate_y = y;
		translate_z = z;
	}
	public void scaleTo(double s){
		scale = s;
	}	
	
	public void openfile(String tempstr){
		String record = null;
		String tokens[] = new String[30];
        int recCount = 0; 
		filename = tempstr;
		double earthradius=3963.189  * 5280;  // 1 mile = 5280 ft
		float radiusL;
		float radiusW;
		float degreeL;    	
		float degreeW;

		try { 
			FileReader fr     = new FileReader(tempstr);
			BufferedReader br = new BufferedReader(fr); 
			record = new String();
			while ((record = br.readLine()) != null) {
				tokens = record.split(" ");
				if(tokens[0].equals("element"))
				{
					//System.out.println("hit element");
					if(tokens[1].equals("vertex"))
					{
						total_v = Integer.parseInt(tokens[2]);
						myX =  new float[total_v];
						myY =  new float[total_v];
						myZ =  new float[total_v];
						
						//System.out.println("hit vertex " + total_v);
					}
					else
					{
						total_ply = Integer.parseInt(tokens[2]);
						myPly = new int[total_ply][3];
						//System.out.println("hit vertex " + total_ply);
					}
				}
				else if(tokens[0].equals("end_header"))
				{
					state = 1;
					reading_v=0;
				}
				else if(state == 1)
				{	
					myX[reading_v] = Float.parseFloat(tokens[0]);
					myY[reading_v] = Float.parseFloat(tokens[1]);
					myZ[reading_v] = Float.parseFloat(tokens[2]);					
					
					if(maxX < myX[reading_v])
						maxX = myX[reading_v];
					if(maxY < myY[reading_v])
						maxY = myY[reading_v];
					if(maxZ < myZ[reading_v])
						maxZ = myZ[reading_v];
					if(minX > myX[reading_v])
						minX = myX[reading_v];
					if(minY > myY[reading_v])
						minY = myY[reading_v];
					if(minZ > myZ[reading_v])
						minZ = myZ[reading_v];
						
					
					reading_v++;
					if(reading_v == total_v)
					{
						state = 2;
						reading_ply = 0;
					}
				}
				else if(state == 2)
				{
					list_num = Integer.parseInt(tokens[0]);
					myPly[reading_ply][0] = Integer.parseInt(tokens[1]);
					myPly[reading_ply][1] = Integer.parseInt(tokens[2]);
					myPly[reading_ply][2] = Integer.parseInt(tokens[3]);
					reading_ply++;
					if(reading_ply==total_ply)
						state = 3;
				}			  
           }
		   
			dif_L=maxX - minX;
			dif_W=maxY - minY;
			dif_H=maxZ - minZ;
			int i;
			for(i=0;i<total_v;i++)
			{

				myX[i] = ((myX[i]-(float)minX)*(float)scale)/((float)dif_L);
				myY[i] = ((myY[i]-(float)minY)*(float)scale)/((float)dif_W);
				myZ[i] = ((myZ[i]-(float)minZ)*(float)scale)/((float)dif_H);
					
					
					
					radiusL = (float) Math.asin((myX[i]/2)/earthradius);
					radiusW = (float) Math.asin((myY[i]/2)/earthradius);
					degreeL = (float) Math.toDegrees(radiusL);    	
					degreeW = (float) Math.toDegrees(radiusW); 
					myX[i] = degreeL;
					myY[i] = degreeW;
					
					
			}

		   
		   
			
        } catch (IOException e) { 
           // catch possible io errors from readLine()
           System.out.println("Uh oh, got an IOException error!");
           e.printStackTrace();
        }
	}
	
	
	
	
	public void renderKML(PrintWriter outp){
	int i;	
			for(i=0;i<total_ply;i++)	//		for(i=0;i<10;i++)
		{
				
		outp.println("<Placemark>");
	    outp.println("<name> a </name>");		
		
		outp.println(" <Polygon>");
		outp.println("<extrude>0</extrude>");
    	outp.println("<altitudeMode>relativeToGround</altitudeMode>");
		outp.println(" <outerBoundaryIs>");
		outp.println("   <LinearRing>");
		
			outp.println("    <coordinates>");
			outp.println(""+ (myX[myPly[i][0]]+translate_x) +','+ (myY[myPly[i][0]]+translate_y)  +','+ (myZ[myPly[i][0]]+translate_z));
			outp.println(""+ (myX[myPly[i][1]]+translate_x) +','+ (myY[myPly[i][1]]+translate_y)  +','+ (myZ[myPly[i][1]]+translate_z));
			outp.println(""+ (myX[myPly[i][2]]+translate_x) +','+ (myY[myPly[i][2]]+translate_y)  +','+ (myZ[myPly[i][2]]+translate_z));
			outp.println(""+ (myX[myPly[i][0]]+translate_x) +','+ (myY[myPly[i][0]]+translate_y)  +','+ (myZ[myPly[i][0]]+translate_z));
			outp.println("     </coordinates>");	
			outp.println("  </LinearRing>");
		outp.println(" </outerBoundaryIs>");
		outp.println("</Polygon>");		
		outp.println("</Placemark> ");
	
		}
		
	
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/*
	public void printhr()
	{
		outp.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		outp.println("<kml xmlns=\"http://earth.google.com/kml/2.0\">\n");
		
	// Add temprory	
		outp.println("<Folder>");
    	outp.println("<description>Blah blah</description>");
    	outp.println("<name>PLT_kml_test</name>");
    	outp.println("<open>0</open>");
		
		}

// Print kml file tail
	public void printtl()
	{
		// Add temprory	
		outp.println("</Folder>");
		outp.append("</kml>\n\n");
		}	
	public void creatkml()
		{

		System.out.print("Trying to create KML script file...\n");
		try{
			PrintWriter out = new PrintWriter(outputFilename);
			outp = out;
			printhr();
		}
		catch (IOException exception)
		{
			System.out.println("Error processing file:   " + exception);
		}
		
		
	}
	
// Close kml file PrintWriter file description and print tail	
	public void endkml()
		{
		printtl();
		outp.close();
		}
	*/
	
	
	

 
}
