001: /*
002: * $RCSfile: CompressionStreamVertex.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 javax.vecmath.Color3f;
048: import javax.vecmath.Color4f;
049: import javax.vecmath.Point3f;
050: import javax.vecmath.Vector3f;
051:
052: /**
053: * This class represents a vertex in a compression stream. It maintains both
054: * floating-point and quantized representations of the vertex position along
055: * with meshing and vertex replacement flags for line and surface
056: * primitives. If normals or colors are bundled with geometry vertices then
057: * instances of this class will also contain references to normal or color
058: * stream elements.
059: */
060: class CompressionStreamVertex extends CompressionStreamElement {
061: private int X, Y, Z;
062: private int meshFlag;
063: private int stripFlag;
064: private float floatX, floatY, floatZ;
065:
066: int xAbsolute, yAbsolute, zAbsolute;
067: CompressionStreamColor color = null;
068: CompressionStreamNormal normal = null;
069:
070: /**
071: * Create a CompressionStreamVertex with the given parameters.
072: *
073: * @param stream CompressionStream associated with this vertex
074: * @param p position
075: * @param n normal bundled with this vertex or null if not bundled
076: * @param c color bundled with this vertex or null if not bundled
077: * @param stripFlag CompressionStream.RESTART,
078: * CompressionStream.REPLACE_OLDEST, or CompressionStream.REPLACE_MIDDLE
079: * @param meshFlag CompressionStream.MESH_PUSH or
080: * CompressionStream.NO_MESH_PUSH
081: */
082: CompressionStreamVertex(CompressionStream stream, Point3f p,
083: Vector3f n, Color3f c, int stripFlag, int meshFlag) {
084:
085: this (stream, p, n, stripFlag, meshFlag);
086:
087: if (stream.vertexColor3)
088: color = new CompressionStreamColor(stream, c);
089: }
090:
091: /**
092: * Create a CompressionStreamVertex with the given parameters.
093: *
094: * @param stream CompressionStream associated with this vertex
095: * @param p position
096: * @param n normal bundled with this vertex or null if not bundled
097: * @param c color bundled with this vertex or null if not bundled
098: * @param stripFlag CompressionStream.RESTART,
099: * CompressionStream.REPLACE_OLDEST, or CompressionStream.REPLACE_MIDDLE
100: * @param meshFlag CompressionStream.MESH_PUSH or
101: * CompressionStream.NO_MESH_PUSH
102: */
103: CompressionStreamVertex(CompressionStream stream, Point3f p,
104: Vector3f n, Color4f c, int stripFlag, int meshFlag) {
105:
106: this (stream, p, n, stripFlag, meshFlag);
107:
108: if (stream.vertexColor4)
109: color = new CompressionStreamColor(stream, c);
110: }
111:
112: /**
113: * Create a CompressionStreamVertex with the given parameters.
114: *
115: * @param stream CompressionStream associated with this vertex
116: * @param p position
117: * @param n normal bundled with this vertex or null if not bundled
118: * @param stripFlag CompressionStream.RESTART,
119: * CompressionStream.REPLACE_OLDEST, or CompressionStream.REPLACE_MIDDLE
120: * @param meshFlag CompressionStream.MESH_PUSH or
121: * CompressionStream.NO_MESH_PUSH
122: */
123: CompressionStreamVertex(CompressionStream stream, Point3f p,
124: Vector3f n, int stripFlag, int meshFlag) {
125:
126: this .stripFlag = stripFlag;
127: this .meshFlag = meshFlag;
128: this .floatX = p.x;
129: this .floatY = p.y;
130: this .floatZ = p.z;
131:
132: stream.byteCount += 12;
133: stream.vertexCount++;
134:
135: if (p.x < stream.mcBounds[0].x)
136: stream.mcBounds[0].x = p.x;
137: if (p.y < stream.mcBounds[0].y)
138: stream.mcBounds[0].y = p.y;
139: if (p.z < stream.mcBounds[0].z)
140: stream.mcBounds[0].z = p.z;
141:
142: if (p.x > stream.mcBounds[1].x)
143: stream.mcBounds[1].x = p.x;
144: if (p.y > stream.mcBounds[1].y)
145: stream.mcBounds[1].y = p.y;
146: if (p.z > stream.mcBounds[1].z)
147: stream.mcBounds[1].z = p.z;
148:
149: if (stream.vertexNormals)
150: normal = new CompressionStreamNormal(stream, n);
151: }
152:
153: /**
154: * Quantize the floating point position to fixed point integer components
155: * of the specified number of bits. The bit length can range from 1 to 16.
156: *
157: * @param stream CompressionStream associated with this element
158: * @param table HuffmanTable for collecting data about the quantized
159: * representation of this element
160: */
161: void quantize(CompressionStream stream, HuffmanTable huffmanTable) {
162: double px, py, pz;
163:
164: // Clamp quantization.
165: int quant = (stream.positionQuant < 1 ? 1
166: : (stream.positionQuant > 16 ? 16
167: : stream.positionQuant));
168:
169: absolute = false;
170: if (stream.firstPosition || stream.positionQuantChanged) {
171: absolute = true;
172: stream.lastPosition[0] = 0;
173: stream.lastPosition[1] = 0;
174: stream.lastPosition[2] = 0;
175: stream.firstPosition = false;
176: stream.positionQuantChanged = false;
177: }
178:
179: // Normalize position to the unit cube. This is bounded by the open
180: // intervals (-1..1) on each axis.
181: px = (floatX - stream.center[0]) * stream.scale;
182: py = (floatY - stream.center[1]) * stream.scale;
183: pz = (floatZ - stream.center[2]) * stream.scale;
184:
185: // Convert the floating point position to s.15 2's complement.
186: // ~1.0 -> 32767 (0x00007fff) [ ~1.0 = 32767.0/32768.0]
187: // ~-1.0 -> -32767 (0xffff8001) [~-1.0 = -32767.0/32768.0]
188: X = (int) (px * 32768.0);
189: Y = (int) (py * 32768.0);
190: Z = (int) (pz * 32768.0);
191:
192: // Compute quantized values.
193: X &= quantizationMask[quant];
194: Y &= quantizationMask[quant];
195: Z &= quantizationMask[quant];
196:
197: // Update quantized bounds.
198: if (X < stream.qcBounds[0].x)
199: stream.qcBounds[0].x = X;
200: if (Y < stream.qcBounds[0].y)
201: stream.qcBounds[0].y = Y;
202: if (Z < stream.qcBounds[0].z)
203: stream.qcBounds[0].z = Z;
204:
205: if (X > stream.qcBounds[1].x)
206: stream.qcBounds[1].x = X;
207: if (Y > stream.qcBounds[1].y)
208: stream.qcBounds[1].y = Y;
209: if (Z > stream.qcBounds[1].z)
210: stream.qcBounds[1].z = Z;
211:
212: // Copy and retain absolute position for mesh buffer lookup.
213: xAbsolute = X;
214: yAbsolute = Y;
215: zAbsolute = Z;
216:
217: // Compute deltas.
218: X -= stream.lastPosition[0];
219: Y -= stream.lastPosition[1];
220: Z -= stream.lastPosition[2];
221:
222: // Update last values.
223: stream.lastPosition[0] += X;
224: stream.lastPosition[1] += Y;
225: stream.lastPosition[2] += Z;
226:
227: // Deltas which exceed the range of 16-bit signed 2's complement
228: // numbers are handled by sign-extension of the 16th bit in order to
229: // effect a 16-bit wrap-around.
230: X = (X << 16) >> 16;
231: Y = (Y << 16) >> 16;
232: Z = (Z << 16) >> 16;
233:
234: // Compute length and shift common to all components.
235: computeLengthShift(X, Y, Z);
236:
237: // 0-length components are allowed only for normals.
238: if (length == 0)
239: length = 1;
240:
241: // Add this element to the Huffman table associated with this stream.
242: huffmanTable.addPositionEntry(length, shift, absolute);
243:
244: // Quantize any bundled color or normal.
245: if (color != null)
246: color.quantize(stream, huffmanTable);
247:
248: if (normal != null)
249: normal.quantize(stream, huffmanTable);
250:
251: // Push this vertex into the mesh buffer mirror, if necessary, so it
252: // can be retrieved for computing deltas when mesh buffer references
253: // are subsequently encountered during the quantization pass.
254: if (meshFlag == stream.MESH_PUSH)
255: stream.meshBuffer.push(this );
256: }
257:
258: /**
259: * Output the final compressed bits to the compression command stream.
260: *
261: * @param table HuffmanTable mapping quantized representations to
262: * compressed encodings
263: * @param output CommandStream for collecting compressed output
264: */
265: void outputCommand(HuffmanTable huffmanTable,
266: CommandStream outputBuffer) {
267:
268: HuffmanNode t;
269: int command = CommandStream.VERTEX;
270:
271: // Look up the Huffman token for this compression stream element. The
272: // values of length and shift found there will override the
273: // corresponding fields in this element, which represent best-case
274: // compression without regard to tag length.
275: t = huffmanTable.getPositionEntry(length, shift, absolute);
276:
277: // Construct the position subcommand.
278: int componentLength = t.dataLength - t.shift;
279: int subcommandLength = t.tagLength + (3 * componentLength);
280:
281: X = (X >> t.shift) & (int) lengthMask[componentLength];
282: Y = (Y >> t.shift) & (int) lengthMask[componentLength];
283: Z = (Z >> t.shift) & (int) lengthMask[componentLength];
284:
285: long positionSubcommand = (((long) t.tag) << (3 * componentLength))
286: | (((long) X) << (2 * componentLength))
287: | (((long) Y) << (1 * componentLength))
288: | (((long) Z) << (0 * componentLength));
289:
290: if (subcommandLength < 6) {
291: // The header will have some empty bits. The Huffman tag
292: // computation will prevent this if necessary.
293: command |= (int) (positionSubcommand << (6 - subcommandLength));
294: subcommandLength = 0;
295: } else {
296: // Move the 1st 6 bits of the subcommand into the header.
297: command |= (int) (positionSubcommand >>> (subcommandLength - 6));
298: subcommandLength -= 6;
299: }
300:
301: // Construct the vertex command body.
302: long body = (((long) stripFlag) << (subcommandLength + 1))
303: | (((long) meshFlag) << (subcommandLength + 0))
304: | (positionSubcommand & lengthMask[subcommandLength]);
305:
306: // Add the vertex command to the output buffer.
307: outputBuffer.addCommand(command, 8, body, subcommandLength + 3);
308:
309: // Output any normal and color subcommands.
310: if (normal != null)
311: normal.outputSubcommand(huffmanTable, outputBuffer);
312:
313: if (color != null)
314: color.outputSubcommand(huffmanTable, outputBuffer);
315: }
316:
317: public String toString() {
318: String d = absolute ? "" : "delta ";
319: String c = (color == null ? "" : "\n\n " + color.toString());
320: String n = (normal == null ? "" : "\n\n " + normal.toString());
321:
322: return "position: " + floatX + " " + floatY + " " + floatZ
323: + "\n" + "fixed point " + d + +X + " " + Y + " " + Z
324: + "\n" + "length " + length + " shift " + shift
325: + (absolute ? " absolute" : " relative") + "\n"
326: + "strip flag " + stripFlag + " mesh flag " + meshFlag
327: + c + n;
328: }
329: }
|