mat2.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 "../System/cl_platform.h"
00038 #include "mat3.h"
00039 #include "mat4.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_Mat2
00058 {
00061 
00062 public:
00064         CL_Mat2() { }
00065 
00067         CL_Mat2(const CL_Mat2<Type> &copy)
00068         {
00069                 for (int i=0; i<4; i++)
00070                         matrix[i] = copy.matrix[i];
00071         }
00072 
00074         CL_Mat2(const CL_Mat3<Type> &copy);
00075 
00077         CL_Mat2(const CL_Mat4<Type> &copy);
00078 
00080         CL_Mat2(const float *init_matrix)
00081         {
00082                 for (int i=0; i<4; i++)
00083                         matrix[i] = (Type) init_matrix[i];
00084         }
00085 
00087         CL_Mat2(Type m00, Type m01, Type m10, Type m11)
00088         {
00089                 matrix[0 * 2 + 0] = m00; matrix[0 * 2 + 1] = m01;
00090                 matrix[1 * 2 + 0] = m10; matrix[1 * 2 + 1] = m11;
00091         }
00092 
00094         CL_Mat2(const double *init_matrix)
00095         {
00096                 for (int i=0; i<4; i++)
00097                         matrix[i] = (Type) init_matrix[i];
00098         }
00099 
00101         CL_Mat2(const cl_byte64 *init_matrix)
00102         {
00103                 for (int i=0; i<4; i++)
00104                         matrix[i] = (Type) init_matrix[i];
00105         }
00106 
00108         CL_Mat2(const cl_byte32 *init_matrix)
00109         {
00110                 for (int i=0; i<4; i++)
00111                         matrix[i] = (Type) init_matrix[i];
00112         }
00113 
00115         CL_Mat2(const cl_byte16 *init_matrix)
00116         {
00117                 for (int i=0; i<4; i++)
00118                         matrix[i] = (Type) init_matrix[i];
00119         }
00120 
00122         CL_Mat2(const cl_byte8 *init_matrix)
00123         {
00124                 for (int i=0; i<4; i++)
00125                         matrix[i] = (Type) init_matrix[i];
00126         }
00127 
00128         static CL_Mat2<Type> null();
00129 
00130         static CL_Mat2<Type> identity();
00131 
00139         static CL_Mat2<Type> multiply(const CL_Mat2<Type> &matrix_1, const CL_Mat2<Type> &matrix_2);
00140 
00148         static CL_Mat2<Type> add(const CL_Mat2<Type> &matrix_1, const CL_Mat2<Type> &matrix_2);
00149 
00157         static CL_Mat2<Type> subtract(const CL_Mat2<Type> &matrix_1, const CL_Mat2<Type> &matrix_2);
00158 
00162 
00163 public:
00164         Type matrix[4];
00165 
00169 
00170 public:
00178         CL_Mat2<Type> &multiply(const CL_Mat2<Type> &mult);
00179 
00187         CL_Mat2<Type> &add(const CL_Mat2<Type> &add_matrix);
00188 
00196         CL_Mat2<Type> &subtract(const CL_Mat2<Type> &subtract_matrix);
00197 
00201 
00202 public:
00204         operator Type const*() const { return matrix; }
00205 
00207         operator Type *() { return matrix; }
00208 
00210         Type &operator[](int i) { return matrix[i]; }
00211 
00213         const Type &operator[](int i) const { return matrix[i]; }
00214 
00216         Type &operator[](unsigned int i) { return matrix[i]; }
00217 
00219         const Type &operator[](unsigned int i) const { return matrix[i]; }
00220 
00222         CL_Mat2<Type> &operator =(const CL_Mat2<Type> &copy) {memcpy(matrix, copy.matrix, sizeof(matrix)); return *this; }
00223 
00225         CL_Mat2<Type> &operator =(const CL_Mat4<Type> &copy);
00226 
00228         CL_Mat2<Type> &operator =(const CL_Mat3<Type> &copy);
00229 
00231         CL_Mat2<Type> operator *(const CL_Mat2<Type> &mult) const { CL_Mat2<Type> result = mult; result.multiply(*this); return result; }
00232 
00234         CL_Mat2<Type> operator +(const CL_Mat2<Type> &add_matrix) const { CL_Mat2<Type> result = add_matrix; result.add(*this); return result; }
00235 
00237         CL_Mat2<Type> operator -(const CL_Mat2<Type> &subtract_matrix) const { CL_Mat2<Type> result = subtract_matrix; result.subtract(*this); return result; }
00238 
00240         bool operator==(const CL_Mat2<Type> &other)
00241         {
00242                 for (int i=0; i<4; i++)
00243                         if (matrix[i] != other.matrix[i]) return false;
00244                 return true;
00245         }
00246 
00248         bool operator!=(const CL_Mat2<Type> &other) { return !((*this) == other); }
00249 
00253 
00254 private:
00256 };
00257 
00258 typedef CL_Mat2<int> CL_Mat2i;
00259 typedef CL_Mat2<float> CL_Mat2f;
00260 typedef CL_Mat2<double> CL_Mat2d;
00261