1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.g2d.nodes.spatial;
14 import java.awt.Graphics2D;
15 import java.awt.RenderingHints;
16 import java.awt.Shape;
17 import java.awt.geom.AffineTransform;
18 import java.awt.geom.Rectangle2D;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Properties;
26 import org.simantics.scenegraph.g2d.G2DParentNode;
27 import org.simantics.scenegraph.g2d.IG2DNode;
28 import org.simantics.scenegraph.g2d.events.Event;
29 import org.simantics.scenegraph.g2d.events.EventTypes;
30 import org.simantics.scenegraph.g2d.events.INodeEventHandlerProvider;
31 import org.simantics.scenegraph.g2d.events.NodeEventHandler;
32 import org.simantics.scenegraph.utils.GeometryUtils;
33 import org.simantics.scenegraph.utils.NodeUtil;
35 import com.infomatiq.jsi.Rectangle;
36 import com.infomatiq.jsi.rtree.RTree;
38 import gnu.trove.TIntObjectHashMap;
39 import gnu.trove.TIntProcedure;
42 * A G2D scene graph node that spatially decomposes all of its immediate child
43 * nodes into an R-Tree structure to optimize the painting of its direct child
44 * based on whether they are visible in the current clip-bounds or not.
47 * The clipping boundary, i.e. the current viewport is retrieved from
48 * {@link Graphics2D#getClip()}.
52 * {@link #setDirty()} will mark the whole r-tree invalid meaning it will be
53 * reconstructed from scratch when it is needed again.
58 * <li>Add interface for marking children added/removed/changed to optimize
60 * <li>With incremental updating, make sure that the r-tree is rebuilt every
61 * once in a while. Updates will most likely cause r-tree performance to slowly
65 * @author Tuukka Lehtonen
67 public class RTreeNode extends G2DParentNode implements INodeEventHandlerProvider {
69 private static final boolean DISABLE_RTREE = false;
71 private static final boolean DEBUG_SHRINK_CLIP_RECT = false;
73 private static final long serialVersionUID = 8988645670981494042L;
76 * A container for everything related to the R-tree based spatial
79 private static class Tree {
80 public final RTree rtree;
83 * Bounds of the whole R-tree.
85 public final Rectangle bounds;
90 public final ArrayList<IG2DNode> boundlessNodes;
93 * Map from R-tree rectangle ID's to scene graph nodes.
95 public final TIntObjectHashMap<IG2DNode> toNodes;
98 * Map from R-tree rectangle ID's to current scene graph node bounds.
99 * This prevents the need to unnecessarily recalculate the node bounds
100 * in the node selection loop.
102 public final TIntObjectHashMap<Rectangle2D> toBounds;
104 public Tree(RTree rtree, ArrayList<IG2DNode> boundlessNodes, TIntObjectHashMap<IG2DNode> toNodes, TIntObjectHashMap<Rectangle2D> toBounds) {
106 this.bounds = rtree.getBounds();
107 this.boundlessNodes = boundlessNodes;
108 this.toNodes = toNodes;
109 this.toBounds = toBounds;
113 private transient volatile Tree tree = null;
114 private transient ArrayList<IG2DNode> collected = new ArrayList<IG2DNode>();
115 private transient Set<IG2DNode> simplified = new HashSet<IG2DNode>();
118 public void render(Graphics2D g) {
124 AffineTransform ot = null;
125 if (!transform.isIdentity()) {
126 ot = g.getTransform();
127 g.transform(transform);
131 // Get transformed clip bounds
132 Shape clipShape = g.getClip();
133 Rectangle2D bounds = null;
134 if (clipShape instanceof Rectangle2D)
135 bounds = (Rectangle2D) clipShape;
136 else if (clipShape != null)
137 bounds = clipShape.getBounds2D();
139 // Calculate values to optimize rendering based on view scale
140 final double viewScale = GeometryUtils.getScale(g.getTransform());
141 if (Math.abs(viewScale) <= Double.MIN_VALUE)
143 final double unitsPerPixel = 1.0 / viewScale;
144 //final double simplificationUnitsPerPixelLimit = 4*unitsPerPixel;
145 //System.out.println("view scale: " + viewScale + " - " + unitsPerPixel + " - " + simplificationUnitsPerPixelLimit);
147 // A bit of added margin for the clipping to
148 // prevent the system from clipping symbols too
149 // early in case they have out-of-bounds decorations.
151 GeometryUtils.expandRectangle(bounds, 5);
154 if (DEBUG_SHRINK_CLIP_RECT) {
155 GeometryUtils.expandRectangle(bounds, -100.0*unitsPerPixel);
159 // Make sure that a spatial decomposition exists.
160 final Tree tree = getSpatialDecomposition();
162 if (bounds == null || tree.bounds == null || containedBy(tree.bounds, bounds)) {
163 // Direct render if no pruning can be done.
164 for (IG2DNode node : getSortedNodes()) {
165 if (node.validate()) {
170 final Object render = g.getRenderingHint(RenderingHints.KEY_RENDERING);
174 if (tree.boundlessNodes != null) {
175 for (int i = 0, n = tree.boundlessNodes.size(); i < n; ++i)
176 collected.add(tree.boundlessNodes.get(i));
179 tree.rtree.intersects(toRectangle(bounds), new TIntProcedure() {
182 public boolean execute(int value) {
183 //System.out.println("exec: " + value);
184 IG2DNode node = tree.toNodes.get(value);
185 //System.out.println(" node: " + node);
186 if (node == null || !node.validate())
189 if (render != RenderingHints.VALUE_RENDER_QUALITY) {
190 Rectangle2D r = tree.toBounds.get(value);
194 double w = r.getWidth();
195 double h = r.getHeight();
197 //System.out.println("size: " + w + " x " + h);
198 if (w < unitsPerPixel && h < unitsPerPixel) {
199 //System.out.println("Skipping node: " + node);
203 // if (w < simplificationUnitsPerPixelLimit && h < simplificationUnitsPerPixelLimit) {
204 // //System.out.println("simplifying node: " + node);
205 // simplified.add(node);
213 Collections.sort(collected, G2DParentNode.G2DNODE_Z_COMPARATOR);
214 //System.out.println("rendering " + collected.size() + "/" + getNodeCount() + " children, " + simplified.size() + " as simplified");
215 if (simplified.isEmpty()) {
216 for (IG2DNode node : collected)
220 for (IG2DNode node : collected) {
221 if (node.validate()) {
222 if (simplified.contains(node)) {
223 g.draw(node.getBoundsInLocal());
233 // // !DEBUG / PROFILING!
234 // g.setStroke(new BasicStroke(0.25f));
235 // drawTree(g, tree.rtree);
243 // private void drawTree(final Graphics2D g, RTree rtree) {
244 // final Rectangle2D r = new Rectangle2D.Double();
245 // tree.rtree.traverse(new IRectangleTraverseProcedure() {
247 // public void call(double x0, double y0, double x1, double y1, int level, Object value) {
248 // r.setFrameFromDiagonal(x0, y0, x1, y1);
249 // g.setColor(getLevelColor(level));
253 // private Color getLevelColor(int level) {
255 // case 0: return Color.DARK_GRAY;
256 // case 1: return Color.RED;
257 // case 2: return Color.PINK;
258 // case 3: return Color.BLUE;
259 // case 4: return Color.ORANGE;
260 // default: return Color.BLACK;
267 public void setDirty() {
271 private Tree getSpatialDecomposition() {
280 Properties props = new Properties();
282 private Tree decompose() {
283 RTree rtree = new RTree();
285 IG2DNode[] nodes = getSortedNodes();
286 TIntObjectHashMap<IG2DNode> toNodes = new TIntObjectHashMap<IG2DNode>(nodes.length);
287 TIntObjectHashMap<Rectangle2D> toBounds = new TIntObjectHashMap<Rectangle2D>(nodes.length);
289 ArrayList<IG2DNode> boundlessNodes = null;
290 for (IG2DNode node : nodes) {
291 Rectangle2D bounds = node.getBounds();
292 if (bounds != null) {
293 Rectangle r = toRectangle(bounds);
296 toNodes.put(id, node);
297 toBounds.put(id, bounds);
299 if (boundlessNodes == null)
300 boundlessNodes = new ArrayList<IG2DNode>();
301 boundlessNodes.add(node);
304 return new Tree(rtree, boundlessNodes, toNodes, toBounds);
311 public static Rectangle toRectangle(Rectangle2D rect) {
312 return new Rectangle((float) rect.getMinX(), (float) rect.getMinY(), (float) rect.getMaxX(), (float) rect.getMaxY());
315 public List<IG2DNode> intersectingNodes(Rectangle2D rect, List<IG2DNode> result) {
316 final Tree tree = getSpatialDecomposition();
317 if (rect == null || tree.bounds == null || containedBy(tree.bounds, rect)) {
318 IG2DNode[] nodes = getSortedNodes();
319 for (IG2DNode node : nodes) {
320 if (node.validate()) {
325 tree.rtree.intersects(toRectangle(rect), value -> {
326 //System.out.println("exec: " + value);
327 IG2DNode node = tree.toNodes.get(value);
328 //System.out.println(" node: " + node);
329 if (node == null || !node.validate())
336 Collections.sort(result, G2DParentNode.G2DNODE_Z_COMPARATOR);
341 * Determine whether this rectangle is contained by the passed rectangle
343 * @param contained the rectangle tested to see if it contained by container
344 * @param container The rectangle that might contain this rectangle
346 * @return <code>true</code> if the container contains contained,
347 * <code>false</code> if it does not
349 public static boolean containedBy(Rectangle contained, Rectangle2D container) {
350 return container.getMaxX() >= contained.maxX && container.getMinX() <= contained.minX
351 && container.getMaxY() >= contained.maxY && container.getMinY() <= contained.minY;
357 //addEventHandler(this);
361 public void cleanup() {
362 //removeEventHandler(this);
367 public int getEventMask() {
368 return EventTypes.MouseMask;
372 public boolean handleEvent(Event e) {
373 // Propagate mouse events to children based on bounds.
374 //System.out.println("R-TREE HANDLE EVENT: " + e);
379 public NodeEventHandler getEventHandler() {
380 // TODO: add an event handler here and propagate mouse events spatially
381 return NodeUtil.getNodeEventHandler(this);