Esempio n. 1
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            GH_Structure <IGH_Goo> treeIn = new GH_Structure <IGH_Goo>();

            DA.GetDataTree <IGH_Goo>(0, out treeIn);

            int pruneDepth = 0;

            DA.GetData <int>(1, ref pruneDepth);

            GH_Structure <IGH_Goo> treeOut = new GH_Structure <IGH_Goo>();

            ///Create list of path strings
            var pathStrings = treeIn.Paths.Select(x => x.ToString());
            ///Find the deepest path in the tree
            var maxDepthPath = pathStrings.Aggregate((max, cur) => max.Split(';').Length > cur.Split(';').Length ? max : cur);
            ///Get number of branches for deepest path
            var maxDepthInt = maxDepthPath.Split(';').Length;

            foreach (var path in treeIn.Paths)
            {
                ///Determine number of branches to prune if any
                GH_Path.SplitPathLikeString(path.ToString(), out string[] path_segments, out string index_segment);
                var numBranchesToRemove = path_segments.Length - (maxDepthInt - pruneDepth);

                var newPath = path;

                if (numBranchesToRemove > 0 && maxDepthInt - pruneDepth > 0)
                {
                    ///Remove pruned branches from path string
                    path_segments = path_segments.Take(path_segments.Count() - numBranchesToRemove).ToArray();
                    int[] path_args = path_segments.Select(int.Parse).ToArray();

                    newPath = new GH_Path(path_args);
                }

                treeOut.AppendRange(treeIn[path], newPath);
            }

            DA.SetDataTree(0, treeOut);
        }
Esempio n. 2
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            //bool inputLock = false;
            string key = "Leopard(" + this.InstanceGuid + ")";
            GH_Structure <IGH_GeometricGoo> inGeoTree;

            if (!DA.GetDataTree <IGH_GeometricGoo>(0, out inGeoTree))
            {
                return;
            }

            //clear all stored selection
            if (resetStoredPath)
            {
                storedPath.Clear();
            }

            //delete the preview baked objects
            if (!freezePreviewObjects)
            {
                RemovePreviewObjects();
            }

            //generate the preview baked objects
            if (!inputLock && (generatePreview || !freezePreviewObjects))
            {
                GeneratePreViewObjectsI(inGeoTree);                                                          // && !freezePreviewObjects)
            }
            //happens when unlock
            //if (addSelection)
            //    SelectStoredPathObj(storedPath);

            GH_Structure <GH_String> pathTree = new GH_Structure <GH_String>(); //a tree that the data stored is its path

            for (int i = 0; i < inGeoTree.PathCount; i++)
            {
                string path = inGeoTree.Paths[i].ToString();
                for (int j = 0; j < inGeoTree.Branches[i].Count; j++)
                {
                    string str = path + "(" + j.ToString() + ")";
                    pathTree.Append(new GH_String(str));
                }
            }
            List <string> pathOrder = new List <string>();

            foreach (GH_String s in pathTree.AllData(false))
            {
                pathOrder.Add(s.ToString());
            }

            GH_Structure <GH_Integer> orderTree = new GH_Structure <GH_Integer>(); //a tree that the data is the order of each data, this tree is reference for sorting

            for (int i = 0; i < pathOrder.Count; i++)
            {
                string[] pathSeg;
                string   indSeg;
                GH_Path.SplitPathLikeString(pathOrder[i], out pathSeg, out indSeg);
                int[] pInd  = System.Array.ConvertAll(pathSeg, str => System.Convert.ToInt32(str));
                int   index = System.Convert.ToInt32(indSeg);
                orderTree.Insert(new GH_Integer(i), new GH_Path(pInd), index);
            }

            GH_Structure <IGH_Goo>    outGeoTree   = new GH_Structure <IGH_Goo>();
            List <IGH_Goo>            outGeoList   = new List <IGH_Goo>();
            GH_Structure <IGH_Goo>    outPathTree  = new GH_Structure <IGH_Goo>();
            List <IGH_Goo>            outPathList  = new List <IGH_Goo>();
            GH_Structure <IGH_Goo>    outIndTree   = new GH_Structure <IGH_Goo>();
            List <IGH_Goo>            outIndList   = new List <IGH_Goo>();
            GH_Structure <GH_Integer> outOrderTree = new GH_Structure <GH_Integer>();
            List <GH_Integer>         outOrderList = new List <GH_Integer>();

            for (int i = 0; i < storedPath.Count; i++)
            {
                string   p = pathOrder[System.Convert.ToInt32(storedPath[i])];
                string[] pathSeg;
                string   indSeg;

                if (GH_Path.SplitPathLikeString(p, out pathSeg, out indSeg))
                {
                    int[]   pInd  = System.Array.ConvertAll(pathSeg, str => System.Convert.ToInt32(str));
                    GH_Path path  = new GH_Path(pInd);
                    int     index = System.Convert.ToInt32(indSeg);
                    if (maintainPath)
                    {
                        outGeoTree.Append((IGH_GeometricGoo)inGeoTree.get_Branch(path)[index], path);
                        outPathTree.Append(new GH_String(path.ToString()), path);
                        outIndTree.Append(new GH_Integer(index), path);
                        outOrderTree.Append((GH_Integer)(orderTree.get_Branch(path)[index]), path);
                    }
                    else
                    {
                        outGeoList.Add((IGH_GeometricGoo)inGeoTree.get_Branch(path)[index]);
                        outPathList.Add(new GH_String(path.ToString()));
                        outIndList.Add(new GH_Integer(index));
                        outOrderList.Add((GH_Integer)orderTree.get_Branch(path)[index]);
                    }
                }
            }
            if (maintainPath)
            {
                if (this.sortByIndex)
                {
                    outGeoTree = SortTreeByIndex(outGeoTree, outOrderTree);
                    outIndTree = SortTreeByIndex(outIndTree, outOrderTree);
                }
                DA.SetDataTree(0, outGeoTree);
                DA.SetDataTree(1, outPathTree);
                DA.SetDataTree(2, outIndTree);
                DA.SetDataTree(3, outOrderTree);
            }
            else
            {
                if (this.sortByIndex)
                {
                    List <IGH_Goo> gooCopy = outGeoList;
                    outGeoList.Sort(new SelectCompareGoo(gooCopy, outOrderList));
                    gooCopy = outPathList;
                    outPathList.Sort(new SelectCompareGoo(gooCopy, outOrderList));
                    gooCopy = outIndList;
                    outIndList.Sort(new SelectCompareGoo(gooCopy, outOrderList));
                }
                DA.SetDataList(0, outGeoList);
                DA.SetDataList(1, outPathList);
                DA.SetDataList(2, outIndList);
                DA.SetDataTree(3, outOrderTree);
            }
        }
Esempio n. 3
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            //bool inputLock = false;
            string key = "Leopard(" + this.InstanceGuid + ")";

            GH_Structure <IGH_GeometricGoo> inGeoTree;

            if (!DA.GetDataTree <IGH_GeometricGoo>(0, out inGeoTree))
            {
                return;
            }

            GH_Structure <IGH_GeometricGoo> ptsGoo = new GH_Structure <IGH_GeometricGoo>();

            for (int i = 0; i < inGeoTree.PathCount; i++)
            {
                for (int j = 0; j < inGeoTree.Branches[i].Count; j++)
                {
                    Mesh mesh;
                    if (!inGeoTree.Branches[i][j].CastTo <Mesh>(out mesh))
                    {
                        continue;
                    }
                    else
                    {
                        GH_Path    path    = inGeoTree.get_Path(i);
                        List <int> newPath = new List <int>();
                        for (int p = 0; p < path.Indices.Length; p++)
                        {
                            newPath.Add(path.Indices[p]);
                        }

                        PlanktonMesh          pMesh = mesh.ToPlanktonMesh();
                        IEnumerable <Point3d> pts   = pMesh.GetPositions();

                        foreach (Point3d p in pts)
                        {
                            if (inGeoTree.PathCount == 1)
                            {
                                ptsGoo.Append(GH_Convert.ToGeometricGoo(p), inGeoTree.get_Path(i));
                            }
                            else
                            {
                                ptsGoo.Append(GH_Convert.ToGeometricGoo(p), new GH_Path(newPath.ToArray()));
                            }
                        }
                    }
                }
            }

            //ptsGoo.Simplify(GH_SimplificationMode.CollapseLeadingOverlaps);

            //clear all stored selection
            if (resetStoredPath)
            {
                storedPath.Clear();
            }

            //delete the preview baked objects
            if (!freezePreviewObjects)
            {
                RemovePreviewObjects();
            }

            //generate the preview baked objects
            if (!inputLock && (generatePreview || !freezePreviewObjects))
            {
                GeneratePreViewObjectsI(ptsGoo);                                                          // && !freezePreviewObjects)
            }
            //happens when unlock
            //if (addSelection)
            //    SelectStoredPathObj(storedPath);


            GH_Structure <GH_String> pathTree = new GH_Structure <GH_String>(); //a tree that the data stored is its path

            for (int i = 0; i < ptsGoo.PathCount; i++)
            {
                string path = ptsGoo.Paths[i].ToString();
                for (int j = 0; j < ptsGoo.Branches[i].Count; j++)
                {
                    string str = path + "(" + j.ToString() + ")";
                    pathTree.Append(new GH_String(str));
                }
            }
            List <string> pathOrder = new List <string>();

            foreach (GH_String s in pathTree.AllData(false))
            {
                pathOrder.Add(s.ToString());
            }

            GH_Structure <GH_Integer> orderTree = new GH_Structure <GH_Integer>(); //a tree that the data is the order of each data, this tree is reference for sorting

            for (int i = 0; i < pathOrder.Count; i++)
            {
                string[] pathSeg;
                string   indSeg;
                GH_Path.SplitPathLikeString(pathOrder[i], out pathSeg, out indSeg);
                int[] pInd  = System.Array.ConvertAll(pathSeg, str => System.Convert.ToInt32(str));
                int   index = System.Convert.ToInt32(indSeg);
                orderTree.Insert(new GH_Integer(i), new GH_Path(pInd), index);
            }

            GH_Structure <IGH_Goo>    outGeoTree   = new GH_Structure <IGH_Goo>();
            GH_Structure <IGH_Goo>    outIndTree   = new GH_Structure <IGH_Goo>();
            GH_Structure <GH_Integer> outOrderTree = new GH_Structure <GH_Integer>();

            for (int i = 0; i < storedPath.Count; i++)
            {
                string   p = pathOrder[System.Convert.ToInt32(storedPath[i])];
                string[] pathSeg;
                string   indSeg;

                if (GH_Path.SplitPathLikeString(p, out pathSeg, out indSeg))
                {
                    int[]   pInd  = System.Array.ConvertAll(pathSeg, str => System.Convert.ToInt32(str));
                    GH_Path path  = new GH_Path(pInd);
                    int     index = System.Convert.ToInt32(indSeg);

                    outGeoTree.Append((IGH_GeometricGoo)ptsGoo.get_Branch(path)[index], path);
                    outIndTree.Append(new GH_Integer(index), path);
                    outOrderTree.Append((GH_Integer)(orderTree.get_Branch(path)[index]), path);
                }
            }

            if (this.sortByIndex)
            {
                outGeoTree = SortTreeByIndex(outGeoTree, outOrderTree);
                outIndTree = SortTreeByIndex(outIndTree, outOrderTree);
            }
            DA.SetDataTree(0, outGeoTree);
            DA.SetDataTree(1, outIndTree);
        }