Exemple #1
0
        public static Mesh DelaunayTriangulate(this PlainShape shape, IntGeom iGeom)
        {
            int n        = shape.points.Length;
            var vertices = new Vector3[n];

            for (int i = 0; i < n; ++i)
            {
                var v = iGeom.Float(shape.points[i]);
                vertices[i] = new Vector3(v.x, v.y, 0);
            }
            var extraPoints = new NativeArray <IntVector>(0, Allocator.Temp);
            var delaunay    = shape.Delaunay(0, extraPoints, Allocator.Temp);

            extraPoints.Dispose();

            var nTriangles = delaunay.Indices(Allocator.Temp);

            delaunay.Dispose();

            var mesh = new Mesh {
                vertices  = vertices,
                triangles = nTriangles.ToArray()
            };

            nTriangles.Dispose();

            return(mesh);
        }
Exemple #2
0
        public NativeArray <Vector3> Vertices(Allocator allocator, IntGeom intGeom, float z = 0)
        {
            int n      = points.Count;
            var result = new NativeArray <Vector3>(n, allocator);

            for (int i = 0; i < n; ++i)
            {
                var p = intGeom.Float(points[i]);
                result[i] = new Vector3(p.x, p.y, z);
            }

            return(result);
        }
Exemple #3
0
        public static List ConvexPolygons(this Delaunay self, IntGeom intGeom, Allocator allocator)
        {
            int n           = self.triangles.Count;
            var dynamicList = new DynamicList(self.points.Count, n >> 1, allocator);
            var visited     = new NativeArray <bool>(n, Allocator.Temp);

            for (int i = 0; i < n; ++i)
            {
                if (visited[i])
                {
                    continue;
                }

                var first = self.triangles[i];
                visited[i] = true;
                var convexPolygon = new ConvexPolygon(first);

                while (convexPolygon.edges.Count > 0)
                {
                    var edge = convexPolygon.edges.Last();
                    convexPolygon.edges.RemoveLast();
                    if (visited[edge.neighbor])
                    {
                        continue;
                    }

                    var next = self.triangles[edge.neighbor];
                    if (convexPolygon.Add(edge, next))
                    {
                        visited[edge.neighbor] = true;
                    }
                }

                var iPoints = convexPolygon.Points(Allocator.Temp);
                var points  = intGeom.Float(iPoints, Allocator.Temp);
                var polygon = new Polygon(points, allocator);
                dynamicList.Add(polygon);

                iPoints.Dispose();
                points.Dispose();
                convexPolygon.Dispose();
            }

            visited.Dispose();

            return(dynamicList.Convert());
        }
        public static List MakeCentroidNet(this PlainShape self, Allocator allocator, IntGeom intGeom, float maxEdge, float maxArea = 0, float minArea = 0, bool onlyConvex = false)
        {
            long  iEdge    = intGeom.Int(maxEdge);
            var   delaunay = self.Delaunay(iEdge, Allocator.Temp);
            float aMaxArea;

            if (maxArea > 0)
            {
                aMaxArea = maxArea;
            }
            else
            {
                aMaxArea = 0.4f * maxEdge * maxEdge;
            }

            delaunay.Tessellate(intGeom, aMaxArea);

            var iMinArea = intGeom.SqrInt(minArea);
            var shape    = delaunay.MakeCentroidNet(Allocator.Temp, iMinArea, onlyConvex);

            delaunay.Dispose();

            int n           = shape.layouts.Length;
            var dynamicList = new DynamicList(8 * n, n, allocator);

            for (int i = 0; i < n; ++i)
            {
                var iPath   = shape.Get(i);
                var path    = intGeom.Float(iPath, Allocator.Temp);
                var polygon = new Polygon(path, Allocator.Temp);
                dynamicList.Add(polygon);
            }

            shape.Dispose();

            return(dynamicList.Convert());
        }
        internal int TestRegular(Triangle triangle)
        {
            var a = intGeom.Float(triangle.vA.point);
            var b = intGeom.Float(triangle.vB.point);
            var c = intGeom.Float(triangle.vC.point);

            var ab = a.SqrDistance(b);
            var ca = c.SqrDistance(a);
            var bc = b.SqrDistance(c);

            float s0 = a.x * (c.y - b.y) + b.x * (a.y - c.y) + c.x * (b.y - a.y);
            float s1;

            int   k;
            float sCos;

            if (ab >= bc + ca)
            {
                // c, ab
                k = 2;
                float l = bc + ca - ab;
                sCos = l * l / (4 * bc * ca);
                s1   = s0 / (1 - sCos);
            }
            else if (bc >= ca + ab)
            {
                // a, bc
                k = 0;
                float l = ca + ab - bc;
                sCos = l * l / (4 * ca * ab);
                s1   = s0 / (1 - sCos);
            }
            else if (ca >= bc + ab)
            {
                // b, ca
                k = 1;
                float l = bc + ab - ca;
                sCos = l * l / (4 * bc * ab);
                s1   = s0 / (1 - sCos);
            }
            else
            {
                if (ab >= bc && ab >= ca)
                {
                    k = 2;
                }
                else if (bc >= ca)
                {
                    k = 0;
                }
                else
                {
                    k = 1;
                }

                s1 = s0;
            }

            if (s1 > maxArea)
            {
                return(k);
            }

            return(-1);
        }