test_lmatrix4.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import pytest
  2. from copy import copy
  3. from panda3d import core
  4. def test_mat4_aliases():
  5. assert core.LMatrix4 is core.Mat4
  6. assert core.LMatrix4f is core.Mat4F
  7. assert core.LMatrix4d is core.Mat4D
  8. assert (core.LMatrix4f is core.Mat4) != (core.LMatrix4d is core.Mat4)
  9. @pytest.mark.parametrize("type", (core.LMatrix4d, core.LMatrix4f))
  10. def test_mat4_constructor(type):
  11. # Test that three ways of construction produce the same matrix.
  12. mat1 = type((1, 2, 3, 4),
  13. (5, 6, 7, 8),
  14. (9, 10, 11, 12),
  15. (13, 14, 15, 16))
  16. mat2 = type(1, 2, 3, 4,
  17. 5, 6, 7, 8,
  18. 9, 10, 11, 12,
  19. 13, 14, 15, 16)
  20. mat3 = type((1, 2, 3, 4,
  21. 5, 6, 7, 8,
  22. 9, 10, 11, 12,
  23. 13, 14, 15, 16))
  24. assert mat1 == mat2
  25. assert mat2 == mat3
  26. assert mat1 == mat3
  27. @pytest.mark.parametrize("type", (core.LMatrix4d, core.LMatrix4f))
  28. def test_mat4_copy_constuctor(type):
  29. mat1 = type((1, 2, 3, 4),
  30. (5, 6, 7, 8),
  31. (9, 10, 11, 12),
  32. (13, 14, 15, 16))
  33. # Make a copy. Changing it should not change the original.
  34. mat2 = type(mat1)
  35. assert mat1 == mat2
  36. mat2[0][0] = 100
  37. assert mat1 != mat2
  38. # Make a copy by unpacking.
  39. mat2 = type(*mat1)
  40. assert mat1 == mat2
  41. mat2[0][0] = 100
  42. assert mat1 != mat2
  43. # Make a copy by calling copy.copy.
  44. mat2 = copy(mat1)
  45. assert mat1 == mat2
  46. mat2[0][0] = 100
  47. assert mat1 != mat2
  48. @pytest.mark.parametrize("type", (core.LMatrix4d, core.LMatrix4f))
  49. def test_mat4_invert_same_type(type):
  50. mat = type((1, 0, 0, 0,
  51. 0, 1, 0, 0,
  52. 0, 0, 1, 0,
  53. 1, 2, 3, 1))
  54. inv = core.invert(mat)
  55. assert mat.__class__ == inv.__class__
  56. @pytest.mark.parametrize("type", (core.LMatrix4d, core.LMatrix4f))
  57. def test_mat4_invert_correct(type):
  58. mat = type((1, 0, 0, 0,
  59. 0, 1, 0, 0,
  60. 0, 0, 1, 0,
  61. 1, 2, 3, 1))
  62. inv = type()
  63. assert inv.invert_from(mat)
  64. assert inv == type(( 1, 0, 0, 0,
  65. 0, 1, 0, 0,
  66. 0, 0, 1, 0,
  67. -1, -2, -3, 1))
  68. assert (mat * inv).is_identity()
  69. assert (inv * mat).is_identity()
  70. @pytest.mark.parametrize("type", (core.LMatrix4d, core.LMatrix4f))
  71. def test_mat4_rows(type):
  72. mat = type((1, 2, 3, 4,
  73. 5, 6, 7, 8,
  74. 9, 10, 11, 12,
  75. 13, 14, 15, 16))
  76. assert mat.rows[0] == (1, 2, 3, 4)
  77. assert mat.rows[1] == (5, 6, 7, 8)
  78. assert mat.rows[2] == (9, 10, 11, 12)
  79. assert mat.rows[3] == (13, 14, 15, 16)
  80. assert mat.get_row3(0) == (1, 2, 3)
  81. assert mat.get_row3(1) == (5, 6, 7)
  82. assert mat.get_row3(2) == (9, 10, 11)
  83. assert mat.get_row3(3) == (13, 14, 15)
  84. @pytest.mark.parametrize("type", (core.LMatrix4d, core.LMatrix4f))
  85. def test_mat4_cols(type):
  86. mat = type((1, 5, 9, 13,
  87. 2, 6, 10, 14,
  88. 3, 7, 11, 15,
  89. 4, 8, 12, 16))
  90. assert mat.cols[0] == (1, 2, 3, 4)
  91. assert mat.cols[1] == (5, 6, 7, 8)
  92. assert mat.cols[2] == (9, 10, 11, 12)
  93. assert mat.cols[3] == (13, 14, 15, 16)
  94. assert mat.get_col3(0) == (1, 2, 3)
  95. assert mat.get_col3(1) == (5, 6, 7)
  96. assert mat.get_col3(2) == (9, 10, 11)
  97. assert mat.get_col3(3) == (13, 14, 15)