Project

General

Profile

« Previous | Next » 

Revision 31cd7256

Added by Leszek Koltunski about 3 years ago

Convert the second and third object, the Pyraminx and the Diamond, to the new Cubit-creating engine.

View differences:

src/main/java/org/distorted/helpers/FactoryCubit.java
89 89
    boolean flip;
90 90
    }
91 91

  
92
  private static final ArrayList<FaceTransform> mFaceTransform = new ArrayList<>();
92
  private static final ArrayList<FaceTransform> mNewFaceTransf = new ArrayList<>();
93
  private static final ArrayList<FaceTransform> mOldFaceTransf = new ArrayList<>();
93 94
  private static final ArrayList<StickerCoords> mStickerCoords = new ArrayList<>();
94 95

  
95 96
///////////////////////////////////////////////////////////////////////////////////////////////////
......
297 298
    return new MeshJoined(meshes);
298 299
    }
299 300

  
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

  
302
  MeshBase createFacesOcta()
303
    {
304
    MeshBase[] meshes = new MeshPolygon[8];
305

  
306
    float E = 0.75f;
307
    float F = 0.5f;
308
    float[] vertices = { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
309
    float[] bands = computeBands(0.05f,35,F,0.8f,6);
310

  
311
    meshes[0] = new MeshPolygon(vertices, bands, 2,2);
312
    meshes[0].setEffectAssociation(0,1,0);
313
    meshes[1] = meshes[0].copy(true);
314
    meshes[1].setEffectAssociation(0,2,0);
315
    meshes[2] = meshes[0].copy(true);
316
    meshes[2].setEffectAssociation(0,4,0);
317
    meshes[3] = meshes[0].copy(true);
318
    meshes[3].setEffectAssociation(0,8,0);
319
    meshes[4] = meshes[0].copy(true);
320
    meshes[4].setEffectAssociation(0,16,0);
321
    meshes[5] = meshes[0].copy(true);
322
    meshes[5].setEffectAssociation(0,32,0);
323
    meshes[6] = meshes[0].copy(true);
324
    meshes[6].setEffectAssociation(0,64,0);
325
    meshes[7] = meshes[0].copy(true);
326
    meshes[7].setEffectAssociation(0,128,0);
327

  
328
    return new MeshJoined(meshes);
329
    }
330

  
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

  
333
  MeshBase createFacesTetra()
334
    {
335
    MeshBase[] meshes = new MeshBase[4];
336

  
337
    float E = 0.75f;
338
    float F = 0.5f;
339
    float[] vertices = { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
340
    float[] bands = computeBands(0.05f,35,F,0.8f,6);
341

  
342
    meshes[0] = new MeshPolygon(vertices, bands, 2,2);
343
    meshes[0].setEffectAssociation(0,1,0);
344
    meshes[1] = meshes[0].copy(true);
345
    meshes[1].setEffectAssociation(0,2,0);
346
    meshes[2] = meshes[0].copy(true);
347
    meshes[2].setEffectAssociation(0,4,0);
348
    meshes[3] = meshes[0].copy(true);
349
    meshes[3].setEffectAssociation(0,8,0);
350

  
351
    return new MeshJoined(meshes);
352
    }
353

  
354 301
///////////////////////////////////////////////////////////////////////////////////////////////////
355 302

  
356 303
  MeshBase createFacesDino()
......
971 918
    return effect;
972 919
    }
973 920

  
974
///////////////////////////////////////////////////////////////////////////////////////////////////
975

  
976
  VertexEffect[] createVertexEffectsOcta()
977
    {
978
    Static1D alpha = new Static1D((float)(-(180/Math.PI)*Math.asin(SQ3/3)));
979
    Static1D angle1= new Static1D( 90);
980
    Static1D angle2= new Static1D(180);
981
    Static1D angle3= new Static1D(270);
982
    Static3D move1 = new Static3D(0,SQ2/2-SQ3/3,0);
983
    Static3D axisX = new Static3D(1,0,0);
984
    Static3D axisY = new Static3D(0,1,0);
985
    Static3D cent0 = new Static3D(0,0,0);
986
    Static3D cent1 = new Static3D(0,SQ2/2,0);
987
    Static3D flipY = new Static3D( 1,-1, 1);
988
    Static3D scale = new Static3D( 1, 2*SQ3/3, 1);
989

  
990
    VertexEffect[] effect = new VertexEffect[7];
991

  
992
    effect[0] = new VertexEffectScale(scale);
993
    effect[1] = new VertexEffectMove(move1);
994
    effect[2] = new VertexEffectRotate(alpha , axisX, cent1);
995
    effect[3] = new VertexEffectRotate(angle1, axisY, cent0);
996
    effect[4] = new VertexEffectRotate(angle2, axisY, cent0);
997
    effect[5] = new VertexEffectRotate(angle3, axisY, cent0);
998
    effect[6] = new VertexEffectScale(flipY);
999

  
1000
    effect[3].setMeshAssociation ( 34,-1); // apply to meshes 1 & 5
1001
    effect[4].setMeshAssociation ( 68,-1); // apply to meshes 2 & 6
1002
    effect[5].setMeshAssociation (136,-1); // apply to meshes 3 & 7
1003
    effect[6].setMeshAssociation (240,-1); // apply to meshes 4,5,6,7
1004

  
1005
    return effect;
1006
    }
1007

  
1008
///////////////////////////////////////////////////////////////////////////////////////////////////
1009

  
1010
  VertexEffect[] createVertexEffectsTetra()
1011
    {
1012
    Static3D flipZ = new Static3D( 1, 1,-1);
1013
    Static1D alpha = new Static1D((float)(-(180/Math.PI)*Math.asin(SQ3/3)));
1014
    Static1D angle1= new Static1D( 90);
1015
    Static1D angle2= new Static1D(180);
1016
    Static3D move1 = new Static3D(0,SQ2/4-SQ3/6,0);
1017
    Static3D axisX = new Static3D(1,0,0);
1018
    Static3D axisY = new Static3D(0,1,0);
1019
    Static3D axisZ = new Static3D(0,0,1);
1020
    Static3D cent0 = new Static3D(0,0,0);
1021
    Static3D cent1 = new Static3D(0,SQ2/4,0);
1022
    Static3D scale = new Static3D( 1, 2*SQ3/3, 1);
1023

  
1024
    VertexEffect[] effect = new VertexEffect[7];
1025

  
1026
    effect[0] = new VertexEffectScale(scale);
1027
    effect[1] = new VertexEffectRotate(angle2, axisZ, cent0);
1028
    effect[2] = new VertexEffectMove(move1);
1029
    effect[3] = new VertexEffectRotate(alpha , axisX, cent1);
1030
    effect[4] = new VertexEffectScale(flipZ);
1031
    effect[5] = new VertexEffectRotate(angle1, axisY, cent0);
1032
    effect[6] = new VertexEffectRotate(angle2, axisZ, cent0);
1033

  
1034
    effect[4].setMeshAssociation(10,-1); // meshes 1 & 3
1035
    effect[5].setMeshAssociation(12,-1); // meshes 2 & 3
1036
    effect[6].setMeshAssociation(12,-1); // meshes 2 & 3
1037

  
1038
    return effect;
1039
    }
1040

  
1041 921
///////////////////////////////////////////////////////////////////////////////////////////////////
1042 922

  
1043 923
  VertexEffect[] createVertexEffectsDino()
......
1708 1588
    return mesh;
1709 1589
    }
1710 1590

  
1711
///////////////////////////////////////////////////////////////////////////////////////////////////
1712
// SKEWB DIAMOND / PYRAMINX
1713

  
1714
  public MeshBase createOctaMesh()
1715
    {
1716
    MeshBase mesh = createFacesOcta();
1717
    VertexEffect[] effects = createVertexEffectsOcta();
1718
    for( VertexEffect effect : effects ) mesh.apply(effect);
1719

  
1720
    Static3D roundingCenter = new Static3D(0,0,0);
1721
    Static3D[] vertices = new Static3D[6];
1722
    vertices[0] = new Static3D(    0, SQ2/2,    0 );
1723
    vertices[1] = new Static3D( 0.5f,     0, 0.5f );
1724
    vertices[2] = new Static3D(-0.5f,     0, 0.5f );
1725
    vertices[3] = new Static3D(    0,-SQ2/2,    0 );
1726
    vertices[4] = new Static3D(-0.5f,     0,-0.5f );
1727
    vertices[5] = new Static3D( 0.5f,     0,-0.5f );
1728

  
1729
    roundCorners(mesh,roundingCenter,vertices,0.06f,0.20f);
1730

  
1731
    mesh.mergeEffComponents();
1732

  
1733
    return mesh;
1734
    }
1735

  
1736
///////////////////////////////////////////////////////////////////////////////////////////////////
1737

  
1738
  public MeshBase createTetraMesh()
1739
    {
1740
    MeshBase mesh = createFacesTetra();
1741
    VertexEffect[] effects = createVertexEffectsTetra();
1742
    for( VertexEffect effect : effects ) mesh.apply(effect);
1743

  
1744
    Static3D roundingCenter = new Static3D(0,0,0);
1745
    Static3D[] verticesRound = new Static3D[4];
1746
    verticesRound[0] = new Static3D(-0.5f,+SQ2/4,   0 );
1747
    verticesRound[1] = new Static3D(+0.5f,+SQ2/4,   0 );
1748
    verticesRound[2] = new Static3D(    0,-SQ2/4,+0.5f);
1749
    verticesRound[3] = new Static3D(    0,-SQ2/4,-0.5f);
1750
    roundCorners(mesh,roundingCenter,verticesRound,0.08f,0.15f);
1751

  
1752
    mesh.mergeEffComponents();
1753
    mesh.addEmptyTexComponent();
1754
    mesh.addEmptyTexComponent();
1755
    mesh.addEmptyTexComponent();
1756
    mesh.addEmptyTexComponent();
1757

  
1758
    return mesh;
1759
    }
1760

  
1761 1591
///////////////////////////////////////////////////////////////////////////////////////////////////
1762 1592
// DINO
1763 1593

  
......
2277 2107

  
2278 2108
///////////////////////////////////////////////////////////////////////////////////////////////////
2279 2109

  
2280
  private void constructNew(FaceTransform info, final double[][] vert3D)
2110
  private FaceTransform constructNewTransform(final double[][] vert3D)
2281 2111
    {
2112
    FaceTransform ft = new FaceTransform();
2113

  
2282 2114
    // compute center of gravity
2283
    info.vx = 0.0f;
2284
    info.vy = 0.0f;
2285
    info.vz = 0.0f;
2115
    ft.vx = 0.0f;
2116
    ft.vy = 0.0f;
2117
    ft.vz = 0.0f;
2286 2118
    int len = vert3D.length;
2287 2119

  
2288 2120
    for (double[] vert : vert3D)
2289 2121
      {
2290
      info.vx += vert[0];
2291
      info.vy += vert[1];
2292
      info.vz += vert[2];
2122
      ft.vx += vert[0];
2123
      ft.vy += vert[1];
2124
      ft.vz += vert[2];
2293 2125
      }
2294 2126

  
2295
    info.vx /= len;
2296
    info.vy /= len;
2297
    info.vz /= len;
2127
    ft.vx /= len;
2128
    ft.vy /= len;
2129
    ft.vz /= len;
2298 2130

  
2299 2131
    // move all vertices so that their center of gravity is at (0,0,0)
2300 2132
    for (int i=0; i<len; i++)
2301 2133
      {
2302
      vert3D[i][0] -= info.vx;
2303
      vert3D[i][1] -= info.vy;
2304
      vert3D[i][2] -= info.vz;
2134
      vert3D[i][0] -= ft.vx;
2135
      vert3D[i][1] -= ft.vy;
2136
      vert3D[i][2] -= ft.vz;
2305 2137
      }
2306 2138

  
2307 2139
    // find 3 non-colinear vertices
......
2367 2199
      }
2368 2200

  
2369 2201
    // fit the whole thing in a square and remember the scale & 2D vertices
2370
    fitInSquare(info, vert3D);
2202
    fitInSquare(ft, vert3D);
2371 2203

  
2372 2204
    // remember the rotation
2373
    info.qx =-mQuat1[0];
2374
    info.qy =-mQuat1[1];
2375
    info.qz =-mQuat1[2];
2376
    info.qw = mQuat1[3];
2205
    ft.qx =-mQuat1[0];
2206
    ft.qy =-mQuat1[1];
2207
    ft.qz =-mQuat1[2];
2208
    ft.qw = mQuat1[3];
2209

  
2210
    return ft;
2377 2211
    }
2378 2212

  
2379 2213
///////////////////////////////////////////////////////////////////////////////////////////////////
......
2421 2255
    }
2422 2256

  
2423 2257
///////////////////////////////////////////////////////////////////////////////////////////////////
2258
// valid for 0<angle<2*PI
2424 2259

  
2425 2260
  private double computeSinHalf(double cos)
2426 2261
    {
......
2428 2263
    }
2429 2264

  
2430 2265
///////////////////////////////////////////////////////////////////////////////////////////////////
2266
// valid for 0<angle<2*PI
2431 2267

  
2432 2268
  private double computeCosHalf(double sin, double cos)
2433 2269
    {
......
2681 2517
    for(int i=0; i<numTexToBeAdded; i++ ) mesh.addEmptyTexComponent();
2682 2518
    }
2683 2519

  
2520
///////////////////////////////////////////////////////////////////////////////////////////////////
2521

  
2522
  private void printTransform(FaceTransform f)
2523
    {
2524
    android.util.Log.e("D", "q=("+f.qx+", "+f.qy+", "+f.qz+", "+f.qw+") v=("
2525
                       +f.vx+", "+f.vy+", "+f.vz+") scale="+f.scale+" sticker="+f.sticker);
2526
    }
2527

  
2684 2528
///////////////////////////////////////////////////////////////////////////////////////////////////
2685 2529
// PUBLIC
2686 2530

  
......
2713 2557

  
2714 2558
  public void printFaceTransform()
2715 2559
    {
2716
    android.util.Log.d("D", "---- FACE TRANSFORM ---");
2560
    android.util.Log.d("D", "---- OLD FACE TRANSFORM ---");
2717 2561

  
2718
    int faces = mFaceTransform.size();
2562
    int oldfaces = mOldFaceTransf.size();
2719 2563

  
2720
    for(int f=0; f<faces; f++)
2564
    for(int f=0; f<oldfaces; f++)
2721 2565
      {
2722
      FaceTransform info = mFaceTransform.get(f);
2566
      printTransform(mOldFaceTransf.get(f));
2567
      }
2568

  
2569
    android.util.Log.d("D", "---- NEW FACE TRANSFORM ---");
2570

  
2571
    int newfaces = mNewFaceTransf.size();
2723 2572

  
2724
      android.util.Log.e("D", "q=("+info.qx+", "+info.qy+", "+info.qz+", "+info.qw+") v=("
2725
                       +info.vx+", "+info.vy+", "+info.vz+") scale="+info.scale+" sticker="+info.sticker);
2573
    for(int f=0; f<newfaces; f++)
2574
      {
2575
      printTransform(mNewFaceTransf.get(f));
2726 2576
      }
2727 2577
    }
2728 2578

  
......
2731 2581
  public void clear()
2732 2582
    {
2733 2583
    mStickerCoords.clear();
2734
    mFaceTransform.clear();
2584
    mNewFaceTransf.clear();
2585
    mOldFaceTransf.clear();
2735 2586
    }
2736 2587

  
2737 2588
///////////////////////////////////////////////////////////////////////////////////////////////////
2738 2589

  
2739 2590
  public void createNewFaceTransform( final double[][] vertices, final int[][] indexes)
2740 2591
    {
2741
    mFaceTransform.clear();
2592
    FaceTransform ft;
2593
    int numNew = mNewFaceTransf.size();
2594

  
2595
    for(int i=0; i<numNew; i++)
2596
      {
2597
      ft = mNewFaceTransf.remove(0);
2598
      mOldFaceTransf.add(ft);
2599
      }
2742 2600

  
2743 2601
    int numFaces = indexes.length;
2744
    FaceTransform oldInfo;
2602
    int numOld = mOldFaceTransf.size();
2745 2603

  
2746
    for(int face=0; face<numFaces; face++)
2604
    for (int face=0; face<numFaces; face++)
2747 2605
      {
2748
      FaceTransform newInfo = new FaceTransform();
2749
      int[] index = indexes[face];
2750
      double[][] vert = constructVert(vertices,index);
2751
      constructNew(newInfo,vert);
2606
      boolean collapsed = false;
2607

  
2608
      double[][] vert = constructVert(vertices, indexes[face]);
2609
      FaceTransform newT = constructNewTransform(vert);
2610

  
2611
      for (int old=0; !collapsed && old<numOld; old++)
2612
        {
2613
        ft = mOldFaceTransf.get(old);
2614
        if (successfullyCollapsedStickers(newT, ft)) collapsed = true;
2615
        }
2752 2616

  
2753
      for(int previous=0; previous<face; previous++)
2617
      for (int pre=0; !collapsed && pre<face; pre++)
2754 2618
        {
2755
        oldInfo = mFaceTransform.get(previous);
2756
        if( successfullyCollapsedStickers(newInfo,oldInfo) ) break;
2619
        ft = mNewFaceTransf.get(pre);
2620
        if (successfullyCollapsedStickers(newT, ft)) collapsed = true;
2757 2621
        }
2758 2622

  
2759
      mFaceTransform.add(newInfo);
2623
      mNewFaceTransf.add(newT);
2760 2624
      }
2761 2625
    }
2762 2626

  
......
2775 2639

  
2776 2640
    for(int face=0; face<numFaces; face++)
2777 2641
      {
2778
      fInfo = mFaceTransform.get(face);
2642
      fInfo = mNewFaceTransf.get(face);
2779 2643
      sInfo = mStickerCoords.get(fInfo.sticker);
2780 2644

  
2781 2645
      double[] verts = sInfo.vertices;
......
2795 2659
    for(int face=0; face<numFaces; face++)
2796 2660
      {
2797 2661
      int assoc = (1<<face);
2798
      fInfo = mFaceTransform.get(face);
2662
      fInfo = mNewFaceTransf.get(face);
2799 2663

  
2800 2664
      float vx = (float)fInfo.vx;
2801 2665
      float vy = (float)fInfo.vy;

Also available in: Unified diff