示例#1
0
        public static double RelationLength(this Graph graph, IRelation relation)
        {
            if (graph == null)
            {
                BH.Engine.Reflection.Compute.RecordError("Cannot query the relation length of a null graph.");
                return(0);
            }

            if (relation == null)
            {
                BH.Engine.Reflection.Compute.RecordError("Cannot query the relation length for a graph if the relation to query is null.");
                return(0);
            }

            double length = 0;

            if (relation.Curve != null)
            {
                length = relation.Curve.ILength();
            }
            else
            {
                IElement0D source = graph.Entities[relation.Source] as IElement0D;
                IElement0D target = graph.Entities[relation.Target] as IElement0D;
                length = source.IGeometry().Distance(target.IGeometry());
            }
            return(length);
        }
示例#2
0
文件: Graph.cs 项目: BHoM/BHoM_Engine
        public static Graph Graph <T>(List <ICurve> connectingCurves, T prototypeEntity, List <IElement0D> entities = null, double snappingTolerance = Tolerance.Distance, RelationDirection relationDirection = RelationDirection.Forwards)
            where T : IElement0D
        {
            if (entities == null)
            {
                entities = new List <IElement0D>();
            }

            List <IElement0D> entitiesCloned = entities.DeepClone();

            List <IRelation> relations = new List <IRelation>();

            foreach (ICurve curve in connectingCurves)
            {
                IElement0D start = FindOrCreateEntity(entitiesCloned, curve.IStartPoint(), snappingTolerance, prototypeEntity);
                IElement0D end   = FindOrCreateEntity(entitiesCloned, curve.IEndPoint(), snappingTolerance, prototypeEntity);

                Relation relation = new Relation()
                {
                    Source = ((IBHoMObject)start).BHoM_Guid,
                    Target = ((IBHoMObject)end).BHoM_Guid,
                    Curve  = curve
                };
                relations.AddRange(RelationsToAdd(relation, relationDirection));
            }
            Graph graph = new Graph();

            entitiesCloned.ForEach(n => graph.Entities.Add(((IBHoMObject)n).BHoM_Guid, ((IBHoMObject)n)));
            graph.Relations = relations;
            graph.UniqueEntityNames();

            return(graph);
        }
 public static List <Point> ElementVertices(this IElement0D element0D)
 {
     return(new List <Point>()
     {
         element0D.IGeometry()
     });
 }
示例#4
0
文件: Graph.cs 项目: BHoM/BHoM_Engine
        /***************************************************/

        private static IElement0D ClonePositionGuid(this IElement0D element0D, Point position)
        {
            element0D = element0D.DeepClone();
            element0D = element0D.ISetGeometry(position);
            ((IBHoMObject)element0D).BHoM_Guid = Guid.NewGuid();
            return(element0D);
        }
示例#5
0
        public static bool IsNearGrid(this IElement0D element0D, Grid grid, double maxDistance)
        {
            Point  position  = element0D.IGeometry().Project(Plane.XY);
            ICurve gridCurve = grid.Curve.IProject(Plane.XY);

            return(position.IDistance(gridCurve) <= maxDistance);
        }
示例#6
0
        public static IElement0D ITransform(this IElement0D element0D, TransformMatrix transform, double tolerance = Tolerance.Distance)
        {
            object result;
            if (!Reflection.Compute.TryRunExtensionMethod(element0D, "Transform", new object[] { transform, tolerance }, out result))
                result = element0D.Transform(transform, tolerance);

            return result as IElement0D;
        }
示例#7
0
        /***************************************************/

        private static IElement0D Transform(this IElement0D element0D, TransformMatrix transform, double tolerance)
        {
            if (!transform.IsRigidTransformation(tolerance))
            {
                BH.Engine.Reflection.Compute.RecordError("Transformation failed: only rigid body transformations are currently supported.");
                return null;
            }

            return element0D.ISetGeometry(Geometry.Modify.Transform(element0D.IGeometry(), transform));
        }
示例#8
0
文件: Graph.cs 项目: BHoM/BHoM_Engine
        /***************************************************/

        private static bool ToCloseToAny(List <IElement0D> entities, IElement0D entity, double tolerance)
        {
            foreach (IElement0D n in entities)
            {
                double d = n.IGeometry().Distance(entity.IGeometry());
                if (d < tolerance)
                {
                    return(true);
                }
            }
            return(false);
        }
示例#9
0
文件: Graph.cs 项目: BHoM/BHoM_Engine
        /***************************************************/
        /****           Private Methods                 ****/
        /***************************************************/
        private static IElement0D FindOrCreateEntity(List <IElement0D> entities, Point point, double tolerance, IElement0D prototypeEntity)
        {
            IElement0D entity = entities.ClosestIElement0D(point);

            if (entity == null || entity.IGeometry().Distance(point) > tolerance)
            {
                entity = prototypeEntity.ClonePositionGuid(point);
                entities.Add(entity);
            }

            return(entity);
        }
示例#10
0
        public static ShortestPathResult AStarShortestPath(this Graph graph, Guid start, Guid end)
        {
            if (graph == null)
            {
                BH.Engine.Reflection.Compute.RecordError("Cannot query the AStar shortest path from a null graph.");
                return(null);
            }

            m_GeometricGraph = graph.IProjectGraph(new GeometricProjection());

            if (m_GeometricGraph.Entities.Count == 0 || m_GeometricGraph.Relations.Count == 0)
            {
                Reflection.Compute.RecordWarning("The graph provided does not contain sufficient spatial entities or relations.\n" +
                                                 "To use a star shortest path provide a graph where some entities implement IElement0D and spatial relations are defined between them.\n" +
                                                 "Shortest path is computed using Dijkstra shortest path instead.");

                return(DijkstraShortestPath(graph, start, end));
            }

            SetFragments(m_GeometricGraph);

            //calculate straight line distance from each entity to the end
            IElement0D endEntity = m_GeometricGraph.Entities[end] as IElement0D;

            foreach (Guid entity in m_GeometricGraph.Entities.Keys.ToList())
            {
                IElement0D element0D = m_GeometricGraph.Entities[entity] as IElement0D;
                m_Fragments[entity].StraightLineDistanceToTarget = element0D.IGeometry().Distance(endEntity.IGeometry());
            }

            AStarSearch(m_GeometricGraph, start, ref end);

            List <Guid> shortestPath = new List <Guid>();

            shortestPath.Add(end);

            double           length    = 0;
            double           cost      = 0;
            List <ICurve>    curves    = new List <ICurve>();
            List <IRelation> relations = new List <IRelation>();

            AStarResult(shortestPath, end, ref length, ref cost, ref curves, ref relations);
            shortestPath.Reverse();

            List <IBHoMObject> objPath = new List <IBHoMObject>();

            shortestPath.ForEach(g => objPath.Add(m_GeometricGraph.Entities[g]));

            List <IBHoMObject> entitiesVisited = m_Fragments.Where(kvp => kvp.Value.Visited).Select(kvp => m_GeometricGraph.Entities[kvp.Key]).ToList();
            ShortestPathResult result          = new ShortestPathResult(graph.BHoM_Guid, "AStarShortestPath", -1, objPath, length, cost, entitiesVisited, relations, curves);

            return(result);
        }
示例#11
0
文件: Graph.cs 项目: BHoM/BHoM_Engine
        public static Graph Graph <T>(int width, int length, int height, double cellSize, T prototypeEntity, RelationDirection relationDirection = RelationDirection.Forwards)
            where T : IElement0D
        {
            Graph graph = new Graph();
            List <List <List <IBHoMObject> > > entityGrid = new List <List <List <IBHoMObject> > >();

            for (int k = 0; k < height; k++)
            {
                List <List <IBHoMObject> > level = new List <List <IBHoMObject> >();
                for (int i = 0; i < width; i++)
                {
                    List <IBHoMObject> col = new List <IBHoMObject>();
                    for (int j = 0; j < length; j++)
                    {
                        Point p = Geometry.Create.Point(i * cellSize, j * cellSize, k * cellSize);

                        IElement0D entity = prototypeEntity.DeepClone();
                        entity = entity.ISetGeometry(p);
                        ((IBHoMObject)entity).BHoM_Guid = Guid.NewGuid();

                        graph.Entities.Add(((IBHoMObject)entity).BHoM_Guid, ((IBHoMObject)entity));

                        col.Add((IBHoMObject)entity);
                    }
                    level.Add(col);
                }
                entityGrid.Add(level);
            }
            for (int k = 0; k < height; k++)
            {
                for (int i = 0; i < width; i++)
                {
                    for (int j = 0; j < length; j++)
                    {
                        List <IBHoMObject> connections = RandomNeighbours(entityGrid, i, j, k);
                        foreach (IBHoMObject c in connections)
                        {
                            Relation relation = new Relation()
                            {
                                Source = entityGrid[k][i][j].BHoM_Guid,
                                Target = c.BHoM_Guid
                            };
                            graph.Relations.AddRange(RelationsToAdd(relation, relationDirection));
                        }
                    }
                }
            }

            graph.UniqueEntityNames();
            return(graph);
        }
示例#12
0
        public static IElement0D ClosestIElement0D <T>(this List <T> entities, Point point)
            where T : IElement0D
        {
            if (entities.Count == 0)
            {
                return(null);
            }

            IElement0D closest = entities.Select(p => new { Node = p, Distance2 = p.IGeometry().Distance(point) })
                                 .Aggregate((p1, p2) => p1.Distance2 < p2.Distance2 ? p1 : p2)
                                 .Node;

            return(closest);
        }
示例#13
0
 private static Graph RelationCurves(this Graph graph, SpatialProjection projection)
 {
     //these should set representationfragment on relations
     foreach (IRelation relation in graph.Relations)
     {
         if (relation.Curve == null)
         {
             IElement0D source = graph.Entities[relation.Source] as IElement0D;
             IElement0D target = graph.Entities[relation.Target] as IElement0D;
             relation.Curve = new Line()
             {
                 Start = source.IGeometry(), End = target.IGeometry()
             };
         }
     }
     return(graph);
 }
示例#14
0
        /***************************************************/
        /**** Private Methods                           ****/
        /***************************************************/

        private static CompositeGeometry SpatialGraphGeometry(Graph spatialGraph)
        {
            List <IGeometry> geometries = new List <IGeometry>();

            foreach (KeyValuePair <System.Guid, IBHoMObject> kvp in spatialGraph?.Entities)
            {
                if (kvp.Value is IElement0D)
                {
                    IElement0D entity = kvp.Value as IElement0D;
                    geometries.Add(entity.IGeometry());
                }
            }
            foreach (IRelation relation in spatialGraph?.Relations)
            {
                geometries.Add(relation?.RelationArrow());
            }

            return(BH.Engine.Geometry.Create.CompositeGeometry(geometries));
        }
示例#15
0
文件: Graph.cs 项目: BHoM/BHoM_Engine
        public static Graph Graph(int entityCount, int branching, BoundingBox boundingBox, IElement0D prototypeEntity, double tolerance = Tolerance.Distance, RelationDirection relationDirection = RelationDirection.Forwards)
        {
            Graph             graph    = new Graph();
            List <IElement0D> entities = new List <IElement0D>();

            for (int i = 0; i < entityCount; i++)
            {
                Point      p      = Geometry.Create.RandomPoint(m_Rnd, boundingBox);
                IElement0D entity = prototypeEntity.ClonePositionGuid(p);

                if (!ToCloseToAny(entities, entity, tolerance))
                {
                    entities.Add(entity);
                }
            }

            List <IRelation> relations = new List <IRelation>();

            foreach (IElement0D entity in entities)
            {
                foreach (IElement0D d in ClosestIElement0Ds(entities, entity, branching))
                {
                    Relation relation = new Relation()
                    {
                        Source = ((IBHoMObject)entity).BHoM_Guid,
                        Target = ((IBHoMObject)d).BHoM_Guid
                    };
                    relations.AddRange(RelationsToAdd(relation, relationDirection));
                }
            }

            graph.UniqueEntityNames();
            entities.ForEach(n => graph.Entities.Add(((IBHoMObject)n).BHoM_Guid, ((IBHoMObject)n)));
            graph.Relations = relations;

            return(graph);
        }
示例#16
0
 public static BoundingBox Bounds(this IElement0D element0D)
 {
     return(Geometry.Query.Bounds(element0D.IGeometry()));
 }
示例#17
0
 public static List <ICurve> ElementCurves(this IElement0D element0D, bool recursive = true)
 {
     return(new List <ICurve>());
 }
示例#18
0
 public static bool IsPlanar(this IElement0D element0D, double tolerance = Tolerance.Distance)
 {
     return(true);
 }
示例#19
0
        /******************************************/
        /****         Private methods          ****/
        /******************************************/

        private static bool IsPlanar(this IElement0D element0D, bool externalOnly = false, double tolerance = Tolerance.Distance)
        {
            return(element0D.IsPlanar(tolerance));
        }
示例#20
0
        /******************************************/

        public static IElement0D Translate(this IElement0D element0D, Vector transform) //todo: move this to analytical along with other IElement methods
        {
            return(element0D.ISetGeometry(Geometry.Modify.Translate(element0D.IGeometry(), transform)));
        }
示例#21
0
 public static IElement0D Translate(this IElement0D element0D, Vector transform)
 {
     return(element0D.ISetGeometry(Geometry.Modify.Translate(element0D.IGeometry(), transform)));
 }
示例#22
0
 public static Point IGeometry(this IElement0D element0D)
 {
     return(Reflection.Compute.RunExtensionMethod(element0D, "Geometry") as Point);
 }
示例#23
0
 public static List <Point> ControlPoints(this IElement0D element0D)
 {
     return(new List <Point> {
         element0D.IGeometry()
     });
 }
 public static bool IHasMergeablePropertiesWith(this IElement0D element, IElement0D other)
 {
     return(HasMergeablePropertiesWithIElement(element, other));
 }
        /***************************************************/

        public static BoundingBox ElementBoundingBox(IElement0D pt, double localXDimension, double localYDimension, double localZDimension)
        {
            throw new NotImplementedException(); //TODO.
        }
示例#26
0
 public static IElement0D RoundCoordinates(this IElement0D element0d, int decimalPlaces = 6)
 {
     return(element0d.ISetGeometry(Geometry.Modify.RoundCoordinates(element0d.IGeometry(), decimalPlaces)));
 }
示例#27
0
 public static Point Centroid(this IElement0D element0D, double tolerance = Tolerance.Distance)
 {
     return(element0D.IGeometry());
 }
示例#28
0
文件: Graph.cs 项目: BHoM/BHoM_Engine
        /***************************************************/

        private static List <IElement0D> ClosestIElement0Ds(List <IElement0D> entities, IElement0D element0D, int branching)
        {
            List <IElement0D> ordered = entities.OrderBy(n => n.IGeometry().Distance(element0D.IGeometry())).ToList();

            return(ordered.GetRange(1, branching));
        }
示例#29
0
        /******************************************/
        /****            IElement0D            ****/
        /******************************************/

        public static IElement0D ISetGeometry(this IElement0D element0D, Point point)
        {
            return(Reflection.Compute.RunExtensionMethod(element0D, "SetGeometry", new object[] { point }) as IElement0D);
        }
示例#30
0
 public static bool IsSelfIntersecting(this IElement0D element0D, double tolerance = Tolerance.Distance)
 {
     return(false);
 }