Triangle.js 7.2 KB

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