Beispiel #1
0
        /// <summary>
        /// Creates a interpolated curve through a set of points.<br/>
        /// <em>Refer to Algorithm A9.1 on The NURBS Book, pp.369-370 for details.</em>
        /// </summary>
        /// <param name="pts">The set of points to interpolate.</param>
        /// <param name="degree">The Curve degree.</param>
        /// <param name="startTangent">The tangent vector for the first point.</param>
        /// <param name="endTangent">The tangent vector for the last point.</param>
        /// <param name="centripetal">True use the chord as per knot spacing, false use the squared chord.</param>
        /// <returns>A the interpolated curve.</returns>
        public static NurbsBase Interpolated(List <Point3> pts, int degree, Vector3?startTangent = null,
                                             Vector3?endTangent = null, bool centripetal = false)
        {
            if (pts.Count < degree + 1)
            {
                throw new Exception($"You must supply at least degree + 1 points. You supplied {pts.Count} pts.");
            }

            // Gets uk parameters.
            List <double> uk = CurveHelpers.Parametrization(pts, centripetal);

            // Compute knot vectors.
            bool       hasTangents = startTangent != null && endTangent != null;
            KnotVector knots       = ComputeKnotsForInterpolation(uk, degree, hasTangents);

            // Global interpolation.
            // Build matrix of basis function coefficients.
            Matrix coeffMatrix = BuildCoefficientsMatrix(pts, degree, hasTangents, uk, knots);
            // Solve for each points.
            List <Point4> ctrlPts = (hasTangents)
                ? SolveCtrlPtsWithTangents(knots, pts, coeffMatrix, degree, new Vector3(startTangent.Value), new Vector3(endTangent.Value))
                : SolveCtrlPts(pts, coeffMatrix);

            return(new NurbsCurve(degree, knots, ctrlPts));
        }
Beispiel #2
0
        public static NurbsBase Approximate(List <Point3> pts, int degree, bool centripetal = false)
        {
            int numberCpts = pts.Count - 1;

            // Gets the parameters curve uk.
            List <double> uk = CurveHelpers.Parametrization(pts, centripetal);

            // Computes knot vectors.
            KnotVector knots = ComputeKnotsForCurveApproximation(uk, degree, numberCpts, pts.Count);

            // Compute matrix N
            Matrix matrixN = new Matrix();

            for (int i = 1; i < pts.Count - 1; i++)
            {
                List <double> tempRow = new List <double>();
                for (int j = 1; j < numberCpts - 1; j++)
                {
                    tempRow.Add(Evaluate.Curve.OneBasisFunction(degree, knots, j, uk[i]));
                }
                matrixN.Add(tempRow);
            }

            // Compute NT matrix.
            Matrix matrixNt = matrixN.Transpose();
            // Compute NTN matrix.
            Matrix matrixNtN = matrixNt * matrixN;

            // Computes Rk - Eqn 9.63.
            List <Point3> Rk = ComputesValuesRk(knots, uk, degree, pts, numberCpts);

            // Compute R - Eqn 9.67.
            var vectorR = ComputeValuesR(knots, uk, Rk, degree, numberCpts);

            // Computes control points, fixing the first and last point from the input points.
            List <Point4> ctrlPts = new List <Point4> {
                pts[0]
            };

            ctrlPts.AddRange(SolveCtrlPts(vectorR, matrixNtN));
            ctrlPts.Add(pts[pts.Count - 1]);

            return(new NurbsCurve(degree, knots, ctrlPts));
        }