Project

General

Profile

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

magiccube / src / main / java / org / distorted / objectlib / Cubit.java @ 588ace55

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