001: /*
002: * $RCSfile: GeometryCompressor.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.5 $
041: * $Date: 2007/02/09 17:20:16 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.utils.compression;
046:
047: import java.io.IOException;
048: import javax.media.j3d.CompressedGeometry;
049: import javax.media.j3d.CompressedGeometryHeader;
050: import javax.vecmath.Point3d;
051:
052: /**
053: * A GeometryCompressor takes a stream of geometric elements and
054: * quantization parameters (the CompressionStream object) and
055: * compresses it into a stream of commands as defined by appendix B
056: * of the Java 3D specification. The resulting data may be output
057: * in the form of a CompressedGeometry node component or appended
058: * to a CompressedGeometryFile.
059: *
060: * @see CompressionStream
061: * @see CompressedGeometry
062: * @see CompressedGeometryFile
063: *
064: * @deprecated As of Java 3D 1.5, replaced by
065: * com.sun.j3d.utils.geometry.compression.{@link com.sun.j3d.utils.geometry.compression.GeometryCompressor}.
066: */
067: public class GeometryCompressor {
068: private static final boolean benchmark = false;
069: private static final boolean printStream = false;
070: private static final boolean printHuffman = false;
071:
072: private HuffmanTable huffmanTable;
073: private CommandStream outputBuffer;
074: private CompressedGeometryHeader cgHeader;
075: private long startTime;
076:
077: public GeometryCompressor() {
078: // Create a compressed geometry header.
079: cgHeader = new CompressedGeometryHeader();
080:
081: // v1.0.0 - pre-FCS
082: // v1.0.1 - fixed winding order, FCS version (J3D 1.1.2)
083: // v1.0.2 - normal component length maximum 6, LII hardware (J3D 1.2)
084: cgHeader.majorVersionNumber = 1;
085: cgHeader.minorVersionNumber = 0;
086: cgHeader.minorMinorVersionNumber = 2;
087: }
088:
089: /**
090: * Compress a stream into a CompressedGeometry node component.
091: *
092: * @param stream CompressionStream containing the geometry to be compressed
093: * @return a CompressedGeometry node component
094: */
095: public CompressedGeometry compress(CompressionStream stream) {
096: CompressedGeometry cg;
097:
098: compressStream(stream);
099: cg = new CompressedGeometry(cgHeader, outputBuffer.getBytes());
100:
101: outputBuffer.clear();
102: return cg;
103: }
104:
105: /**
106: * Compress a stream and append the output to a CompressedGeometryFile.
107: * The resource remains open for subsequent updates; its close() method
108: * must be called to create a valid compressed geometry resource file.
109: *
110: * @param stream CompressionStream containing the geometry to be compressed
111: * @param f a currently open CompressedGeometryFile with write access
112: * @exception IOException if write fails
113: */
114: public void compress(CompressionStream stream,
115: CompressedGeometryFile f) throws IOException {
116:
117: compressStream(stream);
118: f.write(cgHeader, outputBuffer.getBytes());
119:
120: outputBuffer.clear();
121: }
122:
123: //
124: // Compress the stream and put the results in the output buffer.
125: // Set up the CompressedGeometryHeader object.
126: //
127: private void compressStream(CompressionStream stream) {
128: if (benchmark)
129: startTime = System.currentTimeMillis();
130:
131: // Create the Huffman table.
132: huffmanTable = new HuffmanTable();
133:
134: // Quantize the stream, compute deltas between consecutive elements if
135: // possible, and histogram the data length distribution.
136: stream.quantize(huffmanTable);
137:
138: // Compute tags for stream tokens.
139: huffmanTable.computeTags();
140:
141: // Create the output buffer and assemble the compressed output.
142: outputBuffer = new CommandStream(stream.getByteCount() / 3);
143: stream.outputCommands(huffmanTable, outputBuffer);
144:
145: // Print any desired info.
146: if (benchmark)
147: printBench(stream);
148: if (printStream)
149: stream.print();
150: if (printHuffman)
151: huffmanTable.print();
152:
153: // Set up the compressed geometry header object.
154: cgHeader.bufferType = stream.streamType;
155: cgHeader.bufferDataPresent = 0;
156: cgHeader.lowerBound = new Point3d(stream.ncBounds[0]);
157: cgHeader.upperBound = new Point3d(stream.ncBounds[1]);
158:
159: if (stream.vertexNormals)
160: cgHeader.bufferDataPresent |= CompressedGeometryHeader.NORMAL_IN_BUFFER;
161:
162: if (stream.vertexColor3 || stream.vertexColor4)
163: cgHeader.bufferDataPresent |= CompressedGeometryHeader.COLOR_IN_BUFFER;
164:
165: if (stream.vertexColor4)
166: cgHeader.bufferDataPresent |= CompressedGeometryHeader.ALPHA_IN_BUFFER;
167:
168: cgHeader.start = 0;
169: cgHeader.size = outputBuffer.getByteCount();
170:
171: // Clear the huffman table for next use.
172: huffmanTable.clear();
173: }
174:
175: private void printBench(CompressionStream stream) {
176: long t = System.currentTimeMillis() - startTime;
177: int vertexCount = stream.getVertexCount();
178: int meshReferenceCount = stream.getMeshReferenceCount();
179: int totalVertices = meshReferenceCount + vertexCount;
180: float meshPercent = 100f * meshReferenceCount
181: / (float) totalVertices;
182:
183: float compressionRatio = stream.getByteCount()
184: / ((float) outputBuffer.getByteCount());
185:
186: int vertexBytes = 12 + (stream.vertexColor3 ? 12 : 0)
187: + (stream.vertexColor4 ? 16 : 0)
188: + (stream.vertexNormals ? 12 : 0);
189:
190: float compressedVertexBytes = outputBuffer.getByteCount()
191: / (float) totalVertices;
192:
193: System.out.println("\nGeometryCompressor:\n" + totalVertices
194: + " total vertices\n" + vertexCount
195: + " streamed vertices\n" + meshReferenceCount
196: + " mesh buffer references (" + meshPercent + "%)\n"
197: + stream.getByteCount()
198: + " bytes streamed geometry compressed to "
199: + outputBuffer.getByteCount() + " in " + (t / 1000f)
200: + " sec\n" + (stream.getByteCount() / (float) t)
201: + " kbytes/sec, " + "stream compression ratio "
202: + compressionRatio + "\n\n" + vertexBytes
203: + " original bytes per vertex, "
204: + compressedVertexBytes
205: + " compressed bytes per vertex\n"
206: + "total vertex compression ratio "
207: + (vertexBytes / (float) compressedVertexBytes)
208: + "\n\n" + "lower bound "
209: + stream.ncBounds[0].toString() + "\n" + "upper bound "
210: + stream.ncBounds[1].toString());
211: }
212: }
|