import javax.media.j3d.*;
import com.sun.j3d.utils.compression.*;
import java.io.*;

public class CompressionTest
{

    public CompressionTest()
    {
    }

        /**
         * Main test code.  Takes no arguments
         */
    public static void main(String[] args)
    {
                saveCG(createGeometry(),null);
                //saveCG(createGeometry(),"c:\\temp\\test.cg");
    }

        /**
         * Creates the 6 triangle piece of geometry from a real data file that
         * is giving me issues with compression.  The scale is from
         * (-0.99,-0.99,-0.99) to (0.99,0.99,0.99)
         */
        static public Geometry createGeometry()
        {
                float[] coords = new float[54];
                //Obj vertex: 1
                coords[0] = 0.5166348f;
                coords[1] = -0.99f;
                coords[2] = -0.99f;
                //Obj vertex: 2
                coords[3] = 0.99f;
                coords[4] = -0.8282335f;
                coords[5] = -0.99f;
                //Obj vertex: 3
                coords[6] = 0.15761456f;
                coords[7] = 0.08099296f;
                coords[8] = -1.888275E-5f;
                //Obj vertex: 4
                coords[9] = -0.23650905f;
                coords[10] = -0.053812362f;
                coords[11] = -1.888275E-5f;
                //Obj vertex: 5
                coords[12] = 0.15761456f;
                coords[13] = 0.08099296f;
                coords[14] = -1.888275E-5f;
                //Obj vertex: 6
                coords[15] = -0.048135556f;
                coords[16] = 0.30588818f;
                coords[17] = 0.24486668f;
                //Obj vertex: 7
                coords[18] = -0.048135556f;
                coords[19] = 0.30588818f;
                coords[20] = 0.24486668f;
                //Obj vertex: 8
                coords[21] = -0.4231438f;
                coords[22] = 0.17765848f;
                coords[23] = 0.24486668f;
                //Obj vertex: 9
                coords[24] = -0.23650905f;
                coords[25] = -0.053812362f;
                coords[26] = -1.888275E-5f;
                //Obj vertex: 10
                coords[27] = -0.048135556f;
                coords[28] = 0.30588818f;
                coords[29] = 0.24486668f;
                //Obj vertex: 11
                coords[30] = -0.67442214f;
                coords[31] = 0.99f;
                coords[32] = 0.99f;
                //Obj vertex: 12
                coords[33] = -0.99f;
                coords[34] = 0.8821553f;
                coords[35] = 0.99f;
                //Obj vertex: 13
                coords[36] = -0.99f;
                coords[37] = 0.8821553f;
                coords[38] = 0.99f;
                //Obj vertex: 14
                coords[39] = -0.4231438f;
                coords[40] = 0.17765848f;
                coords[41] = 0.24486668f;
                //Obj vertex: 15
                coords[42] = -0.048135556f;
                coords[43] = 0.30588818f;
                coords[44] = 0.24486668f;
                //Obj vertex: 16
                coords[45] = 0.15761456f;
                coords[46] = 0.08099296f;
                coords[47] = -1.888275E-5f;
                //Obj vertex: 17
                coords[48] = -0.23650905f;
                coords[49] = -0.053812362f;
                coords[50] = -1.888275E-5f;
                //Obj vertex: 18
                coords[51] = 0.5166348f;
                coords[52] = -0.99f;
                coords[53] = -0.99f;
                float[] normals = new float[54];
                //Obj normals: 1
                normals[0] = 0.36361238f;
                normals[1] = -0.6481123f;
                normals[2] = 0.6691311f;
                //Obj normals: 2
                normals[3] = 0.36361238f;
                normals[4] = -0.6481123f;
                normals[5] = 0.6691311f;
                //Obj normals: 3
                normals[6] = 0.36361238f;
                normals[7] = -0.6481123f;
                normals[8] = 0.6691311f;
                //Obj normals: 4
                normals[9] = 0.36361238f;
                normals[10] = -0.6481123f;
                normals[11] = 0.6691311f;
                //Obj normals: 5
                normals[12] = 0.36361238f;
                normals[13] = -0.6481123f;
                normals[14] = 0.6691311f;
                //Obj normals: 6
                normals[15] = 0.36361238f;
                normals[16] = -0.6481123f;
                normals[17] = 0.6691311f;

                int[] coordIndices = new int[18];
                coordIndices[0] = 0;
                coordIndices[1] = 1;
                coordIndices[2] = 2;
                coordIndices[3] = 3;
                coordIndices[4] = 2;
                coordIndices[5] = 5;
                coordIndices[6] = 5;
                coordIndices[7] = 7;
                coordIndices[8] = 3;
                coordIndices[9] = 5;
                coordIndices[10] = 10;
                coordIndices[11] = 11;
                coordIndices[12] = 11;
                coordIndices[13] = 7;
                coordIndices[14] = 5;
                coordIndices[15] = 2;
                coordIndices[16] = 3;
                coordIndices[17] = 0;
                int[] normalIndices = new int[18];
                normalIndices[0] = 0;
                normalIndices[1] = 0;
                normalIndices[2] = 0;
                normalIndices[3] = 0;
                normalIndices[4] = 0;
                normalIndices[5] = 0;
                normalIndices[6] = 0;
                normalIndices[7] = 0;
                normalIndices[8] = 0;
                normalIndices[9] = 0;
                normalIndices[10] = 0;
                normalIndices[11] = 0;
                normalIndices[12] = 0;
                normalIndices[13] = 0;
                normalIndices[14] = 0;
                normalIndices[15] = 0;
                normalIndices[16] = 0;
                normalIndices[17] = 0;

                IndexedTriangleArray ita = new IndexedTriangleArray(coords.length/3,
                                                         ita.COORDINATES | ita.NORMALS,coordIndices.length);
                ita.setCoordinates(0,coords);
                ita.setNormals(0,normals);
                ita.setCoordinateIndices(0,coordIndices);
                ita.setNormalIndices(0,normalIndices);
                return ita;
        }

        /*
        //--Obj file with the same geometry
g shape
v 0.5166348 -0.99 -0.99
v 0.99 -0.8282335 -0.99
v 0.15761456 0.08099296 -1.888275E-5
v -0.23650905 -0.053812362 -1.888275E-5
v 0.15761456 0.08099296 -1.888275E-5
v -0.048135556 0.30588818 0.24486668
v -0.048135556 0.30588818 0.24486668
v -0.4231438 0.17765848 0.24486668
v -0.23650905 -0.053812362 -1.888275E-5
v -0.048135556 0.30588818 0.24486668
v -0.67442214 0.99 0.99
v -0.99 0.8821553 0.99
v -0.99 0.8821553 0.99
v -0.4231438 0.17765848 0.24486668
v -0.048135556 0.30588818 0.24486668
v 0.15761456 0.08099296 -1.888275E-5
v -0.23650905 -0.053812362 -1.888275E-5
v 0.5166348 -0.99 -0.99
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
vn 0.36361238 -0.6481123 0.6691311
f 1//1 2//1 3//1
f 4//1 3//1 6//1
f 6//1 8//1 4//1
f 6//1 11//1 12//1
f 12//1 8//1 6//1
f 3//1 4//1 1//1

        */

        /**
         * Saves the Geometry
         * @param geom Geometry to save
         * @param filename Full path of the .cg file to create.  If this is null,
         * it tests compression in memory.  null is the easiest way to test.
         */
        public static void saveCG(Geometry geom,String filename)
        {
                //create the array of shapes for the compression stream
                Shape3D shape = new Shape3D(geom);
                Shape3D[] shapes = new Shape3D[1];
                shapes[0] = shape;

        int positionQuant = 16;
        int colorQuant = 8;
            int normalQuant = 3;
                //create the compression stream
                CompressionStream cStream = new CompressionStream(positionQuant, colorQuant, normalQuant,shapes);
                GeometryCompressor comp = null;
                RandomAccessFile rfile = null;
                CompressedGeometry compGeom = null;
                CompressedGeometryFile file = null;
                try
                {
                        if (filename != null)
                        {
                                //this tests by creating a .cg file
                                //the error happens if you load in the sample with the
                                //compression example.  cgview I believe.
                                rfile = new RandomAccessFile(filename,"rw");
                                file = new CompressedGeometryFile(rfile);
                                comp = new GeometryCompressor();
                                comp.compress(cStream,file);
                                file.close();
                                rfile.close();
                        }
                        else
                        {
                                //this tests by creating an in memory object
                                comp = new GeometryCompressor();
                            compGeom = comp.compress(cStream);
                            compGeom.decompress();
                        }
                }
                catch (java.lang.Exception e)
                {
                        e.printStackTrace();
                        return;
                }
        }
}