affine: Refactoring

This commit is contained in:
Hajime Hoshi 2020-07-04 01:33:28 +09:00
parent 94b5747806
commit 2e8e35750d

View File

@ -88,14 +88,14 @@ func TestColorMIsInvertible(t *testing.T) {
m = m.SetElement(1, 2, .5) m = m.SetElement(1, 2, .5)
m = m.SetElement(1, 3, .5) m = m.SetElement(1, 3, .5)
m = m.SetElement(1, 4, .5) m = m.SetElement(1, 4, .5)
cidentity := &ColorM{} cidentity := &ColorM{}
//
cinvalid := &ColorM{} cinvalid := &ColorM{}
cinvalid = cinvalid.SetElement(0, 0, 0) cinvalid = cinvalid.SetElement(0, 0, 0)
cinvalid = cinvalid.SetElement(1, 1, 0) cinvalid = cinvalid.SetElement(1, 1, 0)
cinvalid = cinvalid.SetElement(2, 2, 0) cinvalid = cinvalid.SetElement(2, 2, 0)
cinvalid = cinvalid.SetElement(3, 3, 0) cinvalid = cinvalid.SetElement(3, 3, 0)
//
cases := []struct { cases := []struct {
In *ColorM In *ColorM
Out bool Out bool
@ -126,66 +126,64 @@ func TestColorMIsInvertible(t *testing.T) {
} }
} }
func arrayToColorM(es [4][5]float32) *ColorM {
var a = &ColorM{}
for j := 0; j < 5; j++ {
for i := 0; i < 4; i++ {
a = a.SetElement(i, j, es[i][j])
}
}
return a
}
func TestColorMInvert(t *testing.T) { func TestColorMInvert(t *testing.T) {
var m [5][4]float32 cases := []struct {
In *ColorM
m = [5][4]float32{ Out *ColorM
{1, 8, -9, 7}, }{
{0, 1, 0, 4}, {
{0, 0, 1, 2}, In: nil,
{0, 0, 0, 1}, Out: nil,
{0, 0, 0, 0}, },
} {
a := &ColorM{} In: arrayToColorM([4][5]float32{
for j := 0; j < 5; j++ { {1, 0, 0, 0, 0},
for i := 0; i < 4; i++ { {8, 1, 0, 0, 0},
a = a.SetElement(i, j, m[j][i]) {-9, 0, 1, 0, 0},
} {7, 4, 2, 1, 0},
}),
Out: arrayToColorM([4][5]float32{
{1, 0, 0, 0, 0},
{-8, 1, 0, 0, 0},
{9, 0, 1, 0, 0},
{7, -4, -2, 1, 0},
}),
},
} }
m = [5][4]float32{ for _, c := range cases {
{1, -8, 9, 7}, if got, want := c.In.Invert(), c.Out; !got.Equals(want) {
{0, 1, 0, -4}, t.Errorf("got: %v, want: %v", got, want)
{0, 0, 1, -2},
{0, 0, 0, 1},
{0, 0, 0, 0},
} }
ia := &ColorM{}
for j := 0; j < 5; j++ {
for i := 0; i < 4; i++ {
ia = ia.SetElement(i, j, m[j][i])
}
}
ia2 := a.Invert()
if !ia.Equals(ia2) {
t.Fail()
} }
} }
func BenchmarkColorMInvert(b *testing.B) { func BenchmarkColorMInvert(b *testing.B) {
r := rand.Float32
b.StopTimer() b.StopTimer()
m := &ColorM{} var m *ColorM
m = m.SetElement(1, 0, rand.Float32()) for m == nil || !m.IsInvertible() {
m = m.SetElement(2, 0, rand.Float32()) m = arrayToColorM([4][5]float32{
m = m.SetElement(3, 0, rand.Float32()) {r(), r(), r(), r(), r() * 10},
m = m.SetElement(0, 1, rand.Float32()) {r(), r(), r(), r(), r() * 10},
m = m.SetElement(2, 1, rand.Float32()) {r(), r(), r(), r(), r() * 10},
m = m.SetElement(3, 1, rand.Float32()) {r(), r(), r(), r(), r() * 10},
m = m.SetElement(0, 2, rand.Float32()) })
m = m.SetElement(1, 2, rand.Float32()) }
m = m.SetElement(3, 2, rand.Float32())
m = m.SetElement(0, 3, rand.Float32())
m = m.SetElement(1, 3, rand.Float32())
m = m.SetElement(2, 3, rand.Float32())
m = m.SetElement(0, 4, rand.Float32()*10)
m = m.SetElement(1, 4, rand.Float32()*10)
m = m.SetElement(2, 4, rand.Float32()*10)
m = m.SetElement(3, 4, rand.Float32()*10)
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if m.IsInvertible() {
m = m.Invert() m = m.Invert()
} }
}
} }