diff --git a/README.md b/README.md index ae2504c..31f2ef3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # ReflexToQ3 Converter to convert reflex maps into quake-style .map format. + +Usage run from commandline: ReflexToQ3.exe [input].map [output].map diff --git a/ReflexToQ3.sln b/ReflexToQ3.sln new file mode 100644 index 0000000..ca960aa --- /dev/null +++ b/ReflexToQ3.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.31101.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ReflexToQ3", "ReflexToQ3\ReflexToQ3.vcxproj", "{56AE2665-4191-464C-A2D8-80E60D44CE17}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {56AE2665-4191-464C-A2D8-80E60D44CE17}.Debug|Win32.ActiveCfg = Debug|Win32 + {56AE2665-4191-464C-A2D8-80E60D44CE17}.Debug|Win32.Build.0 = Debug|Win32 + {56AE2665-4191-464C-A2D8-80E60D44CE17}.Release|Win32.ActiveCfg = Release|Win32 + {56AE2665-4191-464C-A2D8-80E60D44CE17}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/ReflexToQ3/ReflexToQ3.vcxproj b/ReflexToQ3/ReflexToQ3.vcxproj new file mode 100644 index 0000000..6405a95 --- /dev/null +++ b/ReflexToQ3/ReflexToQ3.vcxproj @@ -0,0 +1,90 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {56AE2665-4191-464C-A2D8-80E60D44CE17} + ReflexToQ3 + + + + Application + true + v120 + MultiByte + + + Application + false + v120 + true + MultiByte + + + + + + + + + + + + + + + Level3 + Disabled + true + + + true + + + + + Level3 + MaxSpeed + true + true + true + + + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ReflexToQ3/ReflexToQ3.vcxproj.filters b/ReflexToQ3/ReflexToQ3.vcxproj.filters new file mode 100644 index 0000000..392a952 --- /dev/null +++ b/ReflexToQ3/ReflexToQ3.vcxproj.filters @@ -0,0 +1,75 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + {b02e339e-6cfc-4b44-bf71-2d5fb97b9424} + + + + + Header Files + + + Header Files + + + ckmath + + + ckmath + + + ckmath + + + ckmath + + + ckmath + + + ckmath + + + ckmath + + + ckmath + + + Header Files + + + + + ckmath + + + ckmath + + + ckmath + + + ckmath + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/ReflexToQ3/ckmath/ckmath.h b/ReflexToQ3/ckmath/ckmath.h new file mode 100644 index 0000000..85ed348 --- /dev/null +++ b/ReflexToQ3/ckmath/ckmath.h @@ -0,0 +1,20 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +#pragma once + +#ifndef __MATH_H__ +#define __MATH_H__ + +// Local Includes +#include "ckmath_constants.h" +#include "ckmath_types.h" +#include "ckmath_geometry.h" +#include "ckmath_vector.h" +#include "ckmath_scalar.h" +#include "ckmath_matrix.h" +#include "ckmath_quaternion.h" + +#endif \ No newline at end of file diff --git a/ReflexToQ3/ckmath/ckmath_constants.h b/ReflexToQ3/ckmath/ckmath_constants.h new file mode 100644 index 0000000..e16bd70 --- /dev/null +++ b/ReflexToQ3/ckmath/ckmath_constants.h @@ -0,0 +1,17 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +#pragma once + +#ifndef __MATH_CONSTANTS_H__ +#define __MATH_CONSTANTS_H__ + +// Math +static const double s_kdTau = 6.2831853071795864769252867665590057683943387987502116; +static const double s_kdPi = 3.1415926535897932384626433832795028841971693993751058; +static const float s_kfTau = static_cast(s_kdTau); +static const float s_kfPi = static_cast(s_kdPi); + +#endif \ No newline at end of file diff --git a/ReflexToQ3/ckmath/ckmath_geometry.cpp b/ReflexToQ3/ckmath/ckmath_geometry.cpp new file mode 100644 index 0000000..10da2fd --- /dev/null +++ b/ReflexToQ3/ckmath/ckmath_geometry.cpp @@ -0,0 +1,121 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +// Local Includes +#include "ckmath_scalar.h" +#include "ckmath_vector.h" +#include "ckmath_geometry.h" + +const bool IsIntersection(const TPlane3d& _krA, + const TPlane3d& _krB, + const TPlane3d& _krC) +{ + const TVector3d kNAxNB = CrossProduct(TVector3d(), _krA.m_Normal, _krB.m_Normal); + return(Magnitude(DotProduct(kNAxNB, _krC.m_Normal)) > 0.0); +} + +const bool IsIntersection(const TPlane3f& _krA, + const TPlane3f& _krB, + const TPlane3f& _krC) +{ + const TVector3f kNAxNB = CrossProduct(TVector3f(), _krA.m_Normal, _krB.m_Normal); + return(Magnitude(DotProduct(kNAxNB, _krC.m_Normal)) > 0.0f); +} + +const TVector3d& GetIntersection( TVector3d& _rResult, + const TPlane3d& _krA, + const TPlane3d& _krB, + const TPlane3d& _krC) +{ + const TVector3d kNBxNC = CrossProduct(TVector3d(), _krB.m_Normal, _krC.m_Normal); + const TVector3d kNCxNA = CrossProduct(TVector3d(), _krC.m_Normal, _krA.m_Normal); + const TVector3d kNAxNB = CrossProduct(TVector3d(), _krA.m_Normal, _krB.m_Normal); + + const double kdDistanceA = DotProduct(_krA.m_Normal, _krA.m_Point); + const double kdDistanceB = DotProduct(_krB.m_Normal, _krB.m_Point); + const double kdDistanceC = DotProduct(_krC.m_Normal, _krC.m_Point); + + const TVector3d kDA_NBxNC = ScalarMultiply(TVector3d(), kNBxNC, kdDistanceA); + const TVector3d kDB_NCxNA = ScalarMultiply(TVector3d(), kNCxNA, kdDistanceB); + const TVector3d kDC_NAxNB = ScalarMultiply(TVector3d(), kNAxNB, kdDistanceC); + + const double kdDenom = DotProduct(kNAxNB, _krC.m_Normal); + + const TVector3d kNumer = Add(TVector3d(), Add(TVector3d(), kDA_NBxNC, kDB_NCxNA), kDC_NAxNB); + + _rResult = ScalarMultiply(_rResult, kNumer, (1.0 / kdDenom)); + + return(_rResult); +} + +const TVector3f& GetIntersection( TVector3f& _rResult, + const TPlane3f& _krA, + const TPlane3f& _krB, + const TPlane3f& _krC) +{ + const TVector3f kNBxNC = CrossProduct(TVector3f(), _krB.m_Normal, _krC.m_Normal); + const TVector3f kNCxNA = CrossProduct(TVector3f(), _krC.m_Normal, _krA.m_Normal); + const TVector3f kNAxNB = CrossProduct(TVector3f(), _krA.m_Normal, _krB.m_Normal); + + const float kfDistanceA = DotProduct(_krA.m_Normal, _krA.m_Point); + const float kfDistanceB = DotProduct(_krB.m_Normal, _krB.m_Point); + const float kfDistanceC = DotProduct(_krC.m_Normal, _krC.m_Point); + + const TVector3f kDA_NBxNC = ScalarMultiply(TVector3f(), kNBxNC, kfDistanceA); + const TVector3f kDB_NCxNA = ScalarMultiply(TVector3f(), kNCxNA, kfDistanceB); + const TVector3f kDC_NAxNB = ScalarMultiply(TVector3f(), kNAxNB, kfDistanceC); + + const float kfDenom = DotProduct(kNAxNB, _krC.m_Normal); + + const TVector3f kNumer = Add(TVector3f(), Add(TVector3f(), kDA_NBxNC, kDB_NCxNA), kDC_NAxNB); + + _rResult = ScalarMultiply(_rResult, kNumer, (1.0f / kfDenom)); + + return(_rResult); +} + +const TVector3d& GetPolygonNormal(TVector3d& _rResult, const TVector3d* _kpVertices, const size_t _kVertexCount) +{ + _rResult = ZeroVector(TVector3d()); + + for(size_t i = 0; i < _kVertexCount; ++i) + { + const size_t kIndexA = ((_kVertexCount-1)+i)%_kVertexCount; + const size_t kIndexB = i; + + const TVector3d& krA = _kpVertices[kIndexA]; + const TVector3d& krB = _kpVertices[kIndexB]; + + const TVector3d kCrossProduct = CrossProduct(TVector3d(), krA, krB); + + _rResult = Add(_rResult, _rResult, kCrossProduct); + } + + _rResult = Normalize(_rResult, _rResult); + + return(_rResult); +} + +const TVector3f& GetPolygonNormal(TVector3f& _rResult, const TVector3f* _kpVertices, const size_t _kVertexCount) +{ + _rResult = ZeroVector(TVector3f()); + + for(size_t i = 0; i < _kVertexCount; ++i) + { + const size_t kIndexA = ((_kVertexCount-1)+i)%_kVertexCount; + const size_t kIndexB = i; + + const TVector3f& krA = _kpVertices[kIndexA]; + const TVector3f& krB = _kpVertices[kIndexB]; + + const TVector3f kCrossProduct = CrossProduct(TVector3f(), krA, krB); + + _rResult = Add(_rResult, _rResult, kCrossProduct); + } + + _rResult = Normalize(_rResult, _rResult); + + return(_rResult); +} \ No newline at end of file diff --git a/ReflexToQ3/ckmath/ckmath_geometry.h b/ReflexToQ3/ckmath/ckmath_geometry.h new file mode 100644 index 0000000..dd1d573 --- /dev/null +++ b/ReflexToQ3/ckmath/ckmath_geometry.h @@ -0,0 +1,42 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +#pragma once + +#ifndef __MATH_GEOMETRY_H__ +#define __MATH_GEOMETRY_H__ + +// Local Includes +#include "ckmath_types.h" + + +const bool IsIntersection( const TPlane3d& _krA, + const TPlane3d& _krB, + const TPlane3d& _krC); + +const bool IsIntersection( const TPlane3f& _krA, + const TPlane3f& _krB, + const TPlane3f& _krC); + +const TVector3d& GetIntersection( TVector3d& _rResult, + const TPlane3d& _krA, + const TPlane3d& _krB, + const TPlane3d& _krC); + +const TVector3f& GetIntersection( TVector3f& _rResult, + const TPlane3f& _krA, + const TPlane3f& _krB, + const TPlane3f& _krC); + +const TVector3d& GetPolygonNormal( TVector3d& _rResult, + const TVector3d* _kpVertices, + const size_t _kVertexCount); + +const TVector3f& GetPolygonNormal( TVector3f& _rResult, + const TVector3f* _kpVertices, + const size_t _kVertexCount); + + +#endif \ No newline at end of file diff --git a/ReflexToQ3/ckmath/ckmath_matrix.cpp b/ReflexToQ3/ckmath/ckmath_matrix.cpp new file mode 100644 index 0000000..9f5b841 --- /dev/null +++ b/ReflexToQ3/ckmath/ckmath_matrix.cpp @@ -0,0 +1,2123 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +// Local Includes +#include "ckmath_matrix.h" +#include "ckmath_vector.h" +#include "ckmath_scalar.h" +#include "ckmath_quaternion.h" + +// +// Matrix 4 +// + +const bool Equal(const TMatrix4d& _krA, const TMatrix4d& _krB, const double _kdEpsilon) +{ + const bool kbEqual = (Magnitude(_krA.m_d11 - _krB.m_d11) < _kdEpsilon) + && (Magnitude(_krA.m_d12 - _krB.m_d12) < _kdEpsilon) + && (Magnitude(_krA.m_d13 - _krB.m_d13) < _kdEpsilon) + && (Magnitude(_krA.m_d14 - _krB.m_d14) < _kdEpsilon) + && (Magnitude(_krA.m_d21 - _krB.m_d21) < _kdEpsilon) + && (Magnitude(_krA.m_d22 - _krB.m_d22) < _kdEpsilon) + && (Magnitude(_krA.m_d23 - _krB.m_d23) < _kdEpsilon) + && (Magnitude(_krA.m_d24 - _krB.m_d24) < _kdEpsilon) + && (Magnitude(_krA.m_d31 - _krB.m_d31) < _kdEpsilon) + && (Magnitude(_krA.m_d32 - _krB.m_d32) < _kdEpsilon) + && (Magnitude(_krA.m_d33 - _krB.m_d33) < _kdEpsilon) + && (Magnitude(_krA.m_d34 - _krB.m_d34) < _kdEpsilon) + && (Magnitude(_krA.m_d41 - _krB.m_d41) < _kdEpsilon) + && (Magnitude(_krA.m_d42 - _krB.m_d42) < _kdEpsilon) + && (Magnitude(_krA.m_d43 - _krB.m_d43) < _kdEpsilon) + && (Magnitude(_krA.m_d44 - _krB.m_d44) < _kdEpsilon); + + return(kbEqual); +} + +const bool Equal(const TMatrix4f& _krA, const TMatrix4f& _krB, const float _kfEpsilon) +{ + const bool kbEqual = (Magnitude(_krA.m_f11 - _krB.m_f11) < _kfEpsilon) + && (Magnitude(_krA.m_f12 - _krB.m_f12) < _kfEpsilon) + && (Magnitude(_krA.m_f13 - _krB.m_f13) < _kfEpsilon) + && (Magnitude(_krA.m_f14 - _krB.m_f14) < _kfEpsilon) + && (Magnitude(_krA.m_f21 - _krB.m_f21) < _kfEpsilon) + && (Magnitude(_krA.m_f22 - _krB.m_f22) < _kfEpsilon) + && (Magnitude(_krA.m_f23 - _krB.m_f23) < _kfEpsilon) + && (Magnitude(_krA.m_f24 - _krB.m_f24) < _kfEpsilon) + && (Magnitude(_krA.m_f31 - _krB.m_f31) < _kfEpsilon) + && (Magnitude(_krA.m_f32 - _krB.m_f32) < _kfEpsilon) + && (Magnitude(_krA.m_f33 - _krB.m_f33) < _kfEpsilon) + && (Magnitude(_krA.m_f34 - _krB.m_f34) < _kfEpsilon) + && (Magnitude(_krA.m_f41 - _krB.m_f41) < _kfEpsilon) + && (Magnitude(_krA.m_f42 - _krB.m_f42) < _kfEpsilon) + && (Magnitude(_krA.m_f43 - _krB.m_f43) < _kfEpsilon) + && (Magnitude(_krA.m_f44 - _krB.m_f44) < _kfEpsilon); + + return(kbEqual); +} + +const TMatrix4d& ZeroMatrix(TMatrix4d& _rResult) +{ + _rResult.m_d11 = 0.0; _rResult.m_d12 = 0.0; _rResult.m_d13 = 0.0; _rResult.m_d14 = 0.0; + _rResult.m_d21 = 0.0; _rResult.m_d22 = 0.0; _rResult.m_d23 = 0.0; _rResult.m_d24 = 0.0; + _rResult.m_d31 = 0.0; _rResult.m_d32 = 0.0; _rResult.m_d33 = 0.0; _rResult.m_d34 = 0.0; + _rResult.m_d41 = 0.0; _rResult.m_d42 = 0.0; _rResult.m_d43 = 0.0; _rResult.m_d44 = 0.0; + + return(_rResult); +} + +const TMatrix4f& ZeroMatrix(TMatrix4f& _rResult) +{ + _rResult.m_f11 = 0.0f; _rResult.m_f12 = 0.0f; _rResult.m_f13 = 0.0f; _rResult.m_f14 = 0.0f; + _rResult.m_f21 = 0.0f; _rResult.m_f22 = 0.0f; _rResult.m_f23 = 0.0f; _rResult.m_f24 = 0.0f; + _rResult.m_f31 = 0.0f; _rResult.m_f32 = 0.0f; _rResult.m_f33 = 0.0f; _rResult.m_f34 = 0.0f; + _rResult.m_f41 = 0.0f; _rResult.m_f42 = 0.0f; _rResult.m_f43 = 0.0f; _rResult.m_f44 = 0.0f; + + return(_rResult); +} + +const TMatrix4d& IdentityMatrix(TMatrix4d& _rResult) +{ + _rResult.m_d11 = 1.0; _rResult.m_d12 = 0.0; _rResult.m_d13 = 0.0; _rResult.m_d14 = 0.0; + _rResult.m_d21 = 0.0; _rResult.m_d22 = 1.0; _rResult.m_d23 = 0.0; _rResult.m_d24 = 0.0; + _rResult.m_d31 = 0.0; _rResult.m_d32 = 0.0; _rResult.m_d33 = 1.0; _rResult.m_d34 = 0.0; + _rResult.m_d41 = 0.0; _rResult.m_d42 = 0.0; _rResult.m_d43 = 0.0; _rResult.m_d44 = 1.0; + + return(_rResult); +} + +const TMatrix4f& IdentityMatrix(TMatrix4f& _rResult) +{ + _rResult.m_f11 = 1.0f; _rResult.m_f12 = 0.0f; _rResult.m_f13 = 0.0f; _rResult.m_f14 = 0.0f; + _rResult.m_f21 = 0.0f; _rResult.m_f22 = 1.0f; _rResult.m_f23 = 0.0f; _rResult.m_f24 = 0.0f; + _rResult.m_f31 = 0.0f; _rResult.m_f32 = 0.0f; _rResult.m_f33 = 1.0f; _rResult.m_f34 = 0.0f; + _rResult.m_f41 = 0.0f; _rResult.m_f42 = 0.0f; _rResult.m_f43 = 0.0f; _rResult.m_f44 = 1.0f; + + return(_rResult); +} + +const TMatrix4d& Multiply(TMatrix4d& _rResult, + const TMatrix4d& _krA, + const TMatrix4d& _krB) +{ + for(size_t y = 1; y <= 4; ++y) + { + for(size_t x = 1; x <= 4; ++x) + { + double dAccumulate = 0.0; + for(size_t n = 1; n <= 4; ++n) + { + dAccumulate += GetElement(_krA, y, n) * GetElement(_krB, n, x); + } + SetElement(_rResult, dAccumulate, y, x); + } + } + + return(_rResult); +} + +const TMatrix4f& Multiply(TMatrix4f& _rResult, + const TMatrix4f& _krA, + const TMatrix4f& _krB) +{ + for(size_t x = 1; x <= 4; ++x) + { + for(size_t y = 1; y <= 4; ++y) + { + float fAccumulate = 0.0; + for(size_t n = 1; n <= 4; ++n) + { + fAccumulate += GetElement(_krA, y, n) * GetElement(_krB, n, x); + } + SetElement(_rResult, fAccumulate, y, x); + } + } + + return(_rResult); +} + +const TMatrix4d& ScalarMultiply( TMatrix4d& _rResult, + const TMatrix4d& _krMatrix, + const double _kdScalar) +{ + _rResult.m_d11 = _krMatrix.m_d11 * _kdScalar; + _rResult.m_d12 = _krMatrix.m_d12 * _kdScalar; + _rResult.m_d13 = _krMatrix.m_d13 * _kdScalar; + _rResult.m_d14 = _krMatrix.m_d14 * _kdScalar; + + _rResult.m_d21 = _krMatrix.m_d21 * _kdScalar; + _rResult.m_d22 = _krMatrix.m_d22 * _kdScalar; + _rResult.m_d23 = _krMatrix.m_d23 * _kdScalar; + _rResult.m_d24 = _krMatrix.m_d24 * _kdScalar; + + _rResult.m_d31 = _krMatrix.m_d31 * _kdScalar; + _rResult.m_d32 = _krMatrix.m_d32 * _kdScalar; + _rResult.m_d33 = _krMatrix.m_d33 * _kdScalar; + _rResult.m_d34 = _krMatrix.m_d34 * _kdScalar; + + _rResult.m_d41 = _krMatrix.m_d41 * _kdScalar; + _rResult.m_d42 = _krMatrix.m_d42 * _kdScalar; + _rResult.m_d43 = _krMatrix.m_d43 * _kdScalar; + _rResult.m_d44 = _krMatrix.m_d44 * _kdScalar; + + return(_rResult); +} + +const TMatrix4f& ScalarMultiply( TMatrix4f& _rResult, + const TMatrix4f& _krMatrix, + const float _kfScalar) +{ + _rResult.m_f11 = _krMatrix.m_f11 * _kfScalar; + _rResult.m_f12 = _krMatrix.m_f12 * _kfScalar; + _rResult.m_f13 = _krMatrix.m_f13 * _kfScalar; + _rResult.m_f14 = _krMatrix.m_f14 * _kfScalar; + + _rResult.m_f21 = _krMatrix.m_f21 * _kfScalar; + _rResult.m_f22 = _krMatrix.m_f22 * _kfScalar; + _rResult.m_f23 = _krMatrix.m_f23 * _kfScalar; + _rResult.m_f24 = _krMatrix.m_f24 * _kfScalar; + + _rResult.m_f31 = _krMatrix.m_f31 * _kfScalar; + _rResult.m_f32 = _krMatrix.m_f32 * _kfScalar; + _rResult.m_f33 = _krMatrix.m_f33 * _kfScalar; + _rResult.m_f34 = _krMatrix.m_f34 * _kfScalar; + + _rResult.m_f41 = _krMatrix.m_f41 * _kfScalar; + _rResult.m_f42 = _krMatrix.m_f42 * _kfScalar; + _rResult.m_f43 = _krMatrix.m_f43 * _kfScalar; + _rResult.m_f44 = _krMatrix.m_f44 * _kfScalar; + + return(_rResult); +} + +const TVector4d& VectorMultiply( TVector4d& _rResult, + const TMatrix4d& _krA, + const TVector4d& _krB) +{ + _rResult.m_dX = (_krA.m_d11 * _krB.m_dX) + (_krA.m_d12 * _krB.m_dY) + (_krA.m_d13 * _krB.m_dZ) + (_krA.m_d14 * _krB.m_dW); + _rResult.m_dY = (_krA.m_d21 * _krB.m_dX) + (_krA.m_d22 * _krB.m_dY) + (_krA.m_d23 * _krB.m_dZ) + (_krA.m_d24 * _krB.m_dW); + _rResult.m_dZ = (_krA.m_d31 * _krB.m_dX) + (_krA.m_d32 * _krB.m_dY) + (_krA.m_d33 * _krB.m_dZ) + (_krA.m_d34 * _krB.m_dW); + _rResult.m_dW = (_krA.m_d41 * _krB.m_dX) + (_krA.m_d42 * _krB.m_dY) + (_krA.m_d43 * _krB.m_dZ) + (_krA.m_d44 * _krB.m_dW); + + return(_rResult); +} + +const TVector4f& VectorMultiply( TVector4f& _rResult, + const TMatrix4f& _krA, + const TVector4f& _krB) +{ + _rResult.m_fX = (_krA.m_f11 * _krB.m_fX) + (_krA.m_f12 * _krB.m_fY) + (_krA.m_f13 * _krB.m_fZ) + (_krA.m_f14 * _krB.m_fW); + _rResult.m_fY = (_krA.m_f21 * _krB.m_fX) + (_krA.m_f22 * _krB.m_fY) + (_krA.m_f23 * _krB.m_fZ) + (_krA.m_f24 * _krB.m_fW); + _rResult.m_fZ = (_krA.m_f31 * _krB.m_fX) + (_krA.m_f32 * _krB.m_fY) + (_krA.m_f33 * _krB.m_fZ) + (_krA.m_f34 * _krB.m_fW); + _rResult.m_fW = (_krA.m_f41 * _krB.m_fX) + (_krA.m_f42 * _krB.m_fY) + (_krA.m_f43 * _krB.m_fZ) + (_krA.m_f44 * _krB.m_fW); + + return(_rResult); +} + +const TMatrix4d& Add( TMatrix4d& _rResult, + const TMatrix4d& _krA, + const TMatrix4d& _krB) +{ + _rResult.m_d11 = _krA.m_d11 + _krB.m_d11; + _rResult.m_d12 = _krA.m_d12 + _krB.m_d12; + _rResult.m_d13 = _krA.m_d13 + _krB.m_d13; + _rResult.m_d14 = _krA.m_d14 + _krB.m_d14; + + _rResult.m_d21 = _krA.m_d21 + _krB.m_d21; + _rResult.m_d22 = _krA.m_d22 + _krB.m_d22; + _rResult.m_d23 = _krA.m_d23 + _krB.m_d23; + _rResult.m_d24 = _krA.m_d24 + _krB.m_d24; + + _rResult.m_d31 = _krA.m_d31 + _krB.m_d31; + _rResult.m_d32 = _krA.m_d32 + _krB.m_d32; + _rResult.m_d33 = _krA.m_d33 + _krB.m_d33; + _rResult.m_d34 = _krA.m_d34 + _krB.m_d34; + + _rResult.m_d41 = _krA.m_d41 + _krB.m_d41; + _rResult.m_d42 = _krA.m_d42 + _krB.m_d42; + _rResult.m_d43 = _krA.m_d43 + _krB.m_d43; + _rResult.m_d44 = _krA.m_d44 + _krB.m_d44; + + return(_rResult); +} + +const TMatrix4f& Add( TMatrix4f& _rResult, + const TMatrix4f& _krA, + const TMatrix4f& _krB) +{ + _rResult.m_f11 = _krA.m_f11 + _krB.m_f11; + _rResult.m_f12 = _krA.m_f12 + _krB.m_f12; + _rResult.m_f13 = _krA.m_f13 + _krB.m_f13; + _rResult.m_f14 = _krA.m_f14 + _krB.m_f14; + + _rResult.m_f21 = _krA.m_f21 + _krB.m_f21; + _rResult.m_f22 = _krA.m_f22 + _krB.m_f22; + _rResult.m_f23 = _krA.m_f23 + _krB.m_f23; + _rResult.m_f24 = _krA.m_f24 + _krB.m_f24; + + _rResult.m_f31 = _krA.m_f31 + _krB.m_f31; + _rResult.m_f32 = _krA.m_f32 + _krB.m_f32; + _rResult.m_f33 = _krA.m_f33 + _krB.m_f33; + _rResult.m_f34 = _krA.m_f34 + _krB.m_f34; + + _rResult.m_f41 = _krA.m_f41 + _krB.m_f41; + _rResult.m_f42 = _krA.m_f42 + _krB.m_f42; + _rResult.m_f43 = _krA.m_f43 + _krB.m_f43; + _rResult.m_f44 = _krA.m_f44 + _krB.m_f44; + + return(_rResult); +} + +const TMatrix4d& Transpose( TMatrix4d& _rResult, + const TMatrix4d& _krMatrix) +{ + _rResult.m_d11 = _krMatrix.m_d11; _rResult.m_d12 = _krMatrix.m_d21; _rResult.m_d13 = _krMatrix.m_d31; _rResult.m_d14 = _krMatrix.m_d41; + _rResult.m_d21 = _krMatrix.m_d12; _rResult.m_d22 = _krMatrix.m_d22; _rResult.m_d23 = _krMatrix.m_d32; _rResult.m_d24 = _krMatrix.m_d42; + _rResult.m_d31 = _krMatrix.m_d13; _rResult.m_d32 = _krMatrix.m_d23; _rResult.m_d33 = _krMatrix.m_d33; _rResult.m_d34 = _krMatrix.m_d43; + _rResult.m_d41 = _krMatrix.m_d14; _rResult.m_d42 = _krMatrix.m_d24; _rResult.m_d43 = _krMatrix.m_d34; _rResult.m_d44 = _krMatrix.m_d44; + + return(_rResult); +} + +const TMatrix4f& Transpose( TMatrix4f& _rResult, + const TMatrix4f& _krMatrix) +{ + _rResult.m_f11 = _krMatrix.m_f11; _rResult.m_f12 = _krMatrix.m_f21; _rResult.m_f13 = _krMatrix.m_f31; _rResult.m_f14 = _krMatrix.m_f41; + _rResult.m_f21 = _krMatrix.m_f12; _rResult.m_f22 = _krMatrix.m_f22; _rResult.m_f23 = _krMatrix.m_f32; _rResult.m_f24 = _krMatrix.m_f42; + _rResult.m_f31 = _krMatrix.m_f13; _rResult.m_f32 = _krMatrix.m_f23; _rResult.m_f33 = _krMatrix.m_f33; _rResult.m_f34 = _krMatrix.m_f43; + _rResult.m_f41 = _krMatrix.m_f14; _rResult.m_f42 = _krMatrix.m_f24; _rResult.m_f43 = _krMatrix.m_f34; _rResult.m_f44 = _krMatrix.m_f44; + + return(_rResult); +} + +const double GetElement( const TMatrix4d& _krMatrix, + const size_t _kRow, + const size_t _kColumn) +{ + double dResult; + + if(_kRow == 1) + { + if(_kColumn == 1) + { + dResult = _krMatrix.m_d11; + } + else if(_kColumn == 2) + { + dResult = _krMatrix.m_d12; + } + else if(_kColumn == 3) + { + dResult = _krMatrix.m_d13; + } + else if(_kColumn == 4) + { + dResult = _krMatrix.m_d14; + } + } + else if(_kRow == 2) + { + if(_kColumn == 1) + { + dResult = _krMatrix.m_d21; + } + else if(_kColumn == 2) + { + dResult = _krMatrix.m_d22; + } + else if(_kColumn == 3) + { + dResult = _krMatrix.m_d23; + } + else if(_kColumn == 4) + { + dResult = _krMatrix.m_d24; + } + } + else if(_kRow == 3) + { + if(_kColumn == 1) + { + dResult = _krMatrix.m_d31; + } + else if(_kColumn == 2) + { + dResult = _krMatrix.m_d32; + } + else if(_kColumn == 3) + { + dResult = _krMatrix.m_d33; + } + else if(_kColumn == 4) + { + dResult = _krMatrix.m_d34; + } + } + else if(_kRow == 4) + { + if(_kColumn == 1) + { + dResult = _krMatrix.m_d41; + } + else if(_kColumn == 2) + { + dResult = _krMatrix.m_d42; + } + else if(_kColumn == 3) + { + dResult = _krMatrix.m_d43; + } + else if(_kColumn == 4) + { + dResult = _krMatrix.m_d44; + } + } + + return(dResult); +} + +const float GetElement( const TMatrix4f& _krMatrix, + const size_t _kRow, + const size_t _kColumn) +{ + float fResult; + + if(_kRow == 1) + { + if(_kColumn == 1) + { + fResult = _krMatrix.m_f11; + } + else if(_kColumn == 2) + { + fResult = _krMatrix.m_f12; + } + else if(_kColumn == 3) + { + fResult = _krMatrix.m_f13; + } + else if(_kColumn == 4) + { + fResult = _krMatrix.m_f14; + } + } + else if(_kRow == 2) + { + if(_kColumn == 1) + { + fResult = _krMatrix.m_f21; + } + else if(_kColumn == 2) + { + fResult = _krMatrix.m_f22; + } + else if(_kColumn == 3) + { + fResult = _krMatrix.m_f23; + } + else if(_kColumn == 4) + { + fResult = _krMatrix.m_f24; + } + } + else if(_kRow == 3) + { + if(_kColumn == 1) + { + fResult = _krMatrix.m_f31; + } + else if(_kColumn == 2) + { + fResult = _krMatrix.m_f32; + } + else if(_kColumn == 3) + { + fResult = _krMatrix.m_f33; + } + else if(_kColumn == 4) + { + fResult = _krMatrix.m_f34; + } + } + else if(_kRow == 4) + { + if(_kColumn == 1) + { + fResult = _krMatrix.m_f41; + } + else if(_kColumn == 2) + { + fResult = _krMatrix.m_f42; + } + else if(_kColumn == 3) + { + fResult = _krMatrix.m_f43; + } + else if(_kColumn == 4) + { + fResult = _krMatrix.m_f44; + } + } + + return(fResult); +} + + +TMatrix4d& SetElement( TMatrix4d& _rResult, + const double _kdValue, + const size_t _kRow, + const size_t _kColumn) +{ + if(_kRow == 1) + { + if(_kColumn == 1) + { + _rResult.m_d11 = _kdValue; + } + else if(_kColumn == 2) + { + _rResult.m_d12 = _kdValue; + } + else if(_kColumn == 3) + { + _rResult.m_d13 = _kdValue; + } + else if(_kColumn == 4) + { + _rResult.m_d14 = _kdValue; + } + } + else if(_kRow == 2) + { + if(_kColumn == 1) + { + _rResult.m_d21 = _kdValue; + } + else if(_kColumn == 2) + { + _rResult.m_d22 = _kdValue; + } + else if(_kColumn == 3) + { + _rResult.m_d23 = _kdValue; + } + else if(_kColumn == 4) + { + _rResult.m_d24 = _kdValue; + } + } + else if(_kRow == 3) + { + if(_kColumn == 1) + { + _rResult.m_d31 = _kdValue; + } + else if(_kColumn == 2) + { + _rResult.m_d32 = _kdValue; + } + else if(_kColumn == 3) + { + _rResult.m_d33 = _kdValue; + } + else if(_kColumn == 4) + { + _rResult.m_d34 = _kdValue; + } + } + else if(_kRow == 4) + { + if(_kColumn == 1) + { + _rResult.m_d41 = _kdValue; + } + else if(_kColumn == 2) + { + _rResult.m_d42 = _kdValue; + } + else if(_kColumn == 3) + { + _rResult.m_d43 = _kdValue; + } + else if(_kColumn == 4) + { + _rResult.m_d44 = _kdValue; + } + } + + return(_rResult); +} + +TMatrix4f& SetElement( TMatrix4f& _rResult, + const float _kfValue, + const size_t _kRow, + const size_t _kColumn) +{ + if(_kRow == 1) + { + if(_kColumn == 1) + { + _rResult.m_f11 = _kfValue; + } + else if(_kColumn == 2) + { + _rResult.m_f12 = _kfValue; + } + else if(_kColumn == 3) + { + _rResult.m_f13 = _kfValue; + } + else if(_kColumn == 4) + { + _rResult.m_f14 = _kfValue; + } + } + else if(_kRow == 2) + { + if(_kColumn == 1) + { + _rResult.m_f21 = _kfValue; + } + else if(_kColumn == 2) + { + _rResult.m_f22 = _kfValue; + } + else if(_kColumn == 3) + { + _rResult.m_f23 = _kfValue; + } + else if(_kColumn == 4) + { + _rResult.m_f24 = _kfValue; + } + } + else if(_kRow == 3) + { + if(_kColumn == 1) + { + _rResult.m_f31 = _kfValue; + } + else if(_kColumn == 2) + { + _rResult.m_f32 = _kfValue; + } + else if(_kColumn == 3) + { + _rResult.m_f33 = _kfValue; + } + else if(_kColumn == 4) + { + _rResult.m_f34 = _kfValue; + } + } + else if(_kRow == 4) + { + if(_kColumn == 1) + { + _rResult.m_f41 = _kfValue; + } + else if(_kColumn == 2) + { + _rResult.m_f42 = _kfValue; + } + else if(_kColumn == 3) + { + _rResult.m_f43 = _kfValue; + } + else if(_kColumn == 4) + { + _rResult.m_f44 = _kfValue; + } + } + + return(_rResult); +} + +const TMatrix3d& Submatrix( TMatrix3d& _rResult, + const TMatrix4d& _krMatrix, + const size_t _kDeletedRow, + const size_t _kDeletedColumn) +{ + for(size_t i = 1; i <= 4; ++i) + { + for(size_t j = 1; j <= 4; ++j) + { + if( (i != _kDeletedRow) + && (j != _kDeletedColumn)) + { + size_t InsertI; + size_t InsertJ; + if(i < _kDeletedRow) + { + InsertI = i; + } + else + { + InsertI = i-1; + } + if(j < _kDeletedColumn) + { + InsertJ = j; + } + else + { + InsertJ = j-1; + } + + SetElement(_rResult, GetElement(_krMatrix, i, j), InsertI, InsertJ); + } + } + } + + return(_rResult); +} + +const TMatrix3f& Submatrix( TMatrix3f& _rResult, + const TMatrix4f& _krMatrix, + const size_t _kDeletedRow, + const size_t _kDeletedColumn) +{ + for(size_t i = 1; i <= 4; ++i) + { + for(size_t j = 1; j <= 4; ++j) + { + if( (i != _kDeletedRow) + && (j != _kDeletedColumn)) + { + size_t InsertI; + size_t InsertJ; + if(i < _kDeletedRow) + { + InsertI = i; + } + else + { + InsertI = i-1; + } + if(j < _kDeletedColumn) + { + InsertJ = j; + } + else + { + InsertJ = j-1; + } + + SetElement(_rResult, GetElement(_krMatrix, i, j), InsertI, InsertJ); + } + } + } + + return(_rResult); +} + +const double FirstMinor( const TMatrix4d& _krMatrix, + const size_t _kRow, + const size_t _kColumn) +{ + const TMatrix3d kSubmatrix = Submatrix(TMatrix3d(), _krMatrix, _kRow, _kColumn); + + return(Determinant(kSubmatrix)); +} + +const float FirstMinor( const TMatrix4f& _krMatrix, + const size_t _kRow, + const size_t _kColumn) +{ + const TMatrix3f kSubmatrix = Submatrix(TMatrix3f(), _krMatrix, _kRow, _kColumn); + + return(Determinant(kSubmatrix)); +} + +const TMatrix4d& MatrixOfMinors( TMatrix4d& _rResult, + const TMatrix4d& _krMatrix) +{ + _rResult.m_d11 = FirstMinor(_krMatrix, 1, 1); + _rResult.m_d12 = FirstMinor(_krMatrix, 1, 2); + _rResult.m_d13 = FirstMinor(_krMatrix, 1, 3); + _rResult.m_d14 = FirstMinor(_krMatrix, 1, 4); + + _rResult.m_d21 = FirstMinor(_krMatrix, 2, 1); + _rResult.m_d22 = FirstMinor(_krMatrix, 2, 2); + _rResult.m_d23 = FirstMinor(_krMatrix, 2, 3); + _rResult.m_d24 = FirstMinor(_krMatrix, 2, 4); + + _rResult.m_d31 = FirstMinor(_krMatrix, 3, 1); + _rResult.m_d32 = FirstMinor(_krMatrix, 3, 2); + _rResult.m_d33 = FirstMinor(_krMatrix, 3, 3); + _rResult.m_d34 = FirstMinor(_krMatrix, 3, 4); + + _rResult.m_d41 = FirstMinor(_krMatrix, 4, 1); + _rResult.m_d42 = FirstMinor(_krMatrix, 4, 2); + _rResult.m_d43 = FirstMinor(_krMatrix, 4, 3); + _rResult.m_d44 = FirstMinor(_krMatrix, 4, 4); + + return(_rResult); +} + +const TMatrix4f& MatrixOfMinors( TMatrix4f& _rResult, + const TMatrix4f& _krMatrix) +{ + _rResult.m_f11 = FirstMinor(_krMatrix, 1, 1); + _rResult.m_f12 = FirstMinor(_krMatrix, 1, 2); + _rResult.m_f13 = FirstMinor(_krMatrix, 1, 3); + _rResult.m_f14 = FirstMinor(_krMatrix, 1, 4); + + _rResult.m_f21 = FirstMinor(_krMatrix, 2, 1); + _rResult.m_f22 = FirstMinor(_krMatrix, 2, 2); + _rResult.m_f23 = FirstMinor(_krMatrix, 2, 3); + _rResult.m_f24 = FirstMinor(_krMatrix, 2, 4); + + _rResult.m_f31 = FirstMinor(_krMatrix, 3, 1); + _rResult.m_f32 = FirstMinor(_krMatrix, 3, 2); + _rResult.m_f33 = FirstMinor(_krMatrix, 3, 3); + _rResult.m_f34 = FirstMinor(_krMatrix, 3, 4); + + _rResult.m_f41 = FirstMinor(_krMatrix, 4, 1); + _rResult.m_f42 = FirstMinor(_krMatrix, 4, 2); + _rResult.m_f43 = FirstMinor(_krMatrix, 4, 3); + _rResult.m_f44 = FirstMinor(_krMatrix, 4, 4); + + return(_rResult); +} + +const TMatrix4d& MatrixOfCofactors( TMatrix4d& _rResult, + const TMatrix4d& _krMatrix) +{ + _rResult = MatrixOfMinors(_rResult, _krMatrix); + + _rResult.m_d12 *= -1.0; + _rResult.m_d14 *= -1.0; + + _rResult.m_d21 *= -1.0; + _rResult.m_d23 *= -1.0; + + _rResult.m_d32 *= -1.0; + _rResult.m_d34 *= -1.0; + + _rResult.m_d41 *= -1.0; + _rResult.m_d43 *= -1.0; + + return(_rResult); +} + +const TMatrix4f& MatrixOfCofactors( TMatrix4f& _rResult, + const TMatrix4f& _krMatrix) +{ + _rResult = MatrixOfMinors(_rResult, _krMatrix); + + _rResult.m_f12 *= -1.0f; + _rResult.m_f14 *= -1.0f; + + _rResult.m_f21 *= -1.0f; + _rResult.m_f23 *= -1.0f; + + _rResult.m_f32 *= -1.0f; + _rResult.m_f34 *= -1.0f; + + _rResult.m_f41 *= -1.0f; + _rResult.m_f43 *= -1.0f; + + return(_rResult); +} + +const double Determinant(const TMatrix4d& _krMatrix) +{ + const double kdDeterminant = (_krMatrix.m_d11 * FirstMinor(_krMatrix, 1, 1)) + - (_krMatrix.m_d12 * FirstMinor(_krMatrix, 1, 2)) + + (_krMatrix.m_d13 * FirstMinor(_krMatrix, 1, 3)) + - (_krMatrix.m_d14 * FirstMinor(_krMatrix, 1, 4)); + + return(kdDeterminant); +} + +const float Determinant(const TMatrix4f& _krMatrix) +{ + const float kfDeterminant = (_krMatrix.m_f11 * FirstMinor(_krMatrix, 1, 1)) + - (_krMatrix.m_f12 * FirstMinor(_krMatrix, 1, 2)) + + (_krMatrix.m_f13 * FirstMinor(_krMatrix, 1, 3)) + - (_krMatrix.m_f14 * FirstMinor(_krMatrix, 1, 4)); + + return(kfDeterminant); +} + +const TMatrix4d& Inverse( TMatrix4d& _rResult, + const TMatrix4d& _krMatrix) +{ + _rResult = ScalarMultiply(_rResult, + Transpose(TMatrix4d(), + MatrixOfCofactors(TMatrix4d(), _krMatrix)), + 1.0/Determinant(_krMatrix)); + + return(_rResult); +} + +const TMatrix4f& Inverse( TMatrix4f& _rResult, + const TMatrix4f& _krMatrix) +{ + _rResult = ScalarMultiply(_rResult, + Transpose(TMatrix4f(), + MatrixOfCofactors(TMatrix4f(), _krMatrix)), + 1.0f/Determinant(_krMatrix)); + + return(_rResult); +} + +const TMatrix4d& TranslationMatrix( TMatrix4d& _rResult, + const TVector3d& _krVector) +{ + _rResult.m_d11 = 1.0; _rResult.m_d12 = 0.0; _rResult.m_d13 = 0.0; _rResult.m_d14 = _krVector.m_dX; + _rResult.m_d21 = 0.0; _rResult.m_d22 = 1.0; _rResult.m_d23 = 0.0; _rResult.m_d24 = _krVector.m_dY; + _rResult.m_d31 = 0.0; _rResult.m_d32 = 0.0; _rResult.m_d33 = 1.0; _rResult.m_d34 = _krVector.m_dZ; + _rResult.m_d41 = 0.0; _rResult.m_d42 = 0.0; _rResult.m_d43 = 0.0; _rResult.m_d44 = 1.0; + + return(_rResult); +} + +const TMatrix4f& TranslationMatrix( TMatrix4f& _rResult, + const TVector3f& _krVector) +{ + _rResult.m_f11 = 1.0f; _rResult.m_f12 = 0.0f; _rResult.m_f13 = 0.0f; _rResult.m_f14 = _krVector.m_fX; + _rResult.m_f21 = 0.0f; _rResult.m_f22 = 1.0f; _rResult.m_f23 = 0.0f; _rResult.m_f24 = _krVector.m_fY; + _rResult.m_f31 = 0.0f; _rResult.m_f32 = 0.0f; _rResult.m_f33 = 1.0f; _rResult.m_f34 = _krVector.m_fZ; + _rResult.m_f41 = 0.0f; _rResult.m_f42 = 0.0f; _rResult.m_f43 = 0.0f; _rResult.m_f44 = 1.0f; + + return(_rResult); +} + +const TMatrix4d& ScalingMatrix( TMatrix4d& _rResult, + const double _kdX, + const double _kdY, + const double _kdZ) +{ + _rResult.m_d11 = _kdX; _rResult.m_d12 = 0.0; _rResult.m_d13 = 0.0; _rResult.m_d14 = 0.0; + _rResult.m_d21 = 0.0; _rResult.m_d22 = _kdY; _rResult.m_d23 = 0.0; _rResult.m_d24 = 0.0; + _rResult.m_d31 = 0.0; _rResult.m_d32 = 0.0; _rResult.m_d33 = _kdZ; _rResult.m_d34 = 0.0; + _rResult.m_d41 = 0.0; _rResult.m_d42 = 0.0; _rResult.m_d43 = 0.0; _rResult.m_d44 = 1.0; + + return(_rResult); +} + +const TMatrix4f& ScalingMatrix( TMatrix4f& _rResult, + const float _kfX, + const float _kfY, + const float _kfZ) +{ + _rResult.m_f11 = _kfX; _rResult.m_f12 = 0.0f; _rResult.m_f13 = 0.0f; _rResult.m_f14 = 0.0f; + _rResult.m_f21 = 0.0f; _rResult.m_f22 = _kfY; _rResult.m_f23 = 0.0f; _rResult.m_f24 = 0.0f; + _rResult.m_f31 = 0.0f; _rResult.m_f32 = 0.0f; _rResult.m_f33 = _kfZ; _rResult.m_f34 = 0.0f; + _rResult.m_f41 = 0.0f; _rResult.m_f42 = 0.0f; _rResult.m_f43 = 0.0f; _rResult.m_f44 = 1.0f; + + return(_rResult); +} + +const TMatrix4d& TransformationMatrix(TMatrix4d& _rResult, + const TVector3d& _krBasisX, + const TVector3d& _krBasisY, + const TVector3d& _krBasisZ, + const TVector3d& _krTranslation) +{ + _rResult.m_d11 = _krBasisX.m_dX; _rResult.m_d12 = _krBasisY.m_dX; _rResult.m_d13 = _krBasisZ.m_dX; _rResult.m_d14 = _krTranslation.m_dX; + _rResult.m_d21 = _krBasisX.m_dY; _rResult.m_d22 = _krBasisY.m_dY; _rResult.m_d23 = _krBasisZ.m_dY; _rResult.m_d24 = _krTranslation.m_dY; + _rResult.m_d31 = _krBasisX.m_dZ; _rResult.m_d32 = _krBasisY.m_dZ; _rResult.m_d33 = _krBasisZ.m_dZ; _rResult.m_d34 = _krTranslation.m_dZ; + + _rResult.m_d41 = 0.0; _rResult.m_d42 = 0.0; _rResult.m_d43 = 0.0; _rResult.m_d44 = 1.0; + + return(_rResult); +} + +const TMatrix4f& TransformationMatrix(TMatrix4f& _rResult, + const TVector3f& _krBasisX, + const TVector3f& _krBasisY, + const TVector3f& _krBasisZ, + const TVector3f& _krTranslation) +{ + _rResult.m_f11 = _krBasisX.m_fX; _rResult.m_f12 = _krBasisY.m_fX; _rResult.m_f13 = _krBasisZ.m_fX; _rResult.m_f14 = _krTranslation.m_fX; + _rResult.m_f21 = _krBasisX.m_fY; _rResult.m_f22 = _krBasisY.m_fY; _rResult.m_f23 = _krBasisZ.m_fY; _rResult.m_f24 = _krTranslation.m_fY; + _rResult.m_f31 = _krBasisX.m_fZ; _rResult.m_f32 = _krBasisY.m_fZ; _rResult.m_f33 = _krBasisZ.m_fZ; _rResult.m_f34 = _krTranslation.m_fZ; + + _rResult.m_f41 = 0.0f; _rResult.m_f42 = 0.0f; _rResult.m_f43 = 0.0f; _rResult.m_f44 = 1.0f; + + return(_rResult); +} + +const TMatrix4d& RotationMatrix( TMatrix4d& _rResult, + const TVector4d& _krQuaternion) +{ + _rResult = TransformationMatrix( _rResult, + QuaternionRotate(TVector3d(), TVector3d{1.0, 0.0, 0.0}, _krQuaternion), + QuaternionRotate(TVector3d(), TVector3d{0.0, 1.0, 0.0}, _krQuaternion), + QuaternionRotate(TVector3d(), TVector3d{0.0, 0.0, 1.0}, _krQuaternion), + TVector3d{0.0, 0.0, 0.0}); + + return(_rResult); +} + +const TMatrix4f& RotationMatrix( TMatrix4f& _rResult, + const TVector4f& _krQuaternion) +{ + _rResult = TransformationMatrix( _rResult, + QuaternionRotate(TVector3f(), TVector3f{1.0f, 0.0f, 0.0f}, _krQuaternion), + QuaternionRotate(TVector3f(), TVector3f{0.0f, 1.0f, 0.0f}, _krQuaternion), + QuaternionRotate(TVector3f(), TVector3f{0.0f, 0.0f, 1.0f}, _krQuaternion), + TVector3f{0.0f, 0.0f, 0.0f}); + + return(_rResult); +} + +const TMatrix4d& AxisRotationXMatrix( TMatrix4d& _rResult, + const double _kdAngle) +{ + _rResult.m_d11 = 1.0; _rResult.m_d12 = 0.0; _rResult.m_d13 = 0.0; _rResult.m_d14 = 0.0; + _rResult.m_d21 = 0.0; _rResult.m_d22 = Cosine(_kdAngle); _rResult.m_d23 = -Sine(_kdAngle); _rResult.m_d24 = 0.0; + _rResult.m_d31 = 0.0; _rResult.m_d32 = Sine(_kdAngle); _rResult.m_d33 = Cosine(_kdAngle); _rResult.m_d34 = 0.0; + _rResult.m_d41 = 0.0; _rResult.m_d42 = 0.0; _rResult.m_d43 = 0.0; _rResult.m_d44 = 1.0; + + return(_rResult); +} + +const TMatrix4f& AxisRotationXMatrix( TMatrix4f& _rResult, + const float _kfAngle) +{ + _rResult.m_f11 = 1.0f; _rResult.m_f12 = 0.0f; _rResult.m_f13 = 0.0f; _rResult.m_f14 = 0.0f; + _rResult.m_f21 = 0.0f; _rResult.m_f22 = Cosine(_kfAngle); _rResult.m_f23 = -Sine(_kfAngle); _rResult.m_f24 = 0.0f; + _rResult.m_f31 = 0.0f; _rResult.m_f32 = Sine(_kfAngle); _rResult.m_f33 = Cosine(_kfAngle); _rResult.m_f34 = 0.0f; + _rResult.m_f41 = 0.0f; _rResult.m_f42 = 0.0f; _rResult.m_f43 = 0.0f; _rResult.m_f44 = 1.0f; + + return(_rResult); +} + +const TMatrix4d& AxisRotationYMatrix( TMatrix4d& _rResult, + const double _kdAngle) +{ + _rResult.m_d11 = Cosine(_kdAngle); _rResult.m_d12 = 0.0; _rResult.m_d13 = Sine(_kdAngle); _rResult.m_d14 = 0.0; + _rResult.m_d21 = 0.0; _rResult.m_d22 = 1.0; _rResult.m_d23 = 0.0; _rResult.m_d24 = 0.0; + _rResult.m_d31 = -Sine(_kdAngle); _rResult.m_d32 = 0.0; _rResult.m_d33 = Cosine(_kdAngle); _rResult.m_d34 = 0.0; + _rResult.m_d41 = 0.0; _rResult.m_d42 = 0.0; _rResult.m_d43 = 0.0; _rResult.m_d44 = 1.0; + + return(_rResult); +} + +const TMatrix4f& AxisRotationYMatrix( TMatrix4f& _rResult, + const float _kfAngle) +{ + _rResult.m_f11 = Cosine(_kfAngle); _rResult.m_f12 = 0.0f; _rResult.m_f13 = Sine(_kfAngle); _rResult.m_f14 = 0.0f; + _rResult.m_f21 = 0.0f; _rResult.m_f22 = 1.0f; _rResult.m_f23 = 0.0f; _rResult.m_f24 = 0.0f; + _rResult.m_f31 = -Sine(_kfAngle); _rResult.m_f32 = 0.0f; _rResult.m_f33 = Cosine(_kfAngle); _rResult.m_f34 = 0.0f; + _rResult.m_f41 = 0.0f; _rResult.m_f42 = 0.0f; _rResult.m_f43 = 0.0f; _rResult.m_f44 = 1.0f; + + return(_rResult); +} + +const TMatrix4d& AxisRotationZMatrix( TMatrix4d& _rResult, + const double _kdAngle) +{ + _rResult.m_d11 = Cosine(_kdAngle); _rResult.m_d12 = -Sine(_kdAngle); _rResult.m_d13 = 0.0; _rResult.m_d14 = 0.0; + _rResult.m_d21 = Sine(_kdAngle); _rResult.m_d22 = Cosine(_kdAngle); _rResult.m_d23 = 0.0; _rResult.m_d24 = 0.0; + _rResult.m_d31 = 0.0; _rResult.m_d32 = 0.0; _rResult.m_d33 = 1.0; _rResult.m_d34 = 0.0; + _rResult.m_d41 = 0.0; _rResult.m_d42 = 0.0; _rResult.m_d43 = 0.0; _rResult.m_d44 = 1.0; + + return(_rResult); +} + +const TMatrix4f& AxisRotationZMatrix( TMatrix4f& _rResult, + const float _kfAngle) +{ + _rResult.m_f11 = Cosine(_kfAngle); _rResult.m_f12 = -Sine(_kfAngle); _rResult.m_f13 = 0.0f; _rResult.m_f14 = 0.0f; + _rResult.m_f21 = Sine(_kfAngle); _rResult.m_f22 = Cosine(_kfAngle); _rResult.m_f23 = 0.0f; _rResult.m_f24 = 0.0f; + _rResult.m_f31 = 0.0f; _rResult.m_f32 = 0.0f; _rResult.m_f33 = 1.0f; _rResult.m_f34 = 0.0f; + _rResult.m_f41 = 0.0f; _rResult.m_f42 = 0.0f; _rResult.m_f43 = 0.0f; _rResult.m_f44 = 1.0f; + + return(_rResult); +} + +const TMatrix4d& PerspectiveMatrix( TMatrix4d& _rResult, + const double _kdLeft, const double _kdRight, + const double _kdBottom, const double _kdTop, + const double _kdFar, const double _kdNear) +{ + _rResult.m_d11 = ((2.0 * _kdNear) / (_kdRight - _kdLeft)); _rResult.m_d12 = 0.0; _rResult.m_d13 = ((_kdRight + _kdLeft) / (_kdRight - _kdLeft)); _rResult.m_d14 = 0.0; + _rResult.m_d21 = 0.0; _rResult.m_d22 = ((2.0 * _kdNear) / (_kdTop - _kdBottom)); _rResult.m_d23 = ((_kdTop + _kdBottom) / (_kdTop - _kdBottom)); _rResult.m_d24 = 0.0; + _rResult.m_d31 = 0.0; _rResult.m_d32 = 0.0; _rResult.m_d33 = (-(_kdFar + _kdNear) / (_kdFar - _kdNear)); _rResult.m_d34 = ((-2.0 * _kdFar * _kdNear) / (_kdFar - _kdNear)); + _rResult.m_d41 = 0.0; _rResult.m_d42 = 0.0; _rResult.m_d43 = -1.0; _rResult.m_d44 = 0.0; + + return(_rResult); +} + +const TMatrix4f& PerspectiveMatrix( TMatrix4f& _rResult, + const float _kfLeft, const float _kfRight, + const float _kfBottom, const float _kfTop, + const float _kfFar, const float _kfNear) +{ + _rResult.m_f11 = ((2.0f * _kfNear) / (_kfRight - _kfLeft)); _rResult.m_f12 = 0.0f; _rResult.m_f13 = ((_kfRight + _kfLeft) / (_kfRight - _kfLeft)); _rResult.m_f14 = 0.0f; + _rResult.m_f21 = 0.0f; _rResult.m_f22 = ((2.0f * _kfNear) / (_kfTop - _kfBottom)); _rResult.m_f23 = ((_kfTop + _kfBottom) / (_kfTop - _kfBottom)); _rResult.m_f24 = 0.0f; + _rResult.m_f31 = 0.0f; _rResult.m_f32 = 0.0f; _rResult.m_f33 = (-(_kfFar + _kfNear) / (_kfFar - _kfNear)); _rResult.m_f34 = ((-2.0f * _kfFar * _kfNear) / (_kfFar - _kfNear)); + _rResult.m_f41 = 0.0f; _rResult.m_f42 = 0.0f; _rResult.m_f43 = -1.0f; _rResult.m_f44 = 0.0f; + + return(_rResult); +} + +const TMatrix4d& PerspectiveMatrix( TMatrix4d& _rResult, + const double _kdFovX, const double _kdFovY, + const double _kdFar, const double _kdNear) +{ + _rResult.m_d11 = ArcTan(_kdFovX / 2.0); _rResult.m_d12 = 0.0; _rResult.m_d13 = 0.0; _rResult.m_d14 = 0.0; + _rResult.m_d21 = 0.0; _rResult.m_d22 = ArcTan(_kdFovY / 2.0); _rResult.m_d23 = 0.0; _rResult.m_d24 = 0.0; + _rResult.m_d31 = 0.0; _rResult.m_d32 = 0.0; _rResult.m_d33 = (-(_kdFar + _kdNear) / (_kdFar - _kdNear)); _rResult.m_d34 = ((-2.0 * _kdFar * _kdNear) / (_kdFar - _kdNear)); + _rResult.m_d41 = 0.0; _rResult.m_d42 = 0.0; _rResult.m_d43 = -1.0; _rResult.m_d44 = 0.0; + + return(_rResult); +} + +const TMatrix4f& PerspectiveMatrix( TMatrix4f& _rResult, + const float _kfFovX, const float _kfFovY, + const float _kfFar, const float _kfNear) +{ + _rResult.m_f11 = ArcTan(_kfFovX / 2.0f); _rResult.m_f12 = 0.0f; _rResult.m_f13 = 0.0f; _rResult.m_f14 = 0.0f; + _rResult.m_f21 = 0.0f; _rResult.m_f22 = ArcTan(_kfFovY / 2.0f); _rResult.m_f23 = 0.0f; _rResult.m_f24 = 0.0f; + _rResult.m_f31 = 0.0f; _rResult.m_f32 = 0.0f; _rResult.m_f33 = (-(_kfFar + _kfNear) / (_kfFar - _kfNear)); _rResult.m_f34 = ((-2.0f * _kfFar * _kfNear) / (_kfFar - _kfNear)); + _rResult.m_f41 = 0.0f; _rResult.m_f42 = 0.0f; _rResult.m_f43 = -1.0f; _rResult.m_f44 = 0.0f; + + return(_rResult); +} + +const TMatrix4d& OrthographicMatrix( TMatrix4d& _rResult, + const double _kdLeft, const double _kdRight, + const double _kdBottom, const double _kdTop, + const double _kdFar, const double _kdNear) +{ + _rResult.m_d11 = (2.0 / (_kdRight - _kdLeft)); _rResult.m_d12 = 0.0; _rResult.m_d13 = 0.0; _rResult.m_d14 = (-(_kdRight + _kdLeft) / (_kdRight - _kdLeft)); + _rResult.m_d21 = 0.0; _rResult.m_d22 = (2.0 / (_kdTop - _kdBottom)); _rResult.m_d23 = 0.0; _rResult.m_d24 = (-(_kdTop + _kdBottom) / (_kdTop - _kdBottom)); + _rResult.m_d31 = 0.0; _rResult.m_d32 = 0.0; _rResult.m_d33 = (-2.0 / (_kdFar - _kdNear)); _rResult.m_d34 = (-(_kdFar + _kdNear) / (_kdFar - _kdNear)); + _rResult.m_d41 = 0.0; _rResult.m_d42 = 0.0; _rResult.m_d43 = 0.0; _rResult.m_d44 = 1.0; + + return(_rResult); +} + +const TMatrix4f& OrthographicMatrix( TMatrix4f& _rResult, + const float _kfLeft, const float _kfRight, + const float _kfBottom, const float _kfTop, + const float _kfFar, const float _kfNear) +{ + _rResult.m_f11 = (2.0f / (_kfRight - _kfLeft)); _rResult.m_f12 = 0.0f; _rResult.m_f13 = 0.0f; _rResult.m_f14 = (-(_kfRight + _kfLeft) / (_kfRight - _kfLeft)); + _rResult.m_f21 = 0.0f; _rResult.m_f22 = (2.0f / (_kfTop - _kfBottom)); _rResult.m_f23 = 0.0f; _rResult.m_f24 = (-(_kfTop + _kfBottom) / (_kfTop - _kfBottom)); + _rResult.m_f31 = 0.0f; _rResult.m_f32 = 0.0f; _rResult.m_f33 = (-2.0f / (_kfFar - _kfNear)); _rResult.m_f34 = (-(_kfFar + _kfNear) / (_kfFar - _kfNear)); + _rResult.m_f41 = 0.0f; _rResult.m_f42 = 0.0f; _rResult.m_f43 = 0.0f; _rResult.m_f44 = 1.0f; + + return(_rResult); +} + +const TMatrix4d& OrthographicMatrix( TMatrix4d& _rResult, + const double _kdWidth, const double _kdHeight, + const double _kdFar, const double _kdNear) +{ + const double kdRight = _kdWidth / 2.0; + const double kdTop = _kdHeight / 2.0; + + _rResult.m_d11 = (1.0 / kdRight); _rResult.m_d12 = 0.0; _rResult.m_d13 = 0.0; _rResult.m_d14 = 0.0; + _rResult.m_d21 = 0.0; _rResult.m_d22 = (1.0 / kdTop); _rResult.m_d23 = 0.0; _rResult.m_d24 = 0.0; + _rResult.m_d31 = 0.0; _rResult.m_d32 = 0.0; _rResult.m_d33 = (-2.0 / (_kdFar - _kdNear)); _rResult.m_d34 = (-(_kdFar + _kdNear) / (_kdFar - _kdNear)); + _rResult.m_d41 = 0.0; _rResult.m_d42 = 0.0; _rResult.m_d43 = 0.0; _rResult.m_d44 = 1.0; + + return(_rResult); +} + +const TMatrix4f& OrthographicMatrix( TMatrix4f& _rResult, + const float _kfWidth, const float _kfHeight, + const float _kfFar, const float _kfNear) +{ + const float kfRight = _kfWidth / 2.0f; + const float kfTop = _kfHeight / 2.0f; + + _rResult.m_f11 = (1.0f / kfRight); _rResult.m_f12 = 0.0f; _rResult.m_f13 = 0.0f; _rResult.m_f14 = 0.0f; + _rResult.m_f21 = 0.0f; _rResult.m_f22 = (1.0f / kfTop); _rResult.m_f23 = 0.0f; _rResult.m_f24 = 0.0f; + _rResult.m_f31 = 0.0f; _rResult.m_f32 = 0.0f; _rResult.m_f33 = (-2.0f / (_kfFar - _kfNear)); _rResult.m_f34 = (-(_kfFar + _kfNear) / (_kfFar - _kfNear)); + _rResult.m_f41 = 0.0f; _rResult.m_f42 = 0.0f; _rResult.m_f43 = 0.0f; _rResult.m_f44 = 1.0f; + + return(_rResult); +} + +// +// Matrix 3 +// + +const bool Equal(const TMatrix3d& _krA, const TMatrix3d& _krB, const double _kdEpsilon) +{ + const bool kbEqual = (Magnitude(_krA.m_d11 - _krB.m_d11) < _kdEpsilon) + && (Magnitude(_krA.m_d12 - _krB.m_d12) < _kdEpsilon) + && (Magnitude(_krA.m_d13 - _krB.m_d13) < _kdEpsilon) + && (Magnitude(_krA.m_d21 - _krB.m_d21) < _kdEpsilon) + && (Magnitude(_krA.m_d22 - _krB.m_d22) < _kdEpsilon) + && (Magnitude(_krA.m_d23 - _krB.m_d23) < _kdEpsilon) + && (Magnitude(_krA.m_d31 - _krB.m_d31) < _kdEpsilon) + && (Magnitude(_krA.m_d32 - _krB.m_d32) < _kdEpsilon) + && (Magnitude(_krA.m_d33 - _krB.m_d33) < _kdEpsilon); + + return(kbEqual); +} + +const bool Equal(const TMatrix3f& _krA, const TMatrix3f& _krB, const float _kfEpsilon) +{ + const bool kbEqual = (Magnitude(_krA.m_f11 - _krB.m_f11) < _kfEpsilon) + && (Magnitude(_krA.m_f12 - _krB.m_f12) < _kfEpsilon) + && (Magnitude(_krA.m_f13 - _krB.m_f13) < _kfEpsilon) + && (Magnitude(_krA.m_f21 - _krB.m_f21) < _kfEpsilon) + && (Magnitude(_krA.m_f22 - _krB.m_f22) < _kfEpsilon) + && (Magnitude(_krA.m_f23 - _krB.m_f23) < _kfEpsilon) + && (Magnitude(_krA.m_f31 - _krB.m_f31) < _kfEpsilon) + && (Magnitude(_krA.m_f32 - _krB.m_f32) < _kfEpsilon) + && (Magnitude(_krA.m_f33 - _krB.m_f33) < _kfEpsilon); + + return(kbEqual); +} + +const TMatrix3d& ZeroMatrix(TMatrix3d& _rResult) +{ + _rResult.m_d11 = 0.0; _rResult.m_d12 = 0.0; _rResult.m_d13 = 0.0; + _rResult.m_d21 = 0.0; _rResult.m_d22 = 0.0; _rResult.m_d23 = 0.0; + _rResult.m_d31 = 0.0; _rResult.m_d32 = 0.0; _rResult.m_d33 = 0.0; + + return(_rResult); +} + +const TMatrix3f& ZeroMatrix(TMatrix3f& _rResult) +{ + _rResult.m_f11 = 0.0f; _rResult.m_f12 = 0.0f; _rResult.m_f13 = 0.0f; + _rResult.m_f21 = 0.0f; _rResult.m_f22 = 0.0f; _rResult.m_f23 = 0.0f; + _rResult.m_f31 = 0.0f; _rResult.m_f32 = 0.0f; _rResult.m_f33 = 0.0f; + + return(_rResult); +} + +const TMatrix3d& IdentityMatrix(TMatrix3d& _rResult) +{ + _rResult.m_d11 = 1.0; _rResult.m_d12 = 0.0; _rResult.m_d13 = 0.0; + _rResult.m_d21 = 0.0; _rResult.m_d22 = 1.0; _rResult.m_d23 = 0.0; + _rResult.m_d31 = 0.0; _rResult.m_d32 = 0.0; _rResult.m_d33 = 1.0; + + return(_rResult); +} + +const TMatrix3f& IdentityMatrix(TMatrix3f& _rResult) +{ + _rResult.m_f11 = 1.0f; _rResult.m_f12 = 0.0f; _rResult.m_f13 = 0.0f; + _rResult.m_f21 = 0.0f; _rResult.m_f22 = 1.0f; _rResult.m_f23 = 0.0f; + _rResult.m_f31 = 0.0f; _rResult.m_f32 = 0.0f; _rResult.m_f33 = 1.0f; + + return(_rResult); +} + +const TMatrix3d& Multiply(TMatrix3d& _rResult, + const TMatrix3d& _krA, + const TMatrix3d& _krB) +{ + _rResult.m_d11 = ((_krA.m_d11 * _krB.m_d11) + (_krA.m_d12 * _krB.m_d21) + (_krA.m_d13 * _krB.m_d31)); + _rResult.m_d12 = ((_krA.m_d11 * _krB.m_d12) + (_krA.m_d12 * _krB.m_d22) + (_krA.m_d13 * _krB.m_d32)); + _rResult.m_d13 = ((_krA.m_d11 * _krB.m_d13) + (_krA.m_d12 * _krB.m_d23) + (_krA.m_d13 * _krB.m_d33)); + + _rResult.m_d21 = ((_krA.m_d21 * _krB.m_d11) + (_krA.m_d22 * _krB.m_d21) + (_krA.m_d23 * _krB.m_d31)); + _rResult.m_d22 = ((_krA.m_d21 * _krB.m_d12) + (_krA.m_d22 * _krB.m_d22) + (_krA.m_d23 * _krB.m_d32)); + _rResult.m_d23 = ((_krA.m_d21 * _krB.m_d13) + (_krA.m_d22 * _krB.m_d23) + (_krA.m_d23 * _krB.m_d33)); + + _rResult.m_d31 = ((_krA.m_d31 * _krB.m_d11) + (_krA.m_d32 * _krB.m_d21) + (_krA.m_d33 * _krB.m_d31)); + _rResult.m_d32 = ((_krA.m_d31 * _krB.m_d12) + (_krA.m_d32 * _krB.m_d22) + (_krA.m_d33 * _krB.m_d32)); + _rResult.m_d33 = ((_krA.m_d31 * _krB.m_d13) + (_krA.m_d32 * _krB.m_d23) + (_krA.m_d33 * _krB.m_d33)); + + return(_rResult); +} + +const TMatrix3f& Multiply(TMatrix3f& _rResult, + const TMatrix3f& _krA, + const TMatrix3f& _krB) +{ + _rResult.m_f11 = ((_krA.m_f11 * _krB.m_f11) + (_krA.m_f12 * _krB.m_f21) + (_krA.m_f13 * _krB.m_f31)); + _rResult.m_f12 = ((_krA.m_f11 * _krB.m_f12) + (_krA.m_f12 * _krB.m_f22) + (_krA.m_f13 * _krB.m_f32)); + _rResult.m_f13 = ((_krA.m_f11 * _krB.m_f13) + (_krA.m_f12 * _krB.m_f23) + (_krA.m_f13 * _krB.m_f33)); + + _rResult.m_f21 = ((_krA.m_f21 * _krB.m_f11) + (_krA.m_f22 * _krB.m_f21) + (_krA.m_f23 * _krB.m_f31)); + _rResult.m_f22 = ((_krA.m_f21 * _krB.m_f12) + (_krA.m_f22 * _krB.m_f22) + (_krA.m_f23 * _krB.m_f32)); + _rResult.m_f23 = ((_krA.m_f21 * _krB.m_f13) + (_krA.m_f22 * _krB.m_f23) + (_krA.m_f23 * _krB.m_f33)); + + _rResult.m_f31 = ((_krA.m_f31 * _krB.m_f11) + (_krA.m_f32 * _krB.m_f21) + (_krA.m_f33 * _krB.m_f31)); + _rResult.m_f32 = ((_krA.m_f31 * _krB.m_f12) + (_krA.m_f32 * _krB.m_f22) + (_krA.m_f33 * _krB.m_f32)); + _rResult.m_f33 = ((_krA.m_f31 * _krB.m_f13) + (_krA.m_f32 * _krB.m_f23) + (_krA.m_f33 * _krB.m_f33)); + + return(_rResult); +} + +const TMatrix3d& ScalarMultiply( TMatrix3d& _rResult, + const TMatrix3d& _krMatrix, + const double _kdScalar) +{ + _rResult.m_d11 = _krMatrix.m_d11 * _kdScalar; _rResult.m_d12 = _krMatrix.m_d12 * _kdScalar; _rResult.m_d13 = _krMatrix.m_d13 * _kdScalar; + _rResult.m_d21 = _krMatrix.m_d21 * _kdScalar; _rResult.m_d22 = _krMatrix.m_d22 * _kdScalar; _rResult.m_d23 = _krMatrix.m_d23 * _kdScalar; + _rResult.m_d31 = _krMatrix.m_d31 * _kdScalar; _rResult.m_d32 = _krMatrix.m_d32 * _kdScalar; _rResult.m_d33 = _krMatrix.m_d33 * _kdScalar; + + return(_rResult); +} + +const TMatrix3f& ScalarMultiply( TMatrix3f& _rResult, + const TMatrix3f& _krMatrix, + const float _kfScalar) +{ + _rResult.m_f11 = _krMatrix.m_f11 * _kfScalar; _rResult.m_f12 = _krMatrix.m_f12 * _kfScalar; _rResult.m_f13 = _krMatrix.m_f13 * _kfScalar; + _rResult.m_f21 = _krMatrix.m_f21 * _kfScalar; _rResult.m_f22 = _krMatrix.m_f22 * _kfScalar; _rResult.m_f23 = _krMatrix.m_f23 * _kfScalar; + _rResult.m_f31 = _krMatrix.m_f31 * _kfScalar; _rResult.m_f32 = _krMatrix.m_f32 * _kfScalar; _rResult.m_f33 = _krMatrix.m_f33 * _kfScalar; + + return(_rResult); +} + +const TVector3d& VectorMultiply( TVector3d& _rResult, + const TMatrix3d& _krA, + const TVector3d& _krB) +{ + _rResult.m_dX = (_krA.m_d11 * _krB.m_dX) + (_krA.m_d12 * _krB.m_dY) + (_krA.m_d13 * _krB.m_dZ); + _rResult.m_dY = (_krA.m_d21 * _krB.m_dX) + (_krA.m_d22 * _krB.m_dY) + (_krA.m_d23 * _krB.m_dZ); + _rResult.m_dZ = (_krA.m_d31 * _krB.m_dX) + (_krA.m_d32 * _krB.m_dY) + (_krA.m_d33 * _krB.m_dZ); + + return(_rResult); +} + +const TVector3f& VectorMultiply( TVector3f& _rResult, + const TMatrix3f& _krA, + const TVector3f& _krB) +{ + _rResult.m_fX = (_krA.m_f11 * _krB.m_fX) + (_krA.m_f12 * _krB.m_fY) + (_krA.m_f13 * _krB.m_fZ); + _rResult.m_fY = (_krA.m_f21 * _krB.m_fX) + (_krA.m_f22 * _krB.m_fY) + (_krA.m_f23 * _krB.m_fZ); + _rResult.m_fZ = (_krA.m_f31 * _krB.m_fX) + (_krA.m_f32 * _krB.m_fY) + (_krA.m_f33 * _krB.m_fZ); + + return(_rResult); +} + +const TMatrix3d& Add( TMatrix3d& _rResult, + const TMatrix3d& _krA, + const TMatrix3d& _krB) +{ + _rResult.m_d11 = _krA.m_d11 + _krB.m_d11; _rResult.m_d12 = _krA.m_d12 + _krB.m_d12; _rResult.m_d13 = _krA.m_d13 + _krB.m_d13; + _rResult.m_d21 = _krA.m_d21 + _krB.m_d21; _rResult.m_d22 = _krA.m_d22 + _krB.m_d22; _rResult.m_d23 = _krA.m_d23 + _krB.m_d23; + _rResult.m_d31 = _krA.m_d31 + _krB.m_d31; _rResult.m_d32 = _krA.m_d32 + _krB.m_d32; _rResult.m_d33 = _krA.m_d33 + _krB.m_d33; + + return(_rResult); +} + +const TMatrix3f& Add( TMatrix3f& _rResult, + const TMatrix3f& _krA, + const TMatrix3f& _krB) +{ + _rResult.m_f11 = _krA.m_f11 + _krB.m_f11; _rResult.m_f12 = _krA.m_f12 + _krB.m_f12; _rResult.m_f13 = _krA.m_f13 + _krB.m_f13; + _rResult.m_f21 = _krA.m_f21 + _krB.m_f21; _rResult.m_f22 = _krA.m_f22 + _krB.m_f22; _rResult.m_f23 = _krA.m_f23 + _krB.m_f23; + _rResult.m_f31 = _krA.m_f31 + _krB.m_f31; _rResult.m_f32 = _krA.m_f32 + _krB.m_f32; _rResult.m_f33 = _krA.m_f33 + _krB.m_f33; + + return(_rResult); +} + +const TMatrix3d& Transpose( TMatrix3d& _rResult, + const TMatrix3d& _krMatrix) +{ + _rResult.m_d11 = _krMatrix.m_d11; _rResult.m_d12 = _krMatrix.m_d21; _rResult.m_d13 = _krMatrix.m_d31; + _rResult.m_d21 = _krMatrix.m_d12; _rResult.m_d22 = _krMatrix.m_d22; _rResult.m_d23 = _krMatrix.m_d32; + _rResult.m_d31 = _krMatrix.m_d13; _rResult.m_d32 = _krMatrix.m_d23; _rResult.m_d33 = _krMatrix.m_d33; + + return(_rResult); +} + +const TMatrix3f& Transpose( TMatrix3f& _rResult, + const TMatrix3f& _krMatrix) +{ + _rResult.m_f11 = _krMatrix.m_f11; _rResult.m_f12 = _krMatrix.m_f21; _rResult.m_f13 = _krMatrix.m_f31; + _rResult.m_f21 = _krMatrix.m_f12; _rResult.m_f22 = _krMatrix.m_f22; _rResult.m_f23 = _krMatrix.m_f32; + _rResult.m_f31 = _krMatrix.m_f13; _rResult.m_f32 = _krMatrix.m_f23; _rResult.m_f33 = _krMatrix.m_f33; + + return(_rResult); +} + +const double GetElement( const TMatrix3d& _krMatrix, + const size_t _kRow, + const size_t _kColumn) +{ + double dResult; + + if(_kRow == 1) + { + if(_kColumn == 1) + { + dResult = _krMatrix.m_d11; + } + else if(_kColumn == 2) + { + dResult = _krMatrix.m_d12; + } + else if(_kColumn == 3) + { + dResult = _krMatrix.m_d13; + } + } + else if(_kRow == 2) + { + if(_kColumn == 1) + { + dResult = _krMatrix.m_d21; + } + else if(_kColumn == 2) + { + dResult = _krMatrix.m_d22; + } + else if(_kColumn == 3) + { + dResult = _krMatrix.m_d23; + } + } + else if(_kRow == 3) + { + if(_kColumn == 1) + { + dResult = _krMatrix.m_d31; + } + else if(_kColumn == 2) + { + dResult = _krMatrix.m_d32; + } + else if(_kColumn == 3) + { + dResult = _krMatrix.m_d33; + } + } + + return(dResult); +} + +const float GetElement( const TMatrix3f& _krMatrix, + const size_t _kRow, + const size_t _kColumn) +{ + float fResult; + + if(_kRow == 1) + { + if(_kColumn == 1) + { + fResult = _krMatrix.m_f11; + } + else if(_kColumn == 2) + { + fResult = _krMatrix.m_f12; + } + else if(_kColumn == 3) + { + fResult = _krMatrix.m_f13; + } + } + else if(_kRow == 2) + { + if(_kColumn == 1) + { + fResult = _krMatrix.m_f21; + } + else if(_kColumn == 2) + { + fResult = _krMatrix.m_f22; + } + else if(_kColumn == 3) + { + fResult = _krMatrix.m_f23; + } + } + else if(_kRow == 3) + { + if(_kColumn == 1) + { + fResult = _krMatrix.m_f31; + } + else if(_kColumn == 2) + { + fResult = _krMatrix.m_f32; + } + else if(_kColumn == 3) + { + fResult = _krMatrix.m_f33; + } + } + + return(fResult); +} + +TMatrix3d& SetElement( TMatrix3d& _rResult, + const double _kdValue, + const size_t _kRow, + const size_t _kColumn) +{ + if(_kRow == 1) + { + if(_kColumn == 1) + { + _rResult.m_d11 = _kdValue; + } + else if(_kColumn == 2) + { + _rResult.m_d12 = _kdValue; + } + else if(_kColumn == 3) + { + _rResult.m_d13 = _kdValue; + } + } + else if(_kRow == 2) + { + if(_kColumn == 1) + { + _rResult.m_d21 = _kdValue; + } + else if(_kColumn == 2) + { + _rResult.m_d22 = _kdValue; + } + else if(_kColumn == 3) + { + _rResult.m_d23 = _kdValue; + } + } + else if(_kRow == 3) + { + if(_kColumn == 1) + { + _rResult.m_d31 = _kdValue; + } + else if(_kColumn == 2) + { + _rResult.m_d32 = _kdValue; + } + else if(_kColumn == 3) + { + _rResult.m_d33 = _kdValue; + } + } + + return(_rResult); +} + +TMatrix3f& SetElement( TMatrix3f& _rResult, + const float _kfValue, + const size_t _kRow, + const size_t _kColumn) +{ + if(_kRow == 1) + { + if(_kColumn == 1) + { + _rResult.m_f11 = _kfValue; + } + else if(_kColumn == 2) + { + _rResult.m_f12 = _kfValue; + } + else if(_kColumn == 3) + { + _rResult.m_f13 = _kfValue; + } + } + else if(_kRow == 2) + { + if(_kColumn == 1) + { + _rResult.m_f21 = _kfValue; + } + else if(_kColumn == 2) + { + _rResult.m_f22 = _kfValue; + } + else if(_kColumn == 3) + { + _rResult.m_f23 = _kfValue; + } + } + else if(_kRow == 3) + { + if(_kColumn == 1) + { + _rResult.m_f31 = _kfValue; + } + else if(_kColumn == 2) + { + _rResult.m_f32 = _kfValue; + } + else if(_kColumn == 3) + { + _rResult.m_f33 = _kfValue; + } + } + + return(_rResult); +} + +const TMatrix2d& Submatrix( TMatrix2d& _rResult, + const TMatrix3d& _krMatrix, + const size_t _kDeletedRow, + const size_t _kDeletedColumn) +{ + if(_kDeletedRow == 1) + { + if(_kDeletedColumn == 1) + { + _rResult.m_d11 = _krMatrix.m_d22; _rResult.m_d12 = _krMatrix.m_d23; + _rResult.m_d21 = _krMatrix.m_d32; _rResult.m_d22 = _krMatrix.m_d33; + } + else if(_kDeletedColumn == 2) + { + _rResult.m_d11 = _krMatrix.m_d21; _rResult.m_d12 = _krMatrix.m_d23; + _rResult.m_d21 = _krMatrix.m_d31; _rResult.m_d22 = _krMatrix.m_d33; + } + else if(_kDeletedColumn == 3) + { + _rResult.m_d11 = _krMatrix.m_d21; _rResult.m_d12 = _krMatrix.m_d22; + _rResult.m_d21 = _krMatrix.m_d31; _rResult.m_d22 = _krMatrix.m_d32; + } + } + else if(_kDeletedRow == 2) + { + if(_kDeletedColumn == 1) + { + _rResult.m_d11 = _krMatrix.m_d12; _rResult.m_d12 = _krMatrix.m_d13; + _rResult.m_d21 = _krMatrix.m_d32; _rResult.m_d22 = _krMatrix.m_d33; + } + else if(_kDeletedColumn == 2) + { + _rResult.m_d11 = _krMatrix.m_d11; _rResult.m_d12 = _krMatrix.m_d13; + _rResult.m_d21 = _krMatrix.m_d31; _rResult.m_d22 = _krMatrix.m_d33; + } + else if(_kDeletedColumn == 3) + { + _rResult.m_d11 = _krMatrix.m_d11; _rResult.m_d12 = _krMatrix.m_d12; + _rResult.m_d21 = _krMatrix.m_d31; _rResult.m_d22 = _krMatrix.m_d32; + } + } + else if(_kDeletedRow == 3) + { + if(_kDeletedColumn == 1) + { + _rResult.m_d11 = _krMatrix.m_d12; _rResult.m_d12 = _krMatrix.m_d13; + _rResult.m_d21 = _krMatrix.m_d22; _rResult.m_d22 = _krMatrix.m_d23; + } + else if(_kDeletedColumn == 2) + { + _rResult.m_d11 = _krMatrix.m_d11; _rResult.m_d12 = _krMatrix.m_d13; + _rResult.m_d21 = _krMatrix.m_d21; _rResult.m_d22 = _krMatrix.m_d23; + } + else if(_kDeletedColumn == 3) + { + _rResult.m_d11 = _krMatrix.m_d11; _rResult.m_d12 = _krMatrix.m_d12; + _rResult.m_d21 = _krMatrix.m_d21; _rResult.m_d22 = _krMatrix.m_d22; + } + } + + return(_rResult); +} + +const TMatrix2f& Submatrix( TMatrix2f& _rResult, + const TMatrix3f& _krMatrix, + const size_t _kDeletedRow, + const size_t _kDeletedColumn) +{ + if(_kDeletedRow == 1) + { + if(_kDeletedColumn == 1) + { + _rResult.m_f11 = _krMatrix.m_f22; _rResult.m_f12 = _krMatrix.m_f23; + _rResult.m_f21 = _krMatrix.m_f32; _rResult.m_f22 = _krMatrix.m_f33; + } + else if(_kDeletedColumn == 2) + { + _rResult.m_f11 = _krMatrix.m_f21; _rResult.m_f12 = _krMatrix.m_f23; + _rResult.m_f21 = _krMatrix.m_f31; _rResult.m_f22 = _krMatrix.m_f33; + } + else if(_kDeletedColumn == 3) + { + _rResult.m_f11 = _krMatrix.m_f21; _rResult.m_f12 = _krMatrix.m_f22; + _rResult.m_f21 = _krMatrix.m_f31; _rResult.m_f22 = _krMatrix.m_f32; + } + } + else if(_kDeletedRow == 2) + { + if(_kDeletedColumn == 1) + { + _rResult.m_f11 = _krMatrix.m_f12; _rResult.m_f12 = _krMatrix.m_f13; + _rResult.m_f21 = _krMatrix.m_f32; _rResult.m_f22 = _krMatrix.m_f33; + } + else if(_kDeletedColumn == 2) + { + _rResult.m_f11 = _krMatrix.m_f11; _rResult.m_f12 = _krMatrix.m_f13; + _rResult.m_f21 = _krMatrix.m_f31; _rResult.m_f22 = _krMatrix.m_f33; + } + else if(_kDeletedColumn == 3) + { + _rResult.m_f11 = _krMatrix.m_f11; _rResult.m_f12 = _krMatrix.m_f12; + _rResult.m_f21 = _krMatrix.m_f31; _rResult.m_f22 = _krMatrix.m_f32; + } + } + else if(_kDeletedRow == 3) + { + if(_kDeletedColumn == 1) + { + _rResult.m_f11 = _krMatrix.m_f12; _rResult.m_f12 = _krMatrix.m_f13; + _rResult.m_f21 = _krMatrix.m_f22; _rResult.m_f22 = _krMatrix.m_f23; + } + else if(_kDeletedColumn == 2) + { + _rResult.m_f11 = _krMatrix.m_f11; _rResult.m_f12 = _krMatrix.m_f13; + _rResult.m_f21 = _krMatrix.m_f21; _rResult.m_f22 = _krMatrix.m_f23; + } + else if(_kDeletedColumn == 3) + { + _rResult.m_f11 = _krMatrix.m_f11; _rResult.m_f12 = _krMatrix.m_f12; + _rResult.m_f21 = _krMatrix.m_f21; _rResult.m_f22 = _krMatrix.m_f22; + } + } + + return(_rResult); +} + +const double Determinant(const TMatrix3d& _krMatrix) +{ + const double kdDeterminant = (_krMatrix.m_d11 * _krMatrix.m_d22 * _krMatrix.m_d33) + + (_krMatrix.m_d12 * _krMatrix.m_d23 * _krMatrix.m_d31) + + (_krMatrix.m_d13 * _krMatrix.m_d21 * _krMatrix.m_d32) + - (_krMatrix.m_d13 * _krMatrix.m_d22 * _krMatrix.m_d31) + - (_krMatrix.m_d12 * _krMatrix.m_d21 * _krMatrix.m_d33) + - (_krMatrix.m_d11 * _krMatrix.m_d23 * _krMatrix.m_d32); + + return(kdDeterminant); +} + +const float Determinant(const TMatrix3f& _krMatrix) +{ + const float kfDeterminant = (_krMatrix.m_f11 * _krMatrix.m_f22 * _krMatrix.m_f33) + + (_krMatrix.m_f12 * _krMatrix.m_f23 * _krMatrix.m_f31) + + (_krMatrix.m_f13 * _krMatrix.m_f21 * _krMatrix.m_f32) + - (_krMatrix.m_f13 * _krMatrix.m_f22 * _krMatrix.m_f31) + - (_krMatrix.m_f12 * _krMatrix.m_f21 * _krMatrix.m_f33) + - (_krMatrix.m_f11 * _krMatrix.m_f23 * _krMatrix.m_f32); + + return(kfDeterminant); +} + +const double FirstMinor( const TMatrix3d& _krMatrix, + const size_t _kRow, + const size_t _kColumn) +{ + const TMatrix2d kSubmatrix = Submatrix(TMatrix2d(), _krMatrix, _kRow, _kColumn); + + return(Determinant(kSubmatrix)); +} + +const float FirstMinor( const TMatrix3f& _krMatrix, + const size_t _kRow, + const size_t _kColumn) +{ + const TMatrix2f kSubmatrix = Submatrix(TMatrix2f(), _krMatrix, _kRow, _kColumn); + + return(Determinant(kSubmatrix)); +} + +const TMatrix3d& MatrixOfMinors( TMatrix3d& _rResult, + const TMatrix3d& _krMatrix) +{ + _rResult.m_d11 = FirstMinor(_krMatrix, 1, 1); + _rResult.m_d12 = FirstMinor(_krMatrix, 1, 2); + _rResult.m_d13 = FirstMinor(_krMatrix, 1, 3); + + _rResult.m_d21 = FirstMinor(_krMatrix, 2, 1); + _rResult.m_d22 = FirstMinor(_krMatrix, 2, 2); + _rResult.m_d23 = FirstMinor(_krMatrix, 2, 3); + + _rResult.m_d31 = FirstMinor(_krMatrix, 3, 1); + _rResult.m_d32 = FirstMinor(_krMatrix, 3, 2); + _rResult.m_d33 = FirstMinor(_krMatrix, 3, 3); + + return(_rResult); +} + +const TMatrix3f& MatrixOfMinors( TMatrix3f& _rResult, + const TMatrix3f& _krMatrix) +{ + _rResult.m_f11 = FirstMinor(_krMatrix, 1, 1); + _rResult.m_f12 = FirstMinor(_krMatrix, 1, 2); + _rResult.m_f13 = FirstMinor(_krMatrix, 1, 3); + + _rResult.m_f21 = FirstMinor(_krMatrix, 2, 1); + _rResult.m_f22 = FirstMinor(_krMatrix, 2, 2); + _rResult.m_f23 = FirstMinor(_krMatrix, 2, 3); + + _rResult.m_f31 = FirstMinor(_krMatrix, 3, 1); + _rResult.m_f32 = FirstMinor(_krMatrix, 3, 2); + _rResult.m_f33 = FirstMinor(_krMatrix, 3, 3); + + return(_rResult); +} + +const TMatrix3d& MatrixOfCofactors( TMatrix3d& _rResult, + const TMatrix3d& _krMatrix) +{ + _rResult = MatrixOfMinors(_rResult, _krMatrix); + + _rResult.m_d12 *= -1.0; + + _rResult.m_d21 *= -1.0; + _rResult.m_d23 *= -1.0; + + _rResult.m_d32 *= -1.0; + + return(_rResult); +} + +const TMatrix3f& MatrixOfCofactors( TMatrix3f& _rResult, + const TMatrix3f& _krMatrix) +{ + _rResult = MatrixOfMinors(_rResult, _krMatrix); + + _rResult.m_f12 *= -1.0f; + + _rResult.m_f21 *= -1.0f; + _rResult.m_f23 *= -1.0f; + + _rResult.m_f32 *= -1.0f; + + return(_rResult); +} + +const TMatrix3d& Inverse( TMatrix3d& _rResult, + const TMatrix3d& _krMatrix) +{ + _rResult = ScalarMultiply(_rResult, + Transpose(TMatrix3d(), + MatrixOfCofactors(TMatrix3d(), _krMatrix)), + 1.0/Determinant(_krMatrix)); + + return(_rResult); +} + +const TMatrix3f& Inverse( TMatrix3f& _rResult, + const TMatrix3f& _krMatrix) +{ + _rResult = ScalarMultiply(_rResult, + Transpose(TMatrix3f(), + MatrixOfCofactors(TMatrix3f(), _krMatrix)), + 1.0f/Determinant(_krMatrix)); + + return(_rResult); +} + +// +// Matrix 2 +// + +const bool Equal(const TMatrix2d& _krA, const TMatrix2d& _krB, const double _kdEpsilon) +{ + const bool kbEqual = (Magnitude(_krA.m_d11 - _krB.m_d11) < _kdEpsilon) + && (Magnitude(_krA.m_d12 - _krB.m_d12) < _kdEpsilon) + && (Magnitude(_krA.m_d21 - _krB.m_d21) < _kdEpsilon) + && (Magnitude(_krA.m_d22 - _krB.m_d22) < _kdEpsilon); + + return(kbEqual); +} + +const bool Equal(const TMatrix2f& _krA, const TMatrix2f& _krB, const float _kfEpsilon) +{ + const bool kbEqual = (Magnitude(_krA.m_f11 - _krB.m_f11) < _kfEpsilon) + && (Magnitude(_krA.m_f12 - _krB.m_f12) < _kfEpsilon) + && (Magnitude(_krA.m_f21 - _krB.m_f21) < _kfEpsilon) + && (Magnitude(_krA.m_f22 - _krB.m_f22) < _kfEpsilon); + + return(kbEqual); +} + +const TMatrix2d& ZeroMatrix(TMatrix2d& _rResult) +{ + _rResult.m_d11 = 0.0; _rResult.m_d12 = 0.0; + _rResult.m_d21 = 0.0; _rResult.m_d22 = 0.0; + + return(_rResult); +} + +const TMatrix2f& ZeroMatrix(TMatrix2f& _rResult) +{ + _rResult.m_f11 = 0.0f; _rResult.m_f12 = 0.0f; + _rResult.m_f21 = 0.0f; _rResult.m_f22 = 0.0f; + + return(_rResult); +} + +const TMatrix2d& IdentityMatrix(TMatrix2d& _rResult) +{ + _rResult.m_d11 = 1.0; _rResult.m_d12 = 0.0; + _rResult.m_d21 = 0.0; _rResult.m_d22 = 1.0; + + return(_rResult); +} + +const TMatrix2f& IdentityMatrix(TMatrix2f& _rResult) +{ + _rResult.m_f11 = 1.0f; _rResult.m_f12 = 0.0f; + _rResult.m_f21 = 0.0f; _rResult.m_f22 = 1.0f; + + return(_rResult); +} + +const TMatrix2d& Multiply(TMatrix2d& _rResult, + const TMatrix2d& _krA, + const TMatrix2d& _krB) +{ + _rResult.m_d11 = ((_krA.m_d11 * _krB.m_d11) + (_krA.m_d12 * _krB.m_d21)); + _rResult.m_d12 = ((_krA.m_d11 * _krB.m_d12) + (_krA.m_d12 * _krB.m_d22)); + _rResult.m_d21 = ((_krA.m_d21 * _krB.m_d11) + (_krA.m_d22 * _krB.m_d21)); + _rResult.m_d22 = ((_krA.m_d21 * _krB.m_d12) + (_krA.m_d22 * _krB.m_d22)); + + return(_rResult); +} + +const TMatrix2f& Multiply(TMatrix2f& _rResult, + const TMatrix2f& _krA, + const TMatrix2f& _krB) +{ + _rResult.m_f11 = ((_krA.m_f11 * _krB.m_f11) + (_krA.m_f12 * _krB.m_f21)); + _rResult.m_f12 = ((_krA.m_f11 * _krB.m_f12) + (_krA.m_f12 * _krB.m_f22)); + _rResult.m_f21 = ((_krA.m_f21 * _krB.m_f11) + (_krA.m_f22 * _krB.m_f21)); + _rResult.m_f22 = ((_krA.m_f21 * _krB.m_f12) + (_krA.m_f22 * _krB.m_f22)); + + return(_rResult); +} + +const TMatrix2d& ScalarMultiply( TMatrix2d& _rResult, + const TMatrix2d& _krMatrix, + const double _kdScalar) +{ + _rResult.m_d11 = _krMatrix.m_d11 * _kdScalar; _rResult.m_d12 = _krMatrix.m_d12 * _kdScalar; + _rResult.m_d21 = _krMatrix.m_d21 * _kdScalar; _rResult.m_d22 = _krMatrix.m_d22 * _kdScalar; + + return(_rResult); +} + +const TMatrix2f& ScalarMultiply( TMatrix2f& _rResult, + const TMatrix2f& _krMatrix, + const float _kfScalar) +{ + _rResult.m_f11 = _krMatrix.m_f11 * _kfScalar; _rResult.m_f12 = _krMatrix.m_f12 * _kfScalar; + _rResult.m_f21 = _krMatrix.m_f21 * _kfScalar; _rResult.m_f22 = _krMatrix.m_f22 * _kfScalar; + + return(_rResult); +} + +const TVector2d& VectorMultiply( TVector2d& _rResult, + const TMatrix2d& _krA, + const TVector2d& _krB) +{ + _rResult.m_dX = (_krA.m_d11 * _krB.m_dX) + (_krA.m_d12 * _krB.m_dY); + _rResult.m_dY = (_krA.m_d21 * _krB.m_dX) + (_krA.m_d22 * _krB.m_dY); + + return(_rResult); +} + +const TVector2f& VectorMultiply( TVector2f& _rResult, + const TMatrix2f& _krA, + const TVector2f& _krB) +{ + _rResult.m_fX = (_krA.m_f11 * _krB.m_fX) + (_krA.m_f12 * _krB.m_fY); + _rResult.m_fY = (_krA.m_f21 * _krB.m_fX) + (_krA.m_f22 * _krB.m_fY); + + return(_rResult); +} + +const TMatrix2d& Add( TMatrix2d& _rResult, + const TMatrix2d& _krA, + const TMatrix2d& _krB) +{ + _rResult.m_d11 = _krA.m_d11 + _krB.m_d11; _rResult.m_d12 = _krA.m_d12 + _krB.m_d12; + _rResult.m_d21 = _krA.m_d21 + _krB.m_d21; _rResult.m_d22 = _krA.m_d22 + _krB.m_d22; + + return(_rResult); +} + +const TMatrix2f& Add( TMatrix2f& _rResult, + const TMatrix2f& _krA, + const TMatrix2f& _krB) +{ + _rResult.m_f11 = _krA.m_f11 + _krB.m_f11; _rResult.m_f12 = _krA.m_f12 + _krB.m_f12; + _rResult.m_f21 = _krA.m_f21 + _krB.m_f21; _rResult.m_f22 = _krA.m_f22 + _krB.m_f22; + + return(_rResult); +} + +const TMatrix2d& Transpose( TMatrix2d& _rResult, + const TMatrix2d& _krMatrix) +{ + _rResult.m_d11 = _krMatrix.m_d11; _rResult.m_d12 = _krMatrix.m_d21; + _rResult.m_d21 = _krMatrix.m_d12; _rResult.m_d22 = _krMatrix.m_d22; + + return(_rResult); +} + +const TMatrix2f& Transpose( TMatrix2f& _rResult, + const TMatrix2f& _krMatrix) +{ + _rResult.m_f11 = _krMatrix.m_f11; _rResult.m_f12 = _krMatrix.m_f21; + _rResult.m_f21 = _krMatrix.m_f12; _rResult.m_f22 = _krMatrix.m_f22; + + return(_rResult); +} + +const double GetElement( const TMatrix2d& _krMatrix, + const size_t _kRow, + const size_t _kColumn) +{ + double dResult; + + if(_kRow == 1) + { + if(_kColumn == 1) + { + dResult = _krMatrix.m_d11; + } + else if(_kColumn == 2) + { + dResult = _krMatrix.m_d12; + } + } + else if(_kRow == 2) + { + if(_kColumn == 1) + { + dResult = _krMatrix.m_d21; + } + else if(_kColumn == 2) + { + dResult = _krMatrix.m_d22; + } + } + + return(dResult); +} + +const float GetElement( const TMatrix2f& _krMatrix, + const size_t _kRow, + const size_t _kColumn) +{ + float fResult; + + if(_kRow == 1) + { + if(_kColumn == 1) + { + fResult = _krMatrix.m_f11; + } + else if(_kColumn == 2) + { + fResult = _krMatrix.m_f12; + } + } + else if(_kRow == 2) + { + if(_kColumn == 1) + { + fResult = _krMatrix.m_f21; + } + else if(_kColumn == 2) + { + fResult = _krMatrix.m_f22; + } + } + + return(fResult); +} + +TMatrix2d& SetElement( TMatrix2d& _rResult, + const double _kdValue, + const size_t _kRow, + const size_t _kColumn) +{ + if(_kRow == 1) + { + if(_kColumn == 1) + { + _rResult.m_d11 = _kdValue; + } + else if(_kColumn == 2) + { + _rResult.m_d12 = _kdValue; + } + } + else if(_kRow == 2) + { + if(_kColumn == 1) + { + _rResult.m_d21 = _kdValue; + } + else if(_kColumn == 2) + { + _rResult.m_d22 = _kdValue; + } + } + + return(_rResult); +} + +TMatrix2f& SetElement( TMatrix2f& _rResult, + const float _kfValue, + const size_t _kRow, + const size_t _kColumn) +{ + if(_kRow == 1) + { + if(_kColumn == 1) + { + _rResult.m_f11 = _kfValue; + } + else if(_kColumn == 2) + { + _rResult.m_f12 = _kfValue; + } + } + else if(_kRow == 2) + { + if(_kColumn == 1) + { + _rResult.m_f21 = _kfValue; + } + else if(_kColumn == 2) + { + _rResult.m_f22 = _kfValue; + } + } + + return(_rResult); +} + +const TMatrix2d& Inverse( TMatrix2d& _rResult, + const TMatrix2d& _krMatrix) +{ + const TMatrix2d kTemp{ _krMatrix.m_d22, -_krMatrix.m_d12, + -_krMatrix.m_d21, _krMatrix.m_d11}; + _rResult = ScalarMultiply(TMatrix2d(), kTemp, 1.0 / Determinant(_krMatrix)); + + return(_rResult); +} + +const TMatrix2f& Inverse( TMatrix2f& _rResult, + const TMatrix2f& _krMatrix) +{ + const TMatrix2f kTemp{ _krMatrix.m_f22, -_krMatrix.m_f12, + -_krMatrix.m_f21, _krMatrix.m_f11}; + _rResult = ScalarMultiply(TMatrix2f(), kTemp, 1.0f / Determinant(_krMatrix)); + + return(_rResult); +} + +const double Determinant(const TMatrix2d& _krMatrix) +{ + return( (_krMatrix.m_d11 * _krMatrix.m_d22) + - (_krMatrix.m_d12 * _krMatrix.m_d21)); +} + +const float Determinant(const TMatrix2f& _krMatrix) +{ + return( (_krMatrix.m_f11 * _krMatrix.m_f22) + - (_krMatrix.m_f12 * _krMatrix.m_f21)); +} \ No newline at end of file diff --git a/ReflexToQ3/ckmath/ckmath_matrix.h b/ReflexToQ3/ckmath/ckmath_matrix.h new file mode 100644 index 0000000..0d18b2c --- /dev/null +++ b/ReflexToQ3/ckmath/ckmath_matrix.h @@ -0,0 +1,410 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +#pragma once + +#ifndef __MATH_MATRIX_H__ +#define __MATH_MATRIX_H__ + +// Local Includes +#include "ckmath_types.h" + +// +// Matrix 4 +// + +const bool Equal(const TMatrix4d& _krA, const TMatrix4d& _krB, const double _kdEpsilon); + +const bool Equal(const TMatrix4f& _krA, const TMatrix4f& _krB, const float _kfEpsilon); + +const TMatrix4d& ZeroMatrix(TMatrix4d& _rResult); + +const TMatrix4f& ZeroMatrix(TMatrix4f& _rResult); + +const TMatrix4d& IdentityMatrix(TMatrix4d& _rResult); + +const TMatrix4f& IdentityMatrix(TMatrix4f& _rResult); + +const TMatrix4d& Multiply( TMatrix4d& _rResult, + const TMatrix4d& _krA, + const TMatrix4d& _krB); + +const TMatrix4f& Multiply( TMatrix4f& _rResult, + const TMatrix4f& _krA, + const TMatrix4f& _krB); + +const TMatrix4d& ScalarMultiply(TMatrix4d& _rResult, + const TMatrix4d& _krMatrix, + const double _kdScalar); + +const TMatrix4f& ScalarMultiply(TMatrix4f& _rResult, + const TMatrix4f& _krMatrix, + const float _kfScalar); + +const TVector4d& VectorMultiply(TVector4d& _rResult, + const TMatrix4d& _krA, + const TVector4d& _krB); + +const TVector4f& VectorMultiply(TVector4f& _rResult, + const TMatrix4f& _krA, + const TVector4f& _krB); + +const TMatrix4d& Add( TMatrix4d& _rResult, + const TMatrix4d& _krA, + const TMatrix4d& _krB); + +const TMatrix4f& Add( TMatrix4f& _rResult, + const TMatrix4f& _krA, + const TMatrix4f& _krB); + +const TMatrix4d& Transpose( TMatrix4d& _rResult, + const TMatrix4d& _krMatrix); + +const TMatrix4f& Transpose( TMatrix4f& _rResult, + const TMatrix4f& _krMatrix); + +const double GetElement(const TMatrix4d& _krMatrix, + const size_t _kRow, + const size_t _kColumn); + +const float GetElement(const TMatrix4f& _krMatrix, + const size_t _kRow, + const size_t _kColumn); + +TMatrix4d& SetElement( TMatrix4d& _rResult, + const double _kdValue, + const size_t _kRow, + const size_t _kColumn); + +TMatrix4f& SetElement( TMatrix4f& _rResult, + const float _kfValue, + const size_t _kRow, + const size_t _kColumn); + +const TMatrix3d& Submatrix( TMatrix3d& _rResult, + const TMatrix4d& _krMatrix, + const size_t _kDeletedRow, + const size_t _kDeletedColumn); + +const TMatrix3f& Submatrix( TMatrix3f& _rResult, + const TMatrix4f& _krMatrix, + const size_t _kDeletedRow, + const size_t _kDeletedColumn); + +const double FirstMinor(const TMatrix4d& _krMatrix, + const size_t _kRow, + const size_t _kColumn); + +const float FirstMinor( const TMatrix4f& _krMatrix, + const size_t _kRow, + const size_t _kColumn); + +const TMatrix4d& MatrixOfMinors(TMatrix4d& _rResult, + const TMatrix4d& _krMatrix); + +const TMatrix4f& MatrixOfMinors(TMatrix4f& _rResult, + const TMatrix4f& _krMatrix); + +const TMatrix4d& MatrixOfCofactors( TMatrix4d& _rResult, + const TMatrix4d& _krMatrix); + +const TMatrix4f& MatrixOfCofactors( TMatrix4f& _rResult, + const TMatrix4f& _krMatrix); + +const double Determinant(const TMatrix4d& _krMatrix); + +const float Determinant(const TMatrix4f& _krMatrix); + +const TMatrix4d& Inverse( TMatrix4d& _rResult, + const TMatrix4d& _krMatrix); + +const TMatrix4f& Inverse( TMatrix4f& _rResult, + const TMatrix4f& _krMatrix); + +const TMatrix4d& TranslationMatrix( TMatrix4d& _rResult, + const TVector3d& _krVector); + +const TMatrix4f& TranslationMatrix( TMatrix4f& _rResult, + const TVector3f& _krVector); + +const TMatrix4d& ScalingMatrix( TMatrix4d& _rResult, + const double _kdX, + const double _kdY, + const double _kdZ); + +const TMatrix4f& ScalingMatrix( TMatrix4f& _rResult, + const float _kfX, + const float _kfY, + const float _kfZ); + +const TMatrix4d& TransformationMatrix( TMatrix4d& _rResult, + const TVector3d& _krBasisX, + const TVector3d& _krBasisY, + const TVector3d& _krBasisZ, + const TVector3d& _krTranslation); + +const TMatrix4f& TransformationMatrix( TMatrix4f& _rResult, + const TVector3f& _krBasisX, + const TVector3f& _krBasisY, + const TVector3f& _krBasisZ, + const TVector3f& _krTranslation); + +const TMatrix4d& RotationMatrix( TMatrix4d& _rResult, + const TVector4d& _krQuaternion); + +const TMatrix4f& RotationMatrix( TMatrix4f& _rResult, + const TVector4f& _krQuaternion); + +const TMatrix4d& AxisRotationXMatrix(TMatrix4d& _rResult, + const double _kdAngle); + +const TMatrix4f& AxisRotationXMatrix(TMatrix4f& _rResult, + const float _kfAngle); + +const TMatrix4d& AxisRotationYMatrix(TMatrix4d& _rResult, + const double _kdAngle); + +const TMatrix4f& AxisRotationYMatrix(TMatrix4f& _rResult, + const float _kfAngle); + +const TMatrix4d& AxisRotationZMatrix(TMatrix4d& _rResult, + const double _kdAngle); + +const TMatrix4f& AxisRotationZMatrix(TMatrix4f& _rResult, + const float _kfAngle); + +const TMatrix4d& PerspectiveMatrix( TMatrix4d& _rResult, + const double _kdLeft, const double _kdRight, + const double _kdBottom, const double _kdTop, + const double _kdFar, const double _kdNear); + +const TMatrix4f& PerspectiveMatrix( TMatrix4f& _rResult, + const float _kfLeft, const float _kfRight, + const float _kfBottom, const float _kfTop, + const float _kfFar, const float _kfNear); + +const TMatrix4d& PerspectiveMatrix( TMatrix4d& _rResult, + const double _kdFovX, const double _kdFovY, + const double _kdFar, const double _kdNear); + +const TMatrix4f& PerspectiveMatrix( TMatrix4f& _rResult, + const float _kfFovX, const float _kfFovY, + const float _kfFar, const float _kfNear); + +const TMatrix4d& OrthographicMatrix(TMatrix4d& _rResult, + const double _kdLeft, const double _kdRight, + const double _kdBottom, const double _kdTop, + const double _kdFar, const double _kdNear); + +const TMatrix4f& OrthographicMatrix(TMatrix4f& _rResult, + const float _kfLeft, const float _kfRight, + const float _kfBottom, const float _kfTop, + const float _kfFar, const float _kfNear); + +const TMatrix4d& OrthographicMatrix(TMatrix4d& _rResult, + const double _kdWidth, const double _kdHeight, + const double _kdFar, const double _kdNear); + +const TMatrix4f& OrthographicMatrix(TMatrix4f& _rResult, + const float _kfWidth, const float _kfHeight, + const float _kfFar, const float _kfNear); + +// +// Matrix 3 +// + +const bool Equal(const TMatrix3d& _krA, const TMatrix3d& _krB, const double _kdEpsilon); + +const bool Equal(const TMatrix3f& _krA, const TMatrix3f& _krB, const float _kfEpsilon); + +const TMatrix3d& ZeroMatrix(TMatrix3d& _rResult); + +const TMatrix3f& ZeroMatrix(TMatrix3f& _rResult); + +const TMatrix3d& IdentityMatrix(TMatrix3d& _rResult); + +const TMatrix3f& IdentityMatrix(TMatrix3f& _rResult); + +const TMatrix3d& Multiply( TMatrix3d& _rResult, + const TMatrix3d& _krA, + const TMatrix3d& _krB); + +const TMatrix3f& Multiply( TMatrix3f& _rResult, + const TMatrix3f& _krA, + const TMatrix3f& _krB); + +const TMatrix3d& ScalarMultiply(TMatrix3d& _rResult, + const TMatrix3d& _krMatrix, + const double _kdScalar); + +const TMatrix3f& ScalarMultiply(TMatrix3f& _rResult, + const TMatrix3f& _krMatrix, + const float _kfScalar); + +const TVector3d& VectorMultiply(TVector3d& _rResult, + const TMatrix3d& _krA, + const TVector3d& _krB); + +const TVector3f& VectorMultiply(TVector3f& _rResult, + const TMatrix3f& _krA, + const TVector3f& _krB); + +const TMatrix3d& Add( TMatrix3d& _rResult, + const TMatrix3d& _krA, + const TMatrix3d& _krB); + +const TMatrix3f& Add( TMatrix3f& _rResult, + const TMatrix3f& _krA, + const TMatrix3f& _krB); + +const TMatrix3d& Transpose( TMatrix3d& _rResult, + const TMatrix3d& _krMatrix); + +const TMatrix3f& Transpose( TMatrix3f& _rResult, + const TMatrix3f& _krMatrix); + +const double GetElement(const TMatrix3d& _krMatrix, + const size_t _kRow, + const size_t _kColumn); + +const float GetElement(const TMatrix3f& _krMatrix, + const size_t _kRow, + const size_t _kColumn); + +TMatrix3d& SetElement( TMatrix3d& _rResult, + const double _kdValue, + const size_t _kRow, + const size_t _kColumn); + +TMatrix3f& SetElement( TMatrix3f& _rResult, + const float _kfValue, + const size_t _kRow, + const size_t _kColumn); + +const TMatrix2d& Submatrix( TMatrix2d& _rResult, + const TMatrix3d& _krMatrix, + const size_t _kDeletedRow, + const size_t _kDeletedColumn); + +const TMatrix2f& Submatrix( TMatrix2f& _rResult, + const TMatrix3f& _krMatrix, + const size_t _kDeletedRow, + const size_t _kDeletedColumn); + +const double Determinant(const TMatrix3d& _krMatrix); + +const float Determinant(const TMatrix3f& _krMatrix); + +const double FirstMinor(const TMatrix3d& _krMatrix, + const size_t _kRow, + const size_t _kColumn); + +const float FirstMinor( const TMatrix3f& _krMatrix, + const size_t _kRow, + const size_t _kColumn); + +const TMatrix3d& MatrixOfMinors(TMatrix3d& _rResult, + const TMatrix3d& _krMatrix); + +const TMatrix3f& MatrixOfMinors(TMatrix3f& _rResult, + const TMatrix3f& _krMatrix); + +const TMatrix3d& MatrixOfCofactors( TMatrix3d& _rResult, + const TMatrix3d& _krMatrix); + +const TMatrix3f& MatrixOfCofactors( TMatrix3f& _rResult, + const TMatrix3f& _krMatrix); + +const TMatrix3d& Inverse( TMatrix3d& _rResult, + const TMatrix3d& _krMatrix); + +const TMatrix3f& Inverse( TMatrix3f& _rResult, + const TMatrix3f& _krMatrix); + +// +// Matrix 2 +// + +const bool Equal(const TMatrix2d& _krA, const TMatrix2d& _krB, const double _kdEpsilon); + +const bool Equal(const TMatrix2f& _krA, const TMatrix2f& _krB, const float _kfEpsilon); + +const TMatrix2d& ZeroMatrix(TMatrix2d& _rResult); + +const TMatrix2f& ZeroMatrix(TMatrix2f& _rResult); + +const TMatrix2d& IdentityMatrix(TMatrix2d& _rResult); + +const TMatrix2f& IdentityMatrix(TMatrix2f& _rResult); + +const TMatrix2d& Multiply( TMatrix2d& _rResult, + const TMatrix2d& _krA, + const TMatrix2d& _krB); + +const TMatrix2f& Multiply( TMatrix2f& _rResult, + const TMatrix2f& _krA, + const TMatrix2f& _krB); + +const TMatrix2d& ScalarMultiply(TMatrix2d& _rResult, + const TMatrix2d& _krMatrix, + const double _kdScalar); + +const TMatrix2f& ScalarMultiply(TMatrix2f& _rResult, + const TMatrix2f& _krMatrix, + const float _kfScalar); + +const TVector2d& VectorMultiply(TVector2d& _rResult, + const TMatrix2d& _krA, + const TVector2d& _krB); + +const TVector2f& VectorMultiply(TVector2f& _rResult, + const TMatrix2f& _krA, + const TVector2f& _krB); + +const TMatrix2d& Add( TMatrix2d& _rResult, + const TMatrix2d& _krA, + const TMatrix2d& _krB); + +const TMatrix2f& Add( TMatrix2f& _rResult, + const TMatrix2f& _krA, + const TMatrix2f& _krB); + +const TMatrix2d& Transpose( TMatrix2d& _rResult, + const TMatrix2d& _krMatrix); + +const TMatrix2f& Transpose( TMatrix2f& _rResult, + const TMatrix2f& _krMatrix); + +const double GetElement(const TMatrix2d& _krMatrix, + const size_t _kRow, + const size_t _kColumn); + +const float GetElement(const TMatrix2f& _krMatrix, + const size_t _kRow, + const size_t _kColumn); + +TMatrix2d& SetElement( TMatrix2d& _rResult, + const double _kdValue, + const size_t _kRow, + const size_t _kColumn); + +TMatrix2f& SetElement( TMatrix2f& _rResult, + const float _kfValue, + const size_t _kRow, + const size_t _kColumn); + +const TMatrix2d& Inverse( TMatrix2d& _rResult, + const TMatrix2d& _krMatrix); + +const TMatrix2f& Inverse( TMatrix2f& _rResult, + const TMatrix2f& _krMatrix); + +const double Determinant(const TMatrix2d& _krMatrix); + +const float Determinant(const TMatrix2f& _krMatrix); + + + +#endif \ No newline at end of file diff --git a/ReflexToQ3/ckmath/ckmath_quaternion.cpp b/ReflexToQ3/ckmath/ckmath_quaternion.cpp new file mode 100644 index 0000000..66f91f5 --- /dev/null +++ b/ReflexToQ3/ckmath/ckmath_quaternion.cpp @@ -0,0 +1,236 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +// Local Includes +#include "ckmath_quaternion.h" +#include "ckmath_scalar.h" +#include "ckmath_vector.h" + + +// +// Quaternion +// + +const TVector4d& IdentityQuaternion(TVector4d& _rResult) +{ + _rResult.m_dW = 1.0; + + _rResult.m_dX = 0.0; + _rResult.m_dY = 0.0; + _rResult.m_dZ = 0.0; + + return(_rResult); +} + +const TVector4f& IdentityQuaternion(TVector4f& _rResult) +{ + _rResult.m_fW = 1.0f; + + _rResult.m_fX = 0.0f; + _rResult.m_fY = 0.0f; + _rResult.m_fZ = 0.0f; + + return(_rResult); +} + +const TVector4d& ConjugateQuaternion(TVector4d& _rResult, const TVector4d& _krQuaternion) +{ + _rResult.m_dW = _krQuaternion.m_dW; + + _rResult.m_dX = -_krQuaternion.m_dX; + _rResult.m_dY = -_krQuaternion.m_dY; + _rResult.m_dZ = -_krQuaternion.m_dZ; + + return(_rResult); +} + +const TVector4f& ConjugateQuaternion(TVector4f& _rResult, const TVector4f& _krQuaternion) +{ + _rResult.m_fW = _krQuaternion.m_fW; + + _rResult.m_fX = -_krQuaternion.m_fX; + _rResult.m_fY = -_krQuaternion.m_fY; + _rResult.m_fZ = -_krQuaternion.m_fZ; + + return(_rResult); +} + +const TVector4d& InverseQuaternion(TVector4d& _rResult, const TVector4d& _krQuaternion) +{ + _rResult = ScalarMultiply(_rResult, + ConjugateQuaternion(TVector4d(), _krQuaternion), + (1.0 / Square(QuaternionMagnitude(_krQuaternion)))); + + return(_rResult); +} + +const TVector4f& InverseQuaternion(TVector4f& _rResult, const TVector4f& _krQuaternion) +{ + _rResult = ScalarMultiply(_rResult, + ConjugateQuaternion(TVector4f(), _krQuaternion), + (1.0f / Square(QuaternionMagnitude(_krQuaternion)))); + + return(_rResult); +} + +const TVector4d& UnitQuaternion(TVector4d& _rResult, const TVector4d& _krQuaternion) +{ + _rResult = ScalarMultiply( _rResult, + _krQuaternion, + (1.0 / QuaternionMagnitude(_krQuaternion))); + + return(_rResult); +} + +const TVector4f& UnitQuaternion(TVector4f& _rResult, const TVector4f& _krQuaternion) +{ + _rResult = ScalarMultiply( _rResult, + _krQuaternion, + (1.0f / QuaternionMagnitude(_krQuaternion))); + + return(_rResult); +} + +const TVector4d& AxisAngleQuaternion(TVector4d& _rResult, const TVector3d& _krAxis, const double _kdAngle) +{ + _rResult.m_dW = Cosine(_kdAngle / 2.0); + + _rResult.m_dX = _krAxis.m_dX * Sine(_kdAngle / 2.0); + _rResult.m_dY = _krAxis.m_dY * Sine(_kdAngle / 2.0); + _rResult.m_dZ = _krAxis.m_dZ * Sine(_kdAngle / 2.0); + + return(_rResult); +} + +const TVector4f& AxisAngleQuaternion(TVector4f& _rResult, const TVector3f& _krAxis, const float _kfAngle) +{ + _rResult.m_fW = Cosine(_kfAngle / 2.0f); + + _rResult.m_fX = _krAxis.m_fX * Sine(_kfAngle / 2.0f); + _rResult.m_fY = _krAxis.m_fY * Sine(_kfAngle / 2.0f); + _rResult.m_fZ = _krAxis.m_fZ * Sine(_kfAngle / 2.0f); + + return(_rResult); +} + +const double QuaternionMagnitude(const TVector4d& _krQuaternion) +{ + return(SquareRoot(QuaternionProduct(TVector4d(), + ConjugateQuaternion( TVector4d(), + _krQuaternion), + _krQuaternion).m_dW)); +} + +const float QuaternionMagnitude(const TVector4f& _krQuaternion) +{ + return(SquareRoot(QuaternionProduct(TVector4f(), + ConjugateQuaternion( TVector4f(), + _krQuaternion), + _krQuaternion).m_fW)); +} + +const TVector4d& QuaternionProduct(TVector4d& _rResult, const TVector4d& _krA, const TVector4d& _krB) +{ + _rResult.m_dW = (_krA.m_dW * _krB.m_dW) - (_krA.m_dX * _krB.m_dX) - (_krA.m_dY * _krB.m_dY) - (_krA.m_dZ * _krB.m_dZ); + _rResult.m_dX = (_krA.m_dW * _krB.m_dX) + (_krA.m_dX * _krB.m_dW) + (_krA.m_dY * _krB.m_dZ) - (_krA.m_dZ * _krB.m_dY); + _rResult.m_dY = (_krA.m_dW * _krB.m_dY) - (_krA.m_dX * _krB.m_dZ) + (_krA.m_dY * _krB.m_dW) + (_krA.m_dZ * _krB.m_dX); + _rResult.m_dZ = (_krA.m_dW * _krB.m_dZ) + (_krA.m_dX * _krB.m_dY) - (_krA.m_dY * _krB.m_dX) + (_krA.m_dZ * _krB.m_dW); + + return(_rResult); +} + +const TVector4f& QuaternionProduct(TVector4f& _rResult, const TVector4f& _krA, const TVector4f& _krB) +{ + _rResult.m_fW = (_krA.m_fW * _krB.m_fW) - (_krA.m_fX * _krB.m_fX) - (_krA.m_fY * _krB.m_fY) - (_krA.m_fZ * _krB.m_fZ); + _rResult.m_fX = (_krA.m_fW * _krB.m_fX) + (_krA.m_fX * _krB.m_fW) + (_krA.m_fY * _krB.m_fZ) - (_krA.m_fZ * _krB.m_fY); + _rResult.m_fY = (_krA.m_fW * _krB.m_fY) - (_krA.m_fX * _krB.m_fZ) + (_krA.m_fY * _krB.m_fW) + (_krA.m_fZ * _krB.m_fX); + _rResult.m_fZ = (_krA.m_fW * _krB.m_fZ) + (_krA.m_fX * _krB.m_fY) - (_krA.m_fY * _krB.m_fX) + (_krA.m_fZ * _krB.m_fW); + + return(_rResult); +} + +const TVector3d& QuaternionRotate(TVector3d& _rResult, const TVector3d& _krVector, const TVector4d& _krQuaternion) +{ + const TVector4d kVecAsQuat{_krVector.m_dX, _krVector.m_dY, _krVector.m_dZ, 0.0}; + + const TVector4d kResultAsQuat = QuaternionProduct(TVector4d(), + QuaternionProduct(TVector4d(), _krQuaternion, kVecAsQuat), + ConjugateQuaternion(TVector4d(), _krQuaternion)); + + _rResult.m_dX = kResultAsQuat.m_dX; + _rResult.m_dY = kResultAsQuat.m_dY; + _rResult.m_dZ = kResultAsQuat.m_dZ; + + return(_rResult); +} + +const TVector3f& QuaternionRotate(TVector3f& _rResult, const TVector3f& _krVector, const TVector4f& _krQuaternion) +{ + const TVector4f kVecAsQuat{_krVector.m_fX, _krVector.m_fY, _krVector.m_fZ, 0.0f}; + + const TVector4f kResultAsQuat = QuaternionProduct(TVector4f(), + QuaternionProduct(TVector4f(), _krQuaternion, kVecAsQuat), + ConjugateQuaternion(TVector4f(), _krQuaternion)); + + _rResult.m_fX = kResultAsQuat.m_fX; + _rResult.m_fY = kResultAsQuat.m_fY; + _rResult.m_fZ = kResultAsQuat.m_fZ; + + return(_rResult); +} + +const TVector4d& Slerp(TVector4d& _rResult, const TVector4d& _krA, const TVector4d& _krB, const double _kdT) +{ + const double kdCosOmega = DotProduct(_krA, _krB); + + double dK0, dK1; + if(Magnitude(kdCosOmega) == 1.0) // Avoid divide by zero using lerp + { + dK0 = 1.0 - _kdT; + dK1 = _kdT; + } + else + { + const double kdSinOmega = SquareRoot(1.0 - Square(kdCosOmega)); + const double kdOmega = ArcTan2(kdSinOmega, kdCosOmega); + + dK0 = Sine((1.0 - _kdT) * kdOmega) * (1.0 / kdSinOmega); + dK1 = Sine(_kdT * kdOmega) * (1.0 / kdSinOmega); + } + + _rResult.m_dW = (_krA.m_dW * dK0) + (_krB.m_dW * dK1); + _rResult.m_dX = (_krA.m_dX * dK0) + (_krB.m_dX * dK1); + _rResult.m_dY = (_krA.m_dY * dK0) + (_krB.m_dY * dK1); + _rResult.m_dZ = (_krA.m_dZ * dK0) + (_krB.m_dZ * dK1); + + return(_rResult); +} + +const TVector4f& Slerp(TVector4f& _rResult, const TVector4f& _krA, const TVector4f& _krB, const float _kfT) +{ + const float kfCosOmega = DotProduct(_krA, _krB); + + float fK0, fK1; + if(Magnitude(kfCosOmega) == 1.0f) // Avoid divide by zero using lerp + { + fK0 = 1.0f - _kfT; + fK1 = _kfT; + } + else + { + const float kfSinOmega = SquareRoot(1.0f - Square(kfCosOmega)); + const float kfOmega = ArcTan2(kfSinOmega, kfCosOmega); + + fK0 = Sine((1.0f - _kfT) * kfOmega) * (1.0f / kfSinOmega); + fK1 = Sine(_kfT * kfOmega) * (1.0f / kfSinOmega); + } + + _rResult.m_fW = (_krA.m_fW * fK0) + (_krB.m_fW * fK1); + _rResult.m_fX = (_krA.m_fX * fK0) + (_krB.m_fX * fK1); + _rResult.m_fY = (_krA.m_fY * fK0) + (_krB.m_fY * fK1); + _rResult.m_fZ = (_krA.m_fZ * fK0) + (_krB.m_fZ * fK1); + + return(_rResult); +} \ No newline at end of file diff --git a/ReflexToQ3/ckmath/ckmath_quaternion.h b/ReflexToQ3/ckmath/ckmath_quaternion.h new file mode 100644 index 0000000..f364011 --- /dev/null +++ b/ReflexToQ3/ckmath/ckmath_quaternion.h @@ -0,0 +1,53 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +#pragma once + +#ifndef __MATH_QUATERNION_H__ +#define __MATH_QUATERNION_H__ + +// Local Includes +#include "ckmath_types.h" + +// Quaternion Function Prototypes + +const TVector4d& IdentityQuaternion(TVector4d& _rResult); + +const TVector4f& IdentityQuaternion(TVector4f& _rResult); + +const TVector4d& ConjugateQuaternion(TVector4d& _rResult, const TVector4d& _krQuaternion); + +const TVector4f& ConjugateQuaternion(TVector4f& _rResult, const TVector4f& _krQuaternion); + +const TVector4d& InverseQuaternion(TVector4d& _rResult, const TVector4d& _krQuaternion); + +const TVector4f& InverseQuaternion(TVector4f& _rResult, const TVector4f& _krQuaternion); + +const TVector4d& UnitQuaternion(TVector4d& _rResult, const TVector4d& _krQuaternion); + +const TVector4f& UnitQuaternion(TVector4f& _rResult, const TVector4f& _krQuaternion); + +const TVector4d& AxisAngleQuaternion(TVector4d& _rResult, const TVector3d& _krAxis, const double _kdAngle); + +const TVector4f& AxisAngleQuaternion(TVector4f& _rResult, const TVector3f& _krAxis, const float _kfAngle); + +const double QuaternionMagnitude(const TVector4d& _krQuaternion); + +const float QuaternionMagnitude(const TVector4f& _krQuaternion); + +const TVector4d& QuaternionProduct(TVector4d& _rResult, const TVector4d& _krA, const TVector4d& _krB); + +const TVector4f& QuaternionProduct(TVector4f& _rResult, const TVector4f& _krA, const TVector4f& _krB); + +const TVector3d& QuaternionRotate(TVector3d& _rResult, const TVector3d& _krVector, const TVector4d& _krQuaternion); + +const TVector3f& QuaternionRotate(TVector3f& _rResult, const TVector3f& _krVector, const TVector4f& _krQuaternion); + +const TVector4d& Slerp(TVector4d& _rResult, const TVector4d& _krA, const TVector4d& _krB, const double _kdT); + +const TVector4f& Slerp(TVector4f& _rResult, const TVector4f& _krA, const TVector4f& _krB, const float _kfT); + + +#endif \ No newline at end of file diff --git a/ReflexToQ3/ckmath/ckmath_scalar.h b/ReflexToQ3/ckmath/ckmath_scalar.h new file mode 100644 index 0000000..0c8d2da --- /dev/null +++ b/ReflexToQ3/ckmath/ckmath_scalar.h @@ -0,0 +1,109 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +#pragma once + +#ifndef __MATH_SCALAR_H__ +#define __MATH_SCALAR_H__ + +// Library Includes +#include + +// Scalar Function Prototypes + +// double +inline const double Square(const double _kdScalar) +{ + return(_kdScalar * _kdScalar); +} + +inline const double Magnitude(const double _kdScalar) +{ + return(abs(_kdScalar)); +} + +inline const double SquareRoot(const double _kdScalar) +{ + return(sqrt(_kdScalar)); +} + +inline const double ArcCos(const double _kdScalar) +{ + return(acos(_kdScalar)); +} + +inline const double ArcTan(const double _kdScalar) +{ + return(atan(_kdScalar)); +} + +inline const double ArcTan2(const double _kdX, const double _kdY) +{ + return(atan2(_kdX, _kdY)); +} + +inline const double Sine(const double _kdScalar) +{ + return(sin(_kdScalar)); +} + +inline const double Cosine(const double _kdScalar) +{ + return(cos(_kdScalar)); +} + +inline const bool Equal(const double _kdA, const double _kdB, const double _kdEpsilon) +{ + return((Magnitude(_kdA - _kdB) < _kdEpsilon)); +} + +// float +inline const float Square(const float _kfScalar) +{ + return(_kfScalar * _kfScalar); +} + +inline const float Magnitude(const float _kfScalar) +{ + return(fabsf(_kfScalar)); +} + +inline const float SquareRoot(const float _kfScalar) +{ + return(sqrtf(_kfScalar)); +} + +inline const float ArcCos(const float _kfScalar) +{ + return(acosf(_kfScalar)); +} + +inline const float ArcTan(const float _kfScalar) +{ + return(atanf(_kfScalar)); +} + +inline const float ArcTan2(const float _kfX, const float _kfY) +{ + return(atan2f(_kfX, _kfY)); +} + +inline const float Sine(const float _kfScalar) +{ + return(sinf(_kfScalar)); +} + +inline const float Cosine(const float _kfScalar) +{ + return(cosf(_kfScalar)); +} + +inline const bool Equal(const float _kfA, const float _kfB, const float _kfEpsilon) +{ + return((Magnitude(_kfA - _kfB) < _kfEpsilon)); +} + + +#endif \ No newline at end of file diff --git a/ReflexToQ3/ckmath/ckmath_types.h b/ReflexToQ3/ckmath/ckmath_types.h new file mode 100644 index 0000000..ef129a6 --- /dev/null +++ b/ReflexToQ3/ckmath/ckmath_types.h @@ -0,0 +1,306 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +#pragma once + +#ifndef __MATH_TYPES_H__ +#define __MATH_TYPES_H__ + +// +// +// Struct Prototypes +// +// + + +// +// Vector +// +struct TVector4d +{ + double m_dX; + double m_dY; + double m_dZ; + double m_dW; +}; + +struct TVector4f +{ + float m_fX; + float m_fY; + float m_fZ; + float m_fW; +}; + +struct TVector3d +{ + double m_dX; + double m_dY; + double m_dZ; +}; + +struct TVector3f +{ + float m_fX; + float m_fY; + float m_fZ; +}; + +struct TVector2d +{ + double m_dX; + double m_dY; +}; + +struct TVector2f +{ + float m_fX; + float m_fY; +}; + +// +// Matrix +// +struct TMatrix4d +{ + double m_d11, m_d12, m_d13, m_d14; + double m_d21, m_d22, m_d23, m_d24; + double m_d31, m_d32, m_d33, m_d34; + double m_d41, m_d42, m_d43, m_d44; +}; + +struct TMatrix4f +{ + float m_f11, m_f12, m_f13, m_f14; + float m_f21, m_f22, m_f23, m_f24; + float m_f31, m_f32, m_f33, m_f34; + float m_f41, m_f42, m_f43, m_f44; +}; + +struct TMatrix3d +{ + double m_d11, m_d12, m_d13; + double m_d21, m_d22, m_d23; + double m_d31, m_d32, m_d33; +}; + +struct TMatrix3f +{ + float m_f11, m_f12, m_f13; + float m_f21, m_f22, m_f23; + float m_f31, m_f32, m_f33; +}; + +struct TMatrix2d +{ + double m_d11, m_d12; + double m_d21, m_d22; +}; + +struct TMatrix2f +{ + float m_f11, m_f12; + float m_f21, m_f22; +}; + +// +// Plane +// +struct TPlane3d +{ + TVector3d m_Point; + TVector3d m_Normal; +}; + +struct TPlane3f +{ + TVector3f m_Point; + TVector3f m_Normal; + +}; + +struct TPlane2d +{ + TVector2d m_Point; + TVector2d m_Normal; +}; + +struct TPlane2f +{ + TVector2f m_Point; + TVector2f m_Normal; +}; + +// +// Triangle +// +struct TTriangle3d +{ + TVector3d m_A; + TVector3d m_B; + TVector3d m_C; +}; + +struct TTriangle3f +{ + TVector3f m_A; + TVector3f m_B; + TVector3f m_C; +}; + +struct TTriangle2d +{ + TVector2d m_A; + TVector2d m_B; + TVector2d m_C; +}; + +struct TTriangle2f +{ + TVector2f m_A; + TVector2f m_B; + TVector2f m_C; +}; + +// +// Line +// +struct TLine3d +{ + TVector3d m_A; + TVector3d m_B; +}; + +struct TLine3f +{ + TVector3f m_A; + TVector3f m_B; +}; + +struct TLine2d +{ + TVector2d m_A; + TVector2d m_B; +}; + +struct TLine2f +{ + TVector2f m_A; + TVector2f m_B; +}; + +// +// Ray +// +struct TRay3d +{ + TVector3d m_Start; + TVector3d m_Direction; +}; + +struct TRay3f +{ + TVector3f m_Start; + TVector3f m_Direction; +}; + +struct TRay2d +{ + TVector2d m_Start; + TVector2d m_Direction; +}; + +struct TRay2f +{ + TVector2f m_Start; + TVector2f m_Direction; +}; + +// +// Sphere +// +struct TSphere3d +{ + TVector3d m_Center; + double m_dRadius; +}; + +struct TSphere3f +{ + TVector3f m_Center; + float m_fRadius; +}; + +// +// Circle +// +struct TCircle2d +{ + TVector2d m_Center; + double m_dRadius; +}; + +struct TCircle2f +{ + TVector2f m_Center; + float m_fRadius; +}; + +// +// Capsule +// +struct TCapsule3d +{ + TLine3d m_Line; + double m_dRadius; +}; + +struct TCapsule3f +{ + TLine3f m_Line; + float m_fRadius; +}; + +struct TCapsule2d +{ + TLine2d m_Line; + double m_dRadius; +}; + +struct TCapsule2f +{ + TLine2f m_Line; + float m_fRadius; +}; + +// +// AABB +// +struct TAABB3d +{ + TVector3d m_MinPoint; + TVector3d m_MaxPoint; +}; + +struct TAABB3f +{ + TVector3f m_MinPoint; + TVector3f m_MaxPoint; +}; + +struct TAABB2d +{ + TVector2d m_MinPoint; + TVector2d m_MaxPoint; +}; + +struct TAABB2f +{ + TVector2f m_MinPoint; + TVector2f m_MaxPoint; +}; + + +#endif \ No newline at end of file diff --git a/ReflexToQ3/ckmath/ckmath_vector.cpp b/ReflexToQ3/ckmath/ckmath_vector.cpp new file mode 100644 index 0000000..41c6bba --- /dev/null +++ b/ReflexToQ3/ckmath/ckmath_vector.cpp @@ -0,0 +1,708 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + + +// Local Includes +#include "ckmath_vector.h" +#include "ckmath_scalar.h" + + +// +// Vector 4 +// + +const TVector4d& ZeroVector(TVector4d& _rResult) +{ + _rResult.m_dX = 0.0; + _rResult.m_dY = 0.0; + _rResult.m_dZ = 0.0; + _rResult.m_dW = 0.0; + + return(_rResult); +} + +const TVector4f& ZeroVector(TVector4f& _rResult) +{ + _rResult.m_fX = 0.0f; + _rResult.m_fY = 0.0f; + _rResult.m_fZ = 0.0f; + _rResult.m_fW = 0.0f; + + return(_rResult); +} + +const bool Equal( const TVector4d& _krA, + const TVector4d& _krB, + const double _kdEpsilon) +{ + const bool kbEqual = (Magnitude(_krA.m_dX - _krB.m_dX) < _kdEpsilon) + && (Magnitude(_krA.m_dY - _krB.m_dY) < _kdEpsilon) + && (Magnitude(_krA.m_dZ - _krB.m_dZ) < _kdEpsilon) + && (Magnitude(_krA.m_dW - _krB.m_dW) < _kdEpsilon); + + return(kbEqual); +} + +const bool Equal( const TVector4f& _krA, + const TVector4f& _krB, + const float _kfEpsilon) +{ + const bool kbEqual = (Magnitude(_krA.m_fX - _krB.m_fX) < _kfEpsilon) + && (Magnitude(_krA.m_fY - _krB.m_fY) < _kfEpsilon) + && (Magnitude(_krA.m_fZ - _krB.m_fZ) < _kfEpsilon) + && (Magnitude(_krA.m_fW - _krB.m_fW) < _kfEpsilon); + + return(kbEqual); +} + +const TVector4d& Add( TVector4d& _rResult, + const TVector4d& _krA, + const TVector4d& _krB) +{ + _rResult.m_dX = _krA.m_dX + _krB.m_dX; + _rResult.m_dY = _krA.m_dY + _krB.m_dY; + _rResult.m_dZ = _krA.m_dZ + _krB.m_dZ; + _rResult.m_dW = _krA.m_dW + _krB.m_dW; + + return(_rResult); +} + +const TVector4f& Add( TVector4f& _rResult, + const TVector4f& _krA, + const TVector4f& _krB) +{ + _rResult.m_fX = _krA.m_fX + _krB.m_fX; + _rResult.m_fY = _krA.m_fY + _krB.m_fY; + _rResult.m_fZ = _krA.m_fZ + _krB.m_fZ; + _rResult.m_fW = _krA.m_fW + _krB.m_fW; + + return(_rResult); +} + +const TVector4d& Subtract(TVector4d& _rResult, + const TVector4d& _krA, + const TVector4d& _krB) +{ + _rResult.m_dX = _krA.m_dX - _krB.m_dX; + _rResult.m_dY = _krA.m_dY - _krB.m_dY; + _rResult.m_dZ = _krA.m_dZ - _krB.m_dZ; + _rResult.m_dW = _krA.m_dW - _krB.m_dW; + + return(_rResult); +} + +const TVector4f& Subtract(TVector4f& _rResult, + const TVector4f& _krA, + const TVector4f& _krB) +{ + _rResult.m_fX = _krA.m_fX - _krB.m_fX; + _rResult.m_fY = _krA.m_fY - _krB.m_fY; + _rResult.m_fZ = _krA.m_fZ - _krB.m_fZ; + _rResult.m_fW = _krA.m_fW - _krB.m_fW; + + return(_rResult); +} + +const TVector4d& ScalarMultiply( TVector4d& _rResult, + const TVector4d& _krV, + const double _kdS) +{ + _rResult.m_dX = _krV.m_dX * _kdS; + _rResult.m_dY = _krV.m_dY * _kdS; + _rResult.m_dZ = _krV.m_dZ * _kdS; + _rResult.m_dW = _krV.m_dW * _kdS; + + return(_rResult); +} + +const TVector4f& ScalarMultiply( TVector4f& _rResult, + const TVector4f& _krV, + const float _kfS) +{ + _rResult.m_fX = _krV.m_fX * _kfS; + _rResult.m_fY = _krV.m_fY * _kfS; + _rResult.m_fZ = _krV.m_fZ * _kfS; + _rResult.m_fW = _krV.m_fW * _kfS; + + return(_rResult); +} + +const double VectorMagnitude(const TVector4d& _krV) +{ + return(SquareRoot( Square(_krV.m_dX) + + Square(_krV.m_dY) + + Square(_krV.m_dZ) + + Square(_krV.m_dW))); +} + +const float VectorMagnitude(const TVector4f& _krV) +{ + return(SquareRoot( Square(_krV.m_fX) + + Square(_krV.m_fY) + + Square(_krV.m_fZ) + + Square(_krV.m_fW))); +} + +const double DotProduct( const TVector4d& _krA, + const TVector4d& _krB) +{ + const double kdResult = ( (_krA.m_dX * _krB.m_dX) + + (_krA.m_dY * _krB.m_dY) + + (_krA.m_dZ * _krB.m_dZ) + + (_krA.m_dW * _krB.m_dW)); + + return(kdResult); +} + +const float DotProduct( const TVector4f& _krA, + const TVector4f& _krB) +{ + const float kfResult = ( (_krA.m_fX * _krB.m_fX) + + (_krA.m_fY * _krB.m_fY) + + (_krA.m_fZ * _krB.m_fZ) + + (_krA.m_fW * _krB.m_fW)); + + return(kfResult); +} + +const TVector4d& Normalize( TVector4d& _rResult, + const TVector4d& _krV) +{ + ScalarMultiply(_rResult, _krV, (1.0 / VectorMagnitude(_krV)) ); + return(_rResult); +} + +const TVector4f& Normalize( TVector4f& _rResult, + const TVector4f& _krV) +{ + ScalarMultiply(_rResult, _krV, (1.0f / VectorMagnitude(_krV)) ); + return(_rResult); +} + +const TVector4d& Projection( TVector4d& _rResult, + const TVector4d& _krA, + const TVector4d& _krB) +{ + const double kdDenom = Square(VectorMagnitude(_krB)); + + const TVector4d kNumer = ScalarMultiply(TVector4d(), _krB, DotProduct(_krA, _krB)); + + _rResult = ScalarMultiply(_rResult, kNumer, kdDenom); + + return(_rResult); +} + +const TVector4f& Projection( TVector4f& _rResult, + const TVector4f& _krA, + const TVector4f& _krB) +{ + const float kfDenom = Square(VectorMagnitude(_krB)); + + const TVector4f kNumer = ScalarMultiply(TVector4f(), _krB, DotProduct(_krA, _krB)); + + _rResult = ScalarMultiply(_rResult, kNumer, kfDenom); + + return(_rResult); +} + +const double AngleBetween(const TVector4d& _krA, + const TVector4d& _krB) +{ + const double kdAngle = ArcCos( DotProduct(_krA, _krB) + / ( VectorMagnitude(_krA) * VectorMagnitude(_krB) ) ); + + return(kdAngle); +} + +const float AngleBetween( const TVector4f& _krA, + const TVector4f& _krB) +{ + const float kfAngle = ArcCos( DotProduct(_krA, _krB) + / ( VectorMagnitude(_krA) * VectorMagnitude(_krB) ) ); + + return(kfAngle); +} + +const double Distance(const TVector4d& _krA, + const TVector4d& _krB) +{ + const double kdDistance = VectorMagnitude(Subtract(TVector4d(), _krA, _krB)); + + return(kdDistance); +} + +const float Distance( const TVector4f& _krA, + const TVector4f& _krB) +{ + const float kfDistance = VectorMagnitude(Subtract(TVector4f(), _krA, _krB)); + + return(kfDistance); +} + +// +// Vector 3 +// + +const TVector3d& ZeroVector(TVector3d& _rResult) +{ + _rResult.m_dX = 0.0; + _rResult.m_dY = 0.0; + _rResult.m_dZ = 0.0; + + return(_rResult); +} + +const TVector3f& ZeroVector(TVector3f& _rResult) +{ + _rResult.m_fX = 0.0f; + _rResult.m_fY = 0.0f; + _rResult.m_fZ = 0.0f; + + return(_rResult); +} + +const bool Equal( const TVector3d& _krA, + const TVector3d& _krB, + const double _kdEpsilon) +{ + const bool kbEqual = (Magnitude(_krA.m_dX - _krB.m_dX) < _kdEpsilon) + && (Magnitude(_krA.m_dY - _krB.m_dY) < _kdEpsilon) + && (Magnitude(_krA.m_dZ - _krB.m_dZ) < _kdEpsilon); + + return(kbEqual); +} + +const bool Equal( const TVector3f& _krA, + const TVector3f& _krB, + const float _kfEpsilon) +{ + const bool kbEqual = (Magnitude(_krA.m_fX - _krB.m_fX) < _kfEpsilon) + && (Magnitude(_krA.m_fY - _krB.m_fY) < _kfEpsilon) + && (Magnitude(_krA.m_fZ - _krB.m_fZ) < _kfEpsilon); + + return(kbEqual); +} + +const TVector3d& Add( TVector3d& _rResult, + const TVector3d& _krA, + const TVector3d& _krB) +{ + _rResult.m_dX = _krA.m_dX + _krB.m_dX; + _rResult.m_dY = _krA.m_dY + _krB.m_dY; + _rResult.m_dZ = _krA.m_dZ + _krB.m_dZ; + + return(_rResult); +} + +const TVector3f& Add( TVector3f& _rResult, + const TVector3f& _krA, + const TVector3f& _krB) +{ + _rResult.m_fX = _krA.m_fX + _krB.m_fX; + _rResult.m_fY = _krA.m_fY + _krB.m_fY; + _rResult.m_fZ = _krA.m_fZ + _krB.m_fZ; + + return(_rResult); +} + +const TVector3d& Subtract(TVector3d& _rResult, + const TVector3d& _krA, + const TVector3d& _krB) +{ + _rResult.m_dX = _krA.m_dX - _krB.m_dX; + _rResult.m_dY = _krA.m_dY - _krB.m_dY; + _rResult.m_dZ = _krA.m_dZ - _krB.m_dZ; + + return(_rResult); +} + +const TVector3f& Subtract(TVector3f& _rResult, + const TVector3f& _krA, + const TVector3f& _krB) +{ + _rResult.m_fX = _krA.m_fX - _krB.m_fX; + _rResult.m_fY = _krA.m_fY - _krB.m_fY; + _rResult.m_fZ = _krA.m_fZ - _krB.m_fZ; + + return(_rResult); +} + +const TVector3d& ScalarMultiply( TVector3d& _rResult, + const TVector3d& _krV, + const double _kdS) +{ + _rResult.m_dX = _krV.m_dX * _kdS; + _rResult.m_dY = _krV.m_dY * _kdS; + _rResult.m_dZ = _krV.m_dZ * _kdS; + + return(_rResult); +} + +const TVector3f& ScalarMultiply( TVector3f& _rResult, + const TVector3f& _krV, + const float _kfS) +{ + _rResult.m_fX = _krV.m_fX * _kfS; + _rResult.m_fY = _krV.m_fY * _kfS; + _rResult.m_fZ = _krV.m_fZ * _kfS; + + return(_rResult); +} + +const double VectorMagnitude(const TVector3d& _krV) +{ + return(SquareRoot( Square(_krV.m_dX) + + Square(_krV.m_dY) + + Square(_krV.m_dZ))); +} + +const float VectorMagnitude(const TVector3f& _krV) +{ + return(SquareRoot( Square(_krV.m_fX) + + Square(_krV.m_fY) + + Square(_krV.m_fZ))); +} + +const double DotProduct( const TVector3d& _krA, + const TVector3d& _krB) +{ + return( (_krA.m_dX * _krB.m_dX) + + (_krA.m_dY * _krB.m_dY) + + (_krA.m_dZ * _krB.m_dZ)); +} + +const float DotProduct( const TVector3f& _krA, + const TVector3f& _krB) +{ + return( (_krA.m_fX * _krB.m_fX) + + (_krA.m_fY * _krB.m_fY) + + (_krA.m_fZ * _krB.m_fZ)); +} + +const TVector3d& CrossProduct(TVector3d& _rResult, + const TVector3d& _krA, + const TVector3d& _krB) +{ + _rResult.m_dX = (_krA.m_dY * _krB.m_dZ) - ( _krA.m_dZ * _krB.m_dY); + _rResult.m_dY = (_krA.m_dZ * _krB.m_dX) - ( _krA.m_dX * _krB.m_dZ); + _rResult.m_dZ = (_krA.m_dX * _krB.m_dY) - ( _krA.m_dY * _krB.m_dX); + + return(_rResult); +} + +const TVector3f& CrossProduct(TVector3f& _rResult, + const TVector3f& _krA, + const TVector3f& _krB) +{ + _rResult.m_fX = (_krA.m_fY * _krB.m_fZ) - ( _krA.m_fZ * _krB.m_fY); + _rResult.m_fY = (_krA.m_fZ * _krB.m_fX) - ( _krA.m_fX * _krB.m_fZ); + _rResult.m_fZ = (_krA.m_fX * _krB.m_fY) - ( _krA.m_fY * _krB.m_fX); + + return(_rResult); +} + +const TVector3d& Normalize( TVector3d& _rResult, + const TVector3d& _krV) +{ + ScalarMultiply(_rResult, _krV, (1.0 / VectorMagnitude(_krV)) ); + return(_rResult); +} + +const TVector3f& Normalize( TVector3f& _rResult, + const TVector3f& _krV) +{ + ScalarMultiply(_rResult, _krV, (1.0f / VectorMagnitude(_krV)) ); + return(_rResult); +} + +const TVector3d& Projection( TVector3d& _rResult, + const TVector3d& _krA, + const TVector3d& _krB) +{ + const double kdDenom = Square(VectorMagnitude(_krB)); + + const TVector3d kNumer = ScalarMultiply(TVector3d(), _krB, DotProduct(_krA, _krB)); + + _rResult = ScalarMultiply(_rResult, kNumer, kdDenom); + + return(_rResult); +} + +const TVector3f& Projection( TVector3f& _rResult, + const TVector3f& _krA, + const TVector3f& _krB) +{ + const float kfDenom = Square(VectorMagnitude(_krB)); + + const TVector3f kNumer = ScalarMultiply(TVector3f(), _krB, DotProduct(_krA, _krB)); + + _rResult = ScalarMultiply(_rResult, kNumer, kfDenom); + + return(_rResult); +} + +const double AngleBetween(const TVector3d& _krA, + const TVector3d& _krB) +{ + const double kdAngle = ArcCos( DotProduct(_krA, _krB) + / ( VectorMagnitude(_krA) * VectorMagnitude(_krB) ) ); + + return(kdAngle); +} + +const float AngleBetween( const TVector3f& _krA, + const TVector3f& _krB) +{ + const float kfAngle = ArcCos( DotProduct(_krA, _krB) + / ( VectorMagnitude(_krA) * VectorMagnitude(_krB) ) ); + + return(kfAngle); +} + +const double Distance(const TVector3d& _krA, + const TVector3d& _krB) +{ + const double kdDistance = VectorMagnitude(Subtract(TVector3d(), _krA, _krB)); + + return(kdDistance); +} + +const float Distance( const TVector3f& _krA, + const TVector3f& _krB) +{ + const float kfDistance = VectorMagnitude(Subtract(TVector3f(), _krA, _krB)); + + return(kfDistance); +} + +const double ScalarTripleProduct( const TVector3d& _krA, + const TVector3d& _krB, + const TVector3d& _krC) +{ + return(DotProduct(_krA, CrossProduct(TVector3d(), _krB, _krC))); +} + +const float ScalarTripleProduct( const TVector3f& _krA, + const TVector3f& _krB, + const TVector3f& _krC) +{ + return(DotProduct(_krA, CrossProduct(TVector3f(), _krB, _krC))); +} + +const TVector3d& VectorTripleProduct( TVector3d& _rResult, + const TVector3d& _krA, + const TVector3d& _krB, + const TVector3d& _krC) +{ + return(CrossProduct(_rResult, _krA, CrossProduct(TVector3d(), _krB, _krC))); +} + +const TVector3f& VectorTripleProduct( TVector3f& _rResult, + const TVector3f& _krA, + const TVector3f& _krB, + const TVector3f& _krC) +{ + return(CrossProduct(_rResult, _krA, CrossProduct(TVector3f(), _krB, _krC))); +} + +// +// Vector 2 +// + +const TVector2d& ZeroVector(TVector2d& _rResult) +{ + _rResult.m_dX = 0.0; + _rResult.m_dY = 0.0; + + return(_rResult); +} + +const TVector2f& ZeroVector(TVector2f& _rResult) +{ + _rResult.m_fX = 0.0f; + _rResult.m_fY = 0.0f; + + return(_rResult); +} + +const bool Equal( const TVector2d& _krA, + const TVector2d& _krB, + const double _kdEpsilon) +{ + const bool kbEqual = (Magnitude(_krA.m_dX - _krB.m_dX) < _kdEpsilon) + && (Magnitude(_krA.m_dY - _krB.m_dY) < _kdEpsilon); + + return(kbEqual); +} + +const bool Equal( const TVector2f& _krA, + const TVector2f& _krB, + const float _kfEpsilon) +{ + const bool kbEqual = (Magnitude(_krA.m_fX - _krB.m_fX) < _kfEpsilon) + && (Magnitude(_krA.m_fY - _krB.m_fY) < _kfEpsilon); + + return(kbEqual); +} + +const TVector2d& Add( TVector2d& _rResult, + const TVector2d& _krA, + const TVector2d& _krB) +{ + _rResult.m_dX = _krA.m_dX + _krB.m_dX; + _rResult.m_dY = _krA.m_dY + _krB.m_dY; + + return(_rResult); +} + +const TVector2f& Add( TVector2f& _rResult, + const TVector2f& _krA, + const TVector2f& _krB) +{ + _rResult.m_fX = _krA.m_fX + _krB.m_fX; + _rResult.m_fY = _krA.m_fY + _krB.m_fY; + + return(_rResult); +} + +const TVector2d& Subtract(TVector2d& _rResult, + const TVector2d& _krA, + const TVector2d& _krB) +{ + _rResult.m_dX = _krA.m_dX - _krB.m_dX; + _rResult.m_dY = _krA.m_dY - _krB.m_dY; + + return(_rResult); +} + +const TVector2f& Subtract(TVector2f& _rResult, + const TVector2f& _krA, + const TVector2f& _krB) +{ + _rResult.m_fX = _krA.m_fX - _krB.m_fX; + _rResult.m_fY = _krA.m_fY - _krB.m_fY; + + return(_rResult); +} + +const TVector2d& ScalarMultiply( TVector2d& _rResult, + const TVector2d& _krV, + const double _kdS) +{ + _rResult.m_dX = _krV.m_dX * _kdS; + _rResult.m_dY = _krV.m_dY * _kdS; + + return(_rResult); +} + +const TVector2f& ScalarMultiply( TVector2f& _rResult, + const TVector2f& _krV, + const float _kfS) +{ + _rResult.m_fX = _krV.m_fX * _kfS; + _rResult.m_fY = _krV.m_fY * _kfS; + + return(_rResult); +} + +const double VectorMagnitude(const TVector2d& _krV) +{ + return(SquareRoot( Square(_krV.m_dX) + + Square(_krV.m_dY))); +} + +const float VectorMagnitude(const TVector2f& _krV) +{ + return(SquareRoot( Square(_krV.m_fX) + + Square(_krV.m_fY))); +} + +const double DotProduct( const TVector2d& _krA, + const TVector2d& _krB) +{ + return( (_krA.m_dX * _krB.m_dX) + + (_krA.m_dY * _krB.m_dY)); +} + +const float DotProduct( const TVector2f& _krA, + const TVector2f& _krB) +{ + return( (_krA.m_fX * _krB.m_fX) + + (_krA.m_fY * _krB.m_fY)); +} + +const TVector2d& Normalize( TVector2d& _rResult, + const TVector2d& _krV) +{ + ScalarMultiply(_rResult, _krV, (1.0 / VectorMagnitude(_krV)) ); + return(_rResult); +} + +const TVector2f& Normalize( TVector2f& _rResult, + const TVector2f& _krV) +{ + ScalarMultiply(_rResult, _krV, (1.0f / VectorMagnitude(_krV)) ); + return(_rResult); +} + +const TVector2d& Projection( TVector2d& _rResult, + const TVector2d& _krA, + const TVector2d& _krB) +{ + const double kdDenom = Square(VectorMagnitude(_krB)); + + const TVector2d kNumer = ScalarMultiply(TVector2d(), _krB, DotProduct(_krA, _krB)); + + _rResult = ScalarMultiply(_rResult, kNumer, kdDenom); + + return(_rResult); +} + +const TVector2f& Projection( TVector2f& _rResult, + const TVector2f& _krA, + const TVector2f& _krB) +{ + const float kfDenom = Square(VectorMagnitude(_krB)); + + const TVector2f kNumer = ScalarMultiply(TVector2f(), _krB, DotProduct(_krA, _krB)); + + _rResult = ScalarMultiply(_rResult, kNumer, kfDenom); + + return(_rResult); +} + +const double AngleBetween(const TVector2d& _krA, + const TVector2d& _krB) +{ + const double kdAngle = ArcCos( DotProduct(_krA, _krB) + / ( VectorMagnitude(_krA) * VectorMagnitude(_krB) ) ); + + return(kdAngle); +} + +const float AngleBetween( const TVector2f& _krA, + const TVector2f& _krB) +{ + const float kfAngle = ArcCos( DotProduct(_krA, _krB) + / ( VectorMagnitude(_krA) * VectorMagnitude(_krB) ) ); + + return(kfAngle); +} + +const double Distance(const TVector2d& _krA, + const TVector2d& _krB) +{ + const double kdDistance = VectorMagnitude(Subtract(TVector2d(), _krA, _krB)); + + return(kdDistance); +} + +const float Distance( const TVector2f& _krA, + const TVector2f& _krB) +{ + const float kfDistance = VectorMagnitude(Subtract(TVector2f(), _krA, _krB)); + + return(kfDistance); +} \ No newline at end of file diff --git a/ReflexToQ3/ckmath/ckmath_vector.h b/ReflexToQ3/ckmath/ckmath_vector.h new file mode 100644 index 0000000..e42c6ae --- /dev/null +++ b/ReflexToQ3/ckmath/ckmath_vector.h @@ -0,0 +1,272 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +#pragma once + +#ifndef __MATH_VECTOR_H__ +#define __MATH_VECTOR_H__ + +// Local Includes +#include "ckmath_types.h" + +// Vector Function Prototypes + + +// +// Vector 4 +// + +const TVector4d& ZeroVector(TVector4d& _rResult); + +const TVector4f& ZeroVector(TVector4f& _rResult); + +const bool Equal( const TVector4d& _krA, + const TVector4d& _krB, + const double _kdEpsilon); + +const bool Equal( const TVector4f& _krA, + const TVector4f& _krB, + const float _kfEpsilon); + +const TVector4d& Add( TVector4d& _rResult, + const TVector4d& _krA, + const TVector4d& _krB); + +const TVector4f& Add( TVector4f& _rResult, + const TVector4f& _krA, + const TVector4f& _krB); + +const TVector4d& Subtract( TVector4d& _rResult, + const TVector4d& _krA, + const TVector4d& _krB); + +const TVector4f& Subtract( TVector4f& _rResult, + const TVector4f& _krA, + const TVector4f& _krB); + +const TVector4d& ScalarMultiply(TVector4d& _rResult, + const TVector4d& _krV, + const double _kdS); + +const TVector4f& ScalarMultiply(TVector4f& _rResult, + const TVector4f& _krV, + const float _kfS); + +const double VectorMagnitude(const TVector4d& _krV); + +const float VectorMagnitude(const TVector4f& _krV); + +const double DotProduct(const TVector4d& _krA, + const TVector4d& _krB); + +const float DotProduct( const TVector4f& _krA, + const TVector4f& _krB); + +const TVector4d& Normalize( TVector4d& _rResult, + const TVector4d& _krV); + +const TVector4f& Normalize( TVector4f& _rResult, + const TVector4f& _krV); + +const TVector4d& Projection(TVector4d& _rResult, + const TVector4d& _krA, + const TVector4d& _krB); + +const TVector4f& Projection(TVector4f& _rResult, + const TVector4f& _krA, + const TVector4f& _krB); + +const double AngleBetween( const TVector4d& _krA, + const TVector4d& _krB); + +const float AngleBetween( const TVector4f& _krA, + const TVector4f& _krB); + +const double Distance( const TVector4d& _krA, + const TVector4d& _krB); + +const float Distance( const TVector4f& _krA, + const TVector4f& _krB); + +// +// Vector 3 +// + +const TVector3d& ZeroVector(TVector3d& _rResult); + +const TVector3f& ZeroVector(TVector3f& _rResult); + +const bool Equal( const TVector3d& _krA, + const TVector3d& _krB, + const double _kdEpsilon); + +const bool Equal( const TVector3f& _krA, + const TVector3f& _krB, + const float _kfEpsilon); + +const TVector3d& Add( TVector3d& _rResult, + const TVector3d& _krA, + const TVector3d& _krB); + +const TVector3f& Add( TVector3f& _rResult, + const TVector3f& _krA, + const TVector3f& _krB); + +const TVector3d& Subtract( TVector3d& _rResult, + const TVector3d& _krA, + const TVector3d& _krB); + +const TVector3f& Subtract( TVector3f& _rResult, + const TVector3f& _krA, + const TVector3f& _krB); + +const TVector3d& ScalarMultiply(TVector3d& _rResult, + const TVector3d& _krV, + const double _kdS); + +const TVector3f& ScalarMultiply(TVector3f& _rResult, + const TVector3f& _krV, + const float _kfS); + +const double VectorMagnitude(const TVector3d& _krV); + +const float VectorMagnitude(const TVector3f& _krV); + +const double DotProduct(const TVector3d& _krA, + const TVector3d& _krB); + +const float DotProduct( const TVector3f& _krA, + const TVector3f& _krB); + +const TVector3d& CrossProduct( TVector3d& _rResult, + const TVector3d& _krA, + const TVector3d& _krB); + +const TVector3f& CrossProduct( TVector3f& _rResult, + const TVector3f& _krA, + const TVector3f& _krB); + +const TVector3d& Normalize( TVector3d& _rResult, + const TVector3d& _krV); + +const TVector3f& Normalize( TVector3f& _rResult, + const TVector3f& _krV); + +const TVector3d& Projection(TVector3d& _rResult, + const TVector3d& _krA, + const TVector3d& _krB); + +const TVector3f& Projection(TVector3f& _rResult, + const TVector3f& _krA, + const TVector3f& _krB); + +const double AngleBetween( const TVector3d& _krA, + const TVector3d& _krB); + +const float AngleBetween( const TVector3f& _krA, + const TVector3f& _krB); + +const double Distance( const TVector3d& _krA, + const TVector3d& _krB); + +const float Distance( const TVector3f& _krA, + const TVector3f& _krB); + +const double ScalarTripleProduct( const TVector3d& _krA, + const TVector3d& _krB, + const TVector3d& _krC); + +const float ScalarTripleProduct( const TVector3f& _krA, + const TVector3f& _krB, + const TVector3f& _krC); + +const TVector3d& VectorTripleProduct( TVector3d& _rResult, + const TVector3d& _krA, + const TVector3d& _krB, + const TVector3d& _krC); + +const TVector3f& VectorTripleProduct( TVector3f& _rResult, + const TVector3f& _krA, + const TVector3f& _krB, + const TVector3f& _krC); + +// +// Vector 2 +// + +const TVector2d& ZeroVector(TVector2d& _rResult); + +const TVector2f& ZeroVector(TVector2f& _rResult); + +const bool Equal( const TVector2d& _krA, + const TVector2d& _krB, + const double _kdEpsilon); + +const bool Equal( const TVector2f& _krA, + const TVector2f& _krB, + const float _kfEpsilon); + +const TVector2d& Add( TVector2d& _rResult, + const TVector2d& _krA, + const TVector2d& _krB); + +const TVector2f& Add( TVector2f& _rResult, + const TVector2f& _krA, + const TVector2f& _krB); + +const TVector2d& Subtract( TVector2d& _rResult, + const TVector2d& _krA, + const TVector2d& _krB); + +const TVector2f& Subtract( TVector2f& _rResult, + const TVector2f& _krA, + const TVector2f& _krB); + +const TVector2d& ScalarMultiply(TVector2d& _rResult, + const TVector2d& _krV, + const double _kdS); + +const TVector2f& ScalarMultiply(TVector2f& _rResult, + const TVector2f& _krV, + const float _kfS); + +const double VectorMagnitude(const TVector2d& _krV); + +const float VectorMagnitude(const TVector2f& _krV); + +const double DotProduct(const TVector2d& _krA, + const TVector2d& _krB); + +const float DotProduct( const TVector2f& _krA, + const TVector2f& _krB); + +const TVector2d& Normalize( TVector2d& _rResult, + const TVector2d& _krV); + +const TVector2f& Normalize( TVector2f& _rResult, + const TVector2f& _krV); + +const TVector2d& Projection(TVector2d& _rResult, + const TVector2d& _krA, + const TVector2d& _krB); + +const TVector2f& Projection(TVector2f& _rResult, + const TVector2f& _krA, + const TVector2f& _krB); + +const double AngleBetween( const TVector2d& _krA, + const TVector2d& _krB); + +const float AngleBetween( const TVector2f& _krA, + const TVector2f& _krB); + +const double Distance( const TVector2d& _krA, + const TVector2d& _krB); + +const float Distance( const TVector2f& _krA, + const TVector2f& _krB); + + +#endif \ No newline at end of file diff --git a/ReflexToQ3/libraries.h b/ReflexToQ3/libraries.h new file mode 100644 index 0000000..1c87177 --- /dev/null +++ b/ReflexToQ3/libraries.h @@ -0,0 +1,37 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +#pragma once + +#ifndef __LIBRARIES_H__ +#define __LIBRARIES_H__ + +// Visual Leak Detector +#if defined(DEBUG) || defined(_DEBUG) +//#include +#endif + +// Platform Libraries +#define WIN32_LEAN_AND_MEAN +#include + +// Chronokun Libraries +#include "ckmath/ckmath.h" + +// C++ Libraries +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#endif \ No newline at end of file diff --git a/ReflexToQ3/main.cpp b/ReflexToQ3/main.cpp new file mode 100644 index 0000000..9b5ff67 --- /dev/null +++ b/ReflexToQ3/main.cpp @@ -0,0 +1,121 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +// Libraries Include +#include "libraries.h" +// Local Includes +#include "mapparser.h" + +// Structs +struct TPlanePoints +{ + TVector3f m_A; + TVector3f m_B; + TVector3f m_C; +}; + +TPlanePoints GetPlanePoints(const TVector3f* _kpPoints, const size_t _kNumPoints) +{ + TPlanePoints PlanePoints; + + // Find Normal + const TVector3f kNormal = GetPolygonNormal(TVector3f(), _kpPoints, _kNumPoints); + + // Find center point + TVector3f Temp{0.0f, 0.0f, 0.0f}; + for(size_t i = 0; i < _kNumPoints; ++i) + { + Temp = Add(Temp, Temp, _kpPoints[i]); + } + const TVector3f kCenter = ScalarMultiply(TVector3f(), Temp, (1.0f / (float)_kNumPoints)); + + // Find Tangent + const TVector3f kTangent = Normalize(TVector3f(), Subtract(TVector3f(), _kpPoints[1], _kpPoints[0])); + + // Find BiTangent + const TVector3f kBiTangent = Normalize(TVector3f(), CrossProduct(TVector3f(), kNormal, kTangent)); + + PlanePoints.m_A = Add(TVector3f(), kCenter, kBiTangent); + PlanePoints.m_B = kCenter; + PlanePoints.m_C = Add(TVector3f(), kCenter, kTangent); + + return(PlanePoints); +} + +std::vector GetBrushPlanes(const TBrush& _krBrush) +{ + std::vector Planes; + for(const TFace& krFace : _krBrush.m_Faces) + { + std::vector Verts(krFace.m_Indices.size()); + for(size_t i = 0; i < krFace.m_Indices.size(); ++i) + { + Verts[i] = _krBrush.m_Vertices[krFace.m_Indices[i]]; + } + Planes.push_back(GetPlanePoints(Verts.data(), Verts.size())); + } + + return(Planes); +} + +std::string GetBrushString(const TBrush& _krBrush) +{ + std::vector Planes = GetBrushPlanes(_krBrush); + std::stringstream ssOutput; + + if(Planes.size()) + { + ssOutput << "{" << std::endl; + for(const TPlanePoints& krPlane : Planes) + { + ssOutput << "( " << krPlane.m_A.m_fX << " " << krPlane.m_A.m_fZ << " " << krPlane.m_A.m_fY << " ) "; + ssOutput << "( " << krPlane.m_B.m_fX << " " << krPlane.m_B.m_fZ << " " << krPlane.m_B.m_fY << " ) "; + ssOutput << "( " << krPlane.m_C.m_fX << " " << krPlane.m_C.m_fZ << " " << krPlane.m_C.m_fY << " ) "; + ssOutput << "common/caulk 0 0 0 0.500000 0.500000 0 4 0" << std::endl; + } + ssOutput << "}" << std::endl; + } + + return(ssOutput.str()); +} + +int main(const int _kiArgC, const char** _kppcArgv) +{ + // Check we have correct number of parameters + if(_kiArgC < 3) + { + return(3); + } + + CMapParser Parser; + + const bool kbSuccess = Parser.LoadMap(_kppcArgv[1]); + if(!kbSuccess) + { + return(1); + } + else + { + // Open output file + std::ofstream OutFile; + OutFile.open(_kppcArgv[2]); + if(!OutFile.is_open()) + { + return(2); + } + + OutFile << "{" << std::endl + << "\"classname\" \"worldspawn\"" << std::endl; + + for(const TBrush& krBrush : Parser.m_WorldSpawn.m_Brushes) + { + OutFile << GetBrushString(krBrush); + } + + OutFile << "}" << std::endl; + } + + return(0); +} diff --git a/ReflexToQ3/mapparser.cpp b/ReflexToQ3/mapparser.cpp new file mode 100644 index 0000000..da18d8d --- /dev/null +++ b/ReflexToQ3/mapparser.cpp @@ -0,0 +1,233 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +// Libraries Include +#include "libraries.h" +// This Include +#include "mapparser.h" + +const bool CMapParser::LoadMap(const char* _kpcFileName) +{ + std::ifstream InFile; + EParserState eState = PARSERSTATE_UNKNOWN; + + InFile.open(_kpcFileName, std::ios::in); + if(InFile.is_open()) + { + std::string Line; + bool bAdvance = true; + bool bGoing = true; + while(bGoing) + { + if(bAdvance) + { + if(!std::getline(InFile, Line)) + { + bGoing = false; + } + } + bAdvance = true; + if(eState == PARSERSTATE_UNKNOWN) + { + if(strcmp(Line.c_str(), "entity") == 0) + { + eState = PARSERSTATE_ENTITY; + continue; + } + } + else if(eState == PARSERSTATE_ENTITY) + { + eState = this->ParseEntity(Line); + } + else if(eState == PARSERSTATE_WORLDSPAWN) + { + eState = this->ParseWorldSpawn(Line); + } + else if(eState == PARSERSTATE_BRUSH) + { + eState = this->ParseBrush(Line); + //bAdvance = false; + } + else if(eState == PARSERSTATE_VERTEX) + { + eState = this->ParseVertex(Line); + if(eState != PARSERSTATE_VERTEX) + { + bAdvance = false; + } + } + else if(eState == PARSERSTATE_FACE) + { + eState = this->ParseFace(Line); + if(eState != PARSERSTATE_FACE) + { + bAdvance = false; + } + } + } + InFile.close(); + } + else + { + return(false); + } + + return(true); +} + +EParserState CMapParser::ParseEntity(const std::string _Line) +{ + const size_t kszLineSize = 256; + char pcLine[kszLineSize]; + const char* kpcDelim = " "; + char* pcToken; + char* pcContext; + + strcpy_s(pcLine, _Line.c_str()); + pcToken = strtok_s(pcLine, kpcDelim, &pcContext); + + while(pcToken != nullptr) + { + if(strcmp(pcToken, "\ttype") == 0) + { + pcToken = strtok_s(nullptr, kpcDelim, &pcContext); + if(strcmp(pcToken, "WorldSpawn") == 0) + { + return(PARSERSTATE_WORLDSPAWN); + } + else + { + return(PARSERSTATE_UNKNOWN); + } + } + else + { + return(PARSERSTATE_ENTITY); + } + } + return(PARSERSTATE_UNKNOWN); +} + +EParserState CMapParser::ParseWorldSpawn(const std::string _Line) +{ + if(strcmp(_Line.c_str(), "brush") == 0) + { + this->m_WorldSpawn.m_Brushes.push_back(TBrush()); + return(PARSERSTATE_BRUSH); + } + return(PARSERSTATE_WORLDSPAWN); +} + +EParserState CMapParser::ParseBrush(const std::string _Line) +{ + if(strcmp(_Line.c_str(), "brush") == 0) + { + this->m_WorldSpawn.m_Brushes.push_back(TBrush()); + return(PARSERSTATE_BRUSH); + } + if(strcmp(_Line.c_str(), "\tvertices") == 0) + { + return(PARSERSTATE_VERTEX); + } + else if(strcmp(_Line.c_str(), "\tfaces") == 0) + { + return(PARSERSTATE_FACE); + } + return(PARSERSTATE_BRUSH); +} + +EParserState CMapParser::ParseVertex(const std::string _Line) +{ + const size_t kszLineSize = 256; + char pcLine[kszLineSize]; + const char* kpcDelim = " \t"; + char* pcToken; + char* pcContext; + + strcpy_s(pcLine, _Line.c_str()); + pcToken = strtok_s(pcLine, kpcDelim, &pcContext); + + TVector3f Vert; + int iTokenNum = 0; + while(pcToken != nullptr) + { + if(std::isdigit(pcToken[0], std::locale()) || pcToken[0] == '-') + { + double dValue = std::stod(pcToken); + if(iTokenNum == 0) + { + Vert.m_fX = (float)dValue; + } + else if(iTokenNum == 1) + { + Vert.m_fY = (float)dValue; + } + else if(iTokenNum == 2) + { + Vert.m_fZ = (float)dValue; + } + iTokenNum++; + } + else + { + return(PARSERSTATE_BRUSH); + } + + pcToken = strtok_s(nullptr, kpcDelim, &pcContext); + } + + this->m_WorldSpawn.m_Brushes[this->m_WorldSpawn.m_Brushes.size()-1].m_Vertices.push_back(Vert); + return(PARSERSTATE_VERTEX); +} + +EParserState CMapParser::ParseFace(const std::string _Line) +{ + const size_t kszLineSize = 256; + char pcLine[kszLineSize]; + const char* kpcDelim = " \t"; + char* pcToken; + char* pcContext; + + strcpy_s(pcLine, _Line.c_str()); + pcToken = strtok_s(pcLine, kpcDelim, &pcContext); + + int iTokenNum = 0; + + std::vector Indices; + while(pcToken != nullptr) + { + if(iTokenNum < 5) + { + if(std::isdigit(pcToken[0], std::locale()) || pcToken[0] == '-') + { + double dValue = std::stod(pcToken); + } + else + { + return(PARSERSTATE_BRUSH); + } + } + else if(iTokenNum < 9) + { + if(std::isdigit(pcToken[0], std::locale()) || pcToken[0] == '-') + { + int iValue = std::stoi(pcToken); + Indices.push_back(iValue); + } + else + { + return(PARSERSTATE_BRUSH); + } + } + + iTokenNum++; + pcToken = strtok_s(nullptr, kpcDelim, &pcContext); + } + + TFace Face; + Face.m_Indices = Indices; + this->m_WorldSpawn.m_Brushes[this->m_WorldSpawn.m_Brushes.size()-1].m_Faces.push_back(Face); + return(PARSERSTATE_FACE); +} \ No newline at end of file diff --git a/ReflexToQ3/mapparser.h b/ReflexToQ3/mapparser.h new file mode 100644 index 0000000..46b79d7 --- /dev/null +++ b/ReflexToQ3/mapparser.h @@ -0,0 +1,44 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +#pragma once + +#ifndef __MAPPARSER_H__ +#define __MAPPARSER_H__ + +// Includes +#include "worldspawn.h" + +// enums +enum EParserState +{ + PARSERSTATE_UNKNOWN, + PARSERSTATE_ENTITY, + PARSERSTATE_WORLDSPAWN, + PARSERSTATE_BRUSH, + PARSERSTATE_VERTEX, + PARSERSTATE_FACE +}; + +class CMapParser +{ + // Variables +public: + TWorldSpawn m_WorldSpawn; + + // Functions +public: + const bool LoadMap(const char* _kpcFileName); + +protected: + EParserState ParseEntity(const std::string _Line); + EParserState ParseWorldSpawn(const std::string _Line); + EParserState ParseBrush(const std::string _Line); + EParserState ParseVertex(const std::string _Line); + EParserState ParseFace(const std::string _Line); + +}; + +#endif \ No newline at end of file diff --git a/ReflexToQ3/worldspawn.h b/ReflexToQ3/worldspawn.h new file mode 100644 index 0000000..23915ae --- /dev/null +++ b/ReflexToQ3/worldspawn.h @@ -0,0 +1,34 @@ +// +// Author: Michael Cameron +// Email: chronokun@hotmail.com +// + +#pragma once + +#ifndef __WORLDSPAWN_H__ +#define __WORLDSPAWN_H__ + +struct TFace +{ + float m_fXOffset; + float m_fYOffset; + float m_fXScale; + float m_fYScale; + float m_fRotation; + std::vector m_Indices; + std::string m_Material; +}; + +struct TBrush +{ + std::vector m_Vertices; + std::vector m_Faces; + +}; + +struct TWorldSpawn +{ + std::vector m_Brushes; +}; + +#endif \ No newline at end of file