public static IntervalList GetInsideBoxIntervals(List <Box2> boxes, double split, Vec2.Component comp, Vec2.Component other) { IntervalList iList = new IntervalList(); foreach (Box2 box in boxes) { if (split >= box.min.GetComponent(comp) && split <= box.max.GetComponent(comp)) { iList.Add(new Interval(box.min.GetComponent(other), box.max.GetComponent(other))); } } return(iList); }
private static void RemoveAreasSmallerThan(List <Box2> boxes, double p, int level) { if (boxes.Count == 0) { return; } if (boxes.Count == 1) { if (boxes[0].Dimensions.X < p || boxes[0].Dimensions.Y < p) { boxes.Clear(); } return; } if (level > 20) { throw new Exception("Dit komt nooit goed!"); } //Image img = new Bitmap(500, 500); //Graphics g = Graphics.FromImage(img); //foreach (Box2 b in boxes) //{ // Vec2 min = 5 * (b.min + new Vec2(5, 5)); // Vec2 max = 5 * (b.max + new Vec2(5, 5)); // Vec2 dim = max - min; // Rectangle rec = new Rectangle((int)min.X, (int)min.Y, (int)dim.X, (int)dim.Y); // g.DrawRectangle(Pens.Black, rec); //} //g.Dispose(); //img.Save("C:\\boxes_debug" + (jefke++) + ".png"); bool removedArea = false; foreach (Vec2.Component comp in Enum.GetValues(typeof(Vec2.Component))) { Vec2.Component other = Vec2.Component.X; if (comp == Vec2.Component.X) { other = Vec2.Component.Y; } List <float> splitPoints = new List <float>(); foreach (Box2 box in boxes) { foreach (float d in new float[] { box.min.GetComponent(comp), box.max.GetComponent(comp) }) { if (!splitPoints.Contains(d)) { splitPoints.Add(d); } } } splitPoints.Sort(); for (int splitI = 0; splitI < splitPoints.Count; ++splitI) { float split = splitPoints[splitI]; IntervalList intervals = GetInsideBoxIntervals(boxes, split, comp, other); foreach (Interval i in intervals) { if (i.Length < p) { List <Box2> toDelete = new List <Box2>(); List <Box2> toAdd = new List <Box2>(); foreach (Box2 box in boxes) { if ((new Interval(box.min.GetComponent(other), box.max.GetComponent(other))).Overlaps(i)) { if (box.min.GetComponent(comp) == split) { box.min.SetComponent(comp, splitPoints[splitI + 1]); if (box.min.GetComponent(comp) == box.max.GetComponent(comp)) { toDelete.Add(box); } } else if (box.max.GetComponent(comp) == split) { box.max.SetComponent(comp, splitPoints[splitI - 1]); if (box.min.GetComponent(comp) == box.max.GetComponent(comp)) { toDelete.Add(box); } } else { Box2 newBox = new Box2(box); if (splitI - 1 < 0) { toDelete.Add(box); } else { box.max.SetComponent(comp, splitPoints[splitI - 1]); if (box.min.GetComponent(comp) == box.max.GetComponent(comp)) { toDelete.Add(box); } } if (splitI + 1 < splitPoints.Count) { newBox.min.SetComponent(comp, splitPoints[splitI + 1]); if (newBox.min.GetComponent(comp) != newBox.max.GetComponent(comp)) { toAdd.Add(newBox); } } } } } foreach (Box2 box in toDelete) { boxes.Remove(box); } foreach (Box2 box in toAdd) { boxes.Add(box); } if (toDelete.Count != 0 || toAdd.Count != 0) { RemoveAreasSmallerThan(boxes, p, level + 1); removedArea = true; break; } else { // throw new NotImplementedException("Dit komt nooit goed!"); } } } if (removedArea) { break; } } if (removedArea) { break; } } }