Project

General

Profile

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

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

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.objects;
21

    
22
import android.content.SharedPreferences;
23

    
24
import org.distorted.helpers.QuatHelper;
25
import org.distorted.library.type.Static4D;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
class Cubit
30
  {
31
  private final float[] mOrigPosition;
32
  private final float[] mCurrentPosition;
33
  private TwistyObject mParent;
34
  private final int mNumAxis;
35
  private final int mLen;
36

    
37
  int mQuatIndex;
38
  int[] mRotationRow;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
  Cubit(TwistyObject parent, float[] position, int numAxis)
43
    {
44
    mQuatIndex= 0;
45
    mParent   = parent;
46
    mLen      = position.length;
47

    
48
    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

    
57
    mNumAxis     = numAxis;
58
    mRotationRow = new int[mNumAxis];
59
    computeRotationRow();
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
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
// list QUATS.
66
//
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

    
70
  private int normalizeScrambleQuat(Static4D quat)
71
    {
72
    float x = quat.get0();
73
    float y = quat.get1();
74
    float z = quat.get2();
75
    float w = quat.get3();
76
    float xd,yd,zd,wd;
77
    float diff, mindiff = Float.MAX_VALUE;
78
    int ret=0;
79
    int num_quats = mParent.OBJECT_QUATS.length;
80
    Static4D qt;
81

    
82
    for(int q=0; q<num_quats; q++)
83
      {
84
      qt = mParent.OBJECT_QUATS[q];
85

    
86
      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
        {
95
        ret = q;
96
        mindiff = diff;
97
        }
98

    
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
        {
108
        ret = q;
109
        mindiff = diff;
110
        }
111
      }
112

    
113
    return ret;
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
  private void computeRotationRow()
119
    {
120
    for(int i=0; i<mNumAxis; i++)
121
      {
122
      mRotationRow[i] = mParent.computeRow(mCurrentPosition,i);
123
      }
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  float[] getOrigPosition()
129
    {
130
    return mOrigPosition;
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  void modifyCurrentPosition(Static4D quat)
136
    {
137
    Static4D cubitCenter;
138
    Static4D rotatedCenter;
139
    int len = mLen/3;
140

    
141
    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
      rotatedCenter = QuatHelper.rotateVectorByQuat( cubitCenter, quat);
145

    
146
      mCurrentPosition[3*i  ] = rotatedCenter.get0();
147
      mCurrentPosition[3*i+1] = rotatedCenter.get1();
148
      mCurrentPosition[3*i+2] = rotatedCenter.get2();
149

    
150
      mParent.clampPos(mCurrentPosition, 3*i);
151
      }
152

    
153
    computeRotationRow();
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  int computeAssociation()
159
    {
160
    int result = 0, accumulativeShift = 0;
161

    
162
    for(int axis=0; axis<mNumAxis; axis++)
163
      {
164
      result += (mRotationRow[axis]<<accumulativeShift);
165
      accumulativeShift += ObjectList.MAX_OBJECT_SIZE;
166
      }
167

    
168
    return result;
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
  void savePreferences(SharedPreferences.Editor editor)
174
    {
175
    String number = mOrigPosition[0]+"_"+mOrigPosition[1]+"_"+mOrigPosition[2];
176
    editor.putInt("q_"+number, mQuatIndex);
177
    }
178

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

    
181
  int restorePreferences(SharedPreferences preferences)
182
    {
183
    String number = mOrigPosition[0]+"_"+mOrigPosition[1]+"_"+mOrigPosition[2];
184
    mQuatIndex = preferences.getInt("q_"+number, 0);
185
    return mQuatIndex;
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  int removeRotationNow(Static4D quat)
191
    {
192
    Static4D q = QuatHelper.quatMultiply(quat,mParent.OBJECT_QUATS[mQuatIndex]);
193
    mQuatIndex = normalizeScrambleQuat(q);
194

    
195
    modifyCurrentPosition(quat);
196

    
197
    return mQuatIndex;
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  void solve()
203
    {
204
    mQuatIndex = 0;
205
    System.arraycopy(mOrigPosition, 0, mCurrentPosition, 0, mCurrentPosition.length);
206
    computeRotationRow();
207
    }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
  void releaseResources()
212
    {
213
    mParent = null;
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217
// 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

    
220
  float getDistSquared(float[] point)
221
    {
222
    float dx = mCurrentPosition[0] - point[0];
223
    float dy = mCurrentPosition[1] - point[1];
224
    float dz = mCurrentPosition[2] - point[2];
225

    
226
    return dx*dx + dy*dy + dz*dz;
227
    }
228
}
(1-1/47)