DesignEngrLab / MIConvexHull

A .Net fast convex hull library for 2, 3, and higher dimensions.
http://designengrlab.github.io/MIConvexHull/
MIT License
341 stars 60 forks source link

ConvexHull is throwing an exception for 4D points #37

Closed mutigozel closed 5 years ago

mutigozel commented 5 years ago

Hi,

The program below is throwing exception like:

System.IndexOutOfRangeException: Index was outside the bounds of the array. at MIConvexHull.MathHelper.CalculateFacePlane(ConvexFaceInternal face, Double[] center) in C:\Users\Muti\Desktop\MIConvexHull\MIConvexHull.NET Standard\ConvexHull\MathHelper.cs:line 118

`using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace ConsoleApp1 { class Program { public static class Tools { public static int DBG = 0; }

    static void Main(string[] args)
    {
        var dbg = Tools.DBG;

        List<Point> points = new List<Point>();

        foreach (var x in Enumerable.Range(1, 3))
            foreach (var y in Enumerable.Range(1, 3))
                foreach (var z in Enumerable.Range(1, 3))
                    foreach (var t in Enumerable.Range(1, 3))
                        points.Add(new Point(x, y, z, t));

        var hull = MIConvexHull.ConvexHull.Create(points);

        if (hull is null)
            dbg = Tools.DBG;

        var hullResult = hull.Result;

        if (hullResult is null)
            dbg = Tools.DBG;

        var hullPoints = hullResult.Points.ToList();

        if (hullPoints is null)
            dbg = Tools.DBG;
    }

    public class Point : MIConvexHull.IVertex
    {
        private double[] Values = new double[] { };

        public Point(double a, double b, double c, double d)
        {
            Values = new double[] { a, b, c, d };
        }
        public double[] Position
        {
            get
            {
                return Values;
            }
        }
    }

}

} `

micampbell commented 5 years ago

whoops, that shouldn't have happened. I fixed it now and pushed an update to nuget (1.1.19.1019) and to this repo (https://github.com/DesignEngrLab/MIConvexHull/commit/d5f3a77489f7f6c29ad954b29bfeaf8e4f4b646b). I fear that you may not be satisfied with the answer however. As you likely know the correct answer to your problem is 16 vertices, but MIConvexhull returns 17. this is because the current implementation does not produce either the maximal or minimal set of vertices - it is more based on speed. In your problem, all but one of the 81 vertices is on the convex hull, but many points like: {1,3,1,2} would be along some hyper-edge - and not a corner like {1,1,1,1}. The minimal set would remove these side points. The maximal set would include these. And there are different used cases in which you would want one or the over. Right now, it's all about speed. This is because it has been derived for uses in geometry where the shape is need - not necessarily the membership decision about each vertex.

Well, this is becoming a long-winded answer but I hope it helps you understand where we're coming from. Until we get around to having an input setting to switch between: maximal, minimal, or fast; perhaps you can post process the convex hull to find any vertices that are along edges.

mutigozel commented 5 years ago

Thanks @micampbell . It is solved.