public static void FitParametric(Vector4[] points, int nOutputPoints, out Vector4[] interpolation, out Vector4[] dinterpolation, out Vector4[] ddinterpolation, Vector4 firstDerivative = new Vector4(), Vector4 lastDerivative = new Vector4()) { // Compute distances int n = points.Length; float[] dists = new float[n]; // cumulative distance dists[0] = 0; float totalDist = 0; for (int i = 1; i < n; i++) { Vector4 derivative = points[i] - points[i - 1]; float dist = new Vector3(derivative.x, derivative.y, derivative.z).magnitude; // radius does not counts towards distance covered totalDist += dist; dists[i] = totalDist; } // Create 'times' to interpolate to float dt = totalDist / (nOutputPoints - 1); float[] times = new float[nOutputPoints]; times[0] = 0; for (int i = 1; i < nOutputPoints; i++) { times[i] = times[i - 1] + dt; } // Normalize the slopes, if specified firstDerivative.Normalize(); lastDerivative.Normalize(); // Spline fit both x and y to times CubicSpline xSpline = new CubicSpline(); interpolation = xSpline.FitAndEval(dists, points, times, out dinterpolation, out ddinterpolation, firstDerivative / dt, lastDerivative / dt); }
public void drow() { X = Convert.ToDouble(textBox2.Text); GraphPane grap = zedGraphControl1.GraphPane; grap.CurveList.Clear();// очищаю график double Xmin = 0; double Xmax = 1; PointPairList point = new PointPairList(); double h = 1.0 / X; for (double i = Xmin; i < Xmax; i++) { point.Add(i, funk(i));// заполняю точки } var pane = zedGraphControl1.MasterPane; var toInterpolate = new List <float>(); for (double i = Xmin; i < Xmax; i++) { toInterpolate.Add((float)i); } var spline = new CubicSpline(); var ys = spline.FitAndEval(point.Select(p => (float)p.X).ToArray(), point.Select(p => (float)p.Y).ToArray(), toInterpolate.ToArray()); var splinePairs = new PointPairList(toInterpolate.Select(e => (double)e).ToArray(), ys.Select(e => (double)e).ToArray()); LineItem line = grap.AddCurve("График", point, Color.Red, SymbolType.Star); // стою линию grap.AddCurve("Asaa", splinePairs, Color.Green, SymbolType.Square); zedGraphControl1.AxisChange(); zedGraphControl1.Invalidate(); }
private static float[,] CalcFitMatrix(List <List <float> > measuredValues, float compareValue, List <float> rowMeasurementPoints, List <float> columnMeasurementPoints, int fitDataMatrixWidth, int fitDataMatrixHeight, string fileName) { var currentRowIndex = 0; CubicSpline fitDataCurve = new CubicSpline(); var fitDataMatrix = new float[fitDataMatrixWidth, fitDataMatrixHeight]; float stepSize = (rowMeasurementPoints[rowMeasurementPoints.Count - 1] - rowMeasurementPoints[0]) / (fitDataMatrixWidth - 1); float[] columnIndexes = new float[fitDataMatrixWidth]; for (int i = 0; i < fitDataMatrixWidth; i++) { columnIndexes[i] = rowMeasurementPoints[0] + i * stepSize; } var rowIndexes = new float[fitDataMatrixHeight]; for (var rowIndex = 0; rowIndex < fitDataMatrixHeight; rowIndex++) { rowIndexes[rowIndex] = rowIndex; } foreach (var rowMeasuredSize in measuredValues) { fitDataCurve = new CubicSpline(); float[] rowFitData = fitDataCurve.FitAndEval(rowMeasurementPoints.ToArray(), rowMeasuredSize.ToArray(), columnIndexes); var rowNumber = (int)columnMeasurementPoints[currentRowIndex]; var yIndex = 0; foreach (var rowFitDataValue in rowFitData) { fitDataMatrix[yIndex, rowNumber] = rowFitDataValue; yIndex++; } // string path = @"spline-wikipedia.png"; // PlotSplineSolution("test", rowMeasurementPoints.ToArray(), rowMeasuredSize.ToArray(), columnIndexes, rowFitData, path); currentRowIndex++; } //process all column pixels var columnMeasureSizesAsFloat = new List <float>(); foreach (var yAxisMeasurePoint in columnMeasurementPoints) { columnMeasureSizesAsFloat.Add(yAxisMeasurePoint); } for (var columnIndex = 0; columnIndex < fitDataMatrixWidth; columnIndex++) { fitDataCurve = new CubicSpline(); var columnFitData = new List <float>(); for (var columnFitDataIndex = 0; columnFitDataIndex < columnMeasurementPoints.Count; columnFitDataIndex++) { columnFitData.Add(fitDataMatrix[columnIndex, (int)columnMeasurementPoints[columnFitDataIndex]]); } float[] columnValues = fitDataCurve.FitAndEval(columnMeasureSizesAsFloat.ToArray(), columnFitData.ToArray(), rowIndexes); var measureMatrixRowIndex = 0; foreach (var columnValue in columnValues) { fitDataMatrix[columnIndex, measureMatrixRowIndex] = columnValue; measureMatrixRowIndex++; } } if (fileName != null) { //export matrix to csv var stringBuilder = new StringBuilder(); for (var rowIndex = 0; rowIndex < fitDataMatrixHeight; rowIndex++) { for (var columnIndex = 0; columnIndex < fitDataMatrixWidth; columnIndex++) { stringBuilder.Append(fitDataMatrix[columnIndex, rowIndex] + ";"); } } DebugManager.SaveStringToTextFile(stringBuilder, fileName + ".txt"); using (var heatmap = new Bitmap(fitDataMatrixWidth, fitDataMatrixHeight)) { var g = Graphics.FromImage(heatmap); var lowestOffsetX = compareValue; var highestOffsetX = compareValue; for (var columnIndex = 0; columnIndex < fitDataMatrixWidth; columnIndex++) { for (var rowIndex = 0; rowIndex < fitDataMatrixHeight; rowIndex++) { if (fitDataMatrix[columnIndex, rowIndex] < lowestOffsetX) { lowestOffsetX = fitDataMatrix[columnIndex, rowIndex]; } if (fitDataMatrix[columnIndex, rowIndex] > highestOffsetX) { highestOffsetX = fitDataMatrix[columnIndex, rowIndex]; } } } var totalLowestOffset = compareValue - lowestOffsetX; var totalHighestOffset = highestOffsetX - compareValue; for (var columnIndex = 0; columnIndex < fitDataMatrixWidth; columnIndex++) { for (var rowIndex = 0; rowIndex < fitDataMatrixHeight; rowIndex++) { if (fitDataMatrix[columnIndex, rowIndex] > compareValue) { var xPercentageOffset = (fitDataMatrix[columnIndex, rowIndex] - compareValue) / totalHighestOffset; var bluep = ((int)(xPercentageOffset * (255))); var greenp = 255 - (int)(xPercentageOffset * (255)); g.DrawLine(new Pen(Color.FromArgb(255, 0, greenp, bluep)), columnIndex, rowIndex, columnIndex + 1, rowIndex); } else if (fitDataMatrix[columnIndex, rowIndex] < compareValue) { var xPercentageOffset = (compareValue - fitDataMatrix[columnIndex, rowIndex]) / totalLowestOffset; var redp = ((int)(xPercentageOffset * (255))); var greenp = 255 - (int)(xPercentageOffset * (255)); g.DrawLine(new Pen(Color.FromArgb(255, redp, greenp, 0)), columnIndex, rowIndex, columnIndex + 1, rowIndex); } else if (fitDataMatrix[columnIndex, rowIndex] == 1) { g.DrawLine(new Pen(Color.Green), columnIndex, rowIndex, columnIndex + 1, rowIndex); } } } if (fileName != null) { DebugManager.SaveImage(heatmap, fileName + ".png"); } } } return(fitDataMatrix); }
/// <summary> /// Static all-in-one method to fit the splines and evaluate at X coordinates. /// </summary> /// <param name="x">Input. X coordinates to fit.</param> /// <param name="y">Input. Y coordinates to fit.</param> /// <param name="xs">Input. X coordinates to evaluate the fitted curve at.</param> /// <param name="startSlope">Optional slope constraint for the first point. Single.NaN means no constraint.</param> /// <param name="endSlope">Optional slope constraint for the final point. Single.NaN means no constraint.</param> /// <param name="debug">Turn on console output. Default is false.</param> /// <returns>The computed y values for each xs.</returns> public static Vector4[] Compute(float[] dists, Vector4[] points, float[] times, out Vector4[] dy, out Vector4[] ddy, Vector4 startSlope, Vector4 endSlope, bool debug = false) { CubicSpline spline = new CubicSpline(); return(spline.FitAndEval(dists, points, times, out dy, out ddy, startSlope, endSlope, debug)); }
/// <summary> /// Static all-in-one method to fit the splines and evaluate at X coordinates. /// </summary> /// <param name="x">Input. X coordinates to fit.</param> /// <param name="y">Input. Y coordinates to fit.</param> /// <param name="xs">Input. X coordinates to evaluate the fitted curve at.</param> /// <param name="startSlope">Optional slope constraint for the first point. Single.NaN means no constraint.</param> /// <param name="endSlope">Optional slope constraint for the final point. Single.NaN means no constraint.</param> /// <param name="debug">Turn on console output. Default is false.</param> /// <returns>The computed y values for each xs.</returns> public static float[] Compute(float[] x, float[] y, float[] xs, float startSlope = float.NaN, float endSlope = float.NaN, bool debug = false) { CubicSpline spline = new CubicSpline(); return(spline.FitAndEval(x, y, xs, startSlope, endSlope, debug)); }