2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program 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.
15 See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "Quaternions.h"
25 quaternion Quat_Mult(quaternion q1, quaternion q2)
28 float a, b, c, d, e, f, g, h;
29 a = (q1.w + q1.x) * (q2.w + q2.x);
30 b = (q1.z - q1.y) * (q2.y - q2.z);
31 c = (q1.w - q1.x) * (q2.y + q2.z);
32 d = (q1.y + q1.z) * (q2.w - q2.x);
33 e = (q1.x + q1.z) * (q2.x + q2.y);
34 f = (q1.x - q1.z) * (q2.x - q2.y);
35 g = (q1.w + q1.y) * (q2.w - q2.z);
36 h = (q1.w - q1.y) * (q2.w + q2.z);
37 QResult.w = b + (-e - f + g + h) / 2;
38 QResult.x = a - (e + f + g + h) / 2;
39 QResult.y = c + (e - f + g - h) / 2;
40 QResult.z = d + (e - f - g + h) / 2;
46 quaternion To_Quat(Matrix_t m)
48 // From Jason Shankel, (C) 2000.
49 static quaternion Quat;
51 static double Tr = m[0][0] + m[1][1] + m[2][2] + 1.0, fourD;
57 fourD = 2.0*fast_sqrt(Tr);
59 q[0] = (m[2][1] - m[1][2]) / fourD;
60 q[1] = (m[0][2] - m[2][0]) / fourD;
61 q[2] = (m[1][0] - m[0][1]) / fourD;
65 if (m[0][0] > m[1][1])
73 if (m[2][2] > m[i][i])
79 fourD = 2.0*fast_sqrt(m[i][i] - m[j][j] - m[k][k] + 1.0);
81 q[j] = (m[j][i] + m[i][j]) / fourD;
82 q[k] = (m[k][i] + m[i][k]) / fourD;
83 q[3] = (m[j][k] - m[k][j]) / fourD;
92 void Quat_2_Matrix(quaternion Quat, Matrix_t m)
94 // From the GLVelocity site (http://glvelocity.gamedev.net)
102 m[0][0] = 1.0f - 2.0f * (fYY + fZZ);
103 m[1][0] = 2.0f * (fX * fY + fW * fZ);
104 m[2][0] = 2.0f * (fX * fZ - fW * fY);
106 m[0][1] = 2.0f * (fX * fY - fW * fZ);
107 m[1][1] = 1.0f - 2.0f * (fXX + fZZ);
108 m[2][1] = 2.0f * (fY * fZ + fW * fX);
110 m[0][2] = 2.0f * (fX * fZ + fW * fY);
111 m[1][2] = 2.0f * (fX * fZ - fW * fX);
112 m[2][2] = 1.0f - 2.0f * (fXX + fYY);
119 quaternion To_Quat(angle_axis Ang_Ax)
121 // From the Quaternion Powers article on gamedev.net
122 static quaternion Quat;
124 Quat.x = Ang_Ax.x * sin(Ang_Ax.angle / 2);
125 Quat.y = Ang_Ax.y * sin(Ang_Ax.angle / 2);
126 Quat.z = Ang_Ax.z * sin(Ang_Ax.angle / 2);
127 Quat.w = cos(Ang_Ax.angle / 2);
130 angle_axis Quat_2_AA(quaternion Quat)
132 static angle_axis Ang_Ax;
133 static float scale, tw;
134 tw = (float)acosf(Quat.w) * 2;
135 scale = (float)sin(tw / 2.0);
136 Ang_Ax.x = Quat.x / scale;
137 Ang_Ax.y = Quat.y / scale;
138 Ang_Ax.z = Quat.z / scale;
140 Ang_Ax.angle = 2.0 * acosf(Quat.w)/(float)PI*180;
144 quaternion To_Quat(int In_Degrees, euler Euler)
146 // From the gamasutra quaternion article
147 static quaternion Quat;
148 static float cr, cp, cy, sr, sp, sy, cpcy, spsy;
149 //If we are in Degree mode, convert to Radians
151 Euler.x = Euler.x * (float)PI / 180;
152 Euler.y = Euler.y * (float)PI / 180;
153 Euler.z = Euler.z * (float)PI / 180;
155 //Calculate trig identities
156 //Formerly roll, pitch, yaw
157 cr = float(cos(Euler.x/2));
158 cp = float(cos(Euler.y/2));
159 cy = float(cos(Euler.z/2));
160 sr = float(sin(Euler.x/2));
161 sp = float(sin(Euler.y/2));
162 sy = float(sin(Euler.z/2));
166 Quat.w = cr * cpcy + sr * spsy;
167 Quat.x = sr * cpcy - cr * spsy;
168 Quat.y = cr * sp * cy + sr * cp * sy;
169 Quat.z = cr * cp * sy - sr * sp * cy;
174 quaternion QNormalize(quaternion Quat)
177 norm = Quat.x * Quat.x +
181 Quat.x = float(Quat.x / norm);
182 Quat.y = float(Quat.y / norm);
183 Quat.z = float(Quat.z / norm);
184 Quat.w = float(Quat.w / norm);
188 XYZ Quat2Vector(quaternion Quat)
199 tempvec.x = 2.0f*(fX*fZ-fW*fY);
200 tempvec.y = 2.0f*(fY*fZ+fW*fX);
201 tempvec.z = 1.0f-2.0f*(fX*fX+fY*fY);
206 bool PointInTriangle(Vector *p, Vector normal, float p11, float p12, float p13, float p21, float p22, float p23, float p31, float p32, float p33)
208 static float u0, u1, u2;
209 static float v0, v1, v2;
214 static float pointv[3];
218 static float normalv[3];
243 #define ABS(X) (((X)<0.f)?-(X):(X) )
244 #define MAX(A, B) (((A)<(B))?(B):(A))
245 max = MAX(MAX(ABS(normalv[0]), ABS(normalv[1])), ABS(normalv[2]));
247 if (max == ABS(normalv[0])) {i = 1; j = 2;} // y, z
248 if (max == ABS(normalv[1])) {i = 0; j = 2;} // x, z
249 if (max == ABS(normalv[2])) {i = 0; j = 1;} // x, y
252 u0 = pointv[i] - p1v[i];
253 v0 = pointv[j] - p1v[j];
254 u1 = p2v[i] - p1v[i];
255 v1 = p2v[j] - p1v[j];
256 u2 = p3v[i] - p1v[i];
257 v2 = p3v[j] - p1v[j];
259 if (u1 > -1.0e-05f && u1 < 1.0e-05f)// == 0.0f)
262 if (0.0f <= b && b <= 1.0f)
264 a = (v0 - b * v2) / v1;
265 if ((a >= 0.0f) && (( a + b ) <= 1.0f))
271 b = (v0 * u1 - u0 * v1) / (v2 * u1 - u2 * v1);
272 if (0.0f <= b && b <= 1.0f)
274 a = (u0 - b * u2) / u1;
275 if ((a >= 0.0f) && (( a + b ) <= 1.0f ))
283 bool LineFacet(Vector p1,Vector p2,Vector pa,Vector pb,Vector pc,Vector *p)
286 static float a1,a2,a3;
287 static float total,denom,mu;
288 static Vector n,pa1,pa2,pa3;
290 //Calculate the parameters for the plane
291 n.x = (pb.y - pa.y)*(pc.z - pa.z) - (pb.z - pa.z)*(pc.y - pa.y);
292 n.y = (pb.z - pa.z)*(pc.x - pa.x) - (pb.x - pa.x)*(pc.z - pa.z);
293 n.z = (pb.x - pa.x)*(pc.y - pa.y) - (pb.y - pa.y)*(pc.x - pa.x);
295 d = - n.x * pa.x - n.y * pa.y - n.z * pa.z;
297 //Calculate the position on the line that intersects the plane
298 denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z);
299 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
301 mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom;
302 p->x = p1.x + mu * (p2.x - p1.x);
303 p->y = p1.y + mu * (p2.y - p1.y);
304 p->z = p1.z + mu * (p2.z - p1.z);
305 if (mu < 0 || mu > 1) // Intersection not along line segment
308 if(!PointInTriangle( p, n, pa.x, pa.y, pa.z, pb.x, pb.y, pb.z, pc.x, pc.y, pc.z)){return 0;}
313 bool PointInTriangle(XYZ *p, XYZ normal, XYZ *p1, XYZ *p2, XYZ *p3)
315 static float u0, u1, u2;
316 static float v0, v1, v2;
320 static bool bInter = 0;
321 static float pointv[3];
325 static float normalv[3];
350 #define ABS(X) (((X)<0.f)?-(X):(X) )
351 #define MAX(A, B) (((A)<(B))?(B):(A))
352 max = MAX(MAX(ABS(normalv[0]), ABS(normalv[1])), ABS(normalv[2]));
354 if (max == ABS(normalv[0])) {i = 1; j = 2;} // y, z
355 if (max == ABS(normalv[1])) {i = 0; j = 2;} // x, z
356 if (max == ABS(normalv[2])) {i = 0; j = 1;} // x, y
359 u0 = pointv[i] - p1v[i];
360 v0 = pointv[j] - p1v[j];
361 u1 = p2v[i] - p1v[i];
362 v1 = p2v[j] - p1v[j];
363 u2 = p3v[i] - p1v[i];
364 v2 = p3v[j] - p1v[j];
366 if (u1 > -1.0e-05f && u1 < 1.0e-05f)// == 0.0f)
369 if (0.0f <= b && b <= 1.0f)
371 a = (v0 - b * v2) / v1;
372 if ((a >= 0.0f) && (( a + b ) <= 1.0f))
378 b = (v0 * u1 - u0 * v1) / (v2 * u1 - u2 * v1);
379 if (0.0f <= b && b <= 1.0f)
381 a = (u0 - b * u2) / u1;
382 if ((a >= 0.0f) && (( a + b ) <= 1.0f ))
390 bool LineFacet(XYZ p1,XYZ p2,XYZ pa,XYZ pb,XYZ pc,XYZ *p)
393 static float a1,a2,a3;
394 static float total,denom,mu;
395 static XYZ n,pa1,pa2,pa3;
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)){return 0;}
420 float LineFacetd(XYZ p1,XYZ p2,XYZ pa,XYZ pb,XYZ pc,XYZ *p)
423 static float a1,a2,a3;
424 static float total,denom,mu;
425 static XYZ n,pa1,pa2,pa3;
427 //Calculate the parameters for the plane
428 n.x = (pb.y - pa.y)*(pc.z - pa.z) - (pb.z - pa.z)*(pc.y - pa.y);
429 n.y = (pb.z - pa.z)*(pc.x - pa.x) - (pb.x - pa.x)*(pc.z - pa.z);
430 n.z = (pb.x - pa.x)*(pc.y - pa.y) - (pb.y - pa.y)*(pc.x - pa.x);
432 d = - n.x * pa.x - n.y * pa.y - n.z * pa.z;
434 //Calculate the position on the line that intersects the plane
435 denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z);
436 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
438 mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom;
439 p->x = p1.x + mu * (p2.x - p1.x);
440 p->y = p1.y + mu * (p2.y - p1.y);
441 p->z = p1.z + mu * (p2.z - p1.z);
442 if (mu < 0 || mu > 1) // Intersection not along line segment
445 if(!PointInTriangle( p, n, &pa, &pb, &pc)){return 0;}
450 float LineFacetd(XYZ p1,XYZ p2,XYZ pa,XYZ pb,XYZ pc, XYZ n, XYZ *p)
453 static float a1,a2,a3;
454 static float total,denom,mu;
455 static XYZ pa1,pa2,pa3;
457 //Calculate the parameters for the plane
458 d = - n.x * pa.x - n.y * pa.y - n.z * pa.z;
460 //Calculate the position on the line that intersects the plane
461 denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z);
462 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
464 mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom;
465 p->x = p1.x + mu * (p2.x - p1.x);
466 p->y = p1.y + mu * (p2.y - p1.y);
467 p->z = p1.z + mu * (p2.z - p1.z);
468 if (mu < 0 || mu > 1) // Intersection not along line segment
471 if(!PointInTriangle( p, n, &pa, &pb, &pc)){return 0;}
475 float LineFacetd(XYZ *p1,XYZ *p2,XYZ *pa,XYZ *pb,XYZ *pc, XYZ *p)
478 static float a1,a2,a3;
479 static float total,denom,mu;
480 static XYZ pa1,pa2,pa3,n;
482 //Calculate the parameters for the plane
483 n.x = (pb->y - pa->y)*(pc->z - pa->z) - (pb->z - pa->z)*(pc->y - pa->y);
484 n.y = (pb->z - pa->z)*(pc->x - pa->x) - (pb->x - pa->x)*(pc->z - pa->z);
485 n.z = (pb->x - pa->x)*(pc->y - pa->y) - (pb->y - pa->y)*(pc->x - pa->x);
487 d = - n.x * pa->x - n.y * pa->y - n.z * pa->z;
490 //Calculate the position on the line that intersects the plane
491 denom = n.x * (p2->x - p1->x) + n.y * (p2->y - p1->y) + n.z * (p2->z - p1->z);
492 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
494 mu = - (d + n.x * p1->x + n.y * p1->y + n.z * p1->z) / denom;
495 p->x = p1->x + mu * (p2->x - p1->x);
496 p->y = p1->y + mu * (p2->y - p1->y);
497 p->z = p1->z + mu * (p2->z - p1->z);
498 if (mu < 0 || mu > 1) // Intersection not along line segment
501 if(!PointInTriangle( p, n, pa, pb, pc)){return 0;}
505 float LineFacetd(XYZ *p1,XYZ *p2,XYZ *pa,XYZ *pb,XYZ *pc, XYZ *n, XYZ *p)
508 static float a1,a2,a3;
509 static float total,denom,mu;
510 static XYZ pa1,pa2,pa3;
512 //Calculate the parameters for the plane
513 d = - n->x * pa->x - n->y * pa->y - n->z * pa->z;
515 //Calculate the position on the line that intersects the plane
516 denom = n->x * (p2->x - p1->x) + n->y * (p2->y - p1->y) + n->z * (p2->z - p1->z);
517 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
519 mu = - (d + n->x * p1->x + n->y * p1->y + n->z * p1->z) / denom;
520 p->x = p1->x + mu * (p2->x - p1->x);
521 p->y = p1->y + mu * (p2->y - p1->y);
522 p->z = p1->z + mu * (p2->z - p1->z);
523 if (mu < 0 || mu > 1) // Intersection not along line segment
526 if(!PointInTriangle( p, *n, pa, pb, pc)){return 0;}