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); }
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() }); }
/***************************************************/ private static IElement0D ClonePositionGuid(this IElement0D element0D, Point position) { element0D = element0D.DeepClone(); element0D = element0D.ISetGeometry(position); ((IBHoMObject)element0D).BHoM_Guid = Guid.NewGuid(); return(element0D); }
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 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; }
/***************************************************/ 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)); }
/***************************************************/ 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); }
/***************************************************/ /**** 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); }
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); }
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); }
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); }
/***************************************************/ /**** 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)); }
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); }
public static BoundingBox Bounds(this IElement0D element0D) { return(Geometry.Query.Bounds(element0D.IGeometry())); }
public static List <ICurve> ElementCurves(this IElement0D element0D, bool recursive = true) { return(new List <ICurve>()); }
public static bool IsPlanar(this IElement0D element0D, double tolerance = Tolerance.Distance) { return(true); }
/******************************************/ /**** Private methods ****/ /******************************************/ private static bool IsPlanar(this IElement0D element0D, bool externalOnly = false, double tolerance = Tolerance.Distance) { return(element0D.IsPlanar(tolerance)); }
/******************************************/ 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))); }
public static IElement0D Translate(this IElement0D element0D, Vector transform) { return(element0D.ISetGeometry(Geometry.Modify.Translate(element0D.IGeometry(), transform))); }
public static Point IGeometry(this IElement0D element0D) { return(Reflection.Compute.RunExtensionMethod(element0D, "Geometry") as Point); }
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. }
public static IElement0D RoundCoordinates(this IElement0D element0d, int decimalPlaces = 6) { return(element0d.ISetGeometry(Geometry.Modify.RoundCoordinates(element0d.IGeometry(), decimalPlaces))); }
public static Point Centroid(this IElement0D element0D, double tolerance = Tolerance.Distance) { return(element0D.IGeometry()); }
/***************************************************/ 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)); }
/******************************************/ /**** IElement0D ****/ /******************************************/ public static IElement0D ISetGeometry(this IElement0D element0D, Point point) { return(Reflection.Compute.RunExtensionMethod(element0D, "SetGeometry", new object[] { point }) as IElement0D); }
public static bool IsSelfIntersecting(this IElement0D element0D, double tolerance = Tolerance.Distance) { return(false); }