Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / Cubit.java @ 771f6dfa

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