Dependency Reconfiguration to support MSVC being picky :/
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <J3ML/J3ML.h>
|
||||
|
||||
|
||||
namespace J3ML::LinearAlgebra {
|
||||
@@ -19,4 +20,4 @@ namespace J3ML::LinearAlgebra {
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -3,11 +3,143 @@
|
||||
/// Template Parameterized (Generic) Matrix Functions.
|
||||
|
||||
#include <J3ML/LinearAlgebra/Quaternion.h>
|
||||
|
||||
#include <J3ML/J3ML.h>
|
||||
|
||||
|
||||
namespace J3ML::LinearAlgebra {
|
||||
|
||||
template <typename Matrix>
|
||||
bool InverseMatrix(Matrix &mat, float epsilon)
|
||||
{
|
||||
Matrix inversed = Matrix::Identity;
|
||||
|
||||
const int nc = std::min<int>(Matrix::Rows, Matrix::Cols);
|
||||
|
||||
for (int column = 0; column < nc; ++column)
|
||||
{
|
||||
// find the row i with i >= j such that M has the largest absolute value.
|
||||
int greatest = column;
|
||||
float greatestVal = std::abs(mat[greatest][column]);
|
||||
for (int i = column+1; i < Matrix::Rows; i++)
|
||||
{
|
||||
float val = std::abs(mat[i][column]);
|
||||
if (val > greatestVal) {
|
||||
greatest = i;
|
||||
greatestVal = val;
|
||||
}
|
||||
}
|
||||
|
||||
if (greatestVal < epsilon) {
|
||||
mat = inversed;
|
||||
return false;
|
||||
}
|
||||
|
||||
// exchange rows
|
||||
if (greatest != column) {
|
||||
inversed.SwapRows(greatest, column);
|
||||
mat.SwapRows(greatest, column);
|
||||
}
|
||||
|
||||
// multiply rows
|
||||
assert(!Math::EqualAbs(mat[column][column], 0.f, epsilon));
|
||||
float scale = 1.f / mat[column][column];
|
||||
inversed.ScaleRow(column, scale);
|
||||
mat.ScaleRow(column, scale);
|
||||
|
||||
// add rows
|
||||
for (int i = 0; i < column; i++) {
|
||||
inversed.SetRow(i, inversed.Row(i) - inversed.Row(column) * mat[i][column]);
|
||||
mat.SetRow(i, mat.Row(i) - mat.Row(column) * mat[i][column]);
|
||||
}
|
||||
|
||||
for (int i = column + 1; i < Matrix::Rows; i++) {
|
||||
inversed.SetRow(i, inversed.Row(i) - inversed.Row(column) * mat[i][column]);
|
||||
mat.SetRow(i, mat.Row(i) - mat.Row(column) * mat[i][column]);
|
||||
}
|
||||
}
|
||||
mat = inversed;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// Computes the LU-decomposition on the given square matrix.
|
||||
/// @return True if the composition was successful, false otherwise. If the return value is false, the contents of the output matrix are unspecified.
|
||||
template <typename Matrix>
|
||||
bool LUDecomposeMatrix(const Matrix &mat, Matrix &lower, Matrix &upper)
|
||||
{
|
||||
lower = Matrix::Identity;
|
||||
upper = Matrix::Zero;
|
||||
|
||||
for (int i = 0; i < Matrix::Rows; ++i)
|
||||
{
|
||||
for (int col = i; col < Matrix::Cols; ++col)
|
||||
{
|
||||
upper[i][col] = mat[i][col];
|
||||
for (int k = 0; k < i; ++k)
|
||||
upper[i][col] -= lower[i][k] * upper[k][col];
|
||||
}
|
||||
for (int row = i+1; row < Matrix::Rows; ++row)
|
||||
{
|
||||
lower[row][i] = mat[row][i];
|
||||
for (int k = 0; k < i; ++k)
|
||||
lower[row][i] -= lower[row][k] * upper[k][i];
|
||||
if (Math::EqualAbs(upper[i][i], 0.f))
|
||||
return false;
|
||||
lower[row][i] /= upper[i][i];
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Computes the Cholesky decomposition on the given square matrix *on the real domain*.
|
||||
/// @return True if successful, false otherwise. If the return value is false, the contents of the output matrix are uspecified.
|
||||
template <typename Matrix>
|
||||
bool CholeskyDecomposeMatrix(const Matrix &mat, Matrix& lower)
|
||||
{
|
||||
lower = Matrix::Zero;
|
||||
for (int i = 0; i < Matrix::Rows; ++i)
|
||||
{
|
||||
for (int j = 0; j < i; ++i)
|
||||
{
|
||||
lower[i][j] = mat[i][j];
|
||||
for (int k = 0; k < j; ++k)
|
||||
lower[i][j] -= lower[i][j] * lower[j][k];
|
||||
if (Math::EqualAbs(lower[j][j], 0.f))
|
||||
return false;
|
||||
lower[i][j] /= lower[j][j];
|
||||
}
|
||||
lower[i][i] = mat[i][i];
|
||||
if (lower[i][i])
|
||||
return false;
|
||||
for (int k = 0; k < i; ++k)
|
||||
lower[i][i] -= lower[i][k] * lower[i][k];
|
||||
lower[i][i] = std::sqrt(lower[i][i]);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template<typename Matrix>
|
||||
void SetMatrixRotatePart(Matrix &m, const Quaternion &q) {
|
||||
// See https://www.geometrictools.com/Documentation/LinearAlgebraicQuaternions.pdf .
|
||||
|
||||
assert(q.IsNormalized(1e-3f));
|
||||
const float x = q.x;
|
||||
const float y = q.y;
|
||||
const float z = q.z;
|
||||
const float w = q.w;
|
||||
m[0][0] = 1 - 2 * (y * y + z * z);
|
||||
m[0][1] = 2 * (x * y - z * w);
|
||||
m[0][2] = 2 * (x * y + y * w);
|
||||
m[1][0] = 2 * (x * y + z * w);
|
||||
m[1][1] = 1 - 2 * (x * x + z * z);
|
||||
m[1][2] = 2 * (y * z - x * w);
|
||||
m[2][0] = 2 * (x * z - y * w);
|
||||
m[2][1] = 2 * (y * z + x * w);
|
||||
m[2][2] = 1 - 2 * (x * x + y * y);
|
||||
}
|
||||
|
||||
/** Sets the top-left 3x3 area of the matrix to the rotation matrix about the X-axis. Elements
|
||||
outside the top-left 3x3 area are ignored. This matrix rotates counterclockwise if multiplied
|
||||
in the order M*v, and clockwise if rotated in the order v*M.
|
||||
|
@@ -1,9 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <J3ML/LinearAlgebra/Vector2.h>
|
||||
|
||||
#include <J3ML/LinearAlgebra/Common.h>
|
||||
#include <J3ML/LinearAlgebra/Matrices.inl>
|
||||
|
||||
|
||||
namespace J3ML::LinearAlgebra {
|
||||
class Matrix2x2 {
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <J3ML/LinearAlgebra/Common.h>
|
||||
|
||||
#include <J3ML/LinearAlgebra/Vector2.h>
|
||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||
#include <J3ML/LinearAlgebra/Quaternion.h>
|
||||
@@ -13,25 +13,7 @@ using namespace J3ML::Algorithm;
|
||||
|
||||
namespace J3ML::LinearAlgebra {
|
||||
|
||||
template<typename Matrix>
|
||||
void SetMatrixRotatePart(Matrix &m, const Quaternion &q) {
|
||||
// See https://www.geometrictools.com/Documentation/LinearAlgebraicQuaternions.pdf .
|
||||
|
||||
assert(q.IsNormalized(1e-3f));
|
||||
const float x = q.x;
|
||||
const float y = q.y;
|
||||
const float z = q.z;
|
||||
const float w = q.w;
|
||||
m[0][0] = 1 - 2 * (y * y + z * z);
|
||||
m[0][1] = 2 * (x * y - z * w);
|
||||
m[0][2] = 2 * (x * y + y * w);
|
||||
m[1][0] = 2 * (x * y + z * w);
|
||||
m[1][1] = 1 - 2 * (x * x + z * z);
|
||||
m[1][2] = 2 * (y * z - x * w);
|
||||
m[2][0] = 2 * (x * z - y * w);
|
||||
m[2][1] = 2 * (y * z + x * w);
|
||||
m[2][2] = 1 - 2 * (x * x + y * y);
|
||||
}
|
||||
|
||||
/// A 3-by-3 matrix for linear transformations of 3D geometry.
|
||||
/* This can represent any kind of linear transformations of 3D geometry, which include
|
||||
|
@@ -1,11 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <J3ML/LinearAlgebra/Common.h>
|
||||
|
||||
#include <J3ML/LinearAlgebra/Matrix3x3.h>
|
||||
#include <J3ML/LinearAlgebra/Quaternion.h>
|
||||
#include <J3ML/LinearAlgebra/Vector4.h>
|
||||
#include <J3ML/LinearAlgebra/Matrices.inl>
|
||||
#include <J3ML/LinearAlgebra/Common.h>
|
||||
#include <J3ML/Algorithm/RNG.h>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -14,114 +10,7 @@ using namespace J3ML::Algorithm;
|
||||
|
||||
namespace J3ML::LinearAlgebra {
|
||||
|
||||
template <typename Matrix>
|
||||
bool InverseMatrix(Matrix &mat, float epsilon)
|
||||
{
|
||||
Matrix inversed = Matrix::Identity;
|
||||
|
||||
const int nc = std::min<int>(Matrix::Rows, Matrix::Cols);
|
||||
|
||||
for (int column = 0; column < nc; ++column)
|
||||
{
|
||||
// find the row i with i >= j such that M has the largest absolute value.
|
||||
int greatest = column;
|
||||
float greatestVal = std::abs(mat[greatest][column]);
|
||||
for (int i = column+1; i < Matrix::Rows; i++)
|
||||
{
|
||||
float val = std::abs(mat[i][column]);
|
||||
if (val > greatestVal) {
|
||||
greatest = i;
|
||||
greatestVal = val;
|
||||
}
|
||||
}
|
||||
|
||||
if (greatestVal < epsilon) {
|
||||
mat = inversed;
|
||||
return false;
|
||||
}
|
||||
|
||||
// exchange rows
|
||||
if (greatest != column) {
|
||||
inversed.SwapRows(greatest, column);
|
||||
mat.SwapRows(greatest, column);
|
||||
}
|
||||
|
||||
// multiply rows
|
||||
assert(!Math::EqualAbs(mat[column][column], 0.f, epsilon));
|
||||
float scale = 1.f / mat[column][column];
|
||||
inversed.ScaleRow(column, scale);
|
||||
mat.ScaleRow(column, scale);
|
||||
|
||||
// add rows
|
||||
for (int i = 0; i < column; i++) {
|
||||
inversed.SetRow(i, inversed.Row(i) - inversed.Row(column) * mat[i][column]);
|
||||
mat.SetRow(i, mat.Row(i) - mat.Row(column) * mat[i][column]);
|
||||
}
|
||||
|
||||
for (int i = column + 1; i < Matrix::Rows; i++) {
|
||||
inversed.SetRow(i, inversed.Row(i) - inversed.Row(column) * mat[i][column]);
|
||||
mat.SetRow(i, mat.Row(i) - mat.Row(column) * mat[i][column]);
|
||||
}
|
||||
}
|
||||
mat = inversed;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// Computes the LU-decomposition on the given square matrix.
|
||||
/// @return True if the composition was successful, false otherwise. If the return value is false, the contents of the output matrix are unspecified.
|
||||
template <typename Matrix>
|
||||
bool LUDecomposeMatrix(const Matrix &mat, Matrix &lower, Matrix &upper)
|
||||
{
|
||||
lower = Matrix::Identity;
|
||||
upper = Matrix::Zero;
|
||||
|
||||
for (int i = 0; i < Matrix::Rows; ++i)
|
||||
{
|
||||
for (int col = i; col < Matrix::Cols; ++col)
|
||||
{
|
||||
upper[i][col] = mat[i][col];
|
||||
for (int k = 0; k < i; ++k)
|
||||
upper[i][col] -= lower[i][k] * upper[k][col];
|
||||
}
|
||||
for (int row = i+1; row < Matrix::Rows; ++row)
|
||||
{
|
||||
lower[row][i] = mat[row][i];
|
||||
for (int k = 0; k < i; ++k)
|
||||
lower[row][i] -= lower[row][k] * upper[k][i];
|
||||
if (Math::EqualAbs(upper[i][i], 0.f))
|
||||
return false;
|
||||
lower[row][i] /= upper[i][i];
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Computes the Cholesky decomposition on the given square matrix *on the real domain*.
|
||||
/// @return True if successful, false otherwise. If the return value is false, the contents of the output matrix are uspecified.
|
||||
template <typename Matrix>
|
||||
bool CholeskyDecomposeMatrix(const Matrix &mat, Matrix& lower)
|
||||
{
|
||||
lower = Matrix::Zero;
|
||||
for (int i = 0; i < Matrix::Rows; ++i)
|
||||
{
|
||||
for (int j = 0; j < i; ++i)
|
||||
{
|
||||
lower[i][j] = mat[i][j];
|
||||
for (int k = 0; k < j; ++k)
|
||||
lower[i][j] -= lower[i][j] * lower[j][k];
|
||||
if (Math::EqualAbs(lower[j][j], 0.f))
|
||||
return false;
|
||||
lower[i][j] /= lower[j][j];
|
||||
}
|
||||
lower[i][i] = mat[i][i];
|
||||
if (lower[i][i])
|
||||
return false;
|
||||
for (int k = 0; k < i; ++k)
|
||||
lower[i][i] -= lower[i][k] * lower[i][k];
|
||||
lower[i][i] = std::sqrt(lower[i][i]);
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief A 4-by-4 matrix for affine transformations and perspective projections of 3D geometry.
|
||||
/// This matrix can represent the most generic form of transformations for 3D objects,
|
||||
@@ -416,7 +305,8 @@ namespace J3ML::LinearAlgebra {
|
||||
|
||||
/// Returns only the first three elements of the given column.
|
||||
Vector3 GetColumn3(int index) const;
|
||||
Vector3 Column3(int index) const { return GetColumn3(index);}
|
||||
Vector3 Column3(int index) const;
|
||||
|
||||
Vector3 Col3(int i) const;
|
||||
|
||||
/// Returns the scaling performed by this matrix. This function assumes taht the last row is [0 0 0 1].
|
||||
@@ -539,11 +429,7 @@ namespace J3ML::LinearAlgebra {
|
||||
/// @note This function assumes that this matrix does not contain projection (the fourth row of this matrix is [0 0 0 1]).
|
||||
/// @note This function assumes that this matrix has orthogonal basis vectors (row and column vector sets are orthogonal).
|
||||
/// @note This function does not remove reflection (-1 scale along some axis).
|
||||
void RemoveScale()
|
||||
{
|
||||
float tx = Row3(0).Normalize();
|
||||
float ty = Row3(1).Normalize();
|
||||
}
|
||||
void RemoveScale();
|
||||
|
||||
|
||||
/// Decomposes this matrix to translate, rotate, and scale parts.
|
||||
|
@@ -1,10 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <J3ML/LinearAlgebra/Common.h>
|
||||
#include <J3ML/LinearAlgebra/Matrix3x3.h>
|
||||
#include <J3ML/LinearAlgebra/Matrix4x4.h>
|
||||
#include <J3ML/LinearAlgebra/Vector4.h>
|
||||
#include <J3ML/LinearAlgebra/AxisAngle.h>
|
||||
#include <J3ML/Algorithm/RNG.h>
|
||||
#include <cmath>
|
||||
|
||||
@@ -104,7 +100,7 @@ namespace J3ML::LinearAlgebra
|
||||
|
||||
|
||||
/// Returns a uniformly random unitary quaternion.
|
||||
static Quaternion RandomRotation(J3ML::Algorithm::RNG &rng);
|
||||
static Quaternion RandomRotation(RNG &rng);
|
||||
public:
|
||||
void SetFromAxisAngle(const Vector3 &vector3, float between);
|
||||
|
||||
@@ -216,8 +212,12 @@ namespace J3ML::LinearAlgebra
|
||||
// The rotation q2 is applied first before q1.
|
||||
Quaternion operator * (const Quaternion& rhs) const;
|
||||
|
||||
// Unsafe
|
||||
Quaternion operator * (float scalar) const;
|
||||
|
||||
// Unsafe
|
||||
Quaternion operator / (float scalar) const;
|
||||
|
||||
// Transforms the given vector by this Quaternion.
|
||||
Vector3 operator * (const Vector3& rhs) const;
|
||||
Vector4 operator * (const Vector4& rhs) const;
|
||||
@@ -225,10 +225,9 @@ namespace J3ML::LinearAlgebra
|
||||
// Divides a quaternion by another. Divison "a / b" results in a quaternion that rotates the orientation b to coincide with orientation of
|
||||
Quaternion operator / (const Quaternion& rhs) const;
|
||||
Quaternion operator + (const Quaternion& rhs) const;
|
||||
Quaternion operator / (float scalar) const
|
||||
{
|
||||
assert(!Math::EqualAbs(scalar, 0.f));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Quaternion operator + () const;
|
||||
Quaternion operator - () const;
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <J3ML/J3ML.h>
|
||||
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace J3ML::LinearAlgebra {
|
||||
|
@@ -1,7 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cmath>
|
||||
|
||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||
#include <J3ML/LinearAlgebra/Common.h>
|
||||
|
||||
namespace J3ML::LinearAlgebra {
|
||||
class Vector4 {
|
||||
@@ -20,10 +22,7 @@ namespace J3ML::LinearAlgebra {
|
||||
{
|
||||
return &x;
|
||||
}
|
||||
Vector3 XYZ() const
|
||||
{
|
||||
return {x, y, z};
|
||||
}
|
||||
Vector3 XYZ() const;
|
||||
|
||||
float GetX() const { return x; }
|
||||
float GetY() const { return y; }
|
||||
|
Reference in New Issue
Block a user