Search in sources :

Example 21 with RectDouble2D

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

the class Rect2DTest method centroidTest.

@Test
public void centroidTest() {
    RectDouble2D rect = new RectDouble2D(0, 0, 4, 3);
    HyperPoint centroid = rect.center();
    double x = centroid.coord(0);
    double y = centroid.coord(1);
    assertTrue(x == 2.0d, "Bad X-coord of centroid - expected " + 2.0 + " but was " + x);
    assertTrue(y == 1.5d, "Bad Y-coord of centroid - expected " + 1.5 + " but was " + y);
}
Also used : RectDouble2D(jcog.tree.rtree.rect.RectDouble2D) Test(org.junit.jupiter.api.Test)

Example 22 with RectDouble2D

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

the class Rect2DTest method costTest.

@Test
public void costTest() {
    RectDouble2D rect = new RectDouble2D(0, 0, 4, 3);
    double cost = rect.cost();
    assertTrue(cost == 12.0d, "Bad cost - expected " + 12.0 + " but was " + cost);
}
Also used : RectDouble2D(jcog.tree.rtree.rect.RectDouble2D) Test(org.junit.jupiter.api.Test)

Example 23 with RectDouble2D

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

the class RTree2DTest method generateRandomRects.

/**
 * Generate 'count' random rectangles with fixed ranges.
 * The returned array will be free of duplicates
 *
 * @param count - number of rectangles to generate
 * @return array of generated rectangles
 */
public 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);
    Set<RectDouble2D> added = new HashSet(count);
    final RectDouble2D[] rects = new RectDouble2D[count];
    for (int i = 0; i < count; ) {
        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);
        RectDouble2D next = new RectDouble2D(x1, y1, x2, y2);
        if (added.add(next))
            rects[i++] = next;
    }
    return rects;
}
Also used : RectDouble2D(jcog.tree.rtree.rect.RectDouble2D)

Example 24 with RectDouble2D

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

the class RTree2DTest method rect2DSearchTest.

/**
 * 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 rect2DSearchTest() {
    final int entryCount = 20;
    for (Spatialization.DefaultSplits type : Spatialization.DefaultSplits.values()) {
        RTree<RectDouble2D> rTree = createRect2DTree(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);
        List<RectDouble2D> results = new ArrayList();
        rTree.whileEachIntersecting(searchRect, results::add);
        int resultCount = 0;
        for (int i = 0; i < results.size(); i++) {
            if (results.get(i) != null) {
                resultCount++;
            }
        }
        final int expectedCount = 9;
        // assertEquals("[" + type + "] Search returned incorrect search result count - expected: " + expectedCount + " actual: " + foundCount, expectedCount, foundCount);
        assertEquals(expectedCount, resultCount, "[" + type + "] Search returned incorrect number of rectangles - expected: " + expectedCount + " actual: " + resultCount);
        Collections.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(results.get(i).min.x == i + 2 && results.get(i).min.y == i + 2 && results.get(i).max.x == i + 5 && results.get(i).max.y == i + 5, "Unexpected result found");
        }
    }
}
Also used : RectDouble2D(jcog.tree.rtree.rect.RectDouble2D) Test(org.junit.jupiter.api.Test)

Example 25 with RectDouble2D

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

the class RTree2DTest method rect2DSearchAllTest.

/**
 * 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 rect2DSearchAllTest() {
    final int entryCount = 10000;
    final RectDouble2D[] rects = generateRandomRects(entryCount);
    for (Spatialization.DefaultSplits type : Spatialization.DefaultSplits.values()) {
        RTree<RectDouble2D> rTree = createRect2DTree(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 int expectedCount = entryCount;
        assertTrue(Math.abs(expectedCount - foundCount) < 10, "[" + type + "] Search returned incorrect search result count - expected: " + expectedCount + " actual: " + foundCount);
        assertTrue(Math.abs(expectedCount - resultCount) < 10, "[" + type + "] Search returned incorrect search result count - expected: " + expectedCount + " actual: " + foundCount);
    }
}
Also used : 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