001: /*
002: * $RCSfile: KBCubicSplineCurve.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:11 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.utils.behaviors.interpolators;
046:
047: import javax.media.j3d.*;
048: import java.util.*;
049: import javax.vecmath.*;
050: import com.sun.j3d.internal.J3dUtilsI18N;
051:
052: /**
053: * KBCubicSplineCurve is a container class that holds a number of
054: * KBCubicSplineSegments
055: *
056: * @since Java3D 1.2
057: */
058:
059: public class KBCubicSplineCurve {
060:
061: private float totalCurveLength;
062: private KBCubicSplineSegment[] cubicSplineSegment;
063: public int numSegments;
064:
065: // default constructor
066: KBCubicSplineCurve() {
067: numSegments = 0;
068: totalCurveLength = 0f;
069: }
070:
071: /**
072: * This method takes a list of key frames and creates spline segments
073: * from it. It requires at least four key frames to be passed to it.
074: * Given n key frames, it creates n-3 KBCubicSplineSegments.
075: * @param the list of key frames that specify the motion path
076: */
077:
078: KBCubicSplineCurve(KBKeyFrame keys[]) {
079:
080: int keyLength = keys.length;
081: // Require at least 4 key frames for cubic spline curve
082: if (keyLength < 4)
083: throw new IllegalArgumentException(J3dUtilsI18N
084: .getString("KBCubicSplineCurve0"));
085:
086: numSegments = keyLength - 3;
087: this .cubicSplineSegment = new KBCubicSplineSegment[numSegments];
088:
089: // intialize and calculate coefficients for each segment
090: int k0 = 0;
091: int k1 = 1;
092: int k2 = 2;
093: int k3 = 3;
094: for (; k0 < numSegments; k0++, k1++, k2++, k3++) {
095: this .cubicSplineSegment[k0] = new KBCubicSplineSegment(
096: keys[k0], keys[k1], keys[k2], keys[k3]);
097: }
098:
099: // compute total curve length
100: computeTotalCurveLength();
101: }
102:
103: /**
104: * This method takes a list of spline segments creates the
105: * KBCubicSplineCurve.
106: * @param the list of segments that comprise the complete motion path
107: */
108:
109: KBCubicSplineCurve(KBCubicSplineSegment s[]) {
110:
111: cubicSplineSegment = new KBCubicSplineSegment[s.length];
112: numSegments = cubicSplineSegment.length;
113: for (int i = 0; i < numSegments; i++) {
114: this .cubicSplineSegment[i] = s[i];
115: }
116:
117: // compute total curve length
118: computeTotalCurveLength();
119: }
120:
121: /**
122: * This method takes a list of spline segments to replace the existing
123: * set of KBCubicSplineSegments that comprise the current
124: * KBCubicSplineCurve motion path.
125: * @param s the list of segments that comprise the complete motion path
126: */
127:
128: public void setSegments(KBCubicSplineSegment s[]) {
129:
130: cubicSplineSegment = new KBCubicSplineSegment[s.length];
131: numSegments = cubicSplineSegment.length;
132: for (int i = 0; i < numSegments; i++) {
133: this .cubicSplineSegment[i] = s[i];
134: }
135:
136: // compute total curve length
137: computeTotalCurveLength();
138: }
139:
140: /**
141: * This method returns the KBCubicSplineSegments pointed to by index
142: * @param index the index of the KBCubicSplineSegment required
143: * @return the KBCubicSplineSegment pointed to by index
144: */
145: public KBCubicSplineSegment getSegment(int index) {
146:
147: return this .cubicSplineSegment[index];
148:
149: }
150:
151: // computes the total length of the curve
152: private void computeTotalCurveLength() {
153:
154: totalCurveLength = 0f;
155: for (int i = 0; i < numSegments; i++) {
156: totalCurveLength += cubicSplineSegment[i].length;
157: }
158:
159: }
160:
161: /**
162: * This method returns the total length of the entire KBCubicSplineCurve
163: * motion path.
164: *
165: * @return the length of the KBCubicSplineCurve motion path
166: */
167:
168: public float getTotalCurveLength() {
169:
170: return this.totalCurveLength;
171:
172: }
173:
174: }
|