public void TestLength() { Vector3[] p1 = { new Vector3(0, 0, 0), new Vector3(0, 1, 0) }; float[] t1 = { 0f, 1f }; LinearSplineCurve lp = new LinearSplineCurve(); lp.Init(p1, t1); Assert.Equals(1.0f, lp.totalTime); Vector3[] p2 = { new Vector3(0, 0, 0), new Vector3(1, 1, 1) }; lp = new LinearSplineCurve(); lp.Init(p2, t1); float p2Length = p2[1].magnitude; float lpTotalTime = lp.totalLength; Assert.True(Mathf.Approximately(p2Length, lpTotalTime)); }
public void TestLinearPathExplicitDistances() { Vector3[] p1 = { new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 2, 1) }; float[] t1 = { 0f, 1f, 1f + Mathf.Sqrt(3) }; LinearSplineCurve lp = new LinearSplineCurve(); lp.Init(p1, t1); Vector3 expectedResult = p1[1]; Assert.Equals(expectedResult, lp.GetPosition(1f)); expectedResult = new Vector3(0f, 0.5f, 0f); Assert.Equals(expectedResult, lp.GetPosition(0.5f)); // test start and end conditions Assert.Equals(Vector3.zero, lp.GetPosition(0.0f)); Vector3 endpoint = new Vector3(1, 2, 1); Assert.Equals(endpoint, lp.GetPosition(lp.totalTime)); }
public void TestRenderingPointsBug() { // test bug with renderingpoints being too long Vector3[] p1 = { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(2, 0, 0), new Vector3(3, 0, 0), }; float[] floats = { 1, 2, 3, 4 }; LinearSplineCurve lp = new LinearSplineCurve(); lp.Init(p1, floats); float[] renderPoints = lp.GetRenderPoints(); Vector3[] vs = new Vector3[renderPoints.Length]; for (int i = 0; i < renderPoints.Length; i++) { vs[i] = lp.GetPosition(renderPoints[i]); } string renderPointsStr = "Renderpoints (linear)"; for (int i = 1; i < vs.Length; i++) { float length = (vs[i - 1] - vs[i]).magnitude; Assert.True(Mathf.Approximately(1.0f, length)); renderPointsStr += vs[i - 1] + " - " + vs[i] + "\n"; } Debug.Log(renderPointsStr); Assert.Equals(4, renderPoints.Length); }
public void TestLengthConCat1() { Vector3[] p1 = { new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 0), }; float[] t1 = { 0f, 1f, 2f }; LinearSplineCurve lp = new LinearSplineCurve(); lp.Init(p1, t1); Assert.Equals(2, lp.totalTime); }
public void TestLengthConCat2() { // more advanced test Vector3[] p2 = { new Vector3(0, 0, 0), new Vector3(1, 1, 1), new Vector3(0, 1, 1), new Vector3(0, 4, 1), }; float[] t1 = { 0f, 1f, 2f, 3f }; LinearSplineCurve lp = new LinearSplineCurve(); lp.Init(p2, t1); Assert.Equals(p2[1].magnitude + 1 + 3, lp.totalLength); }
public void TestRenderPoints() { Vector3[] p1 = { new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 0), }; float[] t1 = { 0f, 1f, 2f }; LinearSplineCurve lp = new LinearSplineCurve(); lp.Init(p1, t1); float[] renderPoints = lp.GetRenderPoints(); Assert.Equals(3, renderPoints.Length); Assert.Equals(0f, renderPoints[0]); Assert.Equals(1f, renderPoints[1]); Assert.Equals(2f, renderPoints[2]); }
public void XTestLinearPath() { Vector3[] p1 = { new Vector3(0, 0, 0), new Vector3(1, 2, 3), }; float[] t1 = { 0f, 1f }; LinearSplineCurve lp = new LinearSplineCurve(); lp.Init(p1, t1); Vector3 expectedResult = new Vector3(.5f, 1, 1.5f); Assert.Equals(expectedResult, lp.GetPosition(0.5f * lp.totalTime)); expectedResult = new Vector3(.25f, .5f, 0.75f); Assert.Equals(expectedResult, lp.GetPosition(0.25f * lp.totalTime)); // test start and end conditions Assert.Equals(p1[0], lp.GetPosition(0.0f)); Assert.Equals(p1[1], lp.GetPosition(lp.totalTime)); }
public void DoUpdateSpline() { List <Vector3> controlPoints = new List <Vector3>(); List <Vector3> tangents = new List <Vector3>(); List <float> time = new List <float>(); float lastTime = 0; for (int i = 0; i < transform.childCount; i++) { ControlPointComponent controlPointComp = transform.GetChild(i).gameObject.GetComponent <ControlPointComponent>(); if (controlPointComp != null) { controlPoints.Add(controlPointComp.position); tangents.Add(controlPointComp.tangent); // if illegal time detected, then autoadjust if (lastTime >= controlPointComp.time) { controlPointComp.time += lastTime + 1; } lastTime = controlPointComp.time; time.Add(controlPointComp.time); } } switch (splineType) { case SplineType.LinearSpline: { LinearSplineCurve lSpline = new LinearSplineCurve(); spline = lSpline; spline.lengthPrecision = lengthPrecision; lSpline.Init(controlPoints.ToArray(), time.ToArray()); } break; case SplineType.CatmullRom: { HermiteSplineCurve hSpline = new HermiteSplineCurve(); spline = hSpline; spline.lengthPrecision = lengthPrecision; hSpline.InitCatmullRom(controlPoints.ToArray()); } break; case SplineType.BezierSpline: case SplineType.BezierSmoothSpline: BezierSplineCurve bSpline = new BezierSplineCurve(); spline = bSpline; spline.lengthPrecision = lengthPrecision; switch (splineType) { case SplineType.BezierSpline: bSpline.Init(controlPoints.ToArray()); break; case SplineType.BezierSmoothSpline: bSpline.InitSmoothTangents(controlPoints.ToArray()); break; } break; } // hack to make the editor update the obejct Vector3 p = transform.position; transform.position = Vector3.zero; if (p.sqrMagnitude > 0) { transform.position = p; } updated = true; }