Plane.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { Matrix3 } from './Matrix3.js';
  2. import { Vector3 } from './Vector3.js';
  3. const _vector1 = /*@__PURE__*/ new Vector3();
  4. const _vector2 = /*@__PURE__*/ new Vector3();
  5. const _normalMatrix = /*@__PURE__*/ new Matrix3();
  6. class Plane {
  7. constructor( normal, constant ) {
  8. this.isPlane = true;
  9. // normal is assumed to be normalized
  10. this.normal = ( normal !== undefined ) ? normal : new Vector3( 1, 0, 0 );
  11. this.constant = ( constant !== undefined ) ? constant : 0;
  12. }
  13. set( normal, constant ) {
  14. this.normal.copy( normal );
  15. this.constant = constant;
  16. return this;
  17. }
  18. setComponents( x, y, z, w ) {
  19. this.normal.set( x, y, z );
  20. this.constant = w;
  21. return this;
  22. }
  23. setFromNormalAndCoplanarPoint( normal, point ) {
  24. this.normal.copy( normal );
  25. this.constant = - point.dot( this.normal );
  26. return this;
  27. }
  28. setFromCoplanarPoints( a, b, c ) {
  29. const normal = _vector1.subVectors( c, b ).cross( _vector2.subVectors( a, b ) ).normalize();
  30. // Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
  31. this.setFromNormalAndCoplanarPoint( normal, a );
  32. return this;
  33. }
  34. clone() {
  35. return new this.constructor().copy( this );
  36. }
  37. copy( plane ) {
  38. this.normal.copy( plane.normal );
  39. this.constant = plane.constant;
  40. return this;
  41. }
  42. normalize() {
  43. // Note: will lead to a divide by zero if the plane is invalid.
  44. const inverseNormalLength = 1.0 / this.normal.length();
  45. this.normal.multiplyScalar( inverseNormalLength );
  46. this.constant *= inverseNormalLength;
  47. return this;
  48. }
  49. negate() {
  50. this.constant *= - 1;
  51. this.normal.negate();
  52. return this;
  53. }
  54. distanceToPoint( point ) {
  55. return this.normal.dot( point ) + this.constant;
  56. }
  57. distanceToSphere( sphere ) {
  58. return this.distanceToPoint( sphere.center ) - sphere.radius;
  59. }
  60. projectPoint( point, target ) {
  61. if ( target === undefined ) {
  62. console.warn( 'THREE.Plane: .projectPoint() target is now required' );
  63. target = new Vector3();
  64. }
  65. return target.copy( this.normal ).multiplyScalar( - this.distanceToPoint( point ) ).add( point );
  66. }
  67. intersectLine( line, target ) {
  68. if ( target === undefined ) {
  69. console.warn( 'THREE.Plane: .intersectLine() target is now required' );
  70. target = new Vector3();
  71. }
  72. const direction = line.delta( _vector1 );
  73. const denominator = this.normal.dot( direction );
  74. if ( denominator === 0 ) {
  75. // line is coplanar, return origin
  76. if ( this.distanceToPoint( line.start ) === 0 ) {
  77. return target.copy( line.start );
  78. }
  79. // Unsure if this is the correct method to handle this case.
  80. return undefined;
  81. }
  82. const t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
  83. if ( t < 0 || t > 1 ) {
  84. return undefined;
  85. }
  86. return target.copy( direction ).multiplyScalar( t ).add( line.start );
  87. }
  88. intersectsLine( line ) {
  89. // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
  90. const startSign = this.distanceToPoint( line.start );
  91. const endSign = this.distanceToPoint( line.end );
  92. return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
  93. }
  94. intersectsBox( box ) {
  95. return box.intersectsPlane( this );
  96. }
  97. intersectsSphere( sphere ) {
  98. return sphere.intersectsPlane( this );
  99. }
  100. coplanarPoint( target ) {
  101. if ( target === undefined ) {
  102. console.warn( 'THREE.Plane: .coplanarPoint() target is now required' );
  103. target = new Vector3();
  104. }
  105. return target.copy( this.normal ).multiplyScalar( - this.constant );
  106. }
  107. applyMatrix4( matrix, optionalNormalMatrix ) {
  108. const normalMatrix = optionalNormalMatrix || _normalMatrix.getNormalMatrix( matrix );
  109. const referencePoint = this.coplanarPoint( _vector1 ).applyMatrix4( matrix );
  110. const normal = this.normal.applyMatrix3( normalMatrix ).normalize();
  111. this.constant = - referencePoint.dot( normal );
  112. return this;
  113. }
  114. translate( offset ) {
  115. this.constant -= offset.dot( this.normal );
  116. return this;
  117. }
  118. equals( plane ) {
  119. return plane.normal.equals( this.normal ) && ( plane.constant === this.constant );
  120. }
  121. }
  122. export { Plane };
粤ICP备19079148号