Triangle.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import { Vector3 } from './Vector3.js';
  2. import { Line3 } from './Line3.js';
  3. import { Plane } from './Plane.js';
  4. /**
  5. * @author bhouston / http://clara.io
  6. * @author mrdoob / http://mrdoob.com/
  7. */
  8. function Triangle( a, b, c ) {
  9. this.a = ( a !== undefined ) ? a : new Vector3();
  10. this.b = ( b !== undefined ) ? b : new Vector3();
  11. this.c = ( c !== undefined ) ? c : new Vector3();
  12. }
  13. Object.assign( Triangle, {
  14. normal: function () {
  15. var v0 = new Vector3();
  16. return function normal( a, b, c, optionalTarget ) {
  17. var result = optionalTarget || new Vector3();
  18. result.subVectors( c, b );
  19. v0.subVectors( a, b );
  20. result.cross( v0 );
  21. var resultLengthSq = result.lengthSq();
  22. if ( resultLengthSq > 0 ) {
  23. return result.multiplyScalar( 1 / Math.sqrt( resultLengthSq ) );
  24. }
  25. return result.set( 0, 0, 0 );
  26. };
  27. }(),
  28. // static/instance method to calculate barycentric coordinates
  29. // based on: http://www.blackpawn.com/texts/pointinpoly/default.html
  30. barycoordFromPoint: function () {
  31. var v0 = new Vector3();
  32. var v1 = new Vector3();
  33. var v2 = new Vector3();
  34. return function barycoordFromPoint( point, a, b, c, optionalTarget ) {
  35. v0.subVectors( c, a );
  36. v1.subVectors( b, a );
  37. v2.subVectors( point, a );
  38. var dot00 = v0.dot( v0 );
  39. var dot01 = v0.dot( v1 );
  40. var dot02 = v0.dot( v2 );
  41. var dot11 = v1.dot( v1 );
  42. var dot12 = v1.dot( v2 );
  43. var denom = ( dot00 * dot11 - dot01 * dot01 );
  44. var result = optionalTarget || new Vector3();
  45. // collinear or singular triangle
  46. if ( denom === 0 ) {
  47. // arbitrary location outside of triangle?
  48. // not sure if this is the best idea, maybe should be returning undefined
  49. return result.set( - 2, - 1, - 1 );
  50. }
  51. var invDenom = 1 / denom;
  52. var u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  53. var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  54. // barycentric coordinates must always sum to 1
  55. return result.set( 1 - u - v, v, u );
  56. };
  57. }(),
  58. containsPoint: function () {
  59. var v1 = new Vector3();
  60. return function containsPoint( point, a, b, c ) {
  61. var result = Triangle.barycoordFromPoint( point, a, b, c, v1 );
  62. return ( result.x >= 0 ) && ( result.y >= 0 ) && ( ( result.x + result.y ) <= 1 );
  63. };
  64. }()
  65. } );
  66. Object.assign( Triangle.prototype, {
  67. set: function ( a, b, c ) {
  68. this.a.copy( a );
  69. this.b.copy( b );
  70. this.c.copy( c );
  71. return this;
  72. },
  73. setFromPointsAndIndices: function ( points, i0, i1, i2 ) {
  74. this.a.copy( points[ i0 ] );
  75. this.b.copy( points[ i1 ] );
  76. this.c.copy( points[ i2 ] );
  77. return this;
  78. },
  79. clone: function () {
  80. return new this.constructor().copy( this );
  81. },
  82. copy: function ( triangle ) {
  83. this.a.copy( triangle.a );
  84. this.b.copy( triangle.b );
  85. this.c.copy( triangle.c );
  86. return this;
  87. },
  88. area: function () {
  89. var v0 = new Vector3();
  90. var v1 = new Vector3();
  91. return function area() {
  92. v0.subVectors( this.c, this.b );
  93. v1.subVectors( this.a, this.b );
  94. return v0.cross( v1 ).length() * 0.5;
  95. };
  96. }(),
  97. midpoint: function ( optionalTarget ) {
  98. var result = optionalTarget || new Vector3();
  99. return result.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );
  100. },
  101. normal: function ( optionalTarget ) {
  102. return Triangle.normal( this.a, this.b, this.c, optionalTarget );
  103. },
  104. plane: function ( optionalTarget ) {
  105. var result = optionalTarget || new Plane();
  106. return result.setFromCoplanarPoints( this.a, this.b, this.c );
  107. },
  108. barycoordFromPoint: function ( point, optionalTarget ) {
  109. return Triangle.barycoordFromPoint( point, this.a, this.b, this.c, optionalTarget );
  110. },
  111. containsPoint: function ( point ) {
  112. return Triangle.containsPoint( point, this.a, this.b, this.c );
  113. },
  114. intersectsBox: function ( box ) {
  115. return box.intersectsTriangle( this );
  116. },
  117. closestPointToPoint: function () {
  118. var plane = new Plane();
  119. var edgeList = [ new Line3(), new Line3(), new Line3() ];
  120. var projectedPoint = new Vector3();
  121. var closestPoint = new Vector3();
  122. return function closestPointToPoint( point, optionalTarget ) {
  123. var result = optionalTarget || new Vector3();
  124. var minDistance = Infinity;
  125. // project the point onto the plane of the triangle
  126. plane.setFromCoplanarPoints( this.a, this.b, this.c );
  127. plane.projectPoint( point, projectedPoint );
  128. // check if the projection lies within the triangle
  129. if ( this.containsPoint( projectedPoint ) === true ) {
  130. // if so, this is the closest point
  131. result.copy( projectedPoint );
  132. } else {
  133. // if not, the point falls outside the triangle. the result is the closest point to the triangle's edges or vertices
  134. edgeList[ 0 ].set( this.a, this.b );
  135. edgeList[ 1 ].set( this.b, this.c );
  136. edgeList[ 2 ].set( this.c, this.a );
  137. for ( var i = 0; i < edgeList.length; i ++ ) {
  138. edgeList[ i ].closestPointToPoint( projectedPoint, true, closestPoint );
  139. var distance = projectedPoint.distanceToSquared( closestPoint );
  140. if ( distance < minDistance ) {
  141. minDistance = distance;
  142. result.copy( closestPoint );
  143. }
  144. }
  145. }
  146. return result;
  147. };
  148. }(),
  149. equals: function ( triangle ) {
  150. return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );
  151. }
  152. } );
  153. export { Triangle };