Search in sources :

Example 36 with RectDouble2D

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

the class RTreeNDTest method treeStructureStatsTest.

/**
 * Collect stats making the structure of trees of each split type
 * more visible.
 */
@Disabled
public // This test ignored because output needs to be manually evaluated.
void treeStructureStatsTest() {
    final int entryCount = 50_000;
    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]);
        }
        Stats stats = rTree.stats();
        stats.print(System.out);
    }
}
Also used : Stats(jcog.tree.rtree.util.Stats) RectDouble2D(jcog.tree.rtree.rect.RectDouble2D) Disabled(org.junit.jupiter.api.Disabled)

Example 37 with RectDouble2D

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

the class RTreeNDTest method treeSearchStatsTest.

/**
 * Do a search and collect stats on how many nodes we hit and how many
 * bounding boxes we had to evaluate to get all the results.
 * <p>
 * Preliminary findings:
 * - Evals for QUADRATIC tree increases with size of the search bounding box.
 * - QUADRATIC seems to be ideal for small search bounding boxes.
 */
@Disabled
public // This test ignored because output needs to be manually evaluated.
void treeSearchStatsTest() {
    final int entryCount = 5000;
    final RectDouble2D[] rects = generateRandomRects(entryCount);
    for (int j = 0; j < 6; j++) {
        for (Spatialization.DefaultSplits type : Spatialization.DefaultSplits.values()) {
            RTree<RectDouble2D> rTree = createRectDouble2DTree(2, 12, type);
            for (int i = 0; i < rects.length; i++) {
                rTree.add(rects[i]);
            }
            rTree.instrumentTree();
            final RectDouble2D searchRect = new RectDouble2D(100, 100, 120, 120);
            RectDouble2D[] results = new RectDouble2D[entryCount];
            final long start = System.nanoTime();
            int foundCount = rTree.containedToArray(searchRect, results);
            final long end = System.nanoTime() - start;
            CounterNode<RectDouble2D> root = (CounterNode<RectDouble2D>) rTree.root();
            // System.out.println("[" + type + "] searched " + root.containingCount + " nodes, returning " + foundCount + " entries");
            System.out.println("[" + type + "] evaluated " + CounterNode.bboxEvalCount + " b-boxes, returning " + foundCount + " entries");
            System.out.println("Run was " + end / 1000 + " us");
        }
    }
}
Also used : RectDouble2D(jcog.tree.rtree.rect.RectDouble2D) CounterNode(jcog.tree.rtree.util.CounterNode) Disabled(org.junit.jupiter.api.Disabled)

Example 38 with RectDouble2D

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

the class RTreeNDTest method RectDouble2DSearchTest.

/**
 * Use an small bounding box to ensure that only expected rectangles are returned.
 * Verifies the count returned from search AND the number of rectangles results.
 */
@Test
public void RectDouble2DSearchTest() {
    final int entryCount = 20;
    for (Spatialization.DefaultSplits type : Spatialization.DefaultSplits.values()) {
        RTree<RectDouble2D> rTree = createRectDouble2DTree(2, 8, type);
        for (int i = 0; i < entryCount; i++) {
            rTree.add(new RectDouble2D(i, i, i + 3, i + 3));
        }
        final RectDouble2D searchRect = new RectDouble2D(5, 5, 10, 10);
        RectDouble2D[] results = new RectDouble2D[3];
        final int foundCount = rTree.containedToArray(searchRect, results);
        int resultCount = 0;
        for (int i = 0; i < results.length; i++) {
            if (results[i] != null) {
                resultCount++;
            }
        }
        final int expectedCount = 3;
        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);
        Arrays.sort(results);
        // If the order of nodes in the tree changes, this test may fail while returning the correct results.
        for (int i = 0; i < resultCount; i++) {
            assertTrue(Util.equals(results[i].min.x, (double) (i + 5), Spatialization.EPSILON) && Util.equals(results[i].min.y, (double) (i + 5), Spatialization.EPSILON) && Util.equals(results[i].max.x, (double) (i + 8), Spatialization.EPSILON) && Util.equals(results[i].max.y, (double) (i + 8), Spatialization.EPSILON), "Unexpected result found:" + results[i]);
        }
    }
}
Also used : RectDouble2D(jcog.tree.rtree.rect.RectDouble2D) Test(org.junit.jupiter.api.Test)

Example 39 with RectDouble2D

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

the class RTreeNDTest method treeSingleRemovalTest.

@Test
public void treeSingleRemovalTest() {
    final RTree<RectDouble2D> rTree = createRectDouble2DTree(Spatialization.DefaultSplits.QUADRATIC);
    RectDouble2D rect = new RectDouble2D(0, 0, 2, 2);
    rTree.add(rect);
    assertTrue(rTree.size() > 0, "Did not add HyperRect to Tree");
    rTree.remove(rect);
    assertTrue(rTree.size() == 0, "Did not remove HyperRect from Tree");
    rTree.add(rect);
    assertTrue(rTree.size() > 0, "Tree nulled out and could not add HyperRect back in");
}
Also used : RectDouble2D(jcog.tree.rtree.rect.RectDouble2D) Test(org.junit.jupiter.api.Test)

Example 40 with RectDouble2D

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

the class RTreeNDTest method testAddsubtreeWithSideTree.

@Test
public void testAddsubtreeWithSideTree() {
    final RTree<RectDouble2D> rTree = createRectDouble2DTree(3, 6, Spatialization.DefaultSplits.QUADRATIC);
    final RectDouble2D search;
    rTree.add(new RectDouble2D(2, 2, 4, 4));
    rTree.add(search = new RectDouble2D(5, 2, 6, 3));
    // now make sure root node is a branch
    for (int i = 0; i < 5; i++) {
        rTree.add(new RectDouble2D(3.0 - 1.0 / (10.0 + i), 3.0 - 1.0 / (10.0 + i), 3.0 + 1.0 / (10.0 + i), 3.0 + 1.0 / (10.0 + i)));
    }
    // add subtree/child on first rectangle - fully contained
    rTree.add(new RectDouble2D(2.5, 2.5, 3.5, 3.5));
    assertEquals(8, rTree.size());
    final AtomicInteger hitCount = new AtomicInteger();
    // but 5, 2, 6, 3 must still be found!
    rTree.whileEachContaining(search, (closure) -> {
        hitCount.incrementAndGet();
        return true;
    });
    assertEquals(1, hitCount.get());
}
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