/** * Calculates a Bezier interpolated path for the given points. */ public void Interpolate(Point3DList segmentPoints, double scale) { controlPoints.Clear(); if (segmentPoints.Count < 2) { return; } for (int i = 0; i < segmentPoints.Count; i++) { if (i == 0) // is first { Point3D p1 = segmentPoints[i]; Point3D p2 = segmentPoints[i + 1]; controlPoints.Add(p1); controlPoints.Add(Point3DList.Interpolate(p1, p2, scale)); } else if (i == segmentPoints.Count - 1) //last { Point3D p0 = segmentPoints[i - 1]; Point3D p1 = segmentPoints[i]; controlPoints.Add(Point3DList.Interpolate(p0, p1, scale)); controlPoints.Add(p1); } else { Point3D p0 = segmentPoints[i - 1]; Point3D p1 = segmentPoints[i]; Point3D p2 = segmentPoints[i + 1]; Vector3d tp = (p2.Position - p0.Position).Normalized(); Vector3d pos0 = p1.Position - scale * tp * (p1.Position - p0.Position).Length; Vector3d pos1 = p1.Position + scale * tp * (p2.Position - p1.Position).Length; Vector3d tn = (p2.Normal - p0.Normal).Normalized(); Vector3d norm0 = p1.Normal - scale * tn * (p1.Normal - p0.Normal).Length; Vector3d norm1 = p1.Normal + scale * tn * (p2.Normal - p1.Normal).Length; controlPoints.Add(new Point3D(pos0, norm0, p0.Color.GetStepColor(p1.Color, scale))); controlPoints.Add(p1); controlPoints.Add(new Point3D(pos1, norm1, p1.Color.GetStepColor(p2.Color, scale))); } } curveCount = (controlPoints.Count - 1) / 3; }
/** * Sample the given points as a Bezier path. */ public void SamplePoints(Point3DList sourcePoints, double minSqrDistance, double maxSqrDistance, double scale) { if (sourcePoints.Count < 2) { return; } Stack <Point3D> samplePoints = new Stack <Point3D>(); samplePoints.Push(sourcePoints[0]); Point3D potentialSamplePoint = sourcePoints[1]; int i = 2; for (i = 2; i < sourcePoints.Count; i++) { if ( ((potentialSamplePoint.Position - sourcePoints[i].Position).LengthSquared > minSqrDistance) && ((samplePoints.Peek().Position - sourcePoints[i].Position).LengthSquared > maxSqrDistance)) { samplePoints.Push(potentialSamplePoint); } potentialSamplePoint = sourcePoints[i]; } //now handle last bit of curve Point3D p1 = samplePoints.Pop(); //last sample point Point3D p0 = samplePoints.Peek(); //second last sample point Vector3d posT = (p0.Position - potentialSamplePoint.Position).Normalized(); double pos_d2 = (potentialSamplePoint.Position - p1.Position).Length; double pos_d1 = (p1.Position - p0.Position).Length; double pos_scale = ((pos_d1 - pos_d2) / 2f); Vector3d pos_ = p1.Position + posT * pos_scale; Vector3d normT = (p0.Normal - potentialSamplePoint.Normal).Normalized(); double norm_d2 = (potentialSamplePoint.Normal - p1.Normal).Length; double norm_d1 = (p1.Normal - p0.Normal).Length; Vector3d norm_ = p1.Normal + normT * ((norm_d1 - norm_d2) / 2); samplePoints.Push(new Point3D(pos_, norm_, p1.Color.GetStepColor(p0.Color, pos_scale))); samplePoints.Push(potentialSamplePoint); Point3DList l = new Point3DList(samplePoints.Count); l.AddRange(samplePoints); Interpolate(l, scale); }
Point3DList FindDrawingPoints(int curveIndex) { Point3DList pointList = new Point3DList(); Point3D left = CalculateBezierPoint(curveIndex, 0); Point3D right = CalculateBezierPoint(curveIndex, 1); pointList.Add(left); pointList.Add(right); FindDrawingPoints(curveIndex, 0, 1, pointList, 1); return(pointList); }
/** * This gets the drawing points of a bezier curve, using recursive division, * which results in less points for the same accuracy as the above implementation. */ public Point3DList GetDrawingPoints2() { Point3DList drawingPoints = new Point3DList(); for (int curveIndex = 0; curveIndex < curveCount; curveIndex++) { Point3DList bezierCurveDrawingPoints = FindDrawingPoints(curveIndex); if (curveIndex != 0) { //remove the fist point, as it coincides with the last point of the previous Bezier curve. bezierCurveDrawingPoints.RemoveAt(0); } drawingPoints.AddRange(bezierCurveDrawingPoints); } return(drawingPoints); }
/// <summary> /// Get a interpolated list of this list composed od "count" point based on the Y position of another list of points /// </summary> /// <param name="count"></param> /// <returns></returns> public Point3DList GetYInterpolated(Point3DList reflist) { int count = reflist.Count; Point3DList ret = new Point3DList(count); if (count == 0) { return(ret); } for (int i = 0; i < count; i++) { double y = reflist[i].Position.Y; Point3D p = GetInterpolateByY(reflist[i].Position.Y, i == 0); ret.Add(p); } return(ret); }
/** * Gets the drawing points. This implementation simply calculates a certain number * of points per curve. */ public Point3DList GetDrawingPoints0() { Point3DList drawingPoints = new Point3DList(); for (int curveIndex = 0; curveIndex < curveCount; curveIndex++) { if (curveIndex == 0) //Only do this for the first end point. //When i != 0, this coincides with the //end point of the previous segment, { drawingPoints.Add(CalculateBezierPoint(curveIndex, 0)); } for (int j = 1; j <= SEGMENTS_PER_CURVE; j++) { double t = j / (double)SEGMENTS_PER_CURVE; drawingPoints.Add(CalculateBezierPoint(curveIndex, t)); } } return(drawingPoints); }
/// <summary> /// Get a interpolated list of this list composed od "count" point /// </summary> /// <param name="count"></param> /// <returns></returns> public Point3DList GetInterpolatedList(int count) { Point3DList ret = new Point3DList(count); if (count == 0) { return(ret); } else if (count == Count) { ret.AddRange(this); } else { double max = Max.Y; double d = (max - Min.Y) / count; for (int i = 0; i < count; i++) { Point3D p = GetInterpolateByY(max - i * d, i == 0); ret.Add(p); } } return(ret); }
/** * Sets the control points of this Bezier path. * Points 0-3 forms the first Bezier curve, points * 3-6 forms the second curve, etc. */ public void SetControlPoints(Point3DList newControlPoints) { controlPoints.Clear(); controlPoints.AddRange(newControlPoints); curveCount = (controlPoints.Count - 1) / 3; }
private int curveCount; //how many bezier curves in this path? /** * Constructs a new empty Bezier curve. Use one of these methods * to add points: SetControlPoints, Interpolate, SamplePoints. */ public BezierBuilder(int segmentPerCurve = 10, double minSquareDistance = 0.01f) { controlPoints = new Point3DList(); SEGMENTS_PER_CURVE = segmentPerCurve; MINIMUM_SQR_DISTANCE = minSquareDistance; }