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

Implement Spatial Conditions #12

Merged
merged 29 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
abc1b04
first implementation
alelom Feb 8, 2021
25374e8
changes to structure of conditions
alelom Feb 8, 2021
a596052
Added methods to convert spatialCondition to boundingBoxCondition. Re…
alelom Feb 9, 2021
cc43888
refactoring. added methods for applying condition.
alelom Feb 9, 2021
9be96cf
Added ApplyBoundingBoxCondition. Refactor.
alelom Feb 9, 2021
f95f7f7
Update CIH_Engine.csproj
alelom Feb 9, 2021
e7c7e02
Another iteration
alelom Feb 15, 2021
c639c22
Refactored: added ZoneSpecs/SpatialSpecs.
alelom Feb 15, 2021
d716b6d
Added filter of objects from SpatialSpecification.
alelom Feb 15, 2021
33f5570
Corrections to filter
alelom Feb 16, 2021
621883b
Refactored for 1 zoneSpec per 1 SpatialSpec. Corrections.
alelom Feb 16, 2021
1fd2bb6
Update ApplyLogicalCondition.cs
alelom Feb 16, 2021
6861b87
Correction to IElementBoundingBox.
alelom Feb 17, 2021
50e7415
Added description to BoundingBoxCondition property
alelom Feb 17, 2021
c474940
Added toString() to Zone/Spatial specifications
alelom Feb 17, 2021
1560720
Correction to logicalCondition
alelom Feb 17, 2021
ac7505b
Corrections.
alelom Feb 17, 2021
8a297e9
Corrections to the ToString methods
alelom Feb 17, 2021
b5c9cb8
Added workaround to missing https://github.com/BHoM/BHoM_Engine/issue…
alelom Feb 18, 2021
b16da72
added description
alelom Feb 18, 2021
5f4c43f
Added copyright headers
alelom Feb 18, 2021
66af561
Create ZoneReferenceLine.cs
alelom Feb 18, 2021
7f7ba79
Revert "Added copyright headers"
alelom Feb 18, 2021
5855a44
Update CIH_oM.csproj
alelom Feb 18, 2021
d7cd3cc
ValueCondition accepting more inputs
alelom Feb 19, 2021
d85d746
Update ApplySpatialSpecification.cs
alelom Feb 19, 2021
53d0516
Update ValueFromSource.cs
alelom Feb 19, 2021
160a690
Update IGeometry.cs
alelom Feb 19, 2021
cea8c94
Correction on IElementBoundingBox for vertical elements
alelom Feb 19, 2021
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
32 changes: 30 additions & 2 deletions CIH_Engine/CIH_Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,26 @@
<Reference Include="BHoM_Engine">
<HintPath>C:\ProgramData\BHoM\Assemblies\BHoM_Engine.dll</HintPath>
</Reference>
<Reference Include="Data_Engine, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\ProgramData\BHoM\Assemblies\Data_Engine.dll</HintPath>
</Reference>
<Reference Include="Data_oM, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" />
<Reference Include="Dimensional_oM, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" />
<Reference Include="Geometry_Engine">
<HintPath>C:\ProgramData\BHoM\Assemblies\Geometry_Engine.dll</HintPath>
</Reference>
<Reference Include="Geometry_oM, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\ProgramData\BHoM\Assemblies\Geometry_oM.dll</HintPath>
</Reference>
<Reference Include="Reflection_Engine">
<HintPath>C:\ProgramData\BHoM\Assemblies\Reflection_Engine.dll</HintPath>
</Reference>
<Reference Include="Reflection_oM, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\ProgramData\BHoM\Assemblies\Reflection_oM.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -51,20 +67,31 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Compute\ApplyCondition\ApplyBoundingBoxCondition.cs" />
<Compile Include="Compute\ApplyCondition\ApplyFragmentCondition.cs" />
<Compile Include="Compute\ApplyCondition\ApplySetCondition.cs" />
<Compile Include="Compute\ApplyCondition\ApplySpatialCondition.cs" />
<Compile Include="Compute\ApplyCondition\ApplyValueNullCondition.cs" />
<Compile Include="Compute\ApplySpecification\ApplyZoneSpecification.cs" />
<Compile Include="Compute\ApplySpecification\ApplySpatialSpecification.cs" />
<Compile Include="Compute\ApplySpecification\ApplySpecifications.cs" />
<Compile Include="Compute\Utilities\PopulateSpecificationResult.cs" />
<Compile Include="Compute\Utilities\IsAnyConditionNull.cs" />
<Compile Include="Compute\ApplyCondition\ApplyValueCondition.cs" />
<Compile Include="Compute\ApplyCondition\ApplyDomainCondition.cs" />
<Compile Include="Compute\ApplyCondition\ApplyLogicalCondition.cs" />
<Compile Include="Compute\ApplyCondition\ApplyIdCondition.cs" />
<Compile Include="Compute\ApplyCondition\ApplyTypeCondition.cs" />
<Compile Include="Create\SpatialSpecification.cs" />
<Compile Include="Query\IGeometry.cs" />
<Compile Include="Query\Geometry3D.cs" />
<Compile Include="Query\SpatialBoundingBoxes.cs" />
<Compile Include="Query\IElementBoundingBox.cs" />
<Compile Include="Query\ValueFromSource.cs" />
<Compile Include="Query\ToString.cs" />
<Compile Include="Modify\SetCustomData.cs" />
<Compile Include="Compute\ApplyConditions.cs" />
<Compile Include="Compute\ApplySpecification.cs" />
<Compile Include="Compute\ApplyCondition\IApplyConditions.cs" />
<Compile Include="Compute\ApplySpecification\IApplySpecification.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -73,6 +100,7 @@
<Name>CIH_oM</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
Expand Down
57 changes: 57 additions & 0 deletions CIH_Engine/Compute/ApplyCondition/ApplyBoundingBoxCondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using BH.oM.Data.Conditions;
using BH.Engine.Base;
using BH.oM.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BH.oM.Data;
using BH.oM.Data.Collections;
using BH.oM.Geometry;
using BH.Engine.Geometry;

namespace BH.Engine.CIH
{
public static partial class Compute
{
private static ConditionResult ApplyCondition(List<object> objects, BoundingBoxCondition bbc)
{
ConditionResult result = new ConditionResult() { Condition = bbc };
List<string> failInfo = new List<string>();

foreach (var obj in objects)
{
IGeometry geom = null;
BHoMObject bhomObj = obj as BHoMObject;
if (bhomObj != null)
{
if (bbc.Containment3D)
geom = bhomObj.IGeometry3D();
else
geom = bhomObj.IGeometry();
}

if (obj is IGeometry)
geom = obj as IGeometry;

BoundingBox geomBB = Geometry.Query.IBounds(geom);

bool passed = bbc.BoundingBox.IsContaining(geomBB, true);

if (passed)
result.PassedObjects.Add(obj);
else
{
result.FailedObjects.Add(obj);
failInfo.Add($"Object not in the specified Bounding Box.");
}

result.Pattern.Add(passed);
}

result.FailInfo = failInfo;
return result;
}
}
}
2 changes: 1 addition & 1 deletion CIH_Engine/Compute/ApplyCondition/ApplyDomainCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private static ConditionResult ApplyCondition(List<object> objects, DomainCondit
else
{
result.FailedObjects.Add(obj);
failInfo.Add($"{domainCondition.PropertyName} was {value} which does not respect: {domainCondition.ToString()}");
failInfo.Add($"\"{domainCondition.ToString()}\"\nnot respected: value is {value}.");
}

result.Pattern.Add(passed);
Expand Down
10 changes: 9 additions & 1 deletion CIH_Engine/Compute/ApplyCondition/ApplyLogicalCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ public static partial class Compute
{
private static ConditionResult ApplyCondition(List<object> objects, LogicalCondition logicalCondition)
{
ConditionResult combinedResult = new ConditionResult() { Condition = logicalCondition };

if (logicalCondition.Conditions.Count == 0)
{
combinedResult.PassedObjects = objects;
combinedResult.Pattern = Enumerable.Repeat(true, objects.Count()).ToList();
return combinedResult;
}

if (logicalCondition.BooleanOperator == BooleanOperator.NOT)
{
BH.Engine.Reflection.Compute.RecordError($"Boolean operator `{BooleanOperator.NOT}` is not applicable when combining filters.");
Expand All @@ -26,7 +35,6 @@ private static ConditionResult ApplyCondition(List<object> objects, LogicalCondi
List<bool> passes = new List<bool>();
Enumerable.Repeat(true, objects.Count);

ConditionResult combinedResult = new ConditionResult() { Condition = logicalCondition };
List<ConditionResult> results = new List<ConditionResult>();

// FailInfos matrix: contains all failInfo, for each condition, for each object.
Expand Down
100 changes: 100 additions & 0 deletions CIH_Engine/Compute/ApplyCondition/ApplySpatialCondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2021, 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 BH.oM.Data.Conditions;
using BH.Engine.Base;
using BH.oM.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BH.oM.Data;
using BH.Engine.Diffing;
using BH.oM.Geometry;
using BH.Engine.Geometry;

namespace BH.Engine.CIH
{
public static partial class Compute
{
private static ConditionResult ApplyCondition(List<object> objects, ISpatialCondition cond)
{
// First apply filter to get relevant objects
BoundingBox containingBox = null;

Element0DCondition spec0D = cond as Element0DCondition;
if (spec0D != null)
containingBox = Query.ElementBoundingBox(spec0D.ReferencePoint, spec0D.LocalXDimension, spec0D.LocalYDimension, spec0D.LocalZDimension);

Element1DCondition spec1D = cond as Element1DCondition;
if (spec1D != null)
containingBox = Query.ElementBoundingBox(spec1D.ReferenceLine, spec1D.LocalYDimension, spec1D.LocalZDimension);

Element2DCondition spec2D = cond as Element2DCondition;
if (spec2D != null)
containingBox = Query.ElementBoundingBox(spec2D.ReferenceElement, spec2D.LocalZDimension);

BoundingBoxCondition bbc = cond as BoundingBoxCondition;
if (bbc != null)
containingBox = bbc.BoundingBox;

ConditionResult result = new ConditionResult() { Condition = cond };
List<string> info = new List<string>();

foreach (var obj in objects)
{
bool passed = false;

IBHoMObject iBHoMObj = obj as IBHoMObject;
if (iBHoMObj != null)
{
IGeometry geom = null;

if (cond.Containment3D)
geom = BH.Engine.Base.Query.IGeometry3D(iBHoMObj);

if (!cond.Containment3D || geom == null)
geom = BH.Engine.Base.Query.IGeometry(iBHoMObj);

BoundingBox bb = BH.Engine.Geometry.Query.IBounds(geom);

if (containingBox.IsContaining(bb))
passed = true;
}

if (passed)
result.PassedObjects.Add(obj);
else
{
result.FailedObjects.Add(obj);
info.Add($"Object was not {new BoundingBoxCondition() { BoundingBox = containingBox }.ToString()}.");
}

result.Pattern.Add(passed);
}

result.FailInfo = info;
return result;
}
}
}
29 changes: 27 additions & 2 deletions CIH_Engine/Compute/ApplyCondition/ApplyValueCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,30 @@ private static ConditionResult ApplyCondition(List<object> objects, ValueConditi
}
else
{
if (value is string && refValue is string)
if (value.ToString().Contains("BH.oM") && !(valueCondition.ReferenceValue is Type))
{
// If the value is a Type and the referenceValue is not, try comparing the `Name` property extracted from both.
// In some cases (e.g. Materials) this is useful.
string valueString = BH.Engine.Reflection.Query.PropertyValue(value, "Name") as string;
string referenceValue = BH.Engine.Reflection.Query.PropertyValue(valueCondition.ReferenceValue, "Name") as string;
passed = valueString == referenceValue;

if (!passed)
{
// If the previous failed, try comparing the Name extracted from the Value with the ToString() of the reference value.
// This is useful when an enum is provided as ReferenceValue.
passed = valueString == valueCondition.ReferenceValue.ToString();
}
}
else if (value.ToString().Contains("BH.oM") && (valueCondition.ReferenceValue is Type))
{
passed = value.ToString() == valueCondition.ReferenceValue.ToString();
}
else if (value is string && refValue is string)
passed = value.ToString() == refValue.ToString(); // workaround needed. Not even Convert.ChangeType and dynamic type worked.
else
passed = value == refValue;

}

}
Expand All @@ -110,7 +130,12 @@ private static ConditionResult ApplyCondition(List<object> objects, ValueConditi
else
{
result.FailedObjects.Add(obj);
info.Add($"{valueCondition.PropertyName} was {value ?? "empty"}, which does not respect '{valueCondition.ToString()}'.");
string valueString = value.ToString();

if (valueString.Contains("BH.oM") && !(valueCondition.ReferenceValue is Type))
valueString = BH.Engine.Reflection.Query.PropertyValue(value, "Name") as string;

info.Add($"{valueCondition.PropertyName} was {valueString ?? "empty"}, which does not respect '{valueCondition.ToString()}'.");
}

result.Pattern.Add(passed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ namespace BH.Engine.CIH
{
public static partial class Compute
{

/***************************************************/
/**** Public Methods ****/
/***************************************************/

public static ConditionResult ApplyConditions(List<object> objects, List<ICondition> conditions, BooleanOperator booleanOperator = BooleanOperator.AND)
{
return IApplyCondition(objects, new LogicalCondition() { Conditions = conditions, BooleanOperator = booleanOperator });
}

/***************************************************/
/**** Private Methods ****/
/***************************************************/

private static ConditionResult IApplyCondition(List<object> objects, ICondition condition)
{
if (IsAnyConditionNull(condition))
Expand All @@ -28,6 +37,8 @@ private static ConditionResult IApplyCondition(List<object> objects, ICondition
return ApplyCondition(objects, condition as dynamic);
}

/***************************************************/

//Fallback
private static ConditionResult ApplyCondition(List<object> objects, ICondition condition)
{
Expand Down
Loading