From f1c718cce66e5651a0dae91375db6ebfaded1a92 Mon Sep 17 00:00:00 2001
From: Normand Briere <nbriere@noware.ca>
Date: Sat, 27 Apr 2019 21:33:41 -0400
Subject: [PATCH] Test unfold UV

---
 BoundaryRep.java |  385 ++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 267 insertions(+), 118 deletions(-)

diff --git a/BoundaryRep.java b/BoundaryRep.java
index 4b1c03c..05b3429 100644
--- a/BoundaryRep.java
+++ b/BoundaryRep.java
@@ -15,7 +15,7 @@
     {
         this(0, 0);
     }
-    
+
     void SaveSupports()
     {
         transientsupport = support;
@@ -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);
@@ -786,7 +790,7 @@
             v.weights[k] = other.ComputeWeight(v, toRoot, k); // (float)(supportsize * normalweight * nz / Math.pow(tx*tx+ty*ty+tz*tz, 1));
             v.totalweight += v.weights[k];
             
-            if (CameraPane.CROWD)
+            if (Globals.CROWD)
             {
       //          System.out.print("weight = " + v.weights[k]);
       //          System.out.println("; totalweight = " + v.totalweight);
@@ -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)
@@ -2635,18 +2661,18 @@
         if (false) // slow && stepout && onein)
         {
             // sound
-            cVector eye = CameraPane.theRenderer.eyeCamera.location;
+            cVector eye = Globals.theRenderer.EyeCamera().location;
 
             Vertex v = GetVertex(0);
 
             tmp.set(v);
             tmp.sub(eye);
 
-            if (CameraPane.framecount - lastsoundtime > 30) // 0.25 secs
+            if (Globals.framecount - lastsoundtime > 30) // 0.25 secs
             {
                 GrafreeD.wav.play((Math.random()+0.5)/Math.max(tmp.length2(),0.2)); //, 1);
 
-                lastsoundtime = CameraPane.framecount;
+                lastsoundtime = Globals.framecount;
             }
             
             stepout = false;
@@ -3129,7 +3155,27 @@
      */
     }
 
-    void GenUV()
+    void UnfoldUV()
+    {
+            for (int i = 0; i < VertexCount(); i++)
+            {
+                Vertex v = GetVertex(i);
+                
+                v.x = v.s;
+                v.y = v.t;
+                v.z = 0;
+                
+                v.norm.x = 0;
+                v.norm.y = 0;
+                v.norm.z = 1;
+                
+                SetVertex(v, i);
+            }
+    }
+    
+    float power = 2;
+    
+    void GenUV() // float power)
     {
         Trim();
         
@@ -3193,6 +3239,115 @@
                 y -= 0.5;
                 z -= 0.5;
                 
+                double ax = Math.abs(x);
+                double ay = Math.abs(y);
+                double max = ax;
+                if (max < ay)
+                {
+                    max = ay;
+                }
+                
+                x /= max;
+                y /= max;
+
+                double angle = Math.acos(Math.abs(z*2));
+                
+                double k = angle / Math.PI * 2;
+                
+                // k == 0 => uv = 0 (center)
+                // k == 1 => uv = -1,1 (border)
+                
+                if (i == 0)
+                        System.out.println("power = " + power);
+                
+                double length1 = (ax+ay)/max;
+                double length2 = Math.sqrt(ax*ax + ay*ay) / max;
+
+                double t = k;
+                
+                t = Math.pow(t, 3);
+                
+                // Interpolate between k/length2 (center) and k (border)
+                k = k / length2 * (1 - t) + k * t;
+                
+                double u = k*x;
+                double v = k*y;
+
+                u /= 2;
+                v /= 2;
+                u += 0.5;
+                v += 0.5;
+                
+                uvmap[i2] = (float) u;
+                uvmap[i2+1] = (float) v;
+            }
+    }
+        
+    void GenUVold(float power)
+    {
+        Trim();
+        
+        cVector boxcenter = null;
+          cVector minima, maxima;
+            minima = new cVector();
+            maxima = new cVector();
+            minima.x = minima.y = minima.z = Double.MAX_VALUE;
+            maxima.x = maxima.y = maxima.z = -Double.MAX_VALUE;
+            for (int i = 0; i < VertexCount(); i++)
+            {
+                Vertex v = GetVertex(i);
+
+                if (minima.x > v.x)
+                {
+                    minima.x = v.x;
+                }
+                if (minima.y > v.y)
+                {
+                    minima.y = v.y;
+                }
+                if (minima.z > v.z)
+                {
+                    minima.z = v.z;
+                }
+
+                if (maxima.x < v.x)
+                {
+                    maxima.x = v.x;
+                }
+                if (maxima.y < v.y)
+                {
+                    maxima.y = v.y;
+                }
+                if (maxima.z < v.z)
+                {
+                    maxima.z = v.z;
+                }
+            }
+
+            boxcenter = new cVector((maxima.x + minima.x) / 2, (maxima.y + minima.y) / 2, (maxima.z + minima.z) / 2);
+            int i2 = 0, i3 = 0;
+            for (int i = 0; i < positions.length/3; i++, i3 += 3, i2 += 2)
+            {
+//                //uvmap[i2] = (float) normals[i3]*0.5f + 0.5f; // v.x;
+//                //uvmap[i2 + 1] = (float) normals[i3+1]*0.5f + 0.5f; //z;
+//                uvmap[i2] = (float) (positions[i3] - boxcenter.x);
+//                uvmap[i2 + 1] = (float) (positions[i3+2] - boxcenter.z);
+//                uvmap[i2] = (float) Math.atan2(positions[i3+1] - boxcenter.y, positions[i3] - boxcenter.x);
+//                uvmap[i2 + 1] = (float)(positions[i3+2] - boxcenter.z);
+                // box UV
+                double x = positions[i3] - minima.x; // - Math.floor(positions[i3]);
+                double y = positions[i3+1] - minima.y; // - Math.floor(positions[i3+1]);
+                double z = positions[i3+2] - minima.z; // - Math.floor(positions[i3+2]);
+
+                // [-1/2, 1/2]
+                x /= maxima.x - minima.x;
+                y /= maxima.y - minima.y;
+                z /= maxima.z - minima.z;
+                
+                x -= 0.5;
+                y -= 0.5;
+                z -= 0.5;
+                
           //      x *= 2;
           //      y *= 2;
           //      z *= 2;
@@ -3219,6 +3374,15 @@
 
                 z = Math.cos(angle/2);
                 
+                assert(z >= 0);
+                assert(z <= 1);
+                
+                /**/
+                //z = Math.pow(z, power); //1.08f);
+                
+                if (i == 0)
+                        System.out.println("power = " + power);
+                
                 // sqrt(k2*x2 + k2*z2 + y2) = length
                 // k2*x2 + k2*z2 = length2 - y2
                 // k2 = (length2 - y2) / (x2 + z2)
@@ -3238,6 +3402,7 @@
                 
                 x *= k;
                 y *= k;
+                /**/
                 
                 double max = Math.abs(x);
                 if (max < Math.abs(y))
@@ -3250,10 +3415,15 @@
                 }
 
             //    max = Math.sqrt(max*2)/2;
+//                double x2 = Math.pow(Math.abs(x), 1/power);
+//                double y2 = Math.pow(Math.abs(y), 1/power);
+//                double z2 = Math.pow(Math.abs(z), 1/power);
+//                max = Math.pow(x2 + y2 + z2, power);
                 
 //                if (!(max > 0))
-                    assert(max > 0);
-                
+                    //assert(max > 0);
+                    assert(max >= 0);
+                                    
                 x /= max;
                 y /= max;
                 z /= max;
@@ -3776,10 +3946,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 +3969,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 +3998,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 +4012,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)
@@ -4381,7 +4572,7 @@
         }
     }
     
-    void CullVertex(javax.media.opengl.GL gl, boolean shadow)
+    void CullVertex(javax.media.opengl.GL glNOTUSED, boolean shadowNOTUSED)
     {
         CameraPane.glu.gluProject(vect5.x,vect5.y,vect5.z,
                 CameraPane.tempmat,0, CameraPane.tempmat2,0,
@@ -4413,14 +4604,14 @@
         // june 2014
 //        Camera parentcam = cam;
 //
-//        if (cam == CameraPane.theRenderer.cameras[0])
+//        if (cam == Globals.theRenderer.cameras[0])
 //        {
-//            parentcam = CameraPane.theRenderer.cameras[1];
+//            parentcam = Globals.theRenderer.cameras[1];
 //        }
 //
-//        if (cam == CameraPane.theRenderer.cameras[1])
+//        if (cam == Globals.theRenderer.cameras[1])
 //        {
-//            parentcam = CameraPane.theRenderer.cameras[0];
+//            parentcam = Globals.theRenderer.cameras[0];
 //        }
     
         gl.glGetDoublev(gl.GL_MODELVIEW_MATRIX, CameraPane.tempmat, 0);
@@ -4846,7 +5037,7 @@
         return verticesCopy;
     }
 
-    void PreprocessOcclusion(CameraPane cp, double[][] transform)
+    void PreprocessOcclusion(iCameraPane cp, double[][] transform)
     {
         if (//!trimmed ||
                 AOdone)
@@ -4855,80 +5046,7 @@
             return;
         }
 
-        Camera keep = cp.renderCamera;
-        cp.renderCamera = localcamera;
-
-        if (trimmed)
-        {
-            float[] colors = new float[positions.length / 3];
-
-            int i3 = 0;
-            for (int i = 0; i < positions.length / 3; i++, i3 += 3)
-            {
-                if (normals[i3] == 0 && normals[i3+1] == 0 && normals[i3+2] == 0)
-                    continue;
-
-                from.set(positions[i3], positions[i3 + 1], positions[i3 + 2]);
-                to.set(positions[i3] + normals[i3],
-                        positions[i3 + 1] + normals[i3 + 1],
-                        positions[i3 + 2] + normals[i3 + 2]);
-                LA.xformPos(from, transform, from);
-                LA.xformPos(to, transform, to); // RIGID ONLY
-                localcamera.setAim(from, to);
-
-                CameraPane.occlusionbuffer.display();
-
-                if (CameraPane.DEBUG_OCCLUSION)
-                    cp.display(); // debug
-
-                colors[i] = cp.vertexOcclusion.r;
-                //colors[i3 + 1] = cp.vertexOcclusion.g;
-                //colors[i3 + 2] = cp.vertexOcclusion.b;
-
-                if ((i % 1000) == 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");
-                        System.out.println((int) (100.0 * i / (positions.length / 3)) + "% (" + i + " of " + (positions.length / 3) + ")");
-                }
-            }
-
-            this.colors = colors;
-        }
-        else
-        {
-            for (int i = 0; i < VertexCount(); i++)
-            {
-                Vertex v = GetVertex(i);
-                
-                if (v.norm.x == 0 && v.norm.y == 0 && v.norm.z == 0)
-                    continue;
-
-                from.set(v.x, v.y, v.z);
-                to.set(v.x+v.norm.x, v.y+v.norm.y, v.z+v.norm.z);
-                LA.xformPos(from, transform, from);
-                LA.xformPos(to, transform, to); // RIGID ONLY
-                localcamera.setAim(from, to);
-
-                CameraPane.occlusionbuffer.display();
-
-                if (CameraPane.DEBUG_OCCLUSION)
-                    cp.display(); // debug
-
-                v.AO = cp.vertexOcclusion.r;
-
-                if ((i % 1000) == 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");
-                        System.out.println((int) (100.0 * i / VertexCount()) + "% (" + i + " of " + VertexCount() + ")");
-                }
-            }
-        }
-
-        System.out.println("done.");
-
-        cp.renderCamera = keep;
+                cp.PrepOcclusion(this, transform);
         
         AOdone = true;
     }
@@ -7075,6 +7193,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,59 +7250,90 @@
                 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))
             {
@@ -7554,7 +7704,7 @@
             s3 = new cVector();
         }
         
-        CameraPane.theRenderer.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
+        Globals.theRenderer.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
         
         try
         {
@@ -7649,7 +7799,7 @@
         {
             if (i++%100 == 0)
             {
-                CameraPane.theRenderer.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
+                Globals.theRenderer.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
                 System.out.println("#faces = " + faces.size());
             //    if (i != 1)
             //        break;
@@ -7693,7 +7843,7 @@
         //Trim(true,cJME.gennormals,true,false); // doesn't work
         Trim(true,false,false,false,false);
         
-        CameraPane.theRenderer.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));
+        Globals.theRenderer.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));
     }
     
     void UpdateIndices(Face face, Face minface)
@@ -8275,9 +8425,6 @@
         return "trim = " + trimmed + "; stripped = " + stripified + "; colors = " + colors + "; faces = " + (faces!=null?faces.size():null) + "; triangles = " + (triangles!=null?triangles.length:null) + "; indices = " + indices;
     }
     
-    static Camera localcamera = new Camera();
-    static cVector from = new cVector();
-    static cVector to = new cVector();
     boolean trimmed = false;
     boolean stripified = false;
     transient boolean AOdone = false;
@@ -8285,8 +8432,10 @@
     /*transient*/ int maxIndexV = 0;
     /*transient*/ int bufV, bufF;
     // Raw version
-    private float[] positions;
-    private float[] normals;
+    //private
+            float[] positions;
+    //private
+            float[] normals;
     float[] colors;
     private float[] uvmap;
     private int[] triangles;

--
Gitblit v1.6.2