Esempio n. 1
0
        protected override void SolveInstance(Grasshopper.Kernel.IGH_DataAccess DA)
        {
            init();
            _listSrf = new List <Surface>();
            _listCrv = new List <Curve>();
            listPnt  = new List <Point3d>();
            List <string> crvTypes   = new List <string>();
            List <string> pntHeights = new List <string>();

            if (!DA.GetDataList(0, _listSrf))
            {
                return;
            }
            if (!DA.GetDataList(1, _listCrv))
            {
                return;
            }
            if (!DA.GetDataList(2, crvTypes))
            {
                return;
            }
            if (!DA.GetDataList(3, listPnt))
            {
                listPnt.Clear();
            }
            if (!DA.GetData(4, ref globalC))
            {
                return;
            }

            if (_listCrv.Count != crvTypes.Count)
            {
                AddRuntimeMessage(Grasshopper.Kernel.GH_RuntimeMessageLevel.Error, "need types for curves"); return;
            }

            listSlice  = new Dictionary <string, slice>();
            listSlice2 = new Dictionary <string, slice2>();
            listLeaf   = new List <leaf>();
            listBranch = new List <branch>();
            listNode   = new List <node>();
            myControlBox.clearSliders();

            for (int i = 0; i < _listCrv.Count; i++)
            {
                var branch = new branch();
                branch.crv     = _listCrv[i] as NurbsCurve;
                branch.N       = branch.crv.Points.Count;
                branch.dom     = branch.crv.Domain;
                branch.Dim     = branch.crv.Order;
                branch.dDim    = branch.crv.Order - 1;
                branch.nElem   = branch.N - branch.dDim;
                branch.scaleT  = (branch.dom.T1 - branch.dom.T0) / branch.nElem;
                branch.originT = branch.dom.T0;
                if (crvTypes[i].StartsWith("reinforce"))
                {
                    branch.branchType = branch.type.reinforce;
                    var key = crvTypes[i].Replace("reinforce", "");
                    branch.sliceKey = key;
                    try{
                        branch.slice           = listSlice[key];
                        branch.slice.sliceType = slice.type.fr;
                        branch.slice.lB.Add(branch);
                    }
                    catch (KeyNotFoundException e) {
                        listSlice[key]         = new slice();
                        branch.slice           = listSlice[key];
                        branch.slice.sliceType = slice.type.fr;
                        branch.slice.lB.Add(branch);
                    }
                }
                else if (crvTypes[i].StartsWith("kink"))
                {
                    branch.branchType = branch.type.kink;
                    var    key = crvTypes[i].Replace("kink", "");
                    double lb  = 0.0d;
                    double _lb;
                    bool   res = double.TryParse(key, out _lb);
                    if (res)
                    {
                        lb = _lb;
                    }
                    else
                    {
                        lb = 0.0d;
                    }
                    branch.lb = lb;
                    //int NN;
                    //res = int.TryParse(key, out NN);
                    //if (res) { if (NN == 123) { branch.obj = true; } }
                }
                else if (crvTypes[i].StartsWith("open"))
                {
                    branch.branchType = branch.type.open;
                    var key = crvTypes[i].Replace("open", "");
                    branch.sliceKey = key;
                    try
                    {
                        branch.slice           = listSlice[key];
                        branch.slice.sliceType = slice.type.fr;
                        branch.slice.lB.Add(branch);
                    }
                    catch (KeyNotFoundException e)
                    {
                        listSlice[key]         = new slice();
                        branch.slice           = listSlice[key];
                        branch.slice.sliceType = slice.type.fr;
                        branch.slice.lB.Add(branch);
                    }
                }
                else if (crvTypes[i].StartsWith("fix"))
                {
                    branch.branchType = branch.type.fix;
                    var key = crvTypes[i].Replace("fix", "");
                    branch.sliceKey = key;
                    try
                    {
                        branch.slice2 = listSlice2[key];
                    }
                    catch (KeyNotFoundException e)
                    {
                        listSlice2[key] = new slice2();
                        branch.slice2   = listSlice2[key];

                        var slider = myControlBox.addSliderVert(0, 1, 200, 100);
                        slider.Converter = (val) =>
                        {
                            double height = val / 10d - 10d;
                            branch.slice2.height = height;
                            this.ExpirePreview(true);
                            return(height);
                        };
                    }
                }
                else
                {
                    AddRuntimeMessage(Grasshopper.Kernel.GH_RuntimeMessageLevel.Error, "type should be either of reinforce, kink, fix, or open");
                }
                listBranch.Add(branch);
            }

            // Connect nodes
            foreach (var node in listNode)
            {
                node.N = 0;
                node.share.Clear();
                node.number.Clear();
            }
            foreach (var branch in listBranch)
            {
                var  P    = branch.crv.Points[0].Location;
                bool flag = false;
                foreach (var node in listNode)
                {
                    if (node.compare(P))
                    {
                        flag = true;
                        node.N++;
                        node.share.Add(branch);
                        node.number.Add(0);
                        break;
                    }
                }
                if (!flag)
                {
                    var newNode = new node();
                    listNode.Add(newNode);
                    newNode.N++;
                    newNode.share.Add(branch);
                    newNode.number.Add(0);
                    newNode.x = P.X;
                    newNode.y = P.Y;
                    newNode.z = P.Z;
                }
                var Q = branch.crv.Points[branch.N - 1].Location;
                flag = false;
                foreach (var node in listNode)
                {
                    if (node.compare(Q))
                    {
                        flag = true;
                        node.N++;
                        node.share.Add(branch);
                        node.number.Add(branch.N - 1);
                        break;
                    }
                }
                if (!flag)
                {
                    var newNode = new node();
                    listNode.Add(newNode);
                    newNode.N++;
                    newNode.share.Add(branch);
                    newNode.number.Add(branch.N - 1);
                    newNode.x = Q.X;
                    newNode.y = Q.Y;
                    newNode.z = Q.Z;
                }
            }
            for (int i = 0; i < _listSrf.Count; i++)
            {
                var srf  = _listSrf[i];
                var leaf = new leaf();
                listLeaf.Add(leaf);
                leaf.srf     = srf as NurbsSurface;
                leaf.nU      = leaf.srf.Points.CountU;
                leaf.nV      = leaf.srf.Points.CountV;
                leaf.domU    = leaf.srf.Domain(0);
                leaf.domV    = leaf.srf.Domain(1);
                leaf.uDim    = leaf.srf.OrderU;
                leaf.vDim    = leaf.srf.OrderV;
                leaf.uDdim   = leaf.srf.OrderU - 1;
                leaf.vDdim   = leaf.srf.OrderV - 1;
                leaf.nUelem  = leaf.nU - leaf.uDdim;
                leaf.nVelem  = leaf.nV - leaf.vDdim;
                leaf.scaleU  = (leaf.domU.T1 - leaf.domU.T0) / leaf.nUelem;
                leaf.scaleV  = (leaf.domV.T1 - leaf.domV.T0) / leaf.nVelem;
                leaf.originU = leaf.domU.T0;
                leaf.originV = leaf.domV.T0;
                var domainU = leaf.srf.Domain(0);
                var domainV = leaf.srf.Domain(1);
                //Find corresponding curve
                //(0,0)->(1,0)
                var curve = leaf.srf.IsoCurve(0, domainV.T0) as NurbsCurve;
                leaf.flip[0] = findCurve(leaf, ref leaf.branch[0], listBranch, curve);//bottom
                //(1,0)->(1,1)
                curve        = leaf.srf.IsoCurve(1, domainU.T1) as NurbsCurve;
                leaf.flip[1] = findCurve(leaf, ref leaf.branch[1], listBranch, curve);//right
                //(1,1)->(0,1)
                curve        = leaf.srf.IsoCurve(0, domainV.T1) as NurbsCurve;
                leaf.flip[2] = findCurve(leaf, ref leaf.branch[2], listBranch, curve);//top
                //(0,1)->(0,0)
                curve        = leaf.srf.IsoCurve(1, domainU.T0) as NurbsCurve;
                leaf.flip[3] = findCurve(leaf, ref leaf.branch[3], listBranch, curve);//left
            }
            //multiqudric surface
            var A = new Rhino.Geometry.Matrix(listPnt.Count, listPnt.Count);
            var z = new Rhino.Geometry.Matrix(listPnt.Count, 1);

            for (int i = 0; i < listPnt.Count; i++)
            {
                for (int j = 0; j < listPnt.Count; j++)
                {
                    var pi = listPnt[i];
                    var pj = listPnt[j];
                    A[i, j] = quadFunc(pi.X, pj.X, pi.Y, pj.Y);
                    z[i, 0] = pi.Z;
                }
            }
            A.Invert(0.0);  //this parameter should be 0.0
            var c = A * z;

            globalCoeff = new double[listPnt.Count];
            for (int i = 0; i < listPnt.Count; i++)
            {
                globalCoeff[i] = c[i, 0];
            }
            targetSrf  = new List <Point3d>();
            globalFunc = (xi, yi) => {
                double Z = 0;
                for (int j = 0; j < listPnt.Count; j++)
                {
                    Z = Z + globalCoeff[j] * quadFunc(xi, listPnt[j].X, yi, listPnt[j].Y);
                }
                return(Z);
            };
            foreach (var leaf in listLeaf)
            {
                var domU = leaf.domU;
                var domV = leaf.domV;
                for (double i = 0; i <= 1.0; i += 0.05)
                {
                    for (double j = 0; j < 1.0; j += 0.05)
                    {
                        double u = domU[0] + i * (domU[1] - domU[0]);
                        double v = domV[0] + j * (domV[1] - domV[0]);
                        Rhino.Geometry.Point3d    P;
                        Rhino.Geometry.Vector3d[] V;
                        leaf.srf.Evaluate(u, v, 0, out P, out V);
                        var newP = new Rhino.Geometry.Point3d(P.X, P.Y, globalFunc(P.X, P.Y));
                        targetSrf.Add(newP);
                    }
                }
            }
        }
Esempio n. 2
0
    protected override void SolveInstance(Grasshopper.Kernel.IGH_DataAccess DA)
    {
        init();
            _listSrf = new List<Surface>();
            _listCrv = new List<Curve>();
            listPnt = new List<Point3d>();
            List<string> crvTypes = new List<string>();
            List<string> pntHeights = new List<string>();
            if (!DA.GetDataList(0, _listSrf)) { return; }
            if (!DA.GetDataList(1, _listCrv)) { return; }
            if (!DA.GetDataList(2, crvTypes)) { return; }
            if (!DA.GetDataList(3, listPnt)) { listPnt.Clear(); }
            if (!DA.GetData(4, ref globalC)) { return; }

            if (_listCrv.Count != crvTypes.Count) { AddRuntimeMessage(Grasshopper.Kernel.GH_RuntimeMessageLevel.Error, "need types for curves"); return; }

            listSlice = new Dictionary<string, slice>();
            listSlice2 = new Dictionary<string, slice2>();
            listRange = new Dictionary<string, range>();
            listRangeOpen = new Dictionary<string, range>();
            listRangeLeaf = new Dictionary<string, range>();
            listLeaf = new List<leaf>();
            listBranch = new List<branch>();
            listNode=new List<node>();
            myControlBox.clearSliders();
            for (int i = 0; i < _listCrv.Count; i++)
            {
                var branch = new branch();
                branch.crv = _listCrv[i] as NurbsCurve;
                branch.N = branch.crv.Points.Count;
                branch.dom = branch.crv.Domain;
                branch.Dim= branch.crv.Order;
                branch.dDim = branch.crv.Order - 1;
                branch.nElem = branch.N - branch.dDim;
                branch.scaleT = (branch.dom.T1 - branch.dom.T0) / branch.nElem;
                branch.originT = branch.dom.T0;
                if(crvTypes[i].StartsWith("reinforce"))
                {
                    branch.branchType = branch.type.reinforce;
                    var key=crvTypes[i].Replace("reinforce","");
                    branch.sliceKey=key;
                    try{
                        branch.slice=listSlice[key];
                        branch.slice.sliceType = slice.type.fr;
                        branch.slice.lB.Add(branch);
                    }
                    catch (KeyNotFoundException e){
                        listSlice[key]=new slice();
                        branch.slice=listSlice[key];
                        branch.slice.sliceType=slice.type.fr;
                        branch.slice.lB.Add(branch);
                    }
                }
                else if(crvTypes[i].StartsWith("kink"))
                {
                    branch.branchType = branch.type.kink;
                    var key = crvTypes[i].Replace("kink", "");
                    if (key == "") key = "kink"; else key = "kink:" + key;
                    branch.sliceKey = key;
                    try
                    {
                        branch.range = listRange[key];
                        branch.range.rangeType = range.type.lo;
                        branch.range.lb = 0;
                        branch.range.ub = 0;
                        branch.range.lB.Add(branch);

                    }
                    catch (KeyNotFoundException)
                    {
                        listRange[key] = new range();
                        branch.range = listRange[key];
                        branch.range.rangeType = range.type.lo;
                        branch.range.lb = 0;
                        branch.range.ub = 0;
                        branch.range.lB.Add(branch);
                        var adjuster = myControlBox.addRangeSetter(key, (th,sw, lb, ub) =>
                        {
                            if (listRange[key].firstPathDone)
                            {
                                th.setMeasured(listRange[key].lastMin,listRange[key].lastMax);
                            }
                            foreach (var _branch in listRange[key].lB)
                            {
                                if (sw == 0)
                                {
                                    _branch.range.rangeType = range.type.lo;
                                    _branch.range.lb = lb;
                                    _branch.range.ub = 0d;
                                }
                                if (sw == 2)
                                {
                                    _branch.range.rangeType = range.type.ra;
                                    _branch.range.lb = lb;
                                    _branch.range.ub = ub;
                                }
                            }
                        }
                         );
                    }

                }else if(crvTypes[i].StartsWith("open"))
                {
                    branch.branchType = branch.type.open;
                    var key = crvTypes[i].Replace("open", "");
                    if (key == "") key = "open"; else key = "open:" + key;
                    branch.sliceKey = key;
                    try
                    {
                        branch.slice = listSlice[key];
                        branch.slice.sliceType = slice.type.fr;
                        branch.slice.lB.Add(branch);
                    }
                    catch (KeyNotFoundException e)
                    {
                        listSlice[key] = new slice();
                        branch.slice = listSlice[key];
                        branch.slice.sliceType = slice.type.fr;
                        branch.slice.lB.Add(branch);
                    }
                    try
                    {
                        branch.range = listRangeOpen[key];
                        branch.range.rangeType = range.type.lo;
                        branch.range.lb = 0;
                        branch.range.ub = 0;
                        branch.range.lB.Add(branch);
                    }
                    catch (KeyNotFoundException)
                    {
                        listRangeOpen[key] = new range();
                        branch.range = listRangeOpen[key];
                        branch.range.rangeType = range.type.lo;
                        branch.range.lb = 0;
                        branch.range.ub = 0;
                        branch.range.lB.Add(branch);
                        var adjuster = myControlBox.addRangeSetter(key, (th,sw, lb, ub) =>
                        {
                            if (listRangeOpen[key].firstPathDone)
                            {
                                th.setMeasured(listRangeOpen[key].lastMin,listRangeOpen[key].lastMax);
                            }
                            foreach (var _branch in listRangeOpen[key].lB)
                            {
                                if (sw == 0)
                                {
                                    _branch.range.rangeType = range.type.lo;
                                    _branch.range.lb = lb;
                                    _branch.range.ub = 0d;
                                }
                                if (sw == 2)
                                {
                                    _branch.range.rangeType = range.type.ra;
                                    _branch.range.lb = lb;
                                    _branch.range.ub = ub;
                                }
                            }
                        }
                            );
                    }
                }
                else if (crvTypes[i].StartsWith("fix"))
                {
                    branch.branchType = branch.type.fix;
                    var key = crvTypes[i].Replace("fix", "");
                    branch.sliceKey = key;
                    try
                    {
                        branch.slice2 = listSlice2[key];
                    }
                    catch (KeyNotFoundException e)
                    {
                        listSlice2[key] = new slice2();
                        branch.slice2 = listSlice2[key];

                        var slider = myControlBox.addSliderVert(0, 1, 200, 100);
                        slider.Converter = (val) =>
                            {
                                double height = val / 10d - 10d;
                                branch.slice2.height = height;
                                this.ExpirePreview(true);
                                return height;
                            };
                    }
                }else{
                        AddRuntimeMessage(Grasshopper.Kernel.GH_RuntimeMessageLevel.Error, "type should be either of reinforce, kink, fix, or open");
                }
                listBranch.Add(branch);
            }

            for(int i=0;i<_listSrf.Count;i++)
            {
                var srf = _listSrf[i];
                var leaf=new leaf();
                listLeaf.Add(leaf);
                leaf.srf = srf as NurbsSurface;
                leaf.nU = leaf.srf.Points.CountU;
                leaf.nV = leaf.srf.Points.CountV;
                leaf.domU = leaf.srf.Domain(0);
                leaf.domV = leaf.srf.Domain(1);
                leaf.uDim = leaf.srf.OrderU;
                leaf.vDim = leaf.srf.OrderV;
                leaf.uDdim = leaf.srf.OrderU - 1;
                leaf.vDdim = leaf.srf.OrderV - 1;
                leaf.nUelem = leaf.nU - leaf.uDdim;
                leaf.nVelem = leaf.nV - leaf.vDdim;
                leaf.scaleU = (leaf.domU.T1 - leaf.domU.T0) / leaf.nUelem;
                leaf.scaleV = (leaf.domV.T1 - leaf.domV.T0) / leaf.nVelem;
                leaf.originU = leaf.domU.T0;
                leaf.originV = leaf.domV.T0;
                var domainU = leaf.srf.Domain(0);
                var domainV = leaf.srf.Domain(1);
                //Find corresponding curve
                //(0,0)->(1,0)
                var curve = leaf.srf.IsoCurve(0, domainV.T0) as NurbsCurve;
                leaf.flip[0] = findCurve(leaf,ref leaf.branch[0], listBranch, curve);//bottom
                //(1,0)->(1,1)
                curve = leaf.srf.IsoCurve(1, domainU.T1) as NurbsCurve;
                leaf.flip[1] = findCurve(leaf, ref leaf.branch[1], listBranch, curve);//right
                //(1,1)->(0,1)
                curve = leaf.srf.IsoCurve(0, domainV.T1) as NurbsCurve;
                leaf.flip[2] = findCurve(leaf, ref leaf.branch[2], listBranch, curve);//top
                //(0,1)->(0,0)
                curve = leaf.srf.IsoCurve(1, domainU.T0) as NurbsCurve;
                leaf.flip[3] = findCurve(leaf, ref leaf.branch[3], listBranch, curve);//left

                var key = "leaf";
                try
                {
                    leaf.range = listRangeLeaf[key];
                    leaf.range.rangeType = range.type.lo;
                    leaf.range.lb = 0;
                    leaf.range.ub = 0;
                    leaf.range.lL.Add(leaf);
                }
                catch (KeyNotFoundException)
                {
                    listRangeLeaf[key] = new range();
                    leaf.range = listRangeLeaf[key];
                    leaf.range.rangeType = range.type.lo;
                    leaf.range.lb = 0;
                    leaf.range.ub = 0;
                    leaf.range.lL.Add(leaf);
                    var adjuster = myControlBox.addRangeSetter(key, (th, sw, lb, ub) =>
                    {
                        if (listRangeLeaf[key].firstPathDone)
                        {
                            th.setMeasured(listRangeLeaf[key].lastMin, listRangeLeaf[key].lastMax);
                        }
                        foreach (var _leaf in listRangeLeaf[key].lL)
                        {
                            if (sw == 0)
                            {
                                _leaf.range.rangeType = range.type.lo;
                                _leaf.range.lb = lb;
                                _leaf.range.ub = 0d;
                            }
                            if (sw == 2)
                            {
                                _leaf.range.rangeType = range.type.ra;
                                _leaf.range.lb = lb;
                                _leaf.range.ub = ub;
                            }
                        }
                    }
                     );
                }

            }
            // Connect nodes
            foreach (var leaf in listLeaf)
            {
                leaf.globalIndex = new int[leaf.srf.Points.CountU * leaf.srf.Points.CountV];
                for (int j = 0; j < leaf.srf.Points.CountV; j++)
                {
                    for (int i = 0; i < leaf.srf.Points.CountU; i++)
                    {
                        var P = leaf.srf.Points.GetControlPoint(i, j).Location;
                        bool flag = false;
                        foreach (var node in listNode)
                        {
                            if (node.compare(P))
                            {
                                flag = true;
                                node.N++;
                                node.shareL.Add(leaf);
                                node.numberL.Add(i + j * leaf.nU);
                                leaf.globalIndex[i + j * leaf.nU] = listNode.IndexOf(node);
                                break;
                            }
                        }
                        if (!flag)
                        {
                            var newNode = new node();
                            listNode.Add(newNode);
                            newNode.N = 1;
                            newNode.x = P.X;
                            newNode.y = P.Y;
                            newNode.z = P.Z;
                            newNode.shareL.Add(leaf);
                            newNode.numberL.Add(i + j * leaf.nU);
                            leaf.globalIndex[i + j * leaf.nU] = listNode.IndexOf(newNode);
                        }
                    }
                }
            }
            foreach (var branch in listBranch)
            {
                branch.globalIndex = new int[branch.crv.Points.Count];
                for (int i = 0; i < branch.crv.Points.Count; i++)
                {
                    var P = branch.crv.Points[i].Location;
                    bool flag = false;
                    foreach (var node in listNode)
                    {
                        if (node.compare(P))
                        {
                            flag = true;
                            node.N++;
                            node.shareB.Add(branch);
                            node.numberB.Add(i);
                            if (branch.branchType == branch.type.fix)
                            {
                                node.nodeType = node.type.fx;
                            }
                            branch.globalIndex[i] = listNode.IndexOf(node);
                            break;
                        }
                    }
                    if (!flag)
                    {
                        var newNode = new node();
                        listNode.Add(newNode);
                        newNode.N = 1;
                        newNode.shareB.Add(branch);
                        newNode.numberB.Add(i);
                        newNode.x = P.X;
                        newNode.y = P.Y;
                        newNode.z = P.Z;
                        if (branch.branchType == branch.type.fix)
                        {
                            newNode.nodeType = node.type.fx;
                        }
                        branch.globalIndex[i] = listNode.IndexOf(newNode);
                    }
                }
            }
            //multiqudric surface
            var A=new Rhino.Geometry.Matrix(listPnt.Count,listPnt.Count);
            var z=new Rhino.Geometry.Matrix(listPnt.Count,1);
            for(int i=0;i<listPnt.Count;i++)
            {
                for(int j=0;j<listPnt.Count;j++)
                {
                    var pi=listPnt[i];
                    var pj=listPnt[j];
                    A[i,j]=quadFunc(pi.X,pj.X,pi.Y,pj.Y);
                    z[i,0]=pi.Z;
                }
            }
            A.Invert(0.0);  //this parameter should be 0.0
            var c=A*z;
            globalCoeff=new double[listPnt.Count];
            for(int i=0;i<listPnt.Count;i++)
            {
                globalCoeff[i]=c[i,0];
            }
            targetSrf=new List<Point3d>();
            globalFunc=(xi,yi)=>{
                double Z=0;
                for(int j=0;j<listPnt.Count;j++)
                {
                    Z=Z+globalCoeff[j]*quadFunc(xi,listPnt[j].X,yi,listPnt[j].Y);
                }
                return Z;
                };
            foreach (var leaf in listLeaf)
            {
                var domU = leaf.domU;
                var domV = leaf.domV;
                for (double i = 0; i <= 1.0; i += 0.05)
                {
                    for (double j = 0; j < 1.0; j += 0.05)
                    {
                        double u = domU[0] + i * (domU[1] - domU[0]);
                        double v = domV[0] + j * (domV[1] - domV[0]);
                        Rhino.Geometry.Point3d P;
                        Rhino.Geometry.Vector3d[] V;
                        leaf.srf.Evaluate(u, v, 0, out P, out V);
                        var newP = new Rhino.Geometry.Point3d(P.X, P.Y, globalFunc(P.X, P.Y));
                        targetSrf.Add(newP);
                    }
                }
            }
    }