23 #include "worldsimconfig.h"
28 #include <gsl/gsl_blas.h>
67 wMatrix(
const real* position,
const real* rotation );
86 wVector getEuleroAngles()
const;
112 void transformTriplex( real* dst,
int dstStrideInBytes, real* src,
int srcStrideInBytes,
int count)
const;
131 static wMatrix pitch( real ang );
133 static wMatrix yaw( real ang );
135 static wMatrix roll( real ang );
150 #include "wquaternion.h"
154 inline wMatrix::wMatrix() : x_ax(&data[0]), y_ax(&data[4]), z_ax(&data[8]), w_pos(&data[12]) {
189 x2 = 2.0 * rotation.q1 * rotation.q1;
190 y2 = 2.0 * rotation.q2 * rotation.q2;
191 z2 = 2.0 * rotation.q3 * rotation.q3;
194 xy = 2.0 * rotation.q1 * rotation.q2;
195 xz = 2.0 * rotation.q1 * rotation.q3;
196 xw = 2.0 * rotation.q1 * rotation.q0;
197 yz = 2.0 * rotation.q2 * rotation.q3;
198 yw = 2.0 * rotation.q2 * rotation.q0;
199 zw = 2.0 * rotation.q3 * rotation.q0;
201 x_ax =
wVector( 1.0-y2-z2, xy+zw, xz-yw, 0.0 );
202 y_ax =
wVector( xy-zw, 1.0-x2-z2, yz+xw, 0.0 );
203 z_ax =
wVector( xz+yw, yz-xw, 1.0-x2-y2, 0.0 );
205 w_pos.x = position.x;
206 w_pos.y = position.y;
207 w_pos.z = position.z;
211 inline wMatrix::wMatrix(
const real* pos,
const real* rot ) : x_ax(&data[0]), y_ax(&data[4]), z_ax(&data[8]), w_pos(&data[12]) {
216 x_ax[0]=rot[0]; x_ax[1]=rot[1]; x_ax[2]=rot[2]; x_ax[3]=0.0 ;
217 y_ax[0]=rot[4]; y_ax[1]=rot[5]; y_ax[2]=rot[6]; y_ax[3]=0.0 ;
218 z_ax[0]=rot[8]; z_ax[1]=rot[9]; z_ax[2]=rot[10]; z_ax[3]=0.0 ;
219 w_pos[0]=pos[0]; w_pos[1]=pos[1]; w_pos[2]=pos[2]; w_pos[3]=1.0 ;
227 memcpy(data, other.data, 16 *
sizeof(real));
232 if (&other ==
this) {
236 memcpy(data, other.data, 16 *
sizeof(real));
251 wVector(x_ax.y, y_ax.y, z_ax.y, 0.0f),
252 wVector(x_ax.z, y_ax.z, z_ax.z, 0.0f),
253 wVector(- (w_pos % x_ax), - (w_pos % y_ax), - (w_pos % z_ax), 1.0f));
258 wVector(x_ax.y, y_ax.y, z_ax.y, 0.0f),
259 wVector(x_ax.z, y_ax.z, z_ax.z, 0.0f),
260 wVector(0.0f, 0.0f, 0.0f, 1.0f));
265 wVector(x_ax.y, y_ax.y, z_ax.y, w_pos.y),
266 wVector(x_ax.z, y_ax.z, z_ax.z, w_pos.z),
267 wVector(x_ax.w, y_ax.w, z_ax.w, w_pos.w));
274 const real minSin = 0.99995f;
277 yaw = asin( -
min(
max( matrix[0][2], -0.999999 ), 0.999999) );
278 if( matrix[0][2] < minSin ) {
279 if( matrix[0][2] > -minSin ) {
280 roll = atan2( matrix[0][1], matrix[0][0] );
281 pitch = atan2( matrix[1][2], matrix[2][2] );
283 pitch = atan2( matrix[1][0], matrix[1][1] );
286 pitch = -atan2( matrix[1][0], matrix[1][1] );
289 return wVector(pitch, yaw, roll, 0.0);
319 wVector diffCentre = ret.w_pos - centre;
321 ret.w_pos = diffCentre + centre;
327 return wMatrix (
wVector (A[0][0] * B[0][0] + A[0][1] * B[1][0] + A[0][2] * B[2][0] + A[0][3] * B[3][0],
328 A[0][0] * B[0][1] + A[0][1] * B[1][1] + A[0][2] * B[2][1] + A[0][3] * B[3][1],
329 A[0][0] * B[0][2] + A[0][1] * B[1][2] + A[0][2] * B[2][2] + A[0][3] * B[3][2],
330 A[0][0] * B[0][3] + A[0][1] * B[1][3] + A[0][2] * B[2][3] + A[0][3] * B[3][3]),
331 wVector (A[1][0] * B[0][0] + A[1][1] * B[1][0] + A[1][2] * B[2][0] + A[1][3] * B[3][0],
332 A[1][0] * B[0][1] + A[1][1] * B[1][1] + A[1][2] * B[2][1] + A[1][3] * B[3][1],
333 A[1][0] * B[0][2] + A[1][1] * B[1][2] + A[1][2] * B[2][2] + A[1][3] * B[3][2],
334 A[1][0] * B[0][3] + A[1][1] * B[1][3] + A[1][2] * B[2][3] + A[1][3] * B[3][3]),
335 wVector (A[2][0] * B[0][0] + A[2][1] * B[1][0] + A[2][2] * B[2][0] + A[2][3] * B[3][0],
336 A[2][0] * B[0][1] + A[2][1] * B[1][1] + A[2][2] * B[2][1] + A[2][3] * B[3][1],
337 A[2][0] * B[0][2] + A[2][1] * B[1][2] + A[2][2] * B[2][2] + A[2][3] * B[3][2],
338 A[2][0] * B[0][3] + A[2][1] * B[1][3] + A[2][2] * B[2][3] + A[2][3] * B[3][3]),
346 return wVector( v.x * x_ax.x + v.y * y_ax.x + v.z * z_ax.x,
347 v.x * x_ax.y + v.y * y_ax.y + v.z * z_ax.y,
348 v.x * x_ax.z + v.y * y_ax.z + v.z * z_ax.z);
352 return wVector( v%x_ax, v%y_ax, v%z_ax );
368 dstStrideInBytes /=
sizeof(real);
369 srcStrideInBytes /=
sizeof(real);
371 for(
int i=0; i<count; i++ ) {
375 dst[0] = x*x_ax.x + y*y_ax.x + z*z_ax.x + w_pos.x;
376 dst[1] = x*x_ax.y + y*y_ax.y + z*z_ax.y + w_pos.y;
377 dst[2] = x*x_ax.z + y*y_ax.z + z*z_ax.z + w_pos.z;
378 dst += dstStrideInBytes;
379 src += srcStrideInBytes;
387 cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, 4, 4, 4, 1.0, A.data, 4, B.data, 4, 0.0, res.data, 4);
391 return wMatrix (
wVector (A[0][0] * B[0][0] + A[0][1] * B[1][0] + A[0][2] * B[2][0] + A[0][3] * B[3][0],
392 A[0][0] * B[0][1] + A[0][1] * B[1][1] + A[0][2] * B[2][1] + A[0][3] * B[3][1],
393 A[0][0] * B[0][2] + A[0][1] * B[1][2] + A[0][2] * B[2][2] + A[0][3] * B[3][2],
394 A[0][0] * B[0][3] + A[0][1] * B[1][3] + A[0][2] * B[2][3] + A[0][3] * B[3][3]),
395 wVector (A[1][0] * B[0][0] + A[1][1] * B[1][0] + A[1][2] * B[2][0] + A[1][3] * B[3][0],
396 A[1][0] * B[0][1] + A[1][1] * B[1][1] + A[1][2] * B[2][1] + A[1][3] * B[3][1],
397 A[1][0] * B[0][2] + A[1][1] * B[1][2] + A[1][2] * B[2][2] + A[1][3] * B[3][2],
398 A[1][0] * B[0][3] + A[1][1] * B[1][3] + A[1][2] * B[2][3] + A[1][3] * B[3][3]),
399 wVector (A[2][0] * B[0][0] + A[2][1] * B[1][0] + A[2][2] * B[2][0] + A[2][3] * B[3][0],
400 A[2][0] * B[0][1] + A[2][1] * B[1][1] + A[2][2] * B[2][1] + A[2][3] * B[3][1],
401 A[2][0] * B[0][2] + A[2][1] * B[1][2] + A[2][2] * B[2][2] + A[2][3] * B[3][2],
402 A[2][0] * B[0][3] + A[2][1] * B[1][3] + A[2][2] * B[2][3] + A[2][3] * B[3][3]),
403 wVector (A[3][0] * B[0][0] + A[3][1] * B[1][0] + A[3][2] * B[2][0] + A[3][3] * B[3][0],
404 A[3][0] * B[0][1] + A[3][1] * B[1][1] + A[3][2] * B[2][1] + A[3][3] * B[3][1],
405 A[3][0] * B[0][2] + A[3][1] * B[1][2] + A[3][2] * B[2][2] + A[3][3] * B[3][2],
406 A[3][0] * B[0][3] + A[3][1] * B[1][3] + A[3][2] * B[2][3] + A[3][3] * B[3][3]));
418 tmp.w = globalPlane % w_pos + globalPlane.w;
424 real zz = (x_ax.y*y_ax.z - x_ax.z*y_ax.y)*z_ax.x
425 + (x_ax.z*y_ax.x - x_ax.x*y_ax.z)*z_ax.y
426 + (x_ax.x*y_ax.y - x_ax.y*y_ax.x)*z_ax.z;
430 if( fabs( zz ) < 0.99f) {
453 wVector (0.0f, 1.0f, 0.0f, 0.0f),
454 wVector (0.0f, 0.0f, 1.0f, 0.0f),
455 wVector (0.0f, 0.0f, 0.0f, 1.0f) );
460 wVector (0.0f, 0.0f, 0.0f, 0.0f),
461 wVector (0.0f, 0.0f, 0.0f, 0.0f),
462 wVector (0.0f, 0.0f, 0.0f, 0.0f) );
471 if( fabs(Z.z) > 0.577f ) {
472 X = Z *
wVector( -Z.y, Z.z, 0.0f );
474 X = Z *
wVector( -Z.y, Z.x, 0.0f );
486 real cosAng = cos(ang);
487 real sinAng = sin(ang);
489 wVector(0.0f, cosAng, sinAng, 0.0f),
490 wVector(0.0f, -sinAng, cosAng, 0.0f),
491 wVector(0.0f, 0.0f, 0.0f, 1.0f) );
495 real cosAng = cos(ang);
496 real sinAng = sin(ang);
498 wVector (0.0f, 1.0f, 0.0f, 0.0f),
499 wVector (sinAng, 0.0f, cosAng, 0.0f),
500 wVector (0.0f, 0.0f, 0.0f, 1.0f) );
504 real cosAng = cos(ang);
505 real sinAng = sin(ang);
507 wVector (-sinAng, cosAng, 0.0f, 0.0f),
508 wVector ( 0.0f, 0.0f, 1.0f, 0.0f),
509 wVector ( 0.0f, 0.0f, 0.0f, 1.0f) );