/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
 */
package asc2pov;

import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/**
 *
 * @author mario
 */
public class Asc2Pov {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String nombre;
        String cara="";
        int vertices=0, caras=0;
        Path path = Path.of("/home/mario/povs/jarritos/trenecito.ASC");
        //System.out.println("Empezamos !!");
        try {
           List<String> lineas = Files.readAllLines(path);
           String linea[]=lineas.get(3).split(" ");
           vertices=Integer.parseInt(linea[2]);
           caras=Integer.parseInt(linea[4]);
           //System.out.println(vertices+" "+caras);
           FileWriter writer = writer = new FileWriter("/home/mario/povs/jarritos/temporal");
           writer.write(""+vertices+"\n");
           writer.write(""+caras+"\n");
           int lin=1;
           for (String x : lineas){
              if (lin>=6 &&x.contains("Vertex") && x.contains("X")){
                //System.out.println(x);
                String v[]=x.split(":");
                String X=(v[2].split(" "))[0];
                String Y=(v[3].split(" "))[0];
                String Z=v[4];
                writer.write(X+","+Y+","+Z);
                writer.write("\n");
              }
         
              if (x.contains("Face") && x.contains("A:") && x.contains("B:") && x.contains("C:")){
                String face[]=x.split(":");
                String a=face[2];
                String b=face[3];
                String c=face[4];  
                String v1=a.split(" ")[0];
                String v2=b.split(" ")[0];
                String v3=c.split(" ")[0];
                cara=v1+" "+v2+" "+v3;  
                //System.out.println(cara);
              }
              if (x.contains("Material:")){
                String colores[]=x.split("\"");
                String rojo=(colores[1].split("g"))[0].split("r")[1];
                String verde=(colores[1].split("b"))[0].split("g")[1];
                String azul=(colores[1].split("a"))[0].split("b")[1];
                writer.write(cara+" "+rojo+" "+verde+" "+azul+"\n");
              }
              lin++;
           }
           writer.close();
           //System.out.println("Listo !!");
        }catch(Exception e){
            System.out.println(e.getMessage());
        }  
        
        //System.out.println(vertices);
        //System.out.println(caras);
        //System.out.println(vertices+caras);
        
        try{
           Path path2 = Path.of("/home/mario/povs/jarritos/temporal");
           FileWriter wSalida = new FileWriter("/home/mario/povs/jarritos/trenecito.pov");
           List<String> lineas2 = Files.readAllLines(path2);
           int posInicioCaras=2+vertices;
           String partes[];
           wSalida.write("//Ejemplo de prueba para una escena vista 3D}\n");
           wSalida.write("//Por: Mario H Tiburcio Z\n");

           wSalida.write("#include \"colors.inc\"\n");
           wSalida.write("#include \"librerias.inc\"\n");

           wSalida.write("camera {\n");
           wSalida.write("   location <6,6,-6>\n");
           wSalida.write("   look_at <0,0,0>\n");
           wSalida.write("}\n");

           wSalida.write("light_source {<4,5,-6> color White}\n");
           for (int l=posInicioCaras; l<vertices+caras+2; l++){
               partes=(lineas2.get(l)).split(" ");
               
               wSalida.write("triangle{\n");
               int va=Integer.parseInt(partes[0]);
               int vb=Integer.parseInt(partes[1]);
               int vc=Integer.parseInt(partes[2]);
               wSalida.write("  <"+lineas2.get(va+2)+">,\n");
               wSalida.write("  <"+lineas2.get(vb+2)+">,\n");
               wSalida.write("  <"+lineas2.get(vc+2)+">\n");          
               wSalida.write("  pigment {color <"+partes[3]+","+partes[4]+","+partes[5]+">/255}\n");
               wSalida.write("}\n");
           }
           System.out.println("Listo !!");
           wSalida.close();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }    
   
        }

    }
    