Plane.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. THREE.Plane = function ( normal, constant ) {
  5. this.normal = ( normal !== undefined ) ? normal : new THREE.Vector3( 1, 0, 0 );
  6. this.constant = ( constant !== undefined ) ? constant : 0;
  7. };
  8. THREE.Plane.prototype = {
  9. constructor: THREE.Plane,
  10. set: function ( normal, constant ) {
  11. this.normal.copy( normal );
  12. this.constant = constant;
  13. return this;
  14. },
  15. setComponents: function ( x, y, z, w ) {
  16. this.normal.set( x, y, z );
  17. this.constant = w;
  18. return this;
  19. },
  20. setFromNormalAndCoplanarPoint: function ( normal, point ) {
  21. this.normal.copy( normal );
  22. this.constant = - point.dot( this.normal ); // must be this.normal, not normal, as this.normal is normalized
  23. return this;
  24. },
  25. setFromCoplanarPoints: function ( a, b, c ) {
  26. var normal = THREE.Plane.__v1.subVectors( c, b ).cross( THREE.Plane.__v2.subVectors( a, b ) ).normalize();
  27. // Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
  28. this.setFromNormalAndCoplanarPoint( normal, a );
  29. return this;
  30. },
  31. copy: function ( plane ) {
  32. this.normal.copy( plane.normal );
  33. this.constant = plane.constant;
  34. return this;
  35. },
  36. normalize: function () {
  37. // Note: will lead to a divide by zero if the plane is invalid.
  38. var inverseNormalLength = 1.0 / this.normal.length();
  39. this.normal.multiplyScalar( inverseNormalLength );
  40. this.constant *= inverseNormalLength;
  41. return this;
  42. },
  43. negate: function () {
  44. this.constant *= -1;
  45. this.normal.negate();
  46. return this;
  47. },
  48. distanceToPoint: function ( point ) {
  49. return this.normal.dot( point ) + this.constant;
  50. },
  51. distanceToSphere: function ( sphere ) {
  52. return this.distanceToPoint( sphere.center ) - sphere.radius;
  53. },
  54. projectPoint: function ( point, optionalTarget ) {
  55. return this.orthoPoint( point, optionalTarget ).sub( point ).negate();
  56. },
  57. orthoPoint: function ( point, optionalTarget ) {
  58. var perpendicularMagnitude = this.distanceToPoint( point );
  59. var result = optionalTarget || new THREE.Vector3();
  60. return result.copy( this.normal ).multiplyScalar( perpendicularMagnitude );
  61. },
  62. isIntersectionLine: function ( startPoint, endPoint ) {
  63. // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
  64. var startSign = this.distanceToPoint( startPoint );
  65. var endSign = this.distanceToPoint( endPoint );
  66. return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
  67. },
  68. intersectLine: function ( startPoint, endPoint, optionalTarget ) {
  69. var result = optionalTarget || new THREE.Vector3();
  70. var direction = THREE.Plane.__v1.subVectors( endPoint, startPoint );
  71. var denominator = this.normal.dot( direction );
  72. if ( denominator == 0 ) {
  73. // line is coplanar, return origin
  74. if( this.distanceToPoint( startPoint ) == 0 ) {
  75. return result.copy( startPoint );
  76. }
  77. // Unsure if this is the correct method to handle this case.
  78. return undefined;
  79. }
  80. var t = - ( startPoint.dot( this.normal ) + this.constant ) / denominator;
  81. if( t < 0 || t > 1 ) {
  82. return undefined;
  83. }
  84. return result.copy( direction ).multiplyScalar( t ).add( startPoint );
  85. },
  86. coplanarPoint: function ( optionalTarget ) {
  87. var result = optionalTarget || new THREE.Vector3();
  88. return result.copy( this.normal ).multiplyScalar( - this.constant );
  89. },
  90. transform: function ( matrix, optionalNormalMatrix ) {
  91. // compute new normal based on theory here:
  92. // http://www.songho.ca/opengl/gl_normaltransform.html
  93. optionalNormalMatrix = optionalNormalMatrix || new THREE.Matrix3().getInverse( matrix ).transpose();
  94. var newNormal = THREE.Plane.__v1.copy( this.normal ).applyMatrix3( optionalNormalMatrix );
  95. var newCoplanarPoint = this.coplanarPoint( THREE.Plane.__v2 );
  96. newCoplanarPoint.applyMatrix4( matrix );
  97. this.setFromNormalAndCoplanarPoint( newNormal, newCoplanarPoint );
  98. return this;
  99. },
  100. translate: function ( offset ) {
  101. this.constant = this.constant - offset.dot( this.normal );
  102. return this;
  103. },
  104. equals: function ( plane ) {
  105. return plane.normal.equals( this.normal ) && ( plane.constant == this.constant );
  106. },
  107. clone: function () {
  108. return new THREE.Plane().copy( this );
  109. }
  110. };
  111. THREE.Plane.__vZero = new THREE.Vector3( 0, 0, 0 );
  112. THREE.Plane.__v1 = new THREE.Vector3();
  113. THREE.Plane.__v2 = new THREE.Vector3();