Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/main/java/org/gephi/graph/api/SpatialIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.gephi.graph.api;

import java.util.function.Predicate;

/**
* Query the (quadtree-based) index based on the given rectangle area.
* <p>
Expand Down Expand Up @@ -42,6 +44,15 @@ public interface SpatialIndex {
*/
NodeIterable getNodesInArea(Rect2D rect);

/**
* Returns the nodes in the given area, filtered by the given predicate.
*
* @param rect area to query
* @param predicate filter predicate
* @return nodes in the area
*/
NodeIterable getNodesInArea(Rect2D rect, Predicate<? super Node> predicate);

/**
* Returns the nodes in the given area using a faster, but approximate method.
* <p>
Expand All @@ -53,6 +64,19 @@ public interface SpatialIndex {
*/
NodeIterable getApproximateNodesInArea(Rect2D rect);

/**
* Returns the nodes in the given area using a faster, but approximate method,
* filtered by the given predicate.
* <p>
* All nodes in the provided area are guaranteed to be returned, but some nodes
* outside the area may also be returned.
*
* @param rect area to query
* @param predicate filter predicate
* @return nodes in the area
*/
NodeIterable getApproximateNodesInArea(Rect2D rect, Predicate<? super Node> predicate);

/**
* Returns the edges in the given area. Edges may be returned twice.
*
Expand All @@ -61,6 +85,16 @@ public interface SpatialIndex {
*/
EdgeIterable getEdgesInArea(Rect2D rect);

/**
* Returns the edges in the given area, filtered by the given predicate. Edges
* may be returned twice.
*
* @param rect area to query
* @param predicate filter predicate
* @return edges in the area
*/
EdgeIterable getEdgesInArea(Rect2D rect, Predicate<? super Edge> predicate);

/**
* Returns the edges in the given area using a faster, but approximate method.
* <p>
Expand All @@ -72,6 +106,19 @@ public interface SpatialIndex {
*/
EdgeIterable getApproximateEdgesInArea(Rect2D rect);

/**
* Returns the edges in the given area using a faster, but approximate method,
* filtered by the given predicate.
* <p>
* All edges in the provided area are guaranteed to be returned, but some edges
* outside the area may also be returned. Edges may also be returned twice.
*
* @param rect area to query
* @param predicate filter predicate
* @return edges in the area
*/
EdgeIterable getApproximateEdgesInArea(Rect2D rect, Predicate<? super Edge> predicate);

/**
* Returns the bounding rectangle that contains all nodes in the graph. The
* boundaries are calculated based on each node's position and size.
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/org/gephi/graph/impl/GraphViewDecorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Spliterator;
import java.util.ConcurrentModificationException;
import java.util.function.Consumer;
import java.util.function.Predicate;
import org.gephi.graph.api.DirectedSubgraph;
import org.gephi.graph.api.Edge;
import org.gephi.graph.api.EdgeIterable;
Expand Down Expand Up @@ -874,6 +875,14 @@ public NodeIterable getNodesInArea(Rect2D rect) {
return graphStore.spatialIndex.getNodesInArea(rect, view::containsNode);
}

@Override
public NodeIterable getNodesInArea(Rect2D rect, Predicate<? super Node> predicate) {
if (graphStore.spatialIndex == null) {
throw new UnsupportedOperationException("Spatial index is disabled (from Configuration)");
}
return graphStore.spatialIndex.getNodesInArea(rect, (node) -> view.containsNode(node) && predicate.test(node));
}

@Override
public NodeIterable getApproximateNodesInArea(Rect2D rect) {
if (graphStore.spatialIndex == null) {
Expand All @@ -882,6 +891,15 @@ public NodeIterable getApproximateNodesInArea(Rect2D rect) {
return graphStore.spatialIndex.getApproximateNodesInArea(rect, view::containsNode);
}

@Override
public NodeIterable getApproximateNodesInArea(Rect2D rect, Predicate<? super Node> predicate) {
if (graphStore.spatialIndex == null) {
throw new UnsupportedOperationException("Spatial index is disabled (from Configuration)");
}
return graphStore.spatialIndex
.getApproximateNodesInArea(rect, (node) -> view.containsNode(node) && predicate.test(node));
}

@Override
public EdgeIterable getEdgesInArea(Rect2D rect) {
if (graphStore.spatialIndex == null) {
Expand All @@ -890,6 +908,14 @@ public EdgeIterable getEdgesInArea(Rect2D rect) {
return graphStore.spatialIndex.getEdgesInArea(rect, view::containsEdge);
}

@Override
public EdgeIterable getEdgesInArea(Rect2D rect, Predicate<? super Edge> predicate) {
if (graphStore.spatialIndex == null) {
throw new UnsupportedOperationException("Spatial index is disabled (from Configuration)");
}
return graphStore.spatialIndex.getEdgesInArea(rect, (edge) -> view.containsEdge(edge) && predicate.test(edge));
}

@Override
public EdgeIterable getApproximateEdgesInArea(Rect2D rect) {
if (graphStore.spatialIndex == null) {
Expand All @@ -898,6 +924,15 @@ public EdgeIterable getApproximateEdgesInArea(Rect2D rect) {
return graphStore.spatialIndex.getApproximateEdgesInArea(rect, view::containsEdge);
}

@Override
public EdgeIterable getApproximateEdgesInArea(Rect2D rect, Predicate<? super Edge> predicate) {
if (graphStore.spatialIndex == null) {
throw new UnsupportedOperationException("Spatial index is disabled (from Configuration)");
}
return graphStore.spatialIndex
.getApproximateEdgesInArea(rect, (edge) -> view.containsEdge(edge) && predicate.test(edge));
}

@Override
public Rect2D getBoundaries() {
if (graphStore.spatialIndex == null) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/gephi/graph/impl/SpatialIndexImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,22 @@ public void spatialIndexReadUnlock() {
nodesTree.readUnlock();
}

@Override
public NodeIterable getNodesInArea(Rect2D rect, Predicate<? super Node> predicate) {
return nodesTree.getNodes(rect, false, predicate);
}

@Override
public NodeIterable getApproximateNodesInArea(Rect2D rect, Predicate<? super Node> predicate) {
return nodesTree.getNodes(rect, true, predicate);
}

@Override
public EdgeIterable getEdgesInArea(Rect2D rect, Predicate<? super Edge> predicate) {
return nodesTree.getEdges(rect, false, predicate);
}

@Override
public EdgeIterable getApproximateEdgesInArea(Rect2D rect, Predicate<? super Edge> predicate) {
return nodesTree.getEdges(rect, true, predicate);
}
Expand Down