Implement missing Matrix members
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <J3ML/LinearAlgebra/Vector2.h>
|
||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||
#include <J3ML/LinearAlgebra/Quaternion.h>
|
||||
#include <cstring>
|
||||
|
||||
namespace J3ML::LinearAlgebra {
|
||||
|
||||
@@ -47,7 +48,7 @@ namespace J3ML::LinearAlgebra {
|
||||
@param m The matrix to store the result.
|
||||
@param angle The rotation angle in radians. */
|
||||
template <typename Matrix>
|
||||
void Set3x3RotatePartZ(Matrix &m, float angle)
|
||||
void Set3x3PartRotateZ(Matrix &m, float angle)
|
||||
{
|
||||
float sinz, cosz;
|
||||
sinz = std::sin(angle);
|
||||
@@ -274,7 +275,6 @@ namespace J3ML::LinearAlgebra {
|
||||
/// Sets this matrix to equal the identity.
|
||||
void SetIdentity();
|
||||
|
||||
|
||||
void SwapColumns(int col1, int col2);
|
||||
|
||||
void SwapRows(int row1, int row2);
|
||||
@@ -291,7 +291,7 @@ namespace J3ML::LinearAlgebra {
|
||||
/// Sets all values of this matrix.
|
||||
/// @param valuesThe values in this array will be copied over to this matrix. The source must contain 9 floats in row-major order
|
||||
/// (the same order as the Set() function aove has its input parameters in).
|
||||
void Set(const float *values);
|
||||
void Set(const float *p);
|
||||
|
||||
/// Orthonormalizes the basis formed by the column vectors of this matrix.
|
||||
void Orthonormalize(int c0, int c1, int c2);
|
||||
@@ -383,8 +383,16 @@ namespace J3ML::LinearAlgebra {
|
||||
|
||||
void InverseOrthonormal();
|
||||
|
||||
/// Inverts a symmetric matrix.
|
||||
/** This function is faster than directly calling Inverse()
|
||||
This function computes 6 LOADs, 9 STOREs, 21 MULs, 1 DIV, 1 CMP, and 8 ADDs.
|
||||
@return True if computing the inverse succeeded, false otherwise (determinant was zero).
|
||||
@note If this function fails, the original matrix is not modified.
|
||||
@note This function operates in-place. */
|
||||
bool InverseSymmetric();
|
||||
|
||||
/// Transposes this matrix.
|
||||
/// This operation swaps all elements with respect to the diagonal.
|
||||
void Transpose();
|
||||
|
||||
|
||||
|
@@ -158,7 +158,7 @@ namespace J3ML::LinearAlgebra {
|
||||
Matrix4x4(float val);
|
||||
/// Constructs this Matrix4x4 to represent the same transformation as the given float3x3.
|
||||
/** This function expands the last row and column of this matrix with the elements from the identity matrix. */
|
||||
Matrix4x4(const Matrix3x3&);
|
||||
Matrix4x4(const Matrix3x3& m);
|
||||
explicit Matrix4x4(const float* data);
|
||||
|
||||
/// Constructs a new float4x4 by explicitly specifying all the matrix elements.
|
||||
|
@@ -249,14 +249,14 @@ namespace J3ML::LinearAlgebra {
|
||||
// In the local space, the forward and up directions must be perpendicular to be well-formed.
|
||||
// In the world space, the target direction and world up cannot be degenerate (co-linear)
|
||||
// Generate the third basis vector in the local space;
|
||||
Vector3 localRight = localUp.Cross(forward).Normalize();
|
||||
Vector3 localRight = localUp.Cross(forward).Normalized();
|
||||
// A. Now we have an orthonormal linear basis {localRight, localUp, forward} for the object local space.
|
||||
// Generate the third basis vector for the world space
|
||||
Vector3 worldRight = worldUp.Cross(target).Normalize();
|
||||
Vector3 worldRight = worldUp.Cross(target).Normalized();
|
||||
// Since the input worldUp vector is not necessarily perpendicular to the target direction vector
|
||||
// We need to compute the real world space up vector that the "head" of the object will point
|
||||
// towards when the model is looking towards the desired target direction
|
||||
Vector3 perpWorldUp = target.Cross(worldRight).Normalize();
|
||||
Vector3 perpWorldUp = target.Cross(worldRight).Normalized();
|
||||
// B. Now we have an orthonormal linear basis {worldRight, perpWorldUp, targetDirection } for the desired target orientation.
|
||||
// We want to build a matrix M that performs the following mapping:
|
||||
// 1. localRight must be mapped to worldRight. (M * localRight = worldRight)
|
||||
@@ -487,7 +487,7 @@ namespace J3ML::LinearAlgebra {
|
||||
}
|
||||
|
||||
void Matrix3x3::SetRotatePartZ(float angle) {
|
||||
Set3x3RotatePartZ(*this, angle);
|
||||
Set3x3PartRotateZ(*this, angle);
|
||||
}
|
||||
|
||||
Vector3 Matrix3x3::ExtractScale() const {
|
||||
@@ -772,6 +772,77 @@ namespace J3ML::LinearAlgebra {
|
||||
x[0] = b[0] - x[2] * v02 - x[1] * v01;
|
||||
}
|
||||
|
||||
void Matrix3x3::ScaleCol(int col, float scalar) {
|
||||
assert(col >= 0);
|
||||
assert(col < Cols);
|
||||
At(0, col) *= scalar;
|
||||
At(1, col) *= scalar;
|
||||
At(2, col) *= scalar;
|
||||
}
|
||||
|
||||
void Matrix3x3::ScaleRow(int row, float scalar) {
|
||||
assert(row >= 0);
|
||||
assert(row < Rows);
|
||||
assert(std::isfinite(scalar));
|
||||
At(row, 0) *= scalar;
|
||||
At(row, 1) *= scalar;
|
||||
At(row, 2) *= scalar;
|
||||
}
|
||||
|
||||
Vector3 Matrix3x3::Column3(int index) const {
|
||||
return GetColumn3(index);
|
||||
}
|
||||
|
||||
Vector3 Matrix3x3::Col3(int index) const {
|
||||
return GetColumn3(index);
|
||||
}
|
||||
|
||||
void Matrix3x3::Transpose() {
|
||||
Swap(elems[0][1], elems[1][0]);
|
||||
Swap(elems[0][2], elems[2][0]);
|
||||
Swap(elems[1][2], elems[2][1]);
|
||||
}
|
||||
|
||||
void Matrix3x3::SwapRows(int row1, int row2) {
|
||||
assert(row1 >= 0);
|
||||
assert(row1 < Rows);
|
||||
assert(row2 >= 0);
|
||||
assert(row2 < Rows);
|
||||
Swap(At(row1, 0), At(row2, 0));
|
||||
Swap(At(row1, 1), At(row2, 1));
|
||||
Swap(At(row1, 2), At(row2, 2));
|
||||
}
|
||||
|
||||
void Matrix3x3::SetIdentity() {
|
||||
Set(1,0,0,
|
||||
0,1,0,
|
||||
0,0,1);
|
||||
}
|
||||
|
||||
void Matrix3x3::SwapColumns(int col1, int col2) {
|
||||
assert(col1 >= 0);
|
||||
assert(col1 < Cols);
|
||||
assert(col2 >= 0);
|
||||
assert(col2 < Cols);
|
||||
Swap(At(0, col1), At(0, col2));
|
||||
Swap(At(1, col1), At(1, col2));
|
||||
Swap(At(2, col1), At(2, col2));
|
||||
}
|
||||
|
||||
void Matrix3x3::Set(const float *p) {
|
||||
assert(p);
|
||||
Set(p[0], p[1], p[2],
|
||||
p[3], p[4], p[5],
|
||||
p[6], p[7], p[8]);
|
||||
}
|
||||
|
||||
void
|
||||
Matrix3x3::Set(float _00, float _01, float _02, float _10, float _11, float _12, float _20, float _21, float _22) {
|
||||
At(0, 0) = _00; At(0, 1) = _01; At(0, 2) = _02;
|
||||
At(1, 0) = _10; At(1, 1) = _11; At(1, 2) = _12;
|
||||
At(2, 0) = _20; At(2, 1) = _21; At(2, 2) = _22;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@@ -1019,5 +1019,24 @@ namespace J3ML::LinearAlgebra {
|
||||
return true;
|
||||
}
|
||||
|
||||
void Matrix4x4::SetRotatePartX(float angleRadians) {
|
||||
Set3x3PartRotateX(*this, angleRadians);
|
||||
}
|
||||
|
||||
void Matrix4x4::SetRotatePartY(float angleRadians) {
|
||||
Set3x3PartRotateY(*this, angleRadians);
|
||||
}
|
||||
|
||||
void Matrix4x4::SetRotatePartZ(float angleRadians) {
|
||||
Set3x3PartRotateZ(*this, angleRadians);
|
||||
}
|
||||
|
||||
Matrix4x4::Matrix4x4(const Matrix3x3 &m) {
|
||||
Set(m.At(0,0), m.At(0,1), m.At(0,2), 0.f,
|
||||
m.At(1,0), m.At(1,1), m.At(1,2), 0.f,
|
||||
m.At(2,0), m.At(2,1), m.At(2,2), 0.f,
|
||||
0.f, 0.f, 0.f, 1.f);
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user