internal RectangleR2 Cover() { RectangleR2 r = new RectangleR2(b[0]); for (int i = 1; i < n; i++) { r.Join(b[i]); } return r; }
internal RtreeR2Page SplitPage(Storage storage, RectangleR2 r, IPersistent obj) { int i, j, seed0 = 0, seed1 = 0; double[] rectArea = new double[card + 1]; double waste; //UPGRADE_TODO: The equivalent in .NET for field 'java.lang.Double.MIN_VALUE' may return a different value. double worstWaste = Double.MinValue; // // As the seeds for the two groups, find two rectangles which waste // the most area if covered by a single rectangle. // rectArea[0] = r.Area(); for (i = 0; i < card; i++) { rectArea[i + 1] = b[i].Area(); } RectangleR2 bp = r; for (i = 0; i < card; i++) { for (j = i + 1; j <= card; j++) { waste = RectangleR2.JoinArea(bp, b[j - 1]) - rectArea[i] - rectArea[j]; if (waste > worstWaste) { worstWaste = waste; seed0 = i; seed1 = j; } } bp = b[i]; } byte[] taken = new byte[card]; RectangleR2 group0, group1; double groupArea0, groupArea1; int groupCard0, groupCard1; RtreeR2Page pg; taken[seed1 - 1] = 2; group1 = new RectangleR2(b[seed1 - 1]); if (seed0 == 0) { group0 = new RectangleR2(r); pg = new RtreeR2Page(storage, obj, r); } else { group0 = new RectangleR2(b[seed0 - 1]); pg = new RtreeR2Page(storage, branch.GetRaw(seed0 - 1), group0); SetBranch(seed0 - 1, r, obj); } groupCard0 = groupCard1 = 1; groupArea0 = rectArea[seed0]; groupArea1 = rectArea[seed1]; // // Split remaining rectangles between two groups. // The one chosen is the one with the greatest difference in area // expansion depending on which group - the rect most strongly // attracted to one group and repelled from the other. // while (groupCard0 + groupCard1 < card + 1 && groupCard0 < card + 1 - minFill && groupCard1 < card + 1 - minFill) { int betterGroup = -1, chosen = -1; double biggestDiff = -1; for (i = 0; i < card; i++) { if (taken[i] == 0) { double diff = (RectangleR2.JoinArea(group0, b[i]) - groupArea0) - (RectangleR2.JoinArea(group1, b[i]) - groupArea1); if (diff > biggestDiff || -diff > biggestDiff) { chosen = i; if (diff < 0) { betterGroup = 0; biggestDiff = -diff; } else { betterGroup = 1; biggestDiff = diff; } } } } Assert.That(chosen >= 0); if (betterGroup == 0) { group0.Join(b[chosen]); groupArea0 = group0.Area(); taken[chosen] = 1; pg.SetBranch(groupCard0++, b[chosen], branch.GetRaw(chosen)); } else { groupCard1 += 1; group1.Join(b[chosen]); groupArea1 = group1.Area(); taken[chosen] = 2; } } // If one group gets too full, then remaining rectangle are // split between two groups in such way to balance cards of two groups. if (groupCard0 + groupCard1 < card + 1) { for (i = 0; i < card; i++) { if (taken[i] == 0) { if (groupCard0 >= groupCard1) { taken[i] = 2; groupCard1 += 1; } else { taken[i] = 1; pg.SetBranch(groupCard0++, b[i], branch.GetRaw(i)); } } } } pg.n = groupCard0; n = groupCard1; for (i = 0, j = 0; i < groupCard1; j++) { if (taken[j] == 2) { SetBranch(i++, b[j], branch.GetRaw(j)); } } return pg; }
/// <summary> Non destructive join of two rectangles. </summary> /// <param name="a">first joined rectangle /// </param> /// <param name="b">second joined rectangle /// </param> /// <returns> rectangle containing Cover of these two rectangles /// </returns> public static RectangleR2 Join(RectangleR2 a, RectangleR2 b) { RectangleR2 r = new RectangleR2(a); r.Join(b); return r; }