Package de.extio.spacecraft.shared.world
Class QuadTree<T extends SpatialIndex2Capable>
- java.lang.Object
-
- de.extio.spacecraft.shared.world.QuadTree<T>
-
- All Implemented Interfaces:
SpatialIndex2D<T>
,AutoCloseable
,Iterable<T>
public final class QuadTree<T extends SpatialIndex2Capable> extends Object implements SpatialIndex2D<T>
This is the preferred SpatialIndex2D implementation to index bigger amounts of spatial data and query them efficiently. You can consider using GridIndex2D instead for small grids with one-dimensional objects only. A quadtree is a tree data structure in which each internal node has exactly four children. Quadtrees are the two-dimensional analog of octrees and are most often used to partition a two-dimensional space by recursively subdividing it into four quadrants or regions.
-
-
Field Summary
-
Fields inherited from interface de.extio.spacecraft.shared.world.SpatialIndex2D
SESSION
-
-
Constructor Summary
Constructors Constructor Description QuadTree()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(T obj)
Adds an object to the indexvoid
clear()
Clears but keeps internal state allocated.void
close()
Clears and frees internal statevoid
executeAt(CoordI2 position, Consumer<T> consumer, Predicate<T> filter)
Executes a consumer for all objects at a given position.List<T>
find(CoordI2 position, CoordI2 dimension, Predicate<T> filter)
Queries all object intersecting with a given area.T
findFirstAt(CoordI2 position, Predicate<T> filter)
Returns the first object at a given position.void
forEach(Consumer<? super T> action)
boolean
intersects(RectI2 rect0, RectI2 rect1)
boolean
intersects(de.extio.spacecraft.shared.world.QuadTree.Node<T> node, RectI2 rect)
boolean
intersects(T obj, RectI2 rect)
Iterator<T>
iterator()
void
move(T obj, CoordI2 oldPosition, CoordI2 oldDimension)
Moves an indexed object to a new location.void
remove(T obj)
Removes an object from the index-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
spliterator
-
Methods inherited from interface de.extio.spacecraft.shared.world.SpatialIndex2D
executeAt, find, find, find, findFirstAt
-
-
-
-
Method Detail
-
add
public void add(T obj)
Description copied from interface:SpatialIndex2D
Adds an object to the index- Specified by:
add
in interfaceSpatialIndex2D<T extends SpatialIndex2Capable>
-
remove
public void remove(T obj)
Description copied from interface:SpatialIndex2D
Removes an object from the index- Specified by:
remove
in interfaceSpatialIndex2D<T extends SpatialIndex2Capable>
-
move
public void move(T obj, CoordI2 oldPosition, CoordI2 oldDimension)
Description copied from interface:SpatialIndex2D
Moves an indexed object to a new location. Implementations are usually faster than doing separate remove() and add() operations- Specified by:
move
in interfaceSpatialIndex2D<T extends SpatialIndex2Capable>
-
find
public List<T> find(CoordI2 position, CoordI2 dimension, Predicate<T> filter)
Description copied from interface:SpatialIndex2D
Queries all object intersecting with a given area. Objects can be pre-filtered which is usually faster than returning them first and then filter in your own code.- Specified by:
find
in interfaceSpatialIndex2D<T extends SpatialIndex2Capable>
-
findFirstAt
public T findFirstAt(CoordI2 position, Predicate<T> filter)
Description copied from interface:SpatialIndex2D
Returns the first object at a given position. Objects can be pre-filtered which is usually faster than returning them first and then filter in your own code.- Specified by:
findFirstAt
in interfaceSpatialIndex2D<T extends SpatialIndex2Capable>
-
executeAt
public void executeAt(CoordI2 position, Consumer<T> consumer, Predicate<T> filter)
Description copied from interface:SpatialIndex2D
Executes a consumer for all objects at a given position. Objects can be pre-filtered which is usually faster than returning them first and then filter in your own code.- Specified by:
executeAt
in interfaceSpatialIndex2D<T extends SpatialIndex2Capable>
-
iterator
public Iterator<T> iterator()
- Specified by:
iterator
in interfaceIterable<T extends SpatialIndex2Capable>
-
forEach
public void forEach(Consumer<? super T> action)
- Specified by:
forEach
in interfaceIterable<T extends SpatialIndex2Capable>
-
close
public void close()
Description copied from interface:SpatialIndex2D
Clears and frees internal state- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceSpatialIndex2D<T extends SpatialIndex2Capable>
-
clear
public void clear()
Description copied from interface:SpatialIndex2D
Clears but keeps internal state allocated. Optimization for re-use.- Specified by:
clear
in interfaceSpatialIndex2D<T extends SpatialIndex2Capable>
-
intersects
public boolean intersects(de.extio.spacecraft.shared.world.QuadTree.Node<T> node, RectI2 rect)
-
-