1 /* 2 * Quaternions.h 3 * Copyright (C) 2007 by Bryan Duff <duff0097@gmail.com> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 18 * USA 19 */ 20 21 #ifndef _QUATERNIONS_H_ 22 #define _QUATERNIONS_H_ 23 24 #include "Maths.h" 25 #include <GL/gl.h> 26 #include "PhysicsMath.h" 27 28 /**> Quaternion Structures <**/ 29 #define PI 3.14159265355555897932384626 30 #define RADIANS 0 31 #define DEGREES 1 32 #define deg2rad .0174532925 33 34 using namespace std; 35 typedef float Matrix_t[4][4]; 36 struct euler { 37 float x, y, z; 38 }; 39 struct angle_axis { 40 float x, y, z, angle; 41 }; 42 struct quaternion { 43 float x, y, z, w; 44 }; 45 46 class XYZ { 47 public: 48 float x; 49 float y; 50 float z; 51 XYZ operator+(XYZ add); 52 XYZ operator-(XYZ add); 53 XYZ operator*(float add); 54 XYZ operator*(XYZ add); 55 XYZ operator/(float add); 56 void operator+=(XYZ add); 57 void operator-=(XYZ add); 58 void operator*=(float add); 59 void operator*=(XYZ add); 60 void operator/=(float add); 61 void operator=(float add); 62 void vec(Vector add); 63 bool operator==(XYZ add); 64 }; 65 66 /*********************> Quaternion Function definition <********/ 67 quaternion To_Quat(int Degree_Flag, euler Euler); 68 quaternion To_Quat(angle_axis Ang_Ax); 69 quaternion To_Quat(Matrix_t m); 70 angle_axis Quat_2_AA(quaternion Quat); 71 void Quat_2_Matrix(quaternion Quat, Matrix_t m); 72 quaternion Normalize(quaternion Quat); 73 quaternion Quat_Mult(quaternion q1, quaternion q2); 74 quaternion QNormalize(quaternion Quat); 75 XYZ Quat2Vector(quaternion Quat); 76 77 void CrossProduct(XYZ P, XYZ Q, XYZ * V); 78 void Normalise(XYZ * vectory); 79 float normaldotproduct(XYZ point1, XYZ point2); 80 float fast_sqrt(register float arg); 81 bool PointInTriangle(XYZ * p, XYZ normal, XYZ * p1, XYZ * p2, XYZ * p3); 82 bool LineFacet(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ * p); 83 bool LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ * p); 84 bool LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ n, XYZ * p); 85 bool LineFacetd(XYZ * p1, XYZ * p2, XYZ * pa, XYZ * pb, XYZ * pc, XYZ * n, 86 XYZ * p); 87 bool PointInTriangle(Vector * p, Vector normal, float p11, float p12, float p13, 88 float p21, float p22, float p23, float p31, float p32, 89 float p33); 90 bool LineFacet(Vector p1, Vector p2, Vector pa, Vector pb, Vector pc, 91 Vector * p); 92 void ReflectVector(XYZ * vel, XYZ * n); 93 XYZ DoRotation(XYZ thePoint, float xang, float yang, float zang); 94 XYZ DoRotationRadian(XYZ thePoint, float xang, float yang, float zang); 95 float findDistance(XYZ point1, XYZ point2); 96 float findLength(XYZ point1); 97 float findLengthfast(XYZ point1); 98 float findDistancefast(XYZ point1, XYZ point2); 99 float dotproduct(XYZ point1, XYZ point2); 100 bool sphere_line_intersection(float x1, float y1, float z1, 101 float x2, float y2, float z2, 102 float x3, float y3, float z3, float r); 103 104 #endif 105