Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / Cubit.java @ c7a98f94

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 1ebc4767 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
127
128 e6cf7283 Leszek Koltunski
  float[] getOrigPosition()
129 1ebc4767 Leszek Koltunski
    {
130
    return mOrigPosition;
131
    }
132
133 1d6c1eea Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
134
135
  void modifyCurrentPosition(Static4D quat)
136
    {
137 e6cf7283 Leszek Koltunski
    Static4D cubitCenter;
138
    Static4D rotatedCenter;
139
    int len = mLen/3;
140 1d6c1eea Leszek Koltunski
141 e6cf7283 Leszek Koltunski
    for(int i=0; i<len; i++)
142
      {
143
      cubitCenter =  new Static4D(mCurrentPosition[3*i], mCurrentPosition[3*i+1], mCurrentPosition[3*i+2], 0);
144 b9d4aa3b Leszek Koltunski
      rotatedCenter = QuatHelper.rotateVectorByQuat( cubitCenter, quat);
145 1d6c1eea Leszek Koltunski
146 e6cf7283 Leszek Koltunski
      mCurrentPosition[3*i  ] = rotatedCenter.get0();
147
      mCurrentPosition[3*i+1] = rotatedCenter.get1();
148
      mCurrentPosition[3*i+2] = rotatedCenter.get2();
149 1d6c1eea Leszek Koltunski
150 e6cf7283 Leszek Koltunski
      mParent.clampPos(mCurrentPosition, 3*i);
151
      }
152 1d6c1eea Leszek Koltunski
153
    computeRotationRow();
154
    }
155
156 27e6c301 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
157
158
  int computeAssociation()
159
    {
160 eb389a97 Leszek Koltunski
    int result = 0, accumulativeShift = 0;
161 27e6c301 Leszek Koltunski
162
    for(int axis=0; axis<mNumAxis; axis++)
163
      {
164 f0450fcc Leszek Koltunski
      result += (mRotationRow[axis]<<accumulativeShift);
165 9c2f0c91 Leszek Koltunski
      accumulativeShift += ObjectList.MAX_OBJECT_SIZE;
166 27e6c301 Leszek Koltunski
      }
167
168
    return result;
169
    }
170
171 70b76549 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
172
173
  void savePreferences(SharedPreferences.Editor editor)
174
    {
175 e6cf7283 Leszek Koltunski
    String number = mOrigPosition[0]+"_"+mOrigPosition[1]+"_"+mOrigPosition[2];
176 2fcad75d Leszek Koltunski
    editor.putInt("q_"+number, mQuatIndex);
177 70b76549 Leszek Koltunski
    }
178
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
181 2fcad75d Leszek Koltunski
  int restorePreferences(SharedPreferences preferences)
182 70b76549 Leszek Koltunski
    {
183 e6cf7283 Leszek Koltunski
    String number = mOrigPosition[0]+"_"+mOrigPosition[1]+"_"+mOrigPosition[2];
184 2fcad75d Leszek Koltunski
    mQuatIndex = preferences.getInt("q_"+number, 0);
185 fc3c5170 Leszek Koltunski
    return mQuatIndex;
186 9bcec50a Leszek Koltunski
    }
187
188 70b76549 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
189
190 98904e45 Leszek Koltunski
  int removeRotationNow(Static4D quat)
191 70b76549 Leszek Koltunski
    {
192 7ff38997 Leszek Koltunski
    Static4D q = QuatHelper.quatMultiply(quat,mParent.OBJECT_QUATS[mQuatIndex]);
193 2fcad75d Leszek Koltunski
    mQuatIndex = normalizeScrambleQuat(q);
194 98904e45 Leszek Koltunski
195 001cc0e4 Leszek Koltunski
    modifyCurrentPosition(quat);
196 98904e45 Leszek Koltunski
197 2fcad75d Leszek Koltunski
    return mQuatIndex;
198 70b76549 Leszek Koltunski
    }
199
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
202
  void solve()
203
    {
204 2fcad75d Leszek Koltunski
    mQuatIndex = 0;
205 e6cf7283 Leszek Koltunski
    System.arraycopy(mOrigPosition, 0, mCurrentPosition, 0, mCurrentPosition.length);
206 0e7bcfb1 Leszek Koltunski
    computeRotationRow();
207 70b76549 Leszek Koltunski
    }
208
209 54342a21 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
210
211
  void releaseResources()
212
    {
213
    mParent = null;
214
    }
215
216 9621255f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
217 e6cf7283 Leszek Koltunski
// this is only needed for MODE_REPLACE objects (i.e. - currently - CUBE_3), so it is enough to only
218
// take into consideration the first position.
219 9621255f Leszek Koltunski
220
  float getDistSquared(float[] point)
221
    {
222 e6cf7283 Leszek Koltunski
    float dx = mCurrentPosition[0] - point[0];
223
    float dy = mCurrentPosition[1] - point[1];
224
    float dz = mCurrentPosition[2] - point[2];
225 9621255f Leszek Koltunski
226
    return dx*dx + dy*dy + dz*dz;
227
    }
228 70b76549 Leszek Koltunski
}