Implement Matrix3x3 FromScale and ScaleBy

This commit is contained in:
2024-02-01 21:02:18 -05:00
parent 792f7801bb
commit d37b685df9
3 changed files with 53 additions and 18 deletions

View File

@@ -61,16 +61,7 @@ namespace LinearAlgebra {
void SetColumn(int i, const Vector3& vector);
void SetAt(int x, int y, float value);
void Orthonormalize(int c0, int c1, int c2)
{
Vector3 v0 = GetColumn(c0);
Vector3 v1 = GetColumn(c1);
Vector3 v2 = GetColumn(c2);
Vector3::Orthonormalize(v0, v1, v2);
SetColumn(c0, v0);
SetColumn(c1, v1);
SetColumn(c2, v2);
}
void Orthonormalize(int c0, int c1, int c2);
static Matrix3x3 LookAt(const Vector3& forward, const Vector3& target, const Vector3& localUp, const Vector3& worldUp);
@@ -90,8 +81,8 @@ namespace LinearAlgebra {
/// Creates a new transformation matrix that scales by the given factors.
// This matrix scales with respect to origin.
static Matrix3x3 Scale(float sx, float sy, float sz);
static Matrix3x3 Scale(const Vector3& scale);
static Matrix3x3 FromScale(float sx, float sy, float sz);
static Matrix3x3 FromScale(const Vector3& scale);
/// Returns the main diagonal.
Vector3 Diagonal() const;
@@ -124,10 +115,11 @@ namespace LinearAlgebra {
Vector2 Transform(const Vector2& rhs) const;
Vector3 Transform(const Vector3& rhs) const;
Vector3 operator[](int row) const
{
return Vector3{elems[row][0], elems[row][1], elems[row][2]};
}
Matrix3x3 ScaleBy(const Vector3& rhs);
Vector3 GetScale() const;
Vector3 operator[](int row) const;
Vector3 operator * (const Vector3& rhs) const;
Matrix3x3 operator * (const Matrix3x3& rhs) const;

View File

@@ -118,6 +118,11 @@ namespace LinearAlgebra {
Vector3 GetRow3(int index) const;
Vector3 GetColumn3(int index) const;
Vector3 GetScale() const
{
}
float &At(int row, int col);
float At(int x, int y) const;

View File

@@ -313,16 +313,54 @@ namespace LinearAlgebra {
}
Matrix3x3 Matrix3x3::FromRS(const Quaternion &rotate, const Vector3 &scale) {
return Matrix3x3(rotate) * Matrix3x3::Scale(scale);
return Matrix3x3(rotate) * Matrix3x3::FromScale(scale);
}
Matrix3x3 Matrix3x3::FromRS(const Matrix3x3 &rotate, const Vector3 &scale) {
return rotate * Matrix3x3::Scale(scale);
return rotate * Matrix3x3::FromScale(scale);
}
Matrix3x3 Matrix3x3::FromQuat(const Quaternion &orientation) {
return Matrix3x3(orientation);
}
Matrix3x3 Matrix3x3::ScaleBy(const Vector3 &rhs) {
return *this * FromScale(rhs);
}
Vector3 Matrix3x3::GetScale() const {
return Vector3(GetColumn(0).Length(), GetColumn(1).Length(), GetColumn(2).Length());
}
Vector3 Matrix3x3::operator[](int row) const {
return Vector3{elems[row][0], elems[row][1], elems[row][2]};
}
Matrix3x3 Matrix3x3::FromScale(const Vector3 &scale) {
Matrix3x3 m;
m.At(0,0) = scale.x;
m.At(1,1) = scale.y;
m.At(2,2) = scale.z;
return m;
}
Matrix3x3 Matrix3x3::FromScale(float sx, float sy, float sz) {
Matrix3x3 m;
m.At(0,0) = sx;
m.At(1,1) = sy;
m.At(2,2) = sz;
return m;
}
void Matrix3x3::Orthonormalize(int c0, int c1, int c2) {
Vector3 v0 = GetColumn(c0);
Vector3 v1 = GetColumn(c1);
Vector3 v2 = GetColumn(c2);
Vector3::Orthonormalize(v0, v1, v2);
SetColumn(c0, v0);
SetColumn(c1, v1);
SetColumn(c2, v2);
}
}