package jhpro.engine3d;



import java.io.*;

public class FileParse
{

    public FileParse()
    {
    }

    public static FacetedObject readOffObject(String modelFilename)
        throws IOException
    {
        FacetedObject fo = null;
        int nVerticies = 0;
        int nFacets = 0;
        int nEdges = 0;
        int nEdgesPerFacet = 0;
        BufferedReader is;
        try
        {
            is = FileFetch.openFile(modelFilename);
        }
        catch(Exception e)
        {
            System.out.println("Error ( loadModel ) " + e);
            throw new IOException("load model error: " + e);
        }
        StreamTokenizer stream = new StreamTokenizer(is);
        stream.eolIsSignificant(true);
        stream.commentChar(35);
label0:
        for(boolean gothead = false; !gothead;)
            switch(stream.nextToken())
            {
            default:
                break label0;

            case -2: 
            case 10: // '\n'
                break;

            case -3: 
                if("OFF".equals(stream.sval))
                {
                    while(stream.nextToken() == 10) ;
                    if(stream.ttype == -2)
                    {
                        nVerticies = (int)stream.nval;
                        if(stream.nextToken() == -2)
                        {
                            nFacets = (int)stream.nval;
                            if(stream.nextToken() == -2)
                            {
                                nEdges = (int)stream.nval;
                                gothead = true;
                            } else
                            {
                                throw new IOException("Error in OFF file ");
                            }
                        } else
                        {
                            throw new IOException("Error in OFF file ");
                        }
                    } else
                    {
                        throw new IOException("Error in OFF file ");
                    }
                }
                break;
            }

        fo = new FacetedObject(nVerticies);
        int num = 0;
        int coordnum = 0;
        while(num < nVerticies) 
            switch(stream.nextToken())
            {
            default:
                break;

            case 10: // '\n'
                if(coordnum > 2)
                {
                    coordnum = 0;
                    num++;
                }
                break;

            case -2: 
                point_data[coordnum % 3] = (float)stream.nval;
                if(++coordnum % 3 == 0)
                    fo.setPointData(point_data);
                break;
            }
        num = 0;
        coordnum = 0;
        boolean gotnum = false;
        while(num < nFacets) 
            switch(stream.nextToken())
            {
            default:
                break;

            case 10: // '\n'
                if(gotnum)
                    num++;
                gotnum = false;
                break;

            case -2: 
                if(!gotnum)
                {
                    nEdgesPerFacet = (int)stream.nval;
                    if(num == 0)
                        fo.createVectorIndex(nFacets, nEdgesPerFacet);
                    gotnum = true;
                    coordnum = 0;
                    break;
                }
                if(coordnum < nEdgesPerFacet)
                {
                    fo.setVectorIndex((int)stream.nval);
                    coordnum++;
                }
                break;
            }
        return fo;
    }

    public static FacetedObject readNffObject(String modelName)
        throws IOException
    {
        FacetedObject fo = null;
        int numVerticies = 0;
        int numFaces = 0;
        BufferedReader is;
        try
        {
            is = FileFetch.openFile(modelName);
        }
        catch(Exception e)
        {
            System.out.println("Error ( loadModel ) " + e);
            throw new IOException("load model error: " + e);
        }
        StreamTokenizer st = new StreamTokenizer(is);
        st.eolIsSignificant(true);
        st.commentChar(35);
        st.slashSlashComments(true);
        boolean seekingVerticiesCount = true;
label0:
        while(seekingVerticiesCount) 
        {
            st.nextToken();
            switch(st.ttype)
            {
            default:
                break label0;

            case 10: // '\n'
                break;

            case -2: 
                numVerticies = (int)st.nval;
                seekingVerticiesCount = false;
                break;
            }
        }
        fo = new FacetedObject(numVerticies);
        int valsRead = 0;
        int verticiesRead = 0;
        double vals[] = {
            0.0D, 0.0D, 0.0D
        };
        seekingVerticiesCount = true;
        st.nextToken();
label1:
        while(seekingVerticiesCount) 
        {
            st.nextToken();
            switch(st.ttype)
            {
            default:
                break label1;

            case -2: 
                vals[valsRead++] = st.nval;
                break;

            case 10: // '\n'
                fo.setPointData(vals);
                valsRead = 0;
                if(numVerticies == ++verticiesRead)
                    seekingVerticiesCount = false;
                break;
            }
        }
        st.nextToken();
        if(st.ttype == -2)
        {
            numFaces = (int)st.nval;
            st.nextToken();
            int first = -1;
            int curr = -1;
            int prev = -1;
            for(int i = 0; i < numFaces; i++)
            {
                st.nextToken();
                int n = (int)st.nval;
                if(i == 0)
                    fo.createVectorIndex(numFaces, n);
                for(int j = 0; j < n; j++)
                {
                    st.nextToken();
                    curr = (int)st.nval - 1;
                    fo.setVectorIndex((int)st.nval - 1);
                }

                st.nextToken();
                if(st.ttype == -1)
                    break;
            }

        }
        is.close();
        return fo;
    }

    private static double point_data[] = new double[3];

}

 

Ads help maintain this website.