public static bool Create2D(IList <Vector2> points, out int[] indices, out int dimension, float epsilon = 1E-05f)
 {
     if (points == null || points.Count == 0)
     {
         indices   = null;
         dimension = -1;
         return(false);
     }
     epsilon = ((epsilon >= 0f) ? epsilon : 0f);
     return(ConvexHull2.Create(points, epsilon, out dimension, out indices));
 }
Beispiel #2
0
        public static bool Create(IList <Vector2> vertices, float epsilon, out int dimension, out int[] indices)
        {
            Vector2ex.Information information = Vector2ex.GetInformation(vertices, epsilon);
            if (information == null)
            {
                dimension = -1;
                indices   = null;
                return(false);
            }
            int count = vertices.Count;

            if (information.Dimension == 0)
            {
                dimension = 0;
                int[] array = new int[1];
                indices = array;
                return(true);
            }
            if (information.Dimension == 1)
            {
                float[] array2 = new float[count];
                Vector2 origin = information.Origin;
                Vector2 vector = information.Direction[0];
                for (int i = 0; i < count; i++)
                {
                    Vector2 value = vertices[i] - origin;
                    array2[i] = vector.Dot(value);
                }
                ConvexHull1.Create(array2, epsilon, out dimension, out indices);
                return(true);
            }
            dimension = 2;
            Vector2[] array3 = new Vector2[count];
            Vector2   min    = information.Min;
            float     d      = 1f / information.MaxRange;

            for (int j = 0; j < count; j++)
            {
                array3[j] = (vertices[j] - min) * d;
            }
            Query2 query = new Query2(array3);
            int    num   = information.Extreme[0];
            int    num2  = information.Extreme[1];
            int    num3  = information.Extreme[2];

            ConvexHull2.Edge edge;
            ConvexHull2.Edge edge2;
            ConvexHull2.Edge edge3;
            if (information.ExtremeCCW)
            {
                edge  = new ConvexHull2.Edge(num, num2);
                edge2 = new ConvexHull2.Edge(num2, num3);
                edge3 = new ConvexHull2.Edge(num3, num);
            }
            else
            {
                edge  = new ConvexHull2.Edge(num, num3);
                edge2 = new ConvexHull2.Edge(num3, num2);
                edge3 = new ConvexHull2.Edge(num2, num);
            }
            edge.Insert(edge3, edge2);
            edge2.Insert(edge, edge3);
            edge3.Insert(edge2, edge);
            ConvexHull2.Edge edge4 = edge;
            for (int k = 0; k < count; k++)
            {
                if (!ConvexHull2.Update(ref edge4, k, query))
                {
                    dimension = -1;
                    indices   = null;
                    return(false);
                }
            }
            edge4.GetIndices(out indices);
            return(true);
        }
        public static bool Create(IList <Vector3> vertices, float epsilon, out int dimension, out int[] indices)
        {
            Vector3ex.Information information = Vector3ex.GetInformation(vertices, epsilon);
            if (information == null)
            {
                dimension = -1;
                indices   = null;
                return(false);
            }
            int count = vertices.Count;

            if (information.Dimension == 0)
            {
                dimension = 0;
                int[] array = new int[1];
                indices = array;
                return(true);
            }
            if (information.Dimension == 1)
            {
                float[] array2 = new float[count];
                Vector3 origin = information.Origin;
                Vector3 vector = information.Direction[0];
                for (int i = 0; i < count; i++)
                {
                    Vector3 value = vertices[i] - origin;
                    array2[i] = vector.Dot(value);
                }
                ConvexHull1.Create(array2, epsilon, out dimension, out indices);
                return(true);
            }
            if (information.Dimension == 2)
            {
                Vector2[] array3  = new Vector2[count];
                Vector3   origin2 = information.Origin;
                Vector3   vector2 = information.Direction[0];
                Vector3   vector3 = information.Direction[1];
                for (int j = 0; j < count; j++)
                {
                    Vector3 value2 = vertices[j] - origin2;
                    array3[j] = new Vector2(vector2.Dot(value2), vector3.Dot(value2));
                }
                return(ConvexHull2.Create(array3, epsilon, out dimension, out indices));
            }
            dimension = 3;
            Vector3[] array4 = new Vector3[count];
            Vector3   min    = information.Min;
            float     d      = 1f / information.MaxRange;

            for (int k = 0; k < count; k++)
            {
                array4[k] = (vertices[k] - min) * d;
            }
            Query3 query = new Query3(array4);
            int    v     = information.Extreme[0];
            int    num   = information.Extreme[1];
            int    num2  = information.Extreme[2];
            int    num3  = information.Extreme[3];

            ConvexHull3.Triangle triangle;
            ConvexHull3.Triangle triangle2;
            ConvexHull3.Triangle triangle3;
            ConvexHull3.Triangle triangle4;
            if (information.ExtremeCCW)
            {
                triangle  = new ConvexHull3.Triangle(v, num, num3);
                triangle2 = new ConvexHull3.Triangle(v, num2, num);
                triangle3 = new ConvexHull3.Triangle(v, num3, num2);
                triangle4 = new ConvexHull3.Triangle(num, num2, num3);
                triangle.AttachTo(triangle2, triangle4, triangle3);
                triangle2.AttachTo(triangle3, triangle4, triangle);
                triangle3.AttachTo(triangle, triangle4, triangle2);
                triangle4.AttachTo(triangle2, triangle3, triangle);
            }
            else
            {
                triangle  = new ConvexHull3.Triangle(v, num3, num);
                triangle2 = new ConvexHull3.Triangle(v, num, num2);
                triangle3 = new ConvexHull3.Triangle(v, num2, num3);
                triangle4 = new ConvexHull3.Triangle(num, num3, num2);
                triangle.AttachTo(triangle3, triangle4, triangle2);
                triangle2.AttachTo(triangle, triangle4, triangle3);
                triangle3.AttachTo(triangle2, triangle4, triangle);
                triangle4.AttachTo(triangle, triangle3, triangle2);
            }
            HashSet <ConvexHull3.Triangle> hashSet = new HashSet <ConvexHull3.Triangle>();

            hashSet.Add(triangle);
            hashSet.Add(triangle2);
            hashSet.Add(triangle3);
            hashSet.Add(triangle4);
            for (int l = 0; l < count; l++)
            {
                if (!ConvexHull3.Update(hashSet, l, query))
                {
                    dimension = -1;
                    indices   = null;
                    return(false);
                }
            }
            ConvexHull3.ExtractIndices(hashSet, out indices);
            return(true);
        }