import java.io.*;
import java.util.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.BranchGroup;
import com.sun.j3d.utils.applet.MainFrame;
import javax.media.j3d.*;
import java.awt.*;
import java.awt.BorderLayout;
import com.sun.j3d.utils.applet.MainFrame;
import org.j3d.geom.Torus;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.vp.*;
import com.sun.j3d.utils.behaviors.keyboard.*;

public class Texttest extends  java.applet.Applet {   

 
 
 public Texttest(double Vertices[][][],int Facets[][],int VertexNo[][],int VerticesNo[],int SumOfFacets[],int SumOfFacetsNo[]) {
        
                        
                
                //setting the appearance of torus
                Appearance app = new Appearance();
  Material mat = new Material();
  PolygonAttributes polyAtt= new
PolygonAttributes(PolygonAttributes.POLYGON_FILL,PolygonAttributes.CULL_BACK
, 0.0f);

  app.setMaterial(mat);
  app.setPolygonAttributes(polyAtt);
                
   
   GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

        
        
                Canvas3D canvas = new Canvas3D(config);               
        SimpleUniverse universe = new SimpleUniverse();
                
                add("Center",canvas);
                                        
   BranchGroup group = new BranchGroup();
   
   //create the polyhedron(s)
   
   
        
                
                Polyhedron polyhedron[]=new Polyhedron[Vertices.length];
        
   //Exception in thread "main" java.lang.NullPointerException

   //ADD THE POLYHEDRON(s)
   
   for(int i=0;i<Vertices.length;i++) {
        
    group.addChild(polyhedron[i].getPolyedron(Vertices,Facets,VertexNo,VerticesNo[i],SumOfFacets[i],SumOfFacetsNo[i],i));
        }
    
    
    
    //setting the Light as well as its color
     Color3f light1Color = new Color3f(1.0f, 1.0f, 0.0f);
     Vector3f light1Direction = new Vector3f(4.0f, -7.0f, 12.0f);
     
     DirectionalLight dl = new
DirectionalLight(light1Color,light1Direction);

//    AmbientLight light=new AmbientLight(light1Color); Psilomalakies einai
    BoundingSphere bounds =    new BoundingSphere(new Point3d(0.0, 0.0,
0.0), 100.0);
    dl.setInfluencingBounds(bounds);
    
    group.addChild(dl);
   
   
   //add a colorful background
   
   Background background = new Background(new Color3f(0.0f, 0.0f, 1.0f));
    background.setApplicationBounds(bounds);
    
     group.addChild(background);
    
    //changing the viewer point
   TransformGroup vpTG = null;
    Transform3D vpT3D = new Transform3D();

    vpTG = universe.getViewingPlatform().getViewPlatformTransform();
    
    // add orbit behavior to ViewingPlatform
   OrbitBehavior orbit = new OrbitBehavior(canvas,OrbitBehavior.REVERSE_ALL |
     OrbitBehavior.STOP_ZOOM);
   orbit.setSchedulingBounds(bounds);
   
       

   universe.getViewingPlatform().setViewPlatformBehavior(orbit);


    vpT3D.lookAt(new Point3d(10.0, 0.0, 0.0),
                 new Point3d(0.0, 0.0, 0.0),
                 new Vector3d(0.0, 0.0, 10.0));

    vpT3D.invert();

    vpTG.setTransform(vpT3D);

   
   
   
   
   
   
   
   universe.addBranchGraph(group);      
                
        
        
        
        
        
}
 
 
 /*----------------------------------------Edo Ksekina h Main()-------------------------*/
 
 
 
 
 public static void main(String[] args) 
 throws IOException {           
 
        FileInputStream theFile=new FileInputStream("solid4.txt"); // edo ka8orizo to arxeio
        
        BufferedReader br = new BufferedReader(new InputStreamReader(theFile));
        
        String c=null;
        
        
        double[][][] Vertices;
        int[][] Facets;
        int[][] FacetsNumber;
        int[][] VertexNo;
        int[] VerticesNo;//Pinakas periexei to plh8os ton shmeion ka8e polyedrou
        int[] SumOfFacets;//Pinakas periexei to plh8os ton pleuron ka8e polyedrou
        int[] SumOfFacetsNo;//Pinakas periexei to plh8os ton pleuron ka8e polyedrou
        int FacetsNo=0;
        int FacetsPointsNo=0;
        
        int NumberOfPolyhedron=0; //ari8mos polyedron
        String AllTheString=new String();
        String temp;
        String temp1;
        
        
        
        while((c=br.readLine())!=null) {
        
        AllTheString+=c+" ";
                
  }
        
        int comparefacets=0;                          
        int comparefacetsno=0;
        /*------------------Scanning the File No1------------------------*/
        
  
  StringTokenizer stri = new StringTokenizer(AllTheString," ");               
  
        while(stri.hasMoreTokens()){
                
        temp = stri.nextToken();
        
//Evresh tou ari8mou ton Vertex, Facets
                
                
                if(temp.equals("BEGIN")) {
                        
                        temp1 = stri.nextToken();
                        
                        if(temp1.equals("VERTICES")) {
                          
                          //edo metra ta polyedra se ka8e arxeio
                          NumberOfPolyhedron++;
                          
                            
                }
                
                
                
                if(temp1.equals("FACETS")) {
                          
                          
                            enterLoop2://here the loop2 ends
                                while(true) {
                                        String facet=stri.nextToken().replaceAll(":","");
                                        if(facet.equals("END")) {break enterLoop2;}
                                        
                                        
                                        String checkit=stri.nextToken();
                                        
                                        while(!checkit.endsWith(";")) { 
                                        
                                        FacetsPointsNo++;checkit=stri.nextToken();    }
                                        
                                        
                                        if(checkit.endsWith(";")) {
                                                
                                                
                                                
                                                FacetsPointsNo++;
                                                
                                                }
                                                
                                                
                                                
                                FacetsNo++;     
                        
                                }          
                 if(comparefacets<FacetsPointsNo)
                 {comparefacets=FacetsPointsNo;}
                                  
                 FacetsPointsNo=0;
                 
                 
                 if(comparefacetsno<FacetsNo)
                 {comparefacetsno=FacetsNo;}
                                  
                 FacetsNo=0;
                 
                 
             
            }
             
            
                }
        
        }//End While
        
        
        
        
        SumOfFacets=new int[NumberOfPolyhedron];
        SumOfFacetsNo=new int[NumberOfPolyhedron];
        
        VerticesNo=new int[NumberOfPolyhedron];
        NumberOfPolyhedron=0;
        FacetsNo=0;
        
        
        /*------------------Scanning the File No2------------------------*/
        
  
  StringTokenizer str = new StringTokenizer(AllTheString," ");                
  
        while(str.hasMoreTokens()){             
        temp = str.nextToken();
        
//Evresh tou ari8mou ton Vertex, Facets
                
                
                if(temp.equals("BEGIN")) {
                        
                        temp1 = str.nextToken();
                        
                        if(temp1.equals("VERTICES")) {
                          
                          
                          
                            enterLoop1://here the loop1 ends
                                while(true) {
                                        String vertex=str.nextToken().replaceAll(":","");
                                        if(vertex.equals("END")) {break enterLoop1;}
                                        
                                        int vertexNo=Integer.parseInt(vertex);
                                        str.nextToken();str.nextToken();str.nextToken();
                                        
                                        
                                        //metra ta shmeia tou ka8e polyedrou
                                        
                                        VerticesNo[NumberOfPolyhedron]++;                                       }          
                                        
            
                    
                  
                
            }
            
                     
            
            if(temp1.equals("FACETS")) {
                          
                          
                            enterLoop2://here the loop2 ends
                                while(true) {
                                        String facet=str.nextToken().replaceAll(":","");
                                        if(facet.equals("END")) {break enterLoop2;}
                                        
                                        
                                        String checkit=str.nextToken();
                                        
                                        while(!checkit.endsWith(";")) { 
                                        
                                        FacetsPointsNo++;checkit=str.nextToken();     }
                                        
                                        
                                        if(checkit.endsWith(";")) {
                                                
                                                
                                                
                                                FacetsPointsNo++;
                                                }
                                                
                                                
                                                
                                FacetsNo++;     
                        
                                }          
            SumOfFacets[NumberOfPolyhedron]=FacetsPointsNo;
            //System.out.println(FacetsPointsNo);
            FacetsPointsNo=0;
            
            SumOfFacetsNo[NumberOfPolyhedron]=FacetsNo;
            
            FacetsNo=0;
            
                    
            //edo metra ta polyedra se ka8e arxeio
                          NumberOfPolyhedron++;
                           
            }
            
            
        }
    
        
        //Everesh tou Torus
        /*else if(temp.equals("TORUS")) {
                
                
                System.out.println("John");
        }*/
        
        
     } //Ends While
        
       
       
//Evresh tou megaluterou ari8mo koryfon VerticesNo
        int GreatestNumberOfVertices=0;
for(int i=0;i<VerticesNo.length;i++) {
        if(GreatestNumberOfVertices<VerticesNo[i]) {
                GreatestNumberOfVertices=VerticesNo[i];
        }
}
        
       
        
        
                

        
        //Ka8orismos pinakon
          
          Vertices=new double[NumberOfPolyhedron][GreatestNumberOfVertices][3];         
          Facets=new int[NumberOfPolyhedron][comparefacets];
          VertexNo=new int[NumberOfPolyhedron][comparefacetsno];
          int pol=0;

        
        
        /*--------------------------------Gemisma pinakon--------------------------------*/

StringTokenizer st = new StringTokenizer(AllTheString," ");           


        while(st.hasMoreTokens()){
                
        temp = st.nextToken();
        
//Gemisma
                
                
                if(temp.equals("BEGIN")) {
                        
                        temp1 = st.nextToken();
                        
                        if(temp1.equals("VERTICES")) {
                                
                                                                
                                                          
                            enterLoop1://here the loop1 ends
                                while(true) {
                                        String vertex=st.nextToken().replaceAll(":","");
                                        if(vertex.equals("END")) {break enterLoop1;}
                                        
                                        int vertexNo=Integer.parseInt(vertex);
                                        
                                        Vertices[pol][vertexNo][0]=Double.parseDouble(st.nextToken());
                                    Vertices[pol][vertexNo][1]=Double.parseDouble(st.nextToken());
                                    Vertices[pol][vertexNo][2]=Double.parseDouble(st.nextToken().replaceAll(";",""));
                                    
                                                                        
                                        
                        
                                                                }          
            
                    
              
                
            }
            
                     
            
            if(temp1.equals("FACETS")) {
                
                          int NumberOfFace=0;
                          int counter=0;
                            enterLoop2://here the loop2 ends
                                while(true) {
                                        int countVertex=0;
                                        String facet=st.nextToken().replaceAll(":","");
                                        if(facet.equals("END")) {break enterLoop2;}
                                                                                
                                        String checkit=st.nextToken();
                                        
                                                while(!checkit.endsWith(";")) { 
                                        Facets[pol][counter]=(int)Double.parseDouble(checkit);
                                        checkit=st.nextToken();       
                                        counter++;
                                        countVertex++;}
                                        
                                        
                                        if(checkit.endsWith(";")) {
                                                
                                        Facets[pol][counter]=Integer.parseInt(checkit.replaceAll(";",""));
                                        counter++;
                                        countVertex++;
                                                
                                                
                                                                }
                                                                VertexNo[pol][NumberOfFace]=countVertex;
                                                                NumberOfFace++;
                                        
                                                }          
            
             pol++;           
             
            }
            
            
            
        }               
        
        
        
                
    }//end while
    
    /*System.out.println("Ari8mos Shmeion");
                                        for(int i=0;i<VerticesNo.length;i++) {
                                        
                                                System.out.println(VerticesNo[i]);
                                        }
                                        
                                                System.out.println("Ari8mos pleuron pou summetexoun");
                                        for(int i=0;i<SumOfFacets.length;i++) {
                                        
                                                System.out.println(SumOfFacets[i]);
                                        }
                                        
                                           System.out.println("Ari8mos pleuron ana pleura");
                                        for(int i=0;i<SumOfFacetsNo.length;i++) {
                                        
                                                System.out.println(SumOfFacetsNo[i]);
                                        }                                       
                                                                */
                                        
                                        
/*                                                         
        
                        
        for(int i=0;i<VertexNo.length;i++) {
                System.out.println("Polyhedron : "+(i+1));
                      for(int j=0;j<VertexNo[0].length;j++) {
                        System.out.print(VertexNo[i][j]+" ");
                        }
                        System.out.println();
                     }
                      
                    
for(int j=0;j<Vertices.length;j++) {
                
                System.out.println("Polyhedron : "+(j+1));
                for(int e=0;e<Vertices[0].length;e++) {
                
                                for(int k=0;k<Vertices[0][0].length;k++) {
                                        System.out.print(Vertices[j][e][k]+"--");
                                        
                                        
                                }
                                System.out.println();
                        }
        }

                              
  
        
        
        for(int i=0;i<Facets.length;i++) {
                System.out.println("Polyhedron : "+(i+1));
                      for(int j=0;j<Facets[0].length;j++) {
                        System.out.print(Facets[i][j]+" ");
                        }
                        System.out.println();
                     }
        
        */
        
        
        theFile.close();
        
        
        /*-------------------------------Object Creation Calling-------------------------------*/
        
        
        
        new Texttest(Vertices, Facets, VertexNo,VerticesNo,SumOfFacets,SumOfFacetsNo);
        

         }//end Main()   
         
         }//End Class
        
        
