affine_test.go 2.8 KB

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