001: /*
002: *
003: * Copyright (c) 2000-2001 Silvere Martin-Michiellot All Rights Reserved.
004: *
005: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
006: * royalty free, license to use, modify but not to redistribute this
007: * software in source and binary code form,
008: * provided that i) this copyright notice and license appear on all copies of
009: * the software; and ii) Licensee does not utilize the software in a manner
010: * which is disparaging to Silvere Martin-Michiellot.
011: *
012: * This software is provided "AS IS," without a warranty of any kind. ALL
013: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
014: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
015: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
016: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
017: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
018: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
019: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
020: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
021: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
022: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
023: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
024: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
025: *
026: * This software is not designed or intended for use in on-line control of
027: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
028: * the design, construction, operation or maintenance of any nuclear
029: * facility. Licensee represents and warrants that it will not use or
030: * redistribute the Software for such purposes.
031: *
032: * @Author: Silvere Martin-Michiellot
033: *
034: */
035:
036: package com.db.utils;
037:
038: import javax.media.j3d.*;
039: import javax.vecmath.*;
040: import com.sun.j3d.utils.compression.*;
041:
042: //reads and write Java3D scenegraphs.
043: //there is also a pure reader (loader) in com.db.loaders.j3d.J3DLoader
044: public class Java3DGeometryFile extends Object {
045:
046: private final boolean debug = true;
047:
048: private String filename;
049:
050: private CompressedGeometryFile compressedGeometryFile;
051: private CompressedGeometry compressedGeometry;
052: private CompressionStream compressionStream;
053: private GeometryCompressor geometryCompressor;
054: private Shape3D[] shapes3D;
055:
056: public Java3DGeometryFile() {
057:
058: super ();
059: this .filename = new String();
060:
061: }
062:
063: public Java3DGeometryFile(String filename) {
064:
065: super ();
066: this .filename = filename;
067:
068: }
069:
070: public void setFileName(String filename) {
071:
072: this .filename = filename;
073:
074: }
075:
076: public String getFileName() {
077:
078: return this .filename;
079:
080: }
081:
082: public BranchGroup readCompressedGeometry() {
083:
084: BranchGroup branchGroup = new BranchGroup();
085:
086: if (this .filename != null) {
087:
088: try {
089: compressedGeometryFile = new CompressedGeometryFile(
090: filename);
091: } catch (Exception exception) {
092: if (debug) {
093: if (exception instanceof java.io.FileNotFoundException) {
094: System.out
095: .println("compressed geometry file doesn't exist or cannot be read.");
096: } else if (exception instanceof java.lang.IllegalArgumentException) {
097: System.out
098: .println("the file is not a compressed geometry resource file.");
099: } else if (exception instanceof java.io.IOException) {
100: System.out
101: .println("there is a header or directory read error.");
102: }
103: }
104: }
105:
106: if (compressedGeometryFile != null) {
107:
108: int j = compressedGeometryFile.getObjectCount();
109:
110: for (int i = 0; i < j; i++) {
111: try {
112: compressedGeometry = compressedGeometryFile
113: .read(i);
114: shapes3D = compressedGeometry.decompress();
115: for (int k = 0; k < shapes3D.length; k++) {
116: branchGroup.addChild(shapes3D[k]);
117: }
118: } catch (Exception exception) {
119: if (debug) {
120: if (exception instanceof java.lang.IndexOutOfBoundsException) {
121: System.out
122: .println("compressed geometry object index is out of range.");
123: } else if (exception instanceof java.io.IOException) {
124: System.out
125: .println("compressed geometry file read failed.");
126: }
127: }
128: }
129: }
130:
131: compressedGeometryFile.close();
132: }
133:
134: }
135:
136: return branchGroup;
137:
138: }
139:
140: public void writeCompressedGeometry(Shape3D[] shapes) {
141:
142: if (this .filename != null) {
143:
144: try {
145: compressedGeometryFile = new CompressedGeometryFile(
146: filename, true);
147: } catch (Exception exception) {
148: if (debug) {
149: if (exception instanceof java.io.FileNotFoundException) {
150: System.out
151: .println("compressed geometry file doesn't exist or access permissions disallow access.");
152: } else if (exception instanceof java.lang.IllegalArgumentException) {
153: System.out
154: .println("the file is not a compressed geometry resource file.");
155: } else if (exception instanceof java.io.IOException) {
156: System.out
157: .println("there is a header or directory read error.");
158: }
159: }
160: }
161:
162: if (compressedGeometryFile != null) {
163:
164: try {
165: compressionStream = new CompressionStream(shapes);
166: } catch (Exception exception) {
167: System.out.println(exception);
168: if (exception instanceof java.lang.IllegalArgumentException) {
169: System.out
170: .println("Shape3D has an inconsistent dimensionality or vertex format, or Shape3D contains a geometry component that is not a GeometryArray.");
171: }
172: }
173:
174: if (compressionStream != null) {
175:
176: geometryCompressor = new GeometryCompressor();
177:
178: try {
179: geometryCompressor.compress(compressionStream,
180: compressedGeometryFile);
181: } catch (Exception exception) {
182: if (debug) {
183: if (exception instanceof java.io.IOException) {
184: System.out
185: .println("unable to write compressed geometry file.");
186: }
187: }
188: }
189:
190: compressedGeometryFile.close();
191:
192: }
193: }
194: }
195: }
196:
197: }
|