affine_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package f32
  2. import (
  3. "math"
  4. "testing"
  5. )
  6. var xyTests = []struct {
  7. x, y float32
  8. }{
  9. {0, 0},
  10. {1, 1},
  11. {2, 3},
  12. {6.5, 4.3},
  13. }
  14. var a = Affine{
  15. {3, 4, 5},
  16. {6, 7, 8},
  17. }
  18. func TestInverse(t *testing.T) {
  19. wantInv := Affine{
  20. {-2.33333, 1.33333, 1},
  21. {2, -1, -2},
  22. }
  23. var gotInv Affine
  24. gotInv.Inverse(&a)
  25. if !gotInv.Eq(&wantInv, 0.01) {
  26. t.Errorf("Inverse: got %s want %s", gotInv, wantInv)
  27. }
  28. var wantId, gotId Affine
  29. wantId.Identity()
  30. gotId.Mul(&a, &wantInv)
  31. if !gotId.Eq(&wantId, 0.01) {
  32. t.Errorf("Identity #0: got %s want %s", gotId, wantId)
  33. }
  34. gotId.Mul(&wantInv, &a)
  35. if !gotId.Eq(&wantId, 0.01) {
  36. t.Errorf("Identity #1: got %s want %s", gotId, wantId)
  37. }
  38. }
  39. func TestAffineScale(t *testing.T) {
  40. for _, test := range xyTests {
  41. want := a
  42. want.Mul(&want, &Affine{{test.x, 0, 0}, {0, test.y, 0}})
  43. got := a
  44. got.Scale(&got, test.x, test.y)
  45. if !got.Eq(&want, 0.01) {
  46. t.Errorf("(%.2f, %.2f): got %s want %s", test.x, test.y, got, want)
  47. }
  48. }
  49. }
  50. func TestAffineTranslate(t *testing.T) {
  51. for _, test := range xyTests {
  52. want := a
  53. want.Mul(&want, &Affine{{1, 0, test.x}, {0, 1, test.y}})
  54. got := a
  55. got.Translate(&got, test.x, test.y)
  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. func TestAffineRotate(t *testing.T) {
  62. want := Affine{
  63. {-4.000, 3.000, 5.000},
  64. {-7.000, 6.000, 8.000},
  65. }
  66. got := a
  67. got.Rotate(&got, math.Pi/2)
  68. if !got.Eq(&want, 0.01) {
  69. t.Errorf("rotate π: got %s want %s", got, want)
  70. }
  71. want = a
  72. got = a
  73. got.Rotate(&got, 2*math.Pi)
  74. if !got.Eq(&want, 0.01) {
  75. t.Errorf("rotate 2π: got %s want %s", got, want)
  76. }
  77. got = a
  78. got.Rotate(&got, math.Pi)
  79. got.Rotate(&got, math.Pi)
  80. if !got.Eq(&want, 0.01) {
  81. t.Errorf("rotate π then π: got %s want %s", got, want)
  82. }
  83. got = a
  84. got.Rotate(&got, math.Pi/3)
  85. got.Rotate(&got, -math.Pi/3)
  86. if !got.Eq(&want, 0.01) {
  87. t.Errorf("rotate π/3 then -π/3: got %s want %s", got, want)
  88. }
  89. }
  90. func TestAffineScaleTranslate(t *testing.T) {
  91. mulVec := func(m *Affine, v [2]float32) (mv [2]float32) {
  92. mv[0] = m[0][0]*v[0] + m[0][1]*v[1] + m[0][2]
  93. mv[1] = m[1][0]*v[0] + m[1][1]*v[1] + m[1][2]
  94. return mv
  95. }
  96. v := [2]float32{1, 10}
  97. var sThenT Affine
  98. sThenT.Identity()
  99. sThenT.Scale(&sThenT, 13, 17)
  100. sThenT.Translate(&sThenT, 101, 151)
  101. wantSTT := [2]float32{
  102. 13 * (101 + 1),
  103. 17 * (151 + 10),
  104. }
  105. if got := mulVec(&sThenT, v); got != wantSTT {
  106. t.Errorf("S then T: got %v, want %v", got, wantSTT)
  107. }
  108. var tThenS Affine
  109. tThenS.Identity()
  110. tThenS.Translate(&tThenS, 101, 151)
  111. tThenS.Scale(&tThenS, 13, 17)
  112. wantTTS := [2]float32{
  113. 101 + (13 * 1),
  114. 151 + (17 * 10),
  115. }
  116. if got := mulVec(&tThenS, v); got != wantTTS {
  117. t.Errorf("T then S: got %v, want %v", got, wantTTS)
  118. }
  119. }