Revision 4c568a67
Added by Leszek Koltunski almost 4 years ago
| src/main/java/org/distorted/library/main/QuatHelper.java | ||
|---|---|---|
| 48 | 48 |
return new Static4D(tx,ty,tz,tw); |
| 49 | 49 |
} |
| 50 | 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 |
|
|
| 51 | 122 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 52 | 123 |
// rotate 'vector' by quat ( i.e. return quat*vector*(quat^-1) ) |
| 53 | 124 |
|
| ... | ... | |
| 58 | 129 |
float qz = quat.get2(); |
| 59 | 130 |
float qw = quat.get3(); |
| 60 | 131 |
|
| 61 |
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw); |
|
| 62 |
Static4D tmp = quatMultiply(quat,vector); |
|
| 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); |
|
| 63 | 166 |
|
| 64 |
return quatMultiply(tmp,quatInverted); |
|
| 167 |
quat[0] = -quat[0]; |
|
| 168 |
quat[1] = -quat[1]; |
|
| 169 |
quat[2] = -quat[2]; |
|
| 65 | 170 |
} |
| 66 | 171 |
|
| 67 | 172 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 74 | 179 |
float qz = quat.get2(); |
| 75 | 180 |
float qw = quat.get3(); |
| 76 | 181 |
|
| 77 |
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw); |
|
| 78 |
Static4D tmp = quatMultiply(quatInverted,vector); |
|
| 182 |
Static4D tmp = quatMultiply(-qx,-qy,-qz,qw,vector); |
|
| 79 | 183 |
|
| 80 | 184 |
return quatMultiply(tmp,quat); |
| 81 | 185 |
} |
Also available in: Unified diff
Float vertices - scratchbook