Plane.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. THREE.Plane = function ( normal, constant ) {
  5. if ( normal === undefined && constant === undefined ) {
  6. this.normal = new THREE.Vector3();
  7. this.constant = 0;
  8. } else {
  9. this.normal = normal.clone();
  10. this.constant = constant || 0;
  11. }
  12. };
  13. THREE.Plane.prototype = {
  14. constructor: THREE.Plane,
  15. set: function ( normal, constant ) {
  16. this.normal.copy( normal );
  17. this.constant = constant;
  18. return this;
  19. },
  20. setComponents: function ( x, y, z, w ) {
  21. this.normal.set( x, y, z );
  22. this.constant = w;
  23. return this;
  24. },
  25. setFromNormalAndCoplanarPoint: function ( normal, point ) {
  26. this.normal.copy( normal );
  27. this.constant = - point.dot( normal );
  28. return this;
  29. },
  30. setFromCoplanarPoints: function ( a, b, c ) {
  31. var normal = THREE.Plane.__v1.sub( b, a ).cross(
  32. THREE.Plane.__v2.sub( c, a ) );
  33. // Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
  34. this.setFromNormalAndCoplanarPoint( normal, a );
  35. return this;
  36. },
  37. copy: function ( plane ) {
  38. this.normal.copy( plane.normal );
  39. this.constant = plane.constant;
  40. return this;
  41. },
  42. flip: function () {
  43. this.normal.negate();
  44. return this;
  45. },
  46. normalize: function () {
  47. // Note: will lead to a divide by zero if the plane is invalid.
  48. var inverseNormalLength = 1.0 / this.normal.length();
  49. this.normal.multiplyScalar( inverseNormalLength );
  50. this.constant *= inverseNormalLength;
  51. return this;
  52. },
  53. distanceToPoint: function ( point ) {
  54. return this.normal.dot( point ) + this.constant;
  55. },
  56. distanceToSphere: function ( sphere ) {
  57. return this.distanceToPoint( sphere.center ) - sphere.radius;
  58. },
  59. projectPoint: function ( point ) {
  60. return this.orthoPoint( point ).subSelf( point ).negate();
  61. },
  62. orthoPoint: function ( point ) {
  63. var perpendicularMagnitude = this.distanceToPoint( point );
  64. return new THREE.Vector3().copy( this.normal ).multiplyScalar( perpendicularMagnitude );
  65. },
  66. intersectsLine: function ( startPoint, endPoint ) {
  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( startPoint );
  69. var endSign = this.distanceToPoint( endPoint );
  70. return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
  71. },
  72. coplanarPoint: function () {
  73. return new THREE.Vector3().copy( this.normal ).multiplyScalar( - this.constant );
  74. },
  75. translate: function ( offset ) {
  76. this.constant = - offset.dot( this.normal );
  77. return this;
  78. },
  79. equals: function ( plane ) {
  80. return plane.normal.equals( this.normal ) && ( plane.constant == this.constant );
  81. },
  82. clone: function () {
  83. return new THREE.Plane().copy( this );
  84. }
  85. };
  86. THREE.Plane.__v1 = new THREE.Vector3();
  87. THREE.Plane.__v2 = new THREE.Vector3();