commit 8e4a36706401266c8e82639f4c1c9f9ab2e48114
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Aug 20 23:11:45 2021 +0200

    Convert Megaminx to the new scheme.

diff --git a/src/main/java/org/distorted/objects/TwistyMegaminx.java b/src/main/java/org/distorted/objects/TwistyMegaminx.java
index 01c6c94f..6d40c350 100644
--- a/src/main/java/org/distorted/objects/TwistyMegaminx.java
+++ b/src/main/java/org/distorted/objects/TwistyMegaminx.java
@@ -22,6 +22,7 @@ package org.distorted.objects;
 import android.content.res.Resources;
 
 import org.distorted.helpers.FactoryCubit;
+import org.distorted.helpers.ObjectShape;
 import org.distorted.helpers.ObjectSticker;
 import org.distorted.helpers.QuatHelper;
 import org.distorted.library.effect.MatrixEffectQuaternion;
@@ -298,14 +299,71 @@ public class TwistyMegaminx extends TwistyMinx
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  MeshBase createEdgeMesh(int numLayers, float width, float height)
+  ObjectShape getObjectShape(int cubit, int numLayers)
     {
-    double W = width/2;
-    double X = height*SIN_HALFD;
-    double Y = height*SIN18/COS18;
-    double Z = height*COS_HALFD;
+    int variant = getCubitVariant(cubit,numLayers);
+    int numVariants = getNumCubitVariants(numLayers);
 
-    double[][] vertices = new double[][]
+    if( variant==0 )
+      {
+      float width = (numLayers/3.0f)*(0.5f-MEGA_D)/(0.5f*(numLayers-1));
+      float A = (2*SQ3/3)*SIN54;
+      float B = 0.4f;
+      double X = width*COS18*SIN_HALFD;
+      double Y = width*SIN18;
+      double Z = width*COS18*COS_HALFD;
+      int N = numLayers==3 ? 1:0;
+
+      double[][] vertices = new double[][]
+        {
+            { 0.0, 0.0      , 0.0 },
+            {   X,   Y      ,  -Z },
+            { 0.0, 2*Y      ,-2*Z },
+            {  -X,   Y      ,  -Z },
+            { 0.0, 0.0-width, 0.0 },
+            {   X,   Y-width,  -Z },
+            { 0.0, 2*Y-width,-2*Z },
+            {  -X,   Y-width,  -Z },
+        };
+
+      int[][] vertIndexes = new int[][]
+        {
+            {4,5,1,0},
+            {7,4,0,3},
+            {0,1,2,3},
+            {4,5,6,7},
+            {6,5,1,2},
+            {7,6,2,3}
+        };
+
+      float[][] bands    = new float[][]
+        {
+         {0.04f,34,0.3f,0.2f, 3, N, 0},
+         {0.00f, 0,0.0f,0.0f, 2, N, 0}
+        };
+
+      int[] bandIndices   = new int[] { 0,0,0,1,1,1};
+      float[][] corners   = new float[][] { {0.04f,0.10f} };
+      int[] cornerIndices = new int[] { 0,-1,-1,-1,-1,-1,-1,-1 };
+      float[][] centers   = new float[][] { {0.0f, -(float)Math.sqrt(1-A*A)*B,-A*B} };
+      int[] centerIndices = new int[] { 0,-1,-1,-1,-1,-1,-1,-1 };
+
+      return new ObjectShape(vertices,vertIndexes,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
+      }
+    if( variant<numVariants-1 )
+      {
+      int numCubitsPerCorner = numCubitsPerCorner(numLayers);
+      int numCubitsPerEdge   = numCubitsPerEdge(numLayers);
+      int type = computeEdgeType(cubit,numCubitsPerCorner,numCubitsPerEdge);
+      float height= (numLayers/3.0f)*(0.5f-MEGA_D)*COS18/((numLayers-1)*0.5f);
+      float width = (numLayers/3.0f)*2*MEGA_D + 2*type*height*SIN18/COS18;
+
+      double W = width/2;
+      double X = height*SIN_HALFD;
+      double Y = height*SIN18/COS18;
+      double Z = height*COS_HALFD;
+
+      double[][] vertices = new double[][]
         {
             { 0.0,   W   , 0.0 },
             {   X, W+Y   ,  -Z },
@@ -317,7 +375,7 @@ public class TwistyMegaminx extends TwistyMinx
             {  -X,-W-Y   ,  -Z },
         };
 
-    int[][] vertIndexes = new int[][]
+      int[][] vertIndexes = new int[][]
         {
             {4,5,1,0},
             {7,4,0,3},
@@ -327,130 +385,135 @@ public class TwistyMegaminx extends TwistyMinx
             {4,5,6,7}
         };
 
-    int N = numLayers<=5 ? 5 : 3;
+      int N = numLayers<=5 ? 5 : 3;
 
-    float[][] bands     = new float[][]
-      {
+      float[][] bands     = new float[][]
+        {
          {0.04f,34,0.2f,0.2f,N,0,0},
          {0.00f, 0,0.3f,0.2f,2,0,0}
-      };
-    int[] bandIndexes   = new int[] { 0,0,1,1,1,1};
-    float[][] corners   = new float[][] { {0.04f,0.10f} };
-    int[] cornerIndexes = new int[] { -1,-1,-1,-1, -1,-1,-1,-1 };
-    float[][] centers   = new float[][] { {0.0f, 0.0f, (float)(-2*Z)} };
-    int[] centerIndexes = new int[] { -1,-1,-1,-1, -1,-1,-1,-1 };
-
-    FactoryCubit factory = FactoryCubit.getInstance();
-    factory.createNewFaceTransform(vertices,vertIndexes);
-
-    return factory.createRoundedSolid(vertices, vertIndexes,
-                                      bands, bandIndexes,
-                                      corners, cornerIndexes,
-                                      centers, centerIndexes,
-                                      getNumCubitFaces(), null );
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
+        };
+      int[] bandIndices   = new int[] { 0,0,1,1,1,1};
+      float[][] corners   = new float[][] { {0.04f,0.10f} };
+      int[] cornerIndices = new int[] { -1,-1,-1,-1, -1,-1,-1,-1 };
+      float[][] centers   = new float[][] { {0.0f, 0.0f, (float)(-2*Z)} };
+      int[] centerIndices = new int[] { -1,-1,-1,-1, -1,-1,-1,-1 };
 
-  MeshBase createCenterMesh(int numLayers, float width)
-    {
-    final double V = 0.83;   // ??
-    final double ANGLE = V*Math.PI;
-    final double cosA  = Math.cos(ANGLE);
-    final double sinA  = Math.sin(ANGLE);
-
-    float R  = 0.5f*width/COS54;
-    float X1 = R*COS54;
-    float Y1 = R*SIN54;
-    float X2 = R*COS18;
-    float Y2 = R*SIN18;
-
-    double[][] vertices = new double[][]
+      return new ObjectShape(vertices,vertIndexes,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
+      }
+    else
       {
+      float width = 2 * (numLayers/3.0f) * (MEGA_D+(0.5f-MEGA_D)*SIN18);
+      final double V = 0.83;   // ??
+      final double ANGLE = V*Math.PI;
+      final double cosA  = Math.cos(ANGLE);
+      final double sinA  = Math.sin(ANGLE);
+
+      float R  = 0.5f*width/COS54;
+      float X1 = R*COS54;
+      float Y1 = R*SIN54;
+      float X2 = R*COS18;
+      float Y2 = R*SIN18;
+
+      double[][] vertices = new double[][]
+        {
           {-X1,+Y1*sinA, Y1*cosA},
           {-X2,-Y2*sinA,-Y2*cosA},
           {0.0f,-R*sinA, -R*cosA},
           {+X2,-Y2*sinA,-Y2*cosA},
           {+X1,+Y1*sinA, Y1*cosA}
-      };
+        };
 
-    int[][] vertIndexes = new int[][]
-      {
+      int[][] vertIndexes = new int[][]
+        {
           {0,1,2,3,4},
           {0,1,2,3,4}
-      };
+        };
 
-    int N = numLayers==3 ? 4 : 3;
+      int N = numLayers==3 ? 4 : 3;
 
-    float[][] bands = new float[][]
-      {
+      float[][] bands = new float[][]
+        {
          {0.04f,45, R/3,0.2f,N,0,0},
          {0.00f, 0, R/3,0.2f,2,0,0}
-      };
-    int[] bandIndexes   = new int[] { 0,1 };
-    float[][] corners   = new float[][] { {0.04f,0.10f} };
-    int[] cornerIndexes = new int[] { -1,-1,-1,-1, -1 };
-    float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
-    int[] centerIndexes = new int[] { -1,-1,-1,-1, -1 };
-
-    FactoryCubit factory = FactoryCubit.getInstance();
-    factory.createNewFaceTransform(vertices,vertIndexes);
-
-    return factory.createRoundedSolid(vertices, vertIndexes,
-                                      bands, bandIndexes,
-                                      corners, cornerIndexes,
-                                      centers, centerIndexes,
-                                      getNumCubitFaces(), null );
+        };
+
+      int[] bandIndices   = new int[] { 0,1 };
+      float[][] corners   = new float[][] { {0.04f,0.10f} };
+      int[] cornerIndices = new int[] { -1,-1,-1,-1, -1 };
+      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
+      int[] centerIndices = new int[] { -1,-1,-1,-1, -1 };
+
+      return new ObjectShape(vertices,vertIndexes,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
+      }
     }
 
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  MeshBase createCubitMesh(int cubit, int numLayers)
+  private Static4D getQuat(int cubit, int numLayers)
     {
     int numCubitsPerCorner = numCubitsPerCorner(numLayers);
     int numCubitsPerEdge   = numCubitsPerEdge(numLayers);
+
+    return QUATS[getQuat(cubit,numCubitsPerCorner,numCubitsPerEdge)];
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private int getNumCubitVariants(int numLayers)
+    {
     int[] sizes = ObjectList.MEGA.getSizes();
     int variants = sizes.length;
-    int numShapes = 2+(sizes[variants-1]-1)/2;
-    MeshBase mesh;
 
-    if( mMeshes==null ) mMeshes = new MeshBase[numShapes];
+    return 2+(sizes[variants-1]-1)/2;
+    }
 
-    if( cubit < NUM_CORNERS*numCubitsPerCorner )
-      {
-      if( mMeshes[0]==null )
-        {
-        float width = (numLayers/3.0f)*(0.5f-MEGA_D)/(0.5f*(numLayers-1));
-        mMeshes[0] = createCornerMesh(numLayers, width);
-        }
-      mesh = mMeshes[0].copy(true);
-      }
-    else if( cubit<NUM_CORNERS*numCubitsPerCorner + NUM_EDGES*numCubitsPerEdge )
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  int getCubitVariant(int cubit, int numLayers)
+    {
+    int numCubitsPerCorner = numCubitsPerCorner(numLayers);
+
+    if( cubit<NUM_CORNERS*numCubitsPerCorner ) return 0;
+
+    int numCubitsPerEdge   = numCubitsPerEdge(numLayers);
+
+    if( cubit<NUM_CORNERS*numCubitsPerCorner + NUM_EDGES*numCubitsPerEdge )
       {
       int type = computeEdgeType(cubit,numCubitsPerCorner,numCubitsPerEdge);
+      return type+1;
+      }
 
-      if( mMeshes[1+type]==null )
-        {
-        float height= (numLayers/3.0f)*(0.5f-MEGA_D)*COS18/((numLayers-1)*0.5f);
-        float width = (numLayers/3.0f)*2*MEGA_D + 2*type*height*SIN18/COS18;
+    int[] sizes = ObjectList.MEGA.getSizes();
+    int variants = sizes.length;
+    int numShapes = 2+(sizes[variants-1]-1)/2;
 
-        mMeshes[1+type] = createEdgeMesh(numLayers,width,height);
-        }
-      mesh = mMeshes[1+type].copy(true);
-      }
-    else
+    return numShapes-1;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  MeshBase createCubitMesh(int cubit, int numLayers)
+    {
+    int variant = getCubitVariant(cubit,numLayers);
+
+    if( mMeshes==null )
       {
-      if( mMeshes[numShapes-1]==null )
-        {
-        float width = 2 * (numLayers/3.0f) * (MEGA_D+(0.5f-MEGA_D)*SIN18);
-        mMeshes[numShapes-1] = createCenterMesh(numLayers,width);
-        }
+      FactoryCubit factory = FactoryCubit.getInstance();
+      factory.clear();
+      mMeshes = new MeshBase[getNumCubitVariants(numLayers)];
+      }
 
-      mesh = mMeshes[numShapes-1].copy(true);
+    if( mMeshes[variant]==null )
+      {
+      ObjectShape shape = getObjectShape(cubit,numLayers);
+      FactoryCubit factory = FactoryCubit.getInstance();
+      factory.createNewFaceTransform(shape);
+      mMeshes[variant] = factory.createRoundedSolid(shape);
       }
 
-    Static4D q = QUATS[getQuat(cubit,numCubitsPerCorner,numCubitsPerEdge)];
-    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( q, new Static3D(0,0,0) );
+    MeshBase mesh = mMeshes[variant].copy(true);
+    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit,numLayers), new Static3D(0,0,0) );
     mesh.apply(quat,0xffffffff,0);
 
     return mesh;
diff --git a/src/main/java/org/distorted/objects/TwistyMinx.java b/src/main/java/org/distorted/objects/TwistyMinx.java
index 34620d83..e5f6b934 100644
--- a/src/main/java/org/distorted/objects/TwistyMinx.java
+++ b/src/main/java/org/distorted/objects/TwistyMinx.java
@@ -373,63 +373,6 @@ abstract class TwistyMinx extends TwistyObject
     return 2.0f;
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  MeshBase createCornerMesh(int numLayers, float width)
-    {
-    float A = (2*SQ3/3)*SIN54;
-    float B = 0.4f;
-    int   N = numLayers==3 ? 5 : 3;
-    int   E1= numLayers==3 ? 1 : 0;
-    int   E2= numLayers==3 ? 3 : 0;
-
-    double X = width*COS18*SIN_HALFD;
-    double Y = width*SIN18;
-    double Z = width*COS18*COS_HALFD;
-
-    double[][] vertices = new double[][]
-        {
-            { 0.0, 0.0      , 0.0 },
-            {   X,   Y      ,  -Z },
-            { 0.0, 2*Y      ,-2*Z },
-            {  -X,   Y      ,  -Z },
-            { 0.0, 0.0-width, 0.0 },
-            {   X,   Y-width,  -Z },
-            { 0.0, 2*Y-width,-2*Z },
-            {  -X,   Y-width,  -Z },
-        };
-
-    int[][] vertIndexes = new int[][]
-        {
-            {4,5,1,0},
-            {7,4,0,3},
-            {0,1,2,3},
-            {4,5,6,7},
-            {6,5,1,2},
-            {7,6,2,3}
-        };
-
-    float[][] bands     = new float[][]
-      {
-         {0.04f,34,0.3f,0.2f, N, 1, E1},
-         {0.00f, 0,0.0f,0.0f, 2, 1, E2}
-      };
-    int[] bandIndexes   = new int[] { 0,0,0,1,1,1};
-    float[][] corners   = new float[][] { {0.04f,0.10f} };
-    int[] cornerIndexes = new int[] { 0,-1,-1,-1,-1,-1,-1,-1 };
-    float[][] centers   = new float[][] { {0.0f, -(float)Math.sqrt(1-A*A)*B,-A*B} };
-    int[] centerIndexes = new int[] { 0,-1,-1,-1,-1,-1,-1,-1 };
-
-    FactoryCubit factory = FactoryCubit.getInstance();
-    factory.createNewFaceTransform(vertices,vertIndexes);
-
-    return factory.createRoundedSolid(vertices, vertIndexes,
-                                      bands, bandIndexes,
-                                      corners, cornerIndexes,
-                                      centers, centerIndexes,
-                                      getNumCubitFaces(), null );
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private void initializeScrambleTable(int[] first, int numLayers)
diff --git a/src/main/res/raw/mega5.dmesh b/src/main/res/raw/mega5.dmesh
index 0c4837b9..b34fbba5 100644
Binary files a/src/main/res/raw/mega5.dmesh and b/src/main/res/raw/mega5.dmesh differ
