Example #1
0
        /// <summary>
        /// Calculates the partial v derivation of the <see cref="BSplineSurface"/>.
        /// </summary>
        /// <param name="u">Parameter u.</param>
        /// <param name="v">Parameter v.</param>
        /// <returns>The partial v derivation.</returns>
        public override xyz vDerivation(double u, double v)
        {
            if ((UKnots == null) || (VKnots == null))
            {
                return(new xyz(0, 0, 0));
            }
            Utils.SplineExValue[] VCoeffs = Utils.CoeffSplineEx(VKnots, ControlPoints.GetLength(1), VDegree, VPeriodicity, v);
            double[] UCoeffs  = Utils.CoeffSpline(UKnots, ControlPoints.GetLength(0), UDegree, UPeriodicity, u);
            double   WDerived = 0;
            double   W        = 0;

            for (int i = 0; i < UCoeffs.Length; i++)
            {
                for (int j = 0; j < VCoeffs.Length; j++)
                {
                    W        = W + Weights[i, j] * UCoeffs[i] * VCoeffs[j].Value;
                    WDerived = WDerived + Weights[i, j] * UCoeffs[i] * VCoeffs[j].Derivation;
                }
            }


            xyz Result = new xyz();

            for (int i = 0; i < ControlPoints.GetLength(0); i++)
            {
                for (int k = 0; k < ControlPoints.GetLength(1); k++)
                {
                    Result = Result + ControlPoints[i, k] * (Weights[i, k] * UCoeffs[i] * (VCoeffs[k].Derivation * W - WDerived * VCoeffs[k].Value) / (W * W));
                }
            }
            return(Base.Absolut(Result) - Base.BaseO);
        }
Example #2
0
        /// <summary>
        /// overrides the <see cref="Surface.RefreshEnvBox"/> for the calculation of the enclosing <see cref="Surface.EnvBox"/>.
        /// </summary>
        protected override void RefreshEnvBox()
        {
            xyz[] P  = new xyz[ControlPoints.GetLength(0) * ControlPoints.GetLength(1)];
            int   id = 0;

            for (int i = 0; i < ControlPoints.GetLength(0); i++)
            {
                for (int j = 0; j < ControlPoints.GetLength(1); j++)
                {
                    P[id] = ControlPoints[i, j];
                    id++;
                }
            }
            EnvBox = Box.GetEnvBox(P);
        }
Example #3
0
        void ReCalculateBuffer()
        {
            Rectangled R = DefinitionDomain;

            U_Coeff = new double[100][];
            double Step = (float)R.Width / 100f;

            for (int i = 0; i < 100; i++)
            {
                U_Coeff[i] = Utils.CoeffSpline(UKnots, ControlPoints.GetLength(0), UDegree, UPeriodicity, R.Left + i * Step);
            }

            V_Coeff = new double[100][];
            Step    = (float)R.Height / 100f;
            for (int i = 0; i < 100; i++)
            {
                V_Coeff[i] = Utils.CoeffSpline(VKnots, ControlPoints.GetLength(1), VDegree, VPeriodicity, R.Bottom + i * Step);
            }
        }
Example #4
0
        public Brep ToNurbsSurface()
        {
            var nurbs_surface = Rhino.Geometry.NurbsSurface.Create(
                3,
                false,
                OrderX,
                OrderY,
                ControlPoints.GetLength(0),
                ControlPoints.GetLength(1)
                );

            // add the knots
            for (int u = 1; u < nurbs_surface.KnotsU.Count; u++)
            {
                nurbs_surface.KnotsU[u - 1] = (KnotsX[u] - KnotsX[0]) / (KnotsX[nurbs_surface.KnotsU.Count - 1] - KnotsX[0]);
            }
            nurbs_surface.KnotsU[nurbs_surface.KnotsU.Count - 1] = nurbs_surface.KnotsU[nurbs_surface.KnotsU.Count - 2];
            //nurbs_surface.KnotsU[u] = KnotsX[u];
            for (int v = 1; v < nurbs_surface.KnotsV.Count; v++)
            {
                nurbs_surface.KnotsV[v - 1] = (KnotsY[v] - KnotsY[0]) / (KnotsY[nurbs_surface.KnotsV.Count - 1] - KnotsY[0]);
            }
            nurbs_surface.KnotsV[nurbs_surface.KnotsV.Count - 1] = nurbs_surface.KnotsV[nurbs_surface.KnotsV.Count - 2];
            //nurbs_surface.KnotsV[v] = KnotsY[v];

            // add the control points
            for (int u = 0; u < nurbs_surface.Points.CountU; u++)
            {
                for (int v = 0; v < nurbs_surface.Points.CountV; v++)
                {
                    nurbs_surface.Points.SetPoint(u, v, ControlPoints[u, v]);
                }
            }

            if (nurbs_surface.IsValid)
            {
                return(nurbs_surface.ToBrep());
            }
            return(null);
        }
Example #5
0
        /// <summary>
        /// Calculates the partial v derivation of the <see cref="BSplineSurface"/>.
        /// </summary>
        /// <param name="u">Parameter u.</param>
        /// <param name="v">Parameter v.</param>
        /// <returns>The partial v derivation.</returns>
        public override xyz vDerivation(double u, double v)
        {
            try
            {
                xyz      Result = new xyz(0, 0, 0);
                double[] UCoeff = Utils.CoeffSpline(UKnots, ControlPoints.GetLength(0), UDegree, UPeriodicity, v);
                double[] VCoeff = Utils.CoeffSpline(VKnots, ControlPoints.GetLength(1), VDegree, VPeriodicity, u);
                Utils.SplineExValue[] VCoeffDerived = Utils.CoeffSplineEx(VKnots, ControlPoints.GetLength(1), VDegree, VPeriodicity, u);

                int FirstUCoeffNonZero = 0;
                for (int i = 0; i < UCoeff.Length; i++)
                {
                    if (UCoeff[i] != 0)
                    {
                        FirstUCoeffNonZero = i;
                        break;
                    }
                }

                int FirstVCoeffNonZero = 0;
                for (int i = 0; i < VCoeffDerived.Length; i++)
                {
                    if (VCoeffDerived[i].Value != 0)
                    {
                        FirstVCoeffNonZero = i;
                        break;
                    }
                }
                int UntilU = Math.Min(FirstUCoeffNonZero + UDegree, UCoeff.Length - 1);
                int UntilV = Math.Min(FirstVCoeffNonZero + VDegree, VCoeffDerived.Length - 1);
                // Derivation without Weigths
                for (int i = FirstUCoeffNonZero; i <= UntilU; i++)
                {
                    for (int k = FirstVCoeffNonZero; k <= UntilV; k++)

                    {
                        Result = Result + ControlPoints[i, k] * (VCoeffDerived[k].Derivation * UCoeff[i]);
                    }
                }

                // Derivation width Weigth

                // noch machen

                return(Base.Absolut(Result) - Base.BaseO);
            }
            catch (Exception)
            {
                if ((ControlPoints.GetLength(0) + UDegree + 1) != UKnots.Length)
                {
                    System.Windows.Forms.MessageBox.Show("the definition of the UKnots is wrong.");
                }
                else
                if ((ControlPoints.GetLength(1) + VDegree + 1) != VKnots.Length)
                {
                    System.Windows.Forms.MessageBox.Show("the definition of the VKnots is wrong.");
                }
                System.Windows.Forms.Application.Exit();
                return(new xyz(0, 0, 0));
            }
        }
Example #6
0
        /// <summary>
        /// Calculates the value of the <see cref="BSplineSurface"/>.
        /// </summary>
        /// <param name="u">Parameter u.</param>
        /// <param name="v">Parameter v.</param>
        /// <returns>The Evaluation of the bspline.</returns>
        public override xyz Value(double u, double v)
        {
            try
            {
                if (UKnots == null)
                {
                    UKnots = Utils.StandardKnots(ControlPoints.GetLength(0), UDegree);
                }
                if (VKnots == null)
                {
                    VKnots = Utils.StandardKnots(ControlPoints.GetLength(1), VDegree);
                }
                Rectangled R = DefinitionDomain;

                xyz Result = new xyz(0, 0, 0);

                int      UCount             = ControlPoints.GetLength(0);
                int      VCount             = ControlPoints.GetLength(1);
                double[] UCoeff             = Utils.CoeffSpline(UKnots, UCount, UDegree, UPeriodicity, v);
                double[] VCoeff             = Utils.CoeffSpline(VKnots, VCount, VDegree, VPeriodicity, u);
                int      FirstUCoeffNonZero = 0;
                for (int i = 0; i < UCoeff.Length; i++)
                {
                    if (UCoeff[i] != 0)
                    {
                        FirstUCoeffNonZero = i;
                        break;
                    }
                }

                int FirstVCoeffNonZero = 0;
                for (int i = 0; i < VCoeff.Length; i++)
                {
                    if (VCoeff[i] != 0)
                    {
                        FirstVCoeffNonZero = i;
                        break;
                    }
                }
                int UntilU = Math.Min(FirstUCoeffNonZero + UDegree, UCoeff.Length - 1);
                int UntilV = Math.Min(FirstVCoeffNonZero + VDegree, VCoeff.Length - 1);

                for (int i = 0; i < UCoeff.Length; i++)
                {
                    for (int k = 0; k < VCoeff.Length; k++)

                    {
                        Result = Result + ControlPoints[i, k] * (UCoeff[i] * VCoeff[k]);
                    }
                }
                if (ZHeight(u, v) != 0)
                {
                    return(Base.Absolut(Result + Normal(u, v) * ZHeight(u, v)));
                }
                else
                {
                    return(Base.Absolut(Result));
                }
            }
            catch (Exception)
            {
                if ((ControlPoints.GetLength(0) + UDegree + 1) != UKnots.Length)
                {
                    System.Windows.Forms.MessageBox.Show("the definition of the UKnots is wrong. . It must be UKnots.Length=ControlPoints.GetLength(0) + UDegree + 1");
                }
                else
                if ((ControlPoints.GetLength(1) + VDegree + 1) != VKnots.Length)
                {
                    System.Windows.Forms.MessageBox.Show("the definition of the VKnots is wrong. It must be VKnots.Length=ControlPoints.GetLength(1) + VDegree + 1");
                }

                return(new xyz(0, 0, 0));
            }
        }
Example #7
0
 /// <summary>
 /// sets the <see cref="Utils.DefaultKnots(int, int)"/>.
 /// </summary>
 public void SetDefaultKnots()
 {
     UKnots = Utils.DefaultKnots(ControlPoints.GetLength(0), UDegree);
     VKnots = Utils.DefaultKnots(ControlPoints.GetLength(1), VDegree);
 }
Example #8
0
        /// <summary>
        /// inserts a new <b>VKnot</b>.
        /// </summary>
        /// <param name="newKnot">value of the new knot.</param>
        public void InsertVKnot(double newKnot)
        {
            // Find KnotIndex
            int KnotIndex = -1;

            for (int i = 0; i < VKnots.Length - 1; i++)
            {
                if ((newKnot >= VKnots[i]) && (newKnot < VKnots[i + 1]))
                {
                    KnotIndex = i;
                    break;
                }
                if ((newKnot > VKnots[i]) && (newKnot <= VKnots[i + 1]))
                {
                    KnotIndex = i;
                    break;
                }
            }
            if ((KnotIndex < 0) || (KnotIndex - VDegree + 1 < 0) || (KnotIndex >= VKnots.Length))
            {
                throw new System.Exception("No Knot found");
            }

            xyz[,] q    = new xyz[ControlPoints.GetLength(0), VDegree];
            double[,] w = new double[ControlPoints.GetLength(0), VDegree];

            for (int index = 0; index < ControlPoints.GetLength(0); index++)
            {
                int j = 0;
                for (int i = KnotIndex - VDegree + 1; i <= KnotIndex; i++)
                {
                    double ai = (newKnot - VKnots[i]) / (VKnots[i + VDegree] - VKnots[i]);

                    q[index, j] = ControlPoints[index, i - 1] * (1 - ai) * Weights[index, i - 1] + ControlPoints[index, i] * ai * Weights[index, i];
                    w[index, j] = Weights[index, i - 1] * (1 - ai) + Weights[index, i] * ai;
                    if (w[index, j] != 0)
                    {
                        q[index, j] = q[index, j] * (1 / w[index, j]);
                    }
                    j++;
                }
            }

            // Einfügen
            // Verschieben der ControlPunkte:
            xyz[,] NewCtrlPoints = new xyz[ControlPoints.GetLength(0), ControlPoints.GetLength(1) + 1];
            double[,] NewWeights = new double[ControlPoints.GetLength(0), ControlPoints.GetLength(1) + 1];
            for (int index = 0; index < ControlPoints.GetLength(0); index++)
            {
                for (int i = 0; i < ControlPoints.GetLength(1); i++)
                {
                    if (i <= KnotIndex - VDegree)
                    {
                        NewCtrlPoints[index, i] = ControlPoints[index, i];
                        NewWeights[index, i]    = Weights[index, i];
                        // if (i == KnotIndex - VDegree) NewWeights[index, i+1] = 0; //// <---------- ausbessern
                    }
                    else
                    {
                        NewWeights[index, i + 1]    = Weights[index, i];
                        NewCtrlPoints[index, i + 1] = ControlPoints[index, i];
                    }
                }
            }

            // Einfügen neue
            for (int index = 0; index < ControlPoints.GetLength(0); index++)
            {
                for (int i = KnotIndex - VDegree + 1; i < KnotIndex + 1; i++)
                {
                    NewCtrlPoints[index, i] = q[index, i - KnotIndex + VDegree - 1];
                    NewWeights[index, i]    = w[index, i - KnotIndex + VDegree - 1];
                }
            }

            double[] NewKnots = new double[VKnots.Length + 1];
            for (int i = 0; i < VKnots.Length; i++)
            {
                if (i < KnotIndex + 1)
                {
                    NewKnots[i] = VKnots[i];
                }
                if (i == KnotIndex + 1)
                {
                    NewKnots[i]     = newKnot;
                    NewKnots[i + 1] = VKnots[i];
                }
                if (i > KnotIndex + 1)
                {
                    NewKnots[i + 1] = VKnots[i];
                }
            }
            ControlPoints = NewCtrlPoints;
            VKnots        = NewKnots;
            Weights       = NewWeights;
        }
Example #9
0
        //ControlPoints.length = Knots.Length - Order;
        //ControlPoint.Lenght = Knonot.Length- Order - 1
        /// <summary>
        /// Calculates the values for a nurbs, at the parameters u and v.
        /// </summary>
        /// <param name="u">Specifies the u parameter.</param>
        /// <param name="v">Specifies the v parameter.</param>
        /// <returns>Value of a nurbs at u,v.</returns>
        public override xyz Value(double u, double v)
        {
            try
            {
                xyz Result = new xyz(0, 0, 0);
                if ((UKnots == null) || (VKnots == null))
                {
                    return(Result);
                }
                double[] UCoeff = Utils.CoeffSpline(UKnots, ControlPoints.GetLength(0), UDegree, UPeriodicity, u);
                double[] VCoeff = Utils.CoeffSpline(VKnots, ControlPoints.GetLength(1), VDegree, VPeriodicity, v);
                int      UCount = ControlPoints.GetLength(0);
                int      VCount = ControlPoints.GetLength(1);

                {
                    double W1 = 0;

                    {
                        int VCoeffIndex = 0;
                        int uCoeffIndex = 0;

                        for (int i = 0; i < UCount; i++)
                        {
                            VCoeffIndex = 0;
                            for (int j = 0; j < VCount; j++)
                            {
                                W1 = W1 + Weights[i, j] * UCoeff[uCoeffIndex] * VCoeff[VCoeffIndex];// *VCoeffDerived[j].Value;
                                VCoeffIndex++;
                            }
                            uCoeffIndex++;
                        }
                    }

                    int uCoeffI = 0;
                    int VCoeffI = 0;

                    for (int i = 0; i < UCount; i++)
                    {
                        VCoeffI = 0;

                        for (int k = 0; k < VCount; k++)
                        {
                            Result = Result + ControlPoints[i, k] * (UCoeff[uCoeffI] * VCoeff[VCoeffI]
                                                                     * Weights[i, k] / W1);
                            VCoeffI++;
                        }
                        uCoeffI++;
                    }
                }
                return(Base.Absolut(Result));
            }
            catch (Exception)
            {
                if ((ControlPoints.GetLength(0) + UDegree + 1) != UKnots.Length)
                {
                    System.Windows.Forms.MessageBox.Show("the definition of the UKnots is wrong. It must be UKnots.Length=ControlPoints.GetLength(0) + UDegree + 1");
                }
                else
                if ((ControlPoints.GetLength(1) + VDegree + 1) != VKnots.Length)
                {
                    System.Windows.Forms.MessageBox.Show("the definition of the VKnots is wrong. It must be VKnots.Length=ControlPoints.GetLength(1) + VDegree + 1");
                }
                return(Base.BaseO);
            }
        }