Skip to content

Commit

Permalink
Restore Mat4x4.SetApplyTransform
Browse files Browse the repository at this point in the history
  • Loading branch information
CannibalVox committed Dec 22, 2022
1 parent cb0c7d7 commit 2d6f153
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
4 changes: 4 additions & 0 deletions mat3x3.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ func (m *Mat3x3[T]) SetMultMat3x3(lhs, rhs *Mat3x3[T]) {
// SetApplyTransform applies the right transform matrix to the left transform matrix and overwrites
// the current contents of this matrix with the result. "ApplyTransform" is just a matrix multiply
// with the operands reversed.
//
// lhs - The matrix having a transform applied to it
//
// rhs - The transform being applied
func (m *Mat3x3[T]) SetApplyTransform(lhs, rhs *Mat3x3[T]) {
m00 := rhs[0][0]*lhs[0][0] + rhs[1][0]*lhs[0][1] + rhs[2][0]*lhs[0][2]
m10 := rhs[0][0]*lhs[1][0] + rhs[1][0]*lhs[1][1] + rhs[2][0]*lhs[1][2]
Expand Down
28 changes: 27 additions & 1 deletion mat4x4.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ func (m *Mat4x4[T]) SetLookAt(eyePosition *Vec3[T], target *Vec3[T], up *Vec3[T]
// SetMultMat4x4 multiplies two 4x4 matrices together and overwrites the current contents
// of this matrix with the results. In Matrix Multiplication between transform matrices,
// the left matrix is unintuitively applied to the right matrix rather than the other way around.
// You may prefer to use ApplyTransform.
// You may prefer to use SetApplyTransform.
//
// lhs - The left operand of the multiplication operation
// rhs - The right operand of the multiplication operation
Expand All @@ -679,6 +679,32 @@ func (m *Mat4x4[T]) SetMultMat4x4(lhs, rhs *Mat4x4[T]) {
m[3][3] = lhs[0][3]*rhs[3][0] + lhs[1][3]*rhs[3][1] + lhs[2][3]*rhs[3][2] + lhs[3][3]*rhs[3][3]
}

// SetApplyTransform applies the right transform matrix to the left transform matrix and overwrites
// the current contents of this matrix with the result. "ApplyTransform" is just a matrix multiply
// with the operands reversed.
//
// lhs - The matrix having a transform applied to it
//
// rhs - The transform being applied
func (m *Mat4x4[T]) SetApplyTransform(lhs, rhs *Mat4x4[T]) {
m[0][0] = rhs[0][0]*lhs[0][0] + rhs[1][0]*lhs[0][1] + rhs[2][0]*lhs[0][2] + rhs[3][0]*lhs[0][3]
m[0][1] = rhs[0][1]*lhs[0][0] + rhs[1][1]*lhs[0][1] + rhs[2][1]*lhs[0][2] + rhs[3][1]*lhs[0][3]
m[0][2] = rhs[0][2]*lhs[0][0] + rhs[1][2]*lhs[0][1] + rhs[2][2]*lhs[0][2] + rhs[3][2]*lhs[0][3]
m[0][3] = rhs[0][3]*lhs[0][0] + rhs[1][3]*lhs[0][1] + rhs[2][3]*lhs[0][2] + rhs[3][3]*lhs[0][3]
m[1][0] = rhs[0][0]*lhs[1][0] + rhs[1][0]*lhs[1][1] + rhs[2][0]*lhs[1][2] + rhs[3][0]*lhs[1][3]
m[1][1] = rhs[0][1]*lhs[1][0] + rhs[1][1]*lhs[1][1] + rhs[2][1]*lhs[1][2] + rhs[3][1]*lhs[1][3]
m[1][2] = rhs[0][2]*lhs[1][0] + rhs[1][2]*lhs[1][1] + rhs[2][2]*lhs[1][2] + rhs[3][2]*lhs[1][3]
m[1][3] = rhs[0][3]*lhs[1][0] + rhs[1][3]*lhs[1][1] + rhs[2][3]*lhs[1][2] + rhs[3][3]*lhs[1][3]
m[2][0] = rhs[0][0]*lhs[2][0] + rhs[1][0]*lhs[2][1] + rhs[2][0]*lhs[2][2] + rhs[3][0]*lhs[2][3]
m[2][1] = rhs[0][1]*lhs[2][0] + rhs[1][1]*lhs[2][1] + rhs[2][1]*lhs[2][2] + rhs[3][1]*lhs[2][3]
m[2][2] = rhs[0][2]*lhs[2][0] + rhs[1][2]*lhs[2][1] + rhs[2][2]*lhs[2][2] + rhs[3][2]*lhs[2][3]
m[2][3] = rhs[0][3]*lhs[2][0] + rhs[1][3]*lhs[2][1] + rhs[2][3]*lhs[2][2] + rhs[3][3]*lhs[2][3]
m[3][0] = rhs[0][0]*lhs[3][0] + rhs[1][0]*lhs[3][1] + rhs[2][0]*lhs[3][2] + rhs[3][0]*lhs[3][3]
m[3][1] = rhs[0][1]*lhs[3][0] + rhs[1][1]*lhs[3][1] + rhs[2][1]*lhs[3][2] + rhs[3][1]*lhs[3][3]
m[3][2] = rhs[0][2]*lhs[3][0] + rhs[1][2]*lhs[3][1] + rhs[2][2]*lhs[3][2] + rhs[3][2]*lhs[3][3]
m[3][3] = rhs[0][3]*lhs[3][0] + rhs[1][3]*lhs[3][1] + rhs[2][3]*lhs[3][2] + rhs[3][3]*lhs[3][3]
}

// SetInterpolateMat4x4 overwrites the current contents of this matrix with a transform
// matrix created by interpolating between the rotation and translation of two transform
// matrices.
Expand Down
46 changes: 34 additions & 12 deletions mat4x4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,14 @@ func TestMat4x4_RotateAroundAxis_Translate(t *testing.T) {

func TestMat4x4_MultMatrix4x4(t *testing.T) {
var mat1 Mat4x4[float32]
mat1.SetRotationY(math.Pi / 2.0)
mat1.SetTranslation(1, 2, 3)

var mat2 Mat4x4[float32]
mat2.SetRotationY(math.Pi / 2.0)
mat2.SetScale(2, 2, 2)

var expected Mat4x4[float32]
expected.SetRotationY(math.Pi)
expected.SetScale(2, 2, 2)
expected.Translate(1, 2, 3)

mat1.MultMat4x4(&mat2)

Expand All @@ -602,13 +603,14 @@ func TestMat4x4_MultMatrix4x4(t *testing.T) {

func TestMat4x4_SetMultMatrix4x4(t *testing.T) {
var mat1 Mat4x4[float32]
mat1.SetRotationY(math.Pi / 2.0)
mat1.SetTranslation(1, 2, 3)

var mat2 Mat4x4[float32]
mat2.SetRotationY(math.Pi / 2.0)
mat2.SetScale(2, 2, 2)

var expected Mat4x4[float32]
expected.SetRotationY(math.Pi)
expected.SetScale(2, 2, 2)
expected.Translate(1, 2, 3)

var result Mat4x4[float32]
result.SetMultMat4x4(&mat1, &mat2)
Expand All @@ -618,6 +620,25 @@ func TestMat4x4_SetMultMatrix4x4(t *testing.T) {
require.False(t, mat2.Equal(&expected, 0.0001))
}

func TestMat4x4_SetApplyTransform(t *testing.T) {
var mat1 Mat4x4[float32]
mat1.SetTranslation(1, 2, 3)

var mat2 Mat4x4[float32]
mat2.SetScale(2, 2, 2)

var expected Mat4x4[float32]
expected.SetScale(2, 2, 2)
expected.Translate(1, 2, 3)

var result Mat4x4[float32]
result.SetApplyTransform(&mat2, &mat1)

require.True(t, result.Equal(&expected, 0.0001))
require.False(t, mat1.Equal(&expected, 0.0001))
require.False(t, mat2.Equal(&expected, 0.0001))
}

func TestMat4x4_InterpolateMatrix(t *testing.T) {
var mat1 Mat4x4[float32]
mat1.SetIdentity()
Expand Down Expand Up @@ -664,16 +685,17 @@ func TestMat4x4_IsNormalized(t *testing.T) {

func TestMat4x4_ApplyTransform(t *testing.T) {
var mat1 Mat4x4[float32]
mat1.SetRotationY(math.Pi / 2.0)
mat1.SetTranslation(1, 2, 3)

var mat2 Mat4x4[float32]
mat2.SetRotationY(math.Pi / 2.0)
mat2.SetScale(2, 2, 2)

var expected Mat4x4[float32]
expected.SetRotationY(math.Pi)
expected.SetScale(2, 2, 2)
expected.Translate(1, 2, 3)

mat1.ApplyTransform(&mat2)
mat2.ApplyTransform(&mat1)

require.True(t, mat1.Equal(&expected, 0.0001))
require.False(t, mat2.Equal(&expected, 0.0001))
require.False(t, mat1.Equal(&expected, 0.0001))
require.True(t, mat2.Equal(&expected, 0.0001))
}

0 comments on commit 2d6f153

Please sign in to comment.