Debian Codegraph @

exp/f32/affine_test.go

raw
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package f32
6
7 import (
8 "math"
9 "testing"
10 )
11
12 var xyTests = []struct {
13 x, y float32
14 }{
15 {0, 0},
16 {1, 1},
17 {2, 3},
18 {6.5, 4.3},
19 }
20
21 var a = Affine{
22 {3, 4, 5},
23 {6, 7, 8},
24 }
25
26 func TestInverse(t *testing.T) {
27 wantInv := Affine{
28 {-2.33333, 1.33333, 1},
29 {2, -1, -2},
30 }
31 var gotInv Affine
32 gotInv.Inverse(&a)
33 if !gotInv.Eq(&wantInv, 0.01) {
34 t.Errorf("Inverse: got %s want %s", gotInv, wantInv)
35 }
36
37 var wantId, gotId Affine
38 wantId.Identity()
39 gotId.Mul(&a, &wantInv)
40 if !gotId.Eq(&wantId, 0.01) {
41 t.Errorf("Identity #0: got %s want %s", gotId, wantId)
42 }
43 gotId.Mul(&wantInv, &a)
44 if !gotId.Eq(&wantId, 0.01) {
45 t.Errorf("Identity #1: got %s want %s", gotId, wantId)
46 }
47 }
48
49 func TestAffineScale(t *testing.T) {
50 for _, test := range xyTests {
51 want := a
52 want.Mul(&want, &Affine{{test.x, 0, 0}, {0, test.y, 0}})
53 got := a
54 got.Scale(&got, test.x, test.y)
55
56 if !got.Eq(&want, 0.01) {
57 t.Errorf("(%.2f, %.2f): got %s want %s", test.x, test.y, got, want)
58 }
59 }
60 }
61
62 func TestAffineTranslate(t *testing.T) {
63 for _, test := range xyTests {
64 want := a
65 want.Mul(&want, &Affine{{1, 0, test.x}, {0, 1, test.y}})
66 got := a
67 got.Translate(&got, test.x, test.y)
68
69 if !got.Eq(&want, 0.01) {
70 t.Errorf("(%.2f, %.2f): got %s want %s", test.x, test.y, got, want)
71 }
72 }
73
74 }
75
76 func TestAffineRotate(t *testing.T) {
77 want := Affine{
78 {-4.000, 3.000, 5.000},
79 {-7.000, 6.000, 8.000},
80 }
81 got := a
82 got.Rotate(&got, math.Pi/2)
83 if !got.Eq(&want, 0.01) {
84 t.Errorf("rotate π: got %s want %s", got, want)
85 }
86
87 want = a
88 got = a
89 got.Rotate(&got, 2*math.Pi)
90 if !got.Eq(&want, 0.01) {
91 t.Errorf("rotate 2π: got %s want %s", got, want)
92 }
93
94 got = a
95 got.Rotate(&got, math.Pi)
96 got.Rotate(&got, math.Pi)
97 if !got.Eq(&want, 0.01) {
98 t.Errorf("rotate π then π: got %s want %s", got, want)
99 }
100
101 got = a
102 got.Rotate(&got, math.Pi/3)
103 got.Rotate(&got, -math.Pi/3)
104 if !got.Eq(&want, 0.01) {
105 t.Errorf("rotate π/3 then -π/3: got %s want %s", got, want)
106 }
107 }
108
109 func TestAffineScaleTranslate(t *testing.T) {
110 mulVec := func(m *Affine, v [2]float32) (mv [2]float32) {
111 mv[0] = m[0][0]*v[0] + m[0][1]*v[1] + m[0][2]
112 mv[1] = m[1][0]*v[0] + m[1][1]*v[1] + m[1][2]
113 return mv
114 }
115 v := [2]float32{1, 10}
116
117 var sThenT Affine
118 sThenT.Identity()
119 sThenT.Scale(&sThenT, 13, 17)
120 sThenT.Translate(&sThenT, 101, 151)
121 wantSTT := [2]float32{
122 13 * (101 + 1),
123 17 * (151 + 10),
124 }
125 if got := mulVec(&sThenT, v); got != wantSTT {
126 t.Errorf("S then T: got %v, want %v", got, wantSTT)
127 }
128
129 var tThenS Affine
130 tThenS.Identity()
131 tThenS.Translate(&tThenS, 101, 151)
132 tThenS.Scale(&tThenS, 13, 17)
133 wantTTS := [2]float32{
134 101 + (13 * 1),
135 151 + (17 * 10),
136 }
137 if got := mulVec(&tThenS, v); got != wantTTS {
138 t.Errorf("T then S: got %v, want %v", got, wantTTS)
139 }
140 }