From 6f6aa87201058478da5a0253118f2be10d946982 Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Mon, 26 Aug 2019 16:31:41 +0300 Subject: [PATCH] Handle variable length free end updates properly Depending on which control point was updated, we may need to update free ends position. gitlab #24 gitlab #26 Change-Id: I0f3c99b279ea14d2caf57fcbc1f15a7da7425d3c --- .../scenegraph/controlpoint/PipingRules.java | 54 +++++++++++++------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipingRules.java b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipingRules.java index 6a5d04fb..386cc56a 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipingRules.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipingRules.java @@ -44,29 +44,41 @@ public class PipingRules { private static boolean triedIR = false; - private static List updates = new ArrayList(); + private static List requestUpdates = new ArrayList(); + private static List currentUpdates = new ArrayList(); - private static Object mutex = new Object(); + private static Object updateMutex = new Object(); + private static Object ruleMutex = new Object(); public static void requestUpdate(PipeControlPoint pcp) { if (DEBUG) System.out.println("PipingRules request " + pcp); - synchronized (mutex) { - if (!updates.contains(pcp)) - updates.add(pcp); + synchronized (updateMutex) { + if (!requestUpdates.contains(pcp)) + requestUpdates.add(pcp); } } - public static synchronized boolean update() throws Exception { - if (updates.size() == 0) + public static boolean update() throws Exception { + if (requestUpdates.size() == 0) return false; - List temp = new ArrayList(updates.size()); - synchronized(mutex) { - temp.addAll(updates); - updates.clear(); + + List temp = new ArrayList(requestUpdates.size()); + synchronized(updateMutex) { + temp.addAll(requestUpdates); + requestUpdates.clear(); + } + synchronized (ruleMutex) { + currentUpdates.clear(); + currentUpdates.addAll(temp); + // TODO : we should remove already processed control points from currentUpdates after each _positionUpdate call. + for (PipeControlPoint pcp : currentUpdates) + _positionUpdate(pcp, true); + currentUpdates.clear(); + } + synchronized(updateMutex) { + requestUpdates.removeAll(temp); } - for (PipeControlPoint pcp : temp) - positionUpdate(pcp); return true; } @@ -76,6 +88,16 @@ public class PipingRules { } public static boolean positionUpdate(PipeControlPoint pcp, boolean allowIR) throws Exception { + synchronized (ruleMutex) { + currentUpdates.add(pcp); + boolean b = _positionUpdate(pcp, allowIR); + currentUpdates.clear(); + return b; + } + + } + + private static boolean _positionUpdate(PipeControlPoint pcp, boolean allowIR) throws Exception { if (updating || !enabled) return true; if (pcp.getPipeRun() == null) @@ -106,7 +128,7 @@ public class PipingRules { public static void setEnabled(boolean enabled) { PipingRules.enabled = enabled; if(!enabled) - updates.clear(); + currentUpdates.clear(); } public static boolean isEnabled() { @@ -714,11 +736,13 @@ public class PipingRules { Vector3d dir = new Vector3d(); dir.sub(currentPos, prevPos); - boolean simple = true; + boolean simple = currentUpdates.contains(icp); if (simple) { + // Update based on position -> adjust length double currentLength = (dir.length() - prev.getInlineLength()) * 2.0; icp.setLength(currentLength); } else { + // Update based on neighbour movement -> adjust length and position, so that free end stays in place. double currentLength = icp.getLength(); if (currentLength < MathTools.NEAR_ZERO) { currentLength = (dir.length() - prev.getInlineLength()) * 2.0; -- 2.45.2