001: /*
002: * $RCSfile: FloatValueInterpolator.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.4 $
041: * $Date: 2007/02/09 17:20:07 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.loaders.lw3d;
046:
047: import javax.vecmath.*;
048: import java.util.BitSet;
049: import java.util.Enumeration;
050:
051: import javax.media.j3d.Alpha;
052: import javax.media.j3d.Node;
053: import javax.media.j3d.NodeReferenceTable;
054: import javax.media.j3d.SceneGraphObject;
055: import javax.media.j3d.Interpolator;
056: import com.sun.j3d.internal.J3dUtilsI18N;
057:
058: /**
059: * This class acts as an interpolator between values specified in a
060: * floating point array, based on knot values (keyframes) specified in a
061: * knots array.
062: */
063: abstract class FloatValueInterpolator extends Interpolator {
064:
065: private float knots[];
066: private int knotsLength;
067: protected int currentKnotIndex;
068: protected float currentInterpolationRatio;
069: protected float values[];
070: protected float currentValue;
071:
072: /**
073: * Constructs a new FloatValueInterpolator object.
074: * @param alpha the alpha object for this interpolator
075: * @param knots an array of knot values that specify a spline
076: */
077: FloatValueInterpolator(Alpha alpha, float k[], float v[]) {
078:
079: super (alpha);
080:
081: // Check that first knot = 0.0f
082: knotsLength = k.length;
083: if (k[0] < -0.0001 || k[0] > 0.0001) {
084: throw new IllegalArgumentException(J3dUtilsI18N
085: .getString("FloatValueInterpolator0"));
086: }
087:
088: // Check that last knot = 1.0f
089: if ((k[knotsLength - 1] - 1.0f) < -0.0001
090: || (k[knotsLength - 1] - 1.0f) > 0.0001) {
091:
092: throw new IllegalArgumentException(J3dUtilsI18N
093: .getString("FloatValueInterpolator1"));
094: }
095:
096: // Check to see that knots are in ascending order and copy them
097: this .knots = new float[knotsLength];
098: for (int i = 0; i < knotsLength; i++) {
099: if ((i > 0) && (k[i] < k[i - 1])) {
100: throw new IllegalArgumentException(J3dUtilsI18N
101: .getString("FloatValueInterpolator2"));
102: }
103: this .knots[i] = k[i];
104: }
105:
106: // check to see that we have the same number of values as knots
107: if (knotsLength != v.length) {
108: throw new IllegalArgumentException(J3dUtilsI18N
109: .getString("FloatValueInterpolator3"));
110: }
111:
112: // copy the values
113: this .values = new float[knotsLength];
114: for (int i = 0; i < knotsLength; i++) {
115: this .values[i] = v[i];
116: }
117:
118: }
119:
120: /**
121: * This method sets the value at the specified index for
122: * this interpolator.
123: * @param index the index to be changed
124: * @param position the new value at index
125: */
126: void setValue(int index, float value) {
127: this .values[index] = value;
128: }
129:
130: /**
131: * This method retrieves the value at the specified index.
132: * @param index the index of the value requested
133: * @return the interpolator's value at the index
134: */
135: float getValue(int index) {
136: return this .values[index];
137: }
138:
139: /**
140: * This method computes the bounding knot indices and interpolation value
141: * "currentValue" given the current value of alpha, the knots[] array and
142: * the array of values.
143: * If the index is 0 and there will be no interpolation, both the
144: * index variable and the interpolation variable are set to 0.
145: * Otherwise, currentKnotIndex is set to the lower index of the
146: * two bounding knot points and the currentInterpolationRatio
147: * variable is set to the ratio of the alpha value between these
148: * two bounding knot points.
149: */
150: protected void computePathInterpolation() {
151: float alphaValue = (this .getAlpha()).value();
152:
153: for (int i = 0; i < knotsLength; i++) {
154: if ((i == 0 && alphaValue <= knots[i])
155: || (i > 0 && alphaValue >= knots[i - 1] && alphaValue <= knots[i])) {
156:
157: if (i == 0) {
158: currentInterpolationRatio = 0f;
159: currentKnotIndex = 0;
160: currentValue = values[0];
161: } else {
162: currentInterpolationRatio = (alphaValue - knots[i - 1])
163: / (knots[i] - knots[i - 1]);
164: currentKnotIndex = i - 1;
165: currentValue = values[i - 1]
166: + currentInterpolationRatio
167: * (values[i] - values[i - 1]);
168: }
169: break;
170: }
171: }
172: }
173:
174: }
|