From 4deef43aa555943d2545af3069d3b8785a293b98 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 10 Mar 2016 20:43:18 +0100 Subject: [PATCH 1/2] added methods to get the total points in a path including the subdivisions as vector3 points added methods to get the distance from 0 or from an initial point to an endpoint --- .../ZestKit/Splines/AbstractSplineSolver.cs | 62 +++++++++++++++++-- Assets/ZestKit/Splines/Spline.cs | 45 ++++++++++---- 2 files changed, 90 insertions(+), 17 deletions(-) diff --git a/Assets/ZestKit/Splines/AbstractSplineSolver.cs b/Assets/ZestKit/Splines/AbstractSplineSolver.cs index c66dc38..c475225 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,57 @@ 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 length = 0f; + var totalSudivisions = _nodes.Count * totalSubdivisionsPerNodeForLookupTable; + float timePerSlice = 1f / totalSudivisions; + Vector3 lastPoint = getPoint(initPoint); + + 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 +88,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 +102,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..18026a8 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,25 @@ 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) + { + return _solver.getPathLength(initPoint, endPoint); + } + + /// + /// 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 +159,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(); } From bb329a9925853a432b876901eb1026fc109fca66 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 18 Mar 2016 18:46:52 +0100 Subject: [PATCH 2/2] -added get path length between 2 points with the time parameter --- Assets/ZestKit/Splines/AbstractSplineSolver.cs | 5 +++-- Assets/ZestKit/Splines/Spline.cs | 5 ++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Assets/ZestKit/Splines/AbstractSplineSolver.cs b/Assets/ZestKit/Splines/AbstractSplineSolver.cs index c475225..5f066bb 100644 --- a/Assets/ZestKit/Splines/AbstractSplineSolver.cs +++ b/Assets/ZestKit/Splines/AbstractSplineSolver.cs @@ -52,12 +52,13 @@ public float getPathLength(int endPoint) //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) + public float getPathLength(int initPoint, int endPoint, float normalizedInitPos) { float length = 0f; var totalSudivisions = _nodes.Count * totalSubdivisionsPerNodeForLookupTable; float timePerSlice = 1f / totalSudivisions; - Vector3 lastPoint = getPoint(initPoint); + //get the initial point to work with + Vector3 lastPoint = getPoint(normalizedInitPos); for (int i = initPoint+1; i < endPoint + 1; i++) { diff --git a/Assets/ZestKit/Splines/Spline.cs b/Assets/ZestKit/Splines/Spline.cs index 18026a8..6aece1f 100644 --- a/Assets/ZestKit/Splines/Spline.cs +++ b/Assets/ZestKit/Splines/Spline.cs @@ -43,9 +43,8 @@ public float getPathLength(int endPoint) } - public float getPathLength(int initPoint, int endPoint) - { - return _solver.getPathLength(initPoint, endPoint); + public float getPathLength(int initPoint, int endPoint, float normalizedInitPoint) { + return _solver.getPathLength(initPoint, endPoint, normalizedInitPoint); } ///