spatialindex

add SpatialIndex clear()

docs: QuadTree

wip: SpatialIndex
This commit is contained in:
Hannes Janetzek 2014-06-06 17:52:26 +02:00
parent c29c85db41
commit c0ece9dd7d
3 changed files with 26 additions and 19 deletions

View File

@ -9,14 +9,19 @@ import org.oscim.utils.quadtree.BoxTree.BoxItem;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/**
* Quad-tree with fixed extents.
* This implementation uses int bounding-boxes internally,
* so items extents should be greater than 1. FIXME tests this case
*/
public class QuadTree<T> extends BoxTree<BoxItem<T>, T> implements SpatialIndex<T> { public class QuadTree<T> extends BoxTree<BoxItem<T>, T> implements SpatialIndex<T> {
static final Logger log = LoggerFactory.getLogger(QuadTree.class);
public QuadTree(int extents, int maxDepth) { public QuadTree(int extents, int maxDepth) {
super(extents, maxDepth); super(extents, maxDepth);
} }
static final Logger log = LoggerFactory.getLogger(QuadTree.class);
final Pool<BoxItem<T>> boxPool = new Pool<BoxItem<T>>() { final Pool<BoxItem<T>> boxPool = new Pool<BoxItem<T>>() {
@Override @Override
protected BoxItem<T> createItem() { protected BoxItem<T> createItem() {
@ -67,10 +72,10 @@ public class QuadTree<T> extends BoxTree<BoxItem<T>, T> implements SpatialIndex<
} }
@Override @Override
public int search(Box bbox, SearchCb<T> cb, Object context) { public boolean search(Box bbox, SearchCb<T> cb, Object context) {
BoxItem<T> box = getBox(bbox); BoxItem<T> box = getBox(bbox);
search(box, cb, context); boolean finished = search(box, cb, context);
boxPool.release(box); boxPool.release(box);
return 0; return finished;
} }
} }

View File

@ -316,32 +316,24 @@ public class RTree<T> implements SpatialIndex<T>, Iterable<T> {
* @param a_context User context to pass as parameter to a_resultCallback * @param a_context User context to pass as parameter to a_resultCallback
* @return Returns the number of entries found * @return Returns the number of entries found
*/ */
public int search(double min[], double max[], SearchCb<T> cb, Object context) { public boolean search(double min[], double max[], SearchCb<T> cb, Object context) {
Rect r = getRect(); Rect r = getRect();
r.set(min, max); r.set(min, max);
/* NOTE: May want to return search result another way,
* perhaps returning the number of found elements here. */
//int[] foundCount = { 0 };
//search(mRoot, r, foundCount, cb, context);
searchStack(r, cb, context); searchStack(r, cb, context);
releaseRect(r); releaseRect(r);
return 0; //foundCount[0]; return true;
} }
public int search(Box bbox, SearchCb<T> cb, Object context) { public boolean search(Box bbox, SearchCb<T> cb, Object context) {
Rect r = getRect(); Rect r = getRect();
r.set(bbox); r.set(bbox);
/* NOTE: May want to return search result another way,
* perhaps returning the number of found elements here. */
//int[] foundCount = { 0 };
//search(mRoot, r, foundCount, cb, context);
searchStack(r, cb, context); searchStack(r, cb, context);
releaseRect(r); releaseRect(r);
return 0; //foundCount[0]; return true;
} }
public List<T> search(Box bbox, List<T> results) { public List<T> search(Box bbox, List<T> results) {
@ -385,7 +377,7 @@ public class RTree<T> implements SpatialIndex<T>, Iterable<T> {
/** /**
* Remove all entries from tree. * Remove all entries from tree.
*/ */
public void removeAll() { public void clear() {
/* Delete all existing nodes */ /* Delete all existing nodes */
reset(); reset();

View File

@ -6,6 +6,14 @@ import org.oscim.core.Box;
public interface SpatialIndex<T> { public interface SpatialIndex<T> {
public interface SearchCb<T> { public interface SearchCb<T> {
/**
*
* TODO should be able to return 'continue', 'stop',
* 'remove-current'
*
* @return false to stop search
*/
boolean call(T item, Object context); boolean call(T item, Object context);
} }
@ -15,7 +23,9 @@ public interface SpatialIndex<T> {
public List<T> search(Box bbox, List<T> results); public List<T> search(Box bbox, List<T> results);
public int search(Box bbox, SearchCb<T> cb, Object context); public boolean search(Box bbox, SearchCb<T> cb, Object context);
public int size(); public int size();
public void clear();
} }