Exemplo n.º 1
0
        /// <summary>
        /// Distributes random points within bounds and creates a convex hull around it.
        /// </summary>
        /// <param name="mask"></param>
        /// <param name="bounds"></param>
        /// <param name="count"></param>
        public static List <Vector3> CreateRandomShapeUsingConvexHull(Bounds bounds, int count)
        {
            List <Vector2> points = new List <Vector2>();

            for (int i = 0; i < count; i++)
            {
                points.Add(PolygonUtils.GetRandomPointXZ(bounds));
            }

            List <Vector2> convexHull = PolygonUtility.GetConvexHull(points);

            List <Vector3> nodes = convexHull.ConvertAll <Vector3>(item => new Vector3(item.x, 0, item.y));

            return(nodes);
        }
        /// <summary>
        /// Transform the mask and convert it into its convex hull.
        /// </summary>
        /// <param name="mask"></param>
        public static void ConvexHull(VegetationMaskArea mask)
        {
            Vector3 maskPosition = mask.transform.position;

            List <Vector2> positionsXY = mask.Nodes.ConvertAll <Vector2>(item => new Vector2(item.Position.x, item.Position.z));

            List <Vector2> convexHull = PolygonUtility.GetConvexHull(positionsXY);

            mask.Nodes.Clear();
            foreach (Vector2 nodePosition in convexHull)
            {
                mask.AddNodeToEnd(new Vector3(maskPosition.x + nodePosition.x, 0, maskPosition.z + nodePosition.y));
            }

            // center main handle, implicitly updates the mask
            CenterMainHandle(mask);
        }
Exemplo n.º 3
0
        public void GenerateHullNodes(float tolerance)
        {
            List <Vector2> worldSpacePointList = new List <Vector2>();

            MeshFilter[] mersFilters = GetComponentsInChildren <MeshFilter>();
            for (int i = 0; i <= mersFilters.Length - 1; i++)
            {
                Mesh mesh = mersFilters[i].sharedMesh;
                if (mesh)
                {
                    List <Vector3> verticeList = new List <Vector3>();
                    mesh.GetVertices(verticeList);
                    for (int j = 0; j <= verticeList.Count - 1; j++)
                    {
                        Vector3 worldSpacePosition = mersFilters[i].transform.TransformPoint(verticeList[j]);
                        Vector2 worldSpacePoint    = new Vector2
                        {
                            x = worldSpacePosition.x,
                            y = worldSpacePosition.z
                        };
                        worldSpacePointList.Add(worldSpacePoint);
                    }
                }
            }

            List <Vector2> hullPointList    = PolygonUtility.GetConvexHull(worldSpacePointList);
            List <Vector2> reducedPointList = PolygonUtility.DouglasPeuckerReduction(hullPointList, tolerance);


            if (reducedPointList.Count >= 3)
            {
                ClearNodes();
                for (int i = 0; i <= reducedPointList.Count - 1; i++)
                {
                    Vector3 worldSpacePosition = new Vector3(reducedPointList[i].x, 0, reducedPointList[i].y);
                    AddNode(worldSpacePosition);
                }
                PositionNodes();
            }
        }