Java3DGeometryFile.java :  » 6.0-JDK-Modules » java-3d » com » db » utils » Java Open Source

Java Open Source » 6.0 JDK Modules » java 3d 
java 3d » com » db » utils » Java3DGeometryFile.java
/*
 *
 * Copyright (c) 2000-2001 Silvere Martin-Michiellot All Rights Reserved.
 *
 * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
 * royalty free, license to use, modify but not to redistribute this
 * software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Silvere Martin-Michiellot.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
 * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
 * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 *
 * @Author: Silvere Martin-Michiellot
 *
 */
 
package com.db.utils;

import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.compression.*;

//reads and write Java3D scenegraphs.
//there is also a pure reader (loader) in com.db.loaders.j3d.J3DLoader
public class Java3DGeometryFile extends Object {

    private final boolean debug = true;

    private String filename;

    private CompressedGeometryFile compressedGeometryFile;
    private CompressedGeometry compressedGeometry;
    private CompressionStream compressionStream;
    private GeometryCompressor geometryCompressor;
    private Shape3D[] shapes3D;

    public Java3DGeometryFile () {
        
        super();
        this.filename = new String();

    }

    public Java3DGeometryFile (String filename) {

        super();
        this.filename = filename;

    }

    public void setFileName (String filename) {

        this.filename = filename;

    }

    public String getFileName () {

        return this.filename;

    }

    public BranchGroup readCompressedGeometry() {

        BranchGroup branchGroup = new BranchGroup();

        if (this.filename != null) {

            try {
                compressedGeometryFile = new CompressedGeometryFile(filename);
            }
            catch (Exception exception) {
                if (debug) {
                    if (exception instanceof java.io.FileNotFoundException) {
                        System.out.println("compressed geometry file doesn't exist or cannot be read.");
                    } else
                    if (exception instanceof java.lang.IllegalArgumentException) {
                        System.out.println("the file is not a compressed geometry resource file.");
                    } else
                    if (exception instanceof java.io.IOException) {
                        System.out.println("there is a header or directory read error.");
                    }
                }
            }

            if (compressedGeometryFile != null) {

                int j = compressedGeometryFile.getObjectCount();

                for (int i=0;i<j;i++) {
                    try {
                        compressedGeometry = compressedGeometryFile.read(i);
                        shapes3D = compressedGeometry.decompress();
                        for (int k=0; k<shapes3D.length;k++) {
                            branchGroup.addChild(shapes3D[k]);
                        }
                    }
                    catch (Exception exception) {
                        if (debug) {
                            if (exception instanceof java.lang.IndexOutOfBoundsException) {
                                System.out.println("compressed geometry object index is out of range.");
                            } else
                            if (exception instanceof java.io.IOException) {
                                System.out.println("compressed geometry file read failed.");
                            }
                        }
                    }
                }

                compressedGeometryFile.close();
            }

        }

        return branchGroup;

    }

    public void writeCompressedGeometry(Shape3D[] shapes) {

        if (this.filename != null) {

            try {
                compressedGeometryFile = new CompressedGeometryFile(filename,true);
            }
            catch (Exception exception) {
                if (debug) {
                    if (exception instanceof java.io.FileNotFoundException) {
                        System.out.println("compressed geometry file doesn't exist or access permissions disallow access.");
                    } else
                    if (exception instanceof java.lang.IllegalArgumentException) {
                        System.out.println("the file is not a compressed geometry resource file.");
                    } else
                    if (exception instanceof java.io.IOException) {
                        System.out.println("there is a header or directory read error.");
                    }
                }
            }

            if (compressedGeometryFile != null) {

                try {
                    compressionStream = new CompressionStream(shapes);
                }
                catch (Exception exception) {
                    System.out.println(exception);
                    if (exception instanceof java.lang.IllegalArgumentException) {
                        System.out.println("Shape3D has an inconsistent dimensionality or vertex format, or Shape3D contains a geometry component that is not a GeometryArray.");
                    }
                }

                if (compressionStream != null) {

                    geometryCompressor = new GeometryCompressor();

                    try {
                        geometryCompressor.compress(compressionStream, compressedGeometryFile);
                    }
                    catch (Exception exception) {
                        if (debug) {
                            if (exception instanceof java.io.IOException) {
                                System.out.println("unable to write compressed geometry file.");
                            }
                        }
                    }

                    compressedGeometryFile.close();

                }
            }
        }
    }

}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.