From 02e145cb923d601395acc7f15ae9e13f85ef2fbb Mon Sep 17 00:00:00 2001
From: Normand Briere <nbriere@noware.ca>
Date: Mon, 02 Jul 2018 21:38:58 -0400
Subject: [PATCH] Hip orientation.

---
 BoundaryRep.java |  131 +++++++++++++++++++++++++++++++++++--------
 1 files changed, 105 insertions(+), 26 deletions(-)

diff --git a/BoundaryRep.java b/BoundaryRep.java
index 12a985c..3a1d080 100644
--- a/BoundaryRep.java
+++ b/BoundaryRep.java
@@ -497,10 +497,10 @@
         return dist2;
     }
     
-    static Vertex vertextemp = new Vertex();
-    static Vertex vertextemp2 = new Vertex();
+    static Vertex vertextemp = new Vertex(true);
+    static Vertex vertextemp2 = new Vertex(true);
     
-    static double SEUIL = 0.1; // 0.1 for rag doll; 0.07;
+    static double SEUIL = 0.1f; // 0.1 for rag doll; 0.07;
         
     // Compute weight of point w/r to this
     float ComputeWeight(Vertex v, double[][] toRoot, int k)
@@ -570,8 +570,10 @@
         
 if (dot > distmax)
     dot = distmax;
+    //return 0; // patch for strange behavior
 if (dot < -distmax)
     dot = -distmax;
+    //return 0; // patch for strange behavior
         
 //        v3 = GetVertex(this.startvertices[subsupport] + 16);
 //        
@@ -609,10 +611,12 @@
         
         float dist2 = (float)Distance2(v, v2, 1E10, toRoot, k);
         
-        if (dist2 >= 2 * SEUIL*SEUIL) // && !CameraPane.CROWD) // weightmode
+        double seuil = SEUIL * 2;
+        
+        if (dist2 >= 2 * seuil*seuil) // && !CameraPane.CROWD) // weightmode
             return 0;
         
-        dist2 /= 2 * SEUIL*SEUIL; // multiplied by two because center of support
+        dist2 /= 2 * seuil*seuil; // multiplied by two because center of support
                                   // could be far from closest point
         
 //        dist2 = Math.pow(dist2, 2);
@@ -946,7 +950,7 @@
         
         int nbsupports;
         
-        SEUIL = 0.1; // aout 2013
+        // sept 2017 SEUIL = 0.1; // aout 2013
         
         supports = InitConnections(other);
         
@@ -982,7 +986,7 @@
             
             int subsupports = 0;
             
-            SEUIL = 0.1; // aout 2013
+            // sept 2017 SEUIL = 0.1; // aout 2013
             
             while (subsupports == 0)
             {
@@ -1006,6 +1010,26 @@
 
                     vect.set(v);
                     vect.sub(vect2);
+                    
+//        vertextemp.x = other.averagepoints[c*3];
+//        vertextemp.y = other.averagepoints[c*3+1];
+//        vertextemp.z = other.averagepoints[c*3+2];
+//        
+//        Vertex v3 = vertextemp2;
+//        v3.x = other.extremepoints[c*3];
+//        v3.y = other.extremepoints[c*3+1];
+//        v3.z = other.extremepoints[c*3+2];
+//
+//        vect3.set(v3); // "X" axis apex
+//        vect3.sub(vertextemp); // origin (center)
+//        
+//        double distmax = vect3.length();
+//
+//        vect3.set(v2); // "X" axis apex
+//        vect3.sub(vertextemp); // origin (center)
+//        
+//        if (vect3.length() >= distmax)
+//            continue;
 
                     if (mindistance > vect.dot(vect))
                     {
@@ -1017,7 +1041,9 @@
 
                 subsupports = v.closestsupport==-1 ? 0 : supports[v.closestsupport].Length();
                 
-                SEUIL *= 2;
+                // previously for "contains", now for weights.
+                assert(subsupports > 0);
+                //SEUIL *= 2;
             }
             
             assert(subsupports > 0);
@@ -2163,7 +2189,7 @@
              //   if (slow)
     // aout 2013
                 // sept 2013 merde...
-                W = 13;
+                W = 3; // 13;
 
            // POSERATE
                     if (CameraPane.tickcount > 0 || CameraPane.SLOWPOSE)
@@ -3776,10 +3802,19 @@
         for (int i = 0; i < VertexCount(); i++)
         {
             Vertex v = GetVertex(i);
+            
+            vertextemp.set(v);
+            
             //if (v.norm.x == 0 && v.norm.y == 0 && v.norm.z == 0)
             {
-                GenerateNormal(i, v);
-                SetVertex(v, i);
+                if (!GenerateNormal(i, vertextemp))
+                    continue;
+                
+                if (v.norm.dot(vertextemp.norm) < 0)
+                    vertextemp.norm.mul(-1);
+                    
+                if (v.norm.dot(vertextemp.norm) < 0.9)
+                    SetVertex(vertextemp, i);
             }
         }
 
@@ -3790,7 +3825,7 @@
     static cVector temp2 = new cVector();
     static cVector temp3 = new cVector();
 
-    void GenerateNormal(int index, Vertex v)
+    boolean GenerateNormal(int index, Vertex v)
     {
         //System.out.println("Old normal = " + v.norm);
         LA.setVector(v.norm, 0, 0, 0);
@@ -3819,6 +3854,10 @@
                 LA.vecSub(p/*.pos*/, v/*.pos*/, temp1);
                 LA.vecSub(q/*.pos*/, v/*.pos*/, temp2);
             }
+            else
+            {
+                continue;
+            }
 
             //LA.vecNormalize(temp1);
             //LA.vecNormalize(temp2);
@@ -3829,17 +3868,25 @@
             double s = temp3.length();
             //double c = temp2.dot(temp1);
 
+            if (s == 0)
+                return false;
+            
             float angle = 1; // (float) Math.atan2(s, c);
             //if(angle < 0) angle = -angle;
 
             //LA.vecNormalize(temp3);
             LA.vecScale(temp3, angle / s);
 
+//            if (temp3.dot(v.norm) < 0)
+//                assert(temp3.dot(v.norm) >= 0);
+            
             LA.vecAdd(temp3, v.norm, v.norm);
         }
 
         LA.vecNormalize(v.norm);
     //System.out.println("New normal = " + v.norm);
+        
+        return true;
     }
 
     double Arccos(double x)
@@ -4233,7 +4280,7 @@
         int count = VertexCount();
         
         // mars 2014
-        while (step >= count)
+        while (step > count)
             step /= 10;
         
         for (int i = 0; i < count; i+=step)
@@ -4885,7 +4932,7 @@
                 //colors[i3 + 1] = cp.vertexOcclusion.g;
                 //colors[i3 + 2] = cp.vertexOcclusion.b;
 
-                if ((i % 1000) == 0 && i != 0)
+                if ((i % 100) == 0 && i != 0)
                 {
             CameraPane.theRenderer.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
                     //System.out.println("Color = " + cp.vertexOcclusion.r + ", " + cp.vertexOcclusion.g + ", " + cp.vertexOcclusion.b + "; " + (int)(100.0*i/(positions.length/3)) + "% done");
@@ -4917,7 +4964,7 @@
 
                 v.AO = cp.vertexOcclusion.r;
 
-                if ((i % 1000) == 0 && i != 0)
+                if ((i % 100) == 0 && i != 0)
                 {
             CameraPane.theRenderer.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
                     //System.out.println("Color = " + cp.vertexOcclusion.r + ", " + cp.vertexOcclusion.g + ", " + cp.vertexOcclusion.b + "; " + (int)(100.0*i/(positions.length/3)) + "% done");
@@ -4926,7 +4973,7 @@
             }
         }
 
-        System.out.println("done.");
+        //System.out.println("done.");
 
         cp.renderCamera = keep;
         
@@ -7075,6 +7122,7 @@
             assert(f2.contains(i));
             assert(f3.contains(i));
             
+            // when r is the "center", p is along the boundary
             while (f0.r != i)
             {
                 int t = f0.p;
@@ -7131,60 +7179,91 @@
                 f0 = f3;
                 f3 = t;
             }
-            atleastone = true;
             
+            int va = f0.q;
+            int vb = f0.r;
+            int vc = -1;
+            
+            Face toremove1 = null;
+            Face toremove2 = null;
+                    
+            // f0 is the buffer for the first new triangle,
+            // and otherf is the other upper one.
             Face otherf = null;
             
             if (f1.contains(f0.p))
             {
                 if (f1.p == f0.p)
                 {
+                    assert(false);
                     f0.r = f1.q;
                 }
                 else
                 {
                     assert(f1.q == f0.p);
-                    f0.r = f1.p;
+                    vc = f1.p;
                 }
                 
                 otherf = f2;
-                faces.remove(f1);
-                faces.remove(f3);
+                toremove1 = f1;
+                toremove2 = f3;
             }
             else
             if (f2.contains(f0.p))
             {
                 if (f2.p == f0.p)
                 {
+                    assert(false);
                     f0.r = f2.q;
                 }
                 else
                 {
                     assert(f2.q == f0.p);
-                    f0.r = f2.p;
+                    vc = f2.p;
                 }
                 
                 otherf = f3;
-                faces.remove(f1);
-                faces.remove(f2);
+                toremove1 = f1;
+                toremove2 = f2;
             }
             if (f3.contains(f0.p))
             {
                 if (f3.p == f0.p)
                 {
+//                    assert(false);
+                    new Exception().printStackTrace();
                     f0.r = f3.q;
                 }
                 else
                 {
                     assert(f3.q == f0.p);
-                    f0.r = f3.p;
+                    vc = f3.p;
                 }
                 
                 otherf = f1;
-                faces.remove(f2);
-                faces.remove(f3);
+                toremove1 = f2;
+                toremove2 = f3;
             }
             
+            vertextemp.set(vertices.get(va));
+            vertextemp.sub(vertices.get(vb));
+            vertextemp.normalize();
+            vertextemp2.set(vertices.get(vc));
+            vertextemp2.sub(vertices.get(vb));
+            vertextemp2.normalize();
+
+            if (vertextemp.dot(vertextemp2) > -0.95)
+            {
+                continue;
+            }
+            
+            atleastone = true;
+            
+            f0.r = vc;
+            
+            faces.remove(toremove1);
+            faces.remove(toremove2);
+            
             if (!f0.contains(otherf.p))
             {
                 otherf.r = otherf.p;

--
Gitblit v1.6.2