public unsafe void GenerateBezier() { var points = new List <Vector2>(100); var seed = (uint)1; var random = new Unity.Mathematics.Random(seed); for (int i = 0; i < 100; i++) { points.Add(random.NextFloat2()); } var original = new OPCurves.CurveBuilder(DistanceBetweenPoints, CurveSplitError); for (int i = 0; i < points.Count; i++) { original.AddPoint(points[i]); } var tanL = original.GetLeftTangent(original._pts.Count); var tanR = original.GetRightTangent(original._first); var optimizedPts = new List <Vector2>(); for (int i = 0; i < original._pts.Count; i++) { optimizedPts.Add(original._pts[i]); } var optimizedU = new List <float>(); for (int i = 0; i < original._u.Count; i++) { optimizedU.Add(original._u[i]); } CubicBezier originalBezier; using (original._pts.ViewAsNativeArray(out var pts)) using (original._u.ViewAsNativeArray(out var u)) OptimizedHelpers.GenerateBezierStandard(0, original._pts.Count, tanL, tanR, (Vector2 *)pts.GetUnsafePtr(), (float *)u.GetUnsafePtr(), out originalBezier); CubicBezier optimizedBezier; using (optimizedPts.ViewAsNativeArray(out var pts)) using (optimizedU.ViewAsNativeArray(out var u)) OptimizedHelpers.GenerateBezierV256V1(0, original._pts.Count, tanL, tanR, (Vector2 *)pts.GetUnsafePtr(), (float *)u.GetUnsafePtr(), out optimizedBezier); for (int i = 0; i < original._pts.Count; i++) { Assert.IsTrue(math.all(aprox(original._pts[i], optimizedPts[i]))); } for (int i = 0; i < original._u.Count; i++) { Assert.IsTrue(aprox(original._u[i], optimizedU[i])); } Assert.IsTrue(Equals(originalBezier, optimizedBezier)); }
public static unsafe void RunGenerateBezierTest(List <Vector2> list, uint seed) { var watch = Stopwatch.StartNew(); var original = new OPCurves.CurveBuilder(DistanceBetweenPoints, CurveSplitError); for (int i = 0; i < list.Count; i++) { original.AddPoint(list[i]); } Debug.Log($"Generated curve ({list.Count} points) in: {watch.Elapsed.TotalSeconds} seconds"); var tanL = original.GetLeftTangent(original._pts.Count); var tanR = original.GetRightTangent(0); var optimizedPts = CreateCopyOf(original._pts); var optimizedU = CreateCopyOf(original._u); //Debug.Assert(original._pts.Count <= original._u.Count); const int WarmupCount = 25; // 100 const int MeasurementCount = 100; // 100 const int IterationsPerMeasurement = 25; // 25 #if false Measure.Method(() => { using (optimizedPts.ViewAsNativeArray(out var pts)) using (optimizedU.ViewAsNativeArray(out var u)) OptimizedHelpers.GenerateBezierStandard(0, original._pts.Count, tanL, tanR, (Vector2 *)pts.GetUnsafePtr(), (float *)u.GetUnsafePtr(), out var b); }) .SetUp(() => { CopyTo(optimizedPts, original._pts); CopyTo(optimizedU, original._u); }) .WarmupCount(WarmupCount) .MeasurementCount(MeasurementCount) .IterationsPerMeasurement(IterationsPerMeasurement) .SampleGroup("GenerateBezierStandard") .Run(); #else Measure.Method(() => { using (optimizedPts.ViewAsNativeArray(out var pts)) using (optimizedU.ViewAsNativeArray(out var u)) OptimizedHelpers.GenerateBezierUnityMathematics(0, original._pts.Count, tanL, tanR, (Vector2 *)pts.GetUnsafePtr(), (float *)u.GetUnsafePtr(), out var b); }) .SetUp(() => { CopyTo(optimizedPts, original._pts); CopyTo(optimizedU, original._u); }) .WarmupCount(WarmupCount) .MeasurementCount(MeasurementCount) .IterationsPerMeasurement(IterationsPerMeasurement) .SampleGroup("GenerateBezierUnityMathematics") .Run(); Measure.Method(() => { using (optimizedPts.ViewAsNativeArray(out var pts)) using (optimizedU.ViewAsNativeArray(out var u)) OptimizedHelpers.GenerateBezierV128V0(0, original._pts.Count, tanL, tanR, (Vector2 *)pts.GetUnsafePtr(), (float *)u.GetUnsafePtr(), out var b); }) .SetUp(() => { CopyTo(optimizedPts, original._pts); CopyTo(optimizedU, original._u); }) .WarmupCount(WarmupCount) .MeasurementCount(MeasurementCount) .IterationsPerMeasurement(IterationsPerMeasurement) .SampleGroup("GenerateBezierV128V0") .Run(); Measure.Method(() => { using (optimizedPts.ViewAsNativeArray(out var pts)) using (optimizedU.ViewAsNativeArray(out var u)) OptimizedHelpers.GenerateBezierV128V1(0, original._pts.Count, tanL, tanR, (Vector2 *)pts.GetUnsafePtr(), (float *)u.GetUnsafePtr(), out var b); }) .SetUp(() => { CopyTo(optimizedPts, original._pts); CopyTo(optimizedU, original._u); }) .WarmupCount(WarmupCount) .MeasurementCount(MeasurementCount) .IterationsPerMeasurement(IterationsPerMeasurement) .SampleGroup("GenerateBezierV128V1") .Run(); #if false Measure.Method(() => { using (optimizedPts.ViewAsNativeArray(out var pts)) using (optimizedU.ViewAsNativeArray(out var u)) OptimizedHelpers.GenerateBezierV256V1(0, original._pts.Count, tanL, tanR, (Vector2 *)pts.GetUnsafePtr(), (float *)u.GetUnsafePtr(), out var b); }) .SetUp(() => { CopyTo(optimizedPts, original._pts); CopyTo(optimizedU, original._u); }) .WarmupCount(WarmupCount) .MeasurementCount(MeasurementCount) .IterationsPerMeasurement(IterationsPerMeasurement) .SampleGroup("GenerateBezierV256V1") .Run(); #endif #endif Debug.Log($"{new StackFrame(1).GetMethod().Name} finished"); }