Exemplo n.º 1
0
        /// <summary>
        /// The points index example is as following.
        /// The reason for this setting is to look at the coordinate like points[x][y].
        /// As a result, The number of U count is 3 and that of V count is 5.
        ///
        ///  3*---7*--11*
        ///    |      |    |
        ///  2*---6*--10*
        ///    |      |    |
        ///  1*---5*---9*
        ///    |      |    |
        ///  0*---4*---8*
        ///
        /// </summary>
        /// <param name="_pointsGrid"></param>
        public NURBSSurface(IEnumerable <IEnumerable <IEnumerable <double> > > _pointsGrid, KnotSet uKnotVector, KnotSet vKnotVector, int uDegree, int vDegree, IEnumerable <IEnumerable <double> > _weights)
        {
            List <List <Point3d> > ptss = new List <List <Point3d> >();
            List <List <double> >  wss  = new List <List <double> >();

            for (int i = 0; i < _pointsGrid.Count(); i++)
            {
                wss.Add(new List <double>());
                var ws = _weights.ElementAt(i);

                ptss.Add(new List <Point3d>());
                var pts = _pointsGrid.ElementAt(i);

                for (int j = 0; j < pts.Count(); j++)
                {
                    double w = ws.ElementAt(j);
                    wss.Last().Add(w);

                    IEnumerable <double> pt = pts.ElementAt(j);
                    Point3d target          = new Point3d(pt);
                    ptss.Last().Add(target);
                }
            }
            this.pointsGrid = ptss.Select(n => n.ToArray()).ToArray();
            this.weights    = wss.Select(n => n.ToArray()).ToArray();

            this.uKnotVector = (KnotSet)uKnotVector.Clone();
            this.vKnotVector = (KnotSet)vKnotVector.Clone();
            this.uDegree     = uDegree;
            this.vDegree     = vDegree;
        }
Exemplo n.º 2
0
        public void CreateToUniform()
        {
            KnotSet knotSet          = null;
            int     degree           = int.MinValue;
            int     numControlPoints = int.MinValue;

            double[] r = null;
            double[] a = null;

            numControlPoints = 3;
            degree           = 2;
            knotSet          = KnotSet.CreateToUniform(degree, numControlPoints);
            r = knotSet.GetKnotArray();
            a = new double[] { 0, 0, 0, 1, 1, 1 };

            Assert.IsTrue(r.SequenceEqual(a));


            numControlPoints = 4;
            degree           = 2;
            knotSet          = KnotSet.CreateToUniform(degree, numControlPoints);
            r = knotSet.GetKnotArray();
            a = new double[] { 0, 0, 0, 0.5, 1, 1, 1 };

            Assert.IsTrue(r.SequenceEqual(a));


            numControlPoints = 8;
            degree           = 3;
            knotSet          = KnotSet.CreateToUniform(degree, numControlPoints);
            r = knotSet.GetKnotArray();
            a = new double[] { 0, 0, 0, 0, 0.2, 0.4, 0.6, 0.8, 1, 1, 1, 1 };

            Assert.IsTrue(r.SequenceEqual(a));
        }
Exemplo n.º 3
0
        public void IsValid()
        {
            double[] knots   = null;
            KnotSet  knotSet = null;
            bool     r;
            bool     a;

            knots   = new double[] { 1, 0, 3, 4 };
            knotSet = new KnotSet(knots);
            r       = false;
            a       = knotSet.IsValid();

            Assert.IsTrue(r == a);

            knots   = new double[] { 1, 2, 3, 2 };
            knotSet = new KnotSet(knots);
            r       = false;
            a       = knotSet.IsValid();

            Assert.IsTrue(r == a);

            knots   = new double[] { 1, 1, 3, 3 };
            knotSet = new KnotSet(knots);
            r       = true;
            a       = knotSet.IsValid();

            Assert.IsTrue(r == a);
        }
Exemplo n.º 4
0
        public void GetIndexOf()
        {
            double[] knots   = null;
            KnotSet  knotSet = null;
            double   r;
            double   a;

            knots   = new double[] { 1, 2, 3, 4 };
            knotSet = new KnotSet(knots);
            r       = knotSet.GetIndexOf(1);
            a       = 2;
            Assert.IsTrue(r == a);

            knots   = new double[] { 1, 2, 3, 4 };
            knotSet = new KnotSet(knots);
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => knotSet.GetIndexOf(-1));

            knots   = new double[] { 1, 2, 3, 4 };
            knotSet = new KnotSet(knots);
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => knotSet.GetIndexOf(5));

            knots   = new double[] { 1, 2, 3, 4 };
            knotSet = new KnotSet(knots, false);
            r       = knotSet.GetIndexOf(-1);
            a       = 1;
            Assert.IsTrue(r == a);

            knots   = new double[] { 1, 2, 3, 4 };
            knotSet = new KnotSet(knots, false);
            r       = knotSet.GetIndexOf(7);
            a       = 4;
            Assert.IsTrue(r == a);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Cox-de Boor recursion formula implementation.
        /// </summary>
        protected Func <double, double> GetBasis(int i, int j, KnotSet knots)
        {
            Func <double, double> func = (t) =>
            {
                double t_i      = knots.GetIndexOf(i);
                double t_iplus1 = knots.GetIndexOf(i + 1);

                if (j == 0)
                {
                    if (t_i <= t && t < t_iplus1)
                    {
                        return(1);
                    }
                    else
                    {
                        return(0);
                    }
                }
                double coef1 = GetCoef(i, j, t, knots);
                double coef2 = 1 - GetCoef(i + 1, j, t, knots);

                Func <double, double> firstTerm  = GetBasis(i, j - 1, knots);
                Func <double, double> secondTerm = GetBasis(i + 1, j - 1, knots);

                double first  = firstTerm(t);
                double second = secondTerm(t);

                return(coef1 * first + coef2 * second);
            };

            return(func);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Cox-de Boor recursion formula implementation.
        /// </summary>
        protected double GetCoef(int i, int j, double t, KnotSet knots)
        {
            if (knots.GetIndexOf(i + j) == knots.GetIndexOf(i))
            {
                return(0);
            }

            return((double)(t - knots.GetIndexOf(i)) / (double)(knots.GetIndexOf(i + j) - knots.GetIndexOf(i)));
        }
Exemplo n.º 7
0
        protected override Func <double, double> GetBasisFunction(BasisInfo _info)
        {
            BSplineBasisInfo info = (BSplineBasisInfo)_info;

            int     i     = info.I;
            int     j     = info.J;
            KnotSet knots = info.Knots;

            Func <double, double> basis = GetBasis(i, j, knots);

            return(basis);
        }
Exemplo n.º 8
0
        public void GetMaxKnot()
        {
            KnotSet knots;

            double[] vec;
            double   r, a;

            vec   = new double[] { 0, 1, 2, 3, 4 };
            knots = new KnotSet(vec);
            r     = knots.GetMaxKnot();
            a     = 4;

            Assert.IsTrue(r == a);
        }
Exemplo n.º 9
0
        public BSplineCurve(IEnumerable <IEnumerable <double> > pts, KnotSet knots, int degree)
        {
            var pointList = new List <Point3d>();

            foreach (var pt in pts)
            {
                Point3d pt3d = new Point3d(pt.ToArray());
                pointList.Add(pt3d);
            }

            this.points     = pointList.ToArray();
            this.knotVector = (KnotSet)knots.Clone();
            this.degree     = degree;
        }
Exemplo n.º 10
0
        public void GetKnotArray()
        {
            double[] knots   = null;
            KnotSet  knotSet = null;

            double[] r;
            double[] a;

            knots   = new double[] { 1, 2, 3, 2 };
            knotSet = new KnotSet(knots);

            r = knotSet.GetKnotArray();
            a = new double[] { 1, 2, 3, 2 };

            Assert.IsTrue(r.SequenceEqual(a));
        }
Exemplo n.º 11
0
        public void BSplineCurve()
        {
            double[][]   pts     = null;
            KnotSet      knotset = null;
            int          degree  = int.MinValue;
            BSplineCurve bSpline = null;

            pts = new double[][]
            {
                new double[] { 0, 0, 0 },
                new double[] { 2, 4, 0 },
                new double[] { 4, 0, 0 },
                new double[] { 6, 4, 0 },
            };
            knotset = new KnotSet(new double[] { 0, 0, 0, 0, 1, 1, 1, 1 });
            degree  = 3;
            bSpline = new BSplineCurve(pts, knotset, degree);
        }
Exemplo n.º 12
0
        public void NURBSCurve()
        {
            double[][] pts     = null;
            KnotSet    knotset = null;
            int        degree  = int.MinValue;
            NURBSCurve bSpline = null;

            double[] weights = null;

            pts = new double[][]
            {
                new double[] { 0, 0, 0 },
                new double[] { 2, 4, 0 },
                new double[] { 4, 0, 0 },
                new double[] { 6, 4, 0 },
            };
            knotset = new KnotSet(new double[] { 0, 0, 0, 0, 1, 1, 1, 1 });
            weights = new double[] { 1, 1, 1, 1 };
            degree  = 3;

            bSpline = new NURBSCurve(pts, knotset, weights, degree);
        }
Exemplo n.º 13
0
        public void ParameterAt()
        {
            double[][][]   ptGrid = null;
            double[]       r, a;
            double         u, v, e;
            BSplineSurface surface = null;
            int            uDegree, vDegree;
            KnotSet        uKnot, vKnot;

            uDegree = 2;
            vDegree = 3;
            uKnot   = KnotSet.CreateToUniform(uDegree, 3);
            vKnot   = KnotSet.CreateToUniform(vDegree, 5);

            ptGrid = new double[][][]
            {
                new double[][] {
                    new double[] { 0.0, 0.0, 1.0 },
                    new double[] { 0.0, 1.0, 2.0 },
                    new double[] { 0.0, 2.0, 0.0 },
                    new double[] { 0.0, 3.0, 1.0 },
                    new double[] { 0.0, 4.0, 1.0 },
                },
                new double[][] {
                    new double[] { 1.0, 0.0, 2.0 },
                    new double[] { 1.0, 1.0, 2.0 },
                    new double[] { 1.0, 2.0, 1.0 },
                    new double[] { 1.0, 3.0, 1.0 },
                    new double[] { 1.0, 4.0, 1.0 },
                },
                new double[][] {
                    new double[] { 2.0, 0.0, 2.0 },
                    new double[] { 2.0, 1.0, 1.0 },
                    new double[] { 2.0, 2.0, 1.0 },
                    new double[] { 2.0, 3.0, 1.0 },
                    new double[] { 2.0, 4.0, 2.0 },
                },
            };
            u       = 0.3;
            v       = 0.4;
            e       = 0.0001;
            surface = new BSplineSurface(ptGrid, uKnot, vKnot, uDegree, vDegree);
            r       = surface.ParameterAt(u, v);
            a       = new double[] { 0.6, 1.696, 1.16312 };

            Assert.IsTrue(r.SequenceEqual(a, e));


            uDegree = 2;
            vDegree = 3;
            uKnot   = KnotSet.CreateToUniform(uDegree, 3);
            vKnot   = KnotSet.CreateToUniform(vDegree, 5);

            ptGrid = new double[][][]
            {
                new double[][] {
                    new double[] { 0.0, 0.0, 1.0 },
                    new double[] { 0.0, 1.0, 2.0 },
                    new double[] { 0.0, 2.0, 0.0 },
                    new double[] { 0.0, 3.0, 1.0 },
                    new double[] { 0.0, 4.0, 1.0 },
                },
                new double[][] {
                    new double[] { 1.0, 0.0, 2.0 },
                    new double[] { 1.0, 1.0, 2.0 },
                    new double[] { 1.0, 2.0, 1.0 },
                    new double[] { 1.0, 3.0, 1.0 },
                    new double[] { 1.0, 4.0, 1.0 },
                },
                new double[][] {
                    new double[] { 2.0, 0.0, 2.0 },
                    new double[] { 2.0, 1.0, 1.0 },
                    new double[] { 2.0, 2.0, 1.0 },
                    new double[] { 2.0, 3.0, 1.0 },
                    new double[] { 2.0, 4.0, 2.0 },
                },
            };
            u       = 1;
            v       = 0;
            e       = 0.0001;
            surface = new BSplineSurface(ptGrid, uKnot, vKnot, uDegree, vDegree);
            r       = surface.ParameterAt(u, v);
            a       = new double[] { 2, 0, 2 };

            Assert.IsTrue(r.SequenceEqual(a, e));


            uDegree = 2;
            vDegree = 3;
            uKnot   = KnotSet.CreateToUniform(uDegree, 3);
            vKnot   = KnotSet.CreateToUniform(vDegree, 5);

            ptGrid = new double[][][]
            {
                new double[][] {
                    new double[] { 0.0, 0.0, 1.0 },
                    new double[] { 0.0, 1.0, 2.0 },
                    new double[] { 0.0, 2.0, 0.0 },
                    new double[] { 0.0, 3.0, 1.0 },
                    new double[] { 0.0, 4.0, 1.0 },
                },
                new double[][] {
                    new double[] { 1.0, 0.0, 2.0 },
                    new double[] { 1.0, 1.0, 2.0 },
                    new double[] { 1.0, 2.0, 1.0 },
                    new double[] { 1.0, 3.0, 1.0 },
                    new double[] { 1.0, 4.0, 1.0 },
                },
                new double[][] {
                    new double[] { 2.0, 0.0, 2.0 },
                    new double[] { 2.0, 1.0, 1.0 },
                    new double[] { 2.0, 2.0, 1.0 },
                    new double[] { 2.0, 3.0, 1.0 },
                    new double[] { 2.0, 4.0, 2.0 },
                },
            };
            u       = 1;
            v       = 1;
            e       = 0.0001;
            surface = new BSplineSurface(ptGrid, uKnot, vKnot, uDegree, vDegree);
            r       = surface.ParameterAt(u, v);
            a       = new double[] { 2, 4, 2 };

            Assert.IsTrue(r.SequenceEqual(a, e));
        }
Exemplo n.º 14
0
        public void ParameterAt()
        {
            double[][]   pts     = null;
            KnotSet      knotset = null;
            int          degree  = int.MinValue;
            BSplineCurve bSpline = null;

            double[] r, a;
            double   t;
            double   e;

            pts = new double[][]
            {
                new double[] { 0, 0, 0 },
                new double[] { 2, 4, 0 },
                new double[] { 4, 0, 0 },
                new double[] { 6, 4, 0 },
            };
            knotset = new KnotSet(new double[] { 0, 0, 0, 0, 1, 1, 1, 1 });
            degree  = 3;
            bSpline = new BSplineCurve(pts, knotset, degree);
            t       = 0.5;
            a       = new double[] { 3, 2, 0 };
            r       = bSpline.ParameterAt(t);

            Assert.IsTrue(a.SequenceEqual(r));


            pts = new double[][]
            {
                new double[] { 0, 0, 0 },
                new double[] { 2, 4, 0 },
                new double[] { 4, 0, 0 },
                new double[] { 6, 4, 0 },
            };
            knotset = new KnotSet(new double[] { 0, 0, 0, 0, 1, 1, 1, 1 });
            degree  = 3;
            bSpline = new BSplineCurve(pts, knotset, degree);
            t       = 0.384;
            a       = new double[] { 2.3040000000000003, 1.9750256640000001, 0 };
            r       = bSpline.ParameterAt(t);
            e       = 0.0001;

            Assert.IsTrue(a.SequenceEqual(r, e));


            pts = new double[][]
            {
                new double[] { 0, 0, 0 },
                new double[] { 2, 4, 0 },
                new double[] { 4, 0, 0 },
                new double[] { 6, 4, 0 },
            };
            knotset = new KnotSet(new double[] { 0, 0, 0, 0, 1, 1, 1, 1 });
            degree  = 3;
            bSpline = new BSplineCurve(pts, knotset, degree);
            t       = 0.884;
            a       = new double[] { 5.304, 2.905969664, 0 };
            r       = bSpline.ParameterAt(t);
            e       = 0.0001;

            Assert.IsTrue(a.SequenceEqual(r, e));


            pts = new double[][]
            {
                new double[] { 1, 0, 0 },
                new double[] { 2, 4, 0 },
                new double[] { 4, 0, 0 },
                new double[] { 6, 4, 0 },
            };
            knotset = new KnotSet(new double[] { 0, 0, 0, 0, 1, 1, 1, 1 });
            degree  = 3;
            bSpline = new BSplineCurve(pts, knotset, degree);
            t       = 0;
            a       = new double[] { 1, 0, 0 };
            r       = bSpline.ParameterAt(t);
            e       = 0.0001;

            Assert.IsTrue(a.SequenceEqual(r, e));


            pts = new double[][]
            {
                new double[] { 1, 0, 0 },
                new double[] { 2, 4, 0 },
                new double[] { 6, 4, 0 },
            };
            knotset = new KnotSet(new double[] { 0, 0, 0, 1, 1, 1 });
            degree  = 2;
            bSpline = new BSplineCurve(pts, knotset, degree);
            t       = 0;
            a       = new double[] { 1, 0, 0 };
            r       = bSpline.ParameterAt(t);
            e       = 0.0001;

            Assert.IsTrue(a.SequenceEqual(r, e));


            pts = new double[][]
            {
                new double[] { 0, 0, 0 },
                new double[] { 2, 4, 0 },
                new double[] { 6, 4, 0 },
            };
            knotset = new KnotSet(new double[] { 0, 0, 0, 1, 1, 1 });
            degree  = 2;
            bSpline = new BSplineCurve(pts, knotset, degree);
            t       = 1;
            a       = new double[] { 6, 4, 0 };
            r       = bSpline.ParameterAt(t);
            e       = 0.0001;

            Assert.IsTrue(a.SequenceEqual(r, e));
        }
Exemplo n.º 15
0
        public void ParameterAt()
        {
            double[][] pts     = null;
            double[]   weights = null;
            KnotSet    knotset = null;
            int        degree  = int.MinValue;
            NURBSCurve nurbs   = null;

            double[] r, a;
            double   t, e;

            pts = new double[][]
            {
                new double[] { 0, 0, 0 },
                new double[] { 2, 4, 0 },
                new double[] { 4, 0, 0 },
            };

            weights = new double[] { 1, 3, 1 };
            knotset = new KnotSet(new double[] { 0, 0, 0, 1, 1, 1 });
            degree  = 2;
            nurbs   = new NURBSCurve(pts, knotset, weights, degree);
            t       = 0.5;
            a       = new double[] { 2, 3, 0 };
            r       = nurbs.ParameterAt(t);

            Assert.IsTrue(a.SequenceEqual(r));


            pts = new double[][]
            {
                new double[] { 0, 0, 0 },
                new double[] { 2, 4, 0 },
                new double[] { 4, 0, 0 },
            };

            weights = new double[] { 1, 3, 1 };
            knotset = new KnotSet(new double[] { 0, 0, 0, 1, 1, 1 });
            degree  = 2;
            nurbs   = new NURBSCurve(pts, knotset, weights, degree);
            t       = 1;
            a       = new double[] { 4, 0, 0 };
            r       = nurbs.ParameterAt(t);
            e       = 0.0001;

            Assert.IsTrue(a.SequenceEqual(r, e));


            pts = new double[][]
            {
                new double[] { 0, 0, 0 },
                new double[] { 2, 4, 0 },
                new double[] { 6, 4, 0 },
            };
            weights = new double[] { 1, 1, 1 };
            knotset = new KnotSet(new double[] { 0, 0, 0, 1, 1, 1 });
            degree  = 2;
            nurbs   = new NURBSCurve(pts, knotset, weights, degree);
            t       = 1;
            a       = new double[] { 6, 4, 0 };
            r       = nurbs.ParameterAt(t);
            e       = 0.0001;

            Assert.IsTrue(a.SequenceEqual(r, e));


            pts = new double[][]
            {
                new double[] { 0.0, 0.0, 0.0 },
                new double[] { 4.0, 5.0, 0.0 },
                new double[] { 6.0, 1.0, 0.0 },
                new double[] { 9.0, 5.0, 0.0 },
                new double[] { 11.0, 3.0, 0.0 },
                new double[] { 16.0, 4.0, 0.0 },
            };

            weights = new double[] { 1, 3, 1, 3, 1, 2 };
            knotset = new KnotSet(new double[] { 0, 0, 0, 0, 1d / 3d, 2d / 3d, 1, 1, 1, 1 });
            degree  = 3;
            nurbs   = new NURBSCurve(pts, knotset, weights, degree);
            t       = 0.3;
            a       = new double[] { 5.52901, 3.849042, 0.0 };
            r       = nurbs.ParameterAt(t);

            e = 0.0001;
            Assert.IsTrue(Math.Abs(a[0] - r[0]) < e);
            Assert.IsTrue(Math.Abs(a[1] - r[1]) < e);
            Assert.IsTrue(Math.Abs(a[2] - r[2]) < e);


            pts = new double[][]
            {
                new double[] { 0.0, 0.0, 0.0 },
                new double[] { 4.0, 5.0, 0.0 },
                new double[] { 6.0, 1.0, 0.0 },
                new double[] { 9.0, 5.0, 0.0 },
                new double[] { 11.0, 3.0, 0.0 },
                new double[] { 16.0, 4.0, 0.0 },
            };

            weights = new double[] { 1, 3, 1, 3, 1, 2 };
            knotset = new KnotSet(new double[] { 0, 0, 0, 0, 1d / 3d, 2d / 3d, 1, 1, 1, 1 });
            degree  = 3;
            nurbs   = new NURBSCurve(pts, knotset, weights, degree);
            t       = 0.3948;
            a       = new double[] { 6.9053986159719711, 3.6972003072706183, 0.0 };
            r       = nurbs.ParameterAt(t);

            e = 0.0001;
            Assert.IsTrue(r.SequenceEqual(a, e));


            pts = new double[][]
            {
                new double[] { 0.0, 0.0, 0.0 },
                new double[] { 4.0, 5.0, 0.0 },
                new double[] { 6.0, 1.0, 0.0 },
                new double[] { 9.0, 5.0, 0.0 },
                new double[] { 11.0, 3.0, 0.0 },
                new double[] { 16.0, 4.0, 0.0 },
            };

            weights = new double[] { 1, 3, 1, 3, 1, 2 };
            knotset = new KnotSet(new double[] { 0, 0, 0, 0, 1d / 3d, 2d / 3d, 1, 1, 1, 1 });
            degree  = 3;
            nurbs   = new NURBSCurve(pts, knotset, weights, degree);
            t       = 1;
            a       = new double[] { 16, 4, 0 };
            r       = nurbs.ParameterAt(t);

            e = 0.0001;
            Assert.IsTrue(r.SequenceEqual(a, e));
        }
Exemplo n.º 16
0
 public BSplineBasisInfo(int i, int j, KnotSet knots)
 {
     this.I     = i;
     this.J     = j;
     this.Knots = (KnotSet)knots.Clone();
 }