Line3.tests.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* global QUnit */
  2. import { Line3 } from '../../../../src/math/Line3.js';
  3. import { Vector3 } from '../../../../src/math/Vector3.js';
  4. import { Vector4 } from '../../../../src/math/Vector4.js';
  5. import { Matrix4 } from '../../../../src/math/Matrix4.js';
  6. import {
  7. x,
  8. y,
  9. z,
  10. zero3,
  11. one3,
  12. two3
  13. } from '../../utils/math-constants.js';
  14. export default QUnit.module( 'Maths', () => {
  15. QUnit.module( 'Line3', () => {
  16. // INSTANCING
  17. QUnit.test( 'Instancing', ( assert ) => {
  18. let a = new Line3();
  19. assert.ok( a.start.equals( zero3 ), 'Passed!' );
  20. assert.ok( a.end.equals( zero3 ), 'Passed!' );
  21. a = new Line3( two3.clone(), one3.clone() );
  22. assert.ok( a.start.equals( two3 ), 'Passed!' );
  23. assert.ok( a.end.equals( one3 ), 'Passed!' );
  24. } );
  25. // PUBLIC STUFF
  26. QUnit.test( 'set', ( assert ) => {
  27. const a = new Line3();
  28. a.set( one3, one3 );
  29. assert.ok( a.start.equals( one3 ), 'Passed!' );
  30. assert.ok( a.end.equals( one3 ), 'Passed!' );
  31. } );
  32. QUnit.test( 'copy/equals', ( assert ) => {
  33. const a = new Line3( zero3.clone(), one3.clone() );
  34. const b = new Line3().copy( a );
  35. assert.ok( b.start.equals( zero3 ), 'Passed!' );
  36. assert.ok( b.end.equals( one3 ), 'Passed!' );
  37. // ensure that it is a true copy
  38. a.start = zero3;
  39. a.end = one3;
  40. assert.ok( b.start.equals( zero3 ), 'Passed!' );
  41. assert.ok( b.end.equals( one3 ), 'Passed!' );
  42. } );
  43. QUnit.test( 'clone/equal', ( assert ) => {
  44. let a = new Line3();
  45. const b = new Line3( zero3, new Vector3( 1, 1, 1 ) );
  46. const c = new Line3( zero3, new Vector3( 1, 1, 0 ) );
  47. assert.notOk( a.equals( b ), 'Check a and b aren\'t equal' );
  48. assert.notOk( a.equals( c ), 'Check a and c aren\'t equal' );
  49. assert.notOk( b.equals( c ), 'Check b and c aren\'t equal' );
  50. a = b.clone();
  51. assert.ok( a.equals( b ), 'Check a and b are equal after clone()' );
  52. assert.notOk( a.equals( c ), 'Check a and c aren\'t equal after clone()' );
  53. a.set( zero3, zero3 );
  54. assert.notOk( a.equals( b ), 'Check a and b are not equal after modification' );
  55. } );
  56. QUnit.test( 'getCenter', ( assert ) => {
  57. const center = new Vector3();
  58. const a = new Line3( zero3.clone(), two3.clone() );
  59. assert.ok( a.getCenter( center ).equals( one3.clone() ), 'Passed' );
  60. } );
  61. QUnit.test( 'delta', ( assert ) => {
  62. const delta = new Vector3();
  63. const a = new Line3( zero3.clone(), two3.clone() );
  64. assert.ok( a.delta( delta ).equals( two3.clone() ), 'Passed' );
  65. } );
  66. QUnit.test( 'distanceSq', ( assert ) => {
  67. const a = new Line3( zero3, zero3 );
  68. const b = new Line3( zero3, one3 );
  69. const c = new Line3( one3.clone().negate(), one3 );
  70. const d = new Line3( two3.clone().multiplyScalar( - 2 ), two3.clone().negate() );
  71. assert.numEqual( a.distanceSq(), 0, 'Check squared distance for zero-length line' );
  72. assert.numEqual( b.distanceSq(), 3, 'Check squared distance for simple line' );
  73. assert.numEqual( c.distanceSq(), 12, 'Check squared distance for negative to positive endpoints' );
  74. assert.numEqual( d.distanceSq(), 12, 'Check squared distance for negative to negative endpoints' );
  75. } );
  76. QUnit.test( 'distance', ( assert ) => {
  77. const a = new Line3( zero3, zero3 );
  78. const b = new Line3( zero3, one3 );
  79. const c = new Line3( one3.clone().negate(), one3 );
  80. const d = new Line3( two3.clone().multiplyScalar( - 2 ), two3.clone().negate() );
  81. assert.numEqual( a.distance(), 0, 'Check distance for zero-length line' );
  82. assert.numEqual( b.distance(), Math.sqrt( 3 ), 'Check distance for simple line' );
  83. assert.numEqual( c.distance(), Math.sqrt( 12 ), 'Check distance for negative to positive endpoints' );
  84. assert.numEqual( d.distance(), Math.sqrt( 12 ), 'Check distance for negative to negative endpoints' );
  85. } );
  86. QUnit.test( 'at', ( assert ) => {
  87. const a = new Line3( one3.clone(), new Vector3( 1, 1, 2 ) );
  88. const point = new Vector3();
  89. a.at( - 1, point );
  90. assert.ok( point.distanceTo( new Vector3( 1, 1, 0 ) ) < 0.0001, 'Passed!' );
  91. a.at( 0, point );
  92. assert.ok( point.distanceTo( one3.clone() ) < 0.0001, 'Passed!' );
  93. a.at( 1, point );
  94. assert.ok( point.distanceTo( new Vector3( 1, 1, 2 ) ) < 0.0001, 'Passed!' );
  95. a.at( 2, point );
  96. assert.ok( point.distanceTo( new Vector3( 1, 1, 3 ) ) < 0.0001, 'Passed!' );
  97. } );
  98. QUnit.test( 'closestPointToPoint/closestPointToPointParameter', ( assert ) => {
  99. const a = new Line3( one3.clone(), new Vector3( 1, 1, 2 ) );
  100. const point = new Vector3();
  101. // nearby the ray
  102. assert.ok( a.closestPointToPointParameter( zero3.clone(), true ) == 0, 'Passed!' );
  103. a.closestPointToPoint( zero3.clone(), true, point );
  104. assert.ok( point.distanceTo( new Vector3( 1, 1, 1 ) ) < 0.0001, 'Passed!' );
  105. // nearby the ray
  106. assert.ok( a.closestPointToPointParameter( zero3.clone(), false ) == - 1, 'Passed!' );
  107. a.closestPointToPoint( zero3.clone(), false, point );
  108. assert.ok( point.distanceTo( new Vector3( 1, 1, 0 ) ) < 0.0001, 'Passed!' );
  109. // nearby the ray
  110. assert.ok( a.closestPointToPointParameter( new Vector3( 1, 1, 5 ), true ) == 1, 'Passed!' );
  111. a.closestPointToPoint( new Vector3( 1, 1, 5 ), true, point );
  112. assert.ok( point.distanceTo( new Vector3( 1, 1, 2 ) ) < 0.0001, 'Passed!' );
  113. // exactly on the ray
  114. assert.ok( a.closestPointToPointParameter( one3.clone(), true ) == 0, 'Passed!' );
  115. a.closestPointToPoint( one3.clone(), true, point );
  116. assert.ok( point.distanceTo( one3.clone() ) < 0.0001, 'Passed!' );
  117. } );
  118. QUnit.test( 'applyMatrix4', ( assert ) => {
  119. const a = new Line3( zero3.clone(), two3.clone() );
  120. const b = new Vector4( two3.x, two3.y, two3.z, 1 );
  121. const m = new Matrix4().makeTranslation( x, y, z );
  122. const v = new Vector3( x, y, z );
  123. a.applyMatrix4( m );
  124. assert.ok( a.start.equals( v ), 'Translation: check start' );
  125. assert.ok( a.end.equals( new Vector3( 2 + x, 2 + y, 2 + z ) ), 'Translation: check start' );
  126. // reset starting conditions
  127. a.set( zero3.clone(), two3.clone() );
  128. m.makeRotationX( Math.PI );
  129. a.applyMatrix4( m );
  130. b.applyMatrix4( m );
  131. assert.ok( a.start.equals( zero3 ), 'Rotation: check start' );
  132. assert.numEqual( a.end.x, b.x / b.w, 'Rotation: check end.x' );
  133. assert.numEqual( a.end.y, b.y / b.w, 'Rotation: check end.y' );
  134. assert.numEqual( a.end.z, b.z / b.w, 'Rotation: check end.z' );
  135. // reset starting conditions
  136. a.set( zero3.clone(), two3.clone() );
  137. b.set( two3.x, two3.y, two3.z, 1 );
  138. m.setPosition( v );
  139. a.applyMatrix4( m );
  140. b.applyMatrix4( m );
  141. assert.ok( a.start.equals( v ), 'Both: check start' );
  142. assert.numEqual( a.end.x, b.x / b.w, 'Both: check end.x' );
  143. assert.numEqual( a.end.y, b.y / b.w, 'Both: check end.y' );
  144. assert.numEqual( a.end.z, b.z / b.w, 'Both: check end.z' );
  145. } );
  146. QUnit.test( 'equals', ( assert ) => {
  147. const a = new Line3( zero3.clone(), zero3.clone() );
  148. const b = new Line3();
  149. assert.ok( a.equals( b ), 'Passed' );
  150. } );
  151. } );
  152. } );