예제 #1
0
        /// <summary>
        /// Constructs a ruled surface between two curves.
        /// <em>Follows the algorithm at page 337 of The NURBS Book by Piegl and Tiller.</em>
        /// </summary>
        /// <param name="curveA">The first curve.</param>
        /// <param name="curveB">The second curve.</param>
        /// <returns>A ruled surface.</returns>
        public static NurbsSurface Ruled(NurbsBase curveA, NurbsBase curveB)
        {
            IList <NurbsBase> curves = new[] { curveA, curveB };

            curves = CurveHelpers.NormalizedDegree(curves);
            curves = CurveHelpers.NormalizedKnots(curves);

            return(new NurbsSurface(1, curves[0].Degree, new KnotVector(1, 2), curves[0].Knots,
                                    new List <List <Point4> > {
                curves[0].ControlPoints, curves[1].ControlPoints
            }));
        }
예제 #2
0
        /// <summary>
        /// Constructs a NURBS surface from a set of NURBS curves.<br/>
        /// </summary>
        /// <param name="curves">Set of a minimum of two curves to create the surface.</param>
        /// <param name="loftType">Enum to choose the type of loft generation.</param>
        /// <returns>A NURBS surface.</returns>
        public static NurbsSurface FromLoft(IList <NurbsBase> curves, LoftType loftType = LoftType.Normal)
        {
            if (curves == null)
            {
                throw new ArgumentException("An invalid number of curves to perform the loft.");
            }

            if (curves.Count < 2)
            {
                throw new ArgumentException("An invalid number of curves to perform the loft.");
            }

            if (curves.Any(x => x == null))
            {
                throw new ArgumentException("The input set contains null curves.");
            }

            bool isClosed = curves[0].IsClosed;

            foreach (NurbsBase c in curves.Skip(1))
            {
                if (isClosed != c.IsClosed)
                {
                    throw new ArgumentException("Loft only works if all curves are open, or all curves are closed.");
                }
            }

            // Copy curves for possible operation of homogenization.
            IList <NurbsBase> copyCurves = new List <NurbsBase>(curves);

            // Clamp curves if periodic.
            if (copyCurves[0].IsPeriodic)
            {
                for (int i = 0; i < copyCurves.Count; i++)
                {
                    copyCurves[i] = copyCurves[i].ClampEnds();
                }
            }

            // If necessary, the curves can be brought to a common degree and knots, as we do for the ruled surface.
            // In fact, the ruled surface is a special case of a skinned surface.
            if (copyCurves.Any(c => c.Degree != copyCurves[0].Degree))
            {
                copyCurves = CurveHelpers.NormalizedDegree(copyCurves);
                copyCurves = CurveHelpers.NormalizedKnots(copyCurves);
            }

            int                   degreeV              = copyCurves[0].Degree;
            int                   degreeU              = 3;
            KnotVector            knotVectorU          = new KnotVector();
            KnotVector            knotVectorV          = copyCurves[0].Knots;
            List <List <Point4> > surfaceControlPoints = new List <List <Point4> >();

            switch (loftType)
            {
            case LoftType.Normal:
                List <List <Point4> > tempPts = new List <List <Point4> >();
                for (int n = 0; n < copyCurves[0].ControlPointLocations.Count; n++)
                {
                    List <Point3> pts = copyCurves.Select(c => c.ControlPointLocations[n]).ToList();
                    NurbsBase     crv = Fitting.Curve.Interpolated(pts, degreeU);
                    tempPts.Add(crv.ControlPoints);
                    knotVectorU = crv.Knots;
                }
                surfaceControlPoints = CollectionHelpers.Transpose2DArray(tempPts);
                break;

            case LoftType.Loose:
                surfaceControlPoints = copyCurves.Select(c => c.ControlPoints).ToList();
                knotVectorU          = new KnotVector(degreeU, copyCurves.Count);
                break;
            }
            return(new NurbsSurface(degreeU, degreeV, knotVectorU, knotVectorV, surfaceControlPoints));
        }