2012-08-09 09:45:04 +00:00
|
|
|
using System;
|
2012-10-13 19:43:12 +00:00
|
|
|
using ANX.Framework.NonXNA.Development;
|
2011-11-17 20:35:25 +00:00
|
|
|
|
2012-08-09 09:45:04 +00:00
|
|
|
// This file is part of the ANX.Framework created by the
|
|
|
|
// "ANX.Framework developer group" and released under the Ms-PL license.
|
|
|
|
// For details see: http://anxframework.codeplex.com/license
|
2011-11-17 20:35:25 +00:00
|
|
|
|
|
|
|
namespace ANX.Framework
|
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
#if !WINDOWSMETRO // TODO: find replacement for Win8!
|
|
|
|
[Serializable]
|
|
|
|
#endif
|
|
|
|
[PercentageComplete(100)]
|
|
|
|
[Developer("floAr")]
|
2012-10-13 20:40:50 +00:00
|
|
|
[TestState(TestStateAttribute.TestState.Tested)]
|
2011-11-18 15:29:37 +00:00
|
|
|
public class Curve
|
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
public CurveLoopType PreLoop { get; set; }
|
|
|
|
public CurveLoopType PostLoop { get; set; }
|
|
|
|
public CurveKeyCollection Keys { get; private set; }
|
2011-11-19 01:15:38 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
public bool IsConstant
|
2011-11-18 15:29:37 +00:00
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
get { return Keys.Count <= 1; }
|
2011-11-18 15:29:37 +00:00
|
|
|
}
|
2011-11-19 01:15:38 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
public Curve()
|
2011-11-18 15:29:37 +00:00
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
Keys = new CurveKeyCollection();
|
2011-11-18 15:29:37 +00:00
|
|
|
}
|
2011-11-19 01:15:38 +00:00
|
|
|
|
|
|
|
public Curve Clone()
|
2011-11-18 15:29:37 +00:00
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
return new Curve { Keys = Keys.Clone(), PreLoop = PreLoop, PostLoop = PostLoop };
|
2011-11-18 15:29:37 +00:00
|
|
|
}
|
2011-11-19 01:15:38 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
#region ComputeTangent
|
2011-11-19 01:15:38 +00:00
|
|
|
//formulas from: http://msdn.microsoft.com/de-de/library/microsoft.xna.framework.curvetangent%28v=xnagamestudio.40%29.aspx
|
2012-10-13 19:43:12 +00:00
|
|
|
public void ComputeTangent(int index, CurveTangent tangentInOutType)
|
2011-11-18 15:29:37 +00:00
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
if (index < 0 || index >= Keys.Count)
|
2011-11-19 01:15:38 +00:00
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
}
|
2012-10-13 19:43:12 +00:00
|
|
|
CurveKey prev = index > 0 ? this.Keys[index - 1] : this.Keys[index];
|
|
|
|
CurveKey current = this.Keys[index];
|
2011-11-19 01:15:38 +00:00
|
|
|
current.TangentIn = 0;
|
2012-10-13 19:43:12 +00:00
|
|
|
CurveKey next = index < this.Keys.Count - 1 ? this.Keys[index + 1] : this.Keys[index];
|
2011-11-19 01:15:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
switch (tangentInOutType)
|
|
|
|
{
|
|
|
|
case CurveTangent.Flat:
|
|
|
|
current.TangentIn = 0;
|
|
|
|
current.TangentOut = 0;
|
|
|
|
break;
|
|
|
|
case CurveTangent.Linear:
|
|
|
|
current.TangentIn = current.Value - prev.Value;
|
|
|
|
current.TangentOut = next.Value - current.Value;
|
|
|
|
break;
|
|
|
|
case CurveTangent.Smooth:
|
|
|
|
current.TangentIn = ((next.Value - prev.Value) * ((current.Position - prev.Position) / (next.Position - prev.Position)));
|
|
|
|
current.TangentOut = ((next.Value - prev.Value) * ((next.Position - current.Position) / (next.Position - prev.Position)));
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 15:29:37 +00:00
|
|
|
}
|
2012-10-13 19:43:12 +00:00
|
|
|
|
|
|
|
public void ComputeTangent(int index, CurveTangent tangentInType, CurveTangent tangentOutType)
|
2011-11-19 01:15:38 +00:00
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
if (index < 0 || index >= Keys.Count)
|
2011-11-19 01:15:38 +00:00
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
}
|
2011-11-18 15:34:40 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
CurveKey prev = index > 0 ? this.Keys[index - 1] : this.Keys[index];
|
|
|
|
CurveKey current = this.Keys[index];
|
2011-11-19 01:15:38 +00:00
|
|
|
current.TangentIn = 0;
|
2012-10-13 19:43:12 +00:00
|
|
|
CurveKey next = index < this.Keys.Count - 1 ? this.Keys[index + 1] : this.Keys[index];
|
2011-11-21 14:26:59 +00:00
|
|
|
|
2011-11-19 01:15:38 +00:00
|
|
|
|
|
|
|
switch (tangentInType)
|
|
|
|
{
|
|
|
|
case CurveTangent.Flat:
|
|
|
|
current.TangentIn = 0;
|
|
|
|
break;
|
|
|
|
case CurveTangent.Linear:
|
|
|
|
current.TangentIn = current.Value - prev.Value;
|
|
|
|
break;
|
|
|
|
case CurveTangent.Smooth:
|
2011-11-21 14:26:59 +00:00
|
|
|
current.TangentIn = ((next.Value - prev.Value) * ((current.Position - prev.Position) / (next.Position - prev.Position)));
|
2011-11-19 01:15:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (tangentOutType)
|
|
|
|
{
|
|
|
|
case CurveTangent.Flat:
|
|
|
|
current.TangentOut = 0;
|
|
|
|
break;
|
|
|
|
case CurveTangent.Linear:
|
|
|
|
current.TangentOut = next.Value - current.Value;
|
|
|
|
break;
|
|
|
|
case CurveTangent.Smooth:
|
2011-11-21 14:26:59 +00:00
|
|
|
current.TangentOut = ((next.Value - prev.Value) * ((next.Position - current.Position) / (next.Position - prev.Position)));
|
2011-11-19 01:15:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-10-13 19:43:12 +00:00
|
|
|
|
2011-11-19 01:15:38 +00:00
|
|
|
public void ComputeTangents(CurveTangent tangentInOutType)
|
2011-11-18 15:34:40 +00:00
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
for (int i = 0; i < this.Keys.Count; ++i)
|
2011-11-18 15:34:40 +00:00
|
|
|
{
|
2011-11-19 01:15:38 +00:00
|
|
|
this.ComputeTangent(i, tangentInOutType);
|
|
|
|
}
|
|
|
|
}
|
2012-10-13 19:43:12 +00:00
|
|
|
|
2011-11-19 01:15:38 +00:00
|
|
|
public void ComputeTangents(CurveTangent tangentInType, CurveTangent tangentOutType)
|
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
for (int i = 0; i < this.Keys.Count; ++i)
|
2011-11-19 01:15:38 +00:00
|
|
|
{
|
2011-11-21 14:26:59 +00:00
|
|
|
this.ComputeTangent(i, tangentInType, tangentOutType);
|
2011-11-18 15:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-19 01:15:38 +00:00
|
|
|
#endregion
|
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
public float Evaluate(float position)
|
2011-11-18 15:34:40 +00:00
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
if (Keys.Count == 0)
|
|
|
|
return 0f;
|
2011-11-19 01:15:38 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
if (Keys.Count == 1)
|
|
|
|
return Keys[0].Value;
|
2011-11-19 01:15:38 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
CurveKey firstPointOfCurve = Keys[0];
|
|
|
|
CurveKey lastPointOfCurve = Keys[Keys.Count - 1];
|
2011-11-21 14:26:59 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
if (position < firstPointOfCurve.Position)
|
|
|
|
return EvalualtePreLoop(firstPointOfCurve, lastPointOfCurve, position);
|
2011-11-21 14:26:59 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
if (position > lastPointOfCurve.Position)
|
|
|
|
return EvalualtePostLoop(firstPointOfCurve, lastPointOfCurve, position);
|
2011-11-20 22:22:56 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
return Interpolate(position);
|
|
|
|
}
|
2011-11-19 01:15:38 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
#region EvalualtePreLoop
|
|
|
|
private float EvalualtePreLoop(CurveKey first, CurveKey last, float position)
|
|
|
|
{
|
|
|
|
int wraps;
|
|
|
|
float firstPosition = first.Position;
|
|
|
|
float lastPosition = last.Position;
|
|
|
|
// tspan is min 1 to avoid deadlock at constant curves
|
|
|
|
float timeSpan = Math.Max(1, lastPosition - firstPosition);
|
2011-11-21 14:26:59 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
// Description from : http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.curvelooptype.aspx
|
|
|
|
switch (PreLoop)
|
|
|
|
{
|
|
|
|
case CurveLoopType.Constant:
|
|
|
|
// The Curve will evaluate to its first key for positions before the first point in the Curve and to
|
|
|
|
// the last key for positions after the last point.
|
|
|
|
return first.Value;
|
|
|
|
|
|
|
|
case CurveLoopType.Linear:
|
|
|
|
// Linear interpolation will be performed to determine the value.
|
|
|
|
return first.Value - first.TangentIn * (first.Position - position);
|
|
|
|
|
|
|
|
case CurveLoopType.Cycle:
|
|
|
|
// Positions specified past the ends of the curve will wrap around to the opposite side of the Curve.
|
|
|
|
position -= timeSpan * CountWraps(position);
|
|
|
|
return Interpolate(position);
|
|
|
|
|
|
|
|
case CurveLoopType.CycleOffset:
|
|
|
|
// Positions specified past the ends of the curv will wrap around to the opposite side of the Curve.
|
|
|
|
// The value will be offset by the difference between the values of the first and last CurveKey
|
|
|
|
// multiplied by the number of times the position wraps around. If the position is before the first
|
|
|
|
// point in the Curve, the difference will be subtracted from its value; otherwise, the difference
|
|
|
|
// will be added
|
|
|
|
wraps = CountWraps(position);
|
|
|
|
float difference = (last.Value - first.Value) * wraps;
|
|
|
|
position -= timeSpan * CountWraps(position);
|
|
|
|
return Interpolate(position) + difference;
|
|
|
|
|
|
|
|
case CurveLoopType.Oscillate:
|
|
|
|
// Positions specified past the ends of the Curve act as an offset from the same side of the Curve
|
|
|
|
// toward the opposite side.
|
|
|
|
wraps = CountWraps(position);
|
|
|
|
float offset = position - (first.Position + wraps * timeSpan);
|
|
|
|
float wrappedPosition = (wraps & 1) != 0 ? (last.Position - offset) : (first.Position + offset);
|
|
|
|
return Interpolate(wrappedPosition);
|
|
|
|
}
|
2011-11-21 14:26:59 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
return Interpolate(position);
|
|
|
|
}
|
|
|
|
#endregion
|
2011-11-19 01:15:38 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
#region EvalualtePostLoop
|
|
|
|
private float EvalualtePostLoop(CurveKey first, CurveKey last, float position)
|
|
|
|
{
|
|
|
|
int wraps;
|
|
|
|
float firstPosition = first.Position;
|
|
|
|
float lastPosition = last.Position;
|
|
|
|
//tspan is min 1 to avoid deadlock at constant curves
|
|
|
|
float timeSpan = Math.Max(1, lastPosition - firstPosition);
|
2011-11-21 14:26:59 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
switch (PostLoop)
|
2011-11-19 01:15:38 +00:00
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
case CurveLoopType.Constant:
|
|
|
|
// The Curve will evaluate to its first key for positions before the first point in the Curve and to
|
|
|
|
// the last key for positions after the last point.
|
|
|
|
return last.Value;
|
|
|
|
|
|
|
|
case CurveLoopType.Linear:
|
|
|
|
// Linear interpolation will be performed to determine the value.
|
|
|
|
return last.Value - last.TangentOut * (last.Position - position);
|
|
|
|
|
|
|
|
case CurveLoopType.Cycle:
|
|
|
|
// Positions specified past the ends of the curve will wrap around to the opposite side of the Curve.
|
|
|
|
position -= timeSpan * CountWraps(position);
|
|
|
|
return Interpolate(position);
|
|
|
|
|
|
|
|
case CurveLoopType.CycleOffset:
|
|
|
|
// Positions specified past the ends of the curve will wrap around to the opposite side of the Curve.
|
|
|
|
// The value will be offset by the difference between the values of the first and last CurveKey
|
|
|
|
// multiplied by the number of times the position wraps around. If the position is before the first
|
|
|
|
// point in the Curve, the difference will be subtracted from its value; otherwise, the difference
|
|
|
|
// will be added.
|
|
|
|
wraps = CountWraps(position);
|
|
|
|
float difference = (last.Value - first.Value) * wraps;
|
|
|
|
position -= timeSpan * CountWraps(position);
|
|
|
|
return Interpolate(position) + difference;
|
|
|
|
|
|
|
|
case CurveLoopType.Oscillate:
|
|
|
|
// Positions specified past the ends of the Curve act as an offset from the same side of the Curve
|
|
|
|
// toward the opposite side.
|
|
|
|
wraps = CountWraps(position);
|
|
|
|
float offset = position - (first.Position + wraps * timeSpan);
|
|
|
|
float wrappedPosition = (wraps & 1) != 0 ? (last.Position - offset) : (first.Position + offset);
|
|
|
|
return Interpolate(wrappedPosition);
|
2011-11-19 01:15:38 +00:00
|
|
|
}
|
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
return Interpolate(position);
|
2011-11-21 14:26:59 +00:00
|
|
|
}
|
2012-10-13 19:43:12 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
private int CountWraps(float position)
|
2011-11-21 14:26:59 +00:00
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
float timeRange = Keys[Keys.Count - 1].Position - Keys[0].Position;
|
|
|
|
float wraps = (position - Keys[0].Position) / timeRange;
|
|
|
|
if (wraps < 0)
|
|
|
|
wraps--;
|
|
|
|
|
2011-11-21 14:26:59 +00:00
|
|
|
return (int)wraps;
|
|
|
|
}
|
2011-11-19 01:15:38 +00:00
|
|
|
|
2012-10-13 19:43:12 +00:00
|
|
|
private float Interpolate(float position)
|
2011-11-19 01:15:38 +00:00
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
//interpolate inside the curve with cubic hermite: http://forums.create.msdn.com/forums/p/53392/323814.aspx
|
2011-11-19 01:15:38 +00:00
|
|
|
|
|
|
|
//assume position is inside curve
|
2012-10-13 19:43:12 +00:00
|
|
|
CurveKey a = Keys[0];
|
2011-11-19 01:15:38 +00:00
|
|
|
CurveKey b;
|
2012-10-13 19:43:12 +00:00
|
|
|
for (int nextKeyIndex = 1; nextKeyIndex < Keys.Count; nextKeyIndex++)
|
2011-11-19 01:15:38 +00:00
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
b = Keys[nextKeyIndex];
|
2011-11-19 01:15:38 +00:00
|
|
|
if (b.Position >= position)
|
|
|
|
{
|
|
|
|
//stepping
|
|
|
|
if (a.Continuity == CurveContinuity.Step)
|
2012-10-13 19:43:12 +00:00
|
|
|
return position == b.Position ? b.Value : a.Value;
|
|
|
|
|
2011-11-19 01:15:38 +00:00
|
|
|
//smooth
|
|
|
|
//get the location between a and b in [0,1]
|
|
|
|
float moment = (position - a.Position) / (b.Position - a.Position);
|
|
|
|
return MathHelper.Hermite(a.Value, a.TangentOut, b.Value, b.TangentOut, moment);
|
2011-11-21 14:26:59 +00:00
|
|
|
|
2011-11-19 01:15:38 +00:00
|
|
|
}
|
|
|
|
//get next pair
|
|
|
|
a = b;
|
|
|
|
}
|
|
|
|
return 0f;
|
2011-11-18 15:34:40 +00:00
|
|
|
}
|
2011-11-18 15:29:37 +00:00
|
|
|
}
|
2011-11-17 20:35:25 +00:00
|
|
|
}
|