001: /*
002: * $RCSfile: ObjectFileCompressor.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.3 $
041: * $Date: 2007/02/09 17:21:40 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.geometry_compression;
046:
047: import com.sun.j3d.loaders.IncorrectFormatException;
048: import com.sun.j3d.loaders.ParsingErrorException;
049: import com.sun.j3d.loaders.Scene;
050: import com.sun.j3d.loaders.objectfile.ObjectFile;
051: import com.sun.j3d.utils.geometry.compression.*;
052: import java.io.FileNotFoundException;
053: import java.io.IOException;
054: import java.io.Reader;
055: import java.net.URL;
056: import java.util.Hashtable;
057: import javax.media.j3d.Shape3D;
058:
059: /**
060: * This extension of ObjectFile provides the methods setQuantization() and
061: * compress() to compress Wavefront .obj files into the format described by
062: * appendix B of the Java 3D specification.
063: */
064: public class ObjectFileCompressor extends ObjectFile {
065: private GeometryCompressor compressor = null;
066:
067: public ObjectFileCompressor() {
068: super (STRIPIFY | TRIANGULATE);
069: compressor = new GeometryCompressor();
070: }
071:
072: public ObjectFileCompressor(int flags) {
073: super (flags | STRIPIFY | TRIANGULATE);
074: compressor = new GeometryCompressor();
075: }
076:
077: public ObjectFileCompressor(int flags, float radians) {
078: super (flags | STRIPIFY | TRIANGULATE, radians);
079: compressor = new GeometryCompressor();
080: }
081:
082: public void setFlags(int flags) {
083: super .setFlags(flags | STRIPIFY | TRIANGULATE);
084: }
085:
086: private int positionQuant = 10;
087: private int colorQuant = 8;
088: private int normalQuant = 3;
089:
090: /**
091: * Set the position, normal, and color quantization values for compression.
092: * @param positionQuant number of bits to quantize each position's X, Y,
093: * and Z components, ranging from 1 to 16 with a default of 10
094: * @param colorQuant number of bits to quantize each color's R, G, B, and
095: * alpha components, ranging from 2 to 16 with a default of 8
096: * @param normalQuant number of bits for quantizing each normal's U and V
097: * components, ranging from 0 to 6 with a default of 3
098: */
099: public void setQuantization(int positionQuant, int colorQuant,
100: int normalQuant) {
101:
102: this .positionQuant = positionQuant;
103: this .colorQuant = colorQuant;
104: this .normalQuant = normalQuant;
105: }
106:
107: /**
108: * Compress the specified .obj file into a CompressedGeometryData node
109: * component.
110: * @param objFileName String object representing the path to a .obj file
111: * @return a CompressedGeometryData node component
112: */
113: public CompressedGeometryData compress(String objFileName) {
114: return compressScene(getScene(objFileName));
115: }
116:
117: /**
118: * Compress the specified .obj file and add it to the end of an open
119: * compressed geometry file.
120: * @param objFileName String object representing the path to a .obj file
121: * @param file a currently open CompressedGeometryFile object
122: * @exception IOException - if write fails
123: */
124: public void compress(String objFileName, CompressedGeometryFile file)
125: throws IOException {
126: compressScene(getScene(objFileName), file);
127: }
128:
129: /**
130: * Compress the specified .obj file into a CompressedGeometryData node
131: * component.
132: * @param reader an open .obj file
133: * @return a CompressedGeometryData node component
134: */
135: public CompressedGeometryData compress(Reader reader) {
136: return compressScene(getScene(reader));
137: }
138:
139: /**
140: * Compress the specified .obj file and add it to the end of an open
141: * compressed geometry file.
142: * @param reader an open .obj file
143: * @param file an open CompressedGeometryFile object
144: * @exception IOException - if write fails
145: */
146: public void compress(Reader reader, CompressedGeometryFile file)
147: throws IOException {
148: compressScene(getScene(reader), file);
149: }
150:
151: /**
152: * Compress the specified .obj file into a CompressedGeometryData node
153: * component.
154: * @param url Uniform Resource Locator for the .obj file
155: * @return a CompressedGeometryData node component
156: */
157: public CompressedGeometryData compress(URL url) {
158: return compressScene(getScene(url));
159: }
160:
161: /**
162: * Compress the specified .obj file and add it to the end of an open
163: * compressed geometry file.
164: * @param url Uniform Resource Locator for the .obj file
165: * @param file a currently open CompressedGeometryFile object
166: * @exception IOException - if write fails
167: */
168: public void compress(URL url, CompressedGeometryFile file)
169: throws IOException {
170: compressScene(getScene(url), file);
171: }
172:
173: private CompressedGeometryData compressScene(Scene scene) {
174: return compressor.compress(getStream(scene));
175: }
176:
177: private void compressScene(Scene scene, CompressedGeometryFile file)
178: throws IOException {
179: compressor.compress(getStream(scene), file);
180: }
181:
182: private CompressionStream getStream(Scene scene) {
183: Hashtable objs = scene.getNamedObjects();
184: Shape3D shapes[] = new Shape3D[objs.size()];
185:
186: objs.values().toArray(shapes);
187: return new CompressionStream(positionQuant, colorQuant,
188: normalQuant, shapes);
189: }
190:
191: private Scene getScene(String objFileName) {
192: Scene scene = null;
193: try {
194: scene = load(objFileName);
195: } catch (FileNotFoundException e) {
196: System.err.println(e);
197: System.exit(1);
198: } catch (ParsingErrorException e) {
199: System.err.println(e);
200: System.exit(1);
201: } catch (IncorrectFormatException e) {
202: System.err.println(e);
203: System.exit(1);
204: }
205: return scene;
206: }
207:
208: private Scene getScene(Reader reader) {
209: Scene scene = null;
210: try {
211: scene = load(reader);
212: } catch (FileNotFoundException e) {
213: System.err.println(e);
214: System.exit(1);
215: } catch (ParsingErrorException e) {
216: System.err.println(e);
217: System.exit(1);
218: } catch (IncorrectFormatException e) {
219: System.err.println(e);
220: System.exit(1);
221: }
222: return scene;
223: }
224:
225: private Scene getScene(URL url) {
226: Scene scene = null;
227: try {
228: scene = load(url);
229: } catch (FileNotFoundException e) {
230: System.err.println(e);
231: System.exit(1);
232: } catch (ParsingErrorException e) {
233: System.err.println(e);
234: System.exit(1);
235: } catch (IncorrectFormatException e) {
236: System.err.println(e);
237: System.exit(1);
238: }
239: return scene;
240: }
241: }
|