diff --git a/Assets/ZestKit/Splines/AbstractSplineSolver.cs b/Assets/ZestKit/Splines/AbstractSplineSolver.cs index c66dc38..5f066bb 100644 --- a/Assets/ZestKit/Splines/AbstractSplineSolver.cs +++ b/Assets/ZestKit/Splines/AbstractSplineSolver.cs @@ -8,7 +8,12 @@ public abstract class AbstractSplineSolver { protected List _nodes; public List nodes { get { return _nodes; } } - protected float _pathLength; + + protected List _nodesWithDivisions; + public List nodesWithDivisions { get { return _nodesWithDivisions; } } + + + protected float _pathLength; public float pathLength { @@ -18,10 +23,58 @@ public float pathLength } } + public float allPathLength + { + get + { + return _nodesWithDivisions.Count; + } + } + + //get the distance from the beginning to a point + public float getPathLength(int endPoint) + { + float length = 0f; + var totalSudivisions = _nodes.Count * totalSubdivisionsPerNodeForLookupTable; + float timePerSlice = 1f / totalSudivisions; + Vector3 lastPoint = getPoint(0); + + for (int i = 1; i < endPoint + 1; i++) + { + float currentTime = timePerSlice * i; + var currentPoint = getPoint(currentTime); + length += Vector3.Distance(currentPoint, lastPoint); + lastPoint = currentPoint; + } + + return length; + } + + //get the distance from the initpoint to the endpoint we want + //this points are NOT the waypoints or nodes, these are the subdivisions + public float getPathLength(int initPoint, int endPoint, float normalizedInitPos) + { + float length = 0f; + var totalSudivisions = _nodes.Count * totalSubdivisionsPerNodeForLookupTable; + float timePerSlice = 1f / totalSudivisions; + //get the initial point to work with + Vector3 lastPoint = getPoint(normalizedInitPos); + + for (int i = initPoint+1; i < endPoint + 1; i++) + { + float currentTime = timePerSlice * i; + var currentPoint = getPoint(currentTime); + length += Vector3.Distance(currentPoint, lastPoint); + lastPoint = currentPoint; + } + + return length; + } + - // how many subdivisions should we divide each segment into? higher values take longer to build and lookup but - // result in closer to actual constant velocity - protected int totalSubdivisionsPerNodeForLookupTable = 5; + // how many subdivisions should we divide each segment into? higher values take longer to build and lookup but + // result in closer to actual constant velocity + protected int totalSubdivisionsPerNodeForLookupTable = 5; protected Dictionary _segmentTimeForDistance; // holds data in the form [time:distance] as a lookup table @@ -36,6 +89,7 @@ public virtual void buildPath() // we dont care about the first node for distances because they are always t:0 and len:0 _segmentTimeForDistance = new Dictionary( totalSudivisions ); + _nodesWithDivisions = new List(); var lastPoint = getPoint( 0 ); @@ -49,6 +103,7 @@ public virtual void buildPath() _pathLength += Vector3.Distance( currentPoint, lastPoint ); lastPoint = currentPoint; + _nodesWithDivisions.Add(currentPoint); _segmentTimeForDistance.Add( currentTime, _pathLength ); } } diff --git a/Assets/ZestKit/Splines/Spline.cs b/Assets/ZestKit/Splines/Spline.cs index 7ed042f..6aece1f 100644 --- a/Assets/ZestKit/Splines/Spline.cs +++ b/Assets/ZestKit/Splines/Spline.cs @@ -23,7 +23,9 @@ public class Spline // used by the visual path editor public List nodes { get { return _solver.nodes; } } - private bool _isReversed; // internal flag that lets us know if our nodes are reversed or not + public List nodesWithDivisions { get { return _solver.nodesWithDivisions; } } + + private bool _isReversed; // internal flag that lets us know if our nodes are reversed or not private AbstractSplineSolver _solver; public float pathLength @@ -35,14 +37,24 @@ public float pathLength } - /// - /// generates an arc from start to end with the arc axis perpendicular to start and end points - /// - /// The arc. - /// Start. - /// End. - /// how far away from the line from start to end the arc extends - public static Spline generateArc( Vector3 start, Vector3 end, float curvature ) + public float getPathLength(int endPoint) + { + return _solver.getPathLength(endPoint); + } + + + public float getPathLength(int initPoint, int endPoint, float normalizedInitPoint) { + return _solver.getPathLength(initPoint, endPoint, normalizedInitPoint); + } + + /// + /// generates an arc from start to end with the arc axis perpendicular to start and end points + /// + /// The arc. + /// Start. + /// End. + /// how far away from the line from start to end the arc extends + public static Spline generateArc( Vector3 start, Vector3 end, float curvature ) { return Spline.generateArc( start, end, curvature, Vector3.Cross( start, end ) ); } @@ -146,10 +158,16 @@ public Vector3 getLastNode() } - /// - /// responsible for calculating total length, segmentStartLocations and segmentDistances - /// - public void buildPath() + public float allPathLength { + get { + return _solver.allPathLength; + } + } + + /// + /// responsible for calculating total length, segmentStartLocations and segmentDistances + /// + public void buildPath() { _solver.buildPath(); }