Quat4.hpp

Go to the documentation of this file.
00001 /*
00002    Copyright (C) 1997,1998,1999
00003    Kenji Hiranabe, Eiwa System Management, Inc.
00004 
00005    This program is free software.
00006    Implemented by Kenji Hiranabe(hiranabe@esm.co.jp),
00007    conforming to the Java(TM) 3D API specification by Sun Microsystems.
00008 
00009    Permission to use, copy, modify, distribute and sell this software
00010    and its documentation for any purpose is hereby granted without fee,
00011    provided that the above copyright notice appear in all copies and
00012    that both that copyright notice and this permission notice appear
00013    in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc.
00014    makes no representations about the suitability of this software for any
00015    purpose.  It is provided "AS IS" with NO WARRANTY.
00016 
00017    Copyright (C) 2006
00018    Rune Myrland, Skalden Studio AS
00019    SagaEngine adaptions, involving conversion from floating point values to fixed point values.
00020    Kenji Hiranabe's license apply.
00021 */
00022 #ifndef util_vecmath_Quat4_hpp
00023 #define util_vecmath_Quat4_hpp
00024 
00025 #include "Tuple4.hpp"
00026 #include "../math/CoorT.hpp"
00027 #include "../math/QuatT.hpp"
00028 
00029 namespace se_core {
00036     class _SeCoreExport Quat4 : public Tuple4 {
00037     public:
00045         Quat4(coor_t x, coor_t y, coor_t z, coor_t w): Tuple4(x, y, z, w) { }
00046 
00051         Quat4(const coor_t v[]) : Tuple4(v) { }
00052 
00053 
00058         Quat4(const Tuple4& t1): Tuple4(t1) { }
00059 
00060 
00061         Quat4(const Euler3& a1) { 
00062             set(a1);
00063         }
00064 
00065         Quat4(const AxisAngle4& a1) { 
00066             set(a1);
00067         }
00068 
00072         Quat4(): Tuple4() { }
00073 
00079         void set(const Tuple4& t1) {
00080             Tuple4::set(t1);
00081         }
00082 
00083 
00084         void setIdentity() {
00085             set(0, 0, 0, 1);
00086         }
00087 
00096         void set(coor_t x, coor_t y, coor_t z, coor_t w) {
00097             Tuple4::set(x, y, z, w);
00098         }
00099 
00100 
00105         void conjugate(const Quat4& q1) {
00106             x_ = -q1.x_;
00107             y_ = -q1.y_;
00108             z_ = -q1.z_;
00109             w_ = q1.w_;
00110         }
00111 
00116         void conjugate() {
00117             x_ = -x_;
00118             y_ = -y_;
00119             z_ = -z_;
00120         }
00121 
00129         void mul(const Quat4& q1, const Quat4& q2);
00130 
00136         void mul(const Quat4& q1);
00137 
00138 
00143         inline void rotate(const Quat4& q1) {
00144             mul(q1);
00145         }
00146 
00147         inline void rotateInverse(const Quat4& q1) {
00148             mulInverse(q1);
00149         }
00150 
00151 
00159         void mulInverse(const Quat4& q1, const Quat4& q2);
00160 
00167         void mulInverse(const Quat4& q1);
00168 
00169     protected:
00170         coor_double_t norm() const {
00171             return QuatT::pow2(x_) + QuatT::pow2(y_) + QuatT::pow2(z_) + QuatT::pow2(w_);
00172         }
00173 
00174     public:
00180         void inverse(const Quat4& q1) {
00181             scale_t n = QuatT::oneOver( q1.norm() );
00182             // zero-div may occur.
00183             x_ = -QuatT::scale(n, q1.x_);
00184             y_ = -QuatT::scale(n, q1.y_);
00185             z_ = -QuatT::scale(n, q1.z_);
00186             w_ = QuatT::scale(n, q1.w_);
00187         }
00188 
00193         void inverse() {
00194             scale_t n = QuatT::oneOver( norm() );
00195             // zero-div may occur.
00196 
00197             x_ = -QuatT::scale(n, x_);
00198             y_ = -QuatT::scale(n, y_);
00199             z_ = -QuatT::scale(n, z_);
00200             w_ = QuatT::scale(n, w_);
00201         }
00202 
00208         void normalize(const Quat4& q1) {
00209             scale_t n = QuatT::oneOver( CoorT::sqrt( q1.norm() ) );
00210             // zero-div may occur.
00211             x_ = QuatT::scale(n, q1.x_);
00212             y_ = QuatT::scale(n, q1.y_);
00213             z_ = QuatT::scale(n, q1.z_);
00214             w_ = QuatT::scale(n, q1.w_);
00215         }
00216 
00220         void normalize() {
00221             scale_t n = QuatT::oneOver( CoorT::sqrt( norm() ) );
00222             // zero-div may occur.
00223 
00224             x_ = QuatT::scale(n, x_);
00225             y_ = QuatT::scale(n, y_);
00226             z_ = QuatT::scale(n, z_);
00227             w_ = QuatT::scale(n, w_);
00228         }
00229 
00230 
00231         bool isNormalized() const {
00232             #ifdef SE_FIXED_POINT
00233             LogFatal("Not yet implemented");
00234             return true;
00235             #else
00236             coor_double_t n = norm();
00237             const coor_double_t EPSILON = 0.00001f;
00238             return (n + EPSILON > 1 && n - EPSILON < 1);
00239             #endif
00240         }
00241 
00242         bool isIdentity() const {
00243             return x_ == 0 && y_ == 0 && z_ == 0 && w_ == 1;
00244         }
00245 
00251         void set(const AxisAngle4& a1);
00252 
00253         void set(const Euler3& a1);
00254 
00255         void setYaw(const bray_t yaw);
00256         void setPitch(const bray_t pitch);
00257         void setRoll(const bray_t roll);
00258         void setYawAndPitch(const bray_t yaw, const bray_t pitch);
00259         void setEuler(const bray_t yaw, const bray_t pitch, const bray_t roll);
00260         
00268         void slerp(const Quat4& q1, scale_t alpha, bool findShortestPath = false);
00269 
00277         void slerp(const Quat4& q1, const Quat4& q2, scale_t alpha, bool findShortestPath = false);
00278 
00282         void interpolate(const Quat4& q1, scale_t alpha) {
00283             slerp(q1, alpha, true);
00284         }
00285 
00286 
00287         void interpolate(const Quat4& q1, const Quat4& q2, scale_t alpha) {
00288             slerp(q1, q2, alpha, true);
00289         }
00290 
00291 
00292         void scale(scale_t alpha) {
00293             slerp(Quat4::IDENTITY, 1 - alpha, true);
00294         }
00295         // copy constructor and operator = is made by complier
00296 
00297         //Quat4& operator*=(const Quat4& m1);
00298         //Quat4 operator*(const Quat4& m1) const;
00299 
00300         static const Quat4 IDENTITY;
00301     };
00302 
00303 
00304 }
00305 
00306 
00307 
00308 #endif /* QUAT4__H */

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:13 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo