private void Initialize(List <Vector2> parent, List <Vector2> child) { List <Vector4> connects = null; List <Vector2> source = new List <Vector2>(); if (child != null && child.Count > 0) { EarPolygon polygon = new EarPolygon(); foreach (Vector2 t in parent) { polygon.AddPoint(t[0], t[1]); } EarPolygon chlid = new EarPolygon(polygon); foreach (Vector2 t in child) { chlid.AddPoint(t[0], t[1]); } connects = EarClipping.Merge(polygon); LinkedListNode <EarPoint> point = polygon.Get(); while (point.Next != null) { source.Add(point.Value.mPoint); point = point.Next; } } else { source.AddRange(parent); } mWrapper = PolygonWrapper.Create(source, true, connects, 2); mArea = mWrapper.Area; }
// 带有空洞的多边形 public static PolygonWrapper Create(List <Vector2> data, bool random, List <Vector4> removeEdge = null, float scale = 4.0f) // 单位之间间隔为 1 / scale { PolygonWrapper wrapper = new PolygonWrapper(data); wrapper.Initialize(scale); wrapper.InitializeSegmentsBvh(removeEdge); if (random) { wrapper.ShuffleCenterList(); } return(wrapper); }
public static PolygonWrapper Create(List <Vector2> data, List <Vector2> innerBorder, bool random, float scale = 4.0f) // 单位之间间隔为 1 / scale { PolygonWrapper wrapper = new PolygonWrapper(data); wrapper.Initialize(scale); wrapper.InitializeSegmentsBvh(data, innerBorder); if (innerBorder != null) { wrapper.RemovePointsInPolygon(innerBorder); } if (random) { wrapper.ShuffleCenterList(); } return(wrapper); }
public GeoAABB2 GetRect(float w, float h, int i, ref HashSet <Vector2i> record) { GeoAABB2 rect = new GeoAABB2(); for (int j = 0; j < mPolygonPartition.Count; ++j) { Vector2i key = Vector2i.GetVector2i(i, j); PolygonWrapper poly = mPolygonPartition[j]; if (!record.Contains(key)) { if (poly.GetRectangle(w, h, ref rect)) { return(rect); } else { record.Add(key); } } } return(null); }
/// <summary> /// 1 在某一个段内找不到时,并且找的概率为1,容易出现死循环 nowCount insertMaxCount 来控制 退出循环 /// 2 wrapper.IsFull() 设置占比,退出循环 /// 3 cacheIndex.Count < prob 退出循环 /// </summary> /// <param name="original"></param> /// <param name="probablities"></param> /// <param name="connects"></param> /// <returns></returns> private static List <List <GeoAABB2> > PolygonPartition(List <Vector2> original, List <PartitionUnit> probablities, List <Vector4> connects = null) { List <PartitionUnit> newUnits = new List <PartitionUnit>(); newUnits.AddRange(probablities); PolygonWrapper wrapper = PolygonWrapper.Create(original, true, connects, 2); float total = wrapper.Area; int prob = newUnits.Count; List <List <GeoAABB2> > results = new List <List <GeoAABB2> >(); List <float> resultsArea = new List <float>(); List <float> areaProb = new List <float>(); // 更新概率 float proSum = 0.0f; for (int i = 0; i < prob; ++i) { results.Add(new List <GeoAABB2>()); resultsArea.Add(0.0f); areaProb.Add(newUnits[i].mProbablity); proSum += newUnits[i].mProbablity; } int insertMaxCount = prob * 10; int nowCount = 0; HashSet <Vector2i> cacheWH = new HashSet <Vector2i>(); HashSet <int> cacheIndex = new HashSet <int>(); int index = RandomInt(areaProb, proSum); while (!wrapper.IsFull() && cacheIndex.Count < prob && nowCount < insertMaxCount) { float sizew = RandomUtils.GetRandomFloat(newUnits[index].mMinW, newUnits[index].mMaxW); float sizeh = RandomUtils.GetRandomFloat(newUnits[index].mMinH, newUnits[index].mMaxH); Vector2i temp = new Vector2i((int)(sizew * 100), (int)(sizeh * 100)); if (!cacheWH.Contains(temp)) { GeoAABB2 aabb = new GeoAABB2(); if (!wrapper.GetRectangle(sizew, sizeh, ref aabb)) { cacheWH.Add(temp); if (!cacheIndex.Add(index)) { nowCount++; } } else { results[index].Add(aabb); float aabbArea = aabb.Area(); resultsArea[index] += aabbArea; cacheIndex.Clear(); nowCount = 0; areaProb.Clear(); proSum = 0.0f; for (int a = 0; a < prob; ++a) { float newProb = resultsArea[a] / total - newUnits[a].mProbablity; newProb = newProb > 0 ? 0 : -newProb; areaProb.Add(newProb); proSum += newProb; } if (proSum == 0) { break; } } } else { if (!cacheIndex.Add(index)) { nowCount++; } } index = RandomInt(areaProb, proSum); } for (int a = 0; a < resultsArea.Count; ++a) { float abs = resultsArea[a] / total; Debug.Log(string.Format("count: {0}, xLen: {1}", results[a].Count, abs)); } return(results); }
private void Initialize(List <Vector2> parent, List <Vector2> child, bool shuffle, float scale = 4.0f) { mWrapper = PolygonWrapper.Create(parent, child, shuffle, scale); }
public void AddPolygon(List <Vector2> data) { mPolygonPartition.Add(PolygonWrapper.Create(data, false)); }
public void AddPolygon(PolygonWrapper poly) { mPolygonPartition.Add(poly); }