Ejemplo n.º 1
0
        public static bool IsAdjacentApprox(this Line curve1, Line curve2, double tolerance = Tolerance.Distance)
        {
            if (curve1 == null || curve2 == null)
            {
                BH.Engine.Reflection.Compute.RecordError("Cannot query the approximate adjacency of two lines if either line is null.");
                return(false);
            }

            bool isAdj = false;

            BH.oM.Reflection.Output <Point, Point> results = curve1.CurveProximity(curve2);
            double distance = results.Item1.Distance(results.Item2);

            if (distance < Tolerance.Distance)
            {
                List <Point> testPts = new List <Point> {
                    curve1.Start, curve1.End, curve2.Start, curve2.End
                };
                if (testPts.IsCollinear())
                {
                    Vector scaleVec = new Vector {
                        X = tolerance, Y = tolerance, Z = tolerance
                    };

                    // Check that adjacency is not only at touching endpoints
                    if (results.Item1.Distance(curve1.Start) < tolerance)
                    {
                        Point tempPt = results.Item1.Translate(curve1.Direction().Normalise().Scale(Point.Origin, scaleVec));
                        if (tempPt.Distance(curve2.Start) < results.Item1.Distance(curve2.Start) || tempPt.Distance(curve2.End) < results.Item1.Distance(curve2.End))
                        {
                            isAdj = true;
                        }
                    }
                    else if (results.Item1.Distance(curve1.End) < tolerance)
                    {
                        Point tempPt = results.Item1.Translate(curve1.Direction().Reverse().Normalise().Scale(Point.Origin, scaleVec));
                        if (tempPt.Distance(curve2.Start) < results.Item1.Distance(curve2.Start) || tempPt.Distance(curve2.End) < results.Item1.Distance(curve2.End))
                        {
                            isAdj = true;
                        }
                    }
                    else
                    {
                        isAdj = true;
                    }
                }
            }
            return(isAdj);
        }
Ejemplo n.º 2
0
        public static Output <List <string>, List <List <IElement1D> >, List <List <IElement2D> >, List <List <IElement1D> >, List <List <IElement2D> > > UniqueAdjacencies(IEnumerable <IElement2D> elems, bool splitHorAndVert = false)
        {
            if (elems == null)
            {
                BH.Engine.Reflection.Compute.RecordError("Cannot calculate unique adjacencies if the input elements are null.");
                return(new Output <List <string>, List <List <IElement1D> >, List <List <IElement2D> >, List <List <IElement1D> >, List <List <IElement2D> > >());
            }

            List <IElement2D> elemsList = elems.ToList();

            List <string>             adjacencyIDs = new List <string>();
            List <List <IElement1D> > adjEdges     = new List <List <IElement1D> >();
            List <List <IElement2D> > adjElems     = new List <List <IElement2D> >();
            List <List <IObject> >    elemsPerAdj  = new List <List <IObject> >();

            List <List <IElement1D> > edgePair1 = new List <List <IElement1D> >();
            List <List <IElement1D> > edgePair2 = new List <List <IElement1D> >();
            List <List <IElement2D> > elemPair1 = new List <List <IElement2D> >();
            List <List <IElement2D> > elemPair2 = new List <List <IElement2D> >();

            IEnumerable <Panel> panels = elems.OfType <Panel>();

            foreach (Panel panel in panels.ToList())
            {
                List <Opening> panelOpenings = panel.Openings;
                elemsList.AddRange(panelOpenings);
            }
            List <IElement2D> uniqueElems = elemsList.Distinct().ToList();

            foreach (IElement2D elem in uniqueElems)
            {
                List <IElement2D> tempElems = uniqueElems.Except(new List <IElement2D> {
                    elem
                }).ToList();
                foreach (IEdge edge in elem.IOutlineElements1D())
                {
                    BH.oM.Reflection.Output <List <IElement1D>, List <IElement2D> > result = edge.EdgeAdjacencies(tempElems);
                    for (int i = 0; i < result.Item1.Count; i++)
                    {
                        string            adjPrefix   = "";
                        List <IElement1D> edgeAdjPair = new List <IElement1D> {
                            edge, result.Item1[i]
                        };
                        List <IElement2D> elemAdjPair = new List <IElement2D> {
                            elem, result.Item2[i]
                        };
                        if (splitHorAndVert)
                        {
                            adjPrefix = Math.Abs(edge.Curve.IEndDir().Z) > 0.707 ? "Vertical-" : "Horizontal-"; // check if line is closer to vertical or horizontal
                        }
                        string         adjacencyID = adjPrefix + Query.AdjacencyID(edgeAdjPair, elemAdjPair);
                        List <IObject> adjObjList  = new List <IObject>()
                        {
                            edge, result.Item1[i], elem, result.Item2[i]
                        };
                        if (!adjacencyIDs.Contains(adjacencyID))
                        {
                            adjacencyIDs.Add(adjacencyID);
                            elemsPerAdj.Add(new List <IObject>());
                        }
                        int listNum = adjacencyIDs.IndexOf(adjacencyID);
                        elemsPerAdj[listNum].AddRange(adjObjList);
                    }
                }

                if (elem.InternalOutlineCurves().Count > 0)
                {
                    List <PolyCurve> internalEdgesOfPanel = elem.InternalOutlineCurves().BooleanUnion();
                    List <ICurve>    intCrvs = new List <ICurve>();
                    foreach (PolyCurve intPanelEdge in internalEdgesOfPanel)
                    {
                        List <ICurve> subCrvs = intPanelEdge.SubParts();
                        intCrvs.AddRange(subCrvs);
                    }

                    foreach (ICurve intPanelEdge in intCrvs)
                    {
                        BH.oM.Reflection.Output <List <IElement1D>, List <IElement2D> > result = intPanelEdge.EdgeAdjacencies(tempElems);
                        for (int i = 0; i < result.Item1.Count; i++)
                        {
                            string            adjPrefix   = "";
                            List <IElement1D> edgeAdjPair = new List <IElement1D> {
                                intPanelEdge, result.Item1[i]
                            };
                            List <IElement2D> elemAdjPair = new List <IElement2D> {
                                elem, result.Item2[i]
                            };
                            if (splitHorAndVert)
                            {
                                adjPrefix = Math.Abs(intPanelEdge.IEndDir().Z) > 0.707 ? "Vertical-" : "Horizontal-"; // check if line is closer to vertical or horizontal
                            }
                            string         adjacencyID = adjPrefix + Query.AdjacencyID(edgeAdjPair, elemAdjPair);
                            List <IObject> adjObjList  = new List <IObject>()
                            {
                                intPanelEdge, result.Item1[i], elem, result.Item2[i]
                            };
                            if (!adjacencyIDs.Contains(adjacencyID))
                            {
                                adjacencyIDs.Add(adjacencyID);
                                elemsPerAdj.Add(new List <IObject>());
                            }
                            int listNum = adjacencyIDs.IndexOf(adjacencyID);
                            elemsPerAdj[listNum].AddRange(adjObjList);
                        }
                    }
                }
            }
            foreach (List <IObject> adjObjs in elemsPerAdj)
            {
                List <IObject> edge1List = adjObjs.Where((x, i) => i % 4 == 0).ToList();
                List <IObject> edge2List = adjObjs.Where((x, i) => i % 4 == 1).ToList();
                List <IObject> elem1List = adjObjs.Where((x, i) => i % 4 == 2).ToList();
                List <IObject> elem2List = adjObjs.Where((x, i) => i % 4 == 3).ToList();
                edgePair1.Add(edge1List.Cast <IElement1D>().ToList());
                edgePair2.Add(edge2List.Cast <IElement1D>().ToList());
                elemPair1.Add(elem1List.Cast <IElement2D>().ToList());
                elemPair2.Add(elem2List.Cast <IElement2D>().ToList());
            }
            // Return the adjacency ids and elements as multi output
            return(new Output <List <string>, List <List <IElement1D> >, List <List <IElement2D> >, List <List <IElement1D> >, List <List <IElement2D> > >
            {
                Item1 = adjacencyIDs,
                Item2 = edgePair1,
                Item3 = elemPair1,
                Item4 = edgePair2,
                Item5 = elemPair2,
            });
        }