Mat*Vec mul

This commit is contained in:
bloeys
2021-11-05 07:07:01 +04:00
parent 5f43d7da88
commit 5980c50ced
6 changed files with 108 additions and 0 deletions

View File

@ -104,6 +104,16 @@ func MulMat2(m1, m2 *Mat2) *Mat2 {
} }
} }
//MulMat2Vec2 v2 = m1 * v1
func MulMat2Vec2(m1 *Mat2, v1 *Vec2) *Vec2 {
return &Vec2{
Data: [2]float32{
m1.Data[0]*v1.Data[0] + m1.Data[1]*v1.Data[1],
m1.Data[2]*v1.Data[0] + m1.Data[3]*v1.Data[1],
},
}
}
//NewMat2Id returns the 2x2 identity matrix //NewMat2Id returns the 2x2 identity matrix
func NewMat2Id() *Mat2 { func NewMat2Id() *Mat2 {
return &Mat2{ return &Mat2{

24
gglm/mat2_test.go Executable file
View File

@ -0,0 +1,24 @@
package gglm_test
import (
"testing"
"github.com/bloeys/gglm/gglm"
)
func TestMulMat2Vec2(t *testing.T) {
m := &gglm.Mat2{
Data: [4]float32{
1, 2,
3, 4,
}}
v := &gglm.Vec2{Data: [2]float32{1, 2}}
result := gglm.MulMat2Vec2(m, v)
correctAns := gglm.Vec2{Data: [2]float32{5, 11}}
if !result.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", result.String(), correctAns.String())
}
}

View File

@ -163,6 +163,17 @@ func MulMat3(m1, m2 *Mat3) *Mat3 {
} }
} }
//MulMat3Vec3 v2 = m1 * v1
func MulMat3Vec3(m1 *Mat3, v1 *Vec3) *Vec3 {
return &Vec3{
Data: [3]float32{
m1.Data[0]*v1.Data[0] + m1.Data[1]*v1.Data[1] + m1.Data[2]*v1.Data[2],
m1.Data[3]*v1.Data[0] + m1.Data[4]*v1.Data[1] + m1.Data[5]*v1.Data[2],
m1.Data[6]*v1.Data[0] + m1.Data[7]*v1.Data[1] + m1.Data[8]*v1.Data[2],
},
}
}
//NewMat3Id returns the 3x3 identity matrix //NewMat3Id returns the 3x3 identity matrix
func NewMat3Id() *Mat3 { func NewMat3Id() *Mat3 {
return &Mat3{ return &Mat3{

25
gglm/mat3_test.go Executable file
View File

@ -0,0 +1,25 @@
package gglm_test
import (
"testing"
"github.com/bloeys/gglm/gglm"
)
func TestMulMat3Vec3(t *testing.T) {
m := &gglm.Mat3{
Data: [9]float32{
1, 2, 3,
4, 5, 6,
7, 8, 9,
}}
v := &gglm.Vec3{Data: [3]float32{1, 2, 3}}
result := gglm.MulMat3Vec3(m, v)
correctAns := gglm.Vec3{Data: [3]float32{14, 32, 50}}
if !result.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", result.String(), correctAns.String())
}
}

View File

@ -234,6 +234,18 @@ func MulMat4(m1, m2 *Mat4) *Mat4 {
} }
} }
//MulMat4Vec4 v2 = m1 * v1
func MulMat4Vec4(m1 *Mat4, v1 *Vec4) *Vec4 {
return &Vec4{
Data: [4]float32{
m1.Data[0]*v1.Data[0] + m1.Data[1]*v1.Data[1] + m1.Data[2]*v1.Data[2] + m1.Data[3]*v1.Data[3],
m1.Data[4]*v1.Data[0] + m1.Data[5]*v1.Data[1] + m1.Data[6]*v1.Data[2] + m1.Data[7]*v1.Data[3],
m1.Data[8]*v1.Data[0] + m1.Data[9]*v1.Data[1] + m1.Data[10]*v1.Data[2] + m1.Data[11]*v1.Data[3],
m1.Data[12]*v1.Data[0] + m1.Data[13]*v1.Data[1] + m1.Data[14]*v1.Data[2] + m1.Data[15]*v1.Data[3],
},
}
}
//NewMat4Id returns the 4x4 identity matrix //NewMat4Id returns the 4x4 identity matrix
func NewMat4Id() *Mat4 { func NewMat4Id() *Mat4 {
return &Mat4{ return &Mat4{

26
gglm/mat4_test.go Executable file
View File

@ -0,0 +1,26 @@
package gglm_test
import (
"testing"
"github.com/bloeys/gglm/gglm"
)
func TestMulMat4Vec4(t *testing.T) {
m := &gglm.Mat4{
Data: [16]float32{
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16,
}}
v := &gglm.Vec4{Data: [4]float32{1, 2, 3, 4}}
result := gglm.MulMat4Vec4(m, v)
correctAns := gglm.Vec4{Data: [4]float32{30, 70, 110, 150}}
if !result.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", result.String(), correctAns.String())
}
}