public override void Run() { var interpolation = new SplineInterpolation(new Dictionary <double, double> { { (DateTime.Now - TimeSpan.FromHours(24)).Ticks, 0.5 }, { (DateTime.Now - TimeSpan.FromHours(23)).Ticks, 0.56 }, { (DateTime.Now - TimeSpan.FromHours(22)).Ticks, 0.43 }, { (DateTime.Now - TimeSpan.FromHours(20)).Ticks, 0.78 }, { (DateTime.Now - TimeSpan.FromHours(18)).Ticks, 0.93 }, { (DateTime.Now - TimeSpan.FromHours(17)).Ticks, 0.54 }, { (DateTime.Now - TimeSpan.FromHours(16)).Ticks, 0.23 }, { (DateTime.Now - TimeSpan.FromHours(15)).Ticks, 0.245 } }); var result = new[] { interpolation.GetValue((DateTime.Now - TimeSpan.FromHours(24)).Ticks), interpolation.GetValue((DateTime.Now - TimeSpan.FromHours(23)).Ticks), interpolation.GetValue((DateTime.Now - TimeSpan.FromHours(21)).Ticks), interpolation.GetValue((DateTime.Now - TimeSpan.FromHours(19)).Ticks), interpolation.GetValue((DateTime.Now - TimeSpan.FromHours(15)).Ticks), interpolation.GetValue((DateTime.Now - TimeSpan.FromHours(14)).Ticks), interpolation.GetValue((DateTime.Now - TimeSpan.FromHours(13)).Ticks), interpolation.GetValue((DateTime.Now - TimeSpan.FromHours(12)).Ticks), interpolation.GetValue((DateTime.Now - TimeSpan.FromHours(25)).Ticks) }; }
void CheckWhichAlgorithm() { header = GameObject.Find("Heading").GetComponent <Text>(); if (PlayerPrefs.HasKey("Bernstein")) { header.text = "BB Form".ToString(); SetAllBoolFalse(); bernstien = GameObject.Find("Canvas").GetComponent <BernstienBasis>(); BB = true; } else if (PlayerPrefs.HasKey("DeCastleju")) { header.text = "DeCastleju's Algorithm".ToString(); SetAllBoolFalse(); NLI = GameObject.Find("Canvas").GetComponent <DeCastleju_Algorithm>(); Nested = true; } else if (PlayerPrefs.HasKey("NewtonForm")) { header.text = "Newton's Form".ToString(); SetAllBoolFalse(); newton = GameObject.Find("Canvas").GetComponent <NewtonForm>(); NEWTON = true; } else if (PlayerPrefs.HasKey("SplineInterpolation")) { header.text = "Spline Interpolation 2D".ToString(); SetAllBoolFalse(); spline = GameObject.Find("Canvas").GetComponent <SplineInterpolation>(); spline2D = true; } }
// Update is called once per frame private void Update() { if (target != null) { Vector3 difference = target.transform.position - transform.position; //difference.y = 0f; //difference.Normalize(); //difference.y = -1f; //difference.Normalize(); //difference *= distance; difference.y = 0f; difference.Normalize(); difference *= distance; Vector3 newPos = target.transform.position - difference; newPos.y = height; //newPos.y = cameraHeight; newPos = SplineInterpolation.Interpolate(previousPos, transform.position, newPos, SplineInterpolation.Extrapolate(transform.position, newPos), Time.deltaTime); previousPos = transform.position; transform.position = newPos; Vector3 lookTarget = target.GetComponent <CarController>().GetVelocity() + target.transform.position; transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(lookTarget - transform.position), Time.deltaTime); } else { ResetCamera(); } }
public MatrixValue Function(MatrixValue original, ScalarValue x) { var spline = new SplineInterpolation(original); var M = new MatrixValue(1, 2); M[1, 1].Re = x.Re; M[1, 2].Re = spline.ComputeValue(x.Re); return(M); }
/// <summary> /// Rysuje wykres dla wybranych danych /// </summary> /// <param name="obj"></param> private void DrawChart(object obj) { if (WindValuesChanged) { ClearPlot(); } WindDirection = AvailableWindDirection; WindSpeed = AvailableWindSpeed; WindValuesChanged = false; IsDataChanged = false; var listToInterpolate = new List <PointD>(); if (!AllRecords) { DataCollection = new ObservableCollection <DataGps>(DataCollection.Where(x => x.WindSpeed == WindSpeed && x.WindDirection == WindDirection)); } foreach (var x in DataCollection) { double pointX = Math.Cos((90 - x.BoatDirection) / (180 / Math.PI)) * x.BoatSpeed; double pointY = Math.Sin((90 - x.BoatDirection) / (180 / Math.PI)) * x.BoatSpeed; listToInterpolate.Add(new PointD(pointX, pointY)); FindMinMaxAxis(listToInterpolate); } listToInterpolate = new List <PointD>(SplineInterpolation.FitGeometric(listToInterpolate)); ChartViewModel.AddNewSeries(listToInterpolate, BoatsCollection, SelectedIndexBoat, SessionCollection, SelectedIndexSession, _minX, _maxX, _minY, _maxY, AvailableWindSpeed, AvailableWindDirection, AllRecords, _isDataFromExcel); if (_isDataFromExcel) { DataCollection = new ObservableCollection <DataGps>(ExcelDataCollection); } else { GetData(); } }
public MatrixValue Function(MatrixValue original, MatrixValue x) { var spline = new SplineInterpolation(original); return(spline.ComputeValues(x)); }
public override void Update(GameTime gameTime) { bool updateCurve = false; // When the sample is started or when <Space> is pressed, we create a random curve. if (_curve == null || InputService.IsPressed(Keys.Space, true)) { _curve = new Curve2F { PreLoop = _loopType, PostLoop = _loopType, SmoothEnds = _smoothEnds }; // Create random key points. for (float x = 0; x <= 1; x += 0.1f) { float y = RandomHelper.Random.NextFloat(0, 1); _curve.Add(new CurveKey2F { Point = new Vector2F(x, y), // We use the same interpolation type (spline type) for all curve keys. But // it is possible to use a different interpolation type for each curve key. Interpolation = _interpolationType, }); } updateCurve = true; } // If <1> is pressed, we change the loop type. if (InputService.IsPressed(Keys.D1, true)) { // Choose next enumeration value for _loopType. if ((int)_loopType + 1 >= EnumHelper.GetValues(typeof(CurveLoopType)).Length) _loopType = 0; else _loopType++; _curve.PreLoop = _loopType; _curve.PostLoop = _loopType; updateCurve = true; } // If <2> is pressed, we change the spline type. if (InputService.IsPressed(Keys.D2, true)) { // Choose next enumeration value for _interpolationType. if ((int)_interpolationType + 1 >= EnumHelper.GetValues(typeof(SplineInterpolation)).Length) _interpolationType = 0; else _interpolationType++; // We skip Bézier and Hermite splines because both need additional information per // curve key (control points or tangents), which we did not specify in the curve creation. while (_interpolationType == SplineInterpolation.Bezier || _interpolationType == SplineInterpolation.Hermite) { _interpolationType++; } _curve.ForEach(key => key.Interpolation = _interpolationType); updateCurve = true; } // If <3> is pressed, we toggle "SmoothEnds". // If SmoothEnds is enabled the curve is smoother at the first and at the last // key point. The effect of SmoothEnds is visible, for example, if the loop type // is "Oscillate" and the spline type is "CatmullRom". if (InputService.IsPressed(Keys.D3, true)) { _smoothEnds = !_smoothEnds; _curve.SmoothEnds = _smoothEnds; updateCurve = true; } if (updateCurve) { var debugRenderer = GraphicsScreen.DebugRenderer2D; debugRenderer.Clear(); debugRenderer.DrawText( string.Format("\n\nLoop type = {0}\nInterpolation = {1}\nSmoothEnds = {2}", _curve.PreLoop, _curve[0].Interpolation, _curve.SmoothEnds)); // Draw two lines to create chart axes. debugRenderer.DrawLine(new Vector3F(100, 300, 0), new Vector3F(1000, 300, 0), Color.Black, true); debugRenderer.DrawLine(new Vector3F(300, 100, 0), new Vector3F(300, 320, 0), Color.Black, true); Vector2F scale = new Vector2F(400, 200); Vector2F offset = new Vector2F(300, 100); // Draw a small cross for all curve key points. for (int index = 0; index < _curve.Count; index++) { CurveKey2F key = _curve[index]; Vector2F point = scale * key.Point + offset; debugRenderer.DrawLine(new Vector3F(point.X - 5, point.Y - 5, 0), new Vector3F(point.X + 5, point.Y + 5, 0), Color.Black, true); debugRenderer.DrawLine(new Vector3F(point.X + 5, point.Y - 5, 0), new Vector3F(point.X - 5, point.Y + 5, 0), Color.Black, true); } // Draw the curve. const float stepSize = 0.02f; for (float x = -0.5f; x < 1.7f; x += stepSize) { Vector2F point0 = scale * _curve.GetPoint(x) + offset; Vector2F point1 = scale * _curve.GetPoint(x + stepSize) + offset; debugRenderer.DrawLine(new Vector3F(point0.X, point0.Y, 0), new Vector3F(point1.X, point1.Y, 0), Color.Black, true); } } base.Update(gameTime); }
public PathAnimationSample(Microsoft.Xna.Framework.Game game) : base(game) { Rectangle bounds = GraphicsService.GraphicsDevice.Viewport.Bounds; // Create a 2D path. Path2F path = new Path2F { // Path is cyclic. PreLoop = CurveLoopType.Cycle, PostLoop = CurveLoopType.Cycle, // End of path should smoothly interpolate with start of path. SmoothEnds = true, }; // The spline type. const SplineInterpolation splineInterpolation = SplineInterpolation.BSpline; // Add path keys. The parameter of a path key is the time in seconds. path.Add(new PathKey2F { Parameter = 0, Point = new Vector2F(bounds.Center.X, bounds.Center.Y), Interpolation = splineInterpolation, }); path.Add(new PathKey2F { Parameter = 0.5f, Point = new Vector2F(bounds.Center.X / 2.0f, 2.0f * bounds.Center.Y / 3.0f), Interpolation = splineInterpolation, }); path.Add(new PathKey2F { Parameter = 1.0f, Point = new Vector2F(bounds.Center.X, 1.0f * bounds.Center.Y / 3.0f), Interpolation = splineInterpolation, }); path.Add(new PathKey2F { Parameter = 1.5f, Point = new Vector2F(3.0f * bounds.Center.X / 2.0f, 2.0f * bounds.Center.Y / 3.0f), Interpolation = splineInterpolation, }); path.Add(new PathKey2F { Parameter = 2.0f, Point = new Vector2F(bounds.Center.X, bounds.Center.Y), Interpolation = splineInterpolation, }); path.Add(new PathKey2F { Parameter = 2.5f, Point = new Vector2F(bounds.Center.X / 2.0f, 4.0f * bounds.Center.Y / 3.0f), Interpolation = splineInterpolation, }); path.Add(new PathKey2F { Parameter = 3.0f, Point = new Vector2F(bounds.Center.X, 5.0f * bounds.Center.Y / 3.0f), Interpolation = splineInterpolation, }); path.Add(new PathKey2F { Parameter = 3.5f, Point = new Vector2F(3.0f * bounds.Center.X / 2.0f, 4.0f * bounds.Center.Y / 3.0f), Interpolation = splineInterpolation, }); path.Add(new PathKey2F { Parameter = 4.0f, Point = new Vector2F(bounds.Center.X, bounds.Center.Y), Interpolation = splineInterpolation, }); // Create a path animation using the path. // (Start at parameter value 0 and loop forever.) Path2FAnimation pathAnimation = new Path2FAnimation(path) { StartParameter = 0, EndParameter = float.PositiveInfinity, }; AnimationService.StartAnimation(pathAnimation, _animatablePosition).UpdateAndApply(); }
private void button1_Click(object sender, EventArgs e) { var srcXY = new SortedDictionary <double, double>(); List <double> refinedX = new List <double>(); DataGridViewCell cell; double value, valueX, valueY; object cellValue, cellValueX, cellValueY; foreach (DataGridViewRow r in eZDataGridViewXY.Rows) { cellValueX = r.Cells[0].Value; cellValueY = r.Cells[1].Value; if ((cellValueX != null && double.TryParse(cellValueX.ToString(), out valueX)) && (cellValueY != null && double.TryParse(cellValueY.ToString(), out valueY))) { if (!srcXY.ContainsKey(valueX)) { srcXY.Add(valueX, valueY); } } } foreach (DataGridViewRow r in eZDataGridViewID.Rows) { cellValue = r.Cells[0].Value; if (cellValue != null && double.TryParse(cellValue.ToString(), out value)) { refinedX.Add(value); } } // var srcX = srcXY.Keys; var srcY = srcXY.Values; if ((srcX.Max() < refinedX.Max()) && (srcX.Min() > refinedX.Min())) { MessageBox.Show(@"the range of the refined X must be within the range of the source X", @"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } try { _interpY = SplineInterpolation.Execute(srcX.ToArray(), srcY.ToArray(), refinedX.ToArray()); if (_interpY != null && _interpY.Length <= eZDataGridViewID.RowCount) { for (int i = 0; i < _interpY.Length; i++) { cell = eZDataGridViewID.Rows[i].Cells[1]; cell.Value = _interpY[i]; } } } catch (Exception) { MessageBox.Show(@"Something went wrong while calculating the interpolated Y", @"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
public override void Update(GameTime gameTime) { bool updateCurve = false; // When the sample is started or when <Space> is pressed, we create a random curve. if (_curve == null || InputService.IsPressed(Keys.Space, true)) { _curve = new Curve2F { PreLoop = _loopType, PostLoop = _loopType, SmoothEnds = _smoothEnds }; // Create random key points. for (float x = 0; x <= 1; x += 0.1f) { float y = RandomHelper.Random.NextFloat(0, 1); _curve.Add(new CurveKey2F { Point = new Vector2F(x, y), // We use the same interpolation type (spline type) for all curve keys. But // it is possible to use a different interpolation type for each curve key. Interpolation = _interpolationType, }); } updateCurve = true; } // If <1> is pressed, we change the loop type. if (InputService.IsPressed(Keys.D1, true)) { // Choose next enumeration value for _loopType. if ((int)_loopType + 1 >= EnumHelper.GetValues(typeof(CurveLoopType)).Length) { _loopType = 0; } else { _loopType++; } _curve.PreLoop = _loopType; _curve.PostLoop = _loopType; updateCurve = true; } // If <2> is pressed, we change the spline type. if (InputService.IsPressed(Keys.D2, true)) { // Choose next enumeration value for _interpolationType. if ((int)_interpolationType + 1 >= EnumHelper.GetValues(typeof(SplineInterpolation)).Length) { _interpolationType = 0; } else { _interpolationType++; } // We skip Bezier and Hermite splines because both need additional information per // curve key (control points or tangents), which we did not specify in the curve creation. while (_interpolationType == SplineInterpolation.Bezier || _interpolationType == SplineInterpolation.Hermite) { _interpolationType++; } _curve.ForEach(key => key.Interpolation = _interpolationType); updateCurve = true; } // If <3> is pressed, we toggle "SmoothEnds". // If SmoothEnds is enabled the curve is smoother at the first and at the last // key point. The effect of SmoothEnds is visible, for example, if the loop type // is "Oscillate" and the spline type is "CatmullRom". if (InputService.IsPressed(Keys.D3, true)) { _smoothEnds = !_smoothEnds; _curve.SmoothEnds = _smoothEnds; updateCurve = true; } if (updateCurve) { var debugRenderer = GraphicsScreen.DebugRenderer2D; debugRenderer.Clear(); debugRenderer.DrawText( string.Format("\n\nLoop type = {0}\nInterpolation = {1}\nSmoothEnds = {2}", _curve.PreLoop, _curve[0].Interpolation, _curve.SmoothEnds)); // Draw two lines to create chart axes. debugRenderer.DrawLine(new Vector3F(100, 300, 0), new Vector3F(1000, 300, 0), Color.Black, true); debugRenderer.DrawLine(new Vector3F(300, 100, 0), new Vector3F(300, 320, 0), Color.Black, true); Vector2F scale = new Vector2F(400, 200); Vector2F offset = new Vector2F(300, 100); // Draw a small cross for all curve key points. for (int index = 0; index < _curve.Count; index++) { CurveKey2F key = _curve[index]; Vector2F point = scale * key.Point + offset; debugRenderer.DrawLine(new Vector3F(point.X - 5, point.Y - 5, 0), new Vector3F(point.X + 5, point.Y + 5, 0), Color.Black, true); debugRenderer.DrawLine(new Vector3F(point.X + 5, point.Y - 5, 0), new Vector3F(point.X - 5, point.Y + 5, 0), Color.Black, true); } // Draw the curve. const float stepSize = 0.02f; for (float x = -0.5f; x < 1.7f; x += stepSize) { Vector2F point0 = scale * _curve.GetPoint(x) + offset; Vector2F point1 = scale * _curve.GetPoint(x + stepSize) + offset; debugRenderer.DrawLine(new Vector3F(point0.X, point0.Y, 0), new Vector3F(point1.X, point1.Y, 0), Color.Black, true); } } base.Update(gameTime); }
public static bool GenerateMeshFromSpline <SplinePointCustomData>(Spline.Spline spline, Transform transform, int subdivisions, float radius, int smoothingIterations, Vector2 customDataDefault, ref Mesh mesh) where SplinePointCustomData : ISplinePointCustomData { var splinePoints = spline.GetComponentsInChildren <SplinePoint>(); if (splinePoints.Length < 2) { return(false); } var splinePointCount = splinePoints.Length; if (spline._closed && splinePointCount > 2) { splinePointCount++; } var points = new Vector3[(splinePointCount - 1) * 3 + 1]; if (!SplineInterpolation.GenerateCubicSplineHull(splinePoints, points, spline._closed)) { return(false); } // Sample spline // Estimate total length of spline and use this to compute a sample count var lengthEst = 0f; for (int i = 1; i < splinePointCount; i++) { lengthEst += (splinePoints[i % splinePoints.Length].transform.position - splinePoints[i - 1].transform.position).magnitude; } lengthEst = Mathf.Max(lengthEst, 1f); var spacing = 16f / Mathf.Pow(2f, subdivisions + 1); var pointCount = Mathf.CeilToInt(lengthEst / spacing); pointCount = Mathf.Max(pointCount, 1); var sampledPtsOnSpline = new Vector3[pointCount]; var sampledPtsOffSpline = new Vector3[pointCount]; var customData = new Vector2[pointCount]; // First set of sample points lie on spline sampledPtsOnSpline[0] = points[0]; customData[0] = customDataDefault; if (splinePoints[0].TryGetComponent(out SplinePointCustomData customDataComp00)) { customData[0] = customDataComp00.GetData(); } for (var i = 1; i < pointCount; i++) { float t = i / (float)(pointCount - 1); SplineInterpolation.InterpolateCubicPosition(splinePointCount, points, t, out sampledPtsOnSpline[i]); var tpts = t * (splinePoints.Length - 1f); var spidx = Mathf.FloorToInt(tpts); var alpha = tpts - spidx; var customData0 = customDataDefault; if (splinePoints[spidx].TryGetComponent(out SplinePointCustomData customDataComp0)) { customData0 = customDataComp0.GetData(); } var customData1 = customDataDefault; if (splinePoints[Mathf.Min(spidx + 1, splinePoints.Length - 1)].TryGetComponent(out SplinePointCustomData customDataComp1)) { customData1 = customDataComp1.GetData(); } customData[i] = Vector2.Lerp(customData0, customData1, Mathf.SmoothStep(0f, 1f, alpha)); } // Second set of sample points lie off-spline - some distance to the right for (var i = 0; i < pointCount; i++) { var ibefore = i - 1; var iafter = i + 1; if (!spline._closed) { // Not closed - clamp to range ibefore = Mathf.Max(ibefore, 0); iafter = Mathf.Min(iafter, pointCount - 1); } else { // Closed - wrap into range if (ibefore < 0) { ibefore += pointCount; } iafter %= pointCount; } var tangent = sampledPtsOnSpline[iafter] - sampledPtsOnSpline[ibefore]; var normal = tangent; normal.x = tangent.z; normal.z = -tangent.x; normal.y = 0f; normal = normal.normalized; sampledPtsOffSpline[i] = sampledPtsOnSpline[i] + normal * radius; } if (spline._closed) { var midPoint = Vector3.Lerp(sampledPtsOffSpline[0], sampledPtsOffSpline[sampledPtsOffSpline.Length - 1], 0.5f); sampledPtsOffSpline[0] = sampledPtsOffSpline[sampledPtsOffSpline.Length - 1] = midPoint; } // Blur the second set of points to help solve overlaps or large distortions. Not perfect but helps in many cases. if (smoothingIterations > 0) { var scratchPoints = new Vector3[pointCount]; // Ring buffer style access when closed spline if (!spline._closed) { for (var j = 0; j < smoothingIterations; j++) { scratchPoints[0] = sampledPtsOffSpline[0]; scratchPoints[pointCount - 1] = sampledPtsOffSpline[pointCount - 1]; for (var i = 1; i < pointCount - 1; i++) { scratchPoints[i] = (sampledPtsOffSpline[i] + sampledPtsOffSpline[i + 1] + sampledPtsOffSpline[i - 1]) / 3f; scratchPoints[i] = sampledPtsOnSpline[i] + (scratchPoints[i] - sampledPtsOnSpline[i]).normalized * radius; } var tmp = sampledPtsOffSpline; sampledPtsOffSpline = scratchPoints; scratchPoints = tmp; } } else { for (var j = 0; j < smoothingIterations; j++) { for (var i = 0; i < sampledPtsOffSpline.Length; i++) { // Slightly odd indexing. The first and last point are the same, the indices need to wrap to either the // second element (if overflow) or the penultimate element (if underflow) to ensure tension is maintained at ends. var ibefore = i - 1; var iafter = i + 1; if (ibefore < 0) { ibefore = sampledPtsOffSpline.Length - 2; } if (iafter >= sampledPtsOffSpline.Length) { iafter = 1; } scratchPoints[i] = (sampledPtsOffSpline[i] + sampledPtsOffSpline[iafter] + sampledPtsOffSpline[ibefore]) / 3f; scratchPoints[i] = sampledPtsOnSpline[i] + (scratchPoints[i] - sampledPtsOnSpline[i]).normalized * radius; } var tmp = sampledPtsOffSpline; sampledPtsOffSpline = scratchPoints; scratchPoints = tmp; } } } return(UpdateMesh(transform, sampledPtsOnSpline, sampledPtsOffSpline, customData, spline._closed, ref mesh)); }
static void lab2() { var spline = new SplineInterpolation(Fi2, Xi2); spline.GetAnswer(X2); }