commit a4af26c11ad8833f5768e976f9be84399f216538
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Wed Apr 20 23:41:34 2022 +0200

    Progress with cubit quaternions.

diff --git a/src/main/java/org/distorted/objectlib/main/TwistyObject.java b/src/main/java/org/distorted/objectlib/main/TwistyObject.java
index b29779e2..28e3dcd7 100644
--- a/src/main/java/org/distorted/objectlib/main/TwistyObject.java
+++ b/src/main/java/org/distorted/objectlib/main/TwistyObject.java
@@ -318,6 +318,8 @@ public abstract class TwistyObject
       FactoryCubit factory = FactoryCubit.getInstance();
       factory.clear();
 
+displayCubitQuats();
+
       mOrigQuat = new Static4D[mNumCubits];
       for(int i=0; i<mNumCubits; i++) mOrigQuat[i] = getCubitQuats(i,mNumLayers);
 
@@ -516,16 +518,64 @@ public abstract class TwistyObject
     return nonBlackIndex;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private boolean sticksOut(Static3D[] faceAxis, float[] dist, float x, float y, float z )
+    {
+    final float MAXERR = 0.05f;
+    int numAxis = dist.length;
+    float NUM = mNumLayers[0];
+
+    for(int i=0; i<numAxis; i++)
+      {
+      Static3D ax = faceAxis[i];
+      float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
+      if( len>NUM*dist[i]+MAXERR ) return true;
+      }
+
+    return false;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private boolean doesNotStickOut(int variant, float[] pos, float[] tmp, Static4D quat)
+    {
+    ObjectShape shape = getObjectShape(variant);
+    float[][] vertices = shape.getVertices();
+    Static3D[] axis = getFaceAxis();
+    float[] dist3D = getDist3D(mNumLayers);
+
+    for( float[] vertex : vertices)
+      {
+      float x = vertex[0];
+      float y = vertex[1];
+      float z = vertex[2];
+
+      QuatHelper.rotateVectorByQuat(tmp, x, y, z, 1, quat);
+
+      float mx = tmp[0] + pos[0];
+      float my = tmp[1] + pos[1];
+      float mz = tmp[2] + pos[2];
+
+      if( sticksOut(axis, dist3D, mx,my,mz) ) return false;
+      }
+
+    return true;
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   protected void displayCubitQuats()
     {
     StringBuilder builder = new StringBuilder();
     float[] tmp = new float[4];
-    float MAXERR = 0.01f;
+    float ERR = 0.01f;
 
     for(int cubit=0; cubit<mNumCubits; cubit++)
       {
+      builder.append(cubit);
+      builder.append(" : ");
+
       int refCubit,variant = getCubitVariant(cubit,mNumLayers);
 
       for(refCubit=0; refCubit<mNumCubits; refCubit++)
@@ -550,10 +600,17 @@ public abstract class TwistyObject
         float dy = tmp[1]-curY;
         float dz = tmp[2]-curZ;
 
-        if( dx>-MAXERR && dx<MAXERR && dy>-MAXERR && dy<MAXERR && dz>-MAXERR && dz<MAXERR )
+        if( dx>-ERR && dx<ERR && dy>-ERR && dy<ERR && dz>-ERR && dz<ERR )
           {
-          builder.append(quat);
-          builder.append(',');
+          if( doesNotStickOut(variant,curpos,tmp,mObjectQuats[quat]) )
+            {
+            builder.append(quat);
+            builder.append(',');
+            }
+          else
+            {
+            android.util.Log.e("D", "cubit: "+cubit+" quat: "+quat+" : center correct, but shape sticks out");
+            }
           }
         }
 
diff --git a/src/main/java/org/distorted/objectlib/objects/TwistyAxis.java b/src/main/java/org/distorted/objectlib/objects/TwistyAxis.java
index bfe6dad4..1657728a 100644
--- a/src/main/java/org/distorted/objectlib/objects/TwistyAxis.java
+++ b/src/main/java/org/distorted/objectlib/objects/TwistyAxis.java
@@ -206,7 +206,16 @@ public class TwistyAxis extends ShapeHexahedron
 
   public Static4D getCubitQuats(int cubit, int[] numLayers)
     {
-    if( mQuatIndex==null ) mQuatIndex = new int[] {0,22,10,17,14,19, 0,22,10,17,14,19, 0,22,10, 0,22,10, 0,14, 0,14,10,19,17,22 };
+    if( mQuatIndex==null )
+      {
+      mQuatIndex = new int[] { 0,22,10,17,14,19,
+                               0,22,10,17,14,19,
+                               0,22,10,
+                               0,22,10,
+                               0,14,
+                               0,14,10,19,17,22
+                             };
+      }
     return mObjectQuats[mQuatIndex[cubit]];
     }
 
diff --git a/src/main/java/org/distorted/objectlib/objects/TwistyCrystal.java b/src/main/java/org/distorted/objectlib/objects/TwistyCrystal.java
index 7408f0af..7711bc8b 100644
--- a/src/main/java/org/distorted/objectlib/objects/TwistyCrystal.java
+++ b/src/main/java/org/distorted/objectlib/objects/TwistyCrystal.java
@@ -75,7 +75,7 @@ public class TwistyCrystal extends TwistyMinx
 
   public Static4D getCubitQuats(int cubit, int[] numLayers)
     {
-    if( mQuatIndex==null ) mQuatIndex = new int[] { 17,12,13,20, 0, 4,25, 5,24,16,
+    if( mQuatIndex==null ) mQuatIndex = new int[] {  0,17,12,13,20, 4,25, 5,24,16,
                                                      9,21, 1,34, 8,11,30,43,26,14,
                                                     15,45,33,28,10, 2,29, 6, 7, 3,
 
@@ -88,11 +88,15 @@ public class TwistyCrystal extends TwistyMinx
 
   public ObjectShape getObjectShape(int variant)
     {
+    float[] blueCenter = mCenterCoords[0];
+    float[] greeCenter = mCenterCoords[1];
+    float[] pinkCenter = mCenterCoords[2];
+
     if( variant==0 )
       {
-      float B = mCenterCoords[0][2]-mCorners[0][2];
+      float B = greeCenter[2]-mCorners[0][2];
       float C = A*mCorners[0][1]+(1.0f-A)*mCorners[2][1];
-      float D = mCenterCoords[0][0];
+      float D = greeCenter[0];
 
       float[][] vertices =
          {
@@ -133,9 +137,9 @@ public class TwistyCrystal extends TwistyMinx
              {X1-X, Y1-Y, Z1-Z},
              {X2-X, Y2-Y, Z2-Z},
              {X3-X, Y3-Y, Z3-Z},
-             { mCenterCoords[0][0]-X, mCenterCoords[0][1]-Y, mCenterCoords[0][2]-Z },
-             { mCenterCoords[1][0]-X, mCenterCoords[1][1]-Y, mCenterCoords[1][2]-Z },
-             { mCenterCoords[8][0]-X, mCenterCoords[8][1]-Y, mCenterCoords[8][2]-Z }
+             { greeCenter[0]-X, greeCenter[1]-Y, greeCenter[2]-Z },
+             { pinkCenter[0]-X, pinkCenter[1]-Y, pinkCenter[2]-Z },
+             { blueCenter[0]-X, blueCenter[1]-Y, blueCenter[2]-Z }
          };
       int[][] indices =
          {
diff --git a/src/main/java/org/distorted/objectlib/objects/TwistyJing.java b/src/main/java/org/distorted/objectlib/objects/TwistyJing.java
index e291ea98..7023d954 100644
--- a/src/main/java/org/distorted/objectlib/objects/TwistyJing.java
+++ b/src/main/java/org/distorted/objectlib/objects/TwistyJing.java
@@ -170,7 +170,9 @@ public class TwistyJing extends ShapeTetrahedron
 
   public Static4D getCubitQuats(int cubit, int[] numLayers)
     {
-    if( mQuatIndex==null ) mQuatIndex = new int[] {0,10,5,8,0,5,8,6,7,9,0,10,7,3};
+    if( mQuatIndex==null ) mQuatIndex = new int[] {0,10,5,8,
+                                                   0,5,8,6,7,9,
+                                                   0,10,7,3};
     return mObjectQuats[mQuatIndex[cubit]];
     }
 
diff --git a/src/main/java/org/distorted/objectlib/objects/TwistyMegaminx.java b/src/main/java/org/distorted/objectlib/objects/TwistyMegaminx.java
index a8ffad3f..da370996 100644
--- a/src/main/java/org/distorted/objectlib/objects/TwistyMegaminx.java
+++ b/src/main/java/org/distorted/objectlib/objects/TwistyMegaminx.java
@@ -50,7 +50,7 @@ public class TwistyMegaminx extends TwistyMinx
 
   private void initializeCenterIndices()
     {
-    mQuatCenterIndices = new int[] { 35, 55, 38, 48, 41, 42, 58, 57,  0, 46, 29, 59 };
+    mQuatCenterIndices = new int[] { 0, 35, 55, 38, 48, 41, 42, 58, 57, 46, 29, 59 };
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/objectlib/objects/TwistyMinx.java b/src/main/java/org/distorted/objectlib/objects/TwistyMinx.java
index a90909c7..4d71956b 100644
--- a/src/main/java/org/distorted/objectlib/objects/TwistyMinx.java
+++ b/src/main/java/org/distorted/objectlib/objects/TwistyMinx.java
@@ -104,6 +104,7 @@ abstract class TwistyMinx extends ShapeDodecahedron
     {
     mCenterMap = new int[][]
          {
+           { 0, 12,  8, 10, 16},
            { 0, 12,  4, 14,  2},
            { 0,  2, 18,  6, 16},
            { 6, 18, 11, 19,  7},
@@ -112,7 +113,6 @@ abstract class TwistyMinx extends ShapeDodecahedron
            { 1, 13,  5, 15,  3},
            { 1,  3, 19,  7, 17},
            {10, 16,  6,  7, 17},
-           { 0, 12,  8, 10, 16},
            { 8, 13,  5,  4, 12},
            { 1, 13,  8, 10, 17},
            { 2, 14,  9, 11, 18},
@@ -129,36 +129,36 @@ abstract class TwistyMinx extends ShapeDodecahedron
     {
     mEdgeMap = new int[][]
          {
-           {  0, 12,  0,  8}, //0
-           { 12,  4,  0,  9},
-           {  4, 14,  0,  4},
-           { 14,  2,  0, 11},
-           {  2,  0,  0,  1},
-           { 14,  9, 11,  4}, //5
-           {  9, 11, 11,  3},
-           { 11, 18, 11,  2},
-           { 18,  2, 11,  1},
-           { 18,  6,  1,  2},
-           {  6, 16,  1,  7}, //10
-           { 16,  0,  1,  8},
-           { 16, 10,  8,  7},
-           { 10,  8,  8, 10},
-           {  8, 12,  8,  9},
+           {  2,  0,  1,  2}, //0
+           {  0, 12,  1,  0},
+           { 12,  4,  1,  9},
+           {  4, 14,  1,  5},
+           { 14,  2,  1, 11},
+           { 14,  9, 11,  5}, //5
+           {  9, 11, 11,  4},
+           { 11, 18, 11,  3},
+           { 18,  2, 11,  2},
+           { 18,  6,  2,  3},
+           {  6, 16,  2,  8}, //10
+           { 16,  0,  2,  0},
+           { 16, 10,  0,  8},
+           { 10,  8,  0, 10},
+           {  8, 12,  0,  9},
            {  8, 13,  9, 10}, //15
-           { 13,  5,  9,  5},
-           {  5,  4,  9,  4},
-           {  5, 15,  4,  5},
-           { 15,  9,  4,  3},
-           { 11, 19,  2,  3}, //20
-           { 19,  7,  2,  6},
-           {  7,  6,  2,  7},
-           {  7, 17,  7,  6},
-           { 17, 10,  7, 10},
-           { 17,  1, 10,  6}, //25
-           {  1,  3,  5,  6},
-           {  3, 19,  3,  6},
-           {  1, 13, 10,  5},
-           {  3, 15,  5,  3},
+           { 13,  5,  9,  6},
+           {  5,  4,  9,  5},
+           {  5, 15,  5,  6},
+           { 15,  9,  5,  4},
+           { 11, 19,  3,  4}, //20
+           { 19,  7,  3,  7},
+           {  7,  6,  3,  8},
+           {  7, 17,  8,  7},
+           { 17, 10,  8, 10},
+           { 17,  1, 10,  7}, //25
+           {  1,  3,  6,  7},
+           {  3, 19,  4,  7},
+           {  1, 13, 10,  6},
+           {  3, 15,  6,  4},
          };
     }
 
@@ -168,7 +168,7 @@ abstract class TwistyMinx extends ShapeDodecahedron
     {
     mQuatEdgeIndices = new int[]
       {
-        17, 18, 19, 20,  0, 56, 25,  5, 24, 16,
+         0, 17, 18, 19, 20, 56, 25,  5, 24, 16,
          9, 44,  1, 34, 35, 27, 41, 50, 26, 54,
         15, 49, 39, 28, 10,  2, 48,  6, 46,  3
       };
diff --git a/src/main/java/org/distorted/objectlib/objects/TwistyPentultimate.java b/src/main/java/org/distorted/objectlib/objects/TwistyPentultimate.java
index b2bbcccc..c0b61e91 100644
--- a/src/main/java/org/distorted/objectlib/objects/TwistyPentultimate.java
+++ b/src/main/java/org/distorted/objectlib/objects/TwistyPentultimate.java
@@ -143,9 +143,8 @@ public class TwistyPentultimate extends TwistyMinx
       {
       mQuatIndex = new int[]
         {
-         0, 29, 59, 48, 18, 53, 22, 49, 11, 54,
-        10, 52, 17, 27, 19, 26,  9, 28, 23, 45,
-         0, 59, 15,  6,  4,  3, 29, 10, 35,  8,  2,  5
+         0,29,59,48,18,53,22,49,11,54,10,52,17,27,19,26, 9,28,23,45,
+         0, 4, 5, 6,26, 3, 2, 7,16,13, 1,19
         };
       }
 
@@ -259,7 +258,7 @@ public class TwistyPentultimate extends TwistyMinx
          };
       int[][] indices =
          {
-             {4,3,2,1,0},
+             {0,1,2,3,4},
              {0,5,1},
              {1,5,2},
              {2,5,3},
diff --git a/src/main/java/org/distorted/objectlib/objects/TwistyStarminx.java b/src/main/java/org/distorted/objectlib/objects/TwistyStarminx.java
index 5b7ac91c..578b2488 100644
--- a/src/main/java/org/distorted/objectlib/objects/TwistyStarminx.java
+++ b/src/main/java/org/distorted/objectlib/objects/TwistyStarminx.java
@@ -94,11 +94,11 @@ public class TwistyStarminx extends TwistyMinx
       {
       mQuatIndex = new int[]
         {
-        17,12,13,20, 0, 4,25, 5,24,16,
+         0,17,12,13,20, 4,25, 5,24,16,
          9,21, 1,34, 8,11,30,43,26,14,
         15,45,33,28,10, 2,29, 6, 7, 3,
 
-        35,55,38,48,41,42,58,57, 0,46,29,59,
+         0,35,55,38,48,41,42,58,57,46,29,59,
 
          0,36,44, 29, 2, 7, 59,20,24, 48,42,58,
         18,13,50, 53,41,43, 22,16,39, 49,33,38,
diff --git a/src/main/java/org/distorted/objectlib/objects/TwistyUltimate.java b/src/main/java/org/distorted/objectlib/objects/TwistyUltimate.java
index cc9626eb..14f07ca3 100644
--- a/src/main/java/org/distorted/objectlib/objects/TwistyUltimate.java
+++ b/src/main/java/org/distorted/objectlib/objects/TwistyUltimate.java
@@ -117,7 +117,9 @@ public class TwistyUltimate extends ShapeDodecahedron
 
   public Static4D getCubitQuats(int cubit, int[] numLayers)
     {
-    if( mQuatIndex==null ) mQuatIndex = new int[] { 0,2,5,6,0,8,2,1,0,5,8,9,1,6 };
+    if( mQuatIndex==null ) mQuatIndex = new int[] { 0,2,5,6,
+                                                    0,8,2,1,
+                                                    0,5,8,9,1,6 };
     return mObjectQuats[mQuatIndex[cubit]];
     }
 
