commit d2e06841aa7a91105212d9c76f86d2fed7e4937b
Author: leszek <leszek@koltunski.pl>
Date:   Sun Sep 10 23:54:31 2023 +0200

    progress with TwistyBandagedMegaminx.

diff --git a/src/main/java/org/distorted/objectlib/bandaged/BandagedCubit.java b/src/main/java/org/distorted/objectlib/bandaged/BandagedCubit.java
index c872512b..01dc2551 100644
--- a/src/main/java/org/distorted/objectlib/bandaged/BandagedCubit.java
+++ b/src/main/java/org/distorted/objectlib/bandaged/BandagedCubit.java
@@ -47,6 +47,7 @@ public class BandagedCubit
 
     private float mUnscaledX, mUnscaledY, mUnscaledZ;
     private float[] mPosition;
+    private float[][] mRotRow;
     private boolean mIsAttached;
     private long mMarkedEffectID;
 
@@ -90,6 +91,12 @@ public class BandagedCubit
         Static4D q = fact.getQuaternion(i);
         QuatHelper.rotateVectorByQuat(mTmp,0,0,1,0,q);
         int index = mObject.computeMapsIndex(mTmp);
+
+        if( index>=mTextureMaps.length )
+          {
+          android.util.Log.e("D", "index="+index+" texMapLen="+mTextureMaps.length);
+          }
+
         maps[i] = mTextureMaps[index];
         }
 
@@ -127,6 +134,7 @@ public class BandagedCubit
       mObject = object;
       mRoundCorners = roundCorners;
       mPosition = position;
+      mRotRow = mObject.getRotRows(mPosition);
       mIsAttached = true;
       mMarkedEffectID = -1;
 
@@ -165,6 +173,7 @@ public class BandagedCubit
       System.arraycopy(mPosition, 0, tmpPosition,    0, len1);
       System.arraycopy(position , 0, tmpPosition, len1, len2);
       mPosition = tmpPosition;
+      mRotRow = mObject.getRotRows(mPosition);
 
       computeMove(mPosition);
       MeshBase mesh = mObject.createMesh(mPosition,mRoundCorners);
@@ -256,6 +265,13 @@ public class BandagedCubit
       return mPosition;
       }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    public float[][] getRotRow()
+      {
+      return mRotRow;
+      }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
     public DistortedNode getNode()
diff --git a/src/main/java/org/distorted/objectlib/bandaged/BandagedObject.java b/src/main/java/org/distorted/objectlib/bandaged/BandagedObject.java
index 1182a6e6..e818b17b 100644
--- a/src/main/java/org/distorted/objectlib/bandaged/BandagedObject.java
+++ b/src/main/java/org/distorted/objectlib/bandaged/BandagedObject.java
@@ -23,7 +23,9 @@ public abstract class BandagedObject
     private final DistortedScreen mScreen;
     private final float[][] mFaceAxis;
     private BandagedCubit[] mCubits;
+    private final float[][] mRotAxis;
 
+    float[][] mCuts;
     final int[] mSize;
     final float mDist2D;
 
@@ -32,10 +34,11 @@ public abstract class BandagedObject
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-   BandagedObject(DistortedScreen screen, int numRotAxis)
+   BandagedObject(DistortedScreen screen)
      {
      mScreen = screen;
-     mSize = new int[numRotAxis];
+     mRotAxis = getRotAxis();
+     mSize = new int[mRotAxis.length];
      mDist2D = getDist2D();
      Static3D[] axis = getFaceAxis();
      int numAxis = axis.length;
@@ -45,6 +48,7 @@ public abstract class BandagedObject
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
+   abstract float[][] getRotAxis();
    abstract float getDist2D();
    abstract int[] getColors();
    abstract float[][][] getPositions();
@@ -59,6 +63,38 @@ public abstract class BandagedObject
    public abstract Static3D[] getFaceAxis();
    public abstract float getScreenRatio();
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private float getRotRow(int ax, float d)
+    {
+    float[] cuts = mCuts[ax];
+    int numCuts = cuts.length;
+    for(int c=0; c<numCuts; c++) if( d<=cuts[c] ) return c;
+    return numCuts;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  float[][] getRotRows(float[] pos)
+    {
+    int num = pos.length/3;
+    int numAx = mRotAxis.length;
+    float[][] ret = new float[num][numAx];
+
+    for(int p=0; p<num; p++)
+      for(int a=0; a<numAx; a++)
+        {
+        float[] ax = mRotAxis[a];
+        float x = pos[3*p];
+        float y = pos[3*p+1];
+        float z = pos[3*p+2];
+        float d = ax[0]*x + ax[1]*y + ax[2]*z;
+        ret[p][a] = getRotRow(a,d);
+        }
+
+    return ret;
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
    public void createCubits(Static4D quatT, Static4D quatA, Static3D scale)
@@ -69,7 +105,7 @@ public abstract class BandagedObject
 
      for(float[][] po : pos)
         for(float[] p : po)
-           mCubits[c++]=new BandagedCubit(this, p, quatT, quatA, scale, false);
+          mCubits[c++] = new BandagedCubit(this, p, quatT, quatA, scale, false);
      }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -238,31 +274,43 @@ public abstract class BandagedObject
     float y = point3D[1]*mMax;
     float z = point3D[2]*mMax;
 
-    int minIndex = -1;
-    float minDist = Float.MAX_VALUE;
+    int numAxis = mRotAxis.length;
+    float[] rotRow = new float[numAxis];
+
+    for(int a=0; a<numAxis; a++)
+      {
+      float[] ax = mRotAxis[a];
+      float d = ax[0]*x + ax[1]*y + ax[2]*z;
+      rotRow[a] = getRotRow(a,d);
+      }
+
+    int diff = Integer.MAX_VALUE;
+    int currBest = -1;
 
     for(int c=0; c<mNumCubits; c++)
       if( mCubits[c].isAttached() )
         {
-        float[] pos = mCubits[c].getPosition();
-        int len = pos.length/3;
+        float[][] cubitRotRow = mCubits[c].getRotRow();
 
-        for(int p=0; p<len; p++)
+        for(float[] row : cubitRotRow)
           {
-          float dx = pos[3*p  ]-x;
-          float dy = pos[3*p+1]-y;
-          float dz = pos[3*p+2]-z;
-          float dist = dx*dx + dy*dy + dz*dz;
+          int currDiff = 0;
+
+          for(int a=0; a<numAxis; a++)
+            {
+            float d = rotRow[a]-row[a];
+            currDiff += d*d;
+            }
 
-          if( dist<minDist )
+          if( currDiff<diff )
             {
-            minDist = dist;
-            minIndex = c;
+            diff = currDiff;
+            currBest = c;
             }
           }
         }
 
-    return minIndex;
+    return currBest;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/objectlib/bandaged/BandagedObjectCuboid.java b/src/main/java/org/distorted/objectlib/bandaged/BandagedObjectCuboid.java
index ce02eaf3..61735ba8 100644
--- a/src/main/java/org/distorted/objectlib/bandaged/BandagedObjectCuboid.java
+++ b/src/main/java/org/distorted/objectlib/bandaged/BandagedObjectCuboid.java
@@ -22,6 +22,39 @@ import org.distorted.objectlib.touchcontrol.TouchControlHexahedron;
 
 public class BandagedObjectCuboid extends BandagedObject
 {
+  private float[][] getCuts(int[] numLayers)
+    {
+    float[][] ret = new float[3][];
+
+    for(int axis=0; axis<3; axis++)
+      {
+      int len = numLayers[axis];
+      float start = (2-len)*0.5f;
+
+      if( len>=2 )
+        {
+        ret[axis] = new float[len-1];
+        for(int i=0; i<len-1; i++) ret[axis][i] = start+i;
+        }
+      }
+
+    return ret;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  float[][] getRotAxis()
+    {
+    return new float[][]
+      {
+        {1,0,0},
+        {0,1,0},
+        {0,0,1},
+      };
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
   float getDist2D()
      {
      return 0.5f;
@@ -62,7 +95,7 @@ public class BandagedObjectCuboid extends BandagedObject
 
   public BandagedObjectCuboid(DistortedScreen screen)
      {
-     super(screen,3);
+     super(screen);
      }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -138,6 +171,7 @@ public class BandagedObjectCuboid extends BandagedObject
         mSize[2] = z;
         mMax = x>y ? Math.max(x,z) : Math.max(y,z);
         mNumCubits = ( x<=1 || y<=1 || z<=1 ) ? x*y*z : x*y*z-(x-2)*(y-2)*(z-2);
+        mCuts = getCuts(mSize);
         return true;
         }
 
diff --git a/src/main/java/org/distorted/objectlib/bandaged/BandagedObjectMegaminx.java b/src/main/java/org/distorted/objectlib/bandaged/BandagedObjectMegaminx.java
index f5437d78..f6117b40 100644
--- a/src/main/java/org/distorted/objectlib/bandaged/BandagedObjectMegaminx.java
+++ b/src/main/java/org/distorted/objectlib/bandaged/BandagedObjectMegaminx.java
@@ -10,7 +10,9 @@
 package org.distorted.objectlib.bandaged;
 
 import static org.distorted.objectlib.main.TwistyObject.SQ5;
+import static org.distorted.objectlib.touchcontrol.TouchControlDodecahedron.C2;
 import static org.distorted.objectlib.touchcontrol.TouchControlDodecahedron.COS54;
+import static org.distorted.objectlib.touchcontrol.TouchControlDodecahedron.LEN;
 import static org.distorted.objectlib.touchcontrol.TouchControlDodecahedron.SIN54;
 
 import org.distorted.library.main.DistortedScreen;
@@ -35,6 +37,46 @@ public class BandagedObjectMegaminx extends BandagedObject
   private static final int NUM_CENTERS = 12;
   private static final int NUM_EDGES   = 30;
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private float[][] getCuts(int[] numLayers)
+    {
+    int numL = numLayers[0];
+    int distL = numL;
+    boolean megaminx = isMegaminx(numL);
+    float dist = megaminx ? 0.5f-MEGA_D : 0.5f;
+    if( (numL%2)==0 ) numL++;
+
+    float[][] ret = new float[6][numL-1];
+    float D = distL*TouchControlDodecahedron.DIST3D;
+    float X = 2*D/(2+SIN18);  // height of the 'upper' part of a dodecahedron, i.e. put it on a table,
+                              // its height is then 2D, it has one 'lower' part of height X, one
+                              // 'middle' part of height Y and one upper part of height X again.
+    int num = (numL-1)/2;
+    float G = X*dist/num;     // height of one Layer
+
+    for(int i=0; i<num; i++)
+      {
+      float cut = -D + (i+0.85f)*G;  // 0.85? not fully correct; attempt to make it
+                                     // easier to rotate the outer layers
+      int j = 2*num-1-i;
+      ret[0][i] =  cut;
+      ret[0][j] = -cut;
+      ret[1][i] =  cut;
+      ret[1][j] = -cut;
+      ret[2][i] =  cut;
+      ret[2][j] = -cut;
+      ret[3][i] =  cut;
+      ret[3][j] = -cut;
+      ret[4][i] =  cut;
+      ret[4][j] = -cut;
+      ret[5][i] =  cut;
+      ret[5][j] = -cut;
+      }
+
+    return ret;
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // The pair (distance,angle) defines a point P in R^2 in polar coordinate system. Let V be the vector
 // from the center of the coordinate system to P.
@@ -167,9 +209,9 @@ public class BandagedObjectMegaminx extends BandagedObject
 
       case 5: float x5_0 = 2.5f*(0.5f-MEGA_D);
               float x5_1 = 2.5f*(0.5f+MEGA_D);
-              float x5_2 = 2.5f*(0.5f-MEGA_D)*COS18;
-              float x5_3 = x5_1 + 2.5f*(0.5f-MEGA_D)*SIN18;
-              float x5_4 = 2.5f*SIN54 - x5_2;
+              float x5_2 = x5_0*COS18;
+              float x5_3 = x5_1 + x5_0*SIN18;
+              float x5_4 = 2.5f*SIN54/COS54 - x5_2;
               float d5_0 = len-x5_0*x5_0;
               float d5_1 = len-x5_1*x5_1;
               float d5_2 = len-x5_2*x5_2;
@@ -186,6 +228,21 @@ public class BandagedObjectMegaminx extends BandagedObject
     return false;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  float[][] getRotAxis()
+    {
+    return new float[][]
+      {
+         {    C2/LEN, SIN54/LEN,    0      },
+         {   -C2/LEN, SIN54/LEN,    0      },
+         { 0        ,    C2/LEN, SIN54/LEN },
+         { 0        ,   -C2/LEN, SIN54/LEN },
+         { SIN54/LEN,    0     ,    C2/LEN },
+         { SIN54/LEN,    0     ,   -C2/LEN }
+      };
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   float[][][] getPositions()
@@ -229,7 +286,7 @@ public class BandagedObjectMegaminx extends BandagedObject
 
   public BandagedObjectMegaminx(DistortedScreen screen)
     {
-    super(screen,6);
+    super(screen);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -269,6 +326,7 @@ public class BandagedObjectMegaminx extends BandagedObject
 
       mMax = x;
       mNumCubits = numCubits(x);
+      mCuts = getCuts(mSize);
       return true;
       }
 
diff --git a/src/main/java/org/distorted/objectlib/bandaged/BandagedObjectPyraminx.java b/src/main/java/org/distorted/objectlib/bandaged/BandagedObjectPyraminx.java
index 583b06e6..729d1300 100644
--- a/src/main/java/org/distorted/objectlib/bandaged/BandagedObjectPyraminx.java
+++ b/src/main/java/org/distorted/objectlib/bandaged/BandagedObjectPyraminx.java
@@ -30,6 +30,38 @@ public class BandagedObjectPyraminx extends BandagedObject
     return face>1;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private float[][] getCuts(int[] numLayers)
+    {
+    int numL = numLayers[0];
+    float[][] ret = new float[4][numL-1];
+
+    for(int i=0; i<numL-1; i++)
+      {
+      float cut = (1.0f+i-numL/4.0f)*(SQ6/3);
+      ret[0][i] = cut;
+      ret[1][i] = cut;
+      ret[2][i] = cut;
+      ret[3][i] = cut;
+      }
+
+    return ret;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  float[][] getRotAxis()
+    {
+    return new float[][]
+      {
+        {     0,-SQ3/3,-SQ6/3 },
+        {     0,-SQ3/3, SQ6/3 },
+        { SQ6/3, SQ3/3,     0 },
+        {-SQ6/3, SQ3/3,     0 },
+      };
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   float getDist2D()
@@ -72,7 +104,7 @@ public class BandagedObjectPyraminx extends BandagedObject
 
   public BandagedObjectPyraminx(DistortedScreen screen)
     {
-    super(screen,4);
+    super(screen);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -112,6 +144,7 @@ public class BandagedObjectPyraminx extends BandagedObject
       int numOcta = FactoryBandagedPyraminx.numEntriesInEmptyLattice(x-1);
       int numTetra= FactoryBandagedPyraminx.numEntriesInEmptyLattice(x);
       mNumCubits = numOcta + numTetra;
+      mCuts = getCuts(mSize);
       return true;
       }
 
diff --git a/src/main/java/org/distorted/objectlib/helpers/ObjectSignature.java b/src/main/java/org/distorted/objectlib/helpers/ObjectSignature.java
index ff712c83..239535ee 100644
--- a/src/main/java/org/distorted/objectlib/helpers/ObjectSignature.java
+++ b/src/main/java/org/distorted/objectlib/helpers/ObjectSignature.java
@@ -93,13 +93,13 @@ public class ObjectSignature implements Comparable<ObjectSignature>
 // the signature must contain ceil( 12*(N-1)^2 / 64 ) bytes.
 // a pyraminx of size N has 4 + 6*(N-1)(N-2) places two adjacent cubies can be 'glued' so much less.
 
-// Gigaminx has 480 such places (so more if N<=7)
+// Gigaminx has 540 such places (so more if N<=7)
 
   private static int computeNum()
     {
     int max = MAX_SUPPORTED_SIZE-1;
     int ret = (int)(0.99f + (12*max*max)/64.0f);
-    int giga= (int)(0.99f + (       480)/64.0f);
+    int giga= (int)(0.99f + (       540)/64.0f);
 
     return Math.max(ret,giga);
     }
@@ -1038,8 +1038,6 @@ public class ObjectSignature implements Comparable<ObjectSignature>
 
     ArrayList<float[]> mTouch = new ArrayList<>();
 
-    int t=0;
-
     for(int i=0; i<numCubits; i++)
       {
       float[] first = getPosition(i,centers,numVariants);
@@ -1062,10 +1060,6 @@ public class ObjectSignature implements Comparable<ObjectSignature>
           float zc = (tz+oz)/2;
 
           float[] touch = new float[] {xc, yc, zc};
-
-          android.util.Log.e("D", "cubit touch: "+t+" : "  +xc+" "+yc+" "+zc );
-          t++;
-
           mTouch.add(touch);
           }
         }
