Exemple #1
0
        public static List <Vector2> MinLeastSquare(List <Vector2> polygonPoints, float threshold = 0.1f)
        {
            GeoPolygonUtils.ReverseIfCW(ref polygonPoints);
            List <Vector2> temp   = new List <Vector2>();
            List <Vector2> result = new List <Vector2>();

            temp.AddRange(polygonPoints);
            List <Vector2> kb    = new List <Vector2>();
            int            count = polygonPoints.Count();

            for (int i = 0; i < temp.Count;)
            {
                result.Add(temp[i]);
                int n = i + 2;
                while (n < temp.Count && CalculateLeastSquare(i, n, temp, threshold))
                {
                    ++n;
                }
                i = n - 1;
            }
            if (result.Count != count)
            {
                result = MinLeastSquare(result);
            }
            return(result);
        }
Exemple #2
0
        public static List <Vector3> CalculateConvexHull(List <Vector3> vertices, bool use2d, int ignore = 1)
        {
            List <Vector3> temp = new List <Vector3>();

            if (use2d)
            {
                int x = 0;
                int y = 1;
                if (ignore == 0)
                {
                    x = 1;
                    y = 2;
                }
                else if (ignore == 1)
                {
                    x = 0;
                    y = 2;
                }
                else
                {
                    x = 0;
                    y = 1;
                }
                List <Vector2> temp2 = new List <Vector2>();
                foreach (Vector3 v in vertices)
                {
                    temp2.Add(new Vector2(v[x], v[y]));
                }
                temp2 = JarvisConvex.BuildHull(temp2);
                GeoPolygonUtils.ReverseIfCW(ref temp2);
                foreach (Vector2 v in temp2)
                {
                    temp.Add(vertices.Find((Vector3 v3) => { return(v3[x] == v[0] && v3[y] == v[1]); }));
                }
            }
            else
            {
                temp = QuickHull.BuildHull(vertices);
            }
            return(temp);
        }
Exemple #3
0
        private void RemovePointsInPolygon(List <Vector2> innerPoly)
        {
            List <Vector2> temp = new List <Vector2>();

            temp.AddRange(innerPoly);
            GeoPolygonUtils.ReverseIfCW(ref temp);
            GeoPointsArray2 poly        = new GeoPointsArray2(temp);
            List <int>      removeIndex = new List <int>();

            for (int i = 0; i < mCenterList.Count; ++i)
            {
                Vector2 t = mCenterList[i];
                if (GeoPolygonUtils.IsPointInPolygon2(poly, ref t))
                {
                    removeIndex.Add(i);
                }
            }
            for (int i = removeIndex.Count - 1; i >= 0; --i)
            {
                mCenterList.RemoveAt(removeIndex[i]);
            }
        }
        // 对于环状问题,暂时没处理。处理起来,稍作修改即可。后期遇到时加入(检测环状,然后分离环状,及带孔的多边形环形区域。)
        public static void PolygonExpand(List <Vector2> original, List <Vector2> results, float length = 1.0f, bool useHull = true)
        {
            List <Vector2> origin = new List <Vector2>();

            origin.AddRange(original);
            GeoPointsArray2 hullArray = null;

            if (useHull)
            {
                List <Vector2> hull = JarvisConvex.BuildHull(original);
                GeoPolygonUtils.ReverseIfCW(ref hull);
                hullArray = new GeoPointsArray2(hull);
            }
            float len = length;

            while (results.Count == 0)
            {
                List <Vector2> segs = ExpandPolygonSegments(origin, len);
                float          min  = CheckSegmentsIntersect(segs);
                if (min > len)
                {
                    min = len;
                }
                if (min > 1e-5f)
                {
                    List <Vector2> expands = ExpandPolygon(origin, min);
                    //List<int> vLst = GeoUtils.VertexIndexList(expands);
                    //List<List<int>> circles = CommonUtils.CheckCircle(vLst);
                    // GeoUtils.Split(origin, circles);
                    origin = PolygonIntersectRemove(expands);
                    len    = len - min;
                    if (len < 0.01f)
                    {
                        results.AddRange(origin);
                    }
                }
                else
                {
                    for (int i = 1; i < segs.Count; i += 2)
                    {
                        results.Add(segs[i]);
                    }
                }
                if (useHull && results.Count == 0 && hullArray != null)
                {
                    bool flag = false;
                    for (int i = 0; i < origin.Count; i++)
                    {
                        Vector2 refV = origin[i];
                        if (GeoPolygonUtils.IsPointInConvexPolygon2(hullArray, ref refV))
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        List <Vector2> split = JarvisConvex.BuildHull(origin);
                        GeoPolygonUtils.ReverseIfCW(ref split);
                        split = ConvexMarginalExpand(split, len);
                        results.AddRange(split);
                        len = 0;
                    }
                }
            }
        }