1 /**************************************************************************** 2 3 quaternion.h - A quaternion class 4 5 GLUI User Interface Toolkit (LGPL) 6 Copyright (c) 1998 Paul Rademacher 7 8 --------------------------------------------------------------------- 9 10 WWW: http://sourceforge.net/projects/glui/ 11 Forums: http://sourceforge.net/forum/?group_id=92496 12 13 This library is free software; you can redistribute it and/or 14 modify it under the terms of the GNU Lesser General Public 15 License as published by the Free Software Foundation; either 16 version 2.1 of the License, or (at your option) any later version. 17 18 This library is distributed in the hope that it will be useful, 19 but WITHOUT ANY WARRANTY; without even the implied warranty of 20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 Lesser General Public License for more details. 22 23 You should have received a copy of the GNU Lesser General Public 24 License along with this library; if not, write to the Free Software 25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 26 27 *****************************************************************************/ 28 29 #ifndef GLUI_QUATERNION_H 30 #define GLUI_QUATERNION_H 31 32 #include "algebra3.h" 33 #include <cstdio> 34 35 /* this line defines a new type: pointer to a function which returns a */ 36 /* float and takes as argument a float */ 37 typedef float (*V_FCT_PTR)(float); 38 39 /**************************************************************** 40 * Quaternion * 41 ****************************************************************/ 42 43 class quat 44 { 45 /*protected: */ 46 public: 47 48 vec3 v; /* vector component */ 49 float s; /* scalar component */ 50 51 /*public: */ 52 53 /* Constructors */ 54 55 quat(); 56 quat(float x, float y, float z, float w); 57 quat(const vec3 &v, float s); 58 quat(float s, const vec3 &v); 59 quat(const float *d); /* copy from four-element float array */ 60 quat(const double *f); /* copy from four-element double array */ 61 quat(const quat &q); /* copy from other quat */ 62 63 /* Assignment operators */ 64 65 quat &operator = (const quat &v); /* assignment of a quat */ 66 quat &operator += (const quat &v); /* incrementation by a quat */ 67 quat &operator -= (const quat &v); /* decrementation by a quat */ 68 quat &operator *= (float d); /* multiplication by a constant */ 69 quat &operator /= (float d); /* division by a constant */ 70 71 /* special functions */ 72 73 float length() const; /* length of a quat */ 74 float length2() const; /* squared length of a quat */ 75 quat &normalize(); /* normalize a quat */ 76 quat &apply(V_FCT_PTR fct); /* apply a func. to each component */ 77 vec3 xform(const vec3 &v ); /* q*v*q-1 */ 78 mat4 to_mat4() const; 79 void set_angle(float f); /* set rot angle (degrees) */ 80 void scale_angle(float f); /* scale rot angle (degrees) */ 81 float get_angle() const; /* set rot angle (degrees) */ 82 vec3 get_axis() const; /* get axis */ 83 84 void print( FILE *file, const char *name ) const; /* print to a file */ 85 86 float &operator [] (int i); /* indexing */ 87 const float &operator [] (int i) const; /* indexing */ 88 89 void set(float x, float y, float z); /* set quat */ 90 void set(const vec3 &v, float s); /* set quat */ 91 92 /* friends */ 93 94 friend quat operator - (const quat &v); /* -q1 */ 95 friend quat operator + (const quat &a, const quat &b); /* q1 + q2 */ 96 friend quat operator - (const quat &a, const quat &b); /* q1 - q2 */ 97 friend quat operator * (const quat &a, float d); /* q1 * 3.0 */ 98 friend quat operator * (float d, const quat &a); /* 3.0 * q1 */ 99 friend quat operator * (const quat &a, const quat &b); /* q1 * q2 */ 100 friend quat operator / (const quat &a, float d); /* q1 / 3.0 */ 101 friend int operator == (const quat &a, const quat &b); /* q1 == q2 ? */ 102 friend int operator != (const quat &a, const quat &b); /* q1 != q2 ? */ 103 friend void swap(quat &a, quat &b); /* swap q1 &q2 */ 104 /*friend quat min(const quat &a, const quat &b); -- min(q1, q2) */ 105 /*friend quat max(const quat &a, const quat &b); -- max(q1, q2) */ 106 friend quat prod(const quat &a, const quat &b); /* term by term mult*/ 107 }; 108 109 /* Utility functions */ 110 111 quat quat_identity(); /* Returns quaternion identity element */ 112 quat quat_slerp(const quat &from, const quat &to, float t); 113 114 #endif 115