Exemple #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);
        }
Exemple #2
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);
        }
 public static List <Point> ElementVertices(this IElement0D element0D)
 {
     return(new List <Point>()
     {
         element0D.IGeometry()
     });
 }
Exemple #4
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));
        }
Exemple #5
0
        /***************************************************/

        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);
        }
Exemple #6
0
        /***************************************************/
        /****           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);
        }
        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);
        }
Exemple #8
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);
 }
Exemple #9
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));
        }
Exemple #10
0
 public static Point Centroid(this IElement0D element0D, double tolerance = Tolerance.Distance)
 {
     return(element0D.IGeometry());
 }
Exemple #11
0
 public static BoundingBox Bounds(this IElement0D element0D)
 {
     return(Geometry.Query.Bounds(element0D.IGeometry()));
 }
Exemple #12
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)));
        }
Exemple #13
0
        public static bool IsNearLevel(this IElement0D element0D, Level level, double maxDistance)
        {
            Point position = element0D.IGeometry();

            return(Math.Abs(level.Elevation - position.Z) <= maxDistance);
        }
Exemple #14
0
 public static IElement0D Translate(this IElement0D element0D, Vector transform)
 {
     return(element0D.ISetGeometry(Geometry.Modify.Translate(element0D.IGeometry(), transform)));
 }
Exemple #15
0
 public static List <Point> ControlPoints(this IElement0D element0D)
 {
     return(new List <Point> {
         element0D.IGeometry()
     });
 }
Exemple #16
0
 public static IElement0D RoundCoordinates(this IElement0D element0d, int decimalPlaces = 6)
 {
     return(element0d.ISetGeometry(Geometry.Modify.RoundCoordinates(element0d.IGeometry(), decimalPlaces)));
 }
Exemple #17
0
        public static bool IsFullyOnLevel(this IElement0D element0D, Level level, double tolerance = BH.oM.Geometry.Tolerance.Distance)
        {
            Point position = element0D.IGeometry();

            return(Math.Abs(level.Elevation - position.Z) <= tolerance);
        }
Exemple #18
0
        /***************************************************/

        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));
        }