Project

General

Profile

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

library / src / main / java / org / distorted / library / main / QuatHelper.java @ 0e1d3d2e

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[] q, float[] r )
104
    {
105
    ret[0] = r[3]*q[0] - r[2]*q[1] + r[1]*q[2] + r[0]*q[3];
106
    ret[1] = r[3]*q[1] + r[2]*q[0] + r[1]*q[3] - r[0]*q[2];
107
    ret[2] = r[3]*q[2] + r[2]*q[3] - r[1]*q[0] + r[0]*q[1];
108
    ret[3] = r[3]*q[3] - r[2]*q[2] - r[1]*q[1] - r[0]*q[0];
109
    }
110

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

    
114
  public static void quatMultiply( float[] ret, float qx, float qy, float qz, float qw, float rx, float ry, float rz, float rw )
115
    {
116
    ret[0] = rw*qx - rz*qy + ry*qz + rx*qw;
117
    ret[1] = rw*qy + rz*qx + ry*qw - rx*qz;
118
    ret[2] = rw*qz + rz*qw - ry*qx + rx*qy;
119
    ret[3] = rw*qw - rz*qz - ry*qy - rx*qx;
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 (x1,x2,x3,x4) by quat  ( i.e. return quat*vector*(quat^-1) )
154

    
155
  public static void rotateVectorByQuat(float[] output, float x, float y, float z, float w, Static4D quat)
156
    {
157
    float[] tmp = new float[4];
158

    
159
    float qx = quat.get0();
160
    float qy = quat.get1();
161
    float qz = quat.get2();
162
    float qw = quat.get3();
163

    
164
    quatMultiply(tmp,qx,qy,qz,qw,x,y,z,w);
165
    quatMultiply(output,tmp[0],tmp[1],tmp[2],tmp[3],-qx,-qy,-qz,qw);
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
// rotate vec by quat ( i.e. return quat*vector*(quat^-1) )
170

    
171
  public static void rotateVectorByQuat(float[] output, float[] vec, float[] quat)
172
    {
173
    float[] tmp = new float[4];
174

    
175
    quatMultiply(tmp,quat,vec);
176

    
177
    quat[0] = -quat[0];
178
    quat[1] = -quat[1];
179
    quat[2] = -quat[2];
180

    
181
    quatMultiply(output,tmp,quat);
182

    
183
    quat[0] = -quat[0];
184
    quat[1] = -quat[1];
185
    quat[2] = -quat[2];
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
190

    
191
  public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
192
    {
193
    float qx = quat.get0();
194
    float qy = quat.get1();
195
    float qz = quat.get2();
196
    float qw = quat.get3();
197

    
198
    Static4D tmp = quatMultiply(-qx,-qy,-qz,qw,vector);
199

    
200
    return quatMultiply(tmp,quat);
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  public static Static4D quatFromDrag(float dragX, float dragY)
206
    {
207
    float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
208
    float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
209
    float axisZ = 0;
210
    float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
211

    
212
    if( axisL>0 )
213
      {
214
      axisX /= axisL;
215
      axisY /= axisL;
216
      axisZ /= axisL;
217

    
218
      float ratio = axisL;
219
      ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
220

    
221
      float cosA = (float)Math.cos(Math.PI*ratio);
222
      float sinA = (float)Math.sqrt(1-cosA*cosA);
223

    
224
      return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
225
      }
226

    
227
    return new Static4D(0f, 0f, 0f, 1f);
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  public static double computeCos(double oldX, double oldY, double newX, double newY, double len1, double len2)
233
    {
234
    double ret= (oldX*newX+oldY*newY) / (len1*len2);
235
    if( ret<-1.0 ) return -1.0;
236
    if( ret> 1.0 ) return  1.0;
237

    
238
    return ret;
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
// sin of (signed!) angle between vectors 'old' and 'new', counterclockwise!
243

    
244
  public static double computeSin(double oldX, double oldY, double newX, double newY, double len1, double len2)
245
    {
246
    double ret= (newX*oldY-oldX*newY) / (len1*len2);
247
    if( ret<-1.0 ) return -1.0;
248
    if( ret> 1.0 ) return  1.0;
249

    
250
    return ret;
251
    }
252

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

    
257
  public static Static4D retRotationQuat(float ax, float ay, float az, float bx, float by, float bz)
258
    {
259
    float nx = ay*bz - az*by;
260
    float ny = az*bx - ax*bz;
261
    float nz = ax*by - ay*bx;
262

    
263
    float sin = (float)Math.sqrt(nx*nx + ny*ny + nz*nz);
264
    float cos = ax*bx + ay*by + az*bz;
265

    
266
    if( sin!=0 )
267
      {
268
      nx /= sin;
269
      ny /= sin;
270
      nz /= sin;
271
      }
272

    
273
    // Why sin<=0 and cos>=0 ?
274
    // 0<angle<180 -> 0<halfAngle<90 -> both sin and cos are positive.
275
    // But1: quats work counterclockwise -> negate cos.
276
    // But2: double-cover, we prefer to have the cos positive (so that unit=(0,0,0,1))
277
    // so negate again both cos and sin.
278
    float sinHalf =-(float)Math.sqrt((1-cos)/2);
279
    float cosHalf = (float)Math.sqrt((1+cos)/2);
280

    
281
    return new Static4D(nx*sinHalf,ny*sinHalf,nz*sinHalf,cosHalf);
282
    }
283
  }
(17-17/17)