istereo2
diff libs/vmath/quat.h @ 2:81d35769f546
added the tunnel effect source
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 19 Sep 2015 05:51:51 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/vmath/quat.h Sat Sep 19 05:51:51 2015 +0300 1.3 @@ -0,0 +1,119 @@ 1.4 +/* 1.5 +libvmath - a vector math library 1.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org> 1.7 + 1.8 +This program is free software: you can redistribute it and/or modify 1.9 +it under the terms of the GNU Lesser General Public License as published 1.10 +by the Free Software Foundation, either version 3 of the License, or 1.11 +(at your option) any later version. 1.12 + 1.13 +This program is distributed in the hope that it will be useful, 1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.16 +GNU Lesser General Public License for more details. 1.17 + 1.18 +You should have received a copy of the GNU Lesser General Public License 1.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 1.20 +*/ 1.21 + 1.22 +#ifndef VMATH_QUATERNION_H_ 1.23 +#define VMATH_QUATERNION_H_ 1.24 + 1.25 +#include <stdio.h> 1.26 +#include "vmath_types.h" 1.27 +#include "vector.h" 1.28 + 1.29 +#ifdef __cplusplus 1.30 +extern "C" { 1.31 +#endif /* __cplusplus */ 1.32 + 1.33 +#define quat_cons(s, x, y, z) v4_cons(x, y, z, s) 1.34 +#define quat_vec(q) v3_cons((q).x, (q).y, (q).z) 1.35 +#define quat_s(q) ((q).w) 1.36 +#define quat_identity() quat_cons(1.0, 0.0, 0.0, 0.0) 1.37 +void quat_print(FILE *fp, quat_t q); 1.38 + 1.39 +#define quat_add v4_add 1.40 +#define quat_sub v4_sub 1.41 +#define quat_neg v4_neg 1.42 + 1.43 +static inline quat_t quat_mul(quat_t q1, quat_t q2); 1.44 + 1.45 +static inline quat_t quat_conjugate(quat_t q); 1.46 + 1.47 +#define quat_length v4_length 1.48 +#define quat_length_sq v4_length_sq 1.49 + 1.50 +#define quat_normalize v4_normalize 1.51 +static inline quat_t quat_inverse(quat_t q); 1.52 + 1.53 +quat_t quat_rotate(quat_t q, scalar_t angle, scalar_t x, scalar_t y, scalar_t z); 1.54 +quat_t quat_rotate_quat(quat_t q, quat_t rotq); 1.55 + 1.56 +static inline void quat_to_mat3(mat3_t res, quat_t q); 1.57 +static inline void quat_to_mat4(mat4_t res, quat_t q); 1.58 + 1.59 +#define quat_lerp quat_slerp 1.60 +quat_t quat_slerp(quat_t q1, quat_t q2, scalar_t t); 1.61 + 1.62 + 1.63 +#ifdef __cplusplus 1.64 +} /* extern "C" */ 1.65 + 1.66 +#include <iostream> 1.67 + 1.68 +/* Quaternion */ 1.69 +class Quaternion { 1.70 +public: 1.71 + scalar_t s; 1.72 + Vector3 v; 1.73 + 1.74 + Quaternion(); 1.75 + Quaternion(scalar_t s, const Vector3 &v); 1.76 + Quaternion(scalar_t s, scalar_t x, scalar_t y, scalar_t z); 1.77 + Quaternion(const Vector3 &axis, scalar_t angle); 1.78 + Quaternion(const quat_t &quat); 1.79 + 1.80 + Quaternion operator +(const Quaternion &quat) const; 1.81 + Quaternion operator -(const Quaternion &quat) const; 1.82 + Quaternion operator -() const; 1.83 + Quaternion operator *(const Quaternion &quat) const; 1.84 + 1.85 + void operator +=(const Quaternion &quat); 1.86 + void operator -=(const Quaternion &quat); 1.87 + void operator *=(const Quaternion &quat); 1.88 + 1.89 + void reset_identity(); 1.90 + 1.91 + Quaternion conjugate() const; 1.92 + 1.93 + scalar_t length() const; 1.94 + scalar_t length_sq() const; 1.95 + 1.96 + void normalize(); 1.97 + Quaternion normalized() const; 1.98 + 1.99 + Quaternion inverse() const; 1.100 + 1.101 + void set_rotation(const Vector3 &axis, scalar_t angle); 1.102 + void rotate(const Vector3 &axis, scalar_t angle); 1.103 + /* note: this is a totally different operation from the above 1.104 + * this treats the quaternion as signifying direction and rotates 1.105 + * it by a rotation quaternion by rot * q * rot' 1.106 + */ 1.107 + void rotate(const Quaternion &q); 1.108 + 1.109 + Matrix3x3 get_rotation_matrix() const; 1.110 + 1.111 + friend Quaternion slerp(const Quaternion &q1, const Quaternion &q2, scalar_t t); 1.112 + 1.113 + friend std::ostream &operator <<(std::ostream &out, const Quaternion &q); 1.114 +}; 1.115 + 1.116 +Quaternion slerp(const Quaternion &q1, const Quaternion &q2, scalar_t t); 1.117 +inline Quaternion lerp(const Quaternion &q1, const Quaternion &q2, scalar_t t); 1.118 +#endif /* __cplusplus */ 1.119 + 1.120 +#include "quat.inl" 1.121 + 1.122 +#endif /* VMATH_QUATERNION_H_ */