Plane.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import { Matrix3 } from './Matrix3';
  2. import { Vector3 } from './Vector3';
  3. /**
  4. * @author bhouston / http://clara.io
  5. */
  6. function Plane( normal, constant ) {
  7. this.normal = ( normal !== undefined ) ? normal : new Vector3( 1, 0, 0 );
  8. this.constant = ( constant !== undefined ) ? constant : 0;
  9. }
  10. Plane.prototype = {
  11. constructor: Plane,
  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 ); // must be this.normal, not normal, as this.normal is normalized
  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. return this.orthoPoint( point, optionalTarget ).sub( point ).negate();
  65. },
  66. orthoPoint: function ( point, optionalTarget ) {
  67. var perpendicularMagnitude = this.distanceToPoint( point );
  68. var result = optionalTarget || new Vector3();
  69. return result.copy( this.normal ).multiplyScalar( perpendicularMagnitude );
  70. },
  71. intersectLine: function () {
  72. var v1 = new Vector3();
  73. return function intersectLine( line, optionalTarget ) {
  74. var result = optionalTarget || new Vector3();
  75. var direction = line.delta( v1 );
  76. var denominator = this.normal.dot( direction );
  77. if ( denominator === 0 ) {
  78. // line is coplanar, return origin
  79. if ( this.distanceToPoint( line.start ) === 0 ) {
  80. return result.copy( line.start );
  81. }
  82. // Unsure if this is the correct method to handle this case.
  83. return undefined;
  84. }
  85. var t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
  86. if ( t < 0 || t > 1 ) {
  87. return undefined;
  88. }
  89. return result.copy( direction ).multiplyScalar( t ).add( line.start );
  90. };
  91. }(),
  92. intersectsLine: function ( line ) {
  93. // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
  94. var startSign = this.distanceToPoint( line.start );
  95. var endSign = this.distanceToPoint( line.end );
  96. return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
  97. },
  98. intersectsBox: function ( box ) {
  99. return box.intersectsPlane( this );
  100. },
  101. intersectsSphere: function ( sphere ) {
  102. return sphere.intersectsPlane( this );
  103. },
  104. coplanarPoint: function ( optionalTarget ) {
  105. var result = optionalTarget || new Vector3();
  106. return result.copy( this.normal ).multiplyScalar( - this.constant );
  107. },
  108. applyMatrix4: function () {
  109. var v1 = new Vector3();
  110. var m1 = new Matrix3();
  111. return function applyMatrix4( matrix, optionalNormalMatrix ) {
  112. var referencePoint = this.coplanarPoint( v1 ).applyMatrix4( matrix );
  113. // transform normal based on theory here:
  114. // http://www.songho.ca/opengl/gl_normaltransform.html
  115. var normalMatrix = optionalNormalMatrix || m1.getNormalMatrix( matrix );
  116. var normal = this.normal.applyMatrix3( normalMatrix ).normalize();
  117. // recalculate constant (like in setFromNormalAndCoplanarPoint)
  118. this.constant = - referencePoint.dot( normal );
  119. return this;
  120. };
  121. }(),
  122. translate: function ( offset ) {
  123. this.constant = this.constant - offset.dot( this.normal );
  124. return this;
  125. },
  126. equals: function ( plane ) {
  127. return plane.normal.equals( this.normal ) && ( plane.constant === this.constant );
  128. }
  129. };
  130. export { Plane };
粤ICP备19079148号