Project

General

Profile

Download (11.8 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / object / Cubit.java @ 9f4c44fe

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.object;
21

    
22
import android.content.SharedPreferences;
23

    
24
import org.distorted.library.effect.MatrixEffectMove;
25
import org.distorted.library.effect.MatrixEffectQuaternion;
26
import org.distorted.library.effect.MatrixEffectRotate;
27
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedNode;
29
import org.distorted.library.mesh.MeshBase;
30
import org.distorted.library.message.EffectListener;
31
import org.distorted.library.type.Dynamic1D;
32
import org.distorted.library.type.Static1D;
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35
import org.distorted.magic.RubikSurfaceView;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
class Cubit
40
  {
41
  private static final Static3D matrCenter = new Static3D(0,0,0);
42

    
43
  private final Static3D mOrigPosition;
44

    
45
  private RubikObject mParent;
46
  private MeshBase mMesh;
47
  private Static3D mRotationAxis;
48
  private MatrixEffectRotate mRotateEffect;
49
  private Static3D mCurrentPosition;
50
  private int mNumAxis;
51

    
52
  Dynamic1D mRotationAngle;
53
  DistortedNode mNode;
54
  DistortedEffects mEffect;
55
  Static4D mQuatScramble;
56
  int[] mRotationRow;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
// Because of quatMultiplication, errors can accumulate - so to avoid this, we
60
// correct the value of the 'scramble' quat to what it should be - one of the legal quats from the
61
// list LEGAL_QUATS.
62
//
63
// We also have to remember that the group of unit quaternions is a double-cover of rotations
64
// in 3D ( q represents the same rotation as -q ) - so invert if needed.
65

    
66
  private void normalizeScrambleQuat(Static4D quat)
67
    {
68
    final float MAX_ERROR = 0.0001f;
69

    
70
    float x = quat.get0();
71
    float y = quat.get1();
72
    float z = quat.get2();
73
    float w = quat.get3();
74
    float diff;
75

    
76
    for(float legal: mParent.LEGAL_QUATS)
77
      {
78
      diff = x-legal;
79
      if( diff*diff<MAX_ERROR ) x = legal;
80
      diff = y-legal;
81
      if( diff*diff<MAX_ERROR ) y = legal;
82
      diff = z-legal;
83
      if( diff*diff<MAX_ERROR ) z = legal;
84
      diff = w-legal;
85
      if( diff*diff<MAX_ERROR ) w = legal;
86
      }
87

    
88
    if( w<0 )
89
      {
90
      w = -w;
91
      z = -z;
92
      y = -y;
93
      x = -x;
94
      }
95
    else if( w==0 )
96
      {
97
      if( z<0 )
98
        {
99
        z = -z;
100
        y = -y;
101
        x = -x;
102
        }
103
      else if( z==0 )
104
        {
105
        if( y<0 )
106
          {
107
          y = -y;
108
          x = -x;
109
          }
110
        else if( y==0 )
111
          {
112
          if( x<0 )
113
            {
114
            x = -x;
115
            }
116
          }
117
        }
118
      }
119

    
120
    quat.set(x,y,z,w);
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  private int computeNearestAngle(float angle)
126
    {
127
    final int NEAREST = 90;
128

    
129
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
130
    if( angle< -(NEAREST*0.5) ) tmp-=1;
131

    
132
    return NEAREST*tmp;
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  private void modifyCurrentPosition(Static3D currentPosition, Static4D quat)
138
    {
139
    float diff = 0.5f*(mParent.mSize-1);
140
    float cubitCenterX = currentPosition.get0() - diff;
141
    float cubitCenterY = currentPosition.get1() - diff;
142
    float cubitCenterZ = currentPosition.get2() - diff;
143

    
144
    Static4D cubitCenter =  new Static4D(cubitCenterX, cubitCenterY, cubitCenterZ, 0);
145
    Static4D rotatedCenter = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat);
146

    
147
    float rotatedX = rotatedCenter.get0() + diff;
148
    float rotatedY = rotatedCenter.get1() + diff;
149
    float rotatedZ = rotatedCenter.get2() + diff;
150

    
151
    int roundedX = (int)(rotatedX+0.1f);
152
    int roundedY = (int)(rotatedY+0.1f);
153
    int roundedZ = (int)(rotatedZ+0.1f);
154

    
155
    currentPosition.set(roundedX, roundedY, roundedZ);
156
    computeRotationRow();
157
    }
158

    
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161
// cast current position on axis; use mStart and mStep to compute the rotation row for each axis.
162

    
163
  private void computeRotationRow()
164
    {
165
    float tmp;
166
    Static3D axis;
167
    float x = mCurrentPosition.get0();
168
    float y = mCurrentPosition.get1();
169
    float z = mCurrentPosition.get2();
170

    
171
    for(int i=0; i<mNumAxis; i++)
172
      {
173
      axis = mParent.ROTATION_AXIS[i];
174
      tmp = x*axis.get0() + y*axis.get1() + z*axis.get2();
175
      mRotationRow[i] = (int)( (tmp-mParent.mStart)/mParent.mStep + 0.5f );
176
      }
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  Cubit(RubikObject parent, MeshBase mesh, Static3D position)
182
    {
183
    float x = position.get0();
184
    float y = position.get1();
185
    float z = position.get2();
186

    
187
    float nc = parent.mSize*0.5f;
188
    Static3D vector = new Static3D(x-nc+0.5f, y-nc+0.5f, z-nc+0.5f);
189

    
190
    mParent          = parent;
191
    mMesh            = mesh;
192
    mOrigPosition    = new Static3D(x,y,z);
193
    mQuatScramble    = new Static4D(0,0,0,1);
194
    mRotationAngle   = new Dynamic1D();
195
    mRotationAxis    = new Static3D(1,0,0);
196
    mCurrentPosition = position;
197
    mRotateEffect    = new MatrixEffectRotate(mRotationAngle, mRotationAxis, matrCenter);
198

    
199
    mNumAxis     = mParent.ROTATION_AXIS.length;
200
    mRotationRow = new int[mNumAxis];
201
    computeRotationRow();
202

    
203
    mEffect = new DistortedEffects();
204
    mEffect.apply(mParent.mSinkEffect);
205
    mEffect.apply( new MatrixEffectMove(vector) );
206
    mEffect.apply( new MatrixEffectQuaternion(mQuatScramble, matrCenter));
207
    mEffect.apply(mRotateEffect);
208
    mEffect.apply(mParent.mQuatAEffect);
209
    mEffect.apply(mParent.mQuatCEffect);
210
    mEffect.apply(mParent.mScaleEffect);
211

    
212
    mNode = new DistortedNode(mParent.mTexture,mEffect,mMesh);
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  void savePreferences(SharedPreferences.Editor editor)
218
    {
219
    String number = mOrigPosition.get0()+"_"+mOrigPosition.get1()+"_"+mOrigPosition.get2();
220

    
221
    editor.putFloat("qx_"+number, mQuatScramble.get0());
222
    editor.putFloat("qy_"+number, mQuatScramble.get1());
223
    editor.putFloat("qz_"+number, mQuatScramble.get2());
224
    editor.putFloat("qw_"+number, mQuatScramble.get3());
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  void restorePreferences(SharedPreferences preferences)
230
    {
231
    String number = mOrigPosition.get0()+"_"+mOrigPosition.get1()+"_"+mOrigPosition.get2();
232

    
233
    float qx = preferences.getFloat("qx_"+number, 0.0f);
234
    float qy = preferences.getFloat("qy_"+number, 0.0f);
235
    float qz = preferences.getFloat("qz_"+number, 0.0f);
236
    float qw = preferences.getFloat("qw_"+number, 1.0f);
237

    
238
    mQuatScramble.set(qx,qy,qz,qw);
239
    modifyCurrentPosition( mCurrentPosition, mQuatScramble);
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  long finishRotationNow(EffectListener listener)
245
    {
246
    int pointNum = mRotationAngle.getNumPoints();
247

    
248
    if( pointNum>=1 )
249
      {
250
      float startingAngle = mRotationAngle.getPoint(pointNum-1).get0();
251
      int nearestAngleInDegrees = computeNearestAngle(startingAngle);
252
      mParent.mRotationAngleStatic.set0(startingAngle);
253
      mParent.mRotationAngleFinal.set0(nearestAngleInDegrees);
254
      mParent.mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-startingAngle)*0.2f );
255
      return setUpCallback(listener);
256
      }
257

    
258
    return 0;
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  Static4D returnRotationQuat(int axis)
264
    {
265
    int pointNum = mRotationAngle.getNumPoints();
266

    
267
    if( pointNum>=1 )
268
      {
269
      float axisX = mParent.ROTATION_AXIS[axis].get0();
270
      float axisY = mParent.ROTATION_AXIS[axis].get1();
271
      float axisZ = mParent.ROTATION_AXIS[axis].get2();
272

    
273
      float startingAngle = mRotationAngle.getPoint(pointNum-1).get0();
274
      int nearestAngleInDegrees = computeNearestAngle(startingAngle);
275
      double nearestAngleInRadians = nearestAngleInDegrees*Math.PI/180;
276
      float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
277
      float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
278
      return new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
279
      }
280

    
281
    return null;
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  void removeRotationNow(Static4D quat)
287
    {
288
    mRotationAngle.removeAll();
289
    mQuatScramble.set(RubikSurfaceView.quatMultiply(quat,mQuatScramble));
290
    normalizeScrambleQuat( mQuatScramble );
291
    modifyCurrentPosition( mCurrentPosition,quat);
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
// all DistortedTextures, DistortedNodes, DistortedFramebuffers, DistortedScreens and all types of
296
// Meshes HAVE TO be markedForDeletion when they are no longer needed- otherwise we have a major
297
// memory leak.
298

    
299
  void releaseResources()
300
    {
301
    mMesh.markForDeletion();
302
    mNode.markForDeletion();
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
  void solve()
308
    {
309
    mQuatScramble.set(0,0,0,1);
310
    mCurrentPosition.set(mOrigPosition);
311
    computeRotationRow();
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  void beginNewRotation(int axis)
317
    {
318
    mRotationAxis.set( mParent.ROTATION_AXIS[axis] );
319
    mRotationAngle.add(mParent.mRotationAngleStatic);
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  void addNewRotation(int axis, long durationMillis, int angle)
325
    {
326
    mRotationAxis.set( mParent.ROTATION_AXIS[axis] );
327
    mRotationAngle.setDuration(durationMillis);
328
    mRotationAngle.resetToBeginning();
329
    mRotationAngle.add(new Static1D(0));
330
    mRotationAngle.add(new Static1D(angle));
331
    }
332

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

    
335
  long setUpCallback(EffectListener listener)
336
    {
337
    mRotateEffect.notifyWhenFinished(listener);
338
    return mRotateEffect.getID();
339
    }
340
}
(1-1/7)