Plane.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { Matrix3 } from './Matrix3.js';
  2. import { Vector3 } from './Vector3.js';
  3. /**
  4. * @author bhouston / http://clara.io
  5. */
  6. function Plane( normal, constant ) {
  7. // normal is assumed to be normalized
  8. this.normal = ( normal !== undefined ) ? normal : new Vector3( 1, 0, 0 );
  9. this.constant = ( constant !== undefined ) ? constant : 0;
  10. }
  11. Object.assign( Plane.prototype, {
  12. set: function ( normal, constant ) {
  13. this.normal.copy( normal );
  14. this.constant = constant;
  15. return this;
  16. },
  17. setComponents: function ( x, y, z, w ) {
  18. this.normal.set( x, y, z );
  19. this.constant = w;
  20. return this;
  21. },
  22. setFromNormalAndCoplanarPoint: function ( normal, point ) {
  23. this.normal.copy( normal );
  24. this.constant = - point.dot( this.normal );
  25. return this;
  26. },
  27. setFromCoplanarPoints: function () {
  28. var v1 = new Vector3();
  29. var v2 = new Vector3();
  30. return function setFromCoplanarPoints( a, b, c ) {
  31. var normal = v1.subVectors( c, b ).cross( v2.subVectors( a, b ) ).normalize();
  32. // Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
  33. this.setFromNormalAndCoplanarPoint( normal, a );
  34. return this;
  35. };
  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. var 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, optionalTarget ) {
  64. var result = optionalTarget || new Vector3();
  65. return result.copy( this.normal ).multiplyScalar( - this.distanceToPoint( point ) ).add( point );
  66. },
  67. intersectLine: function () {
  68. var v1 = new Vector3();
  69. return function intersectLine( line, optionalTarget ) {
  70. var result = optionalTarget || new Vector3();
  71. var direction = line.delta( v1 );
  72. var denominator = this.normal.dot( direction );
  73. if ( denominator === 0 ) {
  74. // line is coplanar, return origin
  75. if ( this.distanceToPoint( line.start ) === 0 ) {
  76. return result.copy( line.start );
  77. }
  78. // Unsure if this is the correct method to handle this case.
  79. return undefined;
  80. }
  81. var t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
  82. if ( t < 0 || t > 1 ) {
  83. return undefined;
  84. }
  85. return result.copy( direction ).multiplyScalar( t ).add( line.start );
  86. };
  87. }(),
  88. intersectsLine: function ( line ) {
  89. // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
  90. var startSign = this.distanceToPoint( line.start );
  91. var endSign = this.distanceToPoint( line.end );
  92. return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
  93. },
  94. intersectsBox: function ( box ) {
  95. return box.intersectsPlane( this );
  96. },
  97. intersectsSphere: function ( sphere ) {
  98. return sphere.intersectsPlane( this );
  99. },
  100. coplanarPoint: function ( optionalTarget ) {
  101. var result = optionalTarget || new Vector3();
  102. return result.copy( this.normal ).multiplyScalar( - this.constant );
  103. },
  104. applyMatrix4: function () {
  105. var v1 = new Vector3();
  106. var m1 = new Matrix3();
  107. return function applyMatrix4( matrix, optionalNormalMatrix ) {
  108. var normalMatrix = optionalNormalMatrix || m1.getNormalMatrix( matrix );
  109. var referencePoint = this.coplanarPoint( v1 ).applyMatrix4( matrix );
  110. var normal = this.normal.applyMatrix3( normalMatrix ).normalize();
  111. this.constant = - referencePoint.dot( normal );
  112. return this;
  113. };
  114. }(),
  115. translate: function ( offset ) {
  116. this.constant -= offset.dot( this.normal );
  117. return this;
  118. },
  119. equals: function ( plane ) {
  120. return plane.normal.equals( this.normal ) && ( plane.constant === this.constant );
  121. }
  122. } );
  123. export { Plane };