Triangle.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import { Vector3 } from './Vector3';
  2. import { Line3 } from './Line3';
  3. import { Plane } from './Plane';
  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. constructor: Triangle,
  68. set: function ( a, b, c ) {
  69. this.a.copy( a );
  70. this.b.copy( b );
  71. this.c.copy( c );
  72. return this;
  73. },
  74. setFromPointsAndIndices: function ( points, i0, i1, i2 ) {
  75. this.a.copy( points[ i0 ] );
  76. this.b.copy( points[ i1 ] );
  77. this.c.copy( points[ i2 ] );
  78. return this;
  79. },
  80. clone: function () {
  81. return new this.constructor().copy( this );
  82. },
  83. copy: function ( triangle ) {
  84. this.a.copy( triangle.a );
  85. this.b.copy( triangle.b );
  86. this.c.copy( triangle.c );
  87. return this;
  88. },
  89. area: function () {
  90. var v0 = new Vector3();
  91. var v1 = new Vector3();
  92. return function area() {
  93. v0.subVectors( this.c, this.b );
  94. v1.subVectors( this.a, this.b );
  95. return v0.cross( v1 ).length() * 0.5;
  96. };
  97. }(),
  98. midpoint: function ( optionalTarget ) {
  99. var result = optionalTarget || new Vector3();
  100. return result.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );
  101. },
  102. normal: function ( optionalTarget ) {
  103. return Triangle.normal( this.a, this.b, this.c, optionalTarget );
  104. },
  105. plane: function ( optionalTarget ) {
  106. var result = optionalTarget || new Plane();
  107. return result.setFromCoplanarPoints( this.a, this.b, this.c );
  108. },
  109. barycoordFromPoint: function ( point, optionalTarget ) {
  110. return Triangle.barycoordFromPoint( point, this.a, this.b, this.c, optionalTarget );
  111. },
  112. containsPoint: function ( point ) {
  113. return Triangle.containsPoint( point, this.a, this.b, this.c );
  114. },
  115. closestPointToPoint: function () {
  116. var plane = new Plane();
  117. var edgeList = [ new Line3(), new Line3(), new Line3() ];
  118. var projectedPoint = new Vector3();
  119. var closestPoint = new Vector3();
  120. return function closestPointToPoint( point, optionalTarget ) {
  121. var result = optionalTarget || new Vector3();
  122. var minDistance = Infinity;
  123. // project the point onto the plane of the triangle
  124. plane.setFromCoplanarPoints( this.a, this.b, this.c );
  125. plane.projectPoint( point, projectedPoint );
  126. // check if the projection lies within the triangle
  127. if( this.containsPoint( projectedPoint ) === true ) {
  128. // if so, this is the closest point
  129. result.copy( projectedPoint );
  130. } else {
  131. // if not, the point falls outside the triangle. the result is the closest point to the triangle's edges or vertices
  132. edgeList[ 0 ].set( this.a, this.b );
  133. edgeList[ 1 ].set( this.b, this.c );
  134. edgeList[ 2 ].set( this.c, this.a );
  135. for( var i = 0; i < edgeList.length; i ++ ) {
  136. edgeList[ i ].closestPointToPoint( projectedPoint, true, closestPoint );
  137. var distance = projectedPoint.distanceToSquared( closestPoint );
  138. if( distance < minDistance ) {
  139. minDistance = distance;
  140. result.copy( closestPoint );
  141. }
  142. }
  143. }
  144. return result;
  145. };
  146. }(),
  147. equals: function ( triangle ) {
  148. return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );
  149. }
  150. } );
  151. export { Triangle };