]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Added new diagram element transformation function rotateToNeighborSlope
authorjsimomaa <jani.simomaa@gmail.com>
Sat, 1 Sep 2018 12:12:15 +0000 (15:12 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 5 Sep 2018 11:32:43 +0000 (14:32 +0300)
gitlab #97

Change-Id: I0a86ea5c8bc3f5c6d3019715580e38cd1483d85b
(cherry picked from commit 3e877b48594c98cff85f4db64964f86fe14c0f03)

bundles/org.simantics.diagram/scl/Simantics/Diagram/ElementTransforms.scl [new file with mode: 0644]
bundles/org.simantics.diagram/src/org/simantics/diagram/content/TerminalMap.java
bundles/org.simantics.diagram/src/org/simantics/diagram/elements/ElementTransforms.java

diff --git a/bundles/org.simantics.diagram/scl/Simantics/Diagram/ElementTransforms.scl b/bundles/org.simantics.diagram/scl/Simantics/Diagram/ElementTransforms.scl
new file mode 100644 (file)
index 0000000..9da7ee0
--- /dev/null
@@ -0,0 +1,4 @@
+import "Simantics/DB"
+
+importJava "org.simantics.diagram.elements.ElementTransforms" where
+    rotateToNeighborSlope :: Resource -> <WriteGraph, Proc> Double 
index 45ed01a21f7ee137a1f724a2895b0a2664425b91..896d10e81829cc68520596c9b29abb9653468623 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -8,11 +8,13 @@
  *
  * Contributors:
  *     VTT Technical Research Centre of Finland - initial API and implementation
+ *     Semantum Oy - Added getTerminals & getConnectionPoints
  *******************************************************************************/
 package org.simantics.diagram.content;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 
 import org.simantics.db.Resource;
 
@@ -50,4 +52,18 @@ public class TerminalMap {
         rightToLeft.put(connectionPoint, terminal);
     }
 
+    /**
+     * @since 1.36.0
+     */
+    public Set<Resource> getTerminals() {
+        return leftToRight.keySet();
+    }
+
+    /**
+     * @since 1.36.0
+     */
+    public Set<Resource> getConnectionPoints() {
+        return rightToLeft.keySet();
+    }
+
 }
index 51d37b99ac844a1ab9ec1f05b0b6adec08514f4c..f3b9e7b952fcd489ecd5f7af489977c957b25204 100644 (file)
@@ -7,22 +7,30 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
+import org.simantics.db.Statement;
 import org.simantics.db.UndoContext;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.CommentMetadata;
 import org.simantics.db.common.request.IndexRoot;
 import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
 import org.simantics.db.exception.NoSingleResultException;
 import org.simantics.db.exception.ServiceException;
 import org.simantics.db.request.Write;
+import org.simantics.diagram.content.TerminalMap;
 import org.simantics.diagram.query.DiagramRequests;
 import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.synchronization.graph.BasicResources;
 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
 import org.simantics.g2d.diagram.DiagramClass;
 import org.simantics.g2d.diagram.IDiagram;
@@ -34,7 +42,12 @@ import org.simantics.g2d.element.impl.Element;
 import org.simantics.g2d.utils.GeometryUtils;
 import org.simantics.scl.commands.Command;
 import org.simantics.scl.commands.Commands;
+import org.simantics.structural2.queries.Terminal;
 import org.simantics.ui.SimanticsUI;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import gnu.trove.set.hash.THashSet;
 
 /**
  * Tools to align, rotate, and flip diagram elements.
@@ -48,6 +61,8 @@ import org.simantics.ui.SimanticsUI;
  */
 public final class ElementTransforms {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(ElementTransforms.class);
+
     public static enum SIDE { LEFT, RIGHT, TOP, BOTTOM, VERT, HORIZ, VERT_BTW, HORIZ_BTW };
 
     /**
@@ -621,6 +636,7 @@ public final class ElementTransforms {
             return GeometryUtils.transformShape(bounds, at).getBounds2D();
         }
 
+        @SuppressWarnings("unused")
         public double getDeterminant() {
             return transform[0] * transform[3] - transform[1] * transform[2];
         }
@@ -712,4 +728,177 @@ public final class ElementTransforms {
         }
     }
 
+    // ------------------------------------------------------------------------
+
+    private interface TerminalFunction {
+        /**
+         * @param terminal
+         * @param rotationTheta in radians
+         * @return
+         */
+        AffineTransform transform(Resource connectionPoint, double rotationTheta);
+        default Point2D position(Resource connectionPoint, double rotationTheta) {
+            AffineTransform tr = transform(connectionPoint, rotationTheta);
+            return new Point2D.Double(tr.getTranslateX(), tr.getTranslateY());
+        }
+        Resource toConnectionPoint(Resource terminal);
+        Resource toTerminal(Resource connectionPoint);
+        Set<Resource> connectionPoints();
+        Set<Resource> terminals();
+    }
+
+    private static TerminalFunction terminalPositionFunction(ReadGraph graph, boolean ignoreElementRotation, Resource element) throws DatabaseException {
+        AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, element);
+        if (ignoreElementRotation) {
+            AffineTransform noRotation = AffineTransform.getTranslateInstance(transform.getTranslateX(), transform.getTranslateY());
+            Point2D scale = org.simantics.scenegraph.utils.GeometryUtils.getScale2D(transform);
+            noRotation.scale(scale.getX(), scale.getY());
+            transform = noRotation;
+        }
+        AffineTransform elementTransform = transform;
+
+        TerminalMap elementTerminals = DiagramGraphUtil.getElementTerminals(graph, element);
+        Map<Resource, AffineTransform> terminalTransforms = new HashMap<>();
+        for (Resource terminal : elementTerminals.getTerminals())
+            terminalTransforms.put(elementTerminals.getConnectionPoint(terminal), DiagramGraphUtil.getAffineTransform(graph, terminal));
+
+        return new TerminalFunction() {
+            @Override
+            public AffineTransform transform(Resource connectionPoint, double rotationTheta) {
+                AffineTransform tr = new AffineTransform(elementTransform);
+                AffineTransform terminalTr = terminalTransforms.get(connectionPoint);
+                if (rotationTheta != 0)
+                    tr.rotate(rotationTheta);
+                if (terminalTr != null)
+                    tr.concatenate(terminalTr);
+                return tr;
+            }
+            @Override
+            public Resource toConnectionPoint(Resource terminal) {
+                return elementTerminals.getConnectionPoint(terminal);
+            }
+            @Override
+            public Resource toTerminal(Resource connectionPoint) {
+                return elementTerminals.getTerminal(connectionPoint);
+            }
+            @Override
+            public Set<Resource> connectionPoints() {
+                return elementTerminals.getConnectionPoints();
+            }
+            @Override
+            public Set<Resource> terminals() {
+                return elementTerminals.getTerminals();
+            }
+        };
+    }
+
+    private static Set<Terminal> connectedTo(ReadGraph graph, Resource element, Resource connectionPoint) throws DatabaseException {
+        BasicResources BR = BasicResources.getInstance(graph);
+        Set<Terminal> result = new THashSet<>();
+        for (Resource connector : graph.getObjects(element, connectionPoint)) {
+            Resource connection = graph.getPossibleObject(connector, BR.DIA.IsConnectorOf);
+            if (connection == null)
+                continue;
+            for (Resource otherConnector : graph.getObjects(connection, BR.DIA.HasConnector)) {
+                if (!otherConnector.equals(connector)) {
+                    for (Statement s : graph.getStatements(otherConnector, BR.STR.Connects)) {
+                        if (!s.getObject().equals(connection)) {
+                            result.add( new Terminal(s.getObject(), graph.getInverse(s.getPredicate())) );
+                        }
+                    }
+                }
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Rotates provided element so that its rotation is the same as the slope of a
+     * line drawn between the two other elements the element is connected to.
+     * 
+     * <p>
+     * Note that this requires that the element is connected to exactly two other
+     * elements.
+     * 
+     * @param graph
+     * @param element
+     * @return the chosen rotation in degrees
+     * @throws DatabaseException
+     */
+    public static double rotateToNeighborSlope(WriteGraph graph, Resource element) throws DatabaseException {
+        
+        if (LOGGER.isDebugEnabled())
+            LOGGER.debug("rotateAccordingToNeighborPositions( {} )", graph.getPossibleURI(element));
+
+        TerminalFunction elementTerminals = terminalPositionFunction(graph, true, element);
+        Set<Resource> connectedComponents = new HashSet<>();
+        Map<Resource, TerminalFunction> componentTerminals = new HashMap<>();
+        Map<Resource, Terminal> connectedTerminals = new HashMap<>();
+
+        for (Resource connectionPoint : elementTerminals.connectionPoints()) {
+            Set<Terminal> connectedTo = connectedTo(graph, element, connectionPoint);
+            if (connectedTo.isEmpty())
+                continue;
+            if  (connectedTo.size() > 1) {
+                if (LOGGER.isWarnEnabled())
+                    LOGGER.warn("rotateToNeighbors only supports functional connection points, ignoring {}", NameUtils.getURIOrSafeNameInternal(graph, connectionPoint));
+                continue;
+            }
+
+            Terminal t = connectedTo.iterator().next();
+            TerminalFunction tf = terminalPositionFunction(graph, false, t.getComponent());
+            connectedComponents.add(t.getComponent());
+            componentTerminals.put(t.getComponent(), tf);
+            connectedTerminals.put(connectionPoint, t);
+
+            if (LOGGER.isDebugEnabled())
+                LOGGER.info("{} {} is connected to {}", NameUtils.getSafeName(graph, element), NameUtils.getSafeName(graph, connectionPoint), t.toString(graph));
+        }
+
+        if (connectedComponents.size() != 2) {
+            LOGGER.error("rotateToNeighborPositions only works for elements connected to exactly two other elements, {} is connected to {}", NameUtils.getURIOrSafeNameInternal(graph, element), connectedComponents.size());
+            return Double.NaN;
+        }
+
+        Resource[] ends = connectedComponents.toArray(Resource.NONE);
+        AffineTransform[] endTr = {
+                componentTerminals.get(ends[0]).transform(null, 0),
+                componentTerminals.get(ends[1]).transform(null, 0)
+        };
+
+        double slopeTheta = Math.atan2(endTr[1].getTranslateY() - endTr[0].getTranslateY(), endTr[1].getTranslateX() - endTr[0].getTranslateX());
+        double[] thetas = { slopeTheta, slopeTheta + Math.PI };
+        double selectedRotation = slopeTheta;
+        double minCost = Double.MAX_VALUE;
+        for (int i = 0; i < 2; ++i) {
+            double cost = 0;
+            for (Map.Entry<Resource, Terminal> e : connectedTerminals.entrySet()) {
+                Terminal t = e.getValue();
+                TerminalFunction tf = componentTerminals.get(t.getComponent());
+                Point2D otp = tf.position(t.getRelation(), 0);
+                Point2D etp = elementTerminals.position(e.getKey(), thetas[i]);
+                cost += otp.distance(etp);
+            }
+
+            if (LOGGER.isDebugEnabled())
+                LOGGER.info("total cost of theta {} is {}", Math.toDegrees(thetas[i]), cost);
+
+            if (cost < minCost) {
+                minCost = cost;
+                selectedRotation = thetas[i];
+                if (LOGGER.isDebugEnabled())
+                    LOGGER.debug("new minimum cost {} found", cost);
+            }
+        }
+
+        AffineTransform newTr = elementTerminals.transform(null, 0);
+        newTr.rotate(selectedRotation);
+        DiagramGraphUtil.setTransform(graph, element, newTr);
+
+        if (LOGGER.isDebugEnabled())
+            LOGGER.debug("set rotation to {} degrees", Math.toDegrees(selectedRotation));
+
+        return Math.toDegrees(selectedRotation);
+    }
+
 }