From 6ed65dcb597fb2153cef75bf1845978f1115658c Mon Sep 17 00:00:00 2001
From: Normand Briere <nbriere@noware.ca>
Date: Fri, 14 Dec 2018 22:53:55 -0500
Subject: [PATCH] Repair shadow, normal push + attractor mesh.

---
 cSpring.java |  234 ++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 162 insertions(+), 72 deletions(-)

diff --git a/cSpring.java b/cSpring.java
index 6bebd0f..445f1ac 100644
--- a/cSpring.java
+++ b/cSpring.java
@@ -82,7 +82,7 @@
 
     // timestep
     double timestep = 1;
-    double normalpusH = 0; // 1;
+    double normalpush = 0; // 1;
     static float H0 = 1E1f; // aucune importance...
     // mass
     double M = 1;
@@ -967,6 +967,7 @@
             for (int i = 0; i < allNodes.size(); i++)
             {
                 DynamicNode dn = allNodes.get(i);
+                dn.linked = false;
                 dn.closestpoint = null;
             }
         }
@@ -1057,7 +1058,7 @@
                 vect1.set(v);
                 LA.xformPos(vect1,toRoot,vect1);
             
-                handles.get(usedrep.VertexCount()-1-i).set(vect1);
+                handles.get(i).set(vect1);
             }
         }
         
@@ -1416,6 +1417,7 @@
         //transient double rotangle; // Z of axis
         transient boolean reverse = false;
         
+        transient boolean linked = false;
         transient Vertex closestpoint = null;
         
         //Point3D lastForce = new Point3D();
@@ -1491,6 +1493,11 @@
                         otherForce.add(f.forceOn(this));
                     }
                 }
+            }
+
+            if (Double.isNaN(springForce.y))
+            {
+                    springForce.x = springForce.y = springForce.z = 0;
             }
         }
 
@@ -2325,6 +2332,7 @@
 
         abstract Point3D forceOn(DynamicNode N);
     }
+    
     Point3D force = new Point3D();
     Point3D dir = new Point3D();
 
@@ -2338,28 +2346,7 @@
         return this;
     }
     
-    class Avoider extends Force
-    {
-        static final long serialVersionUID = -8657094699711594990L;
-        
-        Avoider()
-        {
-        }
-
-        Avoider(Object3D col)
-        {
-            SetAvoider(col);
-        }
-
-        void SetParameter(int index, double value)
-        {
-            parameters.setElementAt(new Double(value), index);
-        }
-        
-        void SetAvoider(Object3D col)
-        {
-            avoider = col;
-        }
+        Object3D avoider;
         
         void ResetGlobalTransform()
         {
@@ -2371,10 +2358,6 @@
 
         transient double[][] toRoot; // = new double[4][4];
         
-        //Vector<Vertex> sortedcollider;
-        Object3D avoider;
-        Vector<Double> parameters = new Vector<Double>(); // serial issue
-
         Vertex ClosestPoint(Vector<Vertex> collider, DynamicNode N, int startindex, int endindex, int sortaxis)
         {
             if (startindex == endindex)
@@ -2468,7 +2451,8 @@
             
             for (int i=collider.size(); --i>=0;)
             {
-                if (collider.get(i).norm.dot(((Vertex)N.position).norm) < 0)
+                // Why? 3 dec 2018
+                if (collider.get(i).norm.dot(((Vertex)N.position).norm) < 0.5)
                     continue;
                 
                 temp.set(N.position);
@@ -2486,43 +2470,24 @@
                 }
             }
             
-            return cp;
+            return mindist2 < 1 ? cp : null;
         }
         
-        Point3D forceOn(DynamicNode N)
+    class Avoider extends Force
+    {
+        static final long serialVersionUID = -8657094699711594990L;
+        
+        Avoider()
         {
-            force.x = 0;
-            force.y = 0;
-            force.z = 0;
-            
-        if (avoider.transientrep == null)
-            avoider.Revert();
-            
-            if (avoider.transientrep == null || avoider.transientrep.VertexCount() == 0 || avoider.transientrep.trimmed)
-                return force;
-            
-   ResetGlobalTransform();
-            if (N.closestpoint == null)
-            {
-                //avoider.bRep.GetVertex(0); // "sort"
-                
-                ResetGlobalTransform();
-            
-                N.closestpoint = ClosestPoint(avoider.transientrep./*sortedV*/vertices, N, 0,avoider.transientrep./*sortedV*/vertices.size(), 0);
-                    
-                if (N.closestpoint == null) // ????
-                    return force;
-            }
-            
-            if (avoider == This())
-            {
-                // good but must freeze the order first... not really...
-           //     if (cp.index < ((Vertex)N.position).index)
-           //         return force;
-            }
-            
-            if (normalpusH != 0) // && cp == null /*???*/) // speed == 0)
-            {
+        }
+
+        Avoider(Object3D col)
+        {
+            SetAvoider(col);
+        }
+
+        void Avoidance(DynamicNode N)
+        {
                 temp.set(N.position);
                 if (avoider != This())
                 {
@@ -2530,16 +2495,16 @@
                     temp2.mul(0.001);
                     temp.sub(temp2); // push outside because of mesh artifacts
                 }
-                
+
                 vect1.set(N.closestpoint);
-                
+
                 LA.xformPos(vect1,toRoot,vect1);
                 //avoider.TransformToWorld(vect1, vect1);
-                        
+
                 temp.sub(vect1);
-                
+
                 double distance = Math.sqrt(temp.length2());
-                
+
                 if (distance < 0.01)
                 {
                     temp2.set(N.closestpoint.norm);
@@ -2553,9 +2518,98 @@
                     if (IsAutoFreeze())
                         dot = -dot;
 
-                    temp.mul(normalpusH/*/distance*/*-dot/Math.sqrt(distance));
+                    temp.mul(normalpush/*/distance*/*-dot/Math.sqrt(distance));
                     force.add(temp);
                 }
+        }
+
+        void Attraction(DynamicNode N)
+        {
+                temp.set(N.position);
+                if (avoider != This())
+                {
+                    temp2.set(N.closestpoint.norm);
+                    temp2.mul(0.001);
+                    temp.sub(temp2); // push outside because of mesh artifacts
+                }
+
+                vect1.set(N.closestpoint);
+
+                LA.xformPos(vect1,toRoot,vect1);
+                //avoider.TransformToWorld(vect1, vect1);
+
+                temp.sub(vect1);
+
+                double distance = Math.sqrt(temp.length2());
+
+                //if (distance < 0.01)
+                {
+                    temp2.set(N.closestpoint.norm);
+                    double dot = temp2.dot(temp);
+
+//                    if (dot > 0)
+//                        dot = 0;
+
+                    // normal pushing
+                    temp.set(N.closestpoint.norm);
+//                    if (IsAutoFreeze())
+//                        dot = -dot;
+
+                    temp.mul(normalpush/*/distance*/ * -dot * distance);
+                    force.add(temp);
+                }
+        }
+
+        void SetParameter(int index, double value)
+        {
+            parameters.setElementAt(new Double(value), index);
+        }
+        
+        void SetAvoider(Object3D col)
+        {
+            avoider = col;
+        }
+        
+        //Vector<Vertex> sortedcollider;
+        Vector<Double> parameters = new Vector<Double>(); // serial issue
+
+        Point3D forceOn(DynamicNode N)
+        {
+            force.x = 0;
+            force.y = 0;
+            force.z = 0;
+            
+        if (avoider.transientrep == null)
+            avoider.Revert();
+            
+            if (avoider.transientrep == null || avoider.transientrep.VertexCount() == 0 || avoider.transientrep.trimmed)
+                return force;
+            
+   ResetGlobalTransform();
+            if (!N.linked)
+            {
+                //avoider.bRep.GetVertex(0); // "sort"
+                
+                ResetGlobalTransform();
+            
+                N.closestpoint = ClosestPoint(avoider.transientrep.vertices, N, 0,avoider.transientrep.vertices.size(), 0);
+                N.linked = true;
+            }
+            
+            if (avoider == This())
+            {
+                // good but must freeze the order first... not really...
+           //     if (cp.index < ((Vertex)N.position).index)
+           //         return force;
+            }
+            
+            if (normalpush != 0) // && cp == null /*???*/) // speed == 0)
+            {
+                if (N.closestpoint == null)
+                    return force;
+                
+                //Avoidance(N);
+                Attraction(N);
             }
             
             if (parameters.size() > 0)
@@ -2589,6 +2643,7 @@
                 totalforce = new Point3D();
                     
             totalforce.set(0,0,0);
+            
             if (forces.size() == 0)
                 totalforce.set(super.forceOn(N));
             
@@ -2659,7 +2714,7 @@
             force.y = 0;
             force.z = 0;
                 
-            if (normalpusH != 0) // speed == 0) // Now normal force
+            if (normalpush != 0) // speed == 0) // Now normal force
             {
              //   temp.set(N.position);
              //   temp.sub(Phys.reference);
@@ -2667,7 +2722,7 @@
                 
                 // normal pushing
                 temp.set(N.normal);
-                temp.mul(normalpusH/distance);
+                temp.mul(normalpush/distance);
                 force.add(temp);
             }
             
@@ -2899,6 +2954,7 @@
             return force;
         }
     }
+    
     static int TABLESAC = 1024;
     static double[] tablesac = new double[TABLESAC];
 
@@ -2992,6 +3048,40 @@
             //    mass = M;
             } // ???
 
+            if (avoider != null)
+            {
+                    if (avoider.transientrep == null)
+                        avoider.Revert();
+
+                    if (avoider.transientrep == null || avoider.transientrep.VertexCount() == 0 || avoider.transientrep.trimmed)
+                        return force;
+
+                    ResetGlobalTransform();
+                    if (!N.linked)
+                    {
+                        //avoider.bRep.GetVertex(0); // "sort"
+
+                        //ResetGlobalTransform();
+
+                        N.closestpoint = ClosestPoint(avoider.transientrep.vertices, N, 0,avoider.transientrep.vertices.size(), 0);
+                        N.linked = true;
+                    }
+                    
+                    if (N.closestpoint != null)
+                    {
+                        temp.set(N.position);
+                        vect1.set(N.closestpoint);
+                        LA.xformPos(vect1,toRoot,vect1);
+                        temp.sub(vect1);
+
+                            temp2.set(N.closestpoint.norm);
+                            double dot = temp2.dot(temp);
+
+                            if (dot < 0)
+                                mass = Float.NaN;
+                    }
+            }
+            
             force.x = 0;
             force.y = (float) (-acceleration * mass);
             force.z = 0; // -acceleration * mass * 1000 * Math.cos(time/234.0 + framecount*100); // (Math.random()*2 - 1); //  
@@ -3188,7 +3278,7 @@
             }
             if (false)  // isHandle)
             {
-                double K = Math.exp(-displacement*normalpusH);
+                double K = Math.exp(-displacement*normalpush);
                 magnitude /= (displacement*(1-K) + K);
             }
                 

--
Gitblit v1.6.2