Project

General

Profile

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

library / src / main / java / org / distorted / library / main / QuatHelper.java @ 4c568a67

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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.library.main;
21

    
22
import org.distorted.library.type.Static4D;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

    
26
public class QuatHelper
27
  {
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
// return quat1*quat2
30

    
31
  public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
32
    {
33
    float qx = quat1.get0();
34
    float qy = quat1.get1();
35
    float qz = quat1.get2();
36
    float qw = quat1.get3();
37

    
38
    float rx = quat2.get0();
39
    float ry = quat2.get1();
40
    float rz = quat2.get2();
41
    float rw = quat2.get3();
42

    
43
    float tx = rw*qx - rz*qy + ry*qz + rx*qw;
44
    float ty = rw*qy + rz*qx + ry*qw - rx*qz;
45
    float tz = rw*qz + rz*qw - ry*qx + rx*qy;
46
    float tw = rw*qw - rz*qz - ry*qy - rx*qx;
47

    
48
    return new Static4D(tx,ty,tz,tw);
49
    }
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
// return quat1*(rx,ry,rz,rw)
53

    
54
  public static Static4D quatMultiply( Static4D quat1, float rx, float ry, float rz, float rw )
55
    {
56
    float qx = quat1.get0();
57
    float qy = quat1.get1();
58
    float qz = quat1.get2();
59
    float qw = quat1.get3();
60

    
61
    float tx = rw*qx - rz*qy + ry*qz + rx*qw;
62
    float ty = rw*qy + rz*qx + ry*qw - rx*qz;
63
    float tz = rw*qz + rz*qw - ry*qx + rx*qy;
64
    float tw = rw*qw - rz*qz - ry*qy - rx*qx;
65

    
66
    return new Static4D(tx,ty,tz,tw);
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
// return (qx,qy,qz,qw)*quat2
71

    
72
  public static Static4D quatMultiply( float qx, float qy, float qz, float qw, Static4D quat2 )
73
    {
74
    float rx = quat2.get0();
75
    float ry = quat2.get1();
76
    float rz = quat2.get2();
77
    float rw = quat2.get3();
78

    
79
    float tx = rw*qx - rz*qy + ry*qz + rx*qw;
80
    float ty = rw*qy + rz*qx + ry*qw - rx*qz;
81
    float tz = rw*qz + rz*qw - ry*qx + rx*qy;
82
    float tw = rw*qw - rz*qz - ry*qy - rx*qx;
83

    
84
    return new Static4D(tx,ty,tz,tw);
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
// return (qx,qy,qz,qw)*(rx,ry,rz,rw)
89

    
90
  public static Static4D quatMultiply( float qx, float qy, float qz, float qw, float rx, float ry, float rz, float rw )
91
    {
92
    float tx = rw*qx - rz*qy + ry*qz + rx*qw;
93
    float ty = rw*qy + rz*qx + ry*qw - rx*qz;
94
    float tz = rw*qz + rz*qw - ry*qx + rx*qy;
95
    float tw = rw*qw - rz*qz - ry*qy - rx*qx;
96

    
97
    return new Static4D(tx,ty,tz,tw);
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw)
102

    
103
  public static void quatMultiply( float[] ret, float qx, float qy, float qz, float qw, float rx, float ry, float rz, float rw )
104
    {
105
    ret[0] = rw*qx - rz*qy + ry*qz + rx*qw;
106
    ret[1] = rw*qy + rz*qx + ry*qw - rx*qz;
107
    ret[2] = rw*qz + rz*qw - ry*qx + rx*qy;
108
    ret[3] = rw*qw - rz*qz - ry*qy - rx*qx;
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw)
113

    
114
  public static void quatMultiply( float[] ret, float[] q, float[] r )
115
    {
116
    ret[0] = r[3]*q[0] - r[2]*q[1] + r[1]*q[2] + r[0]*q[3];
117
    ret[1] = r[3]*q[1] + r[2]*q[0] + r[1]*q[3] - r[0]*q[2];
118
    ret[2] = r[3]*q[2] + r[2]*q[3] - r[1]*q[0] + r[0]*q[1];
119
    ret[3] = r[3]*q[3] - r[2]*q[2] - r[1]*q[1] - r[0]*q[0];
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
124

    
125
  public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
126
    {
127
    float qx = quat.get0();
128
    float qy = quat.get1();
129
    float qz = quat.get2();
130
    float qw = quat.get3();
131

    
132
    Static4D tmp = quatMultiply(qx,qy,qz,qw,vector);
133

    
134
    return quatMultiply(tmp,-qx,-qy,-qz,qw);
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
// rotate (x1,x2,x3,x4) by quat  ( i.e. return quat*vector*(quat^-1) )
139

    
140
  public static Static4D rotateVectorByQuat(float x, float y, float z, float w, Static4D quat)
141
    {
142
    float qx = quat.get0();
143
    float qy = quat.get1();
144
    float qz = quat.get2();
145
    float qw = quat.get3();
146

    
147
    Static4D tmp = quatMultiply(qx,qy,qz,qw,x,y,z,w);
148

    
149
    return quatMultiply(tmp,-qx,-qy,-qz,qw);
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
// rotate vec by quat ( i.e. return quat*vector*(quat^-1) )
154

    
155
  public static void rotateVectorByQuat(float[] output, float[] vec, float[] quat)
156
    {
157
    float[] tmp = new float[4];
158

    
159
    quatMultiply(tmp,quat,vec);
160

    
161
    quat[0] = -quat[0];
162
    quat[1] = -quat[1];
163
    quat[2] = -quat[2];
164

    
165
    quatMultiply(output,tmp,quat);
166

    
167
    quat[0] = -quat[0];
168
    quat[1] = -quat[1];
169
    quat[2] = -quat[2];
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
174

    
175
  public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
176
    {
177
    float qx = quat.get0();
178
    float qy = quat.get1();
179
    float qz = quat.get2();
180
    float qw = quat.get3();
181

    
182
    Static4D tmp = quatMultiply(-qx,-qy,-qz,qw,vector);
183

    
184
    return quatMultiply(tmp,quat);
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  public static Static4D quatFromDrag(float dragX, float dragY)
190
    {
191
    float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
192
    float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
193
    float axisZ = 0;
194
    float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
195

    
196
    if( axisL>0 )
197
      {
198
      axisX /= axisL;
199
      axisY /= axisL;
200
      axisZ /= axisL;
201

    
202
      float ratio = axisL;
203
      ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
204

    
205
      float cosA = (float)Math.cos(Math.PI*ratio);
206
      float sinA = (float)Math.sqrt(1-cosA*cosA);
207

    
208
      return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
209
      }
210

    
211
    return new Static4D(0f, 0f, 0f, 1f);
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  public static double computeCos(double oldX, double oldY, double newX, double newY, double len1, double len2)
217
    {
218
    double ret= (oldX*newX+oldY*newY) / (len1*len2);
219
    if( ret<-1.0 ) return -1.0;
220
    if( ret> 1.0 ) return  1.0;
221

    
222
    return ret;
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
// sin of (signed!) angle between vectors 'old' and 'new', counterclockwise!
227

    
228
  public static double computeSin(double oldX, double oldY, double newX, double newY, double len1, double len2)
229
    {
230
    double ret= (newX*oldY-oldX*newY) / (len1*len2);
231
    if( ret<-1.0 ) return -1.0;
232
    if( ret> 1.0 ) return  1.0;
233

    
234
    return ret;
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
// return quat Q that turns 3D vector A=(ax,ay,az) to another 3D vector B=(bx,by,bz)
239
// take care of double-cover by ensuring that always Q.get3() >=0
240

    
241
  public static Static4D retRotationQuat(float ax, float ay, float az, float bx, float by, float bz)
242
    {
243
    float nx = ay*bz - az*by;
244
    float ny = az*bx - ax*bz;
245
    float nz = ax*by - ay*bx;
246

    
247
    float sin = (float)Math.sqrt(nx*nx + ny*ny + nz*nz);
248
    float cos = ax*bx + ay*by + az*bz;
249

    
250
    if( sin!=0 )
251
      {
252
      nx /= sin;
253
      ny /= sin;
254
      nz /= sin;
255
      }
256

    
257
    // Why sin<=0 and cos>=0 ?
258
    // 0<angle<180 -> 0<halfAngle<90 -> both sin and cos are positive.
259
    // But1: quats work counterclockwise -> negate cos.
260
    // But2: double-cover, we prefer to have the cos positive (so that unit=(0,0,0,1))
261
    // so negate again both cos and sin.
262
    float sinHalf =-(float)Math.sqrt((1-cos)/2);
263
    float cosHalf = (float)Math.sqrt((1+cos)/2);
264

    
265
    return new Static4D(nx*sinHalf,ny*sinHalf,nz*sinHalf,cosHalf);
266
    }
267
  }
(17-17/17)