|
| 1 | +/* |
| 2 | + * This file is part of the Buildings and Habitats object Model (BHoM) |
| 3 | + * Copyright (c) 2015 - 2024, the respective contributors. All rights reserved. |
| 4 | + * |
| 5 | + * Each contributor holds copyright over their respective contributions. |
| 6 | + * The project versioning (Git) records all such contribution source information. |
| 7 | + * |
| 8 | + * |
| 9 | + * The BHoM is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Lesser General Public License as published by |
| 11 | + * the Free Software Foundation, either version 3.0 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * The BHoM is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public License |
| 20 | + * along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. |
| 21 | + */ |
| 22 | + |
| 23 | +using BH.oM.Base.Attributes; |
| 24 | +using BH.oM.Geometry; |
| 25 | +using System.Collections.Generic; |
| 26 | +using System.ComponentModel; |
| 27 | +using System.Linq; |
| 28 | + |
| 29 | +namespace BH.Engine.Geometry |
| 30 | +{ |
| 31 | + public static partial class Compute |
| 32 | + { |
| 33 | + /***************************************************/ |
| 34 | + /**** Public Methods ****/ |
| 35 | + /***************************************************/ |
| 36 | + |
| 37 | + [Description("Splits a closed planar polyline at self intersections and returns a collection of closed polylines.")] |
| 38 | + [Input("polyline", "Closed planar polyline to split at self intersections.")] |
| 39 | + [Input("distanceTolerance", "Distance tolerance used in geometrical processing.")] |
| 40 | + [Output("splitOutlines", "Closed polylines representing the input polyline split at self intersections.")] |
| 41 | + public static List<Polyline> SplitAtSelfIntersections(this Polyline polyline, double distanceTolerance = Tolerance.Distance) |
| 42 | + { |
| 43 | + if (polyline.IsNull()) |
| 44 | + return null; |
| 45 | + |
| 46 | + if (!polyline.IsClosed(distanceTolerance)) |
| 47 | + { |
| 48 | + BH.Engine.Base.Compute.RecordError("Splitting at self intersections works only on closed polylines."); |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + if (!polyline.IsPlanar(distanceTolerance)) |
| 53 | + { |
| 54 | + BH.Engine.Base.Compute.RecordError("Splitting at self intersections works only on planar polylines."); |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + List<Line> segments = polyline.SubParts(); |
| 59 | + List<Point> selfIntersections = segments.LineIntersections(false, distanceTolerance).CullDuplicates(); |
| 60 | + segments = segments.SelectMany(x => x.SplitAtPoints(selfIntersections, distanceTolerance)).ToList(); |
| 61 | + return OutlinesFromLines(segments, distanceTolerance); |
| 62 | + } |
| 63 | + |
| 64 | + /***************************************************/ |
| 65 | + } |
| 66 | +} |
0 commit comments