public void test() { if (UseBlend == false) { int frameNum = bp.frames; pos.Clear(); rot.Clear(); BVHParser.BVHBone.BVHChannel[] channels = bp.root.channels; for (int i = 0; i < frameNum; i++) { Vector3 p, r; p = new Vector3(-channels[0].values[i], channels[2].values[i], -channels[1].values[i]); r = new Vector3(channels[3].values[i], channels[4].values[i], channels[5].values[i]); pos.Add(p); rot.Add(r); //Gizmos.DrawSphere(p, 3); } List <Vector3> reduced = CurvePreprocess.RdpReduce(pos, error); // use the Ramer-Douglas-Pueker algorithm to remove unnecessary points Debug.Log(reduced.Count); curves.Clear(); curves.AddRange(CurveFit.Fit(reduced, error2)); // fit the curves to those points createControlObj(); DrawMutiCurve(); } }
public static void Main(string[] args) { // do once so it's in cache List <VECTOR> data = new List <VECTOR>(TestData.data); List <VECTOR> reduced = CurvePreprocess.RdpReduce(data, 2); CurveFit.Fit(reduced, 8); Console.WriteLine("{0,-40}{1}", "typeof(VECTOR)", typeof(VECTOR).FullName); Console.WriteLine("{0,-40}{1}", "RyuJIT enabled", RyuJitStatus.RyuJitEnabled); Console.WriteLine("{0,-40}{1}", "SIMD enabled", RyuJitStatus.SimdEnabled); Console.WriteLine("{0,-40}{1}", "Iterations Per Test", N_ITERS); Console.WriteLine("{0,-40}{1}", "Test Data Size", data.Count); Console.WriteLine(); double totalTime = 0; Stopwatch sw = new Stopwatch(); Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10} {5}", "RDP Error", "Fit Error", "Points", "Curves", "Time (s)", "Time Per Iter (ms)"); Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10} {5}", "---------", "---------", "------", "------", "--------", "------------------"); foreach (FLOAT rdpError in _paramRdpError) { foreach (FLOAT fitError in _paramFitError) { int nPts, nCurves; double t = runTest(sw, data, rdpError, fitError, out nPts, out nCurves); totalTime += t; Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10:N4} {5,-10:N4}", rdpError, fitError, nPts, nCurves, t, (t / N_ITERS) * 1000); } } Console.WriteLine(); Console.WriteLine("TOTAL TIME: " + totalTime); Console.WriteLine(); }
void CreateFitCurves() { //--------------------------------------- //convert point to vector int j = currentPointSet.Count; List <Vector2> data = new List <Vector2>(j); for (int i = 0; i < j; ++i) { Point pp = currentPointSet[i]; data.Add(new Vector2(pp.x, pp.y)); } //CurvePreprocess.Linearize(data, 8); //List<Vector2> reduced = CurvePreprocess.RdpReduce(data, 2); //string code = DumpPointsToString(); //string code2 = DumpPointsToString2(); //var data2 = data; var data2 = CurvePreprocess.RdpReduce(data, 2); j = data2.Count; List <Point> simplifiedPoints = new List <Point>(); this.simplifiedPointSets.Add(simplifiedPoints); for (int i = 0; i < j; ++i) { var pp = data2[i]; simplifiedPoints.Add(new Point((int)pp.x, (int)pp.y)); } CubicBezier[] cubicBzs = CurveFit.Fit(data2, 8); cubicCurves.AddRange(cubicBzs); }
public TOFFitResults FitTOF(TOF t) { TOFFitResults results = new TOFFitResults(); double[] coefficients = new double[4]; // guess some reasonable values coefficients[0] = t.Data[0]; coefficients[1] = ArrayOperation.GetMax(t.Data) - coefficients[0]; double lastT = t.Times[t.Times.Length - 1]; coefficients[2] = (t.Times[0] + lastT) / 2; coefficients[3] = (lastT - t.Times[0]) / 5; double mse = 0; // this is totally tedious! double[] times = new double[t.Times.Length]; for (int i = 0; i < t.Times.Length; i++) { times[i] = (double)t.Times[i]; } CurveFit.NonLinearFit(times, t.Data, new ModelFunctionCallback(gaussian), coefficients, out mse, 1000000); results.Background = coefficients[0]; results.Amplitude = coefficients[1]; results.Centre = coefficients[2]; results.Width = Math.Abs(coefficients[3]); return(results); }
/// <summary> /// Generates an AnimationCurve based on input Ease Equation Method. /// </summary> /// <param name="equation">Enum of Ease Equation To use.</param> /// <param name="animCurve">AnimationCurve that contains converted results.</param> /// <param name="conversionProperties">Struct that defines various curve conversion properties.</param> /// <param name="debug">When true will log various debug statements.</param> public static void ConvertEaseEquationToCurve(EasingEquationsDouble.Equations equation, AnimationCurve animCurve, ConversionProperties conversionProperties, bool debug = false) { m_EaseMethod = ( Ease )Delegate.CreateDelegate(typeof(Ease), typeof(EasingEquationsDouble).GetMethod(equation.ToString())); List <Vector2> inPts = CreatePointsFromEaseEquation(conversionProperties, false); List <Vector2> ppPts = ConvertToAnimationCurve.Preprocess(inPts, conversionProperties.m_PreprocessMode, conversionProperties.m_PointDistance, conversionProperties.m_RdpError); if (debug) { Debug.LogFormat("ConvertEaseEquationToCurve: {0:D3} {1:D4} : {2}", ppPts.Count, inPts.Count, equation); } if (conversionProperties.m_UseCurveFit) { CubicBezier[] curves = CurveFit.Fit(ppPts, conversionProperties.m_FitError); if (debug) { Debug.LogFormat("ConvertEaseEquationToCurve: {0} curves: {1}", equation, curves.Length); } ConvertToAnimationCurve.ConvertToAnimCurve(animCurve, conversionProperties, curves); } else { ConvertToAnimationCurve.ConvertToAnimCurve(animCurve, conversionProperties, ppPts, debug); } }
public void GetSmooth() { if (this.isValidSmooth) { return; } this.isValidSmooth = true; //lets smooth it //string str1 = dbugDumpPointsToString(contPoints); //string str2 = dbugDumpPointsToString2(contPoints); //var data2 = CurvePreprocess.RdpReduce(contPoints, 2); var data2 = contPoints; CubicBezier[] cubicBzs = CurveFit.Fit(data2, 8); //PathWriter pWriter = new PathWriter(); //pWriter.StartFigure(); //int j = cubicBzs.Length; //for (int i = 0; i < j; ++i) //{ // CubicBezier bz = cubicBzs[i]; // pWriter.MoveTo(bz.p0.x, bz.p0.y); // pWriter.LineTo(bz.p0.x, bz.p0.y); // pWriter.Curve4(bz.p1.x, bz.p1.y, // bz.p2.x, bz.p2.y, // bz.p3.x, bz.p3.y); //} //pWriter.CloseFigureCCW(); vxs = new VertexStore(); int j = cubicBzs.Length; //1. if (j > 0) { //1st CubicBezier bz0 = cubicBzs[0]; vxs.AddMoveTo(bz0.p0.x, bz0.p0.y); vxs.AddLineTo(bz0.p0.x, bz0.p0.y); vxs.AddP3c(bz0.p1.x, bz0.p1.y); vxs.AddP3c(bz0.p2.x, bz0.p2.y); vxs.AddLineTo(bz0.p3.x, bz0.p3.y); //------------------------------- for (int i = 1; i < j; ++i) //start at 1 { CubicBezier bz = cubicBzs[i]; vxs.AddP3c(bz.p1.x, bz.p1.y); vxs.AddP3c(bz.p2.x, bz.p2.y); vxs.AddLineTo(bz.p3.x, bz.p3.y); } //------------------------------- //close vxs.AddLineTo(bz0.p0.x, bz0.p0.y); } vxs.AddCloseFigure(); PixelFarm.Agg.VertexSource.CurveFlattener cflat = new PixelFarm.Agg.VertexSource.CurveFlattener(); vxs = cflat.MakeVxs(vxs); }
public void MakeSmoothPath() { if (this.isValidSmooth) { return; } this.isValidSmooth = true; //-------- if (contPoints.Count == 0) { return; } //return; //-------- //lets smooth it //string str1 = dbugDumpPointsToString(contPoints); //string str2 = dbugDumpPointsToString2(contPoints); //var data2 = CurvePreprocess.RdpReduce(contPoints, 2); var data2 = contPoints; CubicBezier[] cubicBzs = CurveFit.Fit(data2, 8); vxs = new VertexStore(); int j = cubicBzs.Length; //1. if (j > 0) { //1st CubicBezier bz0 = cubicBzs[0]; vxs.AddMoveTo(bz0.p0.x, bz0.p0.y); vxs.AddLineTo(bz0.p0.x, bz0.p0.y); vxs.AddCurve4To( bz0.p1.x, bz0.p1.y, bz0.p2.x, bz0.p2.y, bz0.p3.x, bz0.p3.y); //------------------------------- for (int i = 1; i < j; ++i) //start at 1 { CubicBezier bz = cubicBzs[i]; vxs.AddCurve4To( bz.p1.x, bz.p1.y, bz.p2.x, bz.p2.y, bz.p3.x, bz.p3.y); } //------------------------------- //close //TODO: we not need this AddLineTo() vxs.AddLineTo(bz0.p0.x, bz0.p0.y); } vxs.AddCloseFigure(); VertexStore v2 = new VertexStore(); cflat.MakeVxs(vxs, v2); vxs = v2; }
public static double[] FitLorenzianToSlaveData(CavityScanData data, double limitLow, double limitHigh) { double mse = 0; double[] voltages = data.parameters.CalculateRampVoltages(); double[] coefficients = new double[] { (data.parameters.High - data.parameters.Low) / 10, voltages[ArrayOperation.GetIndexOfMax(data.SlavePhotodiodeData)], ArrayOperation.GetMax(data.SlavePhotodiodeData) - ArrayOperation.GetMin(data.SlavePhotodiodeData), 0 }; CurveFit.NonLinearFit(voltages, data.SlavePhotodiodeData, new ModelFunctionCallback(lorentzianNarrow), coefficients, out mse, 4000); fitFailSafe(coefficients, limitLow, limitHigh); return(coefficients); }
public static int Main(string[] args) { // Before we do anything, we want to call these to force them to be JITed. Ohterwise the first time they are called will give wildly different // performance results than the user expects. This only takes a few ms, so it won't slow down app startup that much. List <VECTOR> data = new List <VECTOR>(TestData.data); CurvePreprocess.Linearize(data, 8); List <VECTOR> reduced = CurvePreprocess.RdpReduce(data, 2); CurveFit.Fit(reduced, 8); // Okay, now run the app return(WpfMain.run <App>()); }
private static double runTest(Stopwatch sw, List <VECTOR> data, FLOAT rdpError, FLOAT fitError, out int nPtsReduced, out int nCurves) { List <VECTOR> reduced = null; CubicBezier[] curves = null; sw.Reset(); sw.Start(); for (int i = 0; i < N_ITERS; i++) { reduced = CurvePreprocess.RdpReduce(data, rdpError); curves = CurveFit.Fit(reduced, fitError); } sw.Stop(); nPtsReduced = reduced.Count; nCurves = curves.Length; return(sw.Elapsed.TotalSeconds); }
private CubicBezier[] fitCurves() { // Access the dependency properties outside the stopwatch area FLOAT fitError = ((FLOAT)FittingError).clamp((FLOAT)FIT_ERROR_MIN, (FLOAT)FIT_ERROR_MAX); FLOAT linDist = ((FLOAT)PointDistance).clamp((FLOAT)POINT_DIST_MIN, (FLOAT)POINT_DIST_MAX); FLOAT rdpError = ((FLOAT)RdpError).clamp((FLOAT)RDP_ERROR_MIN, (FLOAT)RDP_ERROR_MAX); PreprocessModes ppMode = PreprocessMode; List <VECTOR> inPts = _points; _stopwatch.Reset(); _stopwatch.Start(); List <VECTOR> ppPts = preprocess(inPts, ppMode, linDist, rdpError); CubicBezier[] curves = CurveFit.Fit(ppPts, fitError); _stopwatch.Stop(); LastFitTime = _stopwatch.Elapsed.TotalMilliseconds; PointCount = inPts.Count + "/" + ppPts.Count; return(curves); }
public void Solve() { var data = new List<CurvePoint>(); data.Add(new CurvePoint(){x = 1, y = Generator(1)}); data.Add(new CurvePoint(){x = 2, y = Generator(2)}); var x = 2; long sumOfError = 0; //We first assume a constant curve if (data[1].y != data[0].y) { sumOfError += data[0].y; do { var fit = new CurveFit(data); x++; data.Add(new CurvePoint() { x = x, y = Generator(x) }); Console.WriteLine(fit.ToString()); if (fit[x] != data.Last().y) { sumOfError += fit[x]; } else { break; } } while (true); } var answer = sumOfError; }
public List <CubicBezier> FitBezier(List <Vector3> pts, List <float> times, bool useRdp = true) { AllInputPoints = new List <Vector3d>(pts.Count); foreach (var pt in pts) { AllInputPoints.Add(pt); } inputPointsTree = new PointAABBTree3(new PointSet(AllInputPoints.ToArray())); AllInputPointTimes = times; if (useRdp) { var reduced = CurvePreprocess.RdpReduce(pts, Globals.RDP_ERROR, out List <int> keepIdx); PointsKeepIdx = keepIdx; InputPoints = reduced; Curves = new List <CubicBezier>(CurveFit.Fit(reduced, Globals.BEZIER_FIT_MAX_ERROR)); } else { InputPoints = pts; Curves = new List <CubicBezier>(CurveFit.Fit(pts, Globals.BEZIER_FIT_MAX_ERROR)); } Curves[0] = new CubicBezier( Curves[0].p0, Curves[0].p0 + (Curves[0].p1 - Curves[0].p0).magnitude * Vector3.forward, Curves[0].p2, Curves[0].p3); if (Curves.Count == 0) { ControlPoints = new Vector3[0]; } else { ControlPoints = new Vector3[1 + 3 * Curves.Count]; ControlPoints[0] = Curves[0].p0; } for (int i = 0; i < Curves.Count; ++i) { ControlPoints[3 * i + 1] = Curves[i].p1; ControlPoints[3 * i + 2] = Curves[i].p2; ControlPoints[3 * i + 3] = Curves[i].p3; } float[] cpData = new float[ControlPoints.Length * 3]; for (int i = 0; i < ControlPoints.Length; ++i) { cpData[3 * i + 0] = ControlPoints[i].x; cpData[3 * i + 1] = ControlPoints[i].y; cpData[3 * i + 2] = ControlPoints[i].z; } if (_path != IntPtr.Zero) { _DeleteUnmanagedPathObject(_path); } _path = _CreateUnmanagedPathObject(cpData, ControlPoints.Length); CacheArcLength(); if (times.Count == 0) { paramToTimeCache = paramToArcLengthCache; } else { CacheTime(); } InputPointArcLengths = new List <float>(InputPoints.Count); foreach (var pt in InputPoints) { var res = GetClosestPoint(pt, out Vector3 closest, out int bezierIdx, out float param); if (res) { InputPointArcLengths.Add(GetArcLengthFromSplineParam(bezierIdx, param)); } else { throw new Exception("Something horrible happened!"); } } return(Curves); }
void SimplifyPaths() { //return; //-------- //lets smooth it //string str1 = dbugDumpPointsToString(contPoints); //string str2 = dbugDumpPointsToString2(contPoints); //var data2 = CurvePreprocess.RdpReduce(contPoints, 2); List <Vector2> data2 = contPoints; CubicBezier[] cubicBzs = CurveFit.Fit(data2, 8); vxs = new VertexStore(); int j = cubicBzs.Length; //1. if (j > 1) { //1st CubicBezier bz0 = cubicBzs[0]; vxs.AddMoveTo(bz0.p0.x, bz0.p0.y); vxs.AddLineTo(bz0.p0.x, bz0.p0.y); if (!bz0.HasSomeNanComponent) { vxs.AddCurve4To( bz0.p1.x, bz0.p1.y, bz0.p2.x, bz0.p2.y, bz0.p3.x, bz0.p3.y); } else { vxs.AddLineTo(bz0.p3.x, bz0.p3.y); } //------------------------------- for (int i = 1; i < j; ++i) //start at 1 { CubicBezier bz = cubicBzs[i]; if (!bz.HasSomeNanComponent) { vxs.AddCurve4To( bz.p1.x, bz.p1.y, bz.p2.x, bz.p2.y, bz.p3.x, bz.p3.y); } else { vxs.AddLineTo(bz0.p3.x, bz0.p3.y); } } //------------------------------- //close //TODO: we not need this AddLineTo() vxs.AddLineTo(bz0.p0.x, bz0.p0.y); vxs.AddCloseFigure(); } else if (j == 1) { CubicBezier bz0 = cubicBzs[0]; vxs.AddMoveTo(bz0.p0.x, bz0.p0.y); if (!bz0.HasSomeNanComponent) { vxs.AddCurve4To( bz0.p1.x, bz0.p1.y, bz0.p2.x, bz0.p2.y, bz0.p3.x, bz0.p3.y); } else { vxs.AddLineTo(bz0.p3.x, bz0.p3.y); } } else { // = 0 } //TODO: review here VertexStore v2 = new VertexStore(); cflat.MakeVxs(vxs, v2); vxs = v2; }
public void FindBestAug(GameObject[] linepoints) { frame = GlobalData.FrameCountList[0]; //hip = GameObject.Find("hip"); // c_numSamples = GlobalData.FrameCount+1; List <Vector3> pts = new List <Vector3>(); for (int i = 0; i < linepoints.Length; i++) { pts.Add(linepoints[i].transform.position); } CurvePreprocess.RemoveDuplicates(pts); List <Vector3> reduced = CurvePreprocess.RdpReduce(pts, 0.0001f); CubicBezier[] curves = CurveFit.Fit(reduced, 0.1f); Vector3 offset = new Vector3(1, 0, 0); for (int i = 0; i < curves.Length; i++) { GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube); obj.name = "conp0"; obj.transform.localPosition = curves[i].p0 - offset; obj.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f); obj.transform.SetParent(this.transform); GameObject obj1 = GameObject.CreatePrimitive(PrimitiveType.Cube); obj1.name = "conp1"; obj1.transform.localPosition = curves[i].p1 - offset; obj1.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f); obj1.transform.SetParent(this.transform); GameObject obj2 = GameObject.CreatePrimitive(PrimitiveType.Cube); obj2.name = "conp2"; obj2.transform.localPosition = curves[i].p2 - offset; obj2.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f); obj2.transform.SetParent(this.transform); GameObject obj3 = GameObject.CreatePrimitive(PrimitiveType.Cube); obj3.name = "conp3"; obj3.transform.localPosition = curves[i].p3 - offset; obj3.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f); obj3.transform.SetParent(this.transform); GameObject[] contset = { obj, obj1, obj2, obj3 }; controlpts.Add(contset); } for (int i = 0; i < controlpts.Count; i++) { Vector3[] vcot = { controlpts[i][0].transform.position, controlpts[i][1].transform.position, controlpts[i][2].transform.position, controlpts[i][3].transform.position }; prevcontrolpts.Add(vcot); } if (controlpts.Count > 1) { c_numSamples = (GlobalData.FrameCountList[0] / controlpts.Count) + 1; } else { c_numSamples = GlobalData.FrameCountList[0]; } //DrawBezier(); calparaBezier(); InitializeArcLengths(); tangentCalculation(); for (int i = 0; i < tangpoint.Count; i++) { origtangpoint.Add(tangpoint[i]); } }