Skip to content

Commit 981d132

Browse files
Jason SamsAndroid (Google) Code Review
authored andcommitted
Merge "Fixes and optimizations of two quaternion functions."
2 parents 257e67e + f13ada9 commit 981d132

File tree

1 file changed

+37
-41
lines changed

1 file changed

+37
-41
lines changed

libs/rs/scriptc/rs_quaternion.rsh

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,6 @@ rsQuaternionMultiply(rs_quaternion *q, float s) {
6565
q->z *= s;
6666
}
6767

68-
/**
69-
* Multiply quaternion by another quaternion
70-
* @param q destination quaternion
71-
* @param rhs right hand side quaternion to multiply by
72-
*/
73-
static void __attribute__((overloadable))
74-
rsQuaternionMultiply(rs_quaternion *q, const rs_quaternion *rhs) {
75-
q->w = -q->x*rhs->x - q->y*rhs->y - q->z*rhs->z + q->w*rhs->w;
76-
q->x = q->x*rhs->w + q->y*rhs->z - q->z*rhs->y + q->w*rhs->x;
77-
q->y = -q->x*rhs->z + q->y*rhs->w + q->z*rhs->x + q->w*rhs->y;
78-
q->z = q->x*rhs->y - q->y*rhs->x + q->z*rhs->w + q->w*rhs->z;
79-
}
80-
8168
/**
8269
* Add two quaternions
8370
* @param q destination quaternion to add to
@@ -167,6 +154,23 @@ rsQuaternionNormalize(rs_quaternion *q) {
167154
}
168155
}
169156

157+
/**
158+
* Multiply quaternion by another quaternion
159+
* @param q destination quaternion
160+
* @param rhs right hand side quaternion to multiply by
161+
*/
162+
static void __attribute__((overloadable))
163+
rsQuaternionMultiply(rs_quaternion *q, const rs_quaternion *rhs) {
164+
rs_quaternion qtmp;
165+
rsQuaternionSet(&qtmp, q);
166+
167+
q->w = qtmp.w*rhs->w - qtmp.x*rhs->x - qtmp.y*rhs->y - qtmp.z*rhs->z;
168+
q->x = qtmp.w*rhs->x + qtmp.x*rhs->w + qtmp.y*rhs->z - qtmp.z*rhs->y;
169+
q->y = qtmp.w*rhs->y + qtmp.y*rhs->w + qtmp.z*rhs->x - qtmp.x*rhs->z;
170+
q->z = qtmp.w*rhs->z + qtmp.z*rhs->w + qtmp.x*rhs->y - qtmp.y*rhs->x;
171+
rsQuaternionNormalize(q);
172+
}
173+
170174
/**
171175
* Performs spherical linear interpolation between two quaternions
172176
* @param q result quaternion from interpolation
@@ -222,34 +226,26 @@ rsQuaternionSlerp(rs_quaternion *q, const rs_quaternion *q0, const rs_quaternion
222226
* @param p normalized quaternion
223227
*/
224228
static void rsQuaternionGetMatrixUnit(rs_matrix4x4 *m, const rs_quaternion *q) {
225-
float x2 = 2.0f * q->x * q->x;
226-
float y2 = 2.0f * q->y * q->y;
227-
float z2 = 2.0f * q->z * q->z;
228-
float xy = 2.0f * q->x * q->y;
229-
float wz = 2.0f * q->w * q->z;
230-
float xz = 2.0f * q->x * q->z;
231-
float wy = 2.0f * q->w * q->y;
232-
float wx = 2.0f * q->w * q->x;
233-
float yz = 2.0f * q->y * q->z;
234-
235-
m->m[0] = 1.0f - y2 - z2;
236-
m->m[1] = xy - wz;
237-
m->m[2] = xz + wy;
238-
m->m[3] = 0.0f;
239-
240-
m->m[4] = xy + wz;
241-
m->m[5] = 1.0f - x2 - z2;
242-
m->m[6] = yz - wx;
243-
m->m[7] = 0.0f;
244-
245-
m->m[8] = xz - wy;
246-
m->m[9] = yz - wx;
247-
m->m[10] = 1.0f - x2 - y2;
248-
m->m[11] = 0.0f;
249-
250-
m->m[12] = 0.0f;
251-
m->m[13] = 0.0f;
252-
m->m[14] = 0.0f;
229+
float xx = q->x * q->x;
230+
float xy = q->x * q->y;
231+
float xz = q->x * q->z;
232+
float xw = q->x * q->w;
233+
float yy = q->y * q->y;
234+
float yz = q->y * q->z;
235+
float yw = q->y * q->w;
236+
float zz = q->z * q->z;
237+
float zw = q->z * q->w;
238+
239+
m->m[0] = 1.0f - 2.0f * ( yy + zz );
240+
m->m[4] = 2.0f * ( xy - zw );
241+
m->m[8] = 2.0f * ( xz + yw );
242+
m->m[1] = 2.0f * ( xy + zw );
243+
m->m[5] = 1.0f - 2.0f * ( xx + zz );
244+
m->m[9] = 2.0f * ( yz - xw );
245+
m->m[2] = 2.0f * ( xz - yw );
246+
m->m[6] = 2.0f * ( yz + xw );
247+
m->m[10] = 1.0f - 2.0f * ( xx + yy );
248+
m->m[3] = m->m[7] = m->m[11] = m->m[12] = m->m[13] = m->m[14] = 0.0f;
253249
m->m[15] = 1.0f;
254250
}
255251

0 commit comments

Comments
 (0)