2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
21 #include "Quaternions.h"
24 quaternion Quat_Mult(quaternion q1, quaternion q2)
27 float a, b, c, d, e, f, g, h;
28 a = (q1.w + q1.x) * (q2.w + q2.x);
29 b = (q1.z - q1.y) * (q2.y - q2.z);
30 c = (q1.w - q1.x) * (q2.y + q2.z);
31 d = (q1.y + q1.z) * (q2.w - q2.x);
32 e = (q1.x + q1.z) * (q2.x + q2.y);
33 f = (q1.x - q1.z) * (q2.x - q2.y);
34 g = (q1.w + q1.y) * (q2.w - q2.z);
35 h = (q1.w - q1.y) * (q2.w + q2.z);
36 QResult.w = b + (-e - f + g + h) / 2;
37 QResult.x = a - (e + f + g + h) / 2;
38 QResult.y = c + (e - f + g - h) / 2;
39 QResult.z = d + (e - f - g + h) / 2;
45 quaternion To_Quat(Matrix_t m)
47 // From Jason Shankel, (C) 2000.
48 static quaternion Quat;
50 static double Tr = m[0][0] + m[1][1] + m[2][2] + 1.0, fourD;
55 fourD = 2.0 * fast_sqrt(Tr);
57 q[0] = (m[2][1] - m[1][2]) / fourD;
58 q[1] = (m[0][2] - m[2][0]) / fourD;
59 q[2] = (m[1][0] - m[0][1]) / fourD;
61 if (m[0][0] > m[1][1]) {
66 if (m[2][2] > m[i][i]) {
71 fourD = 2.0 * fast_sqrt(m[i][i] - m[j][j] - m[k][k] + 1.0);
73 q[j] = (m[j][i] + m[i][j]) / fourD;
74 q[k] = (m[k][i] + m[i][k]) / fourD;
75 q[3] = (m[j][k] - m[k][j]) / fourD;
84 void Quat_2_Matrix(quaternion Quat, Matrix_t m)
86 // From the GLVelocity site (http://glvelocity.gamedev.net)
94 m[0][0] = 1.0f - 2.0f * (fYY + fZZ);
95 m[1][0] = 2.0f * (fX * fY + fW * fZ);
96 m[2][0] = 2.0f * (fX * fZ - fW * fY);
98 m[0][1] = 2.0f * (fX * fY - fW * fZ);
99 m[1][1] = 1.0f - 2.0f * (fXX + fZZ);
100 m[2][1] = 2.0f * (fY * fZ + fW * fX);
102 m[0][2] = 2.0f * (fX * fZ + fW * fY);
103 m[1][2] = 2.0f * (fX * fZ - fW * fX);
104 m[2][2] = 1.0f - 2.0f * (fXX + fYY);
111 quaternion To_Quat(angle_axis Ang_Ax)
113 // From the Quaternion Powers article on gamedev.net
114 static quaternion Quat;
116 Quat.x = Ang_Ax.x * sin(Ang_Ax.angle / 2);
117 Quat.y = Ang_Ax.y * sin(Ang_Ax.angle / 2);
118 Quat.z = Ang_Ax.z * sin(Ang_Ax.angle / 2);
119 Quat.w = cos(Ang_Ax.angle / 2);
122 angle_axis Quat_2_AA(quaternion Quat)
124 static angle_axis Ang_Ax;
125 static float scale, tw;
126 tw = (float)acosf(Quat.w) * 2;
127 scale = (float)sin(tw / 2.0);
128 Ang_Ax.x = Quat.x / scale;
129 Ang_Ax.y = Quat.y / scale;
130 Ang_Ax.z = Quat.z / scale;
132 Ang_Ax.angle = 2.0 * acosf(Quat.w) / (float)PI * 180;
136 quaternion To_Quat(int In_Degrees, euler Euler)
138 // From the gamasutra quaternion article
139 static quaternion Quat;
140 static float cr, cp, cy, sr, sp, sy, cpcy, spsy;
141 //If we are in Degree mode, convert to Radians
143 Euler.x = Euler.x * (float)PI / 180;
144 Euler.y = Euler.y * (float)PI / 180;
145 Euler.z = Euler.z * (float)PI / 180;
147 //Calculate trig identities
148 //Formerly roll, pitch, yaw
149 cr = float(cos(Euler.x / 2));
150 cp = float(cos(Euler.y / 2));
151 cy = float(cos(Euler.z / 2));
152 sr = float(sin(Euler.x / 2));
153 sp = float(sin(Euler.y / 2));
154 sy = float(sin(Euler.z / 2));
158 Quat.w = cr * cpcy + sr * spsy;
159 Quat.x = sr * cpcy - cr * spsy;
160 Quat.y = cr * sp * cy + sr * cp * sy;
161 Quat.z = cr * cp * sy - sr * sp * cy;
166 quaternion QNormalize(quaternion Quat)
169 norm = Quat.x * Quat.x +
173 Quat.x = float(Quat.x / norm);
174 Quat.y = float(Quat.y / norm);
175 Quat.z = float(Quat.z / norm);
176 Quat.w = float(Quat.w / norm);
180 XYZ Quat2Vector(quaternion Quat)
191 tempvec.x = 2.0f * (fX * fZ - fW * fY);
192 tempvec.y = 2.0f * (fY * fZ + fW * fX);
193 tempvec.z = 1.0f - 2.0f * (fX * fX + fY * fY);
198 bool PointInTriangle(Vector *p, Vector normal, float p11, float p12, float p13, float p21, float p22, float p23, float p31, float p32, float p33)
200 static float u0, u1, u2;
201 static float v0, v1, v2;
206 static float pointv[3];
210 static float normalv[3];
231 normalv[0] = normal.x;
232 normalv[1] = normal.y;
233 normalv[2] = normal.z;
235 #define ABS(X) (((X)<0.f)?-(X):(X) )
236 #define MAX(A, B) (((A)<(B))?(B):(A))
237 max = MAX(MAX(ABS(normalv[0]), ABS(normalv[1])), ABS(normalv[2]));
239 if (max == ABS(normalv[0])) {
243 if (max == ABS(normalv[1])) {
247 if (max == ABS(normalv[2])) {
253 u0 = pointv[i] - p1v[i];
254 v0 = pointv[j] - p1v[j];
255 u1 = p2v[i] - p1v[i];
256 v1 = p2v[j] - p1v[j];
257 u2 = p3v[i] - p1v[i];
258 v2 = p3v[j] - p1v[j];
260 if (u1 > -1.0e-05f && u1 < 1.0e-05f) { // == 0.0f)
262 if (0.0f <= b && b <= 1.0f) {
263 a = (v0 - b * v2) / v1;
264 if ((a >= 0.0f) && (( a + b ) <= 1.0f))
268 b = (v0 * u1 - u0 * v1) / (v2 * u1 - u2 * v1);
269 if (0.0f <= b && b <= 1.0f) {
270 a = (u0 - b * u2) / u1;
271 if ((a >= 0.0f) && (( a + b ) <= 1.0f ))
279 bool LineFacet(Vector p1, Vector p2, Vector pa, Vector pb, Vector pc, Vector *p)
282 static float denom, mu;
285 //Calculate the parameters for the plane
286 n.x = (pb.y - pa.y) * (pc.z - pa.z) - (pb.z - pa.z) * (pc.y - pa.y);
287 n.y = (pb.z - pa.z) * (pc.x - pa.x) - (pb.x - pa.x) * (pc.z - pa.z);
288 n.z = (pb.x - pa.x) * (pc.y - pa.y) - (pb.y - pa.y) * (pc.x - pa.x);
290 d = - n.x * pa.x - n.y * pa.y - n.z * pa.z;
292 //Calculate the position on the line that intersects the plane
293 denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z);
294 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
296 mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom;
297 p->x = p1.x + mu * (p2.x - p1.x);
298 p->y = p1.y + mu * (p2.y - p1.y);
299 p->z = p1.z + mu * (p2.z - p1.z);
300 if (mu < 0 || mu > 1) // Intersection not along line segment
303 if (!PointInTriangle( p, n, pa.x, pa.y, pa.z, pb.x, pb.y, pb.z, pc.x, pc.y, pc.z)) {
310 bool PointInTriangle(XYZ *p, XYZ normal, XYZ *p1, XYZ *p2, XYZ *p3)
312 static float u0, u1, u2;
313 static float v0, v1, v2;
317 static bool bInter = 0;
318 static float pointv[3];
322 static float normalv[3];
343 normalv[0] = normal.x;
344 normalv[1] = normal.y;
345 normalv[2] = normal.z;
347 #define ABS(X) (((X)<0.f)?-(X):(X) )
348 #define MAX(A, B) (((A)<(B))?(B):(A))
349 max = MAX(MAX(ABS(normalv[0]), ABS(normalv[1])), ABS(normalv[2]));
351 if (max == ABS(normalv[0])) {
355 if (max == ABS(normalv[1])) {
359 if (max == ABS(normalv[2])) {
365 u0 = pointv[i] - p1v[i];
366 v0 = pointv[j] - p1v[j];
367 u1 = p2v[i] - p1v[i];
368 v1 = p2v[j] - p1v[j];
369 u2 = p3v[i] - p1v[i];
370 v2 = p3v[j] - p1v[j];
372 if (u1 > -1.0e-05f && u1 < 1.0e-05f) { // == 0.0f)
374 if (0.0f <= b && b <= 1.0f) {
375 a = (v0 - b * v2) / v1;
376 if ((a >= 0.0f) && (( a + b ) <= 1.0f))
380 b = (v0 * u1 - u0 * v1) / (v2 * u1 - u2 * v1);
381 if (0.0f <= b && b <= 1.0f) {
382 a = (u0 - b * u2) / u1;
383 if ((a >= 0.0f) && (( a + b ) <= 1.0f ))
391 bool LineFacet(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ *p)
394 static float denom, mu;
397 //Calculate the parameters for the plane
398 n.x = (pb.y - pa.y) * (pc.z - pa.z) - (pb.z - pa.z) * (pc.y - pa.y);
399 n.y = (pb.z - pa.z) * (pc.x - pa.x) - (pb.x - pa.x) * (pc.z - pa.z);
400 n.z = (pb.x - pa.x) * (pc.y - pa.y) - (pb.y - pa.y) * (pc.x - pa.x);
402 d = - n.x * pa.x - n.y * pa.y - n.z * pa.z;
404 //Calculate the position on the line that intersects the plane
405 denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z);
406 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
408 mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom;
409 p->x = p1.x + mu * (p2.x - p1.x);
410 p->y = p1.y + mu * (p2.y - p1.y);
411 p->z = p1.z + mu * (p2.z - p1.z);
412 if (mu < 0 || mu > 1) // Intersection not along line segment
415 if (!PointInTriangle( p, n, &pa, &pb, &pc)) {
422 float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ *p)
425 static float denom, mu;
428 //Calculate the parameters for the plane
429 n.x = (pb.y - pa.y) * (pc.z - pa.z) - (pb.z - pa.z) * (pc.y - pa.y);
430 n.y = (pb.z - pa.z) * (pc.x - pa.x) - (pb.x - pa.x) * (pc.z - pa.z);
431 n.z = (pb.x - pa.x) * (pc.y - pa.y) - (pb.y - pa.y) * (pc.x - pa.x);
433 d = - n.x * pa.x - n.y * pa.y - n.z * pa.z;
435 //Calculate the position on the line that intersects the plane
436 denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z);
437 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
439 mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom;
440 p->x = p1.x + mu * (p2.x - p1.x);
441 p->y = p1.y + mu * (p2.y - p1.y);
442 p->z = p1.z + mu * (p2.z - p1.z);
443 if (mu < 0 || mu > 1) // Intersection not along line segment
446 if (!PointInTriangle( p, n, &pa, &pb, &pc)) {
453 float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ n, XYZ *p)
456 static float denom, mu;
458 //Calculate the parameters for the plane
459 d = - n.x * pa.x - n.y * pa.y - n.z * pa.z;
461 //Calculate the position on the line that intersects the plane
462 denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z);
463 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
465 mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom;
466 p->x = p1.x + mu * (p2.x - p1.x);
467 p->y = p1.y + mu * (p2.y - p1.y);
468 p->z = p1.z + mu * (p2.z - p1.z);
469 if (mu < 0 || mu > 1) // Intersection not along line segment
472 if (!PointInTriangle( p, n, &pa, &pb, &pc)) {
478 float LineFacetd(XYZ *p1, XYZ *p2, XYZ *pa, XYZ *pb, XYZ *pc, XYZ *p)
481 static float denom, mu;
484 //Calculate the parameters for the plane
485 n.x = (pb->y - pa->y) * (pc->z - pa->z) - (pb->z - pa->z) * (pc->y - pa->y);
486 n.y = (pb->z - pa->z) * (pc->x - pa->x) - (pb->x - pa->x) * (pc->z - pa->z);
487 n.z = (pb->x - pa->x) * (pc->y - pa->y) - (pb->y - pa->y) * (pc->x - pa->x);
489 d = - n.x * pa->x - n.y * pa->y - n.z * pa->z;
492 //Calculate the position on the line that intersects the plane
493 denom = n.x * (p2->x - p1->x) + n.y * (p2->y - p1->y) + n.z * (p2->z - p1->z);
494 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
496 mu = - (d + n.x * p1->x + n.y * p1->y + n.z * p1->z) / denom;
497 p->x = p1->x + mu * (p2->x - p1->x);
498 p->y = p1->y + mu * (p2->y - p1->y);
499 p->z = p1->z + mu * (p2->z - p1->z);
500 if (mu < 0 || mu > 1) // Intersection not along line segment
503 if (!PointInTriangle( p, n, pa, pb, pc)) {
509 float LineFacetd(XYZ *p1, XYZ *p2, XYZ *pa, XYZ *pb, XYZ *pc, XYZ *n, XYZ *p)
512 static float denom, mu;
514 //Calculate the parameters for the plane
515 d = - n->x * pa->x - n->y * pa->y - n->z * pa->z;
517 //Calculate the position on the line that intersects the plane
518 denom = n->x * (p2->x - p1->x) + n->y * (p2->y - p1->y) + n->z * (p2->z - p1->z);
519 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
521 mu = - (d + n->x * p1->x + n->y * p1->y + n->z * p1->z) / denom;
522 p->x = p1->x + mu * (p2->x - p1->x);
523 p->y = p1->y + mu * (p2->y - p1->y);
524 p->z = p1->z + mu * (p2->z - p1->z);
525 if (mu < 0 || mu > 1) // Intersection not along line segment
528 if (!PointInTriangle( p, *n, pa, pb, pc)) {