Line3.tests.js 6.1 KB

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