Blender  V2.59
MT_Quaternion.h
Go to the documentation of this file.
00001 /*
00002  * $Id: MT_Quaternion.h 35158 2011-02-25 11:49:19Z jesterking $
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): none yet.
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 /*
00035 
00036  * Copyright (c) 2000 Gino van den Bergen <gino@acm.org>
00037  *
00038  * Permission to use, copy, modify, distribute and sell this software
00039  * and its documentation for any purpose is hereby granted without fee,
00040  * provided that the above copyright notice appear in all copies and
00041  * that both that copyright notice and this permission notice appear
00042  * in supporting documentation.  Gino van den Bergen makes no
00043  * representations about the suitability of this software for any
00044  * purpose.  It is provided "as is" without express or implied warranty.
00045  *
00046  */
00047 
00048 #ifndef MT_QUATERNION_H
00049 #define MT_QUATERNION_H
00050 
00051 #include <MT_assert.h>
00052 
00053 #include "MT_Vector3.h"
00054 #include "MT_Vector4.h"
00055 
00056 class MT_Quaternion : public MT_Vector4 {
00057 public:
00058     MT_Quaternion() {}
00059     MT_Quaternion(const MT_Vector4& v) : MT_Vector4(v) {}
00060     MT_Quaternion(const float v[4]) : MT_Vector4(v) {}
00061     MT_Quaternion(const double v[4]) : MT_Vector4(v) {}
00062     MT_Quaternion(MT_Scalar xx, MT_Scalar yy, MT_Scalar zz, MT_Scalar ww) :
00063         MT_Vector4(xx, yy, zz, ww) {}
00064     MT_Quaternion(const MT_Vector3& axis, MT_Scalar mt_angle) { 
00065         setRotation(axis, mt_angle); 
00066     }
00067     MT_Quaternion(MT_Scalar yaw, MT_Scalar pitch, MT_Scalar roll) { 
00068         setEuler(yaw, pitch, roll); 
00069     }
00070 
00071     void setRotation(const MT_Vector3& axis, MT_Scalar mt_angle) {
00072         MT_Scalar d = axis.length();
00073         MT_assert(!MT_fuzzyZero(d));
00074         MT_Scalar s = sin(mt_angle * MT_Scalar(0.5)) / d;
00075         setValue(axis[0] * s, axis[1] * s, axis[2] * s, 
00076                  cos(mt_angle * MT_Scalar(0.5)));
00077     }
00078 
00079     void setEuler(MT_Scalar yaw, MT_Scalar pitch, MT_Scalar roll) {
00080         MT_Scalar cosYaw = cos(yaw * MT_Scalar(0.5));
00081         MT_Scalar sinYaw = sin(yaw * MT_Scalar(0.5));
00082         MT_Scalar cosPitch = cos(pitch * MT_Scalar(0.5));
00083         MT_Scalar sinPitch = sin(pitch * MT_Scalar(0.5));
00084         MT_Scalar cosRoll = cos(roll * MT_Scalar(0.5));
00085         MT_Scalar sinRoll = sin(roll * MT_Scalar(0.5));
00086         setValue(cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw,
00087                  cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw,
00088                  sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw,
00089                  cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw);
00090     }
00091   
00092     MT_Quaternion& operator*=(const MT_Quaternion& q);
00093   
00094     void conjugate();
00095     MT_Quaternion conjugate() const;
00096 
00097     void invert();
00098     MT_Quaternion inverse() const;
00099 
00100     MT_Scalar angle(const MT_Quaternion& q) const;
00101     MT_Quaternion slerp(const MT_Quaternion& q, const MT_Scalar& t) const;
00102     
00103     static MT_Quaternion random();
00104 };
00105 
00106 MT_Quaternion operator*(const MT_Quaternion& q1, const MT_Quaternion& q2);
00107 MT_Quaternion operator*(const MT_Quaternion& q, const MT_Vector3& w);
00108 MT_Quaternion operator*(const MT_Vector3& w, const MT_Quaternion& q);
00109 
00110 #ifdef GEN_INLINED
00111 #include "MT_Quaternion.inl"
00112 #endif
00113 
00114 #endif
00115