Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment_Engine: Add IsValid check for spaces #2952

Merged
merged 6 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Environment_Engine/Query/IsClosed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static partial class Query

[Description("Determines whether the space is closed by ensuring all edges are connected to other elements")]
[Input("panels", "The collection of Environment Panels that represent the space to check")]
[Input("tolerance", "The tolerance for determining whether the panels are intersecting.")]
[Output("isClosed", "True if the space is closed, false otherwise")]
public static bool IsClosed(this List<Panel> panels, double tolerance = Tolerance.Distance)
{
Expand All @@ -53,7 +54,7 @@ public static bool IsClosed(this List<Panel> panels, double tolerance = Toleranc

foreach(Line l in unique)
{
if(edgeParts.Where(x => x.BooleanIntersection(l) != null).ToList().Count < 2)
if(edgeParts.Where(x => x.BooleanIntersection(l, tolerance) != null).ToList().Count < 2)
return false;
}

Expand Down
94 changes: 94 additions & 0 deletions Environment_Engine/Query/IsValid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2022, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using BH.oM.Environment.Elements;
using BH.oM.Base;
using BH.Engine.Geometry;
using BH.Engine.Spatial;
using BH.Engine.Base;
using BH.oM.Spatial.Layouts;
using BH.oM.Base.Attributes;
using BH.oM.Quantities.Attributes;
using System.ComponentModel;

namespace BH.Engine.Environment
{
public static partial class Query
{
[Description("Checks if the space is valid by checking if the perimeter curve is closed, if the perimeter length is not equal to 0 and if the space perimeter is not self intersecting.")]
[Input("spaces", "The Spaces to check if they are valid to the given conditions.")]
[Input("intersectionTolerance", "Minimum distance to be considered intersecting.", typeof(Length))]
[Input("lengthTolerance", "Minimum distance to considerthat a line has length.", typeof(Length))]
[Input("closedSpacesTolerance", "Distance tolerance for closedness validation.", typeof(Length))]
[MultiOutput(0, "validSpaces", "Returns list of valid spaces.")]
[MultiOutput(1, "selfIntersectingSpaces", "Returns list of invalid spaces due to the perimeter curve self intersecting.")]
[MultiOutput(2, "zeroPerimeterSpaces", "Returns list of invalid spaces due to perimeter length being zero.")]
[MultiOutput(3, "notClosedSpaces", "Returns list of invalid spaces due to not closed perimeter.")]
public static Output<List<Space>, List<Space>, List<Space>, List<Space>> IsValid(this List<Space> spaces, double intersectionTolerance = BH.oM.Geometry.Tolerance.Distance, double lengthTolerance = BH.oM.Geometry.Tolerance.Distance, double closedSpacesTolerance = BH.oM.Geometry.Tolerance.Distance)
kprusicka marked this conversation as resolved.
Show resolved Hide resolved
{
if(spaces == null)
{
BH.Engine.Base.Compute.RecordError("Spaces input cannot be null, please provide valid spaces.");
return null;
}

List<Space> validSpaces = new List<Space>();
kprusicka marked this conversation as resolved.
Show resolved Hide resolved
List<Space> selfIntersectingSpaces = new List<Space>();
List<Space> zeroPerimeterSpaces = new List<Space>();
List<Space> notClosedSpaces = new List<Space>();
foreach (Space space in spaces)
{
if(space == null)
continue;

bool isvalid = true;
kprusicka marked this conversation as resolved.
Show resolved Hide resolved
if (space.IsSelfIntersecting(intersectionTolerance))
{
selfIntersectingSpaces.Add(space);
isvalid = false;
}

double length = space.Perimeter.Length();
if ((length >= (0 - lengthTolerance)) && (length <= (0 + lengthTolerance)))
{
zeroPerimeterSpaces.Add(space);
isvalid = false;
}

if (!space.Perimeter.IIsClosed(closedSpacesTolerance))
{
notClosedSpaces.Add(space);
isvalid = false;
}

if (isvalid)
{
validSpaces.Add(space);
}
}

return new Output<List<Space>, List<Space>, List<Space>, List<Space>>{Item1 = validSpaces, Item2 = selfIntersectingSpaces, Item3 = notClosedSpaces, Item4 = zeroPerimeterSpaces};
}
}
}