use of org.bboxdb.commons.math.BoundingBox in project bboxdb by jnidzwetzki.
the class TestFixedGrid method runExperiment.
/**
* Run this experiment
* @param cellGrid
*/
protected void runExperiment(final CellGrid cellGrid) {
final TupleFileReader tupleFile = new TupleFileReader(filename, format);
final Map<BoundingBox, Integer> bboxes = new HashMap<>();
final ExecutorService executor = ExecutorUtil.getBoundThreadPoolExecutor(10, 100);
tupleFile.addTupleListener(t -> {
executor.submit(() -> {
final Set<BoundingBox> intersectedBoxes = cellGrid.getAllInersectedBoundingBoxes(t.getBoundingBox());
synchronized (bboxes) {
for (final BoundingBox box : intersectedBoxes) {
if (bboxes.containsKey(box)) {
final int oldValue = bboxes.get(box);
bboxes.put(box, oldValue + 1);
} else {
bboxes.put(box, 1);
}
}
}
});
});
try {
System.out.println("# Processing tuples");
tupleFile.processFile();
executor.shutdown();
} catch (IOException e) {
System.err.println("Got an IOException during experiment: " + e);
System.exit(-1);
}
calculateResult(bboxes);
}
use of org.bboxdb.commons.math.BoundingBox in project bboxdb by jnidzwetzki.
the class TestKDTreeSplit method getSplitPosition.
/**
* Calculate the split position
* @param boundingBoxToSplit
* @return
*/
protected double getSplitPosition(final BoundingBox boundingBoxToSplit, final int dimension) {
final List<Double> pointSamples = new ArrayList<>();
final Set<Integer> takenSamples = new HashSet<>();
final List<BoundingBox> elementsToProcess = elements.get(boundingBoxToSplit);
final int numberOfElements = elementsToProcess.size();
final long numberOfSamples = (long) (numberOfElements / 100.0 * SAMPLING_SIZE);
double sample = 0;
// Try to find n samples (= 2n points)
while (pointSamples.size() < (2 * numberOfSamples)) {
sample++;
final int sampleId = ThreadLocalRandom.current().nextInt(numberOfElements);
if (takenSamples.contains(sampleId)) {
continue;
}
takenSamples.add(sampleId);
final BoundingBox bboxSample = elementsToProcess.get(sampleId);
if (bboxSample.getCoordinateLow(dimension) > boundingBoxToSplit.getCoordinateLow(dimension)) {
pointSamples.add(bboxSample.getCoordinateLow(dimension));
}
if (bboxSample.getCoordinateHigh(dimension) < boundingBoxToSplit.getCoordinateHigh(dimension)) {
pointSamples.add(bboxSample.getCoordinateHigh(dimension));
}
// Unable to find enough samples
if (sample > (50 * numberOfSamples)) {
break;
}
}
pointSamples.sort((b1, b2) -> Double.compare(b1, b2));
return pointSamples.get(pointSamples.size() / 2);
}
use of org.bboxdb.commons.math.BoundingBox in project bboxdb by jnidzwetzki.
the class TestKDTreeSplit method splitRegion.
/**
* Split the region
* @param sampleSize
* @param numberOfElements
* @return
*/
protected void splitRegion(final BoundingBox boundingBoxToSplit) {
final int parentBoxDimension = boxDimension.get(boundingBoxToSplit) % dataDimension;
final double splitPosition = getSplitPosition(boundingBoxToSplit, parentBoxDimension);
final BoundingBox leftBBox = boundingBoxToSplit.splitAndGetLeft(splitPosition, parentBoxDimension, true);
final BoundingBox rightBBox = boundingBoxToSplit.splitAndGetRight(splitPosition, parentBoxDimension, false);
// Data to redistribute
final List<BoundingBox> dataToRedistribute = elements.get(boundingBoxToSplit);
// Write the box dimension
boxDimension.put(leftBBox, parentBoxDimension + 1);
boxDimension.put(rightBBox, parentBoxDimension + 1);
// Insert new boxes and remove old one
elements.put(leftBBox, new ArrayList<>());
elements.put(rightBBox, new ArrayList<>());
elements.remove(boundingBoxToSplit);
dataToRedistribute.forEach(b -> {
if (leftBBox.overlaps(b)) {
elements.get(leftBBox).add(b);
}
if (rightBBox.overlaps(b)) {
elements.get(rightBBox).add(b);
}
});
}
use of org.bboxdb.commons.math.BoundingBox in project bboxdb by jnidzwetzki.
the class TestBoundingBox method testFromToString1.
/**
* Test the from and to string method
*/
@Test(timeout = 60000)
public void testFromToString1() {
final BoundingBox boundingBox1 = BoundingBox.FULL_SPACE;
final BoundingBox boundingBox2 = new BoundingBox(0d, 1d);
final BoundingBox boundingBox3 = new BoundingBox(-5d, 5d, -5d, 5d);
final BoundingBox boundingBox4 = new BoundingBox(Arrays.asList(new DoubleInterval(1, 2, false, false)));
final BoundingBox boundingBox5 = new BoundingBox(Arrays.asList(new DoubleInterval(1, 2, false, true)));
final BoundingBox boundingBox6 = new BoundingBox(Arrays.asList(new DoubleInterval(1, 2, true, false)));
final BoundingBox boundingBox7 = new BoundingBox(Arrays.asList(new DoubleInterval(1, 2, true, false), new DoubleInterval(1, 2, false, false)));
final BoundingBox boundingBox8 = BoundingBox.createFullCoveringDimensionBoundingBox(3);
Assert.assertEquals(boundingBox1, new BoundingBox(boundingBox1.toCompactString()));
Assert.assertEquals(boundingBox2, new BoundingBox(boundingBox2.toCompactString()));
Assert.assertEquals(boundingBox3, new BoundingBox(boundingBox3.toCompactString()));
Assert.assertEquals(boundingBox4, new BoundingBox(boundingBox4.toCompactString()));
Assert.assertEquals(boundingBox5, new BoundingBox(boundingBox5.toCompactString()));
Assert.assertEquals(boundingBox6, new BoundingBox(boundingBox6.toCompactString()));
Assert.assertEquals(boundingBox7, new BoundingBox(boundingBox7.toCompactString()));
Assert.assertEquals(boundingBox8, new BoundingBox(boundingBox8.toCompactString()));
}
use of org.bboxdb.commons.math.BoundingBox in project bboxdb by jnidzwetzki.
the class TestBoundingBox method mergeBoxesWithWrongDimension1.
/**
* Merge two boxes with wrong dimension
*/
@Test(expected = IllegalArgumentException.class)
public void mergeBoxesWithWrongDimension1() {
final BoundingBox boundingBox1 = new BoundingBox(1d, 3d, 1d, 3d);
final BoundingBox boundingBox2 = new BoundingBox(1d, 4d, 1d, 4d, 1d, 4d);
final BoundingBox boundingBoxResult3 = BoundingBox.getCoveringBox(boundingBox1, boundingBox2);
Assert.assertTrue(boundingBoxResult3 == null);
}
Aggregations