Plane.js 4.3 KB

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