private void UpdatePieces(List <Point> data) { d_ignoreUpdatePreview = true; bool hasperiod = ValidPeriod; List <Undo.IAction> actions = new List <Undo.IAction>(); Biorob.Math.Range range = DeterminePeriod(); double period = 0; if (hasperiod) { period = Period; } foreach (Cdn.FunctionPolynomialPiece piece in d_function.Pieces) { actions.Add(new Undo.RemoveFunctionPolynomialPiece(d_function, piece)); } if (hasperiod && range != null) { // Remove data outside the range data.RemoveAll(d => d.X <range.Min || d.X> range.Max); } // Then make it periodic Biorob.Math.Interpolation.PChip pchip = new Biorob.Math.Interpolation.PChip(); data.Sort(); if (hasperiod) { Biorob.Math.Interpolation.Periodic.Extend(data, 0, period); } Biorob.Math.Functions.PiecewisePolynomial poly = pchip.InterpolateSorted(data); foreach (Biorob.Math.Functions.PiecewisePolynomial.Piece piece in poly.Pieces) { Cdn.FunctionPolynomialPiece p; p = new FunctionPolynomialPiece(piece.Begin, piece.End, piece.Coefficients); actions.Add(new Undo.AddFunctionPolynomialPiece(d_function, p)); } try { d_actions.Do(new Undo.Group(actions)); } catch (Exception e) { Error(this, e); } d_ignoreUpdatePreview = false; UpdatePreview(); }
private List <Undo.IAction> RemovePeriodicActions() { // Remove pieces which were generated from the period Biorob.Math.Range range = DeterminePeriod(); List <Undo.IAction> actions = new List <Undo.IAction>(); if (range == null) { return(actions); } foreach (Cdn.FunctionPolynomialPiece piece in d_function.Pieces) { if (piece.Begin < range.Min || piece.End > range.Max) { actions.Add(new Undo.RemoveFunctionPolynomialPiece(d_function, piece)); } } return(actions); }
private void UpdatePreview() { if (d_ignoreUpdatePreview) { return; } foreach (Plot.Renderers.Line line in d_previewLines) { d_graph.Graph.Remove(line); } d_previewLines.Clear(); d_dataLine = null; d_iscubic = true; List <Biorob.Math.Functions.PiecewisePolynomial.Piece> pieces = new List <Biorob.Math.Functions.PiecewisePolynomial.Piece>(); // Determine if we are going to draw cubics or just sampled foreach (Cdn.FunctionPolynomialPiece piece in d_function.Pieces) { if (piece.Coefficients.Length != 4) { d_iscubic = false; } pieces.Add(new Biorob.Math.Functions.PiecewisePolynomial.Piece(new Biorob.Math.Range(piece.Begin, piece.End), piece.Coefficients)); } Biorob.Math.Range period = DeterminePeriod(pieces); if (period != null) { d_period.Text = (period.Max - period.Min).ToString(); } else { d_period.Text = ""; } Biorob.Math.Functions.PiecewisePolynomial poly; poly = new Biorob.Math.Functions.PiecewisePolynomial(pieces); double msize = 8; int lw = 2; Plot.Renderers.MarkerStyle mtype = Plot.Renderers.MarkerStyle.FilledCircle; if (poly.Count == 0 && d_lastAddedData != null) { List <Point> data = new List <Point>(); data.Add(d_lastAddedData); Plot.Renderers.Line line = new Plot.Renderers.Line { Data = data, Color = d_graph.Graph.ColorMap[0], YLabel = "preview" }; line.MarkerSize = msize; line.MarkerStyle = mtype; line.LineWidth = lw; d_graph.Graph.Add(line); d_previewLines.Add(line); d_dataLine = line; } else if (d_iscubic && poly.Count != 0) { // If it's cubic, then use the bezier line Plot.Renderers.Bezier bezier = new Plot.Renderers.Bezier { PiecewisePolynomial = poly, Color = d_graph.Graph.ColorMap[0], YLabel = "preview" }; bezier.Periodic = period; bezier.MarkerSize = msize; bezier.MarkerStyle = mtype; bezier.LineWidth = lw; d_graph.Graph.Add(bezier); d_previewLines.Add(bezier); d_dataLine = bezier; } else if (poly.Count != 0) { // Otherwise use two lines, one with markers, the other sampled Plot.Renderers.Line line = new Plot.Renderers.Line { YLabel = "preview" }; line.Color = d_graph.Graph.ColorMap[0]; line.LineWidth = lw; if (pieces.Count > 0) { line.GenerateData(new Biorob.Math.Range(pieces[0].Begin, pieces[pieces.Count - 1].End), 1000, x => new Biorob.Math.Point(x, poly.Evaluate(x))); } d_graph.Graph.Add(line); Plot.Renderers.Line markers = new Plot.Renderers.Line(); markers.Color = line.Color; markers.MarkerSize = msize; markers.MarkerStyle = mtype; List <Point> data = new List <Point>(); foreach (Biorob.Math.Functions.PiecewisePolynomial.Piece piece in pieces) { data.Add(new Point(piece.Begin, piece.Coefficients[piece.Coefficients.Length - 1])); } if (pieces.Count > 0) { Biorob.Math.Functions.PiecewisePolynomial.Piece piece = pieces[pieces.Count - 1]; data.Add(new Point(piece.End, piece.Coefficients.Sum())); } d_graph.Graph.Add(markers); d_previewLines.Add(line); d_previewLines.Add(markers); d_dataLine = markers; } }