001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.perseus.model;
028:
029: /**
030: * @version $Id: FloatRefValues.java,v 1.3 2006/04/21 06:37:00 st125089 Exp $
031: */
032: public class FloatRefValues implements RefValues {
033: /**
034: * This RefValues FloatSegments.
035: */
036: FloatSegment[] segments;
037:
038: /**
039: * A working array to return a value from the compute method.
040: */
041: float[][] w;
042:
043: /**
044: * Used to store the length of this RefValues
045: */
046: float length;
047:
048: /**
049: * Used to store the length of each segment.
050: */
051: float[] segLength;
052:
053: /**
054: * @param i requested segment index.
055: * @return Segment at index i
056: */
057: public Segment getSegment(int i) {
058: return segments[i];
059: }
060:
061: /**
062: * @return the number of segments in refValues
063: */
064: public int getSegments() {
065: return segments.length;
066: }
067:
068: /**
069: * FloatRefValues only have one component. This returns the number of
070: * components in the start value of the first segment.
071: *
072: * @return the number of components. There is an array of float for each
073: * component.
074: */
075: public int getComponents() {
076: return segments[0].start.length;
077: }
078:
079: /**
080: * Computes the value for the input interpolated values.
081: * There should be as many entries in the return array as there
082: * are components in the RefValues.
083: *
084: * @param si the current segment index
085: * @param p the current penetration
086: */
087: public Object[] compute(int si, float p) {
088: segments[si].compute(p, w);
089: return w;
090: }
091:
092: /**
093: * Adds a new time segment so accomodate for discreet behavior.
094: * If there is only one segment for discreet animations, the
095: * last value is never shown. To accomodate for that, this
096: * method should add a segment to the RefValues so that the
097: * last animation value is shown during the last value interval
098: * of a discreet animation.
099: */
100: public void makeDiscrete() {
101: FloatSegment[] tmpSegments = new FloatSegment[segments.length + 1];
102: System.arraycopy(segments, 0, tmpSegments, 0, segments.length);
103: FloatSegment lastSeg = segments[segments.length - 1];
104: FloatSegment newSeg = new FloatSegment();
105: newSeg.start = lastSeg.end;
106: newSeg.end = lastSeg.end;
107: tmpSegments[tmpSegments.length - 1] = newSeg;
108: segments = tmpSegments;
109: }
110:
111: /**
112: * Computes the length of the RefValues. This is meant for paced timing
113: * computation.
114: *
115: * @return the total length of this FloatRefValues. The distance is defined
116: * as the average distance between
117: * r
118: */
119: public float getLength() {
120: return length;
121: }
122:
123: /**
124: * Computes the length of segment at index si
125: *
126: * @param si the segment index.
127: */
128: public float getLength(final int si) {
129: return segLength[si];
130: }
131:
132: /**
133: * Should be called after the RefValue's configuration is complete
134: * to give the implementation a chance to initialize
135: * internal data and cache values.
136: */
137: public void initialize() {
138: // Initialize the working buffer
139: final int nc = segments[0].start.length;
140:
141: w = new float[nc][];
142:
143: final int ns = segments.length;
144:
145: // Initialize the segments.
146: for (int si = 0; si < ns; si++) {
147: segments[si].initialize();
148: }
149:
150: segLength = new float[ns];
151:
152: // The length of a FloatSegment, is the average distance between each
153: // component.
154: for (int ci = 0; ci < nc; ci++) {
155: w[ci] = new float[segments[0].start[ci].length];
156: }
157:
158: length = 0;
159: for (int si = 0; si < ns; si++) {
160: segLength[si] = segments[si].getLength();
161: length += segLength[si];
162: }
163: }
164:
165: /**
166: * Debug helper.
167: */
168: public String toString() {
169: StringBuffer sb = new StringBuffer();
170: sb.append("FloatRefValues[" + getSegments() + "]\n");
171: for (int si = 0; si < getSegments(); si++) {
172: Segment seg = getSegment(si);
173: sb.append("seg[" + si + "] : " + seg.toString() + "\n");
174: }
175: return sb.toString();
176: }
177: }
|