Project

General

Profile

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

library / src / main / java / org / distorted / library / main / QuatHelper.java @ f0c98ff5

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
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
113

    
114
  public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
115
    {
116
    float qx = quat.get0();
117
    float qy = quat.get1();
118
    float qz = quat.get2();
119
    float qw = quat.get3();
120

    
121
    Static4D tmp = quatMultiply(qx,qy,qz,qw,vector);
122

    
123
    return quatMultiply(tmp,-qx,-qy,-qz,qw);
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
// rotate (x1,x2,x3,x4) by quat  ( i.e. return quat*vector*(quat^-1) )
128

    
129
  public static Static4D rotateVectorByQuat(float x, float y, float z, float w, Static4D quat)
130
    {
131
    float qx = quat.get0();
132
    float qy = quat.get1();
133
    float qz = quat.get2();
134
    float qw = quat.get3();
135

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

    
138
    return quatMultiply(tmp,-qx,-qy,-qz,qw);
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
// rotate vec by quat ( i.e. return quat*vector*(quat^-1) )
143

    
144
  public static void rotateVectorByQuat(float[] output, float[] vec, float[] quat)
145
    {
146
    float[] tmp = new float[4];
147

    
148
    quatMultiply(tmp,quat,vec);
149

    
150
    quat[0] = -quat[0];
151
    quat[1] = -quat[1];
152
    quat[2] = -quat[2];
153

    
154
    quatMultiply(output,tmp,quat);
155

    
156
    quat[0] = -quat[0];
157
    quat[1] = -quat[1];
158
    quat[2] = -quat[2];
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
163

    
164
  public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
165
    {
166
    float qx = quat.get0();
167
    float qy = quat.get1();
168
    float qz = quat.get2();
169
    float qw = quat.get3();
170

    
171
    Static4D tmp = quatMultiply(-qx,-qy,-qz,qw,vector);
172

    
173
    return quatMultiply(tmp,quat);
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  public static Static4D quatFromDrag(float dragX, float dragY)
179
    {
180
    float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
181
    float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
182
    float axisZ = 0;
183
    float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
184

    
185
    if( axisL>0 )
186
      {
187
      axisX /= axisL;
188
      axisY /= axisL;
189
      axisZ /= axisL;
190

    
191
      float ratio = axisL;
192
      ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
193

    
194
      float cosA = (float)Math.cos(Math.PI*ratio);
195
      float sinA = (float)Math.sqrt(1-cosA*cosA);
196

    
197
      return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
198
      }
199

    
200
    return new Static4D(0f, 0f, 0f, 1f);
201
    }
202

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

    
205
  public static double computeCos(double oldX, double oldY, double newX, double newY, double len1, double len2)
206
    {
207
    double ret= (oldX*newX+oldY*newY) / (len1*len2);
208
    if( ret<-1.0 ) return -1.0;
209
    if( ret> 1.0 ) return  1.0;
210

    
211
    return ret;
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
// sin of (signed!) angle between vectors 'old' and 'new', counterclockwise!
216

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

    
223
    return ret;
224
    }
225

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

    
230
  public static Static4D retRotationQuat(float ax, float ay, float az, float bx, float by, float bz)
231
    {
232
    float nx = ay*bz - az*by;
233
    float ny = az*bx - ax*bz;
234
    float nz = ax*by - ay*bx;
235

    
236
    float sin = (float)Math.sqrt(nx*nx + ny*ny + nz*nz);
237
    float cos = ax*bx + ay*by + az*bz;
238

    
239
    if( sin!=0 )
240
      {
241
      nx /= sin;
242
      ny /= sin;
243
      nz /= sin;
244
      }
245

    
246
    // Why sin<=0 and cos>=0 ?
247
    // 0<angle<180 -> 0<halfAngle<90 -> both sin and cos are positive.
248
    // But1: quats work counterclockwise -> negate cos.
249
    // But2: double-cover, we prefer to have the cos positive (so that unit=(0,0,0,1))
250
    // so negate again both cos and sin.
251
    float sinHalf =-(float)Math.sqrt((1-cos)/2);
252
    float cosHalf = (float)Math.sqrt((1+cos)/2);
253

    
254
    return new Static4D(nx*sinHalf,ny*sinHalf,nz*sinHalf,cosHalf);
255
    }
256
  }
(17-17/17)