Plane.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 = new Vector3( 1, 0, 0 ), constant = 0 ) {
  8. this.isPlane = true;
  9. // normal is assumed to be normalized
  10. this.normal = normal;
  11. this.constant = constant;
  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. copy( plane ) {
  35. this.normal.copy( plane.normal );
  36. this.constant = plane.constant;
  37. return this;
  38. }
  39. normalize() {
  40. // Note: will lead to a divide by zero if the plane is invalid.
  41. const inverseNormalLength = 1.0 / this.normal.length();
  42. this.normal.multiplyScalar( inverseNormalLength );
  43. this.constant *= inverseNormalLength;
  44. return this;
  45. }
  46. negate() {
  47. this.constant *= - 1;
  48. this.normal.negate();
  49. return this;
  50. }
  51. distanceToPoint( point ) {
  52. return this.normal.dot( point ) + this.constant;
  53. }
  54. distanceToSphere( sphere ) {
  55. return this.distanceToPoint( sphere.center ) - sphere.radius;
  56. }
  57. projectPoint( point, target ) {
  58. return target.copy( point ).addScaledVector( this.normal, - this.distanceToPoint( point ) );
  59. }
  60. intersectLine( line, target ) {
  61. const direction = line.delta( _vector1 );
  62. const denominator = this.normal.dot( direction );
  63. if ( denominator === 0 ) {
  64. // line is coplanar, return origin
  65. if ( this.distanceToPoint( line.start ) === 0 ) {
  66. return target.copy( line.start );
  67. }
  68. // Unsure if this is the correct method to handle this case.
  69. return null;
  70. }
  71. const t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
  72. if ( t < 0 || t > 1 ) {
  73. return null;
  74. }
  75. return target.copy( line.start ).addScaledVector( direction, t );
  76. }
  77. intersectsLine( line ) {
  78. // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
  79. const startSign = this.distanceToPoint( line.start );
  80. const endSign = this.distanceToPoint( line.end );
  81. return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
  82. }
  83. intersectsBox( box ) {
  84. return box.intersectsPlane( this );
  85. }
  86. intersectsSphere( sphere ) {
  87. return sphere.intersectsPlane( this );
  88. }
  89. coplanarPoint( target ) {
  90. return target.copy( this.normal ).multiplyScalar( - this.constant );
  91. }
  92. applyMatrix4( matrix, optionalNormalMatrix ) {
  93. const normalMatrix = optionalNormalMatrix || _normalMatrix.getNormalMatrix( matrix );
  94. const referencePoint = this.coplanarPoint( _vector1 ).applyMatrix4( matrix );
  95. const normal = this.normal.applyMatrix3( normalMatrix ).normalize();
  96. this.constant = - referencePoint.dot( normal );
  97. return this;
  98. }
  99. translate( offset ) {
  100. this.constant -= offset.dot( this.normal );
  101. return this;
  102. }
  103. equals( plane ) {
  104. return plane.normal.equals( this.normal ) && ( plane.constant === this.constant );
  105. }
  106. clone() {
  107. return new this.constructor().copy( this );
  108. }
  109. }
  110. export { Plane };