Plane.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 () {
  26. var v1 = new THREE.Vector3();
  27. var v2 = new THREE.Vector3();
  28. return function ( a, b, c ) {
  29. var normal = v1.subVectors( c, b ).cross( v2.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. }(),
  35. copy: function ( plane ) {
  36. this.normal.copy( plane.normal );
  37. this.constant = plane.constant;
  38. return this;
  39. },
  40. normalize: function () {
  41. // Note: will lead to a divide by zero if the plane is invalid.
  42. var inverseNormalLength = 1.0 / this.normal.length();
  43. this.normal.multiplyScalar( inverseNormalLength );
  44. this.constant *= inverseNormalLength;
  45. return this;
  46. },
  47. negate: function () {
  48. this.constant *= - 1;
  49. this.normal.negate();
  50. return this;
  51. },
  52. distanceToPoint: function ( point ) {
  53. return this.normal.dot( point ) + this.constant;
  54. },
  55. distanceToSphere: function ( sphere ) {
  56. return this.distanceToPoint( sphere.center ) - sphere.radius;
  57. },
  58. projectPoint: function ( point, optionalTarget ) {
  59. return this.orthoPoint( point, optionalTarget ).sub( point ).negate();
  60. },
  61. orthoPoint: function ( point, optionalTarget ) {
  62. var perpendicularMagnitude = this.distanceToPoint( point );
  63. var result = optionalTarget || new THREE.Vector3();
  64. return result.copy( this.normal ).multiplyScalar( perpendicularMagnitude );
  65. },
  66. isIntersectionLine: function ( line ) {
  67. // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
  68. var startSign = this.distanceToPoint( line.start );
  69. var endSign = this.distanceToPoint( line.end );
  70. return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
  71. },
  72. intersectLine: function () {
  73. var v1 = new THREE.Vector3();
  74. return function ( line, optionalTarget ) {
  75. var result = optionalTarget || new THREE.Vector3();
  76. var direction = line.delta( v1 );
  77. var denominator = this.normal.dot( direction );
  78. if ( denominator == 0 ) {
  79. // line is coplanar, return origin
  80. if ( this.distanceToPoint( line.start ) == 0 ) {
  81. return result.copy( line.start );
  82. }
  83. // Unsure if this is the correct method to handle this case.
  84. return undefined;
  85. }
  86. var t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
  87. if ( t < 0 || t > 1 ) {
  88. return undefined;
  89. }
  90. return result.copy( direction ).multiplyScalar( t ).add( line.start );
  91. };
  92. }(),
  93. coplanarPoint: function ( optionalTarget ) {
  94. var result = optionalTarget || new THREE.Vector3();
  95. return result.copy( this.normal ).multiplyScalar( - this.constant );
  96. },
  97. applyMatrix4: function () {
  98. var v1 = new THREE.Vector3();
  99. var v2 = new THREE.Vector3();
  100. var m1 = new THREE.Matrix3();
  101. return function ( matrix, optionalNormalMatrix ) {
  102. // compute new normal based on theory here:
  103. // http://www.songho.ca/opengl/gl_normaltransform.html
  104. var normalMatrix = optionalNormalMatrix || m1.getNormalMatrix( matrix );
  105. var newNormal = v1.copy( this.normal ).applyMatrix3( normalMatrix );
  106. var newCoplanarPoint = this.coplanarPoint( v2 );
  107. newCoplanarPoint.applyMatrix4( matrix );
  108. this.setFromNormalAndCoplanarPoint( newNormal, newCoplanarPoint );
  109. return this;
  110. };
  111. }(),
  112. translate: function ( offset ) {
  113. this.constant = this.constant - offset.dot( this.normal );
  114. return this;
  115. },
  116. equals: function ( plane ) {
  117. return plane.normal.equals( this.normal ) && ( plane.constant == this.constant );
  118. },
  119. clone: function () {
  120. return new THREE.Plane().copy( this );
  121. }
  122. };
粤ICP备19079148号