Search in sources :

Example 31 with RectDouble2D

use of jcog.tree.rtree.rect.RectDouble2D in project narchy by automenta.

the class LinearSplitLeafTest method causeLinearSplitNiceDist.

@Test
public void causeLinearSplitNiceDist() {
    final RTree<RectDouble2D> rTree = RTree2DTest.createRect2DTree(2, 8, TYPE);
    final Random rand = new Random(13);
    for (int i = 0; i < 500; i++) {
        final int x1 = rand.nextInt(250);
        final int y1 = rand.nextInt(250);
        final int x2 = x1 + rand.nextInt(10);
        final int y2 = y1 + rand.nextInt(10);
        rTree.add(new RectDouble2D(x1, y1, x2, y2));
    }
    final Stats stats = rTree.stats();
    stats.print(System.out);
}
Also used : Random(java.util.Random) Stats(jcog.tree.rtree.util.Stats) RectDouble2D(jcog.tree.rtree.rect.RectDouble2D) Test(org.junit.jupiter.api.Test)

Example 32 with RectDouble2D

use of jcog.tree.rtree.rect.RectDouble2D in project narchy by automenta.

the class QuadraticSplitLeafTest method randomEntryTest.

/**
 * Adds many random entries to trees of different types and confirms that
 * no entries are lost during insertion (and split).
 */
@Test
public void randomEntryTest() {
    final int entryCount = 50000;
    final RectDouble2D[] rects = RTree2DTest.generateRandomRects(entryCount);
    final RTree<RectDouble2D> rTree = RTree2DTest.createRect2DTree(TYPE);
    for (int i = 0; i < rects.length; i++) {
        rTree.add(rects[i]);
    }
    final Stats stats = rTree.stats();
    assertTrue(Math.abs(entryCount - stats.getEntryCount()) < 20, "Unexpected number of entries in " + TYPE + " split tree: " + stats.getEntryCount() + " entries - expected: " + entryCount + " actual: " + stats.getEntryCount());
    stats.print(System.out);
}
Also used : Stats(jcog.tree.rtree.util.Stats) RectDouble2D(jcog.tree.rtree.rect.RectDouble2D) Test(org.junit.jupiter.api.Test)

Example 33 with RectDouble2D

use of jcog.tree.rtree.rect.RectDouble2D in project narchy by automenta.

the class RTreeNDTest method generateRandomRects.

static RectDouble2D[] generateRandomRects(int count) {
    final Random rand = new Random(13);
    // changing these values changes the rectangle sizes and consequently the distribution density
    final int minX = 500;
    final int minY = 500;
    final int maxXRange = 25;
    final int maxYRange = 25;
    final double hitProb = 1.0 * count * maxXRange * maxYRange / (minX * minY);
    final RectDouble2D[] rects = new RectDouble2D[count];
    for (int i = 0; i < count; i++) {
        final int x1 = rand.nextInt(minX);
        final int y1 = rand.nextInt(minY);
        final int x2 = x1 + rand.nextInt(maxXRange);
        final int y2 = y1 + rand.nextInt(maxYRange);
        rects[i] = new RectDouble2D(x1, y1, x2, y2);
    }
    return rects;
}
Also used : RectDouble2D(jcog.tree.rtree.rect.RectDouble2D)

Example 34 with RectDouble2D

use of jcog.tree.rtree.rect.RectDouble2D in project narchy by automenta.

the class RTreeNDTest method treeRemoveAndRebalanceTest.

@Disabled
public // This test ignored because output needs to be manually evaluated.
void treeRemoveAndRebalanceTest() {
    final RTree<RectDouble2D> rTree = createRectDouble2DTree(Spatialization.DefaultSplits.QUADRATIC);
    RectDouble2D[] rect = new RectDouble2D[65];
    for (int i = 0; i < rect.length; i++) {
        if (i < 4) {
            rect[i] = new RectDouble2D(0, 0, 1, 1);
        } else if (i < 8) {
            rect[i] = new RectDouble2D(2, 2, 4, 4);
        } else if (i < 12) {
            rect[i] = new RectDouble2D(4, 4, 5, 5);
        } else if (i < 16) {
            rect[i] = new RectDouble2D(5, 5, 6, 6);
        } else if (i < 20) {
            rect[i] = new RectDouble2D(6, 6, 7, 7);
        } else if (i < 24) {
            rect[i] = new RectDouble2D(7, 7, 8, 8);
        } else if (i < 28) {
            rect[i] = new RectDouble2D(8, 8, 9, 9);
        } else if (i < 32) {
            rect[i] = new RectDouble2D(9, 9, 10, 10);
        } else if (i < 36) {
            rect[i] = new RectDouble2D(2, 2, 4, 4);
        } else if (i < 40) {
            rect[i] = new RectDouble2D(4, 4, 5, 5);
        } else if (i < 44) {
            rect[i] = new RectDouble2D(5, 5, 6, 6);
        } else if (i < 48) {
            rect[i] = new RectDouble2D(6, 6, 7, 7);
        } else if (i < 52) {
            rect[i] = new RectDouble2D(7, 7, 8, 8);
        } else if (i < 56) {
            rect[i] = new RectDouble2D(8, 8, 9, 9);
        } else if (i < 60) {
            rect[i] = new RectDouble2D(9, 9, 10, 10);
        } else if (i < 65) {
            rect[i] = new RectDouble2D(1, 1, 2, 2);
        }
    }
    for (int i = 0; i < rect.length; i++) {
        rTree.add(rect[i]);
    }
    Stats stat = rTree.stats();
    stat.print(System.out);
    for (int i = 0; i < 5; i++) {
        rTree.remove(rect[64]);
    }
    Stats stat2 = rTree.stats();
    stat2.print(System.out);
}
Also used : Stats(jcog.tree.rtree.util.Stats) RectDouble2D(jcog.tree.rtree.rect.RectDouble2D) Disabled(org.junit.jupiter.api.Disabled)

Example 35 with RectDouble2D

use of jcog.tree.rtree.rect.RectDouble2D in project narchy by automenta.

the class RTreeNDTest method RectDouble2DSearchAllTest.

/**
 * Use an enormous bounding box to ensure that every rectangle is returned.
 * Verifies the count returned from search AND the number of rectangles results.
 */
@Test
public void RectDouble2DSearchAllTest() {
    final int entryCount = 1000;
    final RectDouble2D[] rects = generateRandomRects(entryCount);
    for (Spatialization.DefaultSplits type : Spatialization.DefaultSplits.values()) {
        RTree<RectDouble2D> rTree = createRectDouble2DTree(2, 8, type);
        for (int i = 0; i < rects.length; i++) {
            rTree.add(rects[i]);
        }
        final RectDouble2D searchRect = new RectDouble2D(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
        RectDouble2D[] results = new RectDouble2D[entryCount];
        final int foundCount = rTree.containedToArray(searchRect, results);
        int resultCount = 0;
        for (int i = 0; i < results.length; i++) {
            if (results[i] != null) {
                resultCount++;
            }
        }
        final AtomicInteger visitCount = new AtomicInteger();
        rTree.whileEachContaining(searchRect, (n) -> {
            visitCount.incrementAndGet();
            return true;
        });
        assertEquals(entryCount, visitCount.get());
        final int expectedCount = entryCount;
        assertEquals(expectedCount, foundCount, "[" + type + "] Search returned incorrect search result count - expected: " + expectedCount + " actual: " + foundCount);
        assertEquals(expectedCount, resultCount, "[" + type + "] Search returned incorrect number of rectangles - expected: " + expectedCount + " actual: " + resultCount);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RectDouble2D(jcog.tree.rtree.rect.RectDouble2D) Test(org.junit.jupiter.api.Test)

Aggregations

RectDouble2D (jcog.tree.rtree.rect.RectDouble2D)48 Test (org.junit.jupiter.api.Test)40 Stats (jcog.tree.rtree.util.Stats)17 Disabled (org.junit.jupiter.api.Disabled)6 Random (java.util.Random)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Double2D (jcog.tree.rtree.point.Double2D)2 CounterNode (jcog.tree.rtree.util.CounterNode)2