Class SpatialGrid<V>
- java.lang.Object
-
- org.jungrapht.visualization.spatial.AbstractSpatial<V,V>
-
- org.jungrapht.visualization.spatial.SpatialGrid<V>
-
- All Implemented Interfaces:
LayoutStateChange.Listener,LayoutVertexPositionChange.Listener<V>,TreeNode,Spatial<V,V>
public class SpatialGrid<V> extends AbstractSpatial<V,V> implements Spatial<V,V>, TreeNode, LayoutVertexPositionChange.Listener<V>
A Spatial Data Structure to optimize rendering performance. The SpatialGrid is used to determine which graph vertices are actually visible for a given rendering situation. Only the visible vertices are passed to the rendering pipeline. When used with Edges, only Edges with at least one visible endpoint are passed to the rendering pipeline.See SimpleGraphSpatialTest (jung-samples) for a rendering that exposes the internals of the SpatialGrid.
- Author:
- Tom Nelson
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jungrapht.visualization.spatial.Spatial
Spatial.NoOp<T,NT>
-
-
Field Summary
-
Fields inherited from class org.jungrapht.visualization.spatial.AbstractSpatial
layoutModel, pickShapes, rectangle
-
-
Constructor Summary
Constructors Constructor Description SpatialGrid(LayoutModel<V> layoutModel)Create an instanceSpatialGrid(LayoutModel<V> layoutModel, Rectangle2D bounds, int horizontalCount, int verticalCount)Create an instance
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()destroy the current spatial structureRectangle2DgetBounds()protected int[]getBoxIndex(double x, double y)given (x,y) in the coordinate system, get the boxX,boxY for the box that it is inprotected intgetBoxNumber(int[] boxXY)protected intgetBoxNumber(int boxX, int boxY)given the box x,y coordinates (not the coordinate system) return the box number (0,0) has box 0 (horizontalCount,horizontalCount) has box horizontalCount*verticalCount - 1protected intgetBoxNumberFromLocation(double x, double y)give a Point in the coordinate system, return the box number that contains itprotected intgetBoxNumberFromLocation(Point p)give a Point in the coordinate system, return the box number that contains itCollection<? extends TreeNode>getChildren()VgetClosestElement(double x, double y)VgetClosestElement(Point2D p)TreeNodegetContainingLeaf(Object element)Set<TreeNode>getContainingLeafs(double x, double y)Set<TreeNode>getContainingLeafs(Point2D p)List<Shape>getGrid()Lazily compute the gridCache if needed.static <V> List<Shape>getGrid(List<Shape> list, SpatialGrid<V> grid)Rectangle2DgetLayoutArea()Map<Integer,? extends Collection<V>>getMap()A Multimap of box number to Lists of vertices in that boxSet<V>getVisibleElements(Shape visibleArea)Given an area, return a collection of the vertices that are contained in it (the vertices that are contained in the boxes that intersect with the area)protected Collection<Integer>getVisibleTiles(Shape visibleArea)given a rectangular area and an offset, return the tile numbers that are contained in itvoidlayoutVertexPositionChanged(LayoutVertexPositionChange.Event<V> evt)voidlayoutVertexPositionChanged(LayoutVertexPositionChange.GraphEvent<V> evt)voidrecalculate()rebuild the data structurevoidrecalculate(Collection<V> vertices)Recalculate the contents of the Map of box number to contained VerticesvoidsetBounds(Rectangle2D bounds)Set the layoutSize of the spatial grid and recompute the box widths and heights.voidupdate(V vertex, Point location)update the location of a vertex in the map of box number to vertex lists-
Methods inherited from class org.jungrapht.visualization.spatial.AbstractSpatial
getClosest, getLayoutModel, getPickShapes, isActive, layoutStateChanged, setActive
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.jungrapht.visualization.layout.event.LayoutStateChange.Listener
layoutStateChanged
-
Methods inherited from interface org.jungrapht.visualization.spatial.Spatial
getLayoutModel, getPickShapes, getUnion, getUnion, isActive, setActive
-
-
-
-
Constructor Detail
-
SpatialGrid
public SpatialGrid(LayoutModel<V> layoutModel)
Create an instance- Parameters:
layoutModel-
-
SpatialGrid
public SpatialGrid(LayoutModel<V> layoutModel, Rectangle2D bounds, int horizontalCount, int verticalCount)
Create an instance- Parameters:
bounds- the area of the gridhorizontalCount- how many tiles in a rowverticalCount- how many tiles in a column
-
-
Method Detail
-
setBounds
public void setBounds(Rectangle2D bounds)
Set the layoutSize of the spatial grid and recompute the box widths and heights. null out the obsolete grid cache
-
getContainingLeafs
public Set<TreeNode> getContainingLeafs(Point2D p)
- Specified by:
getContainingLeafsin interfaceSpatial<V,V>- Parameters:
p- a point to search in the spatial structure- Returns:
- all leaf nodes that contain the passed point
-
getContainingLeafs
public Set<TreeNode> getContainingLeafs(double x, double y)
- Specified by:
getContainingLeafsin interfaceSpatial<V,V>- Parameters:
x- the x location to search fory- the y location to search for- Returns:
- all leaf nodes that contain the passed coordinates
-
getContainingLeaf
public TreeNode getContainingLeaf(Object element)
- Specified by:
getContainingLeafin interfaceSpatial<V,V>- Parameters:
element- element to search for- Returns:
- the leaf node that currently contains the element (not a spatial search)
-
getGrid
public static <V> List<Shape> getGrid(List<Shape> list, SpatialGrid<V> grid)
-
getGrid
public List<Shape> getGrid()
Lazily compute the gridCache if needed. The gridCache is a list of rectangles overlaying the layout area. They are numbered from 0 to horizontalCount*verticalCount-1
-
getMap
public Map<Integer,? extends Collection<V>> getMap()
A Multimap of box number to Lists of vertices in that box- Returns:
- the map of box numbers to contained vertices
-
getBoxNumber
protected int getBoxNumber(int boxX, int boxY)given the box x,y coordinates (not the coordinate system) return the box number (0,0) has box 0 (horizontalCount,horizontalCount) has box horizontalCount*verticalCount - 1- Parameters:
boxX- the x value in the box gridboxY- the y value in the box grid- Returns:
- the box number for boxX,boxY
-
getBoxNumber
protected int getBoxNumber(int[] boxXY)
- Parameters:
boxXY- the (x,y) in the grid coordinate system- Returns:
- the box number for that (x,y)
-
getBoxNumberFromLocation
protected int getBoxNumberFromLocation(Point p)
give a Point in the coordinate system, return the box number that contains it- Parameters:
p- a location in the coordinate system- Returns:
- the box number that contains the passed location
-
getBoxNumberFromLocation
protected int getBoxNumberFromLocation(double x, double y)give a Point in the coordinate system, return the box number that contains it- Parameters:
x- , y a location in the coordinate system- Returns:
- the box number that contains the passed location
-
getBoxIndex
protected int[] getBoxIndex(double x, double y)given (x,y) in the coordinate system, get the boxX,boxY for the box that it is in- Parameters:
x- a location in the coordinate systemy- a location in the coordinate system- Returns:
- a 2 dimensional array of int containing the box x and y coordinates
-
recalculate
public void recalculate()
Description copied from interface:Spatialrebuild the data structure- Specified by:
recalculatein interfaceSpatial<V,V>
-
clear
public void clear()
Description copied from interface:Spatialdestroy the current spatial structure
-
recalculate
public void recalculate(Collection<V> vertices)
Recalculate the contents of the Map of box number to contained Vertices- Parameters:
vertices- the collection of vertices to update in the structure
-
update
public void update(V vertex, Point location)
update the location of a vertex in the map of box number to vertex lists
-
getClosestElement
public V getClosestElement(Point2D p)
- Specified by:
getClosestElementin interfaceSpatial<V,V>- Parameters:
p- a point to search in the spatial structure- Returns:
- the closest element to the passed point
-
getClosestElement
public V getClosestElement(double x, double y)
- Specified by:
getClosestElementin interfaceSpatial<V,V>- Parameters:
x- coordinate of a point to search in the spatial structurey- coordinate of a point to search in the spatial structure- Returns:
- the closest element to the passed coordinates
-
getVisibleTiles
protected Collection<Integer> getVisibleTiles(Shape visibleArea)
given a rectangular area and an offset, return the tile numbers that are contained in it- Parameters:
visibleArea- the (possibly) non-rectangular area of interest- Returns:
- the tile numbers that intersect with the visibleArea
-
getVisibleElements
public Set<V> getVisibleElements(Shape visibleArea)
Given an area, return a collection of the vertices that are contained in it (the vertices that are contained in the boxes that intersect with the area)- Specified by:
getVisibleElementsin interfaceSpatial<V,V>- Parameters:
visibleArea- a shape projected on the grid- Returns:
- the vertices that should be visible
-
getLayoutArea
public Rectangle2D getLayoutArea()
- Specified by:
getLayoutAreain interfaceSpatial<V,V>- Returns:
- the layout area rectangle for this grid
-
getBounds
public Rectangle2D getBounds()
-
getChildren
public Collection<? extends TreeNode> getChildren()
- Specified by:
getChildrenin interfaceTreeNode
-
layoutVertexPositionChanged
public void layoutVertexPositionChanged(LayoutVertexPositionChange.Event<V> evt)
- Specified by:
layoutVertexPositionChangedin interfaceLayoutVertexPositionChange.Listener<V>
-
layoutVertexPositionChanged
public void layoutVertexPositionChanged(LayoutVertexPositionChange.GraphEvent<V> evt)
- Specified by:
layoutVertexPositionChangedin interfaceLayoutVertexPositionChange.Listener<V>
-
-