Line3.tests.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. assert.ok( a.at( - 1 ).distanceTo( new Vector3( 1, 1, 0 ) ) < 0.0001, "Passed!" );
  82. assert.ok( a.at( 0 ).distanceTo( one3.clone() ) < 0.0001, "Passed!" );
  83. assert.ok( a.at( 1 ).distanceTo( new Vector3( 1, 1, 2 ) ) < 0.0001, "Passed!" );
  84. assert.ok( a.at( 2 ).distanceTo( new Vector3( 1, 1, 3 ) ) < 0.0001, "Passed!" );
  85. } );
  86. QUnit.test( "closestPointToPoint/closestPointToPointParameter", ( assert ) => {
  87. var a = new Line3( one3.clone(), new Vector3( 1, 1, 2 ) );
  88. // nearby the ray
  89. assert.ok( a.closestPointToPointParameter( zero3.clone(), true ) == 0, "Passed!" );
  90. var b1 = a.closestPointToPoint( zero3.clone(), true );
  91. assert.ok( b1.distanceTo( new Vector3( 1, 1, 1 ) ) < 0.0001, "Passed!" );
  92. // nearby the ray
  93. assert.ok( a.closestPointToPointParameter( zero3.clone(), false ) == - 1, "Passed!" );
  94. var b2 = a.closestPointToPoint( zero3.clone(), false );
  95. assert.ok( b2.distanceTo( new Vector3( 1, 1, 0 ) ) < 0.0001, "Passed!" );
  96. // nearby the ray
  97. assert.ok( a.closestPointToPointParameter( new Vector3( 1, 1, 5 ), true ) == 1, "Passed!" );
  98. var b = a.closestPointToPoint( new Vector3( 1, 1, 5 ), true );
  99. assert.ok( b.distanceTo( new Vector3( 1, 1, 2 ) ) < 0.0001, "Passed!" );
  100. // exactly on the ray
  101. assert.ok( a.closestPointToPointParameter( one3.clone(), true ) == 0, "Passed!" );
  102. var c = a.closestPointToPoint( one3.clone(), true );
  103. assert.ok( c.distanceTo( one3.clone() ) < 0.0001, "Passed!" );
  104. } );
  105. QUnit.test( "applyMatrix4", ( assert ) => {
  106. var a = new Line3( zero3.clone(), two3.clone() );
  107. var b = new Vector4( two3.x, two3.y, two3.z, 1 );
  108. var m = new Matrix4().makeTranslation( x, y, z );
  109. var v = new Vector3( x, y, z );
  110. a.applyMatrix4( m );
  111. assert.ok( a.start.equals( v ), "Translation: check start" );
  112. assert.ok( a.end.equals( new Vector3( 2 + x, 2 + y, 2 + z ) ), "Translation: check start" );
  113. // reset starting conditions
  114. a.set( zero3.clone(), two3.clone() );
  115. m.makeRotationX( Math.PI );
  116. a.applyMatrix4( m );
  117. b.applyMatrix4( m );
  118. assert.ok( a.start.equals( zero3 ), "Rotation: check start" );
  119. assert.numEqual( a.end.x, b.x / b.w, "Rotation: check end.x" );
  120. assert.numEqual( a.end.y, b.y / b.w, "Rotation: check end.y" );
  121. assert.numEqual( a.end.z, b.z / b.w, "Rotation: check end.z" );
  122. // reset starting conditions
  123. a.set( zero3.clone(), two3.clone() );
  124. b.set( two3.x, two3.y, two3.z, 1 );
  125. m.setPosition( v );
  126. a.applyMatrix4( m );
  127. b.applyMatrix4( m );
  128. assert.ok( a.start.equals( v ), "Both: check start" );
  129. assert.numEqual( a.end.x, b.x / b.w, "Both: check end.x" );
  130. assert.numEqual( a.end.y, b.y / b.w, "Both: check end.y" );
  131. assert.numEqual( a.end.z, b.z / b.w, "Both: check end.z" );
  132. } );
  133. QUnit.todo( "equals", ( assert ) => {
  134. assert.ok( false, "everything's gonna be alright" );
  135. } );
  136. } );
  137. } );
粤ICP备19079148号