ps2quaternion.cpp

00001 /*
00002 * "PS2" Application Framework
00003 *
00004 * University of Abertay Dundee
00005 * May be used for educational purposed only
00006 *
00007 * Author - Dr Henry S Fortuna
00008 *
00009 * $Revision: 1.2 $
00010 * $Date: 2007/08/19 12:45:11 $
00011 *
00012 */#include "ps2quaternion.h"
00013 #include "ps2maths.h"
00014 
00015 using namespace HSFMaths;
00016 
00017 Quaternion::Quaternion(void)
00018 {
00019 }
00020 
00021 Quaternion::Quaternion(const float _x, const float _y, const float _z, const float _w)
00022 :w(_w),x(_x),y(_y),z(_z)
00023 {
00024 }
00025 
00026 Quaternion::Quaternion(Vector4 V, float theta)
00027 {
00028         Vector4 Vnorm = V.Normalise();
00029 
00030         x = Vnorm.x * Sin(theta/2.0f);
00031         y = Vnorm.y * Sin(theta/2.0f);
00032         z = Vnorm.z * Sin(theta/2.0f);
00033         w = Cos(theta/2.0f);
00034 }
00035 
00036 void Quaternion::SetQuaternion(Vector4 V, const float theta)
00037 {
00038         Vector4 Vnorm = V.Normalise();
00039 
00040         x = Vnorm.x * Sin(theta/2.0f);
00041         y = Vnorm.y * Sin(theta/2.0f);
00042         z = Vnorm.z * Sin(theta/2.0f);
00043         w = Cos(theta/2.0f);
00044 }
00045 
00046 Quaternion::~Quaternion(void)
00047 {
00048 }
00049 
00050 Quaternion & Quaternion::NormaliseSelf(void)
00051 {
00052         float s = 1.0f / Sqrt(w * w + x * x + y * y + z * z);
00053         w *= s;
00054         x *= s;
00055         y *= s;
00056         z *= s;
00057 
00058         return *this;
00059 }
00060 
00061 
00062 Quaternion Quaternion::Conjugate(void)
00063 {
00064         Quaternion quat;
00065         quat.x = -x;
00066         quat.y = -y;
00067         quat.z = -z;
00068         quat.w = w;
00069         return quat;
00070 }
00071 
00072 
00073 
00074 Matrix4x4 Quaternion::ToRotationMatrix4x4(void)
00075 {
00076         return Matrix4x4(       1 - 2 * y * y - 2 * z * z,
00077                                         2 * x * y + 2 * w * z,
00078                                                 2 * x * z - 2 * w * y,
00079                                                 0,
00080                                                 2 * x * y - 2 * w * z,
00081                                                 1 - 2 * x * x - 2 * z * z,
00082                                                 2 * y * z + 2 * w * x,
00083                                                 0,
00084                                                 2 * x * z + 2 * w * y,
00085                                                 2 * y * z - 2 * w * x,
00086                                                 1 - 2 * x * x - 2 * y * y,
00087                                                 0,
00088                                                 0,0,0,1);
00089 }
00090 
00091 void Quaternion::DumpQuaternion(const char * s = NULL)
00092 {
00093         if(s != NULL)printf("\n%f %f %f %f %s\n\n", x, y, z, w, s);
00094         else printf("\n%f %f %f %f\n\n", x, y, z, w);
00095 }

Generated on Sun May 18 21:45:08 2008 for PS2X by  doxygen 1.5.4