Mercurial > Math
changeset 9:81d2aa42a860 refactored-1
Matrix22 working, but a few methods not yet written. I started thinking about refactoring again t omake things even cleaner in design, with less repetition.
author | Eris Caffee <discordia@eldalin.com> |
---|---|
date | Fri, 07 Oct 2011 11:08:38 -0500 |
parents | 5377337c7de0 |
children | 139d189290e0 |
files | CMakeLists.txt include/Math.h include/Matrix.h include/Vector.h src/main.cpp |
diffstat | 5 files changed, 612 insertions(+), 599 deletions(-) [+] |
line diff
1.1 --- a/CMakeLists.txt Wed Sep 28 12:53:57 2011 -0500 1.2 +++ b/CMakeLists.txt Fri Oct 07 11:08:38 2011 -0500 1.3 @@ -148,6 +148,7 @@ 1.4 set (SRCS_${App_Name} 1.5 include/Math.h 1.6 include/Vector.h 1.7 + include/Matrix.h 1.8 ) 1.9 1.10 include_directories (
2.1 --- a/include/Math.h Wed Sep 28 12:53:57 2011 -0500 2.2 +++ b/include/Math.h Fri Oct 07 11:08:38 2011 -0500 2.3 @@ -2,7 +2,7 @@ 2.4 #define MATH_H_ 2.5 2.6 #include "Vector.h" 2.7 -// #include "Matrix.h" 2.8 +#include "Matrix.h" 2.9 2.10 namespace arda 2.11 { 2.12 @@ -29,6 +29,35 @@ 2.13 const double PI = 3.14159265358979323846264338327950288; 2.14 } 2.15 2.16 + //////////////////////////////////////////////////////////////////////////////// 2.17 + // Mixed Vector / Matrix operations. 2.18 + // * *= 2.19 + // Defined for Vector * Matrix and Matrix * Vector, but these 2.20 + // operators are not part of the class. They are defined independently. 2.21 + 2.22 + // Vector * Matrix 2.23 + template <typename T> inline Vector2<T>& operator*=(Vector2<T>& v, const Matrix22<T>& m) 2.24 + { 2.25 + Vector2<T> vres(0); 2.26 + vres[0] = v[0]*m[0] + v[1]*m[1]; 2.27 + vres[1] = v[0]*m[2] + v[1]*m[3]; 2.28 + v = vres; 2.29 + return v; 2.30 + } 2.31 + template <typename T> inline Vector2<T> operator*(const Vector2<T>& v, const Matrix22<T>& m) 2.32 + { return Vector2<T>(v) *= m; } 2.33 + 2.34 + // Matrix * Vector 2.35 + // Note: can't define Matrix *= Vector since the result is a Vector. 2.36 + template <typename T> inline Vector2<T> operator*(const Matrix22<T>& m, const Vector2<T>& v) 2.37 + { 2.38 + Vector2<T> vres(0); 2.39 + vres[0] = m[0]*v[0] + m[2]*v[1]; 2.40 + vres[1] = m[1]*v[0] + m[3]*v[1]; 2.41 + return vres; 2.42 + } 2.43 + 2.44 + 2.45 } // namespace arda 2.46 2.47 #endif
3.1 --- a/include/Matrix.h Wed Sep 28 12:53:57 2011 -0500 3.2 +++ b/include/Matrix.h Fri Oct 07 11:08:38 2011 -0500 3.3 @@ -2,520 +2,339 @@ 3.4 #define MATRIX_H_ 3.5 3.6 #include "Vector.h" 3.7 -using namespace arda::Vector; 3.8 3.9 #include <cmath> 3.10 +#include <cstring> 3.11 #include <string> 3.12 +#include <sstream> 3.13 3.14 namespace arda 3.15 { 3.16 - namespace Matrix 3.17 + ////////////////////////////////////////////////////////////////////////// 3.18 + // Maxtrices 3.19 + // 3.20 + // Supported operations on matrices 3.21 + // 3.22 + // [] 3.23 + // If you have a Matrix named m you can access it's member elements as 3.24 + // m[0], m[1], m[2], etc. The indexing is COLUMN MAJOR. So for a 2x2 3.25 + // matrix, m[0] and m[1] are the elements of the first column, not the 3.26 + // first row. 3.27 + // == != + += - -= 3.28 + // Defined for operations on two matrices of the same type. 3.29 + // * *= / /= 3.30 + // Defined for scalar multiplication/division with int, float, and double. 3.31 + // * is defined as a standalone template operator for the scalar * Matrix form. 3.32 + // * *= 3.33 + // Also defined for Matrix * Matrix 3.34 + // 3.35 + // assign() Assign the value of one matrix to another. 3.36 + // det() Calculates the determinant of a matrix. 3.37 + // getstring() Returns a printable string representation of the matrix. 3.38 + // transpose() Returns the transpose of a matrix. 3.39 + // setidentity() Sets a matrix to the identity matrix. 3.40 + 3.41 + // inverse() Returns the inverse of a non-singular matrix. 3.42 + // getcol() Returns the vector that is column n of the matrix. 3.43 + // setcol() Set the vector that is column n of the matrix. 3.44 + 3.45 + ////////////////////////////////////////////////////////////////////////// 3.46 + template <typename T> class Matrix22 3.47 { 3.48 - ////////////////////////////////////////////////////////////////////////// 3.49 - // Maxtrices 3.50 - // 3.51 - // Supported operations on matrices 3.52 - // 3.53 - // getstring*() Returns a printable string representation of the matrix. 3.54 - // assign*() Assign the value of one matrix to another. 3.55 - // add*() Add two matrices 3.56 - // subtract*() A convenience function to avoid writing 3.57 - // add(m1, scale(m2, -1)) 3.58 - // transpose*() Returns the transpose of a matrix. 3.59 - // scale*() Multiplies a matrix by a scalar. 3.60 - // multvec*() Multiplies a matrix by a vector. 3.61 - // multmat*() Multiplies a matrix by another matrix. 3.62 - // det*() Calculates the determinant of a matrix. 3.63 - // inverse*() Returns the inverse of a non-singular matrix. 3.64 - // setidentity*() Sets a matrix to the identity matrix. 3.65 - // getcol*() Returns the vector that is column n of the matrix. 3.66 - // setcol*() Set the vector that is column n of the matrix. 3.67 - // 3.68 - // assign*(), add*(), and subtract*() are defined inline, though this might 3.69 - // might not be the best thing. Some real world testing is needed to decide 3.70 - // whether or not htye should be remain inline. 3.71 - 3.72 - typedef int Matrix2i[4]; 3.73 - typedef float Matrix2f[4]; 3.74 - typedef double Matrix2d[4]; 3.75 - 3.76 - typedef int Matrix3i[9]; 3.77 - typedef float Matrix3f[9]; 3.78 - typedef double Matrix3d[9]; 3.79 - 3.80 - typedef int Matrix4i[16]; 3.81 - typedef float Matrix4f[16]; 3.82 - typedef double Matrix4d[16]; 3.83 - 3.84 - ////////////////////////////////////////////////////////////////////////// 3.85 - // getstring*() 3.86 - std::string & getstring2m(Matrix2i m, std::string & s); 3.87 - std::string & getstring2m(Matrix2f m, std::string & s); 3.88 - std::string & getstring2m(Matrix2d m, std::string & s); 3.89 - 3.90 - std::string & getstring3m(Matrix3i m, std::string & s); 3.91 - std::string & getstring3m(Matrix3f m, std::string & s); 3.92 - std::string & getstring3m(Matrix3d m, std::string & s); 3.93 + public: 3.94 + T m[4]; 3.95 3.96 - std::string & getstring4m(Matrix4i m, std::string & s); 3.97 - std::string & getstring4m(Matrix4f m, std::string & s); 3.98 - std::string & getstring4m(Matrix4d m, std::string & s); 3.99 + // Constructors 3.100 + Matrix22() {} 3.101 + Matrix22(T a) { m[0] = m[1] = m[2] = m[3] = a; } 3.102 + Matrix22(T a0, T a1, T a2, T a3) 3.103 + { 3.104 + m[0] = a0; m[2] = a2; 3.105 + m[1] = a1; m[3] = a3; 3.106 + } 3.107 3.108 - ////////////////////////////////////////////////////////////////////////// 3.109 - // assign*() 3.110 + // Array indexing 3.111 + // Remember: column major order is used. 3.112 + inline T& operator[](unsigned int i) 3.113 + { assert (i<4); return m[i]; } 3.114 + inline const T& operator[](unsigned int i) const 3.115 + { assert (i<4); return m[i]; } 3.116 3.117 - inline int * assign2m(Matrix2i m1, Matrix2i m2) 3.118 - { 3.119 - m1[0] = m2[0]; m1[1] = m2[1]; 3.120 - m1[2] = m2[2]; m1[3] = m2[3]; 3.121 - return m1; 3.122 + // Assignment 3.123 + inline Matrix22<T>& operator=(const Matrix22<T>& m2) 3.124 + { 3.125 + m[0] = m2[0]; m[2] = m2[2]; 3.126 + m[1] = m2[1]; m[3] = m2[3]; 3.127 + return *this; 3.128 } 3.129 - inline float * assign2m(Matrix2f m1, Matrix2f m2) 3.130 + inline Matrix22<T>& assign(T a0, T a1, T a2, T a3) 3.131 { 3.132 - m1[0] = m2[0]; m1[1] = m2[1]; 3.133 - m1[2] = m2[2]; m1[3] = m2[3]; 3.134 - return m1; 3.135 - } 3.136 - inline double * assign2m(Matrix2d m1, Matrix2d m2) 3.137 - { 3.138 - m1[0] = m2[0]; m1[1] = m2[1]; 3.139 - m1[2] = m2[2]; m1[3] = m2[3]; 3.140 - return m1; 3.141 + m[0] = a0; m[2] = a2; 3.142 + m[1] = a1; m[3] = a3; 3.143 + return *this; 3.144 } 3.145 3.146 - inline int * assign3m(Matrix3i m1, Matrix3i m2) 3.147 - { 3.148 - m1[0] = m2[0]; m1[1] = m2[1]; m1[2] = m2[2]; 3.149 - m1[3] = m2[3]; m1[4] = m2[4]; m1[5] = m2[5]; 3.150 - m1[6] = m2[6]; m1[7] = m2[7]; m1[8] = m2[8]; 3.151 - return m1; 3.152 + // Comparison 3.153 + inline bool operator==(Matrix22<T>& m2) const 3.154 + { 3.155 + return 3.156 + m[0] == m2[0] && m[2] == m2[2] && 3.157 + m[1] == m2[1] && m[3] == m2[3]; 3.158 } 3.159 - inline float * assign3m(Matrix3f m1, Matrix3f m2) 3.160 - { 3.161 - m1[0] = m2[0]; m1[1] = m2[1]; m1[2] = m2[2]; 3.162 - m1[3] = m2[3]; m1[4] = m2[4]; m1[5] = m2[5]; 3.163 - m1[6] = m2[6]; m1[7] = m2[7]; m1[8] = m2[8]; 3.164 - return m1; 3.165 + inline bool operator!=(Matrix22<T>& m2) const 3.166 + { return ! (*this == m2); } 3.167 + 3.168 + // Matrix addition 3.169 + inline Matrix22<T>& operator+=(const Matrix22<T>& m2) 3.170 + { 3.171 + m[0] += m2[0]; m[2] += m2[2]; 3.172 + m[1] += m2[1]; m[3] += m2[3]; 3.173 + return *this; 3.174 } 3.175 - inline double * assign3m(Matrix3d m1, Matrix3d m2) 3.176 - { 3.177 - m1[0] = m2[0]; m1[1] = m2[1]; m1[2] = m2[2]; 3.178 - m1[3] = m2[3]; m1[4] = m2[4]; m1[5] = m2[5]; 3.179 - m1[6] = m2[6]; m1[7] = m2[7]; m1[8] = m2[8]; 3.180 - return m1; 3.181 + inline Matrix22<T> operator+(const Matrix22<T>& m2) const 3.182 + { return Matrix22<T>(*this) += m2; } 3.183 + 3.184 + // Matrix subtraction 3.185 + inline Matrix22<T>& operator-=(const Matrix22<T>& m2) 3.186 + { 3.187 + m[0] -= m2[0]; m[2] -= m2[2]; 3.188 + m[1] -= m2[1]; m[3] -= m2[3]; 3.189 + return *this; 3.190 + } 3.191 + inline Matrix22<T> operator-(const Matrix22<T>& m2) const 3.192 + { return Matrix22<T>(*this) -= m2; } 3.193 + 3.194 + // Scalar multiplication 3.195 + inline Matrix22<T>& operator*=(const int a) 3.196 + { 3.197 + m[0] *= a; m[2] *= a; 3.198 + m[1] *= a; m[3] *= a; 3.199 + return *this; 3.200 + } 3.201 + inline Matrix22<T>& operator*=(const float a) 3.202 + { 3.203 + m[0] *= a; m[2] *= a; 3.204 + m[1] *= a; m[3] *= a; 3.205 + return *this; 3.206 + } 3.207 + inline Matrix22<T>& operator*=(const double a) 3.208 + { 3.209 + m[0] *= a; m[2] *= a; 3.210 + m[1] *= a; m[3] *= a; 3.211 + return *this; 3.212 } 3.213 3.214 - inline int * assign4m(Matrix4i m1, Matrix4i m2) 3.215 - { 3.216 - m1[0] = m2[0]; m1[1] = m2[1]; m1[2] = m2[2]; m1[3] = m2[3]; 3.217 - m1[4] = m2[4]; m1[5] = m2[5]; m1[6] = m2[6]; m1[7] = m2[7]; 3.218 - m1[8] = m2[8]; m1[9] = m2[9]; m1[10] = m2[10]; m1[11] = m2[11]; 3.219 - m1[12] = m2[12]; m1[13] = m2[13]; m1[14] = m2[14]; m1[15] = m2[15]; 3.220 - return m1; 3.221 + // Scalar division 3.222 + inline Matrix22<T>& operator/=(const int a) 3.223 + { 3.224 + assert(a!=0); 3.225 + m[0] /= a; m[2] /= a; 3.226 + m[1] /= a; m[3] /= a; 3.227 + return *this; 3.228 } 3.229 - inline float * assign4m(Matrix4f m1, Matrix4f m2) 3.230 - { 3.231 - m1[0] = m2[0]; m1[1] = m2[1]; m1[2] = m2[2]; m1[3] = m2[3]; 3.232 - m1[4] = m2[4]; m1[5] = m2[5]; m1[6] = m2[6]; m1[7] = m2[7]; 3.233 - m1[8] = m2[8]; m1[9] = m2[9]; m1[10] = m2[10]; m1[11] = m2[11]; 3.234 - m1[12] = m2[12]; m1[13] = m2[13]; m1[14] = m2[14]; m1[15] = m2[15]; 3.235 - return m1; 3.236 + inline Matrix22<T>& operator/=(const float a) 3.237 + { 3.238 + assert(a!=0); 3.239 + m[0] /= a; m[2] /= a; 3.240 + m[1] /= a; m[3] /= a; 3.241 + return *this; 3.242 } 3.243 - inline double * assign4m(Matrix4d m1, Matrix4d m2) 3.244 - { 3.245 - m1[0] = m2[0]; m1[1] = m2[1]; m1[2] = m2[2]; m1[3] = m2[3]; 3.246 - m1[4] = m2[4]; m1[5] = m2[5]; m1[6] = m2[6]; m1[7] = m2[7]; 3.247 - m1[8] = m2[8]; m1[9] = m2[9]; m1[10] = m2[10]; m1[11] = m2[11]; 3.248 - m1[12] = m2[12]; m1[13] = m2[13]; m1[14] = m2[14]; m1[15] = m2[15]; 3.249 - return m1; 3.250 + inline Matrix22<T>& operator/=(const double a) 3.251 + { 3.252 + assert(a!=0); 3.253 + m[0] /= a; m[2] /= a; 3.254 + m[1] /= a; m[3] /= a; 3.255 + return *this; 3.256 + } 3.257 + inline Matrix22<T> operator/(const int a) 3.258 + { return Matrix22<T>(*this) /= a; } 3.259 + inline Matrix22<T> operator/(const float a) 3.260 + { return Matrix22<T>(*this) /= a; } 3.261 + inline Matrix22<T> operator/(const double a) 3.262 + { return Matrix22<T>(*this) /= a; } 3.263 + 3.264 + // Matrix multiplication 3.265 + // Not sure if this should be inlined at all. Sure the compiler will 3.266 + // probably ignoe the inline request here, but maybe it won't and maybe 3.267 + // that would be bad. Needs real testing. 3.268 + inline Matrix22<T>& operator*=(Matrix22<T> m2) 3.269 + { 3.270 + const int size=2; 3.271 + Matrix22<T> mres(0); 3.272 + int i, j, k; 3.273 + for (i=0; i<size; ++i) 3.274 + for (j=0; j<size; ++j) 3.275 + for (k=0; k<size; ++k) 3.276 + mres[size*i+j] += (*this)[size*k+j] * m2[size*i+k]; 3.277 + *this = mres; 3.278 + return *this; 3.279 + } 3.280 + inline Matrix22<T> operator*(Matrix22<T> m2) const 3.281 + { return Matrix22<T>(*this) *= m2; } 3.282 + 3.283 + // methods 3.284 + inline double det() const 3.285 + { return (double) (m[0] * m[3]) - (double) (m[1] * m[2]); } 3.286 + 3.287 + std::string & getstring(std::string & s) const; 3.288 + 3.289 + inline Matrix22<T>& setidentity() 3.290 + { 3.291 + memset(m, 0, sizeof(m)); 3.292 + m[0] = m[3] = 1; 3.293 + return *this; 3.294 } 3.295 3.296 - ////////////////////////////////////////////////////////////////////////// 3.297 - // add*() 3.298 + inline Matrix22<T> transpose() const 3.299 + { 3.300 + const int size=2; 3.301 + Matrix22<T> mres(0); 3.302 + int i, j; 3.303 + for (i=0; i<size; ++i) 3.304 + for (j=0; j<size; j++) 3.305 + mres[size*i+j] = m[size*j+i]; 3.306 + return mres; 3.307 + } 3.308 + }; 3.309 3.310 - inline int * add2m(Matrix2i m1, Matrix2i m2) 3.311 - { 3.312 - m1[0] += m2[0]; m1[1] += m2[1]; 3.313 - m1[2] += m2[2]; m1[3] += m2[3]; 3.314 - return m1; 3.315 - } 3.316 - inline float * add2m(Matrix2f m1, Matrix2f m2) 3.317 - { 3.318 - m1[0] += m2[0]; m1[1] += m2[1]; 3.319 - m1[2] += m2[2]; m1[3] += m2[3]; 3.320 - return m1; 3.321 - } 3.322 - inline double * add2m(Matrix2d m1, Matrix2d m2) 3.323 - { 3.324 - m1[0] += m2[0]; m1[1] += m2[1]; 3.325 - m1[2] += m2[2]; m1[3] += m2[3]; 3.326 - return m1; 3.327 - } 3.328 + // Scalar multiplication continued 3.329 + template <typename T> inline Matrix22<T> operator*(const Matrix22<T>& m, const int a) 3.330 + { return Matrix22<T>(m) *= a; } 3.331 + template <typename T> inline Matrix22<T> operator*(const int a, const Matrix22<T>& m) 3.332 + { return Matrix22<T>(m) *= a; } 3.333 + template <typename T> inline Matrix22<T> operator*(const Matrix22<T>& m, const float a) 3.334 + { return Matrix22<T>(m) *= a; } 3.335 + template <typename T> inline Matrix22<T> operator*(const float a, const Matrix22<T>& m) 3.336 + { return Matrix22<T>(m) *= a; } 3.337 + template <typename T> inline Matrix22<T> operator*(const Matrix22<T>& m, const double a) 3.338 + { return Matrix22<T>(m) *= a; } 3.339 + template <typename T> inline Matrix22<T> operator*(const double a, const Matrix22<T>& m) 3.340 + { return Matrix22<T>(m) *= a; } 3.341 3.342 - inline int * add3m(Matrix3i m1, Matrix3i m2) 3.343 - { 3.344 - m1[0] += m2[0]; m1[1] += m2[1]; m1[2] += m2[2]; 3.345 - m1[3] += m2[3]; m1[4] += m2[4]; m1[5] += m2[5]; 3.346 - m1[6] += m2[6]; m1[7] += m2[7]; m1[8] += m2[8]; 3.347 - return m1; 3.348 - } 3.349 - inline float * add3m(Matrix3f m1, Matrix3f m2) 3.350 - { 3.351 - m1[0] += m2[0]; m1[1] += m2[1]; m1[2] += m2[2]; 3.352 - m1[3] += m2[3]; m1[4] += m2[4]; m1[5] += m2[5]; 3.353 - m1[6] += m2[6]; m1[7] += m2[7]; m1[8] += m2[8]; 3.354 - return m1; 3.355 - } 3.356 - inline double * add3m(Matrix3d m1, Matrix3d m2) 3.357 - { 3.358 - m1[0] += m2[0]; m1[1] += m2[1]; m1[2] += m2[2]; 3.359 - m1[3] += m2[3]; m1[4] += m2[4]; m1[5] += m2[5]; 3.360 - m1[6] += m2[6]; m1[7] += m2[7]; m1[8] += m2[8]; 3.361 - return m1; 3.362 - } 3.363 3.364 - inline int * add4m(Matrix4i m1, Matrix4i m2) 3.365 - { 3.366 - m1[0] += m2[0]; m1[1] += m2[1]; m1[2] += m2[2]; m1[3] += m2[3]; 3.367 - m1[4] += m2[4]; m1[5] += m2[5]; m1[6] += m2[6]; m1[7] += m2[7]; 3.368 - m1[8] += m2[8]; m1[9] += m2[9]; m1[10] += m2[10]; m1[11] += m2[11]; 3.369 - m1[12] += m2[12]; m1[13] += m2[13]; m1[14] += m2[14]; m1[15] += m2[15]; 3.370 - return m1; 3.371 - } 3.372 - inline float * add4m(Matrix4f m1, Matrix4f m2) 3.373 - { 3.374 - m1[0] += m2[0]; m1[1] += m2[1]; m1[2] += m2[2]; m1[3] += m2[3]; 3.375 - m1[4] += m2[4]; m1[5] += m2[5]; m1[6] += m2[6]; m1[7] += m2[7]; 3.376 - m1[8] += m2[8]; m1[9] += m2[9]; m1[10] += m2[10]; m1[11] += m2[11]; 3.377 - m1[12] += m2[12]; m1[13] += m2[13]; m1[14] += m2[14]; m1[15] += m2[15]; 3.378 - return m1; 3.379 - } 3.380 - inline double * add4m(Matrix4d m1, Matrix4d m2) 3.381 - { 3.382 - m1[0] += m2[0]; m1[1] += m2[1]; m1[2] += m2[2]; m1[3] += m2[3]; 3.383 - m1[4] += m2[4]; m1[5] += m2[5]; m1[6] += m2[6]; m1[7] += m2[7]; 3.384 - m1[8] += m2[8]; m1[9] += m2[9]; m1[10] += m2[10]; m1[11] += m2[11]; 3.385 - m1[12] += m2[12]; m1[13] += m2[13]; m1[14] += m2[14]; m1[15] += m2[15]; 3.386 - return m1; 3.387 - } 3.388 3.389 - ////////////////////////////////////////////////////////////////////////// 3.390 - // subtract*() 3.391 3.392 - inline int * subtract2m(Matrix2i m1, Matrix2i m2) 3.393 - { 3.394 - m1[0] -= m2[0]; m1[1] -= m2[1]; 3.395 - m1[2] -= m2[2]; m1[3] -= m2[3]; 3.396 - return m1; 3.397 - } 3.398 - inline float * subtract2m(Matrix2f m1, Matrix2f m2) 3.399 - { 3.400 - m1[0] -= m2[0]; m1[1] -= m2[1]; 3.401 - m1[2] -= m2[2]; m1[3] -= m2[3]; 3.402 - return m1; 3.403 - } 3.404 - inline double * subtract2m(Matrix2d m1, Matrix2d m2) 3.405 - { 3.406 - m1[0] -= m2[0]; m1[1] -= m2[1]; 3.407 - m1[2] -= m2[2]; m1[3] -= m2[3]; 3.408 - return m1; 3.409 - } 3.410 + ////////////////////////////////////////////////////////////////////////// 3.411 + typedef Matrix22<int> Matrix22i; 3.412 + typedef Matrix22<float> Matrix22f; 3.413 + typedef Matrix22<double> Matrix22d; 3.414 3.415 - inline int * subtract3m(Matrix3i m1, Matrix3i m2) 3.416 - { 3.417 - m1[0] -= m2[0]; m1[1] -= m2[1]; m1[2] -= m2[2]; 3.418 - m1[3] -= m2[3]; m1[4] -= m2[4]; m1[5] -= m2[5]; 3.419 - m1[6] -= m2[6]; m1[7] -= m2[7]; m1[8] -= m2[8]; 3.420 - return m1; 3.421 - } 3.422 - inline float * subtract3m(Matrix3f m1, Matrix3f m2) 3.423 - { 3.424 - m1[0] -= m2[0]; m1[1] -= m2[1]; m1[2] -= m2[2]; 3.425 - m1[3] -= m2[3]; m1[4] -= m2[4]; m1[5] -= m2[5]; 3.426 - m1[6] -= m2[6]; m1[7] -= m2[7]; m1[8] -= m2[8]; 3.427 - return m1; 3.428 - } 3.429 - inline double * subtract3m(Matrix3d m1, Matrix3d m2) 3.430 - { 3.431 - m1[0] -= m2[0]; m1[1] -= m2[1]; m1[2] -= m2[2]; 3.432 - m1[3] -= m2[3]; m1[4] -= m2[4]; m1[5] -= m2[5]; 3.433 - m1[6] -= m2[6]; m1[7] -= m2[7]; m1[8] -= m2[8]; 3.434 - return m1; 3.435 - } 3.436 +#ifdef NOTHING 3.437 + typedef Matrix33<int> Matrix33i; 3.438 + typedef Matrix33<float> Matrix33f; 3.439 + typedef Matrix33<double> Matrix33d; 3.440 + 3.441 + typedef Matrix44<int> Matrix44i; 3.442 + typedef Matrix44<float> Matrix44f; 3.443 + typedef Matrix44<double> Matrix44d; 3.444 + 3.445 +#endif 3.446 + } // namespace arda 3.447 3.448 - inline int * subtract4m(Matrix4i m1, Matrix4i m2) 3.449 - { 3.450 - m1[0] -= m2[0]; m1[1] -= m2[1]; m1[2] -= m2[2]; m1[3] -= m2[3]; 3.451 - m1[4] -= m2[4]; m1[5] -= m2[5]; m1[6] -= m2[6]; m1[7] -= m2[7]; 3.452 - m1[8] -= m2[8]; m1[9] -= m2[9]; m1[10] -= m2[10]; m1[11] -= m2[11]; 3.453 - m1[12] -= m2[12]; m1[13] -= m2[13]; m1[14] -= m2[14]; m1[15] -= m2[15]; 3.454 - return m1; 3.455 - } 3.456 - inline float * subtract4m(Matrix4f m1, Matrix4f m2) 3.457 - { 3.458 - m1[0] -= m2[0]; m1[1] -= m2[1]; m1[2] -= m2[2]; m1[3] -= m2[3]; 3.459 - m1[4] -= m2[4]; m1[5] -= m2[5]; m1[6] -= m2[6]; m1[7] -= m2[7]; 3.460 - m1[8] -= m2[8]; m1[9] -= m2[9]; m1[10] -= m2[10]; m1[11] -= m2[11]; 3.461 - m1[12] -= m2[12]; m1[13] -= m2[13]; m1[14] -= m2[14]; m1[15] -= m2[15]; 3.462 - return m1; 3.463 - } 3.464 - inline double * subtract4m(Matrix4d m1, Matrix4d m2) 3.465 - { 3.466 - m1[0] -= m2[0]; m1[1] -= m2[1]; m1[2] -= m2[2]; m1[3] -= m2[3]; 3.467 - m1[4] -= m2[4]; m1[5] -= m2[5]; m1[6] -= m2[6]; m1[7] -= m2[7]; 3.468 - m1[8] -= m2[8]; m1[9] -= m2[9]; m1[10] -= m2[10]; m1[11] -= m2[11]; 3.469 - m1[12] -= m2[12]; m1[13] -= m2[13]; m1[14] -= m2[14]; m1[15] -= m2[15]; 3.470 - return m1; 3.471 - } 3.472 +//////////////////////////////////////////////////////////////////////////////// 3.473 +template <typename T> std::string & arda::Matrix22<T>::getstring(std::string & s) const 3.474 + { 3.475 + s.clear(); 3.476 + std::stringstream ss; 3.477 + ss << "[ " 3.478 + "[ " << m[0] << ", " << m[1] << " ], " 3.479 + "[ " << m[2] << ", " << m[3] << " ] ]"; 3.480 + s = ss.str(); 3.481 + return s; 3.482 + } 3.483 3.484 - ////////////////////////////////////////////////////////////////////////// 3.485 - // scale*() 3.486 3.487 - inline int * scale2m(Matrix2i m, int s) 3.488 - { 3.489 - m[0] *= s; m[1] *= s; 3.490 - m[2] *= s; m[3] *= s; 3.491 - return m; 3.492 - } 3.493 - inline float * scale2m(Matrix2f m, float s) 3.494 - { 3.495 - m[0] *= s; m[1] *= s; 3.496 - m[2] *= s; m[3] *= s; 3.497 - return m; 3.498 - } 3.499 - inline double * scale2m(Matrix2d m, double s) 3.500 - { 3.501 - m[0] *= s; m[1] *= s; 3.502 - m[2] *= s; m[3] *= s; 3.503 - return m; 3.504 - } 3.505 3.506 - inline int * scale3m(Matrix3i m, int s) 3.507 - { 3.508 - m[0] *= s; m[1] *= s; m[2] *= s; 3.509 - m[3] *= s; m[4] *= s; m[5] *= s; 3.510 - m[4] *= s; m[5] *= s; m[6] *= s; 3.511 - return m; 3.512 - } 3.513 - inline float * scale3m(Matrix3f m, float s) 3.514 - { 3.515 - m[0] *= s; m[1] *= s; m[2] *= s; 3.516 - m[3] *= s; m[4] *= s; m[5] *= s; 3.517 - m[4] *= s; m[5] *= s; m[6] *= s; 3.518 - return m; 3.519 - } 3.520 - inline double * scale3m(Matrix3d m, double s) 3.521 - { 3.522 - m[0] *= s; m[1] *= s; m[2] *= s; 3.523 - m[3] *= s; m[4] *= s; m[5] *= s; 3.524 - m[4] *= s; m[5] *= s; m[6] *= s; 3.525 - return m; 3.526 - } 3.527 3.528 - inline int * scale4m(Matrix4i m, int s) 3.529 - { 3.530 - m[0] *= s; m[1] *= s; m[2] *= s; m[3] *= s; 3.531 - m[4] *= s; m[5] *= s; m[4] *= s; m[5] *= s; 3.532 - m[6] *= s; m[7] *= s; m[8] *= s; m[9] *= s; 3.533 - m[10] *= s; m[11] *= s; m[12] *= s; m[13] *= s; 3.534 - return m; 3.535 - } 3.536 - inline float * scale4m(Matrix4f m, float s) 3.537 - { 3.538 - m[0] *= s; m[1] *= s; m[2] *= s; m[3] *= s; 3.539 - m[4] *= s; m[5] *= s; m[4] *= s; m[5] *= s; 3.540 - m[6] *= s; m[7] *= s; m[8] *= s; m[9] *= s; 3.541 - m[10] *= s; m[11] *= s; m[12] *= s; m[13] *= s; 3.542 - return m; 3.543 - } 3.544 - inline double * scale4m(Matrix4d m, double s) 3.545 - { 3.546 - m[0] *= s; m[1] *= s; m[2] *= s; m[3] *= s; 3.547 - m[4] *= s; m[5] *= s; m[6] *= s; m[7] *= s; 3.548 - m[8] *= s; m[9] *= s; m[10] *= s; m[11] *= s; 3.549 - m[12] *= s; m[13] *= s; m[14] *= s; m[15] *= s; 3.550 - return m; 3.551 - } 3.552 3.553 - ////////////////////////////////////////////////////////////////////////// 3.554 - // multvec*mv(Matrix m, Vector v, Vector vres) 3.555 - // Right multiplies a matrix m by a (column) vector v and stores 3.556 - // the result in vres. 3.557 - // Returns vres. 3.558 - // 3.559 - // multvec*vm(Vector v, Matrix m, Vector vres) 3.560 - // Left multiplies a (row) vector v by a matrix m and stores the 3.561 - // result in vres. 3.562 - // Returns vres. 3.563 3.564 - int * multvec2mv(Matrix2i m, Vector2i v, Vector2i vres); 3.565 - float * multvec2mv(Matrix2f m, Vector2f v, Vector2f vres); 3.566 - double * multvec2mv(Matrix2d m, Vector2d v, Vector2d vres); 3.567 3.568 - int * multvec3mv(Matrix3i m, Vector3i v, Vector3i vres); 3.569 - float * multvec3mv(Matrix3f m, Vector3f v, Vector3f vres); 3.570 - double * multvec3mv(Matrix3d m, Vector3d v, Vector3d vres); 3.571 3.572 - int * multvec4mv(Matrix4i m, Vector4i v, Vector4i vres); 3.573 - float * multvec4mv(Matrix4f m, Vector4f v, Vector4f vres); 3.574 - double * multvec4mv(Matrix4d m, Vector4d v, Vector4d vres); 3.575 3.576 - int * multvec2vm(Vector2i v, Matrix2i m, Vector2i vres); 3.577 - float * multvec2vm(Vector2f v, Matrix2f m, Vector2f vres); 3.578 - double * multvec2vm(Vector2d v, Matrix2d m, Vector2d vres); 3.579 3.580 - int * multvec3vm(Vector3i v, Matrix3i m, Vector3i vres); 3.581 - float * multvec3vm(Vector3f v, Matrix3f m, Vector3f vres); 3.582 - double * multvec3vm(Vector3d v, Matrix3d m, Vector3d vres); 3.583 3.584 - int * multvec4vm(Vector4i v, Matrix4i m, Vector4i vres); 3.585 - float * multvec4vm(Vector4f v, Matrix4f m, Vector4f vres); 3.586 - double * multvec4vm(Vector4d v, Matrix4d m, Vector4d vres); 3.587 3.588 - ////////////////////////////////////////////////////////////////////////// 3.589 - // multmat*(Matrix m1, Matrix m2, Matrix mres) 3.590 - // Multiply two matrices together and return the result in mres. 3.591 - // returns mres. 3.592 +#ifdef NOTHING 3.593 + ////////////////////////////////////////////////////////////////////////// 3.594 + // det*(Matrix m) 3.595 + // Computer the determinant of a matrix. 3.596 + // Returns the determinant. 3.597 3.598 - int * multmat2(Matrix2i m1, Matrix2i m2, Matrix2i mres); 3.599 - float * multmat2(Matrix2f m1, Matrix2f m2, Matrix2f mres); 3.600 - double * multmat2(Matrix2d m1, Matrix2d m2, Matrix2d mres); 3.601 - 3.602 - int * multmat3(Matrix3i m1, Matrix3i m2, Matrix3i mres); 3.603 - float * multmat3(Matrix3f m1, Matrix3f m2, Matrix3f mres); 3.604 - double * multmat3(Matrix3d m1, Matrix3d m2, Matrix3d mres); 3.605 - 3.606 - int * multmat4(Matrix4i m1, Matrix4i m2, Matrix4i mres); 3.607 - float * multmat4(Matrix4f m1, Matrix4f m2, Matrix4f mres); 3.608 - double * multmat4(Matrix4d m1, Matrix4d m2, Matrix4d mres); 3.609 - 3.610 - ////////////////////////////////////////////////////////////////////////// 3.611 - // transpose*(Matrix m, Matrix mres) 3.612 - // Compute the transpose of a matrix and store the result in mres. 3.613 - // Returns mres. 3.614 - 3.615 - int * transpose2(Matrix2i m, Matrix2i mres); 3.616 - float * transpose2(Matrix2f m, Matrix2f mres); 3.617 - double * transpose2(Matrix2d m, Matrix2d mres); 3.618 - 3.619 - int * transpose3(Matrix3i m, Matrix3i mres); 3.620 - float * transpose3(Matrix3f m, Matrix3f mres); 3.621 - double * transpose3(Matrix3d m, Matrix3d mres); 3.622 - 3.623 - int * transpose4(Matrix4i m, Matrix4i mres); 3.624 - float * transpose4(Matrix4f m, Matrix4f mres); 3.625 - double * transpose4(Matrix4d m, Matrix4d mres); 3.626 - 3.627 - ////////////////////////////////////////////////////////////////////////// 3.628 - // det*(Matrix m) 3.629 - // Computer the determinant of a matrix. 3.630 - // Returns the determinant. 3.631 - 3.632 - inline double det2(Matrix2i m) 3.633 - { return m[0] * m[3] - m[1] * m[2]; } 3.634 - inline double det2(Matrix2f m) 3.635 - { return m[0] * m[3] - m[1] * m[2]; } 3.636 - inline double det2(Matrix2d m) 3.637 - { return m[0] * m[3] - m[1] * m[2]; } 3.638 - 3.639 - inline double det3(Matrix3i m) 3.640 - { return m[0] * ( m[4] * m[8] 3.641 - - m[5] * m[7]) 3.642 - - m[3] * ( m[1] * m[8] 3.643 - - m[2] * m[7]) 3.644 - + m[6] * ( m[1] * m[5] 3.645 - - m[2] * m[4]); } 3.646 - inline double det3(Matrix3f m) 3.647 - { return m[0] * ( m[4] * m[8] 3.648 - - m[5] * m[7]) 3.649 - - m[3] * ( m[1] * m[8] 3.650 - - m[2] * m[7]) 3.651 - + m[6] * ( m[1] * m[5] 3.652 - - m[2] * m[4]); } 3.653 - inline double det3(Matrix3d m) 3.654 - { return m[0] * ( m[4] * m[8] 3.655 - - m[5] * m[7]) 3.656 - - m[3] * ( m[1] * m[8] 3.657 - - m[2] * m[7]) 3.658 - + m[6] * (m[1] * m[5] 3.659 + inline double det3(Matrix3i m) 3.660 + { return m[0] * ( m[4] * m[8] 3.661 + - m[5] * m[7]) 3.662 + - m[3] * ( m[1] * m[8] 3.663 + - m[2] * m[7]) 3.664 + + m[6] * ( m[1] * m[5] 3.665 - m[2] * m[4]); } 3.666 3.667 - inline double det4(Matrix4i m) 3.668 - { return m[0] * (m[5] * (m[10] * m[15] - 3.669 - m[11] * m[14]) - 3.670 - m[9] * (m[6] * m[15] - 3.671 - m[7] * m[14]) + 3.672 - m[13] * (m[6] * m[11] - 3.673 - m[7] * m[10])) - 3.674 - m[4] * (m[1] * (m[10] * m[15] - 3.675 + inline double det4(Matrix4i m) 3.676 + { return m[0] * (m[5] * (m[10] * m[15] - 3.677 m[11] * m[14]) - 3.678 - m[9] * (m[2] * m[15] - 3.679 - m[3] * m[14]) + 3.680 - m[13] * (m[2] * m[11] - 3.681 - m[3] * m[10])) + 3.682 - m[8] * (m[1] * (m[6] * m[15] - 3.683 - m[7] * m[14]) - 3.684 - m[5] * (m[2] * m[15] - 3.685 - m[3] * m[14]) + 3.686 - m[13] * (m[2] * m[7] - 3.687 - m[3] * m[6])) + 3.688 - m[12] * (m[1] * (m[6] * m[11] - 3.689 - m[7] * m[10]) - 3.690 - m[5] * (m[2] * m[11] - 3.691 - m[3] * m[10]) + 3.692 - m[9] * (m[2] * m[7] - 3.693 - m[3] * m[6])); } 3.694 - inline double det4(Matrix4f m) 3.695 - { return m[0] * (m[5] * (m[10] * m[15] - 3.696 - m[11] * m[14]) - 3.697 - m[9] * (m[6] * m[15] - 3.698 - m[7] * m[14]) + 3.699 - m[13] * (m[6] * m[11] - 3.700 - m[7] * m[10])) - 3.701 - m[4] * (m[1] * (m[10] * m[15] - 3.702 - m[11] * m[14]) - 3.703 - m[9] * (m[2] * m[15] - 3.704 - m[3] * m[14]) + 3.705 - m[13] * (m[2] * m[11] - 3.706 - m[3] * m[10])) + 3.707 - m[8] * (m[1] * (m[6] * m[15] - 3.708 - m[7] * m[14]) - 3.709 - m[5] * (m[2] * m[15] - 3.710 - m[3] * m[14]) + 3.711 - m[13] * (m[2] * m[7] - 3.712 - m[3] * m[6])) + 3.713 - m[12] * (m[1] * (m[6] * m[11] - 3.714 - m[7] * m[10]) - 3.715 - m[5] * (m[2] * m[11] - 3.716 - m[3] * m[10]) + 3.717 - m[9] * (m[2] * m[7] - 3.718 - m[3] * m[6])); } 3.719 - inline double det4(Matrix4d m) 3.720 - { return m[0] * (m[5] * (m[10] * m[15] - 3.721 - m[11] * m[14]) - 3.722 - m[9] * (m[6] * m[15] - 3.723 - m[7] * m[14]) + 3.724 - m[13] * (m[6] * m[11] - 3.725 - m[7] * m[10])) - 3.726 - m[4] * (m[1] * (m[10] * m[15] - 3.727 - m[11] * m[14]) - 3.728 - m[9] * (m[2] * m[15] - 3.729 - m[3] * m[14]) + 3.730 - m[13] * (m[2] * m[11] - 3.731 - m[3] * m[10])) + 3.732 - m[8] * (m[1] * (m[6] * m[15] - 3.733 - m[7] * m[14]) - 3.734 - m[5] * (m[2] * m[15] - 3.735 - m[3] * m[14]) + 3.736 - m[13] * (m[2] * m[7] - 3.737 - m[3] * m[6])) + 3.738 - m[12] * (m[1] * (m[6] * m[11] - 3.739 - m[7] * m[10]) - 3.740 - m[5] * (m[2] * m[11] - 3.741 - m[3] * m[10]) + 3.742 - m[9] * (m[2] * m[7] - 3.743 - m[3] * m[6])); } 3.744 - } // namespace Matrix 3.745 + m[9] * (m[6] * m[15] - 3.746 + m[7] * m[14]) + 3.747 + m[13] * (m[6] * m[11] - 3.748 + m[7] * m[10])) - 3.749 + m[4] * (m[1] * (m[10] * m[15] - 3.750 + m[11] * m[14]) - 3.751 + m[9] * (m[2] * m[15] - 3.752 + m[3] * m[14]) + 3.753 + m[13] * (m[2] * m[11] - 3.754 + m[3] * m[10])) + 3.755 + m[8] * (m[1] * (m[6] * m[15] - 3.756 + m[7] * m[14]) - 3.757 + m[5] * (m[2] * m[15] - 3.758 + m[3] * m[14]) + 3.759 + m[13] * (m[2] * m[7] - 3.760 + m[3] * m[6])) + 3.761 + m[12] * (m[1] * (m[6] * m[11] - 3.762 + m[7] * m[10]) - 3.763 + m[5] * (m[2] * m[11] - 3.764 + m[3] * m[10]) + 3.765 + m[9] * (m[2] * m[7] - 3.766 + m[3] * m[6])); } 3.767 3.768 - } // namespace arda 3.769 +//////////////////////////////////////////////////////////////////////////////// 3.770 +int * arda::Matrix::multvec3mv(Matrix3i m, Vector3i v, Vector3i vres) 3.771 + { 3.772 + vres[0] = m[0]*v[0] + m[3]*v[1] + m[6]*v[2]; 3.773 + vres[1] = m[1]*v[0] + m[4]*v[1] + m[7]*v[2]; 3.774 + vres[2] = m[2]*v[0] + m[5]*v[1] + m[8]*v[2]; 3.775 + return vres; 3.776 + } 3.777 + 3.778 + 3.779 +//////////////////////////////////////////////////////////////////////////////// 3.780 +int * arda::Matrix::multvec4mv(Matrix4i m, Vector4i v, Vector4i vres) 3.781 + { 3.782 + vres[0] = m[0]*v[0] + m[4]*v[1] + m[8] *v[2] + m[12]*v[3]; 3.783 + vres[1] = m[1]*v[0] + m[5]*v[1] + m[9]*v[2] + m[13]*v[3]; 3.784 + vres[2] = m[2]*v[0] + m[6]*v[1] + m[10]*v[2] + m[14]*v[3]; 3.785 + vres[3] = m[3]*v[0] + m[7]*v[1] + m[11]*v[2] + m[15]*v[3]; 3.786 + return vres; 3.787 + } 3.788 + 3.789 +//////////////////////////////////////////////////////////////////////////////// 3.790 +int * arda::Matrix::multvec3vm(Vector3i v, Matrix3i m, Vector3i vres) 3.791 + { 3.792 + vres[0] = v[0]*m[0] + v[1]*m[1] + v[2]*m[2]; 3.793 + vres[1] = v[0]*m[3] + v[1]*m[4] + v[2]*m[5]; 3.794 + vres[2] = v[0]*m[6] + v[1]*m[7] + v[2]*m[8]; 3.795 + return vres; 3.796 + } 3.797 + 3.798 +//////////////////////////////////////////////////////////////////////////////// 3.799 +float * arda::Matrix::multvec4vm(Vector4f v, Matrix4f m, Vector4f vres) 3.800 + { 3.801 + vres[0] = v[0]*m[0] + v[1]*m[1] + v[2]*m[2] + v[3]*m[3]; 3.802 + vres[1] = v[0]*m[4] + v[1]*m[5] + v[2]*m[6] + v[3]*m[7]; 3.803 + vres[2] = v[0]*m[8] + v[1]*m[9] + v[2]*m[10] + v[3]*m[11]; 3.804 + vres[3] = v[0]*m[12] + v[1]*m[13] + v[2]*m[14] + v[3]*m[15]; 3.805 + return vres; 3.806 + } 3.807 +#endif 3.808 + 3.809 + 3.810 + 3.811 + 3.812 #endif // MATRIX_H_
4.1 --- a/include/Vector.h Wed Sep 28 12:53:57 2011 -0500 4.2 +++ b/include/Vector.h Fri Oct 07 11:08:38 2011 -0500 4.3 @@ -32,9 +32,7 @@ 4.4 // Defined for operations on two vectors of the same type. 4.5 // * *= / /= 4.6 // Defined for scalar multiplication/division with int, float, and double. 4.7 - // *= and /= are defined as member functions, but * and / are defined 4.8 - // as standalone template operators so that I can support v*a as well as 4.9 - // a*v order of multiplication. 4.10 + // * is defined as a standalone template operator for the scalar * Vector form. 4.11 // assign(T a, T b [, T b [, T d]]) 4.12 // Directly assign values to the vector. 4.13 // cross(Vector3& v2) 4.14 @@ -83,15 +81,16 @@ 4.15 // 4.16 // Scalar multiplication and division are overloaded. Why? 4.17 // I'm not sure I need this. The idea is to support all likely scalar 4.18 - // types without having to rely on implicit or explicity type conversion, 4.19 + // types without having to rely on implicit or explicit type conversion, 4.20 // especially not a conversion that might result in loss of precision. 4.21 - // Multiplying a float Vector by a double scala, for example. 4.22 - // Is there a better way to do this? 4.23 + // Multiplying a float Vector by a double scalar, for example. 4.24 + // 4.25 + // Is there a better way to do this? Am I worrying about nothing? 4.26 // 4.27 // 4.28 // 4.29 // I suppose I could make this into a single Vector template on two parameters 4.30 - // template <class T, unsigned int N> class Vector 4.31 + // template <typename T, unsigned int N> class Vector 4.32 // { 4.33 // T v[N]; 4.34 // 4.35 @@ -102,13 +101,13 @@ 4.36 4.37 4.38 ///////////////////////////////////////////////////////////////////////////// 4.39 - template <class T> class Vector2 4.40 + template <typename T> class Vector2 4.41 { 4.42 public: 4.43 T x, y; 4.44 4.45 // Constructors 4.46 - Vector2() : x (0), y(0) {} 4.47 + Vector2() {} 4.48 Vector2(T a) : x (a), y(a) {} 4.49 Vector2(T a, T b) : x (a), y(b) {} 4.50 4.51 @@ -128,19 +127,19 @@ 4.52 inline bool operator==(Vector2<T>& v2) const 4.53 { return ((x == v2.x) && (y == v2.y)); } 4.54 inline bool operator!=(Vector2<T>& v2) const 4.55 - { return ! ((x == v2.x) && (y == v2.y)); } 4.56 + { return ! (*this == v2); } 4.57 4.58 // Vector addition 4.59 - inline Vector2<T> operator+(const Vector2<T>& v2) const 4.60 - { Vector2<T> vres; vres.x = x + v2.x; vres.y = y + v2.y; return vres; } 4.61 inline Vector2<T>& operator+=(const Vector2<T>& v2) 4.62 { x += v2.x; y += v2.y; return *this; } 4.63 + inline Vector2<T> operator+(const Vector2<T>& v2) const 4.64 + { return Vector2<T>(*this) += v2; } 4.65 4.66 // Vector subtraction 4.67 - inline Vector2<T> operator-(const Vector2<T>& v2) const 4.68 - { Vector2<T> vres; vres.x = x - v2.x; vres.y = y - v2.y; return vres; } 4.69 inline Vector2<T>& operator-=(const Vector2<T>& v2) 4.70 { x -= v2.x; y -= v2.y; return *this; } 4.71 + inline Vector2<T> operator-(const Vector2<T>& v2) const 4.72 + { return Vector2<T>(*this) -= v2; } 4.73 4.74 // Scalar multiplication 4.75 inline Vector2<T>& operator*=(const int a) 4.76 @@ -149,6 +148,12 @@ 4.77 { x *= a; y *= a; return *this; } 4.78 inline Vector2<T>& operator*=(const double a) 4.79 { x *= a; y *= a; return *this; } 4.80 + inline Vector2<T> operator*(const int a) 4.81 + { return Vector2<T>(*this) *= a;} 4.82 + inline Vector2<T> operator*(const float a) 4.83 + { return Vector2<T>(*this) *= a;} 4.84 + inline Vector2<T> operator*(const double a) 4.85 + { return Vector2<T>(*this) *= a;} 4.86 4.87 // Scalar division 4.88 inline Vector2<T>& operator/=(const int a) 4.89 @@ -157,6 +162,12 @@ 4.90 { assert(a!=0); x /= a; y /= a; return *this; } 4.91 inline Vector2<T>& operator/=(const double a) 4.92 { assert(a!=0); x /= a; y /= a; return *this; } 4.93 + inline Vector2<T> operator/(const int a) 4.94 + { return Vector2<T>(*this) /= a;} 4.95 + inline Vector2<T> operator/(const float a) 4.96 + { return Vector2<T>(*this) /= a;} 4.97 + inline Vector2<T> operator/(const double a) 4.98 + { return Vector2<T>(*this) /= a;} 4.99 4.100 4.101 // methods 4.102 @@ -188,41 +199,22 @@ 4.103 }; 4.104 4.105 // Scalar multiplication, continued 4.106 - template <class T> inline Vector2<T> operator*(const Vector2<T>& v, const int a) 4.107 - { return Vector2<T>(v) *= a;} 4.108 - template <class T> inline Vector2<T> operator*(const int a, const Vector2<T>& v) 4.109 - { return Vector2<T>(v) *= a;} 4.110 - template <class T> inline Vector2<T> operator*(const Vector2<T> v, const float a) 4.111 + template <typename T> inline Vector2<T> operator*(const int a, const Vector2<T>& v) 4.112 { return Vector2<T>(v) *= a;} 4.113 - template <class T> inline Vector2<T> operator*(const float a, const Vector2<T> v) 4.114 + template <typename T> inline Vector2<T> operator*(const float a, const Vector2<T> v) 4.115 { return Vector2<T>(v) *= a;} 4.116 - template <class T> inline Vector2<T> operator*(const Vector2<T>& v, const double a) 4.117 - { return Vector2<T>(v) *= a;} 4.118 - template <class T> inline Vector2<T> operator*(const double a, const Vector2<T>& v) 4.119 + template <typename T> inline Vector2<T> operator*(const double a, const Vector2<T>& v) 4.120 { return Vector2<T>(v) *= a;} 4.121 4.122 - // Scalar division, continued 4.123 - template <class T> inline Vector2<T> operator/(const Vector2<T>& v, const int a) 4.124 - { return Vector2<T>(v) /= a;} 4.125 - template <class T> inline Vector2<T> operator/(const int a, const Vector2<T>& v) 4.126 - { return Vector2<T>(v) /= a;} 4.127 - template <class T> inline Vector2<T> operator/(const Vector2<T>& v, const float a) 4.128 - { return Vector2<T>(v) /= a;} 4.129 - template <class T> inline Vector2<T> operator/(const float a, const Vector2<T>& v) 4.130 - { return Vector2<T>(v) /= a;} 4.131 - template <class T> inline Vector2<T> operator/(const Vector2<T>& v, const double a) 4.132 - { return Vector2<T>(v) /= a;} 4.133 - template <class T> inline Vector2<T> operator/(const double a, const Vector2<T>& v) 4.134 - { return Vector2<T>(v) /= a;} 4.135 4.136 ///////////////////////////////////////////////////////////////////////////// 4.137 - template <class T> class Vector3 4.138 + template <typename T> class Vector3 4.139 { 4.140 public: 4.141 T x, y, z; 4.142 4.143 // Constructors 4.144 - Vector3() : x (0), y(0), z(0) {} 4.145 + Vector3() {} 4.146 Vector3(T a) : x (a), y(a), z(a) {} 4.147 Vector3(T a, T b, T c) : x (a), y(b), z(c) {} 4.148 4.149 @@ -242,19 +234,19 @@ 4.150 inline bool operator==(Vector3<T>& v2) const 4.151 { return ((x == v2.x) && (y == v2.y) && (z == v2.z)); } 4.152 inline bool operator!=(Vector3<T>& v2) const 4.153 - { return ! ((x == v2.x) && (y == v2.y) && (z == v2.z)); } 4.154 + { return ! (*this == v2); } 4.155 4.156 // Vector addition 4.157 - inline Vector3<T> operator+(const Vector3<T>& v2) const 4.158 - { Vector3<T> vres; vres.x = x + v2.x; vres.y = y + v2.y; vres.z = z + v2.z; return vres; } 4.159 inline Vector3<T>& operator+=(const Vector3<T>& v2) 4.160 { x += v2.x; y += v2.y; z += v2.z; return *this; } 4.161 + inline Vector3<T> operator+(const Vector3<T>& v2) const 4.162 + { return Vector3<T>(*this) += v2; } 4.163 4.164 // Vector subtraction 4.165 - inline Vector3<T> operator-(const Vector3<T>& v2) const 4.166 - { Vector3<T> vres; vres.x = x - v2.x; vres.y = y - v2.y; vres.z = z - v2.z; return vres; } 4.167 inline Vector3<T>& operator-=(const Vector3<T>& v2) 4.168 { x -= v2.x; y -= v2.y; z -= v2.z; return *this; } 4.169 + inline Vector3<T> operator-(const Vector3<T>& v2) const 4.170 + { return Vector3<T>(*this) -= v2; } 4.171 4.172 // Scalar multiplication 4.173 inline Vector3<T>& operator*=(const int a) 4.174 @@ -263,6 +255,12 @@ 4.175 { x *= a; y *= a; z *= a; return *this; } 4.176 inline Vector3<T>& operator*=(const double a) 4.177 { x *= a; y *= a; z *= a; return *this; } 4.178 + inline Vector3<T> operator*(const int a) 4.179 + { return Vector3<T>(*this) *= a;} 4.180 + inline Vector3<T> operator*(const float a) 4.181 + { return Vector3<T>(*this) *= a;} 4.182 + inline Vector3<T> operator*(const double a) 4.183 + { return Vector3<T>(*this) *= a;} 4.184 4.185 // Scalar division 4.186 inline Vector3<T>& operator/=(const int a) 4.187 @@ -271,6 +269,12 @@ 4.188 { assert(a!=0); x /= a; y /= a; z /= a; return *this; } 4.189 inline Vector3<T>& operator/=(const double a) 4.190 { assert(a!=0); x /= a; y /= a; z /= a; return *this; } 4.191 + inline Vector3<T> operator/(const int a) 4.192 + { return Vector3<T>(*this) /= a;} 4.193 + inline Vector3<T> operator/(const float a) 4.194 + { return Vector3<T>(*this) /= a;} 4.195 + inline Vector3<T> operator/(const double a) 4.196 + { return Vector3<T>(*this) /= a;} 4.197 4.198 4.199 // methods 4.200 @@ -318,41 +322,21 @@ 4.201 }; 4.202 4.203 // Scalar multiplication, continued 4.204 - template <class T> inline Vector3<T> operator*(const Vector3<T>& v, const int a) 4.205 - { return Vector3<T>(v) *= a;} 4.206 - template <class T> inline Vector3<T> operator*(const int a, const Vector3<T>& v) 4.207 - { return Vector3<T>(v) *= a;} 4.208 - template <class T> inline Vector3<T> operator*(const Vector3<T> v, const float a) 4.209 + template <typename T> inline Vector3<T> operator*(const int a, const Vector3<T>& v) 4.210 { return Vector3<T>(v) *= a;} 4.211 - template <class T> inline Vector3<T> operator*(const float a, const Vector3<T> v) 4.212 + template <typename T> inline Vector3<T> operator*(const float a, const Vector3<T> v) 4.213 { return Vector3<T>(v) *= a;} 4.214 - template <class T> inline Vector3<T> operator*(const Vector3<T>& v, const double a) 4.215 - { return Vector3<T>(v) *= a;} 4.216 - template <class T> inline Vector3<T> operator*(const double a, const Vector3<T>& v) 4.217 + template <typename T> inline Vector3<T> operator*(const double a, const Vector3<T>& v) 4.218 { return Vector3<T>(v) *= a;} 4.219 4.220 - // Scalar division, continued 4.221 - template <class T> inline Vector3<T> operator/(const Vector3<T>& v, const int a) 4.222 - { return Vector3<T>(v) /= a;} 4.223 - template <class T> inline Vector3<T> operator/(const int a, const Vector3<T>& v) 4.224 - { return Vector3<T>(v) /= a;} 4.225 - template <class T> inline Vector3<T> operator/(const Vector3<T>& v, const float a) 4.226 - { return Vector3<T>(v) /= a;} 4.227 - template <class T> inline Vector3<T> operator/(const float a, const Vector3<T>& v) 4.228 - { return Vector3<T>(v) /= a;} 4.229 - template <class T> inline Vector3<T> operator/(const Vector3<T>& v, const double a) 4.230 - { return Vector3<T>(v) /= a;} 4.231 - template <class T> inline Vector3<T> operator/(const double a, const Vector3<T>& v) 4.232 - { return Vector3<T>(v) /= a;} 4.233 - 4.234 ///////////////////////////////////////////////////////////////////////////// 4.235 - template <class T> class Vector4 4.236 + template <typename T> class Vector4 4.237 { 4.238 public: 4.239 T x, y, z, w; 4.240 4.241 // Constructors 4.242 - Vector4() : x (0), y(0), z(0), w(0) {} 4.243 + Vector4() {} 4.244 Vector4(T a) : x (a), y(a), z(a), w(a) {} 4.245 Vector4(T a, T b, T c, T d) : x (a), y(b), z(c), w(d) {} 4.246 4.247 @@ -372,19 +356,19 @@ 4.248 inline bool operator==(Vector4<T>& v2) const 4.249 { return ((x == v2.x) && (y == v2.y) && (z == v2.z) && (w == v2.w)); } 4.250 inline bool operator!=(Vector4<T>& v2) const 4.251 - { return ! ((x == v2.x) && (y == v2.y) && (z == v2.z) && (w == v2.w)); } 4.252 + { return ! (*this == v2); } 4.253 4.254 // Vector addition 4.255 - inline Vector4<T> operator+(const Vector4<T>& v2) const 4.256 - { Vector4<T> vres; vres.x = x + v2.x; vres.y = y + v2.y; vres.z = z + v2.z; vres.w = w + v2.w; return vres; } 4.257 inline Vector4<T>& operator+=(const Vector4<T>& v2) 4.258 { x += v2.x; y += v2.y; z += v2.z; w += v2.w; return *this; } 4.259 + inline Vector4<T> operator+(const Vector4<T>& v2) const 4.260 + { return Vector4<T>(*this) += v2; } 4.261 4.262 // Vector subtraction 4.263 - inline Vector4<T> operator-(const Vector4<T>& v2) const 4.264 - { Vector4<T> vres; vres.x = x - v2.x; vres.y = y - v2.y; vres.z = z - v2.z; vres.w = w - v2.w; return vres; } 4.265 inline Vector4<T>& operator-=(const Vector4<T>& v2) 4.266 { x -= v2.x; y -= v2.y; z -= v2.z; w -= v2.w; return *this; } 4.267 + inline Vector4<T> operator-(const Vector4<T>& v2) const 4.268 + { return Vector4<T>(*this) -= v2; } 4.269 4.270 // Scalar multiplication 4.271 inline Vector4<T>& operator*=(const int a) 4.272 @@ -393,6 +377,12 @@ 4.273 { x *= a; y *= a; z *= a; w *= a; return *this; } 4.274 inline Vector4<T>& operator*=(const double a) 4.275 { x *= a; y *= a; z *= a; w *= a; return *this; } 4.276 + inline Vector4<T> operator*(const int a) 4.277 + { return Vector4<T>(*this) *= a;} 4.278 + inline Vector4<T> operator*(const float a) 4.279 + { return Vector4<T>(*this) *= a;} 4.280 + inline Vector4<T> operator*(const double a) 4.281 + { return Vector4<T>(*this) *= a;} 4.282 4.283 // Scalar division 4.284 inline Vector4<T>& operator/=(const int a) 4.285 @@ -401,6 +391,12 @@ 4.286 { assert(a!=0); x /= a; y /= a; z /= a; w /= a; return *this; } 4.287 inline Vector4<T>& operator/=(const double a) 4.288 { assert(a!=0); x /= a; y /= a; z /= a; w /= a; return *this; } 4.289 + inline Vector4<T> operator/(const int a) 4.290 + { return Vector4<T>(*this) /= a;} 4.291 + inline Vector4<T> operator/(const float a) 4.292 + { return Vector4<T>(*this) /= a;} 4.293 + inline Vector4<T> operator/(const double a) 4.294 + { return Vector4<T>(*this) /= a;} 4.295 4.296 4.297 // methods 4.298 @@ -432,32 +428,12 @@ 4.299 }; 4.300 4.301 // Scalar multiplication, continued 4.302 - template <class T> inline Vector4<T> operator*(const Vector4<T>& v, const int a) 4.303 - { return Vector4<T>(v) *= a;} 4.304 - template <class T> inline Vector4<T> operator*(const int a, const Vector4<T>& v) 4.305 - { return Vector4<T>(v) *= a;} 4.306 - template <class T> inline Vector4<T> operator*(const Vector4<T> v, const float a) 4.307 + template <typename T> inline Vector4<T> operator*(const int a, const Vector4<T>& v) 4.308 { return Vector4<T>(v) *= a;} 4.309 - template <class T> inline Vector4<T> operator*(const float a, const Vector4<T> v) 4.310 + template <typename T> inline Vector4<T> operator*(const float a, const Vector4<T> v) 4.311 { return Vector4<T>(v) *= a;} 4.312 - template <class T> inline Vector4<T> operator*(const Vector4<T>& v, const double a) 4.313 + template <typename T> inline Vector4<T> operator*(const double a, const Vector4<T>& v) 4.314 { return Vector4<T>(v) *= a;} 4.315 - template <class T> inline Vector4<T> operator*(const double a, const Vector4<T>& v) 4.316 - { return Vector4<T>(v) *= a;} 4.317 - 4.318 - // Scalar division, continued 4.319 - template <class T> inline Vector4<T> operator/(const Vector4<T>& v, const int a) 4.320 - { return Vector4<T>(v) /= a;} 4.321 - template <class T> inline Vector4<T> operator/(const int a, const Vector4<T>& v) 4.322 - { return Vector4<T>(v) /= a;} 4.323 - template <class T> inline Vector4<T> operator/(const Vector4<T>& v, const float a) 4.324 - { return Vector4<T>(v) /= a;} 4.325 - template <class T> inline Vector4<T> operator/(const float a, const Vector4<T>& v) 4.326 - { return Vector4<T>(v) /= a;} 4.327 - template <class T> inline Vector4<T> operator/(const Vector4<T>& v, const double a) 4.328 - { return Vector4<T>(v) /= a;} 4.329 - template <class T> inline Vector4<T> operator/(const double a, const Vector4<T>& v) 4.330 - { return Vector4<T>(v) /= a;} 4.331 4.332 ///////////////////////////////////////////////////////////////////////////// 4.333 4.334 @@ -476,7 +452,7 @@ 4.335 } // namespace arda 4.336 4.337 //////////////////////////////////////////////////////////////////////////////// 4.338 -template <class T> std::string & arda::Vector2<T>::getstring(std::string & s) const 4.339 +template <typename T> std::string & arda::Vector2<T>::getstring(std::string & s) const 4.340 { 4.341 s.clear(); 4.342 std::stringstream ss; 4.343 @@ -486,7 +462,7 @@ 4.344 } 4.345 4.346 //////////////////////////////////////////////////////////////////////////////// 4.347 -template <class T> std::string & arda::Vector3<T>::getstring(std::string & s) const 4.348 +template <typename T> std::string & arda::Vector3<T>::getstring(std::string & s) const 4.349 { 4.350 s.clear(); 4.351 std::stringstream ss; 4.352 @@ -496,7 +472,7 @@ 4.353 } 4.354 4.355 //////////////////////////////////////////////////////////////////////////////// 4.356 -template <class T> std::string & arda::Vector4<T>::getstring(std::string & s) const 4.357 +template <typename T> std::string & arda::Vector4<T>::getstring(std::string & s) const 4.358 { 4.359 s.clear(); 4.360 std::stringstream ss;
5.1 --- a/src/main.cpp Wed Sep 28 12:53:57 2011 -0500 5.2 +++ b/src/main.cpp Fri Oct 07 11:08:38 2011 -0500 5.3 @@ -2,6 +2,7 @@ 5.4 #include <iomanip> 5.5 #include <limits> 5.6 #include <string> 5.7 + 5.8 using namespace std; 5.9 5.10 #include "Math.h" 5.11 @@ -15,7 +16,7 @@ 5.12 { 5.13 cout << "===============================================================" << endl << 5.14 "Testing Vector constructors" << endl; 5.15 - Vector2i v2i_1; 5.16 + Vector2i v2i_1(0); 5.17 Vector2i v2i_2(1); 5.18 Vector2i v2i_3(1, 2); 5.19 Vector2i v2i_4(v2i_3); 5.20 @@ -25,7 +26,7 @@ 5.21 cout << setw(40) << "v2i_3: " << v2i_3.getstring(s1) << endl; 5.22 cout << setw(40) << "v2i_4: " << v2i_4.getstring(s1) << endl; 5.23 5.24 - Vector3f v3f_1; 5.25 + Vector3f v3f_1(0); 5.26 Vector3f v3f_2(1); 5.27 Vector3f v3f_3(1, 2, 3); 5.28 Vector3f v3f_4(v3f_3); 5.29 @@ -35,7 +36,7 @@ 5.30 cout << setw(40) << "v3f_3: " << v3f_3.getstring(s1) << endl; 5.31 cout << setw(40) << "v3f_4: " << v3f_4.getstring(s1) << endl; 5.32 5.33 - Vector4d v4d_1; 5.34 + Vector4d v4d_1(0); 5.35 Vector4d v4d_2(1); 5.36 Vector4d v4d_3(1, 2, 3, 4); 5.37 Vector4d v4d_4(v4d_3); 5.38 @@ -64,9 +65,9 @@ 5.39 Vector2i v2i(1,2); 5.40 Vector3f v3f(1.1f, 2.2f, 3.3f); 5.41 Vector4d v4d(1.1, 2.2, 3.3, 4.4); 5.42 - Vector2i v2i_2; 5.43 - Vector3f v3f_2; 5.44 - Vector4d v4d_2; 5.45 + Vector2i v2i_2(0); 5.46 + Vector3f v3f_2(0); 5.47 + Vector4d v4d_2(0); 5.48 5.49 cout << "Before assignment" << endl; 5.50 cout << setw(40) << "v2i: " << v2i.getstring(s1) << endl; 5.51 @@ -99,9 +100,12 @@ 5.52 Vector2i v2i(1,2); 5.53 Vector3f v3f(1.1f, 2.2f, 3.3f); 5.54 Vector4d v4d(1.1, 2.2, 3.3, 4.4); 5.55 - Vector2i v2i_2; 5.56 - Vector3f v3f_2; 5.57 - Vector4d v4d_2; 5.58 + Vector2i v2i_2(1,2); 5.59 + Vector3f v3f_2(1.1f, 2.2f, 3.3f); 5.60 + Vector4d v4d_2(1.1, 2.2, 3.3, 4.4); 5.61 + Vector2i v2i_3(0); 5.62 + Vector3f v3f_3(0); 5.63 + Vector4d v4d_3(0); 5.64 5.65 cout << setw(40) << "v2i: " << v2i.getstring(s1) << endl; 5.66 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl; 5.67 @@ -109,13 +113,22 @@ 5.68 cout << setw(40) << "v2i_2: " << v2i_2.getstring(s1) << endl; 5.69 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl; 5.70 cout << setw(40) << "v4d_2: " << v4d_2.getstring(s1) << endl; 5.71 + cout << setw(40) << "v2i_3: " << v2i_3.getstring(s1) << endl; 5.72 + cout << setw(40) << "v3f_3: " << v3f_3.getstring(s1) << endl; 5.73 + cout << setw(40) << "v4d_3: " << v4d_3.getstring(s1) << endl; 5.74 cout << boolalpha; 5.75 cout << setw(40) << "v2i == v2i_2: " << (v2i == v2i_2) << endl; 5.76 + cout << setw(40) << "v2i == v2i_3: " << (v2i == v2i_3) << endl; 5.77 + cout << setw(40) << "v2i != v2i_2: " << (v2i != v2i_2) << endl; 5.78 + cout << setw(40) << "v2i != v2i_3: " << (v2i != v2i_3) << endl; 5.79 cout << setw(40) << "v3f == v3f_2: " << (v3f == v3f_2) << endl; 5.80 + cout << setw(40) << "v3f == v3f_3: " << (v3f == v3f_3) << endl; 5.81 + cout << setw(40) << "v3f != v3f_2: " << (v3f != v3f_2) << endl; 5.82 + cout << setw(40) << "v3f != v3f_3: " << (v3f != v3f_3) << endl; 5.83 cout << setw(40) << "v4d == v4d_2: " << (v4d == v4d_2) << endl; 5.84 - cout << setw(40) << "v2i != v2i_2: " << (v2i != v2i_2) << endl; 5.85 - cout << setw(40) << "v3f != v3f_2: " << (v3f != v3f_2) << endl; 5.86 + cout << setw(40) << "v4d == v4d_3: " << (v4d == v4d_3) << endl; 5.87 cout << setw(40) << "v4d != v4d_2: " << (v4d != v4d_2) << endl; 5.88 + cout << setw(40) << "v4d != v4d_3: " << (v4d != v4d_3) << endl; 5.89 } 5.90 5.91 { 5.92 @@ -127,9 +140,9 @@ 5.93 Vector2i v2i_2(3,4); 5.94 Vector3f v3f_2(4.4, 5.5, 6.6); 5.95 Vector4d v4d_2(5.5, 6.6, 7.7, 8.8); 5.96 - Vector2i v2i_3; 5.97 - Vector3f v3f_3 ; 5.98 - Vector4d v4d_3; 5.99 + Vector2i v2i_3(0); 5.100 + Vector3f v3f_3(0); 5.101 + Vector4d v4d_3(0); 5.102 5.103 cout << setw(40) << "v2i: " << v2i.getstring(s1) << endl; 5.104 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl; 5.105 @@ -160,9 +173,9 @@ 5.106 Vector2i v2i_2(3,4); 5.107 Vector3f v3f_2(4.4, 5.5, 6.6); 5.108 Vector4d v4d_2(5.5, 6.6, 7.7, 8.8); 5.109 - Vector2i v2i_3; 5.110 - Vector3f v3f_3; 5.111 - Vector4d v4d_3; 5.112 + Vector2i v2i_3(0); 5.113 + Vector3f v3f_3(0); 5.114 + Vector4d v4d_3(0); 5.115 5.116 cout << setw(40) << "v2i: " << v2i.getstring(s1) << endl; 5.117 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl; 5.118 @@ -190,13 +203,16 @@ 5.119 Vector2i v2i(1,2); 5.120 Vector3f v3f(1.1f, 2.2f, 3.3f); 5.121 Vector4d v4d(1.1, 2.2, 3.3, 4.4); 5.122 - Vector2i v2i_2; 5.123 - Vector3f v3f_2; 5.124 - Vector4d v4d_2; 5.125 + Vector2i v2i_2(0); 5.126 + Vector3f v3f_2(0); 5.127 + Vector4d v4d_2(0); 5.128 int i = 2; 5.129 float f = 2.f; 5.130 double d = 2.0; 5.131 5.132 + cout << setw(40) << "i: " << i << endl; 5.133 + cout << setw(40) << "f: " << f << endl; 5.134 + cout << setw(40) << "d: " << d << endl; 5.135 cout << setw(40) << "v2i: " << v2i.getstring(s1) << endl; 5.136 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl; 5.137 cout << setw(40) << "v4d: " << v4d.getstring(s1) << endl; 5.138 @@ -223,9 +239,9 @@ 5.139 Vector2i v2i(1,2); 5.140 Vector3f v3f(1.1f, 2.2f, 3.3f); 5.141 Vector4d v4d(1.1, 2.2, 3.3, 4.4); 5.142 - Vector2i v2i_2; 5.143 - Vector3f v3f_2; 5.144 - Vector4d v4d_2; 5.145 + Vector2i v2i_2(0); 5.146 + Vector3f v3f_2(0); 5.147 + Vector4d v4d_2(0); 5.148 int i = 2; 5.149 float f = 2.f; 5.150 double d = 2.0; 5.151 @@ -255,8 +271,8 @@ 5.152 "Testing Vector cross product" << endl; 5.153 Vector3f v3f(1.1f, 2.2f, 3.3f); 5.154 Vector3f v3f_2(4.4, 5.5, 6.6); 5.155 - Vector3f v3f_3; 5.156 - Vector3f v3f_4; 5.157 + Vector3f v3f_3(0); 5.158 + Vector3f v3f_4(0); 5.159 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl; 5.160 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl; 5.161 cout << setw(40) << "v3f_3: " << v3f_3.getstring(s1) << endl; 5.162 @@ -388,13 +404,13 @@ 5.163 "Testing Vector get_proj" << endl; 5.164 Vector2f v2f(1.1, 2.2); 5.165 Vector2f v2f_2(3.3, 4.4); 5.166 - Vector2f v2f_3; 5.167 + Vector2f v2f_3(0); 5.168 Vector3f v3f(1.1f, 2.2f, 3.3f); 5.169 Vector3f v3f_2(4.4, 5.5, 6.6); 5.170 - Vector3f v3f_3; 5.171 + Vector3f v3f_3(0); 5.172 Vector4d v4d(1.1, 2.2, 3.3, 4.4); 5.173 Vector4d v4d_2(5.5, 6.6, 7.7, 8.8); 5.174 - Vector4d v4d_3; 5.175 + Vector4d v4d_3(0); 5.176 cout << setw(40) << "v2f: " << v2f.getstring(s1) << endl; 5.177 cout << setw(40) << "v2f_2: " << v2f_2.getstring(s1) << endl; 5.178 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl; 5.179 @@ -423,7 +439,179 @@ 5.180 //////////////////////////////////////////////////////////////////////////////// 5.181 void test_matrix(void) 5.182 { 5.183 - string s; 5.184 + string s1, s2; 5.185 + 5.186 + { 5.187 + cout << "===============================================================" << endl << 5.188 + "Testing Maxtrix constructors" << endl; 5.189 + Matrix22i m22i_1(0); 5.190 + Matrix22i m22i_2(1); 5.191 + Matrix22i m22i_3(1, 2, 3, 4); 5.192 + Matrix22i m22i_4(m22i_3); 5.193 + 5.194 + cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl; 5.195 + cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl; 5.196 + cout << setw(40) << "m22i_3: " << m22i_3.getstring(s1) << endl; 5.197 + cout << setw(40) << "m22i_4: " << m22i_4.getstring(s1) << endl; 5.198 + } 5.199 + 5.200 + { 5.201 + cout << "===============================================================" << endl << 5.202 + "Testing Maxtrix array indexing" << endl; 5.203 + Matrix22i m22i(1, 2, 3, 4); 5.204 + 5.205 + cout << setw(40) << "m22i: " << m22i[0] << " " << m22i[1] << " " << m22i[2] << " " << m22i[3] << " "<< endl; 5.206 + } 5.207 + 5.208 + { 5.209 + cout << "===============================================================" << endl << 5.210 + "Testing Maxtrix array assignment" << endl; 5.211 + Matrix22i m22i_1(1, 2, 3, 4); 5.212 + Matrix22i m22i_2(5, 6, 7, 8); 5.213 + 5.214 + cout << "Before assignment" << endl; 5.215 + cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl; 5.216 + cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl; 5.217 + 5.218 + m22i_2 = m22i_1; 5.219 + cout << "After assignment by = " << endl; 5.220 + cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl; 5.221 + 5.222 + m22i_2.assign(9, 10, 11, 12); 5.223 + cout << "After assignment by assign" << endl; 5.224 + cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl; 5.225 + } 5.226 + 5.227 + { 5.228 + cout << "===============================================================" << endl << 5.229 + "Testing Maxtrix comparison" << endl; 5.230 + Matrix22i m22i_1(1, 2, 3, 4); 5.231 + Matrix22i m22i_2(1, 2, 3, 4); 5.232 + Matrix22i m22i_3(0); 5.233 + 5.234 + cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl; 5.235 + cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl; 5.236 + cout << setw(40) << "m22i_3: " << m22i_3.getstring(s1) << endl; 5.237 + cout << boolalpha; 5.238 + cout << setw(40) << "m22i_1 == m22i_2: " << (m22i_1 == m22i_2) << endl; 5.239 + cout << setw(40) << "m22i_1 == m22i_3: " << (m22i_2 == m22i_3) << endl; 5.240 + cout << setw(40) << "m22i_1 != m22i_2: " << (m22i_1 != m22i_2) << endl; 5.241 + cout << setw(40) << "m22i_1 != m22i_3: " << (m22i_2 != m22i_3) << endl; 5.242 + } 5.243 + 5.244 + { 5.245 + cout << "===============================================================" << endl << 5.246 + "Testing Maxtrix addition" << endl; 5.247 + Matrix22i m22i_1(1, 2, 3, 4); 5.248 + Matrix22i m22i_2(5, 6, 7, 8); 5.249 + Matrix22i m22i_3(0); 5.250 + 5.251 + cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl; 5.252 + cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl; 5.253 + cout << setw(40) << "m22i_3: " << m22i_3.getstring(s1) << endl; 5.254 + m22i_3 = m22i_1 + m22i_2; 5.255 + cout << setw(40) << "m22i_3 = m22i_1 + m22i_2: " << m22i_3.getstring(s1) << endl; 5.256 + 5.257 + m22i_3 += m22i_1; 5.258 + cout << setw(40) << "m22i_3 += m22i_1: " << m22i_3.getstring(s1) << endl; 5.259 + } 5.260 + 5.261 + { 5.262 + cout << "===============================================================" << endl << 5.263 + "Testing Maxtrix subtraction" << endl; 5.264 + Matrix22i m22i_1(1, 2, 3, 4); 5.265 + Matrix22i m22i_2(5, 6, 7, 8); 5.266 + Matrix22i m22i_3(0); 5.267 + 5.268 + cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl; 5.269 + cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl; 5.270 + cout << setw(40) << "m22i_3: " << m22i_3.getstring(s1) << endl; 5.271 + m22i_3 = m22i_1 - m22i_2; 5.272 + cout << setw(40) << "m22i_3 = m22i_1 - m22i_2: " << m22i_3.getstring(s1) << endl; 5.273 + 5.274 + m22i_3 -= m22i_1; 5.275 + cout << setw(40) << "m22i_3 -= m22i_1: " << m22i_3.getstring(s1) << endl; 5.276 + } 5.277 + 5.278 + { 5.279 + cout << "===============================================================" << endl << 5.280 + "Testing Matrix scalar multiplication" << endl; 5.281 + Matrix22i m22i_1(1, 2, 3, 4); 5.282 + Matrix22i m22i_2(0); 5.283 + int i = 2; 5.284 + 5.285 + cout << setw(40) << "i: " << i << endl; 5.286 + cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl; 5.287 + cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl; 5.288 + m22i_2 = m22i_1 *i; 5.289 + cout << setw(40) << "m22i_2 = m22i_1 * i: " << m22i_2.getstring(s1) << endl; 5.290 + m22i_2 = i * m22i_1; 5.291 + cout << setw(40) << "m22i_2 = i * m22i_1: " << m22i_2.getstring(s1) << endl; 5.292 + m22i_2 *= i; 5.293 + cout << setw(40) << "m22i_2 *= i: " << m22i_2.getstring(s1) << endl; 5.294 + } 5.295 + 5.296 + { 5.297 + cout << "===============================================================" << endl << 5.298 + "Testing Matrix scalar division" << endl; 5.299 + Matrix22i m22i_1(1, 2, 3, 4); 5.300 + Matrix22i m22i_2(0); 5.301 + int i = 2; 5.302 + 5.303 + cout << setw(40) << "i: " << i << endl; 5.304 + cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl; 5.305 + cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl; 5.306 + m22i_2 = m22i_1 / i; 5.307 + cout << setw(40) << "m22i_2 = m22i_1 / i: " << m22i_2.getstring(s1) << endl; 5.308 + m22i_1 /= i; 5.309 + cout << setw(40) << "m22i_1 /= i: " << m22i_2.getstring(s1) << endl; 5.310 + } 5.311 + 5.312 + { 5.313 + cout << "===============================================================" << endl << 5.314 + "Testing Maxtrix multiplication" << endl; 5.315 + Matrix22i m22i_1(1, 2, 3, 4); 5.316 + Matrix22i m22i_2(5, 6, 7, 8); 5.317 + Matrix22i m22i_3(0); 5.318 + 5.319 + cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl; 5.320 + cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl; 5.321 + cout << setw(40) << "m22i_3: " << m22i_3.getstring(s1) << endl; 5.322 + m22i_3 = m22i_1 * m22i_2; 5.323 + cout << setw(40) << "m22i_3 = m22i_1 * m22i_2: " << m22i_3.getstring(s1) << endl; 5.324 + } 5.325 + 5.326 + { 5.327 + cout << "===============================================================" << endl << 5.328 + "Testing Maxtrix det" << endl; 5.329 + Matrix22i m22i_1(1, 2, 3, 4); 5.330 + double d; 5.331 + 5.332 + cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl; 5.333 + d = m22i_1.det(); 5.334 + cout << setw(40) << "d = m22i_1.det(): " << d << endl; 5.335 + } 5.336 + 5.337 + { 5.338 + cout << "===============================================================" << endl << 5.339 + "Testing Maxtrix transpose" << endl; 5.340 + Matrix22i m22i_1(1, 2, 3, 4); 5.341 + Matrix22i m22i_2; 5.342 + 5.343 + cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl; 5.344 + m22i_2 = m22i_1.transpose(); 5.345 + cout << setw(40) << "m22i_2 = m22i_1.transpose(): " << m22i_2.getstring(s1) << endl; 5.346 + } 5.347 + 5.348 + { 5.349 + cout << "===============================================================" << endl << 5.350 + "Testing Maxtrix setidentity" << endl; 5.351 + Matrix22i m22i_1(1, 2, 3, 4); 5.352 + 5.353 + cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl; 5.354 + m22i_1.setidentity(); 5.355 + cout << setw(40) << "m22i_1.setidentity(): " << m22i_1.getstring(s1) << endl; 5.356 + } 5.357 } 5.358 5.359 ////////////////////////////////////////////////////////////////////////////////