5 Commits

Author SHA1 Message Date
2bdd7b2d02 Mat4 tests 2021-11-05 07:39:55 +04:00
61b2298cb1 Mat2+Mat3 tests 2021-11-05 07:31:41 +04:00
62a2e4d7aa Mat2 tests 2021-11-05 07:22:57 +04:00
5980c50ced Mat*Vec mul 2021-11-05 07:07:01 +04:00
5f43d7da88 Vec normalize funcs 2021-11-05 06:43:17 +04:00
10 changed files with 557 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
func NewMat2Id() *Mat2 {
return &Mat2{

148
gglm/mat2_test.go Executable file
View File

@ -0,0 +1,148 @@
package gglm_test
import (
"testing"
"github.com/bloeys/gglm/gglm"
)
func TestMat2GetSet(t *testing.T) {
m1 := gglm.Mat2{}
m1.Set(0, 1, -10)
m1.Set(1, 0, 55)
if m1.Get(0, 1) != -10 {
t.Errorf("Got: %v; Expected: %v", m1.Get(0, 1), -10)
}
if m1.Get(1, 0) != 55 {
t.Errorf("Got: %v; Expected: %v", m1.Get(1, 0), 55)
}
}
func TestMat2Id(t *testing.T) {
correctAns := gglm.Mat2{
Data: [4]float32{
1, 0,
0, 1,
}}
m1 := gglm.NewMat2Id()
if !m1.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", m1.String(), correctAns.String())
}
}
func TestSubMat2(t *testing.T) {
correctAns := gglm.Mat2{
Data: [4]float32{
-4, -4,
-4, -4,
}}
m1 := &gglm.Mat2{
Data: [4]float32{
1, 2,
3, 4,
}}
m2 := &gglm.Mat2{
Data: [4]float32{
5, 6,
7, 8,
}}
result := gglm.SubMat2(m1, m2)
m1.Sub(m2)
if !result.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", result.String(), correctAns.String())
}
if !m1.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", m1.String(), correctAns.String())
}
}
func TestAddMat2(t *testing.T) {
correctAns := gglm.Mat2{
Data: [4]float32{
6, 8,
10, 12,
}}
m1 := &gglm.Mat2{
Data: [4]float32{
1, 2,
3, 4,
}}
m2 := &gglm.Mat2{
Data: [4]float32{
5, 6,
7, 8,
}}
result := gglm.AddMat2(m1, m2)
m1.Add(m2)
if !result.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", result.String(), correctAns.String())
}
if !m1.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", m1.String(), correctAns.String())
}
}
func TestMulMat2(t *testing.T) {
correctAns := gglm.Mat2{
Data: [4]float32{
19, 22,
43, 50,
}}
m1 := &gglm.Mat2{
Data: [4]float32{
1, 2,
3, 4,
}}
m2 := &gglm.Mat2{
Data: [4]float32{
5, 6,
7, 8,
}}
result := gglm.MulMat2(m1, m2)
m1.Mul(m2)
if !result.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", result.String(), correctAns.String())
}
if !m1.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", m1.String(), correctAns.String())
}
}
func TestMulMat2Vec2(t *testing.T) {
correctAns := gglm.Vec2{Data: [2]float32{5, 11}}
m := &gglm.Mat2{
Data: [4]float32{
1, 2,
3, 4,
}}
v := &gglm.Vec2{Data: [2]float32{1, 2}}
result := gglm.MulMat2Vec2(m, v)
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
func NewMat3Id() *Mat3 {
return &Mat3{

163
gglm/mat3_test.go Executable file
View File

@ -0,0 +1,163 @@
package gglm_test
import (
"testing"
"github.com/bloeys/gglm/gglm"
)
func TestMat3GetSet(t *testing.T) {
m1 := gglm.Mat3{}
m1.Set(0, 1, -10)
m1.Set(1, 0, 55)
m1.Set(2, 2, 99)
if m1.Get(0, 1) != -10 {
t.Errorf("Got: %v; Expected: %v", m1.Get(0, 1), -10)
}
if m1.Get(1, 0) != 55 {
t.Errorf("Got: %v; Expected: %v", m1.Get(1, 0), 55)
}
if m1.Get(2, 2) != 99 {
t.Errorf("Got: %v; Expected: %v", m1.Get(2, 2), 99)
}
}
func TestMat3Id(t *testing.T) {
correctAns := gglm.Mat3{
Data: [9]float32{
1, 0, 0,
0, 1, 0,
0, 0, 1,
}}
m1 := gglm.NewMat3Id()
if !m1.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", m1.String(), correctAns.String())
}
}
func TestSubMat3(t *testing.T) {
correctAns := gglm.Mat3{
Data: [9]float32{
-9, -9, -9,
-9, -9, -9,
-9, -9, -9,
}}
m1 := &gglm.Mat3{
Data: [9]float32{
1, 2, 3,
4, 5, 6,
7, 8, 9,
}}
m2 := &gglm.Mat3{
Data: [9]float32{
10, 11, 12,
13, 14, 15,
16, 17, 18,
}}
result := gglm.SubMat3(m1, m2)
m1.Sub(m2)
if !result.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", result.String(), correctAns.String())
}
if !m1.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", m1.String(), correctAns.String())
}
}
func TestAddMat3(t *testing.T) {
correctAns := gglm.Mat3{
Data: [9]float32{
11, 13, 15,
17, 19, 21,
23, 25, 27,
}}
m1 := &gglm.Mat3{
Data: [9]float32{
1, 2, 3,
4, 5, 6,
7, 8, 9,
}}
m2 := &gglm.Mat3{
Data: [9]float32{
10, 11, 12,
13, 14, 15,
16, 17, 18,
}}
result := gglm.AddMat3(m1, m2)
m1.Add(m2)
if !result.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", result.String(), correctAns.String())
}
if !m1.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", m1.String(), correctAns.String())
}
}
func TestMulMat3(t *testing.T) {
correctAns := gglm.Mat3{
Data: [9]float32{
84, 90, 96,
201, 216, 231,
318, 342, 366,
}}
m1 := &gglm.Mat3{
Data: [9]float32{
1, 2, 3,
4, 5, 6,
7, 8, 9,
}}
m2 := &gglm.Mat3{
Data: [9]float32{
10, 11, 12,
13, 14, 15,
16, 17, 18,
}}
result := gglm.MulMat3(m1, m2)
m1.Mul(m2)
if !result.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", result.String(), correctAns.String())
}
if !m1.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", m1.String(), correctAns.String())
}
}
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
func NewMat4Id() *Mat4 {
return &Mat4{

179
gglm/mat4_test.go Executable file
View File

@ -0,0 +1,179 @@
package gglm_test
import (
"testing"
"github.com/bloeys/gglm/gglm"
)
func TestMat4GetSet(t *testing.T) {
m1 := gglm.Mat4{}
m1.Set(0, 1, -10)
m1.Set(1, 0, 55)
m1.Set(2, 2, 99)
m1.Set(3, 3, 513)
if m1.Get(0, 1) != -10 {
t.Errorf("Got: %v; Expected: %v", m1.Get(0, 1), -10)
}
if m1.Get(1, 0) != 55 {
t.Errorf("Got: %v; Expected: %v", m1.Get(1, 0), 55)
}
if m1.Get(2, 2) != 99 {
t.Errorf("Got: %v; Expected: %v", m1.Get(2, 2), 99)
}
if m1.Get(3, 3) != 513 {
t.Errorf("Got: %v; Expected: %v", m1.Get(3, 3), 513)
}
}
func TestMat4Id(t *testing.T) {
correctAns := gglm.Mat4{
Data: [16]float32{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
}}
m1 := gglm.NewMat4Id()
if !m1.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", m1.String(), correctAns.String())
}
}
func TestSubMat4(t *testing.T) {
correctAns := gglm.Mat4{
Data: [16]float32{
-16, -16, -16, -16,
-16, -16, -16, -16,
-16, -16, -16, -16,
-16, -16, -16, -16,
}}
m1 := &gglm.Mat4{
Data: [16]float32{
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16,
}}
m2 := &gglm.Mat4{
Data: [16]float32{
17, 18, 19, 20,
21, 22, 23, 24,
25, 26, 27, 28,
29, 30, 31, 32,
}}
result := gglm.SubMat4(m1, m2)
m1.Sub(m2)
if !result.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", result.String(), correctAns.String())
}
if !m1.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", m1.String(), correctAns.String())
}
}
func TestAddMat4(t *testing.T) {
correctAns := gglm.Mat4{
Data: [16]float32{
18, 20, 22, 24,
26, 28, 30, 32,
34, 36, 38, 40,
42, 44, 46, 48,
}}
m1 := &gglm.Mat4{
Data: [16]float32{
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16,
}}
m2 := &gglm.Mat4{
Data: [16]float32{
17, 18, 19, 20,
21, 22, 23, 24,
25, 26, 27, 28,
29, 30, 31, 32,
}}
result := gglm.AddMat4(m1, m2)
m1.Add(m2)
if !result.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", result.String(), correctAns.String())
}
if !m1.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", m1.String(), correctAns.String())
}
}
func TestMulMat4(t *testing.T) {
correctAns := gglm.Mat4{
Data: [16]float32{
250, 260, 270, 280,
618, 644, 670, 696,
986, 1028, 1070, 1112,
1354, 1412, 1470, 1528,
}}
m1 := &gglm.Mat4{
Data: [16]float32{
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16,
}}
m2 := &gglm.Mat4{
Data: [16]float32{
17, 18, 19, 20,
21, 22, 23, 24,
25, 26, 27, 28,
29, 30, 31, 32,
}}
result := gglm.MulMat4(m1, m2)
m1.Mul(m2)
if !result.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", result.String(), correctAns.String())
}
if !m1.Eq(&correctAns) {
t.Errorf("Got: %v; Expected: %v", m1.String(), correctAns.String())
}
}
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())
}
}

View File

@ -87,6 +87,12 @@ func (v *Vec2) Set(x, y float32) {
v.Data[1] = y
}
func (v *Vec2) Normalize() {
mag := v.Mag()
v.Data[0] /= mag
v.Data[1] /= mag
}
//AddVec2 v3 = v1 + v2
func AddVec2(v1, v2 *Vec2) *Vec2 {
return &Vec2{

View File

@ -105,6 +105,13 @@ func (v *Vec3) Set(x, y, z float32) {
v.Data[2] = z
}
func (v *Vec3) Normalize() {
mag := float32(math.Sqrt(float64(v.X()*v.X() + v.Y()*v.Y() + v.Z()*v.Z())))
v.Data[0] /= mag
v.Data[1] /= mag
v.Data[2] /= mag
}
//AddVec3 v3 = v1 + v2
func AddVec3(v1, v2 *Vec3) *Vec3 {
return &Vec3{

View File

@ -125,6 +125,14 @@ func (v *Vec4) Set(x, y, z, w float32) {
v.Data[3] = w
}
func (v *Vec4) Normalize() {
mag := float32(math.Sqrt(float64(v.Data[0]*v.Data[0] + v.Data[1]*v.Data[1] + v.Data[2]*v.Data[2] + v.Data[3]*v.Data[3])))
v.Data[0] /= mag
v.Data[1] /= mag
v.Data[2] /= mag
v.Data[3] /= mag
}
//AddVec4 v3 = v1 + v2
func AddVec4(v1, v2 *Vec4) *Vec4 {
return &Vec4{

13
main.go
View File

@ -60,6 +60,10 @@ func main() {
v2.Set(1, 2)
println(v1.Eq(v2))
println("V1: " + v1.String())
v1.Normalize()
println("V1 Normal: " + v1.String())
//Vec3
v3 := &gglm.Vec3{Data: [3]float32{1, 2, 3}}
v4 := &gglm.Vec3{Data: [3]float32{4, 5, 6}}
@ -71,6 +75,11 @@ func main() {
println(v3.Eq(v4))
println(gglm.DotVec3(v3, v4))
println(gglm.Cross(v3, v4).String())
println("V3: " + v3.String())
v3.Normalize()
println("V3 Normal: " + v3.String())
//Vec4
v5 := &gglm.Vec4{Data: [4]float32{1, 2, 3, 4}}
@ -83,4 +92,8 @@ func main() {
println(v5.Eq(v6))
println(gglm.DotVec4(v5, v6))
println("V6: " + v6.String())
v6.Normalize()
println("V6 Normal: " + v6.String())
}