Project

General

Profile

Download (9.77 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / helpers / QuatGroupGenerator.java @ 9554f5d4

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 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.objectlib.helpers;
21

    
22
import org.distorted.library.main.QuatHelper;
23
import org.distorted.library.type.Static3D;
24
import org.distorted.library.type.Static4D;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
public class QuatGroupGenerator
29
  {
30
  private static final int MAX_GROUP_SIZE = 256;
31
  private static final double PI = Math.PI;
32
  private static final Static4D[] mTable= new Static4D[MAX_GROUP_SIZE];
33

    
34
  private static int mInserted;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
// sin(A/2)*x, sin(A/2)*y, sin(A/2)*z, cos(A/2)
38

    
39
  private static void createQuat(Static3D axis, double angle, Static4D output)
40
    {
41
    float cosAngle = (float)Math.cos(angle/2);
42
    float sinAngle = (float)Math.sin(angle/2);
43

    
44
    output.set0( sinAngle*axis.get0() );
45
    output.set1( sinAngle*axis.get1() );
46
    output.set2( sinAngle*axis.get2() );
47
    output.set3( cosAngle );
48
    }
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51
// double cover, so q == -q
52

    
53
  private static boolean quatTheSame(Static4D q1, Static4D q2)
54
    {
55
    final float MAX_ERROR = 0.01f;
56

    
57
    float q10 = q1.get0();
58
    float q11 = q1.get1();
59
    float q12 = q1.get2();
60
    float q13 = q1.get3();
61

    
62
    float q20 = q2.get0();
63
    float q21 = q2.get1();
64
    float q22 = q2.get2();
65
    float q23 = q2.get3();
66

    
67
    float d0 = q10-q20;
68
    float d1 = q11-q21;
69
    float d2 = q12-q22;
70
    float d3 = q13-q23;
71

    
72
    if( d0<MAX_ERROR && d0>-MAX_ERROR &&
73
        d1<MAX_ERROR && d1>-MAX_ERROR &&
74
        d2<MAX_ERROR && d2>-MAX_ERROR &&
75
        d3<MAX_ERROR && d3>-MAX_ERROR  ) return true;
76

    
77
    d0 = q10+q20;
78
    d1 = q11+q21;
79
    d2 = q12+q22;
80
    d3 = q13+q23;
81

    
82
    if( d0<MAX_ERROR && d0>-MAX_ERROR &&
83
        d1<MAX_ERROR && d1>-MAX_ERROR &&
84
        d2<MAX_ERROR && d2>-MAX_ERROR &&
85
        d3<MAX_ERROR && d3>-MAX_ERROR  ) return true;
86

    
87
    return false;
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  private static int getIndexInTable(Static4D quat, Static4D[] table)
93
    {
94
    for(int i=0; i<mInserted; i++)
95
      {
96
      if( quatTheSame(quat,table[i]) ) return i;
97
      }
98

    
99
    return -1;
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  private static boolean insertQuat(Static4D quat, Static4D[] table)
105
    {
106
    if( getIndexInTable(quat,table)<0 )
107
      {
108
      if( mInserted<MAX_GROUP_SIZE )
109
        {
110
        table[mInserted] = new Static4D(quat);
111
        mInserted++;
112
        return true;
113
        }
114
      return false;
115
      }
116

    
117
    return true;
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
// PUBLIC API
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  public static Static4D[] computeGroup(Static3D[] rotAxis, int[] basicAngles)
125
    {
126
    Static4D quat = new Static4D(0,0,0,1);
127
    int num,numAxis = rotAxis.length;
128
    mInserted = 0;
129

    
130
    insertQuat(quat,mTable);
131

    
132
    for( int ax=0; ax<numAxis; ax++)
133
      for(int angle=1; angle<basicAngles[ax]; angle++)
134
        {
135
        createQuat(rotAxis[ax], 2*PI*angle/basicAngles[ax], quat);
136
        insertQuat(quat,mTable);
137
        }
138

    
139
    do
140
      {
141
      num = mInserted;
142

    
143
      for(int i=0; i<num; i++)
144
        for(int j=0; j<num; j++)
145
          {
146
          quat = QuatHelper.quatMultiply(mTable[i], mTable[j]);
147

    
148
          if( !insertQuat(quat,mTable) )
149
            {
150
            i=num;j=num;
151
            android.util.Log.e("QuatGroupGenerator", "OVERFLOW");
152
            }
153
          }
154
      }
155
    while( num < mInserted );
156

    
157
    Static4D[] table = new Static4D[mInserted];
158

    
159
    for(int i=0; i<mInserted; i++)
160
      {
161
      table[i] = new Static4D(mTable[i]);
162
      }
163

    
164
    return table;
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  public static void showQuats(Static4D[] table)
170
    {
171
    final float MAX_ERR = 0.00001f;
172
    String dbg = "---------------------------------\n";
173

    
174
    for (Static4D quat : table)
175
      {
176
      float x = quat.get0();
177
      float y = quat.get1();
178
      float z = quat.get2();
179
      float w = quat.get3();
180

    
181
      if( x<MAX_ERR && x>-MAX_ERR ) x = 0.0f;
182
      if( y<MAX_ERR && y>-MAX_ERR ) y = 0.0f;
183
      if( z<MAX_ERR && z>-MAX_ERR ) z = 0.0f;
184
      if( w<MAX_ERR && w>-MAX_ERR ) w = 0.0f;
185

    
186
      if( x<0.5f+MAX_ERR && x>0.5f-MAX_ERR ) x = 0.5f;
187
      if( y<0.5f+MAX_ERR && y>0.5f-MAX_ERR ) y = 0.5f;
188
      if( z<0.5f+MAX_ERR && z>0.5f-MAX_ERR ) z = 0.5f;
189
      if( w<0.5f+MAX_ERR && w>0.5f-MAX_ERR ) w = 0.5f;
190

    
191
      if( x<-0.5f+MAX_ERR && x>-0.5f-MAX_ERR ) x = -0.5f;
192
      if( y<-0.5f+MAX_ERR && y>-0.5f-MAX_ERR ) y = -0.5f;
193
      if( z<-0.5f+MAX_ERR && z>-0.5f-MAX_ERR ) z = -0.5f;
194
      if( w<-0.5f+MAX_ERR && w>-0.5f-MAX_ERR ) w = -0.5f;
195

    
196
      if( x<1.0f+MAX_ERR && x>1.0f-MAX_ERR ) x = 1.0f;
197
      if( y<1.0f+MAX_ERR && y>1.0f-MAX_ERR ) y = 1.0f;
198
      if( z<1.0f+MAX_ERR && z>1.0f-MAX_ERR ) z = 1.0f;
199
      if( w<1.0f+MAX_ERR && w>1.0f-MAX_ERR ) w = 1.0f;
200

    
201
      if( x<-1.0f+MAX_ERR && x>-1.0f-MAX_ERR ) x = -1.0f;
202
      if( y<-1.0f+MAX_ERR && y>-1.0f-MAX_ERR ) y = -1.0f;
203
      if( z<-1.0f+MAX_ERR && z>-1.0f-MAX_ERR ) z = -1.0f;
204
      if( w<-1.0f+MAX_ERR && w>-1.0f-MAX_ERR ) w = -1.0f;
205

    
206
      dbg += ( "("+x+" "+y+" "+z+" "+w+")\n");
207
      }
208

    
209
    android.util.Log.e("D", dbg);
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  public static void showRotations(Static4D[] table)
215
    {
216
    final float MAX_ERR = 0.00001f;
217
    String dbg = "---------------------------------\n";
218

    
219
    for (Static4D quat : table)
220
      {
221
      float cos = quat.get3();
222
      float sin = (float)Math.sqrt(1-cos*cos);
223
      float x = sin==0 ? 0 : quat.get0()/sin;
224
      float y = sin==0 ? 0 : quat.get1()/sin;
225
      float z = sin==0 ? 0 : quat.get2()/sin;
226
      float halfAngle = (float)Math.acos(cos);
227
      int ang = (int) ( (360*halfAngle/Math.PI) + 0.5f);
228

    
229
      if( x<MAX_ERR && x>-MAX_ERR ) x = 0.0f;
230
      if( y<MAX_ERR && y>-MAX_ERR ) y = 0.0f;
231
      if( z<MAX_ERR && z>-MAX_ERR ) z = 0.0f;
232

    
233
      if( x<0.5f+MAX_ERR && x>0.5f-MAX_ERR ) x = 0.5f;
234
      if( y<0.5f+MAX_ERR && y>0.5f-MAX_ERR ) y = 0.5f;
235
      if( z<0.5f+MAX_ERR && z>0.5f-MAX_ERR ) z = 0.5f;
236

    
237
      if( x<-0.5f+MAX_ERR && x>-0.5f-MAX_ERR ) x = -0.5f;
238
      if( y<-0.5f+MAX_ERR && y>-0.5f-MAX_ERR ) y = -0.5f;
239
      if( z<-0.5f+MAX_ERR && z>-0.5f-MAX_ERR ) z = -0.5f;
240

    
241
      if( x<1.0f+MAX_ERR && x>1.0f-MAX_ERR ) x = 1.0f;
242
      if( y<1.0f+MAX_ERR && y>1.0f-MAX_ERR ) y = 1.0f;
243
      if( z<1.0f+MAX_ERR && z>1.0f-MAX_ERR ) z = 1.0f;
244

    
245
      if( x<-1.0f+MAX_ERR && x>-1.0f-MAX_ERR ) x = -1.0f;
246
      if( y<-1.0f+MAX_ERR && y>-1.0f-MAX_ERR ) y = -1.0f;
247
      if( z<-1.0f+MAX_ERR && z>-1.0f-MAX_ERR ) z = -1.0f;
248

    
249
      dbg += ( "("+x+" "+y+" "+z+") "+ang+"\n");
250
      }
251

    
252
    android.util.Log.e("D", dbg);
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  public static void showCubitQuats(Static4D[] table, float[][] pos, int startIndex, int stopIndex)
258
    {
259
    StringBuilder builder = new StringBuilder();
260
    float MAXERR = 0.01f;
261
    float[] tmp = new float[4];
262
    float[] vec = pos[startIndex];
263
    int numQuats = table.length;
264
    float X = vec[0];
265
    float Y = vec[1];
266
    float Z = vec[2];
267

    
268
    for(int cubit=startIndex; cubit<=stopIndex; cubit++)
269
      {
270
      float x = pos[cubit][0];
271
      float y = pos[cubit][1];
272
      float z = pos[cubit][2];
273

    
274
      for(int quat=0; quat<numQuats; quat++)
275
        {
276
        QuatHelper.rotateVectorByQuat(tmp,X,Y,Z,0,table[quat]);
277

    
278
        float dx = tmp[0]-x;
279
        float dy = tmp[1]-y;
280
        float dz = tmp[2]-z;
281

    
282
        if( dx>-MAXERR && dx<MAXERR && dy>-MAXERR && dy<MAXERR && dz>-MAXERR && dz<MAXERR )
283
          {
284
          builder.append(' ');
285
          builder.append(quat);
286
          }
287
        }
288

    
289
      android.util.Log.e("D", "Cubit: "+cubit+" quats: "+builder.toString() );
290
      builder.delete(0, builder.length());
291
      }
292
    }
293
  }
(10-10/10)