Plane.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. THREE.Plane = function ( normal, constant ) {
  5. this.normal = normal !== undefined ? normal.clone() : 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 ).normalize();
  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.sub( c, b ).crossSelf(
  27. THREE.Plane.__v2.sub( a, b ) ).normalize();
  28. // Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
  29. this.setFromNormalAndCoplanarPoint( normal, a );
  30. return this;
  31. },
  32. copy: function ( plane ) {
  33. this.normal.copy( plane.normal );
  34. this.constant = plane.constant;
  35. return this;
  36. },
  37. normalize: function () {
  38. // Note: will lead to a divide by zero if the plane is invalid.
  39. var inverseNormalLength = 1.0 / this.normal.length();
  40. this.normal.multiplyScalar( inverseNormalLength );
  41. this.constant *= inverseNormalLength;
  42. return this;
  43. },
  44. distanceToPoint: function ( point ) {
  45. return this.normal.dot( point ) + this.constant;
  46. },
  47. distanceToSphere: function ( sphere ) {
  48. return this.distanceToPoint( sphere.center ) - sphere.radius;
  49. },
  50. projectPoint: function ( point, optionalTarget ) {
  51. return this.orthoPoint( point, optionalTarget ).subSelf( point ).negate();
  52. },
  53. orthoPoint: function ( point, optionalTarget ) {
  54. var perpendicularMagnitude = this.distanceToPoint( point );
  55. var result = optionalTarget || new THREE.Vector3();
  56. return result.copy( this.normal ).multiplyScalar( perpendicularMagnitude );
  57. },
  58. isIntersectionLine: function ( startPoint, endPoint ) {
  59. // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
  60. var startSign = this.distanceToPoint( startPoint );
  61. var endSign = this.distanceToPoint( endPoint );
  62. return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
  63. },
  64. coplanarPoint: function ( optionalTarget ) {
  65. var result = optionalTarget || new THREE.Vector3();
  66. return result.copy( this.normal ).multiplyScalar( - this.constant );
  67. },
  68. transform: function( matrix, optionalNormalMatrix ) {
  69. var newNormal = THREE.Plane.__v1, newCoplanarPoint = THREE.Plane.__v2;
  70. // compute new normal based on theory here:
  71. // http://www.songho.ca/opengl/gl_normaltransform.html
  72. optionalNormalMatrix = optionalNormalMatrix || new THREE.Matrix3().getInverse( matrix ).transpose();
  73. newNormal = optionalNormalMatrix.multiplyVector3( newNormal.copy( this.normal ) );
  74. newCoplanarPoint = this.coplanarPoint( newCoplanarPoint );
  75. newCoplanarPoint = matrix.multiplyVector3( newCoplanarPoint );
  76. this.setFromNormalAndCoplanarPoint( newNormal, newCoplanarPoint );
  77. return this;
  78. },
  79. translate: function ( offset ) {
  80. this.constant = this.constant - offset.dot( this.normal );
  81. return this;
  82. },
  83. equals: function ( plane ) {
  84. return plane.normal.equals( this.normal ) && ( plane.constant == this.constant );
  85. },
  86. clone: function () {
  87. return new THREE.Plane().copy( this );
  88. }
  89. };
  90. THREE.Plane.__vZero = new THREE.Vector3( 0, 0, 0 );
  91. THREE.Plane.__v1 = new THREE.Vector3();
  92. THREE.Plane.__v2 = new THREE.Vector3();