use of jcog.tree.rtree.rect.RectFloatND in project narchy by automenta.
the class RTreeNDTest method searchAll.
static void searchAll(int minDim, int maxDim, IntFunction<RectFloatND[]> generator) {
for (int dim = minDim; dim <= maxDim; dim++) {
final RectFloatND[] rects = generator.apply(dim);
Set<RectFloatND> input = new HashSet();
Collections.addAll(input, rects);
System.out.println("\tRectNDSearchAllTest[dim=" + dim + ']');
for (Spatialization.DefaultSplits type : Spatialization.DefaultSplits.values()) {
RTree<RectFloatND> rTree = RTree2DTest.createRectNDTree(2, 8, type);
for (int i = 0; i < rects.length; i++) {
rTree.add(rects[i]);
}
final RectFloatND searchRect = new RectFloatND(FloatND.fill(dim, Float.NEGATIVE_INFINITY), FloatND.fill(dim, Float.POSITIVE_INFINITY));
RectFloatND[] results = new RectFloatND[rects.length];
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 = rects.length;
// assertEquals("[" + type + "] Search returned incorrect search result count - expected: " + expectedCount + " actual: " + foundCount, expectedCount, foundCount);
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 number of rectangles - expected: " + expectedCount + " actual: " + resultCount);
Set<RectFloatND> output = new HashSet();
Collections.addAll(output, results);
// assertEquals( " same content", input, output);
Stats s = rTree.stats();
s.print(System.out);
// System.out.println("\t" + rTree.getRoot());
assertTrue(s.getMaxDepth() <= 8);
}
}
}
use of jcog.tree.rtree.rect.RectFloatND in project narchy by automenta.
the class RTree2DTest method generateRandomRectsWithOneDimensionRandomlyInfinite.
/**
* Generate 'count' random rectangles with fixed ranges.
*
* @param count - number of rectangles to generate
* @return array of generated rectangles
*/
public static RectFloatND[] generateRandomRectsWithOneDimensionRandomlyInfinite(int dimension, 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 maxXRange = 25;
Set<RectFloatND> s = new HashSet(count);
final RectFloatND[] rects = new RectFloatND[count];
for (int i = 0; i < count; ) {
float[] min = new float[dimension];
float[] max = new float[dimension];
for (int d = 0; d < dimension; d++) {
float x1 = min[d] = rand.nextInt(minX);
max[d] = x1 + rand.nextInt(maxXRange);
}
// one dimension (0) randomly infinite:
if (rand.nextBoolean()) {
int infDim = 0;
min[infDim] = Float.NEGATIVE_INFINITY;
max[infDim] = Float.POSITIVE_INFINITY;
}
RectFloatND m = new RectFloatND(min, max);
if (s.add(m))
rects[i++] = m;
}
return rects;
}
use of jcog.tree.rtree.rect.RectFloatND in project narchy by automenta.
the class RTreeNDTest method rectNDSearchTest2.
/**
* 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.
* 2D but using N-d impl
*/
@Test
public void rectNDSearchTest2() {
final int entryCount = 20;
System.out.println("rectNDSearchTest2");
for (Spatialization.DefaultSplits type : Spatialization.DefaultSplits.values()) {
RTree<RectFloatND> rTree = RTree2DTest.createRectNDTree(2, 8, type);
for (int i = 0; i < entryCount; i++) {
rTree.add(new RectFloatND(new FloatND(i, i), new FloatND(i + 3, i + 3)));
}
final RectFloatND searchRect = new RectFloatND(new FloatND(5, 5), new FloatND(10, 10));
List<RectFloatND> 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);
// If the order of nodes in the tree changes, this test may fail while returning the correct results.
Collections.sort(results);
for (int i = 0; i < resultCount; i++) {
assertTrue(results.get(i).min.coord(0) == i + 2 && results.get(i).min.coord(1) == i + 2 && results.get(i).max.coord(0) == i + 5 && results.get(i).max.coord(1) == i + 5, "Unexpected result found");
}
System.out.println("\t" + rTree.stats());
}
}
Aggregations