using UnityEngine;
namespace ARLocation
{
///
/// A (open-ended) catmull-rom spline, which interpolates a set points by joining
/// catmull-rom curves together.
///
public class CatmullRomSpline : Spline
{
///
/// The start-point control/handle.
///
private Vector3 startHandle;
///
/// The end-point control/handle.
///
private Vector3 endHandle;
///
/// The alpha/tension parameter of the spline.
///
public float Alpha
{
get
{
return alpha;
}
set
{
alpha = value;
CalculateSegments(lastSampleSize);
}
}
float alpha;
int lastSampleSize;
///
/// Creates a new Catmull-rom spline.
///
/// The interpolated points.
/// The number of samples used in each segment of the spline.
///
public CatmullRomSpline(Vector3[] points, int n, float alpha)
{
Points = (Vector3[])points.Clone();
this.alpha = alpha;
CalculateSegments(n);
}
///
/// Calculate the catmull-rom segments. Also estimates the curve's length.
///
/// The number sample points used to estimate each segment's length.
public sealed override void CalculateSegments(int n)
{
lastSampleSize = n;
segmentCount = (Points.Length - 1);
lengths = new float[segmentCount];
segments = new Curve[segmentCount];
startHandle = 2 * Points[0] - Points[1];
endHandle = 2 * Points[segmentCount] - Points[segmentCount - 1];
Length = 0;
for (var i = 0; i < segmentCount; i++)
{
segments[i] = new CatmullRomCurve(
i == 0 ? startHandle : Points[i - 1],
Points[i],
Points[i + 1],
(i + 1) == segmentCount ? endHandle : Points[i + 2],
Alpha
);
Length += segments[i].EstimateLength(n);
lengths[i] = Length;
}
}
}
}