From 8de6358017cced8121a77fb18f31dd5e0a9dedbe Mon Sep 17 00:00:00 2001 From: lempinen Date: Tue, 21 Dec 2010 12:22:15 +0000 Subject: [PATCH] Reduced memory allocations git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@19187 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../elements2/connections/Dependencies.java | 93 +++++++++++-------- .../connections/DependencyEdgeClass.java | 17 +++- .../elements2/connections/DependencyNode.java | 10 +- .../ui/elements2/connections/Flows.java | 12 ++- 4 files changed, 81 insertions(+), 51 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/Dependencies.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/Dependencies.java index 535778ed..bb0a4bd3 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/Dependencies.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/Dependencies.java @@ -11,7 +11,6 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.elements2.connections; -import java.awt.Shape; import java.awt.geom.Arc2D; import java.awt.geom.Path2D; import java.awt.geom.Point2D; @@ -76,40 +75,22 @@ public class Dependencies { // } // - private static Shape createArrow(double x, double y, double dx, double dy) { - Path2D path = new Path2D.Double(); - path.moveTo(x+ARROW_LENGTH1*dx, y+ARROW_LENGTH1*dy); + private static Path2D createArrow(Path2D shape, double x, double y, double dx, double dy) { + if(shape == null) + shape = new Path2D.Double(); + else + shape.reset(); + + shape.moveTo(x+ARROW_LENGTH1*dx, y+ARROW_LENGTH1*dy); x -= ARROW_LENGTH2*dx; y -= ARROW_LENGTH2*dy; - path.lineTo(x-ARROW_WIDTH*dy, y+ARROW_WIDTH*dx); - path.lineTo(x+ARROW_WIDTH*dy, y-ARROW_WIDTH*dx); - path.closePath(); - return path; + shape.lineTo(x-ARROW_WIDTH*dy, y+ARROW_WIDTH*dx); + shape.lineTo(x+ARROW_WIDTH*dy, y-ARROW_WIDTH*dx); + shape.closePath(); + return shape; } - public static Point2D computeCenter(Rectangle2D tail, Rectangle2D head, double angle) { - - double x0 = tail.getCenterX(); - double y0 = tail.getCenterY(); - double x1 = head.getCenterX(); - double y1 = head.getCenterY(); - -// System.out.println("createArrowShape " + x0 + " " + y0 + " " + x1 + " " + y1); - - double offset = - Math.abs(angle) < 1.0e-6 - ? 1e3 * Math.signum(angle) - : Math.tan(Math.PI*0.5-angle)*0.5; - - double cx = 0.5*(x0+x1) + offset * (y1-y0); - double cy = 0.5*(y0+y1) + offset * (x0-x1); - - return new Point2D.Double(cx, cy); - - } - - public static Pair createArrowShape(Rectangle2D tail, Rectangle2D head, double angle) { - + public static Arc2D createArc(Arc2D arc, Rectangle2D tail, Rectangle2D head, double angle) { double x0 = tail.getCenterX(); double y0 = tail.getCenterY(); double x1 = head.getCenterX(); @@ -150,19 +131,55 @@ public class Dependencies { extent += Math.PI*2.0; else if(extent >= 360.0) extent -= Math.PI*2.0; - Shape shape1 = new Arc2D.Double(cx-r, cy-r, 2*r, 2*r, + if(arc == null) + arc = new Arc2D.Double(); + arc.setArc(cx-r, cy-r, 2*r, 2*r, Math.toDegrees(angle0), Math.toDegrees(extent), Arc2D.OPEN); // - double xx = Math.cos(angle > 0.0 ? angle1 : angle0); - double yy = -Math.sin(angle > 0.0 ? angle1 : angle0); + return arc; + } + + public static Point2D computeCenter(Rectangle2D tail, Rectangle2D head, double angle) { + + double x0 = tail.getCenterX(); + double y0 = tail.getCenterY(); + double x1 = head.getCenterX(); + double y1 = head.getCenterY(); + +// System.out.println("createArrowShape " + x0 + " " + y0 + " " + x1 + " " + y1); + + double offset = + Math.abs(angle) < 1.0e-6 + ? 1e3 * Math.signum(angle) + : Math.tan(Math.PI*0.5-angle)*0.5; + + double cx = 0.5*(x0+x1) + offset * (y1-y0); + double cy = 0.5*(y0+y1) + offset * (x0-x1); + + return new Point2D.Double(cx, cy); + + } + + public static Pair createArrowShape(Pair shapes, Rectangle2D tail, Rectangle2D head, double angle) { + if(shapes == null || shapes.first == null || shapes.second == null) { + shapes = new Pair(new Arc2D.Double(), new Path2D.Double()); + } - Shape arrowShape = createArrow(cx + r*xx, cy + r*yy, - angle < 0.0 ? -yy : yy, - angle > 0.0 ? -xx : xx); + createArc(shapes.first, tail, head, angle); + + double angle0 = Math.toRadians(shapes.first.getAngleStart()); + double angle1 = Math.toRadians(shapes.first.getAngleStart() + shapes.first.getAngleExtent()); + double x = Math.cos(angle > 0.0 ? angle1 : angle0); + double y = -Math.sin(angle > 0.0 ? angle1 : angle0); + double r = shapes.first.getHeight() / 2; + + createArrow(shapes.second, shapes.first.getCenterX() + r*x, shapes.first.getCenterY() + r*y, + angle < 0.0 ? -y : y, + angle > 0.0 ? -x : x); - return Pair.make(shape1, arrowShape); + return shapes; } // diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyEdgeClass.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyEdgeClass.java index b2baef64..fc3412ce 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyEdgeClass.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyEdgeClass.java @@ -15,6 +15,7 @@ import java.awt.BasicStroke; import java.awt.Color; import java.awt.Shape; import java.awt.Stroke; +import java.awt.geom.Arc2D; import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; import java.beans.PropertyChangeEvent; @@ -26,19 +27,19 @@ import org.simantics.g2d.diagram.DiagramHints; import org.simantics.g2d.diagram.DiagramMutator; import org.simantics.g2d.diagram.DiagramUtils; import org.simantics.g2d.diagram.IDiagram; -import org.simantics.g2d.diagram.handler.Topology; import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy; +import org.simantics.g2d.diagram.handler.Topology; import org.simantics.g2d.diagram.handler.Topology.Connection; import org.simantics.g2d.element.ElementClass; import org.simantics.g2d.element.ElementUtils; import org.simantics.g2d.element.IElement; import org.simantics.g2d.element.SceneGraphNodeKey; import org.simantics.g2d.element.handler.EdgeVisuals; +import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd; import org.simantics.g2d.element.handler.Pick; import org.simantics.g2d.element.handler.SceneGraph; import org.simantics.g2d.element.handler.TerminalLayout; import org.simantics.g2d.element.handler.Transform; -import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd; import org.simantics.g2d.element.handler.impl.ConfigurableEdgeVisuals; import org.simantics.g2d.element.handler.impl.ConnectionSelectionOutline; import org.simantics.g2d.element.handler.impl.FillColorImpl; @@ -103,6 +104,8 @@ public class DependencyEdgeClass { public static final Key KEY_SG_NODE = new SceneGraphNodeKey(DependencyNode.class, "EDGE_NODE"); + private transient Arc2D arc = new Arc2D.Double(); + @Override public void init(IElement e, G2DParentNode parent) { ElementUtils.getOrCreateNode(e, parent, KEY_SG_NODE, "edge_" + e.hashCode(), DependencyNode.class); @@ -177,10 +180,14 @@ public class DependencyEdgeClass { // System.out.println("setProperty " + entry.getKey() + " => " + entry.getValue().second); } } - - Pair shapes = Dependencies.createArrowShape(beginTerminalShape.getBounds2D(), endTerminalShape.getBounds2D(), node.getAngle()); EdgeHandler eh = e.getElementClass().getAtMostOneItemOfClass(EdgeHandler.class); - Path2D path = new Path2D.Double(shapes.first); + Path2D path = eh.getPath(e); + if(path == null) + path = new Path2D.Double(); + else + path.reset(); + Dependencies.createArc(arc, beginTerminalShape.getBounds2D(), endTerminalShape.getBounds2D(), node.getAngle()); + path.append(arc, false); eh.setPath(e, path); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyNode.java index 8a6f3625..66a5f1ca 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyNode.java @@ -15,12 +15,12 @@ import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.RenderingHints; -import java.awt.Shape; import java.awt.Stroke; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; -import java.awt.geom.Point2D; +import java.awt.geom.Arc2D; +import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; @@ -42,6 +42,7 @@ public class DependencyNode extends G2DNode implements ISelectionPainterNode, Mo private Rectangle2D beginBounds; private Rectangle2D endBounds; private double angle = 0.1; + private transient Pair shapes = new Pair(new Arc2D.Double(), new Path2D.Double()); transient public boolean hover = false; @@ -128,10 +129,9 @@ public class DependencyNode extends G2DNode implements ISelectionPainterNode, Mo // NICENESS g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + + Dependencies.createArrowShape(shapes, beginBounds, endBounds, angle); - // FIXME: optimize memory allocations - Pair shapes = Dependencies.createArrowShape(beginBounds, endBounds, angle); - boolean selected = NodeUtil.isSelected(this, 2); if(selected) { g.setColor(Color.PINK); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/Flows.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/Flows.java index 64521600..eccf2461 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/Flows.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/Flows.java @@ -33,8 +33,11 @@ public class Flows { double miny = head.getMinY(); double maxy = head.getMaxY(); - arrow = new Path2D.Double(); - + if(arrow == null) + arrow = new Path2D.Double(); + else + arrow.reset(); + // approach from top if (y < miny) { arrow.moveTo(cx, miny); @@ -72,7 +75,10 @@ public class Flows { } private static Path2D createLines(Path2D lines, boolean vertical, double ... coordinates) { - lines = new Path2D.Double(); + if(lines == null) + lines = new Path2D.Double(); + else + lines.reset(); createOffsetLine(lines, vertical, OFFSET, coordinates); createOffsetLine(lines, vertical, -OFFSET, coordinates); return lines; -- 2.47.1