2019-10-22 07:15:35 +02:00
|
|
|
/*
|
2018-12-23 03:07:31 +05:30
|
|
|
* The MIT License
|
2019-10-12 20:05:54 +03:00
|
|
|
* Copyright © 2014-2019 Ilkka Seppälä
|
2018-12-23 03:07:31 +05:30
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2019-10-22 07:15:35 +02:00
|
|
|
|
2018-12-23 03:07:31 +05:30
|
|
|
package com.iluwatar.spatialpartition;
|
|
|
|
|
2020-01-04 22:06:08 +05:30
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
2018-12-23 03:07:31 +05:30
|
|
|
import java.util.ArrayList;
|
2020-01-04 22:06:08 +05:30
|
|
|
import java.util.Collection;
|
2018-12-23 03:07:31 +05:30
|
|
|
import java.util.Hashtable;
|
|
|
|
import java.util.Random;
|
2020-01-04 22:06:08 +05:30
|
|
|
import java.util.stream.Collectors;
|
2018-12-23 03:07:31 +05:30
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Testing QuadTree class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class QuadTreeTest {
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void queryTest() {
|
2020-01-04 22:06:08 +05:30
|
|
|
var points = new ArrayList<Point>();
|
|
|
|
var rand = new Random();
|
2018-12-23 03:07:31 +05:30
|
|
|
for (int i = 0; i < 20; i++) {
|
2020-01-04 22:06:08 +05:30
|
|
|
var p = new Bubble(rand.nextInt(300), rand.nextInt(300), i, rand.nextInt(2) + 1);
|
2018-12-23 03:07:31 +05:30
|
|
|
points.add(p);
|
|
|
|
}
|
2020-01-04 22:06:08 +05:30
|
|
|
var field = new Rect(150, 150, 300, 300); //size of field
|
|
|
|
var queryRange = new Rect(70, 130, 100, 100); //result = all points lying in this rectangle
|
2018-12-23 03:07:31 +05:30
|
|
|
//points found in the query range using quadtree and normal method is same
|
2020-01-04 22:06:08 +05:30
|
|
|
var points1 = QuadTreeTest.quadTreeTest(points, field, queryRange);
|
|
|
|
var points2 = QuadTreeTest.verify(points, queryRange);
|
|
|
|
assertEquals(points1, points2);
|
2018-12-23 03:07:31 +05:30
|
|
|
}
|
|
|
|
|
2020-01-04 22:06:08 +05:30
|
|
|
static Hashtable<Integer, Point> quadTreeTest(Collection<Point> points, Rect field, Rect queryRange) {
|
2018-12-23 03:07:31 +05:30
|
|
|
//creating quadtree and inserting all points
|
2020-01-04 22:06:08 +05:30
|
|
|
var qTree = new QuadTree(queryRange, 4);
|
|
|
|
points.forEach(qTree::insert);
|
2018-12-23 03:07:31 +05:30
|
|
|
|
2020-01-04 22:06:08 +05:30
|
|
|
return qTree
|
|
|
|
.query(field, new ArrayList<>())
|
|
|
|
.stream()
|
|
|
|
.collect(Collectors.toMap(p -> p.id, p -> p, (a, b) -> b, Hashtable::new));
|
2018-12-23 03:07:31 +05:30
|
|
|
}
|
|
|
|
|
2020-01-04 22:06:08 +05:30
|
|
|
static Hashtable<Integer, Point> verify(Collection<Point> points, Rect queryRange) {
|
|
|
|
return points.stream()
|
|
|
|
.filter(queryRange::contains)
|
|
|
|
.collect(Collectors.toMap(point -> point.id, point -> point, (a, b) -> b, Hashtable::new));
|
2018-12-23 03:07:31 +05:30
|
|
|
}
|
|
|
|
}
|