Android Open Source - Schooner-3D Bezier Curve






From Project

Back to project page Schooner-3D.

License

The source code is released under:

Apache License

If you think the Android project Schooner-3D listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2012 Dan Mercer/*from  ww w . j  av a 2s . c  o m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.supermercerbros.gameengine.math;


/**
 * Represents a piecewise Bezier curve, used for interpolation
 */
public class BezierCurve implements Curve {
  private final float[] values;
  private final float[] times;
  private final int lastIndex;
  
  public BezierCurve(float[] x, float[] y) {
    if (x.length != y.length) {
      throw new IllegalArgumentException(
          "x and y arrays must be of equal length.");
    } else if ((x.length - 1) % 3 != 0) {
      throw new IllegalArgumentException(
          "(x.length - 1) % 3 must equal zero.");
    }
    
    times = x;
    values = y;
    lastIndex = times.length - 1;
  }
  
  /* (non-Javadoc)
   * @see com.supermercerbros.gameengine.math.Curve#getInterpolation(float)
   */
  @Override
  public float getInterpolation(float x) {
    final float frame = x * times[lastIndex];
    if (x >= 1) {
      return values[lastIndex];
    } else if (x <= 0) {
      return values[0];
    }
    
    // Get index of lower keyframe
    int keyframe = 0;
    while (frame > times[(keyframe + 1) * 3]) {
      keyframe++;
    }
    
    // Point frame coordinates
    final float fp0 = times[(keyframe * 3) + 0];
    final float fp1 = times[(keyframe * 3) + 1];
    final float fp2 = times[(keyframe * 3) + 2];
    final float fp3 = times[(keyframe * 3) + 3];
    
    // Point value coordinates
    final float vp0 = values[(keyframe * 3) + 0];
    final float vp1 = values[(keyframe * 3) + 1];
    final float vp2 = values[(keyframe * 3) + 2];
    final float vp3 = values[(keyframe * 3) + 3];
    
    // Estimate T given X
    float lowerT = 0;
    float upperT = 1;
    float tGuess = (fp0 - frame) / (fp0 - fp3);
    
    for (int i = 1; i <= 5; i++) {
      final float frameGuess = solve(fp0, fp1, fp2, fp3, tGuess);
      if (frameGuess < frame) {
        lowerT = tGuess;
      } else if (frameGuess > frame){
        upperT = tGuess;
      } else {
        return solve(vp0, vp1, vp2, vp3, tGuess);
      }
      
      tGuess = (lowerT + upperT) / 2;
    }
    
    final float lowerFrame = solve(fp0, fp1, fp2, fp3, lowerT);
    final float upperFrame = solve(fp0, fp1, fp2, fp3, upperT);
    final float alpha = (lowerFrame - frame) / (lowerFrame - upperFrame);
    final float t = lowerT + (upperT - lowerT) * alpha;
    
    // Solve for Y now that we have an estimated T
    return solve(vp0, vp1, vp2, vp3, t);
    
  }
  
  private static float solve(final float p0, final float p1, final float p2,
      final float p3, final float t) {
    if (t == 0.0) {
      return p0;
    } else if (t == 1.0) {
      return p3;
    }
    
    final float d = 1 - t;
    return (d * d * d * p0) +
        (3 * d * d * t * p1) +
        (3 * d * t * t * p2) +
        (t * t * t * p3);
  }
  
  /* (non-Javadoc)
   * @see com.supermercerbros.gameengine.math.Curve#getStartValue()
   */
  @Override
  public float getStartValue() {
    return values[0];
  }
}




Java Source Code List

com.supermercerbros.gameengine.GameActivity.java
com.supermercerbros.gameengine.GameView.java
com.supermercerbros.gameengine.Schooner3D.java
com.supermercerbros.gameengine.TestActivity.java
com.supermercerbros.gameengine.TestMaterials.java
com.supermercerbros.gameengine.TestObjects.java
com.supermercerbros.gameengine.animation.AnimationData.java
com.supermercerbros.gameengine.animation.Keyframe.java
com.supermercerbros.gameengine.animation.MeshAnimation.java
com.supermercerbros.gameengine.armature.ActionData.java
com.supermercerbros.gameengine.armature.Action.java
com.supermercerbros.gameengine.armature.BinarySkeletalVertexModifier.java
com.supermercerbros.gameengine.armature.Bone.java
com.supermercerbros.gameengine.armature.SkeletalVertexModifier.java
com.supermercerbros.gameengine.armature.Skeleton.java
com.supermercerbros.gameengine.collision.Bounds.java
com.supermercerbros.gameengine.collision.Collider.java
com.supermercerbros.gameengine.collision.CollisionDetector.java
com.supermercerbros.gameengine.collision.Collision.java
com.supermercerbros.gameengine.collision.DebugListener.java
com.supermercerbros.gameengine.collision.Edge.java
com.supermercerbros.gameengine.collision.Face.java
com.supermercerbros.gameengine.collision.Feature.java
com.supermercerbros.gameengine.collision.Intersection.java
com.supermercerbros.gameengine.collision.Line.java
com.supermercerbros.gameengine.collision.LocalDistMinimum.java
com.supermercerbros.gameengine.collision.Matrix.java
com.supermercerbros.gameengine.collision.OnCollisionCheckFinishedListener.java
com.supermercerbros.gameengine.collision.Plane.java
com.supermercerbros.gameengine.collision.Point.java
com.supermercerbros.gameengine.collision.Polyhedron.java
com.supermercerbros.gameengine.collision.SphereBounds.java
com.supermercerbros.gameengine.collision.Vector.java
com.supermercerbros.gameengine.collision.Vertex.java
com.supermercerbros.gameengine.debug.JankCatcher.java
com.supermercerbros.gameengine.debug.LoopLog.java
com.supermercerbros.gameengine.engine.Camera.java
com.supermercerbros.gameengine.engine.DataPipe.java
com.supermercerbros.gameengine.engine.EGLContextLostHandler.java
com.supermercerbros.gameengine.engine.Engine.java
com.supermercerbros.gameengine.engine.GameRenderer.java
com.supermercerbros.gameengine.engine.Light.java
com.supermercerbros.gameengine.engine.Normals.java
com.supermercerbros.gameengine.engine.RenderData.java
com.supermercerbros.gameengine.engine.Scene.java
com.supermercerbros.gameengine.engine.Time.java
com.supermercerbros.gameengine.engine.shaders.Material.java
com.supermercerbros.gameengine.engine.shaders.Program.java
com.supermercerbros.gameengine.engine.shaders.ShaderLib.java
com.supermercerbros.gameengine.engine.shaders.Shader.java
com.supermercerbros.gameengine.engine.shaders.VertexModifier.java
com.supermercerbros.gameengine.handlers.OnAnimationCompleteDispatcher.java
com.supermercerbros.gameengine.handlers.OnAnimationCompleteListener.java
com.supermercerbros.gameengine.hud.CoordsConverter.java
com.supermercerbros.gameengine.hud.GameHud.java
com.supermercerbros.gameengine.hud.HudElement.java
com.supermercerbros.gameengine.material.CelShadedMaterial.java
com.supermercerbros.gameengine.material.TexturedMaterial.java
com.supermercerbros.gameengine.math.BezierCurve.java
com.supermercerbros.gameengine.math.Curve.java
com.supermercerbros.gameengine.math.MatrixUtils.java
com.supermercerbros.gameengine.math.Quaternion.java
com.supermercerbros.gameengine.motion.CurveMovement.java
com.supermercerbros.gameengine.motion.MovementData.java
com.supermercerbros.gameengine.motion.Movement.java
com.supermercerbros.gameengine.objects.AnimatedMeshObject.java
com.supermercerbros.gameengine.objects.BasicMaterial.java
com.supermercerbros.gameengine.objects.BonedObject.java
com.supermercerbros.gameengine.objects.GameObject.java
com.supermercerbros.gameengine.objects.Metadata.java
com.supermercerbros.gameengine.parsers.ConstantCurve.java
com.supermercerbros.gameengine.parsers.GameFactory.java
com.supermercerbros.gameengine.parsers.PreBoneData.java
com.supermercerbros.gameengine.parsers.PreObjectData.java
com.supermercerbros.gameengine.parsers.Sch3D.java
com.supermercerbros.gameengine.render.Compositor.java
com.supermercerbros.gameengine.shaders.ProgramSource.java
com.supermercerbros.gameengine.texture.BitmapTexture.java
com.supermercerbros.gameengine.texture.ETC1CompressedTexture.java
com.supermercerbros.gameengine.texture.Texture.java
com.supermercerbros.gameengine.util.BetterDataInputStream.java
com.supermercerbros.gameengine.util.DelayedRunnable.java
com.supermercerbros.gameengine.util.GLES2.java
com.supermercerbros.gameengine.util.IPO.java
com.supermercerbros.gameengine.util.LoopingThread.java
com.supermercerbros.gameengine.util.Toggle.java
com.supermercerbros.gameengine.util.Utils.java