Project

General

Profile

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

library / src / main / java / org / distorted / library / helpers / QuatHelper.java @ c0255951

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.helpers;
22

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

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
104
  public static void quatMultiply( float[] ret, float[] q, float[] r )
105
    {
106
    ret[0] = r[3]*q[0] - r[2]*q[1] + r[1]*q[2] + r[0]*q[3];
107
    ret[1] = r[3]*q[1] + r[2]*q[0] + r[1]*q[3] - r[0]*q[2];
108
    ret[2] = r[3]*q[2] + r[2]*q[3] - r[1]*q[0] + r[0]*q[1];
109
    ret[3] = r[3]*q[3] - r[2]*q[2] - r[1]*q[1] - r[0]*q[0];
110
    }
111

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

    
115
  public static void quatMultiply( float[] ret, float qx, float qy, float qz, float qw, float rx, float ry, float rz, float rw )
116
    {
117
    ret[0] = rw*qx - rz*qy + ry*qz + rx*qw;
118
    ret[1] = rw*qy + rz*qx + ry*qw - rx*qz;
119
    ret[2] = rw*qz + rz*qw - ry*qx + rx*qy;
120
    ret[3] = rw*qw - rz*qz - ry*qy - rx*qx;
121
    }
122

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

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

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

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

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

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

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

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

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
// rotate (x1,x2,x3,x4) by quat  ( i.e. return quat*vector*(quat^-1) )
155

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

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

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

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

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

    
176
    quatMultiply(tmp,quat,vec);
177

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

    
182
    quatMultiply(output,tmp,quat);
183

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

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

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

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

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

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

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

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

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

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

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

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

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

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

    
239
    return ret;
240
    }
241

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

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

    
251
    return ret;
252
    }
253

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

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

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

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

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

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