mat3.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 **    Mark Page
00028 **    Harry Storbacka
00029 */
00030 
00033 
00034 #pragma once
00035 
00036 #include "../api_core.h"
00037 #include "mat2.h"
00038 #include "mat4.h"
00039 #include "../System/cl_platform.h"
00040 
00041 template<typename Type>
00042 class CL_Mat2;
00043 
00044 template<typename Type>
00045 class CL_Mat3;
00046 
00047 template<typename Type>
00048 class CL_Mat4;
00049 
00050 class CL_Angle;
00051 
00056 template<typename Type>
00057 class CL_Mat3
00058 {
00061 
00062 public:
00064         CL_Mat3() { }
00065 
00067         CL_Mat3(const CL_Mat3<Type> &copy)
00068         {
00069                 for (int i=0; i<9; i++)
00070                         matrix[i] = copy.matrix[i];
00071         }
00072 
00074         CL_Mat3(const CL_Mat2<Type> &copy);
00075 
00077         CL_Mat3(const CL_Mat4<Type> &copy);
00078 
00080         CL_Mat3(const float *init_matrix)
00081         {
00082                 for (int i=0; i<9; i++)
00083                         matrix[i] = (Type) init_matrix[i];
00084         }
00085 
00087         CL_Mat3(Type m00, Type m01, Type m02, Type m10, Type m11, Type m12, Type m20, Type m21, Type m22)
00088         {
00089                 matrix[0 * 3 + 0] = m00; matrix[0 * 3 + 1] = m01; matrix[0 * 3 + 2] = m02;
00090                 matrix[1 * 3 + 0] = m10; matrix[1 * 3 + 1] = m11; matrix[1 * 3 + 2] = m12;
00091                 matrix[2 * 3 + 0] = m20; matrix[2 * 3 + 1] = m21; matrix[2 * 3 + 2] = m22;
00092         }
00093 
00095         CL_Mat3(const double *init_matrix)
00096         {
00097                 for (int i=0; i<9; i++)
00098                         matrix[i] = (Type) init_matrix[i];
00099         }
00100 
00102         CL_Mat3(const cl_byte64 *init_matrix)
00103         {
00104                 for (int i=0; i<9; i++)
00105                         matrix[i] = (Type) init_matrix[i];
00106         }
00107 
00109         CL_Mat3(const cl_byte32 *init_matrix)
00110         {
00111                 for (int i=0; i<9; i++)
00112                         matrix[i] = (Type) init_matrix[i];
00113         }
00114 
00116         CL_Mat3(const cl_byte16 *init_matrix)
00117         {
00118                 for (int i=0; i<9; i++)
00119                         matrix[i] = (Type) init_matrix[i];
00120         }
00121 
00123         CL_Mat3(const cl_byte8 *init_matrix)
00124         {
00125                 for (int i=0; i<9; i++)
00126                         matrix[i] = (Type) init_matrix[i];
00127         }
00128 
00129         static CL_Mat3<Type> null();
00130 
00131         static CL_Mat3<Type> identity();
00132 
00142         static CL_Mat3<Type> rotate(const CL_Angle &angle, Type x, Type y, Type z, bool normalize = true);
00143 
00151         static CL_Mat3<Type> multiply(const CL_Mat3<Type> &matrix_1, const CL_Mat3<Type> &matrix_2);
00152 
00160         static CL_Mat3<Type> add(const CL_Mat3<Type> &matrix_1, const CL_Mat3<Type> &matrix_2);
00161 
00169         static CL_Mat3<Type> subtract(const CL_Mat3<Type> &matrix_1, const CL_Mat3<Type> &matrix_2);
00170 
00175         static CL_Mat3<Type> adjoint(const CL_Mat3<Type> &matrix);
00176 
00182         static CL_Mat3<Type> inverse(const CL_Mat3<Type> &matrix);
00183 
00188         static CL_Mat3<Type> transpose(const CL_Mat3<Type> &matrix);
00189 
00193 
00194 public:
00195         Type matrix[9];
00196 
00200 
00201 public:
00209         CL_Mat3<Type> &multiply(const CL_Mat3<Type> &mult);
00210 
00218         CL_Mat3<Type> &add(const CL_Mat3<Type> &add_matrix);
00219 
00227         CL_Mat3<Type> &subtract(const CL_Mat3<Type> &sub_matrix);
00228 
00230         double det() const;
00231 
00235         CL_Mat3<Type> &adjoint();
00236 
00240         CL_Mat3<Type> &inverse();
00241 
00245         CL_Mat3<Type> &transpose();
00246 
00250 
00251 public:
00253         operator Type const*() const { return matrix; }
00254 
00256         operator Type *() { return matrix; }
00257 
00259         Type &operator[](int i) { return matrix[i]; }
00260 
00262         const Type &operator[](int i) const { return matrix[i]; }
00263 
00265         Type &operator[](unsigned int i) { return matrix[i]; }
00266 
00268         const Type &operator[](unsigned int i) const { return matrix[i]; }
00269 
00271         CL_Mat3<Type> &operator =(const CL_Mat3<Type> &copy) {memcpy(matrix, copy.matrix, sizeof(matrix)); return *this; }
00272 
00274         CL_Mat3<Type> &operator =(const CL_Mat4<Type> &copy);
00275 
00277         CL_Mat3<Type> &operator =(const CL_Mat2<Type> &copy);
00278 
00280         CL_Mat3<Type> operator *(const CL_Mat4<Type> &mult) const { CL_Mat3<Type> result = mult; result.multiply(*this); return result; }
00281 
00283         CL_Mat3<Type> operator +(const CL_Mat4<Type> &add_matrix) const { CL_Mat3<Type> result = add_matrix; result.add(*this); return result; }
00284 
00286         CL_Mat3<Type> operator -(const CL_Mat4<Type> &sub_matrix) const { CL_Mat3<Type> result = sub_matrix; result.subtract(*this); return result; }
00287 
00289         bool operator==(const CL_Mat3<Type> &other)
00290         {
00291                 for (int i=0; i<9; i++)
00292                         if (matrix[i] != other.matrix[i]) return false;
00293                 return true;
00294         }
00295 
00297         bool operator!=(const CL_Mat3<Type> &other) { return !((*this) == other); }
00298 
00302 
00303 private:
00305 };
00306 
00307 typedef CL_Mat3<int> CL_Mat3i;
00308 typedef CL_Mat3<float> CL_Mat3f;
00309 typedef CL_Mat3<double> CL_Mat3d;
00310