From 60f448e97a0e4b1982d23f98f332f9845a9c809f Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Sat, 9 Nov 2013 01:23:30 +0100 Subject: [PATCH] Oystermath updated Needs to be tested though. removed OysterMath.cpp because it was not needed anymore --- OysterMath/LinearMath.h | 665 ++++++++++++++++++----------- OysterMath/Matrix.h | 855 +++++++++++++++++++------------------- OysterMath/OysterMath.cpp | 32 -- OysterMath/OysterMath.h | 368 ++++++++-------- OysterMath/Quaternion.h | 167 ++++---- OysterMath/Vector.h | 756 +++++++++++++++++---------------- 6 files changed, 1513 insertions(+), 1330 deletions(-) delete mode 100644 OysterMath/OysterMath.cpp diff --git a/OysterMath/LinearMath.h b/OysterMath/LinearMath.h index a9bdef25..b625a9ed 100644 --- a/OysterMath/LinearMath.h +++ b/OysterMath/LinearMath.h @@ -3,7 +3,6 @@ // © Dan Andersson 2013 ///////////////////////////////////////////////////////////////////// -#pragma once #ifndef LINEARMATH_H #define LINEARMATH_H @@ -12,257 +11,439 @@ #include "Quaternion.h" #include +// x2 + +template +::LinearAlgebra::Matrix2x2 operator * ( const ::LinearAlgebra::Matrix2x2 &left, const ::LinearAlgebra::Matrix2x2 &right ) +{ + return ::LinearAlgebra::Matrix2x2( (left.m11 * right.m11) + (left.m12 * right.m21), + (left.m11 * right.m12) + (left.m12 * right.m22), + (left.m21 * right.m11) + (left.m22 * right.m21), + (left.m21 * right.m12) + (left.m22 * right.m22) ); +} + +template +::LinearAlgebra::Vector2 operator * ( const ::LinearAlgebra::Matrix2x2 &matrix, const ::LinearAlgebra::Vector2 &vector ) +{ + return ::LinearAlgebra::Vector2( (matrix.m11 * vector.x) + (matrix.m12 * vector.y), + (matrix.m21 * vector.x) + (matrix.m22 * vector.y) ); +} + +template +::LinearAlgebra::Vector2 operator * ( const ::LinearAlgebra::Vector2 &vector, const ::LinearAlgebra::Matrix2x2 &left ) +{ + return ::LinearAlgebra::Vector2( (vector.x * matrix.m11) + (vector.y * matrix.m21), + (vector.x * matrix.m12) + (vector.y * matrix.m22) ); +} + +// x3 + +template +::LinearAlgebra::Matrix3x3 operator * ( const ::LinearAlgebra::Matrix3x3 &left, const ::LinearAlgebra::Matrix3x3 &right ) +{ + return ::LinearAlgebra::Matrix3x3( (left.m11 * right.m11) + (left.m12 * right.m21) + (left.m13 * right.m31), (left.m11 * right.m12) + (left.m12 * right.m22) + (left.m13 * right.m32), (left.m11 * right.m13) + (left.m12 * right.m23) + (left.m13 * right.m33), + (left.m21 * right.m11) + (left.m22 * right.m21) + (left.m23 * right.m31), (left.m21 * right.m12) + (left.m22 * right.m22) + (left.m23 * right.m32), (left.m21 * right.m13) + (left.m22 * right.m23) + (left.m23 * right.m33), + (left.m31 * right.m11) + (left.m32 * right.m21) + (left.m33 * right.m31), (left.m31 * right.m12) + (left.m32 * right.m22) + (left.m33 * right.m32), (left.m31 * right.m13) + (left.m32 * right.m23) + (left.m33 * right.m33) ); +} + +template +::LinearAlgebra::Vector3 operator * ( const ::LinearAlgebra::Matrix3x3 &matrix, const ::LinearAlgebra::Vector3 &vector ) +{ + return ::LinearAlgebra::Vector3( (matrix.m11 * vector.x) + (matrix.m12 * vector.y) + (matrix.m13 * vector.z), + (matrix.m21 * vector.x) + (matrix.m22 * vector.y) + (matrix.m23 * vector.z), + (matrix.m31 * vector.x) + (matrix.m32 * vector.y) + (matrix.m33 * vector.z) ); +} + +template +::LinearAlgebra::Vector3 operator * ( const ::LinearAlgebra::Vector3 &vector, const ::LinearAlgebra::Matrix3x3 &left ) +{ + return ::LinearAlgebra::Vector3( (vector.x * matrix.m11) + (vector.y * matrix.m21) + (vector.z * matrix.m31), + (vector.x * matrix.m12) + (vector.y * matrix.m22) + (vector.z * matrix.m32), + (vector.x * matrix.m13) + (vector.y * matrix.m23) + (vector.z * matrix.m33) ); +} + +// x4 + +template +::LinearAlgebra::Matrix4x4 operator * ( const ::LinearAlgebra::Matrix4x4 &left, const ::LinearAlgebra::Matrix4x4 &right ) +{ + return ::LinearAlgebra::Matrix4x4( (left.m11 * right.m11) + (left.m12 * right.m21) + (left.m13 * right.m31) + (left.m14 * right.m41), (left.m11 * right.m12) + (left.m12 * right.m22) + (left.m13 * right.m32) + (left.m14 * right.m42), (left.m11 * right.m13) + (left.m12 * right.m23) + (left.m13 * right.m33) + (left.m14 * right.m43), (left.m11 * right.m14) + (left.m12 * right.m24) + (left.m13 * right.m34) + (left.m14 * right.m44), + (left.m21 * right.m11) + (left.m22 * right.m21) + (left.m23 * right.m31) + (left.m24 * right.m41), (left.m21 * right.m12) + (left.m22 * right.m22) + (left.m23 * right.m32) + (left.m24 * right.m42), (left.m21 * right.m13) + (left.m22 * right.m23) + (left.m23 * right.m33) + (left.m24 * right.m43), (left.m21 * right.m14) + (left.m22 * right.m24) + (left.m23 * right.m34) + (left.m24 * right.m44), + (left.m31 * right.m11) + (left.m32 * right.m21) + (left.m33 * right.m31) + (left.m34 * right.m41), (left.m31 * right.m12) + (left.m32 * right.m22) + (left.m33 * right.m32) + (left.m34 * right.m42), (left.m31 * right.m13) + (left.m32 * right.m23) + (left.m33 * right.m33) + (left.m34 * right.m43), (left.m31 * right.m14) + (left.m32 * right.m24) + (left.m33 * right.m34) + (left.m34 * right.m44), + (left.m41 * right.m11) + (left.m42 * right.m21) + (left.m43 * right.m31) + (left.m44 * right.m41), (left.m41 * right.m12) + (left.m42 * right.m22) + (left.m43 * right.m32) + (left.m44 * right.m42), (left.m41 * right.m13) + (left.m42 * right.m23) + (left.m43 * right.m33) + (left.m44 * right.m43), (left.m41 * right.m14) + (left.m42 * right.m24) + (left.m43 * right.m34) + (left.m44 * right.m44) ); +} + +template +::LinearAlgebra::Vector4 operator * ( const ::LinearAlgebra::Matrix4x4 &matrix, const ::LinearAlgebra::Vector4 &vector ) +{ + return ::LinearAlgebra::Vector4( (matrix.m11 * vector.x) + (matrix.m12 * vector.y) + (matrix.m13 * vector.z) + (matrix.m14 * vector.w), + (matrix.m21 * vector.x) + (matrix.m22 * vector.y) + (matrix.m23 * vector.z) + (matrix.m24 * vector.w), + (matrix.m23 * vector.x) + (matrix.m32 * vector.y) + (matrix.m33 * vector.z) + (matrix.m34 * vector.w), + (matrix.m41 * vector.x) + (matrix.m42 * vector.y) + (matrix.m43 * vector.z) + (matrix.m44 * vector.w) ); +} + +template +::LinearAlgebra::Vector4 operator * ( const ::LinearAlgebra::Vector4 &vector, const ::LinearAlgebra::Matrix4x4 &matrix ) +{ + return ::LinearAlgebra::Vector4( (vector.x * matrix.m11) + (vector.y * matrix.m21) + (vector.z * matrix.m31) + (vector.w * matrix.m41), + (vector.x * matrix.m12) + (vector.y * matrix.m22) + (vector.z * matrix.m32) + (vector.w * matrix.m42), + (vector.x * matrix.m13) + (vector.y * matrix.m23) + (vector.z * matrix.m33) + (vector.w * matrix.m43), + (vector.x * matrix.m14) + (vector.y * matrix.m24) + (vector.z * matrix.m34) + (vector.w * matrix.m44) ); +} + namespace LinearAlgebra { - // x2 - template - Matrix2x2 operator * ( const Matrix2x2 &left, const Matrix2x2 &right ) - { return Matrix2x2( (left.m11 * right.m11) + (left.m21 * right.m12), (left.m11 * right.m21) + (left.m21 * right.m22), (left.m12 * right.m11) + (left.m22 * right.m12), (left.m12 * right.m21) + (left.m22 * right.m22) ); } - - template - Vector2 operator * ( const Matrix2x2 &matrix, const Vector2 &vector ) - { return Vector2( (matrix.m11 * vector.x) + (matrix.m21 * vector.y), (matrix.m12 * vector.x) + (matrix.m22 * vector.y) ); } - - template - Vector2 operator * ( const Vector2 &vector, const Matrix2x2 &left ) - { return Vector2( (vector.x * matrix.m11) + (vector.y * matrix.m12), (vector.x * matrix.m21) + (vector.y * matrix.m22) ); } - - // x3 - template - Matrix3x3 operator * ( const Matrix3x3 &left, const Matrix3x3 &right ) + // Creates a solution matrix for 'out´= 'targetMem' * 'in'. + // Returns false if there is no explicit solution. + template + bool SuperpositionMatrix( const Matrix2x2 &in, const Matrix2x2 &out, Matrix2x2 &targetMem ) { - Matrix3x3 product, leftT = left.getTranspose(); - for( int i = 0; i < 3; ++i ) for( int j = 0; j < 3; ++j ) - product.m[i][j] = leftT.v[i].dot(right.v[j]); - return product; + ScalarType d = in.GetDeterminant(); + if( d == 0 ) return false; + targetMem = out * (in.GetAdjoint() /= d); + return true; } - template - Vector3 operator * ( const Matrix3x3 &matrix, const Vector3 &vector ) - { return Vector3( (matrix.m11 * vector.x) + (matrix.m21 * vector.y) + (matrix.m31 * vector.z), (matrix.m12 * vector.x) + (matrix.m22 * vector.y) + (matrix.m32 * vector.z), (matrix.m13 * vector.x) + (matrix.m23 * vector.y) + (matrix.m33 * vector.z) ); } - - template - Vector3 operator * ( const Vector3 &vector, const Matrix3x3 &left ) - { return Vector3( (vector.x * matrix.m11) + (vector.y * matrix.m12) + (vector.z * matrix.m13), (vector.x * matrix.m21) + (vector.y * matrix.m22) + (vector.z * matrix.m23), (vector.x * matrix.m31) + (vector.y * matrix.m32) + (vector.z * matrix.m33) ); } - - // x4 - template - Matrix4x4 operator * ( const Matrix4x4 &left, const Matrix4x4 &right ) + // Creates a solution matrix for 'out´= 'targetMem' * 'in'. + // Returns false if there is no explicit solution. + template + bool SuperpositionMatrix( const Matrix3x3 &in, const Matrix3x3 &out, Matrix3x3 &targetMem ) { - Matrix4x4 product, rightT = right.getTranspose(); - for( int i = 0; i < 4; ++i ) - { - product.m[i][0] = left.v[i].dot(rightT.v[0]); - product.m[i][1] = left.v[i].dot(rightT.v[1]); - product.m[i][2] = left.v[i].dot(rightT.v[2]); - product.m[i][3] = left.v[i].dot(rightT.v[3]); - } - return product; + ScalarType d = in.GetDeterminant(); + if( d == 0 ) return false; + targetMem = out * (in.GetAdjoint() /= d); + return true; } - template - Vector4 operator * ( const Matrix4x4 &matrix, const Vector4 &vector ) - { return Vector4( (matrix.m11 * vector.x) + (matrix.m21 * vector.y) + (matrix.m31 * vector.z) + (matrix.m41 * vector.w), (matrix.m12 * vector.x) + (matrix.m22 * vector.y) + (matrix.m32 * vector.z) + (matrix.m42 * vector.w), (matrix.m13 * vector.x) + (matrix.m23 * vector.y) + (matrix.m33 * vector.z) + (matrix.m43 * vector.w), (matrix.m14 * vector.x) + (matrix.m24 * vector.y) + (matrix.m34 * vector.z) + (matrix.m44 * vector.w) ); } - - template // works for column weighted matrixes - Vector4 operator * ( const Vector4 &vector, const Matrix4x4 &matrix ) - { return Vector4( (vector.x * matrix.m11) + (vector.y * matrix.m12) + (vector.z * matrix.m13) + (vector.w * matrix.m14), (vector.x * matrix.m21) + (vector.y * matrix.m22) + (vector.z * matrix.m23) + (vector.w * matrix.m24), (vector.x * matrix.m31) + (vector.y * matrix.m32) + (vector.z * matrix.m33) + (vector.w * matrix.m34), (vector.x * matrix.m41) + (vector.y * matrix.m42) + (vector.z * matrix.m43) + (vector.w * matrix.m44) ); } - - namespace _2D + // Creates a solution matrix for 'out´= 'targetMem' * 'in'. + // Returns false if there is no explicit solution. + template + bool SuperpositionMatrix( const Matrix4x4 &in, const Matrix4x4 &out, Matrix4x4 &targetMem ) { - template - inline void translationMatrix( Matrix3x3 &output, const Vector2 &position ) -// { output = Matrix3x3( 1, 0, position.x, 0, 1, position.y, 0, 0, 1 ); } - { output = Matrix3x3( 1, 0, 0, 0, 1, 0, position.x, position.y, 1 ); } - - template - void rotationMatrix( Matrix2x2 &output, const ElementType &radian ) - { - ElementType s = std::sin( radian ), - c = std::cos( radian ); -// output = Matrix2x2( c, -s, s, c ); - output = Matrix2x2( c, s, -s, c ); - } - - template - void rotationMatrix( Matrix3x3 &output, const ElementType &radian ) - { - ElementType s = std::sin( radian ), - c = std::cos( radian ); -// output = Matrix3x3( c, -s, 0, s, c, 0, 0, 0, 1 ); - output = Matrix3x3( c, s, 0, -s, c, 0, 0, 0, 1 ); - } - - template - void rigidBodyMatrix( Matrix3x3 &output, const ElementType &radian, const Vector2 &position ) - { - ElementType s = std::sin( radian ), - c = std::cos( radian ); -// output = Matrix3x3( c, -s, position.x, s, c, position.y, 0, 0, 1 ); - output = Matrix3x3( c, s, 0, -s, c, 0, position.x, position.y, 1 ); - } - } - - namespace _3D - { - template - inline void translationMatrix( Matrix4x4 &output, const Vector3 &position ) -// { output = Matrix4x4( 1, 0, 0, position.x, 0, 1, 0, position.y, 0, 0, 1, position.z, 0, 0, 0, 1 ); } - { output = Matrix4x4( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, position.x, position.y, position.z, 1 ); } - - template - void inverseRigidBody( Matrix4x4 &output, const Matrix4x4 &rigidBody ) - { - output = Matrix4x4( rigidBody.m11, rigidBody.m21, rigidBody.m31, 0, - rigidBody.m12, rigidBody.m22, rigidBody.m32, 0, - rigidBody.m13, rigidBody.m23, rigidBody.m33, 0, - -rigidBody.v[3].xyz.dot(rigidBody.v[0].xyz), - -rigidBody.v[3].xyz.dot(rigidBody.v[1].xyz), - -rigidBody.v[3].xyz.dot(rigidBody.v[2].xyz), 1 ); - } - - template - void rotationMatrix_AxisX( Matrix3x3 &output, const ElementType &radian ) - { - ElementType s = std::sin( radian ), - c = std::cos( radian ); -// output = Matrix3x3( 1, 0, 0, 0, c, -s, 0, s, c ); - output = Matrix3x3( 1, 0, 0, 0, c, s, 0, -s, c ); - } - - template - void rotationMatrix_AxisX( Matrix4x4 &output, const ElementType &radian ) - { - ElementType s = std::sin( radian ), - c = std::cos( radian ); -// output = Matrix4x4( 1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0, 0, 0, 0, 1 ); - output = Matrix4x4( 1, 0, 0, 0, 0, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1 ); - } - - template - void rotationMatrix_AxisY( Matrix3x3 &output, const ElementType &radian ) - { - ElementType s = std::sin( radian ), - c = std::cos( radian ); -// output = Matrix3x3( c, 0, s, 0, 1, 0, -s, 0, c ); - output = Matrix3x3( c, 0, -s, 0, 1, 0, s, 0, c ); - } - - template - void rotationMatrix_AxisY( Matrix4x4 &output, const ElementType &radian ) - { - ElementType s = std::sin( radian ), - c = std::cos( radian ); -// output = Matrix4x4( c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0, 0, 0, 0, 1 ); - output = Matrix4x4( c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1 ); - } - - template - inline void rotationMatrix_AxisZ( Matrix3x3 &output, const ElementType &radian ) - { ::LinearAlgebra::_2D::rotationMatrix( output, radian ); } - - template - void rotationMatrix_AxisZ( Matrix4x4 &output, const ElementType &radian ) - { - ElementType s = std::sin( radian ), - c = std::cos( radian ); -// output = Matrix4x4( c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ); - output = Matrix4x4( c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ); - } - - template - void rotationMatrix( Matrix4x4 &output, const Vector3 &normalizedAxis, const ElementType &radian ) - { // TODO : Optimize - ElementType r = radian * 0.5f, - s = std::sin( r ), - c = std::cos( r ); - Quaternion q( normalizedAxis * s, c ), - qConj = q.getConjugate(); - - output.v[0] = Vector4( (q*Vector3(1,0,0)*qConj).imaginary, 0 ); - output.v[1] = Vector4( (q*Vector3(0,1,0)*qConj).imaginary, 0 ); - output.v[2] = Vector4( (q*Vector3(0,0,1)*qConj).imaginary, 0 ); - output.v[3] = Vector4( 0, 0, 0, 1 ); - } - - /* - returns a deltaAngularAxis which is a vectorProduct of the movementVector and leverVector. - angular: (1/I) * L, there I is known as the "moment of inertia", L as the "angular momentum vector". - lever: Displacement vector relative to the rotation pivot. - Recommended reading: http://en.wikipedia.org/wiki/Torque - */ - template - inline Vector3 deltaAngularAxis( const Vector3 &movement, const Vector3 &lever ) - { return movement.cross( lever ); } - - template - inline Vector3 particleRotationMovement( const Vector3 &deltaRadian, const Vector3 &lever ) - { return lever.cross(deltaRadian) /= lever.dot(lever); } - - template - inline Vector3 vectorProjection( const Vector3 &vector, const Vector3 &axis ) - { return axis * ( vector.dot(axis) / axis.dot(axis) ); } - - /* - output; is set to a rigibody matrix that revolve/rotate around centerOfMass and then translates. - sumDeltaAngularAxis: Sum of all ( (1/I) * ( L x D ) )-vectorproducts. There I is known as "moment of inertia", L as "angular momentum vector" and D the "lever vector". - sumTranslation: Sum of all the translation vectors. - centerOfMass: The point the particles is to revolve around, prior to translation. Default set to null vector aka origo. - Recommended reading: http://en.wikipedia.org/wiki/Torque - */ - template - void rigidBodyMatrix( Matrix4x4 &output, const Vector3 &sumDeltaAngularAxis, const Vector3 &sumTranslation, const Vector3 ¢erOfMass = Vector3::null ) - { - ElementType deltaRadian = sumDeltaAngularAxis.length(); - if( deltaRadian != 0 ) - { - Vector3 axis = sumDeltaAngularAxis / deltaRadian; - rotationMatrix( output, axis, deltaRadian ); - - output.v[3].xyz = centerOfMass; - output.v[3].x -= centerOfMass.dot( output.v[0].xyz ); - output.v[3].y -= centerOfMass.dot( output.v[1].xyz ); - output.v[3].z -= centerOfMass.dot( output.v[2].xyz ); - } - else output = Matrix4x4::identity; - - output.v[3].xyz += sumTranslation; - } - - /* - output; is set to an orthographic projection matrix. - width; of the projection sample volume. - height; of the projection sample volume. - nearClip: Distance to the nearClippingPlane. - farClip: Distance to the farClippingPlane - */ - template - void projectionMatrix_Orthographic( Matrix4x4 &output, const ElementType &width, const ElementType &height, const ElementType &nearClip, const ElementType &farClip ) - { - ElementType c = 1; - c /= nearClip - farClip; - output = Matrix4x4( 2/width, 0, 0, 0, - 0, 2/height, 0, 0, - 0, 0, -c, 0, 0, - 0, nearClip*c, 1 ); - } - - /* - output; is set to a perspective transform matrix. - vertFoV; is the vertical field of vision in radians. (se FoV Hor+ ) - aspect; is the screenratio width/height (example 16/9 or 16/10 ) - nearClip: Distance to the nearClippingPlane - farClip: Distance to the farClippingPlane - */ - template - void projectionMatrix_Perspective( Matrix4x4 &output, const ElementType &vertFoV, const ElementType &aspect, const ElementType &nearClip, const ElementType &farClip ) - { - ElementType fov = 1 / ::std::tan( vertFoV * 0.5f ), - dDepth = farClip; - dDepth /= farClip - nearClip; - output = Matrix4x4( fov / aspect, 0, 0, 0, 0, fov, 0, 0, 0, 0, dDepth, 1, 0, 0, -(dDepth * nearClip), 0 ); - } + ScalarType d = in.GetDeterminant(); + if( d == 0 ) return false; + targetMem = out * (in.GetAdjoint() /= d); + return true; } } +namespace LinearAlgebra2D +{ + template + inline ::LinearAlgebra::Vector2 X_AxisTo( const ::LinearAlgebra::Vector2 &yAxis ) + { return ::LinearAlgebra::Vector2( yAxis.y, -yAxis.x ); } + + template + inline ::LinearAlgebra::Vector2 Y_AxisTo( const ::LinearAlgebra::Vector2 &xAxis ) + { return ::LinearAlgebra::Vector2( -xAxis.y, xAxis.x ); } + + template + inline ::LinearAlgebra::Matrix3x3 & TranslationMatrix( const ::LinearAlgebra::Vector2 &position, ::LinearAlgebra::Matrix3x3 &targetMem = ::LinearAlgebra::Matrix3x3() ) + { + return targetMem = ::LinearAlgebra::Matrix3x3( 1, 0, position.x, + 0, 1, position.y, + 0, 0, 1 ); + } + + template + inline ::LinearAlgebra::Matrix2x2 & RotationMatrix( const ScalarType &radian, ::LinearAlgebra::Matrix2x2 &targetMem = ::LinearAlgebra::Matrix2x2() ) + { + ScalarType s = ::std::sin( radian ), + c = ::std::cos( radian ); + return targetMem = ::LinearAlgebra::Matrix2x2( c, -s, s, c ); + } + + template + inline ::LinearAlgebra::Matrix3x3 & RotationMatrix( const ScalarType &radian, ::LinearAlgebra::Matrix3x3 &targetMem = ::LinearAlgebra::Matrix3x3() ) + { + ScalarType s = ::std::sin( radian ), + c = ::std::cos( radian ); + return targetMem = ::LinearAlgebra::Matrix3x3( c,-s, 0, s, c, 0, 0, 0, 1 ); + } + + template + inline ::LinearAlgebra::Matrix3x3 & OrientationMatrix( const ScalarType &radian, const ::LinearAlgebra::Vector2 &position, ::LinearAlgebra::Matrix3x3 &targetMem = ::LinearAlgebra::Matrix3x3() ) + { + ScalarType s = ::std::sin( radian ), + c = ::std::cos( radian ); + return targetMem = ::LinearAlgebra::Matrix3x3( c,-s, position.x, + s, c, position.y, + 0, 0, 1 ); + } + + template + inline ::LinearAlgebra::Matrix3x3 & OrientationMatrix( const ::LinearAlgebra::Vector2 &lookAt, const ::LinearAlgebra::Vector2 &position, ::LinearAlgebra::Matrix3x3 &targetMem = ::LinearAlgebra::Matrix3x3() ) + { + ::LinearAlgebra::Vector2 direction = ( lookAt - position ).GetNormalized(); + return targetMem = ::LinearAlgebra::Matrix3x3( ::LinearAlgebra::Vector3( X_AxisTo(direction), 0.0f ), + ::LinearAlgebra::Vector3( direction, 0.0f ), + ::LinearAlgebra::Vector3( position, 1.0f ) ); + } + + template + inline ::LinearAlgebra::Matrix3x3 & OrientationMatrix( const ScalarType &radian, const ::LinearAlgebra::Vector2 &position, const ::LinearAlgebra::Vector2 ¢erOfRotation, ::LinearAlgebra::Matrix3x3 &targetMem = ::LinearAlgebra::Matrix3x3() ) + { // TODO: not tested + RotationMatrix( radian, targetMem ); + targetMem.v[2].xy = position + centerOfRotation; + targetMem.v[2].x -= ::LinearAlgebra::Vector2( targetMem.v[0].x, targetMem.v[1].x ).Dot( centerOfRotation ); + targetMem.v[2].y -= ::LinearAlgebra::Vector2( targetMem.v[0].y, targetMem.v[1].y ).Dot( centerOfRotation ); + return targetMem; + } + + template + inline ::LinearAlgebra::Matrix3x3 & InverseOrientationMatrix( const ::LinearAlgebra::Matrix3x3 &orientationMatrix, ::LinearAlgebra::Matrix3x3 &targetMem = ::LinearAlgebra::Matrix3x3() ) + { + return targetMem = ::LinearAlgebra::Matrix3x3( orientationMatrix.m11, orientationMatrix.m21, -orientationMatrix.v[0].xy.Dot(orientationMatrix.v[2].xy), + orientationMatrix.m12, orientationMatrix.m22, -orientationMatrix.v[1].xy.Dot(orientationMatrix.v[2].xy), + 0, 0, 1 ); + } +} + +namespace LinearAlgebra3D +{ + template + inline ::LinearAlgebra::Matrix4x4 & TranslationMatrix( const ::LinearAlgebra::Vector3 &position, ::LinearAlgebra::Matrix4x4 &targetMem = ::LinearAlgebra::Matrix4x4() ) + { + return targetMem = ::LinearAlgebra::Matrix4x4( 1, 0, 0, position.x, + 0, 1, 0, position.y, + 0, 0, 1, position.z, + 0, 0, 0, 1 ); + } + + template + inline ::LinearAlgebra::Matrix3x3 & RotationMatrix_AxisX( const ScalarType &radian, ::LinearAlgebra::Matrix3x3 &targetMem = ::LinearAlgebra::Matrix3x3() ) + { + ScalarType s = std::sin( radian ), + c = std::cos( radian ); + return targetMem = ::LinearAlgebra::Matrix3x3( 1, 0, 0, + 0, c,-s, + 0, s, c ); + } + + template + inline ::LinearAlgebra::Matrix4x4 & RotationMatrix_AxisX( const ScalarType &radian, ::LinearAlgebra::Matrix4x4 &targetMem = ::LinearAlgebra::Matrix4x4() ) + { + ScalarType s = std::sin( radian ), + c = std::cos( radian ); + return targetMem = ::LinearAlgebra::Matrix4x4( 1, 0, 0, 0, + 0, c,-s, 0, + 0, s, c, 0, + 0, 0, 0, 1 ); + } + + template + inline ::LinearAlgebra::Matrix3x3 & RotationMatrix_AxisY( const ScalarType &radian, ::LinearAlgebra::Matrix3x3 &targetMem = ::LinearAlgebra::Matrix3x3() ) + { + ScalarType s = std::sin( radian ), + c = std::cos( radian ); + return targetMem = ::LinearAlgebra::Matrix3x3( c, 0, s, + 0, 1, 0, + -s, 0, c ); + } + + template + inline ::LinearAlgebra::Matrix4x4 & RotationMatrix_AxisY( const ScalarType &radian, ::LinearAlgebra::Matrix4x4 &targetMem = ::LinearAlgebra::Matrix4x4() ) + { + ScalarType s = std::sin( radian ), + c = std::cos( radian ); + return targetMem = ::LinearAlgebra::Matrix4x4( c, 0, s, 0, + 0, 1, 0, 0, + -s, 0, c, 0, + 0, 0, 0, 1 ); + } + + template + inline ::LinearAlgebra::Matrix3x3 & RotationMatrix_AxisZ( const ScalarType &radian, ::LinearAlgebra::Matrix3x3 &targetMem = ::LinearAlgebra::Matrix3x3() ) + { return ::LinearAlgebra2D::RotationMatrix( radian, targetMem ); } + + template + inline ::LinearAlgebra::Matrix4x4 & RotationMatrix_AxisZ( const ScalarType &radian, ::LinearAlgebra::Matrix4x4 &targetMem = ::LinearAlgebra::Matrix4x4() ) + { + ScalarType s = std::sin( radian ), + c = std::cos( radian ); + return targetMem = ::LinearAlgebra::Matrix4x4( c,-s, 0, 0, + s, c, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 ); + } + + template + ::LinearAlgebra::Matrix4x4 & RotationMatrix( const ::LinearAlgebra::Vector3 &normalizedAxis, const ScalarType &radian, ::LinearAlgebra::Matrix4x4 &targetMem ) + { /// TODO: not verified + ScalarType r = radian * 0.5f, + s = std::sin( r ), + c = std::cos( r ); + ::LinearAlgebra::Quaternion q( normalizedAxis * s, c ), + qConj = q.GetConjugate(); + + targetMem.v[0] = ::LinearAlgebra::Vector4( (q*::LinearAlgebra::Vector3(1,0,0)*qConj).imaginary, 0 ); + targetMem.v[1] = ::LinearAlgebra::Vector4( (q*::LinearAlgebra::Vector3(0,1,0)*qConj).imaginary, 0 ); + targetMem.v[2] = ::LinearAlgebra::Vector4( (q*::LinearAlgebra::Vector3(0,0,1)*qConj).imaginary, 0 ); + targetMem.v[3] = ::LinearAlgebra::Vector4::standard_unit_W; + return targetMem; + } + + template + ::LinearAlgebra::Matrix4x4 & OrientationMatrix( const ::LinearAlgebra::Vector3 &sumDeltaAngularAxis, const ::LinearAlgebra::Vector3 &sumTranslation, ::LinearAlgebra::Matrix4x4 &targetMem = ::LinearAlgebra::Matrix4x4() ) + { /// TODO: not tested + ScalarType radian = sumDeltaAngularAxis.Dot( sumDeltaAngularAxis ); + if( radian > 0 ) + { + radian = ::std::sqrt( radian ); + ::LinearAlgebra::Vector3 axis = sumDeltaAngularAxis / radian; + RotationMatrix( axis, radian, targetMem ); + } + else targetMem = ::LinearAlgebra::Matrix4x4::identity; + + targetMem.v[3].xyz = sumTranslation; + return targetMem; + } + + template + ::LinearAlgebra::Matrix4x4 & OrientationMatrix( const ::LinearAlgebra::Vector3 &sumDeltaAngularAxis, const ::LinearAlgebra::Vector3 &sumTranslation, const ::LinearAlgebra::Vector3 ¢erOfRotation, ::LinearAlgebra::Matrix4x4 &targetMem = ::LinearAlgebra::Matrix4x4() ) + { /// TODO: not tested + ScalarType radian = sumDeltaAngularAxis.Dot( sumDeltaAngularAxis ); + if( radian > 0 ) + { + radian = ::std::sqrt( radian ); + ::LinearAlgebra::Vector3 axis = sumDeltaAngularAxis / radian; + RotationMatrix( axis, radian, targetMem ); + } + else targetMem = ::LinearAlgebra::Matrix4x4::identity; + + targetMem.v[3].xyz = sumTranslation + centerOfRotation; + targetMem.v[3].x -= ::LinearAlgebra::Vector3( targetMem.v[0].x, targetMem.v[1].x, targetMem.v[2].x ).Dot( centerOfRotation ); + targetMem.v[3].y -= ::LinearAlgebra::Vector3( targetMem.v[0].y, targetMem.v[1].y, targetMem.v[2].y ).Dot( centerOfRotation ); + targetMem.v[3].z -= ::LinearAlgebra::Vector3( targetMem.v[0].z, targetMem.v[1].z, targetMem.v[2].z ).Dot( centerOfRotation ); + + return targetMem; + } + + template + inline ::LinearAlgebra::Matrix4x4 & InverseOrientationMatrix( const ::LinearAlgebra::Matrix4x4 &orientationMatrix, ::LinearAlgebra::Matrix4x4 &targetMem = ::LinearAlgebra::Matrix4x4() ) + { + return targetMem = ::LinearAlgebra::Matrix4x4( orientationMatrix.m11, orientationMatrix.m21, orientationMatrix.m31, -orientationMatrix.v[0].xyz.Dot(orientationMatrix.v[3].xyz), + orientationMatrix.m12, orientationMatrix.m22, orientationMatrix.m32, -orientationMatrix.v[1].xyz.Dot(orientationMatrix.v[3].xyz), + orientationMatrix.m13, orientationMatrix.m23, orientationMatrix.m33, -orientationMatrix.v[2].xyz.Dot(orientationMatrix.v[3].xyz), + 0, 0, 0, 1 ); + } + + /* Creates an orthographic projection matrix designed for DirectX enviroment. + width; of the projection sample volume. + height; of the projection sample volume. + nearClip: Distance to the nearClippingPlane. + farClip: Distance to the farClippingPlane + targetMem; is set to an orthographic projection matrix and then returned. + */ + template + ::LinearAlgebra::Matrix4x4 & ProjectionMatrix_Orthographic( const ScalarType &width, const ScalarType &height, const ScalarType &nearClip, const ScalarType &farClip, ::LinearAlgebra::Matrix4x4 &targetMem = ::LinearAlgebra::Matrix4x4() ) + { /// TODO: not tested + ScalarType c = 1 / (nearClip - farClip); + return targetMem = ::LinearAlgebra::Matrix4x4( 2/width, 0, 0, 0, + 0, 2/height, 0, 0, + 0, 0, -c, 0, 0, + 0, nearClip*c, 1 ); + } + + /* Creates a perspective projection matrix designed for DirectX enviroment. + vertFoV; is the vertical field of vision in radians. (lookup FoV Hor+ ) + aspect; is the screenratio width/height (example 16/9 or 16/10 ) + nearClip: Distance to the nearClippingPlane + farClip: Distance to the farClippingPlane + targetMem; is set to a perspective transform matrix and then returned. + */ + template + ::LinearAlgebra::Matrix4x4 & ProjectionMatrix_Perspective( const ScalarType &vertFoV, const ScalarType &aspect, const ScalarType &nearClip, const ScalarType &farClip, ::LinearAlgebra::Matrix4x4 &targetMem = ::LinearAlgebra::Matrix4x4() ) + { /// TODO: not tested + ScalarType fov = 1 / ::std::tan( vertFoV * 0.5f ), + dDepth = farClip / (farClip - nearClip); + return targetMem = ::LinearAlgebra::Matrix4x4( fov / aspect, 0, 0, 0, + 0, fov, 0, 0, + 0, 0, dDepth, -(dDepth * nearClip), + 0, 0, 1, 0 ); + } + + template + inline ::LinearAlgebra::Vector3 VectorProjection( const ::LinearAlgebra::Vector3 &vector, const ::LinearAlgebra::Vector3 &axis ) + { return axis * ( vector.Dot(axis) / axis.Dot(axis) ); } +} + +#include "Utilities.h" + +namespace Utility { namespace Value +{ // Utility Value Specializations + + template + inline ::LinearAlgebra::Vector2 Abs( const ::LinearAlgebra::Vector2 &vector ) + { return ::LinearAlgebra::Vector2( Abs(vector.x), Abs(vector.y) ); } + + template + inline ::LinearAlgebra::Vector3 Abs( const ::LinearAlgebra::Vector3 &vector ) + { return ::LinearAlgebra::Vector3( Abs(vector.xy), Abs(vector.z) ); } + + template + inline ::LinearAlgebra::Vector4 Abs( const ::LinearAlgebra::Vector4 &vector ) + { return ::LinearAlgebra::Vector4( Abs(vector.xyz), Abs(vector.w) ); } + + template + inline ::LinearAlgebra::Matrix2x2 Abs( const ::LinearAlgebra::Matrix2x2 &matrix ) + { return ::LinearAlgebra::Matrix2x2( Abs(matrix.v[0]), Abs(matrix.v[1]) ); } + + template + inline ::LinearAlgebra::Matrix3x3 Abs( const ::LinearAlgebra::Matrix3x3 &matrix ) + { return ::LinearAlgebra::Matrix3x3( Abs(matrix.v[0]), Abs(matrix.v[1]), Abs(matrix.v[2]) ); } + + template + inline ::LinearAlgebra::Matrix4x4 Abs( const ::LinearAlgebra::Matrix4x4 &matrix ) + { return ::LinearAlgebra::Matrix4x4( Abs(matrix.v[0]), Abs(matrix.v[1]), Abs(matrix.v[2]), Abs(matrix.v[3]) ); } + + template + inline ::LinearAlgebra::Vector2 Max( const ::LinearAlgebra::Vector2 &vectorA, const ::LinearAlgebra::Vector2 &vectorB ) + { return ::LinearAlgebra::Vector2( Max(vectorA.x, vectorB.x), Max(vectorA.y, vectorB.y) ); } + + template + inline ::LinearAlgebra::Vector3 Max( const ::LinearAlgebra::Vector3 &vectorA, const ::LinearAlgebra::Vector3 &vectorB ) + { return ::LinearAlgebra::Vector3( Max(vectorA.xy, vectorB.xy), Max(vectorA.z, vectorB.z) ); } + + template + inline ::LinearAlgebra::Vector4 Max( const ::LinearAlgebra::Vector4 &vectorA, const ::LinearAlgebra::Vector4 &vectorB ) + { return ::LinearAlgebra::Vector4( Max(vectorA.xyz, vectorB.xyz), Max(vectorA.w, vectorB.w) ); } + + template + inline ::LinearAlgebra::Matrix2x2 Max( const ::LinearAlgebra::Matrix2x2 &matrixA, const ::LinearAlgebra::Matrix2x2 &matrixB ) + { return ::LinearAlgebra::Matrix2x2( Max(matrixA.v[0], matrixB.v[0]), Max(matrixA.v[1], matrixB.v[1]) ); } + + template + inline ::LinearAlgebra::Matrix3x3 Max( const ::LinearAlgebra::Matrix3x3 &matrixA, const ::LinearAlgebra::Matrix3x3 &matrixB ) + { return ::LinearAlgebra::Matrix3x3( Max(matrixA.v[0], matrixB.v[0]), Max(matrixA.v[1], matrixB.v[1]), Max(matrixA.v[2], matrixB.v[2]) ); } + + template + inline ::LinearAlgebra::Matrix4x4 Max( const ::LinearAlgebra::Matrix4x4 &matrixA, const ::LinearAlgebra::Matrix4x4 &matrixB ) + { return ::LinearAlgebra::Matrix4x4( Max(matrixA.v[0], matrixB.v[0]), Max(matrixA.v[1], matrixB.v[1]), Max(matrixA.v[2], matrixB.v[2]), Max(matrixA.v[3], matrixB.v[3]) ); } + + template + inline ::LinearAlgebra::Vector2 Min( const ::LinearAlgebra::Vector2 &vectorA, const ::LinearAlgebra::Vector2 &vectorB ) + { return ::LinearAlgebra::Vector2( Min(vectorA.x, vectorB.x), Min(vectorA.y, vectorB.y) ); } + + template + inline ::LinearAlgebra::Vector3 Min( const ::LinearAlgebra::Vector3 &vectorA, const ::LinearAlgebra::Vector3 &vectorB ) + { return ::LinearAlgebra::Vector3( Min(vectorA.xy, vectorB.xy), Min(vectorA.z, vectorB.z) ); } + + template + inline ::LinearAlgebra::Vector4 Min( const ::LinearAlgebra::Vector4 &vectorA, const ::LinearAlgebra::Vector4 &vectorB ) + { return ::LinearAlgebra::Vector4( Min(vectorA.xyz, vectorB.xyz), Min(vectorA.w, vectorB.w) ); } + + template + inline ::LinearAlgebra::Matrix2x2 Min( const ::LinearAlgebra::Matrix2x2 &matrixA, const ::LinearAlgebra::Matrix2x2 &matrixB ) + { return ::LinearAlgebra::Matrix2x2( Min(matrixA.v[0], matrixB.v[0]), Min(matrixA.v[1], matrixB.v[1]) ); } + + template + inline ::LinearAlgebra::Matrix3x3 Min( const ::LinearAlgebra::Matrix3x3 &matrixA, const ::LinearAlgebra::Matrix3x3 &matrixB ) + { return ::LinearAlgebra::Matrix3x3( Min(matrixA.v[0], matrixB.v[0]), Min(matrixA.v[1], matrixB.v[1]), Min(matrixA.v[2], matrixB.v[2]) ); } + + template + inline ::LinearAlgebra::Matrix4x4 Min( const ::LinearAlgebra::Matrix4x4 &matrixA, const ::LinearAlgebra::Matrix4x4 &matrixB ) + { return ::LinearAlgebra::Matrix4x4( Min(matrixA.v[0], matrixB.v[0]), Min(matrixA.v[1], matrixB.v[1]), Min(matrixA.v[2], matrixB.v[2]), Min(matrixA.v[3], matrixB.v[3]) ); } +} } + #endif \ No newline at end of file diff --git a/OysterMath/Matrix.h b/OysterMath/Matrix.h index ba7b5767..c5c8735e 100644 --- a/OysterMath/Matrix.h +++ b/OysterMath/Matrix.h @@ -1,9 +1,8 @@ ///////////////////////////////////////////////////////////////////// -// Linear Math Matrixes +// Linear Math Matrixes (Column weighted) // © Dan Andersson 2013 ///////////////////////////////////////////////////////////////////// -#pragma once #ifndef LINEARALGEBRA_MATRIX_H #define LINEARALGEBRA_MATRIX_H @@ -12,398 +11,399 @@ namespace LinearAlgebra { - template - class Matrix2x2 + template + struct Matrix2x2 { public: union { - ElementType m[2][2]; - struct{ Vector2 v[2]; }; -// struct{ ElementType m11, m21, m12, m22; }; - struct{ ElementType m11, m12, m21, m22; }; - ElementType element[4]; - char byte[sizeof(ElementType[4])]; + ScalarType m[2][2]; + struct{ Vector2 v[2]; }; + struct{ ScalarType m11, m21, m12, m22; }; + ScalarType element[4]; + char byte[sizeof(ScalarType[4])]; }; - static const Matrix2x2 identity, null; + static const Matrix2x2 identity, null; Matrix2x2( ); - Matrix2x2( const ElementType &m11, const ElementType &m12, - const ElementType &m21, const ElementType &m22 ); - Matrix2x2( const Vector2 vec[2] ); - Matrix2x2( const Vector2 &vec1, const Vector2 &vec2 ); - Matrix2x2( const ElementType element[4] ); - Matrix2x2( const Matrix2x2 &matrix ); + Matrix2x2( const ScalarType &m11, const ScalarType &m12, + const ScalarType &m21, const ScalarType &m22 ); + Matrix2x2( const Vector2 vec[2] ); + Matrix2x2( const Vector2 &vec1, const Vector2 &vec2 ); + explicit Matrix2x2( const ScalarType element[4] ); + Matrix2x2( const Matrix2x2 &matrix ); - operator ElementType* ( ); - operator const ElementType* ( ) const; + operator ScalarType* ( ); + operator const ScalarType* ( ) const; - Matrix2x2 & operator = ( const Vector2 vec[2] ); - Matrix2x2 & operator = ( const ElementType element[4] ); - Matrix2x2 & operator = ( const Matrix2x2 &matrix ); - Matrix2x2 & operator += ( const Matrix2x2 &matrix ); - Matrix2x2 & operator -= ( const Matrix2x2 &matrix ); - Matrix2x2 & operator *= ( const ElementType &scalar ); - Matrix2x2 & operator /= ( const ElementType &scalar ); - Matrix2x2 operator + ( const Matrix2x2 &matrix ) const; - Matrix2x2 operator - ( const Matrix2x2 &matrix ) const; - Matrix2x2 operator * ( const ElementType &scalar ) const; - Matrix2x2 operator / ( const ElementType &scalar ) const; - Matrix2x2 operator - ( ) const; // unary negation + Matrix2x2 & operator = ( const Vector2 vec[2] ); + Matrix2x2 & operator = ( const Matrix2x2 &matrix ); + Matrix2x2 & operator += ( const Matrix2x2 &matrix ); + Matrix2x2 & operator -= ( const Matrix2x2 &matrix ); + Matrix2x2 & operator *= ( const ScalarType &scalar ); + Matrix2x2 & operator /= ( const ScalarType &scalar ); + Matrix2x2 operator + ( const Matrix2x2 &matrix ) const; + Matrix2x2 operator - ( const Matrix2x2 &matrix ) const; + Matrix2x2 operator * ( const ScalarType &scalar ) const; + Matrix2x2 operator / ( const ScalarType &scalar ) const; + Matrix2x2 operator - ( ) const; // unary negation - ElementType getDeterminant( ) const; - Matrix2x2 getAdjoint( ) const; - Matrix2x2 getTranspose( ) const; - Matrix2x2 & transpose( ); - Matrix2x2 getInverse( ) const; - Matrix2x2 getInverse( ElementType &determinant ) const; - Matrix2x2 & invert( ); - Matrix2x2 & invert( ElementType &determinant ); - Vector2 getRowVector( unsigned int rowID ) const; - const Vector2 & getColumnVector( unsigned int colID ) const; + ScalarType GetDeterminant( ) const; + Matrix2x2 GetAdjoint( ) const; + Matrix2x2 GetTranspose( ) const; + Matrix2x2 & Transpose( ); + Matrix2x2 GetInverse( ) const; + Matrix2x2 GetInverse( ScalarType &determinant ) const; + Matrix2x2 & Invert( ); + Matrix2x2 & Invert( ScalarType &determinant ); + Vector2 GetRowVector( unsigned int rowID ) const; + Vector2 GetColumnVector( unsigned int colID ) const; }; - template - class Matrix3x3 + template + struct Matrix3x3 { public: union { - ElementType m[3][3]; - struct{ Vector3 v[3]; }; -// struct{ ElementType m11, m21, m31, m12, m22, m32, m13, m23, m33; }; - struct{ ElementType m11, m12, m13, m21, m22, m23, m31, m32, m33; }; - ElementType element[9]; - char byte[sizeof(ElementType[9])]; + ScalarType m[3][3]; + struct{ Vector3 v[3]; }; + struct{ ScalarType m11, m21, m31, m12, m22, m32, m13, m23, m33; }; + ScalarType element[9]; + char byte[sizeof(ScalarType[9])]; }; - static const Matrix3x3 identity, null; + static const Matrix3x3 identity, null; Matrix3x3( ); - Matrix3x3( const ElementType &m11, const ElementType &m12, const ElementType &m13, - const ElementType &m21, const ElementType &m22, const ElementType &m23, - const ElementType &m31, const ElementType &m32, const ElementType &m33 ); - Matrix3x3( const Vector3 vec[3] ); - Matrix3x3( const Vector3 &vec1, const Vector3 &vec2, const Vector3 &vec3 ); - Matrix3x3( const ElementType element[9] ); - Matrix3x3( const Matrix3x3 &matrix ); + Matrix3x3( const ScalarType &m11, const ScalarType &m12, const ScalarType &m13, + const ScalarType &m21, const ScalarType &m22, const ScalarType &m23, + const ScalarType &m31, const ScalarType &m32, const ScalarType &m33 ); + Matrix3x3( const Vector3 vec[3] ); + Matrix3x3( const Vector3 &vec1, const Vector3 &vec2, const Vector3 &vec3 ); + explicit Matrix3x3( const ScalarType element[9] ); + Matrix3x3( const Matrix3x3 &matrix ); - operator ElementType* ( ); - operator const ElementType* ( ) const; + operator ScalarType* ( ); + operator const ScalarType* ( ) const; - Matrix3x3 & operator = ( const Vector3 vec[3] ); - Matrix3x3 & operator = ( const ElementType element[9] ); - Matrix3x3 & operator = ( const Matrix3x3 &matrix ); - Matrix3x3 & operator += ( const Matrix3x3 &matrix ); - Matrix3x3 & operator -= ( const Matrix3x3 &matrix ); - Matrix3x3 & operator *= ( const ElementType &scalar ); - Matrix3x3 & operator /= ( const ElementType &scalar ); - Matrix3x3 operator + ( const Matrix3x3 &matrix ) const; - Matrix3x3 operator - ( const Matrix3x3 &matrix ) const; - Matrix3x3 operator * ( const ElementType &scalar ) const; - Matrix3x3 operator / ( const ElementType &scalar ) const; - Matrix3x3 operator - ( ) const; // unary negation + Matrix3x3 & operator = ( const Vector3 vec[3] ); + Matrix3x3 & operator = ( const Matrix3x3 &matrix ); + Matrix3x3 & operator += ( const Matrix3x3 &matrix ); + Matrix3x3 & operator -= ( const Matrix3x3 &matrix ); + Matrix3x3 & operator *= ( const ScalarType &scalar ); + Matrix3x3 & operator /= ( const ScalarType &scalar ); + Matrix3x3 operator + ( const Matrix3x3 &matrix ) const; + Matrix3x3 operator - ( const Matrix3x3 &matrix ) const; + Matrix3x3 operator * ( const ScalarType &scalar ) const; + Matrix3x3 operator / ( const ScalarType &scalar ) const; + Matrix3x3 operator - ( ) const; // unary negation - ElementType getDeterminant( ) const; - Matrix3x3 getAdjoint( ) const; - Matrix3x3 getTranspose( ) const; - Matrix3x3 & transpose( ); - Matrix3x3 getInverse( ) const; - Matrix3x3 getInverse( ElementType &determinant ) const; - Matrix3x3 & invert( ); - Matrix3x3 & invert( ElementType &determinant ); - Vector3 getRowVector( unsigned int rowID ) const; - const Vector3 & getColumnVector( unsigned int colID ) const; + ScalarType GetDeterminant( ) const; + Matrix3x3 GetAdjoint( ) const; + Matrix3x3 GetTranspose( ) const; + Matrix3x3 & Transpose( ); + Matrix3x3 GetInverse( ) const; + Matrix3x3 GetInverse( ScalarType &determinant ) const; + Matrix3x3 & Invert( ); + Matrix3x3 & Invert( ScalarType &determinant ); + Vector3 GetRowVector( unsigned int rowID ) const; + Vector3 GetColumnVector( unsigned int colID ) const; }; - template - class Matrix4x4 + template + struct Matrix4x4 { public: union { - ElementType m[4][4]; - struct{ Vector4 v[4]; }; -// struct{ ElementType m11, m21, m31, m41, m12, m22, m32, m42, m13, m23, m33, m43, m14, m24, m34, m44; }; - struct{ ElementType m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44; }; - ElementType element[16]; - char byte[sizeof(ElementType[16])]; + ScalarType m[4][4]; + struct{ Vector4 v[4]; }; + struct{ ScalarType m11, m21, m31, m41, m12, m22, m32, m42, m13, m23, m33, m43, m14, m24, m34, m44; }; + ScalarType element[16]; + char byte[sizeof(ScalarType[16])]; }; - static const Matrix4x4 identity, null; + static const Matrix4x4 identity, null; Matrix4x4( ); - Matrix4x4( const ElementType &m11, const ElementType &m12, const ElementType &m13, const ElementType &m14, - const ElementType &m21, const ElementType &m22, const ElementType &m23, const ElementType &m24, - const ElementType &m31, const ElementType &m32, const ElementType &m33, const ElementType &m34, - const ElementType &m41, const ElementType &m42, const ElementType &m43, const ElementType &m44 ); - Matrix4x4( const Vector4 vec[4] ); - Matrix4x4( const Vector4 &vec1, const Vector4 &vec2, const Vector4 &vec3, const Vector4 &vec4 ); - Matrix4x4( const ElementType element[16] ); - Matrix4x4( const Matrix4x4 &matrix ); + Matrix4x4( const ScalarType &m11, const ScalarType &m12, const ScalarType &m13, const ScalarType &m14, + const ScalarType &m21, const ScalarType &m22, const ScalarType &m23, const ScalarType &m24, + const ScalarType &m31, const ScalarType &m32, const ScalarType &m33, const ScalarType &m34, + const ScalarType &m41, const ScalarType &m42, const ScalarType &m43, const ScalarType &m44 ); + Matrix4x4( const Vector4 vec[4] ); + Matrix4x4( const Vector4 &vec1, const Vector4 &vec2, const Vector4 &vec3, const Vector4 &vec4 ); + explicit Matrix4x4( const ScalarType element[16] ); + Matrix4x4( const Matrix4x4 &matrix ); - operator ElementType* ( ); - operator const ElementType* ( ) const; + operator ScalarType* ( ); + operator const ScalarType* ( ) const; - Matrix4x4 & operator = ( const Vector4 vec[4] ); - Matrix4x4 & operator = ( const ElementType element[16] ); - Matrix4x4 & operator = ( const Matrix4x4 &matrix ); - Matrix4x4 & operator += ( const Matrix4x4 &matrix ); - Matrix4x4 & operator -= ( const Matrix4x4 &matrix ); - Matrix4x4 & operator *= ( const ElementType &scalar ); - Matrix4x4 & operator /= ( const ElementType &scalar ); - Matrix4x4 operator + ( const Matrix4x4 &matrix ) const; - Matrix4x4 operator - ( const Matrix4x4 &matrix ) const; - Matrix4x4 operator * ( const ElementType &scalar ) const; - Matrix4x4 operator / ( const ElementType &scalar ) const; - Matrix4x4 operator - ( ) const; // unary negation + Matrix4x4 & operator = ( const Vector4 vec[4] ); + Matrix4x4 & operator = ( const Matrix4x4 &matrix ); + Matrix4x4 & operator += ( const Matrix4x4 &matrix ); + Matrix4x4 & operator -= ( const Matrix4x4 &matrix ); + Matrix4x4 & operator *= ( const ScalarType &scalar ); + Matrix4x4 & operator /= ( const ScalarType &scalar ); + Matrix4x4 operator + ( const Matrix4x4 &matrix ) const; + Matrix4x4 operator - ( const Matrix4x4 &matrix ) const; + Matrix4x4 operator * ( const ScalarType &scalar ) const; + Matrix4x4 operator / ( const ScalarType &scalar ) const; + Matrix4x4 operator - ( ) const; // unary negation - ElementType getDeterminant( ) const; - Matrix4x4 getAdjoint( ) const; - Matrix4x4 getTranspose( ) const; - Matrix4x4 & transpose( ); - Matrix4x4 getInverse( ) const; - Matrix4x4 getInverse( ElementType &determinant ) const; - Matrix4x4 & invert( ); - Matrix4x4 & invert( ElementType &determinant ); - Vector4 getRowVector( unsigned int rowID ) const; - const Vector4 & getColumnVector( unsigned int colID ) const; + ScalarType GetDeterminant( ) const; + Matrix4x4 GetAdjoint( ) const; + Matrix4x4 GetTranspose( ) const; + Matrix4x4 & Transpose( ); + Matrix4x4 GetInverse( ) const; + Matrix4x4 GetInverse( ScalarType &determinant ) const; + Matrix4x4 & Invert( ); + Matrix4x4 & Invert( ScalarType &determinant ); + Vector4 GetRowVector( unsigned int rowID ) const; + const Vector4 & GetColumnVector( unsigned int colID ) const; }; + /////////////////////////////////////////////////////////////////////////////////// // Body /////////////////////////////////////////////////////////////////////////////////// -// Matrix2x2 /////////////////////////////////////// - template - const Matrix2x2 Matrix2x2::identity = Matrix2x2( 1, 0, 0, 1 ); +// Matrix2x2 /////////////////////////////////////// - template - const Matrix2x2 Matrix2x2::null = Matrix2x2( 0, 0, 0, 0 ); + template + const Matrix2x2 Matrix2x2::identity = Matrix2x2( 1, 0, 0, 1 ); - template - Matrix2x2::Matrix2x2( ) : m11(0), m21(0), m12(0), m22(0) {} + template + const Matrix2x2 Matrix2x2::null = Matrix2x2( 0, 0, 0, 0 ); + + template + Matrix2x2::Matrix2x2( ) : m11(), m21(), m12(), m22() {} - template - Matrix2x2::Matrix2x2( const ElementType &_m11, const ElementType &_m12, const ElementType &_m21, const ElementType &_m22 ) - : m11(_m11), m21(_m21), m12(_m12), m22(_m22) {} + template + Matrix2x2::Matrix2x2( const ScalarType &_m11, const ScalarType &_m12, const ScalarType &_m21, const ScalarType &_m22 ) + { + this->m11 = _m11; this->m12 = _m12; + this->m21 = _m21; this->m22 = _m22; + } - template - Matrix2x2::Matrix2x2( const Vector2 vec[2] ) - : m11(vec[0].x), m21(vec[0].y), m12(vec[1].x), m22(vec[1].y) {} + template + Matrix2x2::Matrix2x2( const Vector2 vec[2] ) + { this->v[0] = vec[0]; this->v[1] = vec[1]; } - template - Matrix2x2::Matrix2x2( const Vector2 &vec1, const Vector2 &vec2 ) - : m11(vec1.x), m21(vec1.y), m12(vec2.x), m22(vec2.y) {} + template + Matrix2x2::Matrix2x2( const Vector2 &vec1, const Vector2 &vec2 ) + { this->v[0] = vec1; this->v[1] = vec2; } - template - Matrix2x2::Matrix2x2( const ElementType _element[4] ) -// : m11(_element[0]), m21(_element[1]), m12(_element[2]), m22(_element[3]) {} - : m11(_element[0]), m12(_element[1]), m21(_element[2]), m22(_element[3]) {} + template + Matrix2x2::Matrix2x2( const ScalarType _element[4] ) + { + this->m11 = _element[0]; this->m12 = _element[1]; + this->m21 = _element[2]; this->m22 = _element[3]; + } - template - Matrix2x2::Matrix2x2( const Matrix2x2 &matrix ) - : m11(matrix.m11), m21(matrix.m12), m12(matrix.m21), m22(matrix.m22) {} + template + Matrix2x2::Matrix2x2( const Matrix2x2 &matrix ) + { this->v[0] = matrix.v[0]; this->v[1] = matrix.v[1]; } - template - inline Matrix2x2::operator ElementType* ( ) + template + inline Matrix2x2::operator ScalarType* ( ) { return this->element; } - template - inline Matrix2x2::operator const ElementType* ( ) const + template + inline Matrix2x2::operator const ScalarType* ( ) const { return this->element; } - template - Matrix2x2 & Matrix2x2::operator = ( const Vector2 vec[2] ) + template + Matrix2x2 & Matrix2x2::operator = ( const Vector2 vec[2] ) { this->v[0] = vec[0]; this->v[1] = vec[1]; return *this; } - template - Matrix2x2 & Matrix2x2::operator = ( const ElementType element[4] ) - { - for( int i = 0; i < 4; ++i ) - this->element[i] = element[i]; - return *this; - } - - template - Matrix2x2 & Matrix2x2::operator = ( const Matrix2x2 &matrix ) + template + Matrix2x2 & Matrix2x2::operator = ( const Matrix2x2 &matrix ) { this->v[0] = matrix.v[0]; this->v[1] = matrix.v[1]; return *this; } - template - Matrix2x2 & Matrix2x2::operator += ( const Matrix2x2 &matrix ) + template + Matrix2x2 & Matrix2x2::operator += ( const Matrix2x2 &matrix ) { this->v[0] += matrix.v[0]; this->v[1] += matrix.v[1]; return *this; } - template - Matrix2x2 & Matrix2x2::operator -= ( const Matrix2x2 &matrix ) + template + Matrix2x2 & Matrix2x2::operator -= ( const Matrix2x2 &matrix ) { this->v[0] -= matrix.v[0]; this->v[1] -= matrix.v[1]; return *this; } - template - Matrix2x2 & Matrix2x2::operator *= ( const ElementType &scalar ) + template + Matrix2x2 & Matrix2x2::operator *= ( const ScalarType &scalar ) { this->v[0] *= scalar; this->v[1] *= scalar; return *this; } - template - Matrix2x2 & Matrix2x2::operator /= ( const ElementType &scalar ) + template + Matrix2x2 & Matrix2x2::operator /= ( const ScalarType &scalar ) { this->v[0] /= scalar; this->v[1] /= scalar; return *this; } - template - inline Matrix2x2 Matrix2x2::operator + ( const Matrix2x2 &matrix ) const - { return Matrix2x2(*this) += matrix; } + template + inline Matrix2x2 Matrix2x2::operator + ( const Matrix2x2 &matrix ) const + { return Matrix2x2(*this) += matrix; } - template - inline Matrix2x2 Matrix2x2::operator - ( const Matrix2x2 &matrix ) const - { return Matrix2x2(*this) -= matrix; } + template + inline Matrix2x2 Matrix2x2::operator - ( const Matrix2x2 &matrix ) const + { return Matrix2x2(*this) -= matrix; } - template - inline Matrix2x2 Matrix2x2::operator * ( const ElementType &scalar ) const - { return Matrix2x2(*this) *= scalar; } + template + inline Matrix2x2 Matrix2x2::operator * ( const ScalarType &scalar ) const + { return Matrix2x2(*this) *= scalar; } - template - inline Matrix2x2 Matrix2x2::operator / ( const ElementType &scalar ) const - { return Matrix2x2(*this) /= scalar; } + template + inline Matrix2x2 Matrix2x2::operator / ( const ScalarType &scalar ) const + { return Matrix2x2(*this) /= scalar; } - template - inline Matrix2x2 Matrix2x2::operator - ( ) const - { return Matrix2x2(-this->v[0], -this->v[1]); } + template + inline Matrix2x2 Matrix2x2::operator - ( ) const + { return Matrix2x2(-this->v[0], -this->v[1]); } - template - ElementType Matrix2x2::getDeterminant( ) const + template + ScalarType Matrix2x2::GetDeterminant( ) const { - ElementType determinant = (this->m11 * this->m22); + ScalarType determinant = (this->m11 * this->m22); return determinant -= (this->m12 * this->m21); } - template - Matrix2x2 Matrix2x2::getAdjoint( ) const - { return Matrix2x2( this->m22, -this->m21, -this->m12, this->m11 ); } + template + Matrix2x2 Matrix2x2::GetAdjoint( ) const + { return Matrix2x2( this->m22, -this->m21, -this->m12, this->m11 ); } - template - inline Matrix2x2 Matrix2x2::getTranspose( ) const - { return Matrix2x2( this->element[0], this->element[1], this->element[2], this->element[3] ); } + template + inline Matrix2x2 Matrix2x2::GetTranspose( ) const + { return Matrix2x2( this->element[0], this->element[1], this->element[2], this->element[3] ); } - template - Matrix2x2 & Matrix2x2::transpose( ) + template + Matrix2x2 & Matrix2x2::Transpose( ) { - ElementType swapSpace; - Utility::Element::swap( this->m12, this->m21, swapSpace ); + ScalarType swapSpace; + ::Utility::Element::Swap( this->m12, this->m21, swapSpace ); return *this; } - template - inline Matrix2x2 Matrix2x2::getInverse( ) const - { return this->getAdjoint() /= this->getDeterminant(); } + template + inline Matrix2x2 Matrix2x2::GetInverse( ) const + { return this->GetAdjoint() /= this->GetDeterminant(); } - template - Matrix2x2 Matrix2x2::getInverse( ElementType &determinant ) const + template + Matrix2x2 Matrix2x2::GetInverse( ScalarType &determinant ) const { - determinant = this->getDeterminant(); + determinant = this->GetDeterminant(); if( determinant != 0 ) - return this->getAdjoint() / determinant; - return Matrix2x2(); + return this->GetAdjoint() / determinant; + return Matrix2x2::null; } - template - Matrix2x2 & Matrix2x2::invert( ) + template + Matrix2x2 & Matrix2x2::Invert( ) { - *this /= this->getDeterminant(); + *this /= this->GetDeterminant(); this->m12 *= -1; this->m21 *= -1; - ElementType swapSpace; - Utility::Element::swap( this->m12, this->m21, swapSpace ); - Utility::Element::swap( this->m11, this->m22, swapSpace ); + ScalarType swapSpace; + Utility::Element::Swap( this->m12, this->m21, swapSpace ); + Utility::Element::Swap( this->m11, this->m22, swapSpace ); return *this; } - template - Matrix2x2 & Matrix2x2::invert( ElementType &determinant ) + template + Matrix2x2 & Matrix2x2::Invert( ScalarType &determinant ) { - determinant = this->getDeterminant(); + determinant = this->GetDeterminant(); if( determinant != 0 ) { *this /= determinant; this->m12 *= -1; this->m21 *= -1; - ElementType swapSpace; - Utility::Element::swap( this->m12, this->m21, swapSpace ); - Utility::Element::swap( this->m11, this->m22, swapSpace ); + ScalarType swapSpace; + Utility::Element::Swap( this->m12, this->m21, swapSpace ); + Utility::Element::Swap( this->m11, this->m22, swapSpace ); } return *this; } - template - inline Vector2 Matrix2x2::getRowVector( unsigned int rowID ) const - { return Vector2( this->m[0][rowID], this->m[1][rowID] ); } + template + inline Vector2 Matrix2x2::GetRowVector( unsigned int rowID ) const + { return Vector2( this->m[0][rowID], this->m[1][rowID] ); } - template - inline const Vector2 & Matrix2x2::getColumnVector( unsigned int colID ) const - { return this->v[colID]; } + template + inline Vector2 Matrix2x2::GetColumnVector( unsigned int colID ) const + { return this->v[colID]; } -// Matrix3x3 /////////////////////////////////////// - template - const Matrix3x3 Matrix3x3::identity = Matrix3x3( 1, 0, 0, 0, 1, 0, 0, 0, 1 ); +// Matrix3x3 /////////////////////////////////////// - template - const Matrix3x3 Matrix3x3::null = Matrix3x3( 0, 0, 0, 0, 0, 0, 0, 0, 0 ); + template + const Matrix3x3 Matrix3x3::identity = Matrix3x3( 1, 0, 0, 0, 1, 0, 0, 0, 1 ); - template - Matrix3x3::Matrix3x3( ) : m11(0), m21(0), m31(0), m12(0), m22(0), m32(0), m13(0), m23(0), m33(0) {} + template + const Matrix3x3 Matrix3x3::null = Matrix3x3( 0, 0, 0, 0, 0, 0, 0, 0, 0 ); + + template + Matrix3x3::Matrix3x3( ): m11(), m21(), m31(), m12(), m22(), m32(), m13(), m23(), m33() {} - template - Matrix3x3::Matrix3x3( const ElementType &_m11, const ElementType &_m12, const ElementType &_m13, const ElementType &_m21, const ElementType &_m22, const ElementType &_m23, const ElementType &_m31, const ElementType &_m32, const ElementType &_m33 ) - : m11(_m11), m21(_m21), m31(_m31), m12(_m12), m22(_m22), m32(_m32), m13(_m13), m23(_m23), m33(_m33) {} + template + Matrix3x3::Matrix3x3( const ScalarType &_m11, const ScalarType &_m12, const ScalarType &_m13, const ScalarType &_m21, const ScalarType &_m22, const ScalarType &_m23, const ScalarType &_m31, const ScalarType &_m32, const ScalarType &_m33 ) + { + this->m11 = _m11; this->m12 = _m12; this->m13 = _m13; + this->m21 = _m21; this->m22 = _m22; this->m23 = _m23; + this->m31 = _m31; this->m32 = _m32; this->m33 = _m33; + } - template - Matrix3x3::Matrix3x3( const Vector3 vec[3] ) - : m11(vec[0].x), m21(vec[0].y), m31(vec[0].z), m12(vec[1].x), m22(vec[1].y), m32(vec[1].z), m13(vec[2].x), m23(vec[2].y), m33(vec[2].z) {} + template + Matrix3x3::Matrix3x3( const Vector3 vec[3] ) + { this->v[0] = vec[0]; this->v[1] = vec[1]; this->v[2] = vec[2]; } - template - Matrix3x3::Matrix3x3( const Vector3 &vec1, const Vector3 &vec2, const Vector3 &vec3 ) - : m11(vec1.x), m21(vec1.y), m31(vec1.z), m12(vec2.x), m22(vec2.y), m32(vec2.z), m13(vec3.x), m23(vec3.y), m33(vec3.z) {} + template + Matrix3x3::Matrix3x3( const Vector3 &vec1, const Vector3 &vec2, const Vector3 &vec3 ) + { this->v[0] = vec1; this->v[1] = vec2; this->v[2] = vec3; } - template - Matrix3x3::Matrix3x3( const ElementType _element[9] ) -// : m11(_element[0]), m21(_element[1]), m31(_element[2]), m12(_element[3]), m22(_element[4]), m32(_element[5]), m13(_element[6]), m23(_element[7]), m33(_element[8]) {} - : m11(_element[0]), m12(_element[1]), m13(_element[2]), m21(_element[3]), m22(_element[4]), m23(_element[5]), m31(_element[6]), m32(_element[7]), m33(_element[8]) {} + template + Matrix3x3::Matrix3x3( const ScalarType _element[9] ) + { + this->m11 = _element[0]; this->m12 = _element[1]; this->m13 = _element[2]; + this->m21 = _element[3]; this->m22 = _element[4]; this->m23 = _element[5]; + this->m31 = _element[6]; this->m32 = _element[7]; this->m33 = _element[8]; + } - template - Matrix3x3::Matrix3x3( const Matrix3x3 &matrix ) - : m11(matrix.m11), m21(matrix.m21), m31(matrix.m31), m12(matrix.m12), m22(matrix.m22), m32(matrix.m32), m13(matrix.m13), m23(matrix.m23), m33(matrix.m33) {} + template + Matrix3x3::Matrix3x3( const Matrix3x3 &matrix ) + { this->v[0] = matrix.v[0]; this->v[1] = matrix.v[1]; this->v[2] = matrix.v[2]; } - template - inline Matrix3x3::operator ElementType* ( ) + template + inline Matrix3x3::operator ScalarType* ( ) { return this->element; } - template - inline Matrix3x3::operator const ElementType* ( ) const + template + inline Matrix3x3::operator const ScalarType* ( ) const { return this->element; } - template - Matrix3x3 & Matrix3x3::operator = ( const Vector3 vec[3] ) + template + Matrix3x3 & Matrix3x3::operator = ( const Vector3 vec[3] ) { this->v[0] = vec[0]; this->v[1] = vec[1]; @@ -411,16 +411,8 @@ namespace LinearAlgebra return *this; } - template - Matrix3x3 & Matrix3x3::operator = ( const ElementType element[9] ) - { - for( int i = 0; i < 9; ++i ) - this->element[i] = element[i]; - return *this; - } - - template - Matrix3x3 & Matrix3x3::operator = ( const Matrix3x3 &matrix ) + template + Matrix3x3 & Matrix3x3::operator = ( const Matrix3x3 &matrix ) { this->v[0] = matrix.v[0]; this->v[1] = matrix.v[1]; @@ -428,8 +420,8 @@ namespace LinearAlgebra return *this; } - template - Matrix3x3 & Matrix3x3::operator += ( const Matrix3x3 &matrix ) + template + Matrix3x3 & Matrix3x3::operator += ( const Matrix3x3 &matrix ) { this->v[0] += matrix.v[0]; this->v[1] += matrix.v[1]; @@ -437,8 +429,8 @@ namespace LinearAlgebra return *this; } - template - Matrix3x3 & Matrix3x3::operator -= ( const Matrix3x3 &matrix ) + template + Matrix3x3 & Matrix3x3::operator -= ( const Matrix3x3 &matrix ) { this->v[0] -= matrix.v[0]; this->v[1] -= matrix.v[1]; @@ -446,8 +438,8 @@ namespace LinearAlgebra return *this; } - template - Matrix3x3 & Matrix3x3::operator *= ( const ElementType &scalar ) + template + Matrix3x3 & Matrix3x3::operator *= ( const ScalarType &scalar ) { this->v[0] *= scalar; this->v[1] *= scalar; @@ -455,8 +447,8 @@ namespace LinearAlgebra return *this; } - template - Matrix3x3 & Matrix3x3::operator /= ( const ElementType &scalar ) + template + Matrix3x3 & Matrix3x3::operator /= ( const ScalarType &scalar ) { this->v[0] /= scalar; this->v[1] /= scalar; @@ -464,30 +456,30 @@ namespace LinearAlgebra return *this; } - template - inline Matrix3x3 Matrix3x3::operator + ( const Matrix3x3 &matrix ) const - { return Matrix3x3(*this) += matrix; } + template + inline Matrix3x3 Matrix3x3::operator + ( const Matrix3x3 &matrix ) const + { return Matrix3x3(*this) += matrix; } - template - inline Matrix3x3 Matrix3x3::operator - ( const Matrix3x3 &matrix ) const - { return Matrix3x3(*this) -= matrix; } + template + inline Matrix3x3 Matrix3x3::operator - ( const Matrix3x3 &matrix ) const + { return Matrix3x3(*this) -= matrix; } - template - inline Matrix3x3 Matrix3x3::operator * ( const ElementType &scalar ) const - { return Matrix3x3(*this) *= scalar; } + template + inline Matrix3x3 Matrix3x3::operator * ( const ScalarType &scalar ) const + { return Matrix3x3(*this) *= scalar; } - template - inline Matrix3x3 Matrix3x3::operator / ( const ElementType &scalar ) const - { return Matrix3x3(*this) /= scalar; } + template + inline Matrix3x3 Matrix3x3::operator / ( const ScalarType &scalar ) const + { return Matrix3x3(*this) /= scalar; } - template - inline Matrix3x3 Matrix3x3::operator - ( ) const - { return Matrix3x3(-this->v[0], -this->v[1], -this->v[2]); } + template + inline Matrix3x3 Matrix3x3::operator - ( ) const + { return Matrix3x3(-this->v[0], -this->v[1], -this->v[2]); } - template - ElementType Matrix3x3::getDeterminant( ) const + template + ScalarType Matrix3x3::GetDeterminant( ) const { - ElementType determinant = (this->m11 * this->m22 * this->m33); + ScalarType determinant = (this->m11 * this->m22 * this->m33); determinant += (this->m12 * this->m23 * this->m31); determinant += (this->m13 * this->m21 * this->m32); determinant -= (this->m11 * this->m23 * this->m32); @@ -495,108 +487,119 @@ namespace LinearAlgebra return determinant -= (this->m13 * this->m22 * this->m31); } - template - Matrix3x3 Matrix3x3::getAdjoint( ) const + template + Matrix3x3 Matrix3x3::GetAdjoint( ) const { - return Matrix3x3( (this->m22*this->m33 - this->m23*this->m32), (this->m13*this->m32 - this->m12*this->m33), (this->m12*this->m23 - this->m13*this->m22), + return Matrix3x3( (this->m22*this->m33 - this->m23*this->m32), (this->m13*this->m32 - this->m12*this->m33), (this->m12*this->m23 - this->m13*this->m22), (this->m23*this->m31 - this->m21*this->m33), (this->m11*this->m33 - this->m13*this->m31), (this->m13*this->m21 - this->m11*this->m23), (this->m21*this->m32 - this->m22*this->m31), (this->m12*this->m31 - this->m11*this->m32), (this->m11*this->m22 - this->m12*this->m21) ); } - template - inline Matrix3x3 Matrix3x3::getTranspose( ) const + template + inline Matrix3x3 Matrix3x3::GetTranspose( ) const { - return Matrix3x3( this->m11, this->m21, this->m31, + return Matrix3x3( this->m11, this->m21, this->m31, this->m12, this->m22, this->m32, this->m13, this->m23, this->m33 ); } - template - Matrix3x3 & Matrix3x3::transpose( ) + template + Matrix3x3 & Matrix3x3::Transpose( ) { - ElementType swapSpace; - Utility::Element::swap( this->m12, this->m21, swapSpace ); - Utility::Element::swap( this->m13, this->m31, swapSpace ); - Utility::Element::swap( this->m23, this->m32, swapSpace ); + ScalarType swapSpace; + Utility::Element::Swap( this->m12, this->m21, swapSpace ); + Utility::Element::Swap( this->m13, this->m31, swapSpace ); + Utility::Element::Swap( this->m23, this->m32, swapSpace ); return *this; } - template - Matrix3x3 Matrix3x3::getInverse( ) const - { return this->getAdjoint() /= this->getDeterminant(); } + template + Matrix3x3 Matrix3x3::GetInverse( ) const + { return this->GetAdjoint() /= this->GetDeterminant(); } - template - Matrix3x3 Matrix3x3::getInverse( ElementType &determinant ) const + template + Matrix3x3 Matrix3x3::GetInverse( ScalarType &determinant ) const { - determinant = this->getDeterminant(); + determinant = this->GetDeterminant(); if( determinant != 0 ) - return this->getAdjoint() /= determinant; - else return Matrix3x3(); + return this->GetAdjoint() /= determinant; + else return Matrix3x3::null; } - template - Matrix3x3 & Matrix3x3::invert( ) - { return *this = this->getAdjoint() /= this->getDeterminant(); } + template + Matrix3x3 & Matrix3x3::Invert( ) + { return *this = this->GetAdjoint() /= this->GetDeterminant(); } - template - Matrix3x3 & Matrix3x3::invert( ElementType &determinant ) + template + Matrix3x3 & Matrix3x3::Invert( ScalarType &determinant ) { - determinant = this->getDeterminant(); + determinant = this->GetDeterminant(); if( determinant != 0 ) - return *this = this->getAdjoint() /= determinant; + return *this = this->GetAdjoint() /= determinant; return *this; } - template - inline Vector3 Matrix3x3::getRowVector( unsigned int rowID ) const - { return Vector3( this->m[0][rowID], this->m[1][rowID], this->m[2][rowID] ); } + template + inline Vector3 Matrix3x3::GetRowVector( unsigned int rowID ) const + { return Vector3( this->m[0][rowID], this->m[1][rowID], this->m[2][rowID] ); } - template - inline const Vector3 & Matrix3x3::getColumnVector( unsigned int colID ) const + template + inline Vector3 Matrix3x3::GetColumnVector( unsigned int colID ) const { return this->v[colID]; } -// Matrix4x4 /////////////////////////////////////// - template - const Matrix4x4 Matrix4x4::identity = Matrix4x4( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ); +// Matrix4x4 /////////////////////////////////////// - template - const Matrix4x4 Matrix4x4::null = Matrix4x4( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ); + template + const Matrix4x4 Matrix4x4::identity = Matrix4x4( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ); + + template + const Matrix4x4 Matrix4x4::null = Matrix4x4( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ); - template - Matrix4x4::Matrix4x4( ) - : m11(0), m21(0), m31(0), m41(0), m12(0), m22(0), m32(0), m42(0), m13(0), m23(0), m33(0), m43(0), m14(0), m24(0), m34(0), m44(0) {} + template + Matrix4x4::Matrix4x4( ) + : m11(), m21(), m31(), m41(), m12(), m22(), m32(), m42(), + m13(), m23(), m33(), m43(), m14(), m24(), m34(), m44() {} - template - Matrix4x4::Matrix4x4( const ElementType &_m11, const ElementType &_m12, const ElementType &_m13, const ElementType &_m14, const ElementType &_m21, const ElementType &_m22, const ElementType &_m23, const ElementType &_m24, const ElementType &_m31, const ElementType &_m32, const ElementType &_m33, const ElementType &_m34, const ElementType &_m41, const ElementType &_m42, const ElementType &_m43, const ElementType &_m44 ) - : m11(_m11), m21(_m21), m31(_m31), m41(_m41), m12(_m12), m22(_m22), m32(_m32), m42(_m42), m13(_m13), m23(_m23), m33(_m33), m43(_m43), m14(_m14), m24(_m24), m34(_m34), m44(_m44) {} + template + Matrix4x4::Matrix4x4( const ScalarType &_m11, const ScalarType &_m12, const ScalarType &_m13, const ScalarType &_m14, const ScalarType &_m21, const ScalarType &_m22, const ScalarType &_m23, const ScalarType &_m24, const ScalarType &_m31, const ScalarType &_m32, const ScalarType &_m33, const ScalarType &_m34, const ScalarType &_m41, const ScalarType &_m42, const ScalarType &_m43, const ScalarType &_m44 ) + { + this->m11 = _m11; this->m12 = _m12; this->m13 = _m13; this->m14 = _m14; + this->m21 = _m21; this->m22 = _m22; this->m23 = _m23; this->m24 = _m24; + this->m31 = _m31; this->m32 = _m32; this->m33 = _m33; this->m34 = _m34; + this->m41 = _m41; this->m42 = _m42; this->m43 = _m43; this->m44 = _m44; + } - template - Matrix4x4::Matrix4x4( const Vector4 vec[4] ) - : m11(vec[0].x), m21(vec[0].y), m31(vec[0].z), m41(vec[0].w), m12(vec[1].x), m22(vec[1].y), m32(vec[1].z), m42(vec[1].w), m13(vec[2].x), m23(vec[2].y), m33(vec[2].z), m43(vec[2].w), m14(vec[3].x), m24(vec[3].y), m34(vec[3].z), m44(vec[3].w) {} + template + Matrix4x4::Matrix4x4( const Vector4 vec[4] ) + { this->v[0] = vec[0]; this->v[1] = vec[1]; this->v[2] = vec[2]; this->v[3] = vec[3]; } - template - Matrix4x4::Matrix4x4( const Vector4 &vec1, const Vector4 &vec2, const Vector4 &vec3, const Vector4 &vec4 ) - : m11(vec1.x), m21(vec1.y), m31(vec1.z), m41(vec1.w), m12(vec2.x), m22(vec2.y), m32(vec2.z), m42(vec2.w), m13(vec3.x), m23(vec3.y), m33(vec3.z), m43(vec3.w), m14(vec4.x), m24(vec4.y), m34(vec4.z), m44(vec4.w) {} + template + Matrix4x4::Matrix4x4( const Vector4 &vec1, const Vector4 &vec2, const Vector4 &vec3, const Vector4 &vec4 ) + { this->v[0] = vec1; this->v[1] = vec2; this->v[2] = vec3; this->v[3] = vec4; } - template - Matrix4x4::Matrix4x4( const ElementType _element[16] ) -// : m11(_element[0]), m21(_element[1]), m31(_element[2]), m41(_element[3]), m12(_element[4]), m22(_element[5]), m32(_element[6]), m42(_element[7]), m13(_element[8]), m23(_element[9]), m33(_element[10]), m43(_element[11]), m14(_element[12]), m24(_element[13]), m34(_element[14]), m44(_element[15]) {} - : m11(_element[0]), m12(_element[1]), m13(_element[2]), m14(_element[3]), m21(_element[4]), m22(_element[5]), m23(_element[6]), m24(_element[7]), m31(_element[8]), m32(_element[9]), m33(_element[10]), m34(_element[11]), m41(_element[12]), m42(_element[13]), m43(_element[14]), m44(_element[15]) {} + template + Matrix4x4::Matrix4x4( const ScalarType _element[16] ) + { + this->m11 = _element[0]; this->m12 = _element[1]; this->m13 = _element[2]; this->m14 = _element[3]; + this->m21 = _element[4]; this->m22 = _element[5]; this->m23 = _element[6]; this->m24 = _element[7]; + this->m31 = _element[8]; this->m32 = _element[9]; this->m33 = _element[10]; this->m34 = _element[11]; + this->m41 = _element[12]; this->m42 = _element[13]; this->m43 = _element[14]; this->m44 = _element[15]; + } - template - Matrix4x4::Matrix4x4( const Matrix4x4 &matrix ) - : m11(matrix.m11), m21(matrix.m21), m31(matrix.m31), m41(matrix.m41), m12(matrix.m12), m22(matrix.m22), m32(matrix.m32), m42(matrix.m42), m13(matrix.m13), m23(matrix.m23), m33(matrix.m33), m43(matrix.m43), m14(matrix.m14), m24(matrix.m24), m34(matrix.m34), m44(matrix.m44) {} + template + Matrix4x4::Matrix4x4( const Matrix4x4 &matrix ) + { this->v[0] = matrix.v[0]; this->v[1] = matrix.v[1]; this->v[2] = matrix.v[2]; this->v[3] = matrix.v[3]; } - template - inline Matrix4x4::operator ElementType* ( ) + template + inline Matrix4x4::operator ScalarType* ( ) { return this->element; } - template - inline Matrix4x4::operator const ElementType* ( ) const + template + inline Matrix4x4::operator const ScalarType* ( ) const { return this->element; } - template - Matrix4x4 & Matrix4x4::operator = ( const Vector4 vec[4] ) + template + Matrix4x4 & Matrix4x4::operator = ( const Vector4 vec[4] ) { this->v[0] = vec[0]; this->v[1] = vec[1]; @@ -605,16 +608,8 @@ namespace LinearAlgebra return *this; } - template - Matrix4x4 & Matrix4x4::operator = ( const ElementType element[16] ) - { - for( int i = 0; i < 16; ++i ) - this->element[i] = element[i]; - return *this; - } - - template - Matrix4x4 & Matrix4x4::operator = ( const Matrix4x4 &matrix ) + template + Matrix4x4 & Matrix4x4::operator = ( const Matrix4x4 &matrix ) { this->v[0] = matrix.v[0]; this->v[1] = matrix.v[1]; @@ -623,8 +618,8 @@ namespace LinearAlgebra return *this; } - template - Matrix4x4 & Matrix4x4::operator += ( const Matrix4x4 &matrix ) + template + Matrix4x4 & Matrix4x4::operator += ( const Matrix4x4 &matrix ) { this->v[0] += matrix.v[0]; this->v[1] += matrix.v[1]; @@ -633,8 +628,8 @@ namespace LinearAlgebra return *this; } - template - Matrix4x4 & Matrix4x4::operator -= ( const Matrix4x4 &matrix ) + template + Matrix4x4 & Matrix4x4::operator -= ( const Matrix4x4 &matrix ) { this->v[0] -= matrix.v[0]; this->v[1] -= matrix.v[1]; @@ -643,8 +638,8 @@ namespace LinearAlgebra return *this; } - template - Matrix4x4 & Matrix4x4::operator *= ( const ElementType &scalar ) + template + Matrix4x4 & Matrix4x4::operator *= ( const ScalarType &scalar ) { this->v[0] *= scalar; this->v[1] *= scalar; @@ -653,8 +648,8 @@ namespace LinearAlgebra return *this; } - template - Matrix4x4 & Matrix4x4::operator /= ( const ElementType &scalar ) + template + Matrix4x4 & Matrix4x4::operator /= ( const ScalarType &scalar ) { this->v[0] /= scalar; this->v[1] /= scalar; @@ -663,98 +658,98 @@ namespace LinearAlgebra return *this; } - template - inline Matrix4x4 Matrix4x4::operator + ( const Matrix4x4 &matrix ) const - { return Matrix4x4(*this) += matrix; } + template + inline Matrix4x4 Matrix4x4::operator + ( const Matrix4x4 &matrix ) const + { return Matrix4x4(*this) += matrix; } - template - inline Matrix4x4 Matrix4x4::operator - ( const Matrix4x4 &matrix ) const - { return Matrix4x4(*this) -= matrix; } + template + inline Matrix4x4 Matrix4x4::operator - ( const Matrix4x4 &matrix ) const + { return Matrix4x4(*this) -= matrix; } - template - inline Matrix4x4 Matrix4x4::operator * ( const ElementType &scalar ) const - { return Matrix4x4(*this) *= scalar; } + template + inline Matrix4x4 Matrix4x4::operator * ( const ScalarType &scalar ) const + { return Matrix4x4(*this) *= scalar; } - template - inline Matrix4x4 Matrix4x4::operator / ( const ElementType &scalar ) const - { return Matrix4x4(*this) /= scalar; } + template + inline Matrix4x4 Matrix4x4::operator / ( const ScalarType &scalar ) const + { return Matrix4x4(*this) /= scalar; } - template - inline Matrix4x4 Matrix4x4::operator - ( ) const - { return Matrix4x4(-this->v[0], -this->v[1], -this->v[2], -this->v[3]); } + template + inline Matrix4x4 Matrix4x4::operator - ( ) const + { return Matrix4x4(-this->v[0], -this->v[1], -this->v[2], -this->v[3]); } - template - ElementType Matrix4x4::getDeterminant( ) const + template + ScalarType Matrix4x4::GetDeterminant( ) const { - ElementType determinant = this->m11 * Matrix3x3(this->m22, this->m23, this->m24, this->m32, this->m33, this->m34, this->m42, this->m43, this->m44).getDeterminant(); - determinant -= this->m12 * Matrix3x3(this->m21, this->m23, this->m24, this->m31, this->m33, this->m34, this->m41, this->m43, this->m44).getDeterminant(); - determinant += this->m13 * Matrix3x3(this->m21, this->m22, this->m24, this->m31, this->m32, this->m34, this->m41, this->m42, this->m44).getDeterminant(); - return determinant -= this->m14 * Matrix3x3(this->m21, this->m22, this->m23, this->m31, this->m32, this->m33, this->m41, this->m42, this->m43).getDeterminant(); + ScalarType determinant = this->m11 * Matrix3x3(this->m22, this->m23, this->m24, this->m32, this->m33, this->m34, this->m42, this->m43, this->m44).GetDeterminant(); + determinant -= this->m12 * Matrix3x3(this->m21, this->m23, this->m24, this->m31, this->m33, this->m34, this->m41, this->m43, this->m44).GetDeterminant(); + determinant += this->m13 * Matrix3x3(this->m21, this->m22, this->m24, this->m31, this->m32, this->m34, this->m41, this->m42, this->m44).GetDeterminant(); + return determinant -= this->m14 * Matrix3x3(this->m21, this->m22, this->m23, this->m31, this->m32, this->m33, this->m41, this->m42, this->m43).GetDeterminant(); } - template - Matrix4x4 Matrix4x4::getAdjoint( ) const + template + Matrix4x4 Matrix4x4::GetAdjoint( ) const { - return Matrix4x4( Matrix3x3(this->m22, this->m23, this->m24, this->m32, this->m33, this->m34, this->m42, this->m43, this->m44).getDeterminant(), -Matrix3x3(this->m12, this->m13, this->m14, this->m32, this->m33, this->m34, this->m42, this->m43, this->m44).getDeterminant(), Matrix3x3(this->m12, this->m13, this->m14, this->m22, this->m23, this->m24, this->m42, this->m43, this->m44).getDeterminant(), -Matrix3x3(this->m12, this->m13, this->m14, this->m22, this->m23, this->m24, this->m32, this->m33, this->m34).getDeterminant(), - -Matrix3x3(this->m21, this->m23, this->m24, this->m31, this->m33, this->m34, this->m41, this->m43, this->m44).getDeterminant(), Matrix3x3(this->m11, this->m13, this->m14, this->m31, this->m33, this->m34, this->m41, this->m43, this->m44).getDeterminant(), -Matrix3x3(this->m11, this->m13, this->m14, this->m21, this->m23, this->m24, this->m41, this->m43, this->m44).getDeterminant(), Matrix3x3(this->m11, this->m13, this->m14, this->m21, this->m23, this->m24, this->m31, this->m33, this->m34).getDeterminant(), - Matrix3x3(this->m21, this->m22, this->m24, this->m31, this->m32, this->m34, this->m41, this->m42, this->m44).getDeterminant(), -Matrix3x3(this->m11, this->m12, this->m14, this->m31, this->m32, this->m34, this->m41, this->m42, this->m44).getDeterminant(), Matrix3x3(this->m11, this->m12, this->m14, this->m21, this->m22, this->m24, this->m41, this->m42, this->m44).getDeterminant(), -Matrix3x3(this->m11, this->m12, this->m14, this->m21, this->m22, this->m24, this->m31, this->m32, this->m34).getDeterminant(), - -Matrix3x3(this->m21, this->m22, this->m23, this->m31, this->m32, this->m33, this->m41, this->m42, this->m43).getDeterminant(), Matrix3x3(this->m11, this->m12, this->m13, this->m31, this->m32, this->m33, this->m41, this->m42, this->m43).getDeterminant(), -Matrix3x3(this->m11, this->m12, this->m13, this->m21, this->m22, this->m23, this->m41, this->m42, this->m43).getDeterminant(), Matrix3x3(this->m11, this->m12, this->m13, this->m21, this->m22, this->m23, this->m31, this->m32, this->m33).getDeterminant() ); + return Matrix4x4( Matrix3x3(this->m22, this->m23, this->m24, this->m32, this->m33, this->m34, this->m42, this->m43, this->m44).GetDeterminant(), -Matrix3x3(this->m12, this->m13, this->m14, this->m32, this->m33, this->m34, this->m42, this->m43, this->m44).GetDeterminant(), Matrix3x3(this->m12, this->m13, this->m14, this->m22, this->m23, this->m24, this->m42, this->m43, this->m44).GetDeterminant(), -Matrix3x3(this->m12, this->m13, this->m14, this->m22, this->m23, this->m24, this->m32, this->m33, this->m34).GetDeterminant(), + -Matrix3x3(this->m21, this->m23, this->m24, this->m31, this->m33, this->m34, this->m41, this->m43, this->m44).GetDeterminant(), Matrix3x3(this->m11, this->m13, this->m14, this->m31, this->m33, this->m34, this->m41, this->m43, this->m44).GetDeterminant(), -Matrix3x3(this->m11, this->m13, this->m14, this->m21, this->m23, this->m24, this->m41, this->m43, this->m44).GetDeterminant(), Matrix3x3(this->m11, this->m13, this->m14, this->m21, this->m23, this->m24, this->m31, this->m33, this->m34).GetDeterminant(), + Matrix3x3(this->m21, this->m22, this->m24, this->m31, this->m32, this->m34, this->m41, this->m42, this->m44).GetDeterminant(), -Matrix3x3(this->m11, this->m12, this->m14, this->m31, this->m32, this->m34, this->m41, this->m42, this->m44).GetDeterminant(), Matrix3x3(this->m11, this->m12, this->m14, this->m21, this->m22, this->m24, this->m41, this->m42, this->m44).GetDeterminant(), -Matrix3x3(this->m11, this->m12, this->m14, this->m21, this->m22, this->m24, this->m31, this->m32, this->m34).GetDeterminant(), + -Matrix3x3(this->m21, this->m22, this->m23, this->m31, this->m32, this->m33, this->m41, this->m42, this->m43).GetDeterminant(), Matrix3x3(this->m11, this->m12, this->m13, this->m31, this->m32, this->m33, this->m41, this->m42, this->m43).GetDeterminant(), -Matrix3x3(this->m11, this->m12, this->m13, this->m21, this->m22, this->m23, this->m41, this->m42, this->m43).GetDeterminant(), Matrix3x3(this->m11, this->m12, this->m13, this->m21, this->m22, this->m23, this->m31, this->m32, this->m33).GetDeterminant() ); } - template - inline Matrix4x4 Matrix4x4::getTranspose( ) const + template + inline Matrix4x4 Matrix4x4::GetTranspose( ) const { - return Matrix4x4( this->m11, this->m21, this->m31, this->m41, - this->m12, this->m22, this->m32, this->m42, - this->m13, this->m23, this->m33, this->m43, - this->m14, this->m24, this->m34, this->m44 ); + return Matrix4x4( this->m11, this->m21, this->m31, this->m41, + this->m12, this->m22, this->m32, this->m42, + this->m13, this->m23, this->m33, this->m43, + this->m14, this->m24, this->m34, this->m44 ); } - template - Matrix4x4 & Matrix4x4::transpose( ) + template + Matrix4x4 & Matrix4x4::Transpose( ) { - ElementType swapSpace; - ::Utility::Element::swap( this->m12, this->m21, swapSpace ); - ::Utility::Element::swap( this->m13, this->m31, swapSpace ); - ::Utility::Element::swap( this->m14, this->m41, swapSpace ); - ::Utility::Element::swap( this->m23, this->m32, swapSpace ); - ::Utility::Element::swap( this->m24, this->m42, swapSpace ); - ::Utility::Element::swap( this->m34, this->m43, swapSpace ); + ScalarType swapSpace; + ::Utility::Element::Swap( this->m12, this->m21, swapSpace ); + ::Utility::Element::Swap( this->m13, this->m31, swapSpace ); + ::Utility::Element::Swap( this->m14, this->m41, swapSpace ); + ::Utility::Element::Swap( this->m23, this->m32, swapSpace ); + ::Utility::Element::Swap( this->m24, this->m42, swapSpace ); + ::Utility::Element::Swap( this->m34, this->m43, swapSpace ); return *this; } - template - inline Matrix4x4 Matrix4x4::getInverse( ) const - { return this->getAdjoint() /= this->getDeterminant() ; } + template + inline Matrix4x4 Matrix4x4::GetInverse( ) const + { return this->GetAdjoint() /= this->GetDeterminant() ; } - template - Matrix4x4 Matrix4x4::getInverse( ElementType &determinant ) const + template + Matrix4x4 Matrix4x4::GetInverse( ScalarType &determinant ) const { - determinant = this->getDeterminant(); + determinant = this->GetDeterminant(); if( determinant != 0.0f ) - return this->getAdjoint() /= determinant; - return Matrix4x4(); + return this->GetAdjoint() /= determinant; + return Matrix4x4::null; } - template - Matrix4x4 & Matrix4x4::invert( ) - { return *this = this->getAdjoint() /= this->getDeterminant(); } + template + Matrix4x4 & Matrix4x4::Invert( ) + { return *this = this->GetAdjoint() /= this->GetDeterminant(); } - template - Matrix4x4 & Matrix4x4::invert( ElementType &determinant ) + template + Matrix4x4 & Matrix4x4::Invert( ScalarType &determinant ) { - determinant = this->getDeterminant(); + determinant = this->GetDeterminant(); if( determinant != 0.0f ) - return *this = this->getAdjoint() /= determinant; + return *this = this->GetAdjoint() /= determinant; return *this; } - template - inline Vector4 Matrix4x4::getRowVector( unsigned int rowID ) const - { return Vector4( this->m[0][rowID], this->m[1][rowID], this->m[2][rowID], this->m[3][rowID] ); } + template + inline Vector4 Matrix4x4::GetRowVector( unsigned int rowID ) const + { return Vector4( this->m[0][rowID], this->m[1][rowID], this->m[2][rowID], this->m[3][rowID] ); } - template - inline const Vector4 & Matrix4x4::getColumnVector( unsigned int colID ) const + template + inline const Vector4 & Matrix4x4::GetColumnVector( unsigned int colID ) const { return this->v[colID]; } } diff --git a/OysterMath/OysterMath.cpp b/OysterMath/OysterMath.cpp deleted file mode 100644 index 43ec9cde..00000000 --- a/OysterMath/OysterMath.cpp +++ /dev/null @@ -1,32 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// by Dan Andersson 2013 -///////////////////////////////////////////////////////////////////// - -#include "OysterMath.h" - -namespace Oyster { namespace Math -{ - Float2 & operator *= ( Float2 &left, const Float2 &right ) - { - left.x *= right.x; - left.y *= right.y; - return left; - } - - Float3 & operator *= ( Float3 &left, const Float3 &right ) - { - left.x *= right.x; - left.y *= right.y; - left.z *= right.z; - return left; - } - - Float4 & operator *= ( Float4 &left, const Float4 &right ) - { - left.x *= right.x; - left.y *= right.y; - left.z *= right.z; - left.w *= right.w; - return left; - } -} } \ No newline at end of file diff --git a/OysterMath/OysterMath.h b/OysterMath/OysterMath.h index 821312fc..e539e485 100644 --- a/OysterMath/OysterMath.h +++ b/OysterMath/OysterMath.h @@ -2,7 +2,6 @@ // by Dan Andersson 2013 ///////////////////////////////////////////////////////////////////// -#pragma once #ifndef OYSTER_MATH_H #define OYSTER_MATH_H @@ -10,211 +9,224 @@ #include "LinearMath.h" #include -namespace Oyster { namespace Math +namespace Oyster { namespace Math /// Oyster's native math library { - typedef float Float; + typedef float Float; /// Oyster's native scalar is float - typedef ::LinearAlgebra::Vector2 Float2; - typedef ::LinearAlgebra::Vector3 Float3; - typedef ::LinearAlgebra::Vector4 Float4; + typedef ::LinearAlgebra::Vector2 Float2; /// 2D Linear Vector for Oyster + typedef ::LinearAlgebra::Vector3 Float3; /// 3D Linear Vector for Oyster + typedef ::LinearAlgebra::Vector4 Float4; /// 4D Linear Vector for Oyster - typedef ::LinearAlgebra::Matrix2x2 Float2x2; - typedef ::LinearAlgebra::Matrix3x3 Float3x3; - typedef ::LinearAlgebra::Matrix4x4 Float4x4; + typedef ::LinearAlgebra::Matrix2x2 Float2x2; /// 2x2 Linear Matrix for Oyster + typedef ::LinearAlgebra::Matrix3x3 Float3x3; /// 3x3 Linear Matrix for Oyster + typedef ::LinearAlgebra::Matrix4x4 Float4x4; /// 4x4 Linear Matrix for Oyster - typedef Float4x4 Matrix; - typedef Float2 Vector2; - typedef Float3 Vector3; - typedef Float4 Vector4; + typedef Float4x4 Matrix; // by popular demand + typedef Float2 Vector2; // by popular demand + typedef Float3 Vector3; // by popular demand + typedef Float4 Vector4; // by popular demand + /// Function Highly recommended to check at start, just in case current version is using a feature that might be available. + /// @todo TODO: create a template UniquePointer to use here + inline bool IsSupported() + { return true; } + /// Creates a solution matrix for 'out´= 'targetMem' * 'in'. + /// Returns false if there is no explicit solution. + inline bool SuperpositionMatrix( const Float2x2 &in, const Float2x2 &out, Float2x2 &targetMem ) + { return ::LinearAlgebra::SuperpositionMatrix( in, out, targetMem ); } - Float2 & operator *= ( Float2 &left, const Float2 &right ); + /// Creates a solution matrix for 'out´= 'targetMem' * 'in'. + /// Returns false if there is no explicit solution. + inline bool SuperpositionMatrix( const Float3x3 &in, const Float3x3 &out, Float3x3 &targetMem ) + { return ::LinearAlgebra::SuperpositionMatrix( in, out, targetMem ); } - inline Float2 operator * ( const Float2 &left, const Float2 &right ) - { return Float2(left) *= right; } - - inline Float2 operator * ( const Float &left, const Float2 &right ) - { return Float2(right) *= left; } - - Float3 & operator *= ( Float3 &left, const Float3 &right ); - - inline Float3 operator * ( const Float3 &left, const Float3 &right ) - { return Float3(left) *= right; } - - inline Float3 operator * ( const Float &left, const Float3 &right ) - { return Float3(right) *= left; } - - Float4 & operator *= ( Float4 &left, const Float4 &right ); - - inline Float4 operator * ( const Float4 &left, const Float4 &right ) - { return Float4(left) *= right; } - - inline Float4 operator * ( const Float &left, const Float4 &right ) - { return Float4(right) *= left; } - - inline Float2x2 operator * ( const Float &left, const Float2x2 &right ) - { return Float2x2(right) *= left; } - - inline Float3x3 operator * ( const Float &left, const Float3x3 &right ) - { return Float3x3(right) *= left; } - - inline Float4x4 operator * ( const Float &left, const Float4x4 &right ) - { return Float4x4(right) *= left; } - - // Deprecated function! Use the static const member identity instead. - inline void identityMatrix( Float2x2 &output ) - { output = Float2x2::identity; } - - // Deprecated function! Use the static const member identity instead. - inline void identityMatrix( Float3x3 &output ) - { output = Float3x3::identity; } - - // Deprecated function! Use the static const member identity instead. - inline void identityMatrix( Float4x4 &output ) - { output = Float4x4::identity; } - - // If rigidBody is assumed to be by all definitions a rigid body matrix. Then this is a faster inverse method. - inline void inverseRigidBodyMatrix( Float4x4 &output, const Float4x4 &rigidBody ) - { ::LinearAlgebra::_3D::inverseRigidBody( output, rigidBody ); } - - inline void translationMatrix( Float4x4 &output, const Float3 &position ) - { ::LinearAlgebra::_3D::translationMatrix( output, position ); } - - // counterclockwise rotation around X axis - inline void rotationMatrix_AxisX( Float4x4 &output, const Float &radian ) - { ::LinearAlgebra::_3D::rotationMatrix_AxisX( output, radian ); } - - // counterclockwise rotation around Y axis - inline void rotationMatrix_AxisY( Float4x4 &output, const Float &radian ) - { ::LinearAlgebra::_3D::rotationMatrix_AxisY( output, radian ); } - - // counterclockwise rotation around Z axis - inline void rotationMatrix_AxisZ( Float4x4 &output, const Float &radian ) - { ::LinearAlgebra::_3D::rotationMatrix_AxisZ( output, radian ); } - - // counterclockwise rotation around any given Float3 vector (normalizedAxis). Please make sure it is normalized. - inline void rotationMatrix( Float4x4 &output, const Float &radian, const Float3 &normalizedAxis ) - { ::LinearAlgebra::_3D::rotationMatrix( output, normalizedAxis, radian ); } - - /* - returns a deltaAngularAxis which is a vectorProduct of the particleMovementVector and leverVector. - angular: (1/I) * L, there I is known as the "moment of inertia", L as the "angular momentum vector". - lever: Displacement vector relative to the center of mass. - Recommended reading: http://en.wikipedia.org/wiki/Torque - */ - inline Float3 deltaAngularAxis( const Float3 &movement, const Float3 &lever ) - { return ::LinearAlgebra::_3D::deltaAngularAxis( movement, lever ); } - - inline Float3 particleRotationMovement( const Float3 &deltaRadian, const Float3 &lever ) - { return ::LinearAlgebra::_3D::particleRotationMovement( deltaRadian, lever ); } - - inline Float3 vectorProjection( const Float3 &vector, const Float3 &axis ) - { return ::LinearAlgebra::_3D::vectorProjection( vector, axis ); } - - /* - output: is set to a rigibody matrix that revolve/rotate around centerOfMass and then translates. - sumDeltaAngularAxis: sum of all ( (1/I) * ( L x D ) )-vectorproducts. There I is known as "moment of inertia", L as "angular momentum vector" and D the "lever vector". - sumTranslation: sum of all the translation vectors. - centerOfMass: the point the particles is to revolve around, prior to translation. Default set to null vector aka origo. - Recommended reading: http://en.wikipedia.org/wiki/Torque - */ - inline void rigidBodyMatrix( Float4x4 &output, const Float3 &sumDeltaAngularAxis, const Float3 &sumTranslation, const Float3 ¢erOfMass = Float3::null ) - { ::LinearAlgebra::_3D::rigidBodyMatrix( output, sumDeltaAngularAxis, sumTranslation, centerOfMass ); } - - /* - output; is set to an orthographic projection matrix. - width; of the projection sample volume. - height; of the projection sample volume. - near: Distance to the nearPlane. - far: Distance to the farPlane - */ - inline void projectionMatrix_Orthographic( Float4x4 &output, const Float &width, const Float &height, const Float &nearClip = ::std::numeric_limits::epsilon(), const Float &farClip = ::std::numeric_limits::max() ) - { ::LinearAlgebra::_3D::projectionMatrix_Orthographic( output, width, height, nearClip, farClip ); } - - /* - output; is set to a perspective transform matrix. - vertFoV; is the vertical field of vision in radians. (se FoV Hor+ ) - aspect; is the screenratio width/height (example 16/9 or 16/10 ) - near: Distance to the nearPlane - far: Distance to the farPlane - */ - inline void projectionMatrix_Perspective( Float4x4 &output, const Float &verticalFoV, const Float &aspectRatio, const Float &nearClip = ::std::numeric_limits::epsilon(), const Float &farClip = ::std::numeric_limits::max() ) - { ::LinearAlgebra::_3D::projectionMatrix_Perspective( output, verticalFoV, aspectRatio, nearClip, farClip ); } - - inline Float4x4 & viewProjectionMatrix( Float4x4 &output, const Float4x4 &view, const Float4x4 &projection ) - { return output = (view * projection).getTranspose(); } - - inline Float4x4 & transformMatrix( Float4x4 &output, const Float4x4 &transformee, const Float4x4 &transformer ) - { return output = transformee * transformer; } - - inline Float4x4 transformMatrix( const Float4x4 &transformee, const Float4x4 &transformer ) - { return transformee * transformer; } - - inline Float4 & transformVector( Float4 &output, const Float4 &transformee, const Float4x4 &transformer ) - { return output = transformer * transformee; } - - inline Float4 transformVector( const Float4 &transformee, const Float4x4 &transformer ) - { return transformee * transformer; } + /// Creates a solution matrix for 'out´= 'targetMem' * 'in'. + /// Returns false if there is no explicit solution. + inline bool SuperpositionMatrix( const Float4x4 &in, const Float4x4 &out, Float4x4 &targetMem ) + { return ::LinearAlgebra::SuperpositionMatrix( in, out, targetMem ); } } } -namespace Utility { namespace Value -{ // Utility Value Specializations - using namespace ::Oyster::Math; +inline ::Oyster::Math::Float2 & operator *= ( ::Oyster::Math::Float2 &left, const ::Oyster::Math::Float2 &right ) +{ + left.x *= right.x; + left.y *= right.y; + return left; +} - template< > inline Float2 abs( const Float2 &value ) - { return Float2( abs(value.x), abs(value.y) ); } +inline ::Oyster::Math::Float2 operator * ( const ::Oyster::Math::Float2 &left, const ::Oyster::Math::Float2 &right ) +{ return ::Oyster::Math::Float2(left) *= right; } - template< > inline Float2 max( const Float2 &valueA, const Float2 &valueB ) - { return Float2( max(valueA.x, valueB.x), max(valueA.y, valueB.y) ); } +inline ::Oyster::Math::Float2 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float2 &right ) +{ return ::Oyster::Math::Float2(right) *= left; } - template< > inline Float2 min( const Float2 &valueA, const Float2 &valueB ) - { return Float2( min(valueA.x, valueB.x), min(valueA.y, valueB.y) ); } +inline ::Oyster::Math::Float3 & operator *= ( ::Oyster::Math::Float3 &left, const ::Oyster::Math::Float3 &right ) +{ + left.x *= right.x; + left.y *= right.y; + left.z *= right.z; + return left; +} - template< > inline Float3 abs( const Float3 &value ) - { return Float3( abs(value.xy), abs(value.z) ); } +inline ::Oyster::Math::Float3 operator * ( const ::Oyster::Math::Float3 &left, const ::Oyster::Math::Float3 &right ) +{ return ::Oyster::Math::Float3(left) *= right; } - template< > inline Float3 max( const Float3 &valueA, const Float3 &valueB ) - { return Float3( max(valueA.xy, valueB.xy), max(valueA.z, valueB.z) ); } +inline ::Oyster::Math::Float3 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float3 &right ) +{ return ::Oyster::Math::Float3(right) *= left; } - template< > inline Float3 min( const Float3 &valueA, const Float3 &valueB ) - { return Float3( min(valueA.xy, valueB.xy), min(valueA.z, valueB.z) ); } +inline ::Oyster::Math::Float4 & operator *= ( ::Oyster::Math::Float4 &left, const ::Oyster::Math::Float4 &right ) +{ + left.x *= right.x; + left.y *= right.y; + left.z *= right.z; + left.w *= right.w; + return left; +} - template< > inline Float4 abs( const Float4 &value ) - { return Float4( abs(value.xyz), abs(value.w) ); } +inline ::Oyster::Math::Float4 operator * ( const ::Oyster::Math::Float4 &left, const ::Oyster::Math::Float4 &right ) +{ return ::Oyster::Math::Float4(left) *= right; } - template< > inline Float4 max( const Float4 &valueA, const Float4 &valueB ) - { return Float4( max(valueA.xyz, valueB.xyz), max(valueA.w, valueB.w) ); } +inline ::Oyster::Math::Float4 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float4 &right ) +{ return ::Oyster::Math::Float4(right) *= left; } - template< > inline Float4 min( const Float4 &valueA, const Float4 &valueB ) - { return Float4( min(valueA.xyz, valueB.xyz), min(valueA.w, valueB.w) ); } +inline ::Oyster::Math::Float2x2 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float2x2 &right ) +{ return ::Oyster::Math::Float2x2(right) *= left; } - template< > inline Float2x2 abs( const Float2x2 &value ) - { return Float2x2( abs(value.v[0]), abs(value.v[1]) ); } +inline ::Oyster::Math::Float3x3 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float3x3 &right ) +{ return ::Oyster::Math::Float3x3(right) *= left; } - template< > inline Float2x2 max( const Float2x2 &valueA, const Float2x2 &valueB ) - { return Float2x2( max(valueA.v[0], valueB.v[0]), max(valueA.v[1], valueB.v[1]) ); } +inline ::Oyster::Math::Float4x4 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float4x4 &right ) +{ return ::Oyster::Math::Float4x4(right) *= left; } - template< > inline Float2x2 min( const Float2x2 &valueA, const Float2x2 &valueB ) - { return Float2x2( min(valueA.v[0], valueB.v[0]), min(valueA.v[1], valueB.v[1]) ); } +namespace Oyster { namespace Math2D /// Oyster's native math library specialized for 2D +{ + using namespace ::Oyster::Math; // deliberate inheritance from ::Oyster::Math namespace - template< > inline Float3x3 abs( const Float3x3 &value ) - { return Float3x3( abs(value.v[0]), abs(value.v[1]), abs(value[2]) ); } + /// If there is an Y-axis on a 2D plane, then there is an explicit X-axis on and that is what is returned. + /// Recommended too make sure that yAxis is normalized. + inline Float2 X_AxisTo( const Float2 &yAxis ) + { return ::LinearAlgebra2D::X_AxisTo(yAxis); } - template< > inline Float3x3 max( const Float3x3 &valueA, const Float3x3 &valueB ) - { return Float3x3( max(valueA.v[0], valueB.v[0]), max(valueA.v[1], valueB.v[1]), max(valueA.v[2], valueB.v[2]) ); } + /// If there is an X-axis on a 2D plane, then there is an explicit Y-axis and that is what is returned. + /// Recommended too make sure that yAxis is normalized. + inline Float2 Y_AxisTo( const Float2 &xAxis ) + { return ::LinearAlgebra2D::Y_AxisTo(xAxis); } - template< > inline Float3x3 min( const Float3x3 &valueA, const Float3x3 &valueB ) - { return Float3x3( min(valueA.v[0], valueB.v[0]), min(valueA.v[1], valueB.v[1]), min(valueA.v[2], valueB.v[2]) ); } + /// Sets and returns targetMem to a translationMatrix with position as translation. + inline Float3x3 & TranslationMatrix( const Float2 &position, Float3x3 &targetMem = Float3x3() ) + { return ::LinearAlgebra2D::TranslationMatrix( position, targetMem ); } - template< > inline Float4x4 abs( const Float4x4 &value ) - { return Float4x4( abs(value.v[0]), abs(value.v[1]), abs(value[2]), abs(value[3]) ); } + /// Sets and returns targetMem as a counterclockwise rotationMatrix + inline Float3x3 & RotationMatrix( const Float &radian, Float3x3 &targetMem = Float3x3() ) + { return ::LinearAlgebra2D::RotationMatrix( radian, targetMem ); } - template< > inline Float4x4 max( const Float4x4 &valueA, const Float4x4 &valueB ) - { return Float4x4( max(valueA.v[0], valueB.v[0]), max(valueA.v[1], valueB.v[1]), max(valueA.v[2], valueB.v[2]), max(valueA.v[3], valueB.v[3]) ); } + /// Sets and returns targetMem as an orientation Matrix with position as translation and radian rotation + inline Float3x3 & OrientationMatrix( const Float2 &position, const Float &radian, Float3x3 &targetMem = Float3x3() ) + { return ::LinearAlgebra2D::OrientationMatrix( radian, position, targetMem ); } - template< > inline Float4x4 min( const Float4x4 &valueA, const Float4x4 &valueB ) - { return Float4x4( min(valueA.v[0], valueB.v[0]), min(valueA.v[1], valueB.v[1]), min(valueA.v[2], valueB.v[2]), min(valueA.v[3], valueB.v[3]) ); } + /// Sets and returns targetMem as an orientation Matrix with position as translation and local y-axis directed at lookAt + inline Float3x3 & OrientationMatrix( const Float2 &position, const Float2 &lookAt, Float3x3 &targetMem = Float3x3() ) + { return ::LinearAlgebra2D::OrientationMatrix( lookAt, position, targetMem ); } + + /// Sets and returns targetMem as an orientation Matrix that is rotated around localCenterOfRotation and then translated with position. + /// TODO: not tested + inline Float3x3 & OrientationMatrix( const Float2 &position, Float radian, const Float2 &localCenterOfRotation, Float3x3 &targetMem = Float3x3() ) + { return ::LinearAlgebra2D::OrientationMatrix( radian, position, localCenterOfRotation, targetMem ); } + + /// If orientationMatrix is assumed to be by all definitions a rigid orientation matrix aka rigid body matrix. Then this is a much faster inverse method. + inline Float3x3 & InverseOrientationMatrix( const Float3x3 &orientationMatrix, Float3x3 &targetMem = Float3x3() ) + { return ::LinearAlgebra2D::InverseOrientationMatrix( orientationMatrix, targetMem ); } +} } + +namespace Oyster { namespace Math3D /// Oyster's native math library specialized for 3D +{ + using namespace ::Oyster::Math; // deliberate inheritance from ::Oyster::Math namespace + + /// Sets and returns targetMem to a translationMatrix with position as translation. + inline Float4x4 & TranslationMatrix( const Float3 &position, Float4x4 &targetMem = Float4x4() ) + { return ::LinearAlgebra3D::TranslationMatrix( position, targetMem ); } + + /// Sets and returns targetMem as an counterclockwise rotation matrix around the global X-axis + inline Float4x4 & RotationMatrix_AxisX( const Float &radian, Float4x4 &targetMem = Float4x4() ) + { return ::LinearAlgebra3D::RotationMatrix_AxisX( radian, targetMem ); } + + /// Sets and returns targetMem as an counterclockwise rotation matrix around the global Y-axis + inline Float4x4 & RotationMatrix_AxisY( const Float &radian, Float4x4 &targetMem = Float4x4() ) + { return ::LinearAlgebra3D::RotationMatrix_AxisY( radian, targetMem ); } + + /// Sets and returns targetMem as an counterclockwise rotation matrix around the global Z-axis + inline Float4x4 & RotationMatrix_AxisZ( const Float &radian, Float4x4 &targetMem = Float4x4() ) + { return ::LinearAlgebra3D::RotationMatrix_AxisZ( radian, targetMem ); } + + /// Sets and returns targetMem as an counterclockwise rotation matrix around the normalizedAxis. + /// Please make sure normalizedAxis is normalized. + inline Float4x4 & RotationMatrix( const Float &radian, const Float3 &normalizedAxis, Float4x4 &targetMem = Float4x4() ) + { return ::LinearAlgebra3D::RotationMatrix( normalizedAxis, radian, targetMem ); } + + /// If orientationMatrix is assumed to be by all definitions a rigid orientation matrix aka rigid body matrix. Then this is a much faster inverse method. + inline Float4x4 & InverseOrientationMatrix( const Float4x4 &orientationMatrix, Float4x4 &targetMem = Float4x4() ) + { return ::LinearAlgebra3D::InverseOrientationMatrix( orientationMatrix, targetMem ); } + + /** Sets and returns targetMem as an orientation Matrix + * @param targetMem: is set to a rigibody matrix that rotate counterclockwise and then translates. + * @param sumDeltaAngularAxis: sum of all ( (1/I) * ( L x D ) )-vectorproducts. There I is known as "moment of inertia", L as "angular momentum vector" and D the "lever vector". + * @param sumTranslation: sum of all the translation vectors. + * @return targetMem + @todo TODO: not tested + */ + inline Float4x4 & OrientationMatrix( const Float3 &sumDeltaAngularAxis, const Float3 &sumTranslation, Float4x4 &targetMem = Float4x4() ) + { return ::LinearAlgebra3D::OrientationMatrix( sumDeltaAngularAxis, sumTranslation, targetMem ); } + + /** Sets and returns targetMem as an orientation Matrix + * @param targetMem: is set to a rigibody matrix that revolve/rotate counterclockwise around centerOfMass and then translates. + * @param sumDeltaAngularAxis: sum of all ( (1/I) * ( L x D ) )-vectorproducts. There I is known as "moment of inertia", L as "angular momentum vector" and D the "lever vector". + * @param sumTranslation: sum of all the translation vectors. + * @param centerOfMass: the point the particles is to revolve around, prior to translation. Default set to null vector aka origo. + * @return targetMem + @todo TODO: not tested + */ + inline Float4x4 & OrientationMatrix( const Float3 &sumDeltaAngularAxis, const Float3 &sumTranslation, const Float3 ¢erOfMass, Float4x4 &targetMem = Float4x4() ) + { return ::LinearAlgebra3D::OrientationMatrix( sumDeltaAngularAxis, sumTranslation, centerOfMass, targetMem ); } + + /** Creates an orthographic projection matrix designed for DirectX enviroment. + * @param targetMem; is set to an orthographic projection matrix. + * @param width; of the projection sample volume. + * @param height; of the projection sample volume. + * @param nearClip: Distance to the nearPlane. + * @param farClip: Distance to the farPlane. + * @return targetMem + @todo TODO: not tested + */ + inline Float4x4 & ProjectionMatrix_Orthographic( const Float &width, const Float &height, const Float &nearClip = ::std::numeric_limits::epsilon(), const Float &farClip = ::std::numeric_limits::max(), Float4x4 &targetMem = Float4x4() ) + { return ::LinearAlgebra3D::ProjectionMatrix_Orthographic( width, height, nearClip, farClip, targetMem ); } + + /** Creates a perspective projection matrix designed for DirectX enviroment. + * @param targetMem; is set to a perspective transform matrix. + * @param vertFoV; is the vertical field of vision in radians. (lookup FoV Hor+ ) + * @param aspect; is the screenratio width/height (example 16/9 or 16/10 ) + * @param nearClip: Distance to the nearPlane + * @param farClip: Distance to the farPlane + * @return targetMem + @todo TODO: not tested + */ + inline Float4x4 & ProjectionMatrix_Perspective( const Float &verticalFoV, const Float &aspectRatio, const Float &nearClip = ::std::numeric_limits::epsilon(), const Float &farClip = ::std::numeric_limits::max(), Float4x4 &targetMem = Float4x4() ) + { return ::LinearAlgebra3D::ProjectionMatrix_Perspective( verticalFoV, aspectRatio, nearClip, farClip, targetMem ); } + + /// returns the component vector of vector that is parallell with axis + inline Float3 VectorProjection( const Float3 &vector, const Float3 &axis ) + { return ::LinearAlgebra3D::VectorProjection( vector, axis ); } + + /// Helper inline function that sets and then returns targetMem = projection * view + inline Float4x4 & ViewProjectionMatrix( const Float4x4 &view, const Float4x4 &projection, Float4x4 &targetMem = Float4x4() ) + { return targetMem = projection * view; } + + /// Helper inline function that sets and then returns targetMem = transformer * transformee + inline Float4x4 & TransformMatrix( const Float4x4 &transformer, const Float4x4 &transformee, Float4x4 &targetMem = Float4x4() ) + { return targetMem = transformer * transformee; } + + /// Helper inline function that sets and then returns targetMem = transformer * transformee + inline Float4 & TransformVector( const Float4x4 &transformer, const Float4 &transformee, Float4 &targetMem = Float4() ) + { return targetMem = transformer * transformee; } } } #endif \ No newline at end of file diff --git a/OysterMath/Quaternion.h b/OysterMath/Quaternion.h index 383c0245..fd19100f 100644 --- a/OysterMath/Quaternion.h +++ b/OysterMath/Quaternion.h @@ -3,7 +3,6 @@ // © Dan Andersson 2013 ///////////////////////////////////////////////////////////////////// -#pragma once #ifndef LINEARALGEBRA_QUATERNION_H #define LINEARALGEBRA_QUATERNION_H @@ -12,172 +11,170 @@ namespace LinearAlgebra { - template - class Quaternion + template + struct Quaternion { public: union { - struct{ Vector3 imaginary; ElementType real; }; - ElementType element[4]; - char byte[sizeof(ElementType[2])]; + struct{ Vector3 imaginary; ScalarType real; }; + ScalarType element[4]; + char byte[sizeof(ScalarType[2])]; }; Quaternion( ); Quaternion( const Quaternion &quaternion ); - Quaternion( const Vector3 &imaginary, const ElementType &real ); - ~Quaternion( ); + Quaternion( const Vector3 &imaginary, const ScalarType &real ); - operator ElementType* ( ); - operator const ElementType* ( ) const; + operator ScalarType* ( ); + operator const ScalarType* ( ) const; operator char* ( ); operator const char* ( ) const; - ElementType & operator [] ( int i ); - const ElementType & operator [] ( int i ) const; + ScalarType & operator [] ( int i ); + const ScalarType & operator [] ( int i ) const; - Quaternion & operator = ( const Quaternion &quaternion ); - Quaternion & operator *= ( const ElementType &scalar ); - Quaternion & operator /= ( const ElementType &scalar ); - Quaternion & operator += ( const Quaternion &quaternion ); - Quaternion & operator -= ( const Quaternion &quaternion ); - Quaternion operator * ( const Quaternion &quaternion ) const; - Quaternion operator * ( const Vector3 &vector ) const; - Quaternion operator * ( const ElementType &scalar ) const; - Quaternion operator / ( const ElementType &scalar ) const; - Quaternion operator + ( const Quaternion &quaternion ) const; - Quaternion operator - ( const Quaternion &quaternion ) const; - Quaternion operator - ( ) const; + Quaternion & operator = ( const Quaternion &quaternion ); + Quaternion & operator *= ( const ScalarType &scalar ); + Quaternion & operator /= ( const ScalarType &scalar ); + Quaternion & operator += ( const Quaternion &quaternion ); + Quaternion & operator -= ( const Quaternion &quaternion ); + Quaternion operator * ( const Quaternion &quaternion ) const; + Quaternion operator * ( const Vector3 &vector ) const; + Quaternion operator * ( const ScalarType &scalar ) const; + Quaternion operator / ( const ScalarType &scalar ) const; + Quaternion operator + ( const Quaternion &quaternion ) const; + Quaternion operator - ( const Quaternion &quaternion ) const; + Quaternion operator - ( ) const; - Quaternion getConjugate( ) const; + Quaternion GetConjugate( ) const; }; /////////////////////////////////////////////////////////////////////////////////// // Body /////////////////////////////////////////////////////////////////////////////////// - template - Quaternion::Quaternion( ) : imaginary(0,0,0), real(0) {} + template + Quaternion::Quaternion( ) : imaginary(), real() {} - template - Quaternion::Quaternion( const Quaternion &quaternion ) : imaginary(quaternion.imaginary), real(quaternion.real) {} + template + Quaternion::Quaternion( const Quaternion &quaternion ) + { this->imaginary = quaternion.imaginary; this->real = quaternion.real; } - template - Quaternion::Quaternion( const Vector3 &_imaginary, const ElementType &_real ) : imaginary(_imaginary), real(_real) {} - - template - Quaternion::~Quaternion( ) { /* Nothing that needs to be done */ } + template + Quaternion::Quaternion( const Vector3 &_imaginary, const ScalarType &_real ) + { this->imaginary = _imaginary; this->real = _real; } - template - inline Quaternion::operator ElementType* ( ) + template + inline Quaternion::operator ScalarType* ( ) { return this->element; } - template - inline Quaternion::operator const ElementType* ( ) const + template + inline Quaternion::operator const ScalarType* ( ) const { return this->element; } - template - inline Quaternion::operator char* ( ) + template + inline Quaternion::operator char* ( ) { return this->byte; } - template - inline Quaternion::operator const char* ( ) const + template + inline Quaternion::operator const char* ( ) const { return this->byte; } - template - inline ElementType & Quaternion::operator [] ( int i ) + template + inline ScalarType & Quaternion::operator [] ( int i ) { return this->element[i]; } - template - inline const ElementType & Quaternion::operator [] ( int i ) const + template + inline const ScalarType & Quaternion::operator [] ( int i ) const { return this->element[i]; } - template - Quaternion & Quaternion::operator = ( const Quaternion &quaternion ) + template + Quaternion & Quaternion::operator = ( const Quaternion &quaternion ) { this->imaginary = quaternion.imaginary; this->real = quaternion.real; return *this; } - template - Quaternion & Quaternion::operator *= ( const ElementType &scalar ) + template + Quaternion & Quaternion::operator *= ( const ScalarType &scalar ) { this->imaginary *= scalar; this->real *= scalar; return *this; } - template - Quaternion & Quaternion::operator /= ( const ElementType &scalar ) + template + Quaternion & Quaternion::operator /= ( const ScalarType &scalar ) { this->imaginary /= scalar; this->real /= scalar; return *this; } - template - Quaternion & Quaternion::operator += ( const Quaternion &quaternion ) + template + Quaternion & Quaternion::operator += ( const Quaternion &quaternion ) { this->imaginary += quaternion.imaginary; this->real += quaternion.real; return *this; } - template - Quaternion & Quaternion::operator -= ( const Quaternion &quaternion ) + template + Quaternion & Quaternion::operator -= ( const Quaternion &quaternion ) { this->imaginary -= quaternion.imaginary; this->real -= quaternion.real; return *this; } - template - Quaternion Quaternion::operator * ( const Quaternion &quaternion ) const + template + Quaternion Quaternion::operator * ( const Quaternion &quaternion ) const { - Vector3 im = this->imaginary.cross( quaternion.imaginary ); + Vector3 im = this->imaginary.Cross( quaternion.imaginary ); im += (quaternion.imaginary * this->real); im += (this->imaginary * quaternion.real); - ElementType re = this->real * quaternion.real; - re -= this->imaginary.dot( quaternion.imaginary ); + ScalarType re = this->real * quaternion.real; + re -= this->imaginary.Dot( quaternion.imaginary ); - return Quaternion( im, re ); + return Quaternion( im, re ); } - template - Quaternion Quaternion::operator * ( const Vector3 &vector ) const + template + Quaternion Quaternion::operator * ( const Vector3 &vector ) const { - Vector3 im = this->imaginary.cross( vector ); + Vector3 im = this->imaginary.Cross( vector ); im += (vector * this->real); - ElementType re = this->imaginary.dot( vector ) * -1; + ScalarType re = this->imaginary.Dot( vector ) * -1; - return Quaternion( im, re ); + return Quaternion( im, re ); } - template - inline Quaternion Quaternion::operator * ( const ElementType &scalar ) const - { return Quaternion(*this) *= scalar; } + template + inline Quaternion Quaternion::operator * ( const ScalarType &scalar ) const + { return Quaternion(*this) *= scalar; } - template - inline Quaternion Quaternion::operator / ( const ElementType &scalar ) const - { return Quaternion(*this) /= scalar; } + template + inline Quaternion Quaternion::operator / ( const ScalarType &scalar ) const + { return Quaternion(*this) /= scalar; } - template - inline Quaternion Quaternion::operator + ( const Quaternion &quaternion ) const - { return Quaternion(*this) += quaternion; } + template + inline Quaternion Quaternion::operator + ( const Quaternion &quaternion ) const + { return Quaternion(*this) += quaternion; } - template - inline Quaternion Quaternion::operator - ( const Quaternion &quaternion ) const - { return Quaternion(*this) -= quaternion; } + template + inline Quaternion Quaternion::operator - ( const Quaternion &quaternion ) const + { return Quaternion(*this) -= quaternion; } - template - inline Quaternion Quaternion::operator - ( ) const - { return Quaternion(-this->imaginary, -this->real); } + template + inline Quaternion Quaternion::operator - ( ) const + { return Quaternion(-this->imaginary, -this->real); } - template - inline Quaternion Quaternion::getConjugate( ) const - { return Quaternion(this->imaginary * -1, this->real ); } + template + inline Quaternion Quaternion::GetConjugate( ) const + { return Quaternion(-this->imaginary, this->real ); } } #endif \ No newline at end of file diff --git a/OysterMath/Vector.h b/OysterMath/Vector.h index 086ed7d8..1f779ab1 100644 --- a/OysterMath/Vector.h +++ b/OysterMath/Vector.h @@ -3,7 +3,6 @@ // © Dan Andersson 2013 ///////////////////////////////////////////////////////////////////// -#pragma once #ifndef LINEARALGEBRA_VECTOR_H #define LINEARALGEBRA_VECTOR_H @@ -11,444 +10,453 @@ namespace LinearAlgebra { - template - class Vector2 + template + struct Vector2 { public: union { - struct { ElementType x, y; }; - ElementType element[2]; - char byte[sizeof(ElementType[2])]; + struct { ScalarType x, y; }; + ScalarType element[2]; + char byte[sizeof(ScalarType[2])]; }; - static const Vector2 null; - static const Vector2 standardUnitX; - static const Vector2 standardUnitY; + static const Vector2 null; + static const Vector2 standard_unit_X; + static const Vector2 standard_unit_Y; Vector2( ); - Vector2( const Vector2 &vector ); - Vector2( const ElementType &element ); - Vector2( const ElementType element[2] ); - Vector2( const ElementType &x, const ElementType &y ); - ~Vector2( ); + Vector2( const Vector2 &vector ); + Vector2( const ScalarType &element ); + Vector2( const ScalarType element[2] ); + Vector2( const ScalarType &x, const ScalarType &y ); - operator ElementType* ( ); - operator const ElementType* ( ) const; + operator ScalarType* ( ); + operator const ScalarType* ( ) const; operator char* ( ); operator const char* ( ) const; - ElementType & operator [] ( int i ); - const ElementType & operator [] ( int i ) const; + ScalarType & operator [] ( int i ); + const ScalarType & operator [] ( int i ) const; - Vector2 & operator = ( const Vector2 &vector ); - Vector2 & operator = ( const ElementType element[2] ); - Vector2 & operator *= ( const ElementType &scalar ); - Vector2 & operator /= ( const ElementType &scalar ); - Vector2 & operator += ( const Vector2 &vector ); - Vector2 & operator -= ( const Vector2 &vector ); - Vector2 operator * ( const ElementType &scalar ) const; - Vector2 operator / ( const ElementType &scalar ) const; - Vector2 operator + ( const Vector2 &vector ) const; - Vector2 operator - ( const Vector2 &vector ) const; - Vector2 operator - ( ) const; // unary negation + Vector2 & operator = ( const Vector2 &vector ); + Vector2 & operator = ( const ScalarType element[2] ); + Vector2 & operator *= ( const ScalarType &scalar ); + Vector2 & operator /= ( const ScalarType &scalar ); + Vector2 & operator += ( const Vector2 &vector ); + Vector2 & operator -= ( const Vector2 &vector ); + Vector2 operator * ( const ScalarType &scalar ) const; + Vector2 operator / ( const ScalarType &scalar ) const; + Vector2 operator + ( const Vector2 &vector ) const; + Vector2 operator - ( const Vector2 &vector ) const; + Vector2 operator - ( ) const; // unary negation - bool operator == ( const Vector2 &vector ) const; - bool operator != ( const Vector2 &vector ) const; + bool operator == ( const Vector2 &vector ) const; + bool operator != ( const Vector2 &vector ) const; - ElementType length( ) const; - ElementType dot( const Vector2 &vector ) const; + ScalarType GetLength( ) const; + ScalarType GetMagnitude( ) const; + ScalarType Dot( const Vector2 &vector ) const; - Vector2 & normalize( ); - Vector2 getNormalized( ) const; + Vector2 & Normalize( ); + Vector2 GetNormalized( ) const; }; - template - class Vector3 + template + struct Vector3 { public: union { - struct { ElementType x, y, z; }; - struct { Vector2 xy; }; - ElementType element[3]; - char byte[sizeof(ElementType[3])]; + struct { ScalarType x, y, z; }; + struct { Vector2 xy; }; + ScalarType element[3]; + char byte[sizeof(ScalarType[3])]; }; - static const Vector3 null; - static const Vector3 standardUnitX; - static const Vector3 standardUnitY; - static const Vector3 standardUnitZ; + static const Vector3 null; + static const Vector3 standard_unit_X; + static const Vector3 standard_unit_Y; + static const Vector3 standard_unit_Z; Vector3( ); - Vector3( const Vector3 &vector ); - Vector3( const Vector2 &vector, const ElementType &z ); - Vector3( const ElementType &element ); - Vector3( const ElementType element[3] ); - Vector3( const ElementType &x, const ElementType &y, const ElementType &z ); - ~Vector3( ); + Vector3( const Vector3 &vector ); + Vector3( const Vector2 &vector, const ScalarType &z ); + Vector3( const ScalarType &element ); + Vector3( const ScalarType element[3] ); + Vector3( const ScalarType &x, const ScalarType &y, const ScalarType &z ); - operator ElementType* (); - operator const ElementType* () const; + operator ScalarType* (); + operator const ScalarType* () const; operator char* ( ); operator const char* ( ) const; - ElementType & operator [] ( int i ); - const ElementType & operator [] ( int i ) const; + ScalarType & operator [] ( int i ); + const ScalarType & operator [] ( int i ) const; - Vector3 & operator = ( const Vector3 &vector ); - Vector3 & operator = ( const ElementType element[3] ); - Vector3 & operator *= ( const ElementType &scalar ); - Vector3 & operator /= ( const ElementType &scalar ); - Vector3 & operator += ( const Vector3 &vector ); - Vector3 & operator -= ( const Vector3 &vector ); - Vector3 operator * ( const ElementType &scalar ) const; - Vector3 operator / ( const ElementType &scalar ) const; - Vector3 operator + ( const Vector3 &vector ) const; - Vector3 operator - ( const Vector3 &vector ) const; - Vector3 operator - ( ) const; // unary negation + Vector3 & operator = ( const Vector3 &vector ); + Vector3 & operator = ( const ScalarType element[3] ); + Vector3 & operator *= ( const ScalarType &scalar ); + Vector3 & operator /= ( const ScalarType &scalar ); + Vector3 & operator += ( const Vector3 &vector ); + Vector3 & operator -= ( const Vector3 &vector ); + Vector3 operator * ( const ScalarType &scalar ) const; + Vector3 operator / ( const ScalarType &scalar ) const; + Vector3 operator + ( const Vector3 &vector ) const; + Vector3 operator - ( const Vector3 &vector ) const; + Vector3 operator - ( ) const; // unary negation - bool operator == ( const Vector3 &vector ) const; - bool operator != ( const Vector3 &vector ) const; + bool operator == ( const Vector3 &vector ) const; + bool operator != ( const Vector3 &vector ) const; - ElementType length( ) const; - ElementType dot( const Vector3 &vector ) const; - Vector3 cross( const Vector3 &vector ) const; + ScalarType GetLength( ) const; + ScalarType GetMagnitude( ) const; + ScalarType Dot( const Vector3 &vector ) const; + Vector3 Cross( const Vector3 &vector ) const; - Vector3 & normalize( ); - Vector3 getNormalized( ) const; + Vector3 & Normalize( ); + Vector3 GetNormalized( ) const; }; - template - class Vector4 + template + struct Vector4 { public: union { - struct { ElementType x, y, z, w; }; - struct { Vector2 xy; }; - struct { Vector3 xyz; }; - ElementType element[4]; - char byte[sizeof(ElementType[4])]; + struct { ScalarType x, y, z, w; }; + struct { Vector2 xy; }; + struct { Vector3 xyz; }; + ScalarType element[4]; + char byte[sizeof(ScalarType[4])]; }; - static const Vector4 null; - static const Vector4 standardUnitX; - static const Vector4 standardUnitY; - static const Vector4 standardUnitZ; - static const Vector4 standardUnitW; + static const Vector4 null; + static const Vector4 standard_unit_X; + static const Vector4 standard_unit_Y; + static const Vector4 standard_unit_Z; + static const Vector4 standard_unit_W; Vector4( ); - Vector4( const Vector4 &vector ); - Vector4( const Vector3 &vector, const ElementType &w ); - Vector4( const Vector2 &vector, const ElementType &z, const ElementType &w ); - Vector4( const ElementType &element ); - Vector4( const ElementType element[4] ); - Vector4( const ElementType &x, const ElementType &y, const ElementType &z, const ElementType &w ); - ~Vector4( ); + Vector4( const Vector4 &vector ); + Vector4( const Vector3 &vector, const ScalarType &w ); + Vector4( const Vector2 &vector, const ScalarType &z, const ScalarType &w ); + Vector4( const ScalarType &element ); + Vector4( const ScalarType element[4] ); + Vector4( const ScalarType &x, const ScalarType &y, const ScalarType &z, const ScalarType &w ); - operator ElementType* (); - operator const ElementType* () const; + operator ScalarType* (); + operator const ScalarType* () const; operator char* ( ); operator const char* ( ) const; - ElementType & operator [] ( int i ); - const ElementType & operator [] ( int i ) const; + ScalarType & operator [] ( int i ); + const ScalarType & operator [] ( int i ) const; - Vector4 & operator = ( const Vector4 &vector ); - Vector4 & operator = ( const ElementType element[4] ); - Vector4 & operator *= ( const ElementType &scalar ); - Vector4 & operator /= ( const ElementType &scalar ); - Vector4 & operator += ( const Vector4 &vector ); - Vector4 & operator -= ( const Vector4 &vector ); - Vector4 operator * ( const ElementType &scalar ) const; - Vector4 operator / ( const ElementType &scalar ) const; - Vector4 operator + ( const Vector4 &vector ) const; - Vector4 operator - ( const Vector4 &vector ) const; - Vector4 operator - ( ) const; // unary negation + Vector4 & operator = ( const Vector4 &vector ); + Vector4 & operator = ( const ScalarType element[4] ); + Vector4 & operator *= ( const ScalarType &scalar ); + Vector4 & operator /= ( const ScalarType &scalar ); + Vector4 & operator += ( const Vector4 &vector ); + Vector4 & operator -= ( const Vector4 &vector ); + Vector4 operator * ( const ScalarType &scalar ) const; + Vector4 operator / ( const ScalarType &scalar ) const; + Vector4 operator + ( const Vector4 &vector ) const; + Vector4 operator - ( const Vector4 &vector ) const; + Vector4 operator - ( ) const; // unary negation - bool operator == ( const Vector4 &vector ) const; - bool operator != ( const Vector4 &vector ) const; + bool operator == ( const Vector4 &vector ) const; + bool operator != ( const Vector4 &vector ) const; - ElementType length( ) const; - ElementType dot( const Vector4 &vector ) const; + ScalarType GetLength( ) const; + ScalarType GetMagnitude( ) const; + ScalarType Dot( const Vector4 &vector ) const; - Vector4 & normalize( ); - Vector4 getNormalized( ) const; + Vector4 & Normalize( ); + Vector4 GetNormalized( ) const; }; /////////////////////////////////////////////////////////////////////////////////// // Body /////////////////////////////////////////////////////////////////////////////////// -// Vector2 /////////////////////////////////////// - template const Vector2 Vector2::null = Vector2( ); - template const Vector2 Vector2::standardUnitX = Vector2( 1, 0 ); - template const Vector2 Vector2::standardUnitY = Vector2( 0, 1 ); +// Vector2 /////////////////////////////////////// - template - Vector2::Vector2( ) : x(0), y(0) {} + template const Vector2 Vector2::null = Vector2( ); + template const Vector2 Vector2::standard_unit_X = Vector2( 1, 0 ); + template const Vector2 Vector2::standard_unit_Y = Vector2( 0, 1 ); - template - Vector2::Vector2( const Vector2 &vector ) : x(vector.x), y(vector.y) + template + Vector2::Vector2( ) : x(), y() {} + + template + Vector2::Vector2( const Vector2 &vector ) { this->x = vector.x; this->y = vector.y; } - template - Vector2::Vector2( const ElementType &_element ) : x(_element), y(_element) + template + Vector2::Vector2( const ScalarType &_element ) { this->x = this->y = _element; } - template - Vector2::Vector2( const ElementType _element[2] ) : x(_element[0]), y(_element[1]) {} + template + Vector2::Vector2( const ScalarType _element[2] ) + { this->x = _element[0]; this->y = _element[1]; } - template - Vector2::Vector2( const ElementType &_x, const ElementType &_y ) : x(_x), y(_y) {} + template + Vector2::Vector2( const ScalarType &_x, const ScalarType &_y ) + { this->x = _x; this->y = _y; } - template - Vector2::~Vector2( ) { /* Nothing that needs to be done */ } - - template - inline Vector2::operator ElementType* () + template + inline Vector2::operator ScalarType* () { return this->element; } - template - inline Vector2::operator const ElementType* () const + template + inline Vector2::operator const ScalarType* () const { return this->element; } - template - inline Vector2::operator char* ( ) + template + inline Vector2::operator char* ( ) { return this->byte; } - template - inline Vector2::operator const char* ( ) const + template + inline Vector2::operator const char* ( ) const { return this->byte; } - template - inline ElementType & Vector2::operator [] ( int i ) + template + inline ScalarType & Vector2::operator [] ( int i ) { return this->element[i]; } - template - inline const ElementType & Vector2::operator [] ( int i ) const + template + inline const ScalarType & Vector2::operator [] ( int i ) const { return this->element[i]; } - template - Vector2 & Vector2::operator = ( const Vector2 &vector ) + template + Vector2 & Vector2::operator = ( const Vector2 &vector ) { this->element[0] = vector.element[0]; this->element[1] = vector.element[1]; return *this; } - template - Vector2 & Vector2::operator = ( const ElementType _element[2] ) + template + Vector2 & Vector2::operator = ( const ScalarType _element[2] ) { this->element[0] = _element[0]; this->element[1] = _element[1]; return *this; } - template - Vector2 & Vector2::operator *= ( const ElementType &scalar ) + template + Vector2 & Vector2::operator *= ( const ScalarType &scalar ) { this->element[0] *= scalar; this->element[1] *= scalar; return *this; } - template - Vector2 & Vector2::operator /= ( const ElementType &scalar ) + template + Vector2 & Vector2::operator /= ( const ScalarType &scalar ) { this->element[0] /= scalar; this->element[1] /= scalar; return *this; } - template - Vector2 & Vector2::operator += ( const Vector2 &vector ) + template + Vector2 & Vector2::operator += ( const Vector2 &vector ) { this->element[0] += vector.element[0]; this->element[1] += vector.element[1]; return *this; } - template - Vector2 & Vector2::operator -= ( const Vector2 &vector ) + template + Vector2 & Vector2::operator -= ( const Vector2 &vector ) { this->element[0] -= vector.element[0]; this->element[1] -= vector.element[1]; return *this; } - template - inline Vector2 Vector2::operator * ( const ElementType &scalar ) const - { return Vector2(*this) *= scalar; } + template + inline Vector2 Vector2::operator * ( const ScalarType &scalar ) const + { return Vector2(*this) *= scalar; } - template - inline Vector2 Vector2::operator / ( const ElementType &scalar ) const - { return Vector2(*this) /= scalar; } + template + inline Vector2 Vector2::operator / ( const ScalarType &scalar ) const + { return Vector2(*this) /= scalar; } - template - inline Vector2 Vector2::operator + ( const Vector2 &vector ) const - { return Vector2(*this) += vector; } + template + inline Vector2 Vector2::operator + ( const Vector2 &vector ) const + { return Vector2(*this) += vector; } - template - inline Vector2 Vector2::operator - ( const Vector2 &vector ) const - { return Vector2(*this) -= vector; } + template + inline Vector2 Vector2::operator - ( const Vector2 &vector ) const + { return Vector2(*this) -= vector; } - template - inline Vector2 Vector2::operator - ( ) const - { return Vector2(-this->x, -this->y); } + template + inline Vector2 Vector2::operator - ( ) const + { return Vector2(-this->x, -this->y); } - template - bool Vector2::operator == ( const Vector2 &vector ) const + template + bool Vector2::operator == ( const Vector2 &vector ) const { if( this->x != vector.x ) return false; if( this->y != vector.y ) return false; return true; } - template - bool Vector2::operator != ( const Vector2 &vector ) const + template + bool Vector2::operator != ( const Vector2 &vector ) const { if( this->x != vector.x ) return true; if( this->y != vector.y ) return true; return false; } - template - inline ElementType Vector2::length( ) const - { return (ElementType) ::sqrt( this->dot(*this) ); } + template + inline ScalarType Vector2::GetLength( ) const + { return this->GetMagnitude(); } - template - ElementType Vector2::dot( const Vector2 &vector ) const + template + inline ScalarType Vector2::GetMagnitude( ) const + { return (ScalarType) ::sqrt( this->Dot(*this) ); } + + template + ScalarType Vector2::Dot( const Vector2 &vector ) const { - ElementType value = 0; + ScalarType value = 0; value += this->element[0] * vector.element[0]; value += this->element[1] * vector.element[1]; return value; } - template - inline Vector2 & Vector2::normalize( ) - { return (*this) /= this->length(); } + template + inline Vector2 & Vector2::Normalize( ) + { return (*this) /= this->GetLength(); } - template - inline Vector2 Vector2::getNormalized( ) const - { return Vector2(*this).normalize(); } + template + inline Vector2 Vector2::GetNormalized( ) const + { return Vector2(*this).Normalize(); } -// Vector3 /////////////////////////////////////// - template const Vector3 Vector3::null = Vector3( ); - template const Vector3 Vector3::standardUnitX = Vector3( 1, 0, 0 ); - template const Vector3 Vector3::standardUnitY = Vector3( 0, 1, 0 ); - template const Vector3 Vector3::standardUnitZ = Vector3( 0, 0, 1 ); +// Vector3 /////////////////////////////////////// - template - Vector3::Vector3( ) : x(0), y(0), z(0) {} + template const Vector3 Vector3::null = Vector3( ); + template const Vector3 Vector3::standard_unit_X = Vector3( 1, 0, 0 ); + template const Vector3 Vector3::standard_unit_Y = Vector3( 0, 1, 0 ); + template const Vector3 Vector3::standard_unit_Z = Vector3( 0, 0, 1 ); - template - Vector3::Vector3( const Vector3 &vector ) : x(vector.x), y(vector.y), z(vector.z) + template + Vector3::Vector3( ) : x(), y(), z() {} + + template + Vector3::Vector3( const Vector3 &vector ) { this->x = vector.x; this->y = vector.y; this->z = vector.z; } - template - Vector3::Vector3( const Vector2 &vector, const ElementType &_z ) : x(vector.x), y(vector.y), z(_z) - { this->x = vector.x; this->y = vector.y; } + template + Vector3::Vector3( const Vector2 &vector, const ScalarType &_z ) + { this->x = vector.x; this->y = vector.y; this->z = _z; } - template - Vector3::Vector3( const ElementType &_element ) : x(_element), y(_element), z(_element) + template + Vector3::Vector3( const ScalarType &_element ) { this->x = this->y = this->z = _element; } - template - Vector3::Vector3( const ElementType _element[3] ) : x(_element[0]), y(_element[1]), z(_element[2]) {} + template + Vector3::Vector3( const ScalarType _element[3] ) + { this->x = _element[0]; this->y = _element[1]; this->z = _element[2]; } - template - Vector3::Vector3( const ElementType &_x, const ElementType &_y, const ElementType &_z ) : x(_x), y(_y), z(_z) + template + Vector3::Vector3( const ScalarType &_x, const ScalarType &_y, const ScalarType &_z ) { this->x = _x; this->y = _y; this->z = _z; } - template - Vector3::~Vector3( ) { /* Nothing that needs to be done */ } - - template - inline Vector3::operator ElementType* () + template + inline Vector3::operator ScalarType* () { return this->element; } - template - inline Vector3::operator const ElementType* () const + template + inline Vector3::operator const ScalarType* () const { return this->element; } - template - inline ElementType & Vector3::operator [] ( int i ) + template + inline ScalarType & Vector3::operator [] ( int i ) { return this->element[i]; } - template - inline const ElementType & Vector3::operator [] ( int i ) const + template + inline const ScalarType & Vector3::operator [] ( int i ) const { return this->element[i]; } - template - Vector3 & Vector3::operator = ( const Vector3 &vector ) + template + Vector3 & Vector3::operator = ( const Vector3 &vector ) { - for( int i = 0; i < 3; ++i ) - this->element[i] = vector.element[i]; + this->element[0] = vector.element[0]; + this->element[1] = vector.element[1]; + this->element[2] = vector.element[2]; return *this; } - template - Vector3 & Vector3::operator = ( const ElementType element[3] ) + template + Vector3 & Vector3::operator = ( const ScalarType element[3] ) { - for( int i = 0; i < 3; ++i ) - this->element[i] = element[i]; + this->element[0] = element[0]; + this->element[1] = element[1]; + this->element[2] = element[2]; return *this; } - template - Vector3 & Vector3::operator *= ( const ElementType &scalar ) + template + Vector3 & Vector3::operator *= ( const ScalarType &scalar ) { - for( int i = 0; i < 3; ++i ) - this->element[i] *= scalar; + this->element[0] *= scalar; + this->element[1] *= scalar; + this->element[2] *= scalar; return *this; } - template - Vector3 & Vector3::operator /= ( const ElementType &scalar ) + template + Vector3 & Vector3::operator /= ( const ScalarType &scalar ) { - for( int i = 0; i < 3; ++i ) - this->element[i] /= scalar; + this->element[0] /= scalar; + this->element[1] /= scalar; + this->element[2] /= scalar; return *this; } - template - Vector3 & Vector3::operator += ( const Vector3 &vector ) + template + Vector3 & Vector3::operator += ( const Vector3 &vector ) { - for( int i = 0; i < 3; ++i ) - this->element[i] += vector.element[i]; + this->element[0] += vector.element[0]; + this->element[1] += vector.element[1]; + this->element[2] += vector.element[2]; return *this; } - template - Vector3 & Vector3::operator -= ( const Vector3 &vector ) + template + Vector3 & Vector3::operator -= ( const Vector3 &vector ) { - for( int i = 0; i < 3; ++i ) - this->element[i] -= vector.element[i]; + this->element[0] -= vector.element[0]; + this->element[1] -= vector.element[1]; + this->element[2] -= vector.element[2]; return *this; } - template - inline Vector3 Vector3::operator * ( const ElementType &scalar ) const - { return Vector3(*this) *= scalar; } + template + inline Vector3 Vector3::operator * ( const ScalarType &scalar ) const + { return Vector3(*this) *= scalar; } - template - inline Vector3 Vector3::operator / ( const ElementType &scalar ) const - { return Vector3(*this) /= scalar; } + template + inline Vector3 Vector3::operator / ( const ScalarType &scalar ) const + { return Vector3(*this) /= scalar; } - template - inline Vector3 Vector3::operator + ( const Vector3 &vector ) const - { return Vector3(*this) += vector; } + template + inline Vector3 Vector3::operator + ( const Vector3 &vector ) const + { return Vector3(*this) += vector; } - template - inline Vector3 Vector3::operator - ( const Vector3 &vector ) const - { return Vector3(*this) -= vector; } + template + inline Vector3 Vector3::operator - ( const Vector3 &vector ) const + { return Vector3(*this) -= vector; } - template - inline Vector3 Vector3::operator - ( ) const - { return Vector3(-this->x, -this->y, -this->z); } + template + inline Vector3 Vector3::operator - ( ) const + { return Vector3(-this->x, -this->y, -this->z); } - template - bool Vector3::operator == ( const Vector3 &vector ) const + template + bool Vector3::operator == ( const Vector3 &vector ) const { if( this->x != vector.x ) return false; if( this->y != vector.y ) return false; @@ -456,8 +464,8 @@ namespace LinearAlgebra return true; } - template - bool Vector3::operator != ( const Vector3 &vector ) const + template + bool Vector3::operator != ( const Vector3 &vector ) const { if( this->x != vector.x ) return true; if( this->y != vector.y ) return true; @@ -465,157 +473,173 @@ namespace LinearAlgebra return false; } - template - inline ElementType Vector3::length( ) const - { return (ElementType) ::sqrt( this->dot(*this) ); } + template + inline ScalarType Vector3::GetLength( ) const + { return this->GetMagnitude(); } - template - ElementType Vector3::dot( const Vector3 &vector ) const + template + inline ScalarType Vector3::GetMagnitude( ) const + { return (ScalarType) ::sqrt( this->Dot(*this) ); } + + template + ScalarType Vector3::Dot( const Vector3 &vector ) const { - ElementType value = 0; - for( int i = 0; i < 3; ++i ) - value += this->element[i] * vector.element[i]; + ScalarType value = 0; + value += this->element[0] * vector.element[0]; + value += this->element[1] * vector.element[1]; + value += this->element[2] * vector.element[2]; return value; } - template - Vector3 Vector3::cross( const Vector3 &vector ) const + template + Vector3 Vector3::Cross( const Vector3 &vector ) const { - return Vector3( (this->y*vector.z) - (this->z*vector.y), + return Vector3( (this->y*vector.z) - (this->z*vector.y), (this->z*vector.x) - (this->x*vector.z), (this->x*vector.y) - (this->y*vector.x) ); } - template - inline Vector3 & Vector3::normalize( ) - { return (*this) /= this->length(); } + template + inline Vector3 & Vector3::Normalize( ) + { return (*this) /= this->GetLength(); } - template - inline Vector3 Vector3::getNormalized( ) const - { return Vector3(*this).normalize(); } + template + inline Vector3 Vector3::GetNormalized( ) const + { return Vector3(*this).Normalize(); } -// Vector4 /////////////////////////////////////// - template const Vector4 Vector4::null = Vector4( ); - template const Vector4 Vector4::standardUnitX = Vector4( 1, 0, 0, 0 ); - template const Vector4 Vector4::standardUnitY = Vector4( 0, 1, 0, 0 ); - template const Vector4 Vector4::standardUnitZ = Vector4( 0, 0, 1, 0 ); - template const Vector4 Vector4::standardUnitW = Vector4( 0, 0, 0, 1 ); +// Vector4 /////////////////////////////////////// - template - Vector4::Vector4( ) : x(0), y(0), z(0), w(0) {} + template const Vector4 Vector4::null = Vector4( ); + template const Vector4 Vector4::standard_unit_X = Vector4( 1, 0, 0, 0 ); + template const Vector4 Vector4::standard_unit_Y = Vector4( 0, 1, 0, 0 ); + template const Vector4 Vector4::standard_unit_Z = Vector4( 0, 0, 1, 0 ); + template const Vector4 Vector4::standard_unit_W = Vector4( 0, 0, 0, 1 ); - template - Vector4::Vector4( const Vector4 &vector ) : x(vector.x), y(vector.y), z(vector.z), w(vector.z) + template + Vector4::Vector4( ) : x(), y(), z(), w() {} + + template + Vector4::Vector4( const Vector4 &vector ) { this->x = vector.x; this->y = vector.y; this->z = vector.z; this->w = vector.w; } - template - Vector4::Vector4( const Vector3 &vector, const ElementType &_w ) : x(vector.x), y(vector.y), z(vector.z), w(_w) - { this->x = vector.x; this->y = vector.y; this->z = vector.z; } + template + Vector4::Vector4( const Vector3 &vector, const ScalarType &_w ) + { this->x = vector.x; this->y = vector.y; this->z = vector.z; this->w = _w; } - template - Vector4::Vector4( const Vector2 &vector, const ElementType &_z, const ElementType &_w ) : x(vector.x), y(vector.y), z(_z), w(_w) + template + Vector4::Vector4( const Vector2 &vector, const ScalarType &_z, const ScalarType &_w ) { this->x = vector.x; this->y = vector.y; this->z = _z; this->w = _w; } - template - Vector4::Vector4( const ElementType &_element ) : x(_element), y(_element), z(_element), w(_element) + template + Vector4::Vector4( const ScalarType &_element ) { this->x = this->y = this->z = this->w = _element; } - template - Vector4::Vector4( const ElementType _element[4] ) : x(_element[0]), y(_element[1]), z(_element[2]), w(_element[3]) {} + template + Vector4::Vector4( const ScalarType _element[4] ) + { this->x = _element[0]; this->y = _element[1]; this->z = _element[2]; this->w = _element[3]; } - template - Vector4::Vector4( const ElementType &_x, const ElementType &_y, const ElementType &_z, const ElementType &_w ) : x(_x), y(_y), z(_z), w(_w) + template + Vector4::Vector4( const ScalarType &_x, const ScalarType &_y, const ScalarType &_z, const ScalarType &_w ) { this->x = _x; this->y = _y; this->z = _z; this->w = _w; } - template - Vector4::~Vector4( ) { /* Nothing that needs to be done */ } - - template - inline Vector4::operator ElementType* () + template + inline Vector4::operator ScalarType* () { return this->element; } - template - inline Vector4::operator const ElementType* () const + template + inline Vector4::operator const ScalarType* () const { return this->element; } - template - inline ElementType & Vector4::operator [] ( int i ) + template + inline ScalarType & Vector4::operator [] ( int i ) { return this->element[i]; } - template - inline const ElementType & Vector4::operator [] ( int i ) const + template + inline const ScalarType & Vector4::operator [] ( int i ) const { return this->element[i]; } - template - Vector4 & Vector4::operator = ( const Vector4 &vector ) + template + Vector4 & Vector4::operator = ( const Vector4 &vector ) { - for( int i = 0; i < 4; ++i ) - this->element[i] = vector.element[i]; + this->element[0] = vector.element[0]; + this->element[1] = vector.element[1]; + this->element[2] = vector.element[2]; + this->element[3] = vector.element[3]; return *this; } - template - Vector4 & Vector4::operator = ( const ElementType element[4] ) + template + Vector4 & Vector4::operator = ( const ScalarType element[4] ) { - for( int i = 0; i < 4; ++i ) - this->element[i] = element[i]; + this->element[0] = element[0]; + this->element[1] = element[1]; + this->element[2] = element[2]; + this->element[3] = element[3]; return *this; } - template - Vector4 & Vector4::operator *= ( const ElementType &scalar ) + template + Vector4 & Vector4::operator *= ( const ScalarType &scalar ) { - for( int i = 0; i < 4; ++i ) - this->element[i] *= scalar; + this->element[0] *= scalar; + this->element[1] *= scalar; + this->element[2] *= scalar; + this->element[3] *= scalar; return *this; } - template - Vector4 & Vector4::operator /= ( const ElementType &scalar ) + template + Vector4 & Vector4::operator /= ( const ScalarType &scalar ) { - for( int i = 0; i < 4; ++i ) - this->element[i] /= scalar; + this->element[0] /= scalar; + this->element[1] /= scalar; + this->element[2] /= scalar; + this->element[3] /= scalar; return *this; } - template - Vector4 & Vector4::operator += ( const Vector4 &vector ) + template + Vector4 & Vector4::operator += ( const Vector4 &vector ) { - for( int i = 0; i < 4; ++i ) - this->element[i] += vector.element[i]; + this->element[0] += vector.element[0]; + this->element[1] += vector.element[1]; + this->element[2] += vector.element[2]; + this->element[3] += vector.element[3]; return *this; } - template - Vector4 & Vector4::operator -= ( const Vector4 &vector ) + template + Vector4 & Vector4::operator -= ( const Vector4 &vector ) { - for( int i = 0; i < 4; ++i ) - this->element[i] -= vector.element[i]; + this->element[0] -= vector.element[0]; + this->element[1] -= vector.element[1]; + this->element[2] -= vector.element[2]; + this->element[3] -= vector.element[3]; return *this; } - template - inline Vector4 Vector4::operator * ( const ElementType &scalar ) const - { return Vector4(*this) *= scalar; } + template + inline Vector4 Vector4::operator * ( const ScalarType &scalar ) const + { return Vector4(*this) *= scalar; } - template - inline Vector4 Vector4::operator / ( const ElementType &scalar ) const - { return Vector4(*this) /= scalar; } + template + inline Vector4 Vector4::operator / ( const ScalarType &scalar ) const + { return Vector4(*this) /= scalar; } - template - inline Vector4 Vector4::operator + ( const Vector4 &vector ) const - { return Vector4(*this) += vector; } + template + inline Vector4 Vector4::operator + ( const Vector4 &vector ) const + { return Vector4(*this) += vector; } - template - inline Vector4 Vector4::operator - ( const Vector4 &vector ) const - { return Vector4(*this) -= vector; } + template + inline Vector4 Vector4::operator - ( const Vector4 &vector ) const + { return Vector4(*this) -= vector; } - template - inline Vector4 Vector4::operator - ( ) const - { return Vector4(-this->x, -this->y, -this->z, -this->w); } + template + inline Vector4 Vector4::operator - ( ) const + { return Vector4(-this->x, -this->y, -this->z, -this->w); } - template - bool Vector4::operator == ( const Vector4 &vector ) const + template + bool Vector4::operator == ( const Vector4 &vector ) const { if( this->x != vector.x ) return false; if( this->y != vector.y ) return false; @@ -624,8 +648,8 @@ namespace LinearAlgebra return true; } - template - bool Vector4::operator != ( const Vector4 &vector ) const + template + bool Vector4::operator != ( const Vector4 &vector ) const { if( this->x != vector.x ) return true; if( this->y != vector.y ) return true; @@ -634,26 +658,32 @@ namespace LinearAlgebra return false; } - template - inline ElementType Vector4::length( ) const - { return (ElementType) ::sqrt( this->dot(*this) ); } + template + inline ScalarType Vector4::GetLength( ) const + { return this->GetMagnitude(); } - template - ElementType Vector4::dot( const Vector4 &vector ) const + template + inline ScalarType Vector4::GetMagnitude( ) const + { return (ScalarType) ::sqrt( this->Dot(*this) ); } + + template + ScalarType Vector4::Dot( const Vector4 &vector ) const { - ElementType value = 0; - for( int i = 0; i < 4; ++i ) - value += this->element[i] * vector.element[i]; + ScalarType value = 0; + value += this->element[0] * vector.element[0]; + value += this->element[1] * vector.element[1]; + value += this->element[2] * vector.element[2]; + value += this->element[3] * vector.element[3]; return value; } - template - inline Vector4 & Vector4::normalize( ) - { return (*this) /= this->length(); } + template + inline Vector4 & Vector4::Normalize( ) + { return (*this) /= this->GetLength(); } - template - inline Vector4 Vector4::getNormalized( ) const - { return Vector4(*this).normalize(); } + template + inline Vector4 Vector4::GetNormalized( ) const + { return Vector4(*this).Normalize(); } } #endif \ No newline at end of file