You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
4.7 KiB

  1. import Vector from "../src/05/vector";
  2. import Matrix from "../src/07/matrix";
  3. import { assert, expect } from 'chai';
  4. import { SingleEntryPlugin } from "webpack";
  5. describe('Matrix', () => {
  6. it('can be initialized with an array consisting of 16 numbers', () => {
  7. const m = new Matrix([
  8. 0, 0, 0, 0,
  9. 0, 0, 0, 0,
  10. 0, 0, 0, 0,
  11. 0, 0, 0, 1
  12. ]);
  13. expect(m).to.be.an('object');
  14. });
  15. it('single values can be set and retrieved', () => {
  16. const m = Matrix.identity();
  17. expect(m).has.property('setVal');
  18. expect(m).has.property('getVal');
  19. m.setVal(1, 2, 342);
  20. expect(m.getVal(1, 2)).to.equal(342);
  21. });
  22. it('all values can be retrieved', () => {
  23. const m = new Matrix([
  24. 0, 1, 2, 3,
  25. 4, 5, 6, 7,
  26. 8, 9, 10, 11,
  27. 12, 13, 14, 15
  28. ]);
  29. expect(m).has.property('getVals');
  30. var mat = m.getVals();
  31. assert.deepEqual(mat, [
  32. 0, 1, 2, 3,
  33. 4, 5, 6, 7,
  34. 8, 9, 10, 11,
  35. 12, 13, 14, 15
  36. ]);
  37. });
  38. it('translation works', () => {
  39. expect(Matrix).has.property('translation');
  40. const t = Matrix.translation(new Vector(1, 2, 3, 0));
  41. expect(t).to.not.be.null;
  42. expect(t).to.be.an('object');
  43. assert.deepEqual(t.getVals(), [
  44. 1, 0, 0, 1,
  45. 0, 1, 0, 2,
  46. 0, 0, 1, 3,
  47. 0, 0, 0, 1
  48. ]);
  49. });
  50. it('rotation around x-axis works', () => {
  51. expect(Matrix).has.property('rotation');
  52. const x = Matrix.rotation(new Vector(1, 0, 0, 0), Math.PI / 4);
  53. expect(x).to.not.be.null;
  54. expect(x).to.be.an('object');
  55. assert.deepEqual(x.getVals(), [
  56. 1, 0, 0, 0,
  57. 0, 0.7071067690849304, -0.7071067690849304, 0,
  58. 0, 0.7071067690849304, 0.7071067690849304, 0,
  59. 0, 0, 0, 1
  60. ]);
  61. });
  62. it('rotation around y-axis works', () => {
  63. expect(Matrix).has.property('rotation');
  64. const y = Matrix.rotation(new Vector(0, 1, 0, 0), -Math.PI / 4);
  65. expect(y).to.not.be.null;
  66. expect(y).to.be.an('object');
  67. assert.deepEqual(y.getVals(), [
  68. 0.7071067690849304, 0, -0.7071067690849304, 0,
  69. 0, 1, 0, 0,
  70. 0.7071067690849304, 0, 0.7071067690849304, 0,
  71. 0, 0, 0, 1
  72. ]);
  73. });
  74. it('rotation around z-axis works', () => {
  75. expect(Matrix).has.property('rotation');
  76. const z = Matrix.rotation(new Vector(0, 0, 1, 0), -Math.PI / 4);
  77. expect(z).to.not.be.null;
  78. expect(z).to.be.an('object');
  79. assert.deepEqual(z.getVals(), [
  80. 0.7071067690849304, 0.7071067690849304, 0, 0,
  81. -0.7071067690849304, 0.7071067690849304, 0, 0,
  82. 0, 0, 1, 0,
  83. 0, 0, 0, 1
  84. ]);
  85. });
  86. it('scaling matrix works', () => {
  87. expect(Matrix).has.property('scaling');
  88. const m = Matrix.scaling(new Vector(2, 3, 4, 0));
  89. expect(m).to.not.be.null;
  90. expect(m).to.be.an('object');
  91. assert.deepEqual(m.getVals(), [
  92. 2, 0, 0, 0,
  93. 0, 3, 0, 0,
  94. 0, 0, 4, 0,
  95. 0, 0, 0, 1
  96. ]);
  97. });
  98. it('matrix vector multiplication works', () => {
  99. let m1: Matrix = new Matrix([
  100. -1, 0, 0, 1,
  101. 0, -1, 0, 2,
  102. 0, 0, 1, 3,
  103. 0, 0, 0, 1 ]);
  104. let a = new Vector(0, 1, 0, 1);
  105. let b = new Vector(-1, 1, 0, 1);
  106. expect(m1).to.not.be.null;
  107. expect(m1).to.be.an('object');
  108. expect(m1).has.property('mulVec');
  109. let at = m1.mulVec(a);
  110. assert.deepEqual(at.valueOf(), [
  111. 1, 1, 3, 1
  112. ]);
  113. let m2: Matrix = new Matrix([
  114. 1, 0, 0, 0,
  115. 0, 0, -1, 0,
  116. 0, 1, 0, 0,
  117. 0, 0, 0, 1 ]);
  118. let bt = m2.mulVec(b);
  119. assert.deepEqual(bt.valueOf(), [
  120. -1, 0, 1, 1
  121. ]);
  122. });
  123. it('matrix matrix multiplication works', () => {
  124. let m1: Matrix = new Matrix([
  125. -1, 0, 0, 1,
  126. 0, -1, 0, 2,
  127. 0, 0, 1, 3,
  128. 0, 0, 0, 1 ]);
  129. expect(m1).to.not.be.null;
  130. expect(m1).to.be.an('object');
  131. expect(m1).has.property('mul');
  132. let m2: Matrix = new Matrix([
  133. 1, 0, 0, 0,
  134. 0, 0, -1, 0,
  135. 0, 1, 0, 0,
  136. 0, 0, 0, 1 ]);
  137. let m = m1.mul(m2);
  138. assert.deepEqual(m.getVals(), [
  139. -1, 0, 0, 1,
  140. 0, 0, 1, 2,
  141. 0, 1, 0, 3,
  142. 0, 0, 0, 1
  143. ]);
  144. });
  145. });