Project

General

Profile

Download (6.18 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / helpers / ObjectShape.java @ aacf5e27

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objectlib.helpers;
11

    
12
import org.distorted.library.helpers.QuatHelper;
13
import org.distorted.library.type.Static3D;
14
import org.distorted.library.type.Static4D;
15
import org.distorted.objectlib.main.TwistyObject;
16

    
17
///////////////////////////////////////////////////////////////////////////////////////////////////
18

    
19
public class ObjectShape
20
  {
21
  private static final float[] mTmp1 = new float[4];
22
  private static final float[] mTmp2 = new float[4];
23

    
24
  private final float[][] mVertices;
25
  private final int[][] mVertIndices;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
  public ObjectShape(float[][] vertices, int[][] vertIndices)
30
    {
31
    mVertices   = vertices;
32
    mVertIndices= vertIndices;
33
    }
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
  public float[][] getVertices()
38
    {
39
    return mVertices;
40
    }
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  public int[][] getVertIndices()
45
    {
46
    return mVertIndices;
47
    }
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
  public static int computeNumComponents(ObjectShape[] shapes)
52
    {
53
    int ret = 0;
54

    
55
    for( ObjectShape shape : shapes )
56
      {
57
      int numShape = shape.mVertIndices.length;
58
      if( numShape>ret ) ret = numShape;
59
      }
60

    
61
    return ret;
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
// take vertex, apply quat, apply move, write to output
66

    
67
  private static void computeVertex(float[] output, float[] vertex, float[] move, Static4D quat)
68
    {
69
    QuatHelper.rotateVectorByQuat(mTmp2,vertex[0],vertex[1],vertex[2],1.0f,quat);
70

    
71
    int numMoves = move.length/3;
72
    float moveX=0.0f, moveY=0.0f, moveZ=0.0f;
73

    
74
    for(int m=0; m<numMoves; m++)
75
      {
76
      moveX += move[3*m  ];
77
      moveY += move[3*m+1];
78
      moveZ += move[3*m+2];
79
      }
80

    
81
    output[0] = mTmp2[0] + moveX/numMoves;
82
    output[1] = mTmp2[1] + moveY/numMoves;
83
    output[2] = mTmp2[2] + moveZ/numMoves;
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  private static boolean vertInFace(float[] vertex, Static3D faceAxis, float dist)
89
    {
90
    final float MAX_ERROR = 0.04f;  // Rex Cube requires this
91

    
92
    float x= faceAxis.get0();
93
    float y= faceAxis.get1();
94
    float z= faceAxis.get2();
95

    
96
    float a = vertex[0]*x + vertex[1]*y + vertex[2]*z;
97
    float diff = a - dist;
98

    
99
    return diff>-MAX_ERROR && diff<MAX_ERROR;
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  private static boolean indexContains(int[] indices, int index)
105
    {
106
    for( int j : indices )
107
      if( j==index ) return true;
108

    
109
    return false;
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  private static int[] computeCubitFaceColors(ObjectShape shape, float[] move, Static4D quat, Static3D[] faceAxis, float[] dist3D, float size)
115
    {
116
    float[][] vertices = shape.getVertices();
117
    int[][] indices = shape.getVertIndices();
118
    int numVert = vertices.length;
119
    int numFaces = faceAxis.length;
120
    int numIndices = indices.length;
121
    int[] cubitFaceColor = new int[numIndices];
122
    for(int face=0; face<numIndices; face++) cubitFaceColor[face] = 0xffffffff;
123

    
124
    for(int vert=0; vert<numVert; vert++)
125
      {
126
      computeVertex(mTmp1,vertices[vert],move,quat);
127
      int vertBelongsBitmap = 0x00000000;
128

    
129
      for(int face=0; face<numFaces; face++)
130
        if( vertInFace(mTmp1,faceAxis[face],dist3D[face]*size) ) vertBelongsBitmap |= (1<<face);
131

    
132
      for(int index=0; index<numIndices; index++)
133
        if( cubitFaceColor[index]!=0 && indexContains(indices[index],vert) ) cubitFaceColor[index] &= vertBelongsBitmap;
134
      }
135

    
136
    return cubitFaceColor;
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  private static void translateFromBitmap(int cubit, int[] colorsBitmap)
142
    {
143
    int len = colorsBitmap.length;
144

    
145
    for(int face=0; face<len; face++)
146
      {
147
      if( colorsBitmap[face]==0 ) colorsBitmap[face] = -1;
148
      else
149
        {
150
        int shift=0;
151

    
152
        while( (colorsBitmap[face]&0x1) != 1 )
153
          {
154
          colorsBitmap[face]>>=1;
155
          shift++;
156
          }
157

    
158
        if( colorsBitmap[face]!=1 )
159
          {
160
          android.util.Log.e("D", "ERROR, cubit= "+cubit+" face "+face+" seems to belong to "+shift+" and still "+colorsBitmap[face]);
161
          }
162

    
163
        colorsBitmap[face] = shift;
164
        }
165
      }
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  public static int[][] computeColors(ObjectShape[] shapes, float[][] moves, Static4D[] quats, TwistyObject object)
171
    {
172
    int numCubits = moves.length;
173
    int[][] colors = new int[numCubits][];
174
    int[] numLayers = object.getNumLayers();
175
    Static3D[] faceAxis = object.getFaceAxis();
176
    float[] dist3D = object.getDist3D(numLayers);
177
    float size = object.getSize();
178

    
179
    for(int cubit=0; cubit<numCubits; cubit++)
180
      {
181
      int variant = object.getCubitVariant(cubit,numLayers);
182
      colors[cubit] = computeCubitFaceColors(shapes[variant],moves[cubit],quats[cubit],faceAxis,dist3D,size);
183
      translateFromBitmap(cubit,colors[cubit]);
184
      }
185

    
186
    return colors;
187
    }
188
  }
(8-8/14)