Triangle.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import { Vector2 } from './Vector2.js';
  2. import { Vector3 } from './Vector3.js';
  3. /**
  4. * @author bhouston / http://clara.io
  5. * @author mrdoob / http://mrdoob.com/
  6. */
  7. function Triangle( a, b, c ) {
  8. this.a = ( a !== undefined ) ? a : new Vector3();
  9. this.b = ( b !== undefined ) ? b : new Vector3();
  10. this.c = ( c !== undefined ) ? c : new Vector3();
  11. }
  12. Object.assign( Triangle, {
  13. getNormal: function () {
  14. var v0 = new Vector3();
  15. return function getNormal( a, b, c, target ) {
  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. }(),
  30. // static/instance method to calculate barycentric coordinates
  31. // based on: http://www.blackpawn.com/texts/pointinpoly/default.html
  32. getBarycoord: function () {
  33. var v0 = new Vector3();
  34. var v1 = new Vector3();
  35. var v2 = new Vector3();
  36. return function getBarycoord( point, a, b, c, target ) {
  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. }(),
  63. containsPoint: function () {
  64. var v1 = new Vector3();
  65. return function containsPoint( point, a, b, c ) {
  66. Triangle.getBarycoord( point, a, b, c, v1 );
  67. return ( v1.x >= 0 ) && ( v1.y >= 0 ) && ( ( v1.x + v1.y ) <= 1 );
  68. };
  69. }(),
  70. uvIntersection: function () {
  71. var barycoord = new Vector3();
  72. var v1 = new Vector2();
  73. var v2 = new Vector2();
  74. var v3 = new Vector2();
  75. return function uvIntersection( point, p1, p2, p3, uv1, uv2, uv3, result ) {
  76. this.getBarycoord( point, p1, p2, p3, barycoord );
  77. v1.copy( uv1 ).multiplyScalar( barycoord.x );
  78. v2.copy( uv2 ).multiplyScalar( barycoord.y );
  79. v3.copy( uv3 ).multiplyScalar( barycoord.z );
  80. v1.add( v2 ).add( v3 );
  81. return result.copy( v1 );
  82. };
  83. }()
  84. } );
  85. Object.assign( Triangle.prototype, {
  86. set: function ( a, b, c ) {
  87. this.a.copy( a );
  88. this.b.copy( b );
  89. this.c.copy( c );
  90. return this;
  91. },
  92. setFromPointsAndIndices: function ( points, i0, i1, i2 ) {
  93. this.a.copy( points[ i0 ] );
  94. this.b.copy( points[ i1 ] );
  95. this.c.copy( points[ i2 ] );
  96. return this;
  97. },
  98. clone: function () {
  99. return new this.constructor().copy( this );
  100. },
  101. copy: function ( triangle ) {
  102. this.a.copy( triangle.a );
  103. this.b.copy( triangle.b );
  104. this.c.copy( triangle.c );
  105. return this;
  106. },
  107. getArea: function () {
  108. var v0 = new Vector3();
  109. var v1 = new Vector3();
  110. return function getArea() {
  111. v0.subVectors( this.c, this.b );
  112. v1.subVectors( this.a, this.b );
  113. return v0.cross( v1 ).length() * 0.5;
  114. };
  115. }(),
  116. getMidpoint: function ( target ) {
  117. if ( target === undefined ) {
  118. console.warn( 'THREE.Triangle: .getMidpoint() target is now required' );
  119. target = new Vector3();
  120. }
  121. return target.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );
  122. },
  123. getNormal: function ( target ) {
  124. return Triangle.getNormal( this.a, this.b, this.c, target );
  125. },
  126. getPlane: function ( target ) {
  127. if ( target === undefined ) {
  128. console.warn( 'THREE.Triangle: .getPlane() target is now required' );
  129. target = new Vector3();
  130. }
  131. return target.setFromCoplanarPoints( this.a, this.b, this.c );
  132. },
  133. getBarycoord: function ( point, target ) {
  134. return Triangle.getBarycoord( point, this.a, this.b, this.c, target );
  135. },
  136. containsPoint: function ( point ) {
  137. return Triangle.containsPoint( point, this.a, this.b, this.c );
  138. },
  139. uvIntersection: function ( point, uv1, uv2, uv3, result ) {
  140. return Triangle.uvIntersection( point, this.a, this.b, this.c, uv1, uv2, uv3, result );
  141. },
  142. intersectsBox: function ( box ) {
  143. return box.intersectsTriangle( this );
  144. },
  145. closestPointToPoint: function () {
  146. var vab = new Vector3();
  147. var vac = new Vector3();
  148. var vbc = new Vector3();
  149. var vap = new Vector3();
  150. var vbp = new Vector3();
  151. var vcp = new Vector3();
  152. return function closestPointToPoint( p, target ) {
  153. if ( target === undefined ) {
  154. console.warn( 'THREE.Triangle: .closestPointToPoint() target is now required' );
  155. target = new Vector3();
  156. }
  157. var a = this.a, b = this.b, c = this.c;
  158. var v, w;
  159. // algorithm thanks to Real-Time Collision Detection by Christer Ericson,
  160. // published by Morgan Kaufmann Publishers, (c) 2005 Elsevier Inc.,
  161. // under the accompanying license; see chapter 5.1.5 for detailed explanation.
  162. // basically, we're distinguishing which of the voronoi regions of the triangle
  163. // the point lies in with the minimum amount of redundant computation.
  164. vab.subVectors( b, a );
  165. vac.subVectors( c, a );
  166. vap.subVectors( p, a );
  167. var d1 = vab.dot( vap );
  168. var d2 = vac.dot( vap );
  169. if ( d1 <= 0 && d2 <= 0 ) {
  170. // vertex region of A; barycentric coords (1, 0, 0)
  171. return target.copy( a );
  172. }
  173. vbp.subVectors( p, b );
  174. var d3 = vab.dot( vbp );
  175. var d4 = vac.dot( vbp );
  176. if ( d3 >= 0 && d4 <= d3 ) {
  177. // vertex region of B; barycentric coords (0, 1, 0)
  178. return target.copy( b );
  179. }
  180. var vc = d1 * d4 - d3 * d2;
  181. if ( vc <= 0 && d1 >= 0 && d3 <= 0 ) {
  182. v = d1 / ( d1 - d3 );
  183. // edge region of AB; barycentric coords (1-v, v, 0)
  184. return target.copy( a ).addScaledVector( vab, v );
  185. }
  186. vcp.subVectors( p, c );
  187. var d5 = vab.dot( vcp );
  188. var d6 = vac.dot( vcp );
  189. if ( d6 >= 0 && d5 <= d6 ) {
  190. // vertex region of C; barycentric coords (0, 0, 1)
  191. return target.copy( c );
  192. }
  193. var vb = d5 * d2 - d1 * d6;
  194. if ( vb <= 0 && d2 >= 0 && d6 <= 0 ) {
  195. w = d2 / ( d2 - d6 );
  196. // edge region of AC; barycentric coords (1-w, 0, w)
  197. return target.copy( a ).addScaledVector( vac, w );
  198. }
  199. var va = d3 * d6 - d5 * d4;
  200. if ( va <= 0 && ( d4 - d3 ) >= 0 && ( d5 - d6 ) >= 0 ) {
  201. vbc.subVectors( c, b );
  202. w = ( d4 - d3 ) / ( ( d4 - d3 ) + ( d5 - d6 ) );
  203. // edge region of BC; barycentric coords (0, 1-w, w)
  204. return target.copy( b ).addScaledVector( vbc, w ); // edge region of BC
  205. }
  206. // face region
  207. var denom = 1 / ( va + vb + vc );
  208. // u = va * denom
  209. v = vb * denom;
  210. w = vc * denom;
  211. return target.copy( a ).addScaledVector( vab, v ).addScaledVector( vac, w );
  212. };
  213. }(),
  214. equals: function ( triangle ) {
  215. return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );
  216. }
  217. } );
  218. export { Triangle };
粤ICP备19079148号