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

Geometry_Engine Move ConvexHull #1332

Merged
merged 2 commits into from
Nov 18, 2019
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
95 changes: 95 additions & 0 deletions Geometry_Engine/Compute/ConvexHull.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2018, 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.Geometry;
using System;
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;

namespace BH.Engine.Geometry
{
public static partial class Compute
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

//TODO: Only works for points in the XY plane - add plane as input?
[Description("Creates a Convex Hull from a list of points. Currently only works for points in the XY plane")]
public static Polyline ConvexHull(List<Point> points)
{
List<Point> hull = new List<Point>();
hull.Add(points.First());

for(int x = 1; x < points.Count; x++)
{
if (hull[0].X > points[x].X)
hull[0] = points[x];
else if (hull[0].X == points[x].X)
{
if (hull[0].Y > points[x].Y)
hull[0] = points[x];
}
}

Point nextPt = new Point();
int counter = 0;
while (counter < hull.Count)
{
nextPt = NextHullPoint(points, hull[counter]);
if (nextPt != hull[0])
hull.Add(nextPt);
counter++;
}

hull.Add(hull[0]);

Polyline hullBoundary = new Polyline() { ControlPoints = hull };
return hullBoundary;
}


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

private static Point NextHullPoint(List<Point> points, Point currentPt)
{
int right = -1;
int none = 0;

Point nextPt = currentPt;
int t;
foreach (Point pt in points)
{
t = ((nextPt.X - currentPt.X) * (pt.Y - currentPt.Y) - (pt.X - currentPt.X) * (nextPt.Y - currentPt.Y)).CompareTo(0);
if (t == right || t == none && Geometry.Query.Distance(currentPt, pt) > Geometry.Query.Distance(currentPt, nextPt))
nextPt = pt;
}

return nextPt;
}

/***************************************************/
}
}
57 changes: 4 additions & 53 deletions Geometry_Engine/Create/ConvexHull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;
using BH.oM.Reflection.Attributes;

namespace BH.Engine.Geometry
{
Expand All @@ -33,63 +34,13 @@ public static partial class Create
/***************************************************/
/**** Public Methods ****/
/***************************************************/

[Deprecated("3.0", "Deprecated, method moved to compute file", null, "BH.Engine.Geometry.Compute.ConvexHull(List<Point>")]

//TODO: Only works for points in the XY plane - add plane as input?
[Description("Creates a Convex Hull from a list of points. Currently only works for points in the XY plane")]
public static Polyline ConvexHull(List<Point> points)
{
List<Point> hull = new List<Point>();
hull.Add(points.First());

for(int x = 1; x < points.Count; x++)
{
if (hull[0].X > points[x].X)
hull[0] = points[x];
else if (hull[0].X == points[x].X)
{
if (hull[0].Y > points[x].Y)
hull[0] = points[x];
}
}

Point nextPt = new Point();
int counter = 0;
while (counter < hull.Count)
{
nextPt = NextHullPoint(points, hull[counter]);
if (nextPt != hull[0])
hull.Add(nextPt);
counter++;
}

hull.Add(hull[0]);

Polyline hullBoundary = new Polyline() { ControlPoints = hull };
return hullBoundary;
}


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

private static Point NextHullPoint(List<Point> points, Point currentPt)
{
int right = -1;
int none = 0;

Point nextPt = currentPt;
int t;
foreach (Point pt in points)
{
t = ((nextPt.X - currentPt.X) * (pt.Y - currentPt.Y) - (pt.X - currentPt.X) * (nextPt.Y - currentPt.Y)).CompareTo(0);
if (t == right || t == none && Geometry.Query.Distance(currentPt, pt) > Geometry.Query.Distance(currentPt, nextPt))
nextPt = pt;
}

return nextPt;
return BH.Engine.Geometry.Compute.ConvexHull(points);
}

/***************************************************/
}
}
1 change: 1 addition & 0 deletions Geometry_Engine/Geometry_Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="Compute\BooleanUnion.cs" />
<Compile Include="Compute\ClusterCollinear.cs" />
<Compile Include="Compute\ClusterCoplanar.cs" />
<Compile Include="Compute\ConvexHull.cs" />
<Compile Include="Compute\CullDuplicates.cs" />
<Compile Include="Compute\DistributeOutlines.cs" />
<Compile Include="Compute\Eigenvalues.cs" />
Expand Down