//pre 

package actor;

/**
  $Id: KeyFrame.java,v 1.8 2004/03/08 08:43:00 awang Exp $
  
  Immutable.
 */
public class KeyFrame
{
  private double cycleTime = -1; // 0-1
  private Point3D[] vertexPositions; // change to Point3Ds

  public KeyFrame() {}
  
  public KeyFrame(double[] vertexPositions)
  {
    this(0, vertexPositions);
  }
  
  public KeyFrame(Point3D[] vertexPositions)
  {
    this(0, vertexPositions);
  }
  
  
  public KeyFrame(double cycleTime, Point3D[] points)
  {
    this.cycleTime = cycleTime;
    this.vertexPositions = points;
  }  
  
  public KeyFrame(double cycleTime, double[] vertexPositions)
  {
    this.cycleTime = cycleTime;
    this.vertexPositions = new Point3D[vertexPositions.length/3];
    for(int i = 0; i < this.vertexPositions.length; i++)
    {
      this.vertexPositions[i].x = vertexPositions[i * 3];
      this.vertexPositions[i].y = vertexPositions[i * 3 + 1];
      this.vertexPositions[i].z = vertexPositions[i * 3 + 2];      
    }      
  }

  public double getCycleTime()
  {
    return cycleTime;
  }

  public void setCycleTime(double cycleTime)
  {
    this.cycleTime = cycleTime;
  }

  public int getVertexCount()
  {
    return vertexPositions.length;
  }
  
  /**
   * These are offsets from the base shape
   *
   */
  
  public Point3D[] getVertexPositions()
  {
    Point3D[] result = new Point3D[vertexPositions.length];
    System.arraycopy(vertexPositions,0,result,0,vertexPositions.length);
    return result;
  }
  
  public Point3D getVertexPosition(int idx){
   
      return vertexPositions[idx];
      
  }

  public void setVertexPositions(Point3D[] vertexPositions)
  {
    this.vertexPositions = vertexPositions;
  }
  
  public Point3D getCoord(int idx)
  {
    return new Point3D(getVertexPositions()[idx]);
  }
  
  public static Point3D[] keyFrameLerp(double t, KeyFrame kf0, KeyFrame kf1)
  {
    Point3D a, b;
    Point3D[] lerpPoint = new Point3D[kf0.getVertexCount()];
    int animationIndex;

    for (int v = 0; v < kf0.getVertexCount(); v++)
    {
      a = kf0.getCoord(v);
      b = kf1.getCoord(v);
      lerpPoint[v] = Point3D.lerp(t % 1, a, b, new Point3D());
    }
    return lerpPoint;
  }

}