Triangle.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import { Vector3 } from './Vector3.js';
  2. /**
  3. * @author bhouston / http://clara.io
  4. * @author mrdoob / http://mrdoob.com/
  5. */
  6. function Triangle( a, b, c ) {
  7. this.a = ( a !== undefined ) ? a : new Vector3();
  8. this.b = ( b !== undefined ) ? b : new Vector3();
  9. this.c = ( c !== undefined ) ? c : new Vector3();
  10. }
  11. Object.assign( Triangle, {
  12. getNormal: function () {
  13. var v0 = new Vector3();
  14. return function getNormal( a, b, c, target ) {
  15. if ( target === undefined ) {
  16. console.warn( 'THREE.Triangle: .getNormal() target is now required' );
  17. target = new Vector3();
  18. }
  19. target.subVectors( c, b );
  20. v0.subVectors( a, b );
  21. target.cross( v0 );
  22. var targetLengthSq = target.lengthSq();
  23. if ( targetLengthSq > 0 ) {
  24. return target.multiplyScalar( 1 / Math.sqrt( targetLengthSq ) );
  25. }
  26. return target.set( 0, 0, 0 );
  27. };
  28. }(),
  29. // static/instance method to calculate barycentric coordinates
  30. // based on: http://www.blackpawn.com/texts/pointinpoly/default.html
  31. getBarycoord: function () {
  32. var v0 = new Vector3();
  33. var v1 = new Vector3();
  34. var v2 = new Vector3();
  35. return function getBarycoord( point, a, b, c, target ) {
  36. v0.subVectors( c, a );
  37. v1.subVectors( b, a );
  38. v2.subVectors( point, a );
  39. var dot00 = v0.dot( v0 );
  40. var dot01 = v0.dot( v1 );
  41. var dot02 = v0.dot( v2 );
  42. var dot11 = v1.dot( v1 );
  43. var dot12 = v1.dot( v2 );
  44. var denom = ( dot00 * dot11 - dot01 * dot01 );
  45. if ( target === undefined ) {
  46. console.warn( 'THREE.Triangle: .getBarycoord() target is now required' );
  47. target = new Vector3();
  48. }
  49. // collinear or singular triangle
  50. if ( denom === 0 ) {
  51. // arbitrary location outside of triangle?
  52. // not sure if this is the best idea, maybe should be returning undefined
  53. return target.set( - 2, - 1, - 1 );
  54. }
  55. var invDenom = 1 / denom;
  56. var u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  57. var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  58. // barycentric coordinates must always sum to 1
  59. return target.set( 1 - u - v, v, u );
  60. };
  61. }(),
  62. containsPoint: function () {
  63. var v1 = new Vector3();
  64. return function containsPoint( point, a, b, c ) {
  65. Triangle.getBarycoord( point, a, b, c, v1 );
  66. return ( v1.x >= 0 ) && ( v1.y >= 0 ) && ( ( v1.x + v1.y ) <= 1 );
  67. };
  68. }()
  69. } );
  70. Object.assign( Triangle.prototype, {
  71. set: function ( a, b, c ) {
  72. this.a.copy( a );
  73. this.b.copy( b );
  74. this.c.copy( c );
  75. return this;
  76. },
  77. setFromPointsAndIndices: function ( points, i0, i1, i2 ) {
  78. this.a.copy( points[ i0 ] );
  79. this.b.copy( points[ i1 ] );
  80. this.c.copy( points[ i2 ] );
  81. return this;
  82. },
  83. clone: function () {
  84. return new this.constructor().copy( this );
  85. },
  86. copy: function ( triangle ) {
  87. this.a.copy( triangle.a );
  88. this.b.copy( triangle.b );
  89. this.c.copy( triangle.c );
  90. return this;
  91. },
  92. getArea: function () {
  93. var v0 = new Vector3();
  94. var v1 = new Vector3();
  95. return function getArea() {
  96. v0.subVectors( this.c, this.b );
  97. v1.subVectors( this.a, this.b );
  98. return v0.cross( v1 ).length() * 0.5;
  99. };
  100. }(),
  101. getMidpoint: function ( target ) {
  102. if ( target === undefined ) {
  103. console.warn( 'THREE.Triangle: .getMidpoint() target is now required' );
  104. target = new Vector3();
  105. }
  106. return target.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );
  107. },
  108. getNormal: function ( target ) {
  109. return Triangle.getNormal( this.a, this.b, this.c, target );
  110. },
  111. getPlane: function ( target ) {
  112. if ( target === undefined ) {
  113. console.warn( 'THREE.Triangle: .getPlane() target is now required' );
  114. target = new Vector3();
  115. }
  116. return target.setFromCoplanarPoints( this.a, this.b, this.c );
  117. },
  118. getBarycoord: function ( point, target ) {
  119. return Triangle.getBarycoord( point, this.a, this.b, this.c, target );
  120. },
  121. containsPoint: function ( point ) {
  122. return Triangle.containsPoint( point, this.a, this.b, this.c );
  123. },
  124. intersectsBox: function ( box ) {
  125. return box.intersectsTriangle( this );
  126. },
  127. closestPointToPoint: function () {
  128. var vab = new Vector3();
  129. var vac = new Vector3();
  130. var vbc = new Vector3();
  131. var vap = new Vector3();
  132. var vbp = new Vector3();
  133. var vcp = new Vector3();
  134. return function closestPointToPoint( p, target ) {
  135. if ( target === undefined ) {
  136. console.warn( 'THREE.Triangle: .closestPointToPoint() target is now required' );
  137. target = new Vector3();
  138. }
  139. var a = this.a, b = this.b, c = this.c;
  140. var v, w;
  141. // algorithm thanks to Real-Time Collision Detection by Christer Ericson,
  142. // published by Morgan Kaufmann Publishers, (c) 2005 Elsevier Inc.,
  143. // under the accompanying license; see chapter 5.1.5 for detailed explanation.
  144. // basically, we're distinguishing which of the voronoi regions of the triangle
  145. // the point lies in with the minimum amount of redundant computation.
  146. vab.subVectors( b, a );
  147. vac.subVectors( c, a );
  148. vap.subVectors( p, a );
  149. var d1 = vab.dot( vap );
  150. var d2 = vac.dot( vap );
  151. if ( d1 <= 0 && d2 <= 0 ) {
  152. // vertex region of A; barycentric coords (1, 0, 0)
  153. return target.copy( a );
  154. }
  155. vbp.subVectors( p, b );
  156. var d3 = vab.dot( vbp );
  157. var d4 = vac.dot( vbp );
  158. if ( d3 >= 0 && d4 <= d3 ) {
  159. // vertex region of B; barycentric coords (0, 1, 0)
  160. return target.copy( b );
  161. }
  162. var vc = d1 * d4 - d3 * d2;
  163. if ( vc <= 0 && d1 >= 0 && d3 <= 0 ) {
  164. v = d1 / ( d1 - d3 );
  165. // edge region of AB; barycentric coords (1-v, v, 0)
  166. return target.copy( a ).addScaledVector( vab, v );
  167. }
  168. vcp.subVectors( p, c );
  169. var d5 = vab.dot( vcp );
  170. var d6 = vac.dot( vcp );
  171. if ( d6 >= 0 && d5 <= d6 ) {
  172. // vertex region of C; barycentric coords (0, 0, 1)
  173. return target.copy( c );
  174. }
  175. var vb = d5 * d2 - d1 * d6;
  176. if ( vb <= 0 && d2 >= 0 && d6 <= 0 ) {
  177. w = d2 / ( d2 - d6 );
  178. // edge region of AC; barycentric coords (1-w, 0, w)
  179. return target.copy( a ).addScaledVector( vac, w );
  180. }
  181. var va = d3 * d6 - d5 * d4;
  182. if ( va <= 0 && ( d4 - d3 ) >= 0 && ( d5 - d6 ) >= 0 ) {
  183. vbc.subVectors( c, b );
  184. w = ( d4 - d3 ) / ( ( d4 - d3 ) + ( d5 - d6 ) );
  185. // edge region of BC; barycentric coords (0, 1-w, w)
  186. return target.copy( b ).addScaledVector( vbc, w ); // edge region of BC
  187. }
  188. // face region
  189. var denom = 1 / ( va + vb + vc );
  190. // u = va * denom
  191. v = vb * denom;
  192. w = vc * denom;
  193. return target.copy( a ).addScaledVector( vab, v ).addScaledVector( vac, w );
  194. };
  195. }(),
  196. equals: function ( triangle ) {
  197. return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );
  198. }
  199. } );
  200. export { Triangle };