Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / Cubit.java @ 234a7582

1 70b76549 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 1f9772f3 Leszek Koltunski
package org.distorted.objects;
21 70b76549 Leszek Koltunski
22
import android.content.SharedPreferences;
23
24
import org.distorted.library.type.Static3D;
25
import org.distorted.library.type.Static4D;
26 1f9772f3 Leszek Koltunski
import org.distorted.main.RubikSurfaceView;
27 70b76549 Leszek Koltunski
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
30
class Cubit
31
  {
32
  private final Static3D mOrigPosition;
33
34 9c2f0c91 Leszek Koltunski
  private TwistyObject mParent;
35 0e7bcfb1 Leszek Koltunski
  private Static3D mCurrentPosition;
36 e844c116 Leszek Koltunski
  private int mNumAxis;
37 70b76549 Leszek Koltunski
38 2fcad75d Leszek Koltunski
  int mQuatIndex;
39 eb389a97 Leszek Koltunski
  int[] mRotationRow;
40 70b76549 Leszek Koltunski
41 470820a7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
42
43 9c2f0c91 Leszek Koltunski
  Cubit(TwistyObject parent, Static3D position)
44 470820a7 Leszek Koltunski
    {
45
    float x = position.get0();
46
    float y = position.get1();
47
    float z = position.get2();
48
49
    mParent          = parent;
50
    mOrigPosition    = new Static3D(x,y,z);
51 2fcad75d Leszek Koltunski
    mCurrentPosition = new Static3D(x,y,z);
52
    mQuatIndex       = 0;
53 470820a7 Leszek Koltunski
54
    mNumAxis     = mParent.ROTATION_AXIS.length;
55 eb389a97 Leszek Koltunski
    mRotationRow = new int[mNumAxis];
56 470820a7 Leszek Koltunski
    computeRotationRow();
57
    }
58
59 70b76549 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60
// Because of quatMultiplication, errors can accumulate - so to avoid this, we
61
// correct the value of the 'scramble' quat to what it should be - one of the legal quats from the
62 98904e45 Leszek Koltunski
// list QUATS.
63 70b76549 Leszek Koltunski
//
64
// We also have to remember that the group of unit quaternions is a double-cover of rotations
65
// in 3D ( q represents the same rotation as -q ) - so invert if needed.
66 9f4c44fe Leszek Koltunski
67 98904e45 Leszek Koltunski
  private int normalizeScrambleQuat(Static4D quat)
68
    {
69 70b76549 Leszek Koltunski
    float x = quat.get0();
70
    float y = quat.get1();
71
    float z = quat.get2();
72
    float w = quat.get3();
73 98904e45 Leszek Koltunski
    float xd,yd,zd,wd;
74
    float diff, mindiff = Float.MAX_VALUE;
75
    int ret=0;
76
    int num_quats = mParent.QUATS.length;
77
    Static4D qt;
78 70b76549 Leszek Koltunski
79 98904e45 Leszek Koltunski
    for(int q=0; q<num_quats; q++)
80 70b76549 Leszek Koltunski
      {
81 98904e45 Leszek Koltunski
      qt = mParent.QUATS[q];
82 70b76549 Leszek Koltunski
83 98904e45 Leszek Koltunski
      xd = x - qt.get0();
84
      yd = y - qt.get1();
85
      zd = z - qt.get2();
86
      wd = w - qt.get3();
87
88
      diff = xd*xd + yd*yd + zd*zd + wd*wd;
89
90
      if( diff < mindiff )
91 70b76549 Leszek Koltunski
        {
92 98904e45 Leszek Koltunski
        ret = q;
93
        mindiff = diff;
94 70b76549 Leszek Koltunski
        }
95 98904e45 Leszek Koltunski
96
      xd = x + qt.get0();
97
      yd = y + qt.get1();
98
      zd = z + qt.get2();
99
      wd = w + qt.get3();
100
101
      diff = xd*xd + yd*yd + zd*zd + wd*wd;
102
103
      if( diff < mindiff )
104 70b76549 Leszek Koltunski
        {
105 98904e45 Leszek Koltunski
        ret = q;
106
        mindiff = diff;
107 70b76549 Leszek Koltunski
        }
108
      }
109
110 98904e45 Leszek Koltunski
    return ret;
111 70b76549 Leszek Koltunski
    }
112 98904e45 Leszek Koltunski
113 10a2e360 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
114
115
  private void computeRotationRow()
116
    {
117 e844c116 Leszek Koltunski
    float x = mCurrentPosition.get0();
118
    float y = mCurrentPosition.get1();
119
    float z = mCurrentPosition.get2();
120
121
    for(int i=0; i<mNumAxis; i++)
122
      {
123 eb389a97 Leszek Koltunski
      mRotationRow[i] = mParent.computeRow(x,y,z,i);
124 e844c116 Leszek Koltunski
      }
125 70b76549 Leszek Koltunski
    }
126
127 1ebc4767 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
128
129
  Static3D getOrigPosition()
130
    {
131
    return mOrigPosition;
132
    }
133
134 1d6c1eea Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
135
136
  void modifyCurrentPosition(Static4D quat)
137
    {
138
    float cubitCenterX = mCurrentPosition.get0();
139
    float cubitCenterY = mCurrentPosition.get1();
140
    float cubitCenterZ = mCurrentPosition.get2();
141
142
    Static4D cubitCenter =  new Static4D(cubitCenterX, cubitCenterY, cubitCenterZ, 0);
143
    Static4D rotatedCenter = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat);
144
145
    float rotatedX = rotatedCenter.get0();
146
    float rotatedY = rotatedCenter.get1();
147
    float rotatedZ = rotatedCenter.get2();
148
149
    mCurrentPosition.set(rotatedX, rotatedY, rotatedZ);
150
    mParent.clampPos(mCurrentPosition);
151
152
    computeRotationRow();
153
    }
154
155 27e6c301 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
156
157
  int computeAssociation()
158
    {
159 eb389a97 Leszek Koltunski
    int result = 0, accumulativeShift = 0;
160 27e6c301 Leszek Koltunski
161
    for(int axis=0; axis<mNumAxis; axis++)
162
      {
163 eb389a97 Leszek Koltunski
      result += (1<<(mRotationRow[axis]+accumulativeShift));
164 9c2f0c91 Leszek Koltunski
      accumulativeShift += ObjectList.MAX_OBJECT_SIZE;
165 27e6c301 Leszek Koltunski
      }
166
167
    return result;
168
    }
169
170 70b76549 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172
  void savePreferences(SharedPreferences.Editor editor)
173
    {
174
    String number = mOrigPosition.get0()+"_"+mOrigPosition.get1()+"_"+mOrigPosition.get2();
175 2fcad75d Leszek Koltunski
    editor.putInt("q_"+number, mQuatIndex);
176 70b76549 Leszek Koltunski
    }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
180 2fcad75d Leszek Koltunski
  int restorePreferences(SharedPreferences preferences)
181 70b76549 Leszek Koltunski
    {
182
    String number = mOrigPosition.get0()+"_"+mOrigPosition.get1()+"_"+mOrigPosition.get2();
183 2fcad75d Leszek Koltunski
    mQuatIndex = preferences.getInt("q_"+number, 0);
184 fc3c5170 Leszek Koltunski
    return mQuatIndex;
185 9bcec50a Leszek Koltunski
    }
186
187 70b76549 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
188
189 98904e45 Leszek Koltunski
  int removeRotationNow(Static4D quat)
190 70b76549 Leszek Koltunski
    {
191 2fcad75d Leszek Koltunski
    Static4D q = RubikSurfaceView.quatMultiply(quat,mParent.QUATS[mQuatIndex]);
192
    mQuatIndex = normalizeScrambleQuat(q);
193 98904e45 Leszek Koltunski
194 001cc0e4 Leszek Koltunski
    modifyCurrentPosition(quat);
195 98904e45 Leszek Koltunski
196 2fcad75d Leszek Koltunski
    return mQuatIndex;
197 70b76549 Leszek Koltunski
    }
198
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201
  void solve()
202
    {
203 2fcad75d Leszek Koltunski
    mQuatIndex = 0;
204 70b76549 Leszek Koltunski
    mCurrentPosition.set(mOrigPosition);
205 0e7bcfb1 Leszek Koltunski
    computeRotationRow();
206 70b76549 Leszek Koltunski
    }
207
208 9621255f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
209
210
  float getDistSquared(float[] point)
211
    {
212
    float dx = mCurrentPosition.get0() - point[0];
213
    float dy = mCurrentPosition.get1() - point[1];
214
    float dz = mCurrentPosition.get2() - point[2];
215
216
    return dx*dx + dy*dy + dz*dz;
217
    }
218 70b76549 Leszek Koltunski
}