]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Handle variable length free end updates properly 61/3161/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Mon, 26 Aug 2019 13:31:41 +0000 (16:31 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Mon, 26 Aug 2019 13:31:41 +0000 (16:31 +0300)
Depending on which control point was updated, we may need to update free
ends position.

gitlab #24
gitlab #26

Change-Id: I0f3c99b279ea14d2caf57fcbc1f15a7da7425d3c

org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipingRules.java

index 6a5d04fb345731d9fb88cbbc86c7494611bc4f9c..386cc56a211d3245d6bb07137550d538d31a50f6 100644 (file)
@@ -44,29 +44,41 @@ public class PipingRules {
        private static boolean triedIR = false;
 
        
-       private static List<PipeControlPoint> updates = new ArrayList<PipeControlPoint>();
+       private static List<PipeControlPoint> requestUpdates = new ArrayList<PipeControlPoint>();
+       private static List<PipeControlPoint> currentUpdates = new ArrayList<PipeControlPoint>();
        
-       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<PipeControlPoint> temp = new ArrayList<PipeControlPoint>(updates.size());
-               synchronized(mutex) {
-                       temp.addAll(updates);
-                       updates.clear();
+               
+               List<PipeControlPoint> temp = new ArrayList<PipeControlPoint>(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;